sem.c 41 KB

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