Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1602424

Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and to_kthread()

From Oleg Nesterov <oleg@redhat.com>
Newsgroups linux.kernel
Subject Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and to_kthread()
Date 2017-03-16 16:00 +0100
Message-ID <tlF2H-8g9-19@gated-at.bofh.it> (permalink)
References <tlqmZ-6ih-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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.

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 1/2] kthread: add barriers to set_kthread_struct() and  to_kthread() Tejun Heo <tj@kernel.org> - 2017-03-16 00:20 +0100
  Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and         to_kthread() Oleg Nesterov <oleg@redhat.com> - 2017-03-16 16:00 +0100
    Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and  to_kthread() Tejun Heo <tj@kernel.org> - 2017-03-16 16:40 +0100
      Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and         to_kthread() Oleg Nesterov <oleg@redhat.com> - 2017-03-16 16:50 +0100
      Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and  to_kthread() Peter Zijlstra <peterz@infradead.org> - 2017-03-16 17:00 +0100
        Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and  to_kthread() Peter Zijlstra <peterz@infradead.org> - 2017-03-16 17:20 +0100
        Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and  to_kthread() Tejun Heo <tj@kernel.org> - 2017-03-16 17:20 +0100
    Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and  to_kthread() Tejun Heo <tj@kernel.org> - 2017-03-16 16:40 +0100

csiph-web