sem.c 33 KB

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