ipc: recompute msgmni on memory add / remove
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / ipc / namespace.c
1 /*
2 * linux/ipc/namespace.c
3 * Copyright (C) 2006 Pavel Emelyanov <xemul@openvz.org> OpenVZ, SWsoft Inc.
4 */
5
6 #include <linux/ipc.h>
7 #include <linux/msg.h>
8 #include <linux/ipc_namespace.h>
9 #include <linux/rcupdate.h>
10 #include <linux/nsproxy.h>
11 #include <linux/slab.h>
12
13 #include "util.h"
14
15 static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)
16 {
17 struct ipc_namespace *ns;
18
19 ns = kmalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
20 if (ns == NULL)
21 return ERR_PTR(-ENOMEM);
22
23 atomic_inc(&nr_ipc_ns);
24
25 sem_init_ns(ns);
26 msg_init_ns(ns);
27 shm_init_ns(ns);
28
29 register_ipcns_notifier(ns);
30
31 kref_init(&ns->kref);
32 return ns;
33 }
34
35 struct ipc_namespace *copy_ipcs(unsigned long flags, struct ipc_namespace *ns)
36 {
37 struct ipc_namespace *new_ns;
38
39 BUG_ON(!ns);
40 get_ipc_ns(ns);
41
42 if (!(flags & CLONE_NEWIPC))
43 return ns;
44
45 new_ns = clone_ipc_ns(ns);
46
47 put_ipc_ns(ns);
48 return new_ns;
49 }
50
51 /*
52 * free_ipcs - free all ipcs of one type
53 * @ns: the namespace to remove the ipcs from
54 * @ids: the table of ipcs to free
55 * @free: the function called to free each individual ipc
56 *
57 * Called for each kind of ipc when an ipc_namespace exits.
58 */
59 void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
60 void (*free)(struct ipc_namespace *, struct kern_ipc_perm *))
61 {
62 struct kern_ipc_perm *perm;
63 int next_id;
64 int total, in_use;
65
66 down_write(&ids->rw_mutex);
67
68 in_use = ids->in_use;
69
70 for (total = 0, next_id = 0; total < in_use; next_id++) {
71 perm = idr_find(&ids->ipcs_idr, next_id);
72 if (perm == NULL)
73 continue;
74 ipc_lock_by_ptr(perm);
75 free(ns, perm);
76 total++;
77 }
78 up_write(&ids->rw_mutex);
79 }
80
81 void free_ipc_ns(struct kref *kref)
82 {
83 struct ipc_namespace *ns;
84
85 ns = container_of(kref, struct ipc_namespace, kref);
86 /*
87 * Unregistering the hotplug notifier at the beginning guarantees
88 * that the ipc namespace won't be freed while we are inside the
89 * callback routine. Since the blocking_notifier_chain_XXX routines
90 * hold a rw lock on the notifier list, unregister_ipcns_notifier()
91 * won't take the rw lock before blocking_notifier_call_chain() has
92 * released the rd lock.
93 */
94 unregister_ipcns_notifier(ns);
95 sem_exit_ns(ns);
96 msg_exit_ns(ns);
97 shm_exit_ns(ns);
98 kfree(ns);
99 atomic_dec(&nr_ipc_ns);
100 }