Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4019270rwd; Mon, 29 May 2023 22:17:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5HK/HvM4Oyl3TP5WINZxnPEtu55yg9ouwPWW2sb+m3Px79QwF88Bi9hamLVG6KLRwn3Qdm X-Received: by 2002:a05:6a00:b8a:b0:63d:38db:60ef with SMTP id g10-20020a056a000b8a00b0063d38db60efmr1271802pfj.26.1685423843307; Mon, 29 May 2023 22:17:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685423843; cv=none; d=google.com; s=arc-20160816; b=wB7cpiUS1v+wOPMvJMszh0vLXcENaMEE1YW2zHACXg3TCkzfmo7VecD9umdsliGhYJ amplADndJM8YDSd2Lp74B0gjQM1kO7oy4l5ALaBKI9tMb6Gmjlg3gVGXxfwwuaW2h9PW kiCxw+GY46FK0ZIOPzZtS0zbpNKiZ66Ncp037kxvlxrXLn0bmgIBN6xWf5/jqAUyDxHl j+a6Kzlsze5dXyZoRAP8Rp7sNImTYhMwOzM+RhxLDv3a4WwPRoqacTjZeTxweFsoF7cw RXhQUV//LYe8aKxYHqIHzbCQwHSsWotk+mi8J2ywhQNT7zvgR3fQT1pZVnK0Pdcrswgb 6h9w== 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=1oENdb/PNBbVjDrw8mDPjI3X3sDQAuL7/2+YTKg1gLI=; b=ZswEhD1A8fgAqmf6oV25yWqdjhai/9+ZKYSrvSqh4WkmFTuap9oSkAdQn9V6j0q1I5 e0WlIK0ENxQMg4h5A8ahumoXWOK2ddJMkX8G2v4PxXqByZD1L//wT19tAdcLDJJP263K a1fYU2lnvxHiLuWZPwpH5VSfa9a0c5RHyFOz5EG8PyXguernlehsS/++uBG7uPZU9Br/ yzflJ0iIRkbinh759acY7tzNg1Xmo7rlEJnYWTbD/Fwe4w4ScItYPeabExkdB1e5GyOU 89ojeNiaFE4/AeQ4AMy9dgRvR88wRySNmNjas/aE850J3DDwFfOLSWKaJZ8XAK4r6ypM RQGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=nNJLCB3F; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x28-20020aa79a5c000000b0063d61f05a8fsi1087227pfj.198.2023.05.29.22.16.57; Mon, 29 May 2023 22:17:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-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=@chromium.org header.s=google header.b=nNJLCB3F; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229749AbjE3FII (ORCPT + 99 others); Tue, 30 May 2023 01:08:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229569AbjE3FIH (ORCPT ); Tue, 30 May 2023 01:08:07 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BA2FEA for ; Mon, 29 May 2023 22:08:04 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-514924ca903so4127079a12.2 for ; Mon, 29 May 2023 22:08:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1685423283; x=1688015283; 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=1oENdb/PNBbVjDrw8mDPjI3X3sDQAuL7/2+YTKg1gLI=; b=nNJLCB3FtxaV/RTzyPCHNuMIjWkv5BTVxn9XL3QJFc08wvDCKVjKgbij8gjObSOdS+ /VCSeFsV5FTltlQWUJuEIR13YYB7lFYb+h6EdNmYLnyPuW7nCQybmtv+uKox2YDZEfXa Ur4JzdagHpdYjJUKfN/QGIp8UCbs47fhyc+YY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685423283; x=1688015283; 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=1oENdb/PNBbVjDrw8mDPjI3X3sDQAuL7/2+YTKg1gLI=; b=UDBGLtb+IMK/4tXtC8OPqmbPks6k0puZ94wuKPUBxY21sii8Co2220bHEJqj8siygJ JBTDRCWd4uQzDbcq4FVaFFZgFWwTX4Vzi2pjpThe2Z3KYIQgdxgeqxOkJ3aHosKF+KE2 Ec2c5CBtRfdfk2AKMVfuSI6VsZYllGcaQI+5Rycc/44UQPyg/Em5FcgRmJ7UkeXtDr8m unggmI2s87KXSTGZApbWIZqjgnMbjjbhtU9VWhCq0l6Y4QAQ90iachs47kUNglPdhtYZ +u5j/+nGu0eEBAuMGCtTZ6GZu0rmdXqOH+IKD+HoS7gApsIMb6K2c6MUXhBcwGZbmvGk vpcQ== X-Gm-Message-State: AC+VfDwX+BZgS8Dz93qy5PqyzpPzN4T99/qBUIwcZYH12D+vZP+1v7SV cXs26yCzJnJ/GgbUOmaiPI56/Z7RsvVDY4FRt/o= X-Received: by 2002:a17:907:d25:b0:968:1102:1fb7 with SMTP id gn37-20020a1709070d2500b0096811021fb7mr1506499ejc.6.1685423282790; Mon, 29 May 2023 22:08:02 -0700 (PDT) Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com. [209.85.221.43]) by smtp.gmail.com with ESMTPSA id j9-20020a17090686c900b00965e68b8df5sm6773589ejy.76.2023.05.29.22.08.01 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 29 May 2023 22:08:01 -0700 (PDT) Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-30af20f5f67so793576f8f.1 for ; Mon, 29 May 2023 22:08:01 -0700 (PDT) X-Received: by 2002:adf:dd92:0:b0:30a:b46a:a443 with SMTP id x18-20020adfdd92000000b0030ab46aa443mr462543wrl.51.1685423280911; Mon, 29 May 2023 22:08:00 -0700 (PDT) MIME-Version: 1.0 References: <20230522234154.2924052-1-yinghsu@chromium.org> In-Reply-To: From: Ying Hsu Date: Tue, 30 May 2023 13:07:23 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2] Bluetooth: Fix l2cap_disconnect_req deadlock To: Luiz Augusto von Dentz Cc: Simon Horman , 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.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable 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-bluetooth@vger.kernel.org Gentle ping, Luiz. On Thu, May 25, 2023 at 12:16=E2=80=AFPM Ying Hsu wr= ote: > > Hi Luiz, > > The proposal solves the deadlock but might introduce other problems as > it breaks the order of l2cap_chan_del. > There are another way to resolve the deadlock: > ``` > @@ -4663,7 +4663,9 @@ static inline int l2cap_disconnect_req(struct > l2cap_conn *conn, > > chan->ops->set_shutdown(chan); > > + l2cap_chan_unlock(chan); > mutex_lock(&conn->chan_lock); > + l2cap_chan_lock(chan); > l2cap_chan_del(chan, ECONNRESET); > mutex_unlock(&conn->chan_lock); > ``` > > If you're okay with it, I'll do some verification and post a full patch. > > Best regards, > Ying > > On Thu, May 25, 2023 at 2:56=E2=80=AFAM Luiz Augusto von Dentz > wrote: > > > > Hi Ying, > > > > On Wed, May 24, 2023 at 3:54=E2=80=AFAM Ying Hsu = wrote: > > > > > > Hi Simon, > > > > > > I understand your concern about the repeated code. > > > However, simply hiding the locking logic in another function > > > introduces hidden assumptions. > > > For this patch, I would like to fix the deadlock in a simple and easy > > > to understand way. > > > We can always refactor the l2cap_chan utility functions later. > > > > > > Hi Luis, > > > > > > I'll add a fixes tag in the next version. > > > > And how about doing this: > > > > https://gist.github.com/Vudentz/e513859ecb31e79c947dfcb4b5c60453 > > > > > Best regards, > > > Ying > > > > > > > > > On Wed, May 24, 2023 at 3:06=E2=80=AFAM Luiz Augusto von Dentz > > > wrote: > > > > > > > > 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 t= he > > > > > > 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_c= ore.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(s= truct l2cap_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, sc= id); > > > > > > 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 chose= n): > > > > > > > > > > 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 > > > > > > > > -- > > > > Luiz Augusto von Dentz > > > > > > > > -- > > Luiz Augusto von Dentz