sem.c 53 KB

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