Skip to content

Commit 4ecdafc

Browse files
oleg-nesterovtorvalds
authored andcommitted
kthread: introduce to_live_kthread()
"k->vfork_done != NULL" with a barrier() after to_kthread(k) in task_get_live_kthread(k) looks unclear, and sub-optimal because we load ->vfork_done twice. All we need is to ensure that we do not return to_kthread(NULL). Add a new trivial helper which loads/checks ->vfork_done once, this also looks more understandable. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Namhyung Kim <namhyung@kernel.org> Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: "Srivatsa S. Bhat" <srivatsa.bhat@linux.vnet.ibm.com> Cc: Tejun Heo <tj@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ec25e24 commit 4ecdafc

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

‎kernel/kthread.c‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,21 @@ enum KTHREAD_BITS {
5252
KTHREAD_IS_PARKED,
5353
};
5454

55-
#define to_kthread(tsk) \
56-
container_of((tsk)->vfork_done, struct kthread, exited)
55+
#define __to_kthread(vfork) \
56+
container_of(vfork, struct kthread, exited)
57+
58+
static inline struct kthread *to_kthread(struct task_struct *k)
59+
{
60+
return __to_kthread(k->vfork_done);
61+
}
62+
63+
static struct kthread *to_live_kthread(struct task_struct *k)
64+
{
65+
struct completion *vfork = ACCESS_ONCE(k->vfork_done);
66+
if (likely(vfork))
67+
return __to_kthread(vfork);
68+
return NULL;
69+
}
5770

5871
/**
5972
* kthread_should_stop - should this kthread return now?
@@ -313,15 +326,8 @@ struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
313326

314327
static struct kthread *task_get_live_kthread(struct task_struct *k)
315328
{
316-
struct kthread *kthread;
317-
318329
get_task_struct(k);
319-
kthread = to_kthread(k);
320-
/* It might have exited */
321-
barrier();
322-
if (k->vfork_done != NULL)
323-
return kthread;
324-
return NULL;
330+
return to_live_kthread(k);
325331
}
326332

327333
static void __kthread_unpark(struct task_struct *k, struct kthread *kthread)

0 commit comments

Comments
 (0)