Skip to contentSkip to conversation

nvmem: fixes for v7.4

PATCH In review [email protected] 2 participants 12 messages Dec 1 – Dec 2

Subsystems (5)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

cover nvmem: fixes for v7.4

From: Srinivas Kandagatla <[email protected]>

Hi Greg,

Here are few nvmem fixes for v7.4, Could you please queue these for v7.4. The branch is based on char-misc-linus.

Patches include
- core fixes around provider disconnect: drain keepout/cell post-processing accesses, exclude disconnected providers from callback admission, release write protection and retain the provider node until device release.
- bounds and input validation fixes in the core, onie-tlv layout and sprd-efuse provider, plus a DT cell-bits validation fix.
- provider fixes for airoha-smc-efuses SMC function ID and imx-ocotp-scu raw_len assignment.

Thanks,
Srini

James Hilliard (6):
nvmem: layouts: onie-tlv: check both field header and value bounds
nvmem: core: validate DT cell bits before allocating the name
nvmem: retain the provider node until device release
nvmem: drain keepout and cell post-processing accesses
nvmem: exclude disconnected providers from callback admission
nvmem: release write protection when the provider disconnects

Sanman Pradhan (2):
nvmem: core: reject reads and writes beyond the device size
nvmem: sprd: fix out-of-bounds read in sprd_efuse_read()

Stefan Ghetu (1):
nvmem: imx-ocotp-scu: assign nvmem_cell_info::raw_len

Zhengping Zhang (1):
nvmem: airoha-smc-efuses: Fix the SMC function ID for eFuse read

 drivers/nvmem/airoha-smc-efuses.c |   4 +-
 drivers/nvmem/core.c              | 237 +++++++++++++++++++-----------
 drivers/nvmem/imx-ocotp-scu.c     |   7 +
 drivers/nvmem/layouts/onie-tlv.c  |   9 +-
 drivers/nvmem/sprd-efuse.c        |  17 ++-
 5 files changed, 179 insertions(+), 95 deletions(-)

base-commit: 89f33682b2c34b12b3a7a549ba94a808f0c7c98d

Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/10 nvmem: layouts: onie-tlv: check both field header and value bounds

From: James Hilliard <[email protected]>

The table CRC does not validate individual field lengths. The field iterator can read a two-byte header when only one byte remains. Its value length check also counts the header as available space and can publish a cell extending beyond the table.

Check the remaining space before reading the header and exclude the header from the space available for the value. Stop at the first truncated field, keeping any complete cells already registered.

Fixes d3c0d12f6474 ("nvmem: layouts: onie-tlv: Add new layout driver")
---
 drivers/nvmem/layouts/onie-tlv.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/nvmem/layouts/onie-tlv.c +8 -1
