sem.c 35 KB

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