From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796383112; b=iwyNfFkcOt+xFWUf4hFYpvFR2FAOxMngyoZb+XsGjxjW2RGMkH2fBHrNKxyNdaKwueMs Ke2vEz81Rif7c1oNTIUPdHWCW9pGCanaIGb8rUc7BNZt+shRgpR47pv7I97OiyqYMvULk RUMbhTif3RJBZGT3lIGZS+1Hys/w2JdeCLpl2eKbot01l3I6IwDIP1HWjtr+WTtvyMKGK dpkoahvzkF4v52cyy9dmx36YQJ8cBi1+c9b9Sg5w2SgToad+Zroiqd8aJmW3R2bSCL+Zi NbW8zwmi/2phc+6HGIWz51H0si/eoonygsZzGe2wy4sPz6bS0L63LJmLDrElnTZ9Dmg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383112; 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=lJ5CY756p21L/mPfIBYfL3bNOILDKLXXXCqw5klPAgE=; b=c+/MmGP9IWEuRFPGXuujF2b/XHuF+bBcLQ/sbXHhazKPWFbQTBHxnLf0ahWqcZ2Bliw/ dshr0eUEvcNGHBykc63nDA6TLzJxeUKwG9F4X5jPpN1v0iXn9qovpsUxh1zgWJHQTFv72 4hUKWhOLBb08FOb6gTJTEj5DeCX0s5fdz/qfQ0yB5yQRb6E/d67X3Ub1Xk6C1YZa23kII 24F9yB/HC4MHkDF0CFfB4KiOrVRPy5Pgg0Taj0R8bhftqkTtP6fh22U852XlSuNpuQZPi FBWN45VVXU8k0m64GoziX/sceTD7W9vbYidEzNBOxPS8v2DVjiiC/dBG0pUFQ6ykS2w== 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=J+xjVzzW; 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 E8E40606A33B; Fri, 4 Dec 2026 11:18:31 +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=J+xjVzzW Received: by smtp.kernel.org (Postfix) with ESMTPSA id 666D11F000C2; Fri, 4 Dec 2026 11:18:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383111; bh=lJ5CY756p21L/mPfIBYfL3bNOILDKLXXXCqw5klPAgE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=J+xjVzzWtewMhqYz1jQSZf/OEEwuogxt2JaLlhpA1MEyuQ+ZkC+/R3m1w/YJGmMwS b/b+sB4pInb94FXCtH2IFSX3+XXEWUkW2I/dcmEek80GuNuFmxJu7aJlOjON43dmY1 DbFqLS5Lvz85mKSxt4Vk+zrQkqsZjbXtx1gaZz/c= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Wentao Liang , Bartosz Golaszewski , Sasha Levin Subject: [PATCH 6.12 075/555] gpio: keep the chip reference when deferred setup fails Date: Fri, 4 Dec 2026 19:01:58 +0900 Message-ID: <20261204100610.114444110@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Wentao Liang [ Upstream commit 7e899fe007d2ffc753e6aa5bc8f2ae0a802b9e07 ] gpiochip_setup_devs() runs once gpiolib has been initialized and finishes the registration of the chips that were added before that. When the registration of such a chip fails, the function drops the reference taken in gpiochip_add_data_with_key() with gpio_device_put(), and only then logs the error through &gdev->dev. For these chips that reference is the last one, so gpiodev_release() frees gdev: the dev_err() dereferences freed memory, and the chip is still linked in gpio_devices, so the next lookup walks into the freed object as well. Here the registration cannot be undone the way gpiochip_add_data_with_key() does it, as the driver already got success from gpiochip_add_data() and will call gpiochip_remove(), which unlinks the chip and drops the reference. Keep the reference and only report the failure. Fixes: 16fdabe143fc ("gpio: Fix resource leaks on errors in gpiochip_add_data_with_key()") Cc: stable@vger.kernel.org Assisted-by: DeepSeek-V4.1-Flash and OpenAI Codex Signed-off-by: Wentao Liang Link: https://patch.msgid.link/20261104025345.1527526-1-vulab@iscas.ac.cn Signed-off-by: Bartosz Golaszewski [ sashal: Adjusted the gpiochip_setup_dev() prototype and its call site to take struct gpio_device *gdev instead of struct gpio_chip *gc, since this tree has no per-chip gc lookup in that loop. ] Signed-off-by: Sasha Levin --- drivers/gpio/gpiolib.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index c9dae5cf4db34..b3a557c41fa91 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -787,7 +787,8 @@ static const struct device_type gpio_dev_type = { /* * An initial reference count has been held in gpiochip_add_data_with_key(). - * The caller should drop the reference via gpio_device_put() on errors. + * A caller that tears the chip down on errors must drop the reference via + * gpio_device_put(). */ static int gpiochip_setup_dev(struct gpio_device *gdev) { @@ -860,12 +861,16 @@ static void gpiochip_setup_devs(void) list_for_each_entry_srcu(gdev, &gpio_devices, list, srcu_read_lock_held(&gpio_devices_srcu)) { + /* + * The reference taken in gpiochip_add_data_with_key() is + * dropped by gpiochip_remove(): this chip is still registered + * and its driver has not been told about the failure, so the + * reference must be kept here. + */ ret = gpiochip_setup_dev(gdev); - if (ret) { - gpio_device_put(gdev); + if (ret) dev_err(&gdev->dev, "Failed to initialize gpio device (%d)\n", ret); - } } } -- 2.53.0