From patchwork Thu Dec 17 11:41:55 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 7872201 Return-Path: X-Original-To: patchwork-linux-block@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D82529F32E for ; Thu, 17 Dec 2015 11:42:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A730220416 for ; Thu, 17 Dec 2015 11:42:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 985882041D for ; Thu, 17 Dec 2015 11:42:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966385AbbLQLmJ (ORCPT ); Thu, 17 Dec 2015 06:42:09 -0500 Received: from mail-bn1bon0099.outbound.protection.outlook.com ([157.56.111.99]:55050 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S966347AbbLQLmB (ORCPT ); Thu, 17 Dec 2015 06:42:01 -0500 Received: from BY2PR02CA0111.namprd02.prod.outlook.com (10.163.44.165) by BY2PR02MB297.namprd02.prod.outlook.com (10.141.140.17) with Microsoft SMTP Server (TLS) id 15.1.355.16; Thu, 17 Dec 2015 11:41:59 +0000 Received: from BY2FFO11FD013.protection.gbl (2a01:111:f400:7c0c::134) by BY2PR02CA0111.outlook.office365.com (2a01:111:e400:5261::37) with Microsoft SMTP Server (TLS) id 15.1.361.13 via Frontend Transport; Thu, 17 Dec 2015 11:41:59 +0000 Authentication-Results: spf=pass (sender IP is 63.163.107.173) smtp.mailfrom=sandisk.com; suse.cz; dkim=none (message not signed) header.d=none;suse.cz; dmarc=bestguesspass action=none header.from=sandisk.com; Received-SPF: Pass (protection.outlook.com: domain of sandisk.com designates 63.163.107.173 as permitted sender) receiver=protection.outlook.com; client-ip=63.163.107.173; helo=milsmgep12.sandisk.com; Received: from milsmgep12.sandisk.com (63.163.107.173) by BY2FFO11FD013.mail.protection.outlook.com (10.1.14.75) with Microsoft SMTP Server id 15.1.355.15 via Frontend Transport; Thu, 17 Dec 2015 11:41:58 +0000 Received: from MILHUBIP04.sdcorp.global.sandisk.com ( [172.22.12.162]) by milsmgep12.sandisk.com (Symantec Messaging Gateway) with SMTP id 84.89.32178.68F92765; Thu, 17 Dec 2015 03:41:58 -0800 (PST) Received: from milsmgip12.sandisk.com (10.177.8.100) by MILHUBIP04.sdcorp.global.sandisk.com (10.177.9.97) with Microsoft SMTP Server id 14.3.248.2; Thu, 17 Dec 2015 03:41:58 -0800 X-AuditID: ac160a69-f79f76d000007db2-65-56729f8654df Received: from [10.50.231.72] ( [10.177.8.100]) by milsmgip12.sandisk.com (Symantec Messaging Gateway) with SMTP id 81.F0.17166.48F92765; Thu, 17 Dec 2015 03:41:58 -0800 (PST) Subject: [PATCH 4/6] block: Make blkdev_issue_discard() submit aligned discard requests To: Jens Axboe References: <56729ECF.50906@sandisk.com> CC: Jan Kara , Christoph Hellwig , Mike Snitzer , "Martin K. Petersen" , Dmitry Monakhov , "linux-block@vger.kernel.org" , device-mapper development From: Bart Van Assche Message-ID: <56729F83.8040005@sandisk.com> Date: Thu, 17 Dec 2015 12:41:55 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: <56729ECF.50906@sandisk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprGIsWRmVeSWpSXmKPExsWyRoxnkW7b/KIwg7YDRhb/9xxjs9j7bjar xYmZnhYrVx9lspg9vZnJYu8tbYvlx/8xWbRt/MrowOExsfkdu8fumw1sHpMOf2by+Pj0FovH +31X2TzOLDjC7vF5k1wAexSXTUpqTmZZ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a8xPa3c5gnMPLPQtIyC0nZLCRlCxiZVzGK5Wbm FOemZxYYGukVJ+alZBZn6yXn525iBAc5Z9QOxusTzQ8xMnFwSjUwzjK1r9P9tXJvCWeVudSs 41I6Hzr/T7ZM/Pncaw7nykqDEhaJGW/C6gvsjrJf2fTo5JXYI09jJWP+LmAyMlSb86R3QVzp me3rXB65T1C82WfcdLW0cZcKc7CyTFlmyUTFeQ++6sTI3cyUXnc+85nBHGH2Lfe4Pu/KbE8U O3D2XWPg9YOpdr+5lViKMxINtZiLihMBiPx4UCICAAA= X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1; BY2FFO11FD013; 1:wTBFY79OHtB7x3Av4PfO9/5rBLWiKyQoWnr7Wg4J5EH3oN8LLbMYoOL1j/b4X+qxt7PtjLzHopdVhRaW89ZHbmvV3eM8WmSl5flkfskJlp7jNUbHIIIqI0VvbXcqtlrrPv5C8MoekpFYTS4zD0qiwbw+0UhlZlr4bowgKp8RHuV4vox3AB/+dMzRZQXerCC1AMLaVYliff6NPYqhODDqywI8/TxCvVNdjMhZH91S5xDioR92YcKH2SYh7ATyPEbLxiRynsNn2mfyUCmlvsQa2qGF2qYalxWqQ1wV2tZVBviJ8bwmYV/bKXpQuBUHIyIdDqk+SbQczN7NFkEHvrHABOLm2Pi7PGR278zMkZH0m1ySDQrm3xsK/2LPtxvYAbBN X-Forefront-Antispam-Report: CIP:63.163.107.173; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(438002)(189002)(199003)(1096002)(87936001)(23676002)(1220700001)(77096005)(5008740100001)(189998001)(230700001)(11100500001)(65816999)(64126003)(106466001)(36756003)(99136001)(50986999)(33656002)(2950100001)(92566002)(65806001)(83506001)(69596002)(59896002)(19580395003)(81156007)(50466002)(586003)(47776003)(110136002)(5001960100002)(4001350100001)(97736004)(65956001)(80316001)(575784001)(86362001)(19580405001)(229853001)(87266999)(54356999)(76176999)(21314002); DIR:OUT; SFP:1101; SCL:1; SRVR:BY2PR02MB297; H:milsmgep12.sandisk.com; FPR:; SPF:Pass; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BY2PR02MB297; 2:2ddYi7ychIo3/gOm6DOUR83VGCY7tzLbFUB7y8gKz/nbIvmpejhX8i5kgXUTRAzS+xaocwhzcaWrw2M/WlxdY5hXbn6jNsplCiSzXtiRUKcbtbC09wdRo1+gdjGC0HfpH07E0niuygTzAFsYDMmENg==; 3:sNHhLoEqJjAjw36chLOmjbyWXzh7bxI0Jz8mFFfdTDNaKTRTUhlj67Q4Pjg2RXCGIOG/EzgQEH79wbHmFmrpq56oR6unf3PkEb9C1ZUekEBvUzPZ6PJfDr8XtjBp0UbLikykfpyE74vEdoLSeLQcETrzgBJ/jhC+fjeDVA7Vh0zgS/yWjbOMGELFxLrsg41NrpJWsO2++xZ0xV4dontn05mRiapldIdT9kN7xe9m6HE2sduLpXsrTrZR6rtB9q3Crdp/qg56zzNa/5uEeEV5Jg==; 25:eKMxCDMH7Py4/FCr+XLJeT1h5e3i+if1lAUtYisNODUEZPkV7d1vL6dGmEmctRA2QG6nqFP+QqlxK9y7bvWGUk6ZESTzOKJCpOuObFgkPVU0qsUwMSsYviMErQ7ks5keyP3lgXxG/VrwiUJKQRrysNeRPhy2KzXSjS43OmX8ipTU0KfeivGQlNPek7isKxC99FyIOVyMYKPKBezi0yxPKU+LAbkLIjq5SVF1sb8pzgDkR8OMQ8WMucERMMO4b7l+VejUmU7zSyjLeAd4+5gyKQ== X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(8251501001); SRVR:BY2PR02MB297; X-Microsoft-Exchange-Diagnostics: 1; BY2PR02MB297; 20:UXhLJ1Mat65yiiQWQXyKQB6wbJEwU40UNkymNr/DU7ShYNdGmX6gs1XGnKrCJke6QnJN1KC/a+PUwd1iMxVixjMTB5tKEmryAb9XoBtYb9oMjuRVY0t6ELBSWZdAahNyFlplKMMKlNUQyuVfL0yhcXOznOGtGvCK1JX+Qpp/pSyJ5eN1tXkW1Dblym1Xx6zD94z7OadjJUC44dVCm9U518TvoNYnGLJ7vwMUdPyb3PA0T+U1nxGJsyllv5CbL1dw31EhKsA9unobqB4OLpCMk/lo70mVkVJxZgtoNPEDqvHQojQHoT/MEfaVbku4RaV0/ZRe9rV+x/cFTlGy07SjsLhbeQ4yqtGbv0YZbaTWnbdUidBa9+W7XJPXICLPKxIZ8r1+c/QOdB3TM2S+FHAa5CG1cW35tiv7WQIw26NE0Wt9/YfgAkQZxto2aTbnp6jwwF85TdmUB5z5MF+HsUmaHviy+gLDnZmo+fBAzfLB442IN2HL//g8TF2eQqhn3BMQ X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(146099531331640)(42932892334569); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(520078)(5005006)(8121501046)(3002001)(10201501046); SRVR:BY2PR02MB297; BCL:0; PCL:0; RULEID:; SRVR:BY2PR02MB297; X-Microsoft-Exchange-Diagnostics: 1; BY2PR02MB297; 4:m3ZcVKWeEO59hRHEIm8sThIaTkkXIwPCx3Q+SQmlcxNhvhVzGWO7WkMpTppQJ6HxY/6g85Xblg0xkPshKUundmF8c32NbtJpUPSwLfXyDXPhLG2RDWElf18gmyrdY7u03T/Zr/9RkxkivSPzp0Ag/cF4HuvBvcCdQdeqm1KRO+Dzbpxv8bwRKv8a5TIrEZBSBJVW+b3dKHz1o/0TOYDppaAjfTTSaF0LRthMcuvmAgiti7dOlHwPJGUF1WRVoyKP5gFwiGVEXKbfyRx2ZwqA7C/JiDRffF3hcZnRErDVAH6cjhIXSLAsQuR80hucsd5skcuLVKC8vmLIBS6u+Fj+pOPGttGSEKYH/Qc4QE0RZLpolOjBhte5N1GmTspOhlMradGbrO0MRSapDtOdqCbZGBsMDYfvOY65QrA8if88x3N+Q1zU7cnACkjcCGtw7cLG+Xrg99RUIGniUG+Ns0iElw== X-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3HE+AbvY2yrUx4LLU4qYI6Z3VKZvmvq4bQ5JAfHEkmXPrf2vb5Vs1oENmEWE0b00aV7cxZf6v2ViXgbQ==; 24:ZF7v1Aen5Ga/lXR0/0fcVKAaDMlkuOM7K+4/stCFxL8r1vkvADJoSTYc16FSY1mPP7xlzNJCbeeK+MzEHmKbFj9b/0ueD9FeDsZ3pz6JdvA=; 20:INsN/c3HvoFVlpGCnj2FYigodUCAiy/LLblF+mnhHbh12AsURqMfOu/ipS8M0/TUH/yVD5m9qdhKWMCVxYHVDi6Lh1ptzLlghlK0S2kn5wIzpYIZ3XnE0K6orsURUQkeN/Tvvs332e+/CJiLUbEJkScYUu0rywMgAo95wcFFeikabYljnEcDkrQSBtrUmp/wI5G5CURu3sWJ0sKWoe3pqe176mICFavR+oZ7KnykBWvze/0HWUru3nbhzuCOwq8t SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: sandisk.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Dec 2015 11:41:58.9685 (UTC) X-MS-Exchange-CrossTenant-Id: fcd9ea9c-ae8c-460c-ab3c-3db42d7ac64d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=fcd9ea9c-ae8c-460c-ab3c-3db42d7ac64d; Ip=[63.163.107.173]; Helo=[milsmgep12.sandisk.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR02MB297 Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Split discard requests as follows: * If the start sector is not aligned, an initial write request up to the first aligned sector. * A discard request from the first aligned sector in the range up to the last aligned sector in the discarded range. * If the end sector is not aligned, a final write request from the last aligned sector up to the end. Note: if the start and/or end sectors are not aligned and if the range is small enough the discard request will be submitted with bi_size == 0. Signed-off-by: Bart Van Assche Cc: Jan Kara Cc: Christoph Hellwig Cc: Mike Snitzer Cc: Martin K. Petersen Cc: Dmitry Monakhov --- block/blk-lib.c | 4 ++-- block/blk-merge.c | 55 ++++++++++++++++++++++++++++++------------------------- block/blk.h | 3 +++ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 1a60b3f..da0a744 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -30,7 +30,7 @@ static void bio_batch_end_io(struct bio *bio) * Return the largest number that is less than or equal to @s and for which * the remainder of the division by @granularity is @alignment. */ -static sector_t blk_round_sect_down(sector_t s, u32 granularity, u32 alignment) +sector_t blk_round_sect_down(sector_t s, u32 granularity, u32 alignment) { sector_t tmp = s, res = s; u32 remainder; @@ -219,7 +219,7 @@ int blkdev_issue_write_same(struct block_device *bdev, sector_t sector, } EXPORT_SYMBOL(blkdev_issue_write_same); -static void bio_add_zero_pages(struct bio *bio, sector_t nr_sects) +void bio_add_zero_pages(struct bio *bio, sector_t nr_sects) { unsigned sz; int ret; diff --git a/block/blk-merge.c b/block/blk-merge.c index e01405a..2ce5a78 100644 --- a/block/blk-merge.c +++ b/block/blk-merge.c @@ -14,42 +14,47 @@ static struct bio *blk_bio_discard_split(struct request_queue *q, struct bio_set *bs, unsigned *nsegs) { + struct bio *wr; unsigned int max_discard_sectors, granularity; int alignment; - sector_t tmp; - unsigned split_sectors; + sector_t start, start_r, end, end_r, skip; *nsegs = 1; /* Zero-sector (unknown) and one-sector granularities are the same. */ granularity = max(q->limits.discard_granularity >> 9, 1U); - + alignment = (q->limits.discard_alignment >> 9) % granularity; max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); - max_discard_sectors -= max_discard_sectors % granularity; - - if (unlikely(!max_discard_sectors)) { - /* XXX: warn */ - return NULL; - } - - if (bio_sectors(bio) <= max_discard_sectors) - return NULL; - - split_sectors = max_discard_sectors; + WARN_ON_ONCE(max_discard_sectors == 0); /* - * If the next starting sector would be misaligned, stop the discard at - * the previous aligned sector. + * If the start or end sector are misaligned, issue a write same + * same request if the discard_zeroes_data flag has been set. */ - alignment = (q->limits.discard_alignment >> 9) % granularity; - - tmp = bio->bi_iter.bi_sector + split_sectors - alignment; - tmp = sector_div(tmp, granularity); - - if (split_sectors > tmp) - split_sectors -= tmp; - - return bio_split(bio, split_sectors, GFP_NOIO, bs); + start = bio->bi_iter.bi_sector; + start_r = blk_round_sect_down(start, granularity, alignment); + end = start + min(max_discard_sectors, bio_sectors(bio)); + end_r = blk_round_sect_down(end, granularity, alignment); + if (start == start_r && start < end_r) { + if (end == end_r && bio_sectors(bio) == end_r - start) + return NULL; + return bio_split(bio, end_r - start, GFP_NOIO, bs); + } else if (q->limits.discard_zeroes_data) { + end = min(end, start_r + granularity); + wr = bio_alloc_bioset(GFP_NOIO, end - start, bs); + if (WARN_ON_ONCE(!wr)) + return NULL; + wr->bi_rw = REQ_WRITE; + wr->bi_iter.bi_sector = start; + wr->bi_bdev = bio->bi_bdev; + bio_add_zero_pages(wr, end - start); + bio_advance(bio, wr->bi_iter.bi_size); + return wr; + } else { + skip = (min(start_r + granularity, end) - start) << 9; + bio_advance(bio, skip); + return NULL; + } } static struct bio *blk_bio_write_same_split(struct request_queue *q, diff --git a/block/blk.h b/block/blk.h index c43926d..e043c34 100644 --- a/block/blk.h +++ b/block/blk.h @@ -36,6 +36,9 @@ extern struct kmem_cache *request_cachep; extern struct kobj_type blk_queue_ktype; extern struct ida blk_queue_ida; +sector_t blk_round_sect_down(sector_t s, u32 granularity, u32 alignment); +void bio_add_zero_pages(struct bio *bio, sector_t nr_sects); + static inline struct blk_flush_queue *blk_get_flush_queue( struct request_queue *q, struct blk_mq_ctx *ctx) {