sem.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. /*
  2. * linux/ipc/sem.c
  3. * Copyright (C) 1992 Krishna Balasubramanian
  4. * Copyright (C) 1995 Eric Schenk, Bruno Haible
  5. *
  6. * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  7. *
  8. * SMP-threaded, sysctl's added
  9. * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
  10. * Enforced range limit on SEM_UNDO
  11. * (c) 2001 Red Hat Inc
  12. * Lockless wakeup
  13. * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
  14. * Further wakeup optimizations, documentation
  15. * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
  16. *
  17. * support for audit of ipc object properties and permission changes
  18. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  19. *
  20. * namespaces support
  21. * OpenVZ, SWsoft Inc.
  22. * Pavel Emelianov <xemul@openvz.org>
  23. *
  24. * Implementation notes: (May 2010)
  25. * This file implements System V semaphores.
  26. *
  27. * User space visible behavior:
  28. * - FIFO ordering for semop() operations (just FIFO, not starvation
  29. * protection)
  30. * - multiple semaphore operations that alter the same semaphore in
  31. * one semop() are handled.
  32. * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
  33. * SETALL calls.
  34. * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
  35. * - undo adjustments at process exit are limited to 0..SEMVMX.
  36. * - namespace are supported.
  37. * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
  38. * to /proc/sys/kernel/sem.
  39. * - statistics about the usage are reported in /proc/sysvipc/sem.
  40. *
  41. * Internals:
  42. * - scalability:
  43. * - all global variables are read-mostly.
  44. * - semop() calls and semctl(RMID) are synchronized by RCU.
  45. * - most operations do write operations (actually: spin_lock calls) to
  46. * the per-semaphore array structure.
  47. * Thus: Perfect SMP scaling between independent semaphore arrays.
  48. * If multiple semaphores in one array are used, then cache line
  49. * trashing on the semaphore array spinlock will limit the scaling.
  50. * - semncnt and semzcnt are calculated on demand in count_semncnt() and
  51. * count_semzcnt()
  52. * - the task that performs a successful semop() scans the list of all
  53. * sleeping tasks and completes any pending operations that can be fulfilled.
  54. * Semaphores are actively given to waiting tasks (necessary for FIFO).
  55. * (see update_queue())
  56. * - To improve the scalability, the actual wake-up calls are performed after
  57. * dropping all locks. (see wake_up_sem_queue_prepare(),
  58. * wake_up_sem_queue_do())
  59. * - All work is done by the waker, the woken up task does not have to do
  60. * anything - not even acquiring a lock or dropping a refcount.
  61. * - A woken up task may not even touch the semaphore array anymore, it may
  62. * have been destroyed already by a semctl(RMID).
  63. * - The synchronizations between wake-ups due to a timeout/signal and a
  64. * wake-up due to a completed semaphore operation is achieved by using an
  65. * intermediate state (IN_WAKEUP).
  66. * - UNDO values are stored in an array (one per process and per
  67. * semaphore array, lazily allocated). For backwards compatibility, multiple
  68. * modes for the UNDO variables are supported (per process, per thread)
  69. * (see copy_semundo, CLONE_SYSVSEM)
  70. * - There are two lists of the pending operations: a per-array list
  71. * and per-semaphore list (stored in the array). This allows to achieve FIFO
  72. * ordering without always scanning all pending operations.
  73. * The worst-case behavior is nevertheless O(N^2) for N wakeups.
  74. */
  75. #include <linux/slab.h>
  76. #include <linux/spinlock.h>
  77. #include <linux/init.h>
  78. #include <linux/proc_fs.h>
  79. #include <linux/time.h>
  80. #include <linux/security.h>
  81. #include <linux/syscalls.h>
  82. #include <linux/audit.h>
  83. #include <linux/capability.h>
  84. #include <linux/seq_file.h>
  85. #include <linux/rwsem.h>
  86. #include <linux/nsproxy.h>
  87. #include <linux/ipc_namespace.h>
  88. #include <asm/uaccess.h>
  89. #include "util.h"
  90. /* One semaphore structure for each semaphore in the system. */
  91. struct sem {
  92. int semval; /* current value */
  93. int sempid; /* pid of last operation */
  94. spinlock_t lock; /* spinlock for fine-grained semtimedop */
  95. struct list_head sem_pending; /* pending single-sop operations */
  96. };
  97. /* One queue for each sleeping process in the system. */
  98. struct sem_queue {
  99. struct list_head list; /* queue of pending operations */
  100. struct task_struct *sleeper; /* this process */
  101. struct sem_undo *undo; /* undo structure */
  102. int pid; /* process id of requesting process */
  103. int status; /* completion status of operation */
  104. struct sembuf *sops; /* array of pending operations */
  105. int nsops; /* number of operations */
  106. int alter; /* does *sops alter the array? */
  107. };
  108. /* Each task has a list of undo requests. They are executed automatically
  109. * when the process exits.
  110. */
  111. struct sem_undo {
  112. struct list_head list_proc; /* per-process list: *
  113. * all undos from one process
  114. * rcu protected */
  115. struct rcu_head rcu; /* rcu struct for sem_undo */
  116. struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
  117. struct list_head list_id; /* per semaphore array list:
  118. * all undos for one array */
  119. int semid; /* semaphore set identifier */
  120. short *semadj; /* array of adjustments */
  121. /* one per semaphore */
  122. };
  123. /* sem_undo_list controls shared access to the list of sem_undo structures
  124. * that may be shared among all a CLONE_SYSVSEM task group.
  125. */
  126. struct sem_undo_list {
  127. atomic_t refcnt;
  128. spinlock_t lock;
  129. struct list_head list_proc;
  130. };
  131. #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
  132. #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
  133. static int newary(struct ipc_namespace *, struct ipc_params *);
  134. static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
  135. #ifdef CONFIG_PROC_FS
  136. static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
  137. #endif
  138. #define SEMMSL_FAST 256 /* 512 bytes on stack */
  139. #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
  140. /*
  141. * linked list protection:
  142. * sem_undo.id_next,
  143. * sem_array.sem_pending{,last},
  144. * sem_array.sem_undo: sem_lock() for read/write
  145. * sem_undo.proc_next: only "current" is allowed to read/write that field.
  146. *
  147. */
  148. #define sc_semmsl sem_ctls[0]
  149. #define sc_semmns sem_ctls[1]
  150. #define sc_semopm sem_ctls[2]
  151. #define sc_semmni sem_ctls[3]
  152. void sem_init_ns(struct ipc_namespace *ns)
  153. {
  154. ns->sc_semmsl = SEMMSL;
  155. ns->sc_semmns = SEMMNS;
  156. ns->sc_semopm = SEMOPM;
  157. ns->sc_semmni = SEMMNI;
  158. ns->used_sems = 0;
  159. ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
  160. }
  161. #ifdef CONFIG_IPC_NS
  162. void sem_exit_ns(struct ipc_namespace *ns)
  163. {
  164. free_ipcs(ns, &sem_ids(ns), freeary);
  165. idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
  166. }
  167. #endif
  168. void __init sem_init (void)
  169. {
  170. sem_init_ns(&init_ipc_ns);
  171. ipc_init_proc_interface("sysvipc/sem",
  172. " key semid perms nsems uid gid cuid cgid otime ctime\n",
  173. IPC_SEM_IDS, sysvipc_sem_proc_show);
  174. }
  175. /*
  176. * If the request contains only one semaphore operation, and there are
  177. * no complex transactions pending, lock only the semaphore involved.
  178. * Otherwise, lock the entire semaphore array, since we either have
  179. * multiple semaphores in our own semops, or we need to look at
  180. * semaphores from other pending complex operations.
  181. *
  182. * Carefully guard against sma->complex_count changing between zero
  183. * and non-zero while we are spinning for the lock. The value of
  184. * sma->complex_count cannot change while we are holding the lock,
  185. * so sem_unlock should be fine.
  186. *
  187. * The global lock path checks that all the local locks have been released,
  188. * checking each local lock once. This means that the local lock paths
  189. * cannot start their critical sections while the global lock is held.
  190. */
  191. static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
  192. int nsops)
  193. {
  194. int locknum;
  195. again:
  196. if (nsops == 1 && !sma->complex_count) {
  197. struct sem *sem = sma->sem_base + sops->sem_num;
  198. /* Lock just the semaphore we are interested in. */
  199. spin_lock(&sem->lock);
  200. /*
  201. * If sma->complex_count was set while we were spinning,
  202. * we may need to look at things we did not lock here.
  203. */
  204. if (unlikely(sma->complex_count)) {
  205. spin_unlock(&sem->lock);
  206. goto lock_array;
  207. }
  208. /*
  209. * Another process is holding the global lock on the
  210. * sem_array; we cannot enter our critical section,
  211. * but have to wait for the global lock to be released.
  212. */
  213. if (unlikely(spin_is_locked(&sma->sem_perm.lock))) {
  214. spin_unlock(&sem->lock);
  215. spin_unlock_wait(&sma->sem_perm.lock);
  216. goto again;
  217. }
  218. locknum = sops->sem_num;
  219. } else {
  220. int i;
  221. /*
  222. * Lock the semaphore array, and wait for all of the
  223. * individual semaphore locks to go away. The code
  224. * above ensures no new single-lock holders will enter
  225. * their critical section while the array lock is held.
  226. */
  227. lock_array:
  228. spin_lock(&sma->sem_perm.lock);
  229. for (i = 0; i < sma->sem_nsems; i++) {
  230. struct sem *sem = sma->sem_base + i;
  231. spin_unlock_wait(&sem->lock);
  232. }
  233. locknum = -1;
  234. }
  235. return locknum;
  236. }
  237. static inline void sem_unlock(struct sem_array *sma, int locknum)
  238. {
  239. if (locknum == -1) {
  240. spin_unlock(&sma->sem_perm.lock);
  241. } else {
  242. struct sem *sem = sma->sem_base + locknum;
  243. spin_unlock(&sem->lock);
  244. }
  245. }
  246. /*
  247. * sem_lock_(check_) routines are called in the paths where the rw_mutex
  248. * is not held.
  249. */
  250. static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
  251. int id, struct sembuf *sops, int nsops, int *locknum)
  252. {
  253. struct kern_ipc_perm *ipcp;
  254. struct sem_array *sma;
  255. rcu_read_lock();
  256. ipcp = ipc_obtain_object(&sem_ids(ns), id);
  257. if (IS_ERR(ipcp)) {
  258. sma = ERR_CAST(ipcp);
  259. goto err;
  260. }
  261. sma = container_of(ipcp, struct sem_array, sem_perm);
  262. *locknum = sem_lock(sma, sops, nsops);
  263. /* ipc_rmid() may have already freed the ID while sem_lock
  264. * was spinning: verify that the structure is still valid
  265. */
  266. if (!ipcp->deleted)
  267. return container_of(ipcp, struct sem_array, sem_perm);
  268. sem_unlock(sma, *locknum);
  269. sma = ERR_PTR(-EINVAL);
  270. err:
  271. rcu_read_unlock();
  272. return sma;
  273. }
  274. static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
  275. {
  276. struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
  277. if (IS_ERR(ipcp))
  278. return ERR_CAST(ipcp);
  279. return container_of(ipcp, struct sem_array, sem_perm);
  280. }
  281. static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
  282. int id)
  283. {
  284. struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
  285. if (IS_ERR(ipcp))
  286. return ERR_CAST(ipcp);
  287. return container_of(ipcp, struct sem_array, sem_perm);
  288. }
  289. static inline void sem_lock_and_putref(struct sem_array *sma)
  290. {
  291. sem_lock(sma, NULL, -1);
  292. ipc_rcu_putref(sma);
  293. }
  294. static inline void sem_putref(struct sem_array *sma)
  295. {
  296. ipc_rcu_putref(sma);
  297. }
  298. static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
  299. {
  300. ipc_rmid(&sem_ids(ns), &s->sem_perm);
  301. }
  302. /*
  303. * Lockless wakeup algorithm:
  304. * Without the check/retry algorithm a lockless wakeup is possible:
  305. * - queue.status is initialized to -EINTR before blocking.
  306. * - wakeup is performed by
  307. * * unlinking the queue entry from sma->sem_pending
  308. * * setting queue.status to IN_WAKEUP
  309. * This is the notification for the blocked thread that a
  310. * result value is imminent.
  311. * * call wake_up_process
  312. * * set queue.status to the final value.
  313. * - the previously blocked thread checks queue.status:
  314. * * if it's IN_WAKEUP, then it must wait until the value changes
  315. * * if it's not -EINTR, then the operation was completed by
  316. * update_queue. semtimedop can return queue.status without
  317. * performing any operation on the sem array.
  318. * * otherwise it must acquire the spinlock and check what's up.
  319. *
  320. * The two-stage algorithm is necessary to protect against the following
  321. * races:
  322. * - if queue.status is set after wake_up_process, then the woken up idle
  323. * thread could race forward and try (and fail) to acquire sma->lock
  324. * before update_queue had a chance to set queue.status
  325. * - if queue.status is written before wake_up_process and if the
  326. * blocked process is woken up by a signal between writing
  327. * queue.status and the wake_up_process, then the woken up
  328. * process could return from semtimedop and die by calling
  329. * sys_exit before wake_up_process is called. Then wake_up_process
  330. * will oops, because the task structure is already invalid.
  331. * (yes, this happened on s390 with sysv msg).
  332. *
  333. */
  334. #define IN_WAKEUP 1
  335. /**
  336. * newary - Create a new semaphore set
  337. * @ns: namespace
  338. * @params: ptr to the structure that contains key, semflg and nsems
  339. *
  340. * Called with sem_ids.rw_mutex held (as a writer)
  341. */
  342. static int newary(struct ipc_namespace *ns, struct ipc_params *params)
  343. {
  344. int id;
  345. int retval;
  346. struct sem_array *sma;
  347. int size;
  348. key_t key = params->key;
  349. int nsems = params->u.nsems;
  350. int semflg = params->flg;
  351. int i;
  352. if (!nsems)
  353. return -EINVAL;
  354. if (ns->used_sems + nsems > ns->sc_semmns)
  355. return -ENOSPC;
  356. size = sizeof (*sma) + nsems * sizeof (struct sem);
  357. sma = ipc_rcu_alloc(size);
  358. if (!sma) {
  359. return -ENOMEM;
  360. }
  361. memset (sma, 0, size);
  362. sma->sem_perm.mode = (semflg & S_IRWXUGO);
  363. sma->sem_perm.key = key;
  364. sma->sem_perm.security = NULL;
  365. retval = security_sem_alloc(sma);
  366. if (retval) {
  367. ipc_rcu_putref(sma);
  368. return retval;
  369. }
  370. id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
  371. if (id < 0) {
  372. security_sem_free(sma);
  373. ipc_rcu_putref(sma);
  374. return id;
  375. }
  376. ns->used_sems += nsems;
  377. sma->sem_base = (struct sem *) &sma[1];
  378. for (i = 0; i < nsems; i++) {
  379. INIT_LIST_HEAD(&sma->sem_base[i].sem_pending);
  380. spin_lock_init(&sma->sem_base[i].lock);
  381. }
  382. sma->complex_count = 0;
  383. INIT_LIST_HEAD(&sma->sem_pending);
  384. INIT_LIST_HEAD(&sma->list_id);
  385. sma->sem_nsems = nsems;
  386. sma->sem_ctime = get_seconds();
  387. sem_unlock(sma, -1);
  388. rcu_read_unlock();
  389. return sma->sem_perm.id;
  390. }
  391. /*
  392. * Called with sem_ids.rw_mutex and ipcp locked.
  393. */
  394. static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
  395. {
  396. struct sem_array *sma;
  397. sma = container_of(ipcp, struct sem_array, sem_perm);
  398. return security_sem_associate(sma, semflg);
  399. }
  400. /*
  401. * Called with sem_ids.rw_mutex and ipcp locked.
  402. */
  403. static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
  404. struct ipc_params *params)
  405. {
  406. struct sem_array *sma;
  407. sma = container_of(ipcp, struct sem_array, sem_perm);
  408. if (params->u.nsems > sma->sem_nsems)
  409. return -EINVAL;
  410. return 0;
  411. }
  412. SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
  413. {
  414. struct ipc_namespace *ns;
  415. struct ipc_ops sem_ops;
  416. struct ipc_params sem_params;
  417. ns = current->nsproxy->ipc_ns;
  418. if (nsems < 0 || nsems > ns->sc_semmsl)
  419. return -EINVAL;
  420. sem_ops.getnew = newary;
  421. sem_ops.associate = sem_security;
  422. sem_ops.more_checks = sem_more_checks;
  423. sem_params.key = key;
  424. sem_params.flg = semflg;
  425. sem_params.u.nsems = nsems;
  426. return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
  427. }
  428. /*
  429. * Determine whether a sequence of semaphore operations would succeed
  430. * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
  431. */
  432. static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
  433. int nsops, struct sem_undo *un, int pid)
  434. {
  435. int result, sem_op;
  436. struct sembuf *sop;
  437. struct sem * curr;
  438. for (sop = sops; sop < sops + nsops; sop++) {
  439. curr = sma->sem_base + sop->sem_num;
  440. sem_op = sop->sem_op;
  441. result = curr->semval;
  442. if (!sem_op && result)
  443. goto would_block;
  444. result += sem_op;
  445. if (result < 0)
  446. goto would_block;
  447. if (result > SEMVMX)
  448. goto out_of_range;
  449. if (sop->sem_flg & SEM_UNDO) {
  450. int undo = un->semadj[sop->sem_num] - sem_op;
  451. /*
  452. * Exceeding the undo range is an error.
  453. */
  454. if (undo < (-SEMAEM - 1) || undo > SEMAEM)
  455. goto out_of_range;
  456. }
  457. curr->semval = result;
  458. }
  459. sop--;
  460. while (sop >= sops) {
  461. sma->sem_base[sop->sem_num].sempid = pid;
  462. if (sop->sem_flg & SEM_UNDO)
  463. un->semadj[sop->sem_num] -= sop->sem_op;
  464. sop--;
  465. }
  466. return 0;
  467. out_of_range:
  468. result = -ERANGE;
  469. goto undo;
  470. would_block:
  471. if (sop->sem_flg & IPC_NOWAIT)
  472. result = -EAGAIN;
  473. else
  474. result = 1;
  475. undo:
  476. sop--;
  477. while (sop >= sops) {
  478. sma->sem_base[sop->sem_num].semval -= sop->sem_op;
  479. sop--;
  480. }
  481. return result;
  482. }
  483. /** wake_up_sem_queue_prepare(q, error): Prepare wake-up
  484. * @q: queue entry that must be signaled
  485. * @error: Error value for the signal
  486. *
  487. * Prepare the wake-up of the queue entry q.
  488. */
  489. static void wake_up_sem_queue_prepare(struct list_head *pt,
  490. struct sem_queue *q, int error)
  491. {
  492. if (list_empty(pt)) {
  493. /*
  494. * Hold preempt off so that we don't get preempted and have the
  495. * wakee busy-wait until we're scheduled back on.
  496. */
  497. preempt_disable();
  498. }
  499. q->status = IN_WAKEUP;
  500. q->pid = error;
  501. list_add_tail(&q->list, pt);
  502. }
  503. /**
  504. * wake_up_sem_queue_do(pt) - do the actual wake-up
  505. * @pt: list of tasks to be woken up
  506. *
  507. * Do the actual wake-up.
  508. * The function is called without any locks held, thus the semaphore array
  509. * could be destroyed already and the tasks can disappear as soon as the
  510. * status is set to the actual return code.
  511. */
  512. static void wake_up_sem_queue_do(struct list_head *pt)
  513. {
  514. struct sem_queue *q, *t;
  515. int did_something;
  516. did_something = !list_empty(pt);
  517. list_for_each_entry_safe(q, t, pt, list) {
  518. wake_up_process(q->sleeper);
  519. /* q can disappear immediately after writing q->status. */
  520. smp_wmb();
  521. q->status = q->pid;
  522. }
  523. if (did_something)
  524. preempt_enable();
  525. }
  526. static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
  527. {
  528. list_del(&q->list);
  529. if (q->nsops > 1)
  530. sma->complex_count--;
  531. }
  532. /** check_restart(sma, q)
  533. * @sma: semaphore array
  534. * @q: the operation that just completed
  535. *
  536. * update_queue is O(N^2) when it restarts scanning the whole queue of
  537. * waiting operations. Therefore this function checks if the restart is
  538. * really necessary. It is called after a previously waiting operation
  539. * was completed.
  540. */
  541. static int check_restart(struct sem_array *sma, struct sem_queue *q)
  542. {
  543. struct sem *curr;
  544. struct sem_queue *h;
  545. /* if the operation didn't modify the array, then no restart */
  546. if (q->alter == 0)
  547. return 0;
  548. /* pending complex operations are too difficult to analyse */
  549. if (sma->complex_count)
  550. return 1;
  551. /* we were a sleeping complex operation. Too difficult */
  552. if (q->nsops > 1)
  553. return 1;
  554. curr = sma->sem_base + q->sops[0].sem_num;
  555. /* No-one waits on this queue */
  556. if (list_empty(&curr->sem_pending))
  557. return 0;
  558. /* the new semaphore value */
  559. if (curr->semval) {
  560. /* It is impossible that someone waits for the new value:
  561. * - q is a previously sleeping simple operation that
  562. * altered the array. It must be a decrement, because
  563. * simple increments never sleep.
  564. * - The value is not 0, thus wait-for-zero won't proceed.
  565. * - If there are older (higher priority) decrements
  566. * in the queue, then they have observed the original
  567. * semval value and couldn't proceed. The operation
  568. * decremented to value - thus they won't proceed either.
  569. */
  570. BUG_ON(q->sops[0].sem_op >= 0);
  571. return 0;
  572. }
  573. /*
  574. * semval is 0. Check if there are wait-for-zero semops.
  575. * They must be the first entries in the per-semaphore queue
  576. */
  577. h = list_first_entry(&curr->sem_pending, struct sem_queue, list);
  578. BUG_ON(h->nsops != 1);
  579. BUG_ON(h->sops[0].sem_num != q->sops[0].sem_num);
  580. /* Yes, there is a wait-for-zero semop. Restart */
  581. if (h->sops[0].sem_op == 0)
  582. return 1;
  583. /* Again - no-one is waiting for the new value. */
  584. return 0;
  585. }
  586. /**
  587. * update_queue(sma, semnum): Look for tasks that can be completed.
  588. * @sma: semaphore array.
  589. * @semnum: semaphore that was modified.
  590. * @pt: list head for the tasks that must be woken up.
  591. *
  592. * update_queue must be called after a semaphore in a semaphore array
  593. * was modified. If multiple semaphores were modified, update_queue must
  594. * be called with semnum = -1, as well as with the number of each modified
  595. * semaphore.
  596. * The tasks that must be woken up are added to @pt. The return code
  597. * is stored in q->pid.
  598. * The function return 1 if at least one semop was completed successfully.
  599. */
  600. static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
  601. {
  602. struct sem_queue *q;
  603. struct list_head *walk;
  604. struct list_head *pending_list;
  605. int semop_completed = 0;
  606. if (semnum == -1)
  607. pending_list = &sma->sem_pending;
  608. else
  609. pending_list = &sma->sem_base[semnum].sem_pending;
  610. again:
  611. walk = pending_list->next;
  612. while (walk != pending_list) {
  613. int error, restart;
  614. q = container_of(walk, struct sem_queue, list);
  615. walk = walk->next;
  616. /* If we are scanning the single sop, per-semaphore list of
  617. * one semaphore and that semaphore is 0, then it is not
  618. * necessary to scan the "alter" entries: simple increments
  619. * that affect only one entry succeed immediately and cannot
  620. * be in the per semaphore pending queue, and decrements
  621. * cannot be successful if the value is already 0.
  622. */
  623. if (semnum != -1 && sma->sem_base[semnum].semval == 0 &&
  624. q->alter)
  625. break;
  626. error = try_atomic_semop(sma, q->sops, q->nsops,
  627. q->undo, q->pid);
  628. /* Does q->sleeper still need to sleep? */
  629. if (error > 0)
  630. continue;
  631. unlink_queue(sma, q);
  632. if (error) {
  633. restart = 0;
  634. } else {
  635. semop_completed = 1;
  636. restart = check_restart(sma, q);
  637. }
  638. wake_up_sem_queue_prepare(pt, q, error);
  639. if (restart)
  640. goto again;
  641. }
  642. return semop_completed;
  643. }
  644. /**
  645. * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
  646. * @sma: semaphore array
  647. * @sops: operations that were performed
  648. * @nsops: number of operations
  649. * @otime: force setting otime
  650. * @pt: list head of the tasks that must be woken up.
  651. *
  652. * do_smart_update() does the required called to update_queue, based on the
  653. * actual changes that were performed on the semaphore array.
  654. * Note that the function does not do the actual wake-up: the caller is
  655. * responsible for calling wake_up_sem_queue_do(@pt).
  656. * It is safe to perform this call after dropping all locks.
  657. */
  658. static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
  659. int otime, struct list_head *pt)
  660. {
  661. int i;
  662. if (sma->complex_count || sops == NULL) {
  663. if (update_queue(sma, -1, pt))
  664. otime = 1;
  665. }
  666. if (!sops) {
  667. /* No semops; something special is going on. */
  668. for (i = 0; i < sma->sem_nsems; i++) {
  669. if (update_queue(sma, i, pt))
  670. otime = 1;
  671. }
  672. goto done;
  673. }
  674. /* Check the semaphores that were modified. */
  675. for (i = 0; i < nsops; i++) {
  676. if (sops[i].sem_op > 0 ||
  677. (sops[i].sem_op < 0 &&
  678. sma->sem_base[sops[i].sem_num].semval == 0))
  679. if (update_queue(sma, sops[i].sem_num, pt))
  680. otime = 1;
  681. }
  682. done:
  683. if (otime)
  684. sma->sem_otime = get_seconds();
  685. }
  686. /* The following counts are associated to each semaphore:
  687. * semncnt number of tasks waiting on semval being nonzero
  688. * semzcnt number of tasks waiting on semval being zero
  689. * This model assumes that a task waits on exactly one semaphore.
  690. * Since semaphore operations are to be performed atomically, tasks actually
  691. * wait on a whole sequence of semaphores simultaneously.
  692. * The counts we return here are a rough approximation, but still
  693. * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
  694. */
  695. static int count_semncnt (struct sem_array * sma, ushort semnum)
  696. {
  697. int semncnt;
  698. struct sem_queue * q;
  699. semncnt = 0;
  700. list_for_each_entry(q, &sma->sem_pending, list) {
  701. struct sembuf * sops = q->sops;
  702. int nsops = q->nsops;
  703. int i;
  704. for (i = 0; i < nsops; i++)
  705. if (sops[i].sem_num == semnum
  706. && (sops[i].sem_op < 0)
  707. && !(sops[i].sem_flg & IPC_NOWAIT))
  708. semncnt++;
  709. }
  710. return semncnt;
  711. }
  712. static int count_semzcnt (struct sem_array * sma, ushort semnum)
  713. {
  714. int semzcnt;
  715. struct sem_queue * q;
  716. semzcnt = 0;
  717. list_for_each_entry(q, &sma->sem_pending, list) {
  718. struct sembuf * sops = q->sops;
  719. int nsops = q->nsops;
  720. int i;
  721. for (i = 0; i < nsops; i++)
  722. if (sops[i].sem_num == semnum
  723. && (sops[i].sem_op == 0)
  724. && !(sops[i].sem_flg & IPC_NOWAIT))
  725. semzcnt++;
  726. }
  727. return semzcnt;
  728. }
  729. /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
  730. * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
  731. * remains locked on exit.
  732. */
  733. static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  734. {
  735. struct sem_undo *un, *tu;
  736. struct sem_queue *q, *tq;
  737. struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
  738. struct list_head tasks;
  739. int i;
  740. /* Free the existing undo structures for this semaphore set. */
  741. assert_spin_locked(&sma->sem_perm.lock);
  742. list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
  743. list_del(&un->list_id);
  744. spin_lock(&un->ulp->lock);
  745. un->semid = -1;
  746. list_del_rcu(&un->list_proc);
  747. spin_unlock(&un->ulp->lock);
  748. kfree_rcu(un, rcu);
  749. }
  750. /* Wake up all pending processes and let them fail with EIDRM. */
  751. INIT_LIST_HEAD(&tasks);
  752. list_for_each_entry_safe(q, tq, &sma->sem_pending, list) {
  753. unlink_queue(sma, q);
  754. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  755. }
  756. for (i = 0; i < sma->sem_nsems; i++) {
  757. struct sem *sem = sma->sem_base + i;
  758. list_for_each_entry_safe(q, tq, &sem->sem_pending, list) {
  759. unlink_queue(sma, q);
  760. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  761. }
  762. }
  763. /* Remove the semaphore set from the IDR */
  764. sem_rmid(ns, sma);
  765. sem_unlock(sma, -1);
  766. rcu_read_unlock();
  767. wake_up_sem_queue_do(&tasks);
  768. ns->used_sems -= sma->sem_nsems;
  769. security_sem_free(sma);
  770. ipc_rcu_putref(sma);
  771. }
  772. static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
  773. {
  774. switch(version) {
  775. case IPC_64:
  776. return copy_to_user(buf, in, sizeof(*in));
  777. case IPC_OLD:
  778. {
  779. struct semid_ds out;
  780. memset(&out, 0, sizeof(out));
  781. ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
  782. out.sem_otime = in->sem_otime;
  783. out.sem_ctime = in->sem_ctime;
  784. out.sem_nsems = in->sem_nsems;
  785. return copy_to_user(buf, &out, sizeof(out));
  786. }
  787. default:
  788. return -EINVAL;
  789. }
  790. }
  791. static int semctl_nolock(struct ipc_namespace *ns, int semid,
  792. int cmd, int version, void __user *p)
  793. {
  794. int err;
  795. struct sem_array *sma;
  796. switch(cmd) {
  797. case IPC_INFO:
  798. case SEM_INFO:
  799. {
  800. struct seminfo seminfo;
  801. int max_id;
  802. err = security_sem_semctl(NULL, cmd);
  803. if (err)
  804. return err;
  805. memset(&seminfo,0,sizeof(seminfo));
  806. seminfo.semmni = ns->sc_semmni;
  807. seminfo.semmns = ns->sc_semmns;
  808. seminfo.semmsl = ns->sc_semmsl;
  809. seminfo.semopm = ns->sc_semopm;
  810. seminfo.semvmx = SEMVMX;
  811. seminfo.semmnu = SEMMNU;
  812. seminfo.semmap = SEMMAP;
  813. seminfo.semume = SEMUME;
  814. down_read(&sem_ids(ns).rw_mutex);
  815. if (cmd == SEM_INFO) {
  816. seminfo.semusz = sem_ids(ns).in_use;
  817. seminfo.semaem = ns->used_sems;
  818. } else {
  819. seminfo.semusz = SEMUSZ;
  820. seminfo.semaem = SEMAEM;
  821. }
  822. max_id = ipc_get_maxid(&sem_ids(ns));
  823. up_read(&sem_ids(ns).rw_mutex);
  824. if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
  825. return -EFAULT;
  826. return (max_id < 0) ? 0: max_id;
  827. }
  828. case IPC_STAT:
  829. case SEM_STAT:
  830. {
  831. struct semid64_ds tbuf;
  832. int id = 0;
  833. memset(&tbuf, 0, sizeof(tbuf));
  834. if (cmd == SEM_STAT) {
  835. rcu_read_lock();
  836. sma = sem_obtain_object(ns, semid);
  837. if (IS_ERR(sma)) {
  838. err = PTR_ERR(sma);
  839. goto out_unlock;
  840. }
  841. id = sma->sem_perm.id;
  842. } else {
  843. rcu_read_lock();
  844. sma = sem_obtain_object_check(ns, semid);
  845. if (IS_ERR(sma)) {
  846. err = PTR_ERR(sma);
  847. goto out_unlock;
  848. }
  849. }
  850. err = -EACCES;
  851. if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
  852. goto out_unlock;
  853. err = security_sem_semctl(sma, cmd);
  854. if (err)
  855. goto out_unlock;
  856. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  857. tbuf.sem_otime = sma->sem_otime;
  858. tbuf.sem_ctime = sma->sem_ctime;
  859. tbuf.sem_nsems = sma->sem_nsems;
  860. rcu_read_unlock();
  861. if (copy_semid_to_user(p, &tbuf, version))
  862. return -EFAULT;
  863. return id;
  864. }
  865. default:
  866. return -EINVAL;
  867. }
  868. out_unlock:
  869. rcu_read_unlock();
  870. return err;
  871. }
  872. static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
  873. unsigned long arg)
  874. {
  875. struct sem_undo *un;
  876. struct sem_array *sma;
  877. struct sem* curr;
  878. int err;
  879. struct list_head tasks;
  880. int val;
  881. #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
  882. /* big-endian 64bit */
  883. val = arg >> 32;
  884. #else
  885. /* 32bit or little-endian 64bit */
  886. val = arg;
  887. #endif
  888. if (val > SEMVMX || val < 0)
  889. return -ERANGE;
  890. INIT_LIST_HEAD(&tasks);
  891. rcu_read_lock();
  892. sma = sem_obtain_object_check(ns, semid);
  893. if (IS_ERR(sma)) {
  894. rcu_read_unlock();
  895. return PTR_ERR(sma);
  896. }
  897. if (semnum < 0 || semnum >= sma->sem_nsems) {
  898. rcu_read_unlock();
  899. return -EINVAL;
  900. }
  901. if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
  902. rcu_read_unlock();
  903. return -EACCES;
  904. }
  905. err = security_sem_semctl(sma, SETVAL);
  906. if (err) {
  907. rcu_read_unlock();
  908. return -EACCES;
  909. }
  910. sem_lock(sma, NULL, -1);
  911. curr = &sma->sem_base[semnum];
  912. assert_spin_locked(&sma->sem_perm.lock);
  913. list_for_each_entry(un, &sma->list_id, list_id)
  914. un->semadj[semnum] = 0;
  915. curr->semval = val;
  916. curr->sempid = task_tgid_vnr(current);
  917. sma->sem_ctime = get_seconds();
  918. /* maybe some queued-up processes were waiting for this */
  919. do_smart_update(sma, NULL, 0, 0, &tasks);
  920. sem_unlock(sma, -1);
  921. rcu_read_unlock();
  922. wake_up_sem_queue_do(&tasks);
  923. return 0;
  924. }
  925. static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
  926. int cmd, void __user *p)
  927. {
  928. struct sem_array *sma;
  929. struct sem* curr;
  930. int err, nsems;
  931. ushort fast_sem_io[SEMMSL_FAST];
  932. ushort* sem_io = fast_sem_io;
  933. struct list_head tasks;
  934. INIT_LIST_HEAD(&tasks);
  935. rcu_read_lock();
  936. sma = sem_obtain_object_check(ns, semid);
  937. if (IS_ERR(sma)) {
  938. rcu_read_unlock();
  939. return PTR_ERR(sma);
  940. }
  941. nsems = sma->sem_nsems;
  942. err = -EACCES;
  943. if (ipcperms(ns, &sma->sem_perm,
  944. cmd == SETALL ? S_IWUGO : S_IRUGO)) {
  945. rcu_read_unlock();
  946. goto out_wakeup;
  947. }
  948. err = security_sem_semctl(sma, cmd);
  949. if (err) {
  950. rcu_read_unlock();
  951. goto out_wakeup;
  952. }
  953. err = -EACCES;
  954. switch (cmd) {
  955. case GETALL:
  956. {
  957. ushort __user *array = p;
  958. int i;
  959. sem_lock(sma, NULL, -1);
  960. if(nsems > SEMMSL_FAST) {
  961. if (!ipc_rcu_getref(sma)) {
  962. sem_unlock(sma, -1);
  963. rcu_read_unlock();
  964. err = -EIDRM;
  965. goto out_free;
  966. }
  967. sem_unlock(sma, -1);
  968. rcu_read_unlock();
  969. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  970. if(sem_io == NULL) {
  971. sem_putref(sma);
  972. return -ENOMEM;
  973. }
  974. rcu_read_lock();
  975. sem_lock_and_putref(sma);
  976. if (sma->sem_perm.deleted) {
  977. sem_unlock(sma, -1);
  978. rcu_read_unlock();
  979. err = -EIDRM;
  980. goto out_free;
  981. }
  982. }
  983. for (i = 0; i < sma->sem_nsems; i++)
  984. sem_io[i] = sma->sem_base[i].semval;
  985. sem_unlock(sma, -1);
  986. rcu_read_unlock();
  987. err = 0;
  988. if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
  989. err = -EFAULT;
  990. goto out_free;
  991. }
  992. case SETALL:
  993. {
  994. int i;
  995. struct sem_undo *un;
  996. if (!ipc_rcu_getref(sma)) {
  997. rcu_read_unlock();
  998. return -EIDRM;
  999. }
  1000. rcu_read_unlock();
  1001. if(nsems > SEMMSL_FAST) {
  1002. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  1003. if(sem_io == NULL) {
  1004. sem_putref(sma);
  1005. return -ENOMEM;
  1006. }
  1007. }
  1008. if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
  1009. sem_putref(sma);
  1010. err = -EFAULT;
  1011. goto out_free;
  1012. }
  1013. for (i = 0; i < nsems; i++) {
  1014. if (sem_io[i] > SEMVMX) {
  1015. sem_putref(sma);
  1016. err = -ERANGE;
  1017. goto out_free;
  1018. }
  1019. }
  1020. rcu_read_lock();
  1021. sem_lock_and_putref(sma);
  1022. if (sma->sem_perm.deleted) {
  1023. sem_unlock(sma, -1);
  1024. rcu_read_unlock();
  1025. err = -EIDRM;
  1026. goto out_free;
  1027. }
  1028. for (i = 0; i < nsems; i++)
  1029. sma->sem_base[i].semval = sem_io[i];
  1030. assert_spin_locked(&sma->sem_perm.lock);
  1031. list_for_each_entry(un, &sma->list_id, list_id) {
  1032. for (i = 0; i < nsems; i++)
  1033. un->semadj[i] = 0;
  1034. }
  1035. sma->sem_ctime = get_seconds();
  1036. /* maybe some queued-up processes were waiting for this */
  1037. do_smart_update(sma, NULL, 0, 0, &tasks);
  1038. err = 0;
  1039. goto out_unlock;
  1040. }
  1041. /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
  1042. }
  1043. err = -EINVAL;
  1044. if (semnum < 0 || semnum >= nsems) {
  1045. rcu_read_unlock();
  1046. goto out_wakeup;
  1047. }
  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. rcu_read_unlock();
  1067. out_wakeup:
  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. rcu_read_unlock();
  1396. goto out_wakeup;
  1397. }
  1398. error = -EACCES;
  1399. if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
  1400. rcu_read_unlock();
  1401. goto out_wakeup;
  1402. }
  1403. error = security_sem_semop(sma, sops, nsops, alter);
  1404. if (error) {
  1405. rcu_read_unlock();
  1406. goto out_wakeup;
  1407. }
  1408. /*
  1409. * semid identifiers are not unique - find_alloc_undo may have
  1410. * allocated an undo structure, it was invalidated by an RMID
  1411. * and now a new array with received the same id. Check and fail.
  1412. * This case can be detected checking un->semid. The existence of
  1413. * "un" itself is guaranteed by rcu.
  1414. */
  1415. error = -EIDRM;
  1416. locknum = sem_lock(sma, sops, nsops);
  1417. if (un && un->semid == -1)
  1418. goto out_unlock_free;
  1419. error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
  1420. if (error <= 0) {
  1421. if (alter && error == 0)
  1422. do_smart_update(sma, sops, nsops, 1, &tasks);
  1423. goto out_unlock_free;
  1424. }
  1425. /* We need to sleep on this operation, so we put the current
  1426. * task into the pending queue and go to sleep.
  1427. */
  1428. queue.sops = sops;
  1429. queue.nsops = nsops;
  1430. queue.undo = un;
  1431. queue.pid = task_tgid_vnr(current);
  1432. queue.alter = alter;
  1433. if (nsops == 1) {
  1434. struct sem *curr;
  1435. curr = &sma->sem_base[sops->sem_num];
  1436. if (alter)
  1437. list_add_tail(&queue.list, &curr->sem_pending);
  1438. else
  1439. list_add(&queue.list, &curr->sem_pending);
  1440. } else {
  1441. if (alter)
  1442. list_add_tail(&queue.list, &sma->sem_pending);
  1443. else
  1444. list_add(&queue.list, &sma->sem_pending);
  1445. sma->complex_count++;
  1446. }
  1447. queue.status = -EINTR;
  1448. queue.sleeper = current;
  1449. sleep_again:
  1450. current->state = TASK_INTERRUPTIBLE;
  1451. sem_unlock(sma, locknum);
  1452. rcu_read_unlock();
  1453. if (timeout)
  1454. jiffies_left = schedule_timeout(jiffies_left);
  1455. else
  1456. schedule();
  1457. error = get_queue_result(&queue);
  1458. if (error != -EINTR) {
  1459. /* fast path: update_queue already obtained all requested
  1460. * resources.
  1461. * Perform a smp_mb(): User space could assume that semop()
  1462. * is a memory barrier: Without the mb(), the cpu could
  1463. * speculatively read in user space stale data that was
  1464. * overwritten by the previous owner of the semaphore.
  1465. */
  1466. smp_mb();
  1467. goto out_free;
  1468. }
  1469. sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
  1470. /*
  1471. * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
  1472. */
  1473. error = get_queue_result(&queue);
  1474. /*
  1475. * Array removed? If yes, leave without sem_unlock().
  1476. */
  1477. if (IS_ERR(sma)) {
  1478. goto out_free;
  1479. }
  1480. /*
  1481. * If queue.status != -EINTR we are woken up by another process.
  1482. * Leave without unlink_queue(), but with sem_unlock().
  1483. */
  1484. if (error != -EINTR) {
  1485. goto out_unlock_free;
  1486. }
  1487. /*
  1488. * If an interrupt occurred we have to clean up the queue
  1489. */
  1490. if (timeout && jiffies_left == 0)
  1491. error = -EAGAIN;
  1492. /*
  1493. * If the wakeup was spurious, just retry
  1494. */
  1495. if (error == -EINTR && !signal_pending(current))
  1496. goto sleep_again;
  1497. unlink_queue(sma, &queue);
  1498. out_unlock_free:
  1499. sem_unlock(sma, locknum);
  1500. rcu_read_unlock();
  1501. out_wakeup:
  1502. wake_up_sem_queue_do(&tasks);
  1503. out_free:
  1504. if(sops != fast_sops)
  1505. kfree(sops);
  1506. return error;
  1507. }
  1508. SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
  1509. unsigned, nsops)
  1510. {
  1511. return sys_semtimedop(semid, tsops, nsops, NULL);
  1512. }
  1513. /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
  1514. * parent and child tasks.
  1515. */
  1516. int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
  1517. {
  1518. struct sem_undo_list *undo_list;
  1519. int error;
  1520. if (clone_flags & CLONE_SYSVSEM) {
  1521. error = get_undo_list(&undo_list);
  1522. if (error)
  1523. return error;
  1524. atomic_inc(&undo_list->refcnt);
  1525. tsk->sysvsem.undo_list = undo_list;
  1526. } else
  1527. tsk->sysvsem.undo_list = NULL;
  1528. return 0;
  1529. }
  1530. /*
  1531. * add semadj values to semaphores, free undo structures.
  1532. * undo structures are not freed when semaphore arrays are destroyed
  1533. * so some of them may be out of date.
  1534. * IMPLEMENTATION NOTE: There is some confusion over whether the
  1535. * set of adjustments that needs to be done should be done in an atomic
  1536. * manner or not. That is, if we are attempting to decrement the semval
  1537. * should we queue up and wait until we can do so legally?
  1538. * The original implementation attempted to do this (queue and wait).
  1539. * The current implementation does not do so. The POSIX standard
  1540. * and SVID should be consulted to determine what behavior is mandated.
  1541. */
  1542. void exit_sem(struct task_struct *tsk)
  1543. {
  1544. struct sem_undo_list *ulp;
  1545. ulp = tsk->sysvsem.undo_list;
  1546. if (!ulp)
  1547. return;
  1548. tsk->sysvsem.undo_list = NULL;
  1549. if (!atomic_dec_and_test(&ulp->refcnt))
  1550. return;
  1551. for (;;) {
  1552. struct sem_array *sma;
  1553. struct sem_undo *un;
  1554. struct list_head tasks;
  1555. int semid, i;
  1556. rcu_read_lock();
  1557. un = list_entry_rcu(ulp->list_proc.next,
  1558. struct sem_undo, list_proc);
  1559. if (&un->list_proc == &ulp->list_proc)
  1560. semid = -1;
  1561. else
  1562. semid = un->semid;
  1563. if (semid == -1) {
  1564. rcu_read_unlock();
  1565. break;
  1566. }
  1567. sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
  1568. /* exit_sem raced with IPC_RMID, nothing to do */
  1569. if (IS_ERR(sma)) {
  1570. rcu_read_unlock();
  1571. continue;
  1572. }
  1573. sem_lock(sma, NULL, -1);
  1574. un = __lookup_undo(ulp, semid);
  1575. if (un == NULL) {
  1576. /* exit_sem raced with IPC_RMID+semget() that created
  1577. * exactly the same semid. Nothing to do.
  1578. */
  1579. sem_unlock(sma, -1);
  1580. rcu_read_unlock();
  1581. continue;
  1582. }
  1583. /* remove un from the linked lists */
  1584. assert_spin_locked(&sma->sem_perm.lock);
  1585. list_del(&un->list_id);
  1586. spin_lock(&ulp->lock);
  1587. list_del_rcu(&un->list_proc);
  1588. spin_unlock(&ulp->lock);
  1589. /* perform adjustments registered in un */
  1590. for (i = 0; i < sma->sem_nsems; i++) {
  1591. struct sem * semaphore = &sma->sem_base[i];
  1592. if (un->semadj[i]) {
  1593. semaphore->semval += un->semadj[i];
  1594. /*
  1595. * Range checks of the new semaphore value,
  1596. * not defined by sus:
  1597. * - Some unices ignore the undo entirely
  1598. * (e.g. HP UX 11i 11.22, Tru64 V5.1)
  1599. * - some cap the value (e.g. FreeBSD caps
  1600. * at 0, but doesn't enforce SEMVMX)
  1601. *
  1602. * Linux caps the semaphore value, both at 0
  1603. * and at SEMVMX.
  1604. *
  1605. * Manfred <manfred@colorfullife.com>
  1606. */
  1607. if (semaphore->semval < 0)
  1608. semaphore->semval = 0;
  1609. if (semaphore->semval > SEMVMX)
  1610. semaphore->semval = SEMVMX;
  1611. semaphore->sempid = task_tgid_vnr(current);
  1612. }
  1613. }
  1614. /* maybe some queued-up processes were waiting for this */
  1615. INIT_LIST_HEAD(&tasks);
  1616. do_smart_update(sma, NULL, 0, 1, &tasks);
  1617. sem_unlock(sma, -1);
  1618. rcu_read_unlock();
  1619. wake_up_sem_queue_do(&tasks);
  1620. kfree_rcu(un, rcu);
  1621. }
  1622. kfree(ulp);
  1623. }
  1624. #ifdef CONFIG_PROC_FS
  1625. static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
  1626. {
  1627. struct user_namespace *user_ns = seq_user_ns(s);
  1628. struct sem_array *sma = it;
  1629. return seq_printf(s,
  1630. "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
  1631. sma->sem_perm.key,
  1632. sma->sem_perm.id,
  1633. sma->sem_perm.mode,
  1634. sma->sem_nsems,
  1635. from_kuid_munged(user_ns, sma->sem_perm.uid),
  1636. from_kgid_munged(user_ns, sma->sem_perm.gid),
  1637. from_kuid_munged(user_ns, sma->sem_perm.cuid),
  1638. from_kgid_munged(user_ns, sma->sem_perm.cgid),
  1639. sma->sem_otime,
  1640. sma->sem_ctime);
  1641. }
  1642. #endif