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