sem.c 56 KB

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