如何正确初始化linux内核中的task_struct

时间:2018-03-30 15:53:34

标签: c linux linux-kernel kernel task

我想在linux内核4.14的struct task_struct中添加自定义uint8_t。

我已使用我的附加字段修改了include/linux/sched.h中的结构:

#ifdef CONFIG_CGROUPS
    /* disallow userland-initiated cgroup migration */
    unsigned            no_cgroup_migration:1;
#endif

    unsigned long           atomic_flags; /* Flags requiring atomic access. */

    struct restart_block        restart_block;

    pid_t               pid;
    pid_t               tgid;

    /*Custom field*/
    uint8_t my_custom_field;

#ifdef CONFIG_CC_STACKPROTECTOR
    /* Canary value for the -fstack-protector GCC feature: */
    unsigned long           stack_canary;
#endif

并添加了以下初始化代码:

在include / linux / init_task.h中,我做了:

        INIT_NUMA_BALANCING(tsk)                    \
        INIT_KASAN(tsk)                         \
        INIT_LIVEPATCH(tsk)                     \
        INIT_TASK_SECURITY                      \
        .my_custom_field=0                              \
    }

在kernel / fork.c中,我确保再次将字段初始化为0:

#ifdef CONFIG_LOCKDEP
    p->lockdep_depth = 0; /* no locks held yet */
    p->curr_chain_key = 0;
    p->lockdep_recursion = 0;
    lockdep_init_task(p);
#endif

#ifdef CONFIG_DEBUG_MUTEXES
    p->blocked_on = NULL; /* not blocked yet */
#endif
#ifdef CONFIG_BCACHE
    p->sequential_io    = 0;
    p->sequential_io_avg    = 0;
#endif

    p->my_custom_field = 0;
    /* Perform scheduler related setup. Assign this task to a CPU. */
    retval = sched_fork(clone_flags, p);
    if (retval)
        goto bad_fork_cleanup_policy;

    retval = perf_event_init_task(p);

但是,当我稍后在调度程序中检查变量时,我得到了各种值,而没有自己修改字段:

[    2.897772] Custom value is 2
[    2.901038] Custom value is 58
[    2.901524] Custom value is 58
[    2.901549] Custom value is 58
[    2.901581] Custom value is 58
[    2.901604] Custom value is 58
[    2.901634] Custom value is 2
[    2.902328] Custom value is 2
[    2.902672] Custom value is 58
[    2.902864] Custom value is 58
[    2.902998] Custom value is 58
[    2.904071] Custom value is 58
[    2.905519] Custom value is 58
[    2.905995] Custom value is 58
[    2.906466] Custom value is 2
[    2.907082] Custom value is 58
[    2.907677] Custom value is 58

但我需要这个值为0,除非我自己改变它。可能是什么原因造成的?我在初始化中忘记了什么吗?

0 个答案:

没有答案