sem.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  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 (!ipcp->deleted)
  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. }
  445. memset (sma, 0, size);
  446. sma->sem_perm.mode = (semflg & S_IRWXUGO);
  447. sma->sem_perm.key = key;
  448. sma->sem_perm.security = NULL;
  449. retval = security_sem_alloc(sma);
  450. if (retval) {
  451. ipc_rcu_putref(sma, ipc_rcu_free);
  452. return retval;
  453. }
  454. id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
  455. if (id < 0) {
  456. ipc_rcu_putref(sma, sem_rcu_free);
  457. return id;
  458. }
  459. ns->used_sems += nsems;
  460. sma->sem_base = (struct sem *) &sma[1];
  461. for (i = 0; i < nsems; i++) {
  462. INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
  463. INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
  464. spin_lock_init(&sma->sem_base[i].lock);
  465. }
  466. sma->complex_count = 0;
  467. INIT_LIST_HEAD(&sma->pending_alter);
  468. INIT_LIST_HEAD(&sma->pending_const);
  469. INIT_LIST_HEAD(&sma->list_id);
  470. sma->sem_nsems = nsems;
  471. sma->sem_ctime = get_seconds();
  472. sem_unlock(sma, -1);
  473. rcu_read_unlock();
  474. return sma->sem_perm.id;
  475. }
  476. /*
  477. * Called with sem_ids.rwsem and ipcp locked.
  478. */
  479. static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
  480. {
  481. struct sem_array *sma;
  482. sma = container_of(ipcp, struct sem_array, sem_perm);
  483. return security_sem_associate(sma, semflg);
  484. }
  485. /*
  486. * Called with sem_ids.rwsem and ipcp locked.
  487. */
  488. static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
  489. struct ipc_params *params)
  490. {
  491. struct sem_array *sma;
  492. sma = container_of(ipcp, struct sem_array, sem_perm);
  493. if (params->u.nsems > sma->sem_nsems)
  494. return -EINVAL;
  495. return 0;
  496. }
  497. SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
  498. {
  499. struct ipc_namespace *ns;
  500. struct ipc_ops sem_ops;
  501. struct ipc_params sem_params;
  502. ns = current->nsproxy->ipc_ns;
  503. if (nsems < 0 || nsems > ns->sc_semmsl)
  504. return -EINVAL;
  505. sem_ops.getnew = newary;
  506. sem_ops.associate = sem_security;
  507. sem_ops.more_checks = sem_more_checks;
  508. sem_params.key = key;
  509. sem_params.flg = semflg;
  510. sem_params.u.nsems = nsems;
  511. return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
  512. }
  513. /** perform_atomic_semop - Perform (if possible) a semaphore operation
  514. * @sma: semaphore array
  515. * @sops: array with operations that should be checked
  516. * @nsems: number of sops
  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. /*
  544. * Exceeding the undo range is an error.
  545. */
  546. if (undo < (-SEMAEM - 1) || undo > SEMAEM)
  547. goto out_of_range;
  548. }
  549. curr->semval = result;
  550. }
  551. sop--;
  552. while (sop >= sops) {
  553. sma->sem_base[sop->sem_num].sempid = pid;
  554. if (sop->sem_flg & SEM_UNDO)
  555. un->semadj[sop->sem_num] -= sop->sem_op;
  556. sop--;
  557. }
  558. return 0;
  559. out_of_range:
  560. result = -ERANGE;
  561. goto undo;
  562. would_block:
  563. if (sop->sem_flg & IPC_NOWAIT)
  564. result = -EAGAIN;
  565. else
  566. result = 1;
  567. undo:
  568. sop--;
  569. while (sop >= sops) {
  570. sma->sem_base[sop->sem_num].semval -= sop->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(pt) - 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(sma, semnum, pt) - 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(sma, sops, nsops, pt) - 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. * do_smart_wakeup_zero() checks all required queue for wait-for-zero
  705. * operations, based on the actual changes that were performed on the
  706. * semaphore array.
  707. * The function returns 1 if at least one operation was completed successfully.
  708. */
  709. static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
  710. int nsops, struct list_head *pt)
  711. {
  712. int i;
  713. int semop_completed = 0;
  714. int got_zero = 0;
  715. /* first: the per-semaphore queues, if known */
  716. if (sops) {
  717. for (i = 0; i < nsops; i++) {
  718. int num = sops[i].sem_num;
  719. if (sma->sem_base[num].semval == 0) {
  720. got_zero = 1;
  721. semop_completed |= wake_const_ops(sma, num, pt);
  722. }
  723. }
  724. } else {
  725. /*
  726. * No sops means modified semaphores not known.
  727. * Assume all were changed.
  728. */
  729. for (i = 0; i < sma->sem_nsems; i++) {
  730. if (sma->sem_base[i].semval == 0) {
  731. got_zero = 1;
  732. semop_completed |= wake_const_ops(sma, i, pt);
  733. }
  734. }
  735. }
  736. /*
  737. * If one of the modified semaphores got 0,
  738. * then check the global queue, too.
  739. */
  740. if (got_zero)
  741. semop_completed |= wake_const_ops(sma, -1, pt);
  742. return semop_completed;
  743. }
  744. /**
  745. * update_queue(sma, semnum): Look for tasks that can be completed.
  746. * @sma: semaphore array.
  747. * @semnum: semaphore that was modified.
  748. * @pt: list head for the tasks that must be woken up.
  749. *
  750. * update_queue must be called after a semaphore in a semaphore array
  751. * was modified. If multiple semaphores were modified, update_queue must
  752. * be called with semnum = -1, as well as with the number of each modified
  753. * semaphore.
  754. * The tasks that must be woken up are added to @pt. The return code
  755. * is stored in q->pid.
  756. * The function internally checks if const operations can now succeed.
  757. *
  758. * The function return 1 if at least one semop was completed successfully.
  759. */
  760. static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
  761. {
  762. struct sem_queue *q;
  763. struct list_head *walk;
  764. struct list_head *pending_list;
  765. int semop_completed = 0;
  766. if (semnum == -1)
  767. pending_list = &sma->pending_alter;
  768. else
  769. pending_list = &sma->sem_base[semnum].pending_alter;
  770. again:
  771. walk = pending_list->next;
  772. while (walk != pending_list) {
  773. int error, restart;
  774. q = container_of(walk, struct sem_queue, list);
  775. walk = walk->next;
  776. /* If we are scanning the single sop, per-semaphore list of
  777. * one semaphore and that semaphore is 0, then it is not
  778. * necessary to scan further: simple increments
  779. * that affect only one entry succeed immediately and cannot
  780. * be in the per semaphore pending queue, and decrements
  781. * cannot be successful if the value is already 0.
  782. */
  783. if (semnum != -1 && sma->sem_base[semnum].semval == 0)
  784. break;
  785. error = perform_atomic_semop(sma, q->sops, q->nsops,
  786. q->undo, q->pid);
  787. /* Does q->sleeper still need to sleep? */
  788. if (error > 0)
  789. continue;
  790. unlink_queue(sma, q);
  791. if (error) {
  792. restart = 0;
  793. } else {
  794. semop_completed = 1;
  795. do_smart_wakeup_zero(sma, q->sops, q->nsops, pt);
  796. restart = check_restart(sma, q);
  797. }
  798. wake_up_sem_queue_prepare(pt, q, error);
  799. if (restart)
  800. goto again;
  801. }
  802. return semop_completed;
  803. }
  804. /**
  805. * set_semotime(sma, sops) - set sem_otime
  806. * @sma: semaphore array
  807. * @sops: operations that modified the array, may be NULL
  808. *
  809. * sem_otime is replicated to avoid cache line trashing.
  810. * This function sets one instance to the current time.
  811. */
  812. static void set_semotime(struct sem_array *sma, struct sembuf *sops)
  813. {
  814. if (sops == NULL) {
  815. sma->sem_base[0].sem_otime = get_seconds();
  816. } else {
  817. sma->sem_base[sops[0].sem_num].sem_otime =
  818. get_seconds();
  819. }
  820. }
  821. /**
  822. * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
  823. * @sma: semaphore array
  824. * @sops: operations that were performed
  825. * @nsops: number of operations
  826. * @otime: force setting otime
  827. * @pt: list head of the tasks that must be woken up.
  828. *
  829. * do_smart_update() does the required calls to update_queue and wakeup_zero,
  830. * based on the actual changes that were performed on the semaphore array.
  831. * Note that the function does not do the actual wake-up: the caller is
  832. * responsible for calling wake_up_sem_queue_do(@pt).
  833. * It is safe to perform this call after dropping all locks.
  834. */
  835. static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
  836. int otime, struct list_head *pt)
  837. {
  838. int i;
  839. otime |= do_smart_wakeup_zero(sma, sops, nsops, pt);
  840. if (!list_empty(&sma->pending_alter)) {
  841. /* semaphore array uses the global queue - just process it. */
  842. otime |= update_queue(sma, -1, pt);
  843. } else {
  844. if (!sops) {
  845. /*
  846. * No sops, thus the modified semaphores are not
  847. * known. Check all.
  848. */
  849. for (i = 0; i < sma->sem_nsems; i++)
  850. otime |= update_queue(sma, i, pt);
  851. } else {
  852. /*
  853. * Check the semaphores that were increased:
  854. * - No complex ops, thus all sleeping ops are
  855. * decrease.
  856. * - if we decreased the value, then any sleeping
  857. * semaphore ops wont be able to run: If the
  858. * previous value was too small, then the new
  859. * value will be too small, too.
  860. */
  861. for (i = 0; i < nsops; i++) {
  862. if (sops[i].sem_op > 0) {
  863. otime |= update_queue(sma,
  864. sops[i].sem_num, pt);
  865. }
  866. }
  867. }
  868. }
  869. if (otime)
  870. set_semotime(sma, sops);
  871. }
  872. /* The following counts are associated to each semaphore:
  873. * semncnt number of tasks waiting on semval being nonzero
  874. * semzcnt number of tasks waiting on semval being zero
  875. * This model assumes that a task waits on exactly one semaphore.
  876. * Since semaphore operations are to be performed atomically, tasks actually
  877. * wait on a whole sequence of semaphores simultaneously.
  878. * The counts we return here are a rough approximation, but still
  879. * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
  880. */
  881. static int count_semncnt (struct sem_array * sma, ushort semnum)
  882. {
  883. int semncnt;
  884. struct sem_queue * q;
  885. semncnt = 0;
  886. list_for_each_entry(q, &sma->sem_base[semnum].pending_alter, list) {
  887. struct sembuf * sops = q->sops;
  888. BUG_ON(sops->sem_num != semnum);
  889. if ((sops->sem_op < 0) && !(sops->sem_flg & IPC_NOWAIT))
  890. semncnt++;
  891. }
  892. list_for_each_entry(q, &sma->pending_alter, list) {
  893. struct sembuf * sops = q->sops;
  894. int nsops = q->nsops;
  895. int i;
  896. for (i = 0; i < nsops; i++)
  897. if (sops[i].sem_num == semnum
  898. && (sops[i].sem_op < 0)
  899. && !(sops[i].sem_flg & IPC_NOWAIT))
  900. semncnt++;
  901. }
  902. return semncnt;
  903. }
  904. static int count_semzcnt (struct sem_array * sma, ushort semnum)
  905. {
  906. int semzcnt;
  907. struct sem_queue * q;
  908. semzcnt = 0;
  909. list_for_each_entry(q, &sma->sem_base[semnum].pending_const, list) {
  910. struct sembuf * sops = q->sops;
  911. BUG_ON(sops->sem_num != semnum);
  912. if ((sops->sem_op == 0) && !(sops->sem_flg & IPC_NOWAIT))
  913. semzcnt++;
  914. }
  915. list_for_each_entry(q, &sma->pending_const, list) {
  916. struct sembuf * sops = q->sops;
  917. int nsops = q->nsops;
  918. int i;
  919. for (i = 0; i < nsops; i++)
  920. if (sops[i].sem_num == semnum
  921. && (sops[i].sem_op == 0)
  922. && !(sops[i].sem_flg & IPC_NOWAIT))
  923. semzcnt++;
  924. }
  925. return semzcnt;
  926. }
  927. /* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
  928. * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
  929. * remains locked on exit.
  930. */
  931. static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  932. {
  933. struct sem_undo *un, *tu;
  934. struct sem_queue *q, *tq;
  935. struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
  936. struct list_head tasks;
  937. int i;
  938. /* Free the existing undo structures for this semaphore set. */
  939. ipc_assert_locked_object(&sma->sem_perm);
  940. list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
  941. list_del(&un->list_id);
  942. spin_lock(&un->ulp->lock);
  943. un->semid = -1;
  944. list_del_rcu(&un->list_proc);
  945. spin_unlock(&un->ulp->lock);
  946. kfree_rcu(un, rcu);
  947. }
  948. /* Wake up all pending processes and let them fail with EIDRM. */
  949. INIT_LIST_HEAD(&tasks);
  950. list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
  951. unlink_queue(sma, q);
  952. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  953. }
  954. list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
  955. unlink_queue(sma, q);
  956. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  957. }
  958. for (i = 0; i < sma->sem_nsems; i++) {
  959. struct sem *sem = sma->sem_base + i;
  960. list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
  961. unlink_queue(sma, q);
  962. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  963. }
  964. list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
  965. unlink_queue(sma, q);
  966. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  967. }
  968. }
  969. /* Remove the semaphore set from the IDR */
  970. sem_rmid(ns, sma);
  971. sem_unlock(sma, -1);
  972. rcu_read_unlock();
  973. wake_up_sem_queue_do(&tasks);
  974. ns->used_sems -= sma->sem_nsems;
  975. ipc_rcu_putref(sma, sem_rcu_free);
  976. }
  977. static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
  978. {
  979. switch(version) {
  980. case IPC_64:
  981. return copy_to_user(buf, in, sizeof(*in));
  982. case IPC_OLD:
  983. {
  984. struct semid_ds out;
  985. memset(&out, 0, sizeof(out));
  986. ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
  987. out.sem_otime = in->sem_otime;
  988. out.sem_ctime = in->sem_ctime;
  989. out.sem_nsems = in->sem_nsems;
  990. return copy_to_user(buf, &out, sizeof(out));
  991. }
  992. default:
  993. return -EINVAL;
  994. }
  995. }
  996. static time_t get_semotime(struct sem_array *sma)
  997. {
  998. int i;
  999. time_t res;
  1000. res = sma->sem_base[0].sem_otime;
  1001. for (i = 1; i < sma->sem_nsems; i++) {
  1002. time_t to = sma->sem_base[i].sem_otime;
  1003. if (to > res)
  1004. res = to;
  1005. }
  1006. return res;
  1007. }
  1008. static int semctl_nolock(struct ipc_namespace *ns, int semid,
  1009. int cmd, int version, void __user *p)
  1010. {
  1011. int err;
  1012. struct sem_array *sma;
  1013. switch(cmd) {
  1014. case IPC_INFO:
  1015. case SEM_INFO:
  1016. {
  1017. struct seminfo seminfo;
  1018. int max_id;
  1019. err = security_sem_semctl(NULL, cmd);
  1020. if (err)
  1021. return err;
  1022. memset(&seminfo,0,sizeof(seminfo));
  1023. seminfo.semmni = ns->sc_semmni;
  1024. seminfo.semmns = ns->sc_semmns;
  1025. seminfo.semmsl = ns->sc_semmsl;
  1026. seminfo.semopm = ns->sc_semopm;
  1027. seminfo.semvmx = SEMVMX;
  1028. seminfo.semmnu = SEMMNU;
  1029. seminfo.semmap = SEMMAP;
  1030. seminfo.semume = SEMUME;
  1031. down_read(&sem_ids(ns).rwsem);
  1032. if (cmd == SEM_INFO) {
  1033. seminfo.semusz = sem_ids(ns).in_use;
  1034. seminfo.semaem = ns->used_sems;
  1035. } else {
  1036. seminfo.semusz = SEMUSZ;
  1037. seminfo.semaem = SEMAEM;
  1038. }
  1039. max_id = ipc_get_maxid(&sem_ids(ns));
  1040. up_read(&sem_ids(ns).rwsem);
  1041. if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
  1042. return -EFAULT;
  1043. return (max_id < 0) ? 0: max_id;
  1044. }
  1045. case IPC_STAT:
  1046. case SEM_STAT:
  1047. {
  1048. struct semid64_ds tbuf;
  1049. int id = 0;
  1050. memset(&tbuf, 0, sizeof(tbuf));
  1051. rcu_read_lock();
  1052. if (cmd == SEM_STAT) {
  1053. sma = sem_obtain_object(ns, semid);
  1054. if (IS_ERR(sma)) {
  1055. err = PTR_ERR(sma);
  1056. goto out_unlock;
  1057. }
  1058. id = sma->sem_perm.id;
  1059. } else {
  1060. sma = sem_obtain_object_check(ns, semid);
  1061. if (IS_ERR(sma)) {
  1062. err = PTR_ERR(sma);
  1063. goto out_unlock;
  1064. }
  1065. }
  1066. err = -EACCES;
  1067. if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
  1068. goto out_unlock;
  1069. err = security_sem_semctl(sma, cmd);
  1070. if (err)
  1071. goto out_unlock;
  1072. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  1073. tbuf.sem_otime = get_semotime(sma);
  1074. tbuf.sem_ctime = sma->sem_ctime;
  1075. tbuf.sem_nsems = sma->sem_nsems;
  1076. rcu_read_unlock();
  1077. if (copy_semid_to_user(p, &tbuf, version))
  1078. return -EFAULT;
  1079. return id;
  1080. }
  1081. default:
  1082. return -EINVAL;
  1083. }
  1084. out_unlock:
  1085. rcu_read_unlock();
  1086. return err;
  1087. }
  1088. static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
  1089. unsigned long arg)
  1090. {
  1091. struct sem_undo *un;
  1092. struct sem_array *sma;
  1093. struct sem* curr;
  1094. int err;
  1095. struct list_head tasks;
  1096. int val;
  1097. #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
  1098. /* big-endian 64bit */
  1099. val = arg >> 32;
  1100. #else
  1101. /* 32bit or little-endian 64bit */
  1102. val = arg;
  1103. #endif
  1104. if (val > SEMVMX || val < 0)
  1105. return -ERANGE;
  1106. INIT_LIST_HEAD(&tasks);
  1107. rcu_read_lock();
  1108. sma = sem_obtain_object_check(ns, semid);
  1109. if (IS_ERR(sma)) {
  1110. rcu_read_unlock();
  1111. return PTR_ERR(sma);
  1112. }
  1113. if (semnum < 0 || semnum >= sma->sem_nsems) {
  1114. rcu_read_unlock();
  1115. return -EINVAL;
  1116. }
  1117. if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
  1118. rcu_read_unlock();
  1119. return -EACCES;
  1120. }
  1121. err = security_sem_semctl(sma, SETVAL);
  1122. if (err) {
  1123. rcu_read_unlock();
  1124. return -EACCES;
  1125. }
  1126. sem_lock(sma, NULL, -1);
  1127. if (sma->sem_perm.deleted) {
  1128. sem_unlock(sma, -1);
  1129. rcu_read_unlock();
  1130. return -EIDRM;
  1131. }
  1132. curr = &sma->sem_base[semnum];
  1133. ipc_assert_locked_object(&sma->sem_perm);
  1134. list_for_each_entry(un, &sma->list_id, list_id)
  1135. un->semadj[semnum] = 0;
  1136. curr->semval = val;
  1137. curr->sempid = task_tgid_vnr(current);
  1138. sma->sem_ctime = get_seconds();
  1139. /* maybe some queued-up processes were waiting for this */
  1140. do_smart_update(sma, NULL, 0, 0, &tasks);
  1141. sem_unlock(sma, -1);
  1142. rcu_read_unlock();
  1143. wake_up_sem_queue_do(&tasks);
  1144. return 0;
  1145. }
  1146. static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
  1147. int cmd, void __user *p)
  1148. {
  1149. struct sem_array *sma;
  1150. struct sem* curr;
  1151. int err, nsems;
  1152. ushort fast_sem_io[SEMMSL_FAST];
  1153. ushort* sem_io = fast_sem_io;
  1154. struct list_head tasks;
  1155. INIT_LIST_HEAD(&tasks);
  1156. rcu_read_lock();
  1157. sma = sem_obtain_object_check(ns, semid);
  1158. if (IS_ERR(sma)) {
  1159. rcu_read_unlock();
  1160. return PTR_ERR(sma);
  1161. }
  1162. nsems = sma->sem_nsems;
  1163. err = -EACCES;
  1164. if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
  1165. goto out_rcu_wakeup;
  1166. err = security_sem_semctl(sma, cmd);
  1167. if (err)
  1168. goto out_rcu_wakeup;
  1169. err = -EACCES;
  1170. switch (cmd) {
  1171. case GETALL:
  1172. {
  1173. ushort __user *array = p;
  1174. int i;
  1175. sem_lock(sma, NULL, -1);
  1176. if (sma->sem_perm.deleted) {
  1177. err = -EIDRM;
  1178. goto out_unlock;
  1179. }
  1180. if(nsems > SEMMSL_FAST) {
  1181. if (!ipc_rcu_getref(sma)) {
  1182. err = -EIDRM;
  1183. goto out_unlock;
  1184. }
  1185. sem_unlock(sma, -1);
  1186. rcu_read_unlock();
  1187. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  1188. if(sem_io == NULL) {
  1189. ipc_rcu_putref(sma, ipc_rcu_free);
  1190. return -ENOMEM;
  1191. }
  1192. rcu_read_lock();
  1193. sem_lock_and_putref(sma);
  1194. if (sma->sem_perm.deleted) {
  1195. err = -EIDRM;
  1196. goto out_unlock;
  1197. }
  1198. }
  1199. for (i = 0; i < sma->sem_nsems; i++)
  1200. sem_io[i] = sma->sem_base[i].semval;
  1201. sem_unlock(sma, -1);
  1202. rcu_read_unlock();
  1203. err = 0;
  1204. if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
  1205. err = -EFAULT;
  1206. goto out_free;
  1207. }
  1208. case SETALL:
  1209. {
  1210. int i;
  1211. struct sem_undo *un;
  1212. if (!ipc_rcu_getref(sma)) {
  1213. err = -EIDRM;
  1214. goto out_rcu_wakeup;
  1215. }
  1216. rcu_read_unlock();
  1217. if(nsems > SEMMSL_FAST) {
  1218. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  1219. if(sem_io == NULL) {
  1220. ipc_rcu_putref(sma, ipc_rcu_free);
  1221. return -ENOMEM;
  1222. }
  1223. }
  1224. if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
  1225. ipc_rcu_putref(sma, ipc_rcu_free);
  1226. err = -EFAULT;
  1227. goto out_free;
  1228. }
  1229. for (i = 0; i < nsems; i++) {
  1230. if (sem_io[i] > SEMVMX) {
  1231. ipc_rcu_putref(sma, ipc_rcu_free);
  1232. err = -ERANGE;
  1233. goto out_free;
  1234. }
  1235. }
  1236. rcu_read_lock();
  1237. sem_lock_and_putref(sma);
  1238. if (sma->sem_perm.deleted) {
  1239. err = -EIDRM;
  1240. goto out_unlock;
  1241. }
  1242. for (i = 0; i < nsems; i++)
  1243. sma->sem_base[i].semval = sem_io[i];
  1244. ipc_assert_locked_object(&sma->sem_perm);
  1245. list_for_each_entry(un, &sma->list_id, list_id) {
  1246. for (i = 0; i < nsems; i++)
  1247. un->semadj[i] = 0;
  1248. }
  1249. sma->sem_ctime = get_seconds();
  1250. /* maybe some queued-up processes were waiting for this */
  1251. do_smart_update(sma, NULL, 0, 0, &tasks);
  1252. err = 0;
  1253. goto out_unlock;
  1254. }
  1255. /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
  1256. }
  1257. err = -EINVAL;
  1258. if (semnum < 0 || semnum >= nsems)
  1259. goto out_rcu_wakeup;
  1260. sem_lock(sma, NULL, -1);
  1261. if (sma->sem_perm.deleted) {
  1262. err = -EIDRM;
  1263. goto out_unlock;
  1264. }
  1265. curr = &sma->sem_base[semnum];
  1266. switch (cmd) {
  1267. case GETVAL:
  1268. err = curr->semval;
  1269. goto out_unlock;
  1270. case GETPID:
  1271. err = curr->sempid;
  1272. goto out_unlock;
  1273. case GETNCNT:
  1274. err = count_semncnt(sma,semnum);
  1275. goto out_unlock;
  1276. case GETZCNT:
  1277. err = count_semzcnt(sma,semnum);
  1278. goto out_unlock;
  1279. }
  1280. out_unlock:
  1281. sem_unlock(sma, -1);
  1282. out_rcu_wakeup:
  1283. rcu_read_unlock();
  1284. wake_up_sem_queue_do(&tasks);
  1285. out_free:
  1286. if(sem_io != fast_sem_io)
  1287. ipc_free(sem_io, sizeof(ushort)*nsems);
  1288. return err;
  1289. }
  1290. static inline unsigned long
  1291. copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
  1292. {
  1293. switch(version) {
  1294. case IPC_64:
  1295. if (copy_from_user(out, buf, sizeof(*out)))
  1296. return -EFAULT;
  1297. return 0;
  1298. case IPC_OLD:
  1299. {
  1300. struct semid_ds tbuf_old;
  1301. if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  1302. return -EFAULT;
  1303. out->sem_perm.uid = tbuf_old.sem_perm.uid;
  1304. out->sem_perm.gid = tbuf_old.sem_perm.gid;
  1305. out->sem_perm.mode = tbuf_old.sem_perm.mode;
  1306. return 0;
  1307. }
  1308. default:
  1309. return -EINVAL;
  1310. }
  1311. }
  1312. /*
  1313. * This function handles some semctl commands which require the rwsem
  1314. * to be held in write mode.
  1315. * NOTE: no locks must be held, the rwsem is taken inside this function.
  1316. */
  1317. static int semctl_down(struct ipc_namespace *ns, int semid,
  1318. int cmd, int version, void __user *p)
  1319. {
  1320. struct sem_array *sma;
  1321. int err;
  1322. struct semid64_ds semid64;
  1323. struct kern_ipc_perm *ipcp;
  1324. if(cmd == IPC_SET) {
  1325. if (copy_semid_from_user(&semid64, p, version))
  1326. return -EFAULT;
  1327. }
  1328. down_write(&sem_ids(ns).rwsem);
  1329. rcu_read_lock();
  1330. ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
  1331. &semid64.sem_perm, 0);
  1332. if (IS_ERR(ipcp)) {
  1333. err = PTR_ERR(ipcp);
  1334. goto out_unlock1;
  1335. }
  1336. sma = container_of(ipcp, struct sem_array, sem_perm);
  1337. err = security_sem_semctl(sma, cmd);
  1338. if (err)
  1339. goto out_unlock1;
  1340. switch (cmd) {
  1341. case IPC_RMID:
  1342. sem_lock(sma, NULL, -1);
  1343. /* freeary unlocks the ipc object and rcu */
  1344. freeary(ns, ipcp);
  1345. goto out_up;
  1346. case IPC_SET:
  1347. sem_lock(sma, NULL, -1);
  1348. err = ipc_update_perm(&semid64.sem_perm, ipcp);
  1349. if (err)
  1350. goto out_unlock0;
  1351. sma->sem_ctime = get_seconds();
  1352. break;
  1353. default:
  1354. err = -EINVAL;
  1355. goto out_unlock1;
  1356. }
  1357. out_unlock0:
  1358. sem_unlock(sma, -1);
  1359. out_unlock1:
  1360. rcu_read_unlock();
  1361. out_up:
  1362. up_write(&sem_ids(ns).rwsem);
  1363. return err;
  1364. }
  1365. SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
  1366. {
  1367. int version;
  1368. struct ipc_namespace *ns;
  1369. void __user *p = (void __user *)arg;
  1370. if (semid < 0)
  1371. return -EINVAL;
  1372. version = ipc_parse_version(&cmd);
  1373. ns = current->nsproxy->ipc_ns;
  1374. switch(cmd) {
  1375. case IPC_INFO:
  1376. case SEM_INFO:
  1377. case IPC_STAT:
  1378. case SEM_STAT:
  1379. return semctl_nolock(ns, semid, cmd, version, p);
  1380. case GETALL:
  1381. case GETVAL:
  1382. case GETPID:
  1383. case GETNCNT:
  1384. case GETZCNT:
  1385. case SETALL:
  1386. return semctl_main(ns, semid, semnum, cmd, p);
  1387. case SETVAL:
  1388. return semctl_setval(ns, semid, semnum, arg);
  1389. case IPC_RMID:
  1390. case IPC_SET:
  1391. return semctl_down(ns, semid, cmd, version, p);
  1392. default:
  1393. return -EINVAL;
  1394. }
  1395. }
  1396. /* If the task doesn't already have a undo_list, then allocate one
  1397. * here. We guarantee there is only one thread using this undo list,
  1398. * and current is THE ONE
  1399. *
  1400. * If this allocation and assignment succeeds, but later
  1401. * portions of this code fail, there is no need to free the sem_undo_list.
  1402. * Just let it stay associated with the task, and it'll be freed later
  1403. * at exit time.
  1404. *
  1405. * This can block, so callers must hold no locks.
  1406. */
  1407. static inline int get_undo_list(struct sem_undo_list **undo_listp)
  1408. {
  1409. struct sem_undo_list *undo_list;
  1410. undo_list = current->sysvsem.undo_list;
  1411. if (!undo_list) {
  1412. undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
  1413. if (undo_list == NULL)
  1414. return -ENOMEM;
  1415. spin_lock_init(&undo_list->lock);
  1416. atomic_set(&undo_list->refcnt, 1);
  1417. INIT_LIST_HEAD(&undo_list->list_proc);
  1418. current->sysvsem.undo_list = undo_list;
  1419. }
  1420. *undo_listp = undo_list;
  1421. return 0;
  1422. }
  1423. static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
  1424. {
  1425. struct sem_undo *un;
  1426. list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
  1427. if (un->semid == semid)
  1428. return un;
  1429. }
  1430. return NULL;
  1431. }
  1432. static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
  1433. {
  1434. struct sem_undo *un;
  1435. assert_spin_locked(&ulp->lock);
  1436. un = __lookup_undo(ulp, semid);
  1437. if (un) {
  1438. list_del_rcu(&un->list_proc);
  1439. list_add_rcu(&un->list_proc, &ulp->list_proc);
  1440. }
  1441. return un;
  1442. }
  1443. /**
  1444. * find_alloc_undo - Lookup (and if not present create) undo array
  1445. * @ns: namespace
  1446. * @semid: semaphore array id
  1447. *
  1448. * The function looks up (and if not present creates) the undo structure.
  1449. * The size of the undo structure depends on the size of the semaphore
  1450. * array, thus the alloc path is not that straightforward.
  1451. * Lifetime-rules: sem_undo is rcu-protected, on success, the function
  1452. * performs a rcu_read_lock().
  1453. */
  1454. static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
  1455. {
  1456. struct sem_array *sma;
  1457. struct sem_undo_list *ulp;
  1458. struct sem_undo *un, *new;
  1459. int nsems, error;
  1460. error = get_undo_list(&ulp);
  1461. if (error)
  1462. return ERR_PTR(error);
  1463. rcu_read_lock();
  1464. spin_lock(&ulp->lock);
  1465. un = lookup_undo(ulp, semid);
  1466. spin_unlock(&ulp->lock);
  1467. if (likely(un!=NULL))
  1468. goto out;
  1469. /* no undo structure around - allocate one. */
  1470. /* step 1: figure out the size of the semaphore array */
  1471. sma = sem_obtain_object_check(ns, semid);
  1472. if (IS_ERR(sma)) {
  1473. rcu_read_unlock();
  1474. return ERR_CAST(sma);
  1475. }
  1476. nsems = sma->sem_nsems;
  1477. if (!ipc_rcu_getref(sma)) {
  1478. rcu_read_unlock();
  1479. un = ERR_PTR(-EIDRM);
  1480. goto out;
  1481. }
  1482. rcu_read_unlock();
  1483. /* step 2: allocate new undo structure */
  1484. new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
  1485. if (!new) {
  1486. ipc_rcu_putref(sma, ipc_rcu_free);
  1487. return ERR_PTR(-ENOMEM);
  1488. }
  1489. /* step 3: Acquire the lock on semaphore array */
  1490. rcu_read_lock();
  1491. sem_lock_and_putref(sma);
  1492. if (sma->sem_perm.deleted) {
  1493. sem_unlock(sma, -1);
  1494. rcu_read_unlock();
  1495. kfree(new);
  1496. un = ERR_PTR(-EIDRM);
  1497. goto out;
  1498. }
  1499. spin_lock(&ulp->lock);
  1500. /*
  1501. * step 4: check for races: did someone else allocate the undo struct?
  1502. */
  1503. un = lookup_undo(ulp, semid);
  1504. if (un) {
  1505. kfree(new);
  1506. goto success;
  1507. }
  1508. /* step 5: initialize & link new undo structure */
  1509. new->semadj = (short *) &new[1];
  1510. new->ulp = ulp;
  1511. new->semid = semid;
  1512. assert_spin_locked(&ulp->lock);
  1513. list_add_rcu(&new->list_proc, &ulp->list_proc);
  1514. ipc_assert_locked_object(&sma->sem_perm);
  1515. list_add(&new->list_id, &sma->list_id);
  1516. un = new;
  1517. success:
  1518. spin_unlock(&ulp->lock);
  1519. sem_unlock(sma, -1);
  1520. out:
  1521. return un;
  1522. }
  1523. /**
  1524. * get_queue_result - Retrieve the result code from sem_queue
  1525. * @q: Pointer to queue structure
  1526. *
  1527. * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
  1528. * q->status, then we must loop until the value is replaced with the final
  1529. * value: This may happen if a task is woken up by an unrelated event (e.g.
  1530. * signal) and in parallel the task is woken up by another task because it got
  1531. * the requested semaphores.
  1532. *
  1533. * The function can be called with or without holding the semaphore spinlock.
  1534. */
  1535. static int get_queue_result(struct sem_queue *q)
  1536. {
  1537. int error;
  1538. error = q->status;
  1539. while (unlikely(error == IN_WAKEUP)) {
  1540. cpu_relax();
  1541. error = q->status;
  1542. }
  1543. return error;
  1544. }
  1545. SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
  1546. unsigned, nsops, const struct timespec __user *, timeout)
  1547. {
  1548. int error = -EINVAL;
  1549. struct sem_array *sma;
  1550. struct sembuf fast_sops[SEMOPM_FAST];
  1551. struct sembuf* sops = fast_sops, *sop;
  1552. struct sem_undo *un;
  1553. int undos = 0, alter = 0, max, locknum;
  1554. struct sem_queue queue;
  1555. unsigned long jiffies_left = 0;
  1556. struct ipc_namespace *ns;
  1557. struct list_head tasks;
  1558. ns = current->nsproxy->ipc_ns;
  1559. if (nsops < 1 || semid < 0)
  1560. return -EINVAL;
  1561. if (nsops > ns->sc_semopm)
  1562. return -E2BIG;
  1563. if(nsops > SEMOPM_FAST) {
  1564. sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
  1565. if(sops==NULL)
  1566. return -ENOMEM;
  1567. }
  1568. if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
  1569. error=-EFAULT;
  1570. goto out_free;
  1571. }
  1572. if (timeout) {
  1573. struct timespec _timeout;
  1574. if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
  1575. error = -EFAULT;
  1576. goto out_free;
  1577. }
  1578. if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
  1579. _timeout.tv_nsec >= 1000000000L) {
  1580. error = -EINVAL;
  1581. goto out_free;
  1582. }
  1583. jiffies_left = timespec_to_jiffies(&_timeout);
  1584. }
  1585. max = 0;
  1586. for (sop = sops; sop < sops + nsops; sop++) {
  1587. if (sop->sem_num >= max)
  1588. max = sop->sem_num;
  1589. if (sop->sem_flg & SEM_UNDO)
  1590. undos = 1;
  1591. if (sop->sem_op != 0)
  1592. alter = 1;
  1593. }
  1594. INIT_LIST_HEAD(&tasks);
  1595. if (undos) {
  1596. /* On success, find_alloc_undo takes the rcu_read_lock */
  1597. un = find_alloc_undo(ns, semid);
  1598. if (IS_ERR(un)) {
  1599. error = PTR_ERR(un);
  1600. goto out_free;
  1601. }
  1602. } else {
  1603. un = NULL;
  1604. rcu_read_lock();
  1605. }
  1606. sma = sem_obtain_object_check(ns, semid);
  1607. if (IS_ERR(sma)) {
  1608. rcu_read_unlock();
  1609. error = PTR_ERR(sma);
  1610. goto out_free;
  1611. }
  1612. error = -EFBIG;
  1613. if (max >= sma->sem_nsems)
  1614. goto out_rcu_wakeup;
  1615. error = -EACCES;
  1616. if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
  1617. goto out_rcu_wakeup;
  1618. error = security_sem_semop(sma, sops, nsops, alter);
  1619. if (error)
  1620. goto out_rcu_wakeup;
  1621. error = -EIDRM;
  1622. locknum = sem_lock(sma, sops, nsops);
  1623. if (sma->sem_perm.deleted)
  1624. goto out_unlock_free;
  1625. /*
  1626. * semid identifiers are not unique - find_alloc_undo may have
  1627. * allocated an undo structure, it was invalidated by an RMID
  1628. * and now a new array with received the same id. Check and fail.
  1629. * This case can be detected checking un->semid. The existence of
  1630. * "un" itself is guaranteed by rcu.
  1631. */
  1632. if (un && un->semid == -1)
  1633. goto out_unlock_free;
  1634. error = perform_atomic_semop(sma, sops, nsops, un,
  1635. task_tgid_vnr(current));
  1636. if (error == 0) {
  1637. /* If the operation was successful, then do
  1638. * the required updates.
  1639. */
  1640. if (alter)
  1641. do_smart_update(sma, sops, nsops, 1, &tasks);
  1642. else
  1643. set_semotime(sma, sops);
  1644. }
  1645. if (error <= 0)
  1646. goto out_unlock_free;
  1647. /* We need to sleep on this operation, so we put the current
  1648. * task into the pending queue and go to sleep.
  1649. */
  1650. queue.sops = sops;
  1651. queue.nsops = nsops;
  1652. queue.undo = un;
  1653. queue.pid = task_tgid_vnr(current);
  1654. queue.alter = alter;
  1655. if (nsops == 1) {
  1656. struct sem *curr;
  1657. curr = &sma->sem_base[sops->sem_num];
  1658. if (alter) {
  1659. if (sma->complex_count) {
  1660. list_add_tail(&queue.list,
  1661. &sma->pending_alter);
  1662. } else {
  1663. list_add_tail(&queue.list,
  1664. &curr->pending_alter);
  1665. }
  1666. } else {
  1667. list_add_tail(&queue.list, &curr->pending_const);
  1668. }
  1669. } else {
  1670. if (!sma->complex_count)
  1671. merge_queues(sma);
  1672. if (alter)
  1673. list_add_tail(&queue.list, &sma->pending_alter);
  1674. else
  1675. list_add_tail(&queue.list, &sma->pending_const);
  1676. sma->complex_count++;
  1677. }
  1678. queue.status = -EINTR;
  1679. queue.sleeper = current;
  1680. sleep_again:
  1681. current->state = TASK_INTERRUPTIBLE;
  1682. sem_unlock(sma, locknum);
  1683. rcu_read_unlock();
  1684. if (timeout)
  1685. jiffies_left = schedule_timeout(jiffies_left);
  1686. else
  1687. schedule();
  1688. error = get_queue_result(&queue);
  1689. if (error != -EINTR) {
  1690. /* fast path: update_queue already obtained all requested
  1691. * resources.
  1692. * Perform a smp_mb(): User space could assume that semop()
  1693. * is a memory barrier: Without the mb(), the cpu could
  1694. * speculatively read in user space stale data that was
  1695. * overwritten by the previous owner of the semaphore.
  1696. */
  1697. smp_mb();
  1698. goto out_free;
  1699. }
  1700. rcu_read_lock();
  1701. sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
  1702. /*
  1703. * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
  1704. */
  1705. error = get_queue_result(&queue);
  1706. /*
  1707. * Array removed? If yes, leave without sem_unlock().
  1708. */
  1709. if (IS_ERR(sma)) {
  1710. rcu_read_unlock();
  1711. goto out_free;
  1712. }
  1713. /*
  1714. * If queue.status != -EINTR we are woken up by another process.
  1715. * Leave without unlink_queue(), but with sem_unlock().
  1716. */
  1717. if (error != -EINTR) {
  1718. goto out_unlock_free;
  1719. }
  1720. /*
  1721. * If an interrupt occurred we have to clean up the queue
  1722. */
  1723. if (timeout && jiffies_left == 0)
  1724. error = -EAGAIN;
  1725. /*
  1726. * If the wakeup was spurious, just retry
  1727. */
  1728. if (error == -EINTR && !signal_pending(current))
  1729. goto sleep_again;
  1730. unlink_queue(sma, &queue);
  1731. out_unlock_free:
  1732. sem_unlock(sma, locknum);
  1733. out_rcu_wakeup:
  1734. rcu_read_unlock();
  1735. wake_up_sem_queue_do(&tasks);
  1736. out_free:
  1737. if(sops != fast_sops)
  1738. kfree(sops);
  1739. return error;
  1740. }
  1741. SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
  1742. unsigned, nsops)
  1743. {
  1744. return sys_semtimedop(semid, tsops, nsops, NULL);
  1745. }
  1746. /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
  1747. * parent and child tasks.
  1748. */
  1749. int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
  1750. {
  1751. struct sem_undo_list *undo_list;
  1752. int error;
  1753. if (clone_flags & CLONE_SYSVSEM) {
  1754. error = get_undo_list(&undo_list);
  1755. if (error)
  1756. return error;
  1757. atomic_inc(&undo_list->refcnt);
  1758. tsk->sysvsem.undo_list = undo_list;
  1759. } else
  1760. tsk->sysvsem.undo_list = NULL;
  1761. return 0;
  1762. }
  1763. /*
  1764. * add semadj values to semaphores, free undo structures.
  1765. * undo structures are not freed when semaphore arrays are destroyed
  1766. * so some of them may be out of date.
  1767. * IMPLEMENTATION NOTE: There is some confusion over whether the
  1768. * set of adjustments that needs to be done should be done in an atomic
  1769. * manner or not. That is, if we are attempting to decrement the semval
  1770. * should we queue up and wait until we can do so legally?
  1771. * The original implementation attempted to do this (queue and wait).
  1772. * The current implementation does not do so. The POSIX standard
  1773. * and SVID should be consulted to determine what behavior is mandated.
  1774. */
  1775. void exit_sem(struct task_struct *tsk)
  1776. {
  1777. struct sem_undo_list *ulp;
  1778. ulp = tsk->sysvsem.undo_list;
  1779. if (!ulp)
  1780. return;
  1781. tsk->sysvsem.undo_list = NULL;
  1782. if (!atomic_dec_and_test(&ulp->refcnt))
  1783. return;
  1784. for (;;) {
  1785. struct sem_array *sma;
  1786. struct sem_undo *un;
  1787. struct list_head tasks;
  1788. int semid, i;
  1789. rcu_read_lock();
  1790. un = list_entry_rcu(ulp->list_proc.next,
  1791. struct sem_undo, list_proc);
  1792. if (&un->list_proc == &ulp->list_proc)
  1793. semid = -1;
  1794. else
  1795. semid = un->semid;
  1796. if (semid == -1) {
  1797. rcu_read_unlock();
  1798. break;
  1799. }
  1800. sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
  1801. /* exit_sem raced with IPC_RMID, nothing to do */
  1802. if (IS_ERR(sma)) {
  1803. rcu_read_unlock();
  1804. continue;
  1805. }
  1806. sem_lock(sma, NULL, -1);
  1807. /* exit_sem raced with IPC_RMID, nothing to do */
  1808. if (sma->sem_perm.deleted) {
  1809. sem_unlock(sma, -1);
  1810. rcu_read_unlock();
  1811. continue;
  1812. }
  1813. un = __lookup_undo(ulp, semid);
  1814. if (un == NULL) {
  1815. /* exit_sem raced with IPC_RMID+semget() that created
  1816. * exactly the same semid. Nothing to do.
  1817. */
  1818. sem_unlock(sma, -1);
  1819. rcu_read_unlock();
  1820. continue;
  1821. }
  1822. /* remove un from the linked lists */
  1823. ipc_assert_locked_object(&sma->sem_perm);
  1824. list_del(&un->list_id);
  1825. spin_lock(&ulp->lock);
  1826. list_del_rcu(&un->list_proc);
  1827. spin_unlock(&ulp->lock);
  1828. /* perform adjustments registered in un */
  1829. for (i = 0; i < sma->sem_nsems; i++) {
  1830. struct sem * semaphore = &sma->sem_base[i];
  1831. if (un->semadj[i]) {
  1832. semaphore->semval += un->semadj[i];
  1833. /*
  1834. * Range checks of the new semaphore value,
  1835. * not defined by sus:
  1836. * - Some unices ignore the undo entirely
  1837. * (e.g. HP UX 11i 11.22, Tru64 V5.1)
  1838. * - some cap the value (e.g. FreeBSD caps
  1839. * at 0, but doesn't enforce SEMVMX)
  1840. *
  1841. * Linux caps the semaphore value, both at 0
  1842. * and at SEMVMX.
  1843. *
  1844. * Manfred <manfred@colorfullife.com>
  1845. */
  1846. if (semaphore->semval < 0)
  1847. semaphore->semval = 0;
  1848. if (semaphore->semval > SEMVMX)
  1849. semaphore->semval = SEMVMX;
  1850. semaphore->sempid = task_tgid_vnr(current);
  1851. }
  1852. }
  1853. /* maybe some queued-up processes were waiting for this */
  1854. INIT_LIST_HEAD(&tasks);
  1855. do_smart_update(sma, NULL, 0, 1, &tasks);
  1856. sem_unlock(sma, -1);
  1857. rcu_read_unlock();
  1858. wake_up_sem_queue_do(&tasks);
  1859. kfree_rcu(un, rcu);
  1860. }
  1861. kfree(ulp);
  1862. }
  1863. #ifdef CONFIG_PROC_FS
  1864. static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
  1865. {
  1866. struct user_namespace *user_ns = seq_user_ns(s);
  1867. struct sem_array *sma = it;
  1868. time_t sem_otime;
  1869. /*
  1870. * The proc interface isn't aware of sem_lock(), it calls
  1871. * ipc_lock_object() directly (in sysvipc_find_ipc).
  1872. * In order to stay compatible with sem_lock(), we must wait until
  1873. * all simple semop() calls have left their critical regions.
  1874. */
  1875. sem_wait_array(sma);
  1876. sem_otime = get_semotime(sma);
  1877. return seq_printf(s,
  1878. "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
  1879. sma->sem_perm.key,
  1880. sma->sem_perm.id,
  1881. sma->sem_perm.mode,
  1882. sma->sem_nsems,
  1883. from_kuid_munged(user_ns, sma->sem_perm.uid),
  1884. from_kgid_munged(user_ns, sma->sem_perm.gid),
  1885. from_kuid_munged(user_ns, sma->sem_perm.cuid),
  1886. from_kgid_munged(user_ns, sma->sem_perm.cgid),
  1887. sem_otime,
  1888. sma->sem_ctime);
  1889. }
  1890. #endif