All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Manfred Spraul <manfred@colorfullife.com>,
	Alan Stern <stern@rowland.harvard.edu>,
	Sasha Levin <sasha.levin@oracle.com>,
	Pablo Neira Ayuso <pablo@netfilter.org>,
	netfilter-devel@vger.kernel.org,
	"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Subject: [PATCH 4.9 61/77] net/netfilter/nf_conntrack_core: Fix net_conntrack_lock()
Date: Sun, 24 Sep 2017 22:32:46 +0200	[thread overview]
Message-ID: <20170924203245.245082015@linuxfoundation.org> (raw)
In-Reply-To: <20170924203242.904856530@linuxfoundation.org>

4.9-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Manfred Spraul <manfred@colorfullife.com>

commit 3ef0c7a730de0bae03d86c19570af764fa3c4445 upstream.

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 ("netfilter: nf_conntrack: use safer way to lock all buckets")
Signed-off-by: Manfred Spraul <manfred@colorfullife.com>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 net/netfilter/nf_conntrack_core.c |   52 +++++++++++++++++++++-----------------
 1 file changed, 29 insertions(+), 23 deletions(-)

--- a/net/netfilter/nf_conntrack_core.c
+++ b/net/netfilter/nf_conntrack_core.c
@@ -95,19 +95,26 @@ static struct conntrack_gc_work conntrac
 
 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);
 
@@ -148,28 +155,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);

  parent reply	other threads:[~2017-09-24 20:39 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-24 20:31 [PATCH 4.9 00/77] 4.9.52-stable review Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 01/77] SUNRPC: Refactor svc_set_num_threads() Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 02/77] NFSv4: Fix callback server shutdown Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 03/77] mm: prevent double decrease of nr_reserved_highatomic Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 04/77] orangefs: Dont clear SGID when inheriting ACLs Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 05/77] IB/{qib, hfi1}: Avoid flow control testing for RDMA write operation Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 06/77] drm/sun4i: Implement drm_driver lastclose to restore fbdev console Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 07/77] IB/addr: Fix setting source address in addr6_resolve() Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 08/77] tty: improve tty_insert_flip_char() fast path Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 09/77] tty: improve tty_insert_flip_char() slow path Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 10/77] tty: fix __tty_insert_flip_char regression Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 11/77] pinctrl/amd: save pin registers over suspend/resume Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 12/77] Input: i8042 - add Gigabyte P57 to the keyboard reset table Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 13/77] MIPS: math-emu: <MAX|MAXA|MIN|MINA>.<D|S>: Fix quiet NaN propagation Greg Kroah-Hartman
