sem.c 47 KB

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