| Message ID | 20261107-submit-rawnand-retained-handle-lifetime-v1-v1-2-0181ef0cc4c8@gmail.com (mailing list archive) |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-sunxi+bounces-27318-sunxi=pue.re@lists.linux.dev>
X-Original-To: noreply@patchwork.local
Delivered-To: noreply@patchwork.local
Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
by mxe881.netcup.net (Postfix) with ESMTPS id 9A66C1C70E2
for <noreply@patchwork.local>; Sat, 7 Nov 2026 11:19:11 +0100 (CET)
Authentication-Results: mxe881;
dkim=pass header.d=gmail.com;
spf=pass (sender IP is 104.64.211.4)
smtp.mailfrom=linux-sunxi+bounces-27318-noreply=patchwork.local@lists.linux.dev
smtp.helo=sin.lore.kernel.org
Received-SPF: pass (mxe881: domain of lists.linux.dev designates 104.64.211.4
as permitted sender) client-ip=104.64.211.4;
envelope-from=linux-sunxi+bounces-27318-noreply=patchwork.local@lists.linux.dev;
helo=sin.lore.kernel.org;
Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org
[100.77.114.92])
by sin.lore.kernel.org (Postfix) with ESMTP id 6BBED3095F
for <noreply@patchwork.local>; Sat, 7 Nov 2026 10:16:50 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1794046592;
b=FFqr+TCiNaQRphZZg2i0UqaOj5+QRlF2vT9e0kbwO/C/yQSmL+qr3ERsoJAeAi+eMS46
f88e0QqTAMydDLEycABV/6YjFUBmhZz7kEElpHHwhMkaPdTo2cap4U2dhQtp2QqqCQQCj
IjVg6k/XufIOqjdnGEjOwynMjO9Y8Ho1AqjLstmcSWCzQG97/uZIGFHVh54a+OxwRjxAo
CDBDakk8W0BEZLoY2vVcpW8KT+lGV1FFFIeRhjAVxBy3wodAxqp0ItVqTY9lgidwMeq9I
s7ob5CUmOIa7fkG2AeZR/1HU5hnDPNxMPSNM8Ut0Ykqsg3q3jqZy2YFNkKe2Id+mn3g==
ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1794046592;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State:
X-Gm-Gg:X-Received:Received:From:Date:Subject:X-Mailing-List:List-Id:
List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
Precedence:MIME-Version:Content-Type:Content-Transfer-Encoding:
Message-Id:References:In-Reply-To:To:Cc:X-Mailer;
bh=Ir8ocEsY2ITCHB73OM8DOt1TO1FexBL3HI8zDqPQjm4=;
b=NNsAFtDZUhEz8l0ecqy46dVjEQKan8MiRlc0cueD3m0noGGrgDpzSvqtpzgfFJLiPhCT
fkFJbfvjdnJTD6Bw5Qicex3J3RfM8GsE95lP18PNrrvgX14efGNGzy/Q4khY2182kRjlG
Qf8z1wEPsK5EQQuZUgE81KvERy/KOgX8G2WnIAWt9F/9AJzuX9w2P8R1WZL3vSW3HR9XU
QvWYVTKtRjKjacAujIc9HjQerHKX2U8e5FNrstrxPZYn3qt66eYiECZ2dJAG71cNC9NnE
d6O/DMKN6um9oMPksZp8Zu7ZoL2ITwr3fTdGRwddSLep+dswn/jZrtrSw9LrHpKfScQ==
ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256
header.s=20260707 header.b=NOfG8kNm;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 228CB606A335;
Sat, 7 Nov 2026 10:16:32 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com
header.a=rsa-sha256 header.s=20260707 header.b=NOfG8kNm
X-Original-To: linux-sunxi@lists.linux.dev
Authentication-Results: smtp.subspace.kernel.org;
arc=none smtp.remote-ip=209.85.210.52
ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1794046592;
b=Cl7raPpQyei9uLCvrduEGbBOo341fZot3gFemXc1m0Mh9H0Q+5bld+PDYrzBfFKlgnZM
8FCBBFw4/cesp2qvm5nlPNkCmxt/1KW1lA0NHN/PFQzGTUr03qokvfnXnmulkPdc7v2+O
GtaNY7paBlUWp2z2BakcDYL+2xCOMqLmQIkgHJ+n1UzZ3SZtQMxcZEy00U4FnmQy+YYyY
2wA3Di8vJGTOlcClUkecmFrKLXy4aWJVSK9y5etR3QTbuojnLOHBFlF4Xrqzt2ediSTLA
Csz/GjxuicWFgMZ9qB0FoPj1dIItU8U6CJDgcukhyulClBxWkcwrBE5s7tsTOxJQpig==
ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1794046592;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received:
From:Date:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:
Message-Id:References:In-Reply-To:To:Cc:X-Mailer;
bh=Ir8ocEsY2ITCHB73OM8DOt1TO1FexBL3HI8zDqPQjm4=;
b=vVMtdTS+hzGGbO+rNsgbpXLFFJzRfxs5dqgCyW/hcrc6MMkqRv61pxDpRsF+wW8YHGEI
ujrPvkWqneOBmYmiRxYhbcvu2SxflHNLeag7jbWf0hRo55VjYON4H8W37eV/MJYD2VfOF
ywdDCsQgPem3a8i1BSHLTicT1CNW5wQu4daYij7WwxORieHws9wA6bu0SrzIzMDeBvPjJ
KwkbaNdXjMs9rkcMRduU4MTrfnG1z2gayKUSocg9FSkBI0CnzGDBhed5YCX6bbCxEALlV
lASeAv4O4OHGiLFaohwcCF0Gkdbnjn7qDFW68gHgWhKxwhsFoSVKA5gw7tVwlk4I/5g==
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256
header.s=20260707 header.b=NOfG8kNm;
arc=none smtp.remote-ip=209.85.210.52
Received: from mail-ot1-f52.google.com (mail-ot1-f52.google.com
[209.85.210.52])
(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest
SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD771606D83C
for <linux-sunxi@lists.linux.dev>; Sat, 7 Nov 2026 10:16:31 +0000 (UTC)
DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AD771606D83C
Authentication-Results: smtp.subspace.kernel.org;
dmarc=pass (p=none dis=none) header.from=gmail.com
Authentication-Results: smtp.subspace.kernel.org;
spf=pass smtp.mailfrom=gmail.com
Received: by mail-ot1-f52.google.com with SMTP id
46e09a7af769-854b6e2e41fso752568a34.3
for <linux-sunxi@lists.linux.dev>;
Sat, 07 Nov 2026 02:16:31 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1794046590; x=1794651390;
darn=lists.linux.dev;
h=cc:to:in-reply-to:references:message-id:content-transfer-encoding
:content-type:mime-version:subject:date:from:from:to:cc:subject:date
:message-id:reply-to:content-type;
bh=Ir8ocEsY2ITCHB73OM8DOt1TO1FexBL3HI8zDqPQjm4=;
b=NOfG8kNmH3Iqhb5vC5/2HMTUoAxgbZBzJMQowXMhl9ELIRQPF8+/9ch4SpXzj8LmUJ
iAdmRfcdjxR8rCBRfNOuz7S7rHuDFBRpfDKUALUrEas8aqxMJJ142j0ExlgjNLRuqHSx
S76hNWVBylMxIQhCM5DxBC8tymvPBvb6Uri/2ZwDw77/2jDbttzfxS0KxIyUbCDWOD9v
j945HD9apdVUtTFGP4KCKzs/wAghLnLzNa9rx3Zb2/b6A+OyvHIUPNQ43SMKd5bkuZTH
W2lEpYkKeCPH6yX9aMrIUf5LkqyVv/GSf1zMR41LQaMbmGhTSaxLPGyvn2XeWbnS64wC
I/JQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1794046590; x=1794651390;
h=cc:to:in-reply-to:references:message-id:content-transfer-encoding
:content-type:mime-version:subject:date:from:x-gm-gg
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
:content-type;
bh=Ir8ocEsY2ITCHB73OM8DOt1TO1FexBL3HI8zDqPQjm4=;
b=Ri9uESJqJrTMPv+jXtm8MB2Yq8sfpY6W96U9uUlEwggHPKPJ9vV3hoov14FcT7E5sK
07IYFLbVC/lk9BWdyEPLa8mqJP8DkUyfltcLjulwQ2sZaswOk3kHSlfHFMebKMcZlIWw
FAbJFB1zfFiyheLFFFHkRpXnzD7CQicteVK0FGfddTpdJ+ewbdQB+ehhcPD/oTw7yqy+
vy1Ie/t6jcMYM9pHm+lJWZKcCt2QLklJUNmGWs6s6RatCrdV6USPcQaEdUjE2qAhh2F1
uVS5DKXg775FImhmYqS5pNsg9azGNbL+ZeUnthWQXxHZI+C3EWOHgAEc6r63wJmpF+7B
gu1g==
X-Forwarded-Encrypted: i=1;
AKwUvBxWBAHZGO+La9gnUid46ZSF6S/GBE7s82LE20gWN6PTYE+FmdQ+WRnIbeD8K5s8+Xm3qUwcwqE1nmSivg==@lists.linux.dev
X-Gm-Message-State: APuU4Z+KAJQKPtoANPEbTc6BNqpwlLaGr4jHTr/vkHwvhiWJHErKkx0Z
aNxHdUqk6Db4Y2Jam1TzNtHiSRzTsD0wFL295Wp7HMEAaXqmC81SgxcL
X-Gm-Gg: AYBFou3UXFJyVlGeyRtWGZRhKYUQHqGBmMe8njMvl2DavQjYNad9ts/mCwpTm1jocAW
p/uvguMT5efipHON3F4q/eGmYMSDLcc8fg/YQ8YvY327a78Q8WsiNH62CZoXQLTjaLYzknqLH2i
pMYEWRwJ3rnJLq5R5iYCE9A2BokwHbfwjUDX6BR8zSRNkAvku//UFj57NfyeUn9xxxm9DTI0759
ajYXs6RqinBebQ4VpyVnC2YBaXWeySUVcbXRTR14E6YzrsfSGig0OatZt9POwrN4llIT5/FVcge
UEc/a86mHOCSI72SoqcrOY/5ZGeLohRIdKRIVgVHcpUH/8FJHvNAhlTyRdHpTJtps/H17KsNjSI
7xXiZBC4VkRrn/SBbNOeYaRjX3/bJq40vCPrytR/6m5nPvl4yTYRxdLms0blUgWgWPMHzYvkTMk
Nun+g1lZe+e0ivTiD7XtJpeKm4QEuw5kFF4R9Bf1aQwKEKSODS2LuXRyKKB1bk/6EiM52j/Wg6c
vyMGM78xsaHyzhqW1C6+XfcCdyd0a5buquTGzVWRTHld9kSbx0ORn/P6EjCxLTu3czsL7nGjVX5
faiIDZubW23XnJTJfWKN5vaOCbXO5KzmJQwYW/iwEMN7bXA7t8Mzi9shNpll0vZ03IhTEhGgPUX
ZR22qJnz6P9kQ3VcmDcNc3R/P3P0NHWYCVtJKLVRmPU62qNMwoffNYpxZQOJ5IqjuXXMNmX3BlT
8Uidd/QJII5VVEERShlHmAQQlU+x4RToPK9+wAJ61qocXdutPjik5b
X-Received: by 2002:a05:6830:4c05:b0:822:e9bf:6445 with SMTP id
46e09a7af769-85f1cf838e3mr5603674a34.9.1794046590411;
Sat, 07 Nov 2026 02:16:30 -0800 (PST)
Received: from [127.0.1.1] (174-29-1-49.hlrn.qwest.net. [174.29.1.49])
by smtp.gmail.com with ESMTPSA id
46e09a7af769-85f1dbef224sm3990269a34.12.2026.11.07.02.16.27
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Sat, 07 Nov 2026 02:16:29 -0800 (PST)
From: James Hilliard <james.hilliard1@gmail.com>
Date: Sat, 07 Nov 2026 03:15:59 -0700
Subject: [PATCH 02/58] nvmem: retain provider context through final device
release
X-Mailing-List: linux-sunxi@lists.linux.dev
List-Id: <linux-sunxi.lists.linux.dev>
List-Post: <mailto:linux-sunxi@lists.linux.dev>
List-Help: <mailto:linux-sunxi+help@lists.linux.dev>
List-Subscribe: <mailto:linux-sunxi+subscribe@lists.linux.dev>
List-Unsubscribe: <mailto:linux-sunxi+unsubscribe@lists.linux.dev>
List-Owner: <mailto:linux-sunxi+owner@lists.linux.dev>
Precedence: list
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Message-Id:
<20261107-submit-rawnand-retained-handle-lifetime-v1-v1-2-0181ef0cc4c8@gmail.com>
References:
<20261107-submit-rawnand-retained-handle-lifetime-v1-v1-0-0181ef0cc4c8@gmail.com>
In-Reply-To:
<20261107-submit-rawnand-retained-handle-lifetime-v1-v1-0-0181ef0cc4c8@gmail.com>
To: Miquel Raynal <miquel.raynal@bootlin.com>,
Richard Weinberger <richard@nod.at>, Vignesh Raghavendra <vigneshr@ti.com>,
Brian Norris <computersforpeace@gmail.com>,
Boris Brezillon <bbrezillon@kernel.org>,
Arseniy Krasnov <AVKrasnov@sberdevices.ru>, Chen-Yu Tsai <wens@kernel.org>,
Jernej Skrabec <jernej.skrabec@gmail.com>,
Samuel Holland <samuel@sholland.org>, Thomas Gleixner <tglx@kernel.org>,
Vitaly Wool <vitalywool@gmail.com>, David Woodhouse <dwmw2@infradead.org>,
Russell King <rmk+kernel@arm.linux.org.uk>,
David Woodhouse <David.Woodhouse@intel.com>,
Mike Rapoport <rppt@kernel.org>,
Maxime Coquelin <mcoquelin.stm32@gmail.com>,
Alexandre Torgue <alexandre.torgue@foss.st.com>,
Christophe Kerello <christophe.kerello@st.com>,
Jason Roberts <jason.e.roberts@intel.com>,
Michal Simek <michal.simek@amd.com>,
Boris Brezillon <boris.brezillon@collabora.com>,
Mason Yang <masonccyang@mxic.com.tw>,
Ramuthevar Vadivel Murugan <vadivel.muruganx.ramuthevar@linux.intel.com>,
Stefan Agner <stefan@agner.ch>, Lucas Stach <dev@lynxeye.de>,
Thierry Reding <thierry.reding@kernel.org>,
Jonathan Hunter <jonathanh@nvidia.com>, Dmitry Osipenko <digetx@gmail.com>,
Matthias Brugger <matthias.bgg@gmail.com>,
AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>,
Jorge Ramirez-Ortiz <jorge.ramirez@oss.qualcomm.com>,
Heiko Stuebner <heiko@sntech.de>, Yifeng Zhao <yifeng.zhao@rock-chips.com>,
Nicolas Ferre <nicolas.ferre@microchip.com>,
Alexandre Belloni <alexandre.belloni@bootlin.com>,
Claudiu Beznea <claudiu.beznea@tuxon.dev>,
Keguang Zhang <keguang.zhang@gmail.com>, Jacky Huang <ychuang3@nuvoton.com>,
Shan-Chun Hung <schung@nuvoton.com>, Frank Li <Frank.Li@nxp.com>,
Sascha Hauer <s.hauer@pengutronix.de>,
Pengutronix Kernel Team <kernel@pengutronix.de>,
Fabio Estevam <festevam@gmail.com>, Han Xu <han.xu@nxp.com>,
Liang Yang <liang.yang@amlogic.com>,
Neil Armstrong <neil.armstrong@linaro.org>,
Kevin Hilman <khilman@baylibre.com>, Jerome Brunet <jbrunet@baylibre.com>,
Martin Blumenstingl <martin.blumenstingl@googlemail.com>,
Manivannan Sadhasivam <mani@kernel.org>,
Kamal Dasu <kamal.dasu@broadcom.com>,
Broadcom internal kernel review list <bcm-kernel-feedback-list@broadcom.com>,
Vladimir Zapolskiy <vz@mleia.com>,
Piotr Wojtaszczyk <piotr.wojtaszczyk@timesys.com>,
Paul Cercueil <paul@crapouillou.net>,
Harvey Hunt <harveyhuntnexus@gmail.com>,
Maxim Levitsky <maximlevitsky@gmail.com>, Michael Walle <michael@walle.cc>,
Srinivas Kandagatla <srini@kernel.org>
Cc: Tze Yee Ng <tze.yee.ng@altera.com>, linux-mtd@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-sunxi@lists.linux.dev, linux-tegra@vger.kernel.org,
linux-mediatek@lists.infradead.org, linux-rockchip@lists.infradead.org,
linux-mips@vger.kernel.org, imx@lists.linux.dev,
linux-renesas-soc@vger.kernel.org, linux-amlogic@lists.infradead.org,
linux-arm-msm@vger.kernel.org, James Hilliard <james.hilliard1@gmail.com>
X-Mailer: b4 0.15.2
X-Rspamd-Server: rspamd-worker-8404
X-Spamd-Result: default: False [4.31 / 15.00];
RBL_SENDERSCORE(2.00)[104.64.211.4:from];
SUSPICIOUS_RECIPS(1.50)[];
DMARC_POLICY_SOFTFAIL(1.00)[gmail.com : SPF not aligned (relaxed),
No valid DKIM,none];
MAILLIST(-0.18)[generic];
MIME_GOOD(-0.10)[text/plain];
BAD_REP_POLICIES(0.10)[];
HAS_LIST_UNSUB(-0.01)[];
DBL_BLOCKED_OPENRESOLVER(0.00)[sin.lore.kernel.org:helo,sin.lore.kernel.org:rdns];
FROM_NEQ_ENVFROM(0.00)[jameshilliard1@gmail.com,linux-sunxi@lists.linux.dev];
RCVD_COUNT_FIVE(0.00)[6];
FROM_HAS_DN(0.00)[];
TAGGED_RCPT(0.00)[kernel];
FREEMAIL_CC(0.00)[altera.com,lists.infradead.org,vger.kernel.org,lists.linux.dev,gmail.com];
FORGED_RECIPIENTS_MAILLIST(0.00)[];
FORGED_SENDER_MAILLIST(0.00)[];
ASN(0.00)[asn:63949, ipnet:104.64.192.0/19, country:SG];
TAGGED_FROM(0.00)[bounces-27318-noreply=patchwork.local];
ARC_ALLOW(0.00)[subspace.kernel.org:s=arc20261015:i=2];
TO_DN_SOME(0.00)[];
FREEMAIL_FROM(0.00)[gmail.com];
RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[209.85.210.52:received,100.77.114.92:received];
RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[104.64.211.4:from];
MID_RHS_MATCH_FROM(0.00)[];
R_SPF_ALLOW(0.00)[+ip4:104.64.211.4];
FREEMAIL_TO(0.00)[bootlin.com,nod.at,ti.com,gmail.com,kernel.org,sberdevices.ru,sholland.org,infradead.org,arm.linux.org.uk,intel.com,foss.st.com,st.com,amd.com,collabora.com,mxic.com.tw,linux.intel.com,agner.ch,lynxeye.de,nvidia.com,oss.qualcomm.com,sntech.de,rock-chips.com,microchip.com,tuxon.dev,nuvoton.com,nxp.com,pengutronix.de,amlogic.com,linaro.org,baylibre.com,googlemail.com,broadcom.com,mleia.com,timesys.com,crapouillou.net,walle.cc];
RCVD_TLS_LAST(0.00)[];
RCPT_COUNT_GT_50(0.00)[73];
MIME_TRACE(0.00)[0:+];
RECEIVED_SPAMHAUS_PBL(0.00)[174.29.1.49:received];
RCVD_VIA_SMTP_AUTH(0.00)[]
X-Rspamd-Queue-Id: 9A66C1C70E2
X-MORS-Enabled: yes
X-MORS-DOMAIN: patchwork.local
X-MORS-HOSTING: hosting172546
X-MORS-USER: hosting172546
X-getmail-retrieved-from-mailbox: INBOX
|
| Series |
mtd: rawnand: retain chip and controller state across removal
|
|
Commit Message
James Hilliard
Nov. 7, 2026, 10:15 a.m. UTC
An NVMEM provider can keep its read/write context in an object separate
from its parent device. Retaining the parent does not keep that object
alive, and nvmem_unregister() can return while consumers still hold the
provider. A device reference can also delay release after the last
consumer is gone.
Add optional paired callbacks to take a reference to the private context
before publishing the provider and drop it from the final device release
callback. Balance the reference on registration errors as well. Require
both callbacks together and document the associated module lifetime
requirement.
MTD OTP providers need this ownership because they are children of the
physical flash device, while their callback context is the separately
allocated MTD master.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 12 +++++++++++-
drivers/nvmem/internals.h | 1 +
include/linux/nvmem-provider.h | 10 ++++++++++
3 files changed, 22 insertions(+), 1 deletion(-)
Comments
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] nvmem: unpinned module callback execution in nvmem_release() -- commit 6223a607fc304676fa9e070c9a5930603931e0e7 Author: James Hilliard <james.hilliard1@gmail.com> nvmem: retain provider context through final device release This commit adds optional priv_get() and priv_put() callbacks to struct nvmem_config so providers can retain their private context until final device release in nvmem_release(). > diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c > index 0556d140170a4..851fa5cabecb2 100644 > --- a/drivers/nvmem/core.c > +++ b/drivers/nvmem/core.c > @@ -541,6 +541,8 @@ static void nvmem_release(struct device *dev) > > ida_free(&nvmem_ida, nvmem->id); > gpiod_put(nvmem->wp_gpio); > + if (nvmem->ops->priv_put) > + nvmem->ops->priv_put(nvmem->priv); > kfree(nvmem->ops); > kfree(nvmem); > } [Severity: Medium] Can executing nvmem->ops->priv_put() here race with module unload and run unmapped module text? The kernel-doc note in include/linux/nvmem-provider.h states that the callbacks must retain any module needed to execute priv_put() until it returns, and commit "mtd: add opt-in lifetime ownership for backing objects" implements this by calling __module_get(THIS_MODULE) in mtd_otp_nvmem_get() and module_put(THIS_MODULE) in mtd_otp_nvmem_put(). However, a callback in a loadable module cannot safely drop the last reference to its own module before returning: CPU0 CPU1 nvmem_release() mtd_otp_nvmem_put() module_put(THIS_MODULE) delete_module() free_module() return; <-- use-after-free nvmem->owner is only held by active consumers and is already dropped in nvmem_device_put() before nvmem_release() runs. The NVMEM core should pin the module owning priv_put() in nvmem_register() and call module_put() in nvmem_release() after nvmem->ops->priv_put() returns.
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 0556d140170a..851fa5cabecb 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -541,6 +541,8 @@ static void nvmem_release(struct device *dev) ida_free(&nvmem_ida, nvmem->id); gpiod_put(nvmem->wp_gpio); + if (nvmem->ops->priv_put) + nvmem->ops->priv_put(nvmem->priv); kfree(nvmem->ops); kfree(nvmem); } @@ -896,6 +898,9 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (!config->reg_read && !config->reg_write) return ERR_PTR(-EINVAL); + if (!!config->priv_get != !!config->priv_put) + return ERR_PTR(-EINVAL); + nvmem = kzalloc_obj(*nvmem); if (!nvmem) return ERR_PTR(-ENOMEM); @@ -922,6 +927,12 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) device_initialize(&nvmem->dev); + nvmem->priv = config->priv; + if (config->priv_get) { + config->priv_get(config->priv); + ops->priv_put = config->priv_put; + } + if (!config->ignore_wp) nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp", GPIOD_OUT_HIGH); @@ -945,7 +956,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) nvmem->word_size = config->word_size ?: 1; nvmem->size = config->size; nvmem->root_only = config->root_only; - nvmem->priv = config->priv; nvmem->type = config->type; nvmem->keepout = config->keepout; nvmem->nkeepout = config->nkeepout; diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index 4e610deeaa7b..dcf5d81ca1b9 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -11,6 +11,7 @@ struct nvmem_operations { nvmem_reg_read_t reg_read; nvmem_reg_write_t reg_write; + void (*priv_put)(void *priv); }; struct nvmem_device { diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h index 6063fe5b7784..03e8522bab17 100644 --- a/include/linux/nvmem-provider.h +++ b/include/linux/nvmem-provider.h @@ -98,6 +98,8 @@ struct nvmem_cell_info { * @word_size: Minimum read/write access granularity. * @stride: Minimum read/write access stride. * @priv: User context passed to read/write callbacks. + * @priv_get: Optional callback taking a reference to @priv before publication. + * @priv_put: Matching callback releasing @priv at final device release. * @ignore_wp: Write Protect pin is managed by the provider. * @layout: Fixed layout associated with this nvmem device. * @@ -107,6 +109,12 @@ struct nvmem_cell_info { * * Note: Specifying name and setting id to -1 implies a unique device * whose name is provided as-is (kept unaltered). + * + * Note: @priv_get and @priv_put must be supplied together. Each @priv_get + * call is balanced by one @priv_put, including on registration failure. + * The put can run after nvmem_unregister() returns, when consumers and + * device references have gone away. These callbacks must also retain any + * module needed to execute @priv_put until it returns. */ struct nvmem_config { struct device *dev; @@ -132,6 +140,8 @@ struct nvmem_config { int word_size; int stride; void *priv; + void (*priv_get)(void *priv); + void (*priv_put)(void *priv); /* To be only used by old driver/misc/eeprom drivers */ bool compat; struct device *base_dev;