sem.c 35 KB

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