2017-09-24 20:31 ` [PATCH 4.9 14/77] MIPS: math-emu: <MAX|MAXA|MIN|MINA>.<D|S>: Fix cases of both inputs zero Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 15/77] MIPS: math-emu: <MAX|MIN>.<D|S>: Fix cases of both inputs negative Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 16/77] MIPS: math-emu: <MAXA|MINA>.<D|S>: Fix cases of input values with opposite signs Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 17/77] MIPS: math-emu: <MAXA|MINA>.<D|S>: Fix cases of both infinite inputs Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 18/77] MIPS: math-emu: MINA.<D|S>: Fix some cases of infinity and zero inputs Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 19/77] MIPS: math-emu: Handle zero accumulator case in MADDF and MSUBF separately Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 20/77] MIPS: math-emu: <MADDF|MSUBF>.<D|S>: Fix NaN propagation Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 21/77] MIPS: math-emu: <MADDF|MSUBF>.<D|S>: Fix some cases of infinite inputs Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 22/77] MIPS: math-emu: <MADDF|MSUBF>.<D|S>: Fix some cases of zero inputs Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 23/77] MIPS: math-emu: <MADDF|MSUBF>.<D|S>: Clean up "maddf_flags" enumeration Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 24/77] MIPS: math-emu: <MADDF|MSUBF>.S: Fix accuracy (32-bit case) Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 25/77] MIPS: math-emu: <MADDF|MSUBF>.D: Fix accuracy (64-bit case) Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 26/77] crypto: ccp - Fix XTS-AES-128 support on v5 CCPs Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 27/77] [PATCH - RESEND] crypto: AF_ALG - remove SGL terminator indicator when chaining Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 28/77] ext4: fix incorrect quotaoff if the quota feature is enabled Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 29/77] ext4: fix quota inconsistency during orphan cleanup for read-only mounts Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 30/77] powerpc: Fix DAR reporting when alignment handler faults Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 31/77] block: Relax a check in blk_start_queue() Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 32/77] md/bitmap: disable bitmap_resize for file-backed bitmaps Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 33/77] skd: Avoid that module unloading triggers a use-after-free Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 34/77] skd: Submit requests to firmware before triggering the doorbell Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 35/77] scsi: zfcp: fix queuecommand for scsi_eh commands when DIX enabled Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 36/77] scsi: zfcp: add handling for FCP_RESID_OVER to the fcp ingress path Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 37/77] scsi: zfcp: fix capping of unsuccessful GPN_FT SAN response trace records Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 38/77] scsi: zfcp: fix passing fsf_req to SCSI trace on TMF to correlate with HBA Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 39/77] scsi: zfcp: fix missing trace records for early returns in TMF eh handlers Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 40/77] scsi: zfcp: fix payload with full FCP_RSP IU in SCSI trace records Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 41/77] scsi: zfcp: trace HBA FSF response by default on dismiss or timedout late response Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 42/77] scsi: zfcp: trace high part of "new" 64 bit SCSI LUN Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 43/77] scsi: megaraid_sas: set minimum value of resetwaittime to be 1 secs Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 44/77] scsi: megaraid_sas: Check valid aen class range to avoid kernel panic Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 45/77] scsi: megaraid_sas: Return pended IOCTLs with cmd_status MFI_STAT_WRONG_STATE in case adapter is dead Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 46/77] scsi: storvsc: fix memory leak on ring buffer busy Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 47/77] scsi: sg: remove save_scat_len Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 48/77] scsi: sg: use standard lists for sg_requests Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 49/77] scsi: sg: off by one in sg_ioctl() Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 50/77] scsi: sg: factor out sg_fill_request_table() Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 51/77] scsi: sg: fixup infoleak when using SG_GET_REQUEST_TABLE Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 52/77] scsi: qla2xxx: Correction to vha->vref_count timeout Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 53/77] scsi: qla2xxx: Fix an integer overflow in sysfs code Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 54/77] ftrace: Fix selftest goto location on error Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 55/77] ftrace: Fix memleak when unregistering dynamic ops when tracing disabled Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 56/77] tracing: Add barrier to trace_printk() buffer nesting modification Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 57/77] tracing: Apply trace_clock changes to instance max buffer Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 58/77] ARC: Re-enable MMU upon Machine Check exception Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 59/77] PCI: shpchp: Enable bridge bus mastering if MSI is enabled Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 60/77] PCI: pciehp: Report power fault only once until we clear it Greg Kroah-Hartman
2017-09-24 20:32 ` Greg Kroah-Hartman [this message]
2017-09-24 20:32 ` [PATCH 4.9 62/77] s390/mm: fix local TLB flushing vs. detach of an mm address space Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 63/77] s390/mm: fix race on mm->context.flush_mm Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 64/77] media: v4l2-compat-ioctl32: Fix timespec conversion Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 65/77] media: uvcvideo: Prevent heap overflow when accessing mapped controls Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 66/77] PM / devfreq: Fix memory leak when fail to register device Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 67/77] bcache: initialize dirty stripes in flash_dev_run() Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 68/77] bcache: Fix leak of bdev reference Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 69/77] bcache: do not subtract sectors_to_gc for bypassed IO Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 70/77] bcache: correct cache_dirty_target in __update_writeback_rate() Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 71/77] bcache: Correct return value for sysfs attach errors Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 72/77] bcache: fix for gc and write-back race Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 73/77] bcache: fix bch_hprint crash and improve output Greg Kroah-Hartman
2017-09-24 20:32 ` [PATCH 4.9 74/77] mac80211: fix VLAN handling with TXQs Greg Kroah-Hartman
2017-09-24 20:33 ` [PATCH 4.9 75/77] mac80211_hwsim: Use proper TX power Greg Kroah-Hartman
2017-09-24 20:33 ` [PATCH 4.9 76/77] mac80211: flush hw_roc_start work before cancelling the ROC Greg Kroah-Hartman
2017-09-24 20:33 ` [PATCH 4.9 77/77] genirq: Make sparse_irq_lock protect what it should protect Greg Kroah-Hartman
2017-09-25  1:05 ` [PATCH 4.9 00/77] 4.9.52-stable review Guenter Roeck
2017-09-25  4:31 ` Tom Gall
2017-09-25  6:22   ` Greg Kroah-Hartman
2017-09-25 23:13 ` Shuah Khan

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=20170924203245.245082015@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=manfred@colorfullife.com \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=sasha.levin@oracle.com \
    --cc=stable@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /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.