sem.c 55 KB

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