V4L/DVB (7369): drivers/media/video/soc_camera.c: reads return size_t
[wandboard.git] / ipc / sem.c
blob0b45a4d383c6ae662845f5e26c6bc19f25c10a22
1 /*
2 * linux/ipc/sem.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
6 * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
7 * This code underwent a massive rewrite in order to solve some problems
8 * with the original code. In particular the original code failed to
9 * wake up processes that were waiting for semval to go to 0 if the
10 * value went to 0 and was then incremented rapidly enough. In solving
11 * this problem I have also modified the implementation so that it
12 * processes pending operations in a FIFO manner, thus give a guarantee
13 * that processes waiting for a lock on the semaphore won't starve
14 * unless another locking process fails to unlock.
15 * In addition the following two changes in behavior have been introduced:
16 * - The original implementation of semop returned the value
17 * last semaphore element examined on success. This does not
18 * match the manual page specifications, and effectively
19 * allows the user to read the semaphore even if they do not
20 * have read permissions. The implementation now returns 0
21 * on success as stated in the manual page.
22 * - There is some confusion over whether the set of undo adjustments
23 * to be performed at exit should be done in an atomic manner.
24 * That is, if we are attempting to decrement the semval should we queue
25 * up and wait until we can do so legally?
26 * The original implementation attempted to do this.
27 * The current implementation does not do so. This is because I don't
28 * think it is the right thing (TM) to do, and because I couldn't
29 * see a clean way to get the old behavior with the new design.
30 * The POSIX standard and SVID should be consulted to determine
31 * what behavior is mandated.
33 * Further notes on refinement (Christoph Rohland, December 1998):
34 * - The POSIX standard says, that the undo adjustments simply should
35 * redo. So the current implementation is o.K.
36 * - The previous code had two flaws:
37 * 1) It actively gave the semaphore to the next waiting process
38 * sleeping on the semaphore. Since this process did not have the
39 * cpu this led to many unnecessary context switches and bad
40 * performance. Now we only check which process should be able to
41 * get the semaphore and if this process wants to reduce some
42 * semaphore value we simply wake it up without doing the
43 * operation. So it has to try to get it later. Thus e.g. the
44 * running process may reacquire the semaphore during the current
45 * time slice. If it only waits for zero or increases the semaphore,
46 * we do the operation in advance and wake it up.
47 * 2) It did not wake up all zero waiting processes. We try to do
48 * better but only get the semops right which only wait for zero or
49 * increase. If there are decrement operations in the operations
50 * array we do the same as before.
52 * With the incarnation of O(1) scheduler, it becomes unnecessary to perform
53 * check/retry algorithm for waking up blocked processes as the new scheduler
54 * is better at handling thread switch than the old one.
56 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
58 * SMP-threaded, sysctl's added
59 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
60 * Enforced range limit on SEM_UNDO
61 * (c) 2001 Red Hat Inc <alan@redhat.com>
62 * Lockless wakeup
63 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
65 * support for audit of ipc object properties and permission changes
66 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
68 * namespaces support
69 * OpenVZ, SWsoft Inc.
70 * Pavel Emelianov <xemul@openvz.org>
73 #include <linux/slab.h>
74 #include <linux/spinlock.h>
75 #include <linux/init.h>
76 #include <linux/proc_fs.h>
77 #include <linux/time.h>
78 #include <linux/security.h>
79 #include <linux/syscalls.h>
80 #include <linux/audit.h>
81 #include <linux/capability.h>
82 #include <linux/seq_file.h>
83 #include <linux/rwsem.h>
84 #include <linux/nsproxy.h>
85 #include <linux/ipc_namespace.h>
87 #include <asm/uaccess.h>
88 #include "util.h"
90 #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
92 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
93 #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
94 #define sem_buildid(id, seq) ipc_buildid(id, seq)
96 static int newary(struct ipc_namespace *, struct ipc_params *);
97 static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
98 #ifdef CONFIG_PROC_FS
99 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
100 #endif
102 #define SEMMSL_FAST 256 /* 512 bytes on stack */
103 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
106 * linked list protection:
107 * sem_undo.id_next,
108 * sem_array.sem_pending{,last},
109 * sem_array.sem_undo: sem_lock() for read/write
110 * sem_undo.proc_next: only "current" is allowed to read/write that field.
114 #define sc_semmsl sem_ctls[0]
115 #define sc_semmns sem_ctls[1]
116 #define sc_semopm sem_ctls[2]
117 #define sc_semmni sem_ctls[3]
119 void sem_init_ns(struct ipc_namespace *ns)
121 ns->sc_semmsl = SEMMSL;
122 ns->sc_semmns = SEMMNS;
123 ns->sc_semopm = SEMOPM;
124 ns->sc_semmni = SEMMNI;
125 ns->used_sems = 0;
126 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
129 #ifdef CONFIG_IPC_NS
130 void sem_exit_ns(struct ipc_namespace *ns)
132 free_ipcs(ns, &sem_ids(ns), freeary);
134 #endif
136 void __init sem_init (void)
138 sem_init_ns(&init_ipc_ns);
139 ipc_init_proc_interface("sysvipc/sem",
140 " key semid perms nsems uid gid cuid cgid otime ctime\n",
141 IPC_SEM_IDS, sysvipc_sem_proc_show);
145 * This routine is called in the paths where the rw_mutex is held to protect
146 * access to the idr tree.
148 static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
149 int id)
151 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
153 if (IS_ERR(ipcp))
154 return (struct sem_array *)ipcp;
156 return container_of(ipcp, struct sem_array, sem_perm);
160 * sem_lock_(check_) routines are called in the paths where the rw_mutex
161 * is not held.
163 static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
165 struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
167 if (IS_ERR(ipcp))
168 return (struct sem_array *)ipcp;
170 return container_of(ipcp, struct sem_array, sem_perm);
173 static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
174 int id)
176 struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
178 if (IS_ERR(ipcp))
179 return (struct sem_array *)ipcp;
181 return container_of(ipcp, struct sem_array, sem_perm);
184 static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
186 ipc_rmid(&sem_ids(ns), &s->sem_perm);
190 * Lockless wakeup algorithm:
191 * Without the check/retry algorithm a lockless wakeup is possible:
192 * - queue.status is initialized to -EINTR before blocking.
193 * - wakeup is performed by
194 * * unlinking the queue entry from sma->sem_pending
195 * * setting queue.status to IN_WAKEUP
196 * This is the notification for the blocked thread that a
197 * result value is imminent.
198 * * call wake_up_process
199 * * set queue.status to the final value.
200 * - the previously blocked thread checks queue.status:
201 * * if it's IN_WAKEUP, then it must wait until the value changes
202 * * if it's not -EINTR, then the operation was completed by
203 * update_queue. semtimedop can return queue.status without
204 * performing any operation on the sem array.
205 * * otherwise it must acquire the spinlock and check what's up.
207 * The two-stage algorithm is necessary to protect against the following
208 * races:
209 * - if queue.status is set after wake_up_process, then the woken up idle
210 * thread could race forward and try (and fail) to acquire sma->lock
211 * before update_queue had a chance to set queue.status
212 * - if queue.status is written before wake_up_process and if the
213 * blocked process is woken up by a signal between writing
214 * queue.status and the wake_up_process, then the woken up
215 * process could return from semtimedop and die by calling
216 * sys_exit before wake_up_process is called. Then wake_up_process
217 * will oops, because the task structure is already invalid.
218 * (yes, this happened on s390 with sysv msg).
221 #define IN_WAKEUP 1
224 * newary - Create a new semaphore set
225 * @ns: namespace
226 * @params: ptr to the structure that contains key, semflg and nsems
228 * Called with sem_ids.rw_mutex held (as a writer)
231 static int newary(struct ipc_namespace *ns, struct ipc_params *params)
233 int id;
234 int retval;
235 struct sem_array *sma;
236 int size;
237 key_t key = params->key;
238 int nsems = params->u.nsems;
239 int semflg = params->flg;
241 if (!nsems)
242 return -EINVAL;
243 if (ns->used_sems + nsems > ns->sc_semmns)
244 return -ENOSPC;
246 size = sizeof (*sma) + nsems * sizeof (struct sem);
247 sma = ipc_rcu_alloc(size);
248 if (!sma) {
249 return -ENOMEM;
251 memset (sma, 0, size);
253 sma->sem_perm.mode = (semflg & S_IRWXUGO);
254 sma->sem_perm.key = key;
256 sma->sem_perm.security = NULL;
257 retval = security_sem_alloc(sma);
258 if (retval) {
259 ipc_rcu_putref(sma);
260 return retval;
263 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
264 if (id < 0) {
265 security_sem_free(sma);
266 ipc_rcu_putref(sma);
267 return id;
269 ns->used_sems += nsems;
271 sma->sem_perm.id = sem_buildid(id, sma->sem_perm.seq);
272 sma->sem_base = (struct sem *) &sma[1];
273 /* sma->sem_pending = NULL; */
274 sma->sem_pending_last = &sma->sem_pending;
275 /* sma->undo = NULL; */
276 sma->sem_nsems = nsems;
277 sma->sem_ctime = get_seconds();
278 sem_unlock(sma);
280 return sma->sem_perm.id;
285 * Called with sem_ids.rw_mutex and ipcp locked.
287 static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
289 struct sem_array *sma;
291 sma = container_of(ipcp, struct sem_array, sem_perm);
292 return security_sem_associate(sma, semflg);
296 * Called with sem_ids.rw_mutex and ipcp locked.
298 static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
299 struct ipc_params *params)
301 struct sem_array *sma;
303 sma = container_of(ipcp, struct sem_array, sem_perm);
304 if (params->u.nsems > sma->sem_nsems)
305 return -EINVAL;
307 return 0;
310 asmlinkage long sys_semget(key_t key, int nsems, int semflg)
312 struct ipc_namespace *ns;
313 struct ipc_ops sem_ops;
314 struct ipc_params sem_params;
316 ns = current->nsproxy->ipc_ns;
318 if (nsems < 0 || nsems > ns->sc_semmsl)
319 return -EINVAL;
321 sem_ops.getnew = newary;
322 sem_ops.associate = sem_security;
323 sem_ops.more_checks = sem_more_checks;
325 sem_params.key = key;
326 sem_params.flg = semflg;
327 sem_params.u.nsems = nsems;
329 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
332 /* Manage the doubly linked list sma->sem_pending as a FIFO:
333 * insert new queue elements at the tail sma->sem_pending_last.
335 static inline void append_to_queue (struct sem_array * sma,
336 struct sem_queue * q)
338 *(q->prev = sma->sem_pending_last) = q;
339 *(sma->sem_pending_last = &q->next) = NULL;
342 static inline void prepend_to_queue (struct sem_array * sma,
343 struct sem_queue * q)
345 q->next = sma->sem_pending;
346 *(q->prev = &sma->sem_pending) = q;
347 if (q->next)
348 q->next->prev = &q->next;
349 else /* sma->sem_pending_last == &sma->sem_pending */
350 sma->sem_pending_last = &q->next;
353 static inline void remove_from_queue (struct sem_array * sma,
354 struct sem_queue * q)
356 *(q->prev) = q->next;
357 if (q->next)
358 q->next->prev = q->prev;
359 else /* sma->sem_pending_last == &q->next */
360 sma->sem_pending_last = q->prev;
361 q->prev = NULL; /* mark as removed */
365 * Determine whether a sequence of semaphore operations would succeed
366 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
369 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
370 int nsops, struct sem_undo *un, int pid)
372 int result, sem_op;
373 struct sembuf *sop;
374 struct sem * curr;
376 for (sop = sops; sop < sops + nsops; sop++) {
377 curr = sma->sem_base + sop->sem_num;
378 sem_op = sop->sem_op;
379 result = curr->semval;
381 if (!sem_op && result)
382 goto would_block;
384 result += sem_op;
385 if (result < 0)
386 goto would_block;
387 if (result > SEMVMX)
388 goto out_of_range;
389 if (sop->sem_flg & SEM_UNDO) {
390 int undo = un->semadj[sop->sem_num] - sem_op;
392 * Exceeding the undo range is an error.
394 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
395 goto out_of_range;
397 curr->semval = result;
400 sop--;
401 while (sop >= sops) {
402 sma->sem_base[sop->sem_num].sempid = pid;
403 if (sop->sem_flg & SEM_UNDO)
404 un->semadj[sop->sem_num] -= sop->sem_op;
405 sop--;
408 sma->sem_otime = get_seconds();
409 return 0;
411 out_of_range:
412 result = -ERANGE;
413 goto undo;
415 would_block:
416 if (sop->sem_flg & IPC_NOWAIT)
417 result = -EAGAIN;
418 else
419 result = 1;
421 undo:
422 sop--;
423 while (sop >= sops) {
424 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
425 sop--;
428 return result;
431 /* Go through the pending queue for the indicated semaphore
432 * looking for tasks that can be completed.
434 static void update_queue (struct sem_array * sma)
436 int error;
437 struct sem_queue * q;
439 q = sma->sem_pending;
440 while(q) {
441 error = try_atomic_semop(sma, q->sops, q->nsops,
442 q->undo, q->pid);
444 /* Does q->sleeper still need to sleep? */
445 if (error <= 0) {
446 struct sem_queue *n;
447 remove_from_queue(sma,q);
448 q->status = IN_WAKEUP;
450 * Continue scanning. The next operation
451 * that must be checked depends on the type of the
452 * completed operation:
453 * - if the operation modified the array, then
454 * restart from the head of the queue and
455 * check for threads that might be waiting
456 * for semaphore values to become 0.
457 * - if the operation didn't modify the array,
458 * then just continue.
460 if (q->alter)
461 n = sma->sem_pending;
462 else
463 n = q->next;
464 wake_up_process(q->sleeper);
465 /* hands-off: q will disappear immediately after
466 * writing q->status.
468 smp_wmb();
469 q->status = error;
470 q = n;
471 } else {
472 q = q->next;
477 /* The following counts are associated to each semaphore:
478 * semncnt number of tasks waiting on semval being nonzero
479 * semzcnt number of tasks waiting on semval being zero
480 * This model assumes that a task waits on exactly one semaphore.
481 * Since semaphore operations are to be performed atomically, tasks actually
482 * wait on a whole sequence of semaphores simultaneously.
483 * The counts we return here are a rough approximation, but still
484 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
486 static int count_semncnt (struct sem_array * sma, ushort semnum)
488 int semncnt;
489 struct sem_queue * q;
491 semncnt = 0;
492 for (q = sma->sem_pending; q; q = q->next) {
493 struct sembuf * sops = q->sops;
494 int nsops = q->nsops;
495 int i;
496 for (i = 0; i < nsops; i++)
497 if (sops[i].sem_num == semnum
498 && (sops[i].sem_op < 0)
499 && !(sops[i].sem_flg & IPC_NOWAIT))
500 semncnt++;
502 return semncnt;
504 static int count_semzcnt (struct sem_array * sma, ushort semnum)
506 int semzcnt;
507 struct sem_queue * q;
509 semzcnt = 0;
510 for (q = sma->sem_pending; q; q = q->next) {
511 struct sembuf * sops = q->sops;
512 int nsops = q->nsops;
513 int i;
514 for (i = 0; i < nsops; i++)
515 if (sops[i].sem_num == semnum
516 && (sops[i].sem_op == 0)
517 && !(sops[i].sem_flg & IPC_NOWAIT))
518 semzcnt++;
520 return semzcnt;
523 /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
524 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
525 * remains locked on exit.
527 static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
529 struct sem_undo *un;
530 struct sem_queue *q;
531 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
533 /* Invalidate the existing undo structures for this semaphore set.
534 * (They will be freed without any further action in exit_sem()
535 * or during the next semop.)
537 for (un = sma->undo; un; un = un->id_next)
538 un->semid = -1;
540 /* Wake up all pending processes and let them fail with EIDRM. */
541 q = sma->sem_pending;
542 while(q) {
543 struct sem_queue *n;
544 /* lazy remove_from_queue: we are killing the whole queue */
545 q->prev = NULL;
546 n = q->next;
547 q->status = IN_WAKEUP;
548 wake_up_process(q->sleeper); /* doesn't sleep */
549 smp_wmb();
550 q->status = -EIDRM; /* hands-off q */
551 q = n;
554 /* Remove the semaphore set from the IDR */
555 sem_rmid(ns, sma);
556 sem_unlock(sma);
558 ns->used_sems -= sma->sem_nsems;
559 security_sem_free(sma);
560 ipc_rcu_putref(sma);
563 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
565 switch(version) {
566 case IPC_64:
567 return copy_to_user(buf, in, sizeof(*in));
568 case IPC_OLD:
570 struct semid_ds out;
572 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
574 out.sem_otime = in->sem_otime;
575 out.sem_ctime = in->sem_ctime;
576 out.sem_nsems = in->sem_nsems;
578 return copy_to_user(buf, &out, sizeof(out));
580 default:
581 return -EINVAL;
585 static int semctl_nolock(struct ipc_namespace *ns, int semid,
586 int cmd, int version, union semun arg)
588 int err = -EINVAL;
589 struct sem_array *sma;
591 switch(cmd) {
592 case IPC_INFO:
593 case SEM_INFO:
595 struct seminfo seminfo;
596 int max_id;
598 err = security_sem_semctl(NULL, cmd);
599 if (err)
600 return err;
602 memset(&seminfo,0,sizeof(seminfo));
603 seminfo.semmni = ns->sc_semmni;
604 seminfo.semmns = ns->sc_semmns;
605 seminfo.semmsl = ns->sc_semmsl;
606 seminfo.semopm = ns->sc_semopm;
607 seminfo.semvmx = SEMVMX;
608 seminfo.semmnu = SEMMNU;
609 seminfo.semmap = SEMMAP;
610 seminfo.semume = SEMUME;
611 down_read(&sem_ids(ns).rw_mutex);
612 if (cmd == SEM_INFO) {
613 seminfo.semusz = sem_ids(ns).in_use;
614 seminfo.semaem = ns->used_sems;
615 } else {
616 seminfo.semusz = SEMUSZ;
617 seminfo.semaem = SEMAEM;
619 max_id = ipc_get_maxid(&sem_ids(ns));
620 up_read(&sem_ids(ns).rw_mutex);
621 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
622 return -EFAULT;
623 return (max_id < 0) ? 0: max_id;
625 case IPC_STAT:
626 case SEM_STAT:
628 struct semid64_ds tbuf;
629 int id;
631 if (cmd == SEM_STAT) {
632 sma = sem_lock(ns, semid);
633 if (IS_ERR(sma))
634 return PTR_ERR(sma);
635 id = sma->sem_perm.id;
636 } else {
637 sma = sem_lock_check(ns, semid);
638 if (IS_ERR(sma))
639 return PTR_ERR(sma);
640 id = 0;
643 err = -EACCES;
644 if (ipcperms (&sma->sem_perm, S_IRUGO))
645 goto out_unlock;
647 err = security_sem_semctl(sma, cmd);
648 if (err)
649 goto out_unlock;
651 memset(&tbuf, 0, sizeof(tbuf));
653 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
654 tbuf.sem_otime = sma->sem_otime;
655 tbuf.sem_ctime = sma->sem_ctime;
656 tbuf.sem_nsems = sma->sem_nsems;
657 sem_unlock(sma);
658 if (copy_semid_to_user (arg.buf, &tbuf, version))
659 return -EFAULT;
660 return id;
662 default:
663 return -EINVAL;
665 return err;
666 out_unlock:
667 sem_unlock(sma);
668 return err;
671 static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
672 int cmd, int version, union semun arg)
674 struct sem_array *sma;
675 struct sem* curr;
676 int err;
677 ushort fast_sem_io[SEMMSL_FAST];
678 ushort* sem_io = fast_sem_io;
679 int nsems;
681 sma = sem_lock_check(ns, semid);
682 if (IS_ERR(sma))
683 return PTR_ERR(sma);
685 nsems = sma->sem_nsems;
687 err = -EACCES;
688 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
689 goto out_unlock;
691 err = security_sem_semctl(sma, cmd);
692 if (err)
693 goto out_unlock;
695 err = -EACCES;
696 switch (cmd) {
697 case GETALL:
699 ushort __user *array = arg.array;
700 int i;
702 if(nsems > SEMMSL_FAST) {
703 ipc_rcu_getref(sma);
704 sem_unlock(sma);
706 sem_io = ipc_alloc(sizeof(ushort)*nsems);
707 if(sem_io == NULL) {
708 ipc_lock_by_ptr(&sma->sem_perm);
709 ipc_rcu_putref(sma);
710 sem_unlock(sma);
711 return -ENOMEM;
714 ipc_lock_by_ptr(&sma->sem_perm);
715 ipc_rcu_putref(sma);
716 if (sma->sem_perm.deleted) {
717 sem_unlock(sma);
718 err = -EIDRM;
719 goto out_free;
723 for (i = 0; i < sma->sem_nsems; i++)
724 sem_io[i] = sma->sem_base[i].semval;
725 sem_unlock(sma);
726 err = 0;
727 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
728 err = -EFAULT;
729 goto out_free;
731 case SETALL:
733 int i;
734 struct sem_undo *un;
736 ipc_rcu_getref(sma);
737 sem_unlock(sma);
739 if(nsems > SEMMSL_FAST) {
740 sem_io = ipc_alloc(sizeof(ushort)*nsems);
741 if(sem_io == NULL) {
742 ipc_lock_by_ptr(&sma->sem_perm);
743 ipc_rcu_putref(sma);
744 sem_unlock(sma);
745 return -ENOMEM;
749 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
750 ipc_lock_by_ptr(&sma->sem_perm);
751 ipc_rcu_putref(sma);
752 sem_unlock(sma);
753 err = -EFAULT;
754 goto out_free;
757 for (i = 0; i < nsems; i++) {
758 if (sem_io[i] > SEMVMX) {
759 ipc_lock_by_ptr(&sma->sem_perm);
760 ipc_rcu_putref(sma);
761 sem_unlock(sma);
762 err = -ERANGE;
763 goto out_free;
766 ipc_lock_by_ptr(&sma->sem_perm);
767 ipc_rcu_putref(sma);
768 if (sma->sem_perm.deleted) {
769 sem_unlock(sma);
770 err = -EIDRM;
771 goto out_free;
774 for (i = 0; i < nsems; i++)
775 sma->sem_base[i].semval = sem_io[i];
776 for (un = sma->undo; un; un = un->id_next)
777 for (i = 0; i < nsems; i++)
778 un->semadj[i] = 0;
779 sma->sem_ctime = get_seconds();
780 /* maybe some queued-up processes were waiting for this */
781 update_queue(sma);
782 err = 0;
783 goto out_unlock;
785 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
787 err = -EINVAL;
788 if(semnum < 0 || semnum >= nsems)
789 goto out_unlock;
791 curr = &sma->sem_base[semnum];
793 switch (cmd) {
794 case GETVAL:
795 err = curr->semval;
796 goto out_unlock;
797 case GETPID:
798 err = curr->sempid;
799 goto out_unlock;
800 case GETNCNT:
801 err = count_semncnt(sma,semnum);
802 goto out_unlock;
803 case GETZCNT:
804 err = count_semzcnt(sma,semnum);
805 goto out_unlock;
806 case SETVAL:
808 int val = arg.val;
809 struct sem_undo *un;
810 err = -ERANGE;
811 if (val > SEMVMX || val < 0)
812 goto out_unlock;
814 for (un = sma->undo; un; un = un->id_next)
815 un->semadj[semnum] = 0;
816 curr->semval = val;
817 curr->sempid = task_tgid_vnr(current);
818 sma->sem_ctime = get_seconds();
819 /* maybe some queued-up processes were waiting for this */
820 update_queue(sma);
821 err = 0;
822 goto out_unlock;
825 out_unlock:
826 sem_unlock(sma);
827 out_free:
828 if(sem_io != fast_sem_io)
829 ipc_free(sem_io, sizeof(ushort)*nsems);
830 return err;
833 struct sem_setbuf {
834 uid_t uid;
835 gid_t gid;
836 mode_t mode;
839 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
841 switch(version) {
842 case IPC_64:
844 struct semid64_ds tbuf;
846 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
847 return -EFAULT;
849 out->uid = tbuf.sem_perm.uid;
850 out->gid = tbuf.sem_perm.gid;
851 out->mode = tbuf.sem_perm.mode;
853 return 0;
855 case IPC_OLD:
857 struct semid_ds tbuf_old;
859 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
860 return -EFAULT;
862 out->uid = tbuf_old.sem_perm.uid;
863 out->gid = tbuf_old.sem_perm.gid;
864 out->mode = tbuf_old.sem_perm.mode;
866 return 0;
868 default:
869 return -EINVAL;
873 static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
874 int cmd, int version, union semun arg)
876 struct sem_array *sma;
877 int err;
878 struct sem_setbuf uninitialized_var(setbuf);
879 struct kern_ipc_perm *ipcp;
881 if(cmd == IPC_SET) {
882 if(copy_semid_from_user (&setbuf, arg.buf, version))
883 return -EFAULT;
885 sma = sem_lock_check_down(ns, semid);
886 if (IS_ERR(sma))
887 return PTR_ERR(sma);
889 ipcp = &sma->sem_perm;
891 err = audit_ipc_obj(ipcp);
892 if (err)
893 goto out_unlock;
895 if (cmd == IPC_SET) {
896 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
897 if (err)
898 goto out_unlock;
900 if (current->euid != ipcp->cuid &&
901 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
902 err=-EPERM;
903 goto out_unlock;
906 err = security_sem_semctl(sma, cmd);
907 if (err)
908 goto out_unlock;
910 switch(cmd){
911 case IPC_RMID:
912 freeary(ns, ipcp);
913 err = 0;
914 break;
915 case IPC_SET:
916 ipcp->uid = setbuf.uid;
917 ipcp->gid = setbuf.gid;
918 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
919 | (setbuf.mode & S_IRWXUGO);
920 sma->sem_ctime = get_seconds();
921 sem_unlock(sma);
922 err = 0;
923 break;
924 default:
925 sem_unlock(sma);
926 err = -EINVAL;
927 break;
929 return err;
931 out_unlock:
932 sem_unlock(sma);
933 return err;
936 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
938 int err = -EINVAL;
939 int version;
940 struct ipc_namespace *ns;
942 if (semid < 0)
943 return -EINVAL;
945 version = ipc_parse_version(&cmd);
946 ns = current->nsproxy->ipc_ns;
948 switch(cmd) {
949 case IPC_INFO:
950 case SEM_INFO:
951 case IPC_STAT:
952 case SEM_STAT:
953 err = semctl_nolock(ns, semid, cmd, version, arg);
954 return err;
955 case GETALL:
956 case GETVAL:
957 case GETPID:
958 case GETNCNT:
959 case GETZCNT:
960 case SETVAL:
961 case SETALL:
962 err = semctl_main(ns,semid,semnum,cmd,version,arg);
963 return err;
964 case IPC_RMID:
965 case IPC_SET:
966 down_write(&sem_ids(ns).rw_mutex);
967 err = semctl_down(ns,semid,semnum,cmd,version,arg);
968 up_write(&sem_ids(ns).rw_mutex);
969 return err;
970 default:
971 return -EINVAL;
975 /* If the task doesn't already have a undo_list, then allocate one
976 * here. We guarantee there is only one thread using this undo list,
977 * and current is THE ONE
979 * If this allocation and assignment succeeds, but later
980 * portions of this code fail, there is no need to free the sem_undo_list.
981 * Just let it stay associated with the task, and it'll be freed later
982 * at exit time.
984 * This can block, so callers must hold no locks.
986 static inline int get_undo_list(struct sem_undo_list **undo_listp)
988 struct sem_undo_list *undo_list;
990 undo_list = current->sysvsem.undo_list;
991 if (!undo_list) {
992 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
993 if (undo_list == NULL)
994 return -ENOMEM;
995 spin_lock_init(&undo_list->lock);
996 atomic_set(&undo_list->refcnt, 1);
997 current->sysvsem.undo_list = undo_list;
999 *undo_listp = undo_list;
1000 return 0;
1003 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1005 struct sem_undo **last, *un;
1007 last = &ulp->proc_list;
1008 un = *last;
1009 while(un != NULL) {
1010 if(un->semid==semid)
1011 break;
1012 if(un->semid==-1) {
1013 *last=un->proc_next;
1014 kfree(un);
1015 } else {
1016 last=&un->proc_next;
1018 un=*last;
1020 return un;
1023 static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
1025 struct sem_array *sma;
1026 struct sem_undo_list *ulp;
1027 struct sem_undo *un, *new;
1028 int nsems;
1029 int error;
1031 error = get_undo_list(&ulp);
1032 if (error)
1033 return ERR_PTR(error);
1035 spin_lock(&ulp->lock);
1036 un = lookup_undo(ulp, semid);
1037 spin_unlock(&ulp->lock);
1038 if (likely(un!=NULL))
1039 goto out;
1041 /* no undo structure around - allocate one. */
1042 sma = sem_lock_check(ns, semid);
1043 if (IS_ERR(sma))
1044 return ERR_PTR(PTR_ERR(sma));
1046 nsems = sma->sem_nsems;
1047 ipc_rcu_getref(sma);
1048 sem_unlock(sma);
1050 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1051 if (!new) {
1052 ipc_lock_by_ptr(&sma->sem_perm);
1053 ipc_rcu_putref(sma);
1054 sem_unlock(sma);
1055 return ERR_PTR(-ENOMEM);
1057 new->semadj = (short *) &new[1];
1058 new->semid = semid;
1060 spin_lock(&ulp->lock);
1061 un = lookup_undo(ulp, semid);
1062 if (un) {
1063 spin_unlock(&ulp->lock);
1064 kfree(new);
1065 ipc_lock_by_ptr(&sma->sem_perm);
1066 ipc_rcu_putref(sma);
1067 sem_unlock(sma);
1068 goto out;
1070 ipc_lock_by_ptr(&sma->sem_perm);
1071 ipc_rcu_putref(sma);
1072 if (sma->sem_perm.deleted) {
1073 sem_unlock(sma);
1074 spin_unlock(&ulp->lock);
1075 kfree(new);
1076 un = ERR_PTR(-EIDRM);
1077 goto out;
1079 new->proc_next = ulp->proc_list;
1080 ulp->proc_list = new;
1081 new->id_next = sma->undo;
1082 sma->undo = new;
1083 sem_unlock(sma);
1084 un = new;
1085 spin_unlock(&ulp->lock);
1086 out:
1087 return un;
1090 asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1091 unsigned nsops, const struct timespec __user *timeout)
1093 int error = -EINVAL;
1094 struct sem_array *sma;
1095 struct sembuf fast_sops[SEMOPM_FAST];
1096 struct sembuf* sops = fast_sops, *sop;
1097 struct sem_undo *un;
1098 int undos = 0, alter = 0, max;
1099 struct sem_queue queue;
1100 unsigned long jiffies_left = 0;
1101 struct ipc_namespace *ns;
1103 ns = current->nsproxy->ipc_ns;
1105 if (nsops < 1 || semid < 0)
1106 return -EINVAL;
1107 if (nsops > ns->sc_semopm)
1108 return -E2BIG;
1109 if(nsops > SEMOPM_FAST) {
1110 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1111 if(sops==NULL)
1112 return -ENOMEM;
1114 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1115 error=-EFAULT;
1116 goto out_free;
1118 if (timeout) {
1119 struct timespec _timeout;
1120 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1121 error = -EFAULT;
1122 goto out_free;
1124 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1125 _timeout.tv_nsec >= 1000000000L) {
1126 error = -EINVAL;
1127 goto out_free;
1129 jiffies_left = timespec_to_jiffies(&_timeout);
1131 max = 0;
1132 for (sop = sops; sop < sops + nsops; sop++) {
1133 if (sop->sem_num >= max)
1134 max = sop->sem_num;
1135 if (sop->sem_flg & SEM_UNDO)
1136 undos = 1;
1137 if (sop->sem_op != 0)
1138 alter = 1;
1141 retry_undos:
1142 if (undos) {
1143 un = find_undo(ns, semid);
1144 if (IS_ERR(un)) {
1145 error = PTR_ERR(un);
1146 goto out_free;
1148 } else
1149 un = NULL;
1151 sma = sem_lock_check(ns, semid);
1152 if (IS_ERR(sma)) {
1153 error = PTR_ERR(sma);
1154 goto out_free;
1158 * semid identifiers are not unique - find_undo may have
1159 * allocated an undo structure, it was invalidated by an RMID
1160 * and now a new array with received the same id. Check and retry.
1162 if (un && un->semid == -1) {
1163 sem_unlock(sma);
1164 goto retry_undos;
1166 error = -EFBIG;
1167 if (max >= sma->sem_nsems)
1168 goto out_unlock_free;
1170 error = -EACCES;
1171 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1172 goto out_unlock_free;
1174 error = security_sem_semop(sma, sops, nsops, alter);
1175 if (error)
1176 goto out_unlock_free;
1178 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
1179 if (error <= 0) {
1180 if (alter && error == 0)
1181 update_queue (sma);
1182 goto out_unlock_free;
1185 /* We need to sleep on this operation, so we put the current
1186 * task into the pending queue and go to sleep.
1189 queue.sma = sma;
1190 queue.sops = sops;
1191 queue.nsops = nsops;
1192 queue.undo = un;
1193 queue.pid = task_tgid_vnr(current);
1194 queue.id = semid;
1195 queue.alter = alter;
1196 if (alter)
1197 append_to_queue(sma ,&queue);
1198 else
1199 prepend_to_queue(sma ,&queue);
1201 queue.status = -EINTR;
1202 queue.sleeper = current;
1203 current->state = TASK_INTERRUPTIBLE;
1204 sem_unlock(sma);
1206 if (timeout)
1207 jiffies_left = schedule_timeout(jiffies_left);
1208 else
1209 schedule();
1211 error = queue.status;
1212 while(unlikely(error == IN_WAKEUP)) {
1213 cpu_relax();
1214 error = queue.status;
1217 if (error != -EINTR) {
1218 /* fast path: update_queue already obtained all requested
1219 * resources */
1220 goto out_free;
1223 sma = sem_lock(ns, semid);
1224 if (IS_ERR(sma)) {
1225 BUG_ON(queue.prev != NULL);
1226 error = -EIDRM;
1227 goto out_free;
1231 * If queue.status != -EINTR we are woken up by another process
1233 error = queue.status;
1234 if (error != -EINTR) {
1235 goto out_unlock_free;
1239 * If an interrupt occurred we have to clean up the queue
1241 if (timeout && jiffies_left == 0)
1242 error = -EAGAIN;
1243 remove_from_queue(sma,&queue);
1244 goto out_unlock_free;
1246 out_unlock_free:
1247 sem_unlock(sma);
1248 out_free:
1249 if(sops != fast_sops)
1250 kfree(sops);
1251 return error;
1254 asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1256 return sys_semtimedop(semid, tsops, nsops, NULL);
1259 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1260 * parent and child tasks.
1263 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1265 struct sem_undo_list *undo_list;
1266 int error;
1268 if (clone_flags & CLONE_SYSVSEM) {
1269 error = get_undo_list(&undo_list);
1270 if (error)
1271 return error;
1272 atomic_inc(&undo_list->refcnt);
1273 tsk->sysvsem.undo_list = undo_list;
1274 } else
1275 tsk->sysvsem.undo_list = NULL;
1277 return 0;
1281 * add semadj values to semaphores, free undo structures.
1282 * undo structures are not freed when semaphore arrays are destroyed
1283 * so some of them may be out of date.
1284 * IMPLEMENTATION NOTE: There is some confusion over whether the
1285 * set of adjustments that needs to be done should be done in an atomic
1286 * manner or not. That is, if we are attempting to decrement the semval
1287 * should we queue up and wait until we can do so legally?
1288 * The original implementation attempted to do this (queue and wait).
1289 * The current implementation does not do so. The POSIX standard
1290 * and SVID should be consulted to determine what behavior is mandated.
1292 void exit_sem(struct task_struct *tsk)
1294 struct sem_undo_list *undo_list;
1295 struct sem_undo *u, **up;
1296 struct ipc_namespace *ns;
1298 undo_list = tsk->sysvsem.undo_list;
1299 if (!undo_list)
1300 return;
1302 if (!atomic_dec_and_test(&undo_list->refcnt))
1303 return;
1305 ns = tsk->nsproxy->ipc_ns;
1306 /* There's no need to hold the semundo list lock, as current
1307 * is the last task exiting for this undo list.
1309 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1310 struct sem_array *sma;
1311 int nsems, i;
1312 struct sem_undo *un, **unp;
1313 int semid;
1315 semid = u->semid;
1317 if(semid == -1)
1318 continue;
1319 sma = sem_lock(ns, semid);
1320 if (IS_ERR(sma))
1321 continue;
1323 if (u->semid == -1)
1324 goto next_entry;
1326 BUG_ON(sem_checkid(sma, u->semid));
1328 /* remove u from the sma->undo list */
1329 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1330 if (u == un)
1331 goto found;
1333 printk ("exit_sem undo list error id=%d\n", u->semid);
1334 goto next_entry;
1335 found:
1336 *unp = un->id_next;
1337 /* perform adjustments registered in u */
1338 nsems = sma->sem_nsems;
1339 for (i = 0; i < nsems; i++) {
1340 struct sem * semaphore = &sma->sem_base[i];
1341 if (u->semadj[i]) {
1342 semaphore->semval += u->semadj[i];
1344 * Range checks of the new semaphore value,
1345 * not defined by sus:
1346 * - Some unices ignore the undo entirely
1347 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1348 * - some cap the value (e.g. FreeBSD caps
1349 * at 0, but doesn't enforce SEMVMX)
1351 * Linux caps the semaphore value, both at 0
1352 * and at SEMVMX.
1354 * Manfred <manfred@colorfullife.com>
1356 if (semaphore->semval < 0)
1357 semaphore->semval = 0;
1358 if (semaphore->semval > SEMVMX)
1359 semaphore->semval = SEMVMX;
1360 semaphore->sempid = task_tgid_vnr(current);
1363 sma->sem_otime = get_seconds();
1364 /* maybe some queued-up processes were waiting for this */
1365 update_queue(sma);
1366 next_entry:
1367 sem_unlock(sma);
1369 kfree(undo_list);
1372 #ifdef CONFIG_PROC_FS
1373 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1375 struct sem_array *sma = it;
1377 return seq_printf(s,
1378 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1379 sma->sem_perm.key,
1380 sma->sem_perm.id,
1381 sma->sem_perm.mode,
1382 sma->sem_nsems,
1383 sma->sem_perm.uid,
1384 sma->sem_perm.gid,
1385 sma->sem_perm.cuid,
1386 sma->sem_perm.cgid,
1387 sma->sem_otime,
1388 sma->sem_ctime);
1390 #endif