Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4769768rwd; Tue, 23 May 2023 12:13:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ61rD/nbEAy2E5eyX7dPSFsWcI22W0nHpknuxq3IfJ7g8porvqsBCQ44mI+tqKkd0IbBGBe X-Received: by 2002:a05:6a00:180d:b0:64e:bada:2411 with SMTP id y13-20020a056a00180d00b0064ebada2411mr10790pfa.25.1684869188472; Tue, 23 May 2023 12:13:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684869188; cv=none; d=google.com; s=arc-20160816; b=P/y9gwDCIS1ia8EulM3nEaLJk0jUB7jvqotlkZP3iUHy7Yt0VgZOQU+gJ80RLkhPlk SFdnZ3LCbdRnPQS4pXPzV6dun95PQnsfdYO/brfs9tMYfevdNny2s9lmzkc/Mn9WQbxJ qP46PNhv6/gm8bicELxhA0IiCWLFQVHvVcLCpDqWwGrZt7vB7QoKzfb2NqkAU9sEjYZN iranPEkImG/XqMyP0+IuucK5gSZ2/6swQ7uoifVey7y8SHSVggGiBaFdnMciRa+panYj ytuoq0YI96CCwr6nxesKFPaK2dcQDgGHDqHiOw1+ybDmKdX99kbbOxuOD7qgQzq7nj6S QErg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=RJFRYUGcMe7LMj5nBQpYtgVrgLs7P4vK7RG89WbfsGw=; b=i9HSJo9YL/jKqLGmudCYIpcyeq8DLsQGskXahMdatonrjKq6zTaSs38JO2WnW2D7B0 7h/qGMcmJzFbX3SNTKd8Q8A28NweGxGKk2+SAyq0vgmh+QiwfgKsx7vjjKNjOewa9tRw eX96RfsAgzfLFYIaoOt7UYsy+4nHpEVyR2j8177U+F78+JMU86fdIzrOlhs1xPnnGUkk NeaNSDexLd9ztyxttL7CWyrB/n5gxOtC9vzoCh6R3YuOOtYcBoQzMGzkeeBjaLo2iVcD 5P01rlXZDYvHTJy4mfeBWvAdKbgrJ4odFn7MmVRidA53Zd9qgPpJxidllixsHbQ20CLi DFuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="QykgS/Bu"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h3-20020aa796c3000000b0064d5f8d8434si2380952pfq.290.2023.05.23.12.12.53; Tue, 23 May 2023 12:13:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="QykgS/Bu"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238226AbjEWTGU (ORCPT + 99 others); Tue, 23 May 2023 15:06:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233642AbjEWTGS (ORCPT ); Tue, 23 May 2023 15:06:18 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CF1391; Tue, 23 May 2023 12:06:17 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2af2c7f2883so2387411fa.3; Tue, 23 May 2023 12:06:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684868775; x=1687460775; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=RJFRYUGcMe7LMj5nBQpYtgVrgLs7P4vK7RG89WbfsGw=; b=QykgS/BuU4h6WbXj5oxEQBuW/llGtZooI+t2jrzEc3uCX0FbFyX35iB08ErewuwgSv g06/wJj+S2AJM47CRYdHYcuir6o7TikahuilCfRoaNDRAGvIGPDmBO0nv8JKsgL6FXWL uU9B+DPEUZinciiYM3A+zy7z9lU9QqBs3Rh0TuNi5l5qPwUTcWqXGud3fGL0disLNoWJ lCWFct1xh8iQEQcI3OtMMXKccGve06VX2VLWbdNws5F77kAX5/F9+05fuaNhDeIcJjAr xLO3/tnD406WklSCJBffDIE2e0vxAUvAp/8p+wZrj161qok6bXhktnkWofb8+NOMkr/w G5sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684868775; x=1687460775; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RJFRYUGcMe7LMj5nBQpYtgVrgLs7P4vK7RG89WbfsGw=; b=HQQAigm12wmney6PEZZM8HwiC7aMdlTFDZ8+vhK1rAWde8wP866hYUEPElzlT8amOX cgbaQ4oSSCp7aV8vxJOO3wOCUxppxBmzEeW0KZIcGL6REkHCtmsuuhHWY5lODpafEuqT /U/aW1o698f7SWOiGFCyAfS0hlolYyM8ChBtXm3jvqX/UQILx2xR6ZUTYVyz3IpdBPem h4UkpBY/DxbMP8kb4KKzyS3Mq9UQkibi5Jx1+ICrKYtXEy4Qc3QYxXXv/KbUxaEkwtnK RWGh7jLw/GIOetnEeRm+YsJ61ybnZba1hEBmiYsbVVsimWEio05ZcxHVqwk16AwPwWOH IC2w== X-Gm-Message-State: AC+VfDyPktge34G9Pt1nUwYTdHZ5x3Avc0dVpom6T8Y849StF82SFShR +STTAVOHMfM4On9c/OBZRLEyXV/xd7ExKfzRaCU= X-Received: by 2002:a2e:8788:0:b0:2ad:8929:e8f4 with SMTP id n8-20020a2e8788000000b002ad8929e8f4mr5370076lji.43.1684868775466; Tue, 23 May 2023 12:06:15 -0700 (PDT) MIME-Version: 1.0 References: <20230522234154.2924052-1-yinghsu@chromium.org> In-Reply-To: From: Luiz Augusto von Dentz Date: Tue, 23 May 2023 12:06:02 -0700 Message-ID: Subject: Re: [PATCH v2] Bluetooth: Fix l2cap_disconnect_req deadlock To: Simon Horman Cc: Ying Hsu , linux-bluetooth@vger.kernel.org, chromeos-bluetooth-upstreaming@chromium.org, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Johan Hedberg , Marcel Holtmann , Paolo Abeni , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Simon, Ying, On Tue, May 23, 2023 at 3:04=E2=80=AFAM Simon Horman wrote: > > On Mon, May 22, 2023 at 11:41:51PM +0000, Ying Hsu wrote: > > L2CAP assumes that the locks conn->chan_lock and chan->lock are > > acquired in the order conn->chan_lock, chan->lock to avoid > > potential deadlock. > > For example, l2sock_shutdown acquires these locks in the order: > > mutex_lock(&conn->chan_lock) > > l2cap_chan_lock(chan) > > > > However, l2cap_disconnect_req acquires chan->lock in > > l2cap_get_chan_by_scid first and then acquires conn->chan_lock > > before calling l2cap_chan_del. This means that these locks are > > acquired in unexpected order, which leads to potential deadlock: > > l2cap_chan_lock(c) > > mutex_lock(&conn->chan_lock) > > > > This patch uses __l2cap_get_chan_by_scid to replace > > l2cap_get_chan_by_scid and adjusts the locking order to avoid the > > potential deadlock. This needs the fixes tag so we can backport it properly. > > Signed-off-by: Ying Hsu > > --- > > This commit has been tested on a Chromebook device. > > > > Changes in v2: > > - Adding the prefix "Bluetooth:" to subject line. > > > > net/bluetooth/l2cap_core.c | 26 ++++++++++++++++++++------ > > 1 file changed, 20 insertions(+), 6 deletions(-) > > > > diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c > > index 376b523c7b26..8f08192b8fb1 100644 > > --- a/net/bluetooth/l2cap_core.c > > +++ b/net/bluetooth/l2cap_core.c > > @@ -4651,8 +4651,16 @@ static inline int l2cap_disconnect_req(struct l2= cap_conn *conn, > > > > BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); > > > > - chan =3D l2cap_get_chan_by_scid(conn, dcid); > > + mutex_lock(&conn->chan_lock); > > + chan =3D __l2cap_get_chan_by_scid(conn, dcid); > > + if (chan) { > > + chan =3D l2cap_chan_hold_unless_zero(chan); > > + if (chan) > > + l2cap_chan_lock(chan); > > + } > > + > > if (!chan) { > > + mutex_unlock(&conn->chan_lock); > > cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid); > > return 0; > > } > > Hi Ying, > > The conditional setting of chan and calling l2cap_chan_lock() > is both non-trivial and repeated. It seems that it ought to be > in a helper. > > Something like this (I'm sure a better function name can be chosen): > > chan =3D __l2cap_get_and_lock_chan_by_scid(conn, dcid); > if (!chan) { > ... > } > > ... Or perhaps we could do something like l2cap_del_chan_by_scid: https://gist.github.com/Vudentz/e513859ecb31e79c947dfcb4b5c60453 --=20 Luiz Augusto von Dentz