From: Loic Poulain <loic.poulain@oss.qualcomm.com>
To: Ulf Hansson <ulfh@kernel.org>, Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Bjorn Andersson <andersson@kernel.org>,
Konrad Dybcio <konradybcio@kernel.org>,
Jens Axboe <axboe@kernel.dk>,
Johannes Berg <johannes@sipsolutions.net>,
Jeff Johnson <jjohnson@kernel.org>,
Bartosz Golaszewski <brgl@kernel.org>,
Marcel Holtmann <marcel@holtmann.org>,
Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
Balakrishna Godavarthi <quic_bgodavar@quicinc.com>,
Rocky Liao <quic_rjliao@quicinc.com>,
"David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Srinivas Kandagatla <srini@kernel.org>,
Andrew Lunn <andrew@lunn.ch>,
Heiner Kallweit <hkallweit1@gmail.com>,
Russell King <linux@armlinux.org.uk>,
Saravana Kannan <saravanak@kernel.org>,
Christian Marangi <ansuelsmth@gmail.com>,
Eric Dumazet <edumazet@kernel.org>,
Abel Vesa <abelvesa@kernel.org>,
Eric Dumazet <edumazet@kernel.org>
Cc: linux-mmc@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org,
linux-block@vger.kernel.org, linux-wireless@vger.kernel.org,
ath10k@lists.infradead.org, linux-bluetooth@vger.kernel.org,
netdev@vger.kernel.org, daniel@makrotopia.org,
Loic Poulain <loic.poulain@oss.qualcomm.com>,
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
Christoph Hellwig <hch@lst.de>
Subject: [PATCH v14 2/2] block: implement NVMEM provider
Date: Tue, 01 Dec 2026 15:05:25 +0100 [thread overview]
Message-ID: <20261201-block-as-nvmem-v14-2-3a07f4ff246c@oss.qualcomm.com> (raw)
In-Reply-To: <20261201-block-as-nvmem-v14-0-3a07f4ff246c@oss.qualcomm.com>
On embedded devices using an eMMC it is common that one or more partitions
on the eMMC are used to store MAC addresses and Wi-Fi calibration EEPROM
data. Allow referencing the partition in device tree for the kernel and
Wi-Fi drivers accessing it via the NVMEM layer.
NVMEM is registered for a block device whose OF node describes an NVMEM
layout, either via an "nvmem-layout" child or by being a "fixed-layout"
node itself (e.g. an eMMC boot partition associated through its mmc-card
node). In the latter case the NVMEM core populates the layout device on the
block device's own OF node, so that node ends up marked OF_POPULATED_BUS
and shared with the layout device for as long as the block device exists.
The NVMEM device is a read-only side channel onto a block device that stays
fully usable, and it never takes ownership of it. Reads are served from the
page cache of the block device, the same way the partition table itself is
read (see read_part_sector()), so they are coherent with the other users of
that same block device, but the data is only as stable as whatever owns the
block device keeps it.
Going through the page cache instead of opening the block device is what
makes the lifetime of the NVMEM device work out. blk_nvmem_del() runs under
disk->open_mutex (drop_partition()) and has to wait, through
nvmem_unregister(), for the in-flight readers to drain before the NVMEM
device is gone, as it is a child of the block device that is about to be
deleted. A reader that opened the block device would take disk->open_mutex
itself and deadlock against that wait. A read stuck on dead hardware
instead delays the partition removal until the driver times out.
Registering a partition happens under the same mutex, and synchronously
probes the layout driver, which for all in-tree layouts is device tree
parsing plus reads through the same page cache path.
Exclusive openers are turned away with -EBUSY instead, a block device
described as an NVMEM provider in the device tree is not a filesystem, md
or dm member. Openers that do not ask for exclusive access (udev, blkid,
dd, and provisioning tools that do not use O_EXCL) keep working and see the
same data through the shared page cache.
Consumers looking up a cell before the provider shows up get -EPROBE_DEFER
from the NVMEM core and retry later.
Reviewed-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
---
MAINTAINERS | 1 +
block/Kconfig | 11 +++++
block/Makefile | 1 +
block/bdev.c | 7 +++
block/blk-nvmem.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++
block/blk.h | 18 +++++++
block/genhd.c | 4 ++
block/partitions/core.c | 5 ++
include/linux/blk_types.h | 4 ++
9 files changed, 172 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index c00d3612c9acf4665bf89376877f1ca883cb1b2a..0527aff11c92291aca50a4e716eca0438a583b28 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -20323,6 +20323,7 @@ S: Maintained
T: git git://git.kernel.org/pub/scm/linux/kernel/git/srini/nvmem.git
F: Documentation/ABI/stable/sysfs-bus-nvmem
F: Documentation/devicetree/bindings/nvmem/
+F: block/blk-nvmem.c
F: drivers/nvmem/
F: include/dt-bindings/nvmem/
F: include/linux/nvmem-consumer.h
diff --git a/block/Kconfig b/block/Kconfig
index 70e4a66d941ff66ecb0ec34f4f7eff7fd1e4be40..4899ad572c71ffdbb62460d57623216254893ddc 100644
--- a/block/Kconfig
+++ b/block/Kconfig
@@ -209,6 +209,17 @@ config BLK_INLINE_ENCRYPTION_FALLBACK
by falling back to the kernel crypto API when inline
encryption hardware is not present.
+config BLK_NVMEM
+ bool "Block device NVMEM provider"
+ depends on OF
+ depends on NVMEM
+ help
+ Allow block devices (or partitions) to act as NVMEM providers,
+ exposing factory-provisioned data such as MAC addresses or Wi-Fi
+ calibration blobs to the drivers that consume them. This is
+ typically used on embedded devices where such data is stored in a
+ dedicated area of an eMMC, instead of a separate EEPROM or OTP.
+
source "block/partitions/Kconfig"
config BLK_PM
diff --git a/block/Makefile b/block/Makefile
index e7bd320e3d6971a8abf584f5de42776bb54e8216..826af86edce4e86eceb06c744e740abd0cab139f 100644
--- a/block/Makefile
+++ b/block/Makefile
@@ -39,3 +39,4 @@ obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += blk-crypto.o blk-crypto-profile.o \
blk-crypto-sysfs.o
obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) += blk-crypto-fallback.o
obj-$(CONFIG_BLOCK_HOLDER_DEPRECATED) += holder.o
+obj-$(CONFIG_BLK_NVMEM) += blk-nvmem.o
diff --git a/block/bdev.c b/block/bdev.c
index e0a3dbb06837f0e82016cd531264e7d55cfddc23..730a42c6161afadb0cdcae6f9681f5f84afcfee5 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -976,6 +976,13 @@ int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder,
int ret;
if (holder) {
+ /*
+ * A block device providing NVMEM cells is not a filesystem,
+ * md or dm member and must not be claimed exclusively.
+ */
+ if (bdev_is_nvmem(bdev))
+ return -EBUSY;
+
mode |= BLK_OPEN_EXCL;
ret = bd_prepare_to_claim(bdev, holder, hops);
if (ret)
diff --git a/block/blk-nvmem.c b/block/blk-nvmem.c
new file mode 100644
index 0000000000000000000000000000000000000000..8d29fd338f0b070f30e70aabb80df8be4d4845a3
--- /dev/null
+++ b/block/blk-nvmem.c
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * block device NVMEM provider
+ *
+ * Copyright (c) 2024 Daniel Golle <daniel@makrotopia.org>
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ *
+ * Useful on devices using a whole disk or a partition (e.g. an eMMC boot
+ * partition) to store MAC addresses, Bluetooth addresses or Wi-Fi
+ * calibration EEPROM data.
+ *
+ * The NVMEM device is a read-only side channel onto a block device that stays
+ * fully usable, and never takes ownership of it. Reads go through the page
+ * cache of the block device, so they are coherent with the other users of that
+ * same block device, but the data is only as stable as whatever owns the block
+ * device keeps it.
+ */
+
+#include <linux/device.h>
+#include <linux/highmem.h>
+#include <linux/nvmem-provider.h>
+#include <linux/of.h>
+#include <linux/pagemap.h>
+
+#include "blk.h"
+
+static int blk_nvmem_reg_read(void *priv, unsigned int from,
+ void *val, size_t bytes)
+{
+ struct block_device *bdev = priv;
+ struct address_space *mapping = bdev->bd_mapping;
+ loff_t pos = from;
+
+ /* the device may have shrunk since the NVMEM device was registered */
+ if (pos + bytes > bdev_nr_bytes(bdev))
+ return -EIO;
+
+ while (bytes) {
+ struct folio *folio;
+ size_t offset, len;
+
+ folio = read_mapping_folio(mapping, pos >> PAGE_SHIFT, NULL);
+ if (IS_ERR(folio))
+ return PTR_ERR(folio);
+
+ offset = offset_in_folio(folio, pos);
+ len = min(bytes, folio_size(folio) - offset);
+ memcpy_from_folio(val, folio, offset, len);
+ folio_put(folio);
+
+ val += len;
+ pos += len;
+ bytes -= len;
+ }
+
+ return 0;
+}
+
+void blk_nvmem_add(struct block_device *bdev)
+{
+ struct device *dev = &bdev->bd_device;
+ struct device_node *np = dev_of_node(dev);
+ loff_t size = bdev_nr_bytes(bdev);
+ struct nvmem_config config = {
+ .id = NVMEM_DEVID_NONE,
+ .owner = THIS_MODULE,
+ .word_size = 1,
+ .stride = 1,
+ .read_only = true,
+ .root_only = true,
+ .ignore_wp = true,
+ .dev = dev,
+ .name = dev_name(dev),
+ .of_node = np,
+ .priv = bdev,
+ .reg_read = blk_nvmem_reg_read,
+ };
+ struct nvmem_device *nvmem;
+ struct device_node *child;
+
+ /* skip devices which do not have a device tree node */
+ if (!np)
+ return;
+
+ /*
+ * Only register devices which the device tree describes a layout for,
+ * either in an "nvmem-layout" child node or on the node itself, as
+ * of_nvmem_layout_get_container() looks it up.
+ */
+ child = of_get_child_by_name(np, "nvmem-layout");
+ if (child)
+ of_node_put(child);
+ else if (!of_device_is_compatible(np, "fixed-layout"))
+ return;
+
+ /* nvmem_config.size is an int, so reject anything that would overflow */
+ if (size > INT_MAX) {
+ dev_warn(dev, "block device too large to be an NVMEM\n");
+ return;
+ }
+
+ config.size = size;
+
+ nvmem = nvmem_register(&config);
+ if (IS_ERR(nvmem)) {
+ dev_err(dev, "Failed to register NVMEM device (%pe)\n", nvmem);
+ return;
+ }
+
+ WRITE_ONCE(bdev->bd_nvmem, nvmem);
+}
+
+/*
+ * Relies on GD_DEAD being set before, so that nvmem readers error out
+ * instead of waiting for the unfreeze.
+ */
+void blk_nvmem_del(struct block_device *bdev)
+{
+ nvmem_unregister(bdev->bd_nvmem);
+ WRITE_ONCE(bdev->bd_nvmem, NULL);
+}
diff --git a/block/blk.h b/block/blk.h
index 105fff157bfbd453ae4cfa993e8971ceb36d916e..94f871c04665937160daa5643e3a15ebc4a14b91 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -839,4 +839,22 @@ static inline void blk_debugfs_unlock(struct request_queue *q,
memalloc_noio_restore(memflags);
}
+#ifdef CONFIG_BLK_NVMEM
+void blk_nvmem_add(struct block_device *bdev);
+void blk_nvmem_del(struct block_device *bdev);
+
+static inline bool bdev_is_nvmem(struct block_device *bdev)
+{
+ return READ_ONCE(bdev->bd_nvmem);
+}
+#else
+static inline void blk_nvmem_add(struct block_device *bdev) {}
+static inline void blk_nvmem_del(struct block_device *bdev) {}
+
+static inline bool bdev_is_nvmem(struct block_device *bdev)
+{
+ return false;
+}
+#endif
+
#endif /* BLK_INTERNAL_H */
diff --git a/block/genhd.c b/block/genhd.c
index 69204d44adae11eb8bccf5db8d5d5eaa0fa186a8..39adc6f5e44cf736161c67e63602249a32b42751 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -417,6 +417,8 @@ static void add_disk_final(struct gendisk *disk)
*/
dev_set_uevent_suppress(ddev, 0);
disk_uevent(disk, KOBJ_ADD);
+
+ blk_nvmem_add(disk->part0);
}
blk_apply_bdi_limits(disk->bdi, &disk->queue->limits);
@@ -708,6 +710,8 @@ static void __del_gendisk(struct gendisk *disk)
disk_del_events(disk);
+ blk_nvmem_del(disk->part0);
+
/*
* Prevent new openers by unlinked the bdev inode.
*/
diff --git a/block/partitions/core.c b/block/partitions/core.c
index eee954df1ac6ef7dd4f02ad5c9d49aed778bf015..c83227710584a6feb25c8abae3b319cfae9cce5b 100644
--- a/block/partitions/core.c
+++ b/block/partitions/core.c
@@ -289,6 +289,8 @@ void drop_partition(struct block_device *part)
xa_erase(&part->bd_disk->part_tbl, bdev_partno(part));
kobject_put(part->bd_holder_dir);
+ blk_nvmem_del(part);
+
device_del(&part->bd_device);
put_device(&part->bd_device);
}
@@ -406,6 +408,9 @@ static struct block_device *add_partition(struct gendisk *disk, int partno,
/* suppress uevent if the disk suppresses it */
if (!dev_get_uevent_suppress(ddev))
kobject_uevent(&pdev->kobj, KOBJ_ADD);
+
+ blk_nvmem_add(bdev);
+
return bdev;
out_del:
diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h
index 644b9528f2749f82df4d014adc0149dc42c986a8..6c442546623e31aae3d4e5c7e64b0b1836ef254b 100644
--- a/include/linux/blk_types.h
+++ b/include/linux/blk_types.h
@@ -15,6 +15,7 @@
struct bio_set;
struct bio;
struct bio_integrity_payload;
+struct nvmem_device;
struct page;
struct io_context;
struct cgroup_subsys_state;
@@ -74,6 +75,9 @@ struct block_device {
int bd_writers;
#ifdef CONFIG_SECURITY
void *bd_security;
+#endif
+#ifdef CONFIG_BLK_NVMEM
+ struct nvmem_device *bd_nvmem;
#endif
/*
* keep this out-of-line as it's both big and not needed in the fast
--
2.34.1
prev parent reply other threads:[~2026-12-01 14:05 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 14:05 [PATCH v14 0/2] Support for block device NVMEM providers Loic Poulain
2026-12-01 14:05 ` [PATCH v14 1/2] block: partitions: of: Attach partition fwnode to the block device Loic Poulain
2026-12-01 14:05 ` Loic Poulain [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201-block-as-nvmem-v14-2-3a07f4ff246c@oss.qualcomm.com \
--to=loic.poulain@oss.qualcomm.com \
--cc=abelvesa@kernel.org \
--cc=andersson@kernel.org \
--cc=andrew@lunn.ch \
--cc=ansuelsmth@gmail.com \
--cc=ath10k@lists.infradead.org \
--cc=axboe@kernel.dk \
--cc=bartosz.golaszewski@oss.qualcomm.com \
--cc=brgl@kernel.org \
--cc=conor+dt@kernel.org \
--cc=daniel@makrotopia.org \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=edumazet@kernel.org \
--cc=hch@lst.de \
--cc=hkallweit1@gmail.com \
--cc=horms@kernel.org \
--cc=jjohnson@kernel.org \
--cc=johannes@sipsolutions.net \
--cc=konradybcio@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-arm-msm@vger.kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mmc@vger.kernel.org \
--cc=linux-wireless@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=quic_bgodavar@quicinc.com \
--cc=quic_rjliao@quicinc.com \
--cc=robh@kernel.org \
--cc=saravanak@kernel.org \
--cc=srini@kernel.org \
--cc=ulfh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®