From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796021280; b=uEOEyhtYCcB3OyPzlDDrMAZsxiZlYVX894oi1bKKDwK/chlpa/3ew/C3Qqy1AxHdElsl B2QInICsFZe03zVVtrL1Y3i6/o+fMyiV2JiUsAg8ZpBR+xXOnJT/BmTzqvqz0AmWT/WPt eg8HYm9w9+wDeJow0o02j2bRA981mPTxPcAbzHeMOckCgb2aEZtmzBGhHi4jiGFUHN0qi 3FPvlbJNwDEfajS/i4x6wU/Mo1Ti8GCupd5Ro+7WDn3NNOU2ZTlBkJdrhp7dcWPxRN/1E EvsyzJe+Jtrw8Iye/gA4gsJF3nlQH4TNJQUxcxJhSmnUrt2o7Tc4CmL7bQb1EpZY7qg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796021280; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=/yYoli+fGkNZRmXe6O2vphy7Wf7vGYwtLrvdhiCnNcg=; b=BrVKG0DgF9QqfZVY45tbCqmde1+ZUuJo2ev6shfo+gX994wQN/MWH/gmWIroW2VUOwcG Cm2C3Vc1VWu4+YUXKhq9JfY0J0NHMm7WBw6CPCCYQEGnQDAuu2ywLSUtVqVrzbjy3VPrb tkB74AF5YcTkXHZpPE7efF3QVEkXO9TZddUtvGj5aRPOOtilqF/G8A6Fm84KbAsJ1kzpw x2bac1l4F79tPvG8Q8zyAGBEYio39pbaXAF+OhxyJ4ZkXgBcrBf6G0MfLbYPMogTMVXGW WnLO5TvEZC6nexdl/Un3/7r69Z0HEcRRKK+yMQXpikbIjjDVNPqG1Sqc27vSpc3gPBQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=K3OXsUng; arc=none smtp.remote-ip=209.85.128.51 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E999606A320 for ; Mon, 30 Nov 2026 06:47:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9E999606A320 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.a=rsa-sha256 header.s=20260707 header.b=K3OXsUng Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4a5348aaba5so11562995e9.0 for ; Sun, 29 Nov 2026 22:47:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796021278; x=1796626078; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=/yYoli+fGkNZRmXe6O2vphy7Wf7vGYwtLrvdhiCnNcg=; b=K3OXsUngcwra78cALLU3LBaSSYQWIBsqYL+VEYd6ZFIX2Vs0aL4Dv0c5PEeCVY8gbS KmAcsyJb5EE3807PFWxWbRPyFuD1YO3XKqzHy0ovvfzOK2gB2TAKluVZDM+68NeBl9/T 2aiBaBd0jzw33z3uz8kSsrPKKuwcex0IjCrtqJVtBEpOEVpG44vCnG7VAoK2zco07fMt y8pjE5/aH5rnR0XnusQF/qaRPPQp/CHOQvSLl5iYRCrmssX8kVrpBKfpewBQUZwJZUgz OQphbtVTHMap0AR7ZbO4KRS+2mzedH/xO0eHcBDbW+aXIMmI6775HUQ0YJJk0NAv2xRd FZbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796021278; x=1796626078; h=content-transfer-encoding:content-type: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:content-type; bh=/yYoli+fGkNZRmXe6O2vphy7Wf7vGYwtLrvdhiCnNcg=; b=xoBlmh5OOVF0R8/u/iNEPSaxneatr8cuSC614NnBMlM5/LY03yBbpqM0oKFvPiV6lQ IT9sc6Mks00PXm7sJJLM4Pn0XIJC0tEIl5+kcp+s7azZydbslX7XcUhoL3WW9VAGN9Xd 7WUw/EmEWR/B7bAmjY8UiY0tfTPssgIeLYaPUOjBBqicqtMwShYHngm1Qkt+6R/bHPHY 0DKmSi1ouHyOIi72+nTpbSlURrDhTMFlc55F3SdlQqBUOJDVrswIxwynaBCc7EOjFPip UGxIQ6StJlBrBc+uS3dwKQ+4v7ah9Bda3W0E0TjHUCCsY02J+PoFEbk/p4VPZBUMIjap 7Heg== X-Gm-Message-State: APuU4Z+1PoiwOY8/BYL5OtEaBjKslsvQWVXDZfdY79B3R3kCSChOMvqN Pkgcl9XPfC9m395FfjrV28i4i075upEG35Lp3/MhCUAE5sEJHkNQ9qeiA/B2ijqn X-Gm-Gg: AZFdTZdd7dSonPTjfILVzjObtrIYQ9bu/z0PMjzYZjcELQWlYqs31hgQP74CuvUMIZw KDexj0BcK5bAHTFoPUNghmXckxmXqS3xB0qolgcIrEFLxq4rfv23pe9lziH0PbGUV3/ljqon0qJ brIuHFk851RSOeIdc59d6/tj4GhovXr/AVQjKF/PfPUuDGdEYProi25pv4ezviTJpOhZnB1Uric HDaZe3HONf/K30BAQxtjVrrZrdluyvVbM796KDPnWY4K0Xl4v6rjuGlgY9eu2gijs18YdOTpt+T dL+nLq6MCQ4RwlqZWR1d2F2kSMfhzRGHa6CdfeEe+MpDlooj4Gw5Q+BaY2IwejxnWndpg7Mu53k KDBM8r18wU6SlnAMv9yepI1/QD5tLVYFOWBnRm+UF0ioCD7G/hxoKmSpbTCjryXfDqWcpNgkGJz TOonT/kdeKrCPzzoK+nXBthn9kCoE/5/sl9rSsPDk+3tY3VTRjtfn/SI9DdBMADyJQ3rA2be6xb c7mcqs5WXAQ8nf5pr6n3kSAUPAkIWz1e0cyzGOfA65WH8MSimEbgx8q50l3pV78QbVZVtza7S4+ 2oQaMR/pRlL4+n9rLpPhdiL0y735MxvUMY5mRW6EtqYhaz1anb/6zDP9qtlQ6/Z/hD/7TcyR X-Received: by 2002:a05:600c:34c5:b0:4bb:bf7e:e264 with SMTP id 5b1f17b1804b1-4bbbf7ee3c5mr100062845e9.2.1796021277736; Sun, 29 Nov 2026 22:47:57 -0800 (PST) Received: from localhost.localdomain ([193.255.169.14]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6b10b2c9sm64802439f8f.31.2026.11.29.22.47.57 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 29 Nov 2026 22:47:57 -0800 (PST) From: =?UTF-8?q?Bilal=20Yaz=C4=B1c=C4=B1o=C4=9Flu?= To: linux-bluetooth@vger.kernel.org Cc: Marcel Holtmann , Luiz Augusto von Dentz , linux-kernel@vger.kernel.org, =?UTF-8?q?Bilal=20Yaz=C4=B1c=C4=B1o=C4=9Flu?= Subject: [PATCH] Bluetooth: btusb: free skb and setup packet of deferred TX URBs Date: Mon, 30 Nov 2026 09:47:51 +0300 Message-ID: <20261130064751.50405-1-ahmetbilalyazicioglu@gmail.com> X-Mailer: git-send-email 2.56.0 X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While the adapter is runtime suspended, submit_or_queue_tx_urb() does not submit TX URBs but anchors them on data->deferred, so that play_deferred() submits them on resume. Until then the driver owns the skb in urb->context and the control request in urb->setup_packet, which are normally freed by the completion handlers. When these URBs are dropped instead of submitted, only the URB itself is freed: - btusb_close() and the failed path of btusb_resume() discard them with usb_scuttle_anchored_urbs(), leaking both the skb and the setup packet. - play_deferred() frees the setup packet but not the skb, both when usb_submit_urb() fails and for the URBs left on the anchor after that. On a ThinkPad T480 (Intel 8265, 8087:0a2b) this happens every time Bluetooth is switched off from the desktop while the adapter is autosuspended. thinkpad_acpi cuts power to the USB device, the HCI_Reset sent by btintel_shutdown_combined() gets deferred, the resume in btusb_close() fails and the queued command is dropped: usb 1-7: USB disconnect, device number 25 Bluetooth: hci0: HCI reset during shutdown failed Bluetooth: hci0: Error when powering off device on rfkill (-19) kmemleak reports three objects per toggle (skb head, skb data and the 8 byte setup packet, 880 bytes in total), allocated from hci_cmd_sync_alloc() and alloc_ctrl_urb(). Add btusb_free_deferred(), which frees the setup packet, the skb and the URB of every URB on the deferred anchor, and use it in btusb_close(), btusb_resume() and play_deferred(). Also free the skb when usb_submit_urb() fails in play_deferred(). Tested on a ThinkPad T480 with KASAN, kmemleak and lockdep enabled, switching Bluetooth off and on through blueman 10 times per run: 30 objects leaked without this patch and none with it, in two runs. There were no KASAN or lockdep reports, and suspend/resume and Wi-Fi rfkill kept working. Only the btusb_close() path was exercised; the btusb_resume() and play_deferred() changes are covered by code inspection only. I ran a debug kernel with kmemleak on my ThinkPad T480 together with Claude Code, which ran the tests and found the leak. It also helped with the analysis and this changelog. I wrote the fix and reviewed the analysis and the test results. Fixes: 7bee549e197c ("Bluetooth: Add USB autosuspend support to btusb driver") Fixes: 7b8e2c1db0f3 ("fix memory leak in fixed btusb_close") Fixes: 19cfe912c37b ("Bluetooth: btusb: Fix memory leak in play_deferred") Signed-off-by: Bilal Yazıcıoğlu --- drivers/bluetooth/btusb_main.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/drivers/bluetooth/btusb_main.c b/drivers/bluetooth/btusb_main.c index 95398e2ca..12396ed93 100644 --- a/drivers/bluetooth/btusb_main.c +++ b/drivers/bluetooth/btusb_main.c @@ -2122,6 +2122,18 @@ static void btusb_isoc_tx_complete(struct urb *urb) kfree_skb(skb); } +static void btusb_free_deferred(struct btusb_data *data) +{ + struct urb *urb; + + /* Deferred URBs were never submitted, so their completion won't run. */ + while ((urb = usb_get_from_anchor(&data->deferred))) { + kfree(urb->setup_packet); + kfree_skb(urb->context); + usb_free_urb(urb); + } +} + static int btusb_open(struct hci_dev *hdev) { struct btusb_data *data = hci_get_drvdata(hdev); @@ -2235,7 +2247,7 @@ static int btusb_close(struct hci_dev *hdev) usb_autopm_put_interface(data->intf); failed: - usb_scuttle_anchored_urbs(&data->deferred); + btusb_free_deferred(data); return 0; } @@ -5000,6 +5012,7 @@ static void play_deferred(struct btusb_data *data) BT_ERR("%s urb %p submission failed (%d)", data->hdev->name, urb, -err); kfree(urb->setup_packet); + kfree_skb(urb->context); usb_unanchor_urb(urb); usb_free_urb(urb); break; @@ -5010,10 +5023,7 @@ static void play_deferred(struct btusb_data *data) } /* Cleanup the rest deferred urbs. */ - while ((urb = usb_get_from_anchor(&data->deferred))) { - kfree(urb->setup_packet); - usb_free_urb(urb); - } + btusb_free_deferred(data); } static int btusb_resume(struct usb_interface *intf) @@ -5073,7 +5083,7 @@ static int btusb_resume(struct usb_interface *intf) return 0; failed: - usb_scuttle_anchored_urbs(&data->deferred); + btusb_free_deferred(data); done: spin_lock_irq(&data->txlock); clear_bit(BTUSB_SUSPENDING, &data->flags); -- 2.56.0