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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com> |
| 7 | * |
| 8 | * SMP-threaded, sysctl's added |
Christian Kujau | 624dffc | 2006-01-15 02:43:54 +0100 | [diff] [blame] | 9 | * (c) 1999 Manfred Spraul <manfred@colorfullife.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Enforced range limit on SEM_UNDO |
Alan Cox | 046c688 | 2009-01-05 14:06:29 +0000 | [diff] [blame] | 11 | * (c) 2001 Red Hat Inc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Lockless wakeup |
| 13 | * (c) 2003 Manfred Spraul <manfred@colorfullife.com> |
Manfred Spraul | c5cf635 | 2010-05-26 14:43:43 -0700 | [diff] [blame] | 14 | * Further wakeup optimizations, documentation |
| 15 | * (c) 2010 Manfred Spraul <manfred@colorfullife.com> |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 16 | * |
| 17 | * support for audit of ipc object properties and permission changes |
| 18 | * Dustin Kirkland <dustin.kirkland@us.ibm.com> |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 19 | * |
| 20 | * namespaces support |
| 21 | * OpenVZ, SWsoft Inc. |
| 22 | * Pavel Emelianov <xemul@openvz.org> |
Manfred Spraul | c5cf635 | 2010-05-26 14:43:43 -0700 | [diff] [blame] | 23 | * |
| 24 | * Implementation notes: (May 2010) |
| 25 | * This file implements System V semaphores. |
| 26 | * |
| 27 | * User space visible behavior: |
| 28 | * - FIFO ordering for semop() operations (just FIFO, not starvation |
| 29 | * protection) |
| 30 | * - multiple semaphore operations that alter the same semaphore in |
| 31 | * one semop() are handled. |
| 32 | * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and |
| 33 | * SETALL calls. |
| 34 | * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO. |
| 35 | * - undo adjustments at process exit are limited to 0..SEMVMX. |
| 36 | * - namespace are supported. |
| 37 | * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing |
| 38 | * to /proc/sys/kernel/sem. |
| 39 | * - statistics about the usage are reported in /proc/sysvipc/sem. |
| 40 | * |
| 41 | * Internals: |
| 42 | * - scalability: |
| 43 | * - all global variables are read-mostly. |
| 44 | * - semop() calls and semctl(RMID) are synchronized by RCU. |
| 45 | * - most operations do write operations (actually: spin_lock calls) to |
| 46 | * the per-semaphore array structure. |
| 47 | * Thus: Perfect SMP scaling between independent semaphore arrays. |
| 48 | * If multiple semaphores in one array are used, then cache line |
| 49 | * trashing on the semaphore array spinlock will limit the scaling. |
| 50 | * - semncnt and semzcnt are calculated on demand in count_semncnt() and |
| 51 | * count_semzcnt() |
| 52 | * - the task that performs a successful semop() scans the list of all |
| 53 | * sleeping tasks and completes any pending operations that can be fulfilled. |
| 54 | * Semaphores are actively given to waiting tasks (necessary for FIFO). |
| 55 | * (see update_queue()) |
| 56 | * - To improve the scalability, the actual wake-up calls are performed after |
| 57 | * dropping all locks. (see wake_up_sem_queue_prepare(), |
| 58 | * wake_up_sem_queue_do()) |
| 59 | * - All work is done by the waker, the woken up task does not have to do |
| 60 | * anything - not even acquiring a lock or dropping a refcount. |
| 61 | * - A woken up task may not even touch the semaphore array anymore, it may |
| 62 | * have been destroyed already by a semctl(RMID). |
| 63 | * - The synchronizations between wake-ups due to a timeout/signal and a |
| 64 | * wake-up due to a completed semaphore operation is achieved by using an |
| 65 | * intermediate state (IN_WAKEUP). |
| 66 | * - UNDO values are stored in an array (one per process and per |
| 67 | * semaphore array, lazily allocated). For backwards compatibility, multiple |
| 68 | * modes for the UNDO variables are supported (per process, per thread) |
| 69 | * (see copy_semundo, CLONE_SYSVSEM) |
| 70 | * - There are two lists of the pending operations: a per-array list |
| 71 | * and per-semaphore list (stored in the array). This allows to achieve FIFO |
| 72 | * ordering without always scanning all pending operations. |
| 73 | * The worst-case behavior is nevertheless O(N^2) for N wakeups. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #include <linux/slab.h> |
| 77 | #include <linux/spinlock.h> |
| 78 | #include <linux/init.h> |
| 79 | #include <linux/proc_fs.h> |
| 80 | #include <linux/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #include <linux/security.h> |
| 82 | #include <linux/syscalls.h> |
| 83 | #include <linux/audit.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 84 | #include <linux/capability.h> |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 85 | #include <linux/seq_file.h> |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 86 | #include <linux/rwsem.h> |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 87 | #include <linux/nsproxy.h> |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 88 | #include <linux/ipc_namespace.h> |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | #include <asm/uaccess.h> |
| 91 | #include "util.h" |
| 92 | |
Manfred Spraul | e57940d | 2011-11-02 13:38:54 -0700 | [diff] [blame] | 93 | /* One semaphore structure for each semaphore in the system. */ |
| 94 | struct sem { |
| 95 | int semval; /* current value */ |
| 96 | int sempid; /* pid of last operation */ |
| 97 | struct list_head sem_pending; /* pending single-sop operations */ |
| 98 | }; |
| 99 | |
| 100 | /* One queue for each sleeping process in the system. */ |
| 101 | struct sem_queue { |
| 102 | struct list_head simple_list; /* queue of pending operations */ |
| 103 | struct list_head list; /* queue of pending operations */ |
| 104 | struct task_struct *sleeper; /* this process */ |
| 105 | struct sem_undo *undo; /* undo structure */ |
| 106 | int pid; /* process id of requesting process */ |
| 107 | int status; /* completion status of operation */ |
| 108 | struct sembuf *sops; /* array of pending operations */ |
| 109 | int nsops; /* number of operations */ |
| 110 | int alter; /* does *sops alter the array? */ |
| 111 | }; |
| 112 | |
| 113 | /* Each task has a list of undo requests. They are executed automatically |
| 114 | * when the process exits. |
| 115 | */ |
| 116 | struct sem_undo { |
| 117 | struct list_head list_proc; /* per-process list: * |
| 118 | * all undos from one process |
| 119 | * rcu protected */ |
| 120 | struct rcu_head rcu; /* rcu struct for sem_undo */ |
| 121 | struct sem_undo_list *ulp; /* back ptr to sem_undo_list */ |
| 122 | struct list_head list_id; /* per semaphore array list: |
| 123 | * all undos for one array */ |
| 124 | int semid; /* semaphore set identifier */ |
| 125 | short *semadj; /* array of adjustments */ |
| 126 | /* one per semaphore */ |
| 127 | }; |
| 128 | |
| 129 | /* sem_undo_list controls shared access to the list of sem_undo structures |
| 130 | * that may be shared among all a CLONE_SYSVSEM task group. |
| 131 | */ |
| 132 | struct sem_undo_list { |
| 133 | atomic_t refcnt; |
| 134 | spinlock_t lock; |
| 135 | struct list_head list_proc; |
| 136 | }; |
| 137 | |
| 138 | |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 139 | #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 141 | #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm) |
Nadia Derbey | 1b531f2 | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 142 | #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 144 | static int newary(struct ipc_namespace *, struct ipc_params *); |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 145 | static void freeary(struct ipc_namespace *, struct kern_ipc_perm *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 147 | static int sysvipc_sem_proc_show(struct seq_file *s, void *it); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | #endif |
| 149 | |
| 150 | #define SEMMSL_FAST 256 /* 512 bytes on stack */ |
| 151 | #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */ |
| 152 | |
| 153 | /* |
| 154 | * linked list protection: |
| 155 | * sem_undo.id_next, |
| 156 | * sem_array.sem_pending{,last}, |
| 157 | * sem_array.sem_undo: sem_lock() for read/write |
| 158 | * sem_undo.proc_next: only "current" is allowed to read/write that field. |
| 159 | * |
| 160 | */ |
| 161 | |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 162 | #define sc_semmsl sem_ctls[0] |
| 163 | #define sc_semmns sem_ctls[1] |
| 164 | #define sc_semopm sem_ctls[2] |
| 165 | #define sc_semmni sem_ctls[3] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 167 | void sem_init_ns(struct ipc_namespace *ns) |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 168 | { |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 169 | ns->sc_semmsl = SEMMSL; |
| 170 | ns->sc_semmns = SEMMNS; |
| 171 | ns->sc_semopm = SEMOPM; |
| 172 | ns->sc_semmni = SEMMNI; |
| 173 | ns->used_sems = 0; |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 174 | ipc_init_ids(&ns->ids[IPC_SEM_IDS]); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 177 | #ifdef CONFIG_IPC_NS |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 178 | void sem_exit_ns(struct ipc_namespace *ns) |
| 179 | { |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 180 | free_ipcs(ns, &sem_ids(ns), freeary); |
Serge E. Hallyn | 7d6feeb | 2009-12-15 16:47:27 -0800 | [diff] [blame] | 181 | idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 182 | } |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 183 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | void __init sem_init (void) |
| 186 | { |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 187 | sem_init_ns(&init_ipc_ns); |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 188 | ipc_init_proc_interface("sysvipc/sem", |
| 189 | " key semid perms nsems uid gid cuid cgid otime ctime\n", |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 190 | IPC_SEM_IDS, sysvipc_sem_proc_show); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 193 | /* |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 194 | * sem_lock_(check_) routines are called in the paths where the rw_mutex |
| 195 | * is not held. |
| 196 | */ |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 197 | static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id) |
| 198 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 199 | struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id); |
| 200 | |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 201 | if (IS_ERR(ipcp)) |
| 202 | return (struct sem_array *)ipcp; |
| 203 | |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 204 | return container_of(ipcp, struct sem_array, sem_perm); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 207 | static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id) |
| 208 | { |
| 209 | struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id); |
| 210 | |
| 211 | if (IS_ERR(ipcp)) |
| 212 | return ERR_CAST(ipcp); |
| 213 | |
| 214 | return container_of(ipcp, struct sem_array, sem_perm); |
| 215 | } |
| 216 | |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 217 | static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns, |
| 218 | int id) |
| 219 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 220 | struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id); |
| 221 | |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 222 | if (IS_ERR(ipcp)) |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 223 | return ERR_CAST(ipcp); |
| 224 | |
| 225 | return container_of(ipcp, struct sem_array, sem_perm); |
| 226 | } |
| 227 | |
| 228 | static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns, |
| 229 | int id) |
| 230 | { |
| 231 | struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id); |
| 232 | |
| 233 | if (IS_ERR(ipcp)) |
| 234 | return ERR_CAST(ipcp); |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 235 | |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 236 | return container_of(ipcp, struct sem_array, sem_perm); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 239 | static inline void sem_lock_and_putref(struct sem_array *sma) |
| 240 | { |
| 241 | ipc_lock_by_ptr(&sma->sem_perm); |
| 242 | ipc_rcu_putref(sma); |
| 243 | } |
| 244 | |
| 245 | static inline void sem_getref_and_unlock(struct sem_array *sma) |
| 246 | { |
| 247 | ipc_rcu_getref(sma); |
| 248 | ipc_unlock(&(sma)->sem_perm); |
| 249 | } |
| 250 | |
| 251 | static inline void sem_putref(struct sem_array *sma) |
| 252 | { |
| 253 | ipc_lock_by_ptr(&sma->sem_perm); |
| 254 | ipc_rcu_putref(sma); |
| 255 | ipc_unlock(&(sma)->sem_perm); |
| 256 | } |
| 257 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 258 | /* |
| 259 | * Call inside the rcu read section. |
| 260 | */ |
| 261 | static inline void sem_getref(struct sem_array *sma) |
| 262 | { |
| 263 | spin_lock(&(sma)->sem_perm.lock); |
| 264 | ipc_rcu_getref(sma); |
| 265 | ipc_unlock(&(sma)->sem_perm); |
| 266 | } |
| 267 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 268 | static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s) |
| 269 | { |
| 270 | ipc_rmid(&sem_ids(ns), &s->sem_perm); |
| 271 | } |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | /* |
| 274 | * Lockless wakeup algorithm: |
| 275 | * Without the check/retry algorithm a lockless wakeup is possible: |
| 276 | * - queue.status is initialized to -EINTR before blocking. |
| 277 | * - wakeup is performed by |
| 278 | * * unlinking the queue entry from sma->sem_pending |
| 279 | * * setting queue.status to IN_WAKEUP |
| 280 | * This is the notification for the blocked thread that a |
| 281 | * result value is imminent. |
| 282 | * * call wake_up_process |
| 283 | * * set queue.status to the final value. |
| 284 | * - the previously blocked thread checks queue.status: |
| 285 | * * if it's IN_WAKEUP, then it must wait until the value changes |
| 286 | * * if it's not -EINTR, then the operation was completed by |
| 287 | * update_queue. semtimedop can return queue.status without |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 288 | * performing any operation on the sem array. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | * * otherwise it must acquire the spinlock and check what's up. |
| 290 | * |
| 291 | * The two-stage algorithm is necessary to protect against the following |
| 292 | * races: |
| 293 | * - if queue.status is set after wake_up_process, then the woken up idle |
| 294 | * thread could race forward and try (and fail) to acquire sma->lock |
| 295 | * before update_queue had a chance to set queue.status |
| 296 | * - if queue.status is written before wake_up_process and if the |
| 297 | * blocked process is woken up by a signal between writing |
| 298 | * queue.status and the wake_up_process, then the woken up |
| 299 | * process could return from semtimedop and die by calling |
| 300 | * sys_exit before wake_up_process is called. Then wake_up_process |
| 301 | * will oops, because the task structure is already invalid. |
| 302 | * (yes, this happened on s390 with sysv msg). |
| 303 | * |
| 304 | */ |
| 305 | #define IN_WAKEUP 1 |
| 306 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 307 | /** |
| 308 | * newary - Create a new semaphore set |
| 309 | * @ns: namespace |
| 310 | * @params: ptr to the structure that contains key, semflg and nsems |
| 311 | * |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 312 | * Called with sem_ids.rw_mutex held (as a writer) |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 313 | */ |
| 314 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 315 | static int newary(struct ipc_namespace *ns, struct ipc_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
| 317 | int id; |
| 318 | int retval; |
| 319 | struct sem_array *sma; |
| 320 | int size; |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 321 | key_t key = params->key; |
| 322 | int nsems = params->u.nsems; |
| 323 | int semflg = params->flg; |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 324 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
| 326 | if (!nsems) |
| 327 | return -EINVAL; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 328 | if (ns->used_sems + nsems > ns->sc_semmns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | return -ENOSPC; |
| 330 | |
| 331 | size = sizeof (*sma) + nsems * sizeof (struct sem); |
| 332 | sma = ipc_rcu_alloc(size); |
| 333 | if (!sma) { |
| 334 | return -ENOMEM; |
| 335 | } |
| 336 | memset (sma, 0, size); |
| 337 | |
| 338 | sma->sem_perm.mode = (semflg & S_IRWXUGO); |
| 339 | sma->sem_perm.key = key; |
| 340 | |
| 341 | sma->sem_perm.security = NULL; |
| 342 | retval = security_sem_alloc(sma); |
| 343 | if (retval) { |
| 344 | ipc_rcu_putref(sma); |
| 345 | return retval; |
| 346 | } |
| 347 | |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 348 | id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni); |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 349 | if (id < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | security_sem_free(sma); |
| 351 | ipc_rcu_putref(sma); |
Pierre Peiffer | 283bb7f | 2007-10-18 23:40:57 -0700 | [diff] [blame] | 352 | return id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 354 | ns->used_sems += nsems; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
| 356 | sma->sem_base = (struct sem *) &sma[1]; |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 357 | |
| 358 | for (i = 0; i < nsems; i++) |
| 359 | INIT_LIST_HEAD(&sma->sem_base[i].sem_pending); |
| 360 | |
| 361 | sma->complex_count = 0; |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 362 | INIT_LIST_HEAD(&sma->sem_pending); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 363 | INIT_LIST_HEAD(&sma->list_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | sma->sem_nsems = nsems; |
| 365 | sma->sem_ctime = get_seconds(); |
| 366 | sem_unlock(sma); |
| 367 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 368 | return sma->sem_perm.id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 371 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 372 | /* |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 373 | * Called with sem_ids.rw_mutex and ipcp locked. |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 374 | */ |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 375 | static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 377 | struct sem_array *sma; |
| 378 | |
| 379 | sma = container_of(ipcp, struct sem_array, sem_perm); |
| 380 | return security_sem_associate(sma, semflg); |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 383 | /* |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 384 | * Called with sem_ids.rw_mutex and ipcp locked. |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 385 | */ |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 386 | static inline int sem_more_checks(struct kern_ipc_perm *ipcp, |
| 387 | struct ipc_params *params) |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 388 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 389 | struct sem_array *sma; |
| 390 | |
| 391 | sma = container_of(ipcp, struct sem_array, sem_perm); |
| 392 | if (params->u.nsems > sma->sem_nsems) |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 393 | return -EINVAL; |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 398 | SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg) |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 399 | { |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 400 | struct ipc_namespace *ns; |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 401 | struct ipc_ops sem_ops; |
| 402 | struct ipc_params sem_params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 404 | ns = current->nsproxy->ipc_ns; |
| 405 | |
| 406 | if (nsems < 0 || nsems > ns->sc_semmsl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | return -EINVAL; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 408 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 409 | sem_ops.getnew = newary; |
| 410 | sem_ops.associate = sem_security; |
| 411 | sem_ops.more_checks = sem_more_checks; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 412 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 413 | sem_params.key = key; |
| 414 | sem_params.flg = semflg; |
| 415 | sem_params.u.nsems = nsems; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 416 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 417 | return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | /* |
| 421 | * Determine whether a sequence of semaphore operations would succeed |
| 422 | * all at once. Return 0 if yes, 1 if need to sleep, else return error code. |
| 423 | */ |
| 424 | |
| 425 | static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops, |
| 426 | int nsops, struct sem_undo *un, int pid) |
| 427 | { |
| 428 | int result, sem_op; |
| 429 | struct sembuf *sop; |
| 430 | struct sem * curr; |
| 431 | |
| 432 | for (sop = sops; sop < sops + nsops; sop++) { |
| 433 | curr = sma->sem_base + sop->sem_num; |
| 434 | sem_op = sop->sem_op; |
| 435 | result = curr->semval; |
| 436 | |
| 437 | if (!sem_op && result) |
| 438 | goto would_block; |
| 439 | |
| 440 | result += sem_op; |
| 441 | if (result < 0) |
| 442 | goto would_block; |
| 443 | if (result > SEMVMX) |
| 444 | goto out_of_range; |
| 445 | if (sop->sem_flg & SEM_UNDO) { |
| 446 | int undo = un->semadj[sop->sem_num] - sem_op; |
| 447 | /* |
| 448 | * Exceeding the undo range is an error. |
| 449 | */ |
| 450 | if (undo < (-SEMAEM - 1) || undo > SEMAEM) |
| 451 | goto out_of_range; |
| 452 | } |
| 453 | curr->semval = result; |
| 454 | } |
| 455 | |
| 456 | sop--; |
| 457 | while (sop >= sops) { |
| 458 | sma->sem_base[sop->sem_num].sempid = pid; |
| 459 | if (sop->sem_flg & SEM_UNDO) |
| 460 | un->semadj[sop->sem_num] -= sop->sem_op; |
| 461 | sop--; |
| 462 | } |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return 0; |
| 465 | |
| 466 | out_of_range: |
| 467 | result = -ERANGE; |
| 468 | goto undo; |
| 469 | |
| 470 | would_block: |
| 471 | if (sop->sem_flg & IPC_NOWAIT) |
| 472 | result = -EAGAIN; |
| 473 | else |
| 474 | result = 1; |
| 475 | |
| 476 | undo: |
| 477 | sop--; |
| 478 | while (sop >= sops) { |
| 479 | sma->sem_base[sop->sem_num].semval -= sop->sem_op; |
| 480 | sop--; |
| 481 | } |
| 482 | |
| 483 | return result; |
| 484 | } |
| 485 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 486 | /** wake_up_sem_queue_prepare(q, error): Prepare wake-up |
| 487 | * @q: queue entry that must be signaled |
| 488 | * @error: Error value for the signal |
| 489 | * |
| 490 | * Prepare the wake-up of the queue entry q. |
Nick Piggin | d421209 | 2009-12-15 16:47:30 -0800 | [diff] [blame] | 491 | */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 492 | static void wake_up_sem_queue_prepare(struct list_head *pt, |
| 493 | struct sem_queue *q, int error) |
Nick Piggin | d421209 | 2009-12-15 16:47:30 -0800 | [diff] [blame] | 494 | { |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 495 | if (list_empty(pt)) { |
| 496 | /* |
| 497 | * Hold preempt off so that we don't get preempted and have the |
| 498 | * wakee busy-wait until we're scheduled back on. |
| 499 | */ |
| 500 | preempt_disable(); |
| 501 | } |
Nick Piggin | d421209 | 2009-12-15 16:47:30 -0800 | [diff] [blame] | 502 | q->status = IN_WAKEUP; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 503 | q->pid = error; |
| 504 | |
| 505 | list_add_tail(&q->simple_list, pt); |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * wake_up_sem_queue_do(pt) - do the actual wake-up |
| 510 | * @pt: list of tasks to be woken up |
| 511 | * |
| 512 | * Do the actual wake-up. |
| 513 | * The function is called without any locks held, thus the semaphore array |
| 514 | * could be destroyed already and the tasks can disappear as soon as the |
| 515 | * status is set to the actual return code. |
| 516 | */ |
| 517 | static void wake_up_sem_queue_do(struct list_head *pt) |
| 518 | { |
| 519 | struct sem_queue *q, *t; |
| 520 | int did_something; |
| 521 | |
| 522 | did_something = !list_empty(pt); |
| 523 | list_for_each_entry_safe(q, t, pt, simple_list) { |
| 524 | wake_up_process(q->sleeper); |
| 525 | /* q can disappear immediately after writing q->status. */ |
| 526 | smp_wmb(); |
| 527 | q->status = q->pid; |
| 528 | } |
| 529 | if (did_something) |
| 530 | preempt_enable(); |
Nick Piggin | d421209 | 2009-12-15 16:47:30 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 533 | static void unlink_queue(struct sem_array *sma, struct sem_queue *q) |
| 534 | { |
| 535 | list_del(&q->list); |
| 536 | if (q->nsops == 1) |
| 537 | list_del(&q->simple_list); |
| 538 | else |
| 539 | sma->complex_count--; |
| 540 | } |
| 541 | |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 542 | /** check_restart(sma, q) |
| 543 | * @sma: semaphore array |
| 544 | * @q: the operation that just completed |
| 545 | * |
| 546 | * update_queue is O(N^2) when it restarts scanning the whole queue of |
| 547 | * waiting operations. Therefore this function checks if the restart is |
| 548 | * really necessary. It is called after a previously waiting operation |
| 549 | * was completed. |
| 550 | */ |
| 551 | static int check_restart(struct sem_array *sma, struct sem_queue *q) |
| 552 | { |
| 553 | struct sem *curr; |
| 554 | struct sem_queue *h; |
| 555 | |
| 556 | /* if the operation didn't modify the array, then no restart */ |
| 557 | if (q->alter == 0) |
| 558 | return 0; |
| 559 | |
| 560 | /* pending complex operations are too difficult to analyse */ |
| 561 | if (sma->complex_count) |
| 562 | return 1; |
| 563 | |
| 564 | /* we were a sleeping complex operation. Too difficult */ |
| 565 | if (q->nsops > 1) |
| 566 | return 1; |
| 567 | |
| 568 | curr = sma->sem_base + q->sops[0].sem_num; |
| 569 | |
| 570 | /* No-one waits on this queue */ |
| 571 | if (list_empty(&curr->sem_pending)) |
| 572 | return 0; |
| 573 | |
| 574 | /* the new semaphore value */ |
| 575 | if (curr->semval) { |
| 576 | /* It is impossible that someone waits for the new value: |
| 577 | * - q is a previously sleeping simple operation that |
| 578 | * altered the array. It must be a decrement, because |
| 579 | * simple increments never sleep. |
| 580 | * - The value is not 0, thus wait-for-zero won't proceed. |
| 581 | * - If there are older (higher priority) decrements |
| 582 | * in the queue, then they have observed the original |
| 583 | * semval value and couldn't proceed. The operation |
| 584 | * decremented to value - thus they won't proceed either. |
| 585 | */ |
| 586 | BUG_ON(q->sops[0].sem_op >= 0); |
| 587 | return 0; |
| 588 | } |
| 589 | /* |
| 590 | * semval is 0. Check if there are wait-for-zero semops. |
| 591 | * They must be the first entries in the per-semaphore simple queue |
| 592 | */ |
| 593 | h = list_first_entry(&curr->sem_pending, struct sem_queue, simple_list); |
| 594 | BUG_ON(h->nsops != 1); |
| 595 | BUG_ON(h->sops[0].sem_num != q->sops[0].sem_num); |
| 596 | |
| 597 | /* Yes, there is a wait-for-zero semop. Restart */ |
| 598 | if (h->sops[0].sem_op == 0) |
| 599 | return 1; |
| 600 | |
| 601 | /* Again - no-one is waiting for the new value. */ |
| 602 | return 0; |
| 603 | } |
| 604 | |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 605 | |
| 606 | /** |
| 607 | * update_queue(sma, semnum): Look for tasks that can be completed. |
| 608 | * @sma: semaphore array. |
| 609 | * @semnum: semaphore that was modified. |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 610 | * @pt: list head for the tasks that must be woken up. |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 611 | * |
| 612 | * update_queue must be called after a semaphore in a semaphore array |
| 613 | * was modified. If multiple semaphore were modified, then @semnum |
| 614 | * must be set to -1. |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 615 | * The tasks that must be woken up are added to @pt. The return code |
| 616 | * is stored in q->pid. |
| 617 | * The function return 1 if at least one semop was completed successfully. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 619 | static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 621 | struct sem_queue *q; |
| 622 | struct list_head *walk; |
| 623 | struct list_head *pending_list; |
| 624 | int offset; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 625 | int semop_completed = 0; |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 626 | |
| 627 | /* if there are complex operations around, then knowing the semaphore |
| 628 | * that was modified doesn't help us. Assume that multiple semaphores |
| 629 | * were modified. |
| 630 | */ |
| 631 | if (sma->complex_count) |
| 632 | semnum = -1; |
| 633 | |
| 634 | if (semnum == -1) { |
| 635 | pending_list = &sma->sem_pending; |
| 636 | offset = offsetof(struct sem_queue, list); |
| 637 | } else { |
| 638 | pending_list = &sma->sem_base[semnum].sem_pending; |
| 639 | offset = offsetof(struct sem_queue, simple_list); |
| 640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
Nick Piggin | 9cad200 | 2009-12-15 16:47:29 -0800 | [diff] [blame] | 642 | again: |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 643 | walk = pending_list->next; |
| 644 | while (walk != pending_list) { |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 645 | int error, restart; |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 646 | |
| 647 | q = (struct sem_queue *)((char *)walk - offset); |
| 648 | walk = walk->next; |
Nick Piggin | 9cad200 | 2009-12-15 16:47:29 -0800 | [diff] [blame] | 649 | |
Manfred Spraul | d987f8b | 2009-12-15 16:47:34 -0800 | [diff] [blame] | 650 | /* If we are scanning the single sop, per-semaphore list of |
| 651 | * one semaphore and that semaphore is 0, then it is not |
| 652 | * necessary to scan the "alter" entries: simple increments |
| 653 | * that affect only one entry succeed immediately and cannot |
| 654 | * be in the per semaphore pending queue, and decrements |
| 655 | * cannot be successful if the value is already 0. |
| 656 | */ |
| 657 | if (semnum != -1 && sma->sem_base[semnum].semval == 0 && |
| 658 | q->alter) |
| 659 | break; |
| 660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | error = try_atomic_semop(sma, q->sops, q->nsops, |
| 662 | q->undo, q->pid); |
| 663 | |
| 664 | /* Does q->sleeper still need to sleep? */ |
Nick Piggin | 9cad200 | 2009-12-15 16:47:29 -0800 | [diff] [blame] | 665 | if (error > 0) |
| 666 | continue; |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 667 | |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 668 | unlink_queue(sma, q); |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 669 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 670 | if (error) { |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 671 | restart = 0; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 672 | } else { |
| 673 | semop_completed = 1; |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 674 | restart = check_restart(sma, q); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 675 | } |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 676 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 677 | wake_up_sem_queue_prepare(pt, q, error); |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 678 | if (restart) |
Nick Piggin | 9cad200 | 2009-12-15 16:47:29 -0800 | [diff] [blame] | 679 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 681 | return semop_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 684 | /** |
| 685 | * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 686 | * @sma: semaphore array |
| 687 | * @sops: operations that were performed |
| 688 | * @nsops: number of operations |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 689 | * @otime: force setting otime |
| 690 | * @pt: list head of the tasks that must be woken up. |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 691 | * |
| 692 | * do_smart_update() does the required called to update_queue, based on the |
| 693 | * actual changes that were performed on the semaphore array. |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 694 | * Note that the function does not do the actual wake-up: the caller is |
| 695 | * responsible for calling wake_up_sem_queue_do(@pt). |
| 696 | * It is safe to perform this call after dropping all locks. |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 697 | */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 698 | static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops, |
| 699 | int otime, struct list_head *pt) |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 700 | { |
| 701 | int i; |
| 702 | |
| 703 | if (sma->complex_count || sops == NULL) { |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 704 | if (update_queue(sma, -1, pt)) |
| 705 | otime = 1; |
| 706 | goto done; |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | for (i = 0; i < nsops; i++) { |
| 710 | if (sops[i].sem_op > 0 || |
| 711 | (sops[i].sem_op < 0 && |
| 712 | sma->sem_base[sops[i].sem_num].semval == 0)) |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 713 | if (update_queue(sma, sops[i].sem_num, pt)) |
| 714 | otime = 1; |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 715 | } |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 716 | done: |
| 717 | if (otime) |
| 718 | sma->sem_otime = get_seconds(); |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | /* The following counts are associated to each semaphore: |
| 723 | * semncnt number of tasks waiting on semval being nonzero |
| 724 | * semzcnt number of tasks waiting on semval being zero |
| 725 | * This model assumes that a task waits on exactly one semaphore. |
| 726 | * Since semaphore operations are to be performed atomically, tasks actually |
| 727 | * wait on a whole sequence of semaphores simultaneously. |
| 728 | * The counts we return here are a rough approximation, but still |
| 729 | * warrant that semncnt+semzcnt>0 if the task is on the pending queue. |
| 730 | */ |
| 731 | static int count_semncnt (struct sem_array * sma, ushort semnum) |
| 732 | { |
| 733 | int semncnt; |
| 734 | struct sem_queue * q; |
| 735 | |
| 736 | semncnt = 0; |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 737 | list_for_each_entry(q, &sma->sem_pending, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | struct sembuf * sops = q->sops; |
| 739 | int nsops = q->nsops; |
| 740 | int i; |
| 741 | for (i = 0; i < nsops; i++) |
| 742 | if (sops[i].sem_num == semnum |
| 743 | && (sops[i].sem_op < 0) |
| 744 | && !(sops[i].sem_flg & IPC_NOWAIT)) |
| 745 | semncnt++; |
| 746 | } |
| 747 | return semncnt; |
| 748 | } |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | static int count_semzcnt (struct sem_array * sma, ushort semnum) |
| 751 | { |
| 752 | int semzcnt; |
| 753 | struct sem_queue * q; |
| 754 | |
| 755 | semzcnt = 0; |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 756 | list_for_each_entry(q, &sma->sem_pending, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | struct sembuf * sops = q->sops; |
| 758 | int nsops = q->nsops; |
| 759 | int i; |
| 760 | for (i = 0; i < nsops; i++) |
| 761 | if (sops[i].sem_num == semnum |
| 762 | && (sops[i].sem_op == 0) |
| 763 | && !(sops[i].sem_flg & IPC_NOWAIT)) |
| 764 | semzcnt++; |
| 765 | } |
| 766 | return semzcnt; |
| 767 | } |
| 768 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 769 | /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked |
| 770 | * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex |
| 771 | * remains locked on exit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | */ |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 773 | static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | { |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 775 | struct sem_undo *un, *tu; |
| 776 | struct sem_queue *q, *tq; |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 777 | struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 778 | struct list_head tasks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 780 | /* Free the existing undo structures for this semaphore set. */ |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 781 | assert_spin_locked(&sma->sem_perm.lock); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 782 | list_for_each_entry_safe(un, tu, &sma->list_id, list_id) { |
| 783 | list_del(&un->list_id); |
| 784 | spin_lock(&un->ulp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | un->semid = -1; |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 786 | list_del_rcu(&un->list_proc); |
| 787 | spin_unlock(&un->ulp->lock); |
Lai Jiangshan | 693a8b6 | 2011-03-18 12:09:35 +0800 | [diff] [blame] | 788 | kfree_rcu(un, rcu); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 789 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
| 791 | /* Wake up all pending processes and let them fail with EIDRM. */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 792 | INIT_LIST_HEAD(&tasks); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 793 | list_for_each_entry_safe(q, tq, &sma->sem_pending, list) { |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 794 | unlink_queue(sma, q); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 795 | wake_up_sem_queue_prepare(&tasks, q, -EIDRM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 798 | /* Remove the semaphore set from the IDR */ |
| 799 | sem_rmid(ns, sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | sem_unlock(sma); |
| 801 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 802 | wake_up_sem_queue_do(&tasks); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 803 | ns->used_sems -= sma->sem_nsems; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | security_sem_free(sma); |
| 805 | ipc_rcu_putref(sma); |
| 806 | } |
| 807 | |
| 808 | static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version) |
| 809 | { |
| 810 | switch(version) { |
| 811 | case IPC_64: |
| 812 | return copy_to_user(buf, in, sizeof(*in)); |
| 813 | case IPC_OLD: |
| 814 | { |
| 815 | struct semid_ds out; |
| 816 | |
Dan Rosenberg | 982f7c2 | 2010-09-30 15:15:31 -0700 | [diff] [blame] | 817 | memset(&out, 0, sizeof(out)); |
| 818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm); |
| 820 | |
| 821 | out.sem_otime = in->sem_otime; |
| 822 | out.sem_ctime = in->sem_ctime; |
| 823 | out.sem_nsems = in->sem_nsems; |
| 824 | |
| 825 | return copy_to_user(buf, &out, sizeof(out)); |
| 826 | } |
| 827 | default: |
| 828 | return -EINVAL; |
| 829 | } |
| 830 | } |
| 831 | |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 832 | static int semctl_nolock(struct ipc_namespace *ns, int semid, |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 833 | int cmd, int version, void __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
Amerigo Wang | e5cc9c7 | 2009-12-15 16:47:35 -0800 | [diff] [blame] | 835 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | struct sem_array *sma; |
| 837 | |
| 838 | switch(cmd) { |
| 839 | case IPC_INFO: |
| 840 | case SEM_INFO: |
| 841 | { |
| 842 | struct seminfo seminfo; |
| 843 | int max_id; |
| 844 | |
| 845 | err = security_sem_semctl(NULL, cmd); |
| 846 | if (err) |
| 847 | return err; |
| 848 | |
| 849 | memset(&seminfo,0,sizeof(seminfo)); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 850 | seminfo.semmni = ns->sc_semmni; |
| 851 | seminfo.semmns = ns->sc_semmns; |
| 852 | seminfo.semmsl = ns->sc_semmsl; |
| 853 | seminfo.semopm = ns->sc_semopm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | seminfo.semvmx = SEMVMX; |
| 855 | seminfo.semmnu = SEMMNU; |
| 856 | seminfo.semmap = SEMMAP; |
| 857 | seminfo.semume = SEMUME; |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 858 | down_read(&sem_ids(ns).rw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (cmd == SEM_INFO) { |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 860 | seminfo.semusz = sem_ids(ns).in_use; |
| 861 | seminfo.semaem = ns->used_sems; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } else { |
| 863 | seminfo.semusz = SEMUSZ; |
| 864 | seminfo.semaem = SEMAEM; |
| 865 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 866 | max_id = ipc_get_maxid(&sem_ids(ns)); |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 867 | up_read(&sem_ids(ns).rw_mutex); |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 868 | if (copy_to_user(p, &seminfo, sizeof(struct seminfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | return -EFAULT; |
| 870 | return (max_id < 0) ? 0: max_id; |
| 871 | } |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 872 | case IPC_STAT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | case SEM_STAT: |
| 874 | { |
| 875 | struct semid64_ds tbuf; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 876 | int id = 0; |
| 877 | |
| 878 | memset(&tbuf, 0, sizeof(tbuf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 880 | if (cmd == SEM_STAT) { |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 881 | rcu_read_lock(); |
| 882 | sma = sem_obtain_object(ns, semid); |
| 883 | if (IS_ERR(sma)) { |
| 884 | err = PTR_ERR(sma); |
| 885 | goto out_unlock; |
| 886 | } |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 887 | id = sma->sem_perm.id; |
| 888 | } else { |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 889 | rcu_read_lock(); |
| 890 | sma = sem_obtain_object_check(ns, semid); |
| 891 | if (IS_ERR(sma)) { |
| 892 | err = PTR_ERR(sma); |
| 893 | goto out_unlock; |
| 894 | } |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 895 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | |
| 897 | err = -EACCES; |
Serge E. Hallyn | b0e7759 | 2011-03-23 16:43:24 -0700 | [diff] [blame] | 898 | if (ipcperms(ns, &sma->sem_perm, S_IRUGO)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | goto out_unlock; |
| 900 | |
| 901 | err = security_sem_semctl(sma, cmd); |
| 902 | if (err) |
| 903 | goto out_unlock; |
| 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm); |
| 906 | tbuf.sem_otime = sma->sem_otime; |
| 907 | tbuf.sem_ctime = sma->sem_ctime; |
| 908 | tbuf.sem_nsems = sma->sem_nsems; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 909 | rcu_read_unlock(); |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 910 | if (copy_semid_to_user(p, &tbuf, version)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | return -EFAULT; |
| 912 | return id; |
| 913 | } |
| 914 | default: |
| 915 | return -EINVAL; |
| 916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | out_unlock: |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 918 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | return err; |
| 920 | } |
| 921 | |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 922 | static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum, |
| 923 | unsigned long arg) |
| 924 | { |
| 925 | struct sem_undo *un; |
| 926 | struct sem_array *sma; |
| 927 | struct sem* curr; |
| 928 | int err; |
| 929 | int nsems; |
| 930 | struct list_head tasks; |
| 931 | int val; |
| 932 | #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN) |
| 933 | /* big-endian 64bit */ |
| 934 | val = arg >> 32; |
| 935 | #else |
| 936 | /* 32bit or little-endian 64bit */ |
| 937 | val = arg; |
| 938 | #endif |
| 939 | |
| 940 | sma = sem_lock_check(ns, semid); |
| 941 | if (IS_ERR(sma)) |
| 942 | return PTR_ERR(sma); |
| 943 | |
| 944 | INIT_LIST_HEAD(&tasks); |
| 945 | nsems = sma->sem_nsems; |
| 946 | |
| 947 | err = -EACCES; |
| 948 | if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) |
| 949 | goto out_unlock; |
| 950 | |
| 951 | err = security_sem_semctl(sma, SETVAL); |
| 952 | if (err) |
| 953 | goto out_unlock; |
| 954 | |
| 955 | err = -EINVAL; |
| 956 | if(semnum < 0 || semnum >= nsems) |
| 957 | goto out_unlock; |
| 958 | |
| 959 | curr = &sma->sem_base[semnum]; |
| 960 | |
| 961 | err = -ERANGE; |
| 962 | if (val > SEMVMX || val < 0) |
| 963 | goto out_unlock; |
| 964 | |
| 965 | assert_spin_locked(&sma->sem_perm.lock); |
| 966 | list_for_each_entry(un, &sma->list_id, list_id) |
| 967 | un->semadj[semnum] = 0; |
| 968 | |
| 969 | curr->semval = val; |
| 970 | curr->sempid = task_tgid_vnr(current); |
| 971 | sma->sem_ctime = get_seconds(); |
| 972 | /* maybe some queued-up processes were waiting for this */ |
| 973 | do_smart_update(sma, NULL, 0, 0, &tasks); |
| 974 | err = 0; |
| 975 | out_unlock: |
| 976 | sem_unlock(sma); |
| 977 | wake_up_sem_queue_do(&tasks); |
| 978 | return err; |
| 979 | } |
| 980 | |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 981 | static int semctl_main(struct ipc_namespace *ns, int semid, int semnum, |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 982 | int cmd, void __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | { |
| 984 | struct sem_array *sma; |
| 985 | struct sem* curr; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 986 | int err, nsems; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | ushort fast_sem_io[SEMMSL_FAST]; |
| 988 | ushort* sem_io = fast_sem_io; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 989 | struct list_head tasks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 991 | INIT_LIST_HEAD(&tasks); |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 992 | |
| 993 | rcu_read_lock(); |
| 994 | sma = sem_obtain_object_check(ns, semid); |
| 995 | if (IS_ERR(sma)) { |
| 996 | rcu_read_unlock(); |
| 997 | return PTR_ERR(sma); |
| 998 | } |
| 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | nsems = sma->sem_nsems; |
| 1001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | err = -EACCES; |
Serge E. Hallyn | b0e7759 | 2011-03-23 16:43:24 -0700 | [diff] [blame] | 1003 | if (ipcperms(ns, &sma->sem_perm, |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1004 | cmd == SETALL ? S_IWUGO : S_IRUGO)) { |
| 1005 | rcu_read_unlock(); |
| 1006 | goto out_wakeup; |
| 1007 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | err = security_sem_semctl(sma, cmd); |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1010 | if (err) { |
| 1011 | rcu_read_unlock(); |
| 1012 | goto out_wakeup; |
| 1013 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
| 1015 | err = -EACCES; |
| 1016 | switch (cmd) { |
| 1017 | case GETALL: |
| 1018 | { |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1019 | ushort __user *array = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | int i; |
| 1021 | |
| 1022 | if(nsems > SEMMSL_FAST) { |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1023 | sem_getref(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
| 1025 | sem_io = ipc_alloc(sizeof(ushort)*nsems); |
| 1026 | if(sem_io == NULL) { |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1027 | sem_putref(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | return -ENOMEM; |
| 1029 | } |
| 1030 | |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1031 | sem_lock_and_putref(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | if (sma->sem_perm.deleted) { |
| 1033 | sem_unlock(sma); |
| 1034 | err = -EIDRM; |
| 1035 | goto out_free; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1039 | spin_lock(&sma->sem_perm.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | for (i = 0; i < sma->sem_nsems; i++) |
| 1041 | sem_io[i] = sma->sem_base[i].semval; |
| 1042 | sem_unlock(sma); |
| 1043 | err = 0; |
| 1044 | if(copy_to_user(array, sem_io, nsems*sizeof(ushort))) |
| 1045 | err = -EFAULT; |
| 1046 | goto out_free; |
| 1047 | } |
| 1048 | case SETALL: |
| 1049 | { |
| 1050 | int i; |
| 1051 | struct sem_undo *un; |
| 1052 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1053 | ipc_rcu_getref(sma); |
| 1054 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | if(nsems > SEMMSL_FAST) { |
| 1057 | sem_io = ipc_alloc(sizeof(ushort)*nsems); |
| 1058 | if(sem_io == NULL) { |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1059 | sem_putref(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | return -ENOMEM; |
| 1061 | } |
| 1062 | } |
| 1063 | |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1064 | if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) { |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1065 | sem_putref(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | err = -EFAULT; |
| 1067 | goto out_free; |
| 1068 | } |
| 1069 | |
| 1070 | for (i = 0; i < nsems; i++) { |
| 1071 | if (sem_io[i] > SEMVMX) { |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1072 | sem_putref(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | err = -ERANGE; |
| 1074 | goto out_free; |
| 1075 | } |
| 1076 | } |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1077 | sem_lock_and_putref(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | if (sma->sem_perm.deleted) { |
| 1079 | sem_unlock(sma); |
| 1080 | err = -EIDRM; |
| 1081 | goto out_free; |
| 1082 | } |
| 1083 | |
| 1084 | for (i = 0; i < nsems; i++) |
| 1085 | sma->sem_base[i].semval = sem_io[i]; |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1086 | |
| 1087 | assert_spin_locked(&sma->sem_perm.lock); |
| 1088 | list_for_each_entry(un, &sma->list_id, list_id) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | for (i = 0; i < nsems; i++) |
| 1090 | un->semadj[i] = 0; |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | sma->sem_ctime = get_seconds(); |
| 1093 | /* maybe some queued-up processes were waiting for this */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1094 | do_smart_update(sma, NULL, 0, 0, &tasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | err = 0; |
| 1096 | goto out_unlock; |
| 1097 | } |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1098 | /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | err = -EINVAL; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1101 | if (semnum < 0 || semnum >= nsems) { |
| 1102 | rcu_read_unlock(); |
| 1103 | goto out_wakeup; |
| 1104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1106 | spin_lock(&sma->sem_perm.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | curr = &sma->sem_base[semnum]; |
| 1108 | |
| 1109 | switch (cmd) { |
| 1110 | case GETVAL: |
| 1111 | err = curr->semval; |
| 1112 | goto out_unlock; |
| 1113 | case GETPID: |
| 1114 | err = curr->sempid; |
| 1115 | goto out_unlock; |
| 1116 | case GETNCNT: |
| 1117 | err = count_semncnt(sma,semnum); |
| 1118 | goto out_unlock; |
| 1119 | case GETZCNT: |
| 1120 | err = count_semzcnt(sma,semnum); |
| 1121 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | out_unlock: |
| 1125 | sem_unlock(sma); |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1126 | out_wakeup: |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1127 | wake_up_sem_queue_do(&tasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | out_free: |
| 1129 | if(sem_io != fast_sem_io) |
| 1130 | ipc_free(sem_io, sizeof(ushort)*nsems); |
| 1131 | return err; |
| 1132 | } |
| 1133 | |
Pierre Peiffer | 016d713 | 2008-04-29 01:00:50 -0700 | [diff] [blame] | 1134 | static inline unsigned long |
| 1135 | copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | { |
| 1137 | switch(version) { |
| 1138 | case IPC_64: |
Pierre Peiffer | 016d713 | 2008-04-29 01:00:50 -0700 | [diff] [blame] | 1139 | if (copy_from_user(out, buf, sizeof(*out))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | case IPC_OLD: |
| 1143 | { |
| 1144 | struct semid_ds tbuf_old; |
| 1145 | |
| 1146 | if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) |
| 1147 | return -EFAULT; |
| 1148 | |
Pierre Peiffer | 016d713 | 2008-04-29 01:00:50 -0700 | [diff] [blame] | 1149 | out->sem_perm.uid = tbuf_old.sem_perm.uid; |
| 1150 | out->sem_perm.gid = tbuf_old.sem_perm.gid; |
| 1151 | out->sem_perm.mode = tbuf_old.sem_perm.mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | default: |
| 1156 | return -EINVAL; |
| 1157 | } |
| 1158 | } |
| 1159 | |
Pierre Peiffer | 522bb2a | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1160 | /* |
| 1161 | * This function handles some semctl commands which require the rw_mutex |
| 1162 | * to be held in write mode. |
| 1163 | * NOTE: no locks must be held, the rw_mutex is taken inside this function. |
| 1164 | */ |
Pierre Peiffer | 21a4826 | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1165 | static int semctl_down(struct ipc_namespace *ns, int semid, |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1166 | int cmd, int version, void __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | { |
| 1168 | struct sem_array *sma; |
| 1169 | int err; |
Pierre Peiffer | 016d713 | 2008-04-29 01:00:50 -0700 | [diff] [blame] | 1170 | struct semid64_ds semid64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | struct kern_ipc_perm *ipcp; |
| 1172 | |
| 1173 | if(cmd == IPC_SET) { |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1174 | if (copy_semid_from_user(&semid64, p, version)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1178 | ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd, |
| 1179 | &semid64.sem_perm, 0); |
Pierre Peiffer | a5f75e7 | 2008-04-29 01:00:54 -0700 | [diff] [blame] | 1180 | if (IS_ERR(ipcp)) |
| 1181 | return PTR_ERR(ipcp); |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1182 | |
Pierre Peiffer | a5f75e7 | 2008-04-29 01:00:54 -0700 | [diff] [blame] | 1183 | sma = container_of(ipcp, struct sem_array, sem_perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | |
| 1185 | err = security_sem_semctl(sma, cmd); |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1186 | if (err) { |
| 1187 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | goto out_unlock; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | switch(cmd){ |
| 1192 | case IPC_RMID: |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1193 | ipc_lock_object(&sma->sem_perm); |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 1194 | freeary(ns, ipcp); |
Pierre Peiffer | 522bb2a | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1195 | goto out_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | case IPC_SET: |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1197 | ipc_lock_object(&sma->sem_perm); |
Eric W. Biederman | 1efdb69 | 2012-02-07 16:54:11 -0800 | [diff] [blame] | 1198 | err = ipc_update_perm(&semid64.sem_perm, ipcp); |
| 1199 | if (err) |
| 1200 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | sma->sem_ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | break; |
| 1203 | default: |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1204 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | err = -EINVAL; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1206 | goto out_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
| 1209 | out_unlock: |
| 1210 | sem_unlock(sma); |
Pierre Peiffer | 522bb2a | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1211 | out_up: |
| 1212 | up_write(&sem_ids(ns).rw_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | return err; |
| 1214 | } |
| 1215 | |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1216 | SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | int version; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1219 | struct ipc_namespace *ns; |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1220 | void __user *p = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
| 1222 | if (semid < 0) |
| 1223 | return -EINVAL; |
| 1224 | |
| 1225 | version = ipc_parse_version(&cmd); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1226 | ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
| 1228 | switch(cmd) { |
| 1229 | case IPC_INFO: |
| 1230 | case SEM_INFO: |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 1231 | case IPC_STAT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | case SEM_STAT: |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1233 | return semctl_nolock(ns, semid, cmd, version, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | case GETALL: |
| 1235 | case GETVAL: |
| 1236 | case GETPID: |
| 1237 | case GETNCNT: |
| 1238 | case GETZCNT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | case SETALL: |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1240 | return semctl_main(ns, semid, semnum, cmd, p); |
| 1241 | case SETVAL: |
| 1242 | return semctl_setval(ns, semid, semnum, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | case IPC_RMID: |
| 1244 | case IPC_SET: |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1245 | return semctl_down(ns, semid, cmd, version, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | default: |
| 1247 | return -EINVAL; |
| 1248 | } |
| 1249 | } |
| 1250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | /* If the task doesn't already have a undo_list, then allocate one |
| 1252 | * here. We guarantee there is only one thread using this undo list, |
| 1253 | * and current is THE ONE |
| 1254 | * |
| 1255 | * If this allocation and assignment succeeds, but later |
| 1256 | * portions of this code fail, there is no need to free the sem_undo_list. |
| 1257 | * Just let it stay associated with the task, and it'll be freed later |
| 1258 | * at exit time. |
| 1259 | * |
| 1260 | * This can block, so callers must hold no locks. |
| 1261 | */ |
| 1262 | static inline int get_undo_list(struct sem_undo_list **undo_listp) |
| 1263 | { |
| 1264 | struct sem_undo_list *undo_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
| 1266 | undo_list = current->sysvsem.undo_list; |
| 1267 | if (!undo_list) { |
Matt Helsley | 2453a30 | 2006-10-02 02:18:25 -0700 | [diff] [blame] | 1268 | undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | if (undo_list == NULL) |
| 1270 | return -ENOMEM; |
Ingo Molnar | 00a5dfd | 2005-08-05 23:05:27 +0200 | [diff] [blame] | 1271 | spin_lock_init(&undo_list->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | atomic_set(&undo_list->refcnt, 1); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1273 | INIT_LIST_HEAD(&undo_list->list_proc); |
| 1274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | current->sysvsem.undo_list = undo_list; |
| 1276 | } |
| 1277 | *undo_listp = undo_list; |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1281 | static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | { |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1283 | struct sem_undo *un; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1285 | list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) { |
| 1286 | if (un->semid == semid) |
| 1287 | return un; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | } |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1289 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1292 | static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid) |
| 1293 | { |
| 1294 | struct sem_undo *un; |
| 1295 | |
| 1296 | assert_spin_locked(&ulp->lock); |
| 1297 | |
| 1298 | un = __lookup_undo(ulp, semid); |
| 1299 | if (un) { |
| 1300 | list_del_rcu(&un->list_proc); |
| 1301 | list_add_rcu(&un->list_proc, &ulp->list_proc); |
| 1302 | } |
| 1303 | return un; |
| 1304 | } |
| 1305 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1306 | /** |
| 1307 | * find_alloc_undo - Lookup (and if not present create) undo array |
| 1308 | * @ns: namespace |
| 1309 | * @semid: semaphore array id |
| 1310 | * |
| 1311 | * The function looks up (and if not present creates) the undo structure. |
| 1312 | * The size of the undo structure depends on the size of the semaphore |
| 1313 | * array, thus the alloc path is not that straightforward. |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1314 | * Lifetime-rules: sem_undo is rcu-protected, on success, the function |
| 1315 | * performs a rcu_read_lock(). |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1316 | */ |
| 1317 | static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | { |
| 1319 | struct sem_array *sma; |
| 1320 | struct sem_undo_list *ulp; |
| 1321 | struct sem_undo *un, *new; |
| 1322 | int nsems; |
| 1323 | int error; |
| 1324 | |
| 1325 | error = get_undo_list(&ulp); |
| 1326 | if (error) |
| 1327 | return ERR_PTR(error); |
| 1328 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1329 | rcu_read_lock(); |
Pierre Peiffer | c530c6a | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 1330 | spin_lock(&ulp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | un = lookup_undo(ulp, semid); |
Pierre Peiffer | c530c6a | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 1332 | spin_unlock(&ulp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | if (likely(un!=NULL)) |
| 1334 | goto out; |
| 1335 | |
| 1336 | /* no undo structure around - allocate one. */ |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1337 | /* step 1: figure out the size of the semaphore array */ |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1338 | sma = sem_obtain_object_check(ns, semid); |
| 1339 | if (IS_ERR(sma)) { |
| 1340 | rcu_read_unlock(); |
Julia Lawall | 4de85cd | 2010-05-26 14:43:44 -0700 | [diff] [blame] | 1341 | return ERR_CAST(sma); |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1342 | } |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | nsems = sma->sem_nsems; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1345 | ipc_rcu_getref(sma); |
| 1346 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1348 | /* step 2: allocate new undo structure */ |
Burman Yan | 4668edc | 2006-12-06 20:38:51 -0800 | [diff] [blame] | 1349 | new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | if (!new) { |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1351 | sem_putref(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | return ERR_PTR(-ENOMEM); |
| 1353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1355 | /* step 3: Acquire the lock on semaphore array */ |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1356 | sem_lock_and_putref(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | if (sma->sem_perm.deleted) { |
| 1358 | sem_unlock(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | kfree(new); |
| 1360 | un = ERR_PTR(-EIDRM); |
| 1361 | goto out; |
| 1362 | } |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1363 | spin_lock(&ulp->lock); |
| 1364 | |
| 1365 | /* |
| 1366 | * step 4: check for races: did someone else allocate the undo struct? |
| 1367 | */ |
| 1368 | un = lookup_undo(ulp, semid); |
| 1369 | if (un) { |
| 1370 | kfree(new); |
| 1371 | goto success; |
| 1372 | } |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1373 | /* step 5: initialize & link new undo structure */ |
| 1374 | new->semadj = (short *) &new[1]; |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1375 | new->ulp = ulp; |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1376 | new->semid = semid; |
| 1377 | assert_spin_locked(&ulp->lock); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1378 | list_add_rcu(&new->list_proc, &ulp->list_proc); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1379 | assert_spin_locked(&sma->sem_perm.lock); |
| 1380 | list_add(&new->list_id, &sma->list_id); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1381 | un = new; |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1382 | |
| 1383 | success: |
| 1384 | spin_unlock(&ulp->lock); |
| 1385 | rcu_read_lock(); |
| 1386 | sem_unlock(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | out: |
| 1388 | return un; |
| 1389 | } |
| 1390 | |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1391 | |
| 1392 | /** |
| 1393 | * get_queue_result - Retrieve the result code from sem_queue |
| 1394 | * @q: Pointer to queue structure |
| 1395 | * |
| 1396 | * Retrieve the return code from the pending queue. If IN_WAKEUP is found in |
| 1397 | * q->status, then we must loop until the value is replaced with the final |
| 1398 | * value: This may happen if a task is woken up by an unrelated event (e.g. |
| 1399 | * signal) and in parallel the task is woken up by another task because it got |
| 1400 | * the requested semaphores. |
| 1401 | * |
| 1402 | * The function can be called with or without holding the semaphore spinlock. |
| 1403 | */ |
| 1404 | static int get_queue_result(struct sem_queue *q) |
| 1405 | { |
| 1406 | int error; |
| 1407 | |
| 1408 | error = q->status; |
| 1409 | while (unlikely(error == IN_WAKEUP)) { |
| 1410 | cpu_relax(); |
| 1411 | error = q->status; |
| 1412 | } |
| 1413 | |
| 1414 | return error; |
| 1415 | } |
| 1416 | |
| 1417 | |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 1418 | SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops, |
| 1419 | unsigned, nsops, const struct timespec __user *, timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | { |
| 1421 | int error = -EINVAL; |
| 1422 | struct sem_array *sma; |
| 1423 | struct sembuf fast_sops[SEMOPM_FAST]; |
| 1424 | struct sembuf* sops = fast_sops, *sop; |
| 1425 | struct sem_undo *un; |
Manfred Spraul | b78755a | 2005-06-23 00:10:06 -0700 | [diff] [blame] | 1426 | int undos = 0, alter = 0, max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | struct sem_queue queue; |
| 1428 | unsigned long jiffies_left = 0; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1429 | struct ipc_namespace *ns; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1430 | struct list_head tasks; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1431 | |
| 1432 | ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
| 1434 | if (nsops < 1 || semid < 0) |
| 1435 | return -EINVAL; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1436 | if (nsops > ns->sc_semopm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | return -E2BIG; |
| 1438 | if(nsops > SEMOPM_FAST) { |
| 1439 | sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL); |
| 1440 | if(sops==NULL) |
| 1441 | return -ENOMEM; |
| 1442 | } |
| 1443 | if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) { |
| 1444 | error=-EFAULT; |
| 1445 | goto out_free; |
| 1446 | } |
| 1447 | if (timeout) { |
| 1448 | struct timespec _timeout; |
| 1449 | if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) { |
| 1450 | error = -EFAULT; |
| 1451 | goto out_free; |
| 1452 | } |
| 1453 | if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 || |
| 1454 | _timeout.tv_nsec >= 1000000000L) { |
| 1455 | error = -EINVAL; |
| 1456 | goto out_free; |
| 1457 | } |
| 1458 | jiffies_left = timespec_to_jiffies(&_timeout); |
| 1459 | } |
| 1460 | max = 0; |
| 1461 | for (sop = sops; sop < sops + nsops; sop++) { |
| 1462 | if (sop->sem_num >= max) |
| 1463 | max = sop->sem_num; |
| 1464 | if (sop->sem_flg & SEM_UNDO) |
Manfred Spraul | b78755a | 2005-06-23 00:10:06 -0700 | [diff] [blame] | 1465 | undos = 1; |
| 1466 | if (sop->sem_op != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | alter = 1; |
| 1468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | if (undos) { |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1471 | un = find_alloc_undo(ns, semid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | if (IS_ERR(un)) { |
| 1473 | error = PTR_ERR(un); |
| 1474 | goto out_free; |
| 1475 | } |
| 1476 | } else |
| 1477 | un = NULL; |
| 1478 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1479 | INIT_LIST_HEAD(&tasks); |
| 1480 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1481 | rcu_read_lock(); |
| 1482 | sma = sem_obtain_object_check(ns, semid); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1483 | if (IS_ERR(sma)) { |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1484 | if (un) |
| 1485 | rcu_read_unlock(); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1486 | error = PTR_ERR(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | goto out_free; |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1490 | error = -EFBIG; |
| 1491 | if (max >= sma->sem_nsems) { |
| 1492 | rcu_read_unlock(); |
| 1493 | goto out_wakeup; |
| 1494 | } |
| 1495 | |
| 1496 | error = -EACCES; |
| 1497 | if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) { |
| 1498 | rcu_read_unlock(); |
| 1499 | goto out_wakeup; |
| 1500 | } |
| 1501 | |
| 1502 | error = security_sem_semop(sma, sops, nsops, alter); |
| 1503 | if (error) { |
| 1504 | rcu_read_unlock(); |
| 1505 | goto out_wakeup; |
| 1506 | } |
| 1507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | /* |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1509 | * semid identifiers are not unique - find_alloc_undo may have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | * allocated an undo structure, it was invalidated by an RMID |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1511 | * and now a new array with received the same id. Check and fail. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1512 | * This case can be detected checking un->semid. The existence of |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1513 | * "un" itself is guaranteed by rcu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | */ |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1515 | error = -EIDRM; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1516 | ipc_lock_object(&sma->sem_perm); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1517 | if (un) { |
| 1518 | if (un->semid == -1) { |
| 1519 | rcu_read_unlock(); |
| 1520 | goto out_unlock_free; |
| 1521 | } else { |
| 1522 | /* |
| 1523 | * rcu lock can be released, "un" cannot disappear: |
| 1524 | * - sem_lock is acquired, thus IPC_RMID is |
| 1525 | * impossible. |
| 1526 | * - exit_sem is impossible, it always operates on |
| 1527 | * current (or a dead task). |
| 1528 | */ |
| 1529 | |
| 1530 | rcu_read_unlock(); |
| 1531 | } |
| 1532 | } |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1533 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1534 | error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (error <= 0) { |
| 1536 | if (alter && error == 0) |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1537 | do_smart_update(sma, sops, nsops, 1, &tasks); |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 1538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | goto out_unlock_free; |
| 1540 | } |
| 1541 | |
| 1542 | /* We need to sleep on this operation, so we put the current |
| 1543 | * task into the pending queue and go to sleep. |
| 1544 | */ |
| 1545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | queue.sops = sops; |
| 1547 | queue.nsops = nsops; |
| 1548 | queue.undo = un; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1549 | queue.pid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | queue.alter = alter; |
| 1551 | if (alter) |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1552 | list_add_tail(&queue.list, &sma->sem_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | else |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1554 | list_add(&queue.list, &sma->sem_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 1556 | if (nsops == 1) { |
| 1557 | struct sem *curr; |
| 1558 | curr = &sma->sem_base[sops->sem_num]; |
| 1559 | |
| 1560 | if (alter) |
| 1561 | list_add_tail(&queue.simple_list, &curr->sem_pending); |
| 1562 | else |
| 1563 | list_add(&queue.simple_list, &curr->sem_pending); |
| 1564 | } else { |
| 1565 | INIT_LIST_HEAD(&queue.simple_list); |
| 1566 | sma->complex_count++; |
| 1567 | } |
| 1568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | queue.status = -EINTR; |
| 1570 | queue.sleeper = current; |
Manfred Spraul | 0b0577f | 2011-11-02 13:38:52 -0700 | [diff] [blame] | 1571 | |
| 1572 | sleep_again: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | current->state = TASK_INTERRUPTIBLE; |
| 1574 | sem_unlock(sma); |
| 1575 | |
| 1576 | if (timeout) |
| 1577 | jiffies_left = schedule_timeout(jiffies_left); |
| 1578 | else |
| 1579 | schedule(); |
| 1580 | |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1581 | error = get_queue_result(&queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | |
| 1583 | if (error != -EINTR) { |
| 1584 | /* fast path: update_queue already obtained all requested |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1585 | * resources. |
| 1586 | * Perform a smp_mb(): User space could assume that semop() |
| 1587 | * is a memory barrier: Without the mb(), the cpu could |
| 1588 | * speculatively read in user space stale data that was |
| 1589 | * overwritten by the previous owner of the semaphore. |
| 1590 | */ |
| 1591 | smp_mb(); |
| 1592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | goto out_free; |
| 1594 | } |
| 1595 | |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1596 | sma = sem_lock(ns, semid); |
Manfred Spraul | d694ad6 | 2011-07-25 17:11:47 -0700 | [diff] [blame] | 1597 | |
| 1598 | /* |
| 1599 | * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing. |
| 1600 | */ |
| 1601 | error = get_queue_result(&queue); |
| 1602 | |
| 1603 | /* |
| 1604 | * Array removed? If yes, leave without sem_unlock(). |
| 1605 | */ |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1606 | if (IS_ERR(sma)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | goto out_free; |
| 1608 | } |
| 1609 | |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | /* |
Manfred Spraul | d694ad6 | 2011-07-25 17:11:47 -0700 | [diff] [blame] | 1612 | * If queue.status != -EINTR we are woken up by another process. |
| 1613 | * Leave without unlink_queue(), but with sem_unlock(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | */ |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | if (error != -EINTR) { |
| 1617 | goto out_unlock_free; |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | * If an interrupt occurred we have to clean up the queue |
| 1622 | */ |
| 1623 | if (timeout && jiffies_left == 0) |
| 1624 | error = -EAGAIN; |
Manfred Spraul | 0b0577f | 2011-11-02 13:38:52 -0700 | [diff] [blame] | 1625 | |
| 1626 | /* |
| 1627 | * If the wakeup was spurious, just retry |
| 1628 | */ |
| 1629 | if (error == -EINTR && !signal_pending(current)) |
| 1630 | goto sleep_again; |
| 1631 | |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 1632 | unlink_queue(sma, &queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | |
| 1634 | out_unlock_free: |
| 1635 | sem_unlock(sma); |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame^] | 1636 | out_wakeup: |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1637 | wake_up_sem_queue_do(&tasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | out_free: |
| 1639 | if(sops != fast_sops) |
| 1640 | kfree(sops); |
| 1641 | return error; |
| 1642 | } |
| 1643 | |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 1644 | SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops, |
| 1645 | unsigned, nsops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | { |
| 1647 | return sys_semtimedop(semid, tsops, nsops, NULL); |
| 1648 | } |
| 1649 | |
| 1650 | /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between |
| 1651 | * parent and child tasks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | */ |
| 1653 | |
| 1654 | int copy_semundo(unsigned long clone_flags, struct task_struct *tsk) |
| 1655 | { |
| 1656 | struct sem_undo_list *undo_list; |
| 1657 | int error; |
| 1658 | |
| 1659 | if (clone_flags & CLONE_SYSVSEM) { |
| 1660 | error = get_undo_list(&undo_list); |
| 1661 | if (error) |
| 1662 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | atomic_inc(&undo_list->refcnt); |
| 1664 | tsk->sysvsem.undo_list = undo_list; |
| 1665 | } else |
| 1666 | tsk->sysvsem.undo_list = NULL; |
| 1667 | |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | /* |
| 1672 | * add semadj values to semaphores, free undo structures. |
| 1673 | * undo structures are not freed when semaphore arrays are destroyed |
| 1674 | * so some of them may be out of date. |
| 1675 | * IMPLEMENTATION NOTE: There is some confusion over whether the |
| 1676 | * set of adjustments that needs to be done should be done in an atomic |
| 1677 | * manner or not. That is, if we are attempting to decrement the semval |
| 1678 | * should we queue up and wait until we can do so legally? |
| 1679 | * The original implementation attempted to do this (queue and wait). |
| 1680 | * The current implementation does not do so. The POSIX standard |
| 1681 | * and SVID should be consulted to determine what behavior is mandated. |
| 1682 | */ |
| 1683 | void exit_sem(struct task_struct *tsk) |
| 1684 | { |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1685 | struct sem_undo_list *ulp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1687 | ulp = tsk->sysvsem.undo_list; |
| 1688 | if (!ulp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | return; |
Manfred Spraul | 9edff4a | 2008-04-29 01:00:57 -0700 | [diff] [blame] | 1690 | tsk->sysvsem.undo_list = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1692 | if (!atomic_dec_and_test(&ulp->refcnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | return; |
| 1694 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1695 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | struct sem_array *sma; |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1697 | struct sem_undo *un; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1698 | struct list_head tasks; |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1699 | int semid; |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1700 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1702 | rcu_read_lock(); |
Jiri Pirko | 05725f7 | 2009-04-14 20:17:16 +0200 | [diff] [blame] | 1703 | un = list_entry_rcu(ulp->list_proc.next, |
| 1704 | struct sem_undo, list_proc); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1705 | if (&un->list_proc == &ulp->list_proc) |
| 1706 | semid = -1; |
| 1707 | else |
| 1708 | semid = un->semid; |
| 1709 | rcu_read_unlock(); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1710 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1711 | if (semid == -1) |
| 1712 | break; |
| 1713 | |
| 1714 | sma = sem_lock_check(tsk->nsproxy->ipc_ns, un->semid); |
| 1715 | |
| 1716 | /* exit_sem raced with IPC_RMID, nothing to do */ |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1717 | if (IS_ERR(sma)) |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1718 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1720 | un = __lookup_undo(ulp, semid); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1721 | if (un == NULL) { |
| 1722 | /* exit_sem raced with IPC_RMID+semget() that created |
| 1723 | * exactly the same semid. Nothing to do. |
| 1724 | */ |
| 1725 | sem_unlock(sma); |
| 1726 | continue; |
| 1727 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1729 | /* remove un from the linked lists */ |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1730 | assert_spin_locked(&sma->sem_perm.lock); |
| 1731 | list_del(&un->list_id); |
| 1732 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1733 | spin_lock(&ulp->lock); |
| 1734 | list_del_rcu(&un->list_proc); |
| 1735 | spin_unlock(&ulp->lock); |
| 1736 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1737 | /* perform adjustments registered in un */ |
| 1738 | for (i = 0; i < sma->sem_nsems; i++) { |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 1739 | struct sem * semaphore = &sma->sem_base[i]; |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1740 | if (un->semadj[i]) { |
| 1741 | semaphore->semval += un->semadj[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | /* |
| 1743 | * Range checks of the new semaphore value, |
| 1744 | * not defined by sus: |
| 1745 | * - Some unices ignore the undo entirely |
| 1746 | * (e.g. HP UX 11i 11.22, Tru64 V5.1) |
| 1747 | * - some cap the value (e.g. FreeBSD caps |
| 1748 | * at 0, but doesn't enforce SEMVMX) |
| 1749 | * |
| 1750 | * Linux caps the semaphore value, both at 0 |
| 1751 | * and at SEMVMX. |
| 1752 | * |
| 1753 | * Manfred <manfred@colorfullife.com> |
| 1754 | */ |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 1755 | if (semaphore->semval < 0) |
| 1756 | semaphore->semval = 0; |
| 1757 | if (semaphore->semval > SEMVMX) |
| 1758 | semaphore->semval = SEMVMX; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1759 | semaphore->sempid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | } |
| 1761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | /* maybe some queued-up processes were waiting for this */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1763 | INIT_LIST_HEAD(&tasks); |
| 1764 | do_smart_update(sma, NULL, 0, 1, &tasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | sem_unlock(sma); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1766 | wake_up_sem_queue_do(&tasks); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1767 | |
Lai Jiangshan | 693a8b6 | 2011-03-18 12:09:35 +0800 | [diff] [blame] | 1768 | kfree_rcu(un, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | } |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1770 | kfree(ulp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1774 | static int sysvipc_sem_proc_show(struct seq_file *s, void *it) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | { |
Eric W. Biederman | 1efdb69 | 2012-02-07 16:54:11 -0800 | [diff] [blame] | 1776 | struct user_namespace *user_ns = seq_user_ns(s); |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1777 | struct sem_array *sma = it; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1779 | return seq_printf(s, |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 1780 | "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n", |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1781 | sma->sem_perm.key, |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 1782 | sma->sem_perm.id, |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1783 | sma->sem_perm.mode, |
| 1784 | sma->sem_nsems, |
Eric W. Biederman | 1efdb69 | 2012-02-07 16:54:11 -0800 | [diff] [blame] | 1785 | from_kuid_munged(user_ns, sma->sem_perm.uid), |
| 1786 | from_kgid_munged(user_ns, sma->sem_perm.gid), |
| 1787 | from_kuid_munged(user_ns, sma->sem_perm.cuid), |
| 1788 | from_kgid_munged(user_ns, sma->sem_perm.cgid), |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 1789 | sma->sem_otime, |
| 1790 | sma->sem_ctime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | } |
| 1792 | #endif |