From patchwork Tue Jan 3 16:59:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Russell King (Oracle)" X-Patchwork-Id: 13087859 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 430E1C4708D for ; Tue, 3 Jan 2023 18:29:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Date:Message-Id:MIME-Version:Subject:Cc :To:From:References:In-Reply-To:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=97nq5l8QOrOErPZ9+Dl11RIrcPDGc/3hGZLOeXnDyuA=; b=p1v8wST1GcGj3r UUasbWLliZ40Mc1IZ1+hgFqzc757ghZZisq41olHGnzAuHr8xEkgMxBqL+3+e6PXsPElSO1ZoQ/ee OzTcBRSq9uL5brGUtwJWX6hm6FSo79+v8MjC4N7pg764Q7BvGkNh+HKusoMdL1qKSm+gxh25ujR/F AIV0+3/DEDxuUnruycFy20LgCHHi8RJKGZPC9qX0GrGccRHjpVDg36j5kzzAF/FB9Pe+rQ45hZeMA jqM4nAkYwiNOeuoXqqcXEu6Xa9GR5RhXibs+ZASW+S0Ft8mJUuOs7aTzD19M4UJGOMtdMHTzG/y/w v/qIDSZrGx5XE7tjkl+w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pCm0X-003s2n-2c; Tue, 03 Jan 2023 18:27:30 +0000 Received: from pandora.armlinux.org.uk ([2001:4d48:ad52:32c8:5054:ff:fe00:142]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pCkdX-003B1I-Os for linux-arm-kernel@lists.infradead.org; Tue, 03 Jan 2023 16:59:53 +0000 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:References: In-Reply-To:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=iEP3KCcIVyEejeSlu7Gu5rS787gtmtbFIPKVaN+x0Fw=; b=yGhj8cyLqUStiPXDvK+FYSdLrS 4tdfWIP2WcJbFrsoAOeeBhVrgKOMKab9o0ATNWRK9ciyb9lzFOISRcmbEgqDqzmz8riba+nKBrGXF BCjlakQhprzZkbomupUCzBgim1YRRMHlQvLxlbjOKG63wAWPUWR7CMgGb8v70s+Fl0KQ3irrdYuut zgUWkPElWw4eR1vBunO66WG3fPfJTjqo3UDDEDeWS5NhaMCVu2Vhgb8BVQTRr6xA48S/98K1jnjZY e/fS+RKEFRi8qNUjS5FcrJSnlCG8daTqSg31D9Ig+ew5qZjSVFyJUGsnxxIf897Obxd/kA2pEe5f3 1kBBGKyA==; Received: from e0022681537dd.dyn.armlinux.org.uk ([fd8f:7570:feb6:1:222:68ff:fe15:37dd]:39580 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 ) id 1pCkdW-0005cj-Be; Tue, 03 Jan 2023 16:59:38 +0000 Received: from rmk by rmk-PC.armlinux.org.uk with local (Exim 4.94.2) (envelope-from ) id 1pCkdV-004hwd-Cx; Tue, 03 Jan 2023 16:59:37 +0000 In-Reply-To: References: From: "Russell King (Oracle)" To: Srinivas Kandagatla Cc: Bartosz Golaszewski , Gaosheng Cui , Greg Kroah-Hartman , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Maxime Ripard , Hector Martin Subject: [PATCH v3 5/5] nvmem: core: fix registration vs use race Bartosz Golaszewski ,Gaosheng Cui ,Greg Kroah-Hartman ,linux-arm-kernel@lists.infradead.org,linux-kernel@vger.kernel.org,Maxime Ripard , Hector Martin MIME-Version: 1.0 Content-Disposition: inline Message-Id: Date: Tue, 03 Jan 2023 16:59:37 +0000 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230103_085951_940608_3E7D4E9D X-CRM114-Status: GOOD ( 13.66 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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) --- 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 19497f555e07..082cda7ac199 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -830,22 +830,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_add(&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) { @@ -862,6 +856,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_add(&nvmem->dev); + if (rval) + goto err_remove_cells; + blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); return nvmem; @@ -871,8 +871,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);