sem.c 35 KB

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