sem.c 52 KB

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