sem.c 54 KB

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