ipc/sem: prevent queue.status tearing in semop
PATCH 1/1 ipc/sem: prevent queue.status tearing in semop
In order for load/store tearing to work, _all_ accesses to the variable in question need to be done around READ and WRITE_ONCE() macros. Ensure everyone does so for q->status variable for semtimedop().
--- ipc/sem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
ipc/sem.c +1 -1
@@ -2125,7 +2125,7 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops, } do { - queue.status = -EINTR; + WRITE_ONCE(queue.status, -EINTR); queue.sleeper = current; __set_current_state(TASK_INTERRUPTIBLE);
Signature · 1 line
2.16.4
Quoted 1 line · Bueso
In order for load/store tearing to work, _all_ accesses to
^ prevention
Hello Davidlohr,
Quoted 4 lines · Davidlohr Bueso
In order for load/store tearing to work, _all_ accesses to the variable in question need to be done around READ and
WRITE_ONCE()macros. Ensure everyone does so forq->statusvariable forsemtimedop().
What is the background of the above rule?
sma->use_global_lock is sometimes used with smp_load_acquire(), sometimes without. So far, I assumed that this is safe.
The same applies for nf_conntrack_locks_all, in nf_conntrack_all_lock()
Quoted 18 lines
Signed-off-by Davidlohr Bueso <[email protected]>--- ipc/sem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)ipc/sem.c +1
-1@@ -2125,7 +2125,7 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops, } do { - queue.status = -EINTR; + WRITE_ONCE(queue.status, -EINTR); queue.sleeper = current; __set_current_state(TASK_INTERRUPTIBLE);
Quoted 8 lines · Manfred Spraul
Hello Davidlohr,
Quoted 4 lines · Davidlohr Bueso
In order for load/store tearing to work, _all_ accesses to the variable in question need to be done around READ and
WRITE_ONCE()macros. Ensure everyone does so forq->statusvariable forsemtimedop().What is the background of the above rule?
The fact that it's done under the ipc lock, doesn't mean that the compiler won't try to get smart. If we have lockless accesses we musn't see partial stores/loads.
Quoted 5 lines
sma->use_global_lockis sometimes used withsmp_load_acquire(), sometimes without. So far, I assumed that this is safe.The same applies for
nf_conntrack_locks_all, innf_conntrack_all_lock()
Oh, yeah I remember _that_ saga. I'll have a look but iirc it seemd ok back then.
Thanks,
Davidlohr
Quoted 4 lines · Manfred Spraul
sma->use_global_lockis sometimes used withsmp_load_acquire(), sometimes without. So far, I assumed that this is safe.The same applies for
nf_conntrack_locks_all, innf_conntrack_all_lock()
So the netfilter code is safe wrt tearing as _all_ accesses are done with barriers and/or under spinlock.
However, this isn't always the case for sma->use_global_lock, albeit harmless.
- sem_lock(): It doesn't matter if we get the first check right as we end up rechecking with locks held.
/*
* Initial check for use_global_lock. Just an optimization,
* no locking, no memory barrier.
*/
if (!sma->use_global_lock) {- complexmode_enter/tryleave() are called under the ipc object lock, so that is safe:
spin_lock()complexmode_enter()
...complexmode_tryleave()spin_unlock()
- newary(): Init, no concurrency, of course.
So while I also like READ/WRITE_ONCE() calls in that it helps document the code, I don't think we need/want want this. There's a comment there in the first place.
Thanks,
Davidlohr