sem.c 47 KB

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