nvmem: complete provider and layout disconnection
Subsystems (6)
v3 cover nvmem: complete provider and layout disconnection
NVMEM unregister already disconnects register callbacks with SRCU. Some accesses extend beyond those callbacks: keepout traversal, cell post-processing, DT fixups and device matching can still use provider state. Layout unbind also leaves its cells and their callbacks available to retained consumers.
Extend the existing drain to those accesses, and retire layout-owned cells independently of provider cells. Old handles retain core-owned metadata and reject further access. Explicit cell references and sysfs group ownership make layout removal and rebind independent of existing consumers. Release the write-protect GPIO when callbacks are drained so a retained consumer does not prevent provider reprobe.
The final patch lets providers distinguish an explicit NULL module owner from the parent-driver fallback, for callbacks implemented by built-in subsystem code.
This separates the NVMEM work previously included in the raw NAND retained-handle series [0], following Paul's request for smaller patches [1]. It builds on the NVMEM for-next tree at 6628e7d4c6b, including the existing SRCU protection and device-reference ownership.
[0] https://lore.kernel.org/r/20261115-submit-rawnand-retained-handle-lifetime-v1-v5-1-cbc49c5c5e74@gmail.com/
[1] https://lore.kernel.org/r/[email protected]/
---
Changes in v3:
- Correct the Fixes tag in patch 4 to point to the commit that
disconnected provider writes but kept the write-protect GPIO held.
- Link to v2: https://patch.msgid.link/20261119-submit-nvmem-provider-lifetime-v1-v2-0-8fca3ec5f18c@gmail.com
Changes in v2:
- Use one SRCU read-side section per register or cell access, including
keepout traversal, partial-byte writes and cell post-processing.
- Use device_set_of_node_from_dev() when inheriting the provider's OF
node, following Bartosz's review.
- Use an SRCU notifier chain so listeners can register another provider
while a cell notification is in progress.
- Check cell publication against disconnection under the list lock and
drain cell-add notifications before reporting provider removal.
- Simplify error unwinding in sysfs cell creation and layout registration.
- Link to v1: https://patch.msgid.link/20261117-submit-nvmem-provider-lifetime-v1-v1-0-3cfbd48812b5@gmail.com
---
James Hilliard (11):
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
nvmem: reference cell entries independently of their provider list
nvmem: manage cell attributes independently of device resources
nvmem: distinguish layout cells from provider cells
nvmem: disconnect cells before removing their layout
nvmem: disconnect published devices before registration rollback
nvmem: u-boot-env: propagate cell registration errors
nvmem: allow providers to specify an explicit NULL owner
drivers/nvmem/core.c | 545 ++++++++++++++++++++++------------- drivers/nvmem/internals.h | 4 + drivers/nvmem/layouts.c | 2 + drivers/nvmem/layouts/fixed-layout.c | 2 +- drivers/nvmem/layouts/onie-tlv.c | 19 +- drivers/nvmem/layouts/sl28vpd.c | 2 +- drivers/nvmem/layouts/u-boot-env.c | 25 +- include/linux/nvmem-provider.h | 16 +- 8 files changed, 401 insertions(+), 214 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
base-tree: https://git.kernel.org/pub/scm/linux/kernel/git/srini/nvmem.git for-next
change-id: 20261117-submit-nvmem-provider-lifetime-v1-fb2b3d192fca
Best regards,
Signature · 1 line
James Hilliard <[email protected]>
PATCH v3 1/11 nvmem: retain the provider node until device release
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.
--- drivers/nvmem/core.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/nvmem/core.c +3 -2
@@ -570,6 +570,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); }
@@ -973,9 +974,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
Srinivas Kandagatla
maintainerRe: [PATCH v3 01/11] nvmem: retain the provider node until device release
Quoted 6 lines · James Hilliard
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")
Thanks for sending out the patches, Few things to remember.
1. None of the fixes have Stable Tag,
2. fixes should be first in the series, so that it becomes easy to pick them up for next rc.
I have picked up few fixes with these changes but pl be mindful next time.
--srini
Quoted 30 lines
Signed-off-by James Hilliard <[email protected]>--- drivers/nvmem/core.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/nvmem/core.c +3
-2@@ -570,6 +570,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); }@@ -973,9 +974,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 :
PATCH v3 2/11 nvmem: drain keepout and cell post-processing accesses
In reply to James HilliardA 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.
--- drivers/nvmem/core.c | 110 ++++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 54 deletions(-)
drivers/nvmem/core.c +56 -54
@@ -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
@@ -1649,10 +1637,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;
@@ -1708,6 +1701,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;
@@ -1727,7 +1721,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;
@@ -1746,8 +1740,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;
@@ -1763,10 +1757,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 ;/*
@@ -1780,12 +1782,12 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, siif (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
PATCH v3 3/11 nvmem: exclude disconnected providers from callback admission
In reply to James HilliardA 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.
--- drivers/nvmem/core.c | 112 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 81 insertions(+), 31 deletions(-)
drivers/nvmem/core.c +81 -31
@@ -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
@@ -535,7 +544,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);
@@ -571,11 +580,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,
@@ -653,6 +666,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 ;
@@ -663,9 +680,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);
@@ -698,11 +718,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);
@@ -715,7 +738,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);
@@ -795,6 +818,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 };
@@ -851,8 +878,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;
@@ -874,6 +904,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
@@ -1034,7 +1075,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;
@@ -1047,10 +1088,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); }
@@ -1063,19 +1102,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); }
@@ -1115,17 +1149,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
PATCH v3 4/11 nvmem: release write protection when the provider disconnects
In reply to James HilliardRetained 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.
--- drivers/nvmem/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvmem/core.c +2 -1
@@ -564,7 +564,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);
@@ -912,6 +911,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
PATCH v3 5/11 nvmem: reference cell entries independently of their provider list
In reply to James HilliardCell handles currently borrow entries from the provider list. Give each list entry a reference and acquire another during lookup, before releasing nvmem_mutex. Return that reference when the consumer releases its handle or handle allocation fails.
This separates cell metadata lifetime from list membership, so a cell can be unpublished while existing consumers retain its description.
--- drivers/nvmem/core.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-)
drivers/nvmem/core.c +27 -8
@@ -27,6 +27,7 @@#define FLAG_COMPAT BIT(0)struct nvmem_cell_entry { +struct kref refcnt;const char *name;int offset;size_t raw_len;
@@ -54,6 +55,8 @@ static LIST_HEAD(nvmem_lookup_list);SRCU_NOTIFIER_HEAD_STATIC (nvmem_notifier); +static void nvmem_cell_entry_put (struct nvmem_cell_entry *cell); +static int nvmem_access_chunk (struct nvmem_device *nvmem,const struct nvmem_operations *ops,unsigned int offset,void *val,size_t bytes,bool write)
@@ -542,22 +545,30 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)#endif /* CONFIG_NVMEM_SYSFS */ -static void nvmem_cell_entry_drop (struct nvmem_cell_entry *cell) +static void nvmem_cell_entry_release (struct kref *ref) { -srcu_notifier_call_chain (&nvmem_notifier,NVMEM_CELL_REMOVE , cell); - scoped_guard(mutex, &nvmem_mutex) -list_del (&cell->node); + struct nvmem_cell_entry *cell = container_of(ref, struct nvmem_cell_entry, refcnt); +of_node_put (cell->np);kfree_const (cell->name);kfree (cell); } +static void nvmem_cell_entry_put (struct nvmem_cell_entry *cell) +{ +kref_put (&cell->refcnt, nvmem_cell_entry_release); +} +static void nvmem_device_remove_all_cells (const struct nvmem_device *nvmem) {struct nvmem_cell_entry *cell, *p; -list_for_each_entry_safe (cell, p, &nvmem->cells, node) -nvmem_cell_entry_drop (cell); +list_for_each_entry_safe (cell, p, &nvmem->cells, node) { +srcu_notifier_call_chain (&nvmem_notifier,NVMEM_CELL_REMOVE , cell); + scoped_guard(mutex, &nvmem_mutex) +list_del (&cell->node); +nvmem_cell_entry_put (cell); + } }static void nvmem_release (struct device *dev)
@@ -584,6 +595,7 @@ static int nvmem_cell_entry_add(struct nvmem_cell_entry *cell) scoped_guard(mutex, &nvmem_mutex) {if (!rcu_access_pointer (cell->nvmem->ops))return -ENODEV ; +kref_init (&cell->refcnt);list_add_tail (&cell->node, &cell->nvmem->cells); }srcu_notifier_call_chain (&nvmem_notifier,NVMEM_CELL_ADD , cell);
@@ -751,6 +763,7 @@ nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)list_for_each_entry (iter, &nvmem->cells, node) {if (strcmp (cell_id, iter->name) ==0 ) { cell = iter; +kref_get (&cell->refcnt);break ; } }
@@ -1397,8 +1410,10 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id) cell =ERR_PTR (-ENOENT ); }else { cell =nvmem_create_cell (cell_entry, con_id,0 ); -if (IS_ERR (cell)) +if (IS_ERR (cell)) { +nvmem_cell_entry_put (cell_entry);nvmem_device_put (nvmem); + } }break ; }
@@ -1422,8 +1437,10 @@ nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *npguard (mutex)(&nvmem_mutex);list_for_each_entry (cell, &nvmem->cells, node) { -if (np == cell->np) +if (np == cell->np) { +kref_get (&cell->refcnt);return cell; + } }return NULL ;
@@ -1522,6 +1539,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id) cell =nvmem_create_cell (cell_entry, id, cell_index);if (IS_ERR (cell)) {nvmem_layout_module_put (nvmem); +nvmem_cell_entry_put (cell_entry);nvmem_device_put (nvmem); }
@@ -1635,6 +1653,7 @@ void nvmem_cell_put(struct nvmem_cell *cell)if (cell->id)kfree_const (cell->id); +nvmem_cell_entry_put (cell->entry);kfree (cell);nvmem_layout_module_put (nvmem);nvmem_device_put (nvmem);
Signature · 1 line
2.53.0
PATCH v3 6/11 nvmem: manage cell attributes independently of device resources
In reply to James HilliardCell attributes are populated once and remain until device deletion. A layout bound after provider registration therefore cannot publish its cells through an existing group, and the group cannot be refreshed when cells are removed.
Keep the group in the NVMEM device and explicitly allocate and release its attributes. Each attribute holds a cell reference until sysfs has drained its readers. Rebuild the group from the current cell list, and unwind partial allocations if population fails.
Serialize group replacement separately from cell-list updates so removal can wait for sysfs readers without holding nvmem_mutex. Remove the group before device deletion on both unregister and registration failure.
--- drivers/nvmem/core.c | 131 +++++++++++++++++++++++++++++++--------------- drivers/nvmem/internals.h | 3 ++ 2 files changed, 92 insertions(+), 42 deletions(-)
drivers/nvmem/core.c +89 -42
@@ -13,6 +13,7 @@#include <linux/idr.h> #include <linux/init.h> #include <linux/kref.h> +#include <linux/lockdep.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/nvmem-consumer.h>
@@ -476,59 +477,98 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)device_remove_bin_file (nvmem->base_dev, &nvmem->eeprom); } -static int nvmem_populate_sysfs_cells (struct nvmem_device *nvmem) +static void nvmem_sysfs_free_cell_attrs (const struct bin_attribute *const *attrs) { -struct attribute_group group = { - .name ="cells" , - }; -struct nvmem_cell_entry *entry; -const struct bin_attribute **pattrs; -struct bin_attribute *attrs; -unsigned int ncells =0 , i =0 ; -int ret; +if (!attrs) +return ; -guard (mutex)(&nvmem_mutex); +for (int i =0 ; attrs[i]; i++) { +nvmem_cell_entry_put (attrs[i]->private); +kfree (attrs[i]->attr.name); +kfree (attrs[i]); + } +kfree (attrs); +} -if (list_empty (&nvmem->cells) || nvmem->sysfs_cells_populated) -return 0 ; +static void __nvmem_sysfs_remove_cells (struct nvmem_device *nvmem) +{ +lockdep_assert_held (&nvmem->sysfs_mutex); -/* Allocate an array of attributes with a sentinel */ - ncells =list_count_nodes (&nvmem->cells); - pattrs =devm_kcalloc (&nvmem->dev, ncells +1 , -sizeof (*pattrs),GFP_KERNEL ); -if (!pattrs) -return -ENOMEM ; +if (!nvmem->sysfs_cells_populated) +return ; - attrs =devm_kcalloc (&nvmem->dev, ncells,sizeof (struct bin_attribute ),GFP_KERNEL ); -if (!attrs) -return -ENOMEM ; +device_remove_group (&nvmem->dev, &nvmem->cells_group); +nvmem_sysfs_free_cell_attrs (nvmem->cells_group.bin_attrs); + nvmem->cells_group.bin_attrs =NULL ; + nvmem->sysfs_cells_populated = false; +} -/* Initialize each attribute to take the name and size of the cell */ -list_for_each_entry (entry, &nvmem->cells, node) { -sysfs_bin_attr_init (&attrs[i]); - attrs[i].attr.name =devm_kasprintf (&nvmem->dev,GFP_KERNEL , -"%s@%x,%x" , entry->name, - entry->offset, - entry->bit_offset); - attrs[i].attr.mode =0444 &nvmem_bin_attr_get_umode (nvmem); - attrs[i].size = entry->bytes; - attrs[i].read = &nvmem_cell_attr_read; - attrs[i].private = entry; -if (!attrs[i].attr.name) -return -ENOMEM ; +static void nvmem_sysfs_remove_cells (struct nvmem_device *nvmem) +{ +guard (mutex)(&nvmem->sysfs_mutex); +__nvmem_sysfs_remove_cells (nvmem); +} - pattrs[i] = &attrs[i]; - i++; - } +static int nvmem_populate_sysfs_cells (struct nvmem_device *nvmem) +{ +struct nvmem_cell_entry *entry; +const struct bin_attribute **attrs =NULL ; +unsigned int i =0 ; +int ret =0 ; - group.bin_attrs = pattrs; +guard (mutex)(&nvmem->sysfs_mutex); - ret =device_add_group (&nvmem->dev, &group); -if (ret) -return ret; +__nvmem_sysfs_remove_cells (nvmem); + + scoped_guard(mutex, &nvmem_mutex) { +if (!rcu_access_pointer (nvmem->ops)) +return -ENODEV ; +if (list_empty (&nvmem->cells)) +return 0 ; - nvmem->sysfs_cells_populated = true; + attrs =kcalloc (list_count_nodes (&nvmem->cells) +1 , +sizeof (*attrs),GFP_KERNEL ); +if (!attrs) +return -ENOMEM ; +list_for_each_entry (entry, &nvmem->cells, node) { +struct bin_attribute *attr; + + attr =kzalloc_obj (*attr); +if (!attr) { + ret = -ENOMEM ; +break ; + } +sysfs_bin_attr_init (attr); + attr->attr.name =kasprintf (GFP_KERNEL ,"%s@%x,%x" , + entry->name, entry->offset, + entry->bit_offset); +if (!attr->attr.name) { +kfree (attr); + ret = -ENOMEM ; +break ; + } + attr->attr.mode =0444 &nvmem_bin_attr_get_umode (nvmem); + attr->size = entry->bytes; + attr->read = nvmem_cell_attr_read; + attr->private = entry; +kref_get (&entry->refcnt); + attrs[i++] = attr; + } + } +if (!ret) { + nvmem->cells_group.name ="cells" ; + nvmem->cells_group.bin_attrs = attrs; + + ret =device_add_group (&nvmem->dev, &nvmem->cells_group); +if (!ret) { + nvmem->sysfs_cells_populated = true; +return 0 ; + } + nvmem->cells_group.bin_attrs =NULL ; + } + +nvmem_sysfs_free_cell_attrs (attrs);return ret; }
@@ -543,6 +583,10 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem) { } +static void nvmem_sysfs_remove_cells (struct nvmem_device *nvmem) +{ +} +#endif /* CONFIG_NVMEM_SYSFS */ static void nvmem_cell_entry_release (struct kref *ref)
@@ -990,6 +1034,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)rcu_assign_pointer (nvmem->ops, ops);device_initialize (&nvmem->dev); +mutex_init (&nvmem->sysfs_mutex);if (!config->ignore_wp) nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
@@ -1098,6 +1143,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)nvmem_destroy_layout (nvmem);#endif err_remove_dev: +nvmem_sysfs_remove_cells (nvmem);device_del (&nvmem->dev); err_remove_compat:nvmem_sysfs_remove_compat (nvmem);
@@ -1125,6 +1171,7 @@ void nvmem_unregister(struct nvmem_device *nvmem)nvmem_sysfs_remove_compat (nvmem);nvmem_destroy_layout (nvmem); +nvmem_sysfs_remove_cells (nvmem);device_unregister (&nvmem->dev); }EXPORT_SYMBOL_GPL (nvmem_unregister);
drivers/nvmem/internals.h +3 -0
@@ -4,6 +4,7 @@#define _LINUX_NVMEM_INTERNALS_H#include <linux/device.h> +#include <linux/mutex.h> #include <linux/nvmem-consumer.h> #include <linux/nvmem-provider.h> #include <linux/srcu.h>
@@ -37,7 +38,9 @@ struct nvmem_device {struct nvmem_layout *layout;struct nvmem_operations __rcu *ops;void *priv; +struct mutex sysfs_mutex;bool sysfs_cells_populated; +struct attribute_group cells_group; };int nvmem_add_cells_from_dt (struct nvmem_device *nvmem,struct device_node *np);
Signature · 1 line
2.53.0
PATCH v3 7/11 nvmem: distinguish layout cells from provider cells
In reply to James HilliardLayouts and providers currently add cells through the same interface. Record the owner when a layout adds a cell so removing that layout can leave independently registered provider cells intact.
Add layout-specific registration helpers and convert all layout drivers. Keep the shared U-Boot environment parser usable by both its layout driver and the legacy provider, recording layout ownership only for the layout caller.
--- drivers/nvmem/core.c | 61 +++++++++++++++++++++++++++++------- drivers/nvmem/internals.h | 1 + drivers/nvmem/layouts/fixed-layout.c | 2 +- drivers/nvmem/layouts/onie-tlv.c | 19 +++++------ drivers/nvmem/layouts/sl28vpd.c | 2 +- drivers/nvmem/layouts/u-boot-env.c | 22 +++++++++---- include/linux/nvmem-provider.h | 10 +++++- 7 files changed, 87 insertions(+), 30 deletions(-)
drivers/nvmem/core.c +49 -12
@@ -29,6 +29,7 @@#define FLAG_COMPAT BIT(0)struct nvmem_cell_entry {struct kref refcnt; +struct nvmem_layout *layout;const char *name;int offset;size_t raw_len;
@@ -707,16 +708,9 @@ static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,return 0 ; } -/** -* nvmem_add_one_cell() - Add one cell information to an nvmem device -* -* @nvmem: nvmem device to add cells to. -* @info: nvmem cell info to add to the device -* -* Return: 0 or negative error code on failure. -*/ -int nvmem_add_one_cell (struct nvmem_device *nvmem, -const struct nvmem_cell_info *info) +static int __nvmem_add_one_cell (struct nvmem_device *nvmem, +const struct nvmem_cell_info *info, +struct nvmem_layout *layout) {struct nvmem_cell_entry *cell;int rval;
@@ -735,6 +729,7 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,return rval; } + cell->layout = layout; rval =nvmem_cell_entry_add (cell);if (rval) {kfree_const (cell->name);
@@ -742,8 +737,38 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem, }return rval; } + +/** +* nvmem_add_one_cell() - Add one cell information to an nvmem device +* +* @nvmem: nvmem device to add cells to. +* @info: nvmem cell info to add to the device +* +* Return: 0 or negative error code on failure. +*/ +int nvmem_add_one_cell (struct nvmem_device *nvmem, +const struct nvmem_cell_info *info) +{ +return __nvmem_add_one_cell (nvmem, info,NULL ); +}EXPORT_SYMBOL_GPL (nvmem_add_one_cell); +/** +* nvmem_layout_add_cell() - Add a cell owned by an NVMEM layout +* @layout: Layout registering the cell from its add_cells callback. +* @info: Cell description. +* +* Associate the cell with the layout rather than the provider. +* +* Return: 0 or a negative error code. +*/ +int nvmem_layout_add_cell (struct nvmem_layout *layout, +const struct nvmem_cell_info *info) +{ +return __nvmem_add_one_cell (layout->nvmem, info, layout); +} +EXPORT_SYMBOL_GPL (nvmem_layout_add_cell); +/** * nvmem_add_cells() - Add cell information to an nvmem device *
@@ -868,7 +893,8 @@ static int nvmem_validate_keepouts(struct nvmem_device *nvmem)return 0 ; } -int nvmem_add_cells_from_dt (struct nvmem_device *nvmem,struct device_node *np) +static int __nvmem_add_cells_from_dt (struct nvmem_device *nvmem,struct device_node *np, +struct nvmem_layout *layout) {struct device *dev = &nvmem->dev;const __be32 *addr;
@@ -910,7 +936,7 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)if (nvmem->fixup_dt_cell_info) nvmem->fixup_dt_cell_info (nvmem, &info); - ret =nvmem_add_one_cell (nvmem, &info); + ret =__nvmem_add_one_cell (nvmem, &info, layout);kfree (info.name);if (ret) {of_node_put (info.np);
@@ -920,8 +946,19 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)return 0 ; } + +int nvmem_add_cells_from_dt (struct nvmem_device *nvmem,struct device_node *np) +{ +return __nvmem_add_cells_from_dt (nvmem, np,NULL ); +}EXPORT_SYMBOL_GPL (nvmem_add_cells_from_dt); +int nvmem_layout_add_cells_from_dt (struct nvmem_layout *layout,struct device_node *np) +{ +return __nvmem_add_cells_from_dt (layout->nvmem, np, layout); +} +EXPORT_SYMBOL_GPL (nvmem_layout_add_cells_from_dt); +static int nvmem_add_cells_from_legacy_of (struct nvmem_device *nvmem) {return nvmem_add_cells_from_dt (nvmem, nvmem->dev.of_node);
drivers/nvmem/internals.h +1 -0
@@ -44,6 +44,7 @@ struct nvmem_device { };int nvmem_add_cells_from_dt (struct nvmem_device *nvmem,struct device_node *np); +int nvmem_layout_add_cells_from_dt (struct nvmem_layout *layout,struct device_node *np);#if IS_ENABLED (CONFIG_OF )int nvmem_layout_bus_register (void );
drivers/nvmem/layouts/fixed-layout.c +1 -1
@@ -19,7 +19,7 @@ static int fixed_layout_add_cells(struct nvmem_layout *layout)if (!np)return -ENOENT ; - ret =nvmem_add_cells_from_dt (layout->nvmem, np); + ret =nvmem_layout_add_cells_from_dt (layout, np);of_node_put (np);return ret;
drivers/nvmem/layouts/onie-tlv.c +10 -9
@@ -95,18 +95,19 @@ static nvmem_cell_post_process_t onie_tlv_read_cb(u8 type, u8 *buf)return NULL ; } -static int onie_tlv_add_cells (struct device *dev,struct nvmem_device *nvmem, +static int onie_tlv_add_cells (struct nvmem_layout *layout,size_t data_len,u8 *data) { +struct device *dev = &layout->dev;struct nvmem_cell_info cell = {}; -struct device_node *layout; +struct device_node *layout_np;struct onie_tlv tlv;unsigned int hdr_len =sizeof (struct onie_tlv_hdr );unsigned int offset =0 ;int ret; - layout =of_nvmem_layout_get_container (nvmem); -if (!layout) + layout_np =of_nvmem_layout_get_container (layout->nvmem); +if (!layout_np)return -ENOENT ;while (offset < data_len) {
@@ -123,13 +124,13 @@ static int onie_tlv_add_cells(struct device *dev, struct nvmem_device *nvmem, cell.offset = hdr_len + offset +sizeof (tlv.type) +sizeof (tlv.len); cell.bytes = tlv.len; - cell.np =of_get_child_by_name (layout, cell.name); + cell.np =of_get_child_by_name (layout_np, cell.name); cell.read_post_process =onie_tlv_read_cb (tlv.type, data + offset +sizeof (tlv)); - ret =nvmem_add_one_cell (nvmem, &cell); + ret =nvmem_layout_add_cell (layout, &cell);if (ret) {of_node_put (cell.np); -of_node_put (layout); +of_node_put (layout_np);return ret; }
@@ -137,7 +138,7 @@ static int onie_tlv_add_cells(struct device *dev, struct nvmem_device *nvmem, offset +=sizeof (tlv) + tlv.len; } -of_node_put (layout); +of_node_put (layout_np);return 0 ; }
@@ -222,7 +223,7 @@ static int onie_tlv_parse_table(struct nvmem_layout *layout)return -EINVAL ; data = table + hdr_len; - ret =onie_tlv_add_cells (dev, nvmem, data_len, data); + ret =onie_tlv_add_cells (layout, data_len, data);if (ret)return ret;
drivers/nvmem/layouts/sl28vpd.c +1 -1
@@ -124,7 +124,7 @@ static int sl28vpd_add_cells(struct nvmem_layout *layout) info.read_post_process = pinfo->read_post_process; info.np =of_get_child_by_name (layout_np, pinfo->name); - ret =nvmem_add_one_cell (nvmem, &info); + ret =nvmem_layout_add_cell (layout, &info);if (ret) {of_node_put (info.np);of_node_put (layout_np);
drivers/nvmem/layouts/u-boot-env.c +16 -6
@@ -49,7 +49,8 @@ static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, ireturn 0 ; } -static int u_boot_env_parse_cells (struct device *dev,struct nvmem_device *nvmem,uint8_t *buf, +static int u_boot_env_parse_cells (struct device *dev,struct nvmem_device *nvmem, +struct nvmem_layout *layout,uint8_t *buf,size_t data_offset,size_t data_len) {char *data = buf + data_offset;
@@ -78,14 +79,17 @@ static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem info.read_post_process = u_boot_env_read_post_process_ethaddr; } -nvmem_add_one_cell (nvmem, &info); +if (layout) +nvmem_layout_add_cell (layout, &info); +else +nvmem_add_one_cell (nvmem, &info); }return 0 ; } -int u_boot_env_parse (struct device *dev,struct nvmem_device *nvmem, -enum u_boot_env_format format) +static int __u_boot_env_parse (struct device *dev,struct nvmem_device *nvmem, +struct nvmem_layout *layout,enum u_boot_env_format format) {size_t crc32_data_offset;size_t crc32_data_len;
@@ -156,13 +160,19 @@ int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem, } buf[dev_size -1 ] ='\0' ; - err =u_boot_env_parse_cells (dev, nvmem, buf, data_offset, data_len); + err =u_boot_env_parse_cells (dev, nvmem, layout, buf, data_offset, data_len); err_kfree:kfree (buf); err_out:return err; } + +int u_boot_env_parse (struct device *dev,struct nvmem_device *nvmem, +enum u_boot_env_format format) +{ +return __u_boot_env_parse (dev, nvmem,NULL , format); +}EXPORT_SYMBOL_GPL (u_boot_env_parse);static int u_boot_env_add_cells (struct nvmem_layout *layout)
@@ -172,7 +182,7 @@ static int u_boot_env_add_cells(struct nvmem_layout *layout) format = (uintptr_t )device_get_match_data (dev); -return u_boot_env_parse (dev, layout->nvmem, format); +return __u_boot_env_parse (dev, layout->nvmem, layout, format); }static int u_boot_env_probe (struct nvmem_layout *layout)
include/linux/nvmem-provider.h +9 -1
@@ -144,7 +144,7 @@ struct nvmem_config {* @nvmem: The underlying NVMEM device * @add_cells: Will be called if a nvmem device is found which * has this layout. The function will add layout -* specific cells with nvmem_add_one_cell(). +* specific cells with nvmem_layout_add_cell(). * * A nvmem device can hold a well defined structure which can just be * evaluated during runtime. For example a TLV list, or a list of "name=val"
@@ -173,6 +173,8 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,int nvmem_add_one_cell (struct nvmem_device *nvmem,const struct nvmem_cell_info *info); +int nvmem_layout_add_cell (struct nvmem_layout *layout, +const struct nvmem_cell_info *info);int nvmem_layout_register (struct nvmem_layout *layout);void nvmem_layout_unregister (struct nvmem_layout *layout);
@@ -214,6 +216,12 @@ static inline int nvmem_layout_register(struct nvmem_layout *layout)static inline void nvmem_layout_unregister (struct nvmem_layout *layout) {} +static inline int nvmem_layout_add_cell (struct nvmem_layout *layout, +const struct nvmem_cell_info *info) +{ +return -EOPNOTSUPP ; +} +static inline int nvmem_add_cells_from_dt (struct nvmem_device *nvmem,struct device_node *np) {
Signature · 1 line
2.53.0
PATCH v3 8/11 nvmem: disconnect cells before removing their layout
In reply to James HilliardA layout can be unbound while NVMEM consumers retain its cells. Keeping the layout module loaded does not retain its binding or private data, and looking up the module again at cell put can access a removed layout.
A failed add_cells callback can also leave cells registered even though the layout never bound. The sysfs failure path instead removes every cell, including cells supplied by the provider.
Remove the layout-owned cells from lookup and mark them disconnected. Drain their sysfs attributes and SRCU-protected accesses before returning to the layout driver. Old handles retain only cell metadata and reject I/O; a later layout probe creates new cells. Use the same cleanup when cell registration or sysfs population fails.
Replace the layout module get/put paths with this drain. Leave provider cells registered and rebuild their attributes after layout removal. Clear the provider-to-layout link when the layout device is unregistered.
--- drivers/nvmem/core.c | 78 +++++++++++++++++++++++-------------------------- drivers/nvmem/layouts.c | 2 ++ 2 files changed, 38 insertions(+), 42 deletions(-)
drivers/nvmem/core.c +36 -42
@@ -29,6 +29,7 @@#define FLAG_COMPAT BIT(0)struct nvmem_cell_entry {struct kref refcnt; +bool disconnected;struct nvmem_layout *layout;const char *name;int offset;
@@ -652,6 +653,7 @@ static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem, struct nvmem_cell_entry *cell) { cell->nvmem = nvmem; + cell->disconnected = false; cell->offset = info->offset; cell->raw_len = info->raw_len ?: info->bytes; cell->bytes = info->bytes;
@@ -976,24 +978,44 @@ int nvmem_layout_register(struct nvmem_layout *layout)return -ENODEV ; ret = layout->add_cells (layout); } -if (ret) -return ret; -#ifdef CONFIG_NVMEM_SYSFS - ret =nvmem_populate_sysfs_cells (layout->nvmem); -if (ret) { -nvmem_device_remove_all_cells (layout->nvmem); -return ret; - } +if (!ret) + ret =nvmem_populate_sysfs_cells (layout->nvmem);#endif - -return 0 ; +if (ret) +nvmem_layout_unregister (layout); +return ret; }EXPORT_SYMBOL_GPL (nvmem_layout_register);void nvmem_layout_unregister (struct nvmem_layout *layout) { -/* Keep the API even with an empty stub in case we need it later */ +struct nvmem_device *nvmem = layout->nvmem; +struct nvmem_cell_entry *cell, *next; +LIST_HEAD (cells); + + scoped_guard(mutex, &nvmem_mutex) { +list_for_each_entry_safe (cell, next, &nvmem->cells, node) { +if (cell->layout != layout) +continue ; +WRITE_ONCE (cell->disconnected, true); +list_move_tail (&cell->node, &cells); + } + } + +nvmem_sysfs_remove_cells (nvmem); + +/* Old cell handles retain data, never a departing layout's callbacks. */ +synchronize_srcu (&nvmem->srcu); +list_for_each_entry_safe (cell, next, &cells, node) { +srcu_notifier_call_chain (&nvmem_notifier,NVMEM_CELL_REMOVE , cell); +list_del (&cell->node); +nvmem_cell_entry_put (cell); + } +#ifdef CONFIG_NVMEM_SYSFS +if (rcu_access_pointer (nvmem->ops) &&nvmem_populate_sysfs_cells (nvmem)) +dev_warn (&nvmem->dev,"Failed to restore provider cell attributes\n" ); +#endif }EXPORT_SYMBOL_GPL (nvmem_layout_unregister);
@@ -1506,12 +1528,6 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)return cell; } -static void nvmem_layout_module_put (struct nvmem_device *nvmem) -{ -if (nvmem->layout && nvmem->layout->dev.driver) -module_put (nvmem->layout->dev.driver->owner); -} -#if IS_ENABLED (CONFIG_OF )static struct nvmem_cell_entry *nvmem_find_cell_entry_by_node (struct nvmem_device *nvmem,struct device_node *np)
@@ -1530,18 +1546,6 @@ nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *npreturn NULL ; } -static int nvmem_layout_module_get_optional (struct nvmem_device *nvmem) -{ -if (!nvmem->layout) -return 0 ; - -if (!nvmem->layout->dev.driver || - !try_module_get (nvmem->layout->dev.driver->owner)) -return -EPROBE_DEFER ; - -return 0 ; -} -/** * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id *
@@ -1604,25 +1608,16 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)return ERR_CAST (nvmem); } - ret =nvmem_layout_module_get_optional (nvmem); -if (ret) { -of_node_put (cell_np); -nvmem_device_put (nvmem); -return ERR_PTR (ret); - } - cell_entry =nvmem_find_cell_entry_by_node (nvmem, cell_np);of_node_put (cell_np);if (!cell_entry) { -nvmem_layout_module_put (nvmem); - ret = nvmem->layout ? -EPROBE_DEFER : -ENOENT ; + ret =READ_ONCE (nvmem->layout) ? -EPROBE_DEFER : -ENOENT ;nvmem_device_put (nvmem);return ERR_PTR (ret); } cell =nvmem_create_cell (cell_entry, id, cell_index);if (IS_ERR (cell)) { -nvmem_layout_module_put (nvmem);nvmem_cell_entry_put (cell_entry);nvmem_device_put (nvmem); }
@@ -1739,7 +1734,6 @@ void nvmem_cell_put(struct nvmem_cell *cell)nvmem_cell_entry_put (cell->entry);kfree (cell); -nvmem_layout_module_put (nvmem);nvmem_device_put (nvmem); }EXPORT_SYMBOL_GPL (nvmem_cell_put);
@@ -1796,7 +1790,7 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,guard (srcu)(&nvmem->srcu); ops =srcu_dereference (nvmem->ops, &nvmem->srcu); -if (!ops) +if (!ops ||READ_ONCE (cell->disconnected))return -ENODEV ; rc =nvmem_access_locked (nvmem, ops, cell->offset, buf, cell->raw_len, false);
@@ -1919,7 +1913,7 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, siguard (srcu)(&nvmem->srcu); ops =srcu_dereference (nvmem->ops, &nvmem->srcu); -if (!ops) +if (!ops ||READ_ONCE (cell->disconnected))return -ENODEV ;if (nvmem->read_only || (cell->bit_offset ==0 && len != cell->bytes))
drivers/nvmem/layouts.c +2 -0
@@ -118,6 +118,7 @@ static int nvmem_layout_create_device(struct nvmem_device *nvmem, ret =device_add (dev);if (ret) { + nvmem->layout =NULL ;put_device (dev);return ret; }
@@ -203,6 +204,7 @@ void nvmem_destroy_layout(struct nvmem_device *nvmem) dev = &nvmem->layout->dev;of_node_clear_flag (dev->of_node,OF_POPULATED_BUS );device_unregister (dev); +WRITE_ONCE (nvmem->layout,NULL ); }int nvmem_layout_bus_register (void )
Signature · 1 line
2.53.0
PATCH v3 9/11 nvmem: disconnect published devices before registration rollback
In reply to James HilliardRegistration can fail after the NVMEM device is visible and a layout has published cells. Disconnect the provider before destroying that layout and deleting the device, just as for normal unregister. This prevents layout removal from rebuilding attributes while rollback removes them.
Reuse nvmem_unregister() for failures after device_add(). Track whether the registration notification was emitted so an unsuccessful registration does not generate an unmatched removal notification.
Document when providers may release their callback state.
--- drivers/nvmem/core.c | 22 ++++++++++++---------- include/linux/nvmem-provider.h | 3 +++ 2 files changed, 15 insertions(+), 10 deletions(-)
drivers/nvmem/core.c +12 -10
@@ -27,6 +27,7 @@#define to_nvmem_device (d) container_of(d, struct nvmem_device, dev)#define FLAG_COMPAT BIT(0) +#define FLAG_REGISTERED BIT(1)struct nvmem_cell_entry {struct kref refcnt;bool disconnected;
@@ -1181,7 +1182,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) rval =nvmem_populate_layout (nvmem);if (rval) - goto err_remove_dev; + goto err_unregister;/* If the device has WP GPIO, default to read-only */ if (nvmem->wp_gpio)
@@ -1190,20 +1191,17 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)#ifdef CONFIG_NVMEM_SYSFS rval =nvmem_populate_sysfs_cells (nvmem);if (rval) - goto err_destroy_layout; + goto err_unregister;#endif + nvmem->flags |=FLAG_REGISTERED ;srcu_notifier_call_chain (&nvmem_notifier,NVMEM_ADD , nvmem);return nvmem; -#ifdef CONFIG_NVMEM_SYSFS -err_destroy_layout: -nvmem_destroy_layout (nvmem); -#endif -err_remove_dev: -nvmem_sysfs_remove_cells (nvmem); -device_del (&nvmem->dev); +err_unregister: +nvmem_unregister (nvmem); +return ERR_PTR (rval); err_remove_compat:nvmem_sysfs_remove_compat (nvmem); err_put_device:
@@ -1218,6 +1216,9 @@ EXPORT_SYMBOL_GPL(nvmem_register);* nvmem_unregister() - Unregister previously registered nvmem device * * @nvmem: Pointer to previously registered nvmem device. +* +* Stop and drain provider callbacks before returning. Existing consumers keep +* their core-owned metadata, but further access returns -ENODEV. */ void nvmem_unregister (struct nvmem_device *nvmem) {
@@ -1225,7 +1226,8 @@ void nvmem_unregister(struct nvmem_device *nvmem)return ;nvmem_disconnect (nvmem); -srcu_notifier_call_chain (&nvmem_notifier,NVMEM_REMOVE , nvmem); +if (nvmem->flags &FLAG_REGISTERED ) +srcu_notifier_call_chain (&nvmem_notifier,NVMEM_REMOVE , nvmem);nvmem_sysfs_remove_compat (nvmem);nvmem_destroy_layout (nvmem);
include/linux/nvmem-provider.h +3 -0
@@ -107,6 +107,9 @@ struct nvmem_cell_info {* * Note: Specifying name and setting id to -1 implies a unique device * whose name is provided as-is (kept unaltered). +* +* Provider callbacks and @priv must remain valid until nvmem_unregister() +* returns, or until a failed nvmem_register() returns. */ struct nvmem_config {struct device *dev;
Signature · 1 line
2.53.0
PATCH v3 10/11 nvmem: u-boot-env: propagate cell registration errors
In reply to James HilliardThe environment parser ignores cell registration failures, so allocation failures can leave a partially populated layout reported as successfully registered. The OF node reference obtained for a rejected cell is also leaked.
Return the registration error and release the node reference that was not transferred to a cell.
--- drivers/nvmem/layouts/u-boot-env.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/nvmem/layouts/u-boot-env.c +7 -4
@@ -55,6 +55,7 @@ static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem {char *data = buf + data_offset;char *var, *value, *eq; +int ret; for (var = data; var < data + data_len && *var;
@@ -79,10 +80,12 @@ static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem info.read_post_process = u_boot_env_read_post_process_ethaddr; } -if (layout) -nvmem_layout_add_cell (layout, &info); -else -nvmem_add_one_cell (nvmem, &info); + ret = layout ?nvmem_layout_add_cell (layout, &info) : +nvmem_add_one_cell (nvmem, &info); +if (ret) { +of_node_put (info.np); +return ret; + } }return 0 ;
Signature · 1 line
2.53.0
PATCH v3 11/11 nvmem: allow providers to specify an explicit NULL owner
In reply to James HilliardThe owner fallback treats NULL as an unspecified module. A provider whose operations belong to built-in subsystem code therefore cannot distinguish its deliberate NULL owner from a request to use the parent driver module.
Add owner_is_explicit so such providers can select their actual callback owner. Leave existing callers on the parent-module fallback. Unregister still disconnects and drains callbacks before the provider releases its state.
--- drivers/nvmem/core.c | 2 +- include/linux/nvmem-provider.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)
drivers/nvmem/core.c +1 -1
@@ -1111,7 +1111,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) ops->reg_write = config->reg_write; nvmem->owner = config->owner; -if (!nvmem->owner && config->dev->driver) +if (!nvmem->owner && !config->owner_is_explicit && config->dev->driver) nvmem->owner = config->dev->driver->owner; nvmem->stride = config->stride ?:1 ; nvmem->word_size = config->word_size ?:1 ;
include/linux/nvmem-provider.h +3 -0
@@ -81,6 +81,8 @@ struct nvmem_cell_info {* @name: Optional name. * @id: Optional device ID used in full name. Ignored if name is NULL. * @owner: Pointer to exporter module. Used for refcounting. +* @owner_is_explicit: Use @owner even when NULL, without defaulting to the +* parent driver's module. * @cells: Optional array of pre-defined NVMEM cells. * @ncells: Number of elements in cells. * @add_legacy_fixed_of_cells: Read fixed NVMEM cells from old OF syntax.
@@ -116,6 +118,7 @@ struct nvmem_config {const char *name;int id;struct module *owner; +bool owner_is_explicit;const struct nvmem_cell_info *cells;int ncells;bool add_legacy_fixed_of_cells;
Signature · 1 line
2.53.0
Re: (subset) [PATCH v3 00/11] nvmem: complete provider and layout disconnection
In reply to James HilliardQuoted 6 lines · James Hilliard
NVMEM unregister already disconnects register callbacks with SRCU. Some accesses extend beyond those callbacks: keepout traversal, cell post-processing, DT fixups and device matching can still use provider state. Layout unbind also leaves its cells and their callbacks available to retained consumers.
Quoted 8 lines
Extend the existing drain to those accesses, and retire layout-owned cells independently of provider cells. Old handles retain core-owned metadata and reject further access. Explicit cell references and sysfs group ownership make layout removal and rebind independent of existing consumers. Release the write-protect GPIO when callbacks are drained so a retained consumer does not prevent provider reprobe.
[...]
Applied, thanks!
[01/11] nvmem: retain the provider node until device release
commit: e5cb8ebd4c5387c5d7eddefbbd89e98efa359878
[02/11] nvmem: drain keepout and cell post-processing accesses
commit: e770f469df98a02f10e97c2ebe6abf93fec460f7
[03/11] nvmem: exclude disconnected providers from callback admission
commit: 4508485e8156afd4c9d901f6192d4fe13867e698
[04/11] nvmem: release write protection when the provider disconnects
commit: 189dc217748ae8ad4198bed150d94aa82a44fa6cBest regards,
Signature · 1 line
Srinivas Kandagatla <[email protected]>