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

ipc: Use generic ns_common::count

Switch over ipc 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/159644978697.604812.16592754423881032385.stgit@localhost.localdomain
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>

authored by

Kirill Tkhai and committed by
Christian Brauner
137ec390 9a56493f

+4 -5
+1 -2
include/linux/ipc_namespace.h
··· 27 27 }; 28 28 29 29 struct ipc_namespace { 30 - refcount_t count; 31 30 struct ipc_ids ids[3]; 32 31 33 32 int sem_ctls[4]; ··· 127 128 static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns) 128 129 { 129 130 if (ns) 130 - refcount_inc(&ns->count); 131 + refcount_inc(&ns->ns.count); 131 132 return ns; 132 133 } 133 134
+1 -1
ipc/msgutil.c
··· 26 26 * and not CONFIG_IPC_NS. 27 27 */ 28 28 struct ipc_namespace init_ipc_ns = { 29 - .count = REFCOUNT_INIT(1), 29 + .ns.count = REFCOUNT_INIT(1), 30 30 .user_ns = &init_user_ns, 31 31 .ns.inum = PROC_IPC_INIT_INO, 32 32 #ifdef CONFIG_IPC_NS
+2 -2
ipc/namespace.c
··· 51 51 goto fail_free; 52 52 ns->ns.ops = &ipcns_operations; 53 53 54 - refcount_set(&ns->count, 1); 54 + refcount_set(&ns->ns.count, 1); 55 55 ns->user_ns = get_user_ns(user_ns); 56 56 ns->ucounts = ucounts; 57 57 ··· 164 164 */ 165 165 void put_ipc_ns(struct ipc_namespace *ns) 166 166 { 167 - if (refcount_dec_and_lock(&ns->count, &mq_lock)) { 167 + if (refcount_dec_and_lock(&ns->ns.count, &mq_lock)) { 168 168 mq_clear_sbinfo(ns); 169 169 spin_unlock(&mq_lock); 170 170