sem.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. /*
  2. * linux/ipc/sem.c
  3. * Copyright (C) 1992 Krishna Balasubramanian
  4. * Copyright (C) 1995 Eric Schenk, Bruno Haible
  5. *
  6. * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
  7. * This code underwent a massive rewrite in order to solve some problems
  8. * with the original code. In particular the original code failed to
  9. * wake up processes that were waiting for semval to go to 0 if the
  10. * value went to 0 and was then incremented rapidly enough. In solving
  11. * this problem I have also modified the implementation so that it
  12. * processes pending operations in a FIFO manner, thus give a guarantee
  13. * that processes waiting for a lock on the semaphore won't starve
  14. * unless another locking process fails to unlock.
  15. * In addition the following two changes in behavior have been introduced:
  16. * - The original implementation of semop returned the value
  17. * last semaphore element examined on success. This does not
  18. * match the manual page specifications, and effectively
  19. * allows the user to read the semaphore even if they do not
  20. * have read permissions. The implementation now returns 0
  21. * on success as stated in the manual page.
  22. * - There is some confusion over whether the set of undo adjustments
  23. * to be performed at exit should be done in an atomic manner.
  24. * That is, if we are attempting to decrement the semval should we queue
  25. * up and wait until we can do so legally?
  26. * The original implementation attempted to do this.
  27. * The current implementation does not do so. This is because I don't
  28. * think it is the right thing (TM) to do, and because I couldn't
  29. * see a clean way to get the old behavior with the new design.
  30. * The POSIX standard and SVID should be consulted to determine
  31. * what behavior is mandated.
  32. *
  33. * Further notes on refinement (Christoph Rohland, December 1998):
  34. * - The POSIX standard says, that the undo adjustments simply should
  35. * redo. So the current implementation is o.K.
  36. * - The previous code had two flaws:
  37. * 1) It actively gave the semaphore to the next waiting process
  38. * sleeping on the semaphore. Since this process did not have the
  39. * cpu this led to many unnecessary context switches and bad
  40. * performance. Now we only check which process should be able to
  41. * get the semaphore and if this process wants to reduce some
  42. * semaphore value we simply wake it up without doing the
  43. * operation. So it has to try to get it later. Thus e.g. the
  44. * running process may reacquire the semaphore during the current
  45. * time slice. If it only waits for zero or increases the semaphore,
  46. * we do the operation in advance and wake it up.
  47. * 2) It did not wake up all zero waiting processes. We try to do
  48. * better but only get the semops right which only wait for zero or
  49. * increase. If there are decrement operations in the operations
  50. * array we do the same as before.
  51. *
  52. * With the incarnation of O(1) scheduler, it becomes unnecessary to perform
  53. * check/retry algorithm for waking up blocked processes as the new scheduler
  54. * is better at handling thread switch than the old one.
  55. *
  56. * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  57. *
  58. * SMP-threaded, sysctl's added
  59. * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
  60. * Enforced range limit on SEM_UNDO
  61. * (c) 2001 Red Hat Inc <alan@redhat.com>
  62. * Lockless wakeup
  63. * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
  64. */
  65. #include <linux/config.h>
  66. #include <linux/slab.h>
  67. #include <linux/spinlock.h>
  68. #include <linux/init.h>
  69. #include <linux/proc_fs.h>
  70. #include <linux/time.h>
  71. #include <linux/smp_lock.h>
  72. #include <linux/security.h>
  73. #include <linux/syscalls.h>
  74. #include <linux/audit.h>
  75. #include <linux/capability.h>
  76. #include <linux/seq_file.h>
  77. #include <asm/uaccess.h>
  78. #include "util.h"
  79. #define sem_lock(id) ((struct sem_array*)ipc_lock(&sem_ids,id))
  80. #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
  81. #define sem_rmid(id) ((struct sem_array*)ipc_rmid(&sem_ids,id))
  82. #define sem_checkid(sma, semid) \
  83. ipc_checkid(&sem_ids,&sma->sem_perm,semid)
  84. #define sem_buildid(id, seq) \
  85. ipc_buildid(&sem_ids, id, seq)
  86. static struct ipc_ids sem_ids;
  87. static int newary (key_t, int, int);
  88. static void freeary (struct sem_array *sma, int id);
  89. #ifdef CONFIG_PROC_FS
  90. static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
  91. #endif
  92. #define SEMMSL_FAST 256 /* 512 bytes on stack */
  93. #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
  94. /*
  95. * linked list protection:
  96. * sem_undo.id_next,
  97. * sem_array.sem_pending{,last},
  98. * sem_array.sem_undo: sem_lock() for read/write
  99. * sem_undo.proc_next: only "current" is allowed to read/write that field.
  100. *
  101. */
  102. int sem_ctls[4] = {SEMMSL, SEMMNS, SEMOPM, SEMMNI};
  103. #define sc_semmsl (sem_ctls[0])
  104. #define sc_semmns (sem_ctls[1])
  105. #define sc_semopm (sem_ctls[2])
  106. #define sc_semmni (sem_ctls[3])
  107. static int used_sems;
  108. void __init sem_init (void)
  109. {
  110. used_sems = 0;
  111. ipc_init_ids(&sem_ids,sc_semmni);
  112. ipc_init_proc_interface("sysvipc/sem",
  113. " key semid perms nsems uid gid cuid cgid otime ctime\n",
  114. &sem_ids,
  115. sysvipc_sem_proc_show);
  116. }
  117. /*
  118. * Lockless wakeup algorithm:
  119. * Without the check/retry algorithm a lockless wakeup is possible:
  120. * - queue.status is initialized to -EINTR before blocking.
  121. * - wakeup is performed by
  122. * * unlinking the queue entry from sma->sem_pending
  123. * * setting queue.status to IN_WAKEUP
  124. * This is the notification for the blocked thread that a
  125. * result value is imminent.
  126. * * call wake_up_process
  127. * * set queue.status to the final value.
  128. * - the previously blocked thread checks queue.status:
  129. * * if it's IN_WAKEUP, then it must wait until the value changes
  130. * * if it's not -EINTR, then the operation was completed by
  131. * update_queue. semtimedop can return queue.status without
  132. * performing any operation on the semaphore array.
  133. * * otherwise it must acquire the spinlock and check what's up.
  134. *
  135. * The two-stage algorithm is necessary to protect against the following
  136. * races:
  137. * - if queue.status is set after wake_up_process, then the woken up idle
  138. * thread could race forward and try (and fail) to acquire sma->lock
  139. * before update_queue had a chance to set queue.status
  140. * - if queue.status is written before wake_up_process and if the
  141. * blocked process is woken up by a signal between writing
  142. * queue.status and the wake_up_process, then the woken up
  143. * process could return from semtimedop and die by calling
  144. * sys_exit before wake_up_process is called. Then wake_up_process
  145. * will oops, because the task structure is already invalid.
  146. * (yes, this happened on s390 with sysv msg).
  147. *
  148. */
  149. #define IN_WAKEUP 1
  150. static int newary (key_t key, int nsems, int semflg)
  151. {
  152. int id;
  153. int retval;
  154. struct sem_array *sma;
  155. int size;
  156. if (!nsems)
  157. return -EINVAL;
  158. if (used_sems + nsems > sc_semmns)
  159. return -ENOSPC;
  160. size = sizeof (*sma) + nsems * sizeof (struct sem);
  161. sma = ipc_rcu_alloc(size);
  162. if (!sma) {
  163. return -ENOMEM;
  164. }
  165. memset (sma, 0, size);
  166. sma->sem_perm.mode = (semflg & S_IRWXUGO);
  167. sma->sem_perm.key = key;
  168. sma->sem_perm.security = NULL;
  169. retval = security_sem_alloc(sma);
  170. if (retval) {
  171. ipc_rcu_putref(sma);
  172. return retval;
  173. }
  174. id = ipc_addid(&sem_ids, &sma->sem_perm, sc_semmni);
  175. if(id == -1) {
  176. security_sem_free(sma);
  177. ipc_rcu_putref(sma);
  178. return -ENOSPC;
  179. }
  180. used_sems += nsems;
  181. sma->sem_id = sem_buildid(id, sma->sem_perm.seq);
  182. sma->sem_base = (struct sem *) &sma[1];
  183. /* sma->sem_pending = NULL; */
  184. sma->sem_pending_last = &sma->sem_pending;
  185. /* sma->undo = NULL; */
  186. sma->sem_nsems = nsems;
  187. sma->sem_ctime = get_seconds();
  188. sem_unlock(sma);
  189. return sma->sem_id;
  190. }
  191. asmlinkage long sys_semget (key_t key, int nsems, int semflg)
  192. {
  193. int id, err = -EINVAL;
  194. struct sem_array *sma;
  195. if (nsems < 0 || nsems > sc_semmsl)
  196. return -EINVAL;
  197. down(&sem_ids.sem);
  198. if (key == IPC_PRIVATE) {
  199. err = newary(key, nsems, semflg);
  200. } else if ((id = ipc_findkey(&sem_ids, key)) == -1) { /* key not used */
  201. if (!(semflg & IPC_CREAT))
  202. err = -ENOENT;
  203. else
  204. err = newary(key, nsems, semflg);
  205. } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
  206. err = -EEXIST;
  207. } else {
  208. sma = sem_lock(id);
  209. if(sma==NULL)
  210. BUG();
  211. if (nsems > sma->sem_nsems)
  212. err = -EINVAL;
  213. else if (ipcperms(&sma->sem_perm, semflg))
  214. err = -EACCES;
  215. else {
  216. int semid = sem_buildid(id, sma->sem_perm.seq);
  217. err = security_sem_associate(sma, semflg);
  218. if (!err)
  219. err = semid;
  220. }
  221. sem_unlock(sma);
  222. }
  223. up(&sem_ids.sem);
  224. return err;
  225. }
  226. /* Manage the doubly linked list sma->sem_pending as a FIFO:
  227. * insert new queue elements at the tail sma->sem_pending_last.
  228. */
  229. static inline void append_to_queue (struct sem_array * sma,
  230. struct sem_queue * q)
  231. {
  232. *(q->prev = sma->sem_pending_last) = q;
  233. *(sma->sem_pending_last = &q->next) = NULL;
  234. }
  235. static inline void prepend_to_queue (struct sem_array * sma,
  236. struct sem_queue * q)
  237. {
  238. q->next = sma->sem_pending;
  239. *(q->prev = &sma->sem_pending) = q;
  240. if (q->next)
  241. q->next->prev = &q->next;
  242. else /* sma->sem_pending_last == &sma->sem_pending */
  243. sma->sem_pending_last = &q->next;
  244. }
  245. static inline void remove_from_queue (struct sem_array * sma,
  246. struct sem_queue * q)
  247. {
  248. *(q->prev) = q->next;
  249. if (q->next)
  250. q->next->prev = q->prev;
  251. else /* sma->sem_pending_last == &q->next */
  252. sma->sem_pending_last = q->prev;
  253. q->prev = NULL; /* mark as removed */
  254. }
  255. /*
  256. * Determine whether a sequence of semaphore operations would succeed
  257. * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
  258. */
  259. static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
  260. int nsops, struct sem_undo *un, int pid)
  261. {
  262. int result, sem_op;
  263. struct sembuf *sop;
  264. struct sem * curr;
  265. for (sop = sops; sop < sops + nsops; sop++) {
  266. curr = sma->sem_base + sop->sem_num;
  267. sem_op = sop->sem_op;
  268. result = curr->semval;
  269. if (!sem_op && result)
  270. goto would_block;
  271. result += sem_op;
  272. if (result < 0)
  273. goto would_block;
  274. if (result > SEMVMX)
  275. goto out_of_range;
  276. if (sop->sem_flg & SEM_UNDO) {
  277. int undo = un->semadj[sop->sem_num] - sem_op;
  278. /*
  279. * Exceeding the undo range is an error.
  280. */
  281. if (undo < (-SEMAEM - 1) || undo > SEMAEM)
  282. goto out_of_range;
  283. }
  284. curr->semval = result;
  285. }
  286. sop--;
  287. while (sop >= sops) {
  288. sma->sem_base[sop->sem_num].sempid = pid;
  289. if (sop->sem_flg & SEM_UNDO)
  290. un->semadj[sop->sem_num] -= sop->sem_op;
  291. sop--;
  292. }
  293. sma->sem_otime = get_seconds();
  294. return 0;
  295. out_of_range:
  296. result = -ERANGE;
  297. goto undo;
  298. would_block:
  299. if (sop->sem_flg & IPC_NOWAIT)
  300. result = -EAGAIN;
  301. else
  302. result = 1;
  303. undo:
  304. sop--;
  305. while (sop >= sops) {
  306. sma->sem_base[sop->sem_num].semval -= sop->sem_op;
  307. sop--;
  308. }
  309. return result;
  310. }
  311. /* Go through the pending queue for the indicated semaphore
  312. * looking for tasks that can be completed.
  313. */
  314. static void update_queue (struct sem_array * sma)
  315. {
  316. int error;
  317. struct sem_queue * q;
  318. q = sma->sem_pending;
  319. while(q) {
  320. error = try_atomic_semop(sma, q->sops, q->nsops,
  321. q->undo, q->pid);
  322. /* Does q->sleeper still need to sleep? */
  323. if (error <= 0) {
  324. struct sem_queue *n;
  325. remove_from_queue(sma,q);
  326. q->status = IN_WAKEUP;
  327. /*
  328. * Continue scanning. The next operation
  329. * that must be checked depends on the type of the
  330. * completed operation:
  331. * - if the operation modified the array, then
  332. * restart from the head of the queue and
  333. * check for threads that might be waiting
  334. * for semaphore values to become 0.
  335. * - if the operation didn't modify the array,
  336. * then just continue.
  337. */
  338. if (q->alter)
  339. n = sma->sem_pending;
  340. else
  341. n = q->next;
  342. wake_up_process(q->sleeper);
  343. /* hands-off: q will disappear immediately after
  344. * writing q->status.
  345. */
  346. smp_wmb();
  347. q->status = error;
  348. q = n;
  349. } else {
  350. q = q->next;
  351. }
  352. }
  353. }
  354. /* The following counts are associated to each semaphore:
  355. * semncnt number of tasks waiting on semval being nonzero
  356. * semzcnt number of tasks waiting on semval being zero
  357. * This model assumes that a task waits on exactly one semaphore.
  358. * Since semaphore operations are to be performed atomically, tasks actually
  359. * wait on a whole sequence of semaphores simultaneously.
  360. * The counts we return here are a rough approximation, but still
  361. * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
  362. */
  363. static int count_semncnt (struct sem_array * sma, ushort semnum)
  364. {
  365. int semncnt;
  366. struct sem_queue * q;
  367. semncnt = 0;
  368. for (q = sma->sem_pending; q; q = q->next) {
  369. struct sembuf * sops = q->sops;
  370. int nsops = q->nsops;
  371. int i;
  372. for (i = 0; i < nsops; i++)
  373. if (sops[i].sem_num == semnum
  374. && (sops[i].sem_op < 0)
  375. && !(sops[i].sem_flg & IPC_NOWAIT))
  376. semncnt++;
  377. }
  378. return semncnt;
  379. }
  380. static int count_semzcnt (struct sem_array * sma, ushort semnum)
  381. {
  382. int semzcnt;
  383. struct sem_queue * q;
  384. semzcnt = 0;
  385. for (q = sma->sem_pending; q; q = q->next) {
  386. struct sembuf * sops = q->sops;
  387. int nsops = q->nsops;
  388. int i;
  389. for (i = 0; i < nsops; i++)
  390. if (sops[i].sem_num == semnum
  391. && (sops[i].sem_op == 0)
  392. && !(sops[i].sem_flg & IPC_NOWAIT))
  393. semzcnt++;
  394. }
  395. return semzcnt;
  396. }
  397. /* Free a semaphore set. freeary() is called with sem_ids.sem down and
  398. * the spinlock for this semaphore set hold. sem_ids.sem remains locked
  399. * on exit.
  400. */
  401. static void freeary (struct sem_array *sma, int id)
  402. {
  403. struct sem_undo *un;
  404. struct sem_queue *q;
  405. int size;
  406. /* Invalidate the existing undo structures for this semaphore set.
  407. * (They will be freed without any further action in exit_sem()
  408. * or during the next semop.)
  409. */
  410. for (un = sma->undo; un; un = un->id_next)
  411. un->semid = -1;
  412. /* Wake up all pending processes and let them fail with EIDRM. */
  413. q = sma->sem_pending;
  414. while(q) {
  415. struct sem_queue *n;
  416. /* lazy remove_from_queue: we are killing the whole queue */
  417. q->prev = NULL;
  418. n = q->next;
  419. q->status = IN_WAKEUP;
  420. wake_up_process(q->sleeper); /* doesn't sleep */
  421. smp_wmb();
  422. q->status = -EIDRM; /* hands-off q */
  423. q = n;
  424. }
  425. /* Remove the semaphore set from the ID array*/
  426. sma = sem_rmid(id);
  427. sem_unlock(sma);
  428. used_sems -= sma->sem_nsems;
  429. size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
  430. security_sem_free(sma);
  431. ipc_rcu_putref(sma);
  432. }
  433. static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
  434. {
  435. switch(version) {
  436. case IPC_64:
  437. return copy_to_user(buf, in, sizeof(*in));
  438. case IPC_OLD:
  439. {
  440. struct semid_ds out;
  441. ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
  442. out.sem_otime = in->sem_otime;
  443. out.sem_ctime = in->sem_ctime;
  444. out.sem_nsems = in->sem_nsems;
  445. return copy_to_user(buf, &out, sizeof(out));
  446. }
  447. default:
  448. return -EINVAL;
  449. }
  450. }
  451. static int semctl_nolock(int semid, int semnum, int cmd, int version, union semun arg)
  452. {
  453. int err = -EINVAL;
  454. struct sem_array *sma;
  455. switch(cmd) {
  456. case IPC_INFO:
  457. case SEM_INFO:
  458. {
  459. struct seminfo seminfo;
  460. int max_id;
  461. err = security_sem_semctl(NULL, cmd);
  462. if (err)
  463. return err;
  464. memset(&seminfo,0,sizeof(seminfo));
  465. seminfo.semmni = sc_semmni;
  466. seminfo.semmns = sc_semmns;
  467. seminfo.semmsl = sc_semmsl;
  468. seminfo.semopm = sc_semopm;
  469. seminfo.semvmx = SEMVMX;
  470. seminfo.semmnu = SEMMNU;
  471. seminfo.semmap = SEMMAP;
  472. seminfo.semume = SEMUME;
  473. down(&sem_ids.sem);
  474. if (cmd == SEM_INFO) {
  475. seminfo.semusz = sem_ids.in_use;
  476. seminfo.semaem = used_sems;
  477. } else {
  478. seminfo.semusz = SEMUSZ;
  479. seminfo.semaem = SEMAEM;
  480. }
  481. max_id = sem_ids.max_id;
  482. up(&sem_ids.sem);
  483. if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
  484. return -EFAULT;
  485. return (max_id < 0) ? 0: max_id;
  486. }
  487. case SEM_STAT:
  488. {
  489. struct semid64_ds tbuf;
  490. int id;
  491. if(semid >= sem_ids.entries->size)
  492. return -EINVAL;
  493. memset(&tbuf,0,sizeof(tbuf));
  494. sma = sem_lock(semid);
  495. if(sma == NULL)
  496. return -EINVAL;
  497. err = -EACCES;
  498. if (ipcperms (&sma->sem_perm, S_IRUGO))
  499. goto out_unlock;
  500. err = security_sem_semctl(sma, cmd);
  501. if (err)
  502. goto out_unlock;
  503. id = sem_buildid(semid, sma->sem_perm.seq);
  504. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  505. tbuf.sem_otime = sma->sem_otime;
  506. tbuf.sem_ctime = sma->sem_ctime;
  507. tbuf.sem_nsems = sma->sem_nsems;
  508. sem_unlock(sma);
  509. if (copy_semid_to_user (arg.buf, &tbuf, version))
  510. return -EFAULT;
  511. return id;
  512. }
  513. default:
  514. return -EINVAL;
  515. }
  516. return err;
  517. out_unlock:
  518. sem_unlock(sma);
  519. return err;
  520. }
  521. static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg)
  522. {
  523. struct sem_array *sma;
  524. struct sem* curr;
  525. int err;
  526. ushort fast_sem_io[SEMMSL_FAST];
  527. ushort* sem_io = fast_sem_io;
  528. int nsems;
  529. sma = sem_lock(semid);
  530. if(sma==NULL)
  531. return -EINVAL;
  532. nsems = sma->sem_nsems;
  533. err=-EIDRM;
  534. if (sem_checkid(sma,semid))
  535. goto out_unlock;
  536. err = -EACCES;
  537. if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
  538. goto out_unlock;
  539. err = security_sem_semctl(sma, cmd);
  540. if (err)
  541. goto out_unlock;
  542. err = -EACCES;
  543. switch (cmd) {
  544. case GETALL:
  545. {
  546. ushort __user *array = arg.array;
  547. int i;
  548. if(nsems > SEMMSL_FAST) {
  549. ipc_rcu_getref(sma);
  550. sem_unlock(sma);
  551. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  552. if(sem_io == NULL) {
  553. ipc_lock_by_ptr(&sma->sem_perm);
  554. ipc_rcu_putref(sma);
  555. sem_unlock(sma);
  556. return -ENOMEM;
  557. }
  558. ipc_lock_by_ptr(&sma->sem_perm);
  559. ipc_rcu_putref(sma);
  560. if (sma->sem_perm.deleted) {
  561. sem_unlock(sma);
  562. err = -EIDRM;
  563. goto out_free;
  564. }
  565. }
  566. for (i = 0; i < sma->sem_nsems; i++)
  567. sem_io[i] = sma->sem_base[i].semval;
  568. sem_unlock(sma);
  569. err = 0;
  570. if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
  571. err = -EFAULT;
  572. goto out_free;
  573. }
  574. case SETALL:
  575. {
  576. int i;
  577. struct sem_undo *un;
  578. ipc_rcu_getref(sma);
  579. sem_unlock(sma);
  580. if(nsems > SEMMSL_FAST) {
  581. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  582. if(sem_io == NULL) {
  583. ipc_lock_by_ptr(&sma->sem_perm);
  584. ipc_rcu_putref(sma);
  585. sem_unlock(sma);
  586. return -ENOMEM;
  587. }
  588. }
  589. if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
  590. ipc_lock_by_ptr(&sma->sem_perm);
  591. ipc_rcu_putref(sma);
  592. sem_unlock(sma);
  593. err = -EFAULT;
  594. goto out_free;
  595. }
  596. for (i = 0; i < nsems; i++) {
  597. if (sem_io[i] > SEMVMX) {
  598. ipc_lock_by_ptr(&sma->sem_perm);
  599. ipc_rcu_putref(sma);
  600. sem_unlock(sma);
  601. err = -ERANGE;
  602. goto out_free;
  603. }
  604. }
  605. ipc_lock_by_ptr(&sma->sem_perm);
  606. ipc_rcu_putref(sma);
  607. if (sma->sem_perm.deleted) {
  608. sem_unlock(sma);
  609. err = -EIDRM;
  610. goto out_free;
  611. }
  612. for (i = 0; i < nsems; i++)
  613. sma->sem_base[i].semval = sem_io[i];
  614. for (un = sma->undo; un; un = un->id_next)
  615. for (i = 0; i < nsems; i++)
  616. un->semadj[i] = 0;
  617. sma->sem_ctime = get_seconds();
  618. /* maybe some queued-up processes were waiting for this */
  619. update_queue(sma);
  620. err = 0;
  621. goto out_unlock;
  622. }
  623. case IPC_STAT:
  624. {
  625. struct semid64_ds tbuf;
  626. memset(&tbuf,0,sizeof(tbuf));
  627. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  628. tbuf.sem_otime = sma->sem_otime;
  629. tbuf.sem_ctime = sma->sem_ctime;
  630. tbuf.sem_nsems = sma->sem_nsems;
  631. sem_unlock(sma);
  632. if (copy_semid_to_user (arg.buf, &tbuf, version))
  633. return -EFAULT;
  634. return 0;
  635. }
  636. /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
  637. }
  638. err = -EINVAL;
  639. if(semnum < 0 || semnum >= nsems)
  640. goto out_unlock;
  641. curr = &sma->sem_base[semnum];
  642. switch (cmd) {
  643. case GETVAL:
  644. err = curr->semval;
  645. goto out_unlock;
  646. case GETPID:
  647. err = curr->sempid;
  648. goto out_unlock;
  649. case GETNCNT:
  650. err = count_semncnt(sma,semnum);
  651. goto out_unlock;
  652. case GETZCNT:
  653. err = count_semzcnt(sma,semnum);
  654. goto out_unlock;
  655. case SETVAL:
  656. {
  657. int val = arg.val;
  658. struct sem_undo *un;
  659. err = -ERANGE;
  660. if (val > SEMVMX || val < 0)
  661. goto out_unlock;
  662. for (un = sma->undo; un; un = un->id_next)
  663. un->semadj[semnum] = 0;
  664. curr->semval = val;
  665. curr->sempid = current->tgid;
  666. sma->sem_ctime = get_seconds();
  667. /* maybe some queued-up processes were waiting for this */
  668. update_queue(sma);
  669. err = 0;
  670. goto out_unlock;
  671. }
  672. }
  673. out_unlock:
  674. sem_unlock(sma);
  675. out_free:
  676. if(sem_io != fast_sem_io)
  677. ipc_free(sem_io, sizeof(ushort)*nsems);
  678. return err;
  679. }
  680. struct sem_setbuf {
  681. uid_t uid;
  682. gid_t gid;
  683. mode_t mode;
  684. };
  685. static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
  686. {
  687. switch(version) {
  688. case IPC_64:
  689. {
  690. struct semid64_ds tbuf;
  691. if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
  692. return -EFAULT;
  693. out->uid = tbuf.sem_perm.uid;
  694. out->gid = tbuf.sem_perm.gid;
  695. out->mode = tbuf.sem_perm.mode;
  696. return 0;
  697. }
  698. case IPC_OLD:
  699. {
  700. struct semid_ds tbuf_old;
  701. if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  702. return -EFAULT;
  703. out->uid = tbuf_old.sem_perm.uid;
  704. out->gid = tbuf_old.sem_perm.gid;
  705. out->mode = tbuf_old.sem_perm.mode;
  706. return 0;
  707. }
  708. default:
  709. return -EINVAL;
  710. }
  711. }
  712. static int semctl_down(int semid, int semnum, int cmd, int version, union semun arg)
  713. {
  714. struct sem_array *sma;
  715. int err;
  716. struct sem_setbuf setbuf;
  717. struct kern_ipc_perm *ipcp;
  718. if(cmd == IPC_SET) {
  719. if(copy_semid_from_user (&setbuf, arg.buf, version))
  720. return -EFAULT;
  721. if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode)))
  722. return err;
  723. }
  724. sma = sem_lock(semid);
  725. if(sma==NULL)
  726. return -EINVAL;
  727. if (sem_checkid(sma,semid)) {
  728. err=-EIDRM;
  729. goto out_unlock;
  730. }
  731. ipcp = &sma->sem_perm;
  732. if (current->euid != ipcp->cuid &&
  733. current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
  734. err=-EPERM;
  735. goto out_unlock;
  736. }
  737. err = security_sem_semctl(sma, cmd);
  738. if (err)
  739. goto out_unlock;
  740. switch(cmd){
  741. case IPC_RMID:
  742. freeary(sma, semid);
  743. err = 0;
  744. break;
  745. case IPC_SET:
  746. ipcp->uid = setbuf.uid;
  747. ipcp->gid = setbuf.gid;
  748. ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
  749. | (setbuf.mode & S_IRWXUGO);
  750. sma->sem_ctime = get_seconds();
  751. sem_unlock(sma);
  752. err = 0;
  753. break;
  754. default:
  755. sem_unlock(sma);
  756. err = -EINVAL;
  757. break;
  758. }
  759. return err;
  760. out_unlock:
  761. sem_unlock(sma);
  762. return err;
  763. }
  764. asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
  765. {
  766. int err = -EINVAL;
  767. int version;
  768. if (semid < 0)
  769. return -EINVAL;
  770. version = ipc_parse_version(&cmd);
  771. switch(cmd) {
  772. case IPC_INFO:
  773. case SEM_INFO:
  774. case SEM_STAT:
  775. err = semctl_nolock(semid,semnum,cmd,version,arg);
  776. return err;
  777. case GETALL:
  778. case GETVAL:
  779. case GETPID:
  780. case GETNCNT:
  781. case GETZCNT:
  782. case IPC_STAT:
  783. case SETVAL:
  784. case SETALL:
  785. err = semctl_main(semid,semnum,cmd,version,arg);
  786. return err;
  787. case IPC_RMID:
  788. case IPC_SET:
  789. down(&sem_ids.sem);
  790. err = semctl_down(semid,semnum,cmd,version,arg);
  791. up(&sem_ids.sem);
  792. return err;
  793. default:
  794. return -EINVAL;
  795. }
  796. }
  797. static inline void lock_semundo(void)
  798. {
  799. struct sem_undo_list *undo_list;
  800. undo_list = current->sysvsem.undo_list;
  801. if (undo_list)
  802. spin_lock(&undo_list->lock);
  803. }
  804. /* This code has an interaction with copy_semundo().
  805. * Consider; two tasks are sharing the undo_list. task1
  806. * acquires the undo_list lock in lock_semundo(). If task2 now
  807. * exits before task1 releases the lock (by calling
  808. * unlock_semundo()), then task1 will never call spin_unlock().
  809. * This leave the sem_undo_list in a locked state. If task1 now creats task3
  810. * and once again shares the sem_undo_list, the sem_undo_list will still be
  811. * locked, and future SEM_UNDO operations will deadlock. This case is
  812. * dealt with in copy_semundo() by having it reinitialize the spin lock when
  813. * the refcnt goes from 1 to 2.
  814. */
  815. static inline void unlock_semundo(void)
  816. {
  817. struct sem_undo_list *undo_list;
  818. undo_list = current->sysvsem.undo_list;
  819. if (undo_list)
  820. spin_unlock(&undo_list->lock);
  821. }
  822. /* If the task doesn't already have a undo_list, then allocate one
  823. * here. We guarantee there is only one thread using this undo list,
  824. * and current is THE ONE
  825. *
  826. * If this allocation and assignment succeeds, but later
  827. * portions of this code fail, there is no need to free the sem_undo_list.
  828. * Just let it stay associated with the task, and it'll be freed later
  829. * at exit time.
  830. *
  831. * This can block, so callers must hold no locks.
  832. */
  833. static inline int get_undo_list(struct sem_undo_list **undo_listp)
  834. {
  835. struct sem_undo_list *undo_list;
  836. int size;
  837. undo_list = current->sysvsem.undo_list;
  838. if (!undo_list) {
  839. size = sizeof(struct sem_undo_list);
  840. undo_list = (struct sem_undo_list *) kmalloc(size, GFP_KERNEL);
  841. if (undo_list == NULL)
  842. return -ENOMEM;
  843. memset(undo_list, 0, size);
  844. spin_lock_init(&undo_list->lock);
  845. atomic_set(&undo_list->refcnt, 1);
  846. current->sysvsem.undo_list = undo_list;
  847. }
  848. *undo_listp = undo_list;
  849. return 0;
  850. }
  851. static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
  852. {
  853. struct sem_undo **last, *un;
  854. last = &ulp->proc_list;
  855. un = *last;
  856. while(un != NULL) {
  857. if(un->semid==semid)
  858. break;
  859. if(un->semid==-1) {
  860. *last=un->proc_next;
  861. kfree(un);
  862. } else {
  863. last=&un->proc_next;
  864. }
  865. un=*last;
  866. }
  867. return un;
  868. }
  869. static struct sem_undo *find_undo(int semid)
  870. {
  871. struct sem_array *sma;
  872. struct sem_undo_list *ulp;
  873. struct sem_undo *un, *new;
  874. int nsems;
  875. int error;
  876. error = get_undo_list(&ulp);
  877. if (error)
  878. return ERR_PTR(error);
  879. lock_semundo();
  880. un = lookup_undo(ulp, semid);
  881. unlock_semundo();
  882. if (likely(un!=NULL))
  883. goto out;
  884. /* no undo structure around - allocate one. */
  885. sma = sem_lock(semid);
  886. un = ERR_PTR(-EINVAL);
  887. if(sma==NULL)
  888. goto out;
  889. un = ERR_PTR(-EIDRM);
  890. if (sem_checkid(sma,semid)) {
  891. sem_unlock(sma);
  892. goto out;
  893. }
  894. nsems = sma->sem_nsems;
  895. ipc_rcu_getref(sma);
  896. sem_unlock(sma);
  897. new = (struct sem_undo *) kmalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
  898. if (!new) {
  899. ipc_lock_by_ptr(&sma->sem_perm);
  900. ipc_rcu_putref(sma);
  901. sem_unlock(sma);
  902. return ERR_PTR(-ENOMEM);
  903. }
  904. memset(new, 0, sizeof(struct sem_undo) + sizeof(short)*nsems);
  905. new->semadj = (short *) &new[1];
  906. new->semid = semid;
  907. lock_semundo();
  908. un = lookup_undo(ulp, semid);
  909. if (un) {
  910. unlock_semundo();
  911. kfree(new);
  912. ipc_lock_by_ptr(&sma->sem_perm);
  913. ipc_rcu_putref(sma);
  914. sem_unlock(sma);
  915. goto out;
  916. }
  917. ipc_lock_by_ptr(&sma->sem_perm);
  918. ipc_rcu_putref(sma);
  919. if (sma->sem_perm.deleted) {
  920. sem_unlock(sma);
  921. unlock_semundo();
  922. kfree(new);
  923. un = ERR_PTR(-EIDRM);
  924. goto out;
  925. }
  926. new->proc_next = ulp->proc_list;
  927. ulp->proc_list = new;
  928. new->id_next = sma->undo;
  929. sma->undo = new;
  930. sem_unlock(sma);
  931. un = new;
  932. unlock_semundo();
  933. out:
  934. return un;
  935. }
  936. asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
  937. unsigned nsops, const struct timespec __user *timeout)
  938. {
  939. int error = -EINVAL;
  940. struct sem_array *sma;
  941. struct sembuf fast_sops[SEMOPM_FAST];
  942. struct sembuf* sops = fast_sops, *sop;
  943. struct sem_undo *un;
  944. int undos = 0, alter = 0, max;
  945. struct sem_queue queue;
  946. unsigned long jiffies_left = 0;
  947. if (nsops < 1 || semid < 0)
  948. return -EINVAL;
  949. if (nsops > sc_semopm)
  950. return -E2BIG;
  951. if(nsops > SEMOPM_FAST) {
  952. sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
  953. if(sops==NULL)
  954. return -ENOMEM;
  955. }
  956. if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
  957. error=-EFAULT;
  958. goto out_free;
  959. }
  960. if (timeout) {
  961. struct timespec _timeout;
  962. if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
  963. error = -EFAULT;
  964. goto out_free;
  965. }
  966. if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
  967. _timeout.tv_nsec >= 1000000000L) {
  968. error = -EINVAL;
  969. goto out_free;
  970. }
  971. jiffies_left = timespec_to_jiffies(&_timeout);
  972. }
  973. max = 0;
  974. for (sop = sops; sop < sops + nsops; sop++) {
  975. if (sop->sem_num >= max)
  976. max = sop->sem_num;
  977. if (sop->sem_flg & SEM_UNDO)
  978. undos = 1;
  979. if (sop->sem_op != 0)
  980. alter = 1;
  981. }
  982. retry_undos:
  983. if (undos) {
  984. un = find_undo(semid);
  985. if (IS_ERR(un)) {
  986. error = PTR_ERR(un);
  987. goto out_free;
  988. }
  989. } else
  990. un = NULL;
  991. sma = sem_lock(semid);
  992. error=-EINVAL;
  993. if(sma==NULL)
  994. goto out_free;
  995. error = -EIDRM;
  996. if (sem_checkid(sma,semid))
  997. goto out_unlock_free;
  998. /*
  999. * semid identifies are not unique - find_undo may have
  1000. * allocated an undo structure, it was invalidated by an RMID
  1001. * and now a new array with received the same id. Check and retry.
  1002. */
  1003. if (un && un->semid == -1) {
  1004. sem_unlock(sma);
  1005. goto retry_undos;
  1006. }
  1007. error = -EFBIG;
  1008. if (max >= sma->sem_nsems)
  1009. goto out_unlock_free;
  1010. error = -EACCES;
  1011. if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
  1012. goto out_unlock_free;
  1013. error = security_sem_semop(sma, sops, nsops, alter);
  1014. if (error)
  1015. goto out_unlock_free;
  1016. error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
  1017. if (error <= 0) {
  1018. if (alter && error == 0)
  1019. update_queue (sma);
  1020. goto out_unlock_free;
  1021. }
  1022. /* We need to sleep on this operation, so we put the current
  1023. * task into the pending queue and go to sleep.
  1024. */
  1025. queue.sma = sma;
  1026. queue.sops = sops;
  1027. queue.nsops = nsops;
  1028. queue.undo = un;
  1029. queue.pid = current->tgid;
  1030. queue.id = semid;
  1031. queue.alter = alter;
  1032. if (alter)
  1033. append_to_queue(sma ,&queue);
  1034. else
  1035. prepend_to_queue(sma ,&queue);
  1036. queue.status = -EINTR;
  1037. queue.sleeper = current;
  1038. current->state = TASK_INTERRUPTIBLE;
  1039. sem_unlock(sma);
  1040. if (timeout)
  1041. jiffies_left = schedule_timeout(jiffies_left);
  1042. else
  1043. schedule();
  1044. error = queue.status;
  1045. while(unlikely(error == IN_WAKEUP)) {
  1046. cpu_relax();
  1047. error = queue.status;
  1048. }
  1049. if (error != -EINTR) {
  1050. /* fast path: update_queue already obtained all requested
  1051. * resources */
  1052. goto out_free;
  1053. }
  1054. sma = sem_lock(semid);
  1055. if(sma==NULL) {
  1056. if(queue.prev != NULL)
  1057. BUG();
  1058. error = -EIDRM;
  1059. goto out_free;
  1060. }
  1061. /*
  1062. * If queue.status != -EINTR we are woken up by another process
  1063. */
  1064. error = queue.status;
  1065. if (error != -EINTR) {
  1066. goto out_unlock_free;
  1067. }
  1068. /*
  1069. * If an interrupt occurred we have to clean up the queue
  1070. */
  1071. if (timeout && jiffies_left == 0)
  1072. error = -EAGAIN;
  1073. remove_from_queue(sma,&queue);
  1074. goto out_unlock_free;
  1075. out_unlock_free:
  1076. sem_unlock(sma);
  1077. out_free:
  1078. if(sops != fast_sops)
  1079. kfree(sops);
  1080. return error;
  1081. }
  1082. asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
  1083. {
  1084. return sys_semtimedop(semid, tsops, nsops, NULL);
  1085. }
  1086. /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
  1087. * parent and child tasks.
  1088. *
  1089. * See the notes above unlock_semundo() regarding the spin_lock_init()
  1090. * in this code. Initialize the undo_list->lock here instead of get_undo_list()
  1091. * because of the reasoning in the comment above unlock_semundo.
  1092. */
  1093. int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
  1094. {
  1095. struct sem_undo_list *undo_list;
  1096. int error;
  1097. if (clone_flags & CLONE_SYSVSEM) {
  1098. error = get_undo_list(&undo_list);
  1099. if (error)
  1100. return error;
  1101. atomic_inc(&undo_list->refcnt);
  1102. tsk->sysvsem.undo_list = undo_list;
  1103. } else
  1104. tsk->sysvsem.undo_list = NULL;
  1105. return 0;
  1106. }
  1107. /*
  1108. * add semadj values to semaphores, free undo structures.
  1109. * undo structures are not freed when semaphore arrays are destroyed
  1110. * so some of them may be out of date.
  1111. * IMPLEMENTATION NOTE: There is some confusion over whether the
  1112. * set of adjustments that needs to be done should be done in an atomic
  1113. * manner or not. That is, if we are attempting to decrement the semval
  1114. * should we queue up and wait until we can do so legally?
  1115. * The original implementation attempted to do this (queue and wait).
  1116. * The current implementation does not do so. The POSIX standard
  1117. * and SVID should be consulted to determine what behavior is mandated.
  1118. */
  1119. void exit_sem(struct task_struct *tsk)
  1120. {
  1121. struct sem_undo_list *undo_list;
  1122. struct sem_undo *u, **up;
  1123. undo_list = tsk->sysvsem.undo_list;
  1124. if (!undo_list)
  1125. return;
  1126. if (!atomic_dec_and_test(&undo_list->refcnt))
  1127. return;
  1128. /* There's no need to hold the semundo list lock, as current
  1129. * is the last task exiting for this undo list.
  1130. */
  1131. for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
  1132. struct sem_array *sma;
  1133. int nsems, i;
  1134. struct sem_undo *un, **unp;
  1135. int semid;
  1136. semid = u->semid;
  1137. if(semid == -1)
  1138. continue;
  1139. sma = sem_lock(semid);
  1140. if (sma == NULL)
  1141. continue;
  1142. if (u->semid == -1)
  1143. goto next_entry;
  1144. BUG_ON(sem_checkid(sma,u->semid));
  1145. /* remove u from the sma->undo list */
  1146. for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
  1147. if (u == un)
  1148. goto found;
  1149. }
  1150. printk ("exit_sem undo list error id=%d\n", u->semid);
  1151. goto next_entry;
  1152. found:
  1153. *unp = un->id_next;
  1154. /* perform adjustments registered in u */
  1155. nsems = sma->sem_nsems;
  1156. for (i = 0; i < nsems; i++) {
  1157. struct sem * sem = &sma->sem_base[i];
  1158. if (u->semadj[i]) {
  1159. sem->semval += u->semadj[i];
  1160. /*
  1161. * Range checks of the new semaphore value,
  1162. * not defined by sus:
  1163. * - Some unices ignore the undo entirely
  1164. * (e.g. HP UX 11i 11.22, Tru64 V5.1)
  1165. * - some cap the value (e.g. FreeBSD caps
  1166. * at 0, but doesn't enforce SEMVMX)
  1167. *
  1168. * Linux caps the semaphore value, both at 0
  1169. * and at SEMVMX.
  1170. *
  1171. * Manfred <manfred@colorfullife.com>
  1172. */
  1173. if (sem->semval < 0)
  1174. sem->semval = 0;
  1175. if (sem->semval > SEMVMX)
  1176. sem->semval = SEMVMX;
  1177. sem->sempid = current->tgid;
  1178. }
  1179. }
  1180. sma->sem_otime = get_seconds();
  1181. /* maybe some queued-up processes were waiting for this */
  1182. update_queue(sma);
  1183. next_entry:
  1184. sem_unlock(sma);
  1185. }
  1186. kfree(undo_list);
  1187. }
  1188. #ifdef CONFIG_PROC_FS
  1189. static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
  1190. {
  1191. struct sem_array *sma = it;
  1192. return seq_printf(s,
  1193. "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
  1194. sma->sem_perm.key,
  1195. sma->sem_id,
  1196. sma->sem_perm.mode,
  1197. sma->sem_nsems,
  1198. sma->sem_perm.uid,
  1199. sma->sem_perm.gid,
  1200. sma->sem_perm.cuid,
  1201. sma->sem_perm.cgid,
  1202. sma->sem_otime,
  1203. sma->sem_ctime);
  1204. }
  1205. #endif