Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp1424193pxb; Fri, 10 Sep 2021 05:46:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwF+kbSfK/IIUjTI/cnglyA+cEqvyjF08oZpHT8SDux0Kx/2LXz4t7LpRfMmbu1YaPsV0A X-Received: by 2002:a05:6638:238c:: with SMTP id q12mr4389357jat.70.1631277985726; Fri, 10 Sep 2021 05:46:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631277985; cv=none; d=google.com; s=arc-20160816; b=ldUv0YST8y0bwRuzBOWDu+ZWu4/YOQRQFc+O97zUw9amjg34NCAEjERa3LfF+Z249D uQu32lmwJ2qkobtqk3tp023Ebx8h+0NU/iWwVZ4T97i1IzU8TNulpWKmluklJ2/6AOuu IJQvX/+TVvlDEhsTP68xXRy/0WZG4ZTN8BU5abaAbo2WyJ2yjYFLbpxruTfAlI+G11yD uaz/IlHY4uDAJCvLlIYC1NxNIOSKj80VwRjZ1Vu5OaBLfQejL/WZQdDK+KM/ThyeAHgd LZHJF9pYVvG8uEFtgDnl1Mx5ntAbJf/rJt1sOzEgPN0+MAv775cyq8RYnIII1eqJwO+9 EatQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=waWbLlTOp8thp1g0zvZ4SoMN8ai00d00qA9cnPCXD14=; b=TC80gErpx90AWob1Is1y5TPSJ9s1Rtyw+peVA4I+SGlA5Wnl7z8TaQT4Y2a3Qwb4is NMWSb8aQM4KXAeXJmm9l4sKui6u6rkqIZNYhJP1S+Jkx0qmAg+g4WKtfcTJ2xMfZsRF/ TIw2ETch6gkmROUKYtOCxwFLBxmhgEyIwuEHTONBgAEBfIJWpCNvMfu20vDNtWjv5ujm n8xdfMry6N/MVARu+RY0LyKum3ievVAfba00qQlUP0Y8lpL16mJqfQSOZTT3dQC8Rjvg XN7YsPQnzDrzqo7CDl0M7g4ckKMW5bsn1aql7sT2SzW0SGEAIn4vAtlBcpj5r/9Rh4AW zufw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ec5mXp8m; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c6si4361376ile.58.2021.09.10.05.46.13; Fri, 10 Sep 2021 05:46:25 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ec5mXp8m; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233473AbhIJMq2 (ORCPT + 99 others); Fri, 10 Sep 2021 08:46:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:41150 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233424AbhIJMq1 (ORCPT ); Fri, 10 Sep 2021 08:46:27 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 68D6160E94; Fri, 10 Sep 2021 12:45:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631277916; bh=u82foOMSBD2SO3pdy2Df3/FLyJdVMTCjSxcxbQ2H6Ss=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ec5mXp8myPUPpCCPqmnP/jTIcA49WxLNi2F+BEGPfUeGlOKRBx+v2ub/x7AEpb2Ry hQdc3DNWqyTbTZz6C0rmrKvvJvqgy9sBu6uXh3nbfSKjyMtWcMoAZrfdziym0kQ0b/ EyNFbDxzt+6dggmkyNoNmm8Na428uLchP6GMDdnSP0/i0DuG+EKGGL8IjuTyCVeNdf NJu1cuUdj5X9rLbRq/MaQJGdIv7xNp8dJbsPV8Ee/0GY28Oe8qBmKVXtCI6BwITS35 1pKmPd9qYi8+jqRnEBU7mW+yVF/fA5IOZwlzrkfP/4slw5gmCq2zkZWQar2GOk33J2 59Kin0IHNAriw== Received: from johan by xi.lan with local (Exim 4.94.2) (envelope-from ) id 1mOftz-0004zV-Bh; Fri, 10 Sep 2021 14:45:07 +0200 Date: Fri, 10 Sep 2021 14:45:07 +0200 From: Johan Hovold To: Zenghui Yu Cc: linux-scsi@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, fujita.tomonori@lab.ntt.co.jp, axboe@kernel.dk, martin.petersen@oracle.com, hch@lst.de, gregkh@linuxfoundation.org, wanghaibin.wang@huawei.com Subject: Re: [PATCH] scsi: bsg: Fix device unregistration Message-ID: References: <20210909034608.1435-1-yuzenghui@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210909034608.1435-1-yuzenghui@huawei.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 09, 2021 at 11:46:08AM +0800, Zenghui Yu wrote: > 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(-) > +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); > +} > @@ -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); Ehh, what about the blatant use-after-free and double-free you just added here? Martin, can this still be dropped from the scsi tree or does it need to be fixed incrementally? Johan