sem.c 43 KB

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