From nobody Wed Dec 17 03:26:11 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BA56235C32 for ; Wed, 11 Dec 2024 20:57:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733950650; cv=none; b=TzckOKnbHuFT4h9ZEOTp/8Cue7X2c23dn8u7FDRmXaJ9edXNB5hiayLT+DObi77oQMfVj4MfeGce9O1h1HNZQGuiYrtUxL+bDahHYGC8MLAqfKOYBKxYV+pPygOiHmZkyRuDBx3PpW17sK9QkJG+57i8ye+z0fImxAhsNJCuFZg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733950650; c=relaxed/simple; bh=XvZ70etxbl7a+HEq8W/PmORE99I680EgH7X9zOxKkeU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Uvnw9vZnIBN5Zi8jQiUgMmoHt4JFQGMXuxX+5zaIW43/hu93nszoQ3NQ5s2zb2Tit6qkl/K9poyqT0i0sojZd+kEwAg3YaTmlLcT6Iipgdwzpss2j+/jeHF9eyCWkh4WPY1ihOUVMgJmOKNtEIH9Jk5URKPSFCMHJBuXk9xJnPk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FhjcpX90; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FhjcpX90" 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() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 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) Reviewed-by: Krzysztof Kozlowski Tested-by: Krzysztof Kozlowski --- 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 @@ =20 static struct platform_driver syscon_driver; =20 -static DEFINE_SPINLOCK(syscon_list_slock); +static DEFINE_MUTEX(syscon_list_lock); static LIST_HEAD(syscon_list); =20 struct syscon { @@ -54,6 +55,8 @@ static struct syscon *of_syscon_register(struct device_no= de *np, bool check_res) struct resource res; struct reset_control *reset; =20 + WARN_ON(!mutex_is_locked(&syscon_list_lock)); + struct syscon *syscon __free(kfree) =3D kzalloc(sizeof(*syscon), GFP_KERN= EL); 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 =3D regmap; syscon->np =3D np; =20 - spin_lock(&syscon_list_slock); list_add_tail(&syscon->list, &syscon_list); - spin_unlock(&syscon_list_slock); =20 return_ptr(syscon); =20 @@ -169,7 +170,7 @@ static struct regmap *device_node_get_regmap(struct dev= ice_node *np, { struct syscon *entry, *syscon =3D NULL; =20 - spin_lock(&syscon_list_slock); + mutex_lock(&syscon_list_lock); =20 list_for_each_entry(entry, &syscon_list, list) if (entry->np =3D=3D np) { @@ -177,11 +178,11 @@ static struct regmap *device_node_get_regmap(struct d= evice_node *np, break; } =20 - spin_unlock(&syscon_list_slock); - if (!syscon) syscon =3D of_syscon_register(np, check_res); =20 + mutex_unlock(&syscon_list_lock); + if (IS_ERR(syscon)) return ERR_CAST(syscon); =20 @@ -212,7 +213,7 @@ int of_syscon_register_regmap(struct device_node *np, s= truct regmap *regmap) return -ENOMEM; =20 /* check if syscon entry already exists */ - spin_lock(&syscon_list_slock); + mutex_lock(&syscon_list_lock); =20 list_for_each_entry(entry, &syscon_list, list) if (entry->np =3D=3D np) { @@ -225,12 +226,12 @@ int of_syscon_register_regmap(struct device_node *np,= struct regmap *regmap) =20 /* register the regmap in syscon list */ list_add_tail(&syscon->list, &syscon_list); - spin_unlock(&syscon_list_slock); + mutex_unlock(&syscon_list_lock); =20 return 0; =20 err_unlock: - spin_unlock(&syscon_list_slock); + mutex_unlock(&syscon_list_lock); kfree(syscon); return ret; } --=20 2.45.2