[PATCH 12/23] user: Free user_ns one RCU grace period after final counter put

From: Kirill Tkhai
Date: Thu Jul 30 2020 - 08:00:35 EST


This is needed to link user_ns into ns_idr in next patch.

Signed-off-by: Kirill Tkhai <ktkhai@xxxxxxxxxxxxx>
---
include/linux/user_namespace.h | 5 ++++-
kernel/user_namespace.c | 9 ++++++++-
2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h
index 64cf8ebdc4ec..58fede304201 100644
--- a/include/linux/user_namespace.h
+++ b/include/linux/user_namespace.h
@@ -79,7 +79,10 @@ struct user_namespace {
#ifdef CONFIG_PERSISTENT_KEYRINGS
struct key *persistent_keyring_register;
#endif
- struct work_struct work;
+ union {
+ struct work_struct work;
+ struct rcu_head rcu;
+ };
#ifdef CONFIG_SYSCTL
struct ctl_table_set set;
struct ctl_table_header *sysctls;
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index 7c2bbe8f3e45..367a942bb484 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -171,6 +171,13 @@ int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
return err;
}

+static void free_user_ns_rcu(struct rcu_head *head)
+{
+ struct user_namespace *ns = container_of(head, struct user_namespace,
+ rcu);
+ kmem_cache_free(user_ns_cachep, ns);
+}
+
static void free_user_ns(struct work_struct *work)
{
struct user_namespace *parent, *ns =
@@ -194,7 +201,7 @@ static void free_user_ns(struct work_struct *work)
retire_userns_sysctls(ns);
key_free_user_ns(ns);
ns_free_inum(&ns->ns);
- kmem_cache_free(user_ns_cachep, ns);
+ call_rcu(&ns->rcu, free_user_ns_rcu);
dec_user_namespaces(ucounts);
ns = parent;
} while (refcount_dec_and_test(&parent->ns.count));