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