Skip to contentSkip to conversation

ipc/sem: prevent queue.status tearing in semop

PATCH In review Davidlohr Bueso 2 participants 5 messages Jul 17, 2018 – Jul 30, 2018

lore
Share & tools
lore
#0 lore raw

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
index 6cbbf34a44ac..ccab4e51d351 100644
@@ -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
#1 lore raw
Quoted 1 line · Bueso

In order for load/store tearing to work, _all_ accesses to

^ prevention

#2 lore raw
In reply to Davidlohr Bueso

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 for q->status variable for semtimedop().

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
---
  ipc/sem.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
ipc/sem.c +1 -1
index 6cbbf34a44ac..ccab4e51d351 100644
@@ -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);
#3 lore raw
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 for q->status variable for semtimedop().

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_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()

Oh, yeah I remember _that_ saga. I'll have a look but iirc it seemd ok back then.

Thanks,
Davidlohr

#4 lore raw
In reply to Manfred Spraul
Quoted 4 lines · Manfred Spraul

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()

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