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