Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp3762570ybc; Thu, 21 Nov 2019 13:21:58 -0800 (PST) X-Google-Smtp-Source: APXvYqyXUUwW+UUWKzjgGDrp8YXlfdKNpwYtI+G3DqK8E7vB7CUDrha5/liPoCvd76nUVzGr3jaK X-Received: by 2002:a17:906:6403:: with SMTP id d3mr16899325ejm.258.1574371318587; Thu, 21 Nov 2019 13:21:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574371318; cv=none; d=google.com; s=arc-20160816; b=UHTDh08IAj8Hh8i/Yon+Ao/gGhHUDdV8jmhbjEHjKmWfuFD+BKrGTpUkTgMydTrW0x BjvXV/RHFPU9JoRSe4kIKK01L3Iy/UJDUSDzbnxzmXXDfFtMVN1djY7qge0mQmnTwdzk 0T/9FkoJLupyT0h0fLPqsoneDW6d999e6btFNO0hk+9S7jNDNrSkVOFE6uNG4it/WJCq HlxccOBzLfXaJozT4x7tqy+eGQj42yPI/hs50vA8yRvgXED4ZBB2Wy1XTO7fmlg13Q1p GlnXvuIucp5qoQX0Yqzv9Ef5qNiONiuwxYSBm/yZXlRs6kv0EFIkQ4f84unPPmFoWDUo g1zQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=L59pxlP2h08giolehrbEu//J/zWHVNPRb2f26ecJqHM=; b=skpI2o59TwglK02Lqv4s1wYMoFzNw2HiaKrZ4mFAH4GIjoB1qyRnXc9PVGtiv7XZNO 7A/owfhGEDU3ycO7Ait9egtQX/X3f16zbsz4GtoMqgmvgBSccFd/He/DYnkLcpDpHKj2 uzQRsmFGvSSMYaqFSQue/zEs/AmwWwBqpz/uF8zyTfRU5MsHuhgjuQ5w9pkeClYFPdL+ hHDxUUZ+1dORItpxy+P2k926V1vVCKmoRH255Egj0WeF3CM/tbORQM5z+0vUVQGGcAwT eWGW9CvR6kxsyaW5Fs3ZXBTAOA5DW/TBoVBQTGDz5VEDRObcK2L8DkkSm9Pe6UV9brcE s9bQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cHI403mF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id oy7si2795104ejb.417.2019.11.21.13.21.33; Thu, 21 Nov 2019 13:21:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cHI403mF; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726658AbfKUVTf (ORCPT + 99 others); Thu, 21 Nov 2019 16:19:35 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:43152 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726293AbfKUVTf (ORCPT ); Thu, 21 Nov 2019 16:19:35 -0500 Received: by mail-lj1-f196.google.com with SMTP id y23so4842020ljh.10 for ; Thu, 21 Nov 2019 13:19:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=L59pxlP2h08giolehrbEu//J/zWHVNPRb2f26ecJqHM=; b=cHI403mFP70A3RgVqDv2UyS2js6BF1mI1YRiR/P7BDbQyTnZFaOYsC3SIL4X1UVXt9 jXdZ/NK2In5G19otIuHbirY05Xi5rSZE+FPhBdyzIgjqlMQXogxnYynlfCsG3zMU+pZR lAIMOqlTk8L0KfipBKDFixcUBmcWuT/u49X2Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=L59pxlP2h08giolehrbEu//J/zWHVNPRb2f26ecJqHM=; b=mpZwNwhZVkvL8SdYFxz2qL6cAe9elmgci4V0vSpqDofGx+J20DCIDVK95cmfXl6mL5 Gs6nlamefcanAO5q3CpTCu0jB9/HHIjg2L4IoFxy2IsQlNXhELOd5zli/6udn2z8RobW 09J92VKAaKghCCTn1fmiz2oF65DsuLw011OByA7wJuM3pA6nVm4D1zwEtnzZgguHrF25 MhkQIvgUB1zzhojAn3ome6WanwkHMXUjYwZSpNZ7mpaeWkMdiVhC7rTzfAyGK6O2QB19 0H9Me2JRkXb/bOmJcBvR2L54a7S4nUXG98WikFHxkUHegZnw9ADcvYzSw2LhArJ3zh9Z rWYA== X-Gm-Message-State: APjAAAUc+R+ZO5jioY4JN0XeuOzAkYVRlc5Pvq3NKfSltXbrXLWijUIb d2nLIcwI+evZdmzyu68DT1KhOecthak= X-Received: by 2002:a2e:9a95:: with SMTP id p21mr9222297lji.175.1574371171016; Thu, 21 Nov 2019 13:19:31 -0800 (PST) Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com. [209.85.208.176]) by smtp.gmail.com with ESMTPSA id m12sm2002295lfb.60.2019.11.21.13.19.28 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Nov 2019 13:19:29 -0800 (PST) Received: by mail-lj1-f176.google.com with SMTP id m4so4852708ljj.8 for ; Thu, 21 Nov 2019 13:19:28 -0800 (PST) X-Received: by 2002:a2e:9208:: with SMTP id k8mr9619555ljg.14.1574371167841; Thu, 21 Nov 2019 13:19:27 -0800 (PST) MIME-Version: 1.0 References: <20191114235008.185111-1-evgreen@chromium.org> <20191114154903.v7.2.I4d476bddbf41a61422ad51502f4361e237d60ad4@changeid> <20191120022518.GU6235@magnolia> <20191120191302.GV6235@magnolia> <20191120194507.GW6235@magnolia> In-Reply-To: <20191120194507.GW6235@magnolia> From: Evan Green Date: Thu, 21 Nov 2019 13:18:51 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v7 2/2] loop: Better discard support for block devices To: "Darrick J. Wong" Cc: Jens Axboe , Martin K Petersen , Gwendal Grignou , Christoph Hellwig , Ming Lei , Alexis Savery , Douglas Anderson , Bart Van Assche , Chaitanya Kulkarni , linux-block , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 20, 2019 at 11:45 AM Darrick J. Wong wrote: > > On Wed, Nov 20, 2019 at 11:25:48AM -0800, Evan Green wrote: > > On Wed, Nov 20, 2019 at 11:13 AM Darrick J. Wong > > wrote: > > > > > > On Wed, Nov 20, 2019 at 10:56:30AM -0800, Evan Green wrote: > > > > On Tue, Nov 19, 2019 at 6:25 PM Darrick J. Wong wrote: > > > > > > > > > > On Thu, Nov 14, 2019 at 03:50:08PM -0800, Evan Green wrote: > > > > > > If the backing device for a loop device is itself a block device, > > > > > > then mirror the "write zeroes" capabilities of the underlying > > > > > > block device into the loop device. Copy this capability into both > > > > > > max_write_zeroes_sectors and max_discard_sectors of the loop device. > > > > > > > > > > > > The reason for this is that REQ_OP_DISCARD on a loop device translates > > > > > > into blkdev_issue_zeroout(), rather than blkdev_issue_discard(). This > > > > > > presents a consistent interface for loop devices (that discarded data > > > > > > is zeroed), regardless of the backing device type of the loop device. > > > > > > There should be no behavior change for loop devices backed by regular > > > > > > files. > > > > (marking this spot for below) > > > > > > > > > > > > > > This change fixes blktest block/003, and removes an extraneous > > > > > > error print in block/013 when testing on a loop device backed > > > > > > by a block device that does not support discard. > > > > > > > > > > > > Signed-off-by: Evan Green > > > > > > Reviewed-by: Gwendal Grignou > > > > > > Reviewed-by: Chaitanya Kulkarni > > > > > > --- > > > > > > > > > > > > Changes in v7: > > > > > > - Rebase on top of Darrick's patch > > > > > > - Tweak opening line of commit description (Darrick) > > > > > > > > > > > > Changes in v6: None > > > > > > Changes in v5: > > > > > > - Don't mirror discard if lo_encrypt_key_size is non-zero (Gwendal) > > > > > > > > > > > > Changes in v4: > > > > > > - Mirror blkdev's write_zeroes into loopdev's discard_sectors. > > > > > > > > > > > > Changes in v3: > > > > > > - Updated commit description > > > > > > > > > > > > Changes in v2: None > > > > > > > > > > > > drivers/block/loop.c | 40 +++++++++++++++++++++++++++++----------- > > > > > > 1 file changed, 29 insertions(+), 11 deletions(-) > > > > > > > > > > > > diff --git a/drivers/block/loop.c b/drivers/block/loop.c > > > > > > index 6a9fe1f9fe84..e8f23e4b78f7 100644 > > > > > > --- a/drivers/block/loop.c > > > > > > +++ b/drivers/block/loop.c > > > > > > @@ -427,11 +427,12 @@ static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, > > > > > > * information. > > > > > > */ > > > > > > struct file *file = lo->lo_backing_file; > > > > > > + struct request_queue *q = lo->lo_queue; > > > > > > int ret; > > > > > > > > > > > > mode |= FALLOC_FL_KEEP_SIZE; > > > > > > > > > > > > - if ((!file->f_op->fallocate) || lo->lo_encrypt_key_size) { > > > > > > + if (!blk_queue_discard(q)) { > > > > > > ret = -EOPNOTSUPP; > > > > > > goto out; > > > > > > } > > > > > > @@ -862,6 +863,21 @@ static void loop_config_discard(struct loop_device *lo) > > > > > > struct file *file = lo->lo_backing_file; > > > > > > struct inode *inode = file->f_mapping->host; > > > > > > struct request_queue *q = lo->lo_queue; > > > > > > + struct request_queue *backingq; > > > > > > + > > > > > > + /* > > > > > > + * If the backing device is a block device, mirror its zeroing > > > > > > + * capability. REQ_OP_DISCARD translates to a zero-out even when backed > > > > > > + * by block devices to keep consistent behavior with file-backed loop > > > > > > + * devices. > > > > > > + */ Wait, I went to make this change and realized there's already a comment here. I can tweak the language a bit, but this is pretty much what you wanted, right? > > > > > > + if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) { > > > > > > + backingq = bdev_get_queue(inode->i_bdev); > > > > > > + blk_queue_max_discard_sectors(q, > > > > > > + backingq->limits.max_write_zeroes_sectors); > > > > > > > > > > max_discard_sectors? > > > > > > > > I didn't plumb max_discard_sectors because for my scenario it never > > > > ends up hitting the block device that way. > > > > > > > > The loop device either uses FL_ZERO_RANGE or FL_PUNCH_HOLE. When > > > > backed by a block device, that ends up in blkdev_fallocate(), which > > > > always translates both of those into blkdev_issue_zeroout(), not > > > > blkdev_issue_discard(). So it's really the zeroing capabilities of the > > > > block device that matters, even for loop discard operations. It seems > > > > weird, but I think this is the right thing because it presents a > > > > consistent interface to loop device users whether backed by a file > > > > system file, or directly by a block device. That is, a previously > > > > discarded range will read back as zeroes. > > > > > > Ah, right. Could you add this paragraph as a comment explaining why > > > we're setting max_discard_sectors from max_write_zeroes_sectors? > > > > Sure. I put an explanation in the commit description (see spot I > > marked above), but I agree a comment is probably also worthwhile. > > Sorry about the churn here. > > I have a strong preference towards documenting decisions like these > directly in the code because (a) I suck at reading patch prologues, (b) > someone reading the code after this gets committed will see it > immediately and right next to the relevant code, and (c) spelunking > through the git history of a file for commit messages is kind of clunky. > > Dunno if that's just my age showing (mmm, pre-bk linux) or what. :/ > > --D > > > > > > > --D > > > > > > > -Evan