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