Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp614180pxb; Fri, 15 Apr 2022 07:21:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2uLdbR9kB0YrwgNl7+TSEX6oYe+PWZbLDdQBCNXHzykWNDIHURA1NoNzbrgu7Dai9y2VN X-Received: by 2002:a05:6a00:1a10:b0:4fc:d6c5:f3ed with SMTP id g16-20020a056a001a1000b004fcd6c5f3edmr19930756pfv.85.1650032498537; Fri, 15 Apr 2022 07:21:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650032498; cv=none; d=google.com; s=arc-20160816; b=p+Zo8m20isHI6Bvudjh9eLlmj6RiyTPaXi2rasYfx+wALQSVJ1ujVn6fnqSGXH14iK pbtZdHF2tstTjpGZDkbaa5DnqiUdS3bqacsrBgnnkPn2Cyzm4k8xaukdSEpnlCECXK/F bO3TKo9VuTx9ZDGLhL2SdOn/3K2bCROwYASDBit0JP59TLy3zEYOkjIgojeJI+9z2+Cm zWeX09Lo9cEldg9PeqE4sKKIpftuo1Jp0Cp6y1BehsTQx7Nyjt1LnqAMfJk9qQlf5wKn 830ShNz3xJiHWmVKW+cSlGFtfqVBx8PptB8sQ/UCL3MRdUAPda/HQONXk++5++rbXPic 1B4Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GLA2nAe/cQO5WqFsrtbqfq8T3xUan5SkfUOusFqqAns=; b=h/ArWVTUV34k5VIL5Nbp3FA4FE0phasUoqKK9Dc8IYnmcJ5VlhxGaGHpEqIjYe25ag XhK/ZU0dYaUm52JdOY2cY+dApb3t07mB848WAfHfMrExw8i5voKJvPRSlat1Bg0XZW9S A8r4U/br/ew/4Mr7E+mdDXO5dZe/pAx8fRKlEelCwg48OXhibMnIvoVa6Wrs+WqeolT8 2vNEd3BvcRFpqBjtWmRBQMB+KXlRw01whskH9NvRun68kepaZpgfuDxZh8wcGzvuDfXN zOcTnOrgZ1B/FWhX4SSEv8tV7sqy+gJl/f2sgEgH2hpTRk+kzRUIG2QHkKPBI/8TpQOX gjbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=VsGSXhUu; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r13-20020a632b0d000000b00384650184ecsi1542737pgr.738.2022.04.15.07.21.22; Fri, 15 Apr 2022 07:21:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=VsGSXhUu; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350127AbiDOE7x (ORCPT + 99 others); Fri, 15 Apr 2022 00:59:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350065AbiDOE6T (ORCPT ); Fri, 15 Apr 2022 00:58:19 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7B82AFB29; Thu, 14 Apr 2022 21:54:14 -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=GLA2nAe/cQO5WqFsrtbqfq8T3xUan5SkfUOusFqqAns=; b=VsGSXhUuIEFuRZtndYvZ6/P8Oc BDcT4E1kdUm0zMpQr8vfARZ9SBXzHbIzUwD1GYcduf3i506YLp6g548TDVxAtbp8KULMmrxF/ukva fgeigpWf0NIDRQPagvS6VzFyZpJ3xcSZOqt1eMagBiZEETs+p7tuGOEblnRA/Ubh/DSR79GR69y6j FXiQ7FqkrDKHIdBeDoZ8IY+N1d7emzC5iIlTdFynPRNmDVt0LMc9eg28VMj13kA7fa+HCrkwQXu48 dUI7fDaAt8ItsFFi9XzyzHuoHEFOt6Vg27L4pMYHOgWMn9i+eAuB2cLGlEi8YHAGqnLzqjEoo43U0 EggVK9WA==; Received: from [2a02:1205:504b:4280:f5dd:42a4:896c:d877] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1nfDyD-008Pdc-VZ; Fri, 15 Apr 2022 04:54:10 +0000 From: Christoph Hellwig To: Jens Axboe Cc: dm-devel@redhat.com, linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-um@lists.infradead.org, linux-block@vger.kernel.org, drbd-dev@lists.linbit.com, nbd@other.debian.org, ceph-devel@vger.kernel.org, virtualization@lists.linux-foundation.org, xen-devel@lists.xenproject.org, linux-bcache@vger.kernel.org, linux-raid@vger.kernel.org, linux-mmc@vger.kernel.org, linux-mtd@lists.infradead.org, linux-nvme@lists.infradead.org, linux-s390@vger.kernel.org, linux-scsi@vger.kernel.org, target-devel@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, cluster-devel@redhat.com, jfs-discussion@lists.sourceforge.net, linux-nilfs@vger.kernel.org, ntfs3@lists.linux.dev, ocfs2-devel@oss.oracle.com, linux-mm@kvack.org, "Martin K . Petersen" , Coly Li Subject: [PATCH 22/27] block: refactor discard bio size limiting Date: Fri, 15 Apr 2022 06:52:53 +0200 Message-Id: <20220415045258.199825-23-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220415045258.199825-1-hch@lst.de> References: <20220415045258.199825-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Move all the logic to limit the discard bio size into a common helper so that it is better documented. Signed-off-by: Christoph Hellwig Reviewed-by: Martin K. Petersen Acked-by: Coly Li --- block/blk-lib.c | 59 ++++++++++++++++++++++++------------------------- block/blk.h | 14 ------------ 2 files changed, 29 insertions(+), 44 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 237d60d8b5857..2ae32a722851c 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -10,6 +10,32 @@ #include "blk.h" +static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector) +{ + unsigned int discard_granularity = + bdev_get_queue(bdev)->limits.discard_granularity; + sector_t granularity_aligned_sector; + + if (bdev_is_partition(bdev)) + sector += bdev->bd_start_sect; + + granularity_aligned_sector = + round_up(sector, discard_granularity >> SECTOR_SHIFT); + + /* + * Make sure subsequent bios start aligned to the discard granularity if + * it needs to be split. + */ + if (granularity_aligned_sector != sector) + return granularity_aligned_sector - sector; + + /* + * Align the bio size to the discard granularity to make splitting the bio + * at discard granularity boundaries easier in the driver if needed. + */ + return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT; +} + int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, int flags, struct bio **biop) @@ -17,7 +43,7 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, struct request_queue *q = bdev_get_queue(bdev); struct bio *bio = *biop; unsigned int op; - sector_t bs_mask, part_offset = 0; + sector_t bs_mask; if (bdev_read_only(bdev)) return -EPERM; @@ -48,36 +74,9 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, if (!nr_sects) return -EINVAL; - /* In case the discard request is in a partition */ - if (bdev_is_partition(bdev)) - part_offset = bdev->bd_start_sect; - while (nr_sects) { - sector_t granularity_aligned_lba, req_sects; - sector_t sector_mapped = sector + part_offset; - - granularity_aligned_lba = round_up(sector_mapped, - q->limits.discard_granularity >> SECTOR_SHIFT); - - /* - * Check whether the discard bio starts at a discard_granularity - * aligned LBA, - * - If no: set (granularity_aligned_lba - sector_mapped) to - * bi_size of the first split bio, then the second bio will - * start at a discard_granularity aligned LBA on the device. - * - If yes: use bio_aligned_discard_max_sectors() as the max - * possible bi_size of the first split bio. Then when this bio - * is split in device drive, the split ones are very probably - * to be aligned to discard_granularity of the device's queue. - */ - if (granularity_aligned_lba == sector_mapped) - req_sects = min_t(sector_t, nr_sects, - bio_aligned_discard_max_sectors(q)); - else - req_sects = min_t(sector_t, nr_sects, - granularity_aligned_lba - sector_mapped); - - WARN_ON_ONCE((req_sects << 9) > UINT_MAX); + sector_t req_sects = + min(nr_sects, bio_discard_limit(bdev, sector)); bio = blk_next_bio(bio, bdev, 0, op, gfp_mask); bio->bi_iter.bi_sector = sector; diff --git a/block/blk.h b/block/blk.h index 4ea5167dc3392..434017701403f 100644 --- a/block/blk.h +++ b/block/blk.h @@ -346,20 +346,6 @@ static inline unsigned int bio_allowed_max_sectors(struct request_queue *q) return round_down(UINT_MAX, queue_logical_block_size(q)) >> 9; } -/* - * The max bio size which is aligned to q->limits.discard_granularity. This - * is a hint to split large discard bio in generic block layer, then if device - * driver needs to split the discard bio into smaller ones, their bi_size can - * be very probably and easily aligned to discard_granularity of the device's - * queue. - */ -static inline unsigned int bio_aligned_discard_max_sectors( - struct request_queue *q) -{ - return round_down(UINT_MAX, q->limits.discard_granularity) >> - SECTOR_SHIFT; -} - /* * Internal io_context interface */ -- 2.30.2