From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754926AbdCPPdH (ORCPT ); Thu, 16 Mar 2017 11:33:07 -0400 Received: from mail-yw0-f193.google.com ([209.85.161.193]:33368 "EHLO mail-yw0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754906AbdCPPdE (ORCPT ); Thu, 16 Mar 2017 11:33:04 -0400 Date: Thu, 16 Mar 2017 11:33:01 -0400 From: Tejun Heo To: Oleg Nesterov Cc: Linus Torvalds , Andrew Morton , Peter Zijlstra , Thomas Gleixner , Chris Mason , linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: Re: [PATCH 1/2] kthread: add barriers to set_kthread_struct() and to_kthread() Message-ID: <20170316153301.GA15810@htj.duckdns.org> References: <20170315231827.GA13656@htj.duckdns.org> <20170316145436.GA24478@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170316145436.GA24478@redhat.com> User-Agent: Mutt/1.7.1 (2016-10-04) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, Oleg. On Thu, Mar 16, 2017 at 03:54:36PM +0100, Oleg Nesterov wrote: > 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. Yeah, I could have been clearer. > > + /* > > + * 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. Ah yeah, that would look better. I vaguely remembered the new macro but couldn't quite remember it fully. :) Will update the patch. Thanks. -- tejun