๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #24192

[PATCH v7 05/14] NFSv4: Clear idmap keyring cache when the last client unloads

#24192
From: Jarkko Sakkinen
Date: Sun, 29 Nov 2026 17:05
106 lines
12383 bytes
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@kernel.org>
---
 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
Technical Details
Message-ID: <20261129170543.2915157-6-jarkko@kernel.org>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373593-linux-kernel-netdev=dewy.ru
References: <20261129170543.2915157-1-jarkko@kernel.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373593-linux-kernel-netdev=dewy.ru
From: jarkko@kernel.org (Jarkko Sakkinen)
Newsgroups: linux.kernel.netdev
Subject: [PATCH v7 05/14] NFSv4: Clear idmap keyring cache when the last client unloads
Date: Sun, 29 Nov 2026 19:05:34 +0200
Organization: Usenet Network
Message-ID: <20261129170543.2915157-6-jarkko@kernel.org>
References: <20261129170543.2915157-1-jarkko@kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1218474"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:SkC5H6Y1WTXe2ph3udV82EQVK8M= sha256:/clm2C+qJsgh7BQdOXroKgwdmUhU9q+1/ABxGdXdUro=
	sha1:schAdIdnO8YWQ4FWBl6kSCB/6A0= sha256:RmrLUq1ncmvFeVb5VAxwGKOpPlPv2MdEves6CLxY1pQ=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 6363E125F96; Sun, 29 Nov 2026 20:06:50 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id YFq_MJu2k_2c;
	Sun, 29 Nov 2026 20:06:47 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-373593-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id 2F372125F98; Sun, 29 Nov 2026 20:06:39 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id 771C72352E; Sun, 29 Nov 2026 17:06:34 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795971977;
	b=4LNhjEvCVSjyNmRfLd7Tk2FjS4BpfUBcKHdpnm4tOQPEIgV6rnX5X2x5xYguB6iBD/fk
	 q3vSzYDbs+9wm6CIJwctANcmZ0ylkM66vX3nxeitjmlAkp7Ut26xr8zvq8kPBp4Sg4WB3
	 skb3Pum80FzeD4jThsTd+q9bbbOmsEJ60i0CVLsjK0Z4v6gT5H/ZJAXDsUrbeMCYji417
	 mi6u9FPQS0j9Ktv97M/A7qqDokZOztj5Z8v9pK2vRkv68h28uVVod5BF2tOFXebo4KUlc
	 ZxRrMeqwflKbxKRgn2m87YJhGz1CGhrzsKag9/P5d4A10qOptf5AbmdmsS7VgkcfNcQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795971977;
	h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List:
	 List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
	 List-Owner:Precedence:MIME-Version:Content-Transfer-Encoding;
	bh=dOpKKqVJNeRvVt9GpV3xXxPTEvUHo51l/zA1M5wdTEs=;
	b=EyBjoGAEjJv2b+ia8vI50R/btfaT/S90/0fuWbCq0/spE1ASPVa9BmvgXBU3HiRgdSfo
	 9wDtgrV+/hbNPm6FvK+sKSWp6dYZwfbe61avt4lMEuBJv5wW859/m/iF2Rml34riqXItX
	 aIsLqMkvHQvPbw5A9HLhrCFeI6E0gWqN/lDAX+Skf+RUYZyOncmVvyZFWJ9l9V1corJNv
	 rc0wrvqciCKp+Ys/Nns/8htiM6j4RJHinGCLwyMLfQ+DPL3lRztsdGUnZWp3D5wh3/bvB
	 6a8GZEh5yvSTBpZp87TAuGcQNg03IKfbm/ePGp/Unchq5QjK6yZP8FkTD+bz6tJjKtQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=WSI6D8fW;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id DD575606A6BB;
	Sun, 29 Nov 2026 17:06:16 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=WSI6D8fW
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795971970;
	b=ZsmH6Z4KuFL4n+J3FxwQiQtVjbxomBXKoznF+a2WwtM3d19XRl75XKQSPtJtcYq6df8y
	 ya7aMyxi43V7hUCLtwAciq8O13fsLJLc1OrPffBAXqlROtojEOovxIO7QkMXQUEV1BE6I
	 oKiS3FQAzZlcQZizqDVgh+Qm5CZb+WVWhHHUU8tUng7WkjXB2Srq+jRasXCtAcuhxCWfo
	 eTSigc3ky7AkUS6u/NZ85KddoBTuS32dyW9+0ET9hvq4wCDFEUH5usT2sLiGPcMy7HQfi
	 Ed7+nBDR+5+HfUorPFw4aM7FXoTzM52/EEA/9U9z09qDv+adc//sSCUXhHChiQUKI2g==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795971970;
	h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer:
	 In-Reply-To:References:MIME-Version:Content-Transfer-Encoding;
	bh=dOpKKqVJNeRvVt9GpV3xXxPTEvUHo51l/zA1M5wdTEs=;
	b=ZBBFdNMHJXZbHOuAxrsVScrf/zQznwz0wJ5OWH4PIVH2TTdlc5/pXxB7Kfcbe1Onigur
	 Oeacj/vnvMq8lRQ43uj7pzFmy7QQpG5k4O3MqGx0bp488cyBv/0m+WS3eATqyXUo6jkX3
	 VQFOZsHmJ/onZVEtb/mh4n2nNA90Saa+/smRX4OzbaNPwU+2+SXLDBIlMvM5H/qlVWsR8
	 JysTo1i9lGZCFFZkyLQs6wZPeQ7EyX9cx7sLeia8QFcrtPJdPdWrOZxW+YlHr0YEH/29D
	 ciz06D/7kv44B5yyOHRqcSDF8EtJivM0XJRJJ/IC050L3cSxZrLr3gQH5thRUPvF0Pg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=WSI6D8fW;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id DA130606A32F;
	Sun, 29 Nov 2026 17:06:10 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id E6E3C1F00931;
	Sun, 29 Nov 2026 17:06:09 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795971970;
	bh=dOpKKqVJNeRvVt9GpV3xXxPTEvUHo51l/zA1M5wdTEs=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References;
	b=WSI6D8fWVDvOOMgjrBDsu3uWtJM3zVJ/cY2cNqA4I7NJtp78/vrkB8wRNur3Bope6
	 nnm7pl/gg3b0FeW/y9RCULzAPQAM4oiTdNDDCppcSivuWTNA3WVGLfsuMg6qvKLgL3
	 +p3791n+Y69/GCHpF0tNsBLGalwpWllKwfMY5eSYpQhuO64ZSaqnh1+x6ugjB9CmxT
	 D58LHN27E3ABlLo/rBx/uoHVmHabXd7Yorp5G6owy52U6tojhW75LyhWcB8KpOngJI
	 MR9AtW1dsebWhoTllZaqGnJG+5m/6RvoS3PCJ4eK5ImT1PocwD4MNhj740GHuXVS4A
	 VikBpN/Vb9O3w==
