Skip to contentSkip to conversation

ipc,sem: fix locking in semctl_main

PATCH In review Rik van Riel 3 participants 3 messages Apr 23, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipc,sem: fix locking in semctl_main

Andrew, here is the patch I promised :)

Everywhere else in the code, we check sma->sem_perm.deleted under the semaphore array lock, so we should do teh same here. The easy fix is to simply not drop and re-take the lock, but keep it locked.

This patch can be folded into ipcsem-fine-grained-locking-for-semtimedop.patch

---
 ipc/sem.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
ipc/sem.c +2 -3
index 5711616..efdaee6 100644
@@ -1243,10 +1243,9 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
 				err = -EIDRM;
 				goto out_free;
 			}
-			sem_unlock(sma, -1);
-		}
+		} else
+			sem_lock(sma, NULL, -1);
 
-		sem_lock(sma, NULL, -1);
 		for (i = 0; i < sma->sem_nsems; i++)
 			sem_io[i] = sma->sem_base[i].semval;
 		sem_unlock(sma, -1);
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Rik van Riel

Andrew, here is the patch I promised :)

Everywhere else in the code, we check sma->sem_perm.deleted under the semaphore array lock, so we should do teh same here. The easy fix is to simply not drop and re-take the lock, but keep it locked.

CCing Linus, Sedat and Emmanuel.

This looks very much like a patch I asked Sedat to test during the weekend, which according to him didn't solve his issue. The patch is correct in any case.

Quoted 3 lines

This patch can be folded into ipcsem-fine-grained-locking-for-semtimedop.patch

Quoted 21 lines
---
 ipc/sem.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
ipc/sem.c +2 -3
index 5711616..efdaee6 100644
@@ -1243,10 +1243,9 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
 				err = -EIDRM;
 				goto out_free;
 			}
-			sem_unlock(sma, -1);
-		}
+		} else
+			sem_lock(sma, NULL, -1);
 
-		sem_lock(sma, NULL, -1);
 		for (i = 0; i < sma->sem_nsems; i++)
 			sem_io[i] = sma->sem_base[i].semval;
 		sem_unlock(sma, -1);
#2 lore raw
Quoted 9 lines · Davidlohr Bueso
Quoted 7 lines · Rik van Riel

Andrew, here is the patch I promised :)

Everywhere else in the code, we check sma->sem_perm.deleted under the semaphore array lock, so we should do teh same here. The easy fix is to simply not drop and re-take the lock, but keep it locked.

Quoted 6 lines

CCing Linus, Sedat and Emmanuel.

This looks very much like a patch I asked Sedat to test during the weekend, which according to him didn't solve his issue. The patch is correct in any case.

Your patch looked a bit different (checked for ipc-lock already taken, both patches - original and refreshed againt -next attached).

- Sedat -

Quoted 23 lines
Quoted 3 lines

This patch can be folded into ipcsem-fine-grained-locking-for-semtimedop.patch

Quoted 17 lines
---
 ipc/sem.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
ipc/sem.c +2 -2
index 5711616..efdaee6 100644
@@ -1243,10 +1243,9 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
                              err = -EIDRM;
                              goto out_free;
                      }
-                     sem_unlock(sma, -1);
-             }
+             } else
+                     sem_lock(sma, NULL, -1);
 
Quoted 6 lines

ipc/sem.c static int semctl_main(struct ipc_namespace *ns, int semid, int semnum, · line 1251

Quoted 4 lines
ipc/sem.c +0 -1
-             sem_lock(sma, NULL, -1);
              for (i = 0; i < sma->sem_nsems; i++)
                      sem_io[i] = sma->sem_base[i].semval;
              sem_unlock(sma, -1);

From: Davidlohr Bueso <[email protected]>

Subject: ipc: sem: Check if the ipc lock has been already taken

Original patch from [1].

[1] http://marc.info/?t=136631457900005&r=1&w=2

ipc/sem.c +3 -2
index 5711616..1dfc3c1 100644
@@ -1243,10 +1243,11 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
 				err = -EIDRM;
 				goto out_free;
 			}
-			sem_unlock(sma, -1);
 		}
 
-		sem_lock(sma, NULL, -1);
+		/* has the ipc lock already been taken? */
+		if(nsems <= SEMMSL_FAST)
+			sem_lock(sma, NULL, -1);
 		for (i = 0; i < sma->sem_nsems; i++)
 			sem_io[i] = sma->sem_base[i].semval;
 		sem_unlock(sma, -1);

From: Davidlohr Bueso <[email protected]>

Subject: ipc, sem: Check if the ipc lock has been already taken

Original patch from [1].

[ dileks: v2: Adapted to fit next-20130419 plus 3 ipc-sem-next patches from [2] ]

[1] http://marc.info/?t=136631457900005&r=1&w=2
[2] http://marc.info/?l=linux-next&m=136640233601913&w=2

ipc/sem.c +3 -2
index acada9f..d90da47 100644
@@ -1259,10 +1259,11 @@ static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
 				err = -EIDRM;
 				goto out_free;
 			}
-			sem_unlock(sma, -1);
 		}
 
-		sem_lock(sma, NULL, -1);
+		/* has the ipc lock already been taken? */
+		if (nsems <= SEMMSL_FAST)
+			sem_lock(sma, NULL, -1);
 		for (i = 0; i < sma->sem_nsems; i++)
 			sem_io[i] = sma->sem_base[i].semval;
 		sem_unlock(sma, -1);