From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB45CC56202 for ; Thu, 26 Nov 2020 16:33:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 58E1B20B80 for ; Thu, 26 Nov 2020 16:33:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 58E1B20B80 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BC4F26B0070; Thu, 26 Nov 2020 11:33:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4F7C6B0071; Thu, 26 Nov 2020 11:33:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A157D6B0072; Thu, 26 Nov 2020 11:33:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0078.hostedemail.com [216.40.44.78]) by kanga.kvack.org (Postfix) with ESMTP id 8879C6B0070 for ; Thu, 26 Nov 2020 11:33:44 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 4F4601EE6 for ; Thu, 26 Nov 2020 16:33:44 +0000 (UTC) X-FDA: 77527115568.14.unit94_2f063a427380 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 28CA318229837 for ; Thu, 26 Nov 2020 16:33:44 +0000 (UTC) X-HE-Tag: unit94_2f063a427380 X-Filterd-Recvd-Size: 5353 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Thu, 26 Nov 2020 16:33:43 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 40B53ACC4; Thu, 26 Nov 2020 16:33:42 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id A9A321E10D0; Thu, 26 Nov 2020 17:33:41 +0100 (CET) Date: Thu, 26 Nov 2020 17:33:41 +0100 From: Jan Kara To: Christoph Hellwig Cc: Jens Axboe , Tejun Heo , Josef Bacik , Coly Li , Mike Snitzer , Greg Kroah-Hartman , Jan Kara , Johannes Thumshirn , dm-devel@redhat.com, Jan Kara , linux-block@vger.kernel.org, linux-bcache@vger.kernel.org, linux-mtd@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH 24/44] block: simplify bdev/disk lookup in blkdev_get Message-ID: <20201126163341.GL422@quack2.suse.cz> References: <20201126130422.92945-1-hch@lst.de> <20201126130422.92945-25-hch@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201126130422.92945-25-hch@lst.de> User-Agent: Mutt/1.10.1 (2018-07-13) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu 26-11-20 14:04:02, Christoph Hellwig wrote: > To simplify block device lookup and a few other upcoming areas, make sure > that we always have a struct block_device available for each disk and > each partition, and only find existing block devices in bdget. The only > downside of this is that each device and partition uses a little more > memory. The upside will be that a lot of code can be simplified. > > With that all we need to look up the block device is to lookup the inode > and do a few sanity checks on the gendisk, instead of the separate lookup > for the gendisk. For blk-cgroup which wants to access a gendisk without > opening it, a new blkdev_{get,put}_no_open low-level interface is added > to replace the previous get_gendisk use. > > Note that the change to look up block device directly instead of the two > step lookup using struct gendisk causes a subtile change in behavior: > accessing a non-existing partition on an existing block device can now > cause a call to request_module. That call is harmless, and in practice > no recent system will access these nodes as they aren't created by udev > and static /dev/ setups are unusual. > > Signed-off-by: Christoph Hellwig I still found some smaller issues (see below) but overall this looks sane and also simpler to review due to patch refactoring so thanks for that! > @@ -1384,7 +1376,7 @@ static int __blkdev_get(struct block_device *bdev, struct gendisk *disk, > struct block_device *whole = bdget_disk(disk, 0); > > mutex_lock_nested(&whole->bd_mutex, 1); > - ret = __blkdev_get(whole, disk, 0, mode); > + ret = __blkdev_get(whole, mode); > if (ret) { > mutex_unlock(&whole->bd_mutex); > bdput(whole); > @@ -1394,9 +1386,8 @@ static int __blkdev_get(struct block_device *bdev, struct gendisk *disk, > mutex_unlock(&whole->bd_mutex); > > bdev->bd_contains = whole; > - bdev->bd_part = disk_get_part(disk, partno); > - if (!(disk->flags & GENHD_FL_UP) || > - !bdev->bd_part || !bdev->bd_part->nr_sects) { > + bdev->bd_part = disk_get_part(disk, bdev->bd_partno); > + if (!bdev->bd_part || !bdev->bd_part->nr_sects) { AFAICT it is still possible that we see !(disk->flags & GENHD_FL_UP) here, isn't it? Is it safe to remove because the nr_sects check is already equivalent to it? Or something else? > __blkdev_put(whole, mode, 1); > bdput(whole); > ret = -ENXIO; > @@ -1426,12 +1417,51 @@ static int __blkdev_get(struct block_device *bdev, struct gendisk *disk, > > out_clear: > disk_put_part(bdev->bd_part); > - bdev->bd_disk = NULL; > bdev->bd_part = NULL; > bdev->bd_contains = NULL; > return ret; > } > > +struct block_device *blkdev_get_no_open(dev_t dev) > +{ > + struct block_device *bdev = bdget(dev); > + struct gendisk *disk; I think bdget() above needs to be already under bdev_lookup_sem. Otherwise disk_to_dev(bdev->bd_disk)->kobj below is a potential use-after-free. > + > + down_read(&bdev_lookup_sem); > + if (!bdev) { > + up_read(&bdev_lookup_sem); > + blk_request_module(dev); > + down_read(&bdev_lookup_sem); > + > + bdev = bdget(dev); > + if (!bdev) > + return NULL; Here you return with bdev_lookup_sem held. > + } > + > + disk = bdev->bd_disk; > + if (!kobject_get_unless_zero(&disk_to_dev(disk)->kobj)) > + goto bdput; > + if ((disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP) > + goto bdput; I think here you need to jump to put_disk. > + if (!try_module_get(bdev->bd_disk->fops->owner)) > + goto put_disk; > + up_read(&bdev_lookup_sem); > + return bdev; > +put_disk: > + put_disk(disk); > +bdput: > + bdput(bdev); > + up_read(&bdev_lookup_sem); > + return NULL; > +} Honza -- Jan Kara SUSE Labs, CR