id_resolver_cache->thread_keyring caches id_resolver_legacy keys until
nfs_idmap_quit() tears down the keyring from module_exit(). Once live
keys pin the key type, cached keys keep nfsv4 pinned so module_exit() is
never reached after all mounts are gone.

Track active idmap instances under a mutex and clear the idmap cache
keyring when the last client is destroyed.

Signed-off-by: Jarkko Sakkinen <[email protected]>
---
 fs/nfs/nfs4idmap.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/fs/nfs/nfs4idmap.c b/fs/nfs/nfs4idmap.c
index bc397110d977..9e3a2255be1e 100644
--- a/fs/nfs/nfs4idmap.c
+++ b/fs/nfs/nfs4idmap.c
@@ -47,6 +47,8 @@
 #include <keys/request_key_auth-type.h>
 #include <linux/module.h>
 #include <linux/user_namespace.h>
+#include <linux/mutex.h>
+#include <linux/ratelimit.h>
 
 #include "internal.h"
 #include "netns.h"
@@ -58,6 +60,10 @@
 static const struct cred *id_resolver_cache;
 static struct key_type key_type_id_resolver_legacy;
 
+/* Serialize idmap admission with the last client's cache clear. */
+static DEFINE_MUTEX(nfs_idmap_cache_mutex);
+static unsigned int nfs_idmap_users;
+
 struct idmap_legacy_upcalldata {
        struct rpc_pipe_msg pipe_msg;
        struct idmap_msg idmap_msg;
@@ -469,7 +475,10 @@ nfs_idmap_new(struct nfs_client *clp)
        if (error)
                goto err_destroy_pipe;
 
+       mutex_lock(&nfs_idmap_cache_mutex);
+       nfs_idmap_users++;
        clp->cl_idmap = idmap;
+       mutex_unlock(&nfs_idmap_cache_mutex);
        return 0;
 err_destroy_pipe:
        rpc_destroy_pipe_data(idmap->idmap_pipe);
@@ -483,6 +492,7 @@ void
 nfs_idmap_delete(struct nfs_client *clp)
 {
        struct idmap *idmap = clp->cl_idmap;
+       int ret;
 
        if (!idmap)
                return;
@@ -493,6 +503,17 @@ nfs_idmap_delete(struct nfs_client *clp)
        rpc_destroy_pipe_data(idmap->idmap_pipe);
        put_user_ns(idmap->user_ns);
        kfree(idmap);
+
+       mutex_lock(&nfs_idmap_cache_mutex);
+       if (--nfs_idmap_users == 0) {
+               ret = keyring_clear(id_resolver_cache->thread_keyring);
+               if (ret)
+                       pr_warn_ratelimited("NFS: Failed to clear idmap cache: 
%d; cache pins remain\n",
+                                           ret);
+               else
+                       key_gc_flush();
+       }
+       mutex_unlock(&nfs_idmap_cache_mutex);
 }
 
 static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
-- 
2.47.3


Reply via email to