sem.c 34 KB

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