[PATCH v7 05/14] NFSv4: Clear idmap keyring cache when the last client unloads
From: Jarkko Sakkinen
Date: Sun Nov 29 2026 - 12:06:56 EST
id_resolver_cache->thread_keyring caches id_resolver and
id_resolver_legacy keys up until nfs_idmap_quit() tears down the keyring.
Once keys pin the kernel module of their key type, the cached keys
prevent module_exit() from ever happening.
Track active idmap instances with an atomic counter and clear the idmap
cache keyring via a work item once the last client is destroyed,
flushing synchronously when called from normal process context while
keeping rpciod and nfsiod release paths non-blocking and reclaim-safe.
Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
fs/nfs/nfs4idmap.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/fs/nfs/nfs4idmap.c b/fs/nfs/nfs4idmap.c
index bc397110d977..b97f68ed209d 100644
--- a/fs/nfs/nfs4idmap.c
+++ b/fs/nfs/nfs4idmap.c
@@ -47,6 +47,10 @@
#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 <linux/sched.h>
+#include <linux/workqueue.h>
#include "internal.h"
#include "netns.h"
@@ -58,6 +62,12 @@
static const struct cred *id_resolver_cache;
static struct key_type key_type_id_resolver_legacy;
+/* Serializes new idmap admission with deferred idmap cache clearing. */
+static DEFINE_MUTEX(nfs_idmap_cache_mutex);
+static atomic_t nfs_idmap_users = ATOMIC_INIT(0);
+static void nfs_idmap_clear_workfn(struct work_struct *work);
+static DECLARE_WORK(nfs_idmap_clear_work, nfs_idmap_clear_workfn);
+
struct idmap_legacy_upcalldata {
struct rpc_pipe_msg pipe_msg;
struct idmap_msg idmap_msg;
@@ -242,6 +252,7 @@ int nfs_idmap_init(void)
void nfs_idmap_quit(void)
{
+ cancel_work_sync(&nfs_idmap_clear_work);
key_revoke(id_resolver_cache->thread_keyring);
unregister_key_type(&key_type_id_resolver);
unregister_key_type(&key_type_id_resolver_legacy);
@@ -469,7 +480,10 @@ nfs_idmap_new(struct nfs_client *clp)
if (error)
goto err_destroy_pipe;
+ mutex_lock(&nfs_idmap_cache_mutex);
+ atomic_inc(&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);
@@ -479,6 +493,24 @@ nfs_idmap_new(struct nfs_client *clp)
return error;
}
+static void nfs_idmap_clear_workfn(struct work_struct *work)
+{
+ int ret;
+
+ mutex_lock(&nfs_idmap_cache_mutex);
+ if (!atomic_read(&nfs_idmap_users)) {
+ 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(&key_type_id_resolver);
+ key_gc_flush(&key_type_id_resolver_legacy);
+ }
+ }
+ mutex_unlock(&nfs_idmap_cache_mutex);
+}
+
void
nfs_idmap_delete(struct nfs_client *clp)
{
@@ -493,6 +525,14 @@ nfs_idmap_delete(struct nfs_client *clp)
rpc_destroy_pipe_data(idmap->idmap_pipe);
put_user_ns(idmap->user_ns);
kfree(idmap);
+
+ if (atomic_dec_and_test(&nfs_idmap_users)) {
+ schedule_work(&nfs_idmap_clear_work);
+ if (!(current->flags &
+ (PF_WQ_WORKER | PF_MEMALLOC | PF_MEMALLOC_NOFS |
+ PF_MEMALLOC_NOIO)))
+ flush_work(&nfs_idmap_clear_work);
+ }
}
static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
--
2.47.3