sem.c 44 KB

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