Message ID | E1pCiUe-00457v-SQ@rmk-PC.armlinux.org.uk |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4660221wrt; Tue, 3 Jan 2023 06:56:04 -0800 (PST) X-Google-Smtp-Source: AMrXdXsWV2H2BsVKTRw0RrRIcfpwxup0zk3ax68yIaScOei1maNZ8XIWCNwwkjzQdoYBnpop/9AH X-Received: by 2002:a17:902:7207:b0:189:6457:4e14 with SMTP id ba7-20020a170902720700b0018964574e14mr45046688plb.8.1672757763703; Tue, 03 Jan 2023 06:56:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672757763; cv=none; d=google.com; s=arc-20160816; b=VpmzwQFXfcbRUaVmqe6LWfdjEb5DBhvbLbnWwHmEOcNtcOFmsrT0GlMCxE1dbdafKF wVltwinowEI2kk9NTfljY5Zayf8luYCOx06+MnvXEwhNqe3VbdMykrD6+K9KjbgJ3Hdk 71ZYOKTOkeqZt3//n0calebHjYzNA8IvfKc1FePgZq0VdmcQ3GTEiQF6Mv93As517z+7 1QAky2nf1sFc2k2EXOAsNWr1LxhoYxcCdGZBoNnvA4CEMNFl/43ucpn/J9qON+TtHOQz jZv5G98csIbdFK4adV/8JWNJlWudl07zxMbAV2TMg8UV9Rb7hE4+ZVf0JtSIlIoklIAb 8hWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:date:sender:message-id:content-transfer-encoding :content-disposition:mime-version:subject:cc:to:from:dkim-signature; bh=Dw5ZgJSaRLkDv4nIzFjapZY+fSw9bSxUXN0tVxPPIDY=; b=weAxtUownGXi4ggvqfns/BVp12r70oLZZUBTSZvap/YrQKLGDNBa11VEwWGqOPdX8w umhk7rtvQRnGoUrXUnnVOFOgL/EeKs9dK5InT8Z6ukotMDpSSmnIjXBnTsp8CO6RN8VW Y1MNiATr+STWNNDqpXqiZs4n9Xijee4is/srlbyW9/IxDOkvUuom561ew8o+K5oa9Is8 /IwJQZTZDqfWOh2V97oPv0Tw8C8qyOJ0WebprcW0vmJdZgvzfQC26IG6kWQg2vFO07fc oac2CLJUhlNsKEZTpyNxsxWWOkpMVhH9P10GNGUz1fcP/EvRXHIGXtTjeGpS5U1ioUHC NoCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=aouNauwu; 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=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x8-20020a654548000000b0049d7f3cc5b9si16343596pgr.335.2023.01.03.06.55.51; Tue, 03 Jan 2023 06:56:03 -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=fail (test mode) header.i=@armlinux.org.uk header.s=pandora-2019 header.b=aouNauwu; 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=fail (p=NONE sp=NONE dis=NONE) header.from=armlinux.org.uk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233417AbjACOmr (ORCPT <rfc822;tmhikaru@gmail.com> + 99 others); Tue, 3 Jan 2023 09:42:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237890AbjACOmZ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 3 Jan 2023 09:42:25 -0500 Received: from pandora.armlinux.org.uk (pandora.armlinux.org.uk [IPv6:2001:4d48:ad52:32c8:5054:ff:fe00:142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14DEBE0AA for <linux-kernel@vger.kernel.org>; Tue, 3 Jan 2023 06:42:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=armlinux.org.uk; s=pandora-2019; h=Date:Sender:Message-Id:Content-Type: Content-Transfer-Encoding:MIME-Version:Subject:Cc:To:From:Reply-To:Content-ID :Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To: Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Dw5ZgJSaRLkDv4nIzFjapZY+fSw9bSxUXN0tVxPPIDY=; b=aouNauwujUkFDlJmZZD20JzSoD siVGt+ePb/Oimb5sXnNpgyecNqCDeYdOx8MwoKE0wLcFXoBtKU8/Dc5VDDTZj7HrqM2EuBeOqw56V gbnvFaYFpWzK2TceB3oad//AEoX1EBy3P4OxI+e0lwPohsX3y4u5bT7gDfoEheCRD4PCKDi0RmWPW XZrVCHfWLxQRSv5kT3IKy3r1mlmKXtMfCrEeb4YULOHEGCYYffQogDriiCVaEssIEEc3da/R9HWZu 0lTCy5SR0qpVAFCa0RnnCegiA/MO0z/8yCu9/TC6AOGR6wNeFoY7MYGeIVGj7TBANhXaS5vKbC+xe sKlu2HzA==; Received: from e0022681537dd.dyn.armlinux.org.uk ([fd8f:7570:feb6:1:222:68ff:fe15:37dd]:34488 helo=rmk-PC.armlinux.org.uk) by pandora.armlinux.org.uk with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from <rmk@armlinux.org.uk>) id 1pCiUf-0005R4-RZ; Tue, 03 Jan 2023 14:42:21 +0000 Received: from rmk by rmk-PC.armlinux.org.uk with local (Exim 4.94.2) (envelope-from <rmk@rmk-PC.armlinux.org.uk>) id 1pCiUe-00457v-SQ; Tue, 03 Jan 2023 14:42:20 +0000 From: "Russell King (Oracle)" <rmk+kernel@armlinux.org.uk> To: Srinivas Kandagatla <srinivas.kandagatla@linaro.org> Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Maxime Ripard <mripard@kernel.org>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Hector Martin <marcan@marcan.st> Subject: [PATCH v2] nvmem: fix registration vs use race MIME-Version: 1.0 Content-Disposition: inline Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" Message-Id: <E1pCiUe-00457v-SQ@rmk-PC.armlinux.org.uk> Sender: Russell King <rmk@armlinux.org.uk> Date: Tue, 03 Jan 2023 14:42:20 +0000 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_NONE 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?1754013644822638453?= X-GMAIL-MSGID: =?utf-8?q?1754013644822638453?= |
Series |
[v2] nvmem: fix registration vs use race
|
|
Commit Message
Russell King (Oracle)
Jan. 3, 2023, 2:42 p.m. UTC
The i.MX6 CPU frequency driver sometimes fails to register at boot time
due to nvmem_cell_read_u32() sporadically returning -ENOENT.
This happens because there is a window where __nvmem_device_get() in
of_nvmem_cell_get() is able to return the nvmem device, but as cells
have been setup, nvmem_find_cell_entry_by_node() returns NULL.
The occurs because the nvmem core registration code violates one of the
fundamental principles of kernel programming: do not publish data
structures before their setup is complete.
Fix this by making nvmem core code conform with this principle.
Fixes: eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers")
Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
---
v2: add fixes tag
drivers/nvmem/core.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
Comments
On 03/01/2023 23.42, Russell King (Oracle) wrote: > The i.MX6 CPU frequency driver sometimes fails to register at boot time > due to nvmem_cell_read_u32() sporadically returning -ENOENT. > > This happens because there is a window where __nvmem_device_get() in > of_nvmem_cell_get() is able to return the nvmem device, but as cells > have been setup, nvmem_find_cell_entry_by_node() returns NULL. > > The occurs because the nvmem core registration code violates one of the > fundamental principles of kernel programming: do not publish data > structures before their setup is complete. > > Fix this by making nvmem core code conform with this principle. > > Fixes: eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers") > Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk> > --- > v2: add fixes tag > > 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..6b89fb6fa582 100644 > --- a/drivers/nvmem/core.c > +++ b/drivers/nvmem/core.c > @@ -835,22 +835,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > nvmem->dev.groups = nvmem_dev_groups; > #endif > > - 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; > + goto err_put_device; You can't call put_device() on a device that hasn't gone through device_initialize() yet. > } > > if (config->compat) { > rval = nvmem_sysfs_setup_compat(nvmem, config); > if (rval) > - goto err_device_del; > + goto err_put_device; Ditto. > } > > if (config->cells) { > @@ -867,6 +861,12 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > if (rval) > goto err_remove_cells; > > + dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); > + > + rval = device_register(&nvmem->dev); > + if (rval) > + goto err_remove_cells; > + > blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); > > return nvmem; > @@ -876,8 +876,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > 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); > - Hector
On Wed, Jan 04, 2023 at 12:20:15AM +0900, Hector Martin wrote: > On 03/01/2023 23.42, Russell King (Oracle) wrote: > > The i.MX6 CPU frequency driver sometimes fails to register at boot time > > due to nvmem_cell_read_u32() sporadically returning -ENOENT. > > > > This happens because there is a window where __nvmem_device_get() in > > of_nvmem_cell_get() is able to return the nvmem device, but as cells > > have been setup, nvmem_find_cell_entry_by_node() returns NULL. > > > > The occurs because the nvmem core registration code violates one of the > > fundamental principles of kernel programming: do not publish data > > structures before their setup is complete. > > > > Fix this by making nvmem core code conform with this principle. > > > > Fixes: eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers") > > Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk> > > --- > > v2: add fixes tag > > > > 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..6b89fb6fa582 100644 > > --- a/drivers/nvmem/core.c > > +++ b/drivers/nvmem/core.c > > @@ -835,22 +835,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) > > nvmem->dev.groups = nvmem_dev_groups; > > #endif > > > > - 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; > > + goto err_put_device; > > You can't call put_device() on a device that hasn't gone through > device_initialize() yet. Right, which is what I just realised while writing the previous reply. We need to use device_initialize() and device_add(), so we can call put_device() on it. Error paths are difficult to properly test. :(
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 321d7d63e068..6b89fb6fa582 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -835,22 +835,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) nvmem->dev.groups = nvmem_dev_groups; #endif - 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; + goto err_put_device; } if (config->compat) { rval = nvmem_sysfs_setup_compat(nvmem, config); if (rval) - goto err_device_del; + goto err_put_device; } if (config->cells) { @@ -867,6 +861,12 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_remove_cells; + dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); + + rval = device_register(&nvmem->dev); + if (rval) + goto err_remove_cells; + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); return nvmem; @@ -876,8 +876,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) 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);