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