Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cgroup: Use generic ns_common::count

Switch over cgroup 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.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/r/159644980994.604812.383801057081594972.stgit@localhost.localdomain
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>

authored by

Kirill Tkhai and committed by
Christian Brauner
f387882d 1a7b8969

+4 -5
+2 -3
include/linux/cgroup.h
··· 854 854 #endif /* CONFIG_CGROUP_DATA */ 855 855 856 856 struct cgroup_namespace { 857 - refcount_t count; 858 857 struct ns_common ns; 859 858 struct user_namespace *user_ns; 860 859 struct ucounts *ucounts; ··· 888 889 static inline void get_cgroup_ns(struct cgroup_namespace *ns) 889 890 { 890 891 if (ns) 891 - refcount_inc(&ns->count); 892 + refcount_inc(&ns->ns.count); 892 893 } 893 894 894 895 static inline void put_cgroup_ns(struct cgroup_namespace *ns) 895 896 { 896 - if (ns && refcount_dec_and_test(&ns->count)) 897 + if (ns && refcount_dec_and_test(&ns->ns.count)) 897 898 free_cgroup_ns(ns); 898 899 } 899 900
+1 -1
kernel/cgroup/cgroup.c
··· 199 199 200 200 /* cgroup namespace for init task */ 201 201 struct cgroup_namespace init_cgroup_ns = { 202 - .count = REFCOUNT_INIT(2), 202 + .ns.count = REFCOUNT_INIT(2), 203 203 .user_ns = &init_user_ns, 204 204 .ns.ops = &cgroupns_operations, 205 205 .ns.inum = PROC_CGROUP_INIT_INO,
+1 -1
kernel/cgroup/namespace.c
··· 32 32 kfree(new_ns); 33 33 return ERR_PTR(ret); 34 34 } 35 - refcount_set(&new_ns->count, 1); 35 + refcount_set(&new_ns->ns.count, 1); 36 36 new_ns->ns.ops = &cgroupns_operations; 37 37 return new_ns; 38 38 }