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