sem.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  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_semcnt()
  51. * - the task that performs a successful semop() scans the list of all
  52. * sleeping tasks and completes any pending operations that can be fulfilled.
  53. * Semaphores are actively given to waiting tasks (necessary for FIFO).
  54. * (see update_queue())
  55. * - To improve the scalability, the actual wake-up calls are performed after
  56. * dropping all locks. (see wake_up_sem_queue_prepare(),
  57. * wake_up_sem_queue_do())
  58. * - All work is done by the waker, the woken up task does not have to do
  59. * anything - not even acquiring a lock or dropping a refcount.
  60. * - A woken up task may not even touch the semaphore array anymore, it may
  61. * have been destroyed already by a semctl(RMID).
  62. * - The synchronizations between wake-ups due to a timeout/signal and a
  63. * wake-up due to a completed semaphore operation is achieved by using an
  64. * intermediate state (IN_WAKEUP).
  65. * - UNDO values are stored in an array (one per process and per
  66. * semaphore array, lazily allocated). For backwards compatibility, multiple
  67. * modes for the UNDO variables are supported (per process, per thread)
  68. * (see copy_semundo, CLONE_SYSVSEM)
  69. * - There are two lists of the pending operations: a per-array list
  70. * and per-semaphore list (stored in the array). This allows to achieve FIFO
  71. * ordering without always scanning all pending operations.
  72. * The worst-case behavior is nevertheless O(N^2) for N wakeups.
  73. */
  74. #include <linux/slab.h>
  75. #include <linux/spinlock.h>
  76. #include <linux/init.h>
  77. #include <linux/proc_fs.h>
  78. #include <linux/time.h>
  79. #include <linux/security.h>
  80. #include <linux/syscalls.h>
  81. #include <linux/audit.h>
  82. #include <linux/capability.h>
  83. #include <linux/seq_file.h>
  84. #include <linux/rwsem.h>
  85. #include <linux/nsproxy.h>
  86. #include <linux/ipc_namespace.h>
  87. #include <linux/uaccess.h>
  88. #include "util.h"
  89. /* One semaphore structure for each semaphore in the system. */
  90. struct sem {
  91. int semval; /* current value */
  92. int sempid; /* pid of last operation */
  93. spinlock_t lock; /* spinlock for fine-grained semtimedop */
  94. struct list_head pending_alter; /* pending single-sop operations */
  95. /* that alter the semaphore */
  96. struct list_head pending_const; /* pending single-sop operations */
  97. /* that do not alter the semaphore*/
  98. time_t sem_otime; /* candidate for sem_otime */
  99. } ____cacheline_aligned_in_smp;
  100. /* One queue for each sleeping process in the system. */
  101. struct sem_queue {
  102. struct list_head list; /* queue of pending operations */
  103. struct task_struct *sleeper; /* this process */
  104. struct sem_undo *undo; /* undo structure */
  105. int pid; /* process id of requesting process */
  106. int status; /* completion status of operation */
  107. struct sembuf *sops; /* array of pending operations */
  108. struct sembuf *blocking; /* the operation that blocked */
  109. int nsops; /* number of operations */
  110. int alter; /* does *sops alter the array? */
  111. };
  112. /* Each task has a list of undo requests. They are executed automatically
  113. * when the process exits.
  114. */
  115. struct sem_undo {
  116. struct list_head list_proc; /* per-process list: *
  117. * all undos from one process
  118. * rcu protected */
  119. struct rcu_head rcu; /* rcu struct for sem_undo */
  120. struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
  121. struct list_head list_id; /* per semaphore array list:
  122. * all undos for one array */
  123. int semid; /* semaphore set identifier */
  124. short *semadj; /* array of adjustments */
  125. /* one per semaphore */
  126. };
  127. /* sem_undo_list controls shared access to the list of sem_undo structures
  128. * that may be shared among all a CLONE_SYSVSEM task group.
  129. */
  130. struct sem_undo_list {
  131. atomic_t refcnt;
  132. spinlock_t lock;
  133. struct list_head list_proc;
  134. };
  135. #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
  136. #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
  137. static int newary(struct ipc_namespace *, struct ipc_params *);
  138. static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
  139. #ifdef CONFIG_PROC_FS
  140. static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
  141. #endif
  142. #define SEMMSL_FAST 256 /* 512 bytes on stack */
  143. #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
  144. /*
  145. * Locking:
  146. * sem_undo.id_next,
  147. * sem_array.complex_count,
  148. * sem_array.pending{_alter,_cont},
  149. * sem_array.sem_undo: global sem_lock() for read/write
  150. * sem_undo.proc_next: only "current" is allowed to read/write that field.
  151. *
  152. * sem_array.sem_base[i].pending_{const,alter}:
  153. * global or semaphore sem_lock() for read/write
  154. */
  155. #define sc_semmsl sem_ctls[0]
  156. #define sc_semmns sem_ctls[1]
  157. #define sc_semopm sem_ctls[2]
  158. #define sc_semmni sem_ctls[3]
  159. void sem_init_ns(struct ipc_namespace *ns)
  160. {
  161. ns->sc_semmsl = SEMMSL;
  162. ns->sc_semmns = SEMMNS;
  163. ns->sc_semopm = SEMOPM;
  164. ns->sc_semmni = SEMMNI;
  165. ns->used_sems = 0;
  166. ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
  167. }
  168. #ifdef CONFIG_IPC_NS
  169. void sem_exit_ns(struct ipc_namespace *ns)
  170. {
  171. free_ipcs(ns, &sem_ids(ns), freeary);
  172. idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
  173. }
  174. #endif
  175. void __init sem_init(void)
  176. {
  177. sem_init_ns(&init_ipc_ns);
  178. ipc_init_proc_interface("sysvipc/sem",
  179. " key semid perms nsems uid gid cuid cgid otime ctime\n",
  180. IPC_SEM_IDS, sysvipc_sem_proc_show);
  181. }
  182. /**
  183. * unmerge_queues - unmerge queues, if possible.
  184. * @sma: semaphore array
  185. *
  186. * The function unmerges the wait queues if complex_count is 0.
  187. * It must be called prior to dropping the global semaphore array lock.
  188. */
  189. static void unmerge_queues(struct sem_array *sma)
  190. {
  191. struct sem_queue *q, *tq;
  192. /* complex operations still around? */
  193. if (sma->complex_count)
  194. return;
  195. /*
  196. * We will switch back to simple mode.
  197. * Move all pending operation back into the per-semaphore
  198. * queues.
  199. */
  200. list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
  201. struct sem *curr;
  202. curr = &sma->sem_base[q->sops[0].sem_num];
  203. list_add_tail(&q->list, &curr->pending_alter);
  204. }
  205. INIT_LIST_HEAD(&sma->pending_alter);
  206. }
  207. /**
  208. * merge_queues - merge single semop queues into global queue
  209. * @sma: semaphore array
  210. *
  211. * This function merges all per-semaphore queues into the global queue.
  212. * It is necessary to achieve FIFO ordering for the pending single-sop
  213. * operations when a multi-semop operation must sleep.
  214. * Only the alter operations must be moved, the const operations can stay.
  215. */
  216. static void merge_queues(struct sem_array *sma)
  217. {
  218. int i;
  219. for (i = 0; i < sma->sem_nsems; i++) {
  220. struct sem *sem = sma->sem_base + i;
  221. list_splice_init(&sem->pending_alter, &sma->pending_alter);
  222. }
  223. }
  224. static void sem_rcu_free(struct rcu_head *head)
  225. {
  226. struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
  227. struct sem_array *sma = ipc_rcu_to_struct(p);
  228. security_sem_free(sma);
  229. ipc_rcu_free(head);
  230. }
  231. /*
  232. * Wait until all currently ongoing simple ops have completed.
  233. * Caller must own sem_perm.lock.
  234. * New simple ops cannot start, because simple ops first check
  235. * that sem_perm.lock is free.
  236. * that a) sem_perm.lock is free and b) complex_count is 0.
  237. */
  238. static void sem_wait_array(struct sem_array *sma)
  239. {
  240. int i;
  241. struct sem *sem;
  242. if (sma->complex_count) {
  243. /* The thread that increased sma->complex_count waited on
  244. * all sem->lock locks. Thus we don't need to wait again.
  245. */
  246. return;
  247. }
  248. for (i = 0; i < sma->sem_nsems; i++) {
  249. sem = sma->sem_base + i;
  250. spin_unlock_wait(&sem->lock);
  251. }
  252. }
  253. /*
  254. * If the request contains only one semaphore operation, and there are
  255. * no complex transactions pending, lock only the semaphore involved.
  256. * Otherwise, lock the entire semaphore array, since we either have
  257. * multiple semaphores in our own semops, or we need to look at
  258. * semaphores from other pending complex operations.
  259. */
  260. static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
  261. int nsops)
  262. {
  263. struct sem *sem;
  264. if (nsops != 1) {
  265. /* Complex operation - acquire a full lock */
  266. ipc_lock_object(&sma->sem_perm);
  267. /* And wait until all simple ops that are processed
  268. * right now have dropped their locks.
  269. */
  270. sem_wait_array(sma);
  271. return -1;
  272. }
  273. /*
  274. * Only one semaphore affected - try to optimize locking.
  275. * The rules are:
  276. * - optimized locking is possible if no complex operation
  277. * is either enqueued or processed right now.
  278. * - The test for enqueued complex ops is simple:
  279. * sma->complex_count != 0
  280. * - Testing for complex ops that are processed right now is
  281. * a bit more difficult. Complex ops acquire the full lock
  282. * and first wait that the running simple ops have completed.
  283. * (see above)
  284. * Thus: If we own a simple lock and the global lock is free
  285. * and complex_count is now 0, then it will stay 0 and
  286. * thus just locking sem->lock is sufficient.
  287. */
  288. sem = sma->sem_base + sops->sem_num;
  289. if (sma->complex_count == 0) {
  290. /*
  291. * It appears that no complex operation is around.
  292. * Acquire the per-semaphore lock.
  293. */
  294. spin_lock(&sem->lock);
  295. /* Then check that the global lock is free */
  296. if (!spin_is_locked(&sma->sem_perm.lock)) {
  297. /* spin_is_locked() is not a memory barrier */
  298. smp_mb();
  299. /* Now repeat the test of complex_count:
  300. * It can't change anymore until we drop sem->lock.
  301. * Thus: if is now 0, then it will stay 0.
  302. */
  303. if (sma->complex_count == 0) {
  304. /* fast path successful! */
  305. return sops->sem_num;
  306. }
  307. }
  308. spin_unlock(&sem->lock);
  309. }
  310. /* slow path: acquire the full lock */
  311. ipc_lock_object(&sma->sem_perm);
  312. if (sma->complex_count == 0) {
  313. /* False alarm:
  314. * There is no complex operation, thus we can switch
  315. * back to the fast path.
  316. */
  317. spin_lock(&sem->lock);
  318. ipc_unlock_object(&sma->sem_perm);
  319. return sops->sem_num;
  320. } else {
  321. /* Not a false alarm, thus complete the sequence for a
  322. * full lock.
  323. */
  324. sem_wait_array(sma);
  325. return -1;
  326. }
  327. }
  328. static inline void sem_unlock(struct sem_array *sma, int locknum)
  329. {
  330. if (locknum == -1) {
  331. unmerge_queues(sma);
  332. ipc_unlock_object(&sma->sem_perm);
  333. } else {
  334. struct sem *sem = sma->sem_base + locknum;
  335. spin_unlock(&sem->lock);
  336. }
  337. }
  338. /*
  339. * sem_lock_(check_) routines are called in the paths where the rwsem
  340. * is not held.
  341. *
  342. * The caller holds the RCU read lock.
  343. */
  344. static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
  345. int id, struct sembuf *sops, int nsops, int *locknum)
  346. {
  347. struct kern_ipc_perm *ipcp;
  348. struct sem_array *sma;
  349. ipcp = ipc_obtain_object(&sem_ids(ns), id);
  350. if (IS_ERR(ipcp))
  351. return ERR_CAST(ipcp);
  352. sma = container_of(ipcp, struct sem_array, sem_perm);
  353. *locknum = sem_lock(sma, sops, nsops);
  354. /* ipc_rmid() may have already freed the ID while sem_lock
  355. * was spinning: verify that the structure is still valid
  356. */
  357. if (ipc_valid_object(ipcp))
  358. return container_of(ipcp, struct sem_array, sem_perm);
  359. sem_unlock(sma, *locknum);
  360. return ERR_PTR(-EINVAL);
  361. }
  362. static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
  363. {
  364. struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
  365. if (IS_ERR(ipcp))
  366. return ERR_CAST(ipcp);
  367. return container_of(ipcp, struct sem_array, sem_perm);
  368. }
  369. static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
  370. int id)
  371. {
  372. struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
  373. if (IS_ERR(ipcp))
  374. return ERR_CAST(ipcp);
  375. return container_of(ipcp, struct sem_array, sem_perm);
  376. }
  377. static inline void sem_lock_and_putref(struct sem_array *sma)
  378. {
  379. sem_lock(sma, NULL, -1);
  380. ipc_rcu_putref(sma, ipc_rcu_free);
  381. }
  382. static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
  383. {
  384. ipc_rmid(&sem_ids(ns), &s->sem_perm);
  385. }
  386. /*
  387. * Lockless wakeup algorithm:
  388. * Without the check/retry algorithm a lockless wakeup is possible:
  389. * - queue.status is initialized to -EINTR before blocking.
  390. * - wakeup is performed by
  391. * * unlinking the queue entry from the pending list
  392. * * setting queue.status to IN_WAKEUP
  393. * This is the notification for the blocked thread that a
  394. * result value is imminent.
  395. * * call wake_up_process
  396. * * set queue.status to the final value.
  397. * - the previously blocked thread checks queue.status:
  398. * * if it's IN_WAKEUP, then it must wait until the value changes
  399. * * if it's not -EINTR, then the operation was completed by
  400. * update_queue. semtimedop can return queue.status without
  401. * performing any operation on the sem array.
  402. * * otherwise it must acquire the spinlock and check what's up.
  403. *
  404. * The two-stage algorithm is necessary to protect against the following
  405. * races:
  406. * - if queue.status is set after wake_up_process, then the woken up idle
  407. * thread could race forward and try (and fail) to acquire sma->lock
  408. * before update_queue had a chance to set queue.status
  409. * - if queue.status is written before wake_up_process and if the
  410. * blocked process is woken up by a signal between writing
  411. * queue.status and the wake_up_process, then the woken up
  412. * process could return from semtimedop and die by calling
  413. * sys_exit before wake_up_process is called. Then wake_up_process
  414. * will oops, because the task structure is already invalid.
  415. * (yes, this happened on s390 with sysv msg).
  416. *
  417. */
  418. #define IN_WAKEUP 1
  419. /**
  420. * newary - Create a new semaphore set
  421. * @ns: namespace
  422. * @params: ptr to the structure that contains key, semflg and nsems
  423. *
  424. * Called with sem_ids.rwsem held (as a writer)
  425. */
  426. static int newary(struct ipc_namespace *ns, struct ipc_params *params)
  427. {
  428. int id;
  429. int retval;
  430. struct sem_array *sma;
  431. int size;
  432. key_t key = params->key;
  433. int nsems = params->u.nsems;
  434. int semflg = params->flg;
  435. int i;
  436. if (!nsems)
  437. return -EINVAL;
  438. if (ns->used_sems + nsems > ns->sc_semmns)
  439. return -ENOSPC;
  440. size = sizeof(*sma) + nsems * sizeof(struct sem);
  441. sma = ipc_rcu_alloc(size);
  442. if (!sma)
  443. return -ENOMEM;
  444. memset(sma, 0, size);
  445. sma->sem_perm.mode = (semflg & S_IRWXUGO);
  446. sma->sem_perm.key = key;
  447. sma->sem_perm.security = NULL;
  448. retval = security_sem_alloc(sma);
  449. if (retval) {
  450. ipc_rcu_putref(sma, ipc_rcu_free);
  451. return retval;
  452. }
  453. id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
  454. if (id < 0) {
  455. ipc_rcu_putref(sma, sem_rcu_free);
  456. return id;
  457. }
  458. ns->used_sems += nsems;
  459. sma->sem_base = (struct sem *) &sma[1];
  460. for (i = 0; i < nsems; i++) {
  461. INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
  462. INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
  463. spin_lock_init(&sma->sem_base[i].lock);
  464. }
  465. sma->complex_count = 0;
  466. INIT_LIST_HEAD(&sma->pending_alter);
  467. INIT_LIST_HEAD(&sma->pending_const);
  468. INIT_LIST_HEAD(&sma->list_id);
  469. sma->sem_nsems = nsems;
  470. sma->sem_ctime = get_seconds();
  471. sem_unlock(sma, -1);
  472. rcu_read_unlock();
  473. return sma->sem_perm.id;
  474. }
  475. /*
  476. * Called with sem_ids.rwsem and ipcp locked.
  477. */
  478. static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
  479. {
  480. struct sem_array *sma;
  481. sma = container_of(ipcp, struct sem_array, sem_perm);
  482. return security_sem_associate(sma, semflg);
  483. }
  484. /*
  485. * Called with sem_ids.rwsem and ipcp locked.
  486. */
  487. static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
  488. struct ipc_params *params)
  489. {
  490. struct sem_array *sma;
  491. sma = container_of(ipcp, struct sem_array, sem_perm);
  492. if (params->u.nsems > sma->sem_nsems)
  493. return -EINVAL;
  494. return 0;
  495. }
  496. SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
  497. {
  498. struct ipc_namespace *ns;
  499. static const struct ipc_ops sem_ops = {
  500. .getnew = newary,
  501. .associate = sem_security,
  502. .more_checks = sem_more_checks,
  503. };
  504. struct ipc_params sem_params;
  505. ns = current->nsproxy->ipc_ns;
  506. if (nsems < 0 || nsems > ns->sc_semmsl)
  507. return -EINVAL;
  508. sem_params.key = key;
  509. sem_params.flg = semflg;
  510. sem_params.u.nsems = nsems;
  511. return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
  512. }
  513. /**
  514. * perform_atomic_semop - Perform (if possible) a semaphore operation
  515. * @sma: semaphore array
  516. * @q: struct sem_queue that describes the operation
  517. *
  518. * Returns 0 if the operation was possible.
  519. * Returns 1 if the operation is impossible, the caller must sleep.
  520. * Negative values are error codes.
  521. */
  522. static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
  523. {
  524. int result, sem_op, nsops, pid;
  525. struct sembuf *sop;
  526. struct sem *curr;
  527. struct sembuf *sops;
  528. struct sem_undo *un;
  529. sops = q->sops;
  530. nsops = q->nsops;
  531. un = q->undo;
  532. for (sop = sops; sop < sops + nsops; sop++) {
  533. curr = sma->sem_base + sop->sem_num;
  534. sem_op = sop->sem_op;
  535. result = curr->semval;
  536. if (!sem_op && result)
  537. goto would_block;
  538. result += sem_op;
  539. if (result < 0)
  540. goto would_block;
  541. if (result > SEMVMX)
  542. goto out_of_range;
  543. if (sop->sem_flg & SEM_UNDO) {
  544. int undo = un->semadj[sop->sem_num] - sem_op;
  545. /* Exceeding the undo range is an error. */
  546. if (undo < (-SEMAEM - 1) || undo > SEMAEM)
  547. goto out_of_range;
  548. un->semadj[sop->sem_num] = undo;
  549. }
  550. curr->semval = result;
  551. }
  552. sop--;
  553. pid = q->pid;
  554. while (sop >= sops) {
  555. sma->sem_base[sop->sem_num].sempid = pid;
  556. sop--;
  557. }
  558. return 0;
  559. out_of_range:
  560. result = -ERANGE;
  561. goto undo;
  562. would_block:
  563. q->blocking = sop;
  564. if (sop->sem_flg & IPC_NOWAIT)
  565. result = -EAGAIN;
  566. else
  567. result = 1;
  568. undo:
  569. sop--;
  570. while (sop >= sops) {
  571. sem_op = sop->sem_op;
  572. sma->sem_base[sop->sem_num].semval -= sem_op;
  573. if (sop->sem_flg & SEM_UNDO)
  574. un->semadj[sop->sem_num] += sem_op;
  575. sop--;
  576. }
  577. return result;
  578. }
  579. /** wake_up_sem_queue_prepare(q, error): Prepare wake-up
  580. * @q: queue entry that must be signaled
  581. * @error: Error value for the signal
  582. *
  583. * Prepare the wake-up of the queue entry q.
  584. */
  585. static void wake_up_sem_queue_prepare(struct list_head *pt,
  586. struct sem_queue *q, int error)
  587. {
  588. if (list_empty(pt)) {
  589. /*
  590. * Hold preempt off so that we don't get preempted and have the
  591. * wakee busy-wait until we're scheduled back on.
  592. */
  593. preempt_disable();
  594. }
  595. q->status = IN_WAKEUP;
  596. q->pid = error;
  597. list_add_tail(&q->list, pt);
  598. }
  599. /**
  600. * wake_up_sem_queue_do - do the actual wake-up
  601. * @pt: list of tasks to be woken up
  602. *
  603. * Do the actual wake-up.
  604. * The function is called without any locks held, thus the semaphore array
  605. * could be destroyed already and the tasks can disappear as soon as the
  606. * status is set to the actual return code.
  607. */
  608. static void wake_up_sem_queue_do(struct list_head *pt)
  609. {
  610. struct sem_queue *q, *t;
  611. int did_something;
  612. did_something = !list_empty(pt);
  613. list_for_each_entry_safe(q, t, pt, list) {
  614. wake_up_process(q->sleeper);
  615. /* q can disappear immediately after writing q->status. */
  616. smp_wmb();
  617. q->status = q->pid;
  618. }
  619. if (did_something)
  620. preempt_enable();
  621. }
  622. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  623. {
  624. list_del(&q->list);
  625. if (q->nsops > 1)
  626. sma->complex_count--;
  627. }
  628. /** check_restart(sma, q)
  629. * @sma: semaphore array
  630. * @q: the operation that just completed
  631. *
  632. * update_queue is O(N^2) when it restarts scanning the whole queue of
  633. * waiting operations. Therefore this function checks if the restart is
  634. * really necessary. It is called after a previously waiting operation
  635. * modified the array.
  636. * Note that wait-for-zero operations are handled without restart.
  637. */
  638. static int check_restart(struct sem_array *sma, struct sem_queue *q)
  639. {
  640. /* pending complex alter operations are too difficult to analyse */
  641. if (!list_empty(&sma->pending_alter))
  642. return 1;
  643. /* we were a sleeping complex operation. Too difficult */
  644. if (q->nsops > 1)
  645. return 1;
  646. /* It is impossible that someone waits for the new value:
  647. * - complex operations always restart.
  648. * - wait-for-zero are handled seperately.
  649. * - q is a previously sleeping simple operation that
  650. * altered the array. It must be a decrement, because
  651. * simple increments never sleep.
  652. * - If there are older (higher priority) decrements
  653. * in the queue, then they have observed the original
  654. * semval value and couldn't proceed. The operation
  655. * decremented to value - thus they won't proceed either.
  656. */
  657. return 0;
  658. }
  659. /**
  660. * wake_const_ops - wake up non-alter tasks
  661. * @sma: semaphore array.
  662. * @semnum: semaphore that was modified.
  663. * @pt: list head for the tasks that must be woken up.
  664. *
  665. * wake_const_ops must be called after a semaphore in a semaphore array
  666. * was set to 0. If complex const operations are pending, wake_const_ops must
  667. * be called with semnum = -1, as well as with the number of each modified
  668. * semaphore.
  669. * The tasks that must be woken up are added to @pt. The return code
  670. * is stored in q->pid.
  671. * The function returns 1 if at least one operation was completed successfully.
  672. */
  673. static int wake_const_ops(struct sem_array *sma, int semnum,
  674. struct list_head *pt)
  675. {
  676. struct sem_queue *q;
  677. struct list_head *walk;
  678. struct list_head *pending_list;
  679. int semop_completed = 0;
  680. if (semnum == -1)
  681. pending_list = &sma->pending_const;
  682. else
  683. pending_list = &sma->sem_base[semnum].pending_const;
  684. walk = pending_list->next;
  685. while (walk != pending_list) {
  686. int error;
  687. q = container_of(walk, struct sem_queue, list);
  688. walk = walk->next;
  689. error = perform_atomic_semop(sma, q);
  690. if (error <= 0) {
  691. /* operation completed, remove from queue & wakeup */
  692. unlink_queue(sma, q);
  693. wake_up_sem_queue_prepare(pt, q, error);
  694. if (error == 0)
  695. semop_completed = 1;
  696. }
  697. }
  698. return semop_completed;
  699. }
  700. /**
  701. * do_smart_wakeup_zero - wakeup all wait for zero tasks
  702. * @sma: semaphore array
  703. * @sops: operations that were performed
  704. * @nsops: number of operations
  705. * @pt: list head of the tasks that must be woken up.
  706. *
  707. * Checks all required queue for wait-for-zero operations, based
  708. * on the actual changes that were performed on the semaphore array.
  709. * The function returns 1 if at least one operation was completed successfully.
  710. */
  711. static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
  712. int nsops, struct list_head *pt)
  713. {
  714. int i;
  715. int semop_completed = 0;
  716. int got_zero = 0;
  717. /* first: the per-semaphore queues, if known */
  718. if (sops) {
  719. for (i = 0; i < nsops; i++) {
  720. int num = sops[i].sem_num;
  721. if (sma->sem_base[num].semval == 0) {
  722. got_zero = 1;
  723. semop_completed |= wake_const_ops(sma, num, pt);
  724. }
  725. }
  726. } else {
  727. /*
  728. * No sops means modified semaphores not known.
  729. * Assume all were changed.
  730. */
  731. for (i = 0; i < sma->sem_nsems; i++) {
  732. if (sma->sem_base[i].semval == 0) {
  733. got_zero = 1;
  734. semop_completed |= wake_const_ops(sma, i, pt);
  735. }
  736. }
  737. }
  738. /*
  739. * If one of the modified semaphores got 0,
  740. * then check the global queue, too.
  741. */
  742. if (got_zero)
  743. semop_completed |= wake_const_ops(sma, -1, pt);
  744. return semop_completed;
  745. }
  746. /**
  747. * update_queue - look for tasks that can be completed.
  748. * @sma: semaphore array.
  749. * @semnum: semaphore that was modified.
  750. * @pt: list head for the tasks that must be woken up.
  751. *
  752. * update_queue must be called after a semaphore in a semaphore array
  753. * was modified. If multiple semaphores were modified, update_queue must
  754. * be called with semnum = -1, as well as with the number of each modified
  755. * semaphore.
  756. * The tasks that must be woken up are added to @pt. The return code
  757. * is stored in q->pid.
  758. * The function internally checks if const operations can now succeed.
  759. *
  760. * The function return 1 if at least one semop was completed successfully.
  761. */
  762. static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
  763. {
  764. struct sem_queue *q;
  765. struct list_head *walk;
  766. struct list_head *pending_list;
  767. int semop_completed = 0;
  768. if (semnum == -1)
  769. pending_list = &sma->pending_alter;
  770. else
  771. pending_list = &sma->sem_base[semnum].pending_alter;
  772. again:
  773. walk = pending_list->next;
  774. while (walk != pending_list) {
  775. int error, restart;
  776. q = container_of(walk, struct sem_queue, list);
  777. walk = walk->next;
  778. /* If we are scanning the single sop, per-semaphore list of
  779. * one semaphore and that semaphore is 0, then it is not
  780. * necessary to scan further: simple increments
  781. * that affect only one entry succeed immediately and cannot
  782. * be in the per semaphore pending queue, and decrements
  783. * cannot be successful if the value is already 0.
  784. */
  785. if (semnum != -1 && sma->sem_base[semnum].semval == 0)
  786. break;
  787. error = perform_atomic_semop(sma, q);
  788. /* Does q->sleeper still need to sleep? */
  789. if (error > 0)
  790. continue;
  791. unlink_queue(sma, q);
  792. if (error) {
  793. restart = 0;
  794. } else {
  795. semop_completed = 1;
  796. do_smart_wakeup_zero(sma, q->sops, q->nsops, pt);
  797. restart = check_restart(sma, q);
  798. }
  799. wake_up_sem_queue_prepare(pt, q, error);
  800. if (restart)
  801. goto again;
  802. }
  803. return semop_completed;
  804. }
  805. /**
  806. * set_semotime - set sem_otime
  807. * @sma: semaphore array
  808. * @sops: operations that modified the array, may be NULL
  809. *
  810. * sem_otime is replicated to avoid cache line trashing.
  811. * This function sets one instance to the current time.
  812. */
  813. static void set_semotime(struct sem_array *sma, struct sembuf *sops)
  814. {
  815. if (sops == NULL) {
  816. sma->sem_base[0].sem_otime = get_seconds();
  817. } else {
  818. sma->sem_base[sops[0].sem_num].sem_otime =
  819. get_seconds();
  820. }
  821. }
  822. /**
  823. * do_smart_update - optimized update_queue
  824. * @sma: semaphore array
  825. * @sops: operations that were performed
  826. * @nsops: number of operations
  827. * @otime: force setting otime
  828. * @pt: list head of the tasks that must be woken up.
  829. *
  830. * do_smart_update() does the required calls to update_queue and wakeup_zero,
  831. * based on the actual changes that were performed on the semaphore array.
  832. * Note that the function does not do the actual wake-up: the caller is
  833. * responsible for calling wake_up_sem_queue_do(@pt).
  834. * It is safe to perform this call after dropping all locks.
  835. */
  836. static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
  837. int otime, struct list_head *pt)
  838. {
  839. int i;
  840. otime |= do_smart_wakeup_zero(sma, sops, nsops, pt);
  841. if (!list_empty(&sma->pending_alter)) {
  842. /* semaphore array uses the global queue - just process it. */
  843. otime |= update_queue(sma, -1, pt);
  844. } else {
  845. if (!sops) {
  846. /*
  847. * No sops, thus the modified semaphores are not
  848. * known. Check all.
  849. */
  850. for (i = 0; i < sma->sem_nsems; i++)
  851. otime |= update_queue(sma, i, pt);
  852. } else {
  853. /*
  854. * Check the semaphores that were increased:
  855. * - No complex ops, thus all sleeping ops are
  856. * decrease.
  857. * - if we decreased the value, then any sleeping
  858. * semaphore ops wont be able to run: If the
  859. * previous value was too small, then the new
  860. * value will be too small, too.
  861. */
  862. for (i = 0; i < nsops; i++) {
  863. if (sops[i].sem_op > 0) {
  864. otime |= update_queue(sma,
  865. sops[i].sem_num, pt);
  866. }
  867. }
  868. }
  869. }
  870. if (otime)
  871. set_semotime(sma, sops);
  872. }
  873. /*
  874. * check_qop: Test if a queued operation sleeps on the semaphore semnum
  875. */
  876. static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
  877. bool count_zero)
  878. {
  879. struct sembuf *sop = q->blocking;
  880. /*
  881. * Linux always (since 0.99.10) reported a task as sleeping on all
  882. * semaphores. This violates SUS, therefore it was changed to the
  883. * standard compliant behavior.
  884. * Give the administrators a chance to notice that an application
  885. * might misbehave because it relies on the Linux behavior.
  886. */
  887. pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
  888. "The task %s (%d) triggered the difference, watch for misbehavior.\n",
  889. current->comm, task_pid_nr(current));
  890. if (sop->sem_num != semnum)
  891. return 0;
  892. if (count_zero && sop->sem_op == 0)
  893. return 1;
  894. if (!count_zero && sop->sem_op < 0)
  895. return 1;
  896. return 0;
  897. }
  898. /* The following counts are associated to each semaphore:
  899. * semncnt number of tasks waiting on semval being nonzero
  900. * semzcnt number of tasks waiting on semval being zero
  901. *
  902. * Per definition, a task waits only on the semaphore of the first semop
  903. * that cannot proceed, even if additional operation would block, too.
  904. */
  905. static int count_semcnt(struct sem_array *sma, ushort semnum,
  906. bool count_zero)
  907. {
  908. struct list_head *l;
  909. struct sem_queue *q;
  910. int semcnt;
  911. semcnt = 0;
  912. /* First: check the simple operations. They are easy to evaluate */
  913. if (count_zero)
  914. l = &sma->sem_base[semnum].pending_const;
  915. else
  916. l = &sma->sem_base[semnum].pending_alter;
  917. list_for_each_entry(q, l, list) {
  918. /* all task on a per-semaphore list sleep on exactly
  919. * that semaphore
  920. */
  921. semcnt++;
  922. }
  923. /* Then: check the complex operations. */
  924. list_for_each_entry(q, &sma->pending_alter, list) {
  925. semcnt += check_qop(sma, semnum, q, count_zero);
  926. }
  927. if (count_zero) {
  928. list_for_each_entry(q, &sma->pending_const, list) {
  929. semcnt += check_qop(sma, semnum, q, count_zero);
  930. }
  931. }
  932. return semcnt;
  933. }
  934. /* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
  935. * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
  936. * remains locked on exit.
  937. */
  938. static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  939. {
  940. struct sem_undo *un, *tu;
  941. struct sem_queue *q, *tq;
  942. struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
  943. struct list_head tasks;
  944. int i;
  945. /* Free the existing undo structures for this semaphore set. */
  946. ipc_assert_locked_object(&sma->sem_perm);
  947. list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
  948. list_del(&un->list_id);
  949. spin_lock(&un->ulp->lock);
  950. un->semid = -1;
  951. list_del_rcu(&un->list_proc);
  952. spin_unlock(&un->ulp->lock);
  953. kfree_rcu(un, rcu);
  954. }
  955. /* Wake up all pending processes and let them fail with EIDRM. */
  956. INIT_LIST_HEAD(&tasks);
  957. list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
  958. unlink_queue(sma, q);
  959. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  960. }
  961. list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
  962. unlink_queue(sma, q);
  963. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  964. }
  965. for (i = 0; i < sma->sem_nsems; i++) {
  966. struct sem *sem = sma->sem_base + i;
  967. list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
  968. unlink_queue(sma, q);
  969. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  970. }
  971. list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
  972. unlink_queue(sma, q);
  973. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  974. }
  975. }
  976. /* Remove the semaphore set from the IDR */
  977. sem_rmid(ns, sma);
  978. sem_unlock(sma, -1);
  979. rcu_read_unlock();
  980. wake_up_sem_queue_do(&tasks);
  981. ns->used_sems -= sma->sem_nsems;
  982. ipc_rcu_putref(sma, sem_rcu_free);
  983. }
  984. static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
  985. {
  986. switch (version) {
  987. case IPC_64:
  988. return copy_to_user(buf, in, sizeof(*in));
  989. case IPC_OLD:
  990. {
  991. struct semid_ds out;
  992. memset(&out, 0, sizeof(out));
  993. ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
  994. out.sem_otime = in->sem_otime;
  995. out.sem_ctime = in->sem_ctime;
  996. out.sem_nsems = in->sem_nsems;
  997. return copy_to_user(buf, &out, sizeof(out));
  998. }
  999. default:
  1000. return -EINVAL;
  1001. }
  1002. }
  1003. static time_t get_semotime(struct sem_array *sma)
  1004. {
  1005. int i;
  1006. time_t res;
  1007. res = sma->sem_base[0].sem_otime;
  1008. for (i = 1; i < sma->sem_nsems; i++) {
  1009. time_t to = sma->sem_base[i].sem_otime;
  1010. if (to > res)
  1011. res = to;
  1012. }
  1013. return res;
  1014. }
  1015. static int semctl_nolock(struct ipc_namespace *ns, int semid,
  1016. int cmd, int version, void __user *p)
  1017. {
  1018. int err;
  1019. struct sem_array *sma;
  1020. switch (cmd) {
  1021. case IPC_INFO:
  1022. case SEM_INFO:
  1023. {
  1024. struct seminfo seminfo;
  1025. int max_id;
  1026. err = security_sem_semctl(NULL, cmd);
  1027. if (err)
  1028. return err;
  1029. memset(&seminfo, 0, sizeof(seminfo));
  1030. seminfo.semmni = ns->sc_semmni;
  1031. seminfo.semmns = ns->sc_semmns;
  1032. seminfo.semmsl = ns->sc_semmsl;
  1033. seminfo.semopm = ns->sc_semopm;
  1034. seminfo.semvmx = SEMVMX;
  1035. seminfo.semmnu = SEMMNU;
  1036. seminfo.semmap = SEMMAP;
  1037. seminfo.semume = SEMUME;
  1038. down_read(&sem_ids(ns).rwsem);
  1039. if (cmd == SEM_INFO) {
  1040. seminfo.semusz = sem_ids(ns).in_use;
  1041. seminfo.semaem = ns->used_sems;
  1042. } else {
  1043. seminfo.semusz = SEMUSZ;
  1044. seminfo.semaem = SEMAEM;
  1045. }
  1046. max_id = ipc_get_maxid(&sem_ids(ns));
  1047. up_read(&sem_ids(ns).rwsem);
  1048. if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
  1049. return -EFAULT;
  1050. return (max_id < 0) ? 0 : max_id;
  1051. }
  1052. case IPC_STAT:
  1053. case SEM_STAT:
  1054. {
  1055. struct semid64_ds tbuf;
  1056. int id = 0;
  1057. memset(&tbuf, 0, sizeof(tbuf));
  1058. rcu_read_lock();
  1059. if (cmd == SEM_STAT) {
  1060. sma = sem_obtain_object(ns, semid);
  1061. if (IS_ERR(sma)) {
  1062. err = PTR_ERR(sma);
  1063. goto out_unlock;
  1064. }
  1065. id = sma->sem_perm.id;
  1066. } else {
  1067. sma = sem_obtain_object_check(ns, semid);
  1068. if (IS_ERR(sma)) {
  1069. err = PTR_ERR(sma);
  1070. goto out_unlock;
  1071. }
  1072. }
  1073. err = -EACCES;
  1074. if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
  1075. goto out_unlock;
  1076. err = security_sem_semctl(sma, cmd);
  1077. if (err)
  1078. goto out_unlock;
  1079. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  1080. tbuf.sem_otime = get_semotime(sma);
  1081. tbuf.sem_ctime = sma->sem_ctime;
  1082. tbuf.sem_nsems = sma->sem_nsems;
  1083. rcu_read_unlock();
  1084. if (copy_semid_to_user(p, &tbuf, version))
  1085. return -EFAULT;
  1086. return id;
  1087. }
  1088. default:
  1089. return -EINVAL;
  1090. }
  1091. out_unlock:
  1092. rcu_read_unlock();
  1093. return err;
  1094. }
  1095. static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
  1096. unsigned long arg)
  1097. {
  1098. struct sem_undo *un;
  1099. struct sem_array *sma;
  1100. struct sem *curr;
  1101. int err;
  1102. struct list_head tasks;
  1103. int val;
  1104. #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
  1105. /* big-endian 64bit */
  1106. val = arg >> 32;
  1107. #else
  1108. /* 32bit or little-endian 64bit */
  1109. val = arg;
  1110. #endif
  1111. if (val > SEMVMX || val < 0)
  1112. return -ERANGE;
  1113. INIT_LIST_HEAD(&tasks);
  1114. rcu_read_lock();
  1115. sma = sem_obtain_object_check(ns, semid);
  1116. if (IS_ERR(sma)) {
  1117. rcu_read_unlock();
  1118. return PTR_ERR(sma);
  1119. }
  1120. if (semnum < 0 || semnum >= sma->sem_nsems) {
  1121. rcu_read_unlock();
  1122. return -EINVAL;
  1123. }
  1124. if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
  1125. rcu_read_unlock();
  1126. return -EACCES;
  1127. }
  1128. err = security_sem_semctl(sma, SETVAL);
  1129. if (err) {
  1130. rcu_read_unlock();
  1131. return -EACCES;
  1132. }
  1133. sem_lock(sma, NULL, -1);
  1134. if (!ipc_valid_object(&sma->sem_perm)) {
  1135. sem_unlock(sma, -1);
  1136. rcu_read_unlock();
  1137. return -EIDRM;
  1138. }
  1139. curr = &sma->sem_base[semnum];
  1140. ipc_assert_locked_object(&sma->sem_perm);
  1141. list_for_each_entry(un, &sma->list_id, list_id)
  1142. un->semadj[semnum] = 0;
  1143. curr->semval = val;
  1144. curr->sempid = task_tgid_vnr(current);
  1145. sma->sem_ctime = get_seconds();
  1146. /* maybe some queued-up processes were waiting for this */
  1147. do_smart_update(sma, NULL, 0, 0, &tasks);
  1148. sem_unlock(sma, -1);
  1149. rcu_read_unlock();
  1150. wake_up_sem_queue_do(&tasks);
  1151. return 0;
  1152. }
  1153. static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
  1154. int cmd, void __user *p)
  1155. {
  1156. struct sem_array *sma;
  1157. struct sem *curr;
  1158. int err, nsems;
  1159. ushort fast_sem_io[SEMMSL_FAST];
  1160. ushort *sem_io = fast_sem_io;
  1161. struct list_head tasks;
  1162. INIT_LIST_HEAD(&tasks);
  1163. rcu_read_lock();
  1164. sma = sem_obtain_object_check(ns, semid);
  1165. if (IS_ERR(sma)) {
  1166. rcu_read_unlock();
  1167. return PTR_ERR(sma);
  1168. }
  1169. nsems = sma->sem_nsems;
  1170. err = -EACCES;
  1171. if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
  1172. goto out_rcu_wakeup;
  1173. err = security_sem_semctl(sma, cmd);
  1174. if (err)
  1175. goto out_rcu_wakeup;
  1176. err = -EACCES;
  1177. switch (cmd) {
  1178. case GETALL:
  1179. {
  1180. ushort __user *array = p;
  1181. int i;
  1182. sem_lock(sma, NULL, -1);
  1183. if (!ipc_valid_object(&sma->sem_perm)) {
  1184. err = -EIDRM;
  1185. goto out_unlock;
  1186. }
  1187. if (nsems > SEMMSL_FAST) {
  1188. if (!ipc_rcu_getref(sma)) {
  1189. err = -EIDRM;
  1190. goto out_unlock;
  1191. }
  1192. sem_unlock(sma, -1);
  1193. rcu_read_unlock();
  1194. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  1195. if (sem_io == NULL) {
  1196. ipc_rcu_putref(sma, ipc_rcu_free);
  1197. return -ENOMEM;
  1198. }
  1199. rcu_read_lock();
  1200. sem_lock_and_putref(sma);
  1201. if (!ipc_valid_object(&sma->sem_perm)) {
  1202. err = -EIDRM;
  1203. goto out_unlock;
  1204. }
  1205. }
  1206. for (i = 0; i < sma->sem_nsems; i++)
  1207. sem_io[i] = sma->sem_base[i].semval;
  1208. sem_unlock(sma, -1);
  1209. rcu_read_unlock();
  1210. err = 0;
  1211. if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
  1212. err = -EFAULT;
  1213. goto out_free;
  1214. }
  1215. case SETALL:
  1216. {
  1217. int i;
  1218. struct sem_undo *un;
  1219. if (!ipc_rcu_getref(sma)) {
  1220. err = -EIDRM;
  1221. goto out_rcu_wakeup;
  1222. }
  1223. rcu_read_unlock();
  1224. if (nsems > SEMMSL_FAST) {
  1225. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  1226. if (sem_io == NULL) {
  1227. ipc_rcu_putref(sma, ipc_rcu_free);
  1228. return -ENOMEM;
  1229. }
  1230. }
  1231. if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
  1232. ipc_rcu_putref(sma, ipc_rcu_free);
  1233. err = -EFAULT;
  1234. goto out_free;
  1235. }
  1236. for (i = 0; i < nsems; i++) {
  1237. if (sem_io[i] > SEMVMX) {
  1238. ipc_rcu_putref(sma, ipc_rcu_free);
  1239. err = -ERANGE;
  1240. goto out_free;
  1241. }
  1242. }
  1243. rcu_read_lock();
  1244. sem_lock_and_putref(sma);
  1245. if (!ipc_valid_object(&sma->sem_perm)) {
  1246. err = -EIDRM;
  1247. goto out_unlock;
  1248. }
  1249. for (i = 0; i < nsems; i++)
  1250. sma->sem_base[i].semval = sem_io[i];
  1251. ipc_assert_locked_object(&sma->sem_perm);
  1252. list_for_each_entry(un, &sma->list_id, list_id) {
  1253. for (i = 0; i < nsems; i++)
  1254. un->semadj[i] = 0;
  1255. }
  1256. sma->sem_ctime = get_seconds();
  1257. /* maybe some queued-up processes were waiting for this */
  1258. do_smart_update(sma, NULL, 0, 0, &tasks);
  1259. err = 0;
  1260. goto out_unlock;
  1261. }
  1262. /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
  1263. }
  1264. err = -EINVAL;
  1265. if (semnum < 0 || semnum >= nsems)
  1266. goto out_rcu_wakeup;
  1267. sem_lock(sma, NULL, -1);
  1268. if (!ipc_valid_object(&sma->sem_perm)) {
  1269. err = -EIDRM;
  1270. goto out_unlock;
  1271. }
  1272. curr = &sma->sem_base[semnum];
  1273. switch (cmd) {
  1274. case GETVAL:
  1275. err = curr->semval;
  1276. goto out_unlock;
  1277. case GETPID:
  1278. err = curr->sempid;
  1279. goto out_unlock;
  1280. case GETNCNT:
  1281. err = count_semcnt(sma, semnum, 0);
  1282. goto out_unlock;
  1283. case GETZCNT:
  1284. err = count_semcnt(sma, semnum, 1);
  1285. goto out_unlock;
  1286. }
  1287. out_unlock:
  1288. sem_unlock(sma, -1);
  1289. out_rcu_wakeup:
  1290. rcu_read_unlock();
  1291. wake_up_sem_queue_do(&tasks);
  1292. out_free:
  1293. if (sem_io != fast_sem_io)
  1294. ipc_free(sem_io, sizeof(ushort)*nsems);
  1295. return err;
  1296. }
  1297. static inline unsigned long
  1298. copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
  1299. {
  1300. switch (version) {
  1301. case IPC_64:
  1302. if (copy_from_user(out, buf, sizeof(*out)))
  1303. return -EFAULT;
  1304. return 0;
  1305. case IPC_OLD:
  1306. {
  1307. struct semid_ds tbuf_old;
  1308. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  1309. return -EFAULT;
  1310. out->sem_perm.uid = tbuf_old.sem_perm.uid;
  1311. out->sem_perm.gid = tbuf_old.sem_perm.gid;
  1312. out->sem_perm.mode = tbuf_old.sem_perm.mode;
  1313. return 0;
  1314. }
  1315. default:
  1316. return -EINVAL;
  1317. }
  1318. }
  1319. /*
  1320. * This function handles some semctl commands which require the rwsem
  1321. * to be held in write mode.
  1322. * NOTE: no locks must be held, the rwsem is taken inside this function.
  1323. */
  1324. static int semctl_down(struct ipc_namespace *ns, int semid,
  1325. int cmd, int version, void __user *p)
  1326. {
  1327. struct sem_array *sma;
  1328. int err;
  1329. struct semid64_ds semid64;
  1330. struct kern_ipc_perm *ipcp;
  1331. if (cmd == IPC_SET) {
  1332. if (copy_semid_from_user(&semid64, p, version))
  1333. return -EFAULT;
  1334. }
  1335. down_write(&sem_ids(ns).rwsem);
  1336. rcu_read_lock();
  1337. ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
  1338. &semid64.sem_perm, 0);
  1339. if (IS_ERR(ipcp)) {
  1340. err = PTR_ERR(ipcp);
  1341. goto out_unlock1;
  1342. }
  1343. sma = container_of(ipcp, struct sem_array, sem_perm);
  1344. err = security_sem_semctl(sma, cmd);
  1345. if (err)
  1346. goto out_unlock1;
  1347. switch (cmd) {
  1348. case IPC_RMID:
  1349. sem_lock(sma, NULL, -1);
  1350. /* freeary unlocks the ipc object and rcu */
  1351. freeary(ns, ipcp);
  1352. goto out_up;
  1353. case IPC_SET:
  1354. sem_lock(sma, NULL, -1);
  1355. err = ipc_update_perm(&semid64.sem_perm, ipcp);
  1356. if (err)
  1357. goto out_unlock0;
  1358. sma->sem_ctime = get_seconds();
  1359. break;
  1360. default:
  1361. err = -EINVAL;
  1362. goto out_unlock1;
  1363. }
  1364. out_unlock0:
  1365. sem_unlock(sma, -1);
  1366. out_unlock1:
  1367. rcu_read_unlock();
  1368. out_up:
  1369. up_write(&sem_ids(ns).rwsem);
  1370. return err;
  1371. }
  1372. SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
  1373. {
  1374. int version;
  1375. struct ipc_namespace *ns;
  1376. void __user *p = (void __user *)arg;
  1377. if (semid < 0)
  1378. return -EINVAL;
  1379. version = ipc_parse_version(&cmd);
  1380. ns = current->nsproxy->ipc_ns;
  1381. switch (cmd) {
  1382. case IPC_INFO:
  1383. case SEM_INFO:
  1384. case IPC_STAT:
  1385. case SEM_STAT:
  1386. return semctl_nolock(ns, semid, cmd, version, p);
  1387. case GETALL:
  1388. case GETVAL:
  1389. case GETPID:
  1390. case GETNCNT:
  1391. case GETZCNT:
  1392. case SETALL:
  1393. return semctl_main(ns, semid, semnum, cmd, p);
  1394. case SETVAL:
  1395. return semctl_setval(ns, semid, semnum, arg);
  1396. case IPC_RMID:
  1397. case IPC_SET:
  1398. return semctl_down(ns, semid, cmd, version, p);
  1399. default:
  1400. return -EINVAL;
  1401. }
  1402. }
  1403. /* If the task doesn't already have a undo_list, then allocate one
  1404. * here. We guarantee there is only one thread using this undo list,
  1405. * and current is THE ONE
  1406. *
  1407. * If this allocation and assignment succeeds, but later
  1408. * portions of this code fail, there is no need to free the sem_undo_list.
  1409. * Just let it stay associated with the task, and it'll be freed later
  1410. * at exit time.
  1411. *
  1412. * This can block, so callers must hold no locks.
  1413. */
  1414. static inline int get_undo_list(struct sem_undo_list **undo_listp)
  1415. {
  1416. struct sem_undo_list *undo_list;
  1417. undo_list = current->sysvsem.undo_list;
  1418. if (!undo_list) {
  1419. undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
  1420. if (undo_list == NULL)
  1421. return -ENOMEM;
  1422. spin_lock_init(&undo_list->lock);
  1423. atomic_set(&undo_list->refcnt, 1);
  1424. INIT_LIST_HEAD(&undo_list->list_proc);
  1425. current->sysvsem.undo_list = undo_list;
  1426. }
  1427. *undo_listp = undo_list;
  1428. return 0;
  1429. }
  1430. static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
  1431. {
  1432. struct sem_undo *un;
  1433. list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
  1434. if (un->semid == semid)
  1435. return un;
  1436. }
  1437. return NULL;
  1438. }
  1439. static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
  1440. {
  1441. struct sem_undo *un;
  1442. assert_spin_locked(&ulp->lock);
  1443. un = __lookup_undo(ulp, semid);
  1444. if (un) {
  1445. list_del_rcu(&un->list_proc);
  1446. list_add_rcu(&un->list_proc, &ulp->list_proc);
  1447. }
  1448. return un;
  1449. }
  1450. /**
  1451. * find_alloc_undo - lookup (and if not present create) undo array
  1452. * @ns: namespace
  1453. * @semid: semaphore array id
  1454. *
  1455. * The function looks up (and if not present creates) the undo structure.
  1456. * The size of the undo structure depends on the size of the semaphore
  1457. * array, thus the alloc path is not that straightforward.
  1458. * Lifetime-rules: sem_undo is rcu-protected, on success, the function
  1459. * performs a rcu_read_lock().
  1460. */
  1461. static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
  1462. {
  1463. struct sem_array *sma;
  1464. struct sem_undo_list *ulp;
  1465. struct sem_undo *un, *new;
  1466. int nsems, error;
  1467. error = get_undo_list(&ulp);
  1468. if (error)
  1469. return ERR_PTR(error);
  1470. rcu_read_lock();
  1471. spin_lock(&ulp->lock);
  1472. un = lookup_undo(ulp, semid);
  1473. spin_unlock(&ulp->lock);
  1474. if (likely(un != NULL))
  1475. goto out;
  1476. /* no undo structure around - allocate one. */
  1477. /* step 1: figure out the size of the semaphore array */
  1478. sma = sem_obtain_object_check(ns, semid);
  1479. if (IS_ERR(sma)) {
  1480. rcu_read_unlock();
  1481. return ERR_CAST(sma);
  1482. }
  1483. nsems = sma->sem_nsems;
  1484. if (!ipc_rcu_getref(sma)) {
  1485. rcu_read_unlock();
  1486. un = ERR_PTR(-EIDRM);
  1487. goto out;
  1488. }
  1489. rcu_read_unlock();
  1490. /* step 2: allocate new undo structure */
  1491. new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
  1492. if (!new) {
  1493. ipc_rcu_putref(sma, ipc_rcu_free);
  1494. return ERR_PTR(-ENOMEM);
  1495. }
  1496. /* step 3: Acquire the lock on semaphore array */
  1497. rcu_read_lock();
  1498. sem_lock_and_putref(sma);
  1499. if (!ipc_valid_object(&sma->sem_perm)) {
  1500. sem_unlock(sma, -1);
  1501. rcu_read_unlock();
  1502. kfree(new);
  1503. un = ERR_PTR(-EIDRM);
  1504. goto out;
  1505. }
  1506. spin_lock(&ulp->lock);
  1507. /*
  1508. * step 4: check for races: did someone else allocate the undo struct?
  1509. */
  1510. un = lookup_undo(ulp, semid);
  1511. if (un) {
  1512. kfree(new);
  1513. goto success;
  1514. }
  1515. /* step 5: initialize & link new undo structure */
  1516. new->semadj = (short *) &new[1];
  1517. new->ulp = ulp;
  1518. new->semid = semid;
  1519. assert_spin_locked(&ulp->lock);
  1520. list_add_rcu(&new->list_proc, &ulp->list_proc);
  1521. ipc_assert_locked_object(&sma->sem_perm);
  1522. list_add(&new->list_id, &sma->list_id);
  1523. un = new;
  1524. success:
  1525. spin_unlock(&ulp->lock);
  1526. sem_unlock(sma, -1);
  1527. out:
  1528. return un;
  1529. }
  1530. /**
  1531. * get_queue_result - retrieve the result code from sem_queue
  1532. * @q: Pointer to queue structure
  1533. *
  1534. * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
  1535. * q->status, then we must loop until the value is replaced with the final
  1536. * value: This may happen if a task is woken up by an unrelated event (e.g.
  1537. * signal) and in parallel the task is woken up by another task because it got
  1538. * the requested semaphores.
  1539. *
  1540. * The function can be called with or without holding the semaphore spinlock.
  1541. */
  1542. static int get_queue_result(struct sem_queue *q)
  1543. {
  1544. int error;
  1545. error = q->status;
  1546. while (unlikely(error == IN_WAKEUP)) {
  1547. cpu_relax();
  1548. error = q->status;
  1549. }
  1550. return error;
  1551. }
  1552. SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
  1553. unsigned, nsops, const struct timespec __user *, timeout)
  1554. {
  1555. int error = -EINVAL;
  1556. struct sem_array *sma;
  1557. struct sembuf fast_sops[SEMOPM_FAST];
  1558. struct sembuf *sops = fast_sops, *sop;
  1559. struct sem_undo *un;
  1560. int undos = 0, alter = 0, max, locknum;
  1561. struct sem_queue queue;
  1562. unsigned long jiffies_left = 0;
  1563. struct ipc_namespace *ns;
  1564. struct list_head tasks;
  1565. ns = current->nsproxy->ipc_ns;
  1566. if (nsops < 1 || semid < 0)
  1567. return -EINVAL;
  1568. if (nsops > ns->sc_semopm)
  1569. return -E2BIG;
  1570. if (nsops > SEMOPM_FAST) {
  1571. sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
  1572. if (sops == NULL)
  1573. return -ENOMEM;
  1574. }
  1575. if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
  1576. error = -EFAULT;
  1577. goto out_free;
  1578. }
  1579. if (timeout) {
  1580. struct timespec _timeout;
  1581. if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
  1582. error = -EFAULT;
  1583. goto out_free;
  1584. }
  1585. if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
  1586. _timeout.tv_nsec >= 1000000000L) {
  1587. error = -EINVAL;
  1588. goto out_free;
  1589. }
  1590. jiffies_left = timespec_to_jiffies(&_timeout);
  1591. }
  1592. max = 0;
  1593. for (sop = sops; sop < sops + nsops; sop++) {
  1594. if (sop->sem_num >= max)
  1595. max = sop->sem_num;
  1596. if (sop->sem_flg & SEM_UNDO)
  1597. undos = 1;
  1598. if (sop->sem_op != 0)
  1599. alter = 1;
  1600. }
  1601. INIT_LIST_HEAD(&tasks);
  1602. if (undos) {
  1603. /* On success, find_alloc_undo takes the rcu_read_lock */
  1604. un = find_alloc_undo(ns, semid);
  1605. if (IS_ERR(un)) {
  1606. error = PTR_ERR(un);
  1607. goto out_free;
  1608. }
  1609. } else {
  1610. un = NULL;
  1611. rcu_read_lock();
  1612. }
  1613. sma = sem_obtain_object_check(ns, semid);
  1614. if (IS_ERR(sma)) {
  1615. rcu_read_unlock();
  1616. error = PTR_ERR(sma);
  1617. goto out_free;
  1618. }
  1619. error = -EFBIG;
  1620. if (max >= sma->sem_nsems)
  1621. goto out_rcu_wakeup;
  1622. error = -EACCES;
  1623. if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
  1624. goto out_rcu_wakeup;
  1625. error = security_sem_semop(sma, sops, nsops, alter);
  1626. if (error)
  1627. goto out_rcu_wakeup;
  1628. error = -EIDRM;
  1629. locknum = sem_lock(sma, sops, nsops);
  1630. /*
  1631. * We eventually might perform the following check in a lockless
  1632. * fashion, considering ipc_valid_object() locking constraints.
  1633. * If nsops == 1 and there is no contention for sem_perm.lock, then
  1634. * only a per-semaphore lock is held and it's OK to proceed with the
  1635. * check below. More details on the fine grained locking scheme
  1636. * entangled here and why it's RMID race safe on comments at sem_lock()
  1637. */
  1638. if (!ipc_valid_object(&sma->sem_perm))
  1639. goto out_unlock_free;
  1640. /*
  1641. * semid identifiers are not unique - find_alloc_undo may have
  1642. * allocated an undo structure, it was invalidated by an RMID
  1643. * and now a new array with received the same id. Check and fail.
  1644. * This case can be detected checking un->semid. The existence of
  1645. * "un" itself is guaranteed by rcu.
  1646. */
  1647. if (un && un->semid == -1)
  1648. goto out_unlock_free;
  1649. queue.sops = sops;
  1650. queue.nsops = nsops;
  1651. queue.undo = un;
  1652. queue.pid = task_tgid_vnr(current);
  1653. queue.alter = alter;
  1654. error = perform_atomic_semop(sma, &queue);
  1655. if (error == 0) {
  1656. /* If the operation was successful, then do
  1657. * the required updates.
  1658. */
  1659. if (alter)
  1660. do_smart_update(sma, sops, nsops, 1, &tasks);
  1661. else
  1662. set_semotime(sma, sops);
  1663. }
  1664. if (error <= 0)
  1665. goto out_unlock_free;
  1666. /* We need to sleep on this operation, so we put the current
  1667. * task into the pending queue and go to sleep.
  1668. */
  1669. if (nsops == 1) {
  1670. struct sem *curr;
  1671. curr = &sma->sem_base[sops->sem_num];
  1672. if (alter) {
  1673. if (sma->complex_count) {
  1674. list_add_tail(&queue.list,
  1675. &sma->pending_alter);
  1676. } else {
  1677. list_add_tail(&queue.list,
  1678. &curr->pending_alter);
  1679. }
  1680. } else {
  1681. list_add_tail(&queue.list, &curr->pending_const);
  1682. }
  1683. } else {
  1684. if (!sma->complex_count)
  1685. merge_queues(sma);
  1686. if (alter)
  1687. list_add_tail(&queue.list, &sma->pending_alter);
  1688. else
  1689. list_add_tail(&queue.list, &sma->pending_const);
  1690. sma->complex_count++;
  1691. }
  1692. queue.status = -EINTR;
  1693. queue.sleeper = current;
  1694. sleep_again:
  1695. current->state = TASK_INTERRUPTIBLE;
  1696. sem_unlock(sma, locknum);
  1697. rcu_read_unlock();
  1698. if (timeout)
  1699. jiffies_left = schedule_timeout(jiffies_left);
  1700. else
  1701. schedule();
  1702. error = get_queue_result(&queue);
  1703. if (error != -EINTR) {
  1704. /* fast path: update_queue already obtained all requested
  1705. * resources.
  1706. * Perform a smp_mb(): User space could assume that semop()
  1707. * is a memory barrier: Without the mb(), the cpu could
  1708. * speculatively read in user space stale data that was
  1709. * overwritten by the previous owner of the semaphore.
  1710. */
  1711. smp_mb();
  1712. goto out_free;
  1713. }
  1714. rcu_read_lock();
  1715. sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
  1716. /*
  1717. * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
  1718. */
  1719. error = get_queue_result(&queue);
  1720. /*
  1721. * Array removed? If yes, leave without sem_unlock().
  1722. */
  1723. if (IS_ERR(sma)) {
  1724. rcu_read_unlock();
  1725. goto out_free;
  1726. }
  1727. /*
  1728. * If queue.status != -EINTR we are woken up by another process.
  1729. * Leave without unlink_queue(), but with sem_unlock().
  1730. */
  1731. if (error != -EINTR)
  1732. goto out_unlock_free;
  1733. /*
  1734. * If an interrupt occurred we have to clean up the queue
  1735. */
  1736. if (timeout && jiffies_left == 0)
  1737. error = -EAGAIN;
  1738. /*
  1739. * If the wakeup was spurious, just retry
  1740. */
  1741. if (error == -EINTR && !signal_pending(current))
  1742. goto sleep_again;
  1743. unlink_queue(sma, &queue);
  1744. out_unlock_free:
  1745. sem_unlock(sma, locknum);
  1746. out_rcu_wakeup:
  1747. rcu_read_unlock();
  1748. wake_up_sem_queue_do(&tasks);
  1749. out_free:
  1750. if (sops != fast_sops)
  1751. kfree(sops);
  1752. return error;
  1753. }
  1754. SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
  1755. unsigned, nsops)
  1756. {
  1757. return sys_semtimedop(semid, tsops, nsops, NULL);
  1758. }
  1759. /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
  1760. * parent and child tasks.
  1761. */
  1762. int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
  1763. {
  1764. struct sem_undo_list *undo_list;
  1765. int error;
  1766. if (clone_flags & CLONE_SYSVSEM) {
  1767. error = get_undo_list(&undo_list);
  1768. if (error)
  1769. return error;
  1770. atomic_inc(&undo_list->refcnt);
  1771. tsk->sysvsem.undo_list = undo_list;
  1772. } else
  1773. tsk->sysvsem.undo_list = NULL;
  1774. return 0;
  1775. }
  1776. /*
  1777. * add semadj values to semaphores, free undo structures.
  1778. * undo structures are not freed when semaphore arrays are destroyed
  1779. * so some of them may be out of date.
  1780. * IMPLEMENTATION NOTE: There is some confusion over whether the
  1781. * set of adjustments that needs to be done should be done in an atomic
  1782. * manner or not. That is, if we are attempting to decrement the semval
  1783. * should we queue up and wait until we can do so legally?
  1784. * The original implementation attempted to do this (queue and wait).
  1785. * The current implementation does not do so. The POSIX standard
  1786. * and SVID should be consulted to determine what behavior is mandated.
  1787. */
  1788. void exit_sem(struct task_struct *tsk)
  1789. {
  1790. struct sem_undo_list *ulp;
  1791. ulp = tsk->sysvsem.undo_list;
  1792. if (!ulp)
  1793. return;
  1794. tsk->sysvsem.undo_list = NULL;
  1795. if (!atomic_dec_and_test(&ulp->refcnt))
  1796. return;
  1797. for (;;) {
  1798. struct sem_array *sma;
  1799. struct sem_undo *un;
  1800. struct list_head tasks;
  1801. int semid, i;
  1802. rcu_read_lock();
  1803. un = list_entry_rcu(ulp->list_proc.next,
  1804. struct sem_undo, list_proc);
  1805. if (&un->list_proc == &ulp->list_proc)
  1806. semid = -1;
  1807. else
  1808. semid = un->semid;
  1809. if (semid == -1) {
  1810. rcu_read_unlock();
  1811. break;
  1812. }
  1813. sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
  1814. /* exit_sem raced with IPC_RMID, nothing to do */
  1815. if (IS_ERR(sma)) {
  1816. rcu_read_unlock();
  1817. continue;
  1818. }
  1819. sem_lock(sma, NULL, -1);
  1820. /* exit_sem raced with IPC_RMID, nothing to do */
  1821. if (!ipc_valid_object(&sma->sem_perm)) {
  1822. sem_unlock(sma, -1);
  1823. rcu_read_unlock();
  1824. continue;
  1825. }
  1826. un = __lookup_undo(ulp, semid);
  1827. if (un == NULL) {
  1828. /* exit_sem raced with IPC_RMID+semget() that created
  1829. * exactly the same semid. Nothing to do.
  1830. */
  1831. sem_unlock(sma, -1);
  1832. rcu_read_unlock();
  1833. continue;
  1834. }
  1835. /* remove un from the linked lists */
  1836. ipc_assert_locked_object(&sma->sem_perm);
  1837. list_del(&un->list_id);
  1838. spin_lock(&ulp->lock);
  1839. list_del_rcu(&un->list_proc);
  1840. spin_unlock(&ulp->lock);
  1841. /* perform adjustments registered in un */
  1842. for (i = 0; i < sma->sem_nsems; i++) {
  1843. struct sem *semaphore = &sma->sem_base[i];
  1844. if (un->semadj[i]) {
  1845. semaphore->semval += un->semadj[i];
  1846. /*
  1847. * Range checks of the new semaphore value,
  1848. * not defined by sus:
  1849. * - Some unices ignore the undo entirely
  1850. * (e.g. HP UX 11i 11.22, Tru64 V5.1)
  1851. * - some cap the value (e.g. FreeBSD caps
  1852. * at 0, but doesn't enforce SEMVMX)
  1853. *
  1854. * Linux caps the semaphore value, both at 0
  1855. * and at SEMVMX.
  1856. *
  1857. * Manfred <manfred@colorfullife.com>
  1858. */
  1859. if (semaphore->semval < 0)
  1860. semaphore->semval = 0;
  1861. if (semaphore->semval > SEMVMX)
  1862. semaphore->semval = SEMVMX;
  1863. semaphore->sempid = task_tgid_vnr(current);
  1864. }
  1865. }
  1866. /* maybe some queued-up processes were waiting for this */
  1867. INIT_LIST_HEAD(&tasks);
  1868. do_smart_update(sma, NULL, 0, 1, &tasks);
  1869. sem_unlock(sma, -1);
  1870. rcu_read_unlock();
  1871. wake_up_sem_queue_do(&tasks);
  1872. kfree_rcu(un, rcu);
  1873. }
  1874. kfree(ulp);
  1875. }
  1876. #ifdef CONFIG_PROC_FS
  1877. static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
  1878. {
  1879. struct user_namespace *user_ns = seq_user_ns(s);
  1880. struct sem_array *sma = it;
  1881. time_t sem_otime;
  1882. /*
  1883. * The proc interface isn't aware of sem_lock(), it calls
  1884. * ipc_lock_object() directly (in sysvipc_find_ipc).
  1885. * In order to stay compatible with sem_lock(), we must wait until
  1886. * all simple semop() calls have left their critical regions.
  1887. */
  1888. sem_wait_array(sma);
  1889. sem_otime = get_semotime(sma);
  1890. return seq_printf(s,
  1891. "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
  1892. sma->sem_perm.key,
  1893. sma->sem_perm.id,
  1894. sma->sem_perm.mode,
  1895. sma->sem_nsems,
  1896. from_kuid_munged(user_ns, sma->sem_perm.uid),
  1897. from_kgid_munged(user_ns, sma->sem_perm.gid),
  1898. from_kuid_munged(user_ns, sma->sem_perm.cuid),
  1899. from_kgid_munged(user_ns, sma->sem_perm.cgid),
  1900. sem_otime,
  1901. sma->sem_ctime);
  1902. }
  1903. #endif