From patchwork Sun Oct 30 15:31:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13025095 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5952BFA3742 for ; Sun, 30 Oct 2022 15:31:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229587AbiJ3Pbv (ORCPT ); Sun, 30 Oct 2022 11:31:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbiJ3Pbu (ORCPT ); Sun, 30 Oct 2022 11:31:50 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A89960D0 for ; Sun, 30 Oct 2022 08:31:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=EFXUQkOni9cxO7luloI3Uc+qTKEe2mxoeIaJyoyA+KI=; b=mYsnIb/vIvKUMPPpC+qPOk1xz8 3BF9sX4dOSriQetS9eLQ5CFQtZUap+bgT2rkB5E0BnxkfwO0pxykV08tmknLMQCKQiPTbmsesOK/r E8sxnGsRfus8SXIwtXBPVfY3xyHjCQVnnilBYSprfesGEO5jQu0ejuYoWgAWwh+Eiw6TnwpfH78uI Yi3mYzRQsKfxTLzqyAm77WV6Liydqct8AzzHYCYKrmKxIrUD8sExKQNkYbqpEX0k6YI0PJzvV7rVb biQ3K+BeTXsx0/dHig3UodLeWaSTgALKExIolzrTHvsKfr4G0AZECRE+jq74sdWHSlHSSviZd5SM1 w5xbYjmA==; Received: from 213-225-37-80.nat.highway.a1.net ([213.225.37.80] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1opAHg-00HVkO-Tg; Sun, 30 Oct 2022 15:31:37 +0000 From: Christoph Hellwig To: Jens Axboe , Alasdair Kergon , Mike Snitzer Cc: Yu Kuai , dm-devel@redhat.com, linux-block@vger.kernel.org Subject: [PATCH 1/7] block: clear ->slave_dir when dropping the main slave_dir reference Date: Sun, 30 Oct 2022 16:31:13 +0100 Message-Id: <20221030153120.1045101-2-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221030153120.1045101-1-hch@lst.de> References: <20221030153120.1045101-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Zero out the pointer to ->slave_dir so that the holder code doesn't incorrectly treat the object as alive when add_disk failed or after del_gendisk was called. Fixes: 89f871af1b26 ("dm: delay registering the gendisk") Reported-by: Yu Kuai Signed-off-by: Christoph Hellwig --- block/genhd.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/block/genhd.c b/block/genhd.c index 17b33c62423df..aa0e2f5684543 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -528,6 +528,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk, blk_unregister_queue(disk); out_put_slave_dir: kobject_put(disk->slave_dir); + disk->slave_dir = NULL; out_put_holder_dir: kobject_put(disk->part0->bd_holder_dir); out_del_integrity: @@ -624,6 +625,7 @@ void del_gendisk(struct gendisk *disk) kobject_put(disk->part0->bd_holder_dir); kobject_put(disk->slave_dir); + disk->slave_dir = NULL; part_stat_set_all(disk->part0, 0); disk->part0->bd_stamp = 0; From patchwork Sun Oct 30 15:31:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13025094 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F133FC38A02 for ; Sun, 30 Oct 2022 15:31:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229781AbiJ3Pbv (ORCPT ); Sun, 30 Oct 2022 11:31:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229587AbiJ3Pbu (ORCPT ); Sun, 30 Oct 2022 11:31:50 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A8E9631C for ; Sun, 30 Oct 2022 08:31:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=zTDppyVAqgjt6x8MHfxYB0HakgzAN80FZOa1iFX34qw=; b=WkZHc3aAk6o0aRz5NSfVrylj0J 3mVSnH+JbxOEdMseCAUOW5TgOQ7HNxWogsjy0RBneiWt78qc2dP7V9eCrhLD8sL/UEqJuHsXzkOqr nx9phXnCq5UDR3oz7nhpBIgOAosY+dHeCCJKZZRltd4sH3xtdXmgo2X6pxkdbXK3mN3TbRzUXcGan PRzyv8mYGwPerJIeKWtXfSoAk+D2hi28xthlhQaF3No9YskEg1H0IH6GdLZN4MWSzpkkviod8vZWS 3X0eumy7ZWLDcuawt2VRnhuY8YRyEwMtHAcYp7PeUU5QdYsvHA5N56Z8JlYdjB7D3Op6EPN93x6lp Wddi5J3w==; Received: from 213-225-37-80.nat.highway.a1.net ([213.225.37.80] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1opAHk-00HVnk-8O; Sun, 30 Oct 2022 15:31:40 +0000 From: Christoph Hellwig To: Jens Axboe , Alasdair Kergon , Mike Snitzer Cc: Yu Kuai , dm-devel@redhat.com, linux-block@vger.kernel.org Subject: [PATCH 2/7] dm: remove free_table_devices Date: Sun, 30 Oct 2022 16:31:14 +0100 Message-Id: <20221030153120.1045101-3-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221030153120.1045101-1-hch@lst.de> References: <20221030153120.1045101-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org free_table_devices just warns and frees all table_device structures when the target removal did not remove them. This should never happen, but if it did, just freeing the structure without deleting them from the list or cleaning up the resources would not help at all. So just WARN on a non-empty list instead. Signed-off-by: Christoph Hellwig --- drivers/md/dm.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 95a1ee3d314eb..19d25bf997be4 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -833,19 +833,6 @@ void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) mutex_unlock(&md->table_devices_lock); } -static void free_table_devices(struct list_head *devices) -{ - struct list_head *tmp, *next; - - list_for_each_safe(tmp, next, devices) { - struct table_device *td = list_entry(tmp, struct table_device, list); - - DMWARN("dm_destroy: %s still exists with %d references", - td->dm_dev.name, refcount_read(&td->count)); - kfree(td); - } -} - /* * Get the geometry associated with a dm device */ @@ -2122,7 +2109,7 @@ static void free_dev(struct mapped_device *md) cleanup_mapped_device(md); - free_table_devices(&md->table_devices); + WARN_ON_ONCE(!list_empty(&md->table_devices)); dm_stats_cleanup(&md->stats); free_minor(minor); From patchwork Sun Oct 30 15:31:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13025096 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 725C2FA3744 for ; Sun, 30 Oct 2022 15:31:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229716AbiJ3Pbx (ORCPT ); Sun, 30 Oct 2022 11:31:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229743AbiJ3Pbv (ORCPT ); Sun, 30 Oct 2022 11:31:51 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A9D2B1D4 for ; Sun, 30 Oct 2022 08:31:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=5KHn8d9R5f3drCTarOv9/68DXszAsgbvoQJ5QGA/2/k=; b=gaQNXlPpPYZIGxp1Jr9OXSztjS QsELoRRaGI93MVcJMJTC73mImeFoVlDwTajVpF02plfeqRGzV8ytDvDxkr1Wb2wkaFb4HSEFWI3Cr Z5ofn/4gb0e0peOBzyeEgbSco4Kf780m0l8G7AKK+SZf1wOx3xqRsyuEyrVQl5E+IvjV/M4xbMJsm A2YEklnSilMCK5eWdx/GF8n6xIBUuSyndFgeoftDF6DbTiuw3EKtkrl+qCwydiVNAcvR0FVwRMxXc 1f3VwdLKy9tSZNWgFjzCj1DbdLkhdLoQ4r+eq8Z+lQ98JQ/wKnecV7h+28rsmyFVrpyQMKQ+YfbhC 1RejSPgA==; Received: from 213-225-37-80.nat.highway.a1.net ([213.225.37.80] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1opAHn-00HVrM-58; Sun, 30 Oct 2022 15:31:43 +0000 From: Christoph Hellwig To: Jens Axboe , Alasdair Kergon , Mike Snitzer Cc: Yu Kuai , dm-devel@redhat.com, linux-block@vger.kernel.org Subject: [PATCH 3/7] dm: cleanup open_table_device Date: Sun, 30 Oct 2022 16:31:15 +0100 Message-Id: <20221030153120.1045101-4-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221030153120.1045101-1-hch@lst.de> References: <20221030153120.1045101-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Move all the logic for allocation the table_device and linking it into the list into the open_table_device. This keeps the code tidy and ensures that the table_devices only exist in fully initialized state. Signed-off-by: Christoph Hellwig --- drivers/md/dm.c | 56 ++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 19d25bf997be4..28d7581b6a826 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -732,28 +732,41 @@ static char *_dm_claim_ptr = "I belong to device-mapper"; /* * Open a table device so we can use it as a map destination. */ -static int open_table_device(struct table_device *td, dev_t dev, - struct mapped_device *md) +static struct table_device *open_table_device(struct mapped_device *md, + dev_t dev, fmode_t mode) { + struct table_device *td; struct block_device *bdev; u64 part_off; int r; - BUG_ON(td->dm_dev.bdev); + td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); + if (!td) + return ERR_PTR(-ENOMEM); + refcount_set(&td->count, 1); - bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr); - if (IS_ERR(bdev)) - return PTR_ERR(bdev); + bdev = blkdev_get_by_dev(dev, mode | FMODE_EXCL, _dm_claim_ptr); + if (IS_ERR(bdev)) { + r = PTR_ERR(bdev); + goto out_free_td; + } r = bd_link_disk_holder(bdev, dm_disk(md)); - if (r) { - blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL); - return r; - } + if (r) + goto out_blkdev_put; + td->dm_dev.mode = mode; td->dm_dev.bdev = bdev; td->dm_dev.dax_dev = fs_dax_get_by_bdev(bdev, &part_off, NULL, NULL); - return 0; + format_dev_t(td->dm_dev.name, dev); + list_add(&td->list, &md->table_devices); + return td; + +out_blkdev_put: + blkdev_put(bdev, mode | FMODE_EXCL); +out_free_td: + kfree(td); + return ERR_PTR(r); } /* @@ -786,31 +799,16 @@ static struct table_device *find_table_device(struct list_head *l, dev_t dev, int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode, struct dm_dev **result) { - int r; struct table_device *td; mutex_lock(&md->table_devices_lock); td = find_table_device(&md->table_devices, dev, mode); if (!td) { - td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id); - if (!td) { - mutex_unlock(&md->table_devices_lock); - return -ENOMEM; - } - - td->dm_dev.mode = mode; - td->dm_dev.bdev = NULL; - - if ((r = open_table_device(td, dev, md))) { + td = open_table_device(md, dev, mode); + if (IS_ERR(td)) { mutex_unlock(&md->table_devices_lock); - kfree(td); - return r; + return PTR_ERR(td); } - - format_dev_t(td->dm_dev.name, dev); - - refcount_set(&td->count, 1); - list_add(&td->list, &md->table_devices); } else { refcount_inc(&td->count); } From patchwork Sun Oct 30 15:31:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13025097 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7FC2FA3745 for ; Sun, 30 Oct 2022 15:31:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229782AbiJ3Pby (ORCPT ); Sun, 30 Oct 2022 11:31:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229783AbiJ3Pbx (ORCPT ); Sun, 30 Oct 2022 11:31:53 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA5E5631C for ; Sun, 30 Oct 2022 08:31:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=NiJnntTQNL/iuGMrxvWwXfMZgNi9d1s0llRGCRimOz4=; b=jOyqM9JD2PM3ouAIgTWkiieguP rhIxjjCIvz7pwTr7LK5R8CRILojXvNtI2+L7zzBO9iDuygs0o6aD0yVhZL1rGG068WVL8PLqYT0c5 nx/CSI4x4P9K5bIWyDRC2fPk4huJDgxSt4izSNKoZT5nYbK762oWSj2PgPdK+IKSj/qU3B58Q+PuL uh45/AW94rmTES0KNsEXq+vPgWcSgVRP35DILxmi3KykM1Oz6/w2+egN7N5964vEED28D95MTgyPf Ts/RFrL1QzCbKm2srMuKt2hg4yQnlbsekICF6HhDwobb33VRhB2gis6iMrXNBaofwyiLWNucwhfWD 3FG93BYQ==; Received: from 213-225-37-80.nat.highway.a1.net ([213.225.37.80] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1opAHq-00HVtq-9o; Sun, 30 Oct 2022 15:31:46 +0000 From: Christoph Hellwig To: Jens Axboe , Alasdair Kergon , Mike Snitzer Cc: Yu Kuai , dm-devel@redhat.com, linux-block@vger.kernel.org Subject: [PATCH 4/7] dm: cleanup close_table_device Date: Sun, 30 Oct 2022 16:31:16 +0100 Message-Id: <20221030153120.1045101-5-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221030153120.1045101-1-hch@lst.de> References: <20221030153120.1045101-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Take the list unlink and free into close_table_device so that no half torn down table_devices exist. Also remove the check for a NULL bdev as that can't happen - open_table_device never adds a table_device to the list that does not have a valid block_device. Signed-off-by: Christoph Hellwig --- drivers/md/dm.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 28d7581b6a826..2917700b1e15c 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -774,14 +774,11 @@ static struct table_device *open_table_device(struct mapped_device *md, */ static void close_table_device(struct table_device *td, struct mapped_device *md) { - if (!td->dm_dev.bdev) - return; - bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); put_dax(td->dm_dev.dax_dev); - td->dm_dev.bdev = NULL; - td->dm_dev.dax_dev = NULL; + list_del(&td->list); + kfree(td); } static struct table_device *find_table_device(struct list_head *l, dev_t dev, @@ -823,11 +820,8 @@ void dm_put_table_device(struct mapped_device *md, struct dm_dev *d) struct table_device *td = container_of(d, struct table_device, dm_dev); mutex_lock(&md->table_devices_lock); - if (refcount_dec_and_test(&td->count)) { + if (refcount_dec_and_test(&td->count)) close_table_device(td, md); - list_del(&td->list); - kfree(td); - } mutex_unlock(&md->table_devices_lock); } From patchwork Sun Oct 30 15:31:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13025098 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B0DAC38A02 for ; Sun, 30 Oct 2022 15:31:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229786AbiJ3Pb6 (ORCPT ); Sun, 30 Oct 2022 11:31:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229743AbiJ3Pb5 (ORCPT ); Sun, 30 Oct 2022 11:31:57 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DECC1B1D4 for ; Sun, 30 Oct 2022 08:31:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=uuYC3KQ/Ip3wZhufBDNGLln2QHFIqhWw6L9MkLncVtM=; b=viCfgK3v6sR9n/ASfPJxi3g3bm T2AePtGXnL/vfyhnqXyojdCbwLGc9xVaCn+RLSFg+BM0HSVAEjWffYfkAA1PeR6EwDKN/Cc3wT9+J rGJ7sWgqt2tYElWudGAO+D5ax61LzM6k5EncNRc2hHSeOIbHqEjdtmWq59u0bSQuZHq5+O2WHyVNG Yq5O0QAIkFuQQ+xdOd2QjL4GzoJ5itEcYVSrdHShwLk1o2tNdqOQtL5pV4QKA0+AK51V8T80+fc4J CCJ9H+ZrPmCLanyi82JgyDMKyRxi7NtW+deo2GXHhs23DE0JXylVsWNrYX7+B3zyEURbKmdGdeBQV nyKZIsLg==; Received: from 213-225-37-80.nat.highway.a1.net ([213.225.37.80] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1opAHt-00HVxu-9m; Sun, 30 Oct 2022 15:31:49 +0000 From: Christoph Hellwig To: Jens Axboe , Alasdair Kergon , Mike Snitzer Cc: Yu Kuai , dm-devel@redhat.com, linux-block@vger.kernel.org Subject: [PATCH 5/7] dm: track per-add_disk holder relations in DM Date: Sun, 30 Oct 2022 16:31:17 +0100 Message-Id: <20221030153120.1045101-6-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221030153120.1045101-1-hch@lst.de> References: <20221030153120.1045101-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org dm is a bit special in that it opens the underlying devices. Commit 89f871af1b26 ("dm: delay registering the gendisk") tried to accomodate that by allowing to add the holder to the list before add_gendisk and then just add them to sysfs once add_disk is called. But that leads to really odd lifetime problems and error handling problems as we can't know the state of the kobjects and don't unwind properly. To fix this switch to just registering all existing table_devices with the holder code right after add_disk, and remove them before calling del_gendisk. Fixes: 89f871af1b26 ("dm: delay registering the gendisk") Reported-by: Yu Kuai Signed-off-by: Christoph Hellwig --- drivers/md/dm.c | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 2917700b1e15c..7b0d6dc957549 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -751,9 +751,16 @@ static struct table_device *open_table_device(struct mapped_device *md, goto out_free_td; } - r = bd_link_disk_holder(bdev, dm_disk(md)); - if (r) - goto out_blkdev_put; + /* + * We can be called before the dm disk is added. In that case we can't + * register the holder relation here. It will be done once add_disk was + * called. + */ + if (md->disk->slave_dir) { + r = bd_link_disk_holder(bdev, md->disk); + if (r) + goto out_blkdev_put; + } td->dm_dev.mode = mode; td->dm_dev.bdev = bdev; @@ -774,7 +781,8 @@ static struct table_device *open_table_device(struct mapped_device *md, */ static void close_table_device(struct table_device *td, struct mapped_device *md) { - bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); + if (md->disk->slave_dir) + bd_unlink_disk_holder(td->dm_dev.bdev, md->disk); blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); put_dax(td->dm_dev.dax_dev); list_del(&td->list); @@ -1951,7 +1959,13 @@ static void cleanup_mapped_device(struct mapped_device *md) md->disk->private_data = NULL; spin_unlock(&_minor_lock); if (dm_get_md_type(md) != DM_TYPE_NONE) { + struct table_device *td; + dm_sysfs_exit(md); + list_for_each_entry(td, &md->table_devices, list) { + bd_unlink_disk_holder(td->dm_dev.bdev, + md->disk); + } del_gendisk(md->disk); } dm_queue_destroy_crypto_profile(md->queue); @@ -2284,6 +2298,7 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) { enum dm_queue_mode type = dm_table_get_type(t); struct queue_limits limits; + struct table_device *td; int r; switch (type) { @@ -2316,13 +2331,27 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) if (r) return r; - r = dm_sysfs_init(md); - if (r) { - del_gendisk(md->disk); - return r; + /* + * Register the holder relationship for devices added before the disk + * was live. + */ + list_for_each_entry(td, &md->table_devices, list) { + r = bd_link_disk_holder(td->dm_dev.bdev, md->disk); + if (r) + goto out_undo_holders; } + + r = dm_sysfs_init(md); + if (r) + goto out_undo_holders; md->type = type; return 0; + +out_undo_holders: + list_for_each_entry_continue_reverse(td, &md->table_devices, list) + bd_unlink_disk_holder(td->dm_dev.bdev, md->disk); + del_gendisk(md->disk); + return r; } struct mapped_device *dm_get_md(dev_t dev) From patchwork Sun Oct 30 15:31:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13025099 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF75EC38A02 for ; Sun, 30 Oct 2022 15:32:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229743AbiJ3PcB (ORCPT ); Sun, 30 Oct 2022 11:32:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229783AbiJ3PcA (ORCPT ); Sun, 30 Oct 2022 11:32:00 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83707B1D8 for ; Sun, 30 Oct 2022 08:31:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=QEkMe4yA7BRgDyHIScWawW3DXudnaQ0F4P9Wl9dPXa0=; b=xN7NWFnxxU9dHiwUnai+//9w/5 P+71cotmzpZdmKkTmhIV/4Id+mRA0iD8kjaOITLM/zl0qGrFMorYopMQtvY9Av4KJi6jkwWErrdl0 uUxR5FSti1zW8elflnuQ1KqOCSsz44gW0nobPzWeUqJjKjfTMOjnwQNgHAUrViyepntGf4kCifPxy UDJR8818574N8F3dXoae8vAz47QUSWHd6yImLYnSwhF9IUlt31xKp9ZjwTMqF6xMBlrGPosXx0oes IaHLn0rPnFq3Hf0+Knqu9hME86OVjx5yT6oZy32Z29te1YgcZozZgDC6uIPWBqxkxqpRU8ENXz22L OjwiSPnw==; Received: from 213-225-37-80.nat.highway.a1.net ([213.225.37.80] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1opAHw-00HW0v-8r; Sun, 30 Oct 2022 15:31:52 +0000 From: Christoph Hellwig To: Jens Axboe , Alasdair Kergon , Mike Snitzer Cc: Yu Kuai , dm-devel@redhat.com, linux-block@vger.kernel.org Subject: [PATCH 6/7] block: remove delayed holder registration Date: Sun, 30 Oct 2022 16:31:18 +0100 Message-Id: <20221030153120.1045101-7-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221030153120.1045101-1-hch@lst.de> References: <20221030153120.1045101-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Now that dm has been fixed to track of holder registrations before add_disk, the somewhat buggy block layer code can be safely removed. Signed-off-by: Christoph Hellwig --- block/genhd.c | 4 --- block/holder.c | 72 ++++++++++++------------------------------ include/linux/blkdev.h | 5 --- 3 files changed, 21 insertions(+), 60 deletions(-) diff --git a/block/genhd.c b/block/genhd.c index aa0e2f5684543..97d2243f07827 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -478,10 +478,6 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk, goto out_put_holder_dir; } - ret = bd_register_pending_holders(disk); - if (ret < 0) - goto out_put_slave_dir; - ret = blk_register_queue(disk); if (ret) goto out_put_slave_dir; diff --git a/block/holder.c b/block/holder.c index 5283bc804cc14..dd9327b43ce05 100644 --- a/block/holder.c +++ b/block/holder.c @@ -29,19 +29,6 @@ static void del_symlink(struct kobject *from, struct kobject *to) sysfs_remove_link(from, kobject_name(to)); } -static int __link_disk_holder(struct block_device *bdev, struct gendisk *disk) -{ - int ret; - - ret = add_symlink(disk->slave_dir, bdev_kobj(bdev)); - if (ret) - return ret; - ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); - if (ret) - del_symlink(disk->slave_dir, bdev_kobj(bdev)); - return ret; -} - /** * bd_link_disk_holder - create symlinks between holding disk and slave bdev * @bdev: the claimed slave bdev @@ -75,6 +62,9 @@ int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) struct bd_holder_disk *holder; int ret = 0; + if (WARN_ON_ONCE(!disk->slave_dir)) + return -EINVAL; + mutex_lock(&disk->open_mutex); WARN_ON_ONCE(!bdev->bd_holder); @@ -94,34 +84,32 @@ int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) INIT_LIST_HEAD(&holder->list); holder->bdev = bdev; holder->refcnt = 1; - if (disk->slave_dir) { - ret = __link_disk_holder(bdev, disk); - if (ret) { - kfree(holder); - goto out_unlock; - } - } - + ret = add_symlink(disk->slave_dir, bdev_kobj(bdev)); + if (ret) + goto out_free_holder; + ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); + if (ret) + goto out_del_symlink; list_add(&holder->list, &disk->slave_bdevs); + /* * del_gendisk drops the initial reference to bd_holder_dir, so we need * to keep our own here to allow for cleanup past that point. */ kobject_get(bdev->bd_holder_dir); + mutex_unlock(&disk->open_mutex); + return 0; +out_del_symlink: + del_symlink(disk->slave_dir, bdev_kobj(bdev)); +out_free_holder: + kfree(holder); out_unlock: mutex_unlock(&disk->open_mutex); return ret; } EXPORT_SYMBOL_GPL(bd_link_disk_holder); -static void __unlink_disk_holder(struct block_device *bdev, - struct gendisk *disk) -{ - del_symlink(disk->slave_dir, bdev_kobj(bdev)); - del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); -} - /** * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder() * @bdev: the calimed slave bdev @@ -136,11 +124,14 @@ void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk) { struct bd_holder_disk *holder; + if (WARN_ON_ONCE(!disk->slave_dir)) + return; + mutex_lock(&disk->open_mutex); holder = bd_find_holder_disk(bdev, disk); if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) { - if (disk->slave_dir) - __unlink_disk_holder(bdev, disk); + del_symlink(disk->slave_dir, bdev_kobj(bdev)); + del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); kobject_put(bdev->bd_holder_dir); list_del_init(&holder->list); kfree(holder); @@ -148,24 +139,3 @@ void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk) mutex_unlock(&disk->open_mutex); } EXPORT_SYMBOL_GPL(bd_unlink_disk_holder); - -int bd_register_pending_holders(struct gendisk *disk) -{ - struct bd_holder_disk *holder; - int ret; - - mutex_lock(&disk->open_mutex); - list_for_each_entry(holder, &disk->slave_bdevs, list) { - ret = __link_disk_holder(holder->bdev, disk); - if (ret) - goto out_undo; - } - mutex_unlock(&disk->open_mutex); - return 0; - -out_undo: - list_for_each_entry_continue_reverse(holder, &disk->slave_bdevs, list) - __unlink_disk_holder(holder->bdev, disk); - mutex_unlock(&disk->open_mutex); - return ret; -} diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 57ed49f20d2eb..df45037815527 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -839,7 +839,6 @@ void set_capacity(struct gendisk *disk, sector_t size); #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk); void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk); -int bd_register_pending_holders(struct gendisk *disk); #else static inline int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) @@ -850,10 +849,6 @@ static inline void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk) { } -static inline int bd_register_pending_holders(struct gendisk *disk) -{ - return 0; -} #endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */ dev_t part_devt(struct gendisk *disk, u8 partno); From patchwork Sun Oct 30 15:31:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13025100 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2B6EECAAA1 for ; Sun, 30 Oct 2022 15:32:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229823AbiJ3PcK (ORCPT ); Sun, 30 Oct 2022 11:32:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229792AbiJ3PcG (ORCPT ); Sun, 30 Oct 2022 11:32:06 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA3DAB1DA for ; Sun, 30 Oct 2022 08:32:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=26EtRfRL6phUjf0F1YzPlLYas95/IR0DO2Dipx+s5No=; b=gbcVDyBrRl6xv782lwm8J23+hg SCUSCdCT7vXg77JP1rK5w7xXmkl2RKwMMl+I7VTOBJSbd5YlBHeYcbDlFnfqS410kArwxflfK6tnZ 0/k1iXZPxoDPrvON1UQWzeT44O88Po57sTQoWXkRmRUa8o9ELTgnWchHahd/2hFxewH8+B/7j/Uwp v11DSSyex3qqwEcM3Zq7IKNFWigV+SZqjKdfK7T5L2PUr8TpM00q5uBj8qY/YnNlXzmbq74KwX6dF iGeT26QJ2A2K591fhAsxRVN818Ijav2PfqXdAvRXSUK5qchMX0qiZO2cxsZPBSInRGwBdgx6CuxXG JiZPt1vw==; Received: from 213-225-37-80.nat.highway.a1.net ([213.225.37.80] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1opAHz-00HW4C-54; Sun, 30 Oct 2022 15:31:55 +0000 From: Christoph Hellwig To: Jens Axboe , Alasdair Kergon , Mike Snitzer Cc: Yu Kuai , dm-devel@redhat.com, linux-block@vger.kernel.org Subject: [PATCH 7/7] block: store the holder kobject in bd_holder_disk Date: Sun, 30 Oct 2022 16:31:19 +0100 Message-Id: <20221030153120.1045101-8-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221030153120.1045101-1-hch@lst.de> References: <20221030153120.1045101-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org We hold a reference to the holder kobject for each bd_holder_disk, so to make the code a bit more robust, use a reference to it instead of the block_device. As long as no one clears ->bd_holder_dir in before freeing the disk, this isn't strictly required, but it does make the code more clear and more robust. Signed-off-by: Christoph Hellwig --- block/holder.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/block/holder.c b/block/holder.c index dd9327b43ce05..a8c355b9d0806 100644 --- a/block/holder.c +++ b/block/holder.c @@ -4,7 +4,7 @@ struct bd_holder_disk { struct list_head list; - struct block_device *bdev; + struct kobject *holder_dir; int refcnt; }; @@ -14,7 +14,7 @@ static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev, struct bd_holder_disk *holder; list_for_each_entry(holder, &disk->slave_bdevs, list) - if (holder->bdev == bdev) + if (holder->holder_dir == bdev->bd_holder_dir) return holder; return NULL; } @@ -82,27 +82,24 @@ int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk) } INIT_LIST_HEAD(&holder->list); - holder->bdev = bdev; holder->refcnt = 1; + holder->holder_dir = kobject_get(bdev->bd_holder_dir); + ret = add_symlink(disk->slave_dir, bdev_kobj(bdev)); if (ret) - goto out_free_holder; - ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); + goto out_put_holder_dir; + ret = add_symlink(holder->holder_dir, &disk_to_dev(disk)->kobj); if (ret) goto out_del_symlink; list_add(&holder->list, &disk->slave_bdevs); - /* - * del_gendisk drops the initial reference to bd_holder_dir, so we need - * to keep our own here to allow for cleanup past that point. - */ - kobject_get(bdev->bd_holder_dir); mutex_unlock(&disk->open_mutex); return 0; out_del_symlink: del_symlink(disk->slave_dir, bdev_kobj(bdev)); -out_free_holder: +out_put_holder_dir: + kobject_put(holder->holder_dir); kfree(holder); out_unlock: mutex_unlock(&disk->open_mutex); @@ -131,8 +128,8 @@ void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk) holder = bd_find_holder_disk(bdev, disk); if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) { del_symlink(disk->slave_dir, bdev_kobj(bdev)); - del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj); - kobject_put(bdev->bd_holder_dir); + del_symlink(holder->holder_dir, &disk_to_dev(disk)->kobj); + kobject_put(holder->holder_dir); list_del_init(&holder->list); kfree(holder); }