sem.c 56 KB

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