From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796167638; b=pnsrRKzE8ewDvUC++AJ9uyjT66PRX4Ie8JOOzi2Zu3TId1Ck+XmUnntQnbcd6fUW7VP3 yfncX4bdCF3ZTtVxaZcGjQWlVHA0L3kLp9dJE3pxVRU6/Qqx6EjDqByXRlGtcbjQ0vpbe OLZGuWIokBNbm1TrV6gDPvNr/eOOT3GWnd5Z7W86BPlXqGrPTRuvLdef8y4npLzgsDPSt ZkBTsrx5wIt9/i9RgzgQ2b07cNf5uM3Jmf/5Lzdm+yiVharFFUT/S/LStCE8gfB0oBcKj /2EGelrTK/Yn9gFtoH39vXqcsfObGeZt3Tul9DbdSADMAA3Qa/13neo8kgXBqRx3zeg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796167638; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=6Xi+47+bV69FbSjK4k0Yy2AhwTpbnn8SovTCMrmuvPo=; b=tBDq4p8N0S3ABpvcgNyA1X6AuXVW+kq/JDagRIWFO6Gw8xh6TkJ/WZd9wRD7DIjYncqJ 4P7SG1MjdNT80adLbCQf9J2+ipH2B10tyuzcgXBUAOL9/PABjQ9BMhxxygNN7lbcGm8Fd dvNRxB1czPVrPqAvPkZJzIBsGgyukLSNaqgIHCsqK1bm3trYzW7mkM5mkpwsoMz9Wb3QF /TQcJZYPE/6yCHqUeeCi0jw4M2oWzxwz60ouq9F3boQEzCR2EWynKsK17pUzV+WVcQXXw Se9yq1SLeVIPPO85lD1GKH1zhC03FrfUerf0AF2pe5MzEBxxDY/QdSHEuFJtytt+8vQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Xqxiilwm; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9085D606A689; Tue, 1 Dec 2026 23:27:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Xqxiilwm Received: by smtp.kernel.org (Postfix) with ESMTPSA id A33B51F00923; Tue, 1 Dec 2026 23:27:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796167637; bh=6Xi+47+bV69FbSjK4k0Yy2AhwTpbnn8SovTCMrmuvPo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=XqxiilwmWWTyGB6pjoYl7pWQ7lpuAYjmyvqlgNB34CXNVPRJArmbynH4HFH1FzxOc X8TyHe9RhOp4LHFv1D7q2jk/CrfcywbNXtTf/GNN91LEWEPe6rV51eyrTfanvfhfDM +pT9Tu0DoANwS9KL11d/9AlTPkArnzBeQa/ILVSnhMjQHeyTwtSMH+2bSZqY8bSTjL CirVrg8F9w5NaWrZFEherydIPlf0rV/RAEQdadq7umwl1Gc+PJmIcaBiAhsZlfZinQ JPyKNB4e7bdMB1i4xW+DfT9LAldAiEVpNYtdCh5lq6rJVMAcqD0y4tBVgz93dBDS66 DPI5yjf7UDBWg== From: srini@kernel.org To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, James Hilliard , stable@vger.kernel.org, Srinivas Kandagatla Subject: [PATCH 05/10] nvmem: exclude disconnected providers from callback admission Date: Tue, 1 Dec 2026 23:26:52 +0000 Message-ID: <20261201232657.537736-6-srini@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201232657.537736-1-srini@kernel.org> References: <20261201232657.537736-1-srini@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: James Hilliard 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") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/sashiko-outbox-219563@kernel.org/ Signed-off-by: James Hilliard Signed-off-by: Srinivas Kandagatla --- drivers/nvmem/core.c | 112 +++++++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 31 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 81d787f7050a..0bfe26ed86f9 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -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); -- 2.53.0