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