sem.c 36 KB

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