Skip to content

Commit fb6a44f

Browse files
Waiman Longingomolnar
authored andcommitted
locking/rwsem: Protect all writes to owner by WRITE_ONCE()
Without using WRITE_ONCE(), the compiler can potentially break a write into multiple smaller ones (store tearing). So a read from the same data by another task concurrently may return a partial result. This can result in a kernel crash if the data is a memory address that is being dereferenced. This patch changes all write to rwsem->owner to use WRITE_ONCE() to make sure that store tearing will not happen. READ_ONCE() may not be needed for rwsem->owner as long as the value is only used for comparison and not dereferencing. Signed-off-by: Waiman Long <Waiman.Long@hpe.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Dave Chinner <david@fromorbit.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Douglas Hatch <doug.hatch@hpe.com> Cc: Jason Low <jason.low2@hp.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Hurley <peter@hurleysoftware.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Scott J Norton <scott.norton@hpe.com> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/1463534783-38814-3-git-send-email-Waiman.Long@hpe.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 19c5d69 commit fb6a44f

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

‎kernel/locking/rwsem.h‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616
#define RWSEM_READER_OWNED ((struct task_struct *)1UL)
1717

1818
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
19+
/*
20+
* All writes to owner are protected by WRITE_ONCE() to make sure that
21+
* store tearing can't happen as optimistic spinners may read and use
22+
* the owner value concurrently without lock. Read from owner, however,
23+
* may not need READ_ONCE() as long as the pointer value is only used
24+
* for comparison and isn't being dereferenced.
25+
*/
1926
static inline void rwsem_set_owner(struct rw_semaphore *sem)
2027
{
21-
sem->owner = current;
28+
WRITE_ONCE(sem->owner, current);
2229
}
2330

2431
static inline void rwsem_clear_owner(struct rw_semaphore *sem)
2532
{
26-
sem->owner = NULL;
33+
WRITE_ONCE(sem->owner, NULL);
2734
}
2835

2936
static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
@@ -34,7 +41,7 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
3441
* to minimize cacheline contention.
3542
*/
3643
if (sem->owner != RWSEM_READER_OWNED)
37-
sem->owner = RWSEM_READER_OWNED;
44+
WRITE_ONCE(sem->owner, RWSEM_READER_OWNED);
3845
}
3946

4047
static inline bool rwsem_owner_is_writer(struct task_struct *owner)

0 commit comments

Comments
 (0)