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=1796167680; b=BitPDDOWJIxRvrWNh9fIE2DDthGS2vidlabsnOq05y2xb96H0JcvfKr9Y7AazND/fwUh 6biSDAD2QL4P8/5LgOlIZ8jjImejli9OqBN7YYFNrz2GQdjSDnpTIhsQqDMqvRBmxGJ47 3P2TENyjnSq13DoPCLUcP0Ew+4JRQp/LXtPh2yyRxU4XC0KAqk89Uozna+2NqFNknqhUz DG0Ulq0so5AWejDB2+a+gx4Irln8QVePwYuOCPeuQ94eT6QeVR/mN9n2YvZGJzWkaLFSn 8a5poSLAZh2G306iOPq/3PIu5efwJIony2lrdfEWzN5lbvX9GR+mBHwBOQDyaqxBf8w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796167680; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=/nh3lvZqJlwb7ivaTUOFErsB7ThH004/nYus52lOCQ4=; b=LMV/zkr/VYK9+c2plWeSHGCvSkpe8zA8QwG/qh6PBFUQwgPDIklYe/cH7rguL9U/802W NaZ8wElLAhrcCkoVPCFr9goSG55Bi15poTlu7zuloY5RV8H9uSc79TMg7F32n0Ykli0/2 DAN2yjnfv8SOYBYBInT2uzSmomxrmZMlOX+t2VEm1MeDhyp6Wb0JicBhzGsJTK1izUo7k ye3WWTIJui931K+nIhtPxjBJsMPxpedGpiQ2jTsyw0I3dtZJ+lNwmdQHvLM274ak45cAr Lj4VMvI7NwmNDt7BAEuJ7AfkScbqIJKDwhn/Hw7+SifD2rSjsziLdhylMmqgnbkjPYw== 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=AsMhNzKH; 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 ACE0C606A684 for ; Tue, 1 Dec 2026 23:28:00 +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=AsMhNzKH Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67A2A1F0092C; Tue, 1 Dec 2026 23:27:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796167680; bh=/nh3lvZqJlwb7ivaTUOFErsB7ThH004/nYus52lOCQ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=AsMhNzKHsCNE+8nAOz2pQA3cmoVJR9/8TMBdFP6zwKj7Kk3ZfUhduc0P1NFb4stgH 0215WKhpo1zGkiJYhbmmEcId8O33+eJu/72tJBnAhv1g1ltbtmWW+pyfSHPIolI75R zAGRdCgBE8Df8mS2+iyecr0SET3GlnOBuF3TnSQP+zvTScHOnLXWVoksSbHViGc4yV 9RepI4d2U2o2T1ncedB0cYxJwYo0W+BOzPUhFTtaqcS8VoBB7CtCQdjOpZLQ5ZBk3P jUQSrZwSpEd6UBbxhdrTbtR6KB1s3YPO2u+ZsOlly3a0IwZmpDnNYjPpPz2gc42IiC UoIOlG7obNDPQ== From: srini@kernel.org To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Bartosz Golaszewski , Srinivas Kandagatla Subject: [PATCH 6/8] nvmem: don't use devres in subsystem code Date: Tue, 1 Dec 2026 23:27:34 +0000 Message-ID: <20261201232736.537847-7-srini@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201232736.537847-1-srini@kernel.org> References: <20261201232736.537847-1-srini@kernel.org> X-Mailing-List: linux-kernel@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: Bartosz Golaszewski Using device-managed APIs at sub-system level can cause issues due to lack of a strict API contract, especially when relying on managed resources being freed on device release. Devres should only be used in drivers, ideally only when it's bound to a driver. In nvmem, the release of the memory allocated for sysfs attributes, happens when the last reference to nvmem->dev is dropped, not in nvmem_unregister(). This means that consumer holding references to nvmem devices keep the memory around after sysfs files are gone which happens in device_del(). The rest of nvmem/core.c manages resources explicitly, while using devres in sysfs registration hides the details of what goes on and is inconsistent. Switch to non-managed interfaces exclusively in nvmem core. Signed-off-by: Bartosz Golaszewski Signed-off-by: Srinivas Kandagatla --- drivers/nvmem/core.c | 60 ++++++++++++++++++++++++++++----------- drivers/nvmem/internals.h | 6 ++++ 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index d976c0d6603b..afe9d3d66914 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -478,12 +478,7 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem) static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem) { - 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; @@ -494,43 +489,72 @@ static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem) /* Allocate an array of attributes with a sentinel */ ncells = list_count_nodes(&nvmem->cells); - pattrs = devm_kzalloc_objs(&nvmem->dev, *pattrs, ncells + 1); + + const struct bin_attribute **pattrs __free(kfree) = kzalloc_objs(*pattrs, ncells + 1); if (!pattrs) return -ENOMEM; - attrs = devm_kzalloc_objs(&nvmem->dev, struct bin_attribute, ncells); + struct bin_attribute *attrs __free(kfree) = kzalloc_objs(*attrs, ncells); if (!attrs) return -ENOMEM; /* 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.name = kasprintf(GFP_KERNEL, "%s@%x,%x", entry->name, + entry->offset, entry->bit_offset); + if (!attrs[i].attr.name) { + list_for_each_entry_continue_reverse(entry, &nvmem->cells, node) + kfree(attrs[--i].attr.name); + return -ENOMEM; + } + 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; pattrs[i] = &attrs[i]; i++; } - group.bin_attrs = pattrs; + memset(&nvmem->sysfs_group, 0, sizeof(nvmem->sysfs_group)); + nvmem->sysfs_group.name = "cells"; + nvmem->sysfs_group.bin_attrs = pattrs; - ret = device_add_group(&nvmem->dev, &group); - if (ret) + ret = device_add_group(&nvmem->dev, &nvmem->sysfs_group); + if (ret) { + i = 0; + list_for_each_entry(entry, &nvmem->cells, node) + kfree(attrs[i++].attr.name); return ret; + } nvmem->sysfs_cells_populated = true; + nvmem->sysfs_attrs = no_free_ptr(attrs); + nvmem->sysfs_pattrs = no_free_ptr(pattrs); return ret; } +static void nvmem_sysfs_remove_cells(struct nvmem_device *nvmem) +{ + int i; + + if (!nvmem->sysfs_cells_populated) + return; + + device_remove_group(&nvmem->dev, &nvmem->sysfs_group); + + for (i = 0; nvmem->sysfs_pattrs[i]; i++) + kfree(nvmem->sysfs_pattrs[i]->attr.name); + + kfree(nvmem->sysfs_attrs); + kfree(nvmem->sysfs_pattrs); + + nvmem->sysfs_cells_populated = false; +} + #else /* CONFIG_NVMEM_SYSFS */ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, @@ -541,6 +565,9 @@ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem) { } +static void nvmem_sysfs_remove_cells(struct nvmem_device *nvmem) +{ +} #endif /* CONFIG_NVMEM_SYSFS */ @@ -1085,6 +1112,7 @@ void nvmem_unregister(struct nvmem_device *nvmem) nvmem_sysfs_remove_compat(nvmem); nvmem_destroy_layout(nvmem); + nvmem_sysfs_remove_cells(nvmem); kfree(ops); device_unregister(&nvmem->dev); diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h index bc7a99f5aefb..b461ec6a0e86 100644 --- a/drivers/nvmem/internals.h +++ b/drivers/nvmem/internals.h @@ -7,6 +7,7 @@ #include #include #include +#include /* Hold pointers to callbacks owned by the nvmem provider module. */ struct nvmem_operations { @@ -37,7 +38,12 @@ struct nvmem_device { struct nvmem_layout *layout; struct nvmem_operations __rcu *ops; void *priv; +#if IS_ENABLED(CONFIG_NVMEM_SYSFS) bool sysfs_cells_populated; + struct attribute_group sysfs_group; + struct bin_attribute *sysfs_attrs; + const struct bin_attribute **sysfs_pattrs; +#endif /* CONFIG_NVMEM_SYSFS */ }; int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np); -- 2.53.0