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