From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
To: Srinivas Kandagatla <srini@kernel.org>,
Bartosz Golaszewski <brgl@kernel.org>,
Johan Hovold <johan@kernel.org>
Cc: linux-kernel@vger.kernel.org, brgl@kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Subject: [PATCH v3 8/9] nvmem: remove duplicated reference counting
Date: Wed, 29 Apr 2026 17:43:23 +0200 [thread overview]
Message-ID: <20260429-nvmem-unbind-v3-8-2a694f95395b@oss.qualcomm.com> (raw)
In-Reply-To: <20260429-nvmem-unbind-v3-0-2a694f95395b@oss.qualcomm.com>
Commit c1de7f43bd84 ("nvmem: use kref") introduced reference counting
with kref to an already reference counted nvmem_device structure. We
only need one refcount so use the one provded by device's kobject and
drop the kref field from struct nvmem_device.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/core.c | 77 +++++++++++++++++++----------------------------
drivers/nvmem/internals.h | 1 -
2 files changed, 31 insertions(+), 47 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 32fe048a4a9152b7a5ec465ae5bbf04e30319554..590d880ede944670c09dd62b394626a959e5cdd1 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -529,24 +529,6 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
#endif /* CONFIG_NVMEM_SYSFS */
-static void nvmem_release(struct device *dev)
-{
- struct nvmem_device *nvmem = to_nvmem_device(dev);
-
- ida_free(&nvmem_ida, nvmem->id);
- gpiod_put(nvmem->wp_gpio);
- kfree(nvmem->ops);
- kfree(nvmem);
-}
-
-static const struct device_type nvmem_provider_type = {
- .release = nvmem_release,
-};
-
-static const struct bus_type nvmem_bus_type = {
- .name = "nvmem",
-};
-
static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
{
blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
@@ -565,6 +547,25 @@ static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
nvmem_cell_entry_drop(cell);
}
+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);
+ kfree(nvmem->ops);
+ kfree(nvmem);
+}
+
+static const struct device_type nvmem_provider_type = {
+ .release = nvmem_release,
+};
+
+static const struct bus_type nvmem_bus_type = {
+ .name = "nvmem",
+};
+
static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
{
scoped_guard(mutex, &nvmem_mutex)
@@ -940,7 +941,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
goto err_put_device;
}
- kref_init(&nvmem->refcnt);
INIT_LIST_HEAD(&nvmem->cells);
nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
@@ -1003,24 +1003,24 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
if (config->cells) {
rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
if (rval)
- goto err_remove_cells;
+ goto err_remove_compat;
}
if (config->add_legacy_fixed_of_cells) {
rval = nvmem_add_cells_from_legacy_of(nvmem);
if (rval)
- goto err_remove_cells;
+ goto err_remove_compat;
}
rval = nvmem_add_cells_from_fixed_layout(nvmem);
if (rval)
- goto err_remove_cells;
+ goto err_remove_compat;
dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
rval = device_add(&nvmem->dev);
if (rval)
- goto err_remove_cells;
+ goto err_remove_compat;
rval = nvmem_populate_layout(nvmem);
if (rval)
@@ -1042,8 +1042,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
#endif
err_remove_dev:
device_del(&nvmem->dev);
-err_remove_cells:
- nvmem_device_remove_all_cells(nvmem);
+err_remove_compat:
nvmem_sysfs_remove_compat(nvmem);
err_put_device:
put_device(&nvmem->dev);
@@ -1052,21 +1051,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
}
EXPORT_SYMBOL_GPL(nvmem_register);
-static void nvmem_device_release(struct kref *kref)
-{
- struct nvmem_device *nvmem;
-
- nvmem = container_of(kref, struct nvmem_device, refcnt);
-
- blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
-
- nvmem_sysfs_remove_compat(nvmem);
-
- nvmem_device_remove_all_cells(nvmem);
- nvmem_destroy_layout(nvmem);
- device_unregister(&nvmem->dev);
-}
-
/**
* nvmem_unregister() - Unregister previously registered nvmem device
*
@@ -1074,8 +1058,12 @@ static void nvmem_device_release(struct kref *kref)
*/
void nvmem_unregister(struct nvmem_device *nvmem)
{
- if (nvmem)
- kref_put(&nvmem->refcnt, nvmem_device_release);
+ blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
+
+ nvmem_sysfs_remove_compat(nvmem);
+ nvmem_destroy_layout(nvmem);
+
+ device_unregister(&nvmem->dev);
}
EXPORT_SYMBOL_GPL(nvmem_unregister);
@@ -1136,8 +1124,6 @@ static struct nvmem_device *nvmem_device_match(void *data,
return ERR_PTR(-EINVAL);
}
- kref_get(&nvmem->refcnt);
-
return nvmem;
}
@@ -1253,9 +1239,8 @@ EXPORT_SYMBOL_GPL(devm_nvmem_device_put);
*/
void nvmem_device_put(struct nvmem_device *nvmem)
{
- put_device(&nvmem->dev);
module_put(nvmem->owner);
- kref_put(&nvmem->refcnt, nvmem_device_release);
+ put_device(&nvmem->dev);
}
EXPORT_SYMBOL_GPL(nvmem_device_put);
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 070eef89ffca78fe033aaaada0f91c257fe2f166..60daa79c404dc915872481bd3b02de2258d33406 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -19,7 +19,6 @@ struct nvmem_device {
int stride;
int word_size;
int id;
- struct kref refcnt;
size_t size;
bool read_only;
bool root_only;
--
2.47.3
next prev parent reply other threads:[~2026-04-29 15:43 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-29 15:43 [PATCH v3 0/9] nvmem: rework nvmem core and allow unbinding with active consumers Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 1/9] nvmem: remove unused field from struct nvmem_device Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 2/9] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 3/9] nvmem: check the return value of gpiod_set_value_cansleep() Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 4/9] nvmem: simplify locking with guard() Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 5/9] nvmem: remove unneeded __nvmem_device_put() Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 6/9] nvmem: split out the reg_read/write() callbacks out of struct nvmem_device Bartosz Golaszewski
2026-04-29 15:43 ` [PATCH v3 7/9] nvmem: simplify nvmem_sysfs_remove_compat() Bartosz Golaszewski
2026-04-29 15:43 ` Bartosz Golaszewski [this message]
2026-04-29 15:43 ` [PATCH v3 9/9] nvmem: protect nvmem_device::ops with SRCU Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260429-nvmem-unbind-v3-8-2a694f95395b@oss.qualcomm.com \
--to=bartosz.golaszewski@oss.qualcomm.com \
--cc=brgl@kernel.org \
--cc=johan@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=srini@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®