sem.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  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. ipc_lock_object(&sma->sem_perm);
  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. ipc_unlock_object(&sma->sem_perm);
  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. int progress;
  659. progress = 1;
  660. retry_global:
  661. if (sma->complex_count) {
  662. if (update_queue(sma, -1, pt)) {
  663. progress = 1;
  664. otime = 1;
  665. sops = NULL;
  666. }
  667. }
  668. if (!progress)
  669. goto done;
  670. if (!sops) {
  671. /* No semops; something special is going on. */
  672. for (i = 0; i < sma->sem_nsems; i++) {
  673. if (update_queue(sma, i, pt)) {
  674. otime = 1;
  675. progress = 1;
  676. }
  677. }
  678. goto done_checkretry;
  679. }
  680. /* Check the semaphores that were modified. */
  681. for (i = 0; i < nsops; i++) {
  682. if (sops[i].sem_op > 0 ||
  683. (sops[i].sem_op < 0 &&
  684. sma->sem_base[sops[i].sem_num].semval == 0))
  685. if (update_queue(sma, sops[i].sem_num, pt)) {
  686. otime = 1;
  687. progress = 1;
  688. }
  689. }
  690. done_checkretry:
  691. if (progress) {
  692. progress = 0;
  693. goto retry_global;
  694. }
  695. done:
  696. if (otime)
  697. sma->sem_otime = get_seconds();
  698. }
  699. /* The following counts are associated to each semaphore:
  700. * semncnt number of tasks waiting on semval being nonzero
  701. * semzcnt number of tasks waiting on semval being zero
  702. * This model assumes that a task waits on exactly one semaphore.
  703. * Since semaphore operations are to be performed atomically, tasks actually
  704. * wait on a whole sequence of semaphores simultaneously.
  705. * The counts we return here are a rough approximation, but still
  706. * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
  707. */
  708. static int count_semncnt (struct sem_array * sma, ushort semnum)
  709. {
  710. int semncnt;
  711. struct sem_queue * q;
  712. semncnt = 0;
  713. list_for_each_entry(q, &sma->sem_base[semnum].sem_pending, list) {
  714. struct sembuf * sops = q->sops;
  715. BUG_ON(sops->sem_num != semnum);
  716. if ((sops->sem_op < 0) && !(sops->sem_flg & IPC_NOWAIT))
  717. semncnt++;
  718. }
  719. list_for_each_entry(q, &sma->sem_pending, list) {
  720. struct sembuf * sops = q->sops;
  721. int nsops = q->nsops;
  722. int i;
  723. for (i = 0; i < nsops; i++)
  724. if (sops[i].sem_num == semnum
  725. && (sops[i].sem_op < 0)
  726. && !(sops[i].sem_flg & IPC_NOWAIT))
  727. semncnt++;
  728. }
  729. return semncnt;
  730. }
  731. static int count_semzcnt (struct sem_array * sma, ushort semnum)
  732. {
  733. int semzcnt;
  734. struct sem_queue * q;
  735. semzcnt = 0;
  736. list_for_each_entry(q, &sma->sem_base[semnum].sem_pending, list) {
  737. struct sembuf * sops = q->sops;
  738. BUG_ON(sops->sem_num != semnum);
  739. if ((sops->sem_op == 0) && !(sops->sem_flg & IPC_NOWAIT))
  740. semzcnt++;
  741. }
  742. list_for_each_entry(q, &sma->sem_pending, list) {
  743. struct sembuf * sops = q->sops;
  744. int nsops = q->nsops;
  745. int i;
  746. for (i = 0; i < nsops; i++)
  747. if (sops[i].sem_num == semnum
  748. && (sops[i].sem_op == 0)
  749. && !(sops[i].sem_flg & IPC_NOWAIT))
  750. semzcnt++;
  751. }
  752. return semzcnt;
  753. }
  754. /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
  755. * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
  756. * remains locked on exit.
  757. */
  758. static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  759. {
  760. struct sem_undo *un, *tu;
  761. struct sem_queue *q, *tq;
  762. struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
  763. struct list_head tasks;
  764. int i;
  765. /* Free the existing undo structures for this semaphore set. */
  766. ipc_assert_locked_object(&sma->sem_perm);
  767. list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
  768. list_del(&un->list_id);
  769. spin_lock(&un->ulp->lock);
  770. un->semid = -1;
  771. list_del_rcu(&un->list_proc);
  772. spin_unlock(&un->ulp->lock);
  773. kfree_rcu(un, rcu);
  774. }
  775. /* Wake up all pending processes and let them fail with EIDRM. */
  776. INIT_LIST_HEAD(&tasks);
  777. list_for_each_entry_safe(q, tq, &sma->sem_pending, list) {
  778. unlink_queue(sma, q);
  779. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  780. }
  781. for (i = 0; i < sma->sem_nsems; i++) {
  782. struct sem *sem = sma->sem_base + i;
  783. list_for_each_entry_safe(q, tq, &sem->sem_pending, list) {
  784. unlink_queue(sma, q);
  785. wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
  786. }
  787. }
  788. /* Remove the semaphore set from the IDR */
  789. sem_rmid(ns, sma);
  790. sem_unlock(sma, -1);
  791. rcu_read_unlock();
  792. wake_up_sem_queue_do(&tasks);
  793. ns->used_sems -= sma->sem_nsems;
  794. security_sem_free(sma);
  795. ipc_rcu_putref(sma);
  796. }
  797. static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
  798. {
  799. switch(version) {
  800. case IPC_64:
  801. return copy_to_user(buf, in, sizeof(*in));
  802. case IPC_OLD:
  803. {
  804. struct semid_ds out;
  805. memset(&out, 0, sizeof(out));
  806. ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
  807. out.sem_otime = in->sem_otime;
  808. out.sem_ctime = in->sem_ctime;
  809. out.sem_nsems = in->sem_nsems;
  810. return copy_to_user(buf, &out, sizeof(out));
  811. }
  812. default:
  813. return -EINVAL;
  814. }
  815. }
  816. static int semctl_nolock(struct ipc_namespace *ns, int semid,
  817. int cmd, int version, void __user *p)
  818. {
  819. int err;
  820. struct sem_array *sma;
  821. switch(cmd) {
  822. case IPC_INFO:
  823. case SEM_INFO:
  824. {
  825. struct seminfo seminfo;
  826. int max_id;
  827. err = security_sem_semctl(NULL, cmd);
  828. if (err)
  829. return err;
  830. memset(&seminfo,0,sizeof(seminfo));
  831. seminfo.semmni = ns->sc_semmni;
  832. seminfo.semmns = ns->sc_semmns;
  833. seminfo.semmsl = ns->sc_semmsl;
  834. seminfo.semopm = ns->sc_semopm;
  835. seminfo.semvmx = SEMVMX;
  836. seminfo.semmnu = SEMMNU;
  837. seminfo.semmap = SEMMAP;
  838. seminfo.semume = SEMUME;
  839. down_read(&sem_ids(ns).rw_mutex);
  840. if (cmd == SEM_INFO) {
  841. seminfo.semusz = sem_ids(ns).in_use;
  842. seminfo.semaem = ns->used_sems;
  843. } else {
  844. seminfo.semusz = SEMUSZ;
  845. seminfo.semaem = SEMAEM;
  846. }
  847. max_id = ipc_get_maxid(&sem_ids(ns));
  848. up_read(&sem_ids(ns).rw_mutex);
  849. if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
  850. return -EFAULT;
  851. return (max_id < 0) ? 0: max_id;
  852. }
  853. case IPC_STAT:
  854. case SEM_STAT:
  855. {
  856. struct semid64_ds tbuf;
  857. int id = 0;
  858. memset(&tbuf, 0, sizeof(tbuf));
  859. rcu_read_lock();
  860. if (cmd == SEM_STAT) {
  861. sma = sem_obtain_object(ns, semid);
  862. if (IS_ERR(sma)) {
  863. err = PTR_ERR(sma);
  864. goto out_unlock;
  865. }
  866. id = sma->sem_perm.id;
  867. } else {
  868. sma = sem_obtain_object_check(ns, semid);
  869. if (IS_ERR(sma)) {
  870. err = PTR_ERR(sma);
  871. goto out_unlock;
  872. }
  873. }
  874. err = -EACCES;
  875. if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
  876. goto out_unlock;
  877. err = security_sem_semctl(sma, cmd);
  878. if (err)
  879. goto out_unlock;
  880. kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
  881. tbuf.sem_otime = sma->sem_otime;
  882. tbuf.sem_ctime = sma->sem_ctime;
  883. tbuf.sem_nsems = sma->sem_nsems;
  884. rcu_read_unlock();
  885. if (copy_semid_to_user(p, &tbuf, version))
  886. return -EFAULT;
  887. return id;
  888. }
  889. default:
  890. return -EINVAL;
  891. }
  892. out_unlock:
  893. rcu_read_unlock();
  894. return err;
  895. }
  896. static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
  897. unsigned long arg)
  898. {
  899. struct sem_undo *un;
  900. struct sem_array *sma;
  901. struct sem* curr;
  902. int err;
  903. struct list_head tasks;
  904. int val;
  905. #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
  906. /* big-endian 64bit */
  907. val = arg >> 32;
  908. #else
  909. /* 32bit or little-endian 64bit */
  910. val = arg;
  911. #endif
  912. if (val > SEMVMX || val < 0)
  913. return -ERANGE;
  914. INIT_LIST_HEAD(&tasks);
  915. rcu_read_lock();
  916. sma = sem_obtain_object_check(ns, semid);
  917. if (IS_ERR(sma)) {
  918. rcu_read_unlock();
  919. return PTR_ERR(sma);
  920. }
  921. if (semnum < 0 || semnum >= sma->sem_nsems) {
  922. rcu_read_unlock();
  923. return -EINVAL;
  924. }
  925. if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
  926. rcu_read_unlock();
  927. return -EACCES;
  928. }
  929. err = security_sem_semctl(sma, SETVAL);
  930. if (err) {
  931. rcu_read_unlock();
  932. return -EACCES;
  933. }
  934. sem_lock(sma, NULL, -1);
  935. curr = &sma->sem_base[semnum];
  936. ipc_assert_locked_object(&sma->sem_perm);
  937. list_for_each_entry(un, &sma->list_id, list_id)
  938. un->semadj[semnum] = 0;
  939. curr->semval = val;
  940. curr->sempid = task_tgid_vnr(current);
  941. sma->sem_ctime = get_seconds();
  942. /* maybe some queued-up processes were waiting for this */
  943. do_smart_update(sma, NULL, 0, 0, &tasks);
  944. sem_unlock(sma, -1);
  945. rcu_read_unlock();
  946. wake_up_sem_queue_do(&tasks);
  947. return 0;
  948. }
  949. static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
  950. int cmd, void __user *p)
  951. {
  952. struct sem_array *sma;
  953. struct sem* curr;
  954. int err, nsems;
  955. ushort fast_sem_io[SEMMSL_FAST];
  956. ushort* sem_io = fast_sem_io;
  957. struct list_head tasks;
  958. INIT_LIST_HEAD(&tasks);
  959. rcu_read_lock();
  960. sma = sem_obtain_object_check(ns, semid);
  961. if (IS_ERR(sma)) {
  962. rcu_read_unlock();
  963. return PTR_ERR(sma);
  964. }
  965. nsems = sma->sem_nsems;
  966. err = -EACCES;
  967. if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
  968. goto out_rcu_wakeup;
  969. err = security_sem_semctl(sma, cmd);
  970. if (err)
  971. goto out_rcu_wakeup;
  972. err = -EACCES;
  973. switch (cmd) {
  974. case GETALL:
  975. {
  976. ushort __user *array = p;
  977. int i;
  978. sem_lock(sma, NULL, -1);
  979. if(nsems > SEMMSL_FAST) {
  980. if (!ipc_rcu_getref(sma)) {
  981. sem_unlock(sma, -1);
  982. rcu_read_unlock();
  983. err = -EIDRM;
  984. goto out_free;
  985. }
  986. sem_unlock(sma, -1);
  987. rcu_read_unlock();
  988. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  989. if(sem_io == NULL) {
  990. sem_putref(sma);
  991. return -ENOMEM;
  992. }
  993. rcu_read_lock();
  994. sem_lock_and_putref(sma);
  995. if (sma->sem_perm.deleted) {
  996. sem_unlock(sma, -1);
  997. rcu_read_unlock();
  998. err = -EIDRM;
  999. goto out_free;
  1000. }
  1001. }
  1002. for (i = 0; i < sma->sem_nsems; i++)
  1003. sem_io[i] = sma->sem_base[i].semval;
  1004. sem_unlock(sma, -1);
  1005. rcu_read_unlock();
  1006. err = 0;
  1007. if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
  1008. err = -EFAULT;
  1009. goto out_free;
  1010. }
  1011. case SETALL:
  1012. {
  1013. int i;
  1014. struct sem_undo *un;
  1015. if (!ipc_rcu_getref(sma)) {
  1016. rcu_read_unlock();
  1017. return -EIDRM;
  1018. }
  1019. rcu_read_unlock();
  1020. if(nsems > SEMMSL_FAST) {
  1021. sem_io = ipc_alloc(sizeof(ushort)*nsems);
  1022. if(sem_io == NULL) {
  1023. sem_putref(sma);
  1024. return -ENOMEM;
  1025. }
  1026. }
  1027. if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
  1028. sem_putref(sma);
  1029. err = -EFAULT;
  1030. goto out_free;
  1031. }
  1032. for (i = 0; i < nsems; i++) {
  1033. if (sem_io[i] > SEMVMX) {
  1034. sem_putref(sma);
  1035. err = -ERANGE;
  1036. goto out_free;
  1037. }
  1038. }
  1039. rcu_read_lock();
  1040. sem_lock_and_putref(sma);
  1041. if (sma->sem_perm.deleted) {
  1042. sem_unlock(sma, -1);
  1043. rcu_read_unlock();
  1044. err = -EIDRM;
  1045. goto out_free;
  1046. }
  1047. for (i = 0; i < nsems; i++)
  1048. sma->sem_base[i].semval = sem_io[i];
  1049. ipc_assert_locked_object(&sma->sem_perm);
  1050. list_for_each_entry(un, &sma->list_id, list_id) {
  1051. for (i = 0; i < nsems; i++)
  1052. un->semadj[i] = 0;
  1053. }
  1054. sma->sem_ctime = get_seconds();
  1055. /* maybe some queued-up processes were waiting for this */
  1056. do_smart_update(sma, NULL, 0, 0, &tasks);
  1057. err = 0;
  1058. goto out_unlock;
  1059. }
  1060. /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
  1061. }
  1062. err = -EINVAL;
  1063. if (semnum < 0 || semnum >= nsems)
  1064. goto out_rcu_wakeup;
  1065. sem_lock(sma, NULL, -1);
  1066. curr = &sma->sem_base[semnum];
  1067. switch (cmd) {
  1068. case GETVAL:
  1069. err = curr->semval;
  1070. goto out_unlock;
  1071. case GETPID:
  1072. err = curr->sempid;
  1073. goto out_unlock;
  1074. case GETNCNT:
  1075. err = count_semncnt(sma,semnum);
  1076. goto out_unlock;
  1077. case GETZCNT:
  1078. err = count_semzcnt(sma,semnum);
  1079. goto out_unlock;
  1080. }
  1081. out_unlock:
  1082. sem_unlock(sma, -1);
  1083. out_rcu_wakeup:
  1084. rcu_read_unlock();
  1085. wake_up_sem_queue_do(&tasks);
  1086. out_free:
  1087. if(sem_io != fast_sem_io)
  1088. ipc_free(sem_io, sizeof(ushort)*nsems);
  1089. return err;
  1090. }
  1091. static inline unsigned long
  1092. copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
  1093. {
  1094. switch(version) {
  1095. case IPC_64:
  1096. if (copy_from_user(out, buf, sizeof(*out)))
  1097. return -EFAULT;
  1098. return 0;
  1099. case IPC_OLD:
  1100. {
  1101. struct semid_ds tbuf_old;
  1102. if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  1103. return -EFAULT;
  1104. out->sem_perm.uid = tbuf_old.sem_perm.uid;
  1105. out->sem_perm.gid = tbuf_old.sem_perm.gid;
  1106. out->sem_perm.mode = tbuf_old.sem_perm.mode;
  1107. return 0;
  1108. }
  1109. default:
  1110. return -EINVAL;
  1111. }
  1112. }
  1113. /*
  1114. * This function handles some semctl commands which require the rw_mutex
  1115. * to be held in write mode.
  1116. * NOTE: no locks must be held, the rw_mutex is taken inside this function.
  1117. */
  1118. static int semctl_down(struct ipc_namespace *ns, int semid,
  1119. int cmd, int version, void __user *p)
  1120. {
  1121. struct sem_array *sma;
  1122. int err;
  1123. struct semid64_ds semid64;
  1124. struct kern_ipc_perm *ipcp;
  1125. if(cmd == IPC_SET) {
  1126. if (copy_semid_from_user(&semid64, p, version))
  1127. return -EFAULT;
  1128. }
  1129. down_write(&sem_ids(ns).rw_mutex);
  1130. rcu_read_lock();
  1131. ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
  1132. &semid64.sem_perm, 0);
  1133. if (IS_ERR(ipcp)) {
  1134. err = PTR_ERR(ipcp);
  1135. /* the ipc lock is not held upon failure */
  1136. goto out_unlock1;
  1137. }
  1138. sma = container_of(ipcp, struct sem_array, sem_perm);
  1139. err = security_sem_semctl(sma, cmd);
  1140. if (err)
  1141. goto out_unlock1;
  1142. switch (cmd) {
  1143. case IPC_RMID:
  1144. sem_lock(sma, NULL, -1);
  1145. /* freeary unlocks the ipc object and rcu */
  1146. freeary(ns, ipcp);
  1147. goto out_up;
  1148. case IPC_SET:
  1149. sem_lock(sma, NULL, -1);
  1150. err = ipc_update_perm(&semid64.sem_perm, ipcp);
  1151. if (err)
  1152. goto out_unlock0;
  1153. sma->sem_ctime = get_seconds();
  1154. break;
  1155. default:
  1156. err = -EINVAL;
  1157. goto out_unlock1;
  1158. }
  1159. out_unlock0:
  1160. sem_unlock(sma, -1);
  1161. out_unlock1:
  1162. rcu_read_unlock();
  1163. out_up:
  1164. up_write(&sem_ids(ns).rw_mutex);
  1165. return err;
  1166. }
  1167. SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
  1168. {
  1169. int version;
  1170. struct ipc_namespace *ns;
  1171. void __user *p = (void __user *)arg;
  1172. if (semid < 0)
  1173. return -EINVAL;
  1174. version = ipc_parse_version(&cmd);
  1175. ns = current->nsproxy->ipc_ns;
  1176. switch(cmd) {
  1177. case IPC_INFO:
  1178. case SEM_INFO:
  1179. case IPC_STAT:
  1180. case SEM_STAT:
  1181. return semctl_nolock(ns, semid, cmd, version, p);
  1182. case GETALL:
  1183. case GETVAL:
  1184. case GETPID:
  1185. case GETNCNT:
  1186. case GETZCNT:
  1187. case SETALL:
  1188. return semctl_main(ns, semid, semnum, cmd, p);
  1189. case SETVAL:
  1190. return semctl_setval(ns, semid, semnum, arg);
  1191. case IPC_RMID:
  1192. case IPC_SET:
  1193. return semctl_down(ns, semid, cmd, version, p);
  1194. default:
  1195. return -EINVAL;
  1196. }
  1197. }
  1198. /* If the task doesn't already have a undo_list, then allocate one
  1199. * here. We guarantee there is only one thread using this undo list,
  1200. * and current is THE ONE
  1201. *
  1202. * If this allocation and assignment succeeds, but later
  1203. * portions of this code fail, there is no need to free the sem_undo_list.
  1204. * Just let it stay associated with the task, and it'll be freed later
  1205. * at exit time.
  1206. *
  1207. * This can block, so callers must hold no locks.
  1208. */
  1209. static inline int get_undo_list(struct sem_undo_list **undo_listp)
  1210. {
  1211. struct sem_undo_list *undo_list;
  1212. undo_list = current->sysvsem.undo_list;
  1213. if (!undo_list) {
  1214. undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
  1215. if (undo_list == NULL)
  1216. return -ENOMEM;
  1217. spin_lock_init(&undo_list->lock);
  1218. atomic_set(&undo_list->refcnt, 1);
  1219. INIT_LIST_HEAD(&undo_list->list_proc);
  1220. current->sysvsem.undo_list = undo_list;
  1221. }
  1222. *undo_listp = undo_list;
  1223. return 0;
  1224. }
  1225. static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
  1226. {
  1227. struct sem_undo *un;
  1228. list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
  1229. if (un->semid == semid)
  1230. return un;
  1231. }
  1232. return NULL;
  1233. }
  1234. static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
  1235. {
  1236. struct sem_undo *un;
  1237. assert_spin_locked(&ulp->lock);
  1238. un = __lookup_undo(ulp, semid);
  1239. if (un) {
  1240. list_del_rcu(&un->list_proc);
  1241. list_add_rcu(&un->list_proc, &ulp->list_proc);
  1242. }
  1243. return un;
  1244. }
  1245. /**
  1246. * find_alloc_undo - Lookup (and if not present create) undo array
  1247. * @ns: namespace
  1248. * @semid: semaphore array id
  1249. *
  1250. * The function looks up (and if not present creates) the undo structure.
  1251. * The size of the undo structure depends on the size of the semaphore
  1252. * array, thus the alloc path is not that straightforward.
  1253. * Lifetime-rules: sem_undo is rcu-protected, on success, the function
  1254. * performs a rcu_read_lock().
  1255. */
  1256. static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
  1257. {
  1258. struct sem_array *sma;
  1259. struct sem_undo_list *ulp;
  1260. struct sem_undo *un, *new;
  1261. int nsems, error;
  1262. error = get_undo_list(&ulp);
  1263. if (error)
  1264. return ERR_PTR(error);
  1265. rcu_read_lock();
  1266. spin_lock(&ulp->lock);
  1267. un = lookup_undo(ulp, semid);
  1268. spin_unlock(&ulp->lock);
  1269. if (likely(un!=NULL))
  1270. goto out;
  1271. /* no undo structure around - allocate one. */
  1272. /* step 1: figure out the size of the semaphore array */
  1273. sma = sem_obtain_object_check(ns, semid);
  1274. if (IS_ERR(sma)) {
  1275. rcu_read_unlock();
  1276. return ERR_CAST(sma);
  1277. }
  1278. nsems = sma->sem_nsems;
  1279. if (!ipc_rcu_getref(sma)) {
  1280. rcu_read_unlock();
  1281. un = ERR_PTR(-EIDRM);
  1282. goto out;
  1283. }
  1284. rcu_read_unlock();
  1285. /* step 2: allocate new undo structure */
  1286. new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
  1287. if (!new) {
  1288. sem_putref(sma);
  1289. return ERR_PTR(-ENOMEM);
  1290. }
  1291. /* step 3: Acquire the lock on semaphore array */
  1292. rcu_read_lock();
  1293. sem_lock_and_putref(sma);
  1294. if (sma->sem_perm.deleted) {
  1295. sem_unlock(sma, -1);
  1296. rcu_read_unlock();
  1297. kfree(new);
  1298. un = ERR_PTR(-EIDRM);
  1299. goto out;
  1300. }
  1301. spin_lock(&ulp->lock);
  1302. /*
  1303. * step 4: check for races: did someone else allocate the undo struct?
  1304. */
  1305. un = lookup_undo(ulp, semid);
  1306. if (un) {
  1307. kfree(new);
  1308. goto success;
  1309. }
  1310. /* step 5: initialize & link new undo structure */
  1311. new->semadj = (short *) &new[1];
  1312. new->ulp = ulp;
  1313. new->semid = semid;
  1314. assert_spin_locked(&ulp->lock);
  1315. list_add_rcu(&new->list_proc, &ulp->list_proc);
  1316. ipc_assert_locked_object(&sma->sem_perm);
  1317. list_add(&new->list_id, &sma->list_id);
  1318. un = new;
  1319. success:
  1320. spin_unlock(&ulp->lock);
  1321. sem_unlock(sma, -1);
  1322. out:
  1323. return un;
  1324. }
  1325. /**
  1326. * get_queue_result - Retrieve the result code from sem_queue
  1327. * @q: Pointer to queue structure
  1328. *
  1329. * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
  1330. * q->status, then we must loop until the value is replaced with the final
  1331. * value: This may happen if a task is woken up by an unrelated event (e.g.
  1332. * signal) and in parallel the task is woken up by another task because it got
  1333. * the requested semaphores.
  1334. *
  1335. * The function can be called with or without holding the semaphore spinlock.
  1336. */
  1337. static int get_queue_result(struct sem_queue *q)
  1338. {
  1339. int error;
  1340. error = q->status;
  1341. while (unlikely(error == IN_WAKEUP)) {
  1342. cpu_relax();
  1343. error = q->status;
  1344. }
  1345. return error;
  1346. }
  1347. SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
  1348. unsigned, nsops, const struct timespec __user *, timeout)
  1349. {
  1350. int error = -EINVAL;
  1351. struct sem_array *sma;
  1352. struct sembuf fast_sops[SEMOPM_FAST];
  1353. struct sembuf* sops = fast_sops, *sop;
  1354. struct sem_undo *un;
  1355. int undos = 0, alter = 0, max, locknum;
  1356. struct sem_queue queue;
  1357. unsigned long jiffies_left = 0;
  1358. struct ipc_namespace *ns;
  1359. struct list_head tasks;
  1360. ns = current->nsproxy->ipc_ns;
  1361. if (nsops < 1 || semid < 0)
  1362. return -EINVAL;
  1363. if (nsops > ns->sc_semopm)
  1364. return -E2BIG;
  1365. if(nsops > SEMOPM_FAST) {
  1366. sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
  1367. if(sops==NULL)
  1368. return -ENOMEM;
  1369. }
  1370. if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
  1371. error=-EFAULT;
  1372. goto out_free;
  1373. }
  1374. if (timeout) {
  1375. struct timespec _timeout;
  1376. if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
  1377. error = -EFAULT;
  1378. goto out_free;
  1379. }
  1380. if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
  1381. _timeout.tv_nsec >= 1000000000L) {
  1382. error = -EINVAL;
  1383. goto out_free;
  1384. }
  1385. jiffies_left = timespec_to_jiffies(&_timeout);
  1386. }
  1387. max = 0;
  1388. for (sop = sops; sop < sops + nsops; sop++) {
  1389. if (sop->sem_num >= max)
  1390. max = sop->sem_num;
  1391. if (sop->sem_flg & SEM_UNDO)
  1392. undos = 1;
  1393. if (sop->sem_op != 0)
  1394. alter = 1;
  1395. }
  1396. INIT_LIST_HEAD(&tasks);
  1397. if (undos) {
  1398. /* On success, find_alloc_undo takes the rcu_read_lock */
  1399. un = find_alloc_undo(ns, semid);
  1400. if (IS_ERR(un)) {
  1401. error = PTR_ERR(un);
  1402. goto out_free;
  1403. }
  1404. } else {
  1405. un = NULL;
  1406. rcu_read_lock();
  1407. }
  1408. sma = sem_obtain_object_check(ns, semid);
  1409. if (IS_ERR(sma)) {
  1410. rcu_read_unlock();
  1411. error = PTR_ERR(sma);
  1412. goto out_free;
  1413. }
  1414. error = -EFBIG;
  1415. if (max >= sma->sem_nsems)
  1416. goto out_rcu_wakeup;
  1417. error = -EACCES;
  1418. if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
  1419. goto out_rcu_wakeup;
  1420. error = security_sem_semop(sma, sops, nsops, alter);
  1421. if (error)
  1422. goto out_rcu_wakeup;
  1423. /*
  1424. * semid identifiers are not unique - find_alloc_undo may have
  1425. * allocated an undo structure, it was invalidated by an RMID
  1426. * and now a new array with received the same id. Check and fail.
  1427. * This case can be detected checking un->semid. The existence of
  1428. * "un" itself is guaranteed by rcu.
  1429. */
  1430. error = -EIDRM;
  1431. locknum = sem_lock(sma, sops, nsops);
  1432. if (un && un->semid == -1)
  1433. goto out_unlock_free;
  1434. error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
  1435. if (error <= 0) {
  1436. if (alter && error == 0)
  1437. do_smart_update(sma, sops, nsops, 1, &tasks);
  1438. goto out_unlock_free;
  1439. }
  1440. /* We need to sleep on this operation, so we put the current
  1441. * task into the pending queue and go to sleep.
  1442. */
  1443. queue.sops = sops;
  1444. queue.nsops = nsops;
  1445. queue.undo = un;
  1446. queue.pid = task_tgid_vnr(current);
  1447. queue.alter = alter;
  1448. if (nsops == 1) {
  1449. struct sem *curr;
  1450. curr = &sma->sem_base[sops->sem_num];
  1451. if (alter)
  1452. list_add_tail(&queue.list, &curr->sem_pending);
  1453. else
  1454. list_add(&queue.list, &curr->sem_pending);
  1455. } else {
  1456. if (alter)
  1457. list_add_tail(&queue.list, &sma->sem_pending);
  1458. else
  1459. list_add(&queue.list, &sma->sem_pending);
  1460. sma->complex_count++;
  1461. }
  1462. queue.status = -EINTR;
  1463. queue.sleeper = current;
  1464. sleep_again:
  1465. current->state = TASK_INTERRUPTIBLE;
  1466. sem_unlock(sma, locknum);
  1467. rcu_read_unlock();
  1468. if (timeout)
  1469. jiffies_left = schedule_timeout(jiffies_left);
  1470. else
  1471. schedule();
  1472. error = get_queue_result(&queue);
  1473. if (error != -EINTR) {
  1474. /* fast path: update_queue already obtained all requested
  1475. * resources.
  1476. * Perform a smp_mb(): User space could assume that semop()
  1477. * is a memory barrier: Without the mb(), the cpu could
  1478. * speculatively read in user space stale data that was
  1479. * overwritten by the previous owner of the semaphore.
  1480. */
  1481. smp_mb();
  1482. goto out_free;
  1483. }
  1484. rcu_read_lock();
  1485. sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
  1486. /*
  1487. * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
  1488. */
  1489. error = get_queue_result(&queue);
  1490. /*
  1491. * Array removed? If yes, leave without sem_unlock().
  1492. */
  1493. if (IS_ERR(sma)) {
  1494. rcu_read_unlock();
  1495. goto out_free;
  1496. }
  1497. /*
  1498. * If queue.status != -EINTR we are woken up by another process.
  1499. * Leave without unlink_queue(), but with sem_unlock().
  1500. */
  1501. if (error != -EINTR) {
  1502. goto out_unlock_free;
  1503. }
  1504. /*
  1505. * If an interrupt occurred we have to clean up the queue
  1506. */
  1507. if (timeout && jiffies_left == 0)
  1508. error = -EAGAIN;
  1509. /*
  1510. * If the wakeup was spurious, just retry
  1511. */
  1512. if (error == -EINTR && !signal_pending(current))
  1513. goto sleep_again;
  1514. unlink_queue(sma, &queue);
  1515. out_unlock_free:
  1516. sem_unlock(sma, locknum);
  1517. out_rcu_wakeup:
  1518. rcu_read_unlock();
  1519. wake_up_sem_queue_do(&tasks);
  1520. out_free:
  1521. if(sops != fast_sops)
  1522. kfree(sops);
  1523. return error;
  1524. }
  1525. SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
  1526. unsigned, nsops)
  1527. {
  1528. return sys_semtimedop(semid, tsops, nsops, NULL);
  1529. }
  1530. /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
  1531. * parent and child tasks.
  1532. */
  1533. int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
  1534. {
  1535. struct sem_undo_list *undo_list;
  1536. int error;
  1537. if (clone_flags & CLONE_SYSVSEM) {
  1538. error = get_undo_list(&undo_list);
  1539. if (error)
  1540. return error;
  1541. atomic_inc(&undo_list->refcnt);
  1542. tsk->sysvsem.undo_list = undo_list;
  1543. } else
  1544. tsk->sysvsem.undo_list = NULL;
  1545. return 0;
  1546. }
  1547. /*
  1548. * add semadj values to semaphores, free undo structures.
  1549. * undo structures are not freed when semaphore arrays are destroyed
  1550. * so some of them may be out of date.
  1551. * IMPLEMENTATION NOTE: There is some confusion over whether the
  1552. * set of adjustments that needs to be done should be done in an atomic
  1553. * manner or not. That is, if we are attempting to decrement the semval
  1554. * should we queue up and wait until we can do so legally?
  1555. * The original implementation attempted to do this (queue and wait).
  1556. * The current implementation does not do so. The POSIX standard
  1557. * and SVID should be consulted to determine what behavior is mandated.
  1558. */
  1559. void exit_sem(struct task_struct *tsk)
  1560. {
  1561. struct sem_undo_list *ulp;
  1562. ulp = tsk->sysvsem.undo_list;
  1563. if (!ulp)
  1564. return;
  1565. tsk->sysvsem.undo_list = NULL;
  1566. if (!atomic_dec_and_test(&ulp->refcnt))
  1567. return;
  1568. for (;;) {
  1569. struct sem_array *sma;
  1570. struct sem_undo *un;
  1571. struct list_head tasks;
  1572. int semid, i;
  1573. rcu_read_lock();
  1574. un = list_entry_rcu(ulp->list_proc.next,
  1575. struct sem_undo, list_proc);
  1576. if (&un->list_proc == &ulp->list_proc)
  1577. semid = -1;
  1578. else
  1579. semid = un->semid;
  1580. if (semid == -1) {
  1581. rcu_read_unlock();
  1582. break;
  1583. }
  1584. sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
  1585. /* exit_sem raced with IPC_RMID, nothing to do */
  1586. if (IS_ERR(sma)) {
  1587. rcu_read_unlock();
  1588. continue;
  1589. }
  1590. sem_lock(sma, NULL, -1);
  1591. un = __lookup_undo(ulp, semid);
  1592. if (un == NULL) {
  1593. /* exit_sem raced with IPC_RMID+semget() that created
  1594. * exactly the same semid. Nothing to do.
  1595. */
  1596. sem_unlock(sma, -1);
  1597. rcu_read_unlock();
  1598. continue;
  1599. }
  1600. /* remove un from the linked lists */
  1601. ipc_assert_locked_object(&sma->sem_perm);
  1602. list_del(&un->list_id);
  1603. spin_lock(&ulp->lock);
  1604. list_del_rcu(&un->list_proc);
  1605. spin_unlock(&ulp->lock);
  1606. /* perform adjustments registered in un */
  1607. for (i = 0; i < sma->sem_nsems; i++) {
  1608. struct sem * semaphore = &sma->sem_base[i];
  1609. if (un->semadj[i]) {
  1610. semaphore->semval += un->semadj[i];
  1611. /*
  1612. * Range checks of the new semaphore value,
  1613. * not defined by sus:
  1614. * - Some unices ignore the undo entirely
  1615. * (e.g. HP UX 11i 11.22, Tru64 V5.1)
  1616. * - some cap the value (e.g. FreeBSD caps
  1617. * at 0, but doesn't enforce SEMVMX)
  1618. *
  1619. * Linux caps the semaphore value, both at 0
  1620. * and at SEMVMX.
  1621. *
  1622. * Manfred <manfred@colorfullife.com>
  1623. */
  1624. if (semaphore->semval < 0)
  1625. semaphore->semval = 0;
  1626. if (semaphore->semval > SEMVMX)
  1627. semaphore->semval = SEMVMX;
  1628. semaphore->sempid = task_tgid_vnr(current);
  1629. }
  1630. }
  1631. /* maybe some queued-up processes were waiting for this */
  1632. INIT_LIST_HEAD(&tasks);
  1633. do_smart_update(sma, NULL, 0, 1, &tasks);
  1634. sem_unlock(sma, -1);
  1635. rcu_read_unlock();
  1636. wake_up_sem_queue_do(&tasks);
  1637. kfree_rcu(un, rcu);
  1638. }
  1639. kfree(ulp);
  1640. }
  1641. #ifdef CONFIG_PROC_FS
  1642. static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
  1643. {
  1644. struct user_namespace *user_ns = seq_user_ns(s);
  1645. struct sem_array *sma = it;
  1646. return seq_printf(s,
  1647. "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
  1648. sma->sem_perm.key,
  1649. sma->sem_perm.id,
  1650. sma->sem_perm.mode,
  1651. sma->sem_nsems,
  1652. from_kuid_munged(user_ns, sma->sem_perm.uid),
  1653. from_kgid_munged(user_ns, sma->sem_perm.gid),
  1654. from_kuid_munged(user_ns, sma->sem_perm.cuid),
  1655. from_kgid_munged(user_ns, sma->sem_perm.cgid),
  1656. sma->sem_otime,
  1657. sma->sem_ctime);
  1658. }
  1659. #endif