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