sem.c 47 KB

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