[PATCH v13 2/2] block: implement NVMEM provider

From: Loic Poulain

Date: Mon Nov 23 2026 - 07:45:08 EST


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).

The NVMEM device is a read-only side channel onto a block device.
Each read opens the block device with BLK_OPEN_EXCL to keep other
exclusive openers away for the duration of the access.

Both the registration and the removal of the NVMEM device are deferred to
an ordered workqueue, because they may run from under disk->open_mutex
(add_partition(), drop_partition()) while:

- nvmem_register() can read the device through ->reg_read() to let a
layout driver populate its cells, and
- nvmem_unregister() waits for the in-flight readers to drain.

As ->reg_read() opens the block device, and thus takes disk->open_mutex
itself, registering synchronously would self-deadlock, and unregistering
synchronously would deadlock against a reader blocked on disk->open_mutex
while holding the NVMEM SRCU read lock.

Keeping the queue ordered guarantees that, for a given block device, the
registration has completed before the removal starts, and that the removal
of a partition completes before the registration of its replacement, which
otherwise would clash on the NVMEM device name when a partition table is
re-read.

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@xxxxxxxxxxxxxxxx>
Reviewed-by: Christoph Hellwig <hch@xxxxxx>
Signed-off-by: Loic Poulain <loic.poulain@xxxxxxxxxxxxxxxx>
---
block/Kconfig | 11 +++
block/Makefile | 1 +
block/blk-nvmem.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++
block/blk.h | 8 ++
block/genhd.c | 4 +
block/partitions/core.c | 5 ++
include/linux/blk_types.h | 4 +
7 files changed, 230 insertions(+)

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..8200297fbcbab304beb4239d0c33da07011c3ed9 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/blk-nvmem.c b/block/blk-nvmem.c
new file mode 100644
index 0000000000000000000000000000000000000000..628681f6b747b865100bcaec18ea650b08c6972f
--- /dev/null
+++ b/block/blk-nvmem.c
@@ -0,0 +1,197 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * block device NVMEM provider
+ *
+ * Copyright (c) 2024 Daniel Golle <daniel@xxxxxxxxxxxxxx>
+ * 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 side channel onto a block device that stays fully
+ * usable. This is somewhat mitigated by opening the device exclusively.
+ */
+
+#include <linux/device.h>
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/nvmem-provider.h>
+#include <linux/nvmem-consumer.h>
+#include <linux/of.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+#include <linux/workqueue.h>
+
+#include "blk.h"
+
+/**
+ * struct blk_nvmem - NVMEM registration state of a block device
+ * @bdev: block device backing the NVMEM device
+ * @nvmem: registered NVMEM device, only accessed from the work items
+ * @size: size of @bdev in bytes, sampled when the registration is queued
+ * @add_work: work item registering @nvmem
+ * @del_work: work item unregistering @nvmem and freeing this structure
+ */
+struct blk_nvmem {
+ struct block_device *bdev;
+ struct nvmem_device *nvmem;
+ loff_t size;
+ struct work_struct add_work;
+ struct work_struct del_work;
+};
+
+/*
+ * The queue must stay ordered: it is what guarantees that the removal of a
+ * block device completes before the registration of its replacement, which
+ * otherwise would clash on the NVMEM device name, e.g. when re-reading a
+ * partition table.
+ */
+static struct workqueue_struct *blk_nvmem_wq;
+
+static int blk_nvmem_reg_read(void *priv, unsigned int from,
+ void *val, size_t bytes)
+{
+ struct block_device *bdev = priv;
+ struct file *bdev_file;
+ loff_t pos = from;
+ ssize_t ret;
+
+ /* open and prevent other exclusive openers */
+ bdev_file = bdev_file_open_by_dev(bdev->bd_dev,
+ BLK_OPEN_READ | BLK_OPEN_EXCL,
+ blk_nvmem_reg_read, NULL);
+ if (IS_ERR(bdev_file))
+ return PTR_ERR(bdev_file);
+
+ /* the minor may have been recycled since the NVMEM was registered */
+ if (file_bdev(bdev_file) != bdev) {
+ bdev_fput(bdev_file);
+ return -ENODEV;
+ }
+
+ ret = kernel_read(bdev_file, val, bytes, &pos);
+
+ /* yield the claim synchronously, the file release is deferred */
+ bdev_fput(bdev_file);
+
+ if (ret < 0)
+ return ret;
+ if (ret != bytes)
+ return -EIO;
+ return 0;
+}
+
+static void blk_nvmem_add_work(struct work_struct *work)
+{
+ struct blk_nvmem *bn = container_of(work, struct blk_nvmem, add_work);
+ struct block_device *bdev = bn->bdev;
+ struct device *dev = &bdev->bd_device;
+ 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 = dev_of_node(dev),
+ .priv = bdev,
+ .reg_read = blk_nvmem_reg_read,
+ .size = bn->size,
+ };
+ struct nvmem_device *nvmem;
+
+ nvmem = nvmem_register(&config);
+ if (IS_ERR(nvmem)) {
+ dev_err_probe(dev, PTR_ERR(nvmem),
+ "Failed to register NVMEM device\n");
+ return;
+ }
+
+ bn->nvmem = nvmem;
+}
+
+static void blk_nvmem_del_work(struct work_struct *work)
+{
+ struct blk_nvmem *bn = container_of(work, struct blk_nvmem, del_work);
+
+ nvmem_unregister(bn->nvmem);
+ put_device(&bn->bdev->bd_device);
+ kfree(bn);
+}
+
+void blk_nvmem_add(struct block_device *bdev)
+{
+ struct device *dev = &bdev->bd_device;
+ struct device_node *np = dev_of_node(dev);
+ struct blk_nvmem *bn;
+ struct device_node *child;
+ loff_t size;
+
+ /* skip devices which do not have a device tree node */
+ if (!np)
+ return;
+
+ /*
+ * The layout is described either by an "nvmem-layout" child node or
+ * by the device node itself being a "fixed-layout" container.
+ */
+ 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;
+
+ /*
+ * Sample the size here so that the value validated against the
+ * nvmem_config.size type is the one actually handed to the NVMEM core,
+ * even if the device is resized before the registration runs.
+ */
+ size = bdev_nr_bytes(bdev);
+ if (size > INT_MAX) {
+ dev_warn(dev, "block device too large to be an NVMEM\n");
+ return;
+ }
+
+ if (!blk_nvmem_wq)
+ return;
+
+ bn = kzalloc_obj(*bn);
+ if (!bn)
+ return;
+
+ bn->bdev = bdev;
+ bn->size = size;
+ INIT_WORK(&bn->add_work, blk_nvmem_add_work);
+ INIT_WORK(&bn->del_work, blk_nvmem_del_work);
+
+ get_device(dev);
+ bdev->bd_nvmem = bn;
+
+ queue_work(blk_nvmem_wq, &bn->add_work);
+}
+
+void blk_nvmem_del(struct block_device *bdev)
+{
+ struct blk_nvmem *bn = bdev->bd_nvmem;
+
+ if (!bn)
+ return;
+
+ bdev->bd_nvmem = NULL;
+
+ queue_work(blk_nvmem_wq, &bn->del_work);
+}
+
+static int __init blk_nvmem_init(void)
+{
+ blk_nvmem_wq = alloc_ordered_workqueue("blk_nvmem", 0);
+ if (!blk_nvmem_wq)
+ return -ENOMEM;
+
+ return 0;
+}
+subsys_initcall(blk_nvmem_init);
diff --git a/block/blk.h b/block/blk.h
index 105fff157bfbd453ae4cfa993e8971ceb36d916e..0920afd23e9a701842f522243c246c75bbd49f54 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -839,4 +839,12 @@ 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);
+#else
+static inline void blk_nvmem_add(struct block_device *bdev) {}
+static inline void blk_nvmem_del(struct block_device *bdev) {}
+#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..8f2708e4075e7390ed58ef764ddd4684d4bc1458 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 blk_nvmem;
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 blk_nvmem *bd_nvmem;
#endif
/*
* keep this out-of-line as it's both big and not needed in the fast

--
2.34.1