From patchwork Sun Jun 25 14:45:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YE Chengfeng X-Patchwork-Id: 696326 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 D56CCEB64DC for ; Sun, 25 Jun 2023 14:45:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229662AbjFYOpU (ORCPT ); Sun, 25 Jun 2023 10:45:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjFYOpT (ORCPT ); Sun, 25 Jun 2023 10:45:19 -0400 Received: from JPN01-OS0-obe.outbound.protection.outlook.com (mail-os0jpn01on2119.outbound.protection.outlook.com [40.107.113.119]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 495EF13E; Sun, 25 Jun 2023 07:45:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ktlZ2iCIORQ9HKd9WUMXr0yZ+11Hwmhiw0XtyZzcJanw3cJl//5xx/4ZV86Bc8mLu7s8/76PJRf3lz4IUlzGRM3DTV2xXQ0Yk9ruoaZHveJUD0wWUCHisq6DUNNEo3APUJv+xIuITCgtb6lEYQdkJD1I+hFYouCf6U86B4IAePPwLvsaN7QDFcRaU6bob8nglu1YPCPof0+XL47oGjlsfAGKO+v1RxohvTB6225dxsYQwlms2Hb9zMmRbIjqB7/g3Ri4Pz31lKpLFkwVJxttYz7tI+3cIabT9nQVPe1jiDOaVpCUUaPoSmOo2mYRJxKnNS1IOsfD3QODpmbNUZ3Ejw== 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=aLfAPeFQeAJ3m/RPTMp2h0zHiacLma+VhXWtrvxXKb8=; b=Iizh5UW/TGB2niqMxJRWU8E4J/MsKsGcINlJ7ThVupTzCgs3T0lJMeaGEByuQUwbaHIfrJA2XtJOPU7WOVJMssGGx+kxl8rR1wWnfOKUzq7T03pbY/DAhumWSuO0MiBrHbpgGY5duKxHibaBgWMMHcpj97X1KrnoFQc3mZrRg3AAWxLcrZYLUfyg1NuvooUK5nDtU2fo6TANREGbpJq3zH7wW8b1RmWFVQWcAlCTd6BB/YaV6PFR0WxL7PwvYEykSW3pAgjUAArKKkHC1kC4bRAQ7Aockd6f/ua7Q/o7Gap2qRfvdhkZg3ABo3gOWtP0+5d9bVrxGEovP0W3BjBTOA== 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=aLfAPeFQeAJ3m/RPTMp2h0zHiacLma+VhXWtrvxXKb8=; b=FQCpw0/wKBFxOrGyOSQ3D0ZK/6wehazQ0rrpnxvfJE3jk0HhLN8jDL7aXF1GbwXL9f8iQk5GJszlNyYM4GLUH9pmUjY1PSMdQm0qrNR27MHapYQskqENXcVI0aJvzbt7XvYHjmqM/MDXjucZORcyGKLB8Pw4vPEZY8/9J6kkZlI= Received: from TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:b7::8) by TY1P286MB3392.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:2ef::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6521.26; Sun, 25 Jun 2023 14:45:13 +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; Sun, 25 Jun 2023 14:45:13 +0000 From: YE Chengfeng To: "linus.walleij@linaro.org" , "brgl@bgdev.pl" , "andy@kernel.org" CC: "linux-gpio@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: [PATCH] gpio: gpiolib-cdev: Fix potential &lr->wait.lock deadlock issue Thread-Topic: [PATCH] gpio: gpiolib-cdev: Fix potential &lr->wait.lock deadlock issue Thread-Index: AQHZp3MKCKremSKpfUKxLQfpASRqlQ== Date: Sun, 25 Jun 2023 14:45:12 +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_|TY1P286MB3392:EE_ x-ms-office365-filtering-correlation-id: bf081694-7031-4843-6e1a-08db758ac851 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 7ZUjEelxDbFQBUCXCaq3/DKKzQa75kKa9LcH+/rxO4gxNyCxxmtLtNv9UoAHe46xERi0TvWDdWxSZWdCiFSv8q9vT/b73nweln3DLlxZI93RCym0tZHsEv9CeOxRcUjTfvs8341sCOxYLNUGw8NsxU/qoaPsvghkiNpd5NGAKkQODaPvEEah6FeN6BAyLfG64d8rqpouqvlePW3d9NXu2NcYD3pRb0Ecu12vd4Qf8V1B7/MzAQ0RdHaecu6jdmSvRvEFO73xd9Jyr7qUuN+VEPdppeO0XoGl+WQKUX+zf9xxESdkBwapoMfKiABjqPHPor3nPaH5/PhfCwbepPOuEKJG1L4fLYGO9/v17EUc50XxOSfjP8czyC/0RdahzUV+U+TVFSCXceK9Z/LnZWoTuto1jKdTlHc3FKqdBngMR1/o9oEGM4bJkTB1JafVOKnfzxskP1kxc2zK4LvfMYLo+NqF4hwSY6XmVhHKmauUKyYbKZxnVO4gBOFHfUebXbpNFjCutJwpx+976QWqKKv07D8jlLNWmgHr0NU0IpJOW3QTDC7Jh9FBt0PqCC54D1LRsrHzJIl+CaUMLkCp0p4pTVu5DuKVp4ZHipbcudP53tnjkFedbQWBXF/YTTdRvPkM 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)(366004)(39860400002)(136003)(396003)(346002)(376002)(451199021)(2906002)(26005)(186003)(71200400001)(786003)(38100700002)(122000001)(9686003)(6506007)(52536014)(5660300002)(83380400001)(41300700001)(8936002)(66446008)(66476007)(76116006)(64756008)(66556008)(66946007)(91956017)(8676002)(33656002)(55016003)(38070700005)(54906003)(7696005)(86362001)(4326008)(316002)(110136005)(478600001); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?eucgb2312_cn?b?aSt3WjZkNXpMMGxGRzZRZDRj?= =?eucgb2312_cn?b?VW5XT1FjaUFOOXRkTmFXSXRtSExlcENLbTlmd3BsN2VEc0JjWTZ0SytwajRHUXJL?= =?eucgb2312_cn?b?Y2xDaExMa2NBRmljTnlJQkswNmIxRXk3SGJseVQxRnY1akU5UUE0aXF3bTBDZFA0?= =?eucgb2312_cn?b?SEUwVXEycnhrMkNMOWVkQzY3eEc2NlZ5LytYa1FhZmFVeEE5MnhYTFYvT1U0dEZX?= =?eucgb2312_cn?b?cUNha1JlR1F1Z1d1S29leWdxd0F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NuaXVqOUVNWjA4T3EwV3QvNkox?= =?eucgb2312_cn?b?cEJUdVpPRlVsejBTaXBnMGNEL0o4SVI5bW8rODBVaTlIRSsyTTBBYzdkQ0FhSnZ4?= =?eucgb2312_cn?b?K2xjTGJtL3pZNjBZN0N1Y3FkYzZrbEZnOXJ2Q2hwSjI5emxqSmpOWGp2Q2V2RHA4?= =?eucgb2312_cn?b?LzVuWHFqMUZlSVdPbXpyMVRtUkQzbk9xenRwNVora3ZjZFRHbUluL2gvMWZDZg==?= 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: bf081694-7031-4843-6e1a-08db758ac851 X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Jun 2023 14:45:12.9239 (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: RNnxSwYCarz9lZmhSrgf2sbvul8P0L9HLyLgMZ9Hr4HF7rGUEvES6g7SIDgCHRK+Ek7Xf+6N4SOwhwrnKKGuGw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TY1P286MB3392 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_irqsave. Signed-off-by: Chengfeng Ye --- drivers/gpio/gpiolib-cdev.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/gpio/gpiolib-cdev.c b/drivers/gpio/gpiolib-cdev.c index 0a33971c964c..714631fde9a8 100644 --- a/drivers/gpio/gpiolib-cdev.c +++ b/drivers/gpio/gpiolib-cdev.c @@ -614,14 +614,15 @@ static void linereq_put_event(struct linereq *lr, struct gpio_v2_line_event *le) { bool overflow = false; + unsigned long flags; - spin_lock(&lr->wait.lock); + spin_lock_irqsave(&lr->wait.lock, flags); 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_irqrestore(&lr->wait.lock, flags); if (!overflow) wake_up_poll(&lr->wait, EPOLLIN); else @@ -1505,6 +1506,7 @@ static ssize_t linereq_read_unlocked(struct file *file, char __user *buf, struct linereq *lr = file->private_data; struct gpio_v2_line_event le; ssize_t bytes_read = 0; + unsigned long flags; int ret; if (!lr->gdev->chip) @@ -1514,28 +1516,28 @@ static ssize_t linereq_read_unlocked(struct file *file, char __user *buf, return -EINVAL; do { - spin_lock(&lr->wait.lock); + spin_lock_irqsave(&lr->wait.lock, flags); if (kfifo_is_empty(&lr->events)) { if (bytes_read) { - spin_unlock(&lr->wait.lock); + spin_unlock_irqrestore(&lr->wait.lock, flags); return bytes_read; } if (file->f_flags & O_NONBLOCK) { - spin_unlock(&lr->wait.lock); + spin_unlock_irqrestore(&lr->wait.lock, flags); return -EAGAIN; } ret = wait_event_interruptible_locked(lr->wait, !kfifo_is_empty(&lr->events)); if (ret) { - spin_unlock(&lr->wait.lock); + spin_unlock_irqrestore(&lr->wait.lock, flags); return ret; } } ret = kfifo_out(&lr->events, &le, 1); - spin_unlock(&lr->wait.lock); + spin_unlock_irqrestore(&lr->wait.lock, flags); if (ret != 1) { /* * This should never happen - we were holding the