sem.c 44 KB

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