X-Cc: Jarkko Sakkinen <jarkko@kernel.org>,
	linux-afs@lists.infradead.org,
	linux-cifs@vger.kernel.org,
	linux-doc@vger.kernel.org,
	linux-integrity@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	linux-nfs@vger.kernel.org,
	linux-s390@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	netdev@vger.kernel.org,
	samba-technical@lists.samba.org,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Anna Schumaker <anna@kernel.org>,
	Bharath SM <bharathsm@microsoft.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	David Howells <dhowells@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Heiko Carstens <hca@linux.ibm.com>,
	Jakub Kicinski <kuba@kernel.org>,
	James Bottomley <James.Bottomley@HansenPartnership.com>,
	James Morris <jmorris@namei.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Marc Dionne <marc.dionne@auristor.com>,
	Marek Behรบn <kabel@kernel.org>,
	Mimi Zohar <zohar@linux.ibm.com>,
	Namjae Jeon <linkinjeon@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Paul Moore <paul@paul-moore.com>,
	Paulo Alcantara <pc@manguebit.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	Ronnie Sahlberg <ronniesahlberg@gmail.com>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Shyam Prasad N <sprasad@microsoft.com>,
	Simon Horman <horms@kernel.org>,
	Sumit Garg <sumit.garg@kernel.org>,
	Sven Schnelle <svens@linux.ibm.com>,
	Tom Talpey <tom@talpey.com>,
	Trond Myklebust <trondmy@kernel.org>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Willem de Bruijn <willemb@google.com>
X-Mailer: git-send-email 2.47.3
X-In-Reply-To: <20261129170543.2915157-1-jarkko@kernel.org>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list