sem.c 55 KB

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