sem.c 33 KB

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