From patchwork Mon Jun 26 10:39:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YE Chengfeng X-Patchwork-Id: 697556 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBD45EB64D7 for ; Mon, 26 Jun 2023 10:40:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229753AbjFZKkB (ORCPT ); Mon, 26 Jun 2023 06:40:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230321AbjFZKkA (ORCPT ); Mon, 26 Jun 2023 06:40:00 -0400 Received: from JPN01-TYC-obe.outbound.protection.outlook.com (mail-tycjpn01on2096.outbound.protection.outlook.com [40.107.114.96]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31B2DE60; Mon, 26 Jun 2023 03:39:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AaK63OLpr1X1S32NUVnyR775R/Feu2mtoSrrBvYiPVUAvTQ9QI5UYPVlLVCP8MNUlz7pgEFMOjDYFwwEtI9AT6R/a4HEYpvjQdnVJ26NVi154r/kMdmu37hCue97YdzTKHYV7EQ2hmqtYckcFYrofyE9niomFfs10CdHcBxzLqgAxMNx+R7osssykPfYAJt7E+PPeP7NGHOL/ZSC0JDYvu90a3kyF6EMUKWXMou1sn1+naWYJz8HMcJUAWoviSfOMr6M72SJ9ajbyoxiWFw5XSIxajQ7AK4CTTwo65AeYJLuhR/fwu1dK9ZWNZfXGQGEBo3jmnU9d6dp/dZldY/l8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JcLbl1H6CCbpkLr3jQks6vKKGlECGtuFUjFpGlylIDU=; b=mS1FCXctugma0zQH9glRKj9F3GO99RFFvZ9El4OmK7FFjdPDwZEbDYkXARI3zOoM47JGE5kEnrzuP60W6W85ixD6LsGo2QkWivpEiTjc8MH5dsA10SUoyl3BgQxOcdAaRccvppZG7kSwOmYJ7a9jTl5IBe1A8L7C6MfpkXa/8Ulutubx/Jng6J9PE0rbKIip5gpNckkuoADwib/Kp8C4t5BYeW7zHC15GqhSBeHZkrhfXQYSoNxIxfANJ6tMjig06lB6k+XFQa1CKbkG/a+0/Gtto3522uY9pGi0mXhq97i9fnEbTzwG/YmwiHwXwESXfNPPgUdZPabzGQjgfHrDFg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=connect.ust.hk; dmarc=pass action=none header.from=connect.ust.hk; dkim=pass header.d=connect.ust.hk; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=connect.ust.hk; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JcLbl1H6CCbpkLr3jQks6vKKGlECGtuFUjFpGlylIDU=; b=dj83qkAI+Wp9Vm+sERG0eF9knmUB6HwgYZSVNStzx2+JqD/VnqoNFnFiAwcSBxTg5hqp316KYYwLJfE9Zn5GsUfpXEbruVdWuupKeZVXJQll5fXbY3tgB7PXbgwpIBokfx4MUHzHUDK5Np6QlfsshI/886bHgHa/Dft3Q1JvxaE= Received: from TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:b7::8) by TYCP286MB1730.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:187::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6521.26; Mon, 26 Jun 2023 10:39:56 +0000 Received: from TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM ([fe80::b429:5aa3:12f1:6b8]) by TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM ([fe80::b429:5aa3:12f1:6b8%4]) with mapi id 15.20.6521.026; Mon, 26 Jun 2023 10:39:56 +0000 From: YE Chengfeng To: Linus Walleij , Bartosz Golaszewski , "andy@kernel.org" CC: "linux-gpio@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: [PATCH v2 1/2] gpiolib: cdev: Fix &lr->wait.lock deadlock issue Thread-Topic: [PATCH v2 1/2] gpiolib: cdev: Fix &lr->wait.lock deadlock issue Thread-Index: AQHZqBNf7gwzAWjB50ahRCRRc44o1w== Date: Mon, 26 Jun 2023 10:39:56 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=connect.ust.hk; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: TYCP286MB1188:EE_|TYCP286MB1730:EE_ x-ms-office365-filtering-correlation-id: 378b0103-0138-4c50-f7e2-08db7631af2e x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 7xi9sYGTueX1qzm6o513b23B0f2ImRAPTZRfSM/hJbGCCY/ovNFRwdQDdN/9dkaY4w1L03wENhd3JoV2okkhWji78vyMWv/WcHGWJRy327C69/nrojncCFSDB36YLrfJ+QzbP7YjQ7llanCLoNp+twjjiaLQHBS4N4yS7cpTE73/RH8hBaEf7/B0wNFdeJCIUmS3Fy+Eiz3Ta9cU/YcyimJpe3Cy3DJ2Q2qbdr6FiWmYoE238HGOMMBzhV+66zN5TgMB5rj8Le2YJ7uTvYJa0hz8U2XPWI5vSsgTrPdZSYH7i6bAZabDb8t89uKk4SP2H2/Lgdq6lLofykLgjFWvaa8cdS7uku3YxA9BiMvBzm7CabYYSnQjTpuiGO3lz6d0QN28284a+oJs2+UXrrSVYSSXIX67zclA89dHU93Hu3bc5aqtXS458cmWpJQ2BkF0NPA2pKPquUYn7xGEsNeqImp99X8QRjIzIezAssE681kcAjoabTsWJ4zmH0lP72oksTii2ZgNXf5X2/GYuAfCSGN8c4e9O1er1y/6Y8i0PeJdN0G+E/prbCcjDc+RRTc2PVzNndv/m4Q3nAlkf8dIbHy9TlJNvC61xmwINK+FtVabBfS0YqYYk9KjNNzsxzPg x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(13230028)(4636009)(396003)(376002)(366004)(136003)(346002)(39860400002)(451199021)(8676002)(8936002)(66446008)(76116006)(55016003)(66476007)(66556008)(66946007)(41300700001)(786003)(316002)(64756008)(4326008)(6506007)(26005)(9686003)(186003)(478600001)(54906003)(110136005)(7696005)(71200400001)(91956017)(2906002)(52536014)(5660300002)(38070700005)(122000001)(38100700002)(33656002)(86362001)(83380400001); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?eucgb2312_cn?b?UERvQ2ptSEVPcmFTYmVXSzAy?= =?eucgb2312_cn?b?ZElyM0JBeHVRN2Q0UEMzUlo5SkpqenNCdWtQZ251dG9HbkZWMFZrTVo2VmQ5ZW1z?= =?eucgb2312_cn?b?eXJlcmkzTFhjdHMwRkpOZTN0dGEyaFMxVkZ1NXJpVGNaL2hGRHp5R3hqU3pZUVNB?= =?eucgb2312_cn?b?b0FobXJiQ2hwOTVTdEJ1NEk4ZDJWVEU3VEEyTEpDYmVWN3krU1V1cVNNczB2V2Zw?= =?eucgb2312_cn?b?QjdZZGFUakVhUFlhWUVRdUR2M0ZIbGxwQWkxOTJqNTJpNVE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S28x?= =?eucgb2312_cn?b?cHlSMHhHS2hZcmFpazNseCtaZXFLRkUxNTl6QU1FSnQvMCs3LzRBbWRaZnZrckE5?= =?eucgb2312_cn?b?Z0Z0YWdESm9VSDNuZ1hRRVAyc2x1TmhPL2tmRnd4dXlLN0VqSGRTR2FCWk1tYkRO?= =?eucgb2312_cn?b?cVdjczNBdUFsNmt3clJqaXZxRkZoRWJDUVJnQTFpK1MyOVZEN2hJb2J5S3ZxNQ==?= MIME-Version: 1.0 X-OriginatorOrg: connect.ust.hk X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: 378b0103-0138-4c50-f7e2-08db7631af2e X-MS-Exchange-CrossTenant-originalarrivaltime: 26 Jun 2023 10:39:56.7464 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 6c1d4152-39d0-44ca-88d9-b8d6ddca0708 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: IjlxLxpJIOA3qd2F1Jn2m6nkiQ1mWPITlHmH2lhbw7JlUPj1UR1NjfRf5GLkx/uMC353P2DLG9+ScDfEc3MAhw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYCP286MB1730 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org linereq_put_event is called from both interrupt context (e.g., edge_irq_thread) and process context (process_hw_ts_thread). Therefore, interrupt should be disabled before acquiring lock &lr->wait.lock inside linereq_put_event to avoid deadlock when the lock is held in process context and edge_irq_thread comes. Similarly, linereq_read_unlocked running in process context also acquies the same lock. It also need to disable interrupt otherwise deadlock could happen if the irq edge_irq_thread comes to execution while the lock is held. Fix the two potential deadlock issues by spin_lock_bh() and spin_lock_irq() separately. Fixes: 73e0341992b6 ("gpiolib: cdev: support edge detection for uAPI v2") Signed-off-by: Chengfeng Ye --- drivers/gpio/gpiolib-cdev.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 0a33971c964c..f768d46bdea7 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -615,13 +615,13 @@ static void linereq_put_event(struct linereq *lr, { bool overflow = false; - spin_lock(&lr->wait.lock); + spin_lock_bh(&lr->wait.lock); if (kfifo_is_full(&lr->events)) { overflow = true; kfifo_skip(&lr->events); } kfifo_in(&lr->events, le, 1); - spin_unlock(&lr->wait.lock); + spin_unlock_bh(&lr->wait.lock); if (!overflow) wake_up_poll(&lr->wait, EPOLLIN); else @@ -1514,28 +1514,28 @@ static ssize_t linereq_read_unlocked(struct file *file, char __user *buf, return -EINVAL; do { - spin_lock(&lr->wait.lock); + spin_lock_irq(&lr->wait.lock); if (kfifo_is_empty(&lr->events)) { if (bytes_read) { - spin_unlock(&lr->wait.lock); + spin_unlock_irq(&lr->wait.lock); return bytes_read; } if (file->f_flags & O_NONBLOCK) { - spin_unlock(&lr->wait.lock); + spin_unlock_irq(&lr->wait.lock); return -EAGAIN; } - ret = wait_event_interruptible_locked(lr->wait, + ret = wait_event_interruptible_locked_irq(lr->wait, !kfifo_is_empty(&lr->events)); if (ret) { - spin_unlock(&lr->wait.lock); + spin_unlock_irq(&lr->wait.lock); return ret; } } ret = kfifo_out(&lr->events, &le, 1); - spin_unlock(&lr->wait.lock); + spin_unlock_irq(&lr->wait.lock); if (ret != 1) { /* * This should never happen - we were holding the From patchwork Mon Jun 26 10:40:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YE Chengfeng X-Patchwork-Id: 696705 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 005F5EB64DC for ; Mon, 26 Jun 2023 10:40:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229901AbjFZKkH (ORCPT ); Mon, 26 Jun 2023 06:40:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229781AbjFZKkE (ORCPT ); Mon, 26 Jun 2023 06:40:04 -0400 Received: from JPN01-TYC-obe.outbound.protection.outlook.com (mail-tycjpn01on2118.outbound.protection.outlook.com [40.107.114.118]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CB4EE71; Mon, 26 Jun 2023 03:40:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aN6OjpdUq+FEJeTZip72qZpjZevNJR3Wn66ht8AOl+RQWXepIqd645G6zJvzLHvEFnKFmynca5kG1ViQavigUTExoSlRsPfWkAzSyAob39xUISsoQe+yk6Q+QBnE6CXI1P/HugNPFZeHZ47dv+vK+1kcb9Eukm1bLNhef+YBgIVWrKIFEk7sy1D4sm64e1aN+P4FM6LXWCOAkCofYXqAVM6RTOYhWH0smLHeH/wV83vH7vbG2NpKrh48yBdFQPfyVEB9Gy7o3pRasa0lvgO4pg4PZljkfvOrBzDvhvnDnB4/N84KDoGdsw3sIy2yZ25YSkQ9K00z8B5Y+pECeE5jSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=x6j7rOPN5CMI8cbyRrOChwwaYx6B0vg2J4bGj7BtG1o=; b=RX/Qbp60PrOf/WPByzGGMxfZhxXoFtXXNGTyXPVpLUAvcbxuqSGQ+5b5SLiFL5hwrUfbYzTnvXADW3egbEPCHko7gCE3/tlN+VCpwf/JYLyXkM3UMLTa2R7xViYGR2kKXKZSQMiV9lh/08GODZmz+ITLQjSrT9oqHC/t5OX7rsKFxn3r1JLeiyYbWH8YfnK3Bh0CZlJS/CZ0ktrzSP/aeCkVsi90zBPx5gSyOySLGMhc6y8M1F/yiYp8jo57QSKcsHhBZnAS3dc5tO3ptELSvlGEhQ3lTJjYdIBD+GHsmH77oYjEQO1uu9e+h7NX3mNOcVkuWnHF0I4hHGfwmd/ZOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=connect.ust.hk; dmarc=pass action=none header.from=connect.ust.hk; dkim=pass header.d=connect.ust.hk; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=connect.ust.hk; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=x6j7rOPN5CMI8cbyRrOChwwaYx6B0vg2J4bGj7BtG1o=; b=AVWZGXFn1LEqj6ILBIeEfU8EPefWrLlPJ8AOFxNUA+BMdDNCGusk5DY11AG0DtXPgPBMTPws2udgoh0lxEljwyZWVO1wiQs8qm/mB5rxPEQyMz+GN6E+tFdVx6kZtSI30xOGZl7mf4TC4yvByLVj6D0ocZtaO0l7RxBEYo0Yv1o= Received: from TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:b7::8) by TYCP286MB1730.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:187::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6521.26; Mon, 26 Jun 2023 10:40:01 +0000 Received: from TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM ([fe80::b429:5aa3:12f1:6b8]) by TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM ([fe80::b429:5aa3:12f1:6b8%4]) with mapi id 15.20.6521.026; Mon, 26 Jun 2023 10:40:01 +0000 From: YE Chengfeng To: Linus Walleij , Bartosz Golaszewski , "andy@kernel.org" CC: "linux-gpio@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: [PATCH v2 2/2] gpiolib: cdev: Fix &le->wait.lock deadlock issue Thread-Topic: [PATCH v2 2/2] gpiolib: cdev: Fix &le->wait.lock deadlock issue Thread-Index: AQHZqBRmxizNLtHXWE+SgIMBvkENRg== Date: Mon, 26 Jun 2023 10:40:01 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=connect.ust.hk; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: TYCP286MB1188:EE_|TYCP286MB1730:EE_ x-ms-office365-filtering-correlation-id: e15352f2-67b3-486f-5c16-08db7631b1b7 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: Dr9nw/jS0l6RiyHxud7Bffx7yCwciiBEBTcC6WoPgDDu7jrKqVcIWUw187PK45W19AwUTgtgfTPXbAEzM09znlg9J+vr+IGddrTQSvxe7D0EUshxwdqSkDItj1FqJo3hFLeUALwEdsSp7zvyyg39KS+7ALxkdub759YA7pxl/LBFnbyARQnKodo+IBKa/lI/AL4ZnJrPvlKbbcHA7VaHotpaolLnGQVgJf/DeVE/F9uLtqqZLJX4g9N+TX6qXiz9So3bfW6urUslnEkMRpAJam5zey5TtMZV3gi3TG61ag4A5g0l4p7lbZgalHoIYe75/5oVTlACii39erFWo/N1ip6s1xiD2SkcUA62Q9qeTb5ufrfq1GFY4S1W5XCydhYfx8nd43ygaYl57NQcSVMHptVz7uvIJtBLgFWVWt01KlQadnshhU3bULsVdouGPQAQilgbuNNcpbPb5+ifMQirepMGXuBdagdvv50zbOUiZe6EpSPNg+MNjLZ6Ftz7hZqsp3CmZrFcEax7QqZmHtHbuascYvAtF6U735Ya7DpJREoD8rCOa1meSHtQ3TiV7qEVcsAEWwU8wvr5tBSyMYQGEVn9V6jVGvx1XF8Iiayc7yEmexDYSBegkMpwT/DI2oMz x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(13230028)(4636009)(396003)(376002)(366004)(136003)(346002)(39860400002)(451199021)(8676002)(8936002)(66446008)(76116006)(55016003)(66476007)(66556008)(66946007)(41300700001)(786003)(316002)(64756008)(4326008)(6506007)(26005)(9686003)(186003)(478600001)(54906003)(110136005)(7696005)(71200400001)(91956017)(2906002)(52536014)(5660300002)(38070700005)(122000001)(38100700002)(33656002)(86362001)(83380400001); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?eucgb2312_cn?b?YnB6MGswQktKUXNiR2E1ZE1S?= =?eucgb2312_cn?b?VElycllTeDJRYy9lWEtpTzhWaEZnbms1aUpyMkhOMEYzaC8xeFA0eWgzTDZvNTVX?= =?eucgb2312_cn?b?Qjl6VXlWaUdoTlBQTko5UU42S1I1OTdLVWo5VEFlSGdaN1dxbjhEZFFQc1JiUlh1?= =?eucgb2312_cn?b?RUxrU1BuZ1B5dW1QVURIVFQ1NmgyTjl4Z3VFU2RoSWtLNmNVTnV2aUszdVM0K1NR?= =?eucgb2312_cn?b?T0dSZFl4R2dWczdiQUpIYnp0a0hZMmN3OTFranl5SWR4OUtVa1kzTzB6amdFOU5I?= =?eucgb2312_cn?b?TVpyaWIyblVmTnovalEvY2lzZEFmdElRQm0zRWh0OFM0NlVqbGs3ZmRxdi9panFh?= =?eucgb2312_cn?b?aWFSdTFWZkZHazYwOUtqdm9nUU8xdzBpa1lhYW9JeGFFR1BEUzVidlVYSGtRMldZ?= =?eucgb2312_cn?b?YmRkSlFud1NoWUdXWk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G5nTEhoUW5hV01qaTlYVlFXaA==?= MIME-Version: 1.0 X-OriginatorOrg: connect.ust.hk X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: e15352f2-67b3-486f-5c16-08db7631b1b7 X-MS-Exchange-CrossTenant-originalarrivaltime: 26 Jun 2023 10:40:01.0071 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 6c1d4152-39d0-44ca-88d9-b8d6ddca0708 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: uZgpypUOGzXVJihksNLP3l3pSMBAVaUKPLWcErNliMryHZzEQyf9Ao8TADPJQLC2j5IRaguRbwGiLZqFmURECw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYCP286MB1730 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org The softirq lineevent_irq_thread() could deadlock on &le->wait.lock if it preempts lineevent_read_unlocked() which acquires the same lock but not disable irq. Fix the potential deadlock by spin_lock_irq(). Fixes: dea9c80ee672 ("gpiolib: rework the locking mechanism for lineevent kfifo") Signed-off-by: Chengfeng Ye --- drivers/gpio/gpiolib-cdev.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index f768d46bdea7..a2c108f172f2 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -1858,28 +1858,28 @@ static ssize_t lineevent_read_unlocked(struct file *file, char __user *buf, return -EINVAL; do { - spin_lock(&le->wait.lock); + spin_lock_irq(&le->wait.lock); if (kfifo_is_empty(&le->events)) { if (bytes_read) { - spin_unlock(&le->wait.lock); + spin_unlock_irq(&le->wait.lock); return bytes_read; } if (file->f_flags & O_NONBLOCK) { - spin_unlock(&le->wait.lock); + spin_unlock_irq(&le->wait.lock); return -EAGAIN; } - ret = wait_event_interruptible_locked(le->wait, + ret = wait_event_interruptible_locked_irq(le->wait, !kfifo_is_empty(&le->events)); if (ret) { - spin_unlock(&le->wait.lock); + spin_unlock_irq(&le->wait.lock); return ret; } } ret = kfifo_out(&le->events, &ge, 1); - spin_unlock(&le->wait.lock); + spin_unlock_irq(&le->wait.lock); if (ret != 1) { /* * This should never happen - we were holding the lock