sem.c 43 KB

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