From patchwork Tue Dec 15 15:45:00 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: 7855791 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 864029F387 for ; Tue, 15 Dec 2015 16:17:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 176D02037C for ; Tue, 15 Dec 2015 16:17:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 96543202FE for ; Tue, 15 Dec 2015 16:17:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753862AbbLOQRK (ORCPT ); Tue, 15 Dec 2015 11:17:10 -0500 Received: from mail-bn1bon0067.outbound.protection.outlook.com ([157.56.111.67]:8845 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753843AbbLOQRI (ORCPT ); Tue, 15 Dec 2015 11:17:08 -0500 X-Greylist: delayed 1144 seconds by postgrey-1.27 at vger.kernel.org; Tue, 15 Dec 2015 11:17:08 EST Received: from BY2PR02CA0014.namprd02.prod.outlook.com (10.242.32.14) by BN1PR0201MB0819.namprd02.prod.outlook.com (10.160.170.151) with Microsoft SMTP Server (TLS) id 15.1.355.16; Tue, 15 Dec 2015 15:45:06 +0000 Received: from BL2FFO11FD027.protection.gbl (2a01:111:f400:7c09::197) by BY2PR02CA0014.outlook.office365.com (2a01:111:e400:2c2a::14) with Microsoft SMTP Server (TLS) id 15.1.361.13 via Frontend Transport; Tue, 15 Dec 2015 15:45:06 +0000 Authentication-Results: spf=pass (sender IP is 63.163.107.173) smtp.mailfrom=sandisk.com; lst.de; dkim=none (message not signed) header.d=none;lst.de; 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 BL2FFO11FD027.mail.protection.outlook.com (10.173.161.106) with Microsoft SMTP Server id 15.1.346.13 via Frontend Transport; Tue, 15 Dec 2015 15:45:04 +0000 Received: from MILHUBIP04.sdcorp.global.sandisk.com ( [172.22.12.162]) by milsmgep12.sandisk.com (Symantec Messaging Gateway) with SMTP id E7.41.32178.F7530765; Tue, 15 Dec 2015 07:45:03 -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; Tue, 15 Dec 2015 07:45:03 -0800 X-AuditID: ac160a69-f79f76d000007db2-95-5670357f4aa9 Received: from [10.50.231.65] ( [10.177.8.100]) by milsmgip12.sandisk.com (Symantec Messaging Gateway) with SMTP id 29.E2.17166.D7530765; Tue, 15 Dec 2015 07:45:02 -0800 (PST) Subject: [PATCH 2/2] block: Fix blkdev_issue_discard() To: Jens Axboe References: <567034B3.90104@sandisk.com> CC: Jan Kara , Christoph Hellwig , "Martin K. Petersen" , Mike Snitzer , "linux-block@vger.kernel.org" From: Bart Van Assche Message-ID: <5670357C.2020100@sandisk.com> Date: Tue, 15 Dec 2015 16:45:00 +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: <567034B3.90104@sandisk.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupmkeLIzCtJLcpLzFFi42JZI8azSLfetCDMYOV8YYv/e46xWaxcfZTJ Yvb0ZiaLvbe0LZYf/8dk0bbxK6MDm8fE5nfsHrtvNrB5fHx6i8Xj/b6rb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l2CsuUltr+dwzyBkX8WkpZZSMpmISlbwMi8ilEsNzOnODc9s8DQSK84MS8l szhbLzk/dxMjOJQ5o3YwXp9ofoiRiYNTqoExRbp6nteatxfMBf+t/deXmXZaueXsrNwvzLMK p7br2s/bmSX0PD33Jlepr013iNFaP5dbndvij275wmkq1jt5k0IwX5LG4clNSkann84VmT41 UZZ7bxaj29z3DVlrQ7PD1vh+mGPLF77Z8Onku2cU9yuocmberF9vK/JsYredsvCmIgbJDdeV WIozEg21mIuKEwHPCt6DFQIAAA== X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1; BL2FFO11FD027; 1:mxMfRQlxN5WNM8WoSIzvPyLGt8mW9Vrf+iMJ7tYnoyDR8iuqiSUigIGKxFvkMWFNNyahzPpFXoCrnwmSzH7SB2jQbkFpV25q+CPj1dj/mlmjBD2Zn8M7U53l7EgAnjU9Ox+pUKhpbme8e2NfriVZyCivO3AetkKEfiMtndJyKEClVIMBc3esOQ2ELzncKpTrfJiHPPAXckgjXKEHW/6uOuXdP8xxJk1rtKkXRZFth0qG4gHD2kK5VBCv7QuIO1wAdazSM72/04dACFbEiKMvLMa29kclaqI6rSyYJlWliZPOjnXWGtY4fx3qP2o4qiYlIzpONIiJIhVhU73t9+yXARrUQT/VQBq2pOl6kmHHOPnLttIiGGNhjq2qzu9wemgj4ikh5Ft6wGzVpGHJtopL2g== X-Forefront-Antispam-Report: CIP:63.163.107.173; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(438002)(189002)(199003)(575784001)(5001960100002)(189998001)(106466001)(1096002)(23676002)(5008740100001)(80316001)(36756003)(19580395003)(230700001)(64126003)(586003)(69596002)(19580405001)(83506001)(33656002)(65816999)(97736004)(76176999)(77096005)(1220700001)(54356999)(92566002)(4001350100001)(110136002)(99136001)(59896002)(11100500001)(50986999)(50466002)(2950100001)(86362001)(65956001)(87936001)(81156007)(47776003)(229853001)(87266999)(65806001); DIR:OUT; SFP:1101; SCL:1; SRVR:BN1PR0201MB0819; H:milsmgep12.sandisk.com; FPR:; SPF:Pass; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BN1PR0201MB0819; 2:4VWwEmFInU638uewdNK5CniFOhGxkelZ+oivmGiAybV7Qd+VMXQu89kIyvO9UnM2s+t1RSpt9M/llYFQmwtJnSTWi2BX0jwbh2/9ERkNLz2zY2vUHdl0cTIo9tnnAqnqJufHHT9r7tfUZ9/Qo1uhvg==; 3:gt6LBkDElfYoPDVVNH6Xf3pjmhk/yUveZ/SZMWWupaEm28NcuaZ195iJ9GA2Y+f1I2mxKN+CQSb0GibqII91wFOXTK+8Nvf1/RbJv1aEoCmZmYYGfuO8OtUB1/MnekQB6W5nfhNvxQIrz581wf79pGmfITFIVhFNc1KRMgCIFKlNNGg2hHvolGZGi6MQSWsqeTHsoccXC1Y+uWV38VpqS2zYtqEJA0ZlINwGciMk0NWJHur6Vq7XIgTPZX9AjPkPr7zrVXJ9vS6AXUVKHlXRsQ==; 25:NydeUbJcY+5fCvNffBBdTKNoHaqVm6NAn08ByUW3eBaqH4WtUC+CG3Q7FILsJx5DPK8fGhn91L1n2habIqJXXAjIrOC6vQeuVd7OL/AEt9fWdLlG+KHcKYNcJOO25oVwxTS4DfKG6UtgAwO9+Vsc/7vfHTTpqOYEsSd2p/IO5nMFdPuixFFaaZ5BrPposjonc7onn+jqouVA6GHV4e4kQB4QSgDbI+87ibtgQ7V8DufblZ2XNBSARuwba51jRpmJ X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(8251501001); SRVR:BN1PR0201MB0819; X-Microsoft-Exchange-Diagnostics: 1; BN1PR0201MB0819; 20:nY9kfuWlQPT3m9DjVo/drRX3Lt16XkzV4XgJf1lV8heZL6/SG5lVvevsrfaFdw2iLpIpypr//jmzHZZo7aKvg42XhIIxhUzChEGbuTRLFa4Adr744kKqcsX5V+xOI67q59L8fw8rBk0J3Qb/u9H+logdr+30bPXFcQFVOcflZO1pwUl6OBFqaZdqgfeWa4UkA3wCspzSoNoNLE+9q1P2WsMchiBrgqWkybrMnxnX3iJTGiviKEJ9kbMRDQiBvBcGkxq1sSHGSlI6o6Jep7DK8qt4lW/kB/KJzFabqj4qrOxsN30lRPSqTiNX6/AzsOC0rLYLSuH8y8545VSSkYlX0tytV1OoMqISGXBRt8ct1vUOxViXMEVjxptjsF28e4s3PA1/Q1lPmXhiVXSXTdgBgCCL96ujT7wxPa02oFDIb2aGSFgJCWh3ki6wD5tobd7QTSnQHykDk+fFOukCubp1XiIrj//pzGDhKAP3nqsyU7c9NUvuUaNqPfOdgxGWDXxR 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)(5005006)(520078)(8121501046)(10201501046)(3002001); SRVR:BN1PR0201MB0819; BCL:0; PCL:0; RULEID:; SRVR:BN1PR0201MB0819; X-Microsoft-Exchange-Diagnostics: 1; BN1PR0201MB0819; 4:jOKMxTg7DsSYSCIBybJ0iRnaLqoqExwyNS1eZNWfpxf8/9jzZuTvO7dIY+sSs6XDegKGDwge0+N4T1+3TKZRdK+5jnfav8Q+pIP/ERzdDeE/GKwo13ywYPqS4vaZI7QfTgUdz+4up0K52DDnXFYwy+jip3o+962vrxq+0DaHn9Rqai80j0jLwPLtzSOdHLxY8fMq2GMQxOi8Bqkg0Tj7cEDy4S+OmgjnvptPro05lXOnMJE0MshSyLi6zzBgaKOEQTw5bd8byN4aAGvwjQf69d9UhQR8Ob8jEbbAs3FCqbUbiZPwdGcQ4+MLQRK1pO1qy6aYgRIevflVEic4HR7drIU887N2j6k3tJrYANS+wEAJYNiqjUSL4k0tt7CbBl0QDKcA78cy0nkmEMxG6PtnQhXRGwLYu8uX8bv40KO9jR7yQtlNzZrRxTeF505AYo/oIYb/cHAbfRVZXYAzeSUVbw== X-Forefront-PRVS: 07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yXcig65UsenM6n6VSBRAAhpRDovg==; 24:2bS7FCKbKkOs9Sg37OudH+ST0NHF5VoDxOqKUYsm2ze8JhY/hr2uUlgNgnkJtOjRUoXlc9WV2t4IT9+a3sXTdXNQ5k05vn0HUfk449aJNAA=; 20:JZIqWMAT08cdc6YUKSM/hqLc/RQUc4u2mQsNsPo8+ExhAFZ7d1DiVwyivWZ0XH8pLqd/7jCh14WK35DfiWYVuDQjEDcoX23BS9vbHYdG+c0dZvGMgblq77aQLBC0v0LcvPLORD7hgBhQAenep4rlFBO88SE+/mcTKAV/rGKTxsKLM6M6E0P2F0er9h3ofx+NzYVBOBuiRtSPocNxx/brGXDUnvLiWpCYzfeObAe38xoIcMlsBmrHJprl7dX6xMzw SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: sandisk.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Dec 2015 15:45:04.5211 (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: BN1PR0201MB0819 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 If the start or the end of a discard range does not satisfy the alignment requirements of a block driver, call __blkdev_issue_zeroout() for the first and/or last part instead of using REQ_DISCARD. Signed-off-by: Bart Van Assche Cc: Jan Kara Cc: Christoph Hellwig Cc: Martin K. Petersen Cc: Ming Lin Cc: Mike Snitzer Cc: stable --- block/blk-lib.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 22e9f0f..6a91711 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -157,6 +157,42 @@ static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, return ret; } +/* + * 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 round_sect_down(sector_t s, u32 granularity, u32 alignment) +{ + sector_t tmp = s, res = s; + u32 remainder; + + remainder = sector_div(tmp, granularity); + if (remainder == alignment) + return res; + res -= remainder - alignment; + if (remainder < alignment) + res -= granularity; + return min(res, s); +} + +/* + * Return the smallest number that is greater than or equal to @s and for which + * the remainder of the division by @granularity is @alignment. + */ +static sector_t round_sect_up(sector_t s, u32 granularity, u32 alignment) +{ + sector_t tmp = s, res = s; + u32 remainder; + + remainder = sector_div(tmp, granularity); + if (remainder == alignment) + return res; + res += alignment - remainder; + if (alignment < remainder) + res += granularity; + return max(res, s); +} + /** * blkdev_issue_discard - queue a discard * @bdev: blockdev to issue discard for @@ -174,8 +210,9 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, DECLARE_COMPLETION_ONSTACK(wait); struct request_queue *q = bdev_get_queue(bdev); int type = REQ_WRITE | REQ_DISCARD; - unsigned int granularity; - int alignment; + unsigned int granularity, req_sects; + sector_t end_sect; + int alignment, res; struct bio_batch bb; struct bio *bio; int ret = 0; @@ -202,10 +239,19 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, bb.wait = &wait; blk_start_plug(&plug); + end_sect = round_sect_up(sector, granularity, alignment); + if (sector < end_sect) { + req_sects = min(end_sect - sector, nr_sects); + res = __blkdev_issue_zeroout(bdev, sector, req_sects, gfp_mask); + if (res) { + bb.error = res; + req_sects = nr_sects; + end_sect = sector + nr_sects; + } + nr_sects -= req_sects; + sector = end_sect; + } while (nr_sects) { - unsigned int req_sects; - sector_t end_sect, tmp; - bio = bio_alloc(gfp_mask, 1); if (!bio) { ret = -ENOMEM; @@ -219,15 +265,12 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, * If splitting a request, and the next starting sector would be * misaligned, stop the discard at the previous aligned sector. */ - end_sect = sector + req_sects; - tmp = end_sect; - if (req_sects < nr_sects && - sector_div(tmp, granularity) != alignment) { - end_sect = end_sect - alignment; - sector_div(end_sect, granularity); - end_sect = end_sect * granularity + alignment; - req_sects = end_sect - sector; - } + end_sect = round_sect_down(sector + req_sects, granularity, + alignment); + if (end_sect <= sector) + break; + + req_sects = end_sect - sector; bio->bi_iter.bi_sector = sector; bio->bi_end_io = bio_batch_end_io; @@ -249,6 +292,9 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, */ cond_resched(); } + res = __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask); + if (res) + bb.error = res; blk_finish_plug(&plug); /* Wait for bios in-flight */