sem.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. /*
  2. * linux/ipc/sem.c
  3. * Copyright (C) 1992 Krishna Balasubramanian
  4. * Copyright (C) 1995 Eric Schenk, Bruno Haible
  5. *
  6. * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  7. *
  8. * SMP-threaded, sysctl's added
  9. * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
  10. * Enforced range limit on SEM_UNDO
  11. * (c) 2001 Red Hat Inc
  12. * Lockless wakeup
  13. * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
  14. * Further wakeup optimizations, documentation
  15. * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
  16. *
  17. * support for audit of ipc object properties and permission changes
  18. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  19. *
  20. * namespaces support
  21. * OpenVZ, SWsoft Inc.
  22. * Pavel Emelianov <xemul@openvz.org>
  23. *
  24. * Implementation notes: (May 2010)
  25. * This file implements System V semaphores.
  26. *
  27. * User space visible behavior:
  28. * - FIFO ordering for semop() operations (just FIFO, not starvation
  29. * protection)
  30. * - multiple semaphore operations that alter the same semaphore in
  31. * one semop() are handled.
  32. * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
  33. * SETALL calls.
  34. * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
  35. * - undo adjustments at process exit are limited to 0..SEMVMX.
  36. * - namespace are supported.
  37. * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
  38. * to /proc/sys/kernel/sem.
  39. * - statistics about the usage are reported in /proc/sysvipc/sem.
  40. *
  41. * Internals:
  42. * - scalability:
  43. * - all global variables are read-mostly.
  44. * - semop() calls and semctl(RMID) are synchronized by RCU.
  45. * - most operations do write operations (actually: spin_lock calls) to
  46. * the per-semaphore array structure.
  47. * Thus: Perfect SMP scaling between independent semaphore arrays.
  48. * If multiple semaphores in one array are used, then cache line
  49. * trashing on the semaphore array spinlock will limit the scaling.
  50. * - semncnt and semzcnt are calculated on demand in count_semncnt() and
  51. * count_semzcnt()
  52. * - the task that performs a successful semop() scans the list of all
  53. * sleeping tasks and completes any pending operations that can be fulfilled.
  54. * Semaphores are actively given to waiting tasks (necessary for FIFO).
  55. * (see update_queue())
  56. * - To improve the scalability, the actual wake-up calls are performed after
  57. * dropping all locks. (see wake_up_sem_queue_prepare(),
  58. * wake_up_sem_queue_do())
  59. * - All work is done by the waker, the woken up task does not have to do
  60. * anything - not even acquiring a lock or dropping a refcount.
  61. * - A woken up task may not even touch the semaphore array anymore, it may
  62. * have been destroyed already by a semctl(RMID).
  63. * - The synchronizations between wake-ups due to a timeout/signal and a
  64. * wake-up due to a completed semaphore operation is achieved by using an
  65. * intermediate state (IN_WAKEUP).
  66. * - UNDO values are stored in an array (one per process and per
  67. * semaphore array, lazily allocated). For backwards compatibility, multiple
  68. * modes for the UNDO variables are supported (per process, per thread)
  69. * (see copy_semundo, CLONE_SYSVSEM)
  70. * - There are two lists of the pending operations: a per-array list
  71. * and per-semaphore list (stored in the array). This allows to achieve FIFO
  72. * ordering without always scanning all pending operations.
  73. * The worst-case behavior is nevertheless O(N^2) for N wakeups.
  74. */
  75. #include <linux/slab.h>
  76. #include <linux/spinlock.h>
  77. #include <linux/init.h>
  78. #include <linux/proc_fs.h>
  79. #include <linux/time.h>
  80. #include <linux/security.h>
  81. #include <linux/syscalls.h>
  82. #include <linux/audit.h>
  83. #include <linux/capability.h>
  84. #include <linux/seq_file.h>
  85. #include <linux/rwsem.h>
  86. #include <linux/nsproxy.h>
  87. #include <linux/ipc_namespace.h>
  88. #include <asm/uaccess.h>
  89. #include "util.h"
  90. #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
  91. #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
  92. #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
  93. static int newary(struct ipc_namespace *, struct ipc_params *);
  94. static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
  95. #ifdef CONFIG_PROC_FS
  96. static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
  97. #endif
  98. #define SEMMSL_FAST 256 /* 512 bytes on stack */
  99. #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
  100. /*
  101. * linked list protection:
  102. * sem_undo.id_next,
  103. * sem_array.sem_pending{,last},
  104. * sem_array.sem_undo: sem_lock() for read/write
  105. * sem_undo.proc_next: only "current" is allowed to read/write that field.
  106. *
  107. */
  108. #define sc_semmsl sem_ctls[0]
  109. #define sc_semmns sem_ctls[1]
  110. #define sc_semopm sem_ctls[2]
  111. #define sc_semmni sem_ctls[3]
  112. void sem_init_ns(struct ipc_namespace *ns)
  113. {
  114. ns->sc_semmsl = SEMMSL;
  115. ns->sc_semmns = SEMMNS;
  116. ns->sc_semopm = SEMOPM;
  117. ns->sc_semmni = SEMMNI;
  118. ns->used_sems = 0;
  119. ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
  120. }
  121. #ifdef CONFIG_IPC_NS
  122. void sem_exit_ns(struct ipc_namespace *ns)
  123. {
  124. free_ipcs(ns, &sem_ids(ns), freeary);
  125. idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
  126. }
  127. #endif
  128. void __init sem_init (void)
  129. {
  130. sem_init_ns(&init_ipc_ns);
  131. ipc_init_proc_interface("sysvipc/sem",
  132. " key semid perms nsems uid gid cuid cgid otime ctime\n",
  133. IPC_SEM_IDS, sysvipc_sem_proc_show);
  134. }
  135. /*
  136. * sem_lock_(check_) routines are called in the paths where the rw_mutex
  137. * is not held.
  138. */
  139. static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
  140. {
  141. struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
  142. if (IS_ERR(ipcp))
  143. return (struct sem_array *)ipcp;
  144. return container_of(ipcp, struct sem_array, sem_perm);
  145. }
  146. static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
  147. int id)
  148. {
  149. struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
  150. if (IS_ERR(ipcp))
  151. return (struct sem_array *)ipcp;
  152. return container_of(ipcp, struct sem_array, sem_perm);
  153. }
  154. static inline void sem_lock_and_putref(struct sem_array *sma)
  155. {
  156. ipc_lock_by_ptr(&sma->sem_perm);
  157. ipc_rcu_putref(sma);
  158. }
  159. static inline void sem_getref_and_unlock(struct sem_array *sma)
  160. {
  161. ipc_rcu_getref(sma);
  162. ipc_unlock(&(sma)->sem_perm);
  163. }
  164. static inline void sem_putref(struct sem_array *sma)
  165. {
  166. ipc_lock_by_ptr(&sma->sem_perm);
  167. ipc_rcu_putref(sma);
  168. ipc_unlock(&(sma)->sem_perm);
  169. }
  170. static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
  171. {
  172. ipc_rmid(&sem_ids(ns), &s->sem_perm);
  173. }
  174. /*
  175. * Lockless wakeup algorithm:
  176. * Without the check/retry algorithm a lockless wakeup is possible:
  177. * - queue.status is initialized to -EINTR before blocking.
  178. * - wakeup is performed by
  179. * * unlinking the queue entry from sma->sem_pending
  180. * * setting queue.status to IN_WAKEUP
  181. * This is the notification for the blocked thread that a
  182. * result value is imminent.
  183. * * call wake_up_process
  184. * * set queue.status to the final value.
  185. * - the previously blocked thread checks queue.status:
  186. * * if it's IN_WAKEUP, then it must wait until the value changes
  187. * * if it's not -EINTR, then the operation was completed by
  188. * update_queue. semtimedop can return queue.status without
  189. * performing any operation on the sem array.
  190. * * otherwise it must acquire the spinlock and check what's up.
  191. *
  192. * The two-stage algorithm is necessary to protect against the following
  193. * races:
  194. * - if queue.status is set after wake_up_process, then the woken up idle
  195. * thread could race forward and try (and fail) to acquire sma->lock
  196. * before update_queue had a chance to set queue.status
  197. * - if queue.status is written before wake_up_process and if the
  198. * blocked process is woken up by a signal between writing
  199. * queue.status and the wake_up_process, then the woken up
  200. * process could return from semtimedop and die by calling
  201. * sys_exit before wake_up_process is called. Then wake_up_process
  202. * will oops, because the task structure is already invalid.
  203. * (yes, this happened on s390 with sysv msg).
  204. *
  205. */
  206. #define IN_WAKEUP 1
  207. /**
  208. * newary - Create a new semaphore set
  209. * @ns: namespace
  210. * @params: ptr to the structure that contains key, semflg and nsems
  211. *
  212. * Called with sem_ids.rw_mutex held (as a writer)
  213. */
  214. static int newary(struct ipc_namespace *ns, struct ipc_params *params)
  215. {
  216. int id;
  217. int retval;
  218. struct sem_array *sma;
  219. int size;
  220. key_t key = params->key;
  221. int nsems = params->u.nsems;
  222. int semflg = params->flg;
  223. int i;
  224. if (!nsems)
  225. return -EINVAL;
  226. if (ns->used_sems + nsems > ns->sc_semmns)
  227. return -ENOSPC;
  228. size = sizeof (*sma) + nsems * sizeof (struct sem);
  229. sma = ipc_rcu_alloc(size);
  230. if (!sma) {
  231. return -ENOMEM;
  232. }
  233. memset (sma, 0, size);
  234. sma->sem_perm.mode = (semflg & S_IRWXUGO);
  235. sma->sem_perm.key = key;
  236. sma->sem_perm.security = NULL;
  237. retval = security_sem_alloc(sma);
  238. if (retval) {
  239. ipc_rcu_putref(sma);
  240. return retval;
  241. }
  242. id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
  243. if (id < 0) {
  244. security_sem_free(sma);
  245. ipc_rcu_putref(sma);
  246. return id;
  247. }
  248. ns->used_sems += nsems;
  249. sma->sem_base = (struct sem *) &sma[1];
  250. for (i = 0; i < nsems; i++)
  251. INIT_LIST_HEAD(&sma->sem_base[i].sem_pending);
  252. sma->complex_count = 0;
  253. INIT_LIST_HEAD(&sma->sem_pending);
  254. INIT_LIST_HEAD(&sma->list_id);
  255. sma->sem_nsems = nsems;
  256. sma->sem_ctime = get_seconds();
  257. sem_unlock(sma);
  258. return sma->sem_perm.id;
  259. }
  260. /*
  261. * Called with sem_ids.rw_mutex and ipcp locked.
  262. */
  263. static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
  264. {
  265. struct sem_array *sma;
  266. sma = container_of(ipcp, struct sem_array, sem_perm);
  267. return security_sem_associate(sma, semflg);
  268. }
  269. /*
  270. * Called with sem_ids.rw_mutex and ipcp locked.
  271. */
  272. static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
  273. struct ipc_params *params)
  274. {
  275. struct sem_array *sma;
  276. sma = container_of(ipcp, struct sem_array, sem_perm);
  277. if (params->u.nsems > sma->sem_nsems)
  278. return -EINVAL;
  279. return 0;
  280. }
  281. SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
  282. {
  283. struct ipc_namespace *ns;
  284. struct ipc_ops sem_ops;
  285. struct ipc_params sem_params;
  286. ns = current->nsproxy->ipc_ns;
  287. if (nsems < 0 || nsems > ns->sc_semmsl)
  288. return -EINVAL;
  289. sem_ops.getnew = newary;
  290. sem_ops.associate = sem_security;
  291. sem_ops.more_checks = sem_more_checks;
  292. sem_params.key = key;
  293. sem_params.flg = semflg;
  294. sem_params.u.nsems = nsems;
  295. return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
  296. }
  297. /*
  298. * Determine whether a sequence of semaphore operations would succeed
  299. * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
  300. */
  301. static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
  302. int nsops, struct sem_undo *un, int pid)
  303. {
  304. int result, sem_op;
  305. struct sembuf *sop;
  306. struct sem * curr;
  307. for (sop = sops; sop < sops + nsops; sop++) {
  308. curr = sma->sem_base + sop->sem_num;
  309. sem_op = sop->sem_op;
  310. result = curr->semval;
  311. if (!sem_op && result)
  312. goto would_block;
  313. result += sem_op;
  314. if (result < 0)
  315. goto would_block;
  316. if (result > SEMVMX)
  317. goto out_of_range;
  318. if (sop->sem_flg & SEM_UNDO) {
  319. int undo = un->semadj[sop->sem_num] - sem_op;
  320. /*
  321. * Exceeding the undo range is an error.
  322. */
  323. if (undo < (-SEMAEM - 1) || undo > SEMAEM)
  324. goto out_of_range;
  325. }
  326. curr->semval = result;
  327. }
  328. sop--;
  329. while (sop >= sops) {
  330. sma->sem_base[sop->sem_num].sempid = pid;
  331. if (sop->sem_flg & SEM_UNDO)
  332. un->semadj[sop->sem_num] -= sop->sem_op;
  333. sop--;
  334. }
  335. return 0;
  336. out_of_range:
  337. result = -ERANGE;
  338. goto undo;
  339. would_block:
  340. if (sop->sem_flg & IPC_NOWAIT)
  341. result = -EAGAIN;
  342. else
  343. result = 1;
  344. undo:
  345. sop--;
  346. while (sop >= sops) {
  347. sma->sem_base[sop->sem_num].semval -= sop->sem_op;
  348. sop--;
  349. }
  350. return result;
  351. }
  352. /** wake_up_sem_queue_prepare(q, error): Prepare wake-up
  353. * @q: queue entry that must be signaled
  354. * @error: Error value for the signal
  355. *
  356. * Prepare the wake-up of the queue entry q.
  357. */
  358. static void wake_up_sem_queue_prepare(struct list_head *pt,
  359. struct sem_queue *q, int error)
  360. {
  361. if (list_empty(pt)) {
  362. /*
  363. * Hold preempt off so that we don't get preempted and have the
  364. * wakee busy-wait until we're scheduled back on.
  365. */
  366. preempt_disable();
  367. }
  368. q->status = IN_WAKEUP;
  369. q->pid = error;
  370. list_add_tail(&q->simple_list, pt);
  371. }
  372. /**
  373. * wake_up_sem_queue_do(pt) - do the actual wake-up
  374. * @pt: list of tasks to be woken up
  375. *
  376. * Do the actual wake-up.
  377. * The function is called without any locks held, thus the semaphore array
  378. * could be destroyed already and the tasks can disappear as soon as the
  379. * status is set to the actual return code.
  380. */
  381. static void wake_up_sem_queue_do(struct list_head *pt)
  382. {
  383. struct sem_queue *q, *t;
  384. int did_something;
  385. did_something = !list_empty(pt);
  386. list_for_each_entry_safe(q, t, pt, simple_list) {
  387. wake_up_process(q->sleeper);
  388. /* q can disappear immediately after writing q->status. */
  389. smp_wmb();
  390. q->status = q->pid;
  391. }
  392. if (did_something)
  393. preempt_enable();
  394. }
  395. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  396. {
  397. list_del(&q->list);
  398. if (q->nsops == 1)
  399. list_del(&q->simple_list);
  400. else
  401. sma->complex_count--;
  402. }
  403. /** check_restart(sma, q)
  404. * @sma: semaphore array
  405. * @q: the operation that just completed
  406. *
  407. * update_queue is O(N^2) when it restarts scanning the whole queue of
  408. * waiting operations. Therefore this function checks if the restart is
  409. * really necessary. It is called after a previously waiting operation
  410. * was completed.
  411. */
  412. static int check_restart(struct sem_array *sma, struct sem_queue *q)
  413. {
  414. struct sem *curr;
  415. struct sem_queue *h;
  416. /* if the operation didn't modify the array, then no restart */
  417. if (q->alter == 0)
  418. return 0;
  419. /* pending complex operations are too difficult to analyse */
  420. if (sma->complex_count)
  421. return 1;
  422. /* we were a sleeping complex operation. Too difficult */
  423. if (q->nsops > 1)
  424. return 1;
  425. curr = sma->sem_base + q->sops[0].sem_num;
  426. /* No-one waits on this queue */
  427. if (list_empty(&curr->sem_pending))
  428. return 0;
  429. /* the new semaphore value */
  430. if (curr->semval) {
  431. /* It is impossible that someone waits for the new value:
  432. * - q is a previously sleeping simple operation that
  433. * altered the array. It must be a decrement, because
  434. * simple increments never sleep.
  435. * - The value is not 0, thus wait-for-zero won't proceed.
  436. * - If there are older (higher priority) decrements
  437. * in the queue, then they have observed the original
  438. * semval value and couldn't proceed. The operation
  439. * decremented to value - thus they won't proceed either.
  440. */
  441. BUG_ON(q->sops[0].sem_op >= 0);
  442. return 0;
  443. }
  444. /*
  445. * semval is 0. Check if there are wait-for-zero semops.
  446. * They must be the first entries in the per-semaphore simple queue
  447. */
  448. h = list_first_entry(&curr->sem_pending, struct sem_queue, simple_list);
  449. BUG_ON(h->nsops != 1);
  450. BUG_ON(h->sops[0].sem_num != q->sops[0].sem_num);
  451. /* Yes, there is a wait-for-zero semop. Restart */
  452. if (h->sops[0].sem_op == 0)
  453. return 1;
  454. /* Again - no-one is waiting for the new value. */
  455. return 0;
  456. }
  457. /**
  458. * update_queue(sma, semnum): Look for tasks that can be completed.
  459. * @sma: semaphore array.
  460. * @semnum: semaphore that was modified.
  461. * @pt: list head for the tasks that must be woken up.
  462. *
  463. * update_queue must be called after a semaphore in a semaphore array
  464. * was modified. If multiple semaphore were modified, then @semnum
  465. * must be set to -1.
  466. * The tasks that must be woken up are added to @pt. The return code
  467. * is stored in q->pid.
  468. * The function return 1 if at least one semop was completed successfully.
  469. */
  470. static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
  471. {
  472. struct sem_queue *q;
  473. struct list_head *walk;
  474. struct list_head *pending_list;
  475. int offset;
  476. int semop_completed = 0;
  477. /* if there are complex operations around, then knowing the semaphore
  478. * that was modified doesn't help us. Assume that multiple semaphores
  479. * were modified.
  480. */
  481. if (sma->complex_count)
  482. semnum = -1;
  483. if (semnum == -1) {
  484. pending_list = &sma->sem_pending;
  485. offset = offsetof(struct sem_queue, list);
  486. } else {
  487. pending_list = &sma->sem_base[semnum].sem_pending;
  488. offset = offsetof(struct sem_queue, simple_list);
  489. }
  490. again:
  491. walk = pending_list->next;
  492. while (walk != pending_list) {
  493. int error, restart;
  494. q = (struct sem_queue *)((char *)walk - offset);
  495. walk = walk->next;
  496. /* If we are scanning the single sop, per-semaphore list of
  497. * one semaphore and that semaphore is 0, then it is not
  498. * necessary to scan the "alter" entries: simple increments
  499. * that affect only one entry succeed immediately and cannot
  500. * be in the per semaphore pending queue, and decrements
  501. * cannot be successful if the value is already 0.
  502. */
  503. if (semnum != -1 && sma->sem_base[semnum].semval == 0 &&
  504. q->alter)
  505. break;
  506. error = try_atomic_semop(sma, q->sops, q->nsops,
  507. q->undo, q->pid);
  508. /* Does q->sleeper still need to sleep? */
  509. if (error > 0)
  510. continue;
  511. unlink_queue(sma, q);
  512. if (error) {
  513. restart = 0;
  514. } else {
  515. semop_completed = 1;
  516. restart = check_restart(sma, q);
  517. }
  518. wake_up_sem_queue_prepare(pt, q, error);
  519. if (restart)
  520. goto again;
  521. }
  522. return semop_completed;
  523. }
  524. /**
  525. * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
  526. * @sma: semaphore array
  527. * @sops: operations that were performed
  528. * @nsops: number of operations
  529. * @otime: force setting otime
  530. * @pt: list head of the tasks that must be woken up.
  531. *
  532. * do_smart_update() does the required called to update_queue, based on the
  533. * actual changes that were performed on the semaphore array.
  534. * Note that the function does not do the actual wake-up: the caller is
  535. * responsible for calling wake_up_sem_queue_do(@pt).
  536. * It is safe to perform this call after dropping all locks.
  537. */
  538. static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
  539. int otime, struct list_head *pt)
  540. {
  541. int i;
  542. if (sma->complex_count || sops == NULL) {
  543. if (update_queue(sma, -1, pt))
  544. otime = 1;
  545. goto done;
  546. }
  547. for (i = 0; i < nsops; i++) {
  548. if (sops[i].sem_op > 0 ||
  549. (sops[i].sem_op < 0 &&
  550. sma->sem_base[sops[i].sem_num].semval == 0))
  551. if (update_queue(sma, sops[i].sem_num, pt))
  552. otime = 1;
  553. }
  554. done:
  555. if (otime)
  556. sma->sem_otime = get_seconds();
  557. }
  558. /* The following counts are associated to each semaphore:
  559. * semncnt number of tasks waiting on semval being nonzero
  560. * semzcnt number of tasks waiting on semval being zero
  561. * This model assumes that a task waits on exactly one semaphore.
  562. * Since semaphore operations are to be performed atomically, tasks actually
  563. * wait on a whole sequence of semaphores simultaneously.
  564. * The counts we return here are a rough approximation, but still
  565. * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
  566. */
  567. static int count_semncnt (struct sem_array * sma, ushort semnum)
  568. {
  569. int semncnt;
  570. struct sem_queue * q;
  571. semncnt = 0;
  572. list_for_each_entry(q, &sma->sem_pending, list) {
  573. struct sembuf * sops = q->sops;
  574. int nsops = q->nsops;
  575. int i;
  576. for (i = 0; i < nsops; i++)
  577. if (sops[i].sem_num == semnum
  578. && (sops[i].sem_op < 0)
  579. && !(sops[i].sem_flg & IPC_NOWAIT))
  580. semncnt++;
  581. }
  582. return semncnt;
  583. }
  584. static int count_semzcnt (struct sem_array * sma, ushort semnum)
  585. {
  586. int semzcnt;
  587. struct sem_queue * q;
  588. semzcnt = 0;
  589. list_for_each_entry(q, &sma->sem_pending, list) {
  590. struct sembuf * sops = q->sops;
  591. int nsops = q->nsops;
  592. int i;
  593. for (i = 0; i < nsops; i++)
  594. if (sops[i].sem_num == semnum
  595. && (sops[i].sem_op == 0)
  596. && !(sops[i].sem_flg & IPC_NOWAIT))
  597. semzcnt++;
  598. }
  599. return semzcnt;
  600. }
  601. static void free_un(struct rcu_head *head)
  602. {
  603. struct sem_undo *un = container_of(head, struct sem_undo, rcu);
  604. kfree(un);
  605. }
  606. /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
  607. * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
  608. * remains locked on exit.
  609. */
  610. static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  611. {
  612. struct sem_undo *un, *tu;
  613. struct sem_queue *q, *tq;
  614. struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
  615. struct list_head tasks;
  616. /* Free the existing undo structures for this semaphore set. */
  617. assert_spin_locked(&sma->sem_perm.lock);
  618. list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
  619. list_del(&un->list_id);
  620. spin_lock(&un->ulp->lock);
  621. un->semid = -1;
  622. list_del_rcu(&un->list_proc);
  623. spin_unlock(&un->ulp->lock);
  624. call_rcu(&un->rcu, free_un);
  625. }
  626. /* Wake up all pending processes and let them fail with EIDRM. */
  627. INIT_LIST_HEAD(&tasks);
  628. list_for_each_entry_safe(q, tq, &sma->sem_pending, list) {
  629. unlink_queue(sma, q);
  630. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  631. }
  632. /* Remove the semaphore set from the IDR */
  633. sem_rmid(ns, sma);
  634. sem_unlock(sma);
  635. wake_up_sem_queue_do(&tasks);
  636. ns->used_sems -= sma->sem_nsems;
  637. security_sem_free(sma);
  638. ipc_rcu_putref(sma);
  639. }
  640. static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
  641. {
  642. switch(version) {
  643. case IPC_64:
  644. return copy_to_user(buf, in, sizeof(*in));
  645. case IPC_OLD:
  646. {
  647. struct semid_ds out;
  648. memset(&out, 0, sizeof(out));
  649. ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
  650. out.sem_otime = in->sem_otime;
  651. out.sem_ctime = in->sem_ctime;
  652. out.sem_nsems = in->sem_nsems;
  653. return copy_to_user(buf, &out, sizeof(out));
  654. }
  655. default:
  656. return -EINVAL;
  657. }
  658. }
  659. static int semctl_nolock(struct ipc_namespace *ns, int semid,
  660. int cmd, int version, union semun arg)
  661. {
  662. int err;
  663. struct sem_array *sma;
  664. switch(cmd) {
  665. case IPC_INFO:
  666. case SEM_INFO:
  667. {
  668. struct seminfo seminfo;
  669. int max_id;
  670. err = security_sem_semctl(NULL, cmd);
  671. if (err)
  672. return err;
  673. memset(&seminfo,0,sizeof(seminfo));
  674. seminfo.semmni = ns->sc_semmni;
  675. seminfo.semmns = ns->sc_semmns;
  676. seminfo.semmsl = ns->sc_semmsl;
  677. seminfo.semopm = ns->sc_semopm;
  678. seminfo.semvmx = SEMVMX;
  679. seminfo.semmnu = SEMMNU;
  680. seminfo.semmap = SEMMAP;
  681. seminfo.semume = SEMUME;
  682. down_read(&sem_ids(ns).rw_mutex);
  683. if (cmd == SEM_INFO) {
  684. seminfo.semusz = sem_ids(ns).in_use;
  685. seminfo.semaem = ns->used_sems;
  686. } else {
  687. seminfo.semusz = SEMUSZ;
  688. seminfo.semaem = SEMAEM;
  689. }
  690. max_id = ipc_get_maxid(&sem_ids(ns));
  691. up_read(&sem_ids(ns).rw_mutex);
  692. if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
  693. return -EFAULT;
  694. return (max_id < 0) ? 0: max_id;
  695. }
  696. case IPC_STAT:
  697. case SEM_STAT:
  698. {
  699. struct semid64_ds tbuf;
  700. int id;
  701. if (cmd == SEM_STAT) {
  702. sma = sem_lock(ns, semid);
  703. if (IS_ERR(sma))
  704. return PTR_ERR(sma);
  705. id = sma->sem_perm.id;
  706. } else {
  707. sma = sem_lock_check(ns, semid);
  708. if (IS_ERR(sma))
  709. return PTR_ERR(sma);
  710. id = 0;
  711. }
  712. err = -EACCES;
  713. if (ipcperms (&sma->sem_perm, S_IRUGO))
  714. goto out_unlock;
  715. err = security_sem_semctl(sma, cmd);
  716. if (err)
  717. goto out_unlock;
  718. memset(&tbuf, 0, sizeof(tbuf));
  719. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  720. tbuf.sem_otime = sma->sem_otime;
  721. tbuf.sem_ctime = sma->sem_ctime;
  722. tbuf.sem_nsems = sma->sem_nsems;
  723. sem_unlock(sma);
  724. if (copy_semid_to_user (arg.buf, &tbuf, version))
  725. return -EFAULT;
  726. return id;
  727. }
  728. default:
  729. return -EINVAL;
  730. }
  731. out_unlock:
  732. sem_unlock(sma);
  733. return err;
  734. }
  735. static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
  736. int cmd, int version, union semun arg)
  737. {
  738. struct sem_array *sma;
  739. struct sem* curr;
  740. int err;
  741. ushort fast_sem_io[SEMMSL_FAST];
  742. ushort* sem_io = fast_sem_io;
  743. int nsems;
  744. struct list_head tasks;
  745. sma = sem_lock_check(ns, semid);
  746. if (IS_ERR(sma))
  747. return PTR_ERR(sma);
  748. INIT_LIST_HEAD(&tasks);
  749. nsems = sma->sem_nsems;
  750. err = -EACCES;
  751. if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
  752. goto out_unlock;
  753. err = security_sem_semctl(sma, cmd);
  754. if (err)
  755. goto out_unlock;
  756. err = -EACCES;
  757. switch (cmd) {
  758. case GETALL:
  759. {
  760. ushort __user *array = arg.array;
  761. int i;
  762. if(nsems > SEMMSL_FAST) {
  763. sem_getref_and_unlock(sma);
  764. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  765. if(sem_io == NULL) {
  766. sem_putref(sma);
  767. return -ENOMEM;
  768. }
  769. sem_lock_and_putref(sma);
  770. if (sma->sem_perm.deleted) {
  771. sem_unlock(sma);
  772. err = -EIDRM;
  773. goto out_free;
  774. }
  775. }
  776. for (i = 0; i < sma->sem_nsems; i++)
  777. sem_io[i] = sma->sem_base[i].semval;
  778. sem_unlock(sma);
  779. err = 0;
  780. if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
  781. err = -EFAULT;
  782. goto out_free;
  783. }
  784. case SETALL:
  785. {
  786. int i;
  787. struct sem_undo *un;
  788. sem_getref_and_unlock(sma);
  789. if(nsems > SEMMSL_FAST) {
  790. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  791. if(sem_io == NULL) {
  792. sem_putref(sma);
  793. return -ENOMEM;
  794. }
  795. }
  796. if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
  797. sem_putref(sma);
  798. err = -EFAULT;
  799. goto out_free;
  800. }
  801. for (i = 0; i < nsems; i++) {
  802. if (sem_io[i] > SEMVMX) {
  803. sem_putref(sma);
  804. err = -ERANGE;
  805. goto out_free;
  806. }
  807. }
  808. sem_lock_and_putref(sma);
  809. if (sma->sem_perm.deleted) {
  810. sem_unlock(sma);
  811. err = -EIDRM;
  812. goto out_free;
  813. }
  814. for (i = 0; i < nsems; i++)
  815. sma->sem_base[i].semval = sem_io[i];
  816. assert_spin_locked(&sma->sem_perm.lock);
  817. list_for_each_entry(un, &sma->list_id, list_id) {
  818. for (i = 0; i < nsems; i++)
  819. un->semadj[i] = 0;
  820. }
  821. sma->sem_ctime = get_seconds();
  822. /* maybe some queued-up processes were waiting for this */
  823. do_smart_update(sma, NULL, 0, 0, &tasks);
  824. err = 0;
  825. goto out_unlock;
  826. }
  827. /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
  828. }
  829. err = -EINVAL;
  830. if(semnum < 0 || semnum >= nsems)
  831. goto out_unlock;
  832. curr = &sma->sem_base[semnum];
  833. switch (cmd) {
  834. case GETVAL:
  835. err = curr->semval;
  836. goto out_unlock;
  837. case GETPID:
  838. err = curr->sempid;
  839. goto out_unlock;
  840. case GETNCNT:
  841. err = count_semncnt(sma,semnum);
  842. goto out_unlock;
  843. case GETZCNT:
  844. err = count_semzcnt(sma,semnum);
  845. goto out_unlock;
  846. case SETVAL:
  847. {
  848. int val = arg.val;
  849. struct sem_undo *un;
  850. err = -ERANGE;
  851. if (val > SEMVMX || val < 0)
  852. goto out_unlock;
  853. assert_spin_locked(&sma->sem_perm.lock);
  854. list_for_each_entry(un, &sma->list_id, list_id)
  855. un->semadj[semnum] = 0;
  856. curr->semval = val;
  857. curr->sempid = task_tgid_vnr(current);
  858. sma->sem_ctime = get_seconds();
  859. /* maybe some queued-up processes were waiting for this */
  860. do_smart_update(sma, NULL, 0, 0, &tasks);
  861. err = 0;
  862. goto out_unlock;
  863. }
  864. }
  865. out_unlock:
  866. sem_unlock(sma);
  867. wake_up_sem_queue_do(&tasks);
  868. out_free:
  869. if(sem_io != fast_sem_io)
  870. ipc_free(sem_io, sizeof(ushort)*nsems);
  871. return err;
  872. }
  873. static inline unsigned long
  874. copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
  875. {
  876. switch(version) {
  877. case IPC_64:
  878. if (copy_from_user(out, buf, sizeof(*out)))
  879. return -EFAULT;
  880. return 0;
  881. case IPC_OLD:
  882. {
  883. struct semid_ds tbuf_old;
  884. if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  885. return -EFAULT;
  886. out->sem_perm.uid = tbuf_old.sem_perm.uid;
  887. out->sem_perm.gid = tbuf_old.sem_perm.gid;
  888. out->sem_perm.mode = tbuf_old.sem_perm.mode;
  889. return 0;
  890. }
  891. default:
  892. return -EINVAL;
  893. }
  894. }
  895. /*
  896. * This function handles some semctl commands which require the rw_mutex
  897. * to be held in write mode.
  898. * NOTE: no locks must be held, the rw_mutex is taken inside this function.
  899. */
  900. static int semctl_down(struct ipc_namespace *ns, int semid,
  901. int cmd, int version, union semun arg)
  902. {
  903. struct sem_array *sma;
  904. int err;
  905. struct semid64_ds semid64;
  906. struct kern_ipc_perm *ipcp;
  907. if(cmd == IPC_SET) {
  908. if (copy_semid_from_user(&semid64, arg.buf, version))
  909. return -EFAULT;
  910. }
  911. ipcp = ipcctl_pre_down(&sem_ids(ns), semid, cmd, &semid64.sem_perm, 0);
  912. if (IS_ERR(ipcp))
  913. return PTR_ERR(ipcp);
  914. sma = container_of(ipcp, struct sem_array, sem_perm);
  915. err = security_sem_semctl(sma, cmd);
  916. if (err)
  917. goto out_unlock;
  918. switch(cmd){
  919. case IPC_RMID:
  920. freeary(ns, ipcp);
  921. goto out_up;
  922. case IPC_SET:
  923. ipc_update_perm(&semid64.sem_perm, ipcp);
  924. sma->sem_ctime = get_seconds();
  925. break;
  926. default:
  927. err = -EINVAL;
  928. }
  929. out_unlock:
  930. sem_unlock(sma);
  931. out_up:
  932. up_write(&sem_ids(ns).rw_mutex);
  933. return err;
  934. }
  935. SYSCALL_DEFINE(semctl)(int semid, int semnum, int cmd, union semun arg)
  936. {
  937. int err = -EINVAL;
  938. int version;
  939. struct ipc_namespace *ns;
  940. if (semid < 0)
  941. return -EINVAL;
  942. version = ipc_parse_version(&cmd);
  943. ns = current->nsproxy->ipc_ns;
  944. switch(cmd) {
  945. case IPC_INFO:
  946. case SEM_INFO:
  947. case IPC_STAT:
  948. case SEM_STAT:
  949. err = semctl_nolock(ns, semid, cmd, version, arg);
  950. return err;
  951. case GETALL:
  952. case GETVAL:
  953. case GETPID:
  954. case GETNCNT:
  955. case GETZCNT:
  956. case SETVAL:
  957. case SETALL:
  958. err = semctl_main(ns,semid,semnum,cmd,version,arg);
  959. return err;
  960. case IPC_RMID:
  961. case IPC_SET:
  962. err = semctl_down(ns, semid, cmd, version, arg);
  963. return err;
  964. default:
  965. return -EINVAL;
  966. }
  967. }
  968. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  969. asmlinkage long SyS_semctl(int semid, int semnum, int cmd, union semun arg)
  970. {
  971. return SYSC_semctl((int) semid, (int) semnum, (int) cmd, arg);
  972. }
  973. SYSCALL_ALIAS(sys_semctl, SyS_semctl);
  974. #endif
  975. /* If the task doesn't already have a undo_list, then allocate one
  976. * here. We guarantee there is only one thread using this undo list,
  977. * and current is THE ONE
  978. *
  979. * If this allocation and assignment succeeds, but later
  980. * portions of this code fail, there is no need to free the sem_undo_list.
  981. * Just let it stay associated with the task, and it'll be freed later
  982. * at exit time.
  983. *
  984. * This can block, so callers must hold no locks.
  985. */
  986. static inline int get_undo_list(struct sem_undo_list **undo_listp)
  987. {
  988. struct sem_undo_list *undo_list;
  989. undo_list = current->sysvsem.undo_list;
  990. if (!undo_list) {
  991. undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
  992. if (undo_list == NULL)
  993. return -ENOMEM;
  994. spin_lock_init(&undo_list->lock);
  995. atomic_set(&undo_list->refcnt, 1);
  996. INIT_LIST_HEAD(&undo_list->list_proc);
  997. current->sysvsem.undo_list = undo_list;
  998. }
  999. *undo_listp = undo_list;
  1000. return 0;
  1001. }
  1002. static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
  1003. {
  1004. struct sem_undo *un;
  1005. list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
  1006. if (un->semid == semid)
  1007. return un;
  1008. }
  1009. return NULL;
  1010. }
  1011. static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
  1012. {
  1013. struct sem_undo *un;
  1014. assert_spin_locked(&ulp->lock);
  1015. un = __lookup_undo(ulp, semid);
  1016. if (un) {
  1017. list_del_rcu(&un->list_proc);
  1018. list_add_rcu(&un->list_proc, &ulp->list_proc);
  1019. }
  1020. return un;
  1021. }
  1022. /**
  1023. * find_alloc_undo - Lookup (and if not present create) undo array
  1024. * @ns: namespace
  1025. * @semid: semaphore array id
  1026. *
  1027. * The function looks up (and if not present creates) the undo structure.
  1028. * The size of the undo structure depends on the size of the semaphore
  1029. * array, thus the alloc path is not that straightforward.
  1030. * Lifetime-rules: sem_undo is rcu-protected, on success, the function
  1031. * performs a rcu_read_lock().
  1032. */
  1033. static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
  1034. {
  1035. struct sem_array *sma;
  1036. struct sem_undo_list *ulp;
  1037. struct sem_undo *un, *new;
  1038. int nsems;
  1039. int error;
  1040. error = get_undo_list(&ulp);
  1041. if (error)
  1042. return ERR_PTR(error);
  1043. rcu_read_lock();
  1044. spin_lock(&ulp->lock);
  1045. un = lookup_undo(ulp, semid);
  1046. spin_unlock(&ulp->lock);
  1047. if (likely(un!=NULL))
  1048. goto out;
  1049. rcu_read_unlock();
  1050. /* no undo structure around - allocate one. */
  1051. /* step 1: figure out the size of the semaphore array */
  1052. sma = sem_lock_check(ns, semid);
  1053. if (IS_ERR(sma))
  1054. return ERR_CAST(sma);
  1055. nsems = sma->sem_nsems;
  1056. sem_getref_and_unlock(sma);
  1057. /* step 2: allocate new undo structure */
  1058. new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
  1059. if (!new) {
  1060. sem_putref(sma);
  1061. return ERR_PTR(-ENOMEM);
  1062. }
  1063. /* step 3: Acquire the lock on semaphore array */
  1064. sem_lock_and_putref(sma);
  1065. if (sma->sem_perm.deleted) {
  1066. sem_unlock(sma);
  1067. kfree(new);
  1068. un = ERR_PTR(-EIDRM);
  1069. goto out;
  1070. }
  1071. spin_lock(&ulp->lock);
  1072. /*
  1073. * step 4: check for races: did someone else allocate the undo struct?
  1074. */
  1075. un = lookup_undo(ulp, semid);
  1076. if (un) {
  1077. kfree(new);
  1078. goto success;
  1079. }
  1080. /* step 5: initialize & link new undo structure */
  1081. new->semadj = (short *) &new[1];
  1082. new->ulp = ulp;
  1083. new->semid = semid;
  1084. assert_spin_locked(&ulp->lock);
  1085. list_add_rcu(&new->list_proc, &ulp->list_proc);
  1086. assert_spin_locked(&sma->sem_perm.lock);
  1087. list_add(&new->list_id, &sma->list_id);
  1088. un = new;
  1089. success:
  1090. spin_unlock(&ulp->lock);
  1091. rcu_read_lock();
  1092. sem_unlock(sma);
  1093. out:
  1094. return un;
  1095. }
  1096. /**
  1097. * get_queue_result - Retrieve the result code from sem_queue
  1098. * @q: Pointer to queue structure
  1099. *
  1100. * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
  1101. * q->status, then we must loop until the value is replaced with the final
  1102. * value: This may happen if a task is woken up by an unrelated event (e.g.
  1103. * signal) and in parallel the task is woken up by another task because it got
  1104. * the requested semaphores.
  1105. *
  1106. * The function can be called with or without holding the semaphore spinlock.
  1107. */
  1108. static int get_queue_result(struct sem_queue *q)
  1109. {
  1110. int error;
  1111. error = q->status;
  1112. while (unlikely(error == IN_WAKEUP)) {
  1113. cpu_relax();
  1114. error = q->status;
  1115. }
  1116. return error;
  1117. }
  1118. SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
  1119. unsigned, nsops, const struct timespec __user *, timeout)
  1120. {
  1121. int error = -EINVAL;
  1122. struct sem_array *sma;
  1123. struct sembuf fast_sops[SEMOPM_FAST];
  1124. struct sembuf* sops = fast_sops, *sop;
  1125. struct sem_undo *un;
  1126. int undos = 0, alter = 0, max;
  1127. struct sem_queue queue;
  1128. unsigned long jiffies_left = 0;
  1129. struct ipc_namespace *ns;
  1130. struct list_head tasks;
  1131. ns = current->nsproxy->ipc_ns;
  1132. if (nsops < 1 || semid < 0)
  1133. return -EINVAL;
  1134. if (nsops > ns->sc_semopm)
  1135. return -E2BIG;
  1136. if(nsops > SEMOPM_FAST) {
  1137. sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
  1138. if(sops==NULL)
  1139. return -ENOMEM;
  1140. }
  1141. if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
  1142. error=-EFAULT;
  1143. goto out_free;
  1144. }
  1145. if (timeout) {
  1146. struct timespec _timeout;
  1147. if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
  1148. error = -EFAULT;
  1149. goto out_free;
  1150. }
  1151. if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
  1152. _timeout.tv_nsec >= 1000000000L) {
  1153. error = -EINVAL;
  1154. goto out_free;
  1155. }
  1156. jiffies_left = timespec_to_jiffies(&_timeout);
  1157. }
  1158. max = 0;
  1159. for (sop = sops; sop < sops + nsops; sop++) {
  1160. if (sop->sem_num >= max)
  1161. max = sop->sem_num;
  1162. if (sop->sem_flg & SEM_UNDO)
  1163. undos = 1;
  1164. if (sop->sem_op != 0)
  1165. alter = 1;
  1166. }
  1167. if (undos) {
  1168. un = find_alloc_undo(ns, semid);
  1169. if (IS_ERR(un)) {
  1170. error = PTR_ERR(un);
  1171. goto out_free;
  1172. }
  1173. } else
  1174. un = NULL;
  1175. INIT_LIST_HEAD(&tasks);
  1176. sma = sem_lock_check(ns, semid);
  1177. if (IS_ERR(sma)) {
  1178. if (un)
  1179. rcu_read_unlock();
  1180. error = PTR_ERR(sma);
  1181. goto out_free;
  1182. }
  1183. /*
  1184. * semid identifiers are not unique - find_alloc_undo may have
  1185. * allocated an undo structure, it was invalidated by an RMID
  1186. * and now a new array with received the same id. Check and fail.
  1187. * This case can be detected checking un->semid. The existance of
  1188. * "un" itself is guaranteed by rcu.
  1189. */
  1190. error = -EIDRM;
  1191. if (un) {
  1192. if (un->semid == -1) {
  1193. rcu_read_unlock();
  1194. goto out_unlock_free;
  1195. } else {
  1196. /*
  1197. * rcu lock can be released, "un" cannot disappear:
  1198. * - sem_lock is acquired, thus IPC_RMID is
  1199. * impossible.
  1200. * - exit_sem is impossible, it always operates on
  1201. * current (or a dead task).
  1202. */
  1203. rcu_read_unlock();
  1204. }
  1205. }
  1206. error = -EFBIG;
  1207. if (max >= sma->sem_nsems)
  1208. goto out_unlock_free;
  1209. error = -EACCES;
  1210. if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
  1211. goto out_unlock_free;
  1212. error = security_sem_semop(sma, sops, nsops, alter);
  1213. if (error)
  1214. goto out_unlock_free;
  1215. error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
  1216. if (error <= 0) {
  1217. if (alter && error == 0)
  1218. do_smart_update(sma, sops, nsops, 1, &tasks);
  1219. goto out_unlock_free;
  1220. }
  1221. /* We need to sleep on this operation, so we put the current
  1222. * task into the pending queue and go to sleep.
  1223. */
  1224. queue.sops = sops;
  1225. queue.nsops = nsops;
  1226. queue.undo = un;
  1227. queue.pid = task_tgid_vnr(current);
  1228. queue.alter = alter;
  1229. if (alter)
  1230. list_add_tail(&queue.list, &sma->sem_pending);
  1231. else
  1232. list_add(&queue.list, &sma->sem_pending);
  1233. if (nsops == 1) {
  1234. struct sem *curr;
  1235. curr = &sma->sem_base[sops->sem_num];
  1236. if (alter)
  1237. list_add_tail(&queue.simple_list, &curr->sem_pending);
  1238. else
  1239. list_add(&queue.simple_list, &curr->sem_pending);
  1240. } else {
  1241. INIT_LIST_HEAD(&queue.simple_list);
  1242. sma->complex_count++;
  1243. }
  1244. queue.status = -EINTR;
  1245. queue.sleeper = current;
  1246. current->state = TASK_INTERRUPTIBLE;
  1247. sem_unlock(sma);
  1248. if (timeout)
  1249. jiffies_left = schedule_timeout(jiffies_left);
  1250. else
  1251. schedule();
  1252. error = get_queue_result(&queue);
  1253. if (error != -EINTR) {
  1254. /* fast path: update_queue already obtained all requested
  1255. * resources.
  1256. * Perform a smp_mb(): User space could assume that semop()
  1257. * is a memory barrier: Without the mb(), the cpu could
  1258. * speculatively read in user space stale data that was
  1259. * overwritten by the previous owner of the semaphore.
  1260. */
  1261. smp_mb();
  1262. goto out_free;
  1263. }
  1264. sma = sem_lock(ns, semid);
  1265. if (IS_ERR(sma)) {
  1266. error = -EIDRM;
  1267. goto out_free;
  1268. }
  1269. error = get_queue_result(&queue);
  1270. /*
  1271. * If queue.status != -EINTR we are woken up by another process
  1272. */
  1273. if (error != -EINTR) {
  1274. goto out_unlock_free;
  1275. }
  1276. /*
  1277. * If an interrupt occurred we have to clean up the queue
  1278. */
  1279. if (timeout && jiffies_left == 0)
  1280. error = -EAGAIN;
  1281. unlink_queue(sma, &queue);
  1282. out_unlock_free:
  1283. sem_unlock(sma);
  1284. wake_up_sem_queue_do(&tasks);
  1285. out_free:
  1286. if(sops != fast_sops)
  1287. kfree(sops);
  1288. return error;
  1289. }
  1290. SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
  1291. unsigned, nsops)
  1292. {
  1293. return sys_semtimedop(semid, tsops, nsops, NULL);
  1294. }
  1295. /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
  1296. * parent and child tasks.
  1297. */
  1298. int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
  1299. {
  1300. struct sem_undo_list *undo_list;
  1301. int error;
  1302. if (clone_flags & CLONE_SYSVSEM) {
  1303. error = get_undo_list(&undo_list);
  1304. if (error)
  1305. return error;
  1306. atomic_inc(&undo_list->refcnt);
  1307. tsk->sysvsem.undo_list = undo_list;
  1308. } else
  1309. tsk->sysvsem.undo_list = NULL;
  1310. return 0;
  1311. }
  1312. /*
  1313. * add semadj values to semaphores, free undo structures.
  1314. * undo structures are not freed when semaphore arrays are destroyed
  1315. * so some of them may be out of date.
  1316. * IMPLEMENTATION NOTE: There is some confusion over whether the
  1317. * set of adjustments that needs to be done should be done in an atomic
  1318. * manner or not. That is, if we are attempting to decrement the semval
  1319. * should we queue up and wait until we can do so legally?
  1320. * The original implementation attempted to do this (queue and wait).
  1321. * The current implementation does not do so. The POSIX standard
  1322. * and SVID should be consulted to determine what behavior is mandated.
  1323. */
  1324. void exit_sem(struct task_struct *tsk)
  1325. {
  1326. struct sem_undo_list *ulp;
  1327. ulp = tsk->sysvsem.undo_list;
  1328. if (!ulp)
  1329. return;
  1330. tsk->sysvsem.undo_list = NULL;
  1331. if (!atomic_dec_and_test(&ulp->refcnt))
  1332. return;
  1333. for (;;) {
  1334. struct sem_array *sma;
  1335. struct sem_undo *un;
  1336. struct list_head tasks;
  1337. int semid;
  1338. int i;
  1339. rcu_read_lock();
  1340. un = list_entry_rcu(ulp->list_proc.next,
  1341. struct sem_undo, list_proc);
  1342. if (&un->list_proc == &ulp->list_proc)
  1343. semid = -1;
  1344. else
  1345. semid = un->semid;
  1346. rcu_read_unlock();
  1347. if (semid == -1)
  1348. break;
  1349. sma = sem_lock_check(tsk->nsproxy->ipc_ns, un->semid);
  1350. /* exit_sem raced with IPC_RMID, nothing to do */
  1351. if (IS_ERR(sma))
  1352. continue;
  1353. un = __lookup_undo(ulp, semid);
  1354. if (un == NULL) {
  1355. /* exit_sem raced with IPC_RMID+semget() that created
  1356. * exactly the same semid. Nothing to do.
  1357. */
  1358. sem_unlock(sma);
  1359. continue;
  1360. }
  1361. /* remove un from the linked lists */
  1362. assert_spin_locked(&sma->sem_perm.lock);
  1363. list_del(&un->list_id);
  1364. spin_lock(&ulp->lock);
  1365. list_del_rcu(&un->list_proc);
  1366. spin_unlock(&ulp->lock);
  1367. /* perform adjustments registered in un */
  1368. for (i = 0; i < sma->sem_nsems; i++) {
  1369. struct sem * semaphore = &sma->sem_base[i];
  1370. if (un->semadj[i]) {
  1371. semaphore->semval += un->semadj[i];
  1372. /*
  1373. * Range checks of the new semaphore value,
  1374. * not defined by sus:
  1375. * - Some unices ignore the undo entirely
  1376. * (e.g. HP UX 11i 11.22, Tru64 V5.1)
  1377. * - some cap the value (e.g. FreeBSD caps
  1378. * at 0, but doesn't enforce SEMVMX)
  1379. *
  1380. * Linux caps the semaphore value, both at 0
  1381. * and at SEMVMX.
  1382. *
  1383. * Manfred <manfred@colorfullife.com>
  1384. */
  1385. if (semaphore->semval < 0)
  1386. semaphore->semval = 0;
  1387. if (semaphore->semval > SEMVMX)
  1388. semaphore->semval = SEMVMX;
  1389. semaphore->sempid = task_tgid_vnr(current);
  1390. }
  1391. }
  1392. /* maybe some queued-up processes were waiting for this */
  1393. INIT_LIST_HEAD(&tasks);
  1394. do_smart_update(sma, NULL, 0, 1, &tasks);
  1395. sem_unlock(sma);
  1396. wake_up_sem_queue_do(&tasks);
  1397. call_rcu(&un->rcu, free_un);
  1398. }
  1399. kfree(ulp);
  1400. }
  1401. #ifdef CONFIG_PROC_FS
  1402. static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
  1403. {
  1404. struct sem_array *sma = it;
  1405. return seq_printf(s,
  1406. "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
  1407. sma->sem_perm.key,
  1408. sma->sem_perm.id,
  1409. sma->sem_perm.mode,
  1410. sma->sem_nsems,
  1411. sma->sem_perm.uid,
  1412. sma->sem_perm.gid,
  1413. sma->sem_perm.cuid,
  1414. sma->sem_perm.cgid,
  1415. sma->sem_otime,
  1416. sma->sem_ctime);
  1417. }
  1418. #endif