History

pid: Use generic ns_common::count

Switch over pid namespaces to use the newly introduced common lifetime counter. Currently every namespace type has its own lifetime counter which is stored in the specific namespace struct. The lifetime counters are used identically for all namespaces types. Namespaces may of course have additional unrelated counters and these are not altered. This introduces a common lifetime counter into struct ns_common. The ns_common struct encompasses information that all namespaces share. That should include the lifetime counter since its common for all of them. It also allows us to unify the type of the counters across all namespaces. Most of them use refcount_t but one uses atomic_t and at least one uses kref. Especially the last one doesn't make much sense since it's just a wrapper around refcount_t since 2016 and actually complicates cleanup operations by having to use container_of() to cast the correct namespace struct out of struct ns_common. Having the lifetime counter for the namespaces in one place reduces maintenance cost. Not just because after switching all namespaces over we will have removed more code than we added but also because the logic is more easily understandable and we indicate to the user that the basic lifetime requirements for all namespaces are currently identical.

Kirill Tkhai committed 6 years ago

Commit 8eb71d91 parent 137ec393 files · +5 −14Browse files

Unified · Split

3 files
  1. include/linux/pid_namespace.h +1 −3
  2. kernel/pid.c +1 −1
  3. kernel/pid_namespace.c +3 −10
include/linux/pid_namespace.h+1−3
@@ -8,7 +8,6 @@ · 7 unmodified lines
8#include <linux/workqueue.h>8#include <linux/workqueue.h>
9#include <linux/threads.h>9#include <linux/threads.h>
10#include <linux/nsproxy.h>10#include <linux/nsproxy.h>
11#include <linux/kref.h>
12#include <linux/ns_common.h>11#include <linux/ns_common.h>
13#include <linux/idr.h>12#include <linux/idr.h>
14 13
@@ -18,7 +17,6 @@ · 3 unmodified lines
18struct fs_pin;17struct fs_pin;
19 18
20struct pid_namespace {19struct pid_namespace {
21 struct kref kref;
22 struct idr idr;20 struct idr idr;
23 struct rcu_head rcu;21 struct rcu_head rcu;
24 unsigned int pid_allocated;22 unsigned int pid_allocated;
@@ -43,7 +41,7 @@ extern struct pid_namespace init_pid_ns; · 18 unmodified lines
43static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)41static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
44{42{
45 if (ns != &init_pid_ns)43 if (ns != &init_pid_ns)
46 kref_get(&ns->kref);44 refcount_inc(&ns->ns.count);
47 return ns;45 return ns;
48}46}
49 47
kernel/pid.c+1−1
@@ -72,7 +72,7 @@ int pid_max_max = PID_MAX_LIMIT; · 71 unmodified lines
72 * the scheme scales to up to 4 million PIDs, runtime.72 * the scheme scales to up to 4 million PIDs, runtime.
73 */73 */
74struct pid_namespace init_pid_ns = {74struct pid_namespace init_pid_ns = {
75 .kref = KREF_INIT(2),75 .ns.count = REFCOUNT_INIT(2),
76 .idr = IDR_INIT(init_pid_ns.idr),76 .idr = IDR_INIT(init_pid_ns.idr),
77 .pid_allocated = PIDNS_ADDING,77 .pid_allocated = PIDNS_ADDING,
78 .level = 0,78 .level = 0,
kernel/pid_namespace.c+3−10
@@ -102,7 +102,7 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns · 101 unmodified lines
102 goto out_free_idr;102 goto out_free_idr;
103 ns->ns.ops = &pidns_operations;103 ns->ns.ops = &pidns_operations;
104 104
105 kref_init(&ns->kref);105 refcount_set(&ns->ns.count, 1);
106 ns->level = level;106 ns->level = level;
107 ns->parent = get_pid_ns(parent_pid_ns);107 ns->parent = get_pid_ns(parent_pid_ns);
108 ns->user_ns = get_user_ns(user_ns);108 ns->user_ns = get_user_ns(user_ns);
@@ -148,22 +148,15 @@ struct pid_namespace *copy_pid_ns(unsigned long flags, · 39 unmodified lines
148 return create_pid_namespace(user_ns, old_ns);148 return create_pid_namespace(user_ns, old_ns);
149}149}
150 150
151static void free_pid_ns(struct kref *kref)
152{
153 struct pid_namespace *ns;
154
155 ns = container_of(kref, struct pid_namespace, kref);
156 destroy_pid_namespace(ns);
157}
158
159void put_pid_ns(struct pid_namespace *ns)151void put_pid_ns(struct pid_namespace *ns)
160{152{
161 struct pid_namespace *parent;153 struct pid_namespace *parent;
162 154
163 while (ns != &init_pid_ns) {155 while (ns != &init_pid_ns) {
164 parent = ns->parent;156 parent = ns->parent;
165 if (!kref_put(&ns->kref, free_pid_ns))157 if (!refcount_dec_and_test(&ns->ns.count))
166 break;158 break;
159 destroy_pid_namespace(ns);
167 ns = parent;160 ns = parent;
168 }161 }
169}162}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t