[MIPS] Alchemy: Get rid of au_ffs().
[pv_ops_mirror.git] / ipc / util.c
blob44e5135aee4785e2da26de92e0d3b52b02246842
1 /*
2 * linux/ipc/util.c
3 * Copyright (C) 1992 Krishna Balasubramanian
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
10 * Manfred Spraul <manfred@colorfullife.com>
11 * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
12 * Mingming Cao <cmm@us.ibm.com>
13 * Mar 2006 - support for audit of ipc object properties
14 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
15 * Jun 2006 - namespaces ssupport
16 * OpenVZ, SWsoft Inc.
17 * Pavel Emelianov <xemul@openvz.org>
20 #include <linux/mm.h>
21 #include <linux/shm.h>
22 #include <linux/init.h>
23 #include <linux/msg.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/capability.h>
27 #include <linux/highuid.h>
28 #include <linux/security.h>
29 #include <linux/rcupdate.h>
30 #include <linux/workqueue.h>
31 #include <linux/seq_file.h>
32 #include <linux/proc_fs.h>
33 #include <linux/audit.h>
34 #include <linux/nsproxy.h>
36 #include <asm/unistd.h>
38 #include "util.h"
40 struct ipc_proc_iface {
41 const char *path;
42 const char *header;
43 int ids;
44 int (*show)(struct seq_file *, void *);
47 struct ipc_namespace init_ipc_ns = {
48 .kref = {
49 .refcount = ATOMIC_INIT(2),
53 static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
55 int err;
56 struct ipc_namespace *ns;
58 err = -ENOMEM;
59 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
60 if (ns == NULL)
61 goto err_mem;
63 err = sem_init_ns(ns);
64 if (err)
65 goto err_sem;
66 err = msg_init_ns(ns);
67 if (err)
68 goto err_msg;
69 err = shm_init_ns(ns);
70 if (err)
71 goto err_shm;
73 kref_init(&ns->kref);
74 return ns;
76 err_shm:
77 msg_exit_ns(ns);
78 err_msg:
79 sem_exit_ns(ns);
80 err_sem:
81 kfree(ns);
82 err_mem:
83 return ERR_PTR(err);
86 struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
88 struct ipc_namespace *new_ns;
90 BUG_ON(!ns);
91 get_ipc_ns(ns);
93 if (!(flags & CLONE_NEWIPC))
94 return ns;
96 new_ns = clone_ipc_ns(ns);
98 put_ipc_ns(ns);
99 return new_ns;
102 void free_ipc_ns(struct kref *kref)
104 struct ipc_namespace *ns;
106 ns = container_of(kref, struct ipc_namespace, kref);
107 sem_exit_ns(ns);
108 msg_exit_ns(ns);
109 shm_exit_ns(ns);
110 kfree(ns);
114 * ipc_init - initialise IPC subsystem
116 * The various system5 IPC resources (semaphores, messages and shared
117 * memory) are initialised
120 static int __init ipc_init(void)
122 sem_init();
123 msg_init();
124 shm_init();
125 return 0;
127 __initcall(ipc_init);
130 * ipc_init_ids - initialise IPC identifiers
131 * @ids: Identifier set
132 * @size: Number of identifiers
134 * Given a size for the ipc identifier range (limited below IPCMNI)
135 * set up the sequence range to use then allocate and initialise the
136 * array itself.
139 void ipc_init_ids(struct ipc_ids* ids, int size)
141 int i;
143 mutex_init(&ids->mutex);
145 if(size > IPCMNI)
146 size = IPCMNI;
147 ids->in_use = 0;
148 ids->max_id = -1;
149 ids->seq = 0;
151 int seq_limit = INT_MAX/SEQ_MULTIPLIER;
152 if(seq_limit > USHRT_MAX)
153 ids->seq_max = USHRT_MAX;
154 else
155 ids->seq_max = seq_limit;
158 ids->entries = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*size +
159 sizeof(struct ipc_id_ary));
161 if(ids->entries == NULL) {
162 printk(KERN_ERR "ipc_init_ids() failed, ipc service disabled.\n");
163 size = 0;
164 ids->entries = &ids->nullentry;
166 ids->entries->size = size;
167 for(i=0;i<size;i++)
168 ids->entries->p[i] = NULL;
171 #ifdef CONFIG_PROC_FS
172 static const struct file_operations sysvipc_proc_fops;
174 * ipc_init_proc_interface - Create a proc interface for sysipc types using a seq_file interface.
175 * @path: Path in procfs
176 * @header: Banner to be printed at the beginning of the file.
177 * @ids: ipc id table to iterate.
178 * @show: show routine.
180 void __init ipc_init_proc_interface(const char *path, const char *header,
181 int ids, int (*show)(struct seq_file *, void *))
183 struct proc_dir_entry *pde;
184 struct ipc_proc_iface *iface;
186 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
187 if (!iface)
188 return;
189 iface->path = path;
190 iface->header = header;
191 iface->ids = ids;
192 iface->show = show;
194 pde = create_proc_entry(path,
195 S_IRUGO, /* world readable */
196 NULL /* parent dir */);
197 if (pde) {
198 pde->data = iface;
199 pde->proc_fops = &sysvipc_proc_fops;
200 } else {
201 kfree(iface);
204 #endif
207 * ipc_findkey - find a key in an ipc identifier set
208 * @ids: Identifier set
209 * @key: The key to find
211 * Requires ipc_ids.mutex locked.
212 * Returns the identifier if found or -1 if not.
215 int ipc_findkey(struct ipc_ids* ids, key_t key)
217 int id;
218 struct kern_ipc_perm* p;
219 int max_id = ids->max_id;
222 * rcu_dereference() is not needed here
223 * since ipc_ids.mutex is held
225 for (id = 0; id <= max_id; id++) {
226 p = ids->entries->p[id];
227 if(p==NULL)
228 continue;
229 if (key == p->key)
230 return id;
232 return -1;
236 * Requires ipc_ids.mutex locked
238 static int grow_ary(struct ipc_ids* ids, int newsize)
240 struct ipc_id_ary* new;
241 struct ipc_id_ary* old;
242 int i;
243 int size = ids->entries->size;
245 if(newsize > IPCMNI)
246 newsize = IPCMNI;
247 if(newsize <= size)
248 return newsize;
250 new = ipc_rcu_alloc(sizeof(struct kern_ipc_perm *)*newsize +
251 sizeof(struct ipc_id_ary));
252 if(new == NULL)
253 return size;
254 new->size = newsize;
255 memcpy(new->p, ids->entries->p, sizeof(struct kern_ipc_perm *)*size);
256 for(i=size;i<newsize;i++) {
257 new->p[i] = NULL;
259 old = ids->entries;
262 * Use rcu_assign_pointer() to make sure the memcpyed contents
263 * of the new array are visible before the new array becomes visible.
265 rcu_assign_pointer(ids->entries, new);
267 __ipc_fini_ids(ids, old);
268 return newsize;
272 * ipc_addid - add an IPC identifier
273 * @ids: IPC identifier set
274 * @new: new IPC permission set
275 * @size: new size limit for the id array
277 * Add an entry 'new' to the IPC arrays. The permissions object is
278 * initialised and the first free entry is set up and the id assigned
279 * is returned. The list is returned in a locked state on success.
280 * On failure the list is not locked and -1 is returned.
282 * Called with ipc_ids.mutex held.
285 int ipc_addid(struct ipc_ids* ids, struct kern_ipc_perm* new, int size)
287 int id;
289 size = grow_ary(ids,size);
292 * rcu_dereference()() is not needed here since
293 * ipc_ids.mutex is held
295 for (id = 0; id < size; id++) {
296 if(ids->entries->p[id] == NULL)
297 goto found;
299 return -1;
300 found:
301 ids->in_use++;
302 if (id > ids->max_id)
303 ids->max_id = id;
305 new->cuid = new->uid = current->euid;
306 new->gid = new->cgid = current->egid;
308 new->seq = ids->seq++;
309 if(ids->seq > ids->seq_max)
310 ids->seq = 0;
312 spin_lock_init(&new->lock);
313 new->deleted = 0;
314 rcu_read_lock();
315 spin_lock(&new->lock);
316 ids->entries->p[id] = new;
317 return id;
321 * ipc_rmid - remove an IPC identifier
322 * @ids: identifier set
323 * @id: Identifier to remove
325 * The identifier must be valid, and in use. The kernel will panic if
326 * fed an invalid identifier. The entry is removed and internal
327 * variables recomputed. The object associated with the identifier
328 * is returned.
329 * ipc_ids.mutex and the spinlock for this ID is hold before this function
330 * is called, and remain locked on the exit.
333 struct kern_ipc_perm* ipc_rmid(struct ipc_ids* ids, int id)
335 struct kern_ipc_perm* p;
336 int lid = id % SEQ_MULTIPLIER;
337 BUG_ON(lid >= ids->entries->size);
340 * do not need a rcu_dereference()() here to force ordering
341 * on Alpha, since the ipc_ids.mutex is held.
343 p = ids->entries->p[lid];
344 ids->entries->p[lid] = NULL;
345 BUG_ON(p==NULL);
346 ids->in_use--;
348 if (lid == ids->max_id) {
349 do {
350 lid--;
351 if(lid == -1)
352 break;
353 } while (ids->entries->p[lid] == NULL);
354 ids->max_id = lid;
356 p->deleted = 1;
357 return p;
361 * ipc_alloc - allocate ipc space
362 * @size: size desired
364 * Allocate memory from the appropriate pools and return a pointer to it.
365 * NULL is returned if the allocation fails
368 void* ipc_alloc(int size)
370 void* out;
371 if(size > PAGE_SIZE)
372 out = vmalloc(size);
373 else
374 out = kmalloc(size, GFP_KERNEL);
375 return out;
379 * ipc_free - free ipc space
380 * @ptr: pointer returned by ipc_alloc
381 * @size: size of block
383 * Free a block created with ipc_alloc(). The caller must know the size
384 * used in the allocation call.
387 void ipc_free(void* ptr, int size)
389 if(size > PAGE_SIZE)
390 vfree(ptr);
391 else
392 kfree(ptr);
396 * rcu allocations:
397 * There are three headers that are prepended to the actual allocation:
398 * - during use: ipc_rcu_hdr.
399 * - during the rcu grace period: ipc_rcu_grace.
400 * - [only if vmalloc]: ipc_rcu_sched.
401 * Their lifetime doesn't overlap, thus the headers share the same memory.
402 * Unlike a normal union, they are right-aligned, thus some container_of
403 * forward/backward casting is necessary:
405 struct ipc_rcu_hdr
407 int refcount;
408 int is_vmalloc;
409 void *data[0];
413 struct ipc_rcu_grace
415 struct rcu_head rcu;
416 /* "void *" makes sure alignment of following data is sane. */
417 void *data[0];
420 struct ipc_rcu_sched
422 struct work_struct work;
423 /* "void *" makes sure alignment of following data is sane. */
424 void *data[0];
427 #define HDRLEN_KMALLOC (sizeof(struct ipc_rcu_grace) > sizeof(struct ipc_rcu_hdr) ? \
428 sizeof(struct ipc_rcu_grace) : sizeof(struct ipc_rcu_hdr))
429 #define HDRLEN_VMALLOC (sizeof(struct ipc_rcu_sched) > HDRLEN_KMALLOC ? \
430 sizeof(struct ipc_rcu_sched) : HDRLEN_KMALLOC)
432 static inline int rcu_use_vmalloc(int size)
434 /* Too big for a single page? */
435 if (HDRLEN_KMALLOC + size > PAGE_SIZE)
436 return 1;
437 return 0;
441 * ipc_rcu_alloc - allocate ipc and rcu space
442 * @size: size desired
444 * Allocate memory for the rcu header structure + the object.
445 * Returns the pointer to the object.
446 * NULL is returned if the allocation fails.
449 void* ipc_rcu_alloc(int size)
451 void* out;
453 * We prepend the allocation with the rcu struct, and
454 * workqueue if necessary (for vmalloc).
456 if (rcu_use_vmalloc(size)) {
457 out = vmalloc(HDRLEN_VMALLOC + size);
458 if (out) {
459 out += HDRLEN_VMALLOC;
460 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 1;
461 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
463 } else {
464 out = kmalloc(HDRLEN_KMALLOC + size, GFP_KERNEL);
465 if (out) {
466 out += HDRLEN_KMALLOC;
467 container_of(out, struct ipc_rcu_hdr, data)->is_vmalloc = 0;
468 container_of(out, struct ipc_rcu_hdr, data)->refcount = 1;
472 return out;
475 void ipc_rcu_getref(void *ptr)
477 container_of(ptr, struct ipc_rcu_hdr, data)->refcount++;
480 static void ipc_do_vfree(struct work_struct *work)
482 vfree(container_of(work, struct ipc_rcu_sched, work));
486 * ipc_schedule_free - free ipc + rcu space
487 * @head: RCU callback structure for queued work
489 * Since RCU callback function is called in bh,
490 * we need to defer the vfree to schedule_work().
492 static void ipc_schedule_free(struct rcu_head *head)
494 struct ipc_rcu_grace *grace =
495 container_of(head, struct ipc_rcu_grace, rcu);
496 struct ipc_rcu_sched *sched =
497 container_of(&(grace->data[0]), struct ipc_rcu_sched, data[0]);
499 INIT_WORK(&sched->work, ipc_do_vfree);
500 schedule_work(&sched->work);
504 * ipc_immediate_free - free ipc + rcu space
505 * @head: RCU callback structure that contains pointer to be freed
507 * Free from the RCU callback context.
509 static void ipc_immediate_free(struct rcu_head *head)
511 struct ipc_rcu_grace *free =
512 container_of(head, struct ipc_rcu_grace, rcu);
513 kfree(free);
516 void ipc_rcu_putref(void *ptr)
518 if (--container_of(ptr, struct ipc_rcu_hdr, data)->refcount > 0)
519 return;
521 if (container_of(ptr, struct ipc_rcu_hdr, data)->is_vmalloc) {
522 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
523 ipc_schedule_free);
524 } else {
525 call_rcu(&container_of(ptr, struct ipc_rcu_grace, data)->rcu,
526 ipc_immediate_free);
531 * ipcperms - check IPC permissions
532 * @ipcp: IPC permission set
533 * @flag: desired permission set.
535 * Check user, group, other permissions for access
536 * to ipc resources. return 0 if allowed
539 int ipcperms (struct kern_ipc_perm *ipcp, short flag)
540 { /* flag will most probably be 0 or S_...UGO from <linux/stat.h> */
541 int requested_mode, granted_mode, err;
543 if (unlikely((err = audit_ipc_obj(ipcp))))
544 return err;
545 requested_mode = (flag >> 6) | (flag >> 3) | flag;
546 granted_mode = ipcp->mode;
547 if (current->euid == ipcp->cuid || current->euid == ipcp->uid)
548 granted_mode >>= 6;
549 else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
550 granted_mode >>= 3;
551 /* is there some bit set in requested_mode but not in granted_mode? */
552 if ((requested_mode & ~granted_mode & 0007) &&
553 !capable(CAP_IPC_OWNER))
554 return -1;
556 return security_ipc_permission(ipcp, flag);
560 * Functions to convert between the kern_ipc_perm structure and the
561 * old/new ipc_perm structures
565 * kernel_to_ipc64_perm - convert kernel ipc permissions to user
566 * @in: kernel permissions
567 * @out: new style IPC permissions
569 * Turn the kernel object @in into a set of permissions descriptions
570 * for returning to userspace (@out).
574 void kernel_to_ipc64_perm (struct kern_ipc_perm *in, struct ipc64_perm *out)
576 out->key = in->key;
577 out->uid = in->uid;
578 out->gid = in->gid;
579 out->cuid = in->cuid;
580 out->cgid = in->cgid;
581 out->mode = in->mode;
582 out->seq = in->seq;
586 * ipc64_perm_to_ipc_perm - convert old ipc permissions to new
587 * @in: new style IPC permissions
588 * @out: old style IPC permissions
590 * Turn the new style permissions object @in into a compatibility
591 * object and store it into the @out pointer.
594 void ipc64_perm_to_ipc_perm (struct ipc64_perm *in, struct ipc_perm *out)
596 out->key = in->key;
597 SET_UID(out->uid, in->uid);
598 SET_GID(out->gid, in->gid);
599 SET_UID(out->cuid, in->cuid);
600 SET_GID(out->cgid, in->cgid);
601 out->mode = in->mode;
602 out->seq = in->seq;
606 * So far only shm_get_stat() calls ipc_get() via shm_get(), so ipc_get()
607 * is called with shm_ids.mutex locked. Since grow_ary() is also called with
608 * shm_ids.mutex down(for Shared Memory), there is no need to add read
609 * barriers here to gurantee the writes in grow_ary() are seen in order
610 * here (for Alpha).
612 * However ipc_get() itself does not necessary require ipc_ids.mutex down. So
613 * if in the future ipc_get() is used by other places without ipc_ids.mutex
614 * down, then ipc_get() needs read memery barriers as ipc_lock() does.
616 struct kern_ipc_perm* ipc_get(struct ipc_ids* ids, int id)
618 struct kern_ipc_perm* out;
619 int lid = id % SEQ_MULTIPLIER;
620 if(lid >= ids->entries->size)
621 return NULL;
622 out = ids->entries->p[lid];
623 return out;
626 struct kern_ipc_perm* ipc_lock(struct ipc_ids* ids, int id)
628 struct kern_ipc_perm* out;
629 int lid = id % SEQ_MULTIPLIER;
630 struct ipc_id_ary* entries;
632 rcu_read_lock();
633 entries = rcu_dereference(ids->entries);
634 if(lid >= entries->size) {
635 rcu_read_unlock();
636 return NULL;
638 out = entries->p[lid];
639 if(out == NULL) {
640 rcu_read_unlock();
641 return NULL;
643 spin_lock(&out->lock);
645 /* ipc_rmid() may have already freed the ID while ipc_lock
646 * was spinning: here verify that the structure is still valid
648 if (out->deleted) {
649 spin_unlock(&out->lock);
650 rcu_read_unlock();
651 return NULL;
653 return out;
656 void ipc_lock_by_ptr(struct kern_ipc_perm *perm)
658 rcu_read_lock();
659 spin_lock(&perm->lock);
662 void ipc_unlock(struct kern_ipc_perm* perm)
664 spin_unlock(&perm->lock);
665 rcu_read_unlock();
668 int ipc_buildid(struct ipc_ids* ids, int id, int seq)
670 return SEQ_MULTIPLIER*seq + id;
673 int ipc_checkid(struct ipc_ids* ids, struct kern_ipc_perm* ipcp, int uid)
675 if(uid/SEQ_MULTIPLIER != ipcp->seq)
676 return 1;
677 return 0;
680 #ifdef __ARCH_WANT_IPC_PARSE_VERSION
684 * ipc_parse_version - IPC call version
685 * @cmd: pointer to command
687 * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
688 * The @cmd value is turned from an encoding command and version into
689 * just the command code.
692 int ipc_parse_version (int *cmd)
694 if (*cmd & IPC_64) {
695 *cmd ^= IPC_64;
696 return IPC_64;
697 } else {
698 return IPC_OLD;
702 #endif /* __ARCH_WANT_IPC_PARSE_VERSION */
704 #ifdef CONFIG_PROC_FS
705 struct ipc_proc_iter {
706 struct ipc_namespace *ns;
707 struct ipc_proc_iface *iface;
710 static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
712 struct ipc_proc_iter *iter = s->private;
713 struct ipc_proc_iface *iface = iter->iface;
714 struct kern_ipc_perm *ipc = it;
715 loff_t p;
716 struct ipc_ids *ids;
718 ids = iter->ns->ids[iface->ids];
720 /* If we had an ipc id locked before, unlock it */
721 if (ipc && ipc != SEQ_START_TOKEN)
722 ipc_unlock(ipc);
725 * p = *pos - 1 (because id 0 starts at position 1)
726 * + 1 (because we increment the position by one)
728 for (p = *pos; p <= ids->max_id; p++) {
729 if ((ipc = ipc_lock(ids, p)) != NULL) {
730 *pos = p + 1;
731 return ipc;
735 /* Out of range - return NULL to terminate iteration */
736 return NULL;
740 * File positions: pos 0 -> header, pos n -> ipc id + 1.
741 * SeqFile iterator: iterator value locked shp or SEQ_TOKEN_START.
743 static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
745 struct ipc_proc_iter *iter = s->private;
746 struct ipc_proc_iface *iface = iter->iface;
747 struct kern_ipc_perm *ipc;
748 loff_t p;
749 struct ipc_ids *ids;
751 ids = iter->ns->ids[iface->ids];
754 * Take the lock - this will be released by the corresponding
755 * call to stop().
757 mutex_lock(&ids->mutex);
759 /* pos < 0 is invalid */
760 if (*pos < 0)
761 return NULL;
763 /* pos == 0 means header */
764 if (*pos == 0)
765 return SEQ_START_TOKEN;
767 /* Find the (pos-1)th ipc */
768 for (p = *pos - 1; p <= ids->max_id; p++) {
769 if ((ipc = ipc_lock(ids, p)) != NULL) {
770 *pos = p + 1;
771 return ipc;
774 return NULL;
777 static void sysvipc_proc_stop(struct seq_file *s, void *it)
779 struct kern_ipc_perm *ipc = it;
780 struct ipc_proc_iter *iter = s->private;
781 struct ipc_proc_iface *iface = iter->iface;
782 struct ipc_ids *ids;
784 /* If we had a locked segment, release it */
785 if (ipc && ipc != SEQ_START_TOKEN)
786 ipc_unlock(ipc);
788 ids = iter->ns->ids[iface->ids];
789 /* Release the lock we took in start() */
790 mutex_unlock(&ids->mutex);
793 static int sysvipc_proc_show(struct seq_file *s, void *it)
795 struct ipc_proc_iter *iter = s->private;
796 struct ipc_proc_iface *iface = iter->iface;
798 if (it == SEQ_START_TOKEN)
799 return seq_puts(s, iface->header);
801 return iface->show(s, it);
804 static struct seq_operations sysvipc_proc_seqops = {
805 .start = sysvipc_proc_start,
806 .stop = sysvipc_proc_stop,
807 .next = sysvipc_proc_next,
808 .show = sysvipc_proc_show,
811 static int sysvipc_proc_open(struct inode *inode, struct file *file)
813 int ret;
814 struct seq_file *seq;
815 struct ipc_proc_iter *iter;
817 ret = -ENOMEM;
818 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
819 if (!iter)
820 goto out;
822 ret = seq_open(file, &sysvipc_proc_seqops);
823 if (ret)
824 goto out_kfree;
826 seq = file->private_data;
827 seq->private = iter;
829 iter->iface = PDE(inode)->data;
830 iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
831 out:
832 return ret;
833 out_kfree:
834 kfree(iter);
835 goto out;
838 static int sysvipc_proc_release(struct inode *inode, struct file *file)
840 struct seq_file *seq = file->private_data;
841 struct ipc_proc_iter *iter = seq->private;
842 put_ipc_ns(iter->ns);
843 return seq_release_private(inode, file);
846 static const struct file_operations sysvipc_proc_fops = {
847 .open = sysvipc_proc_open,
848 .read = seq_read,
849 .llseek = seq_lseek,
850 .release = sysvipc_proc_release,
852 #endif /* CONFIG_PROC_FS */