sem.c 52 KB

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