From: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
To: Hector Martin <marcan@marcan.st>
Cc: Sven Peter <sven@svenpeter.dev>,
Alyssa Rosenzweig <alyssa@rosenzweig.io>,
asahi@lists.linux.dev, linux-kernel@vger.kernel.org,
Russell King <rmk+kernel@armlinux.org.uk>
Subject: Re: [PATCH] nvmem: core: Fix race in nvmem_register()
Date: Tue, 3 Jan 2023 11:24:11 +0000 [thread overview]
Message-ID: <6b7d1e08-e27e-3f8a-5b08-ba622f206295@linaro.org> (raw)
In-Reply-To: <20221214142952.9372-1-marcan@marcan.st>
Thanks Hector for the patch,
On 14/12/2022 14:29, Hector Martin wrote:
> nvmem_register() currently registers the device before adding the nvmem
> cells, which creates a race window where consumers may find the nvmem
> device (and not get PROBE_DEFERred), but then fail to find the cells and
> error out.
>
> Move device registration to the end of nvmem_register(), to close the
> race.
>
> Observed when the stars line up on Apple Silicon machines with the (not
> yet upstream, but trivial) spmi nvmem driver and the macsmc-rtc client:
>
> [ 0.487375] macsmc-rtc macsmc-rtc: error -ENOENT: Failed to get rtc_offset NVMEM cell
>
> Fixes: eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers")
> Signed-off-by: Hector Martin <marcan@marcan.st>
Missing Cc Stable
> ---
> drivers/nvmem/core.c | 18 ++++++++----------
> 1 file changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 321d7d63e068..d255feca5e17 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -837,20 +837,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
>
> dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
>
> - rval = device_register(&nvmem->dev);
> - if (rval)
> - goto err_put_device;
> -
> if (nvmem->nkeepout) {
> rval = nvmem_validate_keepouts(nvmem);
> if (rval)
> - goto err_device_del;
> + return ERR_PTR(rval);
you can not do this as this will leak ida and nvmem itself.
> }
>
> if (config->compat) {
> rval = nvmem_sysfs_setup_compat(nvmem, config);
> if (rval)
> - goto err_device_del;
> + return ERR_PTR(rval);
same here.
> }
>
> if (config->cells) {
> @@ -867,19 +863,21 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
> if (rval)
> goto err_remove_cells;
>
> + rval = device_register(&nvmem->dev);
> + if (rval)
> + goto err_put_device;
> +
> blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
>
> return nvmem;
>
> +err_put_device:
> + put_device(&nvmem->dev);
this will free nvmem and further down you are going to de-reference the
freed pointer.
--srini
> err_remove_cells:
> nvmem_device_remove_all_cells(nvmem);
> err_teardown_compat:
> if (config->compat)
> nvmem_sysfs_remove_compat(nvmem, config);
> -err_device_del:
> - device_del(&nvmem->dev);
> -err_put_device:
> - put_device(&nvmem->dev);
>
> return ERR_PTR(rval);
> }
> --
> 2.35.1
>
prev parent reply other threads:[~2023-01-03 11:24 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-14 14:29 Hector Martin
2022-12-14 14:43 ` Eric Curtin
2023-01-03 11:24 ` Srinivas Kandagatla [this message]
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=6b7d1e08-e27e-3f8a-5b08-ba622f206295@linaro.org \
--to=srinivas.kandagatla@linaro.org \
--cc=alyssa@rosenzweig.io \
--cc=asahi@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=marcan@marcan.st \
--cc=rmk+kernel@armlinux.org.uk \
--cc=sven@svenpeter.dev \
/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®