Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp165381pxb; Wed, 8 Sep 2021 20:48:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzIye3HrtDRFtZeyXxC66EtOP1l9V2XHYReYSKQKVf0+PV1jAKMF4/kfs/mpLmIJv2d1taG X-Received: by 2002:a02:a1c3:: with SMTP id o3mr938938jah.59.1631159295075; Wed, 08 Sep 2021 20:48:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631159295; cv=none; d=google.com; s=arc-20160816; b=NCJLQGYWj+row1gfJUZ9AFWkXGLC6mqx+5VLjxv4S+VWisdXwirGNvYKvrrmR3XKgB IfGMJTGnWKsMm6p+pzLu1Y+elO5Eoyho6gVVh9Klx8r8S9/Je2y19gAhzstFVoWSybgZ ZzDImiL13NZ4HyqpPZMZP9ukogumxtRdTMPBn4ziTMC2qV3CKeC+QYFYfoHmqYI+z/ul pf2oAX4H0AMRfpN+pBnYgCOa6F2av4hV2IBBlzk/Ah00uWwg1hR91UMBn/ALvhn4w4+S /+9vkmRJbXV0iiMZeJJb6CyyoHYr5iAxWJ68Ah+0apBTHxU3j93apJUpFfWfEinP78RV 1Dyw== 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=o0WDzcu7wFJcO7Jcc8W7I8Kwhii+o4bB+Da35YkJODw=; b=uugSozS8k+42F6LuG/ZYpcQ2viDL78xj5a5+SVZU7gUjicCwdUmtu5426iBGvtLNdp T5dYJV0H4ydnZJ8Dd/rxa3/KginhMbbUKUoRc+wxtIEwsvUh3vufulyntv3AsMRKSsfU GqOwkUITAuwNDZmCZx4SuaBbriYtKlgQjQyVp9uQs89It/FVaP+fTnI59eE7kH9p4J5+ b0WJehvQ8B576gggZNv3JoVY/LSgE0NaKpnVrLxCC7pspqpAcsy85+wa7ifDsq/e6HNU 5tNttncD98DVy1E2Iy/7wN4DnLLYXeEHTx2In/J18a496U2pAxURCpXv4kOAEkNIZwvL x8Eg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 f18si587683ilq.43.2021.09.08.20.48.02; Wed, 08 Sep 2021 20:48:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 S234634AbhIIDsA (ORCPT + 99 others); Wed, 8 Sep 2021 23:48:00 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:15311 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230153AbhIIDr7 (ORCPT ); Wed, 8 Sep 2021 23:47:59 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4H4lKk1ZSnz8t02; Thu, 9 Sep 2021 11:46:18 +0800 (CST) Received: from dggema764-chm.china.huawei.com (10.1.198.206) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2308.8; Thu, 9 Sep 2021 11:46:48 +0800 Received: from DESKTOP-8RFUVS3.china.huawei.com (10.174.185.179) by dggema764-chm.china.huawei.com (10.1.198.206) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2308.8; Thu, 9 Sep 2021 11:46:47 +0800 From: Zenghui Yu To: , , CC: , , , , , , Zenghui Yu Subject: [PATCH] scsi: bsg: Fix device unregistration Date: Thu, 9 Sep 2021 11:46:08 +0800 Message-ID: <20210909034608.1435-1-yuzenghui@huawei.com> X-Mailer: git-send-email 2.23.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.174.185.179] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggema764-chm.china.huawei.com (10.1.198.206) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We use device_initialize() to take refcount for the device but forget to put_device() on device teardown, which ends up leaking private data of the driver core, dev_name(), etc. This is reported by kmemleak at boot time if we compile kernel with DEBUG_TEST_DRIVER_REMOVE. Note that adding the missing put_device() is _not_ sufficient to fix device unregistration. As we don't provide the .release() method for device, which turned out to be typically wrong and will be complained loudly by the driver core. Fix both of them. Fixes: ead09dd3aed5 ("scsi: bsg: Simplify device registration") Signed-off-by: Zenghui Yu --- block/bsg.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/block/bsg.c b/block/bsg.c index 351095193788..c3bb92b9af7e 100644 --- a/block/bsg.c +++ b/block/bsg.c @@ -165,13 +165,20 @@ static const struct file_operations bsg_fops = { .llseek = default_llseek, }; +static void bsg_device_release(struct device *dev) +{ + struct bsg_device *bd = container_of(dev, struct bsg_device, device); + + ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt)); + kfree(bd); +} + void bsg_unregister_queue(struct bsg_device *bd) { if (bd->queue->kobj.sd) sysfs_remove_link(&bd->queue->kobj, "bsg"); cdev_device_del(&bd->cdev, &bd->device); - ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt)); - kfree(bd); + put_device(&bd->device); } EXPORT_SYMBOL_GPL(bsg_unregister_queue); @@ -198,6 +205,7 @@ struct bsg_device *bsg_register_queue(struct request_queue *q, bd->device.devt = MKDEV(bsg_major, ret); bd->device.class = bsg_class; bd->device.parent = parent; + bd->device.release = bsg_device_release; dev_set_name(&bd->device, "%s", name); device_initialize(&bd->device); @@ -218,6 +226,7 @@ struct bsg_device *bsg_register_queue(struct request_queue *q, out_device_del: cdev_device_del(&bd->cdev, &bd->device); out_ida_remove: + put_device(&bd->device); ida_simple_remove(&bsg_minor_ida, MINOR(bd->device.devt)); out_kfree: kfree(bd); -- 2.19.1