是否可以从sched_entity中找到相应的task_struct?

Hao*_*hen 4 linux scheduler linux-kernel

我知道如果我们有task_struct,我们可以得到包含的sched_entity,因为它是任务结构中的一个字段.但是我们可以在给定shed_entity的情况下获得指向task_struct的指针吗?以下是sched_entity结构:

struct sched_entity {
    struct load_weight  load;       /* for load-balancing */
    struct rb_node      run_node;
    struct list_head    group_node;
    unsigned int        on_rq;

    u64         exec_start;
    u64         sum_exec_runtime;
    u64         vruntime;
    u64         prev_sum_exec_runtime;

    u64         nr_migrations;


#ifdef CONFIG_SCHEDSTATS
    struct sched_statistics statistics;
#endif

#ifdef CONFIG_FAIR_GROUP_SCHED
    struct sched_entity *parent;
    /* rq on which this entity is (to be) queued: */
    struct cfs_rq       *cfs_rq;
    /* rq "owned" by this entity/group: */
    struct cfs_rq       *my_q;
#endif
};
Run Code Online (Sandbox Code Playgroud)

似乎没有地方可以获得task_struct.我的最终目标是获取任务group_leader的sched_entity包含具有此shed_entity的任务:>

Mat*_*ery 6

Linux内核代码提供了一种标准方法来获取指向结构中包含的元素的指针,并获取指向包含结构的指针:container_of宏,在整个内核中广泛使用.

在这种情况下,如果你有一个struct sched_entity *foo,你可以得到封闭task_struct:

struct task_struct *task = container_of(foo, struct task_struct, se);
Run Code Online (Sandbox Code Playgroud)

(显然,如果您确定原始struct sched_entity *指针指向的是一个struct sched_entity内部struct task_struct,那么这是安全的,所以要小心......)