From patchwork Thu Jul 5 16:01:44 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 1161621 Return-Path: X-Original-To: patchwork-dm-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from mx3-phx2.redhat.com (mx3-phx2.redhat.com [209.132.183.24]) by patchwork2.kernel.org (Postfix) with ESMTP id 21F40DFB7C for ; Thu, 5 Jul 2012 16:05:40 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx3-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q65G2PvF017941; Thu, 5 Jul 2012 12:02:25 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q65G2OmF006385 for ; Thu, 5 Jul 2012 12:02:24 -0400 Received: from mx1.redhat.com (ext-mx13.extmail.prod.ext.phx2.redhat.com [10.5.110.18]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q65G2JZp006250; Thu, 5 Jul 2012 12:02:19 -0400 Received: from mail-gg0-f174.google.com (mail-gg0-f174.google.com [209.85.161.174]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q65G2Icv006731; Thu, 5 Jul 2012 12:02:18 -0400 Received: by gglu4 with SMTP id u4so8399304ggl.33 for ; Thu, 05 Jul 2012 09:02:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=QBFB8sVi5Gyx/h56N4+55qS5WMZ4GfKJzPNUcyym5+g=; b=elcLuBhoL33r1JVt8/Gvsm5ZYc6abUu6Ty3quojB0Z4nLvWUkw4KwyHk7ZrrJHsote ubHBzbWKz2zcGhzqwakNACCn8pdNJ3Ayd0t/8s0+SJXGvP2rZ/cqLfLtTO6NvfhOamV6 b5iuOODtlMzQhfQ7Ws2JtSlYzH88zpuj/D67bRrm1RVe6ClPQ+yL839RYN/9bytsH05N OzKYP4lzg5ytPQ1Q+xGJeYAmqDSK0IPVlPnE62tMigqpVciGCqUMVEtzJZZsEPpkN5Ko TCRjAvzkhFfk/yVPBOpYFMdIbd1R7wcmQRgyKS2wdFJXjSrF4TYeDYwiaTd+Ol49C5xf b87g== Received: by 10.66.77.71 with SMTP id q7mr38317561paw.0.1341504137306; Thu, 05 Jul 2012 09:02:17 -0700 (PDT) Received: from yakj.usersys.redhat.com (93-34-189-113.ip51.fastwebnet.it. [93.34.189.113]) by mx.google.com with ESMTPS id tj4sm19998633pbc.33.2012.07.05.09.02.12 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 05 Jul 2012 09:02:16 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org Date: Thu, 5 Jul 2012 18:01:44 +0200 Message-Id: <1341504104-1674-3-git-send-email-pbonzini@redhat.com> In-Reply-To: <1341504104-1674-1-git-send-email-pbonzini@redhat.com> References: <1341504104-1674-1-git-send-email-pbonzini@redhat.com> X-RedHat-Spam-Score: -0.7 (DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS) X-RedHat-Possible-Forgery: Paolo Bonzini X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-Scanned-By: MIMEDefang 2.68 on 10.5.110.18 X-loop: dm-devel@redhat.com Cc: axboe@kernel.dk, snitzer@redhat.com, martin.petersen@oracle.com, david@fromorbit.com, xfs@oss.sgi.com, dm-devel@redhat.com, hch@lst.de, vgoyal@redhat.com Subject: [dm-devel] [PATCH v3 2/2] block: split discard into aligned requests X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk Reply-To: device-mapper development List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com When a disk has large discard_granularity and small max_discard_sectors, discards are not split with optimal alignment. In the limit case of discard_granularity == max_discard_sectors, no request could be aligned correctly, so in fact you might end up with no discarded logical blocks at all. Another example that helps showing the condition in the patch is with discard_granularity == 64, max_discard_sectors == 128. A request that is submitted for 256 sectors 2..257 will be split in two: 2..129, 130..257. However, only 2 aligned blocks out of 3 are included in the request; 128..191 may be left intact and not discarded. With this patch, the first request will be truncated to ensure good alignment of what's left, and the split will be 2..127, 128..255, 256..257. The patch will also take into account the discard_alignment. At most one extra request will be introduced, because the first request will be reduced by at most granularity-1 sectors, and granularity must be less than max_discard_sectors. Subsequent requests will run on round_down(max_discard_sectors, granularity) sectors, as in the current code. Cc: Jens Axboe Signed-off-by: Paolo Bonzini --- v2->v3: take into account partition alignment (Vivek) block/blk-lib.c | 34 ++++++++++++++++++++++++---------- include/linux/blkdev.h | 10 ++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 16b06f6..19cc761 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -44,7 +44,7 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, struct request_queue *q = bdev_get_queue(bdev); int type = REQ_WRITE | REQ_DISCARD; unsigned int max_discard_sectors; - unsigned int granularity; + unsigned int granularity, alignment, mask; struct bio_batch bb; struct bio *bio; int ret = 0; @@ -57,10 +57,12 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, /* Zero-sector (unknown) and one-sector granularities are the same. */ granularity = max(q->limits.discard_granularity >> 9, 1U); + mask = granularity - 1; + alignment = (bdev_discard_alignment(bdev) >> 9) & mask; /* * Ensure that max_discard_sectors is of the proper - * granularity + * granularity, so that requests stay aligned after a split. */ max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); max_discard_sectors = round_down(max_discard_sectors, granularity); @@ -80,25 +82,37 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, bb.wait = &wait; while (nr_sects) { + unsigned int req_sects; + sector_t end_sect; + bio = bio_alloc(gfp_mask, 1); if (!bio) { ret = -ENOMEM; break; } + req_sects = min_t(sector_t, nr_sects, max_discard_sectors); + + /* + * 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; + if (req_sects < nr_sects && (end_sect & mask) != alignment) { + end_sect = + round_down(end_sect - alignment, granularity) + + alignment; + req_sects = end_sect - sector; + } + bio->bi_sector = sector; bio->bi_end_io = bio_batch_end_io; bio->bi_bdev = bdev; bio->bi_private = &bb; - if (nr_sects > max_discard_sectors) { - bio->bi_size = max_discard_sectors << 9; - nr_sects -= max_discard_sectors; - sector += max_discard_sectors; - } else { - bio->bi_size = nr_sects << 9; - nr_sects = 0; - } + bio->bi_size = req_sects << 9; + nr_sects -= req_sects; + sector = end_sect; atomic_inc(&bb.done); submit_bio(type, bio); diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index ba43f40..218de98 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1125,6 +1125,16 @@ static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector & (lim->discard_granularity - 1); } +static inline int bdev_discard_alignment(struct block_device *bdev) +{ + struct request_queue *q = bdev_get_queue(bdev); + + if (bdev != bdev->bd_contains) + return bdev->bd_part->discard_alignment; + + return q->limits.discard_alignment; +} + static inline unsigned int queue_discard_zeroes_data(struct request_queue *q) { if (q->limits.max_discard_sectors && q->limits.discard_zeroes_data == 1)