From nobody Sat Jun 20 14:13:59 2026 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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 AF54D31DD97 for ; Tue, 14 Apr 2026 18:44:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776192265; cv=none; b=QHVHnfJbwAIKPXv7fbC8UeV3RXjRfp/1UBshRhV+1GM7TaIqNALeLfhhjSxfxXLLUjBHA9dI+1FyaSpwPW+oM+akDOqks3RQCXbW+G+ermPJHq2h82U8sqMBz2MBE5bZSfmiRWpyeWjanIgsGLa8kZxD2baMDIuDDqlMTvjee3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776192265; c=relaxed/simple; bh=TUuhefwC4aFl3YLHYKYGENQxDOTKPvM6Tps0ln9Nurg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=ETGJXsrKR5RqhgRYpNcaHXQhAwJeSLNsbFzOxdGX131IyOIvsmQ4mGWXvA1YMJKoOf/PrbZ7EBY4OkpDZ+reQqVPFLwzb5O1x1tqOT469cE+86D0APVZ30QRH78OzktCRZMVcltxsVrZafnKyfUP6AsYjhmOwxTgCF/NBKqkVwA= 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=ItE2W8Qy; arc=none smtp.client-ip=209.85.167.43 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="ItE2W8Qy" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-5a337552604so5095032e87.2 for ; Tue, 14 Apr 2026 11:44:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776192262; x=1776797062; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=SDSbhtw80z17ovEPSRK2tau26R2taQWyMreMp5Xkpwg=; b=ItE2W8QyZ1Rgt0IwxlxYp5sYLe1lGkUC6QvZJRhPdiVHaybsNWkFjxT8jipJgSdQld uoU9sB6V6Of85TGIRaMkxWTZziWK89yOVba+ZpCGBaZRwz40LYLybrDbXd/zLZ/1q7vp o3Ut3+4RjV049+S+CxqIizLYHzg6jt+u4ZZlbVhsE7szv7lh0+gpbyIP6LI+eWPC8iKb jAiTO7BxNxDHwVZs6PKGI9B1V8q93w+5tv3yG8fIku1+wIOCFOpDFMiNfNt8kvLtrKPV ohTxbwGW6s8TYYhttfHpjNYrsjVJ8xZaonWHnOAsxMvtp7bB7nQQ9fZ5ofKG0yf76TUS B8XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776192262; x=1776797062; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=SDSbhtw80z17ovEPSRK2tau26R2taQWyMreMp5Xkpwg=; b=k8GMP+mdvXHK7cIqbpaKOfXg+lj4yo74K47BNAF+E6th9Gjnm81GXWHVY4G5cbwg1K +M83+ZLEAURXXNOcHIZZsMvG3cS7QOeXGHjI/LpZ5Cr11e8XC4CN1s+Q3UojMZbZSG5D z0WxDVNPT7YsEong0Def+GoBNJ4nzK1W3Xk3hugFPxHcVR4crZpKALkc0wEWf0jgEK3o KXW+VxGdH4N2r7fR21UIbTWxRR9MBdYlsH6JQrLabX6ACt//sXqIS7JJq83vSMyl+hzz QGm0ehQfOQLytw/lxETqSSiAQVLBwoMgrPMGzb883sdthNChuUNFqeLqACjLiNCQx45l /zYg== X-Forwarded-Encrypted: i=1; AFNElJ9O++BhtfGSBZGUx8gfmTf7vD8NMIhU7g0QfJdCr4Ix3zZfAN9qwBObxU5NNXDqubnzz+mOBaD2TzdLEFg=@vger.kernel.org X-Gm-Message-State: AOJu0Yxa5NIXUzMlBwhMhwq+xS7Lc8vjSgo0WQjkFaTjsCAusyQ7hfZZ 2mPvl1sZsDvaWNQttdACN9onWVSU6o3knTSMA1W9KI4tMQYLwGPzokde2WArIEPabNGfEw== X-Gm-Gg: AeBDieu+6WWEildC1QPKLDBE2GjD9x6SIbQjl2TWHxA0/7unsS/AyNkDvTtsJOm6l3v ZMoAAbm8SLmf/n9ITDoYfjBf9wQT0FhwyUcLRZjOPIVPNlIhx2Gh0wm0JQtneKY3FL3KlQazCE8 BUcc+Ak7/KUJp693gAqFZivxrv5NmtSa70EtbPFDZDiZ/vuRTyua9q3p1Gtox9gzAL+ms2PY70z ioozou8uctGnm17TU4bP327EbxHE24BptXlmDkB4xNvpBuaoSM8eWuM6mWQ0vrRD+oVhQvIzm48 nS6L6Xt4gcGh/MT+/kb0/tDw95fbXQ0YobGzUGlXMv6ZQP49nHUs8m8nsY7BT7gTJFeRW9DD/84 spNhTYz6VtV1w3ErXcACQj0q8vQSlWp3WDXxhmqNdbw3EZBV91VTtrbkQ1FmEKxI42TBoL4ridm DktnjveVnpH3aN4jcFLPwe0zXvwty/TROadpCBVBJTwH1H X-Received: by 2002:a05:6512:b86:b0:5a3:ffe5:caec with SMTP id 2adb3069b0e04-5a3ffe5cbdcmr2436008e87.34.1776192261504; Tue, 14 Apr 2026 11:44:21 -0700 (PDT) Received: from localhost ([188.234.148.119]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5a3fe377e5dsm1436814e87.30.2026.04.14.11.44.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2026 11:44:20 -0700 (PDT) From: Mikhail Gavrilov To: luiz.dentz@gmail.com, marcel@holtmann.org Cc: pmenzel@molgen.mpg.de, linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, Mikhail Gavrilov Subject: [PATCH v4] Bluetooth: l2cap: defer conn param update to avoid conn->lock/hdev->lock inversion Date: Tue, 14 Apr 2026 23:43:42 +0500 Message-ID: <20260414184415.17114-1-mikhail.v.gavrilov@gmail.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable When a BLE peripheral sends an L2CAP Connection Parameter Update Request the processing path is: process_pending_rx() [takes conn->lock] l2cap_le_sig_channel() l2cap_conn_param_update_req() hci_le_conn_update() [takes hdev->lock] Meanwhile other code paths take the locks in the opposite order: l2cap_chan_connect() [takes hdev->lock] ... mutex_lock(&conn->lock) l2cap_conn_ready() [hdev->lock via hci_cb_list_lock] ... mutex_lock(&conn->lock) This is a classic AB/BA deadlock which lockdep reports as a circular locking dependency when connecting a BLE MIDI keyboard (Carry-On FC-49). Fix this by making hci_le_conn_update() defer the HCI command through hci_cmd_sync_queue() so it no longer needs to take hdev->lock in the caller context. The sync callback uses __hci_cmd_sync_status_sk() to wait for the HCI_EV_LE_CONN_UPDATE_COMPLETE event, then updates the stored connection parameters (hci_conn_params) and notifies userspace (mgmt_new_conn_param) only after the controller has confirmed the update. A reference on hci_conn is held via hci_conn_get()/hci_conn_put() for the lifetime of the queued work to prevent use-after-free, and hci_conn_valid() is checked before proceeding in case the connection was removed while the work was pending. Fixes: f044eb0524a0 ("Bluetooth: Store latency and supervision timeout in c= onnection params") Signed-off-by: Mikhail Gavrilov Reviewed-by: Paul Menzel --- Changes in v4 (Luiz Augusto von Dentz, Sashiko/Gemini AI review): - Use hci_conn_get()/hci_conn_put() to hold a reference while work is queued - Use __hci_cmd_sync_status_sk() to wait for HCI_EV_LE_CONN_UPDATE_COMPLETE, then do params update + mgmt notification in the sync callback =E2=80=94 = removes the need for hci_event.c changes and hci_sent_cmd_data() - Use kzalloc_obj() per checkpatch recommendation - Fix checkpatch alignment issues =20 Changes in v3 (Luiz Augusto von Dentz): - Move hci_cmd_sync_queue into hci_le_conn_update itself instead of open-co= ding the deferral in l2cap_core.c - Move conn_params update and mgmt_new_conn_param into hci_le_conn_update_complete_evt, using hci_sent_cmd_data to retrieve the originally requested parameters =20 Changes in v2 (Paul Menzel, Sashiko/Gemini AI review): - Allocate before sending ACCEPTED response to avoid state mismatch on OOM - Verify connection handle and address in sync callback against reuse race - Expand commit message with implementation details include/net/bluetooth/hci_core.h | 2 +- net/bluetooth/hci_conn.c | 98 +++++++++++++++++++++++++------- net/bluetooth/l2cap_core.c | 12 +--- 3 files changed, 82 insertions(+), 30 deletions(-) diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_c= ore.h index a7bffb908c1e..aa600fbf9a53 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -2495,7 +2495,7 @@ void mgmt_adv_monitor_device_lost(struct hci_dev *hde= v, u16 handle, bdaddr_t *bdaddr, u8 addr_type); =20 int hci_abort_conn(struct hci_conn *conn, u8 reason); -u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency, +void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 laten= cy, u16 to_multiplier); void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, __u8 ltk[16], __u8 key_size); diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index 11d3ad8d2551..c9cc18fab1f2 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -480,40 +480,100 @@ bool hci_setup_sync(struct hci_conn *conn, __u16 han= dle) return hci_setup_sync_conn(conn, handle); } =20 -u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency, - u16 to_multiplier) +struct le_conn_update_data { + struct hci_conn *conn; + u16 min; + u16 max; + u16 latency; + u16 to_multiplier; +}; + +static int le_conn_update_sync(struct hci_dev *hdev, void *data) { - struct hci_dev *hdev =3D conn->hdev; + struct le_conn_update_data *d =3D data; + struct hci_conn *conn =3D d->conn; struct hci_conn_params *params; struct hci_cp_le_conn_update cp; + u8 store_hint; + int err; =20 + /* Verify connection is still alive. */ hci_dev_lock(hdev); - - params =3D hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); - if (params) { - params->conn_min_interval =3D min; - params->conn_max_interval =3D max; - params->conn_latency =3D latency; - params->supervision_timeout =3D to_multiplier; + if (!hci_conn_valid(hdev, conn)) { + hci_dev_unlock(hdev); + return -ECANCELED; } - hci_dev_unlock(hdev); =20 memset(&cp, 0, sizeof(cp)); cp.handle =3D cpu_to_le16(conn->handle); - cp.conn_interval_min =3D cpu_to_le16(min); - cp.conn_interval_max =3D cpu_to_le16(max); - cp.conn_latency =3D cpu_to_le16(latency); - cp.supervision_timeout =3D cpu_to_le16(to_multiplier); + cp.conn_interval_min =3D cpu_to_le16(d->min); + cp.conn_interval_max =3D cpu_to_le16(d->max); + cp.conn_latency =3D cpu_to_le16(d->latency); + cp.supervision_timeout =3D cpu_to_le16(d->to_multiplier); cp.min_ce_len =3D cpu_to_le16(0x0000); cp.max_ce_len =3D cpu_to_le16(0x0000); =20 - hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); + err =3D __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CONN_UPDATE, + sizeof(cp), &cp, + HCI_EV_LE_CONN_UPDATE_COMPLETE, + HCI_CMD_TIMEOUT, NULL); + if (err) + return err; + + /* Update stored connection parameters after the controller has + * confirmed the update via the LE Connection Update Complete event. + */ + hci_dev_lock(hdev); =20 - if (params) - return 0x01; + params =3D hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); + if (params) { + params->conn_min_interval =3D d->min; + params->conn_max_interval =3D d->max; + params->conn_latency =3D d->latency; + params->supervision_timeout =3D d->to_multiplier; + store_hint =3D 0x01; + } else { + store_hint =3D 0x00; + } =20 - return 0x00; + hci_dev_unlock(hdev); + + mgmt_new_conn_param(hdev, &conn->dst, conn->dst_type, store_hint, + d->min, d->max, d->latency, d->to_multiplier); + + return 0; +} + +static void le_conn_update_complete(struct hci_dev *hdev, void *data, int = err) +{ + struct le_conn_update_data *d =3D data; + + hci_conn_put(d->conn); + kfree(d); +} + +void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 laten= cy, + u16 to_multiplier) +{ + struct le_conn_update_data *d; + + d =3D kzalloc_obj(*d); + if (!d) + return; + + hci_conn_get(conn); + d->conn =3D conn; + d->min =3D min; + d->max =3D max; + d->latency =3D latency; + d->to_multiplier =3D to_multiplier; + + if (hci_cmd_sync_queue(conn->hdev, le_conn_update_sync, d, + le_conn_update_complete) < 0) { + hci_conn_put(conn); + kfree(d); + } } =20 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 95c65fece39b..aac2db1d6fbb 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -4706,16 +4706,8 @@ static inline int l2cap_conn_param_update_req(struct= l2cap_conn *conn, l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, sizeof(rsp), &rsp); =20 - if (!err) { - u8 store_hint; - - store_hint =3D hci_le_conn_update(hcon, min, max, latency, - to_multiplier); - mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type, - store_hint, min, max, latency, - to_multiplier); - - } + if (!err) + hci_le_conn_update(hcon, min, max, latency, to_multiplier); =20 return 0; } --=20 2.53.0