sem.c 46 KB

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