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
88 #include <linux/workqueue.h>
99 #include <linux/threads.h>
1010 #include <linux/nsproxy.h>
11−#include <linux/kref.h>
1211 #include <linux/ns_common.h>
1312 #include <linux/idr.h>
1413
@@ -18,7 +17,6 @@ · 3 unmodified lines
1817 struct fs_pin;
1918
2019 struct pid_namespace {
21− struct kref kref;
2220 struct idr idr;
2321 struct rcu_head rcu;
2422 unsigned int pid_allocated;
@@ -43,7 +41,7 @@ extern struct pid_namespace init_pid_ns; · 18 unmodified lines
4341 static inline struct pid_namespace *get_pid_ns(struct pid_namespace *ns)
4442 {
4543 if (ns != &init_pid_ns)
46− kref_get(&ns->kref);
44+ refcount_inc(&ns->ns.count);
4745 return ns;
4846 }
4947
kernel/pid.c+1−1
@@ -72,7 +72,7 @@ int pid_max_max = PID_MAX_LIMIT; · 71 unmodified lines
7272 * the scheme scales to up to 4 million PIDs, runtime.
7373 */
7474 struct pid_namespace init_pid_ns = {
75− .kref = KREF_INIT(2),
75+ .ns.count = REFCOUNT_INIT(2),
7676 .idr = IDR_INIT(init_pid_ns.idr),
7777 .pid_allocated = PIDNS_ADDING,
7878 .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
102102 goto out_free_idr;
103103 ns->ns.ops = &pidns_operations;
104104
105− kref_init(&ns->kref);
105+ refcount_set(&ns->ns.count, 1);
106106 ns->level = level;
107107 ns->parent = get_pid_ns(parent_pid_ns);
108108 ns->user_ns = get_user_ns(user_ns);
@@ -148,22 +148,15 @@ struct pid_namespace *copy_pid_ns(unsigned long flags, · 39 unmodified lines
148148 return create_pid_namespace(user_ns, old_ns);
149149 }
150150
151−static 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−
159151 void put_pid_ns(struct pid_namespace *ns)
160152 {
161153 struct pid_namespace *parent;
162154
163155 while (ns != &init_pid_ns) {
164156 parent = ns->parent;
165− if (!kref_put(&ns->kref, free_pid_ns))
157+ if (!refcount_dec_and_test(&ns->ns.count))
166158 break;
159+ destroy_pid_namespace(ns);
167160 ns = parent;
168161 }
169162 }

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