fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.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  * 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.
32  *
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.
51  *
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.
55  *
56  * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
57  *
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>
64  *
65  * support for audit of ipc object properties and permission changes
66  * Dustin Kirkland <dustin.kirkland@us.ibm.com>
67  *
68  * namespaces support
69  * OpenVZ, SWsoft Inc.
70  * Pavel Emelianov <xemul@openvz.org>
71  */
72
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/smp_lock.h>
79 #include <linux/security.h>
80 #include <linux/syscalls.h>
81 #include <linux/audit.h>
82 #include <linux/capability.h>
83 #include <linux/seq_file.h>
84 #include <linux/mutex.h>
85 #include <linux/nsproxy.h>
86 #include <linux/vs_base.h>
87 #include <linux/vs_limit.h>
88
89 #include <asm/uaccess.h>
90 #include "util.h"
91
92 #define sem_ids(ns)     (*((ns)->ids[IPC_SEM_IDS]))
93
94 #define sem_lock(ns, id)        ((struct sem_array*)ipc_lock(&sem_ids(ns), id))
95 #define sem_unlock(sma)         ipc_unlock(&(sma)->sem_perm)
96 #define sem_rmid(ns, id)        ((struct sem_array*)ipc_rmid(&sem_ids(ns), id))
97 #define sem_checkid(ns, sma, semid)     \
98         ipc_checkid(&sem_ids(ns),&sma->sem_perm,semid)
99 #define sem_buildid(ns, id, seq) \
100         ipc_buildid(&sem_ids(ns), id, seq)
101
102 static struct ipc_ids init_sem_ids;
103
104 static int newary(struct ipc_namespace *, key_t, int, int);
105 static void freeary(struct ipc_namespace *ns, struct sem_array *sma, int id);
106 #ifdef CONFIG_PROC_FS
107 static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
108 #endif
109
110 #define SEMMSL_FAST     256 /* 512 bytes on stack */
111 #define SEMOPM_FAST     64  /* ~ 372 bytes on stack */
112
113 /*
114  * linked list protection:
115  *      sem_undo.id_next,
116  *      sem_array.sem_pending{,last},
117  *      sem_array.sem_undo: sem_lock() for read/write
118  *      sem_undo.proc_next: only "current" is allowed to read/write that field.
119  *      
120  */
121
122 #define sc_semmsl       sem_ctls[0]
123 #define sc_semmns       sem_ctls[1]
124 #define sc_semopm       sem_ctls[2]
125 #define sc_semmni       sem_ctls[3]
126
127 static void __ipc_init __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
128 {
129         ns->ids[IPC_SEM_IDS] = ids;
130         ns->sc_semmsl = SEMMSL;
131         ns->sc_semmns = SEMMNS;
132         ns->sc_semopm = SEMOPM;
133         ns->sc_semmni = SEMMNI;
134         ns->used_sems = 0;
135         ipc_init_ids(ids, ns->sc_semmni);
136 }
137
138 #ifdef CONFIG_IPC_NS
139 int sem_init_ns(struct ipc_namespace *ns)
140 {
141         struct ipc_ids *ids;
142
143         ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
144         if (ids == NULL)
145                 return -ENOMEM;
146
147         __sem_init_ns(ns, ids);
148         return 0;
149 }
150
151 void sem_exit_ns(struct ipc_namespace *ns)
152 {
153         int i;
154         struct sem_array *sma;
155
156         mutex_lock(&sem_ids(ns).mutex);
157         for (i = 0; i <= sem_ids(ns).max_id; i++) {
158                 sma = sem_lock(ns, i);
159                 if (sma == NULL)
160                         continue;
161
162                 freeary(ns, sma, i);
163         }
164         mutex_unlock(&sem_ids(ns).mutex);
165
166         ipc_fini_ids(ns->ids[IPC_SEM_IDS]);
167         kfree(ns->ids[IPC_SEM_IDS]);
168         ns->ids[IPC_SEM_IDS] = NULL;
169 }
170 #endif
171
172 void __init sem_init (void)
173 {
174         __sem_init_ns(&init_ipc_ns, &init_sem_ids);
175         ipc_init_proc_interface("sysvipc/sem",
176                                 "       key      semid perms      nsems   uid   gid  cuid  cgid      otime      ctime\n",
177                                 IPC_SEM_IDS, sysvipc_sem_proc_show);
178 }
179
180 /*
181  * Lockless wakeup algorithm:
182  * Without the check/retry algorithm a lockless wakeup is possible:
183  * - queue.status is initialized to -EINTR before blocking.
184  * - wakeup is performed by
185  *      * unlinking the queue entry from sma->sem_pending
186  *      * setting queue.status to IN_WAKEUP
187  *        This is the notification for the blocked thread that a
188  *        result value is imminent.
189  *      * call wake_up_process
190  *      * set queue.status to the final value.
191  * - the previously blocked thread checks queue.status:
192  *      * if it's IN_WAKEUP, then it must wait until the value changes
193  *      * if it's not -EINTR, then the operation was completed by
194  *        update_queue. semtimedop can return queue.status without
195  *        performing any operation on the sem array.
196  *      * otherwise it must acquire the spinlock and check what's up.
197  *
198  * The two-stage algorithm is necessary to protect against the following
199  * races:
200  * - if queue.status is set after wake_up_process, then the woken up idle
201  *   thread could race forward and try (and fail) to acquire sma->lock
202  *   before update_queue had a chance to set queue.status
203  * - if queue.status is written before wake_up_process and if the
204  *   blocked process is woken up by a signal between writing
205  *   queue.status and the wake_up_process, then the woken up
206  *   process could return from semtimedop and die by calling
207  *   sys_exit before wake_up_process is called. Then wake_up_process
208  *   will oops, because the task structure is already invalid.
209  *   (yes, this happened on s390 with sysv msg).
210  *
211  */
212 #define IN_WAKEUP       1
213
214 static int newary (struct ipc_namespace *ns, key_t key, int nsems, int semflg)
215 {
216         int id;
217         int retval;
218         struct sem_array *sma;
219         int size;
220
221         if (!nsems)
222                 return -EINVAL;
223         if (ns->used_sems + nsems > ns->sc_semmns)
224                 return -ENOSPC;
225
226         size = sizeof (*sma) + nsems * sizeof (struct sem);
227         sma = ipc_rcu_alloc(size);
228         if (!sma) {
229                 return -ENOMEM;
230         }
231         memset (sma, 0, size);
232
233         sma->sem_perm.mode = (semflg & S_IRWXUGO);
234         sma->sem_perm.key = key;
235         sma->sem_perm.xid = vx_current_xid();
236
237         sma->sem_perm.security = NULL;
238         retval = security_sem_alloc(sma);
239         if (retval) {
240                 ipc_rcu_putref(sma);
241                 return retval;
242         }
243
244         id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
245         if(id == -1) {
246                 security_sem_free(sma);
247                 ipc_rcu_putref(sma);
248                 return -ENOSPC;
249         }
250         ns->used_sems += nsems;
251         /* FIXME: obsoleted? */
252         vx_semary_inc(sma);
253         vx_nsems_add(sma, nsems);
254
255         sma->sem_id = sem_buildid(ns, id, sma->sem_perm.seq);
256         sma->sem_base = (struct sem *) &sma[1];
257         /* sma->sem_pending = NULL; */
258         sma->sem_pending_last = &sma->sem_pending;
259         /* sma->undo = NULL; */
260         sma->sem_nsems = nsems;
261         sma->sem_ctime = get_seconds();
262         sem_unlock(sma);
263
264         return sma->sem_id;
265 }
266
267 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
268 {
269         int id, err = -EINVAL;
270         struct sem_array *sma;
271         struct ipc_namespace *ns;
272
273         ns = current->nsproxy->ipc_ns;
274
275         if (nsems < 0 || nsems > ns->sc_semmsl)
276                 return -EINVAL;
277         mutex_lock(&sem_ids(ns).mutex);
278         
279         if (key == IPC_PRIVATE) {
280                 err = newary(ns, key, nsems, semflg);
281         } else if ((id = ipc_findkey(&sem_ids(ns), key)) == -1) {  /* key not used */
282                 if (!(semflg & IPC_CREAT))
283                         err = -ENOENT;
284                 else
285                         err = newary(ns, key, nsems, semflg);
286         } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
287                 err = -EEXIST;
288         } else {
289                 sma = sem_lock(ns, id);
290                 BUG_ON(sma==NULL);
291                 if (nsems > sma->sem_nsems)
292                         err = -EINVAL;
293                 else if (ipcperms(&sma->sem_perm, semflg))
294                         err = -EACCES;
295                 else {
296                         int semid = sem_buildid(ns, id, sma->sem_perm.seq);
297                         err = security_sem_associate(sma, semflg);
298                         if (!err)
299                                 err = semid;
300                 }
301                 sem_unlock(sma);
302         }
303
304         mutex_unlock(&sem_ids(ns).mutex);
305         return err;
306 }
307
308 /* Manage the doubly linked list sma->sem_pending as a FIFO:
309  * insert new queue elements at the tail sma->sem_pending_last.
310  */
311 static inline void append_to_queue (struct sem_array * sma,
312                                     struct sem_queue * q)
313 {
314         *(q->prev = sma->sem_pending_last) = q;
315         *(sma->sem_pending_last = &q->next) = NULL;
316 }
317
318 static inline void prepend_to_queue (struct sem_array * sma,
319                                      struct sem_queue * q)
320 {
321         q->next = sma->sem_pending;
322         *(q->prev = &sma->sem_pending) = q;
323         if (q->next)
324                 q->next->prev = &q->next;
325         else /* sma->sem_pending_last == &sma->sem_pending */
326                 sma->sem_pending_last = &q->next;
327 }
328
329 static inline void remove_from_queue (struct sem_array * sma,
330                                       struct sem_queue * q)
331 {
332         *(q->prev) = q->next;
333         if (q->next)
334                 q->next->prev = q->prev;
335         else /* sma->sem_pending_last == &q->next */
336                 sma->sem_pending_last = q->prev;
337         q->prev = NULL; /* mark as removed */
338 }
339
340 /*
341  * Determine whether a sequence of semaphore operations would succeed
342  * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
343  */
344
345 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
346                              int nsops, struct sem_undo *un, int pid)
347 {
348         int result, sem_op;
349         struct sembuf *sop;
350         struct sem * curr;
351
352         for (sop = sops; sop < sops + nsops; sop++) {
353                 curr = sma->sem_base + sop->sem_num;
354                 sem_op = sop->sem_op;
355                 result = curr->semval;
356   
357                 if (!sem_op && result)
358                         goto would_block;
359
360                 result += sem_op;
361                 if (result < 0)
362                         goto would_block;
363                 if (result > SEMVMX)
364                         goto out_of_range;
365                 if (sop->sem_flg & SEM_UNDO) {
366                         int undo = un->semadj[sop->sem_num] - sem_op;
367                         /*
368                          *      Exceeding the undo range is an error.
369                          */
370                         if (undo < (-SEMAEM - 1) || undo > SEMAEM)
371                                 goto out_of_range;
372                 }
373                 curr->semval = result;
374         }
375
376         sop--;
377         while (sop >= sops) {
378                 sma->sem_base[sop->sem_num].sempid = pid;
379                 if (sop->sem_flg & SEM_UNDO)
380                         un->semadj[sop->sem_num] -= sop->sem_op;
381                 sop--;
382         }
383         
384         sma->sem_otime = get_seconds();
385         return 0;
386
387 out_of_range:
388         result = -ERANGE;
389         goto undo;
390
391 would_block:
392         if (sop->sem_flg & IPC_NOWAIT)
393                 result = -EAGAIN;
394         else
395                 result = 1;
396
397 undo:
398         sop--;
399         while (sop >= sops) {
400                 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
401                 sop--;
402         }
403
404         return result;
405 }
406
407 /* Go through the pending queue for the indicated semaphore
408  * looking for tasks that can be completed.
409  */
410 static void update_queue (struct sem_array * sma)
411 {
412         int error;
413         struct sem_queue * q;
414
415         q = sma->sem_pending;
416         while(q) {
417                 error = try_atomic_semop(sma, q->sops, q->nsops,
418                                          q->undo, q->pid);
419
420                 /* Does q->sleeper still need to sleep? */
421                 if (error <= 0) {
422                         struct sem_queue *n;
423                         remove_from_queue(sma,q);
424                         q->status = IN_WAKEUP;
425                         /*
426                          * Continue scanning. The next operation
427                          * that must be checked depends on the type of the
428                          * completed operation:
429                          * - if the operation modified the array, then
430                          *   restart from the head of the queue and
431                          *   check for threads that might be waiting
432                          *   for semaphore values to become 0.
433                          * - if the operation didn't modify the array,
434                          *   then just continue.
435                          */
436                         if (q->alter)
437                                 n = sma->sem_pending;
438                         else
439                                 n = q->next;
440                         wake_up_process(q->sleeper);
441                         /* hands-off: q will disappear immediately after
442                          * writing q->status.
443                          */
444                         smp_wmb();
445                         q->status = error;
446                         q = n;
447                 } else {
448                         q = q->next;
449                 }
450         }
451 }
452
453 /* The following counts are associated to each semaphore:
454  *   semncnt        number of tasks waiting on semval being nonzero
455  *   semzcnt        number of tasks waiting on semval being zero
456  * This model assumes that a task waits on exactly one semaphore.
457  * Since semaphore operations are to be performed atomically, tasks actually
458  * wait on a whole sequence of semaphores simultaneously.
459  * The counts we return here are a rough approximation, but still
460  * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
461  */
462 static int count_semncnt (struct sem_array * sma, ushort semnum)
463 {
464         int semncnt;
465         struct sem_queue * q;
466
467         semncnt = 0;
468         for (q = sma->sem_pending; q; q = q->next) {
469                 struct sembuf * sops = q->sops;
470                 int nsops = q->nsops;
471                 int i;
472                 for (i = 0; i < nsops; i++)
473                         if (sops[i].sem_num == semnum
474                             && (sops[i].sem_op < 0)
475                             && !(sops[i].sem_flg & IPC_NOWAIT))
476                                 semncnt++;
477         }
478         return semncnt;
479 }
480 static int count_semzcnt (struct sem_array * sma, ushort semnum)
481 {
482         int semzcnt;
483         struct sem_queue * q;
484
485         semzcnt = 0;
486         for (q = sma->sem_pending; q; q = q->next) {
487                 struct sembuf * sops = q->sops;
488                 int nsops = q->nsops;
489                 int i;
490                 for (i = 0; i < nsops; i++)
491                         if (sops[i].sem_num == semnum
492                             && (sops[i].sem_op == 0)
493                             && !(sops[i].sem_flg & IPC_NOWAIT))
494                                 semzcnt++;
495         }
496         return semzcnt;
497 }
498
499 /* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
500  * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
501  * on exit.
502  */
503 static void freeary (struct ipc_namespace *ns, struct sem_array *sma, int id)
504 {
505         struct sem_undo *un;
506         struct sem_queue *q;
507         int size;
508
509         /* Invalidate the existing undo structures for this semaphore set.
510          * (They will be freed without any further action in exit_sem()
511          * or during the next semop.)
512          */
513         for (un = sma->undo; un; un = un->id_next)
514                 un->semid = -1;
515
516         /* Wake up all pending processes and let them fail with EIDRM. */
517         q = sma->sem_pending;
518         while(q) {
519                 struct sem_queue *n;
520                 /* lazy remove_from_queue: we are killing the whole queue */
521                 q->prev = NULL;
522                 n = q->next;
523                 q->status = IN_WAKEUP;
524                 wake_up_process(q->sleeper); /* doesn't sleep */
525                 smp_wmb();
526                 q->status = -EIDRM;     /* hands-off q */
527                 q = n;
528         }
529
530         /* Remove the semaphore set from the ID array*/
531         sma = sem_rmid(ns, id);
532         sem_unlock(sma);
533
534         ns->used_sems -= sma->sem_nsems;
535         /* FIXME: obsoleted? */
536         vx_nsems_sub(sma, sma->sem_nsems);
537         vx_semary_dec(sma);
538         size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
539         security_sem_free(sma);
540         ipc_rcu_putref(sma);
541 }
542
543 static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
544 {
545         switch(version) {
546         case IPC_64:
547                 return copy_to_user(buf, in, sizeof(*in));
548         case IPC_OLD:
549             {
550                 struct semid_ds out;
551
552                 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
553
554                 out.sem_otime   = in->sem_otime;
555                 out.sem_ctime   = in->sem_ctime;
556                 out.sem_nsems   = in->sem_nsems;
557
558                 return copy_to_user(buf, &out, sizeof(out));
559             }
560         default:
561                 return -EINVAL;
562         }
563 }
564
565 static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
566                 int cmd, int version, union semun arg)
567 {
568         int err = -EINVAL;
569         struct sem_array *sma;
570
571         switch(cmd) {
572         case IPC_INFO:
573         case SEM_INFO:
574         {
575                 struct seminfo seminfo;
576                 int max_id;
577
578                 err = security_sem_semctl(NULL, cmd);
579                 if (err)
580                         return err;
581                 
582                 memset(&seminfo,0,sizeof(seminfo));
583                 seminfo.semmni = ns->sc_semmni;
584                 seminfo.semmns = ns->sc_semmns;
585                 seminfo.semmsl = ns->sc_semmsl;
586                 seminfo.semopm = ns->sc_semopm;
587                 seminfo.semvmx = SEMVMX;
588                 seminfo.semmnu = SEMMNU;
589                 seminfo.semmap = SEMMAP;
590                 seminfo.semume = SEMUME;
591                 mutex_lock(&sem_ids(ns).mutex);
592                 if (cmd == SEM_INFO) {
593                         seminfo.semusz = sem_ids(ns).in_use;
594                         seminfo.semaem = ns->used_sems;
595                 } else {
596                         seminfo.semusz = SEMUSZ;
597                         seminfo.semaem = SEMAEM;
598                 }
599                 max_id = sem_ids(ns).max_id;
600                 mutex_unlock(&sem_ids(ns).mutex);
601                 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo))) 
602                         return -EFAULT;
603                 return (max_id < 0) ? 0: max_id;
604         }
605         case SEM_STAT:
606         {
607                 struct semid64_ds tbuf;
608                 int id;
609
610                 if(semid >= sem_ids(ns).entries->size)
611                         return -EINVAL;
612
613                 memset(&tbuf,0,sizeof(tbuf));
614
615                 sma = sem_lock(ns, semid);
616                 if(sma == NULL)
617                         return -EINVAL;
618
619                 err = -EACCES;
620                 if (ipcperms (&sma->sem_perm, S_IRUGO))
621                         goto out_unlock;
622
623                 err = security_sem_semctl(sma, cmd);
624                 if (err)
625                         goto out_unlock;
626
627                 id = sem_buildid(ns, semid, sma->sem_perm.seq);
628
629                 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
630                 tbuf.sem_otime  = sma->sem_otime;
631                 tbuf.sem_ctime  = sma->sem_ctime;
632                 tbuf.sem_nsems  = sma->sem_nsems;
633                 sem_unlock(sma);
634                 if (copy_semid_to_user (arg.buf, &tbuf, version))
635                         return -EFAULT;
636                 return id;
637         }
638         default:
639                 return -EINVAL;
640         }
641         return err;
642 out_unlock:
643         sem_unlock(sma);
644         return err;
645 }
646
647 static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
648                 int cmd, int version, union semun arg)
649 {
650         struct sem_array *sma;
651         struct sem* curr;
652         int err;
653         ushort fast_sem_io[SEMMSL_FAST];
654         ushort* sem_io = fast_sem_io;
655         int nsems;
656
657         sma = sem_lock(ns, semid);
658         if(sma==NULL)
659                 return -EINVAL;
660
661         nsems = sma->sem_nsems;
662
663         err=-EIDRM;
664         if (sem_checkid(ns,sma,semid))
665                 goto out_unlock;
666
667         err = -EACCES;
668         if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
669                 goto out_unlock;
670
671         err = security_sem_semctl(sma, cmd);
672         if (err)
673                 goto out_unlock;
674
675         err = -EACCES;
676         switch (cmd) {
677         case GETALL:
678         {
679                 ushort __user *array = arg.array;
680                 int i;
681
682                 if(nsems > SEMMSL_FAST) {
683                         ipc_rcu_getref(sma);
684                         sem_unlock(sma);                        
685
686                         sem_io = ipc_alloc(sizeof(ushort)*nsems);
687                         if(sem_io == NULL) {
688                                 ipc_lock_by_ptr(&sma->sem_perm);
689                                 ipc_rcu_putref(sma);
690                                 sem_unlock(sma);
691                                 return -ENOMEM;
692                         }
693
694                         ipc_lock_by_ptr(&sma->sem_perm);
695                         ipc_rcu_putref(sma);
696                         if (sma->sem_perm.deleted) {
697                                 sem_unlock(sma);
698                                 err = -EIDRM;
699                                 goto out_free;
700                         }
701                 }
702
703                 for (i = 0; i < sma->sem_nsems; i++)
704                         sem_io[i] = sma->sem_base[i].semval;
705                 sem_unlock(sma);
706                 err = 0;
707                 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
708                         err = -EFAULT;
709                 goto out_free;
710         }
711         case SETALL:
712         {
713                 int i;
714                 struct sem_undo *un;
715
716                 ipc_rcu_getref(sma);
717                 sem_unlock(sma);
718
719                 if(nsems > SEMMSL_FAST) {
720                         sem_io = ipc_alloc(sizeof(ushort)*nsems);
721                         if(sem_io == NULL) {
722                                 ipc_lock_by_ptr(&sma->sem_perm);
723                                 ipc_rcu_putref(sma);
724                                 sem_unlock(sma);
725                                 return -ENOMEM;
726                         }
727                 }
728
729                 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
730                         ipc_lock_by_ptr(&sma->sem_perm);
731                         ipc_rcu_putref(sma);
732                         sem_unlock(sma);
733                         err = -EFAULT;
734                         goto out_free;
735                 }
736
737                 for (i = 0; i < nsems; i++) {
738                         if (sem_io[i] > SEMVMX) {
739                                 ipc_lock_by_ptr(&sma->sem_perm);
740                                 ipc_rcu_putref(sma);
741                                 sem_unlock(sma);
742                                 err = -ERANGE;
743                                 goto out_free;
744                         }
745                 }
746                 ipc_lock_by_ptr(&sma->sem_perm);
747                 ipc_rcu_putref(sma);
748                 if (sma->sem_perm.deleted) {
749                         sem_unlock(sma);
750                         err = -EIDRM;
751                         goto out_free;
752                 }
753
754                 for (i = 0; i < nsems; i++)
755                         sma->sem_base[i].semval = sem_io[i];
756                 for (un = sma->undo; un; un = un->id_next)
757                         for (i = 0; i < nsems; i++)
758                                 un->semadj[i] = 0;
759                 sma->sem_ctime = get_seconds();
760                 /* maybe some queued-up processes were waiting for this */
761                 update_queue(sma);
762                 err = 0;
763                 goto out_unlock;
764         }
765         case IPC_STAT:
766         {
767                 struct semid64_ds tbuf;
768                 memset(&tbuf,0,sizeof(tbuf));
769                 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
770                 tbuf.sem_otime  = sma->sem_otime;
771                 tbuf.sem_ctime  = sma->sem_ctime;
772                 tbuf.sem_nsems  = sma->sem_nsems;
773                 sem_unlock(sma);
774                 if (copy_semid_to_user (arg.buf, &tbuf, version))
775                         return -EFAULT;
776                 return 0;
777         }
778         /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
779         }
780         err = -EINVAL;
781         if(semnum < 0 || semnum >= nsems)
782                 goto out_unlock;
783
784         curr = &sma->sem_base[semnum];
785
786         switch (cmd) {
787         case GETVAL:
788                 err = curr->semval;
789                 goto out_unlock;
790         case GETPID:
791                 err = curr->sempid;
792                 goto out_unlock;
793         case GETNCNT:
794                 err = count_semncnt(sma,semnum);
795                 goto out_unlock;
796         case GETZCNT:
797                 err = count_semzcnt(sma,semnum);
798                 goto out_unlock;
799         case SETVAL:
800         {
801                 int val = arg.val;
802                 struct sem_undo *un;
803                 err = -ERANGE;
804                 if (val > SEMVMX || val < 0)
805                         goto out_unlock;
806
807                 for (un = sma->undo; un; un = un->id_next)
808                         un->semadj[semnum] = 0;
809                 curr->semval = val;
810                 curr->sempid = current->tgid;
811                 sma->sem_ctime = get_seconds();
812                 /* maybe some queued-up processes were waiting for this */
813                 update_queue(sma);
814                 err = 0;
815                 goto out_unlock;
816         }
817         }
818 out_unlock:
819         sem_unlock(sma);
820 out_free:
821         if(sem_io != fast_sem_io)
822                 ipc_free(sem_io, sizeof(ushort)*nsems);
823         return err;
824 }
825
826 struct sem_setbuf {
827         uid_t   uid;
828         gid_t   gid;
829         mode_t  mode;
830 };
831
832 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
833 {
834         switch(version) {
835         case IPC_64:
836             {
837                 struct semid64_ds tbuf;
838
839                 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
840                         return -EFAULT;
841
842                 out->uid        = tbuf.sem_perm.uid;
843                 out->gid        = tbuf.sem_perm.gid;
844                 out->mode       = tbuf.sem_perm.mode;
845
846                 return 0;
847             }
848         case IPC_OLD:
849             {
850                 struct semid_ds tbuf_old;
851
852                 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
853                         return -EFAULT;
854
855                 out->uid        = tbuf_old.sem_perm.uid;
856                 out->gid        = tbuf_old.sem_perm.gid;
857                 out->mode       = tbuf_old.sem_perm.mode;
858
859                 return 0;
860             }
861         default:
862                 return -EINVAL;
863         }
864 }
865
866 static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
867                 int cmd, int version, union semun arg)
868 {
869         struct sem_array *sma;
870         int err;
871         struct sem_setbuf setbuf;
872         struct kern_ipc_perm *ipcp;
873
874         if(cmd == IPC_SET) {
875                 if(copy_semid_from_user (&setbuf, arg.buf, version))
876                         return -EFAULT;
877         }
878         sma = sem_lock(ns, semid);
879         if(sma==NULL)
880                 return -EINVAL;
881
882         if (sem_checkid(ns,sma,semid)) {
883                 err=-EIDRM;
884                 goto out_unlock;
885         }       
886         ipcp = &sma->sem_perm;
887
888         err = audit_ipc_obj(ipcp);
889         if (err)
890                 goto out_unlock;
891
892         if (cmd == IPC_SET) {
893                 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
894                 if (err)
895                         goto out_unlock;
896         }
897         if (current->euid != ipcp->cuid && 
898             current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
899                 err=-EPERM;
900                 goto out_unlock;
901         }
902
903         err = security_sem_semctl(sma, cmd);
904         if (err)
905                 goto out_unlock;
906
907         switch(cmd){
908         case IPC_RMID:
909                 freeary(ns, sma, semid);
910                 err = 0;
911                 break;
912         case IPC_SET:
913                 ipcp->uid = setbuf.uid;
914                 ipcp->gid = setbuf.gid;
915                 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
916                                 | (setbuf.mode & S_IRWXUGO);
917                 sma->sem_ctime = get_seconds();
918                 sem_unlock(sma);
919                 err = 0;
920                 break;
921         default:
922                 sem_unlock(sma);
923                 err = -EINVAL;
924                 break;
925         }
926         return err;
927
928 out_unlock:
929         sem_unlock(sma);
930         return err;
931 }
932
933 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
934 {
935         int err = -EINVAL;
936         int version;
937         struct ipc_namespace *ns;
938
939         if (semid < 0)
940                 return -EINVAL;
941
942         version = ipc_parse_version(&cmd);
943         ns = current->nsproxy->ipc_ns;
944
945         switch(cmd) {
946         case IPC_INFO:
947         case SEM_INFO:
948         case SEM_STAT:
949                 err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
950                 return err;
951         case GETALL:
952         case GETVAL:
953         case GETPID:
954         case GETNCNT:
955         case GETZCNT:
956         case IPC_STAT:
957         case SETVAL:
958         case SETALL:
959                 err = semctl_main(ns,semid,semnum,cmd,version,arg);
960                 return err;
961         case IPC_RMID:
962         case IPC_SET:
963                 mutex_lock(&sem_ids(ns).mutex);
964                 err = semctl_down(ns,semid,semnum,cmd,version,arg);
965                 mutex_unlock(&sem_ids(ns).mutex);
966                 return err;
967         default:
968                 return -EINVAL;
969         }
970 }
971
972 static inline void lock_semundo(void)
973 {
974         struct sem_undo_list *undo_list;
975
976         undo_list = current->sysvsem.undo_list;
977         if (undo_list)
978                 spin_lock(&undo_list->lock);
979 }
980
981 /* This code has an interaction with copy_semundo().
982  * Consider; two tasks are sharing the undo_list. task1
983  * acquires the undo_list lock in lock_semundo().  If task2 now
984  * exits before task1 releases the lock (by calling
985  * unlock_semundo()), then task1 will never call spin_unlock().
986  * This leave the sem_undo_list in a locked state.  If task1 now creats task3
987  * and once again shares the sem_undo_list, the sem_undo_list will still be
988  * locked, and future SEM_UNDO operations will deadlock.  This case is
989  * dealt with in copy_semundo() by having it reinitialize the spin lock when 
990  * the refcnt goes from 1 to 2.
991  */
992 static inline void unlock_semundo(void)
993 {
994         struct sem_undo_list *undo_list;
995
996         undo_list = current->sysvsem.undo_list;
997         if (undo_list)
998                 spin_unlock(&undo_list->lock);
999 }
1000
1001
1002 /* If the task doesn't already have a undo_list, then allocate one
1003  * here.  We guarantee there is only one thread using this undo list,
1004  * and current is THE ONE
1005  *
1006  * If this allocation and assignment succeeds, but later
1007  * portions of this code fail, there is no need to free the sem_undo_list.
1008  * Just let it stay associated with the task, and it'll be freed later
1009  * at exit time.
1010  *
1011  * This can block, so callers must hold no locks.
1012  */
1013 static inline int get_undo_list(struct sem_undo_list **undo_listp)
1014 {
1015         struct sem_undo_list *undo_list;
1016
1017         undo_list = current->sysvsem.undo_list;
1018         if (!undo_list) {
1019                 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
1020                 if (undo_list == NULL)
1021                         return -ENOMEM;
1022                 spin_lock_init(&undo_list->lock);
1023                 atomic_set(&undo_list->refcnt, 1);
1024                 current->sysvsem.undo_list = undo_list;
1025         }
1026         *undo_listp = undo_list;
1027         return 0;
1028 }
1029
1030 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1031 {
1032         struct sem_undo **last, *un;
1033
1034         last = &ulp->proc_list;
1035         un = *last;
1036         while(un != NULL) {
1037                 if(un->semid==semid)
1038                         break;
1039                 if(un->semid==-1) {
1040                         *last=un->proc_next;
1041                         kfree(un);
1042                 } else {
1043                         last=&un->proc_next;
1044                 }
1045                 un=*last;
1046         }
1047         return un;
1048 }
1049
1050 static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
1051 {
1052         struct sem_array *sma;
1053         struct sem_undo_list *ulp;
1054         struct sem_undo *un, *new;
1055         int nsems;
1056         int error;
1057
1058         error = get_undo_list(&ulp);
1059         if (error)
1060                 return ERR_PTR(error);
1061
1062         lock_semundo();
1063         un = lookup_undo(ulp, semid);
1064         unlock_semundo();
1065         if (likely(un!=NULL))
1066                 goto out;
1067
1068         /* no undo structure around - allocate one. */
1069         sma = sem_lock(ns, semid);
1070         un = ERR_PTR(-EINVAL);
1071         if(sma==NULL)
1072                 goto out;
1073         un = ERR_PTR(-EIDRM);
1074         if (sem_checkid(ns,sma,semid)) {
1075                 sem_unlock(sma);
1076                 goto out;
1077         }
1078         nsems = sma->sem_nsems;
1079         ipc_rcu_getref(sma);
1080         sem_unlock(sma);
1081
1082         new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1083         if (!new) {
1084                 ipc_lock_by_ptr(&sma->sem_perm);
1085                 ipc_rcu_putref(sma);
1086                 sem_unlock(sma);
1087                 return ERR_PTR(-ENOMEM);
1088         }
1089         new->semadj = (short *) &new[1];
1090         new->semid = semid;
1091
1092         lock_semundo();
1093         un = lookup_undo(ulp, semid);
1094         if (un) {
1095                 unlock_semundo();
1096                 kfree(new);
1097                 ipc_lock_by_ptr(&sma->sem_perm);
1098                 ipc_rcu_putref(sma);
1099                 sem_unlock(sma);
1100                 goto out;
1101         }
1102         ipc_lock_by_ptr(&sma->sem_perm);
1103         ipc_rcu_putref(sma);
1104         if (sma->sem_perm.deleted) {
1105                 sem_unlock(sma);
1106                 unlock_semundo();
1107                 kfree(new);
1108                 un = ERR_PTR(-EIDRM);
1109                 goto out;
1110         }
1111         new->proc_next = ulp->proc_list;
1112         ulp->proc_list = new;
1113         new->id_next = sma->undo;
1114         sma->undo = new;
1115         sem_unlock(sma);
1116         un = new;
1117         unlock_semundo();
1118 out:
1119         return un;
1120 }
1121
1122 asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1123                         unsigned nsops, const struct timespec __user *timeout)
1124 {
1125         int error = -EINVAL;
1126         struct sem_array *sma;
1127         struct sembuf fast_sops[SEMOPM_FAST];
1128         struct sembuf* sops = fast_sops, *sop;
1129         struct sem_undo *un;
1130         int undos = 0, alter = 0, max;
1131         struct sem_queue queue;
1132         unsigned long jiffies_left = 0;
1133         struct ipc_namespace *ns;
1134
1135         ns = current->nsproxy->ipc_ns;
1136
1137         if (nsops < 1 || semid < 0)
1138                 return -EINVAL;
1139         if (nsops > ns->sc_semopm)
1140                 return -E2BIG;
1141         if(nsops > SEMOPM_FAST) {
1142                 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1143                 if(sops==NULL)
1144                         return -ENOMEM;
1145         }
1146         if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1147                 error=-EFAULT;
1148                 goto out_free;
1149         }
1150         if (timeout) {
1151                 struct timespec _timeout;
1152                 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1153                         error = -EFAULT;
1154                         goto out_free;
1155                 }
1156                 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1157                         _timeout.tv_nsec >= 1000000000L) {
1158                         error = -EINVAL;
1159                         goto out_free;
1160                 }
1161                 jiffies_left = timespec_to_jiffies(&_timeout);
1162         }
1163         max = 0;
1164         for (sop = sops; sop < sops + nsops; sop++) {
1165                 if (sop->sem_num >= max)
1166                         max = sop->sem_num;
1167                 if (sop->sem_flg & SEM_UNDO)
1168                         undos = 1;
1169                 if (sop->sem_op != 0)
1170                         alter = 1;
1171         }
1172
1173 retry_undos:
1174         if (undos) {
1175                 un = find_undo(ns, semid);
1176                 if (IS_ERR(un)) {
1177                         error = PTR_ERR(un);
1178                         goto out_free;
1179                 }
1180         } else
1181                 un = NULL;
1182
1183         sma = sem_lock(ns, semid);
1184         error=-EINVAL;
1185         if(sma==NULL)
1186                 goto out_free;
1187         error = -EIDRM;
1188         if (sem_checkid(ns,sma,semid))
1189                 goto out_unlock_free;
1190         /*
1191          * semid identifies are not unique - find_undo may have
1192          * allocated an undo structure, it was invalidated by an RMID
1193          * and now a new array with received the same id. Check and retry.
1194          */
1195         if (un && un->semid == -1) {
1196                 sem_unlock(sma);
1197                 goto retry_undos;
1198         }
1199         error = -EFBIG;
1200         if (max >= sma->sem_nsems)
1201                 goto out_unlock_free;
1202
1203         error = -EACCES;
1204         if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1205                 goto out_unlock_free;
1206
1207         error = security_sem_semop(sma, sops, nsops, alter);
1208         if (error)
1209                 goto out_unlock_free;
1210
1211         error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
1212         if (error <= 0) {
1213                 if (alter && error == 0)
1214                         update_queue (sma);
1215                 goto out_unlock_free;
1216         }
1217
1218         /* We need to sleep on this operation, so we put the current
1219          * task into the pending queue and go to sleep.
1220          */
1221                 
1222         queue.sma = sma;
1223         queue.sops = sops;
1224         queue.nsops = nsops;
1225         queue.undo = un;
1226         queue.pid = current->tgid;
1227         queue.id = semid;
1228         queue.alter = alter;
1229         if (alter)
1230                 append_to_queue(sma ,&queue);
1231         else
1232                 prepend_to_queue(sma ,&queue);
1233
1234         queue.status = -EINTR;
1235         queue.sleeper = current;
1236         current->state = TASK_INTERRUPTIBLE;
1237         sem_unlock(sma);
1238
1239         if (timeout)
1240                 jiffies_left = schedule_timeout(jiffies_left);
1241         else
1242                 schedule();
1243
1244         error = queue.status;
1245         while(unlikely(error == IN_WAKEUP)) {
1246                 cpu_relax();
1247                 error = queue.status;
1248         }
1249
1250         if (error != -EINTR) {
1251                 /* fast path: update_queue already obtained all requested
1252                  * resources */
1253                 goto out_free;
1254         }
1255
1256         sma = sem_lock(ns, semid);
1257         if(sma==NULL) {
1258                 BUG_ON(queue.prev != NULL);
1259                 error = -EIDRM;
1260                 goto out_free;
1261         }
1262
1263         /*
1264          * If queue.status != -EINTR we are woken up by another process
1265          */
1266         error = queue.status;
1267         if (error != -EINTR) {
1268                 goto out_unlock_free;
1269         }
1270
1271         /*
1272          * If an interrupt occurred we have to clean up the queue
1273          */
1274         if (timeout && jiffies_left == 0)
1275                 error = -EAGAIN;
1276         remove_from_queue(sma,&queue);
1277         goto out_unlock_free;
1278
1279 out_unlock_free:
1280         sem_unlock(sma);
1281 out_free:
1282         if(sops != fast_sops)
1283                 kfree(sops);
1284         return error;
1285 }
1286
1287 asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1288 {
1289         return sys_semtimedop(semid, tsops, nsops, NULL);
1290 }
1291
1292 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1293  * parent and child tasks.
1294  *
1295  * See the notes above unlock_semundo() regarding the spin_lock_init()
1296  * in this code.  Initialize the undo_list->lock here instead of get_undo_list()
1297  * because of the reasoning in the comment above unlock_semundo.
1298  */
1299
1300 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1301 {
1302         struct sem_undo_list *undo_list;
1303         int error;
1304
1305         if (clone_flags & CLONE_SYSVSEM) {
1306                 error = get_undo_list(&undo_list);
1307                 if (error)
1308                         return error;
1309                 atomic_inc(&undo_list->refcnt);
1310                 tsk->sysvsem.undo_list = undo_list;
1311         } else 
1312                 tsk->sysvsem.undo_list = NULL;
1313
1314         return 0;
1315 }
1316
1317 /*
1318  * add semadj values to semaphores, free undo structures.
1319  * undo structures are not freed when semaphore arrays are destroyed
1320  * so some of them may be out of date.
1321  * IMPLEMENTATION NOTE: There is some confusion over whether the
1322  * set of adjustments that needs to be done should be done in an atomic
1323  * manner or not. That is, if we are attempting to decrement the semval
1324  * should we queue up and wait until we can do so legally?
1325  * The original implementation attempted to do this (queue and wait).
1326  * The current implementation does not do so. The POSIX standard
1327  * and SVID should be consulted to determine what behavior is mandated.
1328  */
1329 void exit_sem(struct task_struct *tsk)
1330 {
1331         struct sem_undo_list *undo_list;
1332         struct sem_undo *u, **up;
1333         struct ipc_namespace *ns;
1334
1335         undo_list = tsk->sysvsem.undo_list;
1336         if (!undo_list)
1337                 return;
1338
1339         if (!atomic_dec_and_test(&undo_list->refcnt))
1340                 return;
1341
1342         ns = tsk->nsproxy->ipc_ns;
1343         /* There's no need to hold the semundo list lock, as current
1344          * is the last task exiting for this undo list.
1345          */
1346         for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1347                 struct sem_array *sma;
1348                 int nsems, i;
1349                 struct sem_undo *un, **unp;
1350                 int semid;
1351                
1352                 semid = u->semid;
1353
1354                 if(semid == -1)
1355                         continue;
1356                 sma = sem_lock(ns, semid);
1357                 if (sma == NULL)
1358                         continue;
1359
1360                 if (u->semid == -1)
1361                         goto next_entry;
1362
1363                 BUG_ON(sem_checkid(ns,sma,u->semid));
1364
1365                 /* remove u from the sma->undo list */
1366                 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1367                         if (u == un)
1368                                 goto found;
1369                 }
1370                 printk ("exit_sem undo list error id=%d\n", u->semid);
1371                 goto next_entry;
1372 found:
1373                 *unp = un->id_next;
1374                 /* perform adjustments registered in u */
1375                 nsems = sma->sem_nsems;
1376                 for (i = 0; i < nsems; i++) {
1377                         struct sem * semaphore = &sma->sem_base[i];
1378                         if (u->semadj[i]) {
1379                                 semaphore->semval += u->semadj[i];
1380                                 /*
1381                                  * Range checks of the new semaphore value,
1382                                  * not defined by sus:
1383                                  * - Some unices ignore the undo entirely
1384                                  *   (e.g. HP UX 11i 11.22, Tru64 V5.1)
1385                                  * - some cap the value (e.g. FreeBSD caps
1386                                  *   at 0, but doesn't enforce SEMVMX)
1387                                  *
1388                                  * Linux caps the semaphore value, both at 0
1389                                  * and at SEMVMX.
1390                                  *
1391                                  *      Manfred <manfred@colorfullife.com>
1392                                  */
1393                                 if (semaphore->semval < 0)
1394                                         semaphore->semval = 0;
1395                                 if (semaphore->semval > SEMVMX)
1396                                         semaphore->semval = SEMVMX;
1397                                 semaphore->sempid = current->tgid;
1398                         }
1399                 }
1400                 sma->sem_otime = get_seconds();
1401                 /* maybe some queued-up processes were waiting for this */
1402                 update_queue(sma);
1403 next_entry:
1404                 sem_unlock(sma);
1405         }
1406         kfree(undo_list);
1407 }
1408
1409 #ifdef CONFIG_PROC_FS
1410 static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1411 {
1412         struct sem_array *sma = it;
1413
1414         return seq_printf(s,
1415                           "%10d %10d  %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1416                           sma->sem_perm.key,
1417                           sma->sem_id,
1418                           sma->sem_perm.mode,
1419                           sma->sem_nsems,
1420                           sma->sem_perm.uid,
1421                           sma->sem_perm.gid,
1422                           sma->sem_perm.cuid,
1423                           sma->sem_perm.cgid,
1424                           sma->sem_otime,
1425                           sma->sem_ctime);
1426 }
1427 #endif