index 4da3a704388e..1e397565b928 100644
@@ -110,8 +110,15 @@ static int onie_tlv_add_cells(struct device *dev, struct nvmem_device *nvmem,
 		return -ENOENT;
 
 	while (offset < data_len) {
+		size_t remaining = data_len - offset;
+
+		if (remaining < sizeof(tlv)) {
+			dev_err(dev, "Truncated field header at 0x%x\n", hdr_len + offset);
+			break;
+		}
+
 		memcpy(&tlv, data + offset, sizeof(tlv));
-		if (offset + tlv.len >= data_len) {
+		if (tlv.len > remaining - sizeof(tlv)) {
 			dev_err(dev, "Out of bounds field (0x%x bytes at 0x%x)\n",
 				tlv.len, hdr_len + offset);
 			break;
Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/10 nvmem: core: validate DT cell bits before allocating the name

In reply to [email protected]

From: James Hilliard <[email protected]>

An invalid bits property makes nvmem_add_cells_from_dt() return before it frees the cell name. Allocate the name after validating the property, and fail before invoking the provider fixup if allocation fails.

Fixes def3173d4f17 ("nvmem: core: Print error on wrong bits DT property")
---
 drivers/nvmem/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/nvmem/core.c +3 -1
index d976c0d6603b..c54d57e4bb4a 100644
@@ -818,7 +818,6 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
 
 		info.offset = be32_to_cpup(addr++);
 		info.bytes = be32_to_cpup(addr);
-		info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
 
 		addr = of_get_property(child, "bits", &len);
 		if (addr && len == (2 * sizeof(u32))) {
@@ -832,6 +831,9 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
 			}
 		}
 
+		info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
+		if (!info.name)
+			return -ENOMEM;
 		info.np = of_node_get(child);
 
 		if (nvmem->fixup_dt_cell_info)
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH 3/10 nvmem: retain the provider node until device release

In reply to [email protected]

From: James Hilliard <[email protected]>

An NVMEM device can outlive its provider after unregister. Take a reference to the configured OF node, or reuse the provider device's node when none is specified. Drop the reference in the device release path so the node remains valid for the lifetime of the NVMEM device.

Fixes 1333a6779501 ("nvmem: core: allow specifying of_node")
---
 drivers/nvmem/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/nvmem/core.c +3 -2
index c54d57e4bb4a..be7c046b703d 100644
@@ -569,6 +569,7 @@ static void nvmem_release(struct device *dev)
 	gpiod_put(nvmem->wp_gpio);
 	nvmem_device_remove_all_cells(nvmem);
 	ida_free(&nvmem_ida, nvmem->id);
+	of_node_put(nvmem->dev.of_node);
 	cleanup_srcu_struct(&nvmem->srcu);
 	kfree(nvmem);
 }
@@ -974,9 +975,9 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	nvmem->keepout = config->keepout;
 	nvmem->nkeepout = config->nkeepout;
 	if (config->of_node)
-		nvmem->dev.of_node = config->of_node;
+		nvmem->dev.of_node = of_node_get(config->of_node);
 	else
-		nvmem->dev.of_node = config->dev->of_node;
+		device_set_of_node_from_dev(&nvmem->dev, config->dev);
 
 	switch (config->id) {
 	case NVMEM_DEVID_NONE:
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH 4/10 nvmem: drain keepout and cell post-processing accesses

In reply to [email protected]

From: James Hilliard <[email protected]>

A provider can unregister while a read is still walking its keepout table or running a cell's post-processing callback. Waiting for the register callback alone does not keep that provider state alive.

Take the SRCU read lock once for each register or cell access. Use the same operations pointer for every chunk, including the reads needed for a partial-byte write. Unregister then waits for the entire operation; new accesses fail even if they would only touch a keepout.

Fixes 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
---
 drivers/nvmem/core.c | 110 ++++++++++++++++++++++---------------------
 1 file changed, 56 insertions(+), 54 deletions(-)
drivers/nvmem/core.c +56 -54
index be7c046b703d..81d787f7050a 100644
@@ -54,32 +54,18 @@ static LIST_HEAD(nvmem_lookup_list);
 
 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
 
-static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
-			    void *val, size_t bytes)
+static int nvmem_access_chunk(struct nvmem_device *nvmem,
+			      const struct nvmem_operations *ops,
+			      unsigned int offset, void *val, size_t bytes, bool write)
 {
-	struct nvmem_operations *ops;
-
-	guard(srcu)(&nvmem->srcu);
-	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
-	if (!ops)
-		return -ENODEV;
-
-	if (!ops->reg_read)
-		return -EOPNOTSUPP;
-
-	return ops->reg_read(nvmem->priv, offset, val, bytes);
-}
-
-static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
-			     void *val, size_t bytes)
-{
-	struct nvmem_operations *ops;
 	int ret, wr_ok;
 
-	guard(srcu)(&nvmem->srcu);
-	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
-	if (!ops)
-		return -ENODEV;
+	if (!write) {
+		if (!ops->reg_read)
+			return -EOPNOTSUPP;
+
+		return ops->reg_read(nvmem->priv, offset, val, bytes);
+	}
 
 	if (!ops->reg_write)
 		return -EOPNOTSUPP;
@@ -97,17 +83,20 @@ static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
 	return wr_ok;
 }
 
-static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
-				      unsigned int offset, void *val,
-				      size_t bytes, int write)
+static int nvmem_access_locked(struct nvmem_device *nvmem,
+			       const struct nvmem_operations *ops,
+			       unsigned int offset, void *val, size_t bytes, bool write)
 {
-
 	unsigned int end = offset + bytes;
 	unsigned int kend, ksize;
 	const struct nvmem_keepout *keepout = nvmem->keepout;
-	const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
+	const struct nvmem_keepout *keepoutend;
 	int rc;
 
+	if (!nvmem->nkeepout)
+		return nvmem_access_chunk(nvmem, ops, offset, val, bytes, write);
+
+	keepoutend = keepout + nvmem->nkeepout;
 	/*
 	 * Skip all keepouts before the range being accessed.
 	 * Keepouts are sorted.
@@ -120,11 +109,7 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
 		if (offset < keepout->start) {
 			kend = min(end, keepout->start);
 			ksize = kend - offset;
-			if (write)
-				rc = __nvmem_reg_write(nvmem, offset, val, ksize);
-			else
-				rc = __nvmem_reg_read(nvmem, offset, val, ksize);
-
+			rc = nvmem_access_chunk(nvmem, ops, offset, val, ksize, write);
 			if (rc)
 				return rc;
 
@@ -150,13 +135,8 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
 	 * If we ran out of keepouts but there's still stuff to do, send it
 	 * down directly
 	 */
-	if (offset < end) {
-		ksize = end - offset;
-		if (write)
-			return __nvmem_reg_write(nvmem, offset, val, ksize);
-		else
-			return __nvmem_reg_read(nvmem, offset, val, ksize);
-	}
+	if (offset < end)
+		return nvmem_access_chunk(nvmem, ops, offset, val, end - offset, write);
 
 	return 0;
 }
