From patchwork Tue Dec 17 18:11:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rob Herring (Arm)" X-Patchwork-Id: 13912386 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 69076E7717F for ; Tue, 17 Dec 2024 18:14:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=+t8rVtjAi1IFxV4C7otumnejZNAjS74Ra9Tp0jk2qvI=; b=vhqsa8xxZTKNz7l6Ro5y1/JTis c7URnOONvAzLvHVSRHKfbgEDs0flIKRMSzR/kqJ6yhEhn2YaZs5JxjMZdjDeNw0i75g0Op2mZ0jxU kxaa8m0rqWWlEHVawNsLroE7PGPYAchNVWfmBFe3C2zeI8HBtkZuv62AP0po6HMntxV8wP6MIFT0z ud8X/CW4vyMf3oA3vFXQ0qylim3jORj4NgZvtUVH7C4JQg/zUMdjR3GJg96bgWc3nOmCDiq0V93mg AWHEbdmeonaGRTjlgnilbZ04/NCjQbtzDTCuv82VrkrE2fa80VUJKWg6GHfbxadxTTxXAk+VV1vMw hDqY7JXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tNc5D-0000000EUpx-2txt; Tue, 17 Dec 2024 18:14:11 +0000 Received: from nyc.source.kernel.org ([147.75.193.91]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tNc34-0000000EUSu-36E7 for linux-arm-kernel@lists.infradead.org; Tue, 17 Dec 2024 18:12:00 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 47C91A41F38; Tue, 17 Dec 2024 18:10:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88018C4CED3; Tue, 17 Dec 2024 18:11:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734459117; bh=CEymn1dapn9L+g1VujOTt+GizasEFssPjK9PHGiZzV0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rW5mzLpRUZmMPMfwrPOLSvNWDY0Y8MLCq63tgBR5e1U5vZ+ooFeHLat8qsB87q37v +vWDOTSiBkV1hvsM+Nq+Q768NomnFyiVhKpsruu0syZ2QdbqYeq7YhZq/10Mg6RPrJ 0N/YRxCrVtbGtSFOL1/bwVJAy5u/Mv5i05KViZCEeZC3UP9DJWFFhwdarFs7XLZoM1 kS6GDIyjknB1EEZMOW2pGQLGqCJ3EPIZ+bI+8o9pe7M+S80cfSbeHarlAERpWVgDvG A2m5s0LB9VjgUtBnn4jZzCM8pWD8dKJD2xTKXy1d05YA9wb2ly9PhEH2QU7r/AkmF5 K84vafo7doEBg== From: "Rob Herring (Arm)" Date: Tue, 17 Dec 2024 12:11:40 -0600 Subject: [PATCH v2 1/3] mfd: syscon: Fix race in device_node_get_regmap() MIME-Version: 1.0 Message-Id: <20241217-syscon-fixes-v2-1-4f56d750541d@kernel.org> References: <20241217-syscon-fixes-v2-0-4f56d750541d@kernel.org> In-Reply-To: <20241217-syscon-fixes-v2-0-4f56d750541d@kernel.org> To: Lee Jones , Arnd Bergmann , Pankaj Dubey , Heiko Stuebner , Liviu Dudau , Sudeep Holla , Lorenzo Pieralisi Cc: Peter Griffin , Will McVicker , John Madieu , Krzysztof Kozlowski , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org X-Mailer: b4 0.15-dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241217_101158_914356_3D4BF47D X-CRM114-Status: GOOD ( 14.55 ) 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 It is possible for multiple, simultaneous callers calling device_node_get_regmap() with the same node to fail to find an entry in the syscon_list. There is a period of time while the first caller is calling of_syscon_register() that subsequent callers also fail to find an entry in the syscon_list and then call of_syscon_register() a second time. Fix this by keeping the lock held until after of_syscon_register() completes and adds the node to syscon_list. Convert the spinlock to a mutex as many of the functions called in of_syscon_register() such as kzalloc() and of_clk_get() may sleep. Fixes: bdb0066df96e ("mfd: syscon: Decouple syscon interface from platform devices") Reviewed-by: Krzysztof Kozlowski Tested-by: Krzysztof Kozlowski Signed-off-by: Rob Herring (Arm) Tested-by: Will McVicker --- drivers/mfd/syscon.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/drivers/mfd/syscon.c b/drivers/mfd/syscon.c index 3e1d699ba9340f8135dfdeae6feca474980cc48d..72f20de9652da2d7bad12e4bc2c43ac0c9a97f76 100644 --- a/drivers/mfd/syscon.c +++ b/drivers/mfd/syscon.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -27,7 +28,7 @@ static struct platform_driver syscon_driver; -static DEFINE_SPINLOCK(syscon_list_slock); +static DEFINE_MUTEX(syscon_list_lock); static LIST_HEAD(syscon_list); struct syscon { @@ -54,6 +55,8 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_res) struct resource res; struct reset_control *reset; + WARN_ON(!mutex_is_locked(&syscon_list_lock)); + struct syscon *syscon __free(kfree) = kzalloc(sizeof(*syscon), GFP_KERNEL); if (!syscon) return ERR_PTR(-ENOMEM); @@ -146,9 +149,7 @@ static struct syscon *of_syscon_register(struct device_node *np, bool check_res) syscon->regmap = regmap; syscon->np = np; - spin_lock(&syscon_list_slock); list_add_tail(&syscon->list, &syscon_list); - spin_unlock(&syscon_list_slock); return_ptr(syscon); @@ -169,7 +170,7 @@ static struct regmap *device_node_get_regmap(struct device_node *np, { struct syscon *entry, *syscon = NULL; - spin_lock(&syscon_list_slock); + mutex_lock(&syscon_list_lock); list_for_each_entry(entry, &syscon_list, list) if (entry->np == np) { @@ -177,11 +178,11 @@ static struct regmap *device_node_get_regmap(struct device_node *np, break; } - spin_unlock(&syscon_list_slock); - if (!syscon) syscon = of_syscon_register(np, check_res); + mutex_unlock(&syscon_list_lock); + if (IS_ERR(syscon)) return ERR_CAST(syscon); @@ -212,7 +213,7 @@ int of_syscon_register_regmap(struct device_node *np, struct regmap *regmap) return -ENOMEM; /* check if syscon entry already exists */ - spin_lock(&syscon_list_slock); + mutex_lock(&syscon_list_lock); list_for_each_entry(entry, &syscon_list, list) if (entry->np == np) { @@ -225,12 +226,12 @@ int of_syscon_register_regmap(struct device_node *np, struct regmap *regmap) /* register the regmap in syscon list */ list_add_tail(&syscon->list, &syscon_list); - spin_unlock(&syscon_list_slock); + mutex_unlock(&syscon_list_lock); return 0; err_unlock: - spin_unlock(&syscon_list_slock); + mutex_unlock(&syscon_list_lock); kfree(syscon); return ret; }