From: Davidlohr Bueso <davidlohr.bueso@hp.com> 

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

diff --git a/ipc/sem.c b/ipc/sem.c
index acada9f..d90da47 100644
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -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);
