sem.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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. q->status = error;
  346. q = n;
  347. } else {
  348. q = q->next;
  349. }
  350. }
  351. }
  352. /* The following counts are associated to each semaphore:
  353. * semncnt number of tasks waiting on semval being nonzero
  354. * semzcnt number of tasks waiting on semval being zero
  355. * This model assumes that a task waits on exactly one semaphore.
  356. * Since semaphore operations are to be performed atomically, tasks actually
  357. * wait on a whole sequence of semaphores simultaneously.
  358. * The counts we return here are a rough approximation, but still
  359. * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
  360. */
  361. static int count_semncnt (struct sem_array * sma, ushort semnum)
  362. {
  363. int semncnt;
  364. struct sem_queue * q;
  365. semncnt = 0;
  366. for (q = sma->sem_pending; q; q = q->next) {
  367. struct sembuf * sops = q->sops;
  368. int nsops = q->nsops;
  369. int i;
  370. for (i = 0; i < nsops; i++)
  371. if (sops[i].sem_num == semnum
  372. && (sops[i].sem_op < 0)
  373. && !(sops[i].sem_flg & IPC_NOWAIT))
  374. semncnt++;
  375. }
  376. return semncnt;
  377. }
  378. static int count_semzcnt (struct sem_array * sma, ushort semnum)
  379. {
  380. int semzcnt;
  381. struct sem_queue * q;
  382. semzcnt = 0;
  383. for (q = sma->sem_pending; q; q = q->next) {
  384. struct sembuf * sops = q->sops;
  385. int nsops = q->nsops;
  386. int i;
  387. for (i = 0; i < nsops; i++)
  388. if (sops[i].sem_num == semnum
  389. && (sops[i].sem_op == 0)
  390. && !(sops[i].sem_flg & IPC_NOWAIT))
  391. semzcnt++;
  392. }
  393. return semzcnt;
  394. }
  395. /* Free a semaphore set. freeary() is called with sem_ids.sem down and
  396. * the spinlock for this semaphore set hold. sem_ids.sem remains locked
  397. * on exit.
  398. */
  399. static void freeary (struct sem_array *sma, int id)
  400. {
  401. struct sem_undo *un;
  402. struct sem_queue *q;
  403. int size;
  404. /* Invalidate the existing undo structures for this semaphore set.
  405. * (They will be freed without any further action in exit_sem()
  406. * or during the next semop.)
  407. */
  408. for (un = sma->undo; un; un = un->id_next)
  409. un->semid = -1;
  410. /* Wake up all pending processes and let them fail with EIDRM. */
  411. q = sma->sem_pending;
  412. while(q) {
  413. struct sem_queue *n;
  414. /* lazy remove_from_queue: we are killing the whole queue */
  415. q->prev = NULL;
  416. n = q->next;
  417. q->status = IN_WAKEUP;
  418. wake_up_process(q->sleeper); /* doesn't sleep */
  419. q->status = -EIDRM; /* hands-off q */
  420. q = n;
  421. }
  422. /* Remove the semaphore set from the ID array*/
  423. sma = sem_rmid(id);
  424. sem_unlock(sma);
  425. used_sems -= sma->sem_nsems;
  426. size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
  427. security_sem_free(sma);
  428. ipc_rcu_putref(sma);
  429. }
  430. static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
  431. {
  432. switch(version) {
  433. case IPC_64:
  434. return copy_to_user(buf, in, sizeof(*in));
  435. case IPC_OLD:
  436. {
  437. struct semid_ds out;
  438. ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
  439. out.sem_otime = in->sem_otime;
  440. out.sem_ctime = in->sem_ctime;
  441. out.sem_nsems = in->sem_nsems;
  442. return copy_to_user(buf, &out, sizeof(out));
  443. }
  444. default:
  445. return -EINVAL;
  446. }
  447. }
  448. static int semctl_nolock(int semid, int semnum, int cmd, int version, union semun arg)
  449. {
  450. int err = -EINVAL;
  451. struct sem_array *sma;
  452. switch(cmd) {
  453. case IPC_INFO:
  454. case SEM_INFO:
  455. {
  456. struct seminfo seminfo;
  457. int max_id;
  458. err = security_sem_semctl(NULL, cmd);
  459. if (err)
  460. return err;
  461. memset(&seminfo,0,sizeof(seminfo));
  462. seminfo.semmni = sc_semmni;
  463. seminfo.semmns = sc_semmns;
  464. seminfo.semmsl = sc_semmsl;
  465. seminfo.semopm = sc_semopm;
  466. seminfo.semvmx = SEMVMX;
  467. seminfo.semmnu = SEMMNU;
  468. seminfo.semmap = SEMMAP;
  469. seminfo.semume = SEMUME;
  470. down(&sem_ids.sem);
  471. if (cmd == SEM_INFO) {
  472. seminfo.semusz = sem_ids.in_use;
  473. seminfo.semaem = used_sems;
  474. } else {
  475. seminfo.semusz = SEMUSZ;
  476. seminfo.semaem = SEMAEM;
  477. }
  478. max_id = sem_ids.max_id;
  479. up(&sem_ids.sem);
  480. if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
  481. return -EFAULT;
  482. return (max_id < 0) ? 0: max_id;
  483. }
  484. case SEM_STAT:
  485. {
  486. struct semid64_ds tbuf;
  487. int id;
  488. if(semid >= sem_ids.entries->size)
  489. return -EINVAL;
  490. memset(&tbuf,0,sizeof(tbuf));
  491. sma = sem_lock(semid);
  492. if(sma == NULL)
  493. return -EINVAL;
  494. err = -EACCES;
  495. if (ipcperms (&sma->sem_perm, S_IRUGO))
  496. goto out_unlock;
  497. err = security_sem_semctl(sma, cmd);
  498. if (err)
  499. goto out_unlock;
  500. id = sem_buildid(semid, sma->sem_perm.seq);
  501. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  502. tbuf.sem_otime = sma->sem_otime;
  503. tbuf.sem_ctime = sma->sem_ctime;
  504. tbuf.sem_nsems = sma->sem_nsems;
  505. sem_unlock(sma);
  506. if (copy_semid_to_user (arg.buf, &tbuf, version))
  507. return -EFAULT;
  508. return id;
  509. }
  510. default:
  511. return -EINVAL;
  512. }
  513. return err;
  514. out_unlock:
  515. sem_unlock(sma);
  516. return err;
  517. }
  518. static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg)
  519. {
  520. struct sem_array *sma;
  521. struct sem* curr;
  522. int err;
  523. ushort fast_sem_io[SEMMSL_FAST];
  524. ushort* sem_io = fast_sem_io;
  525. int nsems;
  526. sma = sem_lock(semid);
  527. if(sma==NULL)
  528. return -EINVAL;
  529. nsems = sma->sem_nsems;
  530. err=-EIDRM;
  531. if (sem_checkid(sma,semid))
  532. goto out_unlock;
  533. err = -EACCES;
  534. if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
  535. goto out_unlock;
  536. err = security_sem_semctl(sma, cmd);
  537. if (err)
  538. goto out_unlock;
  539. err = -EACCES;
  540. switch (cmd) {
  541. case GETALL:
  542. {
  543. ushort __user *array = arg.array;
  544. int i;
  545. if(nsems > SEMMSL_FAST) {
  546. ipc_rcu_getref(sma);
  547. sem_unlock(sma);
  548. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  549. if(sem_io == NULL) {
  550. ipc_lock_by_ptr(&sma->sem_perm);
  551. ipc_rcu_putref(sma);
  552. sem_unlock(sma);
  553. return -ENOMEM;
  554. }
  555. ipc_lock_by_ptr(&sma->sem_perm);
  556. ipc_rcu_putref(sma);
  557. if (sma->sem_perm.deleted) {
  558. sem_unlock(sma);
  559. err = -EIDRM;
  560. goto out_free;
  561. }
  562. }
  563. for (i = 0; i < sma->sem_nsems; i++)
  564. sem_io[i] = sma->sem_base[i].semval;
  565. sem_unlock(sma);
  566. err = 0;
  567. if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
  568. err = -EFAULT;
  569. goto out_free;
  570. }
  571. case SETALL:
  572. {
  573. int i;
  574. struct sem_undo *un;
  575. ipc_rcu_getref(sma);
  576. sem_unlock(sma);
  577. if(nsems > SEMMSL_FAST) {
  578. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  579. if(sem_io == NULL) {
  580. ipc_lock_by_ptr(&sma->sem_perm);
  581. ipc_rcu_putref(sma);
  582. sem_unlock(sma);
  583. return -ENOMEM;
  584. }
  585. }
  586. if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
  587. ipc_lock_by_ptr(&sma->sem_perm);
  588. ipc_rcu_putref(sma);
  589. sem_unlock(sma);
  590. err = -EFAULT;
  591. goto out_free;
  592. }
  593. for (i = 0; i < nsems; i++) {
  594. if (sem_io[i] > SEMVMX) {
  595. ipc_lock_by_ptr(&sma->sem_perm);
  596. ipc_rcu_putref(sma);
  597. sem_unlock(sma);
  598. err = -ERANGE;
  599. goto out_free;
  600. }
  601. }
  602. ipc_lock_by_ptr(&sma->sem_perm);
  603. ipc_rcu_putref(sma);
  604. if (sma->sem_perm.deleted) {
  605. sem_unlock(sma);
  606. err = -EIDRM;
  607. goto out_free;
  608. }
  609. for (i = 0; i < nsems; i++)
  610. sma->sem_base[i].semval = sem_io[i];
  611. for (un = sma->undo; un; un = un->id_next)
  612. for (i = 0; i < nsems; i++)
  613. un->semadj[i] = 0;
  614. sma->sem_ctime = get_seconds();
  615. /* maybe some queued-up processes were waiting for this */
  616. update_queue(sma);
  617. err = 0;
  618. goto out_unlock;
  619. }
  620. case IPC_STAT:
  621. {
  622. struct semid64_ds tbuf;
  623. memset(&tbuf,0,sizeof(tbuf));
  624. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  625. tbuf.sem_otime = sma->sem_otime;
  626. tbuf.sem_ctime = sma->sem_ctime;
  627. tbuf.sem_nsems = sma->sem_nsems;
  628. sem_unlock(sma);
  629. if (copy_semid_to_user (arg.buf, &tbuf, version))
  630. return -EFAULT;
  631. return 0;
  632. }
  633. /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
  634. }
  635. err = -EINVAL;
  636. if(semnum < 0 || semnum >= nsems)
  637. goto out_unlock;
  638. curr = &sma->sem_base[semnum];
  639. switch (cmd) {
  640. case GETVAL:
  641. err = curr->semval;
  642. goto out_unlock;
  643. case GETPID:
  644. err = curr->sempid;
  645. goto out_unlock;
  646. case GETNCNT:
  647. err = count_semncnt(sma,semnum);
  648. goto out_unlock;
  649. case GETZCNT:
  650. err = count_semzcnt(sma,semnum);
  651. goto out_unlock;
  652. case SETVAL:
  653. {
  654. int val = arg.val;
  655. struct sem_undo *un;
  656. err = -ERANGE;
  657. if (val > SEMVMX || val < 0)
  658. goto out_unlock;
  659. for (un = sma->undo; un; un = un->id_next)
  660. un->semadj[semnum] = 0;
  661. curr->semval = val;
  662. curr->sempid = current->tgid;
  663. sma->sem_ctime = get_seconds();
  664. /* maybe some queued-up processes were waiting for this */
  665. update_queue(sma);
  666. err = 0;
  667. goto out_unlock;
  668. }
  669. }
  670. out_unlock:
  671. sem_unlock(sma);
  672. out_free:
  673. if(sem_io != fast_sem_io)
  674. ipc_free(sem_io, sizeof(ushort)*nsems);
  675. return err;
  676. }
  677. struct sem_setbuf {
  678. uid_t uid;
  679. gid_t gid;
  680. mode_t mode;
  681. };
  682. static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
  683. {
  684. switch(version) {
  685. case IPC_64:
  686. {
  687. struct semid64_ds tbuf;
  688. if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
  689. return -EFAULT;
  690. out->uid = tbuf.sem_perm.uid;
  691. out->gid = tbuf.sem_perm.gid;
  692. out->mode = tbuf.sem_perm.mode;
  693. return 0;
  694. }
  695. case IPC_OLD:
  696. {
  697. struct semid_ds tbuf_old;
  698. if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  699. return -EFAULT;
  700. out->uid = tbuf_old.sem_perm.uid;
  701. out->gid = tbuf_old.sem_perm.gid;
  702. out->mode = tbuf_old.sem_perm.mode;
  703. return 0;
  704. }
  705. default:
  706. return -EINVAL;
  707. }
  708. }
  709. static int semctl_down(int semid, int semnum, int cmd, int version, union semun arg)
  710. {
  711. struct sem_array *sma;
  712. int err;
  713. struct sem_setbuf setbuf;
  714. struct kern_ipc_perm *ipcp;
  715. if(cmd == IPC_SET) {
  716. if(copy_semid_from_user (&setbuf, arg.buf, version))
  717. return -EFAULT;
  718. if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode)))
  719. return err;
  720. }
  721. sma = sem_lock(semid);
  722. if(sma==NULL)
  723. return -EINVAL;
  724. if (sem_checkid(sma,semid)) {
  725. err=-EIDRM;
  726. goto out_unlock;
  727. }
  728. ipcp = &sma->sem_perm;
  729. if (current->euid != ipcp->cuid &&
  730. current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
  731. err=-EPERM;
  732. goto out_unlock;
  733. }
  734. err = security_sem_semctl(sma, cmd);
  735. if (err)
  736. goto out_unlock;
  737. switch(cmd){
  738. case IPC_RMID:
  739. freeary(sma, semid);
  740. err = 0;
  741. break;
  742. case IPC_SET:
  743. ipcp->uid = setbuf.uid;
  744. ipcp->gid = setbuf.gid;
  745. ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
  746. | (setbuf.mode & S_IRWXUGO);
  747. sma->sem_ctime = get_seconds();
  748. sem_unlock(sma);
  749. err = 0;
  750. break;
  751. default:
  752. sem_unlock(sma);
  753. err = -EINVAL;
  754. break;
  755. }
  756. return err;
  757. out_unlock:
  758. sem_unlock(sma);
  759. return err;
  760. }
  761. asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
  762. {
  763. int err = -EINVAL;
  764. int version;
  765. if (semid < 0)
  766. return -EINVAL;
  767. version = ipc_parse_version(&cmd);
  768. switch(cmd) {
  769. case IPC_INFO:
  770. case SEM_INFO:
  771. case SEM_STAT:
  772. err = semctl_nolock(semid,semnum,cmd,version,arg);
  773. return err;
  774. case GETALL:
  775. case GETVAL:
  776. case GETPID:
  777. case GETNCNT:
  778. case GETZCNT:
  779. case IPC_STAT:
  780. case SETVAL:
  781. case SETALL:
  782. err = semctl_main(semid,semnum,cmd,version,arg);
  783. return err;
  784. case IPC_RMID:
  785. case IPC_SET:
  786. down(&sem_ids.sem);
  787. err = semctl_down(semid,semnum,cmd,version,arg);
  788. up(&sem_ids.sem);
  789. return err;
  790. default:
  791. return -EINVAL;
  792. }
  793. }
  794. static inline void lock_semundo(void)
  795. {
  796. struct sem_undo_list *undo_list;
  797. undo_list = current->sysvsem.undo_list;
  798. if (undo_list)
  799. spin_lock(&undo_list->lock);
  800. }
  801. /* This code has an interaction with copy_semundo().
  802. * Consider; two tasks are sharing the undo_list. task1
  803. * acquires the undo_list lock in lock_semundo(). If task2 now
  804. * exits before task1 releases the lock (by calling
  805. * unlock_semundo()), then task1 will never call spin_unlock().
  806. * This leave the sem_undo_list in a locked state. If task1 now creats task3
  807. * and once again shares the sem_undo_list, the sem_undo_list will still be
  808. * locked, and future SEM_UNDO operations will deadlock. This case is
  809. * dealt with in copy_semundo() by having it reinitialize the spin lock when
  810. * the refcnt goes from 1 to 2.
  811. */
  812. static inline void unlock_semundo(void)
  813. {
  814. struct sem_undo_list *undo_list;
  815. undo_list = current->sysvsem.undo_list;
  816. if (undo_list)
  817. spin_unlock(&undo_list->lock);
  818. }
  819. /* If the task doesn't already have a undo_list, then allocate one
  820. * here. We guarantee there is only one thread using this undo list,
  821. * and current is THE ONE
  822. *
  823. * If this allocation and assignment succeeds, but later
  824. * portions of this code fail, there is no need to free the sem_undo_list.
  825. * Just let it stay associated with the task, and it'll be freed later
  826. * at exit time.
  827. *
  828. * This can block, so callers must hold no locks.
  829. */
  830. static inline int get_undo_list(struct sem_undo_list **undo_listp)
  831. {
  832. struct sem_undo_list *undo_list;
  833. int size;
  834. undo_list = current->sysvsem.undo_list;
  835. if (!undo_list) {
  836. size = sizeof(struct sem_undo_list);
  837. undo_list = (struct sem_undo_list *) kmalloc(size, GFP_KERNEL);
  838. if (undo_list == NULL)
  839. return -ENOMEM;
  840. memset(undo_list, 0, size);
  841. spin_lock_init(&undo_list->lock);
  842. atomic_set(&undo_list->refcnt, 1);
  843. current->sysvsem.undo_list = undo_list;
  844. }
  845. *undo_listp = undo_list;
  846. return 0;
  847. }
  848. static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
  849. {
  850. struct sem_undo **last, *un;
  851. last = &ulp->proc_list;
  852. un = *last;
  853. while(un != NULL) {
  854. if(un->semid==semid)
  855. break;
  856. if(un->semid==-1) {
  857. *last=un->proc_next;
  858. kfree(un);
  859. } else {
  860. last=&un->proc_next;
  861. }
  862. un=*last;
  863. }
  864. return un;
  865. }
  866. static struct sem_undo *find_undo(int semid)
  867. {
  868. struct sem_array *sma;
  869. struct sem_undo_list *ulp;
  870. struct sem_undo *un, *new;
  871. int nsems;
  872. int error;
  873. error = get_undo_list(&ulp);
  874. if (error)
  875. return ERR_PTR(error);
  876. lock_semundo();
  877. un = lookup_undo(ulp, semid);
  878. unlock_semundo();
  879. if (likely(un!=NULL))
  880. goto out;
  881. /* no undo structure around - allocate one. */
  882. sma = sem_lock(semid);
  883. un = ERR_PTR(-EINVAL);
  884. if(sma==NULL)
  885. goto out;
  886. un = ERR_PTR(-EIDRM);
  887. if (sem_checkid(sma,semid)) {
  888. sem_unlock(sma);
  889. goto out;
  890. }
  891. nsems = sma->sem_nsems;
  892. ipc_rcu_getref(sma);
  893. sem_unlock(sma);
  894. new = (struct sem_undo *) kmalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
  895. if (!new) {
  896. ipc_lock_by_ptr(&sma->sem_perm);
  897. ipc_rcu_putref(sma);
  898. sem_unlock(sma);
  899. return ERR_PTR(-ENOMEM);
  900. }
  901. memset(new, 0, sizeof(struct sem_undo) + sizeof(short)*nsems);
  902. new->semadj = (short *) &new[1];
  903. new->semid = semid;
  904. lock_semundo();
  905. un = lookup_undo(ulp, semid);
  906. if (un) {
  907. unlock_semundo();
  908. kfree(new);
  909. ipc_lock_by_ptr(&sma->sem_perm);
  910. ipc_rcu_putref(sma);
  911. sem_unlock(sma);
  912. goto out;
  913. }
  914. ipc_lock_by_ptr(&sma->sem_perm);
  915. ipc_rcu_putref(sma);
  916. if (sma->sem_perm.deleted) {
  917. sem_unlock(sma);
  918. unlock_semundo();
  919. kfree(new);
  920. un = ERR_PTR(-EIDRM);
  921. goto out;
  922. }
  923. new->proc_next = ulp->proc_list;
  924. ulp->proc_list = new;
  925. new->id_next = sma->undo;
  926. sma->undo = new;
  927. sem_unlock(sma);
  928. un = new;
  929. unlock_semundo();
  930. out:
  931. return un;
  932. }
  933. asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
  934. unsigned nsops, const struct timespec __user *timeout)
  935. {
  936. int error = -EINVAL;
  937. struct sem_array *sma;
  938. struct sembuf fast_sops[SEMOPM_FAST];
  939. struct sembuf* sops = fast_sops, *sop;
  940. struct sem_undo *un;
  941. int undos = 0, alter = 0, max;
  942. struct sem_queue queue;
  943. unsigned long jiffies_left = 0;
  944. if (nsops < 1 || semid < 0)
  945. return -EINVAL;
  946. if (nsops > sc_semopm)
  947. return -E2BIG;
  948. if(nsops > SEMOPM_FAST) {
  949. sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
  950. if(sops==NULL)
  951. return -ENOMEM;
  952. }
  953. if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
  954. error=-EFAULT;
  955. goto out_free;
  956. }
  957. if (timeout) {
  958. struct timespec _timeout;
  959. if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
  960. error = -EFAULT;
  961. goto out_free;
  962. }
  963. if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
  964. _timeout.tv_nsec >= 1000000000L) {
  965. error = -EINVAL;
  966. goto out_free;
  967. }
  968. jiffies_left = timespec_to_jiffies(&_timeout);
  969. }
  970. max = 0;
  971. for (sop = sops; sop < sops + nsops; sop++) {
  972. if (sop->sem_num >= max)
  973. max = sop->sem_num;
  974. if (sop->sem_flg & SEM_UNDO)
  975. undos = 1;
  976. if (sop->sem_op != 0)
  977. alter = 1;
  978. }
  979. retry_undos:
  980. if (undos) {
  981. un = find_undo(semid);
  982. if (IS_ERR(un)) {
  983. error = PTR_ERR(un);
  984. goto out_free;
  985. }
  986. } else
  987. un = NULL;
  988. sma = sem_lock(semid);
  989. error=-EINVAL;
  990. if(sma==NULL)
  991. goto out_free;
  992. error = -EIDRM;
  993. if (sem_checkid(sma,semid))
  994. goto out_unlock_free;
  995. /*
  996. * semid identifies are not unique - find_undo may have
  997. * allocated an undo structure, it was invalidated by an RMID
  998. * and now a new array with received the same id. Check and retry.
  999. */
  1000. if (un && un->semid == -1) {
  1001. sem_unlock(sma);
  1002. goto retry_undos;
  1003. }
  1004. error = -EFBIG;
  1005. if (max >= sma->sem_nsems)
  1006. goto out_unlock_free;
  1007. error = -EACCES;
  1008. if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
  1009. goto out_unlock_free;
  1010. error = security_sem_semop(sma, sops, nsops, alter);
  1011. if (error)
  1012. goto out_unlock_free;
  1013. error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
  1014. if (error <= 0) {
  1015. if (alter && error == 0)
  1016. update_queue (sma);
  1017. goto out_unlock_free;
  1018. }
  1019. /* We need to sleep on this operation, so we put the current
  1020. * task into the pending queue and go to sleep.
  1021. */
  1022. queue.sma = sma;
  1023. queue.sops = sops;
  1024. queue.nsops = nsops;
  1025. queue.undo = un;
  1026. queue.pid = current->tgid;
  1027. queue.id = semid;
  1028. queue.alter = alter;
  1029. if (alter)
  1030. append_to_queue(sma ,&queue);
  1031. else
  1032. prepend_to_queue(sma ,&queue);
  1033. queue.status = -EINTR;
  1034. queue.sleeper = current;
  1035. current->state = TASK_INTERRUPTIBLE;
  1036. sem_unlock(sma);
  1037. if (timeout)
  1038. jiffies_left = schedule_timeout(jiffies_left);
  1039. else
  1040. schedule();
  1041. error = queue.status;
  1042. while(unlikely(error == IN_WAKEUP)) {
  1043. cpu_relax();
  1044. error = queue.status;
  1045. }
  1046. if (error != -EINTR) {
  1047. /* fast path: update_queue already obtained all requested
  1048. * resources */
  1049. goto out_free;
  1050. }
  1051. sma = sem_lock(semid);
  1052. if(sma==NULL) {
  1053. if(queue.prev != NULL)
  1054. BUG();
  1055. error = -EIDRM;
  1056. goto out_free;
  1057. }
  1058. /*
  1059. * If queue.status != -EINTR we are woken up by another process
  1060. */
  1061. error = queue.status;
  1062. if (error != -EINTR) {
  1063. goto out_unlock_free;
  1064. }
  1065. /*
  1066. * If an interrupt occurred we have to clean up the queue
  1067. */
  1068. if (timeout && jiffies_left == 0)
  1069. error = -EAGAIN;
  1070. remove_from_queue(sma,&queue);
  1071. goto out_unlock_free;
  1072. out_unlock_free:
  1073. sem_unlock(sma);
  1074. out_free:
  1075. if(sops != fast_sops)
  1076. kfree(sops);
  1077. return error;
  1078. }
  1079. asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
  1080. {
  1081. return sys_semtimedop(semid, tsops, nsops, NULL);
  1082. }
  1083. /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
  1084. * parent and child tasks.
  1085. *
  1086. * See the notes above unlock_semundo() regarding the spin_lock_init()
  1087. * in this code. Initialize the undo_list->lock here instead of get_undo_list()
  1088. * because of the reasoning in the comment above unlock_semundo.
  1089. */
  1090. int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
  1091. {
  1092. struct sem_undo_list *undo_list;
  1093. int error;
  1094. if (clone_flags & CLONE_SYSVSEM) {
  1095. error = get_undo_list(&undo_list);
  1096. if (error)
  1097. return error;
  1098. atomic_inc(&undo_list->refcnt);
  1099. tsk->sysvsem.undo_list = undo_list;
  1100. } else
  1101. tsk->sysvsem.undo_list = NULL;
  1102. return 0;
  1103. }
  1104. /*
  1105. * add semadj values to semaphores, free undo structures.
  1106. * undo structures are not freed when semaphore arrays are destroyed
  1107. * so some of them may be out of date.
  1108. * IMPLEMENTATION NOTE: There is some confusion over whether the
  1109. * set of adjustments that needs to be done should be done in an atomic
  1110. * manner or not. That is, if we are attempting to decrement the semval
  1111. * should we queue up and wait until we can do so legally?
  1112. * The original implementation attempted to do this (queue and wait).
  1113. * The current implementation does not do so. The POSIX standard
  1114. * and SVID should be consulted to determine what behavior is mandated.
  1115. */
  1116. void exit_sem(struct task_struct *tsk)
  1117. {
  1118. struct sem_undo_list *undo_list;
  1119. struct sem_undo *u, **up;
  1120. undo_list = tsk->sysvsem.undo_list;
  1121. if (!undo_list)
  1122. return;
  1123. if (!atomic_dec_and_test(&undo_list->refcnt))
  1124. return;
  1125. /* There's no need to hold the semundo list lock, as current
  1126. * is the last task exiting for this undo list.
  1127. */
  1128. for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
  1129. struct sem_array *sma;
  1130. int nsems, i;
  1131. struct sem_undo *un, **unp;
  1132. int semid;
  1133. semid = u->semid;
  1134. if(semid == -1)
  1135. continue;
  1136. sma = sem_lock(semid);
  1137. if (sma == NULL)
  1138. continue;
  1139. if (u->semid == -1)
  1140. goto next_entry;
  1141. BUG_ON(sem_checkid(sma,u->semid));
  1142. /* remove u from the sma->undo list */
  1143. for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
  1144. if (u == un)
  1145. goto found;
  1146. }
  1147. printk ("exit_sem undo list error id=%d\n", u->semid);
  1148. goto next_entry;
  1149. found:
  1150. *unp = un->id_next;
  1151. /* perform adjustments registered in u */
  1152. nsems = sma->sem_nsems;
  1153. for (i = 0; i < nsems; i++) {
  1154. struct sem * sem = &sma->sem_base[i];
  1155. if (u->semadj[i]) {
  1156. sem->semval += u->semadj[i];
  1157. /*
  1158. * Range checks of the new semaphore value,
  1159. * not defined by sus:
  1160. * - Some unices ignore the undo entirely
  1161. * (e.g. HP UX 11i 11.22, Tru64 V5.1)
  1162. * - some cap the value (e.g. FreeBSD caps
  1163. * at 0, but doesn't enforce SEMVMX)
  1164. *
  1165. * Linux caps the semaphore value, both at 0
  1166. * and at SEMVMX.
  1167. *
  1168. * Manfred <manfred@colorfullife.com>
  1169. */
  1170. if (sem->semval < 0)
  1171. sem->semval = 0;
  1172. if (sem->semval > SEMVMX)
  1173. sem->semval = SEMVMX;
  1174. sem->sempid = current->tgid;
  1175. }
  1176. }
  1177. sma->sem_otime = get_seconds();
  1178. /* maybe some queued-up processes were waiting for this */
  1179. update_queue(sma);
  1180. next_entry:
  1181. sem_unlock(sma);
  1182. }
  1183. kfree(undo_list);
  1184. }
  1185. #ifdef CONFIG_PROC_FS
  1186. static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
  1187. {
  1188. struct sem_array *sma = it;
  1189. return seq_printf(s,
  1190. "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
  1191. sma->sem_perm.key,
  1192. sma->sem_id,
  1193. sma->sem_perm.mode,
  1194. sma->sem_nsems,
  1195. sma->sem_perm.uid,
  1196. sma->sem_perm.gid,
  1197. sma->sem_perm.cuid,
  1198. sma->sem_perm.cgid,
  1199. sma->sem_otime,
  1200. sma->sem_ctime);
  1201. }
  1202. #endif