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