Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2554541pxb; Tue, 12 Oct 2021 08:55:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwo9nmFvwAI+p92faHC54E93mK1ZwJVh4y2SiBlEUiUKmogcFp0WoUi9z4Uzg2U4jm1ifpd X-Received: by 2002:a63:7402:: with SMTP id p2mr23264464pgc.472.1634054119688; Tue, 12 Oct 2021 08:55:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634054119; cv=none; d=google.com; s=arc-20160816; b=R5VOz4pt7UCsgrIagsWOoRWyTl9NHxYHz4CWd6yZ4cdXCJDe9+Zb7akZX7wDgRsDDs MU/XQ71ThvelA9PQektkClcPVNR54e0lq6+cZ1znmYgZHxzQm0tyOagARsT4jIihfTr/ s2Qq3vnb/9rudHJZr3Ugvj/uf0kihxSpYJ2obr5gOn2QQZhpGx+pMq4uF/D/v15hLqN1 0f6fxFOCYSTHhmqHAbSDAvm5iAezaoacbA0/W4Sl6dJOBs/Yp+J+a3LAHQTAw8uNlWyG JGJA91w4VkuPF0VQK3khxHqarujbfxPLoN7mpnu+ji7/wrur3WSLKiAJL94URV331ejB Gt+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version; bh=DGXat5qU42QNJWbJ9k5KQOrSr4Bziy9H/SjAhkKqjCY=; b=EQts7w1NR2brRWfIaSbNpuDgh4d9tTgHEKiQfkORMJRW1mDASPxYZKvsDE1/FtjA9O lni9DOTgfuSgvumq+P9wqSeG5CHt60CMgEihtXBkFJBvhR3BFg5Rgc/3XE7eUtuyUeU2 4JN2AojM0oe/jPaZW9ExlVRvJhuNIXeLTFpmtd+VCYvzFb9xIOUJSdpyWsY2dP+f4XCW StngOLaAXhomqaTsRY98GrAAajHBhpf3XeT4ei0KSELCRs0WKYCj76w7VqF3JOBKTrnb fxaEPRnAwoB2+aEzFLKX8nYhsMvpZD5V/0xVz6jticT+YNhRdhx2Zfd3X6sNcKCW02wV pEtQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q5si21120530pgg.65.2021.10.12.08.55.02; Tue, 12 Oct 2021 08:55:19 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236864AbhJLP4f convert rfc822-to-8bit (ORCPT + 99 others); Tue, 12 Oct 2021 11:56:35 -0400 Received: from coyote.holtmann.net ([212.227.132.17]:41950 "EHLO mail.holtmann.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234892AbhJLP4f (ORCPT ); Tue, 12 Oct 2021 11:56:35 -0400 Received: from smtpclient.apple (p4fefcb73.dip0.t-ipconnect.de [79.239.203.115]) by mail.holtmann.org (Postfix) with ESMTPSA id 9DA0ACECE1; Tue, 12 Oct 2021 17:54:32 +0200 (CEST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: [PATCH] Bluetooth: Fix memory leak of hci device From: Marcel Holtmann In-Reply-To: <20211012075634.8041-1-weiyongjun1@huawei.com> Date: Tue, 12 Oct 2021 17:54:32 +0200 Cc: linux-bluetooth , Johan Hedberg , Luiz Augusto von Dentz Content-Transfer-Encoding: 8BIT Message-Id: <3EC78E44-6E60-4B99-B880-2F5CC468C424@holtmann.org> References: <20211012075634.8041-1-weiyongjun1@huawei.com> To: Wei Yongjun X-Mailer: Apple Mail (2.3654.120.0.1.13) Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi Wei, > 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 kfree(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 need to set hdev HCI_UNREGISTER flag > in hci_alloc_dev_priv(). > > Signed-off-by: Wei Yongjun > > diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c > index 8a47a3017d61..42410f568e90 100644 > --- a/net/bluetooth/hci_core.c > +++ b/net/bluetooth/hci_core.c > @@ -3876,6 +3876,11 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) > INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout); > INIT_DELAYED_WORK(&hdev->ncmd_timer, hci_ncmd_timeout); > > + /* We need to set HCI_UNREGISTER flag to correctly release > + * the device in hci_free_dev() > + */ > + hci_dev_set_flag(hdev, HCI_UNREGISTER); > + I can see the point in the bug report, but I don’t see that the fix is correct. Can you prove that this fix is correct when hci_register_dev is actually called. Regards Marcel