sem.c 34 KB

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