sem.c 55 KB

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