From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752743AbdGFSqH (ORCPT ); Thu, 6 Jul 2017 14:46:07 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:34802 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752582AbdGFSqE (ORCPT ); Thu, 6 Jul 2017 14:46:04 -0400 Subject: Re: [PATCH v2 1/9] net/netfilter/nf_conntrack_core: Fix net_conntrack_lock() To: "Paul E. McKenney" , linux-kernel@vger.kernel.org Cc: netfilter-devel@vger.kernel.org, netdev@vger.kernel.org, oleg@redhat.com, akpm@linux-foundation.org, mingo@redhat.com, dave@stgolabs.net, tj@kernel.org, arnd@arndb.de, linux-arch@vger.kernel.org, will.deacon@arm.com, peterz@infradead.org, stern@rowland.harvard.edu, parri.andrea@gmail.com, torvalds@linux-foundation.org, stable@vger.kernel.org, Sasha Levin , Pablo Neira Ayuso References: <20170705232955.GA15992@linux.vnet.ibm.com> <1499297503-23852-1-git-send-email-paulmck@linux.vnet.ibm.com> From: Manfred Spraul Message-ID: <113516eb-8615-4468-0127-1a491d34c83c@colorfullife.com> Date: Thu, 6 Jul 2017 20:45:59 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 In-Reply-To: <1499297503-23852-1-git-send-email-paulmck@linux.vnet.ibm.com> Content-Type: multipart/mixed; boundary="------------8805E1252AD032F77ACB3F79" Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a multi-part message in MIME format. --------------8805E1252AD032F77ACB3F79 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi Paul, On 07/06/2017 01:31 AM, Paul E. McKenney wrote: > From: Manfred Spraul > > As we want to remove spin_unlock_wait() and replace it with explicit > spin_lock()/spin_unlock() calls, we can use this to simplify the > locking. > > In addition: > - Reading nf_conntrack_locks_all needs ACQUIRE memory ordering. > - The new code avoids the backwards loop. > > Only slightly tested, I did not manage to trigger calls to > nf_conntrack_all_lock(). If you want: Attached would be V2, with adapted comments. -- Manfred --------------8805E1252AD032F77ACB3F79 Content-Type: text/x-patch; name="0001-net-netfilter-nf_conntrack_core-Fix-net_conntrack_lo.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-net-netfilter-nf_conntrack_core-Fix-net_conntrack_lo.pa"; filename*1="tch" >>From e3562faa1bc96e883108505e05deecaf38c87a26 Mon Sep 17 00:00:00 2001 From: Manfred Spraul Date: Sun, 21 Aug 2016 07:17:55 +0200 Subject: [PATCH 1/2] net/netfilter/nf_conntrack_core: Fix net_conntrack_lock() As we want to remove spin_unlock_wait() and replace it with explicit spin_lock()/spin_unlock() calls, we can use this to simplify the locking. In addition: - Reading nf_conntrack_locks_all needs ACQUIRE memory ordering. - The new code avoids the backwards loop. Only slightly tested, I did not manage to trigger calls to nf_conntrack_all_lock(). V2: With improved comments, to clearly show how the barriers pair. Fixes: b16c29191dc8 Signed-off-by: Manfred Spraul Cc: Cc: Alan Stern Cc: Sasha Levin Cc: Pablo Neira Ayuso Cc: netfilter-devel@vger.kernel.org --- net/netfilter/nf_conntrack_core.c | 52 ++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/net/netfilter/nf_conntrack_core.c b/net/netfilter/nf_conntrack_core.c index 9979f46..51390fe 100644 --- a/net/netfilter/nf_conntrack_core.c +++ b/net/netfilter/nf_conntrack_core.c @@ -96,19 +96,26 @@ static struct conntrack_gc_work conntrack_gc_work; void nf_conntrack_lock(spinlock_t *lock) __acquires(lock) { + /* 1) Acquire the lock */ spin_lock(lock); - while (unlikely(nf_conntrack_locks_all)) { - spin_unlock(lock); - /* - * Order the 'nf_conntrack_locks_all' load vs. the - * spin_unlock_wait() loads below, to ensure - * that 'nf_conntrack_locks_all_lock' is indeed held: - */ - smp_rmb(); /* spin_lock(&nf_conntrack_locks_all_lock) */ - spin_unlock_wait(&nf_conntrack_locks_all_lock); - spin_lock(lock); - } + /* 2) read nf_conntrack_locks_all, with ACQUIRE semantics + * It pairs with the smp_store_release() in nf_conntrack_all_unlock() + */ + if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false)) + return; + + /* fast path failed, unlock */ + spin_unlock(lock); + + /* Slow path 1) get global lock */ + spin_lock(&nf_conntrack_locks_all_lock); + + /* Slow path 2) get the lock we want */ + spin_lock(lock); + + /* Slow path 3) release the global lock */ + spin_unlock(&nf_conntrack_locks_all_lock); } EXPORT_SYMBOL_GPL(nf_conntrack_lock); @@ -149,28 +156,27 @@ static void nf_conntrack_all_lock(void) int i; spin_lock(&nf_conntrack_locks_all_lock); - nf_conntrack_locks_all = true; - /* - * Order the above store of 'nf_conntrack_locks_all' against - * the spin_unlock_wait() loads below, such that if - * nf_conntrack_lock() observes 'nf_conntrack_locks_all' - * we must observe nf_conntrack_locks[] held: - */ - smp_mb(); /* spin_lock(&nf_conntrack_locks_all_lock) */ + nf_conntrack_locks_all = true; for (i = 0; i < CONNTRACK_LOCKS; i++) { - spin_unlock_wait(&nf_conntrack_locks[i]); + spin_lock(&nf_conntrack_locks[i]); + + /* This spin_unlock provides the "release" to ensure that + * nf_conntrack_locks_all==true is visible to everyone that + * acquired spin_lock(&nf_conntrack_locks[]). + */ + spin_unlock(&nf_conntrack_locks[i]); } } static void nf_conntrack_all_unlock(void) { - /* - * All prior stores must be complete before we clear + /* All prior stores must be complete before we clear * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock() * might observe the false value but not the entire - * critical section: + * critical section. + * It pairs with the smp_load_acquire() in nf_conntrack_lock() */ smp_store_release(&nf_conntrack_locks_all, false); spin_unlock(&nf_conntrack_locks_all_lock); -- 2.9.4 --------------8805E1252AD032F77ACB3F79-- From mboxrd@z Thu Jan 1 00:00:00 1970 From: Manfred Spraul Subject: Re: [PATCH v2 1/9] net/netfilter/nf_conntrack_core: Fix net_conntrack_lock() Date: Thu, 6 Jul 2017 20:45:59 +0200 Message-ID: <113516eb-8615-4468-0127-1a491d34c83c@colorfullife.com> References: <20170705232955.GA15992@linux.vnet.ibm.com> <1499297503-23852-1-git-send-email-paulmck@linux.vnet.ibm.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------8805E1252AD032F77ACB3F79" Return-path: In-Reply-To: <1499297503-23852-1-git-send-email-paulmck@linux.vnet.ibm.com> Content-Language: en-US Sender: stable-owner@vger.kernel.org To: "Paul E. McKenney" , linux-kernel@vger.kernel.org Cc: netfilter-devel@vger.kernel.org, netdev@vger.kernel.org, oleg@redhat.com, akpm@linux-foundation.org, mingo@redhat.com, dave@stgolabs.net, tj@kernel.org, arnd@arndb.de, linux-arch@vger.kernel.org, will.deacon@arm.com, peterz@infradead.org, stern@rowland.harvard.edu, parri.andrea@gmail.com, torvalds@linux-foundation.org, stable@vger.kernel.org, Sasha Levin , Pablo Neira Ayuso List-Id: linux-arch.vger.kernel.org This is a multi-part message in MIME format. --------------8805E1252AD032F77ACB3F79 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi Paul, On 07/06/2017 01:31 AM, Paul E. McKenney wrote: > From: Manfred Spraul > > As we want to remove spin_unlock_wait() and replace it with explicit > spin_lock()/spin_unlock() calls, we can use this to simplify the > locking. > > In addition: > - Reading nf_conntrack_locks_all needs ACQUIRE memory ordering. > - The new code avoids the backwards loop. > > Only slightly tested, I did not manage to trigger calls to > nf_conntrack_all_lock(). If you want: Attached would be V2, with adapted comments. -- Manfred --------------8805E1252AD032F77ACB3F79 Content-Type: text/x-patch; name="0001-net-netfilter-nf_conntrack_core-Fix-net_conntrack_lo.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-net-netfilter-nf_conntrack_core-Fix-net_conntrack_lo.pa"; filename*1="tch" >From e3562faa1bc96e883108505e05deecaf38c87a26 Mon Sep 17 00:00:00 2001 From: Manfred Spraul Date: Sun, 21 Aug 2016 07:17:55 +0200 Subject: [PATCH 1/2] net/netfilter/nf_conntrack_core: Fix net_conntrack_lock() As we want to remove spin_unlock_wait() and replace it with explicit spin_lock()/spin_unlock() calls, we can use this to simplify the locking. In addition: - Reading nf_conntrack_locks_all needs ACQUIRE memory ordering. - The new code avoids the backwards loop. Only slightly tested, I did not manage to trigger calls to nf_conntrack_all_lock(). V2: With improved comments, to clearly show how the barriers pair. Fixes: b16c29191dc8 Signed-off-by: Manfred Spraul Cc: Cc: Alan Stern Cc: Sasha Levin Cc: Pablo Neira Ayuso Cc: netfilter-devel@vger.kernel.org --- net/netfilter/nf_conntrack_core.c | 52 ++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/net/netfilter/nf_conntrack_core.c b/net/netfilter/nf_conntrack_core.c index 9979f46..51390fe 100644 --- a/net/netfilter/nf_conntrack_core.c +++ b/net/netfilter/nf_conntrack_core.c @@ -96,19 +96,26 @@ static struct conntrack_gc_work conntrack_gc_work; void nf_conntrack_lock(spinlock_t *lock) __acquires(lock) { + /* 1) Acquire the lock */ spin_lock(lock); - while (unlikely(nf_conntrack_locks_all)) { - spin_unlock(lock); - /* - * Order the 'nf_conntrack_locks_all' load vs. the - * spin_unlock_wait() loads below, to ensure - * that 'nf_conntrack_locks_all_lock' is indeed held: - */ - smp_rmb(); /* spin_lock(&nf_conntrack_locks_all_lock) */ - spin_unlock_wait(&nf_conntrack_locks_all_lock); - spin_lock(lock); - } + /* 2) read nf_conntrack_locks_all, with ACQUIRE semantics + * It pairs with the smp_store_release() in nf_conntrack_all_unlock() + */ + if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false)) + return; + + /* fast path failed, unlock */ + spin_unlock(lock); + + /* Slow path 1) get global lock */ + spin_lock(&nf_conntrack_locks_all_lock); + + /* Slow path 2) get the lock we want */ + spin_lock(lock); + + /* Slow path 3) release the global lock */ + spin_unlock(&nf_conntrack_locks_all_lock); } EXPORT_SYMBOL_GPL(nf_conntrack_lock); @@ -149,28 +156,27 @@ static void nf_conntrack_all_lock(void) int i; spin_lock(&nf_conntrack_locks_all_lock); - nf_conntrack_locks_all = true; - /* - * Order the above store of 'nf_conntrack_locks_all' against - * the spin_unlock_wait() loads below, such that if - * nf_conntrack_lock() observes 'nf_conntrack_locks_all' - * we must observe nf_conntrack_locks[] held: - */ - smp_mb(); /* spin_lock(&nf_conntrack_locks_all_lock) */ + nf_conntrack_locks_all = true; for (i = 0; i < CONNTRACK_LOCKS; i++) { - spin_unlock_wait(&nf_conntrack_locks[i]); + spin_lock(&nf_conntrack_locks[i]); + + /* This spin_unlock provides the "release" to ensure that + * nf_conntrack_locks_all==true is visible to everyone that + * acquired spin_lock(&nf_conntrack_locks[]). + */ + spin_unlock(&nf_conntrack_locks[i]); } } static void nf_conntrack_all_unlock(void) { - /* - * All prior stores must be complete before we clear + /* All prior stores must be complete before we clear * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock() * might observe the false value but not the entire - * critical section: + * critical section. + * It pairs with the smp_load_acquire() in nf_conntrack_lock() */ smp_store_release(&nf_conntrack_locks_all, false); spin_unlock(&nf_conntrack_locks_all_lock); -- 2.9.4 --------------8805E1252AD032F77ACB3F79-- From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f193.google.com ([209.85.128.193]:35755 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752469AbdGFSqE (ORCPT ); Thu, 6 Jul 2017 14:46:04 -0400 Received: by mail-wr0-f193.google.com with SMTP id z45so2404359wrb.2 for ; Thu, 06 Jul 2017 11:46:03 -0700 (PDT) Subject: Re: [PATCH v2 1/9] net/netfilter/nf_conntrack_core: Fix net_conntrack_lock() References: <20170705232955.GA15992@linux.vnet.ibm.com> <1499297503-23852-1-git-send-email-paulmck@linux.vnet.ibm.com> From: Manfred Spraul Message-ID: <113516eb-8615-4468-0127-1a491d34c83c@colorfullife.com> Date: Thu, 6 Jul 2017 20:45:59 +0200 MIME-Version: 1.0 In-Reply-To: <1499297503-23852-1-git-send-email-paulmck@linux.vnet.ibm.com> Content-Type: multipart/mixed; boundary="------------8805E1252AD032F77ACB3F79" Content-Language: en-US Sender: linux-arch-owner@vger.kernel.org List-ID: To: "Paul E. McKenney" , linux-kernel@vger.kernel.org Cc: netfilter-devel@vger.kernel.org, netdev@vger.kernel.org, oleg@redhat.com, akpm@linux-foundation.org, mingo@redhat.com, dave@stgolabs.net, tj@kernel.org, arnd@arndb.de, linux-arch@vger.kernel.org, will.deacon@arm.com, peterz@infradead.org, stern@rowland.harvard.edu, parri.andrea@gmail.com, torvalds@linux-foundation.org, stable@vger.kernel.org, Sasha Levin , Pablo Neira Ayuso Message-ID: <20170706184559.kqJMrgzjO6IvHTKfMnJyBATTxehWk6bzNzveRN28dkI@z> This is a multi-part message in MIME format. --------------8805E1252AD032F77ACB3F79 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi Paul, On 07/06/2017 01:31 AM, Paul E. McKenney wrote: > From: Manfred Spraul > > As we want to remove spin_unlock_wait() and replace it with explicit > spin_lock()/spin_unlock() calls, we can use this to simplify the > locking. > > In addition: > - Reading nf_conntrack_locks_all needs ACQUIRE memory ordering. > - The new code avoids the backwards loop. > > Only slightly tested, I did not manage to trigger calls to > nf_conntrack_all_lock(). If you want: Attached would be V2, with adapted comments. -- Manfred --------------8805E1252AD032F77ACB3F79 Content-Type: text/x-patch; name="0001-net-netfilter-nf_conntrack_core-Fix-net_conntrack_lo.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-net-netfilter-nf_conntrack_core-Fix-net_conntrack_lo.pa"; filename*1="tch" --------------8805E1252AD032F77ACB3F79--