ipc: scale msgmni to the number of ipc namespaces
[deliverable/linux.git] / ipc / util.c
CommitLineData
1da177e4
LT
1/*
2 * linux/ipc/util.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 *
5 * Sep 1997 - Call suser() last after "normal" permission checks so we
6 * get BSD style process accounting right.
7 * Occurs in several places in the IPC code.
8 * Chris Evans, <chris@ferret.lmh.ox.ac.uk>
9 * Nov 1999 - ipc helper functions, unified SMP locking
624dffcb 10 * Manfred Spraul <manfred@colorfullife.com>
1da177e4
LT
11 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12 * Mingming Cao <cmm@us.ibm.com>
073115d6
SG
13 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
73ea4130
KK
15 * Jun 2006 - namespaces ssupport
16 * OpenVZ, SWsoft Inc.
17 * Pavel Emelianov <xemul@openvz.org>
1da177e4
LT
18 */
19
1da177e4
LT
20#include <linux/mm.h>
21#include <linux/shm.h>
22#include <linux/init.h>
23#include <linux/msg.h>
1da177e4
LT
24#include <linux/vmalloc.h>
25#include <linux/slab.h>
c59ede7b 26#include <linux/capability.h>
1da177e4
LT
27#include <linux/highuid.h>
28#include <linux/security.h>
29#include <linux/rcupdate.h>
30#include <linux/workqueue.h>
ae781774
MW
31#include <linux/seq_file.h>
32#include <linux/proc_fs.h>
073115d6 33#include <linux/audit.h>
73ea4130 34#include <linux/nsproxy.h>
3e148c79 35#include <linux/rwsem.h>
ae5e1b22 36#include <linux/ipc_namespace.h>
1da177e4
LT
37
38#include <asm/unistd.h>
39
40#include "util.h"
41
ae781774
MW
42struct ipc_proc_iface {
43 const char *path;
44 const char *header;
73ea4130 45 int ids;
ae781774
MW
46 int (*show)(struct seq_file *, void *);
47};
48
73ea4130
KK
49struct ipc_namespace init_ipc_ns = {
50 .kref = {
51 .refcount = ATOMIC_INIT(2),
52 },
53};
54
4d89dc6a
ND
55atomic_t nr_ipc_ns = ATOMIC_INIT(1);
56
57
1da177e4
LT
58/**
59 * ipc_init - initialise IPC subsystem
60 *
61 * The various system5 IPC resources (semaphores, messages and shared
72fd4a35 62 * memory) are initialised
1da177e4
LT
63 */
64
65static int __init ipc_init(void)
66{
67 sem_init();
68 msg_init();
69 shm_init();
70 return 0;
71}
72__initcall(ipc_init);
73
74/**
75 * ipc_init_ids - initialise IPC identifiers
76 * @ids: Identifier set
1da177e4 77 *
7ca7e564
ND
78 * Set up the sequence range to use for the ipc identifier range (limited
79 * below IPCMNI) then initialise the ids idr.
1da177e4
LT
80 */
81
7ca7e564 82void ipc_init_ids(struct ipc_ids *ids)
1da177e4 83{
3e148c79 84 init_rwsem(&ids->rw_mutex);
1da177e4 85
1da177e4 86 ids->in_use = 0;
1da177e4
LT
87 ids->seq = 0;
88 {
89 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
90 if(seq_limit > USHRT_MAX)
91 ids->seq_max = USHRT_MAX;
92 else
93 ids->seq_max = seq_limit;
94 }
95
7ca7e564 96 idr_init(&ids->ipcs_idr);
1da177e4
LT
97}
98
ae781774 99#ifdef CONFIG_PROC_FS
9a32144e 100static const struct file_operations sysvipc_proc_fops;
ae781774 101/**
72fd4a35 102 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
ae781774
MW
103 * @path: Path in procfs
104 * @header: Banner to be printed at the beginning of the file.
105 * @ids: ipc id table to iterate.
106 * @show: show routine.
107 */
108void __init ipc_init_proc_interface(const char *path, const char *header,
73ea4130 109 int ids, int (*show)(struct seq_file *, void *))
ae781774
MW
110{
111 struct proc_dir_entry *pde;
112 struct ipc_proc_iface *iface;
113
114 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
115 if (!iface)
116 return;
117 iface->path = path;
118 iface->header = header;
119 iface->ids = ids;
120 iface->show = show;
121
122 pde = create_proc_entry(path,
123 S_IRUGO, /* world readable */
124 NULL /* parent dir */);
125 if (pde) {
126 pde->data = iface;
127 pde->proc_fops = &sysvipc_proc_fops;
128 } else {
129 kfree(iface);
130 }
131}
132#endif
133
1da177e4
LT
134/**
135 * ipc_findkey - find a key in an ipc identifier set
136 * @ids: Identifier set
137 * @key: The key to find
138 *
3e148c79 139 * Requires ipc_ids.rw_mutex locked.
7ca7e564
ND
140 * Returns the LOCKED pointer to the ipc structure if found or NULL
141 * if not.
f4566f04 142 * If key is found ipc points to the owning ipc structure
1da177e4
LT
143 */
144
7748dbfa 145static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
1da177e4 146{
7ca7e564
ND
147 struct kern_ipc_perm *ipc;
148 int next_id;
149 int total;
1da177e4 150
7ca7e564
ND
151 for (total = 0, next_id = 0; total < ids->in_use; next_id++) {
152 ipc = idr_find(&ids->ipcs_idr, next_id);
153
154 if (ipc == NULL)
1da177e4 155 continue;
7ca7e564
ND
156
157 if (ipc->key != key) {
158 total++;
159 continue;
160 }
161
162 ipc_lock_by_ptr(ipc);
163 return ipc;
1da177e4 164 }
7ca7e564
ND
165
166 return NULL;
1da177e4
LT
167}
168
7ca7e564
ND
169/**
170 * ipc_get_maxid - get the last assigned id
171 * @ids: IPC identifier set
172 *
3e148c79 173 * Called with ipc_ids.rw_mutex held.
1da177e4 174 */
1da177e4 175
7ca7e564
ND
176int ipc_get_maxid(struct ipc_ids *ids)
177{
178 struct kern_ipc_perm *ipc;
179 int max_id = -1;
180 int total, id;
181
182 if (ids->in_use == 0)
183 return -1;
1da177e4 184
7ca7e564
ND
185 if (ids->in_use == IPCMNI)
186 return IPCMNI - 1;
187
188 /* Look for the last assigned id */
189 total = 0;
190 for (id = 0; id < IPCMNI && total < ids->in_use; id++) {
191 ipc = idr_find(&ids->ipcs_idr, id);
192 if (ipc != NULL) {
193 max_id = id;
194 total++;
195 }
196 }
197 return max_id;
1da177e4
LT
198}
199
200/**
201 * ipc_addid - add an IPC identifier
202 * @ids: IPC identifier set
203 * @new: new IPC permission set
7ca7e564 204 * @size: limit for the number of used ids
1da177e4 205 *
f4566f04 206 * Add an entry 'new' to the IPC ids idr. The permissions object is
1da177e4 207 * initialised and the first free entry is set up and the id assigned
f4566f04 208 * is returned. The 'new' entry is returned in a locked state on success.
283bb7fa 209 * On failure the entry is not locked and a negative err-code is returned.
1da177e4 210 *
3e148c79 211 * Called with ipc_ids.rw_mutex held as a writer.
1da177e4
LT
212 */
213
214int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
215{
7ca7e564 216 int id, err;
1da177e4 217
7ca7e564
ND
218 if (size > IPCMNI)
219 size = IPCMNI;
220
221 if (ids->in_use >= size)
283bb7fa 222 return -ENOSPC;
7ca7e564
ND
223
224 err = idr_get_new(&ids->ipcs_idr, new, &id);
225 if (err)
283bb7fa 226 return err;
7ca7e564 227
1da177e4 228 ids->in_use++;
1da177e4
LT
229
230 new->cuid = new->uid = current->euid;
231 new->gid = new->cgid = current->egid;
232
233 new->seq = ids->seq++;
234 if(ids->seq > ids->seq_max)
235 ids->seq = 0;
236
48dea404 237 new->id = ipc_buildid(id, new->seq);
1da177e4
LT
238 spin_lock_init(&new->lock);
239 new->deleted = 0;
240 rcu_read_lock();
241 spin_lock(&new->lock);
1da177e4
LT
242 return id;
243}
244
7748dbfa
ND
245/**
246 * ipcget_new - create a new ipc object
247 * @ns: namespace
f4566f04 248 * @ids: IPC identifer set
7748dbfa
ND
249 * @ops: the actual creation routine to call
250 * @params: its parameters
251 *
f4566f04
ND
252 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
253 * when the key is IPC_PRIVATE.
7748dbfa 254 */
b2d75cdd 255static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
7748dbfa
ND
256 struct ipc_ops *ops, struct ipc_params *params)
257{
258 int err;
283bb7fa 259retry:
7748dbfa
ND
260 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
261
262 if (!err)
263 return -ENOMEM;
264
3e148c79 265 down_write(&ids->rw_mutex);
7748dbfa 266 err = ops->getnew(ns, params);
3e148c79 267 up_write(&ids->rw_mutex);
7748dbfa 268
283bb7fa
PP
269 if (err == -EAGAIN)
270 goto retry;
271
7748dbfa
ND
272 return err;
273}
274
275/**
276 * ipc_check_perms - check security and permissions for an IPC
277 * @ipcp: ipc permission set
7748dbfa
ND
278 * @ops: the actual security routine to call
279 * @params: its parameters
f4566f04
ND
280 *
281 * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
282 * when the key is not IPC_PRIVATE and that key already exists in the
283 * ids IDR.
284 *
285 * On success, the IPC id is returned.
286 *
3e148c79 287 * It is called with ipc_ids.rw_mutex and ipcp->lock held.
7748dbfa
ND
288 */
289static int ipc_check_perms(struct kern_ipc_perm *ipcp, struct ipc_ops *ops,
290 struct ipc_params *params)
291{
292 int err;
293
294 if (ipcperms(ipcp, params->flg))
295 err = -EACCES;
296 else {
297 err = ops->associate(ipcp, params->flg);
298 if (!err)
299 err = ipcp->id;
300 }
301
302 return err;
303}
304
305/**
306 * ipcget_public - get an ipc object or create a new one
307 * @ns: namespace
f4566f04 308 * @ids: IPC identifer set
7748dbfa
ND
309 * @ops: the actual creation routine to call
310 * @params: its parameters
311 *
f4566f04
ND
312 * This routine is called by sys_msgget, sys_semget() and sys_shmget()
313 * when the key is not IPC_PRIVATE.
314 * It adds a new entry if the key is not found and does some permission
315 * / security checkings if the key is found.
316 *
317 * On success, the ipc id is returned.
7748dbfa 318 */
b2d75cdd 319static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
7748dbfa
ND
320 struct ipc_ops *ops, struct ipc_params *params)
321{
322 struct kern_ipc_perm *ipcp;
323 int flg = params->flg;
324 int err;
283bb7fa 325retry:
7748dbfa
ND
326 err = idr_pre_get(&ids->ipcs_idr, GFP_KERNEL);
327
3e148c79
ND
328 /*
329 * Take the lock as a writer since we are potentially going to add
330 * a new entry + read locks are not "upgradable"
331 */
332 down_write(&ids->rw_mutex);
7748dbfa
ND
333 ipcp = ipc_findkey(ids, params->key);
334 if (ipcp == NULL) {
335 /* key not used */
336 if (!(flg & IPC_CREAT))
337 err = -ENOENT;
338 else if (!err)
339 err = -ENOMEM;
340 else
341 err = ops->getnew(ns, params);
342 } else {
343 /* ipc object has been locked by ipc_findkey() */
344
345 if (flg & IPC_CREAT && flg & IPC_EXCL)
346 err = -EEXIST;
347 else {
348 err = 0;
349 if (ops->more_checks)
350 err = ops->more_checks(ipcp, params);
351 if (!err)
f4566f04
ND
352 /*
353 * ipc_check_perms returns the IPC id on
354 * success
355 */
7748dbfa
ND
356 err = ipc_check_perms(ipcp, ops, params);
357 }
358 ipc_unlock(ipcp);
359 }
3e148c79 360 up_write(&ids->rw_mutex);
7748dbfa 361
283bb7fa
PP
362 if (err == -EAGAIN)
363 goto retry;
364
7748dbfa
ND
365 return err;
366}
367
368
1da177e4
LT
369/**
370 * ipc_rmid - remove an IPC identifier
f4566f04
ND
371 * @ids: IPC identifier set
372 * @ipcp: ipc perm structure containing the identifier to remove
1da177e4 373 *
3e148c79
ND
374 * ipc_ids.rw_mutex (as a writer) and the spinlock for this ID are held
375 * before this function is called, and remain locked on the exit.
1da177e4
LT
376 */
377
7ca7e564 378void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
1da177e4 379{
ce621f5b 380 int lid = ipcid_to_idx(ipcp->id);
7ca7e564
ND
381
382 idr_remove(&ids->ipcs_idr, lid);
1da177e4 383
1da177e4
LT
384 ids->in_use--;
385
7ca7e564
ND
386 ipcp->deleted = 1;
387
388 return;
1da177e4
LT
389}
390
391/**
392 * ipc_alloc - allocate ipc space
393 * @size: size desired
394 *
395 * Allocate memory from the appropriate pools and return a pointer to it.
396 * NULL is returned if the allocation fails
397 */
398
399void* ipc_alloc(int size)
400{
401 void* out;
402 if(size > PAGE_SIZE)
403 out = vmalloc(size);
404 else
405 out = kmalloc(size, GFP_KERNEL);
406 return out;
407}
408
409/**
410 * ipc_free - free ipc space
411 * @ptr: pointer returned by ipc_alloc
412 * @size: size of block
413 *
72fd4a35 414 * Free a block created with ipc_alloc(). The caller must know the size
1da177e4
LT
415 * used in the allocation call.
416 */
417
418void ipc_free(void* ptr, int size)
419{
420 if(size > PAGE_SIZE)
421 vfree(ptr);
422 else
423 kfree(ptr);
424}
425
426/*
427 * rcu allocations:
428 * There are three headers that are prepended to the actual allocation:
429 * - during use: ipc_rcu_hdr.
430 * - during the rcu grace period: ipc_rcu_grace.
431 * - [only if vmalloc]: ipc_rcu_sched.
432 * Their lifetime doesn't overlap, thus the headers share the same memory.
433 * Unlike a normal union, they are right-aligned, thus some container_of
434 * forward/backward casting is necessary:
435 */
436struct ipc_rcu_hdr
437{
438 int refcount;
439 int is_vmalloc;
440 void *data[0];
441};
442
443
444struct ipc_rcu_grace
445{
446 struct rcu_head rcu;
447 /* "void *" makes sure alignment of following data is sane. */
448 void *data[0];
449};
450
451struct ipc_rcu_sched
452{
453 struct work_struct work;
454 /* "void *" makes sure alignment of following data is sane. */
455 void *data[0];
456};
457
458#define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
459 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
460#define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
461 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
462
463static inline int rcu_use_vmalloc(int size)
464{
465 /* Too big for a single page? */
466 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
467 return 1;
468 return 0;
469}
470
471/**
472 * ipc_rcu_alloc - allocate ipc and rcu space
473 * @size: size desired
474 *
475 * Allocate memory for the rcu header structure + the object.
476 * Returns the pointer to the object.
477 * NULL is returned if the allocation fails.
478 */
479
480void* ipc_rcu_alloc(int size)
481{
482 void* out;
483 /*
484 * We prepend the allocation with the rcu struct, and
485 * workqueue if necessary (for vmalloc).
486 */
487 if (rcu_use_vmalloc(size)) {
488 out = vmalloc(HDRLEN_VMALLOC + size);
489 if (out) {
490 out += HDRLEN_VMALLOC;
491 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
492 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
493 }
494 } else {
495 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
496 if (out) {
497 out += HDRLEN_KMALLOC;
498 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
499 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
500 }
501 }
502
503 return out;
504}
505
506void ipc_rcu_getref(void *ptr)
507{
508 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
509}
510
65f27f38
DH
511static void ipc_do_vfree(struct work_struct *work)
512{
513 vfree(container_of(work, struct ipc_rcu_sched, work));
514}
515
1da177e4 516/**
1e5d5331
RD
517 * ipc_schedule_free - free ipc + rcu space
518 * @head: RCU callback structure for queued work
1da177e4
LT
519 *
520 * Since RCU callback function is called in bh,
72fd4a35 521 * we need to defer the vfree to schedule_work().
1da177e4
LT
522 */
523static void ipc_schedule_free(struct rcu_head *head)
524{
f4566f04
ND
525 struct ipc_rcu_grace *grace;
526 struct ipc_rcu_sched *sched;
527
528 grace = container_of(head, struct ipc_rcu_grace, rcu);
529 sched = container_of(&(grace->data[0]), struct ipc_rcu_sched,
530 data[0]);
1da177e4 531
65f27f38 532 INIT_WORK(&sched->work, ipc_do_vfree);
1da177e4
LT
533 schedule_work(&sched->work);
534}
535
536/**
1e5d5331
RD
537 * ipc_immediate_free - free ipc + rcu space
538 * @head: RCU callback structure that contains pointer to be freed
1da177e4 539 *
72fd4a35 540 * Free from the RCU callback context.
1da177e4
LT
541 */
542static void ipc_immediate_free(struct rcu_head *head)
543{
544 struct ipc_rcu_grace *free =
545 container_of(head, struct ipc_rcu_grace, rcu);
546 kfree(free);
547}
548
549void ipc_rcu_putref(void *ptr)
550{
551 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
552 return;
553
554 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
555 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
556 ipc_schedule_free);
557 } else {
558 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
559 ipc_immediate_free);
560 }
561}
562
563/**
564 * ipcperms - check IPC permissions
565 * @ipcp: IPC permission set
566 * @flag: desired permission set.
567 *
568 * Check user, group, other permissions for access
569 * to ipc resources. return 0 if allowed
570 */
571
572int ipcperms (struct kern_ipc_perm *ipcp, short flag)
573{ /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
073115d6 574 int requested_mode, granted_mode, err;
1da177e4 575
073115d6
SG
576 if (unlikely((err = audit_ipc_obj(ipcp))))
577 return err;
1da177e4
LT
578 requested_mode = (flag >> 6) | (flag >> 3) | flag;
579 granted_mode = ipcp->mode;
580 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
581 granted_mode >>= 6;
582 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
583 granted_mode >>= 3;
584 /* is there some bit set in requested_mode but not in granted_mode? */
585 if ((requested_mode & ~granted_mode & 0007) &&
586 !capable(CAP_IPC_OWNER))
587 return -1;
588
589 return security_ipc_permission(ipcp, flag);
590}
591
592/*
593 * Functions to convert between the kern_ipc_perm structure and the
594 * old/new ipc_perm structures
595 */
596
597/**
598 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
599 * @in: kernel permissions
600 * @out: new style IPC permissions
601 *
72fd4a35
RD
602 * Turn the kernel object @in into a set of permissions descriptions
603 * for returning to userspace (@out).
1da177e4
LT
604 */
605
606
607void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
608{
609 out->key = in->key;
610 out->uid = in->uid;
611 out->gid = in->gid;
612 out->cuid = in->cuid;
613 out->cgid = in->cgid;
614 out->mode = in->mode;
615 out->seq = in->seq;
616}
617
618/**
f4566f04 619 * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
1da177e4
LT
620 * @in: new style IPC permissions
621 * @out: old style IPC permissions
622 *
72fd4a35
RD
623 * Turn the new style permissions object @in into a compatibility
624 * object and store it into the @out pointer.
1da177e4
LT
625 */
626
627void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
628{
629 out->key = in->key;
630 SET_UID(out->uid, in->uid);
631 SET_GID(out->gid, in->gid);
632 SET_UID(out->cuid, in->cuid);
633 SET_GID(out->cgid, in->cgid);
634 out->mode = in->mode;
635 out->seq = in->seq;
636}
637
f4566f04 638/**
3e148c79 639 * ipc_lock - Lock an ipc structure without rw_mutex held
f4566f04
ND
640 * @ids: IPC identifier set
641 * @id: ipc id to look for
642 *
643 * Look for an id in the ipc ids idr and lock the associated ipc object.
644 *
f4566f04 645 * The ipc object is locked on exit.
3e148c79
ND
646 *
647 * This is the routine that should be called when the rw_mutex is not already
648 * held, i.e. idr tree not protected: it protects the idr tree in read mode
649 * during the idr_find().
f4566f04
ND
650 */
651
7ca7e564 652struct kern_ipc_perm *ipc_lock(struct ipc_ids *ids, int id)
1da177e4 653{
7ca7e564 654 struct kern_ipc_perm *out;
ce621f5b 655 int lid = ipcid_to_idx(id);
1da177e4 656
3e148c79
ND
657 down_read(&ids->rw_mutex);
658
1da177e4 659 rcu_read_lock();
7ca7e564
ND
660 out = idr_find(&ids->ipcs_idr, lid);
661 if (out == NULL) {
1da177e4 662 rcu_read_unlock();
3e148c79 663 up_read(&ids->rw_mutex);
023a5355 664 return ERR_PTR(-EINVAL);
1da177e4 665 }
7ca7e564 666
3e148c79
ND
667 up_read(&ids->rw_mutex);
668
1da177e4
LT
669 spin_lock(&out->lock);
670
671 /* ipc_rmid() may have already freed the ID while ipc_lock
672 * was spinning: here verify that the structure is still valid
673 */
674 if (out->deleted) {
675 spin_unlock(&out->lock);
676 rcu_read_unlock();
023a5355 677 return ERR_PTR(-EINVAL);
1da177e4 678 }
7ca7e564 679
1da177e4
LT
680 return out;
681}
682
3e148c79
ND
683/**
684 * ipc_lock_down - Lock an ipc structure with rw_sem held
685 * @ids: IPC identifier set
686 * @id: ipc id to look for
687 *
688 * Look for an id in the ipc ids idr and lock the associated ipc object.
689 *
690 * The ipc object is locked on exit.
691 *
692 * This is the routine that should be called when the rw_mutex is already
693 * held, i.e. idr tree protected.
694 */
695
696struct kern_ipc_perm *ipc_lock_down(struct ipc_ids *ids, int id)
697{
698 struct kern_ipc_perm *out;
699 int lid = ipcid_to_idx(id);
700
701 rcu_read_lock();
702 out = idr_find(&ids->ipcs_idr, lid);
703 if (out == NULL) {
704 rcu_read_unlock();
705 return ERR_PTR(-EINVAL);
706 }
707
708 spin_lock(&out->lock);
709
710 /*
711 * No need to verify that the structure is still valid since the
712 * rw_mutex is held.
713 */
714 return out;
715}
716
b2d75cdd
PE
717struct kern_ipc_perm *ipc_lock_check_down(struct ipc_ids *ids, int id)
718{
719 struct kern_ipc_perm *out;
720
721 out = ipc_lock_down(ids, id);
722 if (IS_ERR(out))
723 return out;
724
725 if (ipc_checkid(out, id)) {
726 ipc_unlock(out);
727 return ERR_PTR(-EIDRM);
728 }
729
730 return out;
731}
732
733struct kern_ipc_perm *ipc_lock_check(struct ipc_ids *ids, int id)
734{
735 struct kern_ipc_perm *out;
736
737 out = ipc_lock(ids, id);
738 if (IS_ERR(out))
739 return out;
740
741 if (ipc_checkid(out, id)) {
742 ipc_unlock(out);
743 return ERR_PTR(-EIDRM);
744 }
745
746 return out;
747}
748
749/**
750 * ipcget - Common sys_*get() code
751 * @ns : namsepace
752 * @ids : IPC identifier set
753 * @ops : operations to be called on ipc object creation, permission checks
754 * and further checks
755 * @params : the parameters needed by the previous operations.
756 *
757 * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
758 */
759int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
760 struct ipc_ops *ops, struct ipc_params *params)
761{
762 if (params->key == IPC_PRIVATE)
763 return ipcget_new(ns, ids, ops, params);
764 else
765 return ipcget_public(ns, ids, ops, params);
766}
767
1da177e4
LT
768#ifdef __ARCH_WANT_IPC_PARSE_VERSION
769
770
771/**
772 * ipc_parse_version - IPC call version
773 * @cmd: pointer to command
774 *
775 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
72fd4a35 776 * The @cmd value is turned from an encoding command and version into
1da177e4
LT
777 * just the command code.
778 */
779
780int ipc_parse_version (int *cmd)
781{
782 if (*cmd & IPC_64) {
783 *cmd ^= IPC_64;
784 return IPC_64;
785 } else {
786 return IPC_OLD;
787 }
788}
789
790#endif /* __ARCH_WANT_IPC_PARSE_VERSION */
ae781774
MW
791
792#ifdef CONFIG_PROC_FS
bc1fc6d8
EB
793struct ipc_proc_iter {
794 struct ipc_namespace *ns;
795 struct ipc_proc_iface *iface;
796};
797
7ca7e564
ND
798/*
799 * This routine locks the ipc structure found at least at position pos.
800 */
b524b9ad
AB
801static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
802 loff_t *new_pos)
ae781774 803{
7ca7e564
ND
804 struct kern_ipc_perm *ipc;
805 int total, id;
73ea4130 806
7ca7e564
ND
807 total = 0;
808 for (id = 0; id < pos && total < ids->in_use; id++) {
809 ipc = idr_find(&ids->ipcs_idr, id);
810 if (ipc != NULL)
811 total++;
812 }
ae781774 813
7ca7e564
ND
814 if (total >= ids->in_use)
815 return NULL;
ae781774 816
7ca7e564
ND
817 for ( ; pos < IPCMNI; pos++) {
818 ipc = idr_find(&ids->ipcs_idr, pos);
819 if (ipc != NULL) {
820 *new_pos = pos + 1;
821 ipc_lock_by_ptr(ipc);
ae781774
MW
822 return ipc;
823 }
824 }
825
826 /* Out of range - return NULL to terminate iteration */
827 return NULL;
828}
829
7ca7e564
ND
830static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
831{
832 struct ipc_proc_iter *iter = s->private;
833 struct ipc_proc_iface *iface = iter->iface;
834 struct kern_ipc_perm *ipc = it;
835
836 /* If we had an ipc id locked before, unlock it */
837 if (ipc && ipc != SEQ_START_TOKEN)
838 ipc_unlock(ipc);
839
ed2ddbf8 840 return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
7ca7e564
ND
841}
842
ae781774 843/*
f4566f04
ND
844 * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
845 * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
ae781774
MW
846 */
847static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
848{
bc1fc6d8
EB
849 struct ipc_proc_iter *iter = s->private;
850 struct ipc_proc_iface *iface = iter->iface;
73ea4130
KK
851 struct ipc_ids *ids;
852
ed2ddbf8 853 ids = &iter->ns->ids[iface->ids];
ae781774
MW
854
855 /*
856 * Take the lock - this will be released by the corresponding
857 * call to stop().
858 */
3e148c79 859 down_read(&ids->rw_mutex);
ae781774
MW
860
861 /* pos < 0 is invalid */
862 if (*pos < 0)
863 return NULL;
864
865 /* pos == 0 means header */
866 if (*pos == 0)
867 return SEQ_START_TOKEN;
868
869 /* Find the (pos-1)th ipc */
7ca7e564 870 return sysvipc_find_ipc(ids, *pos - 1, pos);
ae781774
MW
871}
872
873static void sysvipc_proc_stop(struct seq_file *s, void *it)
874{
875 struct kern_ipc_perm *ipc = it;
bc1fc6d8
EB
876 struct ipc_proc_iter *iter = s->private;
877 struct ipc_proc_iface *iface = iter->iface;
73ea4130 878 struct ipc_ids *ids;
ae781774 879
f4566f04 880 /* If we had a locked structure, release it */
ae781774
MW
881 if (ipc && ipc != SEQ_START_TOKEN)
882 ipc_unlock(ipc);
883
ed2ddbf8 884 ids = &iter->ns->ids[iface->ids];
ae781774 885 /* Release the lock we took in start() */
3e148c79 886 up_read(&ids->rw_mutex);
ae781774
MW
887}
888
889static int sysvipc_proc_show(struct seq_file *s, void *it)
890{
bc1fc6d8
EB
891 struct ipc_proc_iter *iter = s->private;
892 struct ipc_proc_iface *iface = iter->iface;
ae781774
MW
893
894 if (it == SEQ_START_TOKEN)
895 return seq_puts(s, iface->header);
896
897 return iface->show(s, it);
898}
899
900static struct seq_operations sysvipc_proc_seqops = {
901 .start = sysvipc_proc_start,
902 .stop = sysvipc_proc_stop,
903 .next = sysvipc_proc_next,
904 .show = sysvipc_proc_show,
905};
906
bc1fc6d8
EB
907static int sysvipc_proc_open(struct inode *inode, struct file *file)
908{
ae781774
MW
909 int ret;
910 struct seq_file *seq;
bc1fc6d8
EB
911 struct ipc_proc_iter *iter;
912
913 ret = -ENOMEM;
914 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
915 if (!iter)
916 goto out;
ae781774
MW
917
918 ret = seq_open(file, &sysvipc_proc_seqops);
bc1fc6d8
EB
919 if (ret)
920 goto out_kfree;
921
922 seq = file->private_data;
923 seq->private = iter;
924
925 iter->iface = PDE(inode)->data;
926 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
927out:
ae781774 928 return ret;
bc1fc6d8
EB
929out_kfree:
930 kfree(iter);
931 goto out;
932}
933
934static int sysvipc_proc_release(struct inode *inode, struct file *file)
935{
936 struct seq_file *seq = file->private_data;
937 struct ipc_proc_iter *iter = seq->private;
938 put_ipc_ns(iter->ns);
939 return seq_release_private(inode, file);
ae781774
MW
940}
941
9a32144e 942static const struct file_operations sysvipc_proc_fops = {
ae781774
MW
943 .open = sysvipc_proc_open,
944 .read = seq_read,
945 .llseek = seq_lseek,
bc1fc6d8 946 .release = sysvipc_proc_release,
ae781774
MW
947};
948#endif /* CONFIG_PROC_FS */
This page took 0.494474 seconds and 5 git commands to generate.