sem.c 56 KB

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