sem.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /*
  2. * linux/ipc/sem.c
  3. * Copyright (C) 1992 Krishna Balasubramanian
  4. * Copyright (C) 1995 Eric Schenk, Bruno Haible
  5. *
  6. * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
  7. * This code underwent a massive rewrite in order to solve some problems
  8. * with the original code. In particular the original code failed to
  9. * wake up processes that were waiting for semval to go to 0 if the
  10. * value went to 0 and was then incremented rapidly enough. In solving
  11. * this problem I have also modified the implementation so that it
  12. * processes pending operations in a FIFO manner, thus give a guarantee
  13. * that processes waiting for a lock on the semaphore won't starve
  14. * unless another locking process fails to unlock.
  15. * In addition the following two changes in behavior have been introduced:
  16. * - The original implementation of semop returned the value
  17. * last semaphore element examined on success. This does not
  18. * match the manual page specifications, and effectively
  19. * allows the user to read the semaphore even if they do not
  20. * have read permissions. The implementation now returns 0
  21. * on success as stated in the manual page.
  22. * - There is some confusion over whether the set of undo adjustments
  23. * to be performed at exit should be done in an atomic manner.
  24. * That is, if we are attempting to decrement the semval should we queue
  25. * up and wait until we can do so legally?
  26. * The original implementation attempted to do this.
  27. * The current implementation does not do so. This is because I don't
  28. * think it is the right thing (TM) to do, and because I couldn't
  29. * see a clean way to get the old behavior with the new design.
  30. * The POSIX standard and SVID should be consulted to determine
  31. * what behavior is mandated.
  32. *
  33. * Further notes on refinement (Christoph Rohland, December 1998):
  34. * - The POSIX standard says, that the undo adjustments simply should
  35. * redo. So the current implementation is o.K.
  36. * - The previous code had two flaws:
  37. * 1) It actively gave the semaphore to the next waiting process
  38. * sleeping on the semaphore. Since this process did not have the
  39. * cpu this led to many unnecessary context switches and bad
  40. * performance. Now we only check which process should be able to
  41. * get the semaphore and if this process wants to reduce some
  42. * semaphore value we simply wake it up without doing the
  43. * operation. So it has to try to get it later. Thus e.g. the
  44. * running process may reacquire the semaphore during the current
  45. * time slice. If it only waits for zero or increases the semaphore,
  46. * we do the operation in advance and wake it up.
  47. * 2) It did not wake up all zero waiting processes. We try to do
  48. * better but only get the semops right which only wait for zero or
  49. * increase. If there are decrement operations in the operations
  50. * array we do the same as before.
  51. *
  52. * With the incarnation of O(1) scheduler, it becomes unnecessary to perform
  53. * check/retry algorithm for waking up blocked processes as the new scheduler
  54. * is better at handling thread switch than the old one.
  55. *
  56. * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  57. *
  58. * SMP-threaded, sysctl's added
  59. * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
  60. * Enforced range limit on SEM_UNDO
  61. * (c) 2001 Red Hat Inc <alan@redhat.com>
  62. * Lockless wakeup
  63. * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
  64. *
  65. * support for audit of ipc object properties and permission changes
  66. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  67. *
  68. * namespaces support
  69. * OpenVZ, SWsoft Inc.
  70. * Pavel Emelianov <xemul@openvz.org>
  71. */
  72. #include <linux/slab.h>
  73. #include <linux/spinlock.h>
  74. #include <linux/init.h>
  75. #include <linux/proc_fs.h>
  76. #include <linux/time.h>
  77. #include <linux/security.h>
  78. #include <linux/syscalls.h>
  79. #include <linux/audit.h>
  80. #include <linux/capability.h>
  81. #include <linux/seq_file.h>
  82. #include <linux/rwsem.h>
  83. #include <linux/nsproxy.h>
  84. #include <linux/ipc_namespace.h>
  85. #include <asm/uaccess.h>
  86. #include "util.h"
  87. #define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
  88. #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
  89. #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
  90. #define sem_buildid(id, seq) ipc_buildid(id, seq)
  91. static struct ipc_ids init_sem_ids;
  92. static int newary(struct ipc_namespace *, struct ipc_params *);
  93. static void freeary(struct ipc_namespace *, struct sem_array *);
  94. #ifdef CONFIG_PROC_FS
  95. static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
  96. #endif
  97. #define SEMMSL_FAST 256 /* 512 bytes on stack */
  98. #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
  99. /*
  100. * linked list protection:
  101. * sem_undo.id_next,
  102. * sem_array.sem_pending{,last},
  103. * sem_array.sem_undo: sem_lock() for read/write
  104. * sem_undo.proc_next: only "current" is allowed to read/write that field.
  105. *
  106. */
  107. #define sc_semmsl sem_ctls[0]
  108. #define sc_semmns sem_ctls[1]
  109. #define sc_semopm sem_ctls[2]
  110. #define sc_semmni sem_ctls[3]
  111. static void __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
  112. {
  113. ns->ids[IPC_SEM_IDS] = ids;
  114. ns->sc_semmsl = SEMMSL;
  115. ns->sc_semmns = SEMMNS;
  116. ns->sc_semopm = SEMOPM;
  117. ns->sc_semmni = SEMMNI;
  118. ns->used_sems = 0;
  119. ipc_init_ids(ids);
  120. }
  121. #ifdef CONFIG_IPC_NS
  122. int sem_init_ns(struct ipc_namespace *ns)
  123. {
  124. struct ipc_ids *ids;
  125. ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
  126. if (ids == NULL)
  127. return -ENOMEM;
  128. __sem_init_ns(ns, ids);
  129. return 0;
  130. }
  131. void sem_exit_ns(struct ipc_namespace *ns)
  132. {
  133. struct sem_array *sma;
  134. struct kern_ipc_perm *perm;
  135. int next_id;
  136. int total, in_use;
  137. down_write(&sem_ids(ns).rw_mutex);
  138. in_use = sem_ids(ns).in_use;
  139. for (total = 0, next_id = 0; total < in_use; next_id++) {
  140. perm = idr_find(&sem_ids(ns).ipcs_idr, next_id);
  141. if (perm == NULL)
  142. continue;
  143. ipc_lock_by_ptr(perm);
  144. sma = container_of(perm, struct sem_array, sem_perm);
  145. freeary(ns, sma);
  146. total++;
  147. }
  148. up_write(&sem_ids(ns).rw_mutex);
  149. kfree(ns->ids[IPC_SEM_IDS]);
  150. ns->ids[IPC_SEM_IDS] = NULL;
  151. }
  152. #endif
  153. void __init sem_init (void)
  154. {
  155. __sem_init_ns(&init_ipc_ns, &init_sem_ids);
  156. ipc_init_proc_interface("sysvipc/sem",
  157. " key semid perms nsems uid gid cuid cgid otime ctime\n",
  158. IPC_SEM_IDS, sysvipc_sem_proc_show);
  159. }
  160. /*
  161. * This routine is called in the paths where the rw_mutex is held to protect
  162. * access to the idr tree.
  163. */
  164. static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
  165. int id)
  166. {
  167. struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
  168. if (IS_ERR(ipcp))
  169. return (struct sem_array *)ipcp;
  170. return container_of(ipcp, struct sem_array, sem_perm);
  171. }
  172. /*
  173. * sem_lock_(check_) routines are called in the paths where the rw_mutex
  174. * is not held.
  175. */
  176. static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
  177. {
  178. struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
  179. if (IS_ERR(ipcp))
  180. return (struct sem_array *)ipcp;
  181. return container_of(ipcp, struct sem_array, sem_perm);
  182. }
  183. static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
  184. int id)
  185. {
  186. struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
  187. if (IS_ERR(ipcp))
  188. return (struct sem_array *)ipcp;
  189. return container_of(ipcp, struct sem_array, sem_perm);
  190. }
  191. static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
  192. {
  193. ipc_rmid(&sem_ids(ns), &s->sem_perm);
  194. }
  195. /*
  196. * Lockless wakeup algorithm:
  197. * Without the check/retry algorithm a lockless wakeup is possible:
  198. * - queue.status is initialized to -EINTR before blocking.
  199. * - wakeup is performed by
  200. * * unlinking the queue entry from sma->sem_pending
  201. * * setting queue.status to IN_WAKEUP
  202. * This is the notification for the blocked thread that a
  203. * result value is imminent.
  204. * * call wake_up_process
  205. * * set queue.status to the final value.
  206. * - the previously blocked thread checks queue.status:
  207. * * if it's IN_WAKEUP, then it must wait until the value changes
  208. * * if it's not -EINTR, then the operation was completed by
  209. * update_queue. semtimedop can return queue.status without
  210. * performing any operation on the sem array.
  211. * * otherwise it must acquire the spinlock and check what's up.
  212. *
  213. * The two-stage algorithm is necessary to protect against the following
  214. * races:
  215. * - if queue.status is set after wake_up_process, then the woken up idle
  216. * thread could race forward and try (and fail) to acquire sma->lock
  217. * before update_queue had a chance to set queue.status
  218. * - if queue.status is written before wake_up_process and if the
  219. * blocked process is woken up by a signal between writing
  220. * queue.status and the wake_up_process, then the woken up
  221. * process could return from semtimedop and die by calling
  222. * sys_exit before wake_up_process is called. Then wake_up_process
  223. * will oops, because the task structure is already invalid.
  224. * (yes, this happened on s390 with sysv msg).
  225. *
  226. */
  227. #define IN_WAKEUP 1
  228. /**
  229. * newary - Create a new semaphore set
  230. * @ns: namespace
  231. * @params: ptr to the structure that contains key, semflg and nsems
  232. *
  233. * Called with sem_ids.rw_mutex held (as a writer)
  234. */
  235. static int newary(struct ipc_namespace *ns, struct ipc_params *params)
  236. {
  237. int id;
  238. int retval;
  239. struct sem_array *sma;
  240. int size;
  241. key_t key = params->key;
  242. int nsems = params->u.nsems;
  243. int semflg = params->flg;
  244. if (!nsems)
  245. return -EINVAL;
  246. if (ns->used_sems + nsems > ns->sc_semmns)
  247. return -ENOSPC;
  248. size = sizeof (*sma) + nsems * sizeof (struct sem);
  249. sma = ipc_rcu_alloc(size);
  250. if (!sma) {
  251. return -ENOMEM;
  252. }
  253. memset (sma, 0, size);
  254. sma->sem_perm.mode = (semflg & S_IRWXUGO);
  255. sma->sem_perm.key = key;
  256. sma->sem_perm.security = NULL;
  257. retval = security_sem_alloc(sma);
  258. if (retval) {
  259. ipc_rcu_putref(sma);
  260. return retval;
  261. }
  262. id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
  263. if (id < 0) {
  264. security_sem_free(sma);
  265. ipc_rcu_putref(sma);
  266. return id;
  267. }
  268. ns->used_sems += nsems;
  269. sma->sem_perm.id = sem_buildid(id, sma->sem_perm.seq);
  270. sma->sem_base = (struct sem *) &sma[1];
  271. /* sma->sem_pending = NULL; */
  272. sma->sem_pending_last = &sma->sem_pending;
  273. /* sma->undo = NULL; */
  274. sma->sem_nsems = nsems;
  275. sma->sem_ctime = get_seconds();
  276. sem_unlock(sma);
  277. return sma->sem_perm.id;
  278. }
  279. /*
  280. * Called with sem_ids.rw_mutex and ipcp locked.
  281. */
  282. static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
  283. {
  284. struct sem_array *sma;
  285. sma = container_of(ipcp, struct sem_array, sem_perm);
  286. return security_sem_associate(sma, semflg);
  287. }
  288. /*
  289. * Called with sem_ids.rw_mutex and ipcp locked.
  290. */
  291. static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
  292. struct ipc_params *params)
  293. {
  294. struct sem_array *sma;
  295. sma = container_of(ipcp, struct sem_array, sem_perm);
  296. if (params->u.nsems > sma->sem_nsems)
  297. return -EINVAL;
  298. return 0;
  299. }
  300. asmlinkage long sys_semget(key_t key, int nsems, int semflg)
  301. {
  302. struct ipc_namespace *ns;
  303. struct ipc_ops sem_ops;
  304. struct ipc_params sem_params;
  305. ns = current->nsproxy->ipc_ns;
  306. if (nsems < 0 || nsems > ns->sc_semmsl)
  307. return -EINVAL;
  308. sem_ops.getnew = newary;
  309. sem_ops.associate = sem_security;
  310. sem_ops.more_checks = sem_more_checks;
  311. sem_params.key = key;
  312. sem_params.flg = semflg;
  313. sem_params.u.nsems = nsems;
  314. return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
  315. }
  316. /* Manage the doubly linked list sma->sem_pending as a FIFO:
  317. * insert new queue elements at the tail sma->sem_pending_last.
  318. */
  319. static inline void append_to_queue (struct sem_array * sma,
  320. struct sem_queue * q)
  321. {
  322. *(q->prev = sma->sem_pending_last) = q;
  323. *(sma->sem_pending_last = &q->next) = NULL;
  324. }
  325. static inline void prepend_to_queue (struct sem_array * sma,
  326. struct sem_queue * q)
  327. {
  328. q->next = sma->sem_pending;
  329. *(q->prev = &sma->sem_pending) = q;
  330. if (q->next)
  331. q->next->prev = &q->next;
  332. else /* sma->sem_pending_last == &sma->sem_pending */
  333. sma->sem_pending_last = &q->next;
  334. }
  335. static inline void remove_from_queue (struct sem_array * sma,
  336. struct sem_queue * q)
  337. {
  338. *(q->prev) = q->next;
  339. if (q->next)
  340. q->next->prev = q->prev;
  341. else /* sma->sem_pending_last == &q->next */
  342. sma->sem_pending_last = q->prev;
  343. q->prev = NULL; /* mark as removed */
  344. }
  345. /*
  346. * Determine whether a sequence of semaphore operations would succeed
  347. * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
  348. */
  349. static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
  350. int nsops, struct sem_undo *un, int pid)
  351. {
  352. int result, sem_op;
  353. struct sembuf *sop;
  354. struct sem * curr;
  355. for (sop = sops; sop < sops + nsops; sop++) {
  356. curr = sma->sem_base + sop->sem_num;
  357. sem_op = sop->sem_op;
  358. result = curr->semval;
  359. if (!sem_op && result)
  360. goto would_block;
  361. result += sem_op;
  362. if (result < 0)
  363. goto would_block;
  364. if (result > SEMVMX)
  365. goto out_of_range;
  366. if (sop->sem_flg & SEM_UNDO) {
  367. int undo = un->semadj[sop->sem_num] - sem_op;
  368. /*
  369. * Exceeding the undo range is an error.
  370. */
  371. if (undo < (-SEMAEM - 1) || undo > SEMAEM)
  372. goto out_of_range;
  373. }
  374. curr->semval = result;
  375. }
  376. sop--;
  377. while (sop >= sops) {
  378. sma->sem_base[sop->sem_num].sempid = pid;
  379. if (sop->sem_flg & SEM_UNDO)
  380. un->semadj[sop->sem_num] -= sop->sem_op;
  381. sop--;
  382. }
  383. sma->sem_otime = get_seconds();
  384. return 0;
  385. out_of_range:
  386. result = -ERANGE;
  387. goto undo;
  388. would_block:
  389. if (sop->sem_flg & IPC_NOWAIT)
  390. result = -EAGAIN;
  391. else
  392. result = 1;
  393. undo:
  394. sop--;
  395. while (sop >= sops) {
  396. sma->sem_base[sop->sem_num].semval -= sop->sem_op;
  397. sop--;
  398. }
  399. return result;
  400. }
  401. /* Go through the pending queue for the indicated semaphore
  402. * looking for tasks that can be completed.
  403. */
  404. static void update_queue (struct sem_array * sma)
  405. {
  406. int error;
  407. struct sem_queue * q;
  408. q = sma->sem_pending;
  409. while(q) {
  410. error = try_atomic_semop(sma, q->sops, q->nsops,
  411. q->undo, q->pid);
  412. /* Does q->sleeper still need to sleep? */
  413. if (error <= 0) {
  414. struct sem_queue *n;
  415. remove_from_queue(sma,q);
  416. q->status = IN_WAKEUP;
  417. /*
  418. * Continue scanning. The next operation
  419. * that must be checked depends on the type of the
  420. * completed operation:
  421. * - if the operation modified the array, then
  422. * restart from the head of the queue and
  423. * check for threads that might be waiting
  424. * for semaphore values to become 0.
  425. * - if the operation didn't modify the array,
  426. * then just continue.
  427. */
  428. if (q->alter)
  429. n = sma->sem_pending;
  430. else
  431. n = q->next;
  432. wake_up_process(q->sleeper);
  433. /* hands-off: q will disappear immediately after
  434. * writing q->status.
  435. */
  436. smp_wmb();
  437. q->status = error;
  438. q = n;
  439. } else {
  440. q = q->next;
  441. }
  442. }
  443. }
  444. /* The following counts are associated to each semaphore:
  445. * semncnt number of tasks waiting on semval being nonzero
  446. * semzcnt number of tasks waiting on semval being zero
  447. * This model assumes that a task waits on exactly one semaphore.
  448. * Since semaphore operations are to be performed atomically, tasks actually
  449. * wait on a whole sequence of semaphores simultaneously.
  450. * The counts we return here are a rough approximation, but still
  451. * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
  452. */
  453. static int count_semncnt (struct sem_array * sma, ushort semnum)
  454. {
  455. int semncnt;
  456. struct sem_queue * q;
  457. semncnt = 0;
  458. for (q = sma->sem_pending; q; q = q->next) {
  459. struct sembuf * sops = q->sops;
  460. int nsops = q->nsops;
  461. int i;
  462. for (i = 0; i < nsops; i++)
  463. if (sops[i].sem_num == semnum
  464. && (sops[i].sem_op < 0)
  465. && !(sops[i].sem_flg & IPC_NOWAIT))
  466. semncnt++;
  467. }
  468. return semncnt;
  469. }
  470. static int count_semzcnt (struct sem_array * sma, ushort semnum)
  471. {
  472. int semzcnt;
  473. struct sem_queue * q;
  474. semzcnt = 0;
  475. for (q = sma->sem_pending; q; q = q->next) {
  476. struct sembuf * sops = q->sops;
  477. int nsops = q->nsops;
  478. int i;
  479. for (i = 0; i < nsops; i++)
  480. if (sops[i].sem_num == semnum
  481. && (sops[i].sem_op == 0)
  482. && !(sops[i].sem_flg & IPC_NOWAIT))
  483. semzcnt++;
  484. }
  485. return semzcnt;
  486. }
  487. /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
  488. * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
  489. * remains locked on exit.
  490. */
  491. static void freeary(struct ipc_namespace *ns, struct sem_array *sma)
  492. {
  493. struct sem_undo *un;
  494. struct sem_queue *q;
  495. /* Invalidate the existing undo structures for this semaphore set.
  496. * (They will be freed without any further action in exit_sem()
  497. * or during the next semop.)
  498. */
  499. for (un = sma->undo; un; un = un->id_next)
  500. un->semid = -1;
  501. /* Wake up all pending processes and let them fail with EIDRM. */
  502. q = sma->sem_pending;
  503. while(q) {
  504. struct sem_queue *n;
  505. /* lazy remove_from_queue: we are killing the whole queue */
  506. q->prev = NULL;
  507. n = q->next;
  508. q->status = IN_WAKEUP;
  509. wake_up_process(q->sleeper); /* doesn't sleep */
  510. smp_wmb();
  511. q->status = -EIDRM; /* hands-off q */
  512. q = n;
  513. }
  514. /* Remove the semaphore set from the IDR */
  515. sem_rmid(ns, sma);
  516. sem_unlock(sma);
  517. ns->used_sems -= sma->sem_nsems;
  518. security_sem_free(sma);
  519. ipc_rcu_putref(sma);
  520. }
  521. static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
  522. {
  523. switch(version) {
  524. case IPC_64:
  525. return copy_to_user(buf, in, sizeof(*in));
  526. case IPC_OLD:
  527. {
  528. struct semid_ds out;
  529. ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
  530. out.sem_otime = in->sem_otime;
  531. out.sem_ctime = in->sem_ctime;
  532. out.sem_nsems = in->sem_nsems;
  533. return copy_to_user(buf, &out, sizeof(out));
  534. }
  535. default:
  536. return -EINVAL;
  537. }
  538. }
  539. static int semctl_nolock(struct ipc_namespace *ns, int semid,
  540. int cmd, int version, union semun arg)
  541. {
  542. int err = -EINVAL;
  543. struct sem_array *sma;
  544. switch(cmd) {
  545. case IPC_INFO:
  546. case SEM_INFO:
  547. {
  548. struct seminfo seminfo;
  549. int max_id;
  550. err = security_sem_semctl(NULL, cmd);
  551. if (err)
  552. return err;
  553. memset(&seminfo,0,sizeof(seminfo));
  554. seminfo.semmni = ns->sc_semmni;
  555. seminfo.semmns = ns->sc_semmns;
  556. seminfo.semmsl = ns->sc_semmsl;
  557. seminfo.semopm = ns->sc_semopm;
  558. seminfo.semvmx = SEMVMX;
  559. seminfo.semmnu = SEMMNU;
  560. seminfo.semmap = SEMMAP;
  561. seminfo.semume = SEMUME;
  562. down_read(&sem_ids(ns).rw_mutex);
  563. if (cmd == SEM_INFO) {
  564. seminfo.semusz = sem_ids(ns).in_use;
  565. seminfo.semaem = ns->used_sems;
  566. } else {
  567. seminfo.semusz = SEMUSZ;
  568. seminfo.semaem = SEMAEM;
  569. }
  570. max_id = ipc_get_maxid(&sem_ids(ns));
  571. up_read(&sem_ids(ns).rw_mutex);
  572. if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
  573. return -EFAULT;
  574. return (max_id < 0) ? 0: max_id;
  575. }
  576. case IPC_STAT:
  577. case SEM_STAT:
  578. {
  579. struct semid64_ds tbuf;
  580. int id;
  581. if (cmd == SEM_STAT) {
  582. sma = sem_lock(ns, semid);
  583. if (IS_ERR(sma))
  584. return PTR_ERR(sma);
  585. id = sma->sem_perm.id;
  586. } else {
  587. sma = sem_lock_check(ns, semid);
  588. if (IS_ERR(sma))
  589. return PTR_ERR(sma);
  590. id = 0;
  591. }
  592. err = -EACCES;
  593. if (ipcperms (&sma->sem_perm, S_IRUGO))
  594. goto out_unlock;
  595. err = security_sem_semctl(sma, cmd);
  596. if (err)
  597. goto out_unlock;
  598. memset(&tbuf, 0, sizeof(tbuf));
  599. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  600. tbuf.sem_otime = sma->sem_otime;
  601. tbuf.sem_ctime = sma->sem_ctime;
  602. tbuf.sem_nsems = sma->sem_nsems;
  603. sem_unlock(sma);
  604. if (copy_semid_to_user (arg.buf, &tbuf, version))
  605. return -EFAULT;
  606. return id;
  607. }
  608. default:
  609. return -EINVAL;
  610. }
  611. return err;
  612. out_unlock:
  613. sem_unlock(sma);
  614. return err;
  615. }
  616. static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
  617. int cmd, int version, union semun arg)
  618. {
  619. struct sem_array *sma;
  620. struct sem* curr;
  621. int err;
  622. ushort fast_sem_io[SEMMSL_FAST];
  623. ushort* sem_io = fast_sem_io;
  624. int nsems;
  625. sma = sem_lock_check(ns, semid);
  626. if (IS_ERR(sma))
  627. return PTR_ERR(sma);
  628. nsems = sma->sem_nsems;
  629. err = -EACCES;
  630. if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
  631. goto out_unlock;
  632. err = security_sem_semctl(sma, cmd);
  633. if (err)
  634. goto out_unlock;
  635. err = -EACCES;
  636. switch (cmd) {
  637. case GETALL:
  638. {
  639. ushort __user *array = arg.array;
  640. int i;
  641. if(nsems > SEMMSL_FAST) {
  642. ipc_rcu_getref(sma);
  643. sem_unlock(sma);
  644. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  645. if(sem_io == NULL) {
  646. ipc_lock_by_ptr(&sma->sem_perm);
  647. ipc_rcu_putref(sma);
  648. sem_unlock(sma);
  649. return -ENOMEM;
  650. }
  651. ipc_lock_by_ptr(&sma->sem_perm);
  652. ipc_rcu_putref(sma);
  653. if (sma->sem_perm.deleted) {
  654. sem_unlock(sma);
  655. err = -EIDRM;
  656. goto out_free;
  657. }
  658. }
  659. for (i = 0; i < sma->sem_nsems; i++)
  660. sem_io[i] = sma->sem_base[i].semval;
  661. sem_unlock(sma);
  662. err = 0;
  663. if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
  664. err = -EFAULT;
  665. goto out_free;
  666. }
  667. case SETALL:
  668. {
  669. int i;
  670. struct sem_undo *un;
  671. ipc_rcu_getref(sma);
  672. sem_unlock(sma);
  673. if(nsems > SEMMSL_FAST) {
  674. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  675. if(sem_io == NULL) {
  676. ipc_lock_by_ptr(&sma->sem_perm);
  677. ipc_rcu_putref(sma);
  678. sem_unlock(sma);
  679. return -ENOMEM;
  680. }
  681. }
  682. if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
  683. ipc_lock_by_ptr(&sma->sem_perm);
  684. ipc_rcu_putref(sma);
  685. sem_unlock(sma);
  686. err = -EFAULT;
  687. goto out_free;
  688. }
  689. for (i = 0; i < nsems; i++) {
  690. if (sem_io[i] > SEMVMX) {
  691. ipc_lock_by_ptr(&sma->sem_perm);
  692. ipc_rcu_putref(sma);
  693. sem_unlock(sma);
  694. err = -ERANGE;
  695. goto out_free;
  696. }
  697. }
  698. ipc_lock_by_ptr(&sma->sem_perm);
  699. ipc_rcu_putref(sma);
  700. if (sma->sem_perm.deleted) {
  701. sem_unlock(sma);
  702. err = -EIDRM;
  703. goto out_free;
  704. }
  705. for (i = 0; i < nsems; i++)
  706. sma->sem_base[i].semval = sem_io[i];
  707. for (un = sma->undo; un; un = un->id_next)
  708. for (i = 0; i < nsems; i++)
  709. un->semadj[i] = 0;
  710. sma->sem_ctime = get_seconds();
  711. /* maybe some queued-up processes were waiting for this */
  712. update_queue(sma);
  713. err = 0;
  714. goto out_unlock;
  715. }
  716. /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
  717. }
  718. err = -EINVAL;
  719. if(semnum < 0 || semnum >= nsems)
  720. goto out_unlock;
  721. curr = &sma->sem_base[semnum];
  722. switch (cmd) {
  723. case GETVAL:
  724. err = curr->semval;
  725. goto out_unlock;
  726. case GETPID:
  727. err = curr->sempid;
  728. goto out_unlock;
  729. case GETNCNT:
  730. err = count_semncnt(sma,semnum);
  731. goto out_unlock;
  732. case GETZCNT:
  733. err = count_semzcnt(sma,semnum);
  734. goto out_unlock;
  735. case SETVAL:
  736. {
  737. int val = arg.val;
  738. struct sem_undo *un;
  739. err = -ERANGE;
  740. if (val > SEMVMX || val < 0)
  741. goto out_unlock;
  742. for (un = sma->undo; un; un = un->id_next)
  743. un->semadj[semnum] = 0;
  744. curr->semval = val;
  745. curr->sempid = task_tgid_vnr(current);
  746. sma->sem_ctime = get_seconds();
  747. /* maybe some queued-up processes were waiting for this */
  748. update_queue(sma);
  749. err = 0;
  750. goto out_unlock;
  751. }
  752. }
  753. out_unlock:
  754. sem_unlock(sma);
  755. out_free:
  756. if(sem_io != fast_sem_io)
  757. ipc_free(sem_io, sizeof(ushort)*nsems);
  758. return err;
  759. }
  760. struct sem_setbuf {
  761. uid_t uid;
  762. gid_t gid;
  763. mode_t mode;
  764. };
  765. static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
  766. {
  767. switch(version) {
  768. case IPC_64:
  769. {
  770. struct semid64_ds tbuf;
  771. if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
  772. return -EFAULT;
  773. out->uid = tbuf.sem_perm.uid;
  774. out->gid = tbuf.sem_perm.gid;
  775. out->mode = tbuf.sem_perm.mode;
  776. return 0;
  777. }
  778. case IPC_OLD:
  779. {
  780. struct semid_ds tbuf_old;
  781. if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  782. return -EFAULT;
  783. out->uid = tbuf_old.sem_perm.uid;
  784. out->gid = tbuf_old.sem_perm.gid;
  785. out->mode = tbuf_old.sem_perm.mode;
  786. return 0;
  787. }
  788. default:
  789. return -EINVAL;
  790. }
  791. }
  792. static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
  793. int cmd, int version, union semun arg)
  794. {
  795. struct sem_array *sma;
  796. int err;
  797. struct sem_setbuf uninitialized_var(setbuf);
  798. struct kern_ipc_perm *ipcp;
  799. if(cmd == IPC_SET) {
  800. if(copy_semid_from_user (&setbuf, arg.buf, version))
  801. return -EFAULT;
  802. }
  803. sma = sem_lock_check_down(ns, semid);
  804. if (IS_ERR(sma))
  805. return PTR_ERR(sma);
  806. ipcp = &sma->sem_perm;
  807. err = audit_ipc_obj(ipcp);
  808. if (err)
  809. goto out_unlock;
  810. if (cmd == IPC_SET) {
  811. err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
  812. if (err)
  813. goto out_unlock;
  814. }
  815. if (current->euid != ipcp->cuid &&
  816. current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
  817. err=-EPERM;
  818. goto out_unlock;
  819. }
  820. err = security_sem_semctl(sma, cmd);
  821. if (err)
  822. goto out_unlock;
  823. switch(cmd){
  824. case IPC_RMID:
  825. freeary(ns, sma);
  826. err = 0;
  827. break;
  828. case IPC_SET:
  829. ipcp->uid = setbuf.uid;
  830. ipcp->gid = setbuf.gid;
  831. ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
  832. | (setbuf.mode & S_IRWXUGO);
  833. sma->sem_ctime = get_seconds();
  834. sem_unlock(sma);
  835. err = 0;
  836. break;
  837. default:
  838. sem_unlock(sma);
  839. err = -EINVAL;
  840. break;
  841. }
  842. return err;
  843. out_unlock:
  844. sem_unlock(sma);
  845. return err;
  846. }
  847. asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
  848. {
  849. int err = -EINVAL;
  850. int version;
  851. struct ipc_namespace *ns;
  852. if (semid < 0)
  853. return -EINVAL;
  854. version = ipc_parse_version(&cmd);
  855. ns = current->nsproxy->ipc_ns;
  856. switch(cmd) {
  857. case IPC_INFO:
  858. case SEM_INFO:
  859. case IPC_STAT:
  860. case SEM_STAT:
  861. err = semctl_nolock(ns, semid, cmd, version, arg);
  862. return err;
  863. case GETALL:
  864. case GETVAL:
  865. case GETPID:
  866. case GETNCNT:
  867. case GETZCNT:
  868. case SETVAL:
  869. case SETALL:
  870. err = semctl_main(ns,semid,semnum,cmd,version,arg);
  871. return err;
  872. case IPC_RMID:
  873. case IPC_SET:
  874. down_write(&sem_ids(ns).rw_mutex);
  875. err = semctl_down(ns,semid,semnum,cmd,version,arg);
  876. up_write(&sem_ids(ns).rw_mutex);
  877. return err;
  878. default:
  879. return -EINVAL;
  880. }
  881. }
  882. /* If the task doesn't already have a undo_list, then allocate one
  883. * here. We guarantee there is only one thread using this undo list,
  884. * and current is THE ONE
  885. *
  886. * If this allocation and assignment succeeds, but later
  887. * portions of this code fail, there is no need to free the sem_undo_list.
  888. * Just let it stay associated with the task, and it'll be freed later
  889. * at exit time.
  890. *
  891. * This can block, so callers must hold no locks.
  892. */
  893. static inline int get_undo_list(struct sem_undo_list **undo_listp)
  894. {
  895. struct sem_undo_list *undo_list;
  896. undo_list = current->sysvsem.undo_list;
  897. if (!undo_list) {
  898. undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
  899. if (undo_list == NULL)
  900. return -ENOMEM;
  901. spin_lock_init(&undo_list->lock);
  902. atomic_set(&undo_list->refcnt, 1);
  903. current->sysvsem.undo_list = undo_list;
  904. }
  905. *undo_listp = undo_list;
  906. return 0;
  907. }
  908. static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
  909. {
  910. struct sem_undo **last, *un;
  911. last = &ulp->proc_list;
  912. un = *last;
  913. while(un != NULL) {
  914. if(un->semid==semid)
  915. break;
  916. if(un->semid==-1) {
  917. *last=un->proc_next;
  918. kfree(un);
  919. } else {
  920. last=&un->proc_next;
  921. }
  922. un=*last;
  923. }
  924. return un;
  925. }
  926. static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
  927. {
  928. struct sem_array *sma;
  929. struct sem_undo_list *ulp;
  930. struct sem_undo *un, *new;
  931. int nsems;
  932. int error;
  933. error = get_undo_list(&ulp);
  934. if (error)
  935. return ERR_PTR(error);
  936. spin_lock(&ulp->lock);
  937. un = lookup_undo(ulp, semid);
  938. spin_unlock(&ulp->lock);
  939. if (likely(un!=NULL))
  940. goto out;
  941. /* no undo structure around - allocate one. */
  942. sma = sem_lock_check(ns, semid);
  943. if (IS_ERR(sma))
  944. return ERR_PTR(PTR_ERR(sma));
  945. nsems = sma->sem_nsems;
  946. ipc_rcu_getref(sma);
  947. sem_unlock(sma);
  948. new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
  949. if (!new) {
  950. ipc_lock_by_ptr(&sma->sem_perm);
  951. ipc_rcu_putref(sma);
  952. sem_unlock(sma);
  953. return ERR_PTR(-ENOMEM);
  954. }
  955. new->semadj = (short *) &new[1];
  956. new->semid = semid;
  957. spin_lock(&ulp->lock);
  958. un = lookup_undo(ulp, semid);
  959. if (un) {
  960. spin_unlock(&ulp->lock);
  961. kfree(new);
  962. ipc_lock_by_ptr(&sma->sem_perm);
  963. ipc_rcu_putref(sma);
  964. sem_unlock(sma);
  965. goto out;
  966. }
  967. ipc_lock_by_ptr(&sma->sem_perm);
  968. ipc_rcu_putref(sma);
  969. if (sma->sem_perm.deleted) {
  970. sem_unlock(sma);
  971. spin_unlock(&ulp->lock);
  972. kfree(new);
  973. un = ERR_PTR(-EIDRM);
  974. goto out;
  975. }
  976. new->proc_next = ulp->proc_list;
  977. ulp->proc_list = new;
  978. new->id_next = sma->undo;
  979. sma->undo = new;
  980. sem_unlock(sma);
  981. un = new;
  982. spin_unlock(&ulp->lock);
  983. out:
  984. return un;
  985. }
  986. asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
  987. unsigned nsops, const struct timespec __user *timeout)
  988. {
  989. int error = -EINVAL;
  990. struct sem_array *sma;
  991. struct sembuf fast_sops[SEMOPM_FAST];
  992. struct sembuf* sops = fast_sops, *sop;
  993. struct sem_undo *un;
  994. int undos = 0, alter = 0, max;
  995. struct sem_queue queue;
  996. unsigned long jiffies_left = 0;
  997. struct ipc_namespace *ns;
  998. ns = current->nsproxy->ipc_ns;
  999. if (nsops < 1 || semid < 0)
  1000. return -EINVAL;
  1001. if (nsops > ns->sc_semopm)
  1002. return -E2BIG;
  1003. if(nsops > SEMOPM_FAST) {
  1004. sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
  1005. if(sops==NULL)
  1006. return -ENOMEM;
  1007. }
  1008. if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
  1009. error=-EFAULT;
  1010. goto out_free;
  1011. }
  1012. if (timeout) {
  1013. struct timespec _timeout;
  1014. if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
  1015. error = -EFAULT;
  1016. goto out_free;
  1017. }
  1018. if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
  1019. _timeout.tv_nsec >= 1000000000L) {
  1020. error = -EINVAL;
  1021. goto out_free;
  1022. }
  1023. jiffies_left = timespec_to_jiffies(&_timeout);
  1024. }
  1025. max = 0;
  1026. for (sop = sops; sop < sops + nsops; sop++) {
  1027. if (sop->sem_num >= max)
  1028. max = sop->sem_num;
  1029. if (sop->sem_flg & SEM_UNDO)
  1030. undos = 1;
  1031. if (sop->sem_op != 0)
  1032. alter = 1;
  1033. }
  1034. retry_undos:
  1035. if (undos) {
  1036. un = find_undo(ns, semid);
  1037. if (IS_ERR(un)) {
  1038. error = PTR_ERR(un);
  1039. goto out_free;
  1040. }
  1041. } else
  1042. un = NULL;
  1043. sma = sem_lock_check(ns, semid);
  1044. if (IS_ERR(sma)) {
  1045. error = PTR_ERR(sma);
  1046. goto out_free;
  1047. }
  1048. /*
  1049. * semid identifiers are not unique - find_undo may have
  1050. * allocated an undo structure, it was invalidated by an RMID
  1051. * and now a new array with received the same id. Check and retry.
  1052. */
  1053. if (un && un->semid == -1) {
  1054. sem_unlock(sma);
  1055. goto retry_undos;
  1056. }
  1057. error = -EFBIG;
  1058. if (max >= sma->sem_nsems)
  1059. goto out_unlock_free;
  1060. error = -EACCES;
  1061. if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
  1062. goto out_unlock_free;
  1063. error = security_sem_semop(sma, sops, nsops, alter);
  1064. if (error)
  1065. goto out_unlock_free;
  1066. error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
  1067. if (error <= 0) {
  1068. if (alter && error == 0)
  1069. update_queue (sma);
  1070. goto out_unlock_free;
  1071. }
  1072. /* We need to sleep on this operation, so we put the current
  1073. * task into the pending queue and go to sleep.
  1074. */
  1075. queue.sma = sma;
  1076. queue.sops = sops;
  1077. queue.nsops = nsops;
  1078. queue.undo = un;
  1079. queue.pid = task_tgid_vnr(current);
  1080. queue.id = semid;
  1081. queue.alter = alter;
  1082. if (alter)
  1083. append_to_queue(sma ,&queue);
  1084. else
  1085. prepend_to_queue(sma ,&queue);
  1086. queue.status = -EINTR;
  1087. queue.sleeper = current;
  1088. current->state = TASK_INTERRUPTIBLE;
  1089. sem_unlock(sma);
  1090. if (timeout)
  1091. jiffies_left = schedule_timeout(jiffies_left);
  1092. else
  1093. schedule();
  1094. error = queue.status;
  1095. while(unlikely(error == IN_WAKEUP)) {
  1096. cpu_relax();
  1097. error = queue.status;
  1098. }
  1099. if (error != -EINTR) {
  1100. /* fast path: update_queue already obtained all requested
  1101. * resources */
  1102. goto out_free;
  1103. }
  1104. sma = sem_lock(ns, semid);
  1105. if (IS_ERR(sma)) {
  1106. BUG_ON(queue.prev != NULL);
  1107. error = -EIDRM;
  1108. goto out_free;
  1109. }
  1110. /*
  1111. * If queue.status != -EINTR we are woken up by another process
  1112. */
  1113. error = queue.status;
  1114. if (error != -EINTR) {
  1115. goto out_unlock_free;
  1116. }
  1117. /*
  1118. * If an interrupt occurred we have to clean up the queue
  1119. */
  1120. if (timeout && jiffies_left == 0)
  1121. error = -EAGAIN;
  1122. remove_from_queue(sma,&queue);
  1123. goto out_unlock_free;
  1124. out_unlock_free:
  1125. sem_unlock(sma);
  1126. out_free:
  1127. if(sops != fast_sops)
  1128. kfree(sops);
  1129. return error;
  1130. }
  1131. asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
  1132. {
  1133. return sys_semtimedop(semid, tsops, nsops, NULL);
  1134. }
  1135. /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
  1136. * parent and child tasks.
  1137. */
  1138. int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
  1139. {
  1140. struct sem_undo_list *undo_list;
  1141. int error;
  1142. if (clone_flags & CLONE_SYSVSEM) {
  1143. error = get_undo_list(&undo_list);
  1144. if (error)
  1145. return error;
  1146. atomic_inc(&undo_list->refcnt);
  1147. tsk->sysvsem.undo_list = undo_list;
  1148. } else
  1149. tsk->sysvsem.undo_list = NULL;
  1150. return 0;
  1151. }
  1152. /*
  1153. * add semadj values to semaphores, free undo structures.
  1154. * undo structures are not freed when semaphore arrays are destroyed
  1155. * so some of them may be out of date.
  1156. * IMPLEMENTATION NOTE: There is some confusion over whether the
  1157. * set of adjustments that needs to be done should be done in an atomic
  1158. * manner or not. That is, if we are attempting to decrement the semval
  1159. * should we queue up and wait until we can do so legally?
  1160. * The original implementation attempted to do this (queue and wait).
  1161. * The current implementation does not do so. The POSIX standard
  1162. * and SVID should be consulted to determine what behavior is mandated.
  1163. */
  1164. void exit_sem(struct task_struct *tsk)
  1165. {
  1166. struct sem_undo_list *undo_list;
  1167. struct sem_undo *u, **up;
  1168. struct ipc_namespace *ns;
  1169. undo_list = tsk->sysvsem.undo_list;
  1170. if (!undo_list)
  1171. return;
  1172. if (!atomic_dec_and_test(&undo_list->refcnt))
  1173. return;
  1174. ns = tsk->nsproxy->ipc_ns;
  1175. /* There's no need to hold the semundo list lock, as current
  1176. * is the last task exiting for this undo list.
  1177. */
  1178. for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
  1179. struct sem_array *sma;
  1180. int nsems, i;
  1181. struct sem_undo *un, **unp;
  1182. int semid;
  1183. semid = u->semid;
  1184. if(semid == -1)
  1185. continue;
  1186. sma = sem_lock(ns, semid);
  1187. if (IS_ERR(sma))
  1188. continue;
  1189. if (u->semid == -1)
  1190. goto next_entry;
  1191. BUG_ON(sem_checkid(sma, u->semid));
  1192. /* remove u from the sma->undo list */
  1193. for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
  1194. if (u == un)
  1195. goto found;
  1196. }
  1197. printk ("exit_sem undo list error id=%d\n", u->semid);
  1198. goto next_entry;
  1199. found:
  1200. *unp = un->id_next;
  1201. /* perform adjustments registered in u */
  1202. nsems = sma->sem_nsems;
  1203. for (i = 0; i < nsems; i++) {
  1204. struct sem * semaphore = &sma->sem_base[i];
  1205. if (u->semadj[i]) {
  1206. semaphore->semval += u->semadj[i];
  1207. /*
  1208. * Range checks of the new semaphore value,
  1209. * not defined by sus:
  1210. * - Some unices ignore the undo entirely
  1211. * (e.g. HP UX 11i 11.22, Tru64 V5.1)
  1212. * - some cap the value (e.g. FreeBSD caps
  1213. * at 0, but doesn't enforce SEMVMX)
  1214. *
  1215. * Linux caps the semaphore value, both at 0
  1216. * and at SEMVMX.
  1217. *
  1218. * Manfred <manfred@colorfullife.com>
  1219. */
  1220. if (semaphore->semval < 0)
  1221. semaphore->semval = 0;
  1222. if (semaphore->semval > SEMVMX)
  1223. semaphore->semval = SEMVMX;
  1224. semaphore->sempid = task_tgid_vnr(current);
  1225. }
  1226. }
  1227. sma->sem_otime = get_seconds();
  1228. /* maybe some queued-up processes were waiting for this */
  1229. update_queue(sma);
  1230. next_entry:
  1231. sem_unlock(sma);
  1232. }
  1233. kfree(undo_list);
  1234. }
  1235. #ifdef CONFIG_PROC_FS
  1236. static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
  1237. {
  1238. struct sem_array *sma = it;
  1239. return seq_printf(s,
  1240. "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
  1241. sma->sem_perm.key,
  1242. sma->sem_perm.id,
  1243. sma->sem_perm.mode,
  1244. sma->sem_nsems,
  1245. sma->sem_perm.uid,
  1246. sma->sem_perm.gid,
  1247. sma->sem_perm.cuid,
  1248. sma->sem_perm.cgid,
  1249. sma->sem_otime,
  1250. sma->sem_ctime);
  1251. }
  1252. #endif