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. |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 50 | * - semncnt and semzcnt are calculated on demand in count_semcnt() |
Manfred Spraul | c5cf635 | 2010-05-26 14:43:43 -0700 | [diff] [blame] | 51 | * - the task that performs a successful semop() scans the list of all |
| 52 | * sleeping tasks and completes any pending operations that can be fulfilled. |
| 53 | * Semaphores are actively given to waiting tasks (necessary for FIFO). |
| 54 | * (see update_queue()) |
| 55 | * - To improve the scalability, the actual wake-up calls are performed after |
| 56 | * dropping all locks. (see wake_up_sem_queue_prepare(), |
| 57 | * wake_up_sem_queue_do()) |
| 58 | * - All work is done by the waker, the woken up task does not have to do |
| 59 | * anything - not even acquiring a lock or dropping a refcount. |
| 60 | * - A woken up task may not even touch the semaphore array anymore, it may |
| 61 | * have been destroyed already by a semctl(RMID). |
| 62 | * - The synchronizations between wake-ups due to a timeout/signal and a |
| 63 | * wake-up due to a completed semaphore operation is achieved by using an |
| 64 | * intermediate state (IN_WAKEUP). |
| 65 | * - UNDO values are stored in an array (one per process and per |
| 66 | * semaphore array, lazily allocated). For backwards compatibility, multiple |
| 67 | * modes for the UNDO variables are supported (per process, per thread) |
| 68 | * (see copy_semundo, CLONE_SYSVSEM) |
| 69 | * - There are two lists of the pending operations: a per-array list |
| 70 | * and per-semaphore list (stored in the array). This allows to achieve FIFO |
| 71 | * ordering without always scanning all pending operations. |
| 72 | * The worst-case behavior is nevertheless O(N^2) for N wakeups. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #include <linux/slab.h> |
| 76 | #include <linux/spinlock.h> |
| 77 | #include <linux/init.h> |
| 78 | #include <linux/proc_fs.h> |
| 79 | #include <linux/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #include <linux/security.h> |
| 81 | #include <linux/syscalls.h> |
| 82 | #include <linux/audit.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 83 | #include <linux/capability.h> |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 84 | #include <linux/seq_file.h> |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 85 | #include <linux/rwsem.h> |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 86 | #include <linux/nsproxy.h> |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 87 | #include <linux/ipc_namespace.h> |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 88 | |
Paul McQuade | 7153e40 | 2014-06-06 14:37:37 -0700 | [diff] [blame] | 89 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | #include "util.h" |
| 91 | |
Manfred Spraul | e57940d | 2011-11-02 13:38:54 -0700 | [diff] [blame] | 92 | /* One semaphore structure for each semaphore in the system. */ |
| 93 | struct sem { |
| 94 | int semval; /* current value */ |
Davidlohr Bueso | a5f4db8 | 2016-03-22 14:27:48 -0700 | [diff] [blame] | 95 | /* |
| 96 | * PID of the process that last modified the semaphore. For |
| 97 | * Linux, specifically these are: |
| 98 | * - semop |
| 99 | * - semctl, via SETVAL and SETALL. |
| 100 | * - at task exit when performing undo adjustments (see exit_sem). |
| 101 | */ |
| 102 | int sempid; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 103 | spinlock_t lock; /* spinlock for fine-grained semtimedop */ |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 104 | struct list_head pending_alter; /* pending single-sop operations */ |
| 105 | /* that alter the semaphore */ |
| 106 | struct list_head pending_const; /* pending single-sop operations */ |
| 107 | /* that do not alter the semaphore*/ |
Manfred Spraul | d12e1e5 | 2013-07-08 16:01:25 -0700 | [diff] [blame] | 108 | time_t sem_otime; /* candidate for sem_otime */ |
Manfred Spraul | f5c936c | 2013-07-08 16:01:22 -0700 | [diff] [blame] | 109 | } ____cacheline_aligned_in_smp; |
Manfred Spraul | e57940d | 2011-11-02 13:38:54 -0700 | [diff] [blame] | 110 | |
| 111 | /* One queue for each sleeping process in the system. */ |
| 112 | struct sem_queue { |
Manfred Spraul | e57940d | 2011-11-02 13:38:54 -0700 | [diff] [blame] | 113 | struct list_head list; /* queue of pending operations */ |
| 114 | struct task_struct *sleeper; /* this process */ |
| 115 | struct sem_undo *undo; /* undo structure */ |
| 116 | int pid; /* process id of requesting process */ |
| 117 | int status; /* completion status of operation */ |
| 118 | struct sembuf *sops; /* array of pending operations */ |
Manfred Spraul | ed247b7 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 119 | struct sembuf *blocking; /* the operation that blocked */ |
Manfred Spraul | e57940d | 2011-11-02 13:38:54 -0700 | [diff] [blame] | 120 | int nsops; /* number of operations */ |
| 121 | int alter; /* does *sops alter the array? */ |
| 122 | }; |
| 123 | |
| 124 | /* Each task has a list of undo requests. They are executed automatically |
| 125 | * when the process exits. |
| 126 | */ |
| 127 | struct sem_undo { |
| 128 | struct list_head list_proc; /* per-process list: * |
| 129 | * all undos from one process |
| 130 | * rcu protected */ |
| 131 | struct rcu_head rcu; /* rcu struct for sem_undo */ |
| 132 | struct sem_undo_list *ulp; /* back ptr to sem_undo_list */ |
| 133 | struct list_head list_id; /* per semaphore array list: |
| 134 | * all undos for one array */ |
| 135 | int semid; /* semaphore set identifier */ |
| 136 | short *semadj; /* array of adjustments */ |
| 137 | /* one per semaphore */ |
| 138 | }; |
| 139 | |
| 140 | /* sem_undo_list controls shared access to the list of sem_undo structures |
| 141 | * that may be shared among all a CLONE_SYSVSEM task group. |
| 142 | */ |
| 143 | struct sem_undo_list { |
| 144 | atomic_t refcnt; |
| 145 | spinlock_t lock; |
| 146 | struct list_head list_proc; |
| 147 | }; |
| 148 | |
| 149 | |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 150 | #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Nadia Derbey | 1b531f2 | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 152 | #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 154 | static int newary(struct ipc_namespace *, struct ipc_params *); |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 155 | static void freeary(struct ipc_namespace *, struct kern_ipc_perm *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 157 | static int sysvipc_sem_proc_show(struct seq_file *s, void *it); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | #endif |
| 159 | |
| 160 | #define SEMMSL_FAST 256 /* 512 bytes on stack */ |
| 161 | #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */ |
| 162 | |
| 163 | /* |
Manfred Spraul | 758a6ba3 | 2013-07-08 16:01:26 -0700 | [diff] [blame] | 164 | * Locking: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | * sem_undo.id_next, |
Manfred Spraul | 758a6ba3 | 2013-07-08 16:01:26 -0700 | [diff] [blame] | 166 | * sem_array.complex_count, |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 167 | * sem_array.pending{_alter,_cont}, |
Manfred Spraul | 758a6ba3 | 2013-07-08 16:01:26 -0700 | [diff] [blame] | 168 | * sem_array.sem_undo: global sem_lock() for read/write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | * sem_undo.proc_next: only "current" is allowed to read/write that field. |
Paul McQuade | 46c0a8c | 2014-06-06 14:37:37 -0700 | [diff] [blame] | 170 | * |
Manfred Spraul | 758a6ba3 | 2013-07-08 16:01:26 -0700 | [diff] [blame] | 171 | * sem_array.sem_base[i].pending_{const,alter}: |
| 172 | * global or semaphore sem_lock() for read/write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | */ |
| 174 | |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 175 | #define sc_semmsl sem_ctls[0] |
| 176 | #define sc_semmns sem_ctls[1] |
| 177 | #define sc_semopm sem_ctls[2] |
| 178 | #define sc_semmni sem_ctls[3] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 180 | void sem_init_ns(struct ipc_namespace *ns) |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 181 | { |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 182 | ns->sc_semmsl = SEMMSL; |
| 183 | ns->sc_semmns = SEMMNS; |
| 184 | ns->sc_semopm = SEMOPM; |
| 185 | ns->sc_semmni = SEMMNI; |
| 186 | ns->used_sems = 0; |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 187 | ipc_init_ids(&ns->ids[IPC_SEM_IDS]); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 190 | #ifdef CONFIG_IPC_NS |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 191 | void sem_exit_ns(struct ipc_namespace *ns) |
| 192 | { |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 193 | free_ipcs(ns, &sem_ids(ns), freeary); |
Serge E. Hallyn | 7d6feeb | 2009-12-15 16:47:27 -0800 | [diff] [blame] | 194 | idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 195 | } |
Pavel Emelyanov | ae5e1b2 | 2008-02-08 04:18:22 -0800 | [diff] [blame] | 196 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 198 | void __init sem_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Pierre Peiffer | ed2ddbf | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 200 | sem_init_ns(&init_ipc_ns); |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 201 | ipc_init_proc_interface("sysvipc/sem", |
| 202 | " key semid perms nsems uid gid cuid cgid otime ctime\n", |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 203 | IPC_SEM_IDS, sysvipc_sem_proc_show); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Manfred Spraul | f269f40 | 2013-07-08 16:01:24 -0700 | [diff] [blame] | 206 | /** |
| 207 | * unmerge_queues - unmerge queues, if possible. |
| 208 | * @sma: semaphore array |
| 209 | * |
| 210 | * The function unmerges the wait queues if complex_count is 0. |
| 211 | * It must be called prior to dropping the global semaphore array lock. |
| 212 | */ |
| 213 | static void unmerge_queues(struct sem_array *sma) |
| 214 | { |
| 215 | struct sem_queue *q, *tq; |
| 216 | |
| 217 | /* complex operations still around? */ |
| 218 | if (sma->complex_count) |
| 219 | return; |
| 220 | /* |
| 221 | * We will switch back to simple mode. |
| 222 | * Move all pending operation back into the per-semaphore |
| 223 | * queues. |
| 224 | */ |
| 225 | list_for_each_entry_safe(q, tq, &sma->pending_alter, list) { |
| 226 | struct sem *curr; |
| 227 | curr = &sma->sem_base[q->sops[0].sem_num]; |
| 228 | |
| 229 | list_add_tail(&q->list, &curr->pending_alter); |
| 230 | } |
| 231 | INIT_LIST_HEAD(&sma->pending_alter); |
| 232 | } |
| 233 | |
| 234 | /** |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 235 | * merge_queues - merge single semop queues into global queue |
Manfred Spraul | f269f40 | 2013-07-08 16:01:24 -0700 | [diff] [blame] | 236 | * @sma: semaphore array |
| 237 | * |
| 238 | * This function merges all per-semaphore queues into the global queue. |
| 239 | * It is necessary to achieve FIFO ordering for the pending single-sop |
| 240 | * operations when a multi-semop operation must sleep. |
| 241 | * Only the alter operations must be moved, the const operations can stay. |
| 242 | */ |
| 243 | static void merge_queues(struct sem_array *sma) |
| 244 | { |
| 245 | int i; |
| 246 | for (i = 0; i < sma->sem_nsems; i++) { |
| 247 | struct sem *sem = sma->sem_base + i; |
| 248 | |
| 249 | list_splice_init(&sem->pending_alter, &sma->pending_alter); |
| 250 | } |
| 251 | } |
| 252 | |
Davidlohr Bueso | 53dad6d | 2013-09-23 17:04:45 -0700 | [diff] [blame] | 253 | static void sem_rcu_free(struct rcu_head *head) |
| 254 | { |
| 255 | struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu); |
| 256 | struct sem_array *sma = ipc_rcu_to_struct(p); |
| 257 | |
| 258 | security_sem_free(sma); |
| 259 | ipc_rcu_free(head); |
| 260 | } |
| 261 | |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 262 | /* |
Manfred Spraul | 5e9d527 | 2013-09-30 13:45:04 -0700 | [diff] [blame] | 263 | * Wait until all currently ongoing simple ops have completed. |
| 264 | * Caller must own sem_perm.lock. |
| 265 | * New simple ops cannot start, because simple ops first check |
| 266 | * that sem_perm.lock is free. |
Manfred Spraul | 6d07b68 | 2013-09-30 13:45:06 -0700 | [diff] [blame] | 267 | * that a) sem_perm.lock is free and b) complex_count is 0. |
Manfred Spraul | 5e9d527 | 2013-09-30 13:45:04 -0700 | [diff] [blame] | 268 | */ |
| 269 | static void sem_wait_array(struct sem_array *sma) |
| 270 | { |
| 271 | int i; |
| 272 | struct sem *sem; |
| 273 | |
Manfred Spraul | 6d07b68 | 2013-09-30 13:45:06 -0700 | [diff] [blame] | 274 | if (sma->complex_count) { |
| 275 | /* The thread that increased sma->complex_count waited on |
| 276 | * all sem->lock locks. Thus we don't need to wait again. |
| 277 | */ |
| 278 | return; |
| 279 | } |
| 280 | |
Manfred Spraul | 5e9d527 | 2013-09-30 13:45:04 -0700 | [diff] [blame] | 281 | for (i = 0; i < sma->sem_nsems; i++) { |
| 282 | sem = sma->sem_base + i; |
| 283 | spin_unlock_wait(&sem->lock); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /* |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 288 | * If the request contains only one semaphore operation, and there are |
| 289 | * no complex transactions pending, lock only the semaphore involved. |
| 290 | * Otherwise, lock the entire semaphore array, since we either have |
| 291 | * multiple semaphores in our own semops, or we need to look at |
| 292 | * semaphores from other pending complex operations. |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 293 | */ |
| 294 | static inline int sem_lock(struct sem_array *sma, struct sembuf *sops, |
| 295 | int nsops) |
| 296 | { |
Manfred Spraul | 5e9d527 | 2013-09-30 13:45:04 -0700 | [diff] [blame] | 297 | struct sem *sem; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 298 | |
Manfred Spraul | 5e9d527 | 2013-09-30 13:45:04 -0700 | [diff] [blame] | 299 | if (nsops != 1) { |
| 300 | /* Complex operation - acquire a full lock */ |
| 301 | ipc_lock_object(&sma->sem_perm); |
| 302 | |
| 303 | /* And wait until all simple ops that are processed |
| 304 | * right now have dropped their locks. |
| 305 | */ |
| 306 | sem_wait_array(sma); |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Only one semaphore affected - try to optimize locking. |
| 312 | * The rules are: |
| 313 | * - optimized locking is possible if no complex operation |
| 314 | * is either enqueued or processed right now. |
| 315 | * - The test for enqueued complex ops is simple: |
| 316 | * sma->complex_count != 0 |
| 317 | * - Testing for complex ops that are processed right now is |
| 318 | * a bit more difficult. Complex ops acquire the full lock |
| 319 | * and first wait that the running simple ops have completed. |
| 320 | * (see above) |
| 321 | * Thus: If we own a simple lock and the global lock is free |
| 322 | * and complex_count is now 0, then it will stay 0 and |
| 323 | * thus just locking sem->lock is sufficient. |
| 324 | */ |
| 325 | sem = sma->sem_base + sops->sem_num; |
| 326 | |
| 327 | if (sma->complex_count == 0) { |
| 328 | /* |
| 329 | * It appears that no complex operation is around. |
| 330 | * Acquire the per-semaphore lock. |
| 331 | */ |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 332 | spin_lock(&sem->lock); |
| 333 | |
Manfred Spraul | 5e9d527 | 2013-09-30 13:45:04 -0700 | [diff] [blame] | 334 | /* Then check that the global lock is free */ |
| 335 | if (!spin_is_locked(&sma->sem_perm.lock)) { |
Manfred Spraul | 2e094ab | 2014-12-12 16:58:11 -0800 | [diff] [blame] | 336 | /* |
Manfred Spraul | 3ed1f8a | 2015-08-14 15:35:10 -0700 | [diff] [blame] | 337 | * We need a memory barrier with acquire semantics, |
| 338 | * otherwise we can race with another thread that does: |
Manfred Spraul | 2e094ab | 2014-12-12 16:58:11 -0800 | [diff] [blame] | 339 | * complex_count++; |
| 340 | * spin_unlock(sem_perm.lock); |
| 341 | */ |
Peter Zijlstra | 33ac279 | 2016-05-24 13:17:12 +0200 | [diff] [blame] | 342 | smp_acquire__after_ctrl_dep(); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 343 | |
Manfred Spraul | 2e094ab | 2014-12-12 16:58:11 -0800 | [diff] [blame] | 344 | /* |
| 345 | * Now repeat the test of complex_count: |
Manfred Spraul | 5e9d527 | 2013-09-30 13:45:04 -0700 | [diff] [blame] | 346 | * It can't change anymore until we drop sem->lock. |
| 347 | * Thus: if is now 0, then it will stay 0. |
| 348 | */ |
| 349 | if (sma->complex_count == 0) { |
| 350 | /* fast path successful! */ |
| 351 | return sops->sem_num; |
| 352 | } |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 353 | } |
Manfred Spraul | 5e9d527 | 2013-09-30 13:45:04 -0700 | [diff] [blame] | 354 | spin_unlock(&sem->lock); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 355 | } |
Manfred Spraul | 5e9d527 | 2013-09-30 13:45:04 -0700 | [diff] [blame] | 356 | |
| 357 | /* slow path: acquire the full lock */ |
| 358 | ipc_lock_object(&sma->sem_perm); |
| 359 | |
| 360 | if (sma->complex_count == 0) { |
| 361 | /* False alarm: |
| 362 | * There is no complex operation, thus we can switch |
| 363 | * back to the fast path. |
| 364 | */ |
| 365 | spin_lock(&sem->lock); |
| 366 | ipc_unlock_object(&sma->sem_perm); |
| 367 | return sops->sem_num; |
| 368 | } else { |
| 369 | /* Not a false alarm, thus complete the sequence for a |
| 370 | * full lock. |
| 371 | */ |
| 372 | sem_wait_array(sma); |
| 373 | return -1; |
| 374 | } |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static inline void sem_unlock(struct sem_array *sma, int locknum) |
| 378 | { |
| 379 | if (locknum == -1) { |
Manfred Spraul | f269f40 | 2013-07-08 16:01:24 -0700 | [diff] [blame] | 380 | unmerge_queues(sma); |
Davidlohr Bueso | cf9d5d7 | 2013-07-08 16:01:11 -0700 | [diff] [blame] | 381 | ipc_unlock_object(&sma->sem_perm); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 382 | } else { |
| 383 | struct sem *sem = sma->sem_base + locknum; |
| 384 | spin_unlock(&sem->lock); |
| 385 | } |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 389 | * sem_lock_(check_) routines are called in the paths where the rwsem |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 390 | * is not held. |
Linus Torvalds | 321310c | 2013-05-04 10:47:57 -0700 | [diff] [blame] | 391 | * |
| 392 | * The caller holds the RCU read lock. |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 393 | */ |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 394 | static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns, |
| 395 | int id, struct sembuf *sops, int nsops, int *locknum) |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 396 | { |
Rik van Riel | c460b66 | 2013-04-30 19:15:35 -0700 | [diff] [blame] | 397 | struct kern_ipc_perm *ipcp; |
| 398 | struct sem_array *sma; |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 399 | |
Davidlohr Bueso | 55b7ae5 | 2015-06-30 14:58:42 -0700 | [diff] [blame] | 400 | ipcp = ipc_obtain_object_idr(&sem_ids(ns), id); |
Linus Torvalds | 321310c | 2013-05-04 10:47:57 -0700 | [diff] [blame] | 401 | if (IS_ERR(ipcp)) |
| 402 | return ERR_CAST(ipcp); |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 403 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 404 | sma = container_of(ipcp, struct sem_array, sem_perm); |
| 405 | *locknum = sem_lock(sma, sops, nsops); |
Rik van Riel | c460b66 | 2013-04-30 19:15:35 -0700 | [diff] [blame] | 406 | |
| 407 | /* ipc_rmid() may have already freed the ID while sem_lock |
| 408 | * was spinning: verify that the structure is still valid |
| 409 | */ |
Rafael Aquini | 72a8ff2 | 2014-01-27 17:07:02 -0800 | [diff] [blame] | 410 | if (ipc_valid_object(ipcp)) |
Rik van Riel | c460b66 | 2013-04-30 19:15:35 -0700 | [diff] [blame] | 411 | return container_of(ipcp, struct sem_array, sem_perm); |
| 412 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 413 | sem_unlock(sma, *locknum); |
Linus Torvalds | 321310c | 2013-05-04 10:47:57 -0700 | [diff] [blame] | 414 | return ERR_PTR(-EINVAL); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 417 | static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id) |
| 418 | { |
Davidlohr Bueso | 55b7ae5 | 2015-06-30 14:58:42 -0700 | [diff] [blame] | 419 | struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id); |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 420 | |
| 421 | if (IS_ERR(ipcp)) |
| 422 | return ERR_CAST(ipcp); |
| 423 | |
| 424 | return container_of(ipcp, struct sem_array, sem_perm); |
| 425 | } |
| 426 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 427 | static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns, |
| 428 | int id) |
| 429 | { |
| 430 | struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id); |
| 431 | |
| 432 | if (IS_ERR(ipcp)) |
| 433 | return ERR_CAST(ipcp); |
Pierre Peiffer | b1ed88b | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 434 | |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 435 | return container_of(ipcp, struct sem_array, sem_perm); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 438 | static inline void sem_lock_and_putref(struct sem_array *sma) |
| 439 | { |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 440 | sem_lock(sma, NULL, -1); |
Fabian Frederick | 9b24fef | 2016-08-02 14:03:07 -0700 | [diff] [blame] | 441 | ipc_rcu_putref(sma, sem_rcu_free); |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 444 | static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s) |
| 445 | { |
| 446 | ipc_rmid(&sem_ids(ns), &s->sem_perm); |
| 447 | } |
| 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | /* |
| 450 | * Lockless wakeup algorithm: |
| 451 | * Without the check/retry algorithm a lockless wakeup is possible: |
| 452 | * - queue.status is initialized to -EINTR before blocking. |
| 453 | * - wakeup is performed by |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 454 | * * unlinking the queue entry from the pending list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | * * setting queue.status to IN_WAKEUP |
| 456 | * This is the notification for the blocked thread that a |
| 457 | * result value is imminent. |
| 458 | * * call wake_up_process |
| 459 | * * set queue.status to the final value. |
| 460 | * - the previously blocked thread checks queue.status: |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 461 | * * if it's IN_WAKEUP, then it must wait until the value changes |
| 462 | * * if it's not -EINTR, then the operation was completed by |
| 463 | * update_queue. semtimedop can return queue.status without |
| 464 | * performing any operation on the sem array. |
| 465 | * * otherwise it must acquire the spinlock and check what's up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | * |
| 467 | * The two-stage algorithm is necessary to protect against the following |
| 468 | * races: |
| 469 | * - if queue.status is set after wake_up_process, then the woken up idle |
| 470 | * thread could race forward and try (and fail) to acquire sma->lock |
| 471 | * before update_queue had a chance to set queue.status |
| 472 | * - if queue.status is written before wake_up_process and if the |
| 473 | * blocked process is woken up by a signal between writing |
| 474 | * queue.status and the wake_up_process, then the woken up |
| 475 | * process could return from semtimedop and die by calling |
| 476 | * sys_exit before wake_up_process is called. Then wake_up_process |
| 477 | * will oops, because the task structure is already invalid. |
| 478 | * (yes, this happened on s390 with sysv msg). |
| 479 | * |
| 480 | */ |
| 481 | #define IN_WAKEUP 1 |
| 482 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 483 | /** |
| 484 | * newary - Create a new semaphore set |
| 485 | * @ns: namespace |
| 486 | * @params: ptr to the structure that contains key, semflg and nsems |
| 487 | * |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 488 | * Called with sem_ids.rwsem held (as a writer) |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 489 | */ |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 490 | static int newary(struct ipc_namespace *ns, struct ipc_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
| 492 | int id; |
| 493 | int retval; |
| 494 | struct sem_array *sma; |
| 495 | int size; |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 496 | key_t key = params->key; |
| 497 | int nsems = params->u.nsems; |
| 498 | int semflg = params->flg; |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 499 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | if (!nsems) |
| 502 | return -EINVAL; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 503 | if (ns->used_sems + nsems > ns->sc_semmns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | return -ENOSPC; |
| 505 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 506 | size = sizeof(*sma) + nsems * sizeof(struct sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | sma = ipc_rcu_alloc(size); |
Davidlohr Bueso | 3ab08fe | 2014-01-27 17:07:06 -0800 | [diff] [blame] | 508 | if (!sma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | return -ENOMEM; |
Davidlohr Bueso | 3ab08fe | 2014-01-27 17:07:06 -0800 | [diff] [blame] | 510 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 511 | memset(sma, 0, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
| 513 | sma->sem_perm.mode = (semflg & S_IRWXUGO); |
| 514 | sma->sem_perm.key = key; |
| 515 | |
| 516 | sma->sem_perm.security = NULL; |
| 517 | retval = security_sem_alloc(sma); |
| 518 | if (retval) { |
Davidlohr Bueso | 53dad6d | 2013-09-23 17:04:45 -0700 | [diff] [blame] | 519 | ipc_rcu_putref(sma, ipc_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return retval; |
| 521 | } |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | sma->sem_base = (struct sem *) &sma[1]; |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 524 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 525 | for (i = 0; i < nsems; i++) { |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 526 | INIT_LIST_HEAD(&sma->sem_base[i].pending_alter); |
| 527 | INIT_LIST_HEAD(&sma->sem_base[i].pending_const); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 528 | spin_lock_init(&sma->sem_base[i].lock); |
| 529 | } |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 530 | |
| 531 | sma->complex_count = 0; |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 532 | INIT_LIST_HEAD(&sma->pending_alter); |
| 533 | INIT_LIST_HEAD(&sma->pending_const); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 534 | INIT_LIST_HEAD(&sma->list_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | sma->sem_nsems = nsems; |
| 536 | sma->sem_ctime = get_seconds(); |
Manfred Spraul | e8577d1 | 2014-12-02 15:59:34 -0800 | [diff] [blame] | 537 | |
| 538 | id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni); |
| 539 | if (id < 0) { |
| 540 | ipc_rcu_putref(sma, sem_rcu_free); |
| 541 | return id; |
| 542 | } |
| 543 | ns->used_sems += nsems; |
| 544 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 545 | sem_unlock(sma, -1); |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 546 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 548 | return sma->sem_perm.id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 551 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 552 | /* |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 553 | * Called with sem_ids.rwsem and ipcp locked. |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 554 | */ |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 555 | static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 557 | struct sem_array *sma; |
| 558 | |
| 559 | sma = container_of(ipcp, struct sem_array, sem_perm); |
| 560 | return security_sem_associate(sma, semflg); |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 563 | /* |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 564 | * Called with sem_ids.rwsem and ipcp locked. |
Nadia Derbey | f4566f0 | 2007-10-18 23:40:53 -0700 | [diff] [blame] | 565 | */ |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 566 | static inline int sem_more_checks(struct kern_ipc_perm *ipcp, |
| 567 | struct ipc_params *params) |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 568 | { |
Nadia Derbey | 03f02c7 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 569 | struct sem_array *sma; |
| 570 | |
| 571 | sma = container_of(ipcp, struct sem_array, sem_perm); |
| 572 | if (params->u.nsems > sma->sem_nsems) |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 573 | return -EINVAL; |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 578 | SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg) |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 579 | { |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 580 | struct ipc_namespace *ns; |
Mathias Krause | eb66ec4 | 2014-06-06 14:37:36 -0700 | [diff] [blame] | 581 | static const struct ipc_ops sem_ops = { |
| 582 | .getnew = newary, |
| 583 | .associate = sem_security, |
| 584 | .more_checks = sem_more_checks, |
| 585 | }; |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 586 | struct ipc_params sem_params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 588 | ns = current->nsproxy->ipc_ns; |
| 589 | |
| 590 | if (nsems < 0 || nsems > ns->sc_semmsl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | return -EINVAL; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 592 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 593 | sem_params.key = key; |
| 594 | sem_params.flg = semflg; |
| 595 | sem_params.u.nsems = nsems; |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 596 | |
Nadia Derbey | 7748dbf | 2007-10-18 23:40:49 -0700 | [diff] [blame] | 597 | return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Petr Mladek | 78f5009 | 2014-01-27 17:07:00 -0800 | [diff] [blame] | 600 | /** |
| 601 | * perform_atomic_semop - Perform (if possible) a semaphore operation |
Manfred Spraul | 758a6ba3 | 2013-07-08 16:01:26 -0700 | [diff] [blame] | 602 | * @sma: semaphore array |
Manfred Spraul | d198cd6 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 603 | * @q: struct sem_queue that describes the operation |
Manfred Spraul | 758a6ba3 | 2013-07-08 16:01:26 -0700 | [diff] [blame] | 604 | * |
| 605 | * Returns 0 if the operation was possible. |
| 606 | * Returns 1 if the operation is impossible, the caller must sleep. |
| 607 | * Negative values are error codes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | */ |
Manfred Spraul | d198cd6 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 609 | static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Manfred Spraul | d198cd6 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 611 | int result, sem_op, nsops, pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | struct sembuf *sop; |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 613 | struct sem *curr; |
Manfred Spraul | d198cd6 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 614 | struct sembuf *sops; |
| 615 | struct sem_undo *un; |
| 616 | |
| 617 | sops = q->sops; |
| 618 | nsops = q->nsops; |
| 619 | un = q->undo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
| 621 | for (sop = sops; sop < sops + nsops; sop++) { |
| 622 | curr = sma->sem_base + sop->sem_num; |
| 623 | sem_op = sop->sem_op; |
| 624 | result = curr->semval; |
Petr Mladek | 78f5009 | 2014-01-27 17:07:00 -0800 | [diff] [blame] | 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | if (!sem_op && result) |
| 627 | goto would_block; |
| 628 | |
| 629 | result += sem_op; |
| 630 | if (result < 0) |
| 631 | goto would_block; |
| 632 | if (result > SEMVMX) |
| 633 | goto out_of_range; |
Petr Mladek | 78f5009 | 2014-01-27 17:07:00 -0800 | [diff] [blame] | 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | if (sop->sem_flg & SEM_UNDO) { |
| 636 | int undo = un->semadj[sop->sem_num] - sem_op; |
Petr Mladek | 78f5009 | 2014-01-27 17:07:00 -0800 | [diff] [blame] | 637 | /* Exceeding the undo range is an error. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | if (undo < (-SEMAEM - 1) || undo > SEMAEM) |
| 639 | goto out_of_range; |
Petr Mladek | 78f5009 | 2014-01-27 17:07:00 -0800 | [diff] [blame] | 640 | un->semadj[sop->sem_num] = undo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
Petr Mladek | 78f5009 | 2014-01-27 17:07:00 -0800 | [diff] [blame] | 642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | curr->semval = result; |
| 644 | } |
| 645 | |
| 646 | sop--; |
Manfred Spraul | d198cd6 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 647 | pid = q->pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | while (sop >= sops) { |
| 649 | sma->sem_base[sop->sem_num].sempid = pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | sop--; |
| 651 | } |
Petr Mladek | 78f5009 | 2014-01-27 17:07:00 -0800 | [diff] [blame] | 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | return 0; |
| 654 | |
| 655 | out_of_range: |
| 656 | result = -ERANGE; |
| 657 | goto undo; |
| 658 | |
| 659 | would_block: |
Manfred Spraul | ed247b7 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 660 | q->blocking = sop; |
| 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | if (sop->sem_flg & IPC_NOWAIT) |
| 663 | result = -EAGAIN; |
| 664 | else |
| 665 | result = 1; |
| 666 | |
| 667 | undo: |
| 668 | sop--; |
| 669 | while (sop >= sops) { |
Petr Mladek | 78f5009 | 2014-01-27 17:07:00 -0800 | [diff] [blame] | 670 | sem_op = sop->sem_op; |
| 671 | sma->sem_base[sop->sem_num].semval -= sem_op; |
| 672 | if (sop->sem_flg & SEM_UNDO) |
| 673 | un->semadj[sop->sem_num] += sem_op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | sop--; |
| 675 | } |
| 676 | |
| 677 | return result; |
| 678 | } |
| 679 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 680 | /** wake_up_sem_queue_prepare(q, error): Prepare wake-up |
| 681 | * @q: queue entry that must be signaled |
| 682 | * @error: Error value for the signal |
| 683 | * |
| 684 | * Prepare the wake-up of the queue entry q. |
Nick Piggin | d421209 | 2009-12-15 16:47:30 -0800 | [diff] [blame] | 685 | */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 686 | static void wake_up_sem_queue_prepare(struct list_head *pt, |
| 687 | struct sem_queue *q, int error) |
Nick Piggin | d421209 | 2009-12-15 16:47:30 -0800 | [diff] [blame] | 688 | { |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 689 | if (list_empty(pt)) { |
| 690 | /* |
| 691 | * Hold preempt off so that we don't get preempted and have the |
| 692 | * wakee busy-wait until we're scheduled back on. |
| 693 | */ |
| 694 | preempt_disable(); |
| 695 | } |
Nick Piggin | d421209 | 2009-12-15 16:47:30 -0800 | [diff] [blame] | 696 | q->status = IN_WAKEUP; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 697 | q->pid = error; |
| 698 | |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 699 | list_add_tail(&q->list, pt); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /** |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 703 | * wake_up_sem_queue_do - do the actual wake-up |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 704 | * @pt: list of tasks to be woken up |
| 705 | * |
| 706 | * Do the actual wake-up. |
| 707 | * The function is called without any locks held, thus the semaphore array |
| 708 | * could be destroyed already and the tasks can disappear as soon as the |
| 709 | * status is set to the actual return code. |
| 710 | */ |
| 711 | static void wake_up_sem_queue_do(struct list_head *pt) |
| 712 | { |
| 713 | struct sem_queue *q, *t; |
| 714 | int did_something; |
| 715 | |
| 716 | did_something = !list_empty(pt); |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 717 | list_for_each_entry_safe(q, t, pt, list) { |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 718 | wake_up_process(q->sleeper); |
| 719 | /* q can disappear immediately after writing q->status. */ |
| 720 | smp_wmb(); |
| 721 | q->status = q->pid; |
| 722 | } |
| 723 | if (did_something) |
| 724 | preempt_enable(); |
Nick Piggin | d421209 | 2009-12-15 16:47:30 -0800 | [diff] [blame] | 725 | } |
| 726 | |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 727 | static void unlink_queue(struct sem_array *sma, struct sem_queue *q) |
| 728 | { |
| 729 | list_del(&q->list); |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 730 | if (q->nsops > 1) |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 731 | sma->complex_count--; |
| 732 | } |
| 733 | |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 734 | /** check_restart(sma, q) |
| 735 | * @sma: semaphore array |
| 736 | * @q: the operation that just completed |
| 737 | * |
| 738 | * update_queue is O(N^2) when it restarts scanning the whole queue of |
| 739 | * waiting operations. Therefore this function checks if the restart is |
| 740 | * really necessary. It is called after a previously waiting operation |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 741 | * modified the array. |
| 742 | * Note that wait-for-zero operations are handled without restart. |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 743 | */ |
| 744 | static int check_restart(struct sem_array *sma, struct sem_queue *q) |
| 745 | { |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 746 | /* pending complex alter operations are too difficult to analyse */ |
| 747 | if (!list_empty(&sma->pending_alter)) |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 748 | return 1; |
| 749 | |
| 750 | /* we were a sleeping complex operation. Too difficult */ |
| 751 | if (q->nsops > 1) |
| 752 | return 1; |
| 753 | |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 754 | /* It is impossible that someone waits for the new value: |
| 755 | * - complex operations always restart. |
| 756 | * - wait-for-zero are handled seperately. |
| 757 | * - q is a previously sleeping simple operation that |
| 758 | * altered the array. It must be a decrement, because |
| 759 | * simple increments never sleep. |
| 760 | * - If there are older (higher priority) decrements |
| 761 | * in the queue, then they have observed the original |
| 762 | * semval value and couldn't proceed. The operation |
| 763 | * decremented to value - thus they won't proceed either. |
| 764 | */ |
| 765 | return 0; |
| 766 | } |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 767 | |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 768 | /** |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 769 | * wake_const_ops - wake up non-alter tasks |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 770 | * @sma: semaphore array. |
| 771 | * @semnum: semaphore that was modified. |
| 772 | * @pt: list head for the tasks that must be woken up. |
| 773 | * |
| 774 | * wake_const_ops must be called after a semaphore in a semaphore array |
| 775 | * was set to 0. If complex const operations are pending, wake_const_ops must |
| 776 | * be called with semnum = -1, as well as with the number of each modified |
| 777 | * semaphore. |
| 778 | * The tasks that must be woken up are added to @pt. The return code |
| 779 | * is stored in q->pid. |
| 780 | * The function returns 1 if at least one operation was completed successfully. |
| 781 | */ |
| 782 | static int wake_const_ops(struct sem_array *sma, int semnum, |
| 783 | struct list_head *pt) |
| 784 | { |
| 785 | struct sem_queue *q; |
| 786 | struct list_head *walk; |
| 787 | struct list_head *pending_list; |
| 788 | int semop_completed = 0; |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 789 | |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 790 | if (semnum == -1) |
| 791 | pending_list = &sma->pending_const; |
| 792 | else |
| 793 | pending_list = &sma->sem_base[semnum].pending_const; |
| 794 | |
| 795 | walk = pending_list->next; |
| 796 | while (walk != pending_list) { |
| 797 | int error; |
| 798 | |
| 799 | q = container_of(walk, struct sem_queue, list); |
| 800 | walk = walk->next; |
| 801 | |
Manfred Spraul | d198cd6 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 802 | error = perform_atomic_semop(sma, q); |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 803 | |
| 804 | if (error <= 0) { |
| 805 | /* operation completed, remove from queue & wakeup */ |
| 806 | |
| 807 | unlink_queue(sma, q); |
| 808 | |
| 809 | wake_up_sem_queue_prepare(pt, q, error); |
| 810 | if (error == 0) |
| 811 | semop_completed = 1; |
| 812 | } |
| 813 | } |
| 814 | return semop_completed; |
| 815 | } |
| 816 | |
| 817 | /** |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 818 | * do_smart_wakeup_zero - wakeup all wait for zero tasks |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 819 | * @sma: semaphore array |
| 820 | * @sops: operations that were performed |
| 821 | * @nsops: number of operations |
| 822 | * @pt: list head of the tasks that must be woken up. |
| 823 | * |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 824 | * Checks all required queue for wait-for-zero operations, based |
| 825 | * on the actual changes that were performed on the semaphore array. |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 826 | * The function returns 1 if at least one operation was completed successfully. |
| 827 | */ |
| 828 | static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops, |
| 829 | int nsops, struct list_head *pt) |
| 830 | { |
| 831 | int i; |
| 832 | int semop_completed = 0; |
| 833 | int got_zero = 0; |
| 834 | |
| 835 | /* first: the per-semaphore queues, if known */ |
| 836 | if (sops) { |
| 837 | for (i = 0; i < nsops; i++) { |
| 838 | int num = sops[i].sem_num; |
| 839 | |
| 840 | if (sma->sem_base[num].semval == 0) { |
| 841 | got_zero = 1; |
| 842 | semop_completed |= wake_const_ops(sma, num, pt); |
| 843 | } |
| 844 | } |
| 845 | } else { |
| 846 | /* |
| 847 | * No sops means modified semaphores not known. |
| 848 | * Assume all were changed. |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 849 | */ |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 850 | for (i = 0; i < sma->sem_nsems; i++) { |
| 851 | if (sma->sem_base[i].semval == 0) { |
| 852 | got_zero = 1; |
| 853 | semop_completed |= wake_const_ops(sma, i, pt); |
| 854 | } |
| 855 | } |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 856 | } |
| 857 | /* |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 858 | * If one of the modified semaphores got 0, |
| 859 | * then check the global queue, too. |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 860 | */ |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 861 | if (got_zero) |
| 862 | semop_completed |= wake_const_ops(sma, -1, pt); |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 863 | |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 864 | return semop_completed; |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 867 | |
| 868 | /** |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 869 | * update_queue - look for tasks that can be completed. |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 870 | * @sma: semaphore array. |
| 871 | * @semnum: semaphore that was modified. |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 872 | * @pt: list head for the tasks that must be woken up. |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 873 | * |
| 874 | * update_queue must be called after a semaphore in a semaphore array |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 875 | * was modified. If multiple semaphores were modified, update_queue must |
| 876 | * be called with semnum = -1, as well as with the number of each modified |
| 877 | * semaphore. |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 878 | * The tasks that must be woken up are added to @pt. The return code |
| 879 | * is stored in q->pid. |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 880 | * The function internally checks if const operations can now succeed. |
| 881 | * |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 882 | * The function return 1 if at least one semop was completed successfully. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 884 | 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] | 885 | { |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 886 | struct sem_queue *q; |
| 887 | struct list_head *walk; |
| 888 | struct list_head *pending_list; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 889 | int semop_completed = 0; |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 890 | |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 891 | if (semnum == -1) |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 892 | pending_list = &sma->pending_alter; |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 893 | else |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 894 | pending_list = &sma->sem_base[semnum].pending_alter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Nick Piggin | 9cad200 | 2009-12-15 16:47:29 -0800 | [diff] [blame] | 896 | again: |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 897 | walk = pending_list->next; |
| 898 | while (walk != pending_list) { |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 899 | int error, restart; |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 900 | |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 901 | q = container_of(walk, struct sem_queue, list); |
Manfred Spraul | 636c6be | 2009-12-15 16:47:33 -0800 | [diff] [blame] | 902 | walk = walk->next; |
Nick Piggin | 9cad200 | 2009-12-15 16:47:29 -0800 | [diff] [blame] | 903 | |
Manfred Spraul | d987f8b | 2009-12-15 16:47:34 -0800 | [diff] [blame] | 904 | /* If we are scanning the single sop, per-semaphore list of |
| 905 | * one semaphore and that semaphore is 0, then it is not |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 906 | * necessary to scan further: simple increments |
Manfred Spraul | d987f8b | 2009-12-15 16:47:34 -0800 | [diff] [blame] | 907 | * that affect only one entry succeed immediately and cannot |
| 908 | * be in the per semaphore pending queue, and decrements |
| 909 | * cannot be successful if the value is already 0. |
| 910 | */ |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 911 | if (semnum != -1 && sma->sem_base[semnum].semval == 0) |
Manfred Spraul | d987f8b | 2009-12-15 16:47:34 -0800 | [diff] [blame] | 912 | break; |
| 913 | |
Manfred Spraul | d198cd6 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 914 | error = perform_atomic_semop(sma, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
| 916 | /* Does q->sleeper still need to sleep? */ |
Nick Piggin | 9cad200 | 2009-12-15 16:47:29 -0800 | [diff] [blame] | 917 | if (error > 0) |
| 918 | continue; |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 919 | |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 920 | unlink_queue(sma, q); |
Manfred Spraul | a1193f8 | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 921 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 922 | if (error) { |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 923 | restart = 0; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 924 | } else { |
| 925 | semop_completed = 1; |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 926 | do_smart_wakeup_zero(sma, q->sops, q->nsops, pt); |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 927 | restart = check_restart(sma, q); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 928 | } |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 929 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 930 | wake_up_sem_queue_prepare(pt, q, error); |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 931 | if (restart) |
Nick Piggin | 9cad200 | 2009-12-15 16:47:29 -0800 | [diff] [blame] | 932 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 934 | return semop_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 937 | /** |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 938 | * set_semotime - set sem_otime |
Manfred Spraul | 0e8c665 | 2013-09-30 13:45:25 -0700 | [diff] [blame] | 939 | * @sma: semaphore array |
| 940 | * @sops: operations that modified the array, may be NULL |
| 941 | * |
| 942 | * sem_otime is replicated to avoid cache line trashing. |
| 943 | * This function sets one instance to the current time. |
| 944 | */ |
| 945 | static void set_semotime(struct sem_array *sma, struct sembuf *sops) |
| 946 | { |
| 947 | if (sops == NULL) { |
| 948 | sma->sem_base[0].sem_otime = get_seconds(); |
| 949 | } else { |
| 950 | sma->sem_base[sops[0].sem_num].sem_otime = |
| 951 | get_seconds(); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | /** |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 956 | * do_smart_update - optimized update_queue |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 957 | * @sma: semaphore array |
| 958 | * @sops: operations that were performed |
| 959 | * @nsops: number of operations |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 960 | * @otime: force setting otime |
| 961 | * @pt: list head of the tasks that must be woken up. |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 962 | * |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 963 | * do_smart_update() does the required calls to update_queue and wakeup_zero, |
| 964 | * based on the actual changes that were performed on the semaphore array. |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 965 | * Note that the function does not do the actual wake-up: the caller is |
| 966 | * responsible for calling wake_up_sem_queue_do(@pt). |
| 967 | * It is safe to perform this call after dropping all locks. |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 968 | */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 969 | static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops, |
| 970 | int otime, struct list_head *pt) |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 971 | { |
| 972 | int i; |
| 973 | |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 974 | otime |= do_smart_wakeup_zero(sma, sops, nsops, pt); |
| 975 | |
Manfred Spraul | f269f40 | 2013-07-08 16:01:24 -0700 | [diff] [blame] | 976 | if (!list_empty(&sma->pending_alter)) { |
| 977 | /* semaphore array uses the global queue - just process it. */ |
| 978 | otime |= update_queue(sma, -1, pt); |
| 979 | } else { |
| 980 | if (!sops) { |
| 981 | /* |
| 982 | * No sops, thus the modified semaphores are not |
| 983 | * known. Check all. |
| 984 | */ |
| 985 | for (i = 0; i < sma->sem_nsems; i++) |
| 986 | otime |= update_queue(sma, i, pt); |
| 987 | } else { |
| 988 | /* |
| 989 | * Check the semaphores that were increased: |
| 990 | * - No complex ops, thus all sleeping ops are |
| 991 | * decrease. |
| 992 | * - if we decreased the value, then any sleeping |
| 993 | * semaphore ops wont be able to run: If the |
| 994 | * previous value was too small, then the new |
| 995 | * value will be too small, too. |
| 996 | */ |
| 997 | for (i = 0; i < nsops; i++) { |
| 998 | if (sops[i].sem_op > 0) { |
| 999 | otime |= update_queue(sma, |
| 1000 | sops[i].sem_num, pt); |
| 1001 | } |
Manfred Spraul | ab465df | 2013-05-26 11:08:52 +0200 | [diff] [blame] | 1002 | } |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 1003 | } |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 1004 | } |
Manfred Spraul | 0e8c665 | 2013-09-30 13:45:25 -0700 | [diff] [blame] | 1005 | if (otime) |
| 1006 | set_semotime(sma, sops); |
Manfred Spraul | fd5db42 | 2010-05-26 14:43:40 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1009 | /* |
Manfred Spraul | b220c57 | 2014-06-06 14:37:51 -0700 | [diff] [blame] | 1010 | * check_qop: Test if a queued operation sleeps on the semaphore semnum |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1011 | */ |
| 1012 | static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q, |
| 1013 | bool count_zero) |
| 1014 | { |
Manfred Spraul | b220c57 | 2014-06-06 14:37:51 -0700 | [diff] [blame] | 1015 | struct sembuf *sop = q->blocking; |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1016 | |
Manfred Spraul | 9b44ee2 | 2014-06-06 14:37:52 -0700 | [diff] [blame] | 1017 | /* |
| 1018 | * Linux always (since 0.99.10) reported a task as sleeping on all |
| 1019 | * semaphores. This violates SUS, therefore it was changed to the |
| 1020 | * standard compliant behavior. |
| 1021 | * Give the administrators a chance to notice that an application |
| 1022 | * might misbehave because it relies on the Linux behavior. |
| 1023 | */ |
| 1024 | pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n" |
| 1025 | "The task %s (%d) triggered the difference, watch for misbehavior.\n", |
| 1026 | current->comm, task_pid_nr(current)); |
| 1027 | |
Manfred Spraul | b220c57 | 2014-06-06 14:37:51 -0700 | [diff] [blame] | 1028 | if (sop->sem_num != semnum) |
| 1029 | return 0; |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1030 | |
Manfred Spraul | b220c57 | 2014-06-06 14:37:51 -0700 | [diff] [blame] | 1031 | if (count_zero && sop->sem_op == 0) |
| 1032 | return 1; |
| 1033 | if (!count_zero && sop->sem_op < 0) |
| 1034 | return 1; |
| 1035 | |
| 1036 | return 0; |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | /* The following counts are associated to each semaphore: |
| 1040 | * semncnt number of tasks waiting on semval being nonzero |
| 1041 | * semzcnt number of tasks waiting on semval being zero |
Manfred Spraul | b220c57 | 2014-06-06 14:37:51 -0700 | [diff] [blame] | 1042 | * |
| 1043 | * Per definition, a task waits only on the semaphore of the first semop |
| 1044 | * that cannot proceed, even if additional operation would block, too. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | */ |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1046 | static int count_semcnt(struct sem_array *sma, ushort semnum, |
| 1047 | bool count_zero) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | { |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1049 | struct list_head *l; |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1050 | struct sem_queue *q; |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1051 | int semcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1053 | semcnt = 0; |
| 1054 | /* First: check the simple operations. They are easy to evaluate */ |
| 1055 | if (count_zero) |
| 1056 | l = &sma->sem_base[semnum].pending_const; |
| 1057 | else |
| 1058 | l = &sma->sem_base[semnum].pending_alter; |
| 1059 | |
| 1060 | list_for_each_entry(q, l, list) { |
| 1061 | /* all task on a per-semaphore list sleep on exactly |
| 1062 | * that semaphore |
| 1063 | */ |
| 1064 | semcnt++; |
Rik van Riel | de2657f | 2013-05-09 16:59:59 -0400 | [diff] [blame] | 1065 | } |
| 1066 | |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1067 | /* Then: check the complex operations. */ |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 1068 | list_for_each_entry(q, &sma->pending_alter, list) { |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1069 | semcnt += check_qop(sma, semnum, q, count_zero); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | } |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1071 | if (count_zero) { |
| 1072 | list_for_each_entry(q, &sma->pending_const, list) { |
| 1073 | semcnt += check_qop(sma, semnum, q, count_zero); |
| 1074 | } |
Rik van Riel | ebc2e5e | 2013-05-09 16:53:28 -0400 | [diff] [blame] | 1075 | } |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1076 | return semcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 1079 | /* Free a semaphore set. freeary() is called with sem_ids.rwsem locked |
| 1080 | * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem |
Nadia Derbey | 3e148c7 | 2007-10-18 23:40:54 -0700 | [diff] [blame] | 1081 | * remains locked on exit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | */ |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 1083 | static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | { |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1085 | struct sem_undo *un, *tu; |
| 1086 | struct sem_queue *q, *tq; |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 1087 | struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1088 | struct list_head tasks; |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 1089 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1091 | /* Free the existing undo structures for this semaphore set. */ |
Davidlohr Bueso | cf9d5d7 | 2013-07-08 16:01:11 -0700 | [diff] [blame] | 1092 | ipc_assert_locked_object(&sma->sem_perm); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1093 | list_for_each_entry_safe(un, tu, &sma->list_id, list_id) { |
| 1094 | list_del(&un->list_id); |
| 1095 | spin_lock(&un->ulp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | un->semid = -1; |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1097 | list_del_rcu(&un->list_proc); |
| 1098 | spin_unlock(&un->ulp->lock); |
Lai Jiangshan | 693a8b6 | 2011-03-18 12:09:35 +0800 | [diff] [blame] | 1099 | kfree_rcu(un, rcu); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
| 1102 | /* Wake up all pending processes and let them fail with EIDRM. */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1103 | INIT_LIST_HEAD(&tasks); |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 1104 | list_for_each_entry_safe(q, tq, &sma->pending_const, list) { |
| 1105 | unlink_queue(sma, q); |
| 1106 | wake_up_sem_queue_prepare(&tasks, q, -EIDRM); |
| 1107 | } |
| 1108 | |
| 1109 | list_for_each_entry_safe(q, tq, &sma->pending_alter, list) { |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 1110 | unlink_queue(sma, q); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1111 | wake_up_sem_queue_prepare(&tasks, q, -EIDRM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | } |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 1113 | for (i = 0; i < sma->sem_nsems; i++) { |
| 1114 | struct sem *sem = sma->sem_base + i; |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 1115 | list_for_each_entry_safe(q, tq, &sem->pending_const, list) { |
| 1116 | unlink_queue(sma, q); |
| 1117 | wake_up_sem_queue_prepare(&tasks, q, -EIDRM); |
| 1118 | } |
| 1119 | list_for_each_entry_safe(q, tq, &sem->pending_alter, list) { |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 1120 | unlink_queue(sma, q); |
| 1121 | wake_up_sem_queue_prepare(&tasks, q, -EIDRM); |
| 1122 | } |
| 1123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 1125 | /* Remove the semaphore set from the IDR */ |
| 1126 | sem_rmid(ns, sma); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1127 | sem_unlock(sma, -1); |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 1128 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1130 | wake_up_sem_queue_do(&tasks); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1131 | ns->used_sems -= sma->sem_nsems; |
Davidlohr Bueso | 53dad6d | 2013-09-23 17:04:45 -0700 | [diff] [blame] | 1132 | ipc_rcu_putref(sma, sem_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version) |
| 1136 | { |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1137 | switch (version) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | case IPC_64: |
| 1139 | return copy_to_user(buf, in, sizeof(*in)); |
| 1140 | case IPC_OLD: |
| 1141 | { |
| 1142 | struct semid_ds out; |
| 1143 | |
Dan Rosenberg | 982f7c2 | 2010-09-30 15:15:31 -0700 | [diff] [blame] | 1144 | memset(&out, 0, sizeof(out)); |
| 1145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm); |
| 1147 | |
| 1148 | out.sem_otime = in->sem_otime; |
| 1149 | out.sem_ctime = in->sem_ctime; |
| 1150 | out.sem_nsems = in->sem_nsems; |
| 1151 | |
| 1152 | return copy_to_user(buf, &out, sizeof(out)); |
| 1153 | } |
| 1154 | default: |
| 1155 | return -EINVAL; |
| 1156 | } |
| 1157 | } |
| 1158 | |
Manfred Spraul | d12e1e5 | 2013-07-08 16:01:25 -0700 | [diff] [blame] | 1159 | static time_t get_semotime(struct sem_array *sma) |
| 1160 | { |
| 1161 | int i; |
| 1162 | time_t res; |
| 1163 | |
| 1164 | res = sma->sem_base[0].sem_otime; |
| 1165 | for (i = 1; i < sma->sem_nsems; i++) { |
| 1166 | time_t to = sma->sem_base[i].sem_otime; |
| 1167 | |
| 1168 | if (to > res) |
| 1169 | res = to; |
| 1170 | } |
| 1171 | return res; |
| 1172 | } |
| 1173 | |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 1174 | static int semctl_nolock(struct ipc_namespace *ns, int semid, |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1175 | int cmd, int version, void __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
Amerigo Wang | e5cc9c7 | 2009-12-15 16:47:35 -0800 | [diff] [blame] | 1177 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | struct sem_array *sma; |
| 1179 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1180 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | case IPC_INFO: |
| 1182 | case SEM_INFO: |
| 1183 | { |
| 1184 | struct seminfo seminfo; |
| 1185 | int max_id; |
| 1186 | |
| 1187 | err = security_sem_semctl(NULL, cmd); |
| 1188 | if (err) |
| 1189 | return err; |
Paul McQuade | 46c0a8c | 2014-06-06 14:37:37 -0700 | [diff] [blame] | 1190 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1191 | memset(&seminfo, 0, sizeof(seminfo)); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1192 | seminfo.semmni = ns->sc_semmni; |
| 1193 | seminfo.semmns = ns->sc_semmns; |
| 1194 | seminfo.semmsl = ns->sc_semmsl; |
| 1195 | seminfo.semopm = ns->sc_semopm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | seminfo.semvmx = SEMVMX; |
| 1197 | seminfo.semmnu = SEMMNU; |
| 1198 | seminfo.semmap = SEMMAP; |
| 1199 | seminfo.semume = SEMUME; |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 1200 | down_read(&sem_ids(ns).rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | if (cmd == SEM_INFO) { |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1202 | seminfo.semusz = sem_ids(ns).in_use; |
| 1203 | seminfo.semaem = ns->used_sems; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } else { |
| 1205 | seminfo.semusz = SEMUSZ; |
| 1206 | seminfo.semaem = SEMAEM; |
| 1207 | } |
Nadia Derbey | 7ca7e56 | 2007-10-18 23:40:48 -0700 | [diff] [blame] | 1208 | max_id = ipc_get_maxid(&sem_ids(ns)); |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 1209 | up_read(&sem_ids(ns).rwsem); |
Paul McQuade | 46c0a8c | 2014-06-06 14:37:37 -0700 | [diff] [blame] | 1210 | if (copy_to_user(p, &seminfo, sizeof(struct seminfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | return -EFAULT; |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1212 | return (max_id < 0) ? 0 : max_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | } |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 1214 | case IPC_STAT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | case SEM_STAT: |
| 1216 | { |
| 1217 | struct semid64_ds tbuf; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1218 | int id = 0; |
| 1219 | |
| 1220 | memset(&tbuf, 0, sizeof(tbuf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
Linus Torvalds | 941b030 | 2013-05-04 11:04:29 -0700 | [diff] [blame] | 1222 | rcu_read_lock(); |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 1223 | if (cmd == SEM_STAT) { |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1224 | sma = sem_obtain_object(ns, semid); |
| 1225 | if (IS_ERR(sma)) { |
| 1226 | err = PTR_ERR(sma); |
| 1227 | goto out_unlock; |
| 1228 | } |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 1229 | id = sma->sem_perm.id; |
| 1230 | } else { |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1231 | sma = sem_obtain_object_check(ns, semid); |
| 1232 | if (IS_ERR(sma)) { |
| 1233 | err = PTR_ERR(sma); |
| 1234 | goto out_unlock; |
| 1235 | } |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 1236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | |
| 1238 | err = -EACCES; |
Serge E. Hallyn | b0e7759 | 2011-03-23 16:43:24 -0700 | [diff] [blame] | 1239 | if (ipcperms(ns, &sma->sem_perm, S_IRUGO)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | goto out_unlock; |
| 1241 | |
| 1242 | err = security_sem_semctl(sma, cmd); |
| 1243 | if (err) |
| 1244 | goto out_unlock; |
| 1245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm); |
Manfred Spraul | d12e1e5 | 2013-07-08 16:01:25 -0700 | [diff] [blame] | 1247 | tbuf.sem_otime = get_semotime(sma); |
| 1248 | tbuf.sem_ctime = sma->sem_ctime; |
| 1249 | tbuf.sem_nsems = sma->sem_nsems; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1250 | rcu_read_unlock(); |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1251 | if (copy_semid_to_user(p, &tbuf, version)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | return -EFAULT; |
| 1253 | return id; |
| 1254 | } |
| 1255 | default: |
| 1256 | return -EINVAL; |
| 1257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | out_unlock: |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1259 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | return err; |
| 1261 | } |
| 1262 | |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1263 | static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum, |
| 1264 | unsigned long arg) |
| 1265 | { |
| 1266 | struct sem_undo *un; |
| 1267 | struct sem_array *sma; |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1268 | struct sem *curr; |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1269 | int err; |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1270 | struct list_head tasks; |
| 1271 | int val; |
| 1272 | #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN) |
| 1273 | /* big-endian 64bit */ |
| 1274 | val = arg >> 32; |
| 1275 | #else |
| 1276 | /* 32bit or little-endian 64bit */ |
| 1277 | val = arg; |
| 1278 | #endif |
| 1279 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1280 | if (val > SEMVMX || val < 0) |
| 1281 | return -ERANGE; |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1282 | |
| 1283 | INIT_LIST_HEAD(&tasks); |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1284 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1285 | rcu_read_lock(); |
| 1286 | sma = sem_obtain_object_check(ns, semid); |
| 1287 | if (IS_ERR(sma)) { |
| 1288 | rcu_read_unlock(); |
| 1289 | return PTR_ERR(sma); |
| 1290 | } |
| 1291 | |
| 1292 | if (semnum < 0 || semnum >= sma->sem_nsems) { |
| 1293 | rcu_read_unlock(); |
| 1294 | return -EINVAL; |
| 1295 | } |
| 1296 | |
| 1297 | |
| 1298 | if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) { |
| 1299 | rcu_read_unlock(); |
| 1300 | return -EACCES; |
| 1301 | } |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1302 | |
| 1303 | err = security_sem_semctl(sma, SETVAL); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1304 | if (err) { |
| 1305 | rcu_read_unlock(); |
| 1306 | return -EACCES; |
| 1307 | } |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1308 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1309 | sem_lock(sma, NULL, -1); |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1310 | |
Rafael Aquini | 0f3d2b0 | 2014-01-27 17:07:01 -0800 | [diff] [blame] | 1311 | if (!ipc_valid_object(&sma->sem_perm)) { |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 1312 | sem_unlock(sma, -1); |
| 1313 | rcu_read_unlock(); |
| 1314 | return -EIDRM; |
| 1315 | } |
| 1316 | |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1317 | curr = &sma->sem_base[semnum]; |
| 1318 | |
Davidlohr Bueso | cf9d5d7 | 2013-07-08 16:01:11 -0700 | [diff] [blame] | 1319 | ipc_assert_locked_object(&sma->sem_perm); |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1320 | list_for_each_entry(un, &sma->list_id, list_id) |
| 1321 | un->semadj[semnum] = 0; |
| 1322 | |
| 1323 | curr->semval = val; |
| 1324 | curr->sempid = task_tgid_vnr(current); |
| 1325 | sma->sem_ctime = get_seconds(); |
| 1326 | /* maybe some queued-up processes were waiting for this */ |
| 1327 | do_smart_update(sma, NULL, 0, 0, &tasks); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1328 | sem_unlock(sma, -1); |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 1329 | rcu_read_unlock(); |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1330 | wake_up_sem_queue_do(&tasks); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1331 | return 0; |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1332 | } |
| 1333 | |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1334 | static int semctl_main(struct ipc_namespace *ns, int semid, int semnum, |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1335 | int cmd, void __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | { |
| 1337 | struct sem_array *sma; |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1338 | struct sem *curr; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1339 | int err, nsems; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | ushort fast_sem_io[SEMMSL_FAST]; |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1341 | ushort *sem_io = fast_sem_io; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1342 | struct list_head tasks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1344 | INIT_LIST_HEAD(&tasks); |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1345 | |
| 1346 | rcu_read_lock(); |
| 1347 | sma = sem_obtain_object_check(ns, semid); |
| 1348 | if (IS_ERR(sma)) { |
| 1349 | rcu_read_unlock(); |
| 1350 | return PTR_ERR(sma); |
| 1351 | } |
| 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | nsems = sma->sem_nsems; |
| 1354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | err = -EACCES; |
Linus Torvalds | c728b9c | 2013-05-04 11:04:29 -0700 | [diff] [blame] | 1356 | if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO)) |
| 1357 | goto out_rcu_wakeup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | |
| 1359 | err = security_sem_semctl(sma, cmd); |
Linus Torvalds | c728b9c | 2013-05-04 11:04:29 -0700 | [diff] [blame] | 1360 | if (err) |
| 1361 | goto out_rcu_wakeup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | err = -EACCES; |
| 1364 | switch (cmd) { |
| 1365 | case GETALL: |
| 1366 | { |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1367 | ushort __user *array = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | int i; |
| 1369 | |
Al Viro | ce85722 | 2013-05-03 00:30:49 +0100 | [diff] [blame] | 1370 | sem_lock(sma, NULL, -1); |
Rafael Aquini | 0f3d2b0 | 2014-01-27 17:07:01 -0800 | [diff] [blame] | 1371 | if (!ipc_valid_object(&sma->sem_perm)) { |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 1372 | err = -EIDRM; |
| 1373 | goto out_unlock; |
| 1374 | } |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1375 | if (nsems > SEMMSL_FAST) { |
Al Viro | ce85722 | 2013-05-03 00:30:49 +0100 | [diff] [blame] | 1376 | if (!ipc_rcu_getref(sma)) { |
Al Viro | ce85722 | 2013-05-03 00:30:49 +0100 | [diff] [blame] | 1377 | err = -EIDRM; |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 1378 | goto out_unlock; |
Al Viro | ce85722 | 2013-05-03 00:30:49 +0100 | [diff] [blame] | 1379 | } |
| 1380 | sem_unlock(sma, -1); |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 1381 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | sem_io = ipc_alloc(sizeof(ushort)*nsems); |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1383 | if (sem_io == NULL) { |
Fabian Frederick | 9b24fef | 2016-08-02 14:03:07 -0700 | [diff] [blame] | 1384 | ipc_rcu_putref(sma, sem_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | return -ENOMEM; |
| 1386 | } |
| 1387 | |
Linus Torvalds | 4091fd94 | 2013-05-04 10:13:40 -0700 | [diff] [blame] | 1388 | rcu_read_lock(); |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1389 | sem_lock_and_putref(sma); |
Rafael Aquini | 0f3d2b0 | 2014-01-27 17:07:01 -0800 | [diff] [blame] | 1390 | if (!ipc_valid_object(&sma->sem_perm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | err = -EIDRM; |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 1392 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | } |
Al Viro | ce85722 | 2013-05-03 00:30:49 +0100 | [diff] [blame] | 1394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | for (i = 0; i < sma->sem_nsems; i++) |
| 1396 | sem_io[i] = sma->sem_base[i].semval; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1397 | sem_unlock(sma, -1); |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 1398 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | err = 0; |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1400 | if (copy_to_user(array, sem_io, nsems*sizeof(ushort))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | err = -EFAULT; |
| 1402 | goto out_free; |
| 1403 | } |
| 1404 | case SETALL: |
| 1405 | { |
| 1406 | int i; |
| 1407 | struct sem_undo *un; |
| 1408 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1409 | if (!ipc_rcu_getref(sma)) { |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 1410 | err = -EIDRM; |
| 1411 | goto out_rcu_wakeup; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1412 | } |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1413 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1415 | if (nsems > SEMMSL_FAST) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | sem_io = ipc_alloc(sizeof(ushort)*nsems); |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1417 | if (sem_io == NULL) { |
Fabian Frederick | 9b24fef | 2016-08-02 14:03:07 -0700 | [diff] [blame] | 1418 | ipc_rcu_putref(sma, sem_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | return -ENOMEM; |
| 1420 | } |
| 1421 | } |
| 1422 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1423 | if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) { |
Fabian Frederick | 9b24fef | 2016-08-02 14:03:07 -0700 | [diff] [blame] | 1424 | ipc_rcu_putref(sma, sem_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | err = -EFAULT; |
| 1426 | goto out_free; |
| 1427 | } |
| 1428 | |
| 1429 | for (i = 0; i < nsems; i++) { |
| 1430 | if (sem_io[i] > SEMVMX) { |
Fabian Frederick | 9b24fef | 2016-08-02 14:03:07 -0700 | [diff] [blame] | 1431 | ipc_rcu_putref(sma, sem_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | err = -ERANGE; |
| 1433 | goto out_free; |
| 1434 | } |
| 1435 | } |
Linus Torvalds | 4091fd94 | 2013-05-04 10:13:40 -0700 | [diff] [blame] | 1436 | rcu_read_lock(); |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1437 | sem_lock_and_putref(sma); |
Rafael Aquini | 0f3d2b0 | 2014-01-27 17:07:01 -0800 | [diff] [blame] | 1438 | if (!ipc_valid_object(&sma->sem_perm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | err = -EIDRM; |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 1440 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Davidlohr Bueso | a5f4db8 | 2016-03-22 14:27:48 -0700 | [diff] [blame] | 1443 | for (i = 0; i < nsems; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | sma->sem_base[i].semval = sem_io[i]; |
Davidlohr Bueso | a5f4db8 | 2016-03-22 14:27:48 -0700 | [diff] [blame] | 1445 | sma->sem_base[i].sempid = task_tgid_vnr(current); |
| 1446 | } |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1447 | |
Davidlohr Bueso | cf9d5d7 | 2013-07-08 16:01:11 -0700 | [diff] [blame] | 1448 | ipc_assert_locked_object(&sma->sem_perm); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1449 | list_for_each_entry(un, &sma->list_id, list_id) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | for (i = 0; i < nsems; i++) |
| 1451 | un->semadj[i] = 0; |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1452 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | sma->sem_ctime = get_seconds(); |
| 1454 | /* maybe some queued-up processes were waiting for this */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1455 | do_smart_update(sma, NULL, 0, 0, &tasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | err = 0; |
| 1457 | goto out_unlock; |
| 1458 | } |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1459 | /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | } |
| 1461 | err = -EINVAL; |
Linus Torvalds | c728b9c | 2013-05-04 11:04:29 -0700 | [diff] [blame] | 1462 | if (semnum < 0 || semnum >= nsems) |
| 1463 | goto out_rcu_wakeup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1465 | sem_lock(sma, NULL, -1); |
Rafael Aquini | 0f3d2b0 | 2014-01-27 17:07:01 -0800 | [diff] [blame] | 1466 | if (!ipc_valid_object(&sma->sem_perm)) { |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 1467 | err = -EIDRM; |
| 1468 | goto out_unlock; |
| 1469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | curr = &sma->sem_base[semnum]; |
| 1471 | |
| 1472 | switch (cmd) { |
| 1473 | case GETVAL: |
| 1474 | err = curr->semval; |
| 1475 | goto out_unlock; |
| 1476 | case GETPID: |
| 1477 | err = curr->sempid; |
| 1478 | goto out_unlock; |
| 1479 | case GETNCNT: |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1480 | err = count_semcnt(sma, semnum, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | goto out_unlock; |
| 1482 | case GETZCNT: |
Manfred Spraul | 2f2ed41d | 2014-06-06 14:37:48 -0700 | [diff] [blame] | 1483 | err = count_semcnt(sma, semnum, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | } |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | out_unlock: |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1488 | sem_unlock(sma, -1); |
Linus Torvalds | c728b9c | 2013-05-04 11:04:29 -0700 | [diff] [blame] | 1489 | out_rcu_wakeup: |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 1490 | rcu_read_unlock(); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1491 | wake_up_sem_queue_do(&tasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | out_free: |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1493 | if (sem_io != fast_sem_io) |
Tetsuo Handa | 1d5cfdb | 2016-01-22 15:11:02 -0800 | [diff] [blame] | 1494 | ipc_free(sem_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | return err; |
| 1496 | } |
| 1497 | |
Pierre Peiffer | 016d713 | 2008-04-29 01:00:50 -0700 | [diff] [blame] | 1498 | static inline unsigned long |
| 1499 | 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] | 1500 | { |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1501 | switch (version) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | case IPC_64: |
Pierre Peiffer | 016d713 | 2008-04-29 01:00:50 -0700 | [diff] [blame] | 1503 | if (copy_from_user(out, buf, sizeof(*out))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | case IPC_OLD: |
| 1507 | { |
| 1508 | struct semid_ds tbuf_old; |
| 1509 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1510 | if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | return -EFAULT; |
| 1512 | |
Pierre Peiffer | 016d713 | 2008-04-29 01:00:50 -0700 | [diff] [blame] | 1513 | out->sem_perm.uid = tbuf_old.sem_perm.uid; |
| 1514 | out->sem_perm.gid = tbuf_old.sem_perm.gid; |
| 1515 | out->sem_perm.mode = tbuf_old.sem_perm.mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | default: |
| 1520 | return -EINVAL; |
| 1521 | } |
| 1522 | } |
| 1523 | |
Pierre Peiffer | 522bb2a | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1524 | /* |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 1525 | * This function handles some semctl commands which require the rwsem |
Pierre Peiffer | 522bb2a | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1526 | * to be held in write mode. |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 1527 | * NOTE: no locks must be held, the rwsem is taken inside this function. |
Pierre Peiffer | 522bb2a | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1528 | */ |
Pierre Peiffer | 21a4826 | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1529 | static int semctl_down(struct ipc_namespace *ns, int semid, |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1530 | int cmd, int version, void __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | { |
| 1532 | struct sem_array *sma; |
| 1533 | int err; |
Pierre Peiffer | 016d713 | 2008-04-29 01:00:50 -0700 | [diff] [blame] | 1534 | struct semid64_ds semid64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | struct kern_ipc_perm *ipcp; |
| 1536 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1537 | if (cmd == IPC_SET) { |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1538 | if (copy_semid_from_user(&semid64, p, version)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 1542 | down_write(&sem_ids(ns).rwsem); |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1543 | rcu_read_lock(); |
| 1544 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1545 | ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd, |
| 1546 | &semid64.sem_perm, 0); |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1547 | if (IS_ERR(ipcp)) { |
| 1548 | err = PTR_ERR(ipcp); |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1549 | goto out_unlock1; |
| 1550 | } |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1551 | |
Pierre Peiffer | a5f75e7 | 2008-04-29 01:00:54 -0700 | [diff] [blame] | 1552 | sma = container_of(ipcp, struct sem_array, sem_perm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
| 1554 | err = security_sem_semctl(sma, cmd); |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1555 | if (err) |
| 1556 | goto out_unlock1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1558 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | case IPC_RMID: |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1560 | sem_lock(sma, NULL, -1); |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1561 | /* freeary unlocks the ipc object and rcu */ |
Pierre Peiffer | 01b8b07 | 2008-02-08 04:18:57 -0800 | [diff] [blame] | 1562 | freeary(ns, ipcp); |
Pierre Peiffer | 522bb2a | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1563 | goto out_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | case IPC_SET: |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1565 | sem_lock(sma, NULL, -1); |
Eric W. Biederman | 1efdb69 | 2012-02-07 16:54:11 -0800 | [diff] [blame] | 1566 | err = ipc_update_perm(&semid64.sem_perm, ipcp); |
| 1567 | if (err) |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1568 | goto out_unlock0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | sma->sem_ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | break; |
| 1571 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | err = -EINVAL; |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1573 | goto out_unlock1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1576 | out_unlock0: |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1577 | sem_unlock(sma, -1); |
Davidlohr Bueso | 7b4cc5d | 2013-07-08 16:01:12 -0700 | [diff] [blame] | 1578 | out_unlock1: |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 1579 | rcu_read_unlock(); |
Pierre Peiffer | 522bb2a | 2008-04-29 01:00:49 -0700 | [diff] [blame] | 1580 | out_up: |
Davidlohr Bueso | d9a605e | 2013-09-11 14:26:24 -0700 | [diff] [blame] | 1581 | up_write(&sem_ids(ns).rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | return err; |
| 1583 | } |
| 1584 | |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1585 | SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | int version; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1588 | struct ipc_namespace *ns; |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1589 | void __user *p = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
| 1591 | if (semid < 0) |
| 1592 | return -EINVAL; |
| 1593 | |
| 1594 | version = ipc_parse_version(&cmd); |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1595 | ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1597 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | case IPC_INFO: |
| 1599 | case SEM_INFO: |
Pierre Peiffer | 4b9fcb0 | 2008-02-08 04:18:56 -0800 | [diff] [blame] | 1600 | case IPC_STAT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | case SEM_STAT: |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1602 | return semctl_nolock(ns, semid, cmd, version, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | case GETALL: |
| 1604 | case GETVAL: |
| 1605 | case GETPID: |
| 1606 | case GETNCNT: |
| 1607 | case GETZCNT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | case SETALL: |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1609 | return semctl_main(ns, semid, semnum, cmd, p); |
| 1610 | case SETVAL: |
| 1611 | return semctl_setval(ns, semid, semnum, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | case IPC_RMID: |
| 1613 | case IPC_SET: |
Al Viro | e1fd1f4 | 2013-03-05 15:04:55 -0500 | [diff] [blame] | 1614 | return semctl_down(ns, semid, cmd, version, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | default: |
| 1616 | return -EINVAL; |
| 1617 | } |
| 1618 | } |
| 1619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | /* If the task doesn't already have a undo_list, then allocate one |
| 1621 | * here. We guarantee there is only one thread using this undo list, |
| 1622 | * and current is THE ONE |
| 1623 | * |
| 1624 | * If this allocation and assignment succeeds, but later |
| 1625 | * portions of this code fail, there is no need to free the sem_undo_list. |
| 1626 | * Just let it stay associated with the task, and it'll be freed later |
| 1627 | * at exit time. |
| 1628 | * |
| 1629 | * This can block, so callers must hold no locks. |
| 1630 | */ |
| 1631 | static inline int get_undo_list(struct sem_undo_list **undo_listp) |
| 1632 | { |
| 1633 | struct sem_undo_list *undo_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | |
| 1635 | undo_list = current->sysvsem.undo_list; |
| 1636 | if (!undo_list) { |
Matt Helsley | 2453a30 | 2006-10-02 02:18:25 -0700 | [diff] [blame] | 1637 | undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | if (undo_list == NULL) |
| 1639 | return -ENOMEM; |
Ingo Molnar | 00a5dfd | 2005-08-05 23:05:27 +0200 | [diff] [blame] | 1640 | spin_lock_init(&undo_list->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | atomic_set(&undo_list->refcnt, 1); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1642 | INIT_LIST_HEAD(&undo_list->list_proc); |
| 1643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | current->sysvsem.undo_list = undo_list; |
| 1645 | } |
| 1646 | *undo_listp = undo_list; |
| 1647 | return 0; |
| 1648 | } |
| 1649 | |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1650 | 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] | 1651 | { |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1652 | struct sem_undo *un; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1654 | list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) { |
| 1655 | if (un->semid == semid) |
| 1656 | return un; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | } |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1658 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1661 | static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid) |
| 1662 | { |
| 1663 | struct sem_undo *un; |
| 1664 | |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1665 | assert_spin_locked(&ulp->lock); |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 1666 | |
| 1667 | un = __lookup_undo(ulp, semid); |
| 1668 | if (un) { |
| 1669 | list_del_rcu(&un->list_proc); |
| 1670 | list_add_rcu(&un->list_proc, &ulp->list_proc); |
| 1671 | } |
| 1672 | return un; |
| 1673 | } |
| 1674 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1675 | /** |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 1676 | * find_alloc_undo - lookup (and if not present create) undo array |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1677 | * @ns: namespace |
| 1678 | * @semid: semaphore array id |
| 1679 | * |
| 1680 | * The function looks up (and if not present creates) the undo structure. |
| 1681 | * The size of the undo structure depends on the size of the semaphore |
| 1682 | * array, thus the alloc path is not that straightforward. |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1683 | * Lifetime-rules: sem_undo is rcu-protected, on success, the function |
| 1684 | * performs a rcu_read_lock(). |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1685 | */ |
| 1686 | 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] | 1687 | { |
| 1688 | struct sem_array *sma; |
| 1689 | struct sem_undo_list *ulp; |
| 1690 | struct sem_undo *un, *new; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1691 | int nsems, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | |
| 1693 | error = get_undo_list(&ulp); |
| 1694 | if (error) |
| 1695 | return ERR_PTR(error); |
| 1696 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1697 | rcu_read_lock(); |
Pierre Peiffer | c530c6a | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 1698 | spin_lock(&ulp->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | un = lookup_undo(ulp, semid); |
Pierre Peiffer | c530c6a | 2007-10-18 23:40:55 -0700 | [diff] [blame] | 1700 | spin_unlock(&ulp->lock); |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1701 | if (likely(un != NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | goto out; |
| 1703 | |
| 1704 | /* no undo structure around - allocate one. */ |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1705 | /* step 1: figure out the size of the semaphore array */ |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1706 | sma = sem_obtain_object_check(ns, semid); |
| 1707 | if (IS_ERR(sma)) { |
| 1708 | rcu_read_unlock(); |
Julia Lawall | 4de85cd | 2010-05-26 14:43:44 -0700 | [diff] [blame] | 1709 | return ERR_CAST(sma); |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1710 | } |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | nsems = sma->sem_nsems; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1713 | if (!ipc_rcu_getref(sma)) { |
| 1714 | rcu_read_unlock(); |
| 1715 | un = ERR_PTR(-EIDRM); |
| 1716 | goto out; |
| 1717 | } |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1718 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1720 | /* step 2: allocate new undo structure */ |
Burman Yan | 4668edc | 2006-12-06 20:38:51 -0800 | [diff] [blame] | 1721 | new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | if (!new) { |
Fabian Frederick | 9b24fef | 2016-08-02 14:03:07 -0700 | [diff] [blame] | 1723 | ipc_rcu_putref(sma, sem_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | return ERR_PTR(-ENOMEM); |
| 1725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1727 | /* step 3: Acquire the lock on semaphore array */ |
Linus Torvalds | 4091fd94 | 2013-05-04 10:13:40 -0700 | [diff] [blame] | 1728 | rcu_read_lock(); |
Pierre Peiffer | 6ff3797 | 2008-04-29 01:00:46 -0700 | [diff] [blame] | 1729 | sem_lock_and_putref(sma); |
Rafael Aquini | 0f3d2b0 | 2014-01-27 17:07:01 -0800 | [diff] [blame] | 1730 | if (!ipc_valid_object(&sma->sem_perm)) { |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1731 | sem_unlock(sma, -1); |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 1732 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | kfree(new); |
| 1734 | un = ERR_PTR(-EIDRM); |
| 1735 | goto out; |
| 1736 | } |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1737 | spin_lock(&ulp->lock); |
| 1738 | |
| 1739 | /* |
| 1740 | * step 4: check for races: did someone else allocate the undo struct? |
| 1741 | */ |
| 1742 | un = lookup_undo(ulp, semid); |
| 1743 | if (un) { |
| 1744 | kfree(new); |
| 1745 | goto success; |
| 1746 | } |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1747 | /* step 5: initialize & link new undo structure */ |
| 1748 | new->semadj = (short *) &new[1]; |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1749 | new->ulp = ulp; |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1750 | new->semid = semid; |
| 1751 | assert_spin_locked(&ulp->lock); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1752 | list_add_rcu(&new->list_proc, &ulp->list_proc); |
Davidlohr Bueso | cf9d5d7 | 2013-07-08 16:01:11 -0700 | [diff] [blame] | 1753 | ipc_assert_locked_object(&sma->sem_perm); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1754 | list_add(&new->list_id, &sma->list_id); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1755 | un = new; |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1756 | |
| 1757 | success: |
| 1758 | spin_unlock(&ulp->lock); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1759 | sem_unlock(sma, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | out: |
| 1761 | return un; |
| 1762 | } |
| 1763 | |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1764 | |
| 1765 | /** |
Davidlohr Bueso | 8001c85 | 2014-01-27 17:07:05 -0800 | [diff] [blame] | 1766 | * get_queue_result - retrieve the result code from sem_queue |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1767 | * @q: Pointer to queue structure |
| 1768 | * |
| 1769 | * Retrieve the return code from the pending queue. If IN_WAKEUP is found in |
| 1770 | * q->status, then we must loop until the value is replaced with the final |
| 1771 | * value: This may happen if a task is woken up by an unrelated event (e.g. |
| 1772 | * signal) and in parallel the task is woken up by another task because it got |
| 1773 | * the requested semaphores. |
| 1774 | * |
| 1775 | * The function can be called with or without holding the semaphore spinlock. |
| 1776 | */ |
| 1777 | static int get_queue_result(struct sem_queue *q) |
| 1778 | { |
| 1779 | int error; |
| 1780 | |
| 1781 | error = q->status; |
| 1782 | while (unlikely(error == IN_WAKEUP)) { |
| 1783 | cpu_relax(); |
| 1784 | error = q->status; |
| 1785 | } |
| 1786 | |
| 1787 | return error; |
| 1788 | } |
| 1789 | |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 1790 | SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops, |
| 1791 | unsigned, nsops, const struct timespec __user *, timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | { |
| 1793 | int error = -EINVAL; |
| 1794 | struct sem_array *sma; |
| 1795 | struct sembuf fast_sops[SEMOPM_FAST]; |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1796 | struct sembuf *sops = fast_sops, *sop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | struct sem_undo *un; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1798 | int undos = 0, alter = 0, max, locknum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | struct sem_queue queue; |
| 1800 | unsigned long jiffies_left = 0; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1801 | struct ipc_namespace *ns; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1802 | struct list_head tasks; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1803 | |
| 1804 | ns = current->nsproxy->ipc_ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
| 1806 | if (nsops < 1 || semid < 0) |
| 1807 | return -EINVAL; |
Kirill Korotaev | e389353 | 2006-10-02 02:18:22 -0700 | [diff] [blame] | 1808 | if (nsops > ns->sc_semopm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | return -E2BIG; |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1810 | if (nsops > SEMOPM_FAST) { |
| 1811 | sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL); |
| 1812 | if (sops == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | return -ENOMEM; |
| 1814 | } |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 1815 | if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) { |
| 1816 | error = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | goto out_free; |
| 1818 | } |
| 1819 | if (timeout) { |
| 1820 | struct timespec _timeout; |
| 1821 | if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) { |
| 1822 | error = -EFAULT; |
| 1823 | goto out_free; |
| 1824 | } |
| 1825 | if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 || |
| 1826 | _timeout.tv_nsec >= 1000000000L) { |
| 1827 | error = -EINVAL; |
| 1828 | goto out_free; |
| 1829 | } |
| 1830 | jiffies_left = timespec_to_jiffies(&_timeout); |
| 1831 | } |
| 1832 | max = 0; |
| 1833 | for (sop = sops; sop < sops + nsops; sop++) { |
| 1834 | if (sop->sem_num >= max) |
| 1835 | max = sop->sem_num; |
| 1836 | if (sop->sem_flg & SEM_UNDO) |
Manfred Spraul | b78755a | 2005-06-23 00:10:06 -0700 | [diff] [blame] | 1837 | undos = 1; |
| 1838 | if (sop->sem_op != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | alter = 1; |
| 1840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1842 | INIT_LIST_HEAD(&tasks); |
| 1843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | if (undos) { |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1845 | /* On success, find_alloc_undo takes the rcu_read_lock */ |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1846 | un = find_alloc_undo(ns, semid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | if (IS_ERR(un)) { |
| 1848 | error = PTR_ERR(un); |
| 1849 | goto out_free; |
| 1850 | } |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1851 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | un = NULL; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1853 | rcu_read_lock(); |
| 1854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1856 | sma = sem_obtain_object_check(ns, semid); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1857 | if (IS_ERR(sma)) { |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1858 | rcu_read_unlock(); |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1859 | error = PTR_ERR(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | goto out_free; |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1863 | error = -EFBIG; |
Linus Torvalds | c728b9c | 2013-05-04 11:04:29 -0700 | [diff] [blame] | 1864 | if (max >= sma->sem_nsems) |
| 1865 | goto out_rcu_wakeup; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1866 | |
| 1867 | error = -EACCES; |
Linus Torvalds | c728b9c | 2013-05-04 11:04:29 -0700 | [diff] [blame] | 1868 | if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) |
| 1869 | goto out_rcu_wakeup; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1870 | |
| 1871 | error = security_sem_semop(sma, sops, nsops, alter); |
Linus Torvalds | c728b9c | 2013-05-04 11:04:29 -0700 | [diff] [blame] | 1872 | if (error) |
| 1873 | goto out_rcu_wakeup; |
Davidlohr Bueso | 16df367 | 2013-04-30 19:15:29 -0700 | [diff] [blame] | 1874 | |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 1875 | error = -EIDRM; |
| 1876 | locknum = sem_lock(sma, sops, nsops); |
Rafael Aquini | 0f3d2b0 | 2014-01-27 17:07:01 -0800 | [diff] [blame] | 1877 | /* |
| 1878 | * We eventually might perform the following check in a lockless |
| 1879 | * fashion, considering ipc_valid_object() locking constraints. |
| 1880 | * If nsops == 1 and there is no contention for sem_perm.lock, then |
| 1881 | * only a per-semaphore lock is held and it's OK to proceed with the |
| 1882 | * check below. More details on the fine grained locking scheme |
| 1883 | * entangled here and why it's RMID race safe on comments at sem_lock() |
| 1884 | */ |
| 1885 | if (!ipc_valid_object(&sma->sem_perm)) |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 1886 | goto out_unlock_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | /* |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1888 | * semid identifiers are not unique - find_alloc_undo may have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | * allocated an undo structure, it was invalidated by an RMID |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1890 | * 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] | 1891 | * This case can be detected checking un->semid. The existence of |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 1892 | * "un" itself is guaranteed by rcu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | */ |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1894 | if (un && un->semid == -1) |
| 1895 | goto out_unlock_free; |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 1896 | |
Manfred Spraul | d198cd6 | 2014-06-06 14:37:49 -0700 | [diff] [blame] | 1897 | queue.sops = sops; |
| 1898 | queue.nsops = nsops; |
| 1899 | queue.undo = un; |
| 1900 | queue.pid = task_tgid_vnr(current); |
| 1901 | queue.alter = alter; |
| 1902 | |
| 1903 | error = perform_atomic_semop(sma, &queue); |
Manfred Spraul | 0e8c665 | 2013-09-30 13:45:25 -0700 | [diff] [blame] | 1904 | if (error == 0) { |
| 1905 | /* If the operation was successful, then do |
| 1906 | * the required updates. |
| 1907 | */ |
| 1908 | if (alter) |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 1909 | do_smart_update(sma, sops, nsops, 1, &tasks); |
Manfred Spraul | 0e8c665 | 2013-09-30 13:45:25 -0700 | [diff] [blame] | 1910 | else |
| 1911 | set_semotime(sma, sops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | } |
Manfred Spraul | 0e8c665 | 2013-09-30 13:45:25 -0700 | [diff] [blame] | 1913 | if (error <= 0) |
| 1914 | goto out_unlock_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
| 1916 | /* We need to sleep on this operation, so we put the current |
| 1917 | * task into the pending queue and go to sleep. |
| 1918 | */ |
Paul McQuade | 46c0a8c | 2014-06-06 14:37:37 -0700 | [diff] [blame] | 1919 | |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 1920 | if (nsops == 1) { |
| 1921 | struct sem *curr; |
| 1922 | curr = &sma->sem_base[sops->sem_num]; |
| 1923 | |
Manfred Spraul | f269f40 | 2013-07-08 16:01:24 -0700 | [diff] [blame] | 1924 | if (alter) { |
| 1925 | if (sma->complex_count) { |
| 1926 | list_add_tail(&queue.list, |
| 1927 | &sma->pending_alter); |
| 1928 | } else { |
| 1929 | |
| 1930 | list_add_tail(&queue.list, |
| 1931 | &curr->pending_alter); |
| 1932 | } |
| 1933 | } else { |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 1934 | list_add_tail(&queue.list, &curr->pending_const); |
Manfred Spraul | f269f40 | 2013-07-08 16:01:24 -0700 | [diff] [blame] | 1935 | } |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 1936 | } else { |
Manfred Spraul | f269f40 | 2013-07-08 16:01:24 -0700 | [diff] [blame] | 1937 | if (!sma->complex_count) |
| 1938 | merge_queues(sma); |
| 1939 | |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 1940 | if (alter) |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 1941 | list_add_tail(&queue.list, &sma->pending_alter); |
Rik van Riel | 9f1bc2c9 | 2013-04-30 19:15:39 -0700 | [diff] [blame] | 1942 | else |
Manfred Spraul | 1a82e9e | 2013-07-08 16:01:23 -0700 | [diff] [blame] | 1943 | list_add_tail(&queue.list, &sma->pending_const); |
| 1944 | |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 1945 | sma->complex_count++; |
| 1946 | } |
| 1947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | queue.status = -EINTR; |
| 1949 | queue.sleeper = current; |
Manfred Spraul | 0b0577f | 2011-11-02 13:38:52 -0700 | [diff] [blame] | 1950 | |
| 1951 | sleep_again: |
Davidlohr Bueso | 52644c9 | 2015-02-17 13:47:55 -0800 | [diff] [blame] | 1952 | __set_current_state(TASK_INTERRUPTIBLE); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1953 | sem_unlock(sma, locknum); |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 1954 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | |
| 1956 | if (timeout) |
| 1957 | jiffies_left = schedule_timeout(jiffies_left); |
| 1958 | else |
| 1959 | schedule(); |
| 1960 | |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1961 | error = get_queue_result(&queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | |
| 1963 | if (error != -EINTR) { |
| 1964 | /* fast path: update_queue already obtained all requested |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1965 | * resources. |
| 1966 | * Perform a smp_mb(): User space could assume that semop() |
| 1967 | * is a memory barrier: Without the mb(), the cpu could |
| 1968 | * speculatively read in user space stale data that was |
| 1969 | * overwritten by the previous owner of the semaphore. |
| 1970 | */ |
| 1971 | smp_mb(); |
| 1972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | goto out_free; |
| 1974 | } |
| 1975 | |
Linus Torvalds | 321310c | 2013-05-04 10:47:57 -0700 | [diff] [blame] | 1976 | rcu_read_lock(); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 1977 | sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum); |
Manfred Spraul | d694ad6 | 2011-07-25 17:11:47 -0700 | [diff] [blame] | 1978 | |
| 1979 | /* |
| 1980 | * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing. |
| 1981 | */ |
| 1982 | error = get_queue_result(&queue); |
| 1983 | |
| 1984 | /* |
| 1985 | * Array removed? If yes, leave without sem_unlock(). |
| 1986 | */ |
Nadia Derbey | 023a535 | 2007-10-18 23:40:51 -0700 | [diff] [blame] | 1987 | if (IS_ERR(sma)) { |
Linus Torvalds | 321310c | 2013-05-04 10:47:57 -0700 | [diff] [blame] | 1988 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | goto out_free; |
| 1990 | } |
| 1991 | |
Manfred Spraul | c61284e | 2010-07-20 13:24:23 -0700 | [diff] [blame] | 1992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | /* |
Manfred Spraul | d694ad6 | 2011-07-25 17:11:47 -0700 | [diff] [blame] | 1994 | * If queue.status != -EINTR we are woken up by another process. |
| 1995 | * Leave without unlink_queue(), but with sem_unlock(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | */ |
Davidlohr Bueso | 3ab08fe | 2014-01-27 17:07:06 -0800 | [diff] [blame] | 1997 | if (error != -EINTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | goto out_unlock_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | |
| 2000 | /* |
| 2001 | * If an interrupt occurred we have to clean up the queue |
| 2002 | */ |
| 2003 | if (timeout && jiffies_left == 0) |
| 2004 | error = -EAGAIN; |
Manfred Spraul | 0b0577f | 2011-11-02 13:38:52 -0700 | [diff] [blame] | 2005 | |
| 2006 | /* |
| 2007 | * If the wakeup was spurious, just retry |
| 2008 | */ |
| 2009 | if (error == -EINTR && !signal_pending(current)) |
| 2010 | goto sleep_again; |
| 2011 | |
Manfred Spraul | b97e820 | 2009-12-15 16:47:32 -0800 | [diff] [blame] | 2012 | unlink_queue(sma, &queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | |
| 2014 | out_unlock_free: |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 2015 | sem_unlock(sma, locknum); |
Linus Torvalds | c728b9c | 2013-05-04 11:04:29 -0700 | [diff] [blame] | 2016 | out_rcu_wakeup: |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 2017 | rcu_read_unlock(); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 2018 | wake_up_sem_queue_do(&tasks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | out_free: |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 2020 | if (sops != fast_sops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | kfree(sops); |
| 2022 | return error; |
| 2023 | } |
| 2024 | |
Heiko Carstens | d5460c9 | 2009-01-14 14:14:27 +0100 | [diff] [blame] | 2025 | SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops, |
| 2026 | unsigned, nsops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | { |
| 2028 | return sys_semtimedop(semid, tsops, nsops, NULL); |
| 2029 | } |
| 2030 | |
| 2031 | /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between |
| 2032 | * parent and child tasks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | */ |
| 2034 | |
| 2035 | int copy_semundo(unsigned long clone_flags, struct task_struct *tsk) |
| 2036 | { |
| 2037 | struct sem_undo_list *undo_list; |
| 2038 | int error; |
| 2039 | |
| 2040 | if (clone_flags & CLONE_SYSVSEM) { |
| 2041 | error = get_undo_list(&undo_list); |
| 2042 | if (error) |
| 2043 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | atomic_inc(&undo_list->refcnt); |
| 2045 | tsk->sysvsem.undo_list = undo_list; |
Paul McQuade | 46c0a8c | 2014-06-06 14:37:37 -0700 | [diff] [blame] | 2046 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | tsk->sysvsem.undo_list = NULL; |
| 2048 | |
| 2049 | return 0; |
| 2050 | } |
| 2051 | |
| 2052 | /* |
| 2053 | * add semadj values to semaphores, free undo structures. |
| 2054 | * undo structures are not freed when semaphore arrays are destroyed |
| 2055 | * so some of them may be out of date. |
| 2056 | * IMPLEMENTATION NOTE: There is some confusion over whether the |
| 2057 | * set of adjustments that needs to be done should be done in an atomic |
| 2058 | * manner or not. That is, if we are attempting to decrement the semval |
| 2059 | * should we queue up and wait until we can do so legally? |
| 2060 | * The original implementation attempted to do this (queue and wait). |
| 2061 | * The current implementation does not do so. The POSIX standard |
| 2062 | * and SVID should be consulted to determine what behavior is mandated. |
| 2063 | */ |
| 2064 | void exit_sem(struct task_struct *tsk) |
| 2065 | { |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 2066 | struct sem_undo_list *ulp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 2068 | ulp = tsk->sysvsem.undo_list; |
| 2069 | if (!ulp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | return; |
Manfred Spraul | 9edff4a | 2008-04-29 01:00:57 -0700 | [diff] [blame] | 2071 | tsk->sysvsem.undo_list = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 2073 | if (!atomic_dec_and_test(&ulp->refcnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | return; |
| 2075 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2076 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | struct sem_array *sma; |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2078 | struct sem_undo *un; |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 2079 | struct list_head tasks; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 2080 | int semid, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2082 | rcu_read_lock(); |
Jiri Pirko | 05725f7 | 2009-04-14 20:17:16 +0200 | [diff] [blame] | 2083 | un = list_entry_rcu(ulp->list_proc.next, |
| 2084 | struct sem_undo, list_proc); |
Herton R. Krzesinski | 602b859 | 2015-08-14 15:35:02 -0700 | [diff] [blame] | 2085 | if (&un->list_proc == &ulp->list_proc) { |
| 2086 | /* |
| 2087 | * We must wait for freeary() before freeing this ulp, |
| 2088 | * in case we raced with last sem_undo. There is a small |
| 2089 | * possibility where we exit while freeary() didn't |
| 2090 | * finish unlocking sem_undo_list. |
| 2091 | */ |
| 2092 | spin_unlock_wait(&ulp->lock); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 2093 | rcu_read_unlock(); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2094 | break; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 2095 | } |
Herton R. Krzesinski | 602b859 | 2015-08-14 15:35:02 -0700 | [diff] [blame] | 2096 | spin_lock(&ulp->lock); |
| 2097 | semid = un->semid; |
| 2098 | spin_unlock(&ulp->lock); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2099 | |
Herton R. Krzesinski | 602b859 | 2015-08-14 15:35:02 -0700 | [diff] [blame] | 2100 | /* exit_sem raced with IPC_RMID, nothing to do */ |
| 2101 | if (semid == -1) { |
| 2102 | rcu_read_unlock(); |
| 2103 | continue; |
| 2104 | } |
| 2105 | |
| 2106 | sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2107 | /* exit_sem raced with IPC_RMID, nothing to do */ |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 2108 | if (IS_ERR(sma)) { |
| 2109 | rcu_read_unlock(); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2110 | continue; |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 2111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 2113 | sem_lock(sma, NULL, -1); |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 2114 | /* exit_sem raced with IPC_RMID, nothing to do */ |
Rafael Aquini | 0f3d2b0 | 2014-01-27 17:07:01 -0800 | [diff] [blame] | 2115 | if (!ipc_valid_object(&sma->sem_perm)) { |
Manfred Spraul | 6e224f9 | 2013-10-16 13:46:45 -0700 | [diff] [blame] | 2116 | sem_unlock(sma, -1); |
| 2117 | rcu_read_unlock(); |
| 2118 | continue; |
| 2119 | } |
Nick Piggin | bf17bb7 | 2009-12-15 16:47:28 -0800 | [diff] [blame] | 2120 | un = __lookup_undo(ulp, semid); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2121 | if (un == NULL) { |
| 2122 | /* exit_sem raced with IPC_RMID+semget() that created |
| 2123 | * exactly the same semid. Nothing to do. |
| 2124 | */ |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 2125 | sem_unlock(sma, -1); |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 2126 | rcu_read_unlock(); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2127 | continue; |
| 2128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2130 | /* remove un from the linked lists */ |
Davidlohr Bueso | cf9d5d7 | 2013-07-08 16:01:11 -0700 | [diff] [blame] | 2131 | ipc_assert_locked_object(&sma->sem_perm); |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 2132 | list_del(&un->list_id); |
| 2133 | |
Herton R. Krzesinski | a979558 | 2015-08-14 15:35:05 -0700 | [diff] [blame] | 2134 | /* we are the last process using this ulp, acquiring ulp->lock |
| 2135 | * isn't required. Besides that, we are also protected against |
| 2136 | * IPC_RMID as we hold sma->sem_perm lock now |
| 2137 | */ |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2138 | list_del_rcu(&un->list_proc); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2139 | |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 2140 | /* perform adjustments registered in un */ |
| 2141 | for (i = 0; i < sma->sem_nsems; i++) { |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 2142 | struct sem *semaphore = &sma->sem_base[i]; |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 2143 | if (un->semadj[i]) { |
| 2144 | semaphore->semval += un->semadj[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | /* |
| 2146 | * Range checks of the new semaphore value, |
| 2147 | * not defined by sus: |
| 2148 | * - Some unices ignore the undo entirely |
| 2149 | * (e.g. HP UX 11i 11.22, Tru64 V5.1) |
| 2150 | * - some cap the value (e.g. FreeBSD caps |
| 2151 | * at 0, but doesn't enforce SEMVMX) |
| 2152 | * |
| 2153 | * Linux caps the semaphore value, both at 0 |
| 2154 | * and at SEMVMX. |
| 2155 | * |
Manfred Spraul | 239521f | 2014-01-27 17:07:04 -0800 | [diff] [blame] | 2156 | * Manfred <manfred@colorfullife.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | */ |
Ingo Molnar | 5f921ae | 2006-03-26 01:37:17 -0800 | [diff] [blame] | 2158 | if (semaphore->semval < 0) |
| 2159 | semaphore->semval = 0; |
| 2160 | if (semaphore->semval > SEMVMX) |
| 2161 | semaphore->semval = SEMVMX; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2162 | semaphore->sempid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | } |
| 2164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | /* maybe some queued-up processes were waiting for this */ |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 2166 | INIT_LIST_HEAD(&tasks); |
| 2167 | do_smart_update(sma, NULL, 0, 1, &tasks); |
Rik van Riel | 6062a8d | 2013-04-30 19:15:44 -0700 | [diff] [blame] | 2168 | sem_unlock(sma, -1); |
Linus Torvalds | 6d49dab | 2013-05-03 15:04:40 -0700 | [diff] [blame] | 2169 | rcu_read_unlock(); |
Manfred Spraul | 0a2b9d4 | 2010-05-26 14:43:41 -0700 | [diff] [blame] | 2170 | wake_up_sem_queue_do(&tasks); |
Manfred Spraul | 380af1b | 2008-07-25 01:48:06 -0700 | [diff] [blame] | 2171 | |
Lai Jiangshan | 693a8b6 | 2011-03-18 12:09:35 +0800 | [diff] [blame] | 2172 | kfree_rcu(un, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | } |
Manfred Spraul | 4daa28f | 2008-07-25 01:48:04 -0700 | [diff] [blame] | 2174 | kfree(ulp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | #ifdef CONFIG_PROC_FS |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 2178 | static int sysvipc_sem_proc_show(struct seq_file *s, void *it) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | { |
Eric W. Biederman | 1efdb69 | 2012-02-07 16:54:11 -0800 | [diff] [blame] | 2180 | struct user_namespace *user_ns = seq_user_ns(s); |
Mike Waychison | 19b4946 | 2005-09-06 15:17:10 -0700 | [diff] [blame] | 2181 | struct sem_array *sma = it; |
Manfred Spraul | d12e1e5 | 2013-07-08 16:01:25 -0700 | [diff] [blame] | 2182 | time_t sem_otime; |
| 2183 | |
Manfred Spraul | d8c6337 | 2013-09-30 13:45:07 -0700 | [diff] [blame] | 2184 | /* |
| 2185 | * The proc interface isn't aware of sem_lock(), it calls |
| 2186 | * ipc_lock_object() directly (in sysvipc_find_ipc). |
| 2187 | * In order to stay compatible with sem_lock(), we must wait until |
| 2188 | * all simple semop() calls have left their critical regions. |
| 2189 | */ |
| 2190 | sem_wait_array(sma); |
| 2191 | |
Manfred Spraul | d12e1e5 | 2013-07-08 16:01:25 -0700 | [diff] [blame] | 2192 | sem_otime = get_semotime(sma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | |
Joe Perches | 7f032d6 | 2015-04-15 16:17:54 -0700 | [diff] [blame] | 2194 | seq_printf(s, |
| 2195 | "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n", |
| 2196 | sma->sem_perm.key, |
| 2197 | sma->sem_perm.id, |
| 2198 | sma->sem_perm.mode, |
| 2199 | sma->sem_nsems, |
| 2200 | from_kuid_munged(user_ns, sma->sem_perm.uid), |
| 2201 | from_kgid_munged(user_ns, sma->sem_perm.gid), |
| 2202 | from_kuid_munged(user_ns, sma->sem_perm.cuid), |
| 2203 | from_kgid_munged(user_ns, sma->sem_perm.cgid), |
| 2204 | sem_otime, |
| 2205 | sma->sem_ctime); |
| 2206 | |
| 2207 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | } |
| 2209 | #endif |