sem.c 34 KB

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