Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3236599pxb; Wed, 13 Oct 2021 01:43:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMFe0XmQXcVlmHzHytqIWF2KsEWpJFl/WG+Zb9hgMWkg+J/2PhZFhVHBsCWg0gc5tqfOMn X-Received: by 2002:a17:90b:1102:: with SMTP id gi2mr11851042pjb.43.1634114592389; Wed, 13 Oct 2021 01:43:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634114592; cv=none; d=google.com; s=arc-20160816; b=U8zYvTkh5HhC/ZaG1jayMj8H/Xr/tcCqCzzBUVIrVTJ41sJIU1CDk5X9lB4EMQmw05 xBYCR6TR6pwqy+zgRKbxeciAzsYnmb32QyDSITrwXEB+Z4jzg2BwvsJ0utmtFGT/ABqK Xk+TtYZBJhu9OVdYSCyHsSMQuC/0VOJbpnn/7O/tovO45s8JIWcB5H8tSv9TCgKo+r6c aavwcE11O4bPcF08+8md2xnoHAKkGp0lW8VR6ce+DS4n8FQILui4CUuBtCCloKwq3cge 7BP3ljwma2IJx4ReS/gGWK+WxpZ2viba4ZGea/7keMZGuYY4i+7mz8+OhePS0gnwffc9 vIqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=2rfphd2BHM322XjJ/BxAUHiL0vp0hWcQ+1hBHUvPEdY=; b=MSXHNkHhl1VYioF4aE9LAyrGTTunIcBU2XJHoxN0wecpCPw1A5O0VJzmc8wO/FnMUr BmhSSjKlMkjyRt9g+hvToLf0sMhImysni3OASl6LbQHXyQ01rZJpN1V1jbOyYTfTnexu UK640vJcBenscDdpXqA3Cquizp1Rw8BeuwXSN8Hxe+Vu1cgzunJXNxSgwfjG0pnUKID9 oTVvtELGI71REaSiKW28y0ui1P8oXp414zWLJ8AufNXAmS/mTz+KzZOLxfkLeDb6pKzT 2FqFG4yzshtTswGcaGF3/05/XeYl+HgSFrdXu4tc7xit396v5dHusLZO3v5seh/+ojO2 wNig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u3si17609108plh.445.2021.10.13.01.42.28; Wed, 13 Oct 2021 01:43:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229644AbhJMIn3 (ORCPT + 99 others); Wed, 13 Oct 2021 04:43:29 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:14343 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235764AbhJMIn2 (ORCPT ); Wed, 13 Oct 2021 04:43:28 -0400 Received: from dggeml709-chm.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4HTm8y50qlz900t; Wed, 13 Oct 2021 16:36:34 +0800 (CST) Received: from localhost.localdomain.localdomain (10.175.113.25) by dggeml709-chm.china.huawei.com (10.3.17.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.8; Wed, 13 Oct 2021 16:41:24 +0800 From: Wei Yongjun To: CC: Wei Yongjun , Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz Subject: [PATCH v2] Bluetooth: Fix memory leak of hci device Date: Wed, 13 Oct 2021 16:55:01 +0800 Message-ID: <20211013085501.101286-1-weiyongjun1@huawei.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.113.25] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggeml709-chm.china.huawei.com (10.3.17.139) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Fault injection test reported memory leak of hci device as follows: unreferenced object 0xffff88800b858000 (size 8192): comm "kworker/0:2", pid 167, jiffies 4294955747 (age 557.148s) hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 ad 4e ad de .............N.. backtrace: [<0000000070eb1059>] kmem_cache_alloc_trace mm/slub.c:3208 [<00000000015eb521>] hci_alloc_dev_priv include/linux/slab.h:591 [<00000000dcfc1e21>] bpa10x_probe include/net/bluetooth/hci_core.h:1240 [<000000005d3028c7>] usb_probe_interface drivers/usb/core/driver.c:397 [<00000000cbac9243>] really_probe drivers/base/dd.c:517 [<0000000024cab3f0>] __driver_probe_device drivers/base/dd.c:751 [<00000000202135cb>] driver_probe_device drivers/base/dd.c:782 [<000000000761f2bc>] __device_attach_driver drivers/base/dd.c:899 [<00000000f7d63134>] bus_for_each_drv drivers/base/bus.c:427 [<00000000c9551f0b>] __device_attach drivers/base/dd.c:971 [<000000007f79bd16>] bus_probe_device drivers/base/bus.c:487 [<000000007bb8b95a>] device_add drivers/base/core.c:3364 [<000000009564d9ea>] usb_set_configuration drivers/usb/core/message.c:2171 [<00000000e4657087>] usb_generic_driver_probe drivers/usb/core/generic.c:239 [<0000000071ede518>] usb_probe_device drivers/usb/core/driver.c:294 [<00000000cbac9243>] really_probe drivers/base/dd.c:517 hci_alloc_dev() do not init the device's flag. And hci_free_dev() using put_device() to free the memory allocated for this device, but it calls just put_device(dev) only in case of HCI_UNREGISTER flag is set, So any error handing before hci_register_dev() success will cause memory leak. To avoid this behaviour we can using kfree() to release dev before hci_register_dev() success. Signed-off-by: Wei Yongjun diff --git a/net/bluetooth/hci_sysfs.c b/net/bluetooth/hci_sysfs.c index 7827639ecf5c..4e3e0451b08c 100644 --- a/net/bluetooth/hci_sysfs.c +++ b/net/bluetooth/hci_sysfs.c @@ -86,6 +86,8 @@ static void bt_host_release(struct device *dev) if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) hci_release_dev(hdev); + else + kfree(hdev); module_put(THIS_MODULE); } -- 2.25.1