sem.c 35 KB

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