sem.c 36 KB

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