[RFC PATCH v3 03/11] NFSv4: Clear idmap keyring cache when the last client unloads

From: Jarkko Sakkinen

Date: Wed Nov 04 2026 - 22:07:26 EST


id_resolver_cache->thread_keyring caches id_resolver_legacy keys up until
nfs_idmap_quit() tears down the keyring.

Once keys pin to the kernel module of their key type, the cached keys
prevent module_exit() from ever happening.

Track the active idmap instances under a mutex and clear the idmap cache
keyring once the last client has been destroyed.

Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
fs/nfs/nfs4idmap.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)

diff --git a/fs/nfs/nfs4idmap.c b/fs/nfs/nfs4idmap.c
index bc397110d977..c88444cc6bf6 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;

+/* Serializes the idmap cache. */
+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