From nobody Mon May 25 00:08:59 2026 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D68737DE8E for ; Wed, 20 May 2026 07:50:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779263403; cv=none; b=boWIgHzX4MLXvLPP1v/7feKccV3F80yWZutizQjUmVAYt0lGcuWjvbNQArFZuSNNZRF0NJXA17hlfwi/95pp3NdGo6wOR8h1kBsd9o/+Wtbkj6WqrnOZAz3PrxDkq5q3Dpz3x8Ctq5OKUqVmB4Yndq0QTMgObPZYyAHA3onik24= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779263403; c=relaxed/simple; bh=v8QO5wIg8fMTR7uQ8ckLfU5/0hxPkOwG5s8qbIzOad8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B7dNQxL/xsFJjfVWq8U1Y4bp8/QrD0l65dw22WMMotA6+8sz9rV9442k+w6E04M72Ud4RleLj0LJWmFnMIZDNx/hR8tSYZdhQxSPWP6g5FiZ8imx6H9BhoQl8X7PpZNMAUDQlqK+I8a6ESpeAvamaKIO2QlD41/1eFidcnEFhGs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MM/YiFnH; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MM/YiFnH" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-48909558b3aso47869845e9.0 for ; Wed, 20 May 2026 00:50:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779263401; x=1779868201; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Thg0qkEsY0JjFZeY1AfIvrImoT1KU/tjOzYFDJJSqYU=; b=MM/YiFnHIBWAfXqauOYaU6lKNLzl7ah3KqyUBCiksDawFWQoIpv5anAvXv6w/RIZzk MGqxLq4ITMXP4dVbc3bO9Pim/NLPkFoFXP5Ei7C4L5wJWYfvJeAlyNeyk8gA845yIq6u OehCc0WzW0okoCl25J1LDgIr956hEipd7GhUlR3ektEcJUsrCfgoIPAqbanfEkVSdMfv P94GJNwaDuGDW3eg6LqYAnXHpUu+guAmxvf7rha40QeG6zdy9s7V04oOCsxbgdG7hX6j xxGB5w8kcle50Qk6g//NO6tb97KokLfH+6Vn7+CQfG3+hXdSOkqEyb6Lg8jelQMWYohM jUJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779263401; x=1779868201; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Thg0qkEsY0JjFZeY1AfIvrImoT1KU/tjOzYFDJJSqYU=; b=Cn0vB90mE5vC7TcORFPU4F6eanu5Xo+mJlMLzk/fuSf/2aKI/IHy9S7tB0t9IyCF7e nsp5SmYr5ZORqQSNMXfCMl1qwHs9ZcjNIhH++FcxnHwV08wLjElrMo5jN20FUtipG5LY 6DcQU7suyC2pigHtd33Kd0rcAPON6I63OqtcUseTFlop6nuSCaWDi19kWgt2gzi6TSSe 9rN9ybHnIhodtANpXWY80DZlcMwz4A7J85u8m3qt0N5isRYU5mWvgeOAcRTeHeANe19e b5TZywpp9RSqT274qmZAK7TPYax/B8vgx5jQH/21CDd8OR8fGNaqDORAPo5Q+b73iPwn 7LWw== X-Forwarded-Encrypted: i=1; AFNElJ8GL7r5nFDSVs82e4A6+mVdZBqI9bynmsu7acVVD99oibbVCGNMAawxUYgjv2eo5hZR4Nn3P8aq55oc7b0=@vger.kernel.org X-Gm-Message-State: AOJu0Yz8NDdhg7mnVYaBIC1feB0TUmnIFZz6uEIsRQAM30PFItT752Ia 8w4yYO09dJSwppWuZ4NCucIVrTl0KzvoDBzrDFI3n7P3l98EgoaMKCD4 X-Gm-Gg: Acq92OHzaZwQgRWxGnYSJw8y+vb01REybgTeRGrSPWejK05Pr2ODIq1rqhzGfE56i5Q 1/ITqkOMZac7W+23rOHHord3WiQKuhZBUATjvQ+agV79acHgK/E7kNB5o/SfxtXQHV8/FvTBVz0 AqF1fQ3aTwou+liRGhOVWGhRBUIlOUirbUzwx66YcpwauBLjWjnR6LuTh4wTl1r0+/Ub3h4fxTu Di60Y6KatDRMcZuGGqVjwTMsmjpmWNc0idKRqxoSNqCyAbUweIkkElnDlNnIEphk6A4g7uwlerp XDJROjRQy9uba74tk+KKsgtg/t+oQoj4+Zn+Uoby58ddTmBulLgVpECDpDIAWkljhxO/jXe1isE ReYbIzDawI53JpctZDKCyCj/JkULNZkXkqLWo2fTRMEwCZhWVB8w2IK+/vCpm4BIj3N5qX6lkYC 3fVkfGjhepebdkc8p+sVxel0AfT6uSOw== X-Received: by 2002:a05:600c:2ad3:b0:490:f32:3a07 with SMTP id 5b1f17b1804b1-4900f323a11mr124130385e9.30.1779263400772; Wed, 20 May 2026 00:50:00 -0700 (PDT) Received: from G614PR ([2a01:e11:202b:40:328e:213e:a3da:580f]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48fffb9aac4sm496536215e9.9.2026.05.20.00.49.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 00:50:00 -0700 (PDT) From: "Marco Scardovi (scardracs)" To: andriy.shevchenko@linux.intel.com Cc: brgl@kernel.org, linusw@kernel.org, linux-acpi@vger.kernel.org, linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, mika.westerberg@linux.intel.com, mscardovi95@gmail.com, westeri@kernel.org Subject: [PATCH v3 1/2] gpiolib: acpi: Add robust bounds-checking for GPIO pin resources Date: Wed, 20 May 2026 09:45:46 +0200 Message-ID: <20260520074955.55443-2-mscardovi95@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260520074955.55443-1-mscardovi95@gmail.com> References: <20260520074955.55443-1-mscardovi95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Ensure that the GPIO pin resource arrays are safely bounded before accessing indices. Add bounds checking in acpi_request_own_gpiod(), acpi_gpio_irq_is_wake(), and acpi_gpiochip_alloc_event() to prevent out-of-bounds array reads if the ACPI namespace provides malformed or empty pin tables. Assisted-by: Antigravity:gemini-3-flash Signed-off-by: Marco Scardovi --- drivers/gpio/gpiolib-acpi-core.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib-acpi-core.c b/drivers/gpio/gpiolib-acpi-c= ore.c index eb8a40cfb7a9..a6d78dad299e 100644 --- a/drivers/gpio/gpiolib-acpi-core.c +++ b/drivers/gpio/gpiolib-acpi-core.c @@ -320,10 +320,18 @@ static struct gpio_desc *acpi_request_own_gpiod(struc= t gpio_chip *chip, unsigned int index, const char *label) { - int polarity =3D GPIO_ACTIVE_HIGH; - enum gpiod_flags flags =3D acpi_gpio_to_gpiod_flags(agpio, polarity); - unsigned int pin =3D agpio->pin_table[index]; struct gpio_desc *desc; + enum gpiod_flags flags; + unsigned int pin; + int polarity; + + polarity =3D GPIO_ACTIVE_HIGH; + flags =3D acpi_gpio_to_gpiod_flags(agpio, polarity); + + if (index >=3D agpio->pin_table_length) + return ERR_PTR(-EINVAL); + + pin =3D agpio->pin_table[index]; =20 desc =3D gpiochip_request_own_desc(chip, pin, label, polarity, flags); if (IS_ERR(desc)) @@ -337,11 +345,16 @@ static struct gpio_desc *acpi_request_own_gpiod(struc= t gpio_chip *chip, static bool acpi_gpio_irq_is_wake(struct device *parent, const struct acpi_resource_gpio *agpio) { - unsigned int pin =3D agpio->pin_table[0]; + unsigned int pin; =20 if (agpio->wake_capable !=3D ACPI_WAKE_CAPABLE) return false; =20 + if (agpio->pin_table_length =3D=3D 0) + return false; + + pin =3D agpio->pin_table[0]; + if (acpi_gpio_in_ignore_list(ACPI_GPIO_IGNORE_WAKE, dev_name(parent), pin= )) { dev_info(parent, "Ignoring wakeup on pin %u\n", pin); return false; @@ -368,6 +381,9 @@ static acpi_status acpi_gpiochip_alloc_event(struct acp= i_resource *ares, return AE_OK; =20 handle =3D ACPI_HANDLE(chip->parent); + if (agpio->pin_table_length =3D=3D 0) + return AE_OK; + pin =3D agpio->pin_table[0]; =20 if (pin <=3D 255) { --=20 2.54.0 From nobody Mon May 25 00:08:59 2026 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1517394797 for ; Wed, 20 May 2026 07:50:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779263407; cv=none; b=FmYvXQO+ulUcSMpoivcJQ3vlGtdzKdDQktswHCafM1rFyNeGAGWHO1liDBQpm20kxOlxMx3CeCQ8YmlTi/bWZr2NCUO7pNMvd+Hu9tqa31q+54bajIaadvDH4b3Jv+B9Rb3fQlBKBhTvjshpfGLzGgo1UlGEpBFyxACDHoKPXsQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779263407; c=relaxed/simple; bh=JpwGT7jPG6Jpd6iciTDyHBYp8cTWxrIHabsT4vt79w4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=En7tVm9dpvDzS03DMuAIIzD1Nq/p32B5er2qAKGTmoGA4NDkw9x3mp+BhF2A8axkMuRZ7t+tw5KQFqtq9vfLfGmS3K6SB9OWp3nM+RE18MM642ehC4cHrRjvTc4y22Q+uwC0mb4NGGy1GF/hUD4OdAwxcUntbMGKkd/994rxAow= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fidlUptY; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fidlUptY" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-488b0e1b870so66179925e9.2 for ; Wed, 20 May 2026 00:50:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779263402; x=1779868202; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KB4yfPsy6uJwPClVU4IsavfoLMQM3N7e3v0jOtaKpeg=; b=fidlUptYPdikL6Z94f+SdSyKPifx3vVnj/IIWFrqNXiWmUXxAWXLVX3AhRCwvRezoG BxcmaQ4DWAvjotM6VJENxqwz38CwsDhrwG1bNawSC9nufkg/9OpcvmNYYgwlToT55g+4 EezXzqsqiayPx0kuTWhXROQgApTJOdi7DORksF1dy/grNJxx/gli+hTYMpB8nMZq7RQK rqxhcxlGlx6xaj5iPpmoDyEu/3SXRQASE8z2LqWXWdNgbnN1r02AODVfsRmRsUV6sezP 4Xec89dB0idQxri9rQ5G0uXPgg/DmIKvKwsELmADJQY/1JJfQQpusMBZO/3b4tXkEzEc Di8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779263402; x=1779868202; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=KB4yfPsy6uJwPClVU4IsavfoLMQM3N7e3v0jOtaKpeg=; b=gTdYGWnRP8CMgetkDfVEQASgqWFNpzrfy0t7uxrQ0Nikne/BukrrF+fKmG+01K2Rje qYN7KO0yZ5z1kuVtLHyQ5Hdiu2AkSe/v8qYM3cxKaPHS5MTL7RLIJ66wUgQwez+6BTzX nempq+nlpSbxAh2dOvhfVemCplm94Ic7iv/QQ6H/69grLd2b2Gibut/7pY8t6WfM4xjz UklvTu3cVj37+TE/OZbOAUSn5Zw0mzy2RYljASLgzUUeHQz5MSozDXTX7Ueum64Xsf2T G+JjaZAUtkws5nd5xHQ5sT7dOzVNvUaP65MqZyXciOErejWVJkQLdmfM3e46bICEDW+l yXXQ== X-Forwarded-Encrypted: i=1; AFNElJ99FHEQ7LYALHE5KEtud3ZdxBtpPOzuKQBqKxprSKxMwaqNl186muCg+Wd/dc8uro4l9mpEKxjSN6xYY+k=@vger.kernel.org X-Gm-Message-State: AOJu0Yxf+EVjhxuNfuWvAdBeT7jJDJfB0E1b8GOMneB2EQhpVYRPxQWq AXR2L2fYCXsdR3zi2SVzZGwXX/77tfbzhdnyea3Ixazqp8adh8VBNM1x X-Gm-Gg: Acq92OG4ladU0MO0/wymImS9HSDmRajOn4yrc+7RmZaD969ndyBF7R0Tdlyd70B5/10 Y2xeewHeARiIlQF+Zfh2k03375OtLXFcy49HQW5yfKEmy3Dkx6lQ2HI+9lOlp3frEjt+NCRxzBn Loz+lW56CFu0qwj1XTnGl6g8CHuZ/YiHebwKN3t6umM+dXiLgnZxN8U08PQG6BKmaYPLZPRXn30 jbY+W+0sWhhsdTPdH1CTmEzbuWjmh41UgKWOHq2Ghf/bQOYGXwymHIzBvpEz+qAXQ+utcS5bn9B oCH5nPvdQNue94nZkpZlF0ghgIvQGAw7buGIM304RRLvtr2DwkCKrNt8alZXNVZs580zLHEq/xJ mz8F7voL1VBDK/XXl1BzV1EHGFaFX7OBvB9/jeWpxaxLvlzU3d8siuxm9GYgbCzV0ln21ZF4wVu BUCTZS1JTMvr4bSGohNEw= X-Received: by 2002:a05:600c:c087:b0:48d:c0a:3813 with SMTP id 5b1f17b1804b1-48fe60de6a7mr280665505e9.3.1779263402181; Wed, 20 May 2026 00:50:02 -0700 (PDT) Received: from G614PR ([2a01:e11:202b:40:328e:213e:a3da:580f]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48fffb9aac4sm496536215e9.9.2026.05.20.00.50.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 00:50:01 -0700 (PDT) From: "Marco Scardovi (scardracs)" To: andriy.shevchenko@linux.intel.com Cc: brgl@kernel.org, linusw@kernel.org, linux-acpi@vger.kernel.org, linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, mika.westerberg@linux.intel.com, mscardovi95@gmail.com, westeri@kernel.org Subject: [PATCH v3 2/2] Fixes: gpiolib: acpi: resource leak in OpRegion Date: Wed, 20 May 2026 09:45:47 +0200 Message-ID: <20260520074955.55443-3-mscardovi95@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260520074955.55443-1-mscardovi95@gmail.com> References: <20260520074955.55443-1-mscardovi95@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If acpi_remove_address_space_handler() fails, the cleanup function acpi_gpiochip_free_regions() previously returned early. This leaks the connections list and all requested GPIO descriptors. Similarly, if acpi_gpio_adr_space_handler() fails to allocate a connection or request a GPIO descriptor during a multi-pin transaction, it exits without freeing the descriptors and connections that were already allocated in the same call. To avoid leaks, introduce a localized new_conns list inside the handler to track the new connections requested during the current transaction. On error, roll back only the connections in the new_conns list (avoiding deadlock on the non-recursive conn_lock and preventing over-cleanup of historic connections). On success, splice the new_conns list into the global achip->conns list under the conn_lock. Additionally, rename the global connection cleanup function to acpi_gpiochip_free_all_connections() and call it in the GPIO chip teardown path. Fixes: 473ed7be0da0 ("gpio / ACPI: Add support for ACPI GPIO operation regi= ons") Assisted-by: Antigravity:gemini-3-flash Signed-off-by: Marco Scardovi --- drivers/gpio/gpiolib-acpi-core.c | 133 ++++++++++++++++++++++++------- 1 file changed, 102 insertions(+), 31 deletions(-) diff --git a/drivers/gpio/gpiolib-acpi-core.c b/drivers/gpio/gpiolib-acpi-c= ore.c index a6d78dad299e..f891451ef0ba 100644 --- a/drivers/gpio/gpiolib-acpi-core.c +++ b/drivers/gpio/gpiolib-acpi-core.c @@ -1094,6 +1094,46 @@ int acpi_dev_gpio_irq_wake_get_by(struct acpi_device= *adev, const char *con_id, } EXPORT_SYMBOL_GPL(acpi_dev_gpio_irq_wake_get_by); =20 +static void acpi_gpiochip_free_connection_list(struct list_head *list) +{ + struct acpi_gpio_connection *conn; + struct acpi_gpio_connection *tmp; + + list_for_each_entry_safe_reverse(conn, tmp, list, node) { + gpiochip_free_own_desc(conn->desc); + list_del(&conn->node); + kfree(conn); + } +} + +static void acpi_gpiochip_free_all_connections(struct acpi_gpio_chip *achi= p) +{ + guard(mutex)(&achip->conn_lock); + + acpi_gpiochip_free_connection_list(&achip->conns); +} + +static struct acpi_gpio_connection * +acpi_gpiochip_find_conn(struct acpi_gpio_chip *achip, + struct list_head *new_conns, unsigned int pin) +{ + struct acpi_gpio_connection *conn; + + list_for_each_entry(conn, &achip->conns, node) { + if (conn->pin =3D=3D pin) + return conn; + } + + if (new_conns) { + list_for_each_entry(conn, new_conns, node) { + if (conn->pin =3D=3D pin) + return conn; + } + } + + return NULL; +} + static acpi_status acpi_gpio_adr_space_handler(u32 function, acpi_physical_address address, u32 bits, u64 *value, void *handler_context, @@ -1101,11 +1141,16 @@ acpi_gpio_adr_space_handler(u32 function, acpi_phys= ical_address address, { struct acpi_gpio_chip *achip =3D region_context; struct gpio_chip *chip =3D achip->chip; + struct acpi_gpio_connection *other; + struct acpi_gpio_connection *conn; struct acpi_resource_gpio *agpio; struct acpi_resource *ares; u16 pin_index =3D address; + LIST_HEAD(new_conns); acpi_status status; int length; + u16 shift; + u16 word; int i; =20 status =3D acpi_buffer_to_resource(achip->conn_info.connection, @@ -1129,20 +1174,15 @@ acpi_gpio_adr_space_handler(u32 function, acpi_phys= ical_address address, length =3D min(agpio->pin_table_length, pin_index + bits); for (i =3D pin_index; i < length; ++i) { unsigned int pin =3D agpio->pin_table[i]; - struct acpi_gpio_connection *conn; struct gpio_desc *desc; - u16 word, shift; - bool found; + bool found =3D false; =20 mutex_lock(&achip->conn_lock); =20 - found =3D false; - list_for_each_entry(conn, &achip->conns, node) { - if (conn->pin =3D=3D pin) { - found =3D true; - desc =3D conn->desc; - break; - } + conn =3D acpi_gpiochip_find_conn(achip, &new_conns, pin); + if (conn) { + desc =3D conn->desc; + found =3D true; } =20 /* @@ -1163,34 +1203,62 @@ acpi_gpio_adr_space_handler(u32 function, acpi_phys= ical_address address, } } =20 + mutex_unlock(&achip->conn_lock); + if (!found) { desc =3D acpi_request_own_gpiod(chip, agpio, i, "ACPI:OpRegion"); if (IS_ERR(desc)) { - mutex_unlock(&achip->conn_lock); + /* + * In case of concurrent handler execution, if + * another thread has already requested the same + * GPIO pin, this call might fail with -EBUSY. + * Acquire the lock and re-check both lists. If + * the other thread has added it in the meantime, + * we use that one. + */ + if (PTR_ERR(desc) =3D=3D -EBUSY) { + mutex_lock(&achip->conn_lock); + conn =3D acpi_gpiochip_find_conn(achip, &new_conns, pin); + if (conn) { + desc =3D conn->desc; + found =3D true; + } + mutex_unlock(&achip->conn_lock); + if (found) + goto use_existing; + } status =3D AE_ERROR; - goto out; + goto err_free_new_conns; } =20 conn =3D kzalloc_obj(*conn); if (!conn) { gpiochip_free_own_desc(desc); - mutex_unlock(&achip->conn_lock); status =3D AE_NO_MEMORY; - goto out; + goto err_free_new_conns; } =20 conn->pin =3D pin; conn->desc =3D desc; - list_add_tail(&conn->node, &achip->conns); - } =20 - mutex_unlock(&achip->conn_lock); + mutex_lock(&achip->conn_lock); + /* + * Re-check both lists again before adding to local + * new_conns, as another thread could have completed + * and spliced its list while conn_lock was released. + */ + other =3D acpi_gpiochip_find_conn(achip, &new_conns, pin); + if (other) { + gpiochip_free_own_desc(conn->desc); + kfree(conn); + desc =3D other->desc; + } else { + list_add_tail(&conn->node, &new_conns); + } + mutex_unlock(&achip->conn_lock); + } =20 - /* - * For the cases when OperationRegion() consists of more than - * 64 bits calculate the word and bit shift to use that one to - * access the value. - */ +use_existing: word =3D i / 64; shift =3D i % 64; =20 @@ -1204,9 +1272,19 @@ acpi_gpio_adr_space_handler(u32 function, acpi_physi= cal_address address, } } =20 + mutex_lock(&achip->conn_lock); + list_splice_tail(&new_conns, &achip->conns); + mutex_unlock(&achip->conn_lock); + + status =3D AE_OK; + out: ACPI_FREE(ares); return status; + +err_free_new_conns: + acpi_gpiochip_free_connection_list(&new_conns); + goto out; } =20 static void acpi_gpiochip_request_regions(struct acpi_gpio_chip *achip) @@ -1229,22 +1307,15 @@ static void acpi_gpiochip_free_regions(struct acpi_= gpio_chip *achip) { struct gpio_chip *chip =3D achip->chip; acpi_handle handle =3D ACPI_HANDLE(chip->parent); - struct acpi_gpio_connection *conn, *tmp; acpi_status status; =20 status =3D acpi_remove_address_space_handler(handle, ACPI_ADR_SPACE_GPIO, acpi_gpio_adr_space_handler); - if (ACPI_FAILURE(status)) { + if (ACPI_FAILURE(status)) dev_err(chip->parent, "Failed to remove GPIO OpRegion handler\n"); - return; - } =20 - list_for_each_entry_safe_reverse(conn, tmp, &achip->conns, node) { - gpiochip_free_own_desc(conn->desc); - list_del(&conn->node); - kfree(conn); - } + acpi_gpiochip_free_all_connections(achip); } =20 void acpi_gpiochip_add(struct gpio_chip *chip) --=20 2.54.0