From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
stable@vger.kernel.org,
Linus Torvalds <torvalds@linux-foundation.org>,
Steven Rostedt <srostedt@redhat.com>,
Peter Zijlstra <peterz@infradead.org>
Subject: [ 20/29] spinlocks and preemption points need to be at least compiler barriers
Date: Wed, 10 Apr 2013 15:49:38 -0700 [thread overview]
Message-ID: <20130410224806.152999875@linuxfoundation.org> (raw)
In-Reply-To: <20130410224804.061806042@linuxfoundation.org>
3.4-stable review patch. If anyone has any objections, please let me know.
------------------
From: Linus Torvalds <torvalds@linux-foundation.org>
commit 386afc91144b36b42117b0092893f15bc8798a80 upstream.
In UP and non-preempt respectively, the spinlocks and preemption
disable/enable points are stubbed out entirely, because there is no
regular code that can ever hit the kind of concurrency they are meant to
protect against.
However, while there is no regular code that can cause scheduling, we
_do_ end up having some exceptional (literally!) code that can do so,
and that we need to make sure does not ever get moved into the critical
region by the compiler.
In particular, get_user() and put_user() is generally implemented as
inline asm statements (even if the inline asm may then make a call
instruction to call out-of-line), and can obviously cause a page fault
and IO as a result. If that inline asm has been scheduled into the
middle of a preemption-safe (or spinlock-protected) code region, we
obviously lose.
Now, admittedly this is *very* unlikely to actually ever happen, and
we've not seen examples of actual bugs related to this. But partly
exactly because it's so hard to trigger and the resulting bug is so
subtle, we should be extra careful to get this right.
So make sure that even when preemption is disabled, and we don't have to
generate any actual *code* to explicitly tell the system that we are in
a preemption-disabled region, we need to at least tell the compiler not
to move things around the critical region.
This patch grew out of the same discussion that caused commits
79e5f05edcbf ("ARC: Add implicit compiler barrier to raw_local_irq*
functions") and 3e2e0d2c222b ("tile: comment assumption about
__insn_mtspr for <asm/irqflags.h>") to come about.
Note for stable: use discretion when/if applying this. As mentioned,
this bug may never have actually bitten anybody, and gcc may never have
done the required code motion for it to possibly ever trigger in
practice.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Steven Rostedt <srostedt@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
include/linux/preempt.h | 20 +++++++++++++-------
include/linux/spinlock_up.h | 29 ++++++++++++++++++-----------
2 files changed, 31 insertions(+), 18 deletions(-)
--- a/include/linux/preempt.h
+++ b/include/linux/preempt.h
@@ -93,14 +93,20 @@ do { \
#else /* !CONFIG_PREEMPT_COUNT */
-#define preempt_disable() do { } while (0)
-#define sched_preempt_enable_no_resched() do { } while (0)
-#define preempt_enable_no_resched() do { } while (0)
-#define preempt_enable() do { } while (0)
+/*
+ * Even if we don't have any preemption, we need preempt disable/enable
+ * to be barriers, so that we don't have things like get_user/put_user
+ * that can cause faults and scheduling migrate into our preempt-protected
+ * region.
+ */
+#define preempt_disable() barrier()
+#define sched_preempt_enable_no_resched() barrier()
+#define preempt_enable_no_resched() barrier()
+#define preempt_enable() barrier()
-#define preempt_disable_notrace() do { } while (0)
-#define preempt_enable_no_resched_notrace() do { } while (0)
-#define preempt_enable_notrace() do { } while (0)
+#define preempt_disable_notrace() barrier()
+#define preempt_enable_no_resched_notrace() barrier()
+#define preempt_enable_notrace() barrier()
#endif /* CONFIG_PREEMPT_COUNT */
--- a/include/linux/spinlock_up.h
+++ b/include/linux/spinlock_up.h
@@ -16,7 +16,10 @@
* In the debug case, 1 means unlocked, 0 means locked. (the values
* are inverted, to catch initialization bugs)
*
- * No atomicity anywhere, we are on UP.
+ * No atomicity anywhere, we are on UP. However, we still need
+ * the compiler barriers, because we do not want the compiler to
+ * move potentially faulting instructions (notably user accesses)
+ * into the locked sequence, resulting in non-atomic execution.
*/
#ifdef CONFIG_DEBUG_SPINLOCK
@@ -25,6 +28,7 @@
static inline void arch_spin_lock(arch_spinlock_t *lock)
{
lock->slock = 0;
+ barrier();
}
static inline void
@@ -32,6 +36,7 @@ arch_spin_lock_flags(arch_spinlock_t *lo
{
local_irq_save(flags);
lock->slock = 0;
+ barrier();
}
static inline int arch_spin_trylock(arch_spinlock_t *lock)
@@ -39,32 +44,34 @@ static inline int arch_spin_trylock(arch
char oldval = lock->slock;
lock->slock = 0;
+ barrier();
return oldval > 0;
}
static inline void arch_spin_unlock(arch_spinlock_t *lock)
{
+ barrier();
lock->slock = 1;
}
/*
* Read-write spinlocks. No debug version.
*/
-#define arch_read_lock(lock) do { (void)(lock); } while (0)
-#define arch_write_lock(lock) do { (void)(lock); } while (0)
-#define arch_read_trylock(lock) ({ (void)(lock); 1; })
-#define arch_write_trylock(lock) ({ (void)(lock); 1; })
-#define arch_read_unlock(lock) do { (void)(lock); } while (0)
-#define arch_write_unlock(lock) do { (void)(lock); } while (0)
+#define arch_read_lock(lock) do { barrier(); (void)(lock); } while (0)
+#define arch_write_lock(lock) do { barrier(); (void)(lock); } while (0)
+#define arch_read_trylock(lock) ({ barrier(); (void)(lock); 1; })
+#define arch_write_trylock(lock) ({ barrier(); (void)(lock); 1; })
+#define arch_read_unlock(lock) do { barrier(); (void)(lock); } while (0)
+#define arch_write_unlock(lock) do { barrier(); (void)(lock); } while (0)
#else /* DEBUG_SPINLOCK */
#define arch_spin_is_locked(lock) ((void)(lock), 0)
/* for sched.c and kernel_lock.c: */
-# define arch_spin_lock(lock) do { (void)(lock); } while (0)
-# define arch_spin_lock_flags(lock, flags) do { (void)(lock); } while (0)
-# define arch_spin_unlock(lock) do { (void)(lock); } while (0)
-# define arch_spin_trylock(lock) ({ (void)(lock); 1; })
+# define arch_spin_lock(lock) do { barrier(); (void)(lock); } while (0)
+# define arch_spin_lock_flags(lock, flags) do { barrier(); (void)(lock); } while (0)
+# define arch_spin_unlock(lock) do { barrier(); (void)(lock); } while (0)
+# define arch_spin_trylock(lock) ({ barrier(); (void)(lock); 1; })
#endif /* DEBUG_SPINLOCK */
#define arch_spin_is_contended(lock) (((void)(lock), 0))
next prev parent reply other threads:[~2013-04-10 23:00 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-04-10 22:49 [ 00/29] 3.4.40-stable review Greg Kroah-Hartman
2013-04-10 22:49 ` [ 01/29] ASoC: dma-sh7760: Fix compile error Greg Kroah-Hartman
2013-04-10 22:49 ` [ 02/29] regmap: cache Fix regcache-rbtree sync Greg Kroah-Hartman
2013-04-10 22:49 ` [ 03/29] spi/s3c64xx: modified error interrupt handling and init Greg Kroah-Hartman
2013-04-10 22:49 ` [ 04/29] spi/mpc512x-psc: optionally keep PSC SS asserted across xfer segmensts Greg Kroah-Hartman
2013-04-10 22:49 ` [ 05/29] UBIFS: make space fixup work in the remount case Greg Kroah-Hartman
2013-04-10 22:49 ` [ 06/29] reiserfs: Fix warning and inode leak when deleting inode with xattrs Greg Kroah-Hartman
2013-04-10 22:49 ` [ 07/29] ALSA: hda - bug fix on return value when getting HDMI ELD info Greg Kroah-Hartman
2013-04-10 22:49 ` [ 08/29] ALSA: hda - Enabling Realtek ALC 671 codec Greg Kroah-Hartman
2013-04-10 22:49 ` [ 09/29] ALSA: hda - fix typo in proc output Greg Kroah-Hartman
2013-04-10 22:49 ` [ 10/29] EISA/PCI: Init EISA early, before PNP Greg Kroah-Hartman
2013-04-10 22:49 ` [ 11/29] EISA/PCI: Fix bus res reference Greg Kroah-Hartman
2013-04-10 22:49 ` [ 12/29] libata: Use integer return value for atapi_command_packet_set Greg Kroah-Hartman
2013-04-10 22:49 ` [ 13/29] libata: Set max sector to 65535 for Slimtype DVD A DS8A8SH drive Greg Kroah-Hartman
2013-04-10 22:49 ` [ 14/29] alpha: Add irongate_io to PCI bus resources Greg Kroah-Hartman
2013-04-10 22:49 ` [ 15/29] ata_piix: Fix DVD not dectected at some Haswell platforms Greg Kroah-Hartman
2013-04-10 22:49 ` [ 16/29] ftrace: Consistently restore trace function on sysctl enabling Greg Kroah-Hartman
2013-04-10 22:49 ` [ 17/29] powerpc: pSeries_lpar_hpte_remove fails from Adjunct partition being performed before the ANDCOND test Greg Kroah-Hartman
2013-04-10 22:49 ` [ 18/29] x86: remove the x32 syscall bitmask from syscall_get_nr() Greg Kroah-Hartman
2013-04-10 22:49 ` [ 19/29] hwspinlock: fix __hwspin_lock_request error path Greg Kroah-Hartman
2013-04-10 22:49 ` Greg Kroah-Hartman [this message]
2013-04-10 22:49 ` [ 21/29] crypto: gcm - fix assumption that assoc has one segment Greg Kroah-Hartman
2013-04-10 22:49 ` [ 22/29] block: avoid using uninitialized value in from queue_var_store Greg Kroah-Hartman
2013-04-10 22:49 ` [ 23/29] x86: Fix rebuild with EFI_STUB enabled Greg Kroah-Hartman
2013-04-10 22:49 ` [ 24/29] thermal: return an error on failure to register thermal class Greg Kroah-Hartman
2013-04-10 22:49 ` [ 25/29] panic: fix a possible deadlock in panic() Greg Kroah-Hartman
2013-04-10 22:49 ` [ 26/29] mm: prevent mmap_cache race in find_vma() Greg Kroah-Hartman
2013-04-10 22:49 ` [ 27/29] Revert "mwifiex: cancel cmd timer and free curr_cmd in shutdown process Greg Kroah-Hartman
2013-04-10 22:49 ` [ 28/29] can: gw: use kmem_cache_free() instead of kfree() Greg Kroah-Hartman
2013-04-10 22:49 ` [ 29/29] rt2x00: rt2x00pci_regbusy_read() - only print register access failure once Greg Kroah-Hartman
2013-04-11 16:29 ` [ 00/29] 3.4.40-stable review 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=20130410224806.152999875@linuxfoundation.org \
--to=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=srostedt@redhat.com \
--cc=stable@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).