All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@redhat.com>
To: Tejun Heo <tj@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>, Chris Mason <clm@fb.com>,
	linux-kernel@vger.kernel.org, kernel-team@fb.com
Subject: Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and to_kthread()
Date: Thu, 16 Mar 2017 15:54:36 +0100	[thread overview]
Message-ID: <20170316145436.GA24478@redhat.com> (raw)
In-Reply-To: <20170315231827.GA13656@htj.duckdns.org>

Hi Tejun,

On 03/15, Tejun Heo wrote:
>
> Until now, all to_kthread() users are interlocked with kthread
> creation and there's no need to have explicit barriers when setting
> the kthread pointer or dereferencing it.
>
> However, There is a race condition where userland can interfere with a
> kthread while it's being initialized.  To close it, to_kthread() needs
> to be used from an unsynchronized context.

So this is preparation for 2/2... IIUC, the current code is not buggy,
just you need to add kthread_initialized() which can't work without
this change.

>  static inline void set_kthread_struct(void *kthread)
>  {
> +	/* paired with smp_read_data_barrier_depends() in to_kthread() */
> +	smp_wmb();
> +
>  	/*
>  	 * We abuse ->set_child_tid to avoid the new member and because it
>  	 * can't be wrongly copied by copy_process(). We also rely on fact
> @@ -67,8 +70,19 @@ static inline void set_kthread_struct(vo
>
>  static inline struct kthread *to_kthread(struct task_struct *k)
>  {
> +	void *ptr;
> +
>  	WARN_ON(!(k->flags & PF_KTHREAD));
> -	return (__force void *)k->set_child_tid;
> +
> +	ptr = (__force void *)k->set_child_tid;
> +
> +	/*
> +	 * Paired with smp_wmb() in set_kthread_struct() and ensures that
> +	 * the caller sees initialized content of the returned kthread.
> +	 */
> +	smp_read_barrier_depends();
> +
> +	return ptr;

This is almost off-topic, but I think lockless_dereference() will look
better in to_kthread().

And perhaps we should add another helper, say,

	#define lockless_assign_pointer(ptr, val)	\
		smp_store_release(&ptr, val)

for set_kthread_struct() ? it can have more users.

Not that I think you should change your patch, I am just asking.

Oleg.

  parent reply	other threads:[~2017-03-16 14:58 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-15 23:18 [PATCH 1/2] kthread: add barriers to set_kthread_struct() and to_kthread() Tejun Heo
2017-03-15 23:19 ` [PATCH 2/2] kthread, cgroup: close race window where new kthreads can be migrated to non-root cgroups Tejun Heo
2017-03-16 15:02   ` Oleg Nesterov
     [not found]     ` <20170316150233.GB24478-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2017-03-16 15:39       ` Oleg Nesterov
2017-03-16 15:39         ` Oleg Nesterov
     [not found]         ` <20170316153925.GA26391-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2017-03-16 16:07           ` Tejun Heo
2017-03-16 16:07             ` Tejun Heo
     [not found]             ` <20170316160734.GD15810-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2017-03-16 16:31               ` Oleg Nesterov
2017-03-16 16:31                 ` Oleg Nesterov
2017-03-16 17:41                 ` Tejun Heo
2017-03-16 16:05     ` Tejun Heo
     [not found]       ` <20170316160544.GC15810-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2017-03-16 16:17         ` Oleg Nesterov
2017-03-16 16:17           ` Oleg Nesterov
2017-03-16 17:03           ` Tejun Heo
     [not found]   ` <20170315231920.GB13656-piEFEHQLUPpN0TnZuCh8vA@public.gmane.org>
2017-03-16 20:54     ` [PATCH v2] cgroup, kthread: " Tejun Heo
2017-03-16 20:54       ` Tejun Heo
2017-03-17 13:50       ` Oleg Nesterov
2017-03-17 14:44         ` Tejun Heo
2017-03-16 14:54 ` Oleg Nesterov [this message]
2017-03-16 15:33   ` [PATCH 1/2] kthread: add barriers to set_kthread_struct() and to_kthread() Tejun Heo
2017-03-16 15:38     ` Tejun Heo
2017-03-16 15:46       ` Oleg Nesterov
2017-03-16 15:55       ` Peter Zijlstra
2017-03-16 16:09         ` Tejun Heo
2017-03-16 16:14           ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170316145436.GA24478@redhat.com \
    --to=oleg@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=clm@fb.com \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.