From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796021279; b=Q571b2b4kNYe53mSb/ouyNRSmlDPqfacPXgLMnaj/qf3hUHcwuVghzIOEgk0QiC1X9i1 tPDcr4sgfsd3nu++efVUI0X11m4ATOPGmWT4jHkuCbYUPCk9r7tLby4AjUPtBQ9VcEKl4 ZmzW40tcFYDWFIdo6H0hI3I5cqdfutKjyUSrrxNpnVipD7+84FXjFdyGIYfJ+deciIisv kayUsR6gtYHmuuGmp+RaGvuilJi5QMgjmeu3cQqxj7fZdpG9R0Ju78ygm571UmrMqk0ko VNj+dxCHvT0DfUVD0nRf48rpXaAtSR02xhDl+f1JTrkDLFgND9yEj2CqDezf37xq5YQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796021279; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted: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=LbXXNhnDp19Zdr8FikG9rQ2mBJVXvyW59RsHwN7MjjwSwGaOvnI68Pp8uoNgjHgkdBfS hgSblwuaKhiDjPQDKIqP/zqyJF9Z1LmgFiBkDGYD/8Z42WsOgt9gf8/gqWrC6tKYQrslK VnExOmQ+1vpIAKUNOkJKUR3YbBWsR0TnZmAshkQfACHOVo0/1bjOlNq4SYGewK4vkd8jp ytB9sbbU635/ZeFRg9Q4rh5JhfEzl/51FugGcWY3365k14A4yNqH4fe0F93CMXxDDFU63 IkuKLBj8cIGp5prF3c8/pTXQzv2zY4nMlt8IuHg6h+d86lG3r1tByEmv5MsOzXFjUhw== 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.54 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 7CB44606A31C for ; Mon, 30 Nov 2026 06:47:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7CB44606A31C 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-f54.google.com with SMTP id 5b1f17b1804b1-4bb72b6e1b2so8340015e9.2 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=M9HrKqwMBs3T0BkesGELnaL1a6D7d0YrCwP1jpa7luzlLflNrfuhSgMiS8yZ8LNtj0 j8SZAJEncuPHtStitDbDucv7QKNF/FPSZB8ysOJKlBMNg3wT9+sfotngW2s+nD1I+Awn 9j6Ubs8d0cWBOw6K8A80GlntC8guw6qJlavUX1Aq96XNggko0LwK61ubR4FtUy6COHZ+ XEsWi0hLYFM3qeODepMJoGKxg/ubAxl8p0hzXdFvpxh0VEqZebDT9hAY7cAnkHx0+txO G4nRj0FE8ChqJpUBZsmR5JPqEr1W1yBSGynVqwRIWdy0ICiUXJ1AuWPTYOQbXcrwZyKO NnKA== X-Forwarded-Encrypted: i=1; AL+hErFlCWVoDEbSFYezKxs8CBsxoO6ClE7k1v+zwdHv6s1bBqAknd3NkC8A/WCu1FGg8K3+FlMh5fqAypC9DkE=@vger.kernel.org X-Gm-Message-State: APuU4Z9dbP9G2yxk0FPFLeS1QAgky0NzT7D2hPX97xJhLzMjXcQMXiHl OP6oLWwaai2IkBXsJy+s6c25nMAZxfxVq0QmkRsZZkrCLfXl+8gdBjzf X-Gm-Gg: AZFdTZc2IFYc42DmzPJQeCp88Qhb0Nc5dt0OCMxcbceG2Th7v5Ds1j+kW4XdbKmnUTu xaJJnMX/yK0S1PORE55d3bBwvBtU+SBfrLyUyHFaEQMAnwF96bYwrGfkGnYflekyVNIYVWs/j3l RK9s/AgOZPeh43p1k9vzHc/3GZdfX4a/dXPVdXmsibVUhFHZAOpgYQ4tQwG1FTTR1lDhOmqTe1W pchthPb71ufNd0rJMgxKwSmbBvdWPoQ+jfXJxOp/Ml1Db8lpfPmglV84xFbeFOqX4nEz7oJb5Rs et6L9fy5xmtkvDPSMmSivCoFUJbSolxBpoqspzdKpNBiVgfQBwIncncdHPJYjdv9Hh161ee1mVH mIfvSo3y1naHK+owm/01GFgerF9dTDYLBLZPSl8fpHnVX0vMD2FaRtJcyRL8fRKtmU/6EvXkQCq VWQqRIpwBvIDef9nBiCW3Ly7M1A9B+pIylU6AUKlVHJXWj5Vo9ENtPZXJYBCFywdQYmgPNjx7XU HkQNFqUCrX8EhoXc3zQFGg8Nf/UzeVeY5eG2igTI544AHQxmVdon02oCP2XYJTFJePoOWHK7MDO ZSp8SZiEmAMT+nWKhegdoTYsrlM5BzlUAipJM8nnq2s2401vCb8p6oRthbODIcC25XRr4Vo2 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-kernel@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