From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752582AbbIRNh1 (ORCPT ); Fri, 18 Sep 2015 09:37:27 -0400 Received: from mail-yk0-f171.google.com ([209.85.160.171]:35410 "EHLO mail-yk0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751068AbbIRNhZ (ORCPT ); Fri, 18 Sep 2015 09:37:25 -0400 Date: Fri, 18 Sep 2015 09:37:19 -0400 From: Tejun Heo To: Herbert Xu Cc: Cong Wang , David Miller , Tom Herbert , kafai@fb.com, kernel-team , "linux-kernel@vger.kernel.org" , netdev , Linus Torvalds , Jiri Pirko , Nicolas Dichtel , Thomas Graf , Scott Feldman Subject: Re: [PATCH v3] netlink: Fix autobind race condition that leads to zero port ID Message-ID: <20150918133719.GB10877@mtj.duckdns.org> References: <20150917022909.GA22754@htj.duckdns.org> <20150917030845.GA19162@gondor.apana.org.au> <20150917034134.GA19327@gondor.apana.org.au> <20150917051503.GA19978@gondor.apana.org.au> <20150917113034.GA14471@htj.duckdns.org> <20150918063609.GA31747@gondor.apana.org.au> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20150918063609.GA31747@gondor.apana.org.au> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello, Herbert. On Fri, Sep 18, 2015 at 02:36:10PM +0800, Herbert Xu wrote: > On Thu, Sep 17, 2015 at 07:30:34AM -0400, Tejun Heo wrote: > > > > Maybe add that this led to a deadlock and add a Link tag to this > > thread? > > I'll add a note about the deadlock but I don't like Link tags > because websites die and you can always just google the patch > subject. That's why we use http://lkml.kernel.org/r/MSG_ID links. > > > + nlk_sk(sk)->bound = !!portid; > > > > !! isn't necessasry and this creates ordering between two stores. > > !! was necessary because we're going from a u32 to a bool. bool casting actually collapses the source value to a boolean value. No need for casting regardless of data type. > @@ -1076,17 +1076,19 @@ static int netlink_insert(struct sock *sk, u32 portid) > unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX)) > goto err; > > - nlk_sk(sk)->portid = portid; > + nlk_sk(sk)->rhash_portid = portid; > sock_hold(sk); > > err = __netlink_insert(table, sk); > if (err) { > if (err == -EEXIST) > err = -EADDRINUSE; > - nlk_sk(sk)->portid = 0; > sock_put(sk); > + goto err; > } > > + nlk_sk(sk)->portid = portid; So, this doesn't necessarily make the ordering problem go away. The hash lookup would be fine but imagine a code path like the following. rcu_read_lock(); sock = rhash lookup(some port number); do some operation which may use sock->portid; rcu_read_unlock(); Now, that some operation may see 0 as the port number. I don't think you can avoid doing some type of memory barrier operations if you wanna gate autobind w/o grabbing locks. Thanks. -- tejun