From patchwork Wed Dec 11 20:57:12 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: 13904198 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 4C3B1E7717D for ; Wed, 11 Dec 2024 20:59:53 +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=uNxSgfoUi8tDW+hq+LeNi/x382IgwL+KHg1Qj4RYtoE=; b=dD6a6v2rHTNeqFM4sOnI2c6u8X YOSYk1/XEzPENyZYcw2588IJ7wx6llzKDa9WXFmeDGQLInyCkj9Nor3OlhdBU9Tc5R6mzyXxhItbj IoZYsagTuhSjJsKbfpc9KisAcdjp8HOAHPeDiFXH9HgsQffKfkvInplNmgZE7Li1W5E32afDMjyg/ sQ/+5uGH6nod+pzk3J54tOkKdxQAz0g15/NbWBdHqt1Jf9qhvzXQL7jeIfHSeok/eur/5VhoDbhF9 6dEcdCCQ1uxGMUl3VJmxEJkVi+jzaKP1ic8b3tNNa6KFwp9d6ngOLmm80c9snGoDH0JylQS2ttEpK ol2hPAzw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tLTo5-0000000G64q-24cm; Wed, 11 Dec 2024 20:59:41 +0000 Received: from nyc.source.kernel.org ([2604:1380:45d1:ec00::3]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tLTm0-0000000G5Tl-06ts for linux-arm-kernel@lists.infradead.org; Wed, 11 Dec 2024 20:57:33 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id DAFACA41BAE; Wed, 11 Dec 2024 20:55:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C643CC4CEDD; Wed, 11 Dec 2024 20:57:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733950650; bh=XvZ70etxbl7a+HEq8W/PmORE99I680EgH7X9zOxKkeU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=FhjcpX904qnBERVAcvLCNepto31hdwld4EYeU1UemcH4x0AkLC9lvG+vQO3caXnc5 e8QlKXo25UKC+eYBrRzv/M5XJ47aws1MUKuu/30z+RTh1n9g19S6C/ZmCSJl/csmAu yKnsVsCOYVpFZItt///kQdR/aNJIeUClZX9WVVsjMvbRn2n44OH2fNZfcCTNnCDMkq gNcZOOLNB5FcPebsjx2tuQqU0AWcn9nQlzxFL+LiItqfDnwR5hxLsaUDQU1rtnrXVe NdzNwgTcGEZn5BqsvTMJKHbWhnRu0+NA7UZEhX/wwh5o60dr/OHf24MsN0AOjt4XCP dq9WQtAydnghg== From: "Rob Herring (Arm)" Date: Wed, 11 Dec 2024 14:57:12 -0600 Subject: [PATCH 1/3] mfd: syscon: Fix race in device_node_get_regmap() MIME-Version: 1.0 Message-Id: <20241211-syscon-fixes-v1-1-b5ac8c219e96@kernel.org> References: <20241211-syscon-fixes-v1-0-b5ac8c219e96@kernel.org> In-Reply-To: <20241211-syscon-fixes-v1-0-b5ac8c219e96@kernel.org> To: Lee Jones , Arnd Bergmann , Pankaj Dubey , Heiko Stuebner , Liviu Dudau , Sudeep Holla , Lorenzo Pieralisi Cc: Peter Griffin , Will McVicker , 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-20241211_125732_196142_9F7FAE89 X-CRM114-Status: GOOD ( 14.33 ) 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() may sleep. Fixes: bdb0066df96e ("mfd: syscon: Decouple syscon interface from platform devices") Signed-off-by: Rob Herring (Arm) --- 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 3e1d699ba934..72f20de9652d 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; }