sem.c 35 KB

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