Message ID | 20221214142952.9372-1-marcan@marcan.st |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp260271wrn; Wed, 14 Dec 2022 06:40:25 -0800 (PST) X-Google-Smtp-Source: AA0mqf5Xy9T33vTt5r0/cNHzFU9qeQuwTg9mh/UHmvWjAD5fM39xt+gHvgXyadHD4q+4Qa9Kl9bQ X-Received: by 2002:aa7:c944:0:b0:467:75c6:4565 with SMTP id h4-20020aa7c944000000b0046775c64565mr18556928edt.9.1671028825422; Wed, 14 Dec 2022 06:40:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671028825; cv=none; d=google.com; s=arc-20160816; b=FXTlLTt8o5pa5/olCVj9S5ASfW/yq/e305xNRblYEgErpONNRfbpZc/WcnMMTST67q XQBwn7XRd/HCcOYSkxcTLIFb2xo5z7+J59W3AJrUO89pPbSdnh7mVGGFAlJTc8lQ4LuA GFz0aRFj4qfMqSLX8Z0BdByml4V+JOptRzcoTY92nRlsUV0bSZsuHUs39cGfNjI2Ipzr JLceue+10BaVxeWyyZ8taR8Y4W96NrAbk1Rdgi70JoE6oNVYTYuZTEJAGsBZ93dCu+kU Z+NKLAxeybY0WWNrV2TUEMqdk+oVYm9/iYOA2MJzXoVo6i/30apoAfuVsF9B7qqjRGKJ YHBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=hOEi70POmwghbeOz/mI+z34ez5YS1hOilop/0EXPECE=; b=d7joixBy7MT/QkbTqysvMW8F0hg/xBD27AfulhRaVz1qm8UFVl+DNl43Qshx4cMQst unEovuZmwNSfiXB+Yqi98PUH2t4f1AOguPon0V90E4omEzo/PaIizDuTvTGs494qElQf 3b8moxyW5f9IU+yhX/xvkKtE1VJd284Vb32ZCdp2Zxc/+ILiTc4lO1ahdkIE9YnyGKSD RV0vt8aJetxarjlLoVcrgz1LrUUf5TIF58Jqbb71Kkx9V9Vv3Z38ErzuZYN5XQPa2uFX Hbs8qEjjP0NR5VEDGFVt+lR3p4YKEcOaN0hDP4pscTC134tuNaL6tP4uQRUPuCcXSzWU 7GRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@marcan.st header.s=default header.b=IJBCeFCQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=marcan.st Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w16-20020a05640234d000b0046314dd20a0si15389679edc.3.2022.12.14.06.39.52; Wed, 14 Dec 2022 06:40:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@marcan.st header.s=default header.b=IJBCeFCQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=marcan.st Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238501AbiLNOaw (ORCPT <rfc822;jeantsuru.cumc.mandola@gmail.com> + 99 others); Wed, 14 Dec 2022 09:30:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238517AbiLNOar (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 14 Dec 2022 09:30:47 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7533F2252A for <linux-kernel@vger.kernel.org>; Wed, 14 Dec 2022 06:30:44 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: sendonly@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id CAFB44203C; Wed, 14 Dec 2022 14:30:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=marcan.st; s=default; t=1671028241; bh=l+loTAS240QxDlt5drk5S5kcxptCa1dPnxhdg+DTW3U=; h=From:To:Cc:Subject:Date; b=IJBCeFCQH+TjndfqhW2YKGwN35jxyrebppxh1BAD9aW1t6mE8Rlk+WsJsPvbVKc9W WE0gZdAqDHMTYTcbzTuM9MPy3ZnAggFrY0Rx9RVFnYwyM++hyM2fx4dYFk9WYBiTUL oIynwVHHTREcP2MX0cFkmNgYMvEk4sodUvLvrDzDZlCsGtYamqRD4usFnCgEWwK0O4 Vzwpg1uD9V9DWG7hkGcug+H64jkIbWiAcBt7FTiwXAPXOVa4LrAQ64L5noO5Lxt/h7 xvxOcr536nEQmKA85VB1Jwc8qb7MZFsuGpqqeoMrwxv6lkA3xzk2FS8NmCOdTMJ31T 4OifBSZLzDcSA== From: Hector Martin <marcan@marcan.st> To: Srinivas Kandagatla <srinivas.kandagatla@linaro.org> Cc: Sven Peter <sven@svenpeter.dev>, Alyssa Rosenzweig <alyssa@rosenzweig.io>, asahi@lists.linux.dev, linux-kernel@vger.kernel.org, Hector Martin <marcan@marcan.st> Subject: [PATCH] nvmem: core: Fix race in nvmem_register() Date: Wed, 14 Dec 2022 23:29:53 +0900 Message-Id: <20221214142952.9372-1-marcan@marcan.st> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752200721715143224?= X-GMAIL-MSGID: =?utf-8?q?1752200721715143224?= |
Series |
nvmem: core: Fix race in nvmem_register()
|
|
Commit Message
Hector Martin
Dec. 14, 2022, 2:29 p.m. UTC
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>
---
drivers/nvmem/core.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
--
2.35.1
Comments
On Wed, 14 Dec 2022 at 14:40, Hector Martin <marcan@marcan.st> 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> Reviewed-by: Eric Curtin <ecurtin@redhat.com> > --- > 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); > } > > if (config->compat) { > rval = nvmem_sysfs_setup_compat(nvmem, config); > if (rval) > - goto err_device_del; > + return ERR_PTR(rval); > } > > 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); > 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 > >
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 >
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); } if (config->compat) { rval = nvmem_sysfs_setup_compat(nvmem, config); if (rval) - goto err_device_del; + return ERR_PTR(rval); } 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); 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); }