ipc/sem.c: use READ_ONCE()/WRITE_ONCE() for use_global_lock
PATCH 1/1 ipc/sem.c: use READ_ONCE()/WRITE_ONCE() for use_global_lock
The patch solves two weaknesses in ipc/sem.c:
1) The initial read of use_global_lock in sem_lock() is an intentional race. KCSAN detects these accesses and prints a warning.
2) The code assumes that plain C read/writes are not mangled by the CPU or the compiler.
To solve both issues, use READ_ONCE()/WRITE_ONCE(). Plain C reads are used in code that owns sma->sem_perm.lock.
--- ipc/sem.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
ipc/sem.c +7 -4
@@ -217,6 +217,8 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it);* this smp_load_acquire(), this is guaranteed because the smp_load_acquire() * is inside a spin_lock() and after a write from 0 to non-zero a * spin_lock()+spin_unlock() is done. +* To prevent the compiler/cpu temporarily writing 0 to use_global_lock, +* READ_ONCE()/WRITE_ONCE() is used. * * 2) queue.status: (SEM_BARRIER_2) * Initialization is done while holding sem_lock(), so no further barrier is
@@ -342,10 +344,10 @@ static void complexmode_enter(struct sem_array *sma)* Nothing to do, just reset the * counter until we return to simple mode. */ - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );return ; } - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );for (i =0 ; i < sma->sem_nsems; i++) { sem = &sma->sems[i];
@@ -371,7 +373,8 @@ static void complexmode_tryleave(struct sem_array *sma)/* See SEM_BARRIER_1 for purpose/pairing */ smp_store_release (&sma->use_global_lock,0 ); }else { - sma->use_global_lock--; +WRITE_ONCE (sma->use_global_lock, + sma->use_global_lock-1 ); } }
@@ -412,7 +415,7 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,* Initial check for use_global_lock. Just an optimization, * no locking, no memory barrier. */ -if (!sma->use_global_lock) { +if (!READ_ONCE (sma->use_global_lock)) {/* * It appears that no complex operation is around. * Acquire the per-semaphore lock.
Signature · 1 line
2.31.1
Quoted 6 lines · Manfred Spraul
The patch solves two weaknesses in
ipc/sem.c:1) The initial read of
use_global_lockinsem_lock()is an intentional race. KCSAN detects these accesses and prints a warning.
Quoted 7 lines
2) The code assumes that plain C read/writes are not mangled by the CPU or the compiler.
To solve both issues, use READ_ONCE()/WRITE_ONCE(). Plain C reads are used in code that owns
sma->sem_perm.lock.Signed-off-by Manfred Spraul <[email protected]>
One follow-up question: If I am reading the code correctly, there is a call to complexmode_enter() from sysvipc_sem_proc_show() that does not hold the global lock. Does this mean that the first check of ->use_global_lock in complexmode_enter() should be marked?
Thanx, Paul
Quoted 52 lines
--- ipc/sem.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)ipc/sem.c +7
-4@@ -217,6 +217,8 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it);* this smp_load_acquire(), this is guaranteed because the smp_load_acquire() * is inside a spin_lock() and after a write from 0 to non-zero a * spin_lock()+spin_unlock() is done. +* To prevent the compiler/cpu temporarily writing 0 to use_global_lock, +* READ_ONCE()/WRITE_ONCE() is used. * * 2) queue.status: (SEM_BARRIER_2) * Initialization is done while holding sem_lock(), so no further barrier is @@ -342,10 +344,10 @@ static void complexmode_enter(struct sem_array *sma)* Nothing to do, just reset the * counter until we return to simple mode. */ - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );return ; } - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );for (i =0 ; i < sma->sem_nsems; i++) { sem = &sma->sems[i];@@ -371,7 +373,8 @@ static void complexmode_tryleave(struct sem_array *sma)/* See SEM_BARRIER_1 for purpose/pairing */ smp_store_release (&sma->use_global_lock,0 ); }else { - sma->use_global_lock--; +WRITE_ONCE (sma->use_global_lock, + sma->use_global_lock-1 ); } }@@ -412,7 +415,7 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,* Initial check for use_global_lock. Just an optimization, * no locking, no memory barrier. */ -if (!sma->use_global_lock) { +if (!READ_ONCE (sma->use_global_lock)) {/* * It appears that no complex operation is around. * Acquire the per-semaphore lock. Signature · 1 line
2.31.1
Hi Paul,
Quoted 13 lines · Paul E. McKenney
Quoted 12 lines · Manfred Spraul
The patch solves two weaknesses in
ipc/sem.c:1) The initial read of
use_global_lockinsem_lock()is an intentional race. KCSAN detects these accesses and prints a warning.2) The code assumes that plain C read/writes are not mangled by the CPU or the compiler.
To solve both issues, use READ_ONCE()/WRITE_ONCE(). Plain C reads are used in code that owns
sma->sem_perm.lock.
Quoted 7 lines
Quoted 1 line
Signed-off-by Manfred Spraul <[email protected]>Reviewed-by Paul E. McKenney <[email protected]>One follow-up question: If I am reading the code correctly, there is a call to
complexmode_enter()fromsysvipc_sem_proc_show()that does not hold the global lock. Does this mean that the first check of ->use_global_lock incomplexmode_enter()should be marked?
Now you made me nervous, usually I do not test the proc interface.
According to the documentation in sysvipc_sem_proc_show(), sysvipc_find_ipc() acquires the global lock.
Quoted 6 lines
/*
* The proc interface isn't aware ofsem_lock(), it calls
*ipc_lock_object()directly (insysvipc_find_ipc).
* In order to stay compatible withsem_lock(), we must
* enter / leavecomplex_mode.
*/
I have just tested it again: Yes, this is still true.
Perhaps, as future improvement: The rest of ipc/sem.c speaks about "sem_perm.lock", and here we suddenly use a function name instead of the structure member name.
Quoted 56 lines
"it calls
ipc_lock_object()(i.e.:spin_lock(&sma->sem_perm.lock)).Thanx, Paul
Quoted 52 lines
--- ipc/sem.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)ipc/sem.c +7
-4@@ -217,6 +217,8 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it);* this smp_load_acquire(), this is guaranteed because the smp_load_acquire() * is inside a spin_lock() and after a write from 0 to non-zero a * spin_lock()+spin_unlock() is done. +* To prevent the compiler/cpu temporarily writing 0 to use_global_lock, +* READ_ONCE()/WRITE_ONCE() is used. * * 2) queue.status: (SEM_BARRIER_2) * Initialization is done while holding sem_lock(), so no further barrier is @@ -342,10 +344,10 @@ static void complexmode_enter(struct sem_array *sma)* Nothing to do, just reset the * counter until we return to simple mode. */ - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );return ; } - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );for (i =0 ; i < sma->sem_nsems; i++) { sem = &sma->sems[i];@@ -371,7 +373,8 @@ static void complexmode_tryleave(struct sem_array *sma)/* See SEM_BARRIER_1 for purpose/pairing */ smp_store_release (&sma->use_global_lock,0 ); }else { - sma->use_global_lock--; +WRITE_ONCE (sma->use_global_lock, + sma->use_global_lock-1 ); } }@@ -412,7 +415,7 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,* Initial check for use_global_lock. Just an optimization, * no locking, no memory barrier. */ -if (!sma->use_global_lock) { +if (!READ_ONCE (sma->use_global_lock)) {/* * It appears that no complex operation is around. * Acquire the per-semaphore lock. Signature · 1 line
2.31.1
Quoted 19 lines · Manfred Spraul
Hi Paul,
Quoted 16 lines · Paul E. McKenney
Quoted 6 lines · Manfred Spraul
The patch solves two weaknesses in
ipc/sem.c:1) The initial read of
use_global_lockinsem_lock()is an intentional race. KCSAN detects these accesses and prints a warning.Quoted 7 lines
2) The code assumes that plain C read/writes are not mangled by the CPU or the compiler.
To solve both issues, use READ_ONCE()/WRITE_ONCE(). Plain C reads are used in code that owns
sma->sem_perm.lock.Signed-off-by Manfred Spraul <[email protected]>Reviewed-by Paul E. McKenney <[email protected]>
Quoted 8 lines
Quoted 4 lines
One follow-up question: If I am reading the code correctly, there is a call to
complexmode_enter()fromsysvipc_sem_proc_show()that does not hold the global lock. Does this mean that the first check of ->use_global_lock incomplexmode_enter()should be marked?Now you made me nervous, usually I do not test the proc interface. According to the documentation in
sysvipc_sem_proc_show(),sysvipc_find_ipc()acquires the global lock.
"It is a service that I provide." ;-)
Quoted 7 lines
Quoted 6 lines
/*
* The proc interface isn't aware ofsem_lock(), it calls
*ipc_lock_object()directly (insysvipc_find_ipc).
* In order to stay compatible withsem_lock(), we must
* enter / leavecomplex_mode.
*/I have just tested it again: Yes, this is still true.
OK, so the sequence of events is as follow?
o sysvipc_proc_start() is invoked to start, as the name implies.
o sysvipc_proc_start() invokes sysvipc_find_ipc(), which
scans the IDs and invokes ipc_lock_object() on the one at pos.
o ipc_lock_object() acquires the corresponding lock, which
seems unlikely to be sem_perm.lock, though I freely admit that I do not know this code very well.
Ah, I see it now. The kernel_ipc_perm that sysvipc_find_ipc is looking at is the first member of the sem_array structure, and that member is named sem_perm.
Quoted 5 lines
Perhaps, as future improvement: The rest of
ipc/sem.cspeaks about "sem_perm.lock", and here we suddenly use a function name instead of the structure member name.Quoted 1 line
"it calls
ipc_lock_object()(i.e.:spin_lock(&sma->sem_perm.lock)).
As usual, it seems obvious once you know the trick. ;-)
Thanx, Paul
Quoted 51 lines
Quoted 50 lines
Quoted 50 lines
--- ipc/sem.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)ipc/sem.c +7
-4@@ -217,6 +217,8 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it);* this smp_load_acquire(), this is guaranteed because the smp_load_acquire() * is inside a spin_lock() and after a write from 0 to non-zero a * spin_lock()+spin_unlock() is done. +* To prevent the compiler/cpu temporarily writing 0 to use_global_lock, +* READ_ONCE()/WRITE_ONCE() is used. * * 2) queue.status: (SEM_BARRIER_2) * Initialization is done while holding sem_lock(), so no further barrier is @@ -342,10 +344,10 @@ static void complexmode_enter(struct sem_array *sma)* Nothing to do, just reset the * counter until we return to simple mode. */ - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );return ; } - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );for (i =0 ; i < sma->sem_nsems; i++) { sem = &sma->sems[i];@@ -371,7 +373,8 @@ static void complexmode_tryleave(struct sem_array *sma)/* See SEM_BARRIER_1 for purpose/pairing */ smp_store_release (&sma->use_global_lock,0 ); }else { - sma->use_global_lock--; +WRITE_ONCE (sma->use_global_lock, + sma->use_global_lock-1 ); } }@@ -412,7 +415,7 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,* Initial check for use_global_lock. Just an optimization, * no locking, no memory barrier. */ -if (!sma->use_global_lock) { +if (!READ_ONCE (sma->use_global_lock)) {/* * It appears that no complex operation is around. * Acquire the per-semaphore lock. Signature · 1 line
2.31.1
Quoted 6 lines · Manfred Spraul
The patch solves two weaknesses in
ipc/sem.c:1) The initial read of
use_global_lockinsem_lock()is an intentional race. KCSAN detects these accesses and prints a warning.
Quoted 6 lines
2) The code assumes that plain C read/writes are not mangled by the CPU or the compiler.
To solve both issues, use READ_ONCE()/WRITE_ONCE(). Plain C reads are used in code that owns
sma->sem_perm.lock.
Quoted 53 lines
Signed-off-by Manfred Spraul <[email protected]>---
ipc/sem.c| 11 +++++++----1 file changed, 7 insertions(+), 4 deletions(-)ipc/sem.c +7
-4@@ -217,6 +217,8 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it);* this smp_load_acquire(), this is guaranteed because the smp_load_acquire() * is inside a spin_lock() and after a write from 0 to non-zero a * spin_lock()+spin_unlock() is done. +* To prevent the compiler/cpu temporarily writing 0 to use_global_lock, +* READ_ONCE()/WRITE_ONCE() is used. * * 2) queue.status: (SEM_BARRIER_2) * Initialization is done while holding sem_lock(), so no further barrier is @@ -342,10 +344,10 @@ static void complexmode_enter(struct sem_array *sma)* Nothing to do, just reset the * counter until we return to simple mode. */ - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );return ; } - sma->use_global_lock =USE_GLOBAL_LOCK_HYSTERESIS ; +WRITE_ONCE (sma->use_global_lock,USE_GLOBAL_LOCK_HYSTERESIS );for (i =0 ; i < sma->sem_nsems; i++) { sem = &sma->sems[i];@@ -371,7 +373,8 @@ static void complexmode_tryleave(struct sem_array *sma)/* See SEM_BARRIER_1 for purpose/pairing */ smp_store_release (&sma->use_global_lock,0 ); }else { - sma->use_global_lock--; +WRITE_ONCE (sma->use_global_lock, + sma->use_global_lock-1 ); } }@@ -412,7 +415,7 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,* Initial check for use_global_lock. Just an optimization, * no locking, no memory barrier. */ -if (!sma->use_global_lock) { +if (!READ_ONCE (sma->use_global_lock)) {/* * It appears that no complex operation is around. * Acquire the per-semaphore lock. Signature · 1 line
2.31.1