blob: f68b61749a85eda29a033126ae3efa98cf8916a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/sem.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
7 *
8 * SMP-threaded, sysctl's added
Christian Kujau624dffc2006-01-15 02:43:54 +01009 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Enforced range limit on SEM_UNDO
Alan Cox046c6882009-01-05 14:06:29 +000011 * (c) 2001 Red Hat Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Lockless wakeup
13 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070014 * Further wakeup optimizations, documentation
15 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040016 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaeve3893532006-10-02 02:18:22 -070019 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070023 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/security.h>
82#include <linux/syscalls.h>
83#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080084#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070085#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070086#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070087#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080088#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <asm/uaccess.h>
91#include "util.h"
92
Manfred Spraule57940d2011-11-02 13:38:54 -070093/* One semaphore structure for each semaphore in the system. */
94struct sem {
95 int semval; /* current value */
96 int sempid; /* pid of last operation */
97 struct list_head sem_pending; /* pending single-sop operations */
98};
99
100/* One queue for each sleeping process in the system. */
101struct sem_queue {
Manfred Spraule57940d2011-11-02 13:38:54 -0700102 struct list_head list; /* queue of pending operations */
103 struct task_struct *sleeper; /* this process */
104 struct sem_undo *undo; /* undo structure */
105 int pid; /* process id of requesting process */
106 int status; /* completion status of operation */
107 struct sembuf *sops; /* array of pending operations */
108 int nsops; /* number of operations */
109 int alter; /* does *sops alter the array? */
110};
111
112/* Each task has a list of undo requests. They are executed automatically
113 * when the process exits.
114 */
115struct sem_undo {
116 struct list_head list_proc; /* per-process list: *
117 * all undos from one process
118 * rcu protected */
119 struct rcu_head rcu; /* rcu struct for sem_undo */
120 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
121 struct list_head list_id; /* per semaphore array list:
122 * all undos for one array */
123 int semid; /* semaphore set identifier */
124 short *semadj; /* array of adjustments */
125 /* one per semaphore */
126};
127
128/* sem_undo_list controls shared access to the list of sem_undo structures
129 * that may be shared among all a CLONE_SYSVSEM task group.
130 */
131struct sem_undo_list {
132 atomic_t refcnt;
133 spinlock_t lock;
134 struct list_head list_proc;
135};
136
137
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800138#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Kirill Korotaeve3893532006-10-02 02:18:22 -0700140#define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
Nadia Derbey1b531f22007-10-18 23:40:55 -0700141#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700143static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800144static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700146static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#endif
148
149#define SEMMSL_FAST 256 /* 512 bytes on stack */
150#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
151
152/*
153 * linked list protection:
154 * sem_undo.id_next,
155 * sem_array.sem_pending{,last},
156 * sem_array.sem_undo: sem_lock() for read/write
157 * sem_undo.proc_next: only "current" is allowed to read/write that field.
158 *
159 */
160
Kirill Korotaeve3893532006-10-02 02:18:22 -0700161#define sc_semmsl sem_ctls[0]
162#define sc_semmns sem_ctls[1]
163#define sc_semopm sem_ctls[2]
164#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800166void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700167{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700168 ns->sc_semmsl = SEMMSL;
169 ns->sc_semmns = SEMMNS;
170 ns->sc_semopm = SEMOPM;
171 ns->sc_semmni = SEMMNI;
172 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800173 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700174}
175
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800176#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700177void sem_exit_ns(struct ipc_namespace *ns)
178{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800179 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800180 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700181}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800182#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184void __init sem_init (void)
185{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800186 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700187 ipc_init_proc_interface("sysvipc/sem",
188 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700189 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Nadia Derbey3e148c72007-10-18 23:40:54 -0700192/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700193 * sem_lock_(check_) routines are called in the paths where the rw_mutex
194 * is not held.
195 */
Rik van Rielc460b662013-04-30 19:15:35 -0700196static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns, int id)
Nadia Derbey023a5352007-10-18 23:40:51 -0700197{
Rik van Rielc460b662013-04-30 19:15:35 -0700198 struct kern_ipc_perm *ipcp;
199 struct sem_array *sma;
Nadia Derbey03f02c72007-10-18 23:40:51 -0700200
Rik van Rielc460b662013-04-30 19:15:35 -0700201 rcu_read_lock();
202 ipcp = ipc_obtain_object(&sem_ids(ns), id);
203 if (IS_ERR(ipcp)) {
204 sma = ERR_CAST(ipcp);
205 goto err;
206 }
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800207
Rik van Rielc460b662013-04-30 19:15:35 -0700208 spin_lock(&ipcp->lock);
209
210 /* ipc_rmid() may have already freed the ID while sem_lock
211 * was spinning: verify that the structure is still valid
212 */
213 if (!ipcp->deleted)
214 return container_of(ipcp, struct sem_array, sem_perm);
215
216 spin_unlock(&ipcp->lock);
217 sma = ERR_PTR(-EINVAL);
218err:
219 rcu_read_unlock();
220 return sma;
Nadia Derbey023a5352007-10-18 23:40:51 -0700221}
222
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700223static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
224{
225 struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
226
227 if (IS_ERR(ipcp))
228 return ERR_CAST(ipcp);
229
230 return container_of(ipcp, struct sem_array, sem_perm);
231}
232
Nadia Derbey023a5352007-10-18 23:40:51 -0700233static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
234 int id)
235{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700236 struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
237
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800238 if (IS_ERR(ipcp))
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700239 return ERR_CAST(ipcp);
240
241 return container_of(ipcp, struct sem_array, sem_perm);
242}
243
244static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
245 int id)
246{
247 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
248
249 if (IS_ERR(ipcp))
250 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800251
Nadia Derbey03f02c72007-10-18 23:40:51 -0700252 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700253}
254
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700255static inline void sem_lock_and_putref(struct sem_array *sma)
256{
257 ipc_lock_by_ptr(&sma->sem_perm);
258 ipc_rcu_putref(sma);
259}
260
261static inline void sem_getref_and_unlock(struct sem_array *sma)
262{
263 ipc_rcu_getref(sma);
264 ipc_unlock(&(sma)->sem_perm);
265}
266
267static inline void sem_putref(struct sem_array *sma)
268{
269 ipc_lock_by_ptr(&sma->sem_perm);
270 ipc_rcu_putref(sma);
271 ipc_unlock(&(sma)->sem_perm);
272}
273
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700274/*
275 * Call inside the rcu read section.
276 */
277static inline void sem_getref(struct sem_array *sma)
278{
279 spin_lock(&(sma)->sem_perm.lock);
280 ipc_rcu_getref(sma);
281 ipc_unlock(&(sma)->sem_perm);
282}
283
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700284static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
285{
286 ipc_rmid(&sem_ids(ns), &s->sem_perm);
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*
290 * Lockless wakeup algorithm:
291 * Without the check/retry algorithm a lockless wakeup is possible:
292 * - queue.status is initialized to -EINTR before blocking.
293 * - wakeup is performed by
294 * * unlinking the queue entry from sma->sem_pending
295 * * setting queue.status to IN_WAKEUP
296 * This is the notification for the blocked thread that a
297 * result value is imminent.
298 * * call wake_up_process
299 * * set queue.status to the final value.
300 * - the previously blocked thread checks queue.status:
301 * * if it's IN_WAKEUP, then it must wait until the value changes
302 * * if it's not -EINTR, then the operation was completed by
303 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800304 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * * otherwise it must acquire the spinlock and check what's up.
306 *
307 * The two-stage algorithm is necessary to protect against the following
308 * races:
309 * - if queue.status is set after wake_up_process, then the woken up idle
310 * thread could race forward and try (and fail) to acquire sma->lock
311 * before update_queue had a chance to set queue.status
312 * - if queue.status is written before wake_up_process and if the
313 * blocked process is woken up by a signal between writing
314 * queue.status and the wake_up_process, then the woken up
315 * process could return from semtimedop and die by calling
316 * sys_exit before wake_up_process is called. Then wake_up_process
317 * will oops, because the task structure is already invalid.
318 * (yes, this happened on s390 with sysv msg).
319 *
320 */
321#define IN_WAKEUP 1
322
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700323/**
324 * newary - Create a new semaphore set
325 * @ns: namespace
326 * @params: ptr to the structure that contains key, semflg and nsems
327 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700328 * Called with sem_ids.rw_mutex held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700329 */
330
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700331static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 int id;
334 int retval;
335 struct sem_array *sma;
336 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700337 key_t key = params->key;
338 int nsems = params->u.nsems;
339 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800340 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (!nsems)
343 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700344 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -ENOSPC;
346
347 size = sizeof (*sma) + nsems * sizeof (struct sem);
348 sma = ipc_rcu_alloc(size);
349 if (!sma) {
350 return -ENOMEM;
351 }
352 memset (sma, 0, size);
353
354 sma->sem_perm.mode = (semflg & S_IRWXUGO);
355 sma->sem_perm.key = key;
356
357 sma->sem_perm.security = NULL;
358 retval = security_sem_alloc(sma);
359 if (retval) {
360 ipc_rcu_putref(sma);
361 return retval;
362 }
363
Kirill Korotaeve3893532006-10-02 02:18:22 -0700364 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700365 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 security_sem_free(sma);
367 ipc_rcu_putref(sma);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700368 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700370 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800373
374 for (i = 0; i < nsems; i++)
375 INIT_LIST_HEAD(&sma->sem_base[i].sem_pending);
376
377 sma->complex_count = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700378 INIT_LIST_HEAD(&sma->sem_pending);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700379 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 sma->sem_nsems = nsems;
381 sma->sem_ctime = get_seconds();
382 sem_unlock(sma);
383
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700384 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700387
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700388/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700389 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700390 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700391static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700393 struct sem_array *sma;
394
395 sma = container_of(ipcp, struct sem_array, sem_perm);
396 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700397}
398
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700399/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700400 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700401 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700402static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
403 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700404{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700405 struct sem_array *sma;
406
407 sma = container_of(ipcp, struct sem_array, sem_perm);
408 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700409 return -EINVAL;
410
411 return 0;
412}
413
Heiko Carstensd5460c92009-01-14 14:14:27 +0100414SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700415{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700416 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700417 struct ipc_ops sem_ops;
418 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Kirill Korotaeve3893532006-10-02 02:18:22 -0700420 ns = current->nsproxy->ipc_ns;
421
422 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700424
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700425 sem_ops.getnew = newary;
426 sem_ops.associate = sem_security;
427 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700428
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700429 sem_params.key = key;
430 sem_params.flg = semflg;
431 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700432
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700433 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/*
437 * Determine whether a sequence of semaphore operations would succeed
438 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
439 */
440
441static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
442 int nsops, struct sem_undo *un, int pid)
443{
444 int result, sem_op;
445 struct sembuf *sop;
446 struct sem * curr;
447
448 for (sop = sops; sop < sops + nsops; sop++) {
449 curr = sma->sem_base + sop->sem_num;
450 sem_op = sop->sem_op;
451 result = curr->semval;
452
453 if (!sem_op && result)
454 goto would_block;
455
456 result += sem_op;
457 if (result < 0)
458 goto would_block;
459 if (result > SEMVMX)
460 goto out_of_range;
461 if (sop->sem_flg & SEM_UNDO) {
462 int undo = un->semadj[sop->sem_num] - sem_op;
463 /*
464 * Exceeding the undo range is an error.
465 */
466 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
467 goto out_of_range;
468 }
469 curr->semval = result;
470 }
471
472 sop--;
473 while (sop >= sops) {
474 sma->sem_base[sop->sem_num].sempid = pid;
475 if (sop->sem_flg & SEM_UNDO)
476 un->semadj[sop->sem_num] -= sop->sem_op;
477 sop--;
478 }
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
481
482out_of_range:
483 result = -ERANGE;
484 goto undo;
485
486would_block:
487 if (sop->sem_flg & IPC_NOWAIT)
488 result = -EAGAIN;
489 else
490 result = 1;
491
492undo:
493 sop--;
494 while (sop >= sops) {
495 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
496 sop--;
497 }
498
499 return result;
500}
501
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700502/** wake_up_sem_queue_prepare(q, error): Prepare wake-up
503 * @q: queue entry that must be signaled
504 * @error: Error value for the signal
505 *
506 * Prepare the wake-up of the queue entry q.
Nick Piggind4212092009-12-15 16:47:30 -0800507 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700508static void wake_up_sem_queue_prepare(struct list_head *pt,
509 struct sem_queue *q, int error)
Nick Piggind4212092009-12-15 16:47:30 -0800510{
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700511 if (list_empty(pt)) {
512 /*
513 * Hold preempt off so that we don't get preempted and have the
514 * wakee busy-wait until we're scheduled back on.
515 */
516 preempt_disable();
517 }
Nick Piggind4212092009-12-15 16:47:30 -0800518 q->status = IN_WAKEUP;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700519 q->pid = error;
520
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700521 list_add_tail(&q->list, pt);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700522}
523
524/**
525 * wake_up_sem_queue_do(pt) - do the actual wake-up
526 * @pt: list of tasks to be woken up
527 *
528 * Do the actual wake-up.
529 * The function is called without any locks held, thus the semaphore array
530 * could be destroyed already and the tasks can disappear as soon as the
531 * status is set to the actual return code.
532 */
533static void wake_up_sem_queue_do(struct list_head *pt)
534{
535 struct sem_queue *q, *t;
536 int did_something;
537
538 did_something = !list_empty(pt);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700539 list_for_each_entry_safe(q, t, pt, list) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700540 wake_up_process(q->sleeper);
541 /* q can disappear immediately after writing q->status. */
542 smp_wmb();
543 q->status = q->pid;
544 }
545 if (did_something)
546 preempt_enable();
Nick Piggind4212092009-12-15 16:47:30 -0800547}
548
Manfred Spraulb97e8202009-12-15 16:47:32 -0800549static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
550{
551 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700552 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800553 sma->complex_count--;
554}
555
Manfred Spraulfd5db422010-05-26 14:43:40 -0700556/** check_restart(sma, q)
557 * @sma: semaphore array
558 * @q: the operation that just completed
559 *
560 * update_queue is O(N^2) when it restarts scanning the whole queue of
561 * waiting operations. Therefore this function checks if the restart is
562 * really necessary. It is called after a previously waiting operation
563 * was completed.
564 */
565static int check_restart(struct sem_array *sma, struct sem_queue *q)
566{
567 struct sem *curr;
568 struct sem_queue *h;
569
570 /* if the operation didn't modify the array, then no restart */
571 if (q->alter == 0)
572 return 0;
573
574 /* pending complex operations are too difficult to analyse */
575 if (sma->complex_count)
576 return 1;
577
578 /* we were a sleeping complex operation. Too difficult */
579 if (q->nsops > 1)
580 return 1;
581
582 curr = sma->sem_base + q->sops[0].sem_num;
583
584 /* No-one waits on this queue */
585 if (list_empty(&curr->sem_pending))
586 return 0;
587
588 /* the new semaphore value */
589 if (curr->semval) {
590 /* It is impossible that someone waits for the new value:
591 * - q is a previously sleeping simple operation that
592 * altered the array. It must be a decrement, because
593 * simple increments never sleep.
594 * - The value is not 0, thus wait-for-zero won't proceed.
595 * - If there are older (higher priority) decrements
596 * in the queue, then they have observed the original
597 * semval value and couldn't proceed. The operation
598 * decremented to value - thus they won't proceed either.
599 */
600 BUG_ON(q->sops[0].sem_op >= 0);
601 return 0;
602 }
603 /*
604 * semval is 0. Check if there are wait-for-zero semops.
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700605 * They must be the first entries in the per-semaphore queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700606 */
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700607 h = list_first_entry(&curr->sem_pending, struct sem_queue, list);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700608 BUG_ON(h->nsops != 1);
609 BUG_ON(h->sops[0].sem_num != q->sops[0].sem_num);
610
611 /* Yes, there is a wait-for-zero semop. Restart */
612 if (h->sops[0].sem_op == 0)
613 return 1;
614
615 /* Again - no-one is waiting for the new value. */
616 return 0;
617}
618
Manfred Spraul636c6be2009-12-15 16:47:33 -0800619
620/**
621 * update_queue(sma, semnum): Look for tasks that can be completed.
622 * @sma: semaphore array.
623 * @semnum: semaphore that was modified.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700624 * @pt: list head for the tasks that must be woken up.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800625 *
626 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700627 * was modified. If multiple semaphores were modified, update_queue must
628 * be called with semnum = -1, as well as with the number of each modified
629 * semaphore.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700630 * The tasks that must be woken up are added to @pt. The return code
631 * is stored in q->pid.
632 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700634static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Manfred Spraul636c6be2009-12-15 16:47:33 -0800636 struct sem_queue *q;
637 struct list_head *walk;
638 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700639 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800640
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700641 if (semnum == -1)
Manfred Spraul636c6be2009-12-15 16:47:33 -0800642 pending_list = &sma->sem_pending;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700643 else
Manfred Spraul636c6be2009-12-15 16:47:33 -0800644 pending_list = &sma->sem_base[semnum].sem_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Nick Piggin9cad2002009-12-15 16:47:29 -0800646again:
Manfred Spraul636c6be2009-12-15 16:47:33 -0800647 walk = pending_list->next;
648 while (walk != pending_list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700649 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800650
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700651 q = container_of(walk, struct sem_queue, list);
Manfred Spraul636c6be2009-12-15 16:47:33 -0800652 walk = walk->next;
Nick Piggin9cad2002009-12-15 16:47:29 -0800653
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800654 /* If we are scanning the single sop, per-semaphore list of
655 * one semaphore and that semaphore is 0, then it is not
656 * necessary to scan the "alter" entries: simple increments
657 * that affect only one entry succeed immediately and cannot
658 * be in the per semaphore pending queue, and decrements
659 * cannot be successful if the value is already 0.
660 */
661 if (semnum != -1 && sma->sem_base[semnum].semval == 0 &&
662 q->alter)
663 break;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 error = try_atomic_semop(sma, q->sops, q->nsops,
666 q->undo, q->pid);
667
668 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800669 if (error > 0)
670 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700671
Manfred Spraulb97e8202009-12-15 16:47:32 -0800672 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700673
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700674 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700675 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700676 } else {
677 semop_completed = 1;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700678 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700679 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700680
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700681 wake_up_sem_queue_prepare(pt, q, error);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700682 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800683 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700685 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700688/**
689 * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700690 * @sma: semaphore array
691 * @sops: operations that were performed
692 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700693 * @otime: force setting otime
694 * @pt: list head of the tasks that must be woken up.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700695 *
696 * do_smart_update() does the required called to update_queue, based on the
697 * actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700698 * Note that the function does not do the actual wake-up: the caller is
699 * responsible for calling wake_up_sem_queue_do(@pt).
700 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700701 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700702static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
703 int otime, struct list_head *pt)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700704{
705 int i;
706
707 if (sma->complex_count || sops == NULL) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700708 if (update_queue(sma, -1, pt))
709 otime = 1;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700710 }
711
712 if (!sops) {
713 /* No semops; something special is going on. */
714 for (i = 0; i < sma->sem_nsems; i++) {
715 if (update_queue(sma, i, pt))
716 otime = 1;
717 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700718 goto done;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700719 }
720
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700721 /* Check the semaphores that were modified. */
Manfred Spraulfd5db422010-05-26 14:43:40 -0700722 for (i = 0; i < nsops; i++) {
723 if (sops[i].sem_op > 0 ||
724 (sops[i].sem_op < 0 &&
725 sma->sem_base[sops[i].sem_num].semval == 0))
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700726 if (update_queue(sma, sops[i].sem_num, pt))
727 otime = 1;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700728 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700729done:
730 if (otime)
731 sma->sem_otime = get_seconds();
Manfred Spraulfd5db422010-05-26 14:43:40 -0700732}
733
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/* The following counts are associated to each semaphore:
736 * semncnt number of tasks waiting on semval being nonzero
737 * semzcnt number of tasks waiting on semval being zero
738 * This model assumes that a task waits on exactly one semaphore.
739 * Since semaphore operations are to be performed atomically, tasks actually
740 * wait on a whole sequence of semaphores simultaneously.
741 * The counts we return here are a rough approximation, but still
742 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
743 */
744static int count_semncnt (struct sem_array * sma, ushort semnum)
745{
746 int semncnt;
747 struct sem_queue * q;
748
749 semncnt = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700750 list_for_each_entry(q, &sma->sem_pending, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct sembuf * sops = q->sops;
752 int nsops = q->nsops;
753 int i;
754 for (i = 0; i < nsops; i++)
755 if (sops[i].sem_num == semnum
756 && (sops[i].sem_op < 0)
757 && !(sops[i].sem_flg & IPC_NOWAIT))
758 semncnt++;
759 }
760 return semncnt;
761}
Manfred Spraula1193f82008-07-25 01:48:06 -0700762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763static int count_semzcnt (struct sem_array * sma, ushort semnum)
764{
765 int semzcnt;
766 struct sem_queue * q;
767
768 semzcnt = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700769 list_for_each_entry(q, &sma->sem_pending, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 struct sembuf * sops = q->sops;
771 int nsops = q->nsops;
772 int i;
773 for (i = 0; i < nsops; i++)
774 if (sops[i].sem_num == semnum
775 && (sops[i].sem_op == 0)
776 && !(sops[i].sem_flg & IPC_NOWAIT))
777 semzcnt++;
778 }
779 return semzcnt;
780}
781
Nadia Derbey3e148c72007-10-18 23:40:54 -0700782/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
783 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
784 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800786static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Manfred Spraul380af1b2008-07-25 01:48:06 -0700788 struct sem_undo *un, *tu;
789 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800790 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700791 struct list_head tasks;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700792 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Manfred Spraul380af1b2008-07-25 01:48:06 -0700794 /* Free the existing undo structures for this semaphore set. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700795 assert_spin_locked(&sma->sem_perm.lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700796 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
797 list_del(&un->list_id);
798 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -0700800 list_del_rcu(&un->list_proc);
801 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +0800802 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700806 INIT_LIST_HEAD(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700807 list_for_each_entry_safe(q, tq, &sma->sem_pending, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -0800808 unlink_queue(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700809 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700811 for (i = 0; i < sma->sem_nsems; i++) {
812 struct sem *sem = sma->sem_base + i;
813 list_for_each_entry_safe(q, tq, &sem->sem_pending, list) {
814 unlink_queue(sma, q);
815 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
816 }
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700819 /* Remove the semaphore set from the IDR */
820 sem_rmid(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 sem_unlock(sma);
822
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700823 wake_up_sem_queue_do(&tasks);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700824 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 security_sem_free(sma);
826 ipc_rcu_putref(sma);
827}
828
829static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
830{
831 switch(version) {
832 case IPC_64:
833 return copy_to_user(buf, in, sizeof(*in));
834 case IPC_OLD:
835 {
836 struct semid_ds out;
837
Dan Rosenberg982f7c22010-09-30 15:15:31 -0700838 memset(&out, 0, sizeof(out));
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
841
842 out.sem_otime = in->sem_otime;
843 out.sem_ctime = in->sem_ctime;
844 out.sem_nsems = in->sem_nsems;
845
846 return copy_to_user(buf, &out, sizeof(out));
847 }
848 default:
849 return -EINVAL;
850 }
851}
852
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800853static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -0500854 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Amerigo Wange5cc9c72009-12-15 16:47:35 -0800856 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 struct sem_array *sma;
858
859 switch(cmd) {
860 case IPC_INFO:
861 case SEM_INFO:
862 {
863 struct seminfo seminfo;
864 int max_id;
865
866 err = security_sem_semctl(NULL, cmd);
867 if (err)
868 return err;
869
870 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700871 seminfo.semmni = ns->sc_semmni;
872 seminfo.semmns = ns->sc_semmns;
873 seminfo.semmsl = ns->sc_semmsl;
874 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 seminfo.semvmx = SEMVMX;
876 seminfo.semmnu = SEMMNU;
877 seminfo.semmap = SEMMAP;
878 seminfo.semume = SEMUME;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700879 down_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700881 seminfo.semusz = sem_ids(ns).in_use;
882 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 } else {
884 seminfo.semusz = SEMUSZ;
885 seminfo.semaem = SEMAEM;
886 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700887 max_id = ipc_get_maxid(&sem_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700888 up_read(&sem_ids(ns).rw_mutex);
Al Viroe1fd1f42013-03-05 15:04:55 -0500889 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -EFAULT;
891 return (max_id < 0) ? 0: max_id;
892 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800893 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 case SEM_STAT:
895 {
896 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700897 int id = 0;
898
899 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800901 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700902 rcu_read_lock();
903 sma = sem_obtain_object(ns, semid);
904 if (IS_ERR(sma)) {
905 err = PTR_ERR(sma);
906 goto out_unlock;
907 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800908 id = sma->sem_perm.id;
909 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700910 rcu_read_lock();
911 sma = sem_obtain_object_check(ns, semid);
912 if (IS_ERR(sma)) {
913 err = PTR_ERR(sma);
914 goto out_unlock;
915 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700919 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto out_unlock;
921
922 err = security_sem_semctl(sma, cmd);
923 if (err)
924 goto out_unlock;
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
927 tbuf.sem_otime = sma->sem_otime;
928 tbuf.sem_ctime = sma->sem_ctime;
929 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700930 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -0500931 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return -EFAULT;
933 return id;
934 }
935 default:
936 return -EINVAL;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700939 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return err;
941}
942
Al Viroe1fd1f42013-03-05 15:04:55 -0500943static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
944 unsigned long arg)
945{
946 struct sem_undo *un;
947 struct sem_array *sma;
948 struct sem* curr;
949 int err;
950 int nsems;
951 struct list_head tasks;
952 int val;
953#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
954 /* big-endian 64bit */
955 val = arg >> 32;
956#else
957 /* 32bit or little-endian 64bit */
958 val = arg;
959#endif
960
961 sma = sem_lock_check(ns, semid);
962 if (IS_ERR(sma))
963 return PTR_ERR(sma);
964
965 INIT_LIST_HEAD(&tasks);
966 nsems = sma->sem_nsems;
967
968 err = -EACCES;
969 if (ipcperms(ns, &sma->sem_perm, S_IWUGO))
970 goto out_unlock;
971
972 err = security_sem_semctl(sma, SETVAL);
973 if (err)
974 goto out_unlock;
975
976 err = -EINVAL;
977 if(semnum < 0 || semnum >= nsems)
978 goto out_unlock;
979
980 curr = &sma->sem_base[semnum];
981
982 err = -ERANGE;
983 if (val > SEMVMX || val < 0)
984 goto out_unlock;
985
986 assert_spin_locked(&sma->sem_perm.lock);
987 list_for_each_entry(un, &sma->list_id, list_id)
988 un->semadj[semnum] = 0;
989
990 curr->semval = val;
991 curr->sempid = task_tgid_vnr(current);
992 sma->sem_ctime = get_seconds();
993 /* maybe some queued-up processes were waiting for this */
994 do_smart_update(sma, NULL, 0, 0, &tasks);
995 err = 0;
996out_unlock:
997 sem_unlock(sma);
998 wake_up_sem_queue_do(&tasks);
999 return err;
1000}
1001
Kirill Korotaeve3893532006-10-02 02:18:22 -07001002static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001003 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct sem_array *sma;
1006 struct sem* curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001007 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ushort fast_sem_io[SEMMSL_FAST];
1009 ushort* sem_io = fast_sem_io;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001010 struct list_head tasks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001012 INIT_LIST_HEAD(&tasks);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001013
1014 rcu_read_lock();
1015 sma = sem_obtain_object_check(ns, semid);
1016 if (IS_ERR(sma)) {
1017 rcu_read_unlock();
1018 return PTR_ERR(sma);
1019 }
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 nsems = sma->sem_nsems;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001024 if (ipcperms(ns, &sma->sem_perm,
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001025 cmd == SETALL ? S_IWUGO : S_IRUGO)) {
1026 rcu_read_unlock();
1027 goto out_wakeup;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001031 if (err) {
1032 rcu_read_unlock();
1033 goto out_wakeup;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 err = -EACCES;
1037 switch (cmd) {
1038 case GETALL:
1039 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001040 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 int i;
1042
1043 if(nsems > SEMMSL_FAST) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001044 sem_getref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1047 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001048 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -ENOMEM;
1050 }
1051
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001052 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (sma->sem_perm.deleted) {
1054 sem_unlock(sma);
1055 err = -EIDRM;
1056 goto out_free;
1057 }
1058 }
1059
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001060 spin_lock(&sma->sem_perm.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 for (i = 0; i < sma->sem_nsems; i++)
1062 sem_io[i] = sma->sem_base[i].semval;
1063 sem_unlock(sma);
1064 err = 0;
1065 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1066 err = -EFAULT;
1067 goto out_free;
1068 }
1069 case SETALL:
1070 {
1071 int i;
1072 struct sem_undo *un;
1073
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001074 ipc_rcu_getref(sma);
1075 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if(nsems > SEMMSL_FAST) {
1078 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1079 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001080 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return -ENOMEM;
1082 }
1083 }
1084
Al Viroe1fd1f42013-03-05 15:04:55 -05001085 if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001086 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 err = -EFAULT;
1088 goto out_free;
1089 }
1090
1091 for (i = 0; i < nsems; i++) {
1092 if (sem_io[i] > SEMVMX) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001093 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 err = -ERANGE;
1095 goto out_free;
1096 }
1097 }
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001098 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (sma->sem_perm.deleted) {
1100 sem_unlock(sma);
1101 err = -EIDRM;
1102 goto out_free;
1103 }
1104
1105 for (i = 0; i < nsems; i++)
1106 sma->sem_base[i].semval = sem_io[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001107
1108 assert_spin_locked(&sma->sem_perm.lock);
1109 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 for (i = 0; i < nsems; i++)
1111 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 sma->sem_ctime = get_seconds();
1114 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001115 do_smart_update(sma, NULL, 0, 0, &tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 err = 0;
1117 goto out_unlock;
1118 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001119 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121 err = -EINVAL;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001122 if (semnum < 0 || semnum >= nsems) {
1123 rcu_read_unlock();
1124 goto out_wakeup;
1125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001127 spin_lock(&sma->sem_perm.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 curr = &sma->sem_base[semnum];
1129
1130 switch (cmd) {
1131 case GETVAL:
1132 err = curr->semval;
1133 goto out_unlock;
1134 case GETPID:
1135 err = curr->sempid;
1136 goto out_unlock;
1137 case GETNCNT:
1138 err = count_semncnt(sma,semnum);
1139 goto out_unlock;
1140 case GETZCNT:
1141 err = count_semzcnt(sma,semnum);
1142 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145out_unlock:
1146 sem_unlock(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001147out_wakeup:
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001148 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149out_free:
1150 if(sem_io != fast_sem_io)
1151 ipc_free(sem_io, sizeof(ushort)*nsems);
1152 return err;
1153}
1154
Pierre Peiffer016d7132008-04-29 01:00:50 -07001155static inline unsigned long
1156copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 switch(version) {
1159 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001160 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 case IPC_OLD:
1164 {
1165 struct semid_ds tbuf_old;
1166
1167 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1168 return -EFAULT;
1169
Pierre Peiffer016d7132008-04-29 01:00:50 -07001170 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1171 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1172 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 return 0;
1175 }
1176 default:
1177 return -EINVAL;
1178 }
1179}
1180
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001181/*
1182 * This function handles some semctl commands which require the rw_mutex
1183 * to be held in write mode.
1184 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
1185 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001186static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001187 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 struct sem_array *sma;
1190 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001191 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 struct kern_ipc_perm *ipcp;
1193
1194 if(cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001195 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001199 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1200 &semid64.sem_perm, 0);
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001201 if (IS_ERR(ipcp))
1202 return PTR_ERR(ipcp);
Steve Grubb073115d2006-04-02 17:07:33 -04001203
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001204 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001207 if (err) {
1208 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 goto out_unlock;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 switch(cmd){
1213 case IPC_RMID:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001214 ipc_lock_object(&sma->sem_perm);
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001215 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001216 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 case IPC_SET:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001218 ipc_lock_object(&sma->sem_perm);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001219 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1220 if (err)
1221 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 break;
1224 default:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001225 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 err = -EINVAL;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001227 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230out_unlock:
1231 sem_unlock(sma);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001232out_up:
1233 up_write(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return err;
1235}
1236
Al Viroe1fd1f42013-03-05 15:04:55 -05001237SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001240 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001241 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (semid < 0)
1244 return -EINVAL;
1245
1246 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001247 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 switch(cmd) {
1250 case IPC_INFO:
1251 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001252 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001254 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 case GETALL:
1256 case GETVAL:
1257 case GETPID:
1258 case GETNCNT:
1259 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001261 return semctl_main(ns, semid, semnum, cmd, p);
1262 case SETVAL:
1263 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 case IPC_RMID:
1265 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001266 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 default:
1268 return -EINVAL;
1269 }
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/* If the task doesn't already have a undo_list, then allocate one
1273 * here. We guarantee there is only one thread using this undo list,
1274 * and current is THE ONE
1275 *
1276 * If this allocation and assignment succeeds, but later
1277 * portions of this code fail, there is no need to free the sem_undo_list.
1278 * Just let it stay associated with the task, and it'll be freed later
1279 * at exit time.
1280 *
1281 * This can block, so callers must hold no locks.
1282 */
1283static inline int get_undo_list(struct sem_undo_list **undo_listp)
1284{
1285 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 undo_list = current->sysvsem.undo_list;
1288 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001289 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (undo_list == NULL)
1291 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001292 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001294 INIT_LIST_HEAD(&undo_list->list_proc);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 current->sysvsem.undo_list = undo_list;
1297 }
1298 *undo_listp = undo_list;
1299 return 0;
1300}
1301
Nick Pigginbf17bb72009-12-15 16:47:28 -08001302static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001304 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Nick Pigginbf17bb72009-12-15 16:47:28 -08001306 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1307 if (un->semid == semid)
1308 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001310 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Nick Pigginbf17bb72009-12-15 16:47:28 -08001313static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1314{
1315 struct sem_undo *un;
1316
1317 assert_spin_locked(&ulp->lock);
1318
1319 un = __lookup_undo(ulp, semid);
1320 if (un) {
1321 list_del_rcu(&un->list_proc);
1322 list_add_rcu(&un->list_proc, &ulp->list_proc);
1323 }
1324 return un;
1325}
1326
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001327/**
1328 * find_alloc_undo - Lookup (and if not present create) undo array
1329 * @ns: namespace
1330 * @semid: semaphore array id
1331 *
1332 * The function looks up (and if not present creates) the undo structure.
1333 * The size of the undo structure depends on the size of the semaphore
1334 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001335 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1336 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001337 */
1338static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct sem_array *sma;
1341 struct sem_undo_list *ulp;
1342 struct sem_undo *un, *new;
1343 int nsems;
1344 int error;
1345
1346 error = get_undo_list(&ulp);
1347 if (error)
1348 return ERR_PTR(error);
1349
Manfred Spraul380af1b2008-07-25 01:48:06 -07001350 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001351 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001353 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (likely(un!=NULL))
1355 goto out;
1356
1357 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001358 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001359 sma = sem_obtain_object_check(ns, semid);
1360 if (IS_ERR(sma)) {
1361 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001362 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001363 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001366 ipc_rcu_getref(sma);
1367 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001369 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001370 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (!new) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001372 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return ERR_PTR(-ENOMEM);
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Manfred Spraul380af1b2008-07-25 01:48:06 -07001376 /* step 3: Acquire the lock on semaphore array */
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001377 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (sma->sem_perm.deleted) {
1379 sem_unlock(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 kfree(new);
1381 un = ERR_PTR(-EIDRM);
1382 goto out;
1383 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001384 spin_lock(&ulp->lock);
1385
1386 /*
1387 * step 4: check for races: did someone else allocate the undo struct?
1388 */
1389 un = lookup_undo(ulp, semid);
1390 if (un) {
1391 kfree(new);
1392 goto success;
1393 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001394 /* step 5: initialize & link new undo structure */
1395 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001396 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001397 new->semid = semid;
1398 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001399 list_add_rcu(&new->list_proc, &ulp->list_proc);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001400 assert_spin_locked(&sma->sem_perm.lock);
1401 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001402 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001403
1404success:
1405 spin_unlock(&ulp->lock);
1406 rcu_read_lock();
1407 sem_unlock(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408out:
1409 return un;
1410}
1411
Manfred Spraulc61284e2010-07-20 13:24:23 -07001412
1413/**
1414 * get_queue_result - Retrieve the result code from sem_queue
1415 * @q: Pointer to queue structure
1416 *
1417 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1418 * q->status, then we must loop until the value is replaced with the final
1419 * value: This may happen if a task is woken up by an unrelated event (e.g.
1420 * signal) and in parallel the task is woken up by another task because it got
1421 * the requested semaphores.
1422 *
1423 * The function can be called with or without holding the semaphore spinlock.
1424 */
1425static int get_queue_result(struct sem_queue *q)
1426{
1427 int error;
1428
1429 error = q->status;
1430 while (unlikely(error == IN_WAKEUP)) {
1431 cpu_relax();
1432 error = q->status;
1433 }
1434
1435 return error;
1436}
1437
1438
Heiko Carstensd5460c92009-01-14 14:14:27 +01001439SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1440 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 int error = -EINVAL;
1443 struct sem_array *sma;
1444 struct sembuf fast_sops[SEMOPM_FAST];
1445 struct sembuf* sops = fast_sops, *sop;
1446 struct sem_undo *un;
Manfred Spraulb78755a2005-06-23 00:10:06 -07001447 int undos = 0, alter = 0, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 struct sem_queue queue;
1449 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001450 struct ipc_namespace *ns;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001451 struct list_head tasks;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001452
1453 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (nsops < 1 || semid < 0)
1456 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001457 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return -E2BIG;
1459 if(nsops > SEMOPM_FAST) {
1460 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1461 if(sops==NULL)
1462 return -ENOMEM;
1463 }
1464 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1465 error=-EFAULT;
1466 goto out_free;
1467 }
1468 if (timeout) {
1469 struct timespec _timeout;
1470 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1471 error = -EFAULT;
1472 goto out_free;
1473 }
1474 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1475 _timeout.tv_nsec >= 1000000000L) {
1476 error = -EINVAL;
1477 goto out_free;
1478 }
1479 jiffies_left = timespec_to_jiffies(&_timeout);
1480 }
1481 max = 0;
1482 for (sop = sops; sop < sops + nsops; sop++) {
1483 if (sop->sem_num >= max)
1484 max = sop->sem_num;
1485 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001486 undos = 1;
1487 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 alter = 1;
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (undos) {
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001492 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (IS_ERR(un)) {
1494 error = PTR_ERR(un);
1495 goto out_free;
1496 }
1497 } else
1498 un = NULL;
1499
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001500 INIT_LIST_HEAD(&tasks);
1501
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001502 rcu_read_lock();
1503 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001504 if (IS_ERR(sma)) {
Manfred Spraul380af1b2008-07-25 01:48:06 -07001505 if (un)
1506 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001507 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001509 }
1510
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001511 error = -EFBIG;
1512 if (max >= sma->sem_nsems) {
1513 rcu_read_unlock();
1514 goto out_wakeup;
1515 }
1516
1517 error = -EACCES;
1518 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1519 rcu_read_unlock();
1520 goto out_wakeup;
1521 }
1522
1523 error = security_sem_semop(sma, sops, nsops, alter);
1524 if (error) {
1525 rcu_read_unlock();
1526 goto out_wakeup;
1527 }
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001530 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001532 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001533 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001534 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001536 error = -EIDRM;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001537 ipc_lock_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001538 if (un) {
1539 if (un->semid == -1) {
1540 rcu_read_unlock();
1541 goto out_unlock_free;
1542 } else {
1543 /*
1544 * rcu lock can be released, "un" cannot disappear:
1545 * - sem_lock is acquired, thus IPC_RMID is
1546 * impossible.
1547 * - exit_sem is impossible, it always operates on
1548 * current (or a dead task).
1549 */
1550
1551 rcu_read_unlock();
1552 }
1553 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001554
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001555 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (error <= 0) {
1557 if (alter && error == 0)
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001558 do_smart_update(sma, sops, nsops, 1, &tasks);
Manfred Spraul636c6be2009-12-15 16:47:33 -08001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto out_unlock_free;
1561 }
1562
1563 /* We need to sleep on this operation, so we put the current
1564 * task into the pending queue and go to sleep.
1565 */
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 queue.sops = sops;
1568 queue.nsops = nsops;
1569 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001570 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 queue.alter = alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Manfred Spraulb97e8202009-12-15 16:47:32 -08001573 if (nsops == 1) {
1574 struct sem *curr;
1575 curr = &sma->sem_base[sops->sem_num];
1576
1577 if (alter)
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001578 list_add_tail(&queue.list, &curr->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001579 else
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001580 list_add(&queue.list, &curr->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001581 } else {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001582 if (alter)
1583 list_add_tail(&queue.list, &sma->sem_pending);
1584 else
1585 list_add(&queue.list, &sma->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001586 sma->complex_count++;
1587 }
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 queue.status = -EINTR;
1590 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001591
1592sleep_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 current->state = TASK_INTERRUPTIBLE;
1594 sem_unlock(sma);
1595
1596 if (timeout)
1597 jiffies_left = schedule_timeout(jiffies_left);
1598 else
1599 schedule();
1600
Manfred Spraulc61284e2010-07-20 13:24:23 -07001601 error = get_queue_result(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 if (error != -EINTR) {
1604 /* fast path: update_queue already obtained all requested
Manfred Spraulc61284e2010-07-20 13:24:23 -07001605 * resources.
1606 * Perform a smp_mb(): User space could assume that semop()
1607 * is a memory barrier: Without the mb(), the cpu could
1608 * speculatively read in user space stale data that was
1609 * overwritten by the previous owner of the semaphore.
1610 */
1611 smp_mb();
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto out_free;
1614 }
1615
Rik van Rielc460b662013-04-30 19:15:35 -07001616 sma = sem_obtain_lock(ns, semid);
Manfred Sprauld694ad62011-07-25 17:11:47 -07001617
1618 /*
1619 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1620 */
1621 error = get_queue_result(&queue);
1622
1623 /*
1624 * Array removed? If yes, leave without sem_unlock().
1625 */
Nadia Derbey023a5352007-10-18 23:40:51 -07001626 if (IS_ERR(sma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 goto out_free;
1628 }
1629
Manfred Spraulc61284e2010-07-20 13:24:23 -07001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
Manfred Sprauld694ad62011-07-25 17:11:47 -07001632 * If queue.status != -EINTR we are woken up by another process.
1633 * Leave without unlink_queue(), but with sem_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 */
Manfred Spraulc61284e2010-07-20 13:24:23 -07001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (error != -EINTR) {
1637 goto out_unlock_free;
1638 }
1639
1640 /*
1641 * If an interrupt occurred we have to clean up the queue
1642 */
1643 if (timeout && jiffies_left == 0)
1644 error = -EAGAIN;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001645
1646 /*
1647 * If the wakeup was spurious, just retry
1648 */
1649 if (error == -EINTR && !signal_pending(current))
1650 goto sleep_again;
1651
Manfred Spraulb97e8202009-12-15 16:47:32 -08001652 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654out_unlock_free:
1655 sem_unlock(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001656out_wakeup:
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001657 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658out_free:
1659 if(sops != fast_sops)
1660 kfree(sops);
1661 return error;
1662}
1663
Heiko Carstensd5460c92009-01-14 14:14:27 +01001664SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
1665 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 return sys_semtimedop(semid, tsops, nsops, NULL);
1668}
1669
1670/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1671 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 */
1673
1674int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1675{
1676 struct sem_undo_list *undo_list;
1677 int error;
1678
1679 if (clone_flags & CLONE_SYSVSEM) {
1680 error = get_undo_list(&undo_list);
1681 if (error)
1682 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 atomic_inc(&undo_list->refcnt);
1684 tsk->sysvsem.undo_list = undo_list;
1685 } else
1686 tsk->sysvsem.undo_list = NULL;
1687
1688 return 0;
1689}
1690
1691/*
1692 * add semadj values to semaphores, free undo structures.
1693 * undo structures are not freed when semaphore arrays are destroyed
1694 * so some of them may be out of date.
1695 * IMPLEMENTATION NOTE: There is some confusion over whether the
1696 * set of adjustments that needs to be done should be done in an atomic
1697 * manner or not. That is, if we are attempting to decrement the semval
1698 * should we queue up and wait until we can do so legally?
1699 * The original implementation attempted to do this (queue and wait).
1700 * The current implementation does not do so. The POSIX standard
1701 * and SVID should be consulted to determine what behavior is mandated.
1702 */
1703void exit_sem(struct task_struct *tsk)
1704{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001705 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001707 ulp = tsk->sysvsem.undo_list;
1708 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07001710 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001712 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return;
1714
Manfred Spraul380af1b2008-07-25 01:48:06 -07001715 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001717 struct sem_undo *un;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001718 struct list_head tasks;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001719 int semid;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001720 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Manfred Spraul380af1b2008-07-25 01:48:06 -07001722 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02001723 un = list_entry_rcu(ulp->list_proc.next,
1724 struct sem_undo, list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001725 if (&un->list_proc == &ulp->list_proc)
1726 semid = -1;
1727 else
1728 semid = un->semid;
1729 rcu_read_unlock();
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001730
Manfred Spraul380af1b2008-07-25 01:48:06 -07001731 if (semid == -1)
1732 break;
1733
1734 sma = sem_lock_check(tsk->nsproxy->ipc_ns, un->semid);
1735
1736 /* exit_sem raced with IPC_RMID, nothing to do */
Nadia Derbey023a5352007-10-18 23:40:51 -07001737 if (IS_ERR(sma))
Manfred Spraul380af1b2008-07-25 01:48:06 -07001738 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Nick Pigginbf17bb72009-12-15 16:47:28 -08001740 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001741 if (un == NULL) {
1742 /* exit_sem raced with IPC_RMID+semget() that created
1743 * exactly the same semid. Nothing to do.
1744 */
1745 sem_unlock(sma);
1746 continue;
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Manfred Spraul380af1b2008-07-25 01:48:06 -07001749 /* remove un from the linked lists */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001750 assert_spin_locked(&sma->sem_perm.lock);
1751 list_del(&un->list_id);
1752
Manfred Spraul380af1b2008-07-25 01:48:06 -07001753 spin_lock(&ulp->lock);
1754 list_del_rcu(&un->list_proc);
1755 spin_unlock(&ulp->lock);
1756
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001757 /* perform adjustments registered in un */
1758 for (i = 0; i < sma->sem_nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001759 struct sem * semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001760 if (un->semadj[i]) {
1761 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /*
1763 * Range checks of the new semaphore value,
1764 * not defined by sus:
1765 * - Some unices ignore the undo entirely
1766 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1767 * - some cap the value (e.g. FreeBSD caps
1768 * at 0, but doesn't enforce SEMVMX)
1769 *
1770 * Linux caps the semaphore value, both at 0
1771 * and at SEMVMX.
1772 *
1773 * Manfred <manfred@colorfullife.com>
1774 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001775 if (semaphore->semval < 0)
1776 semaphore->semval = 0;
1777 if (semaphore->semval > SEMVMX)
1778 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001779 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001783 INIT_LIST_HEAD(&tasks);
1784 do_smart_update(sma, NULL, 0, 1, &tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 sem_unlock(sma);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001786 wake_up_sem_queue_do(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001787
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001788 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001790 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
1793#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001794static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001796 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07001797 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Mike Waychison19b49462005-09-06 15:17:10 -07001799 return seq_printf(s,
Manfred Spraulb97e8202009-12-15 16:47:32 -08001800 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
Mike Waychison19b49462005-09-06 15:17:10 -07001801 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001802 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001803 sma->sem_perm.mode,
1804 sma->sem_nsems,
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001805 from_kuid_munged(user_ns, sma->sem_perm.uid),
1806 from_kgid_munged(user_ns, sma->sem_perm.gid),
1807 from_kuid_munged(user_ns, sma->sem_perm.cuid),
1808 from_kgid_munged(user_ns, sma->sem_perm.cgid),
Mike Waychison19b49462005-09-06 15:17:10 -07001809 sma->sem_otime,
1810 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812#endif