Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795617954; b=hP689MX+0a5q/pzSP5ZXfGh2LLwDDqCCsBQQFIMP4CXVaEsBWvfHv0AQNRuFA0Uausbo eLn9oUzW2QCdpYXeHiNaRzB6oiRCIHYX1Vj0qFEHzK1XPNwbjRjhCs9caFGLvkqjvilVn klJmYhhHdInF0NLu9PKpIhMXnyyTCSpkdpmaIUoFjKui8vScqJTeziiTQSEJy9E5xTjMJ 6pQMnADo0BFICAd9jBDyq71umyT/VBltpPaUs48liNt2FikwtzYIevT4p0pQzvzIwxU0I YEq8ZvcY+ZRaKd5R3gF2q/6ppiw18Jb8AXWpiPsKK5yTOkh6eBwxW1chD+zeITrC6Ug== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795617954; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=LOULytElIcCdbknHKWZ9k8xYt2sauWk1H6MtYi8p7wc=; b=H+VqWuJ2RDZS0OINDIz/O9IrZp+5/BCMyCNuMJTPAiDaRYRSSvoU0i9EVxz2KYNufWno XvHDf0oZxnOjR5+LTnTfSYQ2INjRKSackVjRAhSDRr5yK8qFGl7PwuIv02zBzlUSLqq4X t4I/n6NswM6nI0nsK1TN/Prot/jDPjNnWY4gUWL5ShxWUb0J4/LnGoN9Y64ox0D4QwfbP RUT0jwcO86ZIoZYslCF1qdQEdry+sPjfFuD7SZVKdWAd4ZBQbB3pPUk4OTbjl8qOevGJM p4gb617upz4qSWug61/X72h1AZgbFICCX+gn07g37Tk36cno7Ws2hTAueif7+GOjW2A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=wVADc6Xw; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8BB40606A31E; Wed, 25 Nov 2026 14:45:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=wVADc6Xw Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F56C1F000C4; Wed, 25 Nov 2026 14:45:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795617953; bh=LOULytElIcCdbknHKWZ9k8xYt2sauWk1H6MtYi8p7wc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=wVADc6XwMB7LSOLT3+SipvoZz98hkV2OYfuFnBmKvKfffRVtUhQ2aCzx5+N1GquhD bFrb31p90RElnivdUAOQMDehKssVRo24uMrZUNTpHuBmLKhLsZC4Cf/YW8Brn88d0S F0IdIcQmIpZ84EdAoKhDAXRs/GStQhj/20+TWlGE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Shawn Guo , Marek Szyprowski , Bartosz Golaszewski , Sasha Levin Subject: [PATCH 7.3 0128/1005] gpio: shared-proxy: Fix race between concurrent first users Date: Wed, 25 Nov 2026 15:24:26 +0100 Message-ID: <20261125143824.737680351@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125143821.729802100@linuxfoundation.org> References: <20261125143821.729802100@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Shawn Guo [ Upstream commit eb70d2419e79b6720fd465b905a2dddd751f2a48 ] The proxy lets a consumer set the line direction and reset the default value and vote tally only when usecnt == 1, i.e. when it is the sole requester. But request() and direction_output() take the descriptor mutex separately. When two consumers probe concurrently, both request the line before either configures it, both see usecnt == 2, both fall into the shared path, read back the still unconfigured direction (input) and fail with -EPERM. On Nord RRD the two DAC supplies are reg-fixed-voltage regulators sharing tlmm GPIO 41. They probe asynchronously and intermittently both fail, leaving the codecs without supplies: reg-fixed-voltage regulator-3p3-dac: setup of GPIO (default) failed: -1 reg-fixed-voltage regulator-5p0-dac: setup of GPIO (default) failed: -1 Requesting the line is not evidence that a consumer has claimed it. Instead, count proxies that have actually configured the line (set its direction or voted for a value) in a new cfgcnt, with a per-proxy configured flag, and allow (re)initialization only while no other proxy has done so. A consumer obtained with GPIOD_ASIS that votes via gpiod_set_value() without ever setting the direction counts as configured, so a later user can never reset def_val/votecnt behind its vote. The count is dropped when the proxy frees the line. Fixes: e992d54c6f97 ("gpio: shared-proxy: implement the shared GPIO proxy driver") Signed-off-by: Shawn Guo Tested-by: Marek Szyprowski Link: https://patch.msgid.link/20261028123448.510401-2-shengchao.guo@oss.qualcomm.com Signed-off-by: Bartosz Golaszewski Signed-off-by: Sasha Levin --- drivers/gpio/gpio-shared-proxy.c | 78 ++++++++++++++++++++++++++++---- drivers/gpio/gpiolib-shared.h | 1 + 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/drivers/gpio/gpio-shared-proxy.c b/drivers/gpio/gpio-shared-proxy.c index bc69b8729d196..2e9ccd8a8d835 100644 --- a/drivers/gpio/gpio-shared-proxy.c +++ b/drivers/gpio/gpio-shared-proxy.c @@ -21,9 +21,37 @@ struct gpio_shared_proxy_data { struct gpio_chip gc; struct gpio_shared_desc *shared_desc; struct device *dev; + bool configured; bool voted_change; }; +/* + * A proxy may (re)initialize the line - set its direction and reset the + * default value and the vote tally - only while no other proxy has set the + * direction or voted for a value. Requesting the line is not enough: consumers + * may request concurrently, so counting requests lets every one of them + * conclude that it is not first. + */ +static bool gpio_shared_proxy_may_init(struct gpio_shared_proxy_data *proxy) +{ + unsigned int cfgcnt = proxy->shared_desc->cfgcnt; + + lockdep_assert_held(&proxy->shared_desc->mutex); + + return !cfgcnt || (cfgcnt == 1 && proxy->configured); +} + +static void gpio_shared_proxy_mark_configured(struct gpio_shared_proxy_data *proxy) +{ + lockdep_assert_held(&proxy->shared_desc->mutex); + + if (proxy->configured) + return; + + proxy->configured = true; + proxy->shared_desc->cfgcnt++; +} + static int gpio_shared_proxy_set_unlocked(struct gpio_shared_proxy_data *proxy, int value) { @@ -117,6 +145,11 @@ static void gpio_shared_proxy_free(struct gpio_chip *gc, unsigned int offset) proxy->shared_desc->usecnt--; + if (proxy->configured) { + proxy->configured = false; + shared_desc->cfgcnt--; + } + dev_dbg(proxy->dev, "Shared GPIO freed, number of users: %u\n", proxy->shared_desc->usecnt); } @@ -131,7 +164,7 @@ static int gpio_shared_proxy_set_config(struct gpio_chip *gc, guard(mutex)(&shared_desc->mutex); - if (shared_desc->usecnt > 1) { + if (!gpio_shared_proxy_may_init(proxy)) { if (shared_desc->cfg != cfg) { dev_dbg(proxy->dev, "Shared GPIO's configuration already set, accepting changes but users may conflict!!\n"); @@ -155,15 +188,21 @@ static int gpio_shared_proxy_direction_input(struct gpio_chip *gc, struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); struct gpio_shared_desc *shared_desc = proxy->shared_desc; struct gpio_desc *desc = shared_desc->desc; - int dir; + int ret, dir; guard(mutex)(&shared_desc->mutex); - if (shared_desc->usecnt == 1) { + if (gpio_shared_proxy_may_init(proxy)) { dev_dbg(proxy->dev, - "Only one user of this shared GPIO, allowing to set direction to input\n"); + "No other user configured this shared GPIO, allowing to set direction to input\n"); + + ret = gpiod_direction_input(desc); + if (ret) + return ret; - return gpiod_direction_input(desc); + gpio_shared_proxy_mark_configured(proxy); + + return 0; } dir = gpiod_get_direction(desc); @@ -176,6 +215,8 @@ static int gpio_shared_proxy_direction_input(struct gpio_chip *gc, return -EPERM; } + gpio_shared_proxy_mark_configured(proxy); + return 0; } @@ -189,18 +230,20 @@ static int gpio_shared_proxy_direction_output(struct gpio_chip *gc, guard(mutex)(&shared_desc->mutex); - if (shared_desc->usecnt == 1) { + if (gpio_shared_proxy_may_init(proxy)) { dev_dbg(proxy->dev, - "Only one user of this shared GPIO, allowing to set direction to output with value '%s'\n", + "No other user configured this shared GPIO, allowing to set direction to output with value '%s'\n", str_high_low(value)); ret = gpiod_direction_output(desc, value); if (ret) return ret; + /* Only this proxy can have voted, so the tally is ours to reset */ shared_desc->def_val = value; shared_desc->votecnt = 0; proxy->voted_change = false; + gpio_shared_proxy_mark_configured(proxy); return 0; } @@ -215,7 +258,13 @@ static int gpio_shared_proxy_direction_output(struct gpio_chip *gc, return -EPERM; } - return gpio_shared_proxy_set_unlocked(proxy, value); + ret = gpio_shared_proxy_set_unlocked(proxy, value); + if (ret) + return ret; + + gpio_shared_proxy_mark_configured(proxy); + + return 0; } static int gpio_shared_proxy_get_cansleep(struct gpio_chip *gc, @@ -230,10 +279,21 @@ static int gpio_shared_proxy_set_cansleep(struct gpio_chip *gc, unsigned int offset, int value) { struct gpio_shared_proxy_data *proxy = gpiochip_get_data(gc); + int ret; guard(mutex)(&proxy->shared_desc->mutex); - return gpio_shared_proxy_set_unlocked(proxy, value); + /* + * A GPIOD_ASIS consumer may vote without ever setting the direction; + * count it as configured so a later user can't reset its vote. + */ + ret = gpio_shared_proxy_set_unlocked(proxy, value); + if (ret) + return ret; + + gpio_shared_proxy_mark_configured(proxy); + + return 0; } static int gpio_shared_proxy_get_direction(struct gpio_chip *gc, diff --git a/drivers/gpio/gpiolib-shared.h b/drivers/gpio/gpiolib-shared.h index 618756f6c6aaf..ae10506b7029f 100644 --- a/drivers/gpio/gpiolib-shared.h +++ b/drivers/gpio/gpiolib-shared.h @@ -41,6 +41,7 @@ struct gpio_shared_desc { struct gpio_desc *desc; unsigned long cfg; unsigned int usecnt; + unsigned int cfgcnt; unsigned int votecnt; int def_val; struct mutex mutex; /* serializes all proxy operations on this descriptor */ -- 2.53.0