@@ -164,19 +144,27 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
 static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
 			  void *val, size_t bytes)
 {
-	if (!nvmem->nkeepout)
-		return __nvmem_reg_read(nvmem, offset, val, bytes);
+	const struct nvmem_operations *ops;
+
+	guard(srcu)(&nvmem->srcu);
+	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+	if (!ops)
+		return -ENODEV;
 
-	return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
+	return nvmem_access_locked(nvmem, ops, offset, val, bytes, false);
 }
 
 static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
 			   void *val, size_t bytes)
 {
-	if (!nvmem->nkeepout)
-		return __nvmem_reg_write(nvmem, offset, val, bytes);
+	const struct nvmem_operations *ops;
+
+	guard(srcu)(&nvmem->srcu);
+	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+	if (!ops)
+		return -ENODEV;
 
-	return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
+	return nvmem_access_locked(nvmem, ops, offset, val, bytes, true);
 }
 
 #ifdef CONFIG_NVMEM_SYSFS
@@ -1650,10 +1638,15 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
 			     struct nvmem_cell_entry *cell,
 			     void *buf, size_t *len, const char *id, int index)
 {
+	const struct nvmem_operations *ops;
 	int rc;
 
-	rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
+	guard(srcu)(&nvmem->srcu);
+	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+	if (!ops)
+		return -ENODEV;
 
+	rc = nvmem_access_locked(nvmem, ops, cell->offset, buf, cell->raw_len, false);
 	if (rc)
 		return rc;
 
@@ -1709,6 +1702,7 @@ void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
 EXPORT_SYMBOL_GPL(nvmem_cell_read);
 
 static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
+					     const struct nvmem_operations *ops,
 					     u8 *_buf, int len)
 {
 	struct nvmem_device *nvmem = cell->nvmem;
@@ -1728,7 +1722,7 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
 		*b <<= bit_offset;
 
 		/* setup the first byte with lsb bits from nvmem */
-		rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
+		rc = nvmem_access_locked(nvmem, ops, cell->offset, &v, 1, false);
 		if (rc)
 			goto err;
 		*b++ |= GENMASK(bit_offset - 1, 0) & v;
@@ -1747,8 +1741,8 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
 	/* if it's not end on byte boundary */
 	if ((nbits + bit_offset) % BITS_PER_BYTE) {
 		/* setup the last byte with msb bits from nvmem */
-		rc = nvmem_reg_read(nvmem,
-				    cell->offset + cell->bytes - 1, &v, 1);
+		rc = nvmem_access_locked(nvmem, ops, cell->offset + cell->bytes - 1,
+					 &v, 1, false);
 		if (rc)
 			goto err;
 		*p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
@@ -1764,10 +1758,18 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
 static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
 {
 	struct nvmem_device *nvmem = cell->nvmem;
+	const struct nvmem_operations *ops;
 	int rc;
 
-	if (!nvmem || nvmem->read_only ||
-	    (cell->bit_offset == 0 && len != cell->bytes))
+	if (!nvmem)
+		return -EINVAL;
+
+	guard(srcu)(&nvmem->srcu);
+	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+	if (!ops)
+		return -ENODEV;
+
+	if (nvmem->read_only || (cell->bit_offset == 0 && len != cell->bytes))
 		return -EINVAL;
 
 	/*
@@ -1781,12 +1783,12 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
 	if (cell->bit_offset || cell->nbits) {
 		if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes)
 			return -EINVAL;
-		buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
+		buf = nvmem_cell_prepare_write_buffer(cell, ops, buf, len);
 		if (IS_ERR(buf))
 			return PTR_ERR(buf);
 	}
 
-	rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
+	rc = nvmem_access_locked(nvmem, ops, cell->offset, buf, cell->bytes, true);
 
 	/* free the tmp buffer */
 	if (cell->bit_offset || cell->nbits)
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH 5/10 nvmem: exclude disconnected providers from callback admission

In reply to [email protected]

From: James Hilliard <[email protected]>

A provider remains visible until device_unregister(), after its operations have been disconnected. Device matching can still call into it during that interval, and consumer acquisition can use its module pointer after unregister has returned. Cell registration and DT fixups also lack a callback drain.

Cover matching, cell addition, DT fixups and layout population with the provider SRCU domain. Serialize acquiring the module reference and publishing new cells with disconnection under nvmem_mutex. Check sysfs permissions under SRCU as well.

Cell addition notifies listeners while it is still covered by the provider drain. A listener such as cp500 can register a different NVMEM device or unwind its registration. Use a separate SRCU notifier chain so nested notifications do not block on a waiting notifier writer. Notify listeners of removal only after admitted cell additions have finished.

Fixes 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
---
 drivers/nvmem/core.c | 112 +++++++++++++++++++++++++++++++------------
 1 file changed, 81 insertions(+), 31 deletions(-)
drivers/nvmem/core.c +81 -31
index 81d787f7050a..0bfe26ed86f9 100644
@@ -52,7 +52,7 @@ static DEFINE_IDA(nvmem_ida);
 static DEFINE_MUTEX(nvmem_lookup_mutex);
 static LIST_HEAD(nvmem_lookup_list);
 
-static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
+SRCU_NOTIFIER_HEAD_STATIC(nvmem_notifier);
 
 static int nvmem_access_chunk(struct nvmem_device *nvmem,
 			      const struct nvmem_operations *ops,
@@ -287,10 +287,14 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
 
 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
 {
-	struct nvmem_operations *ops = rcu_dereference_raw(nvmem->ops);
-
+	struct nvmem_operations *ops;
 	umode_t mode = 0400;
 
+	guard(srcu)(&nvmem->srcu);
+	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+	if (!ops)
+		return 0;
+
 	if (!nvmem->root_only)
 		mode |= 0044;
 
@@ -331,7 +335,12 @@ static umode_t nvmem_attr_is_visible(struct kobject *kobj,
 {
 	struct device *dev = kobj_to_dev(kobj);
 	struct nvmem_device *nvmem = to_nvmem_device(dev);
-	struct nvmem_operations *ops = rcu_dereference_raw(nvmem->ops);
+	struct nvmem_operations *ops;
+
+	guard(srcu)(&nvmem->srcu);
+	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+	if (!ops)
+		return 0;
 
 	/*
 	 * If the device has no .reg_write operation, do not allow
@@ -534,7 +543,7 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
 
 static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
 {
-	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
+	srcu_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
 	scoped_guard(mutex, &nvmem_mutex)
 		list_del(&cell->node);
 	of_node_put(cell->np);
@@ -570,11 +579,15 @@ static const struct bus_type nvmem_bus_type = {
 	.name		= "nvmem",
 };
 
-static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
+static int nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
 {
-	scoped_guard(mutex, &nvmem_mutex)
+	scoped_guard(mutex, &nvmem_mutex) {
+		if (!rcu_access_pointer(cell->nvmem->ops))
+			return -ENODEV;
 		list_add_tail(&cell->node, &cell->nvmem->cells);
-	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
+	}
+	srcu_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
+	return 0;
 }
 
 static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
@@ -652,6 +665,10 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
 	struct nvmem_cell_entry *cell;
 	int rval;
 
+	guard(srcu)(&nvmem->srcu);
+	if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+		return -ENODEV;
+
 	cell = kzalloc_obj(*cell);
 	if (!cell)
 		return -ENOMEM;
@@ -662,9 +679,12 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
 		return rval;
 	}
 
-	nvmem_cell_entry_add(cell);
-
-	return 0;
+	rval = nvmem_cell_entry_add(cell);
+	if (rval) {
+		kfree_const(cell->name);
+		kfree(cell);
+	}
+	return rval;
 }
 EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
 
@@ -697,11 +717,14 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
  *
  * @nb: notifier block to be called on nvmem events.
  *
+ * A cell-add notification runs as part of the operation that unregister
+ * drains. Do not synchronously unregister that provider from its callback.
+ *
  * Return: 0 on success, negative error number on failure.
  */
 int nvmem_register_notifier(struct notifier_block *nb)
 {
-	return blocking_notifier_chain_register(&nvmem_notifier, nb);
+	return srcu_notifier_chain_register(&nvmem_notifier, nb);
 }
 EXPORT_SYMBOL_GPL(nvmem_register_notifier);
 
@@ -714,7 +737,7 @@ EXPORT_SYMBOL_GPL(nvmem_register_notifier);
  */
 int nvmem_unregister_notifier(struct notifier_block *nb)
 {
-	return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
+	return srcu_notifier_chain_unregister(&nvmem_notifier, nb);
 }
 EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
 
@@ -794,6 +817,10 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
 	const __be32 *addr;
 	int len, ret;
 
+	guard(srcu)(&nvmem->srcu);
+	if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+		return -ENODEV;
+
 	for_each_child_of_node_scoped(np, child) {
 		struct nvmem_cell_info info = {0};
 
@@ -852,8 +879,11 @@ int nvmem_layout_register(struct nvmem_layout *layout)
 	if (!layout->add_cells)
 		return -EINVAL;
 
-	/* Populate the cells */
-	ret = layout->add_cells(layout);
+	scoped_guard(srcu, &layout->nvmem->srcu) {
+		if (!srcu_dereference(layout->nvmem->ops, &layout->nvmem->srcu))
+			return -ENODEV;
+		ret = layout->add_cells(layout);
+	}
 	if (ret)
 		return ret;
 
@@ -875,6 +905,17 @@ void nvmem_layout_unregister(struct nvmem_layout *layout)
 }
 EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
 
+static void nvmem_disconnect(struct nvmem_device *nvmem)
+{
+	struct nvmem_operations *ops;
+
+	scoped_guard(mutex, &nvmem_mutex)
+		ops = rcu_replace_pointer(nvmem->ops, NULL,
+					 lockdep_is_held(&nvmem_mutex));
+	synchronize_srcu(&nvmem->srcu);
+	kfree(ops);
+}
+
 /**
  * nvmem_register() - Register a nvmem device for given nvmem_config.
  * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
@@ -1035,7 +1076,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 		goto err_destroy_layout;
 #endif
 
-	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
+	srcu_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
 
 	return nvmem;
 
@@ -1048,10 +1089,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 err_remove_compat:
 	nvmem_sysfs_remove_compat(nvmem);
 err_put_device:
-	ops = rcu_replace_pointer(nvmem->ops, NULL, true);
-	synchronize_srcu(&nvmem->srcu);
+	nvmem_disconnect(nvmem);
 	put_device(&nvmem->dev);
-	kfree(ops);
 
 	return ERR_PTR(rval);
 }
@@ -1064,19 +1103,14 @@ EXPORT_SYMBOL_GPL(nvmem_register);
  */
 void nvmem_unregister(struct nvmem_device *nvmem)
 {
-	struct nvmem_operations *ops;
-
 	if (!nvmem)
 		return;
 
-	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
-
-	ops = rcu_replace_pointer(nvmem->ops, NULL, true);
-	synchronize_srcu(&nvmem->srcu);
+	nvmem_disconnect(nvmem);
+	srcu_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
 
 	nvmem_sysfs_remove_compat(nvmem);
 	nvmem_destroy_layout(nvmem);
-	kfree(ops);
 
 	device_unregister(&nvmem->dev);
 }
@@ -1116,17 +1150,33 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_nvmem_register);
 
+struct nvmem_match_data {
+	void *data;
+	int (*match)(struct device *dev, const void *data);
+};
+
+static int nvmem_match_device(struct device *dev, const void *data)
+{
+	const struct nvmem_match_data *match = data;
+	struct nvmem_device *nvmem = to_nvmem_device(dev);
+
+	guard(srcu)(&nvmem->srcu);
+	if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+		return 0;
+	return match->match(dev, match->data);
+}
+
 static struct nvmem_device *nvmem_device_match(void *data,
 			int (*match)(struct device *dev, const void *data))
 {
+	struct nvmem_match_data match_data = { .data = data, .match = match };
 	struct nvmem_device *nvmem = NULL;
 	struct device *dev;
 
-	scoped_guard(mutex, &nvmem_mutex) {
-		dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
-		if (dev)
-			nvmem = to_nvmem_device(dev);
-	}
+	guard(mutex)(&nvmem_mutex);
+	dev = bus_find_device(&nvmem_bus_type, NULL, &match_data, nvmem_match_device);
+	if (dev)
+		nvmem = to_nvmem_device(dev);
 	if (!nvmem)
 		return ERR_PTR(-EPROBE_DEFER);
 
Signature · 1 line
2.53.0
author maintainer
#6 lore raw

PATCH 6/10 nvmem: release write protection when the provider disconnects

In reply to [email protected]

From: James Hilliard <[email protected]>

Retained NVMEM consumers keep the device allocation alive after unregister. Keeping the write-protect GPIO until final device release prevents a replacement provider from requesting the same GPIO.

Release it after draining provider callbacks, when no write can still use it. Do this for registration failures as well as unregister.

Fixes 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
---
 drivers/nvmem/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvmem/core.c +2 -1
index 0bfe26ed86f9..b334a64b85fc 100644
@@ -563,7 +563,6 @@ static void nvmem_release(struct device *dev)
 {
 	struct nvmem_device *nvmem = to_nvmem_device(dev);
 
-	gpiod_put(nvmem->wp_gpio);
 	nvmem_device_remove_all_cells(nvmem);
 	ida_free(&nvmem_ida, nvmem->id);
 	of_node_put(nvmem->dev.of_node);
@@ -913,6 +912,8 @@ static void nvmem_disconnect(struct nvmem_device *nvmem)
 		ops = rcu_replace_pointer(nvmem->ops, NULL,
 					 lockdep_is_held(&nvmem_mutex));
 	synchronize_srcu(&nvmem->srcu);
+	gpiod_put(nvmem->wp_gpio);
+	nvmem->wp_gpio = NULL;
 	kfree(ops);
 }
 
Signature · 1 line
2.53.0
author maintainer
#7 lore raw

PATCH 7/10 nvmem: airoha-smc-efuses: Fix the SMC function ID for eFuse read

In reply to [email protected]

From: Zhengping Zhang <[email protected]>

The Airoha firmware uses SMC function ID 0x82000005 for the eFuse word-read service. The driver currently uses 0x82000001, which does not select this service and prevents eFuse read from working correctly.

Use the correct function ID and rename the constant to AIROHA_SMC_EFUSE_READ_WORD_FID to make the selected operation explicit.

Fixes b7846af2e6ca ("nvmem: airoha: Add support for SMC eFUSE")
---
 drivers/nvmem/airoha-smc-efuses.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvmem/airoha-smc-efuses.c +2 -2
index f43c8c4c30ce..7ee8123490d0 100644
@@ -11,7 +11,7 @@
 #include <linux/of.h>
 #include <linux/regmap.h>
 
-#define AIROHA_SMC_EFUSE_FID		0x82000001
+#define AIROHA_SMC_EFUSE_READ_WORD_FID	0x82000005
 #define AIROHA_SMC_EFUSE_SUB_ID_READ	0x44414552
 
 #define AIROHA_EFUSE_CELLS		64
@@ -35,7 +35,7 @@ static int airoha_efuse_reg_read(void *context, unsigned int offset,
 	struct airoha_efuse_bank_priv *priv = context;
 	struct arm_smccc_res res;
 
-	arm_smccc_1_1_invoke(AIROHA_SMC_EFUSE_FID,
+	arm_smccc_1_1_invoke(AIROHA_SMC_EFUSE_READ_WORD_FID,
 			     AIROHA_SMC_EFUSE_SUB_ID_READ,
 			     priv->bank_index, offset, 0, 0, 0, 0, &res);
 
Signature · 1 line
2.53.0
maintainer
#8 lore raw

Re: [PATCH 07/10] nvmem: airoha-smc-efuses: Fix the SMC function ID for eFuse read

Quoted 7 lines · On Tue, Dec 01, 2026 at 11:26:54PM +0000, [email protected] wrote:

From: Zhengping Zhang <[email protected]>

The Airoha firmware uses SMC function ID 0x82000005 for the eFuse word-read service. The driver currently uses 0x82000001, which does not select this service and prevents eFuse read from working correctly.

Quoted 8 lines

Use the correct function ID and rename the constant to AIROHA_SMC_EFUSE_READ_WORD_FID to make the selected operation explicit.

Fixes b7846af2e6ca ("nvmem: airoha: Add support for SMC eFUSE")

No blank line is allowed after "Fixes:", I'll go fix this up...

author maintainer
#9 lore raw

PATCH 8/10 nvmem: imx-ocotp-scu: assign nvmem_cell_info::raw_len

In reply to [email protected]

From: Stefan Ghetu <[email protected]>

Avoid getting error messages at startup like the following on i.MX8QXP, i.MX8QM and i.MX8DXL, printed once for each MAC address cell:

nvmem imx-scu-ocotp0: cell mac raw len 6 unaligned to nvmem word size 4

The same was already done for imx-ocotp and imx-ocotp-ele by commit 48b5163c9575 ("nvmem: imx: assign nvmem_cell_info::raw_len"), but the SCU based driver was not covered.

This shouldn't cause any functional change as this alignment would otherwise be done in nvmem_cell_info_to_nvmem_cell_entry_nodup().

Fixes 13bcd440f2ff ("nvmem: core: verify cell's raw_len")
---
 drivers/nvmem/imx-ocotp-scu.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/nvmem/imx-ocotp-scu.c +7 -0
index 20312fba22d2..80f78bcf3841 100644
@@ -218,6 +218,12 @@ static int imx_scu_ocotp_write(void *context, unsigned int offset,
 	return res.a0;
 }
 
+static void imx_scu_ocotp_fixup_dt_cell_info(struct nvmem_device *nvmem,
+					     struct nvmem_cell_info *cell)
+{
+	cell->raw_len = round_up(cell->bytes, 4);
+}
+
 static struct nvmem_config imx_scu_ocotp_nvmem_config = {
 	.name = "imx-scu-ocotp",
 	.add_legacy_fixed_of_cells = true,
@@ -227,6 +233,7 @@ static struct nvmem_config imx_scu_ocotp_nvmem_config = {
 	.owner = THIS_MODULE,
 	.reg_read = imx_scu_ocotp_read,
 	.reg_write = imx_scu_ocotp_write,
+	.fixup_dt_cell_info = imx_scu_ocotp_fixup_dt_cell_info,
 };
 
 static const struct of_device_id imx_scu_ocotp_dt_ids[] = {
Signature · 1 line
2.53.0
author maintainer
#10 lore raw

PATCH 9/10 nvmem: core: reject reads and writes beyond the device size

In reply to [email protected]

From: Sanman Pradhan <[email protected]>

The nvmem core passes the requested offset and length to the provider callbacks without checking them against the device size. The sysfs binary attribute is bounded by its size, but the in-kernel device and cell APIs are not, so a caller can request a range that extends past the end of the device.

Add a bounds check to nvmem_access_locked(), the common entry point for device and cell reads and writes, so out-of-range requests are rejected before keepout processing or provider access, rather than relying on each provider to validate. The subtraction-based test avoids an offset + bytes overflow.

Fixes 69aba7948cbe ("nvmem: Add a simple NVMEM framework for consumers")
---
 drivers/nvmem/core.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/nvmem/core.c +3 -0
index b334a64b85fc..fe9b8a3a5ea0 100644
@@ -93,6 +93,9 @@ static int nvmem_access_locked(struct nvmem_device *nvmem,
 	const struct nvmem_keepout *keepoutend;
 	int rc;
 
+	if (offset > nvmem->size || bytes > nvmem->size - offset)
+		return -ERANGE;
+
 	if (!nvmem->nkeepout)
 		return nvmem_access_chunk(nvmem, ops, offset, val, bytes, write);
 
Signature · 1 line
2.53.0
author maintainer
#11 lore raw

PATCH 10/10 nvmem: sprd: fix out-of-bounds read in sprd_efuse_read()

In reply to [email protected]

From: Sanman Pradhan <[email protected]>

sprd_efuse_read() reads a single four-byte block into the stack variable "data" and then copies the caller's full "bytes" length from it. The provider advertises word_size and stride of 1, so a whole-device sysfs read reaches the callback as a single 96-byte request, and __nvmem_cell_read() likewise submits cell->raw_len in one call.

Reading the whole device (96 bytes for ums312_data) therefore copies 92 bytes past the end of the four-byte stack variable into the caller's buffer. The read is reachable from the nvmem sysfs file when CONFIG_NVMEM_SYSFS is enabled, and from any DT cell wider than four bytes. Only the first block is read, so the returned data is also
incorrect: later blocks are never fetched.

Read and copy one block at a time, capping each copy to the bytes left in the block so it cannot exceed sizeof(data), and account for an unaligned starting offset.

Fixes 096030e7f449 ("nvmem: sprd: Add Spreadtrum SoCs eFuse support")
---
 drivers/nvmem/sprd-efuse.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/nvmem/sprd-efuse.c +14 -3
index 5395eb460d34..c4d242326808 100644
@@ -5,6 +5,7 @@
 #include <linux/delay.h>
 #include <linux/hwspinlock.h>
 #include <linux/io.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/nvmem-provider.h>
 #include <linux/of.h>
@@ -297,6 +298,7 @@ static int sprd_efuse_read(void *context, u32 offset, void *val, size_t bytes)
 	bool blk_double = efuse->data->blk_double;
 	u32 index = offset / SPRD_EFUSE_BLOCK_WIDTH + efuse->data->blk_offset;
 	u32 blk_offset = (offset % SPRD_EFUSE_BLOCK_WIDTH) * BITS_PER_BYTE;
+	u8 *buf = val;
 	u32 data;
 	int ret;
 
@@ -308,10 +310,19 @@ static int sprd_efuse_read(void *context, u32 offset, void *val, size_t bytes)
 	if (ret)
 		goto unlock;
 
-	ret = sprd_efuse_raw_read(efuse, index, &data, blk_double);
-	if (!ret) {
+	while (bytes) {
+		size_t avail = SPRD_EFUSE_BLOCK_WIDTH - blk_offset / BITS_PER_BYTE;
+		size_t count = min(bytes, avail);
+
+		ret = sprd_efuse_raw_read(efuse, index++, &data, blk_double);
+		if (ret)
+			break;
+
 		data >>= blk_offset;
-		memcpy(val, &data, bytes);
+		memcpy(buf, &data, count);
+		buf += count;
+		bytes -= count;
+		blk_offset = 0;
 	}
 
 	clk_disable_unprepare(efuse->clk);
Signature · 1 line
2.53.0