Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp4458305ybz; Tue, 28 Apr 2020 11:38:02 -0700 (PDT) X-Google-Smtp-Source: APiQypKNftcGNyYuwWILkw1/63TEuliJIRnP+jVNVwice3Bl6X+7P2bAeH3sk05LBUEvutVmMX6n X-Received: by 2002:a50:8dc2:: with SMTP id s2mr8606652edh.318.1588099082339; Tue, 28 Apr 2020 11:38:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588099082; cv=none; d=google.com; s=arc-20160816; b=fqgztkG7NbJoqbNDIgmyaoXZVfDYekoyuZ6WlQDso0fsF5PVB9OhbB14r1LHVVoaBa sLdzXLRR/jKmlCqeQGQdZLiruZ0ra9ESalTQomCdTUe6vrqJJhJQvIQrGwcYx1h9Zvnq Ln/wph6+8bokkoCfbL8w347BZ4OjieWRfaRpnDbs/hvcREv0SeJ2avvDTXV7JMR4mbpj v/whUMTYil1Ffuv6KpUUXF5oGqz85ovw/VXo9cGB+B+SB7NsIdO7+hSMDXTcV1bR2o/X hITEQAA2xl03PtnDHGUtgbHZdt8VnpTce5PtznXpFqimw5da3enVvyg4NyiP8QtqNPm2 IlTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=t4ERmxtxVPUvPk097EfZCw7Ga9W04iQiXRVSJRKc7WU=; b=U0vDbiCVrdwXtxND7zaIwlI4ZsidkWgYG5qyWRCj/pJra9TV9hCCx/BvSxlP4AItP8 J7tRzomJLMey2HQA/Hs1JopSpxhvMDTWYx1L9T7uvT/JsTHpXvzDU42JAI8Gdq7SVloX zTG3rjcXRWszj+H5HsCzz5twRxaSt7qfcEJF2C+MBn8a32aIY4z9+vQdFytqiCAT+6DV Y+xmt8Hmrrg4xNvpuyZ5KIMw7rkbV87HgE2G1+a8fxLPEk8y4UCi/NZDi6jMWwzH3U0q Wr5qMTOeas/lZiLG6uBU5H5Hk66P0PlC0CvcFgvbstUu6cOQRo//9n/lT6oqJ9gCdsl8 bQKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VRUVJpeg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qp6si2333128ejb.16.2020.04.28.11.37.38; Tue, 28 Apr 2020 11:38:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VRUVJpeg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730232AbgD1SfU (ORCPT + 99 others); Tue, 28 Apr 2020 14:35:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:52384 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730230AbgD1SfS (ORCPT ); Tue, 28 Apr 2020 14:35:18 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 709BD20575; Tue, 28 Apr 2020 18:35:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588098917; bh=38NxofAiyb8p6dCyu/kgO+Z94MPWWVwNSISyDYxxWwQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VRUVJpegpHqIYlgUwuoyKT5Sjr2QoGy+Zti1tEtbOG6XkIMRQhbkt3yjQ8FFs6Nx0 p80Y9xEE3M5TW0GaqJ7cfrhsJU8gBie06jJ0eLHXrRZEQiEIY67qTh9EFRYkLDLHgq qPiXfkOIV8vomU3AKPZ4bmOb1kMFdVbuWhK/wvZM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Evan Green , Gwendal Grignou , Chaitanya Kulkarni , Andrzej Pietrasiewicz , Christoph Hellwig , Jens Axboe , Sasha Levin Subject: [PATCH 5.4 031/168] loop: Better discard support for block devices Date: Tue, 28 Apr 2020 20:23:25 +0200 Message-Id: <20200428182235.668410989@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200428182231.704304409@linuxfoundation.org> References: <20200428182231.704304409@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Evan Green [ Upstream commit c52abf563049e787c1341cdf15c7dbe1bfbc951b ] 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. 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 [used updated version of Evan's comment in loop_config_discard()] [moved backingq to local scope, removed redundant braces] Signed-off-by: Andrzej Pietrasiewicz Reviewed-by: Christoph Hellwig Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/block/loop.c | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index ef6e251857c8c..57ed6b70d2950 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; } @@ -863,28 +864,47 @@ static void loop_config_discard(struct loop_device *lo) struct inode *inode = file->f_mapping->host; struct request_queue *q = lo->lo_queue; + /* + * If the backing device is a block device, mirror its zeroing + * capability. Set the discard sectors to the block device's zeroing + * capabilities because loop discards result in blkdev_issue_zeroout(), + * not blkdev_issue_discard(). This maintains consistent behavior with + * file-backed loop devices: discarded regions read back as zero. + */ + if (S_ISBLK(inode->i_mode) && !lo->lo_encrypt_key_size) { + struct request_queue *backingq; + + backingq = bdev_get_queue(inode->i_bdev); + blk_queue_max_discard_sectors(q, + backingq->limits.max_write_zeroes_sectors); + + blk_queue_max_write_zeroes_sectors(q, + backingq->limits.max_write_zeroes_sectors); + /* * We use punch hole to reclaim the free space used by the * image a.k.a. discard. However we do not support discard if * encryption is enabled, because it may give an attacker * useful information. */ - if ((!file->f_op->fallocate) || - lo->lo_encrypt_key_size) { + } else if (!file->f_op->fallocate || lo->lo_encrypt_key_size) { q->limits.discard_granularity = 0; q->limits.discard_alignment = 0; blk_queue_max_discard_sectors(q, 0); blk_queue_max_write_zeroes_sectors(q, 0); - blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); - return; - } - q->limits.discard_granularity = inode->i_sb->s_blocksize; - q->limits.discard_alignment = 0; + } else { + q->limits.discard_granularity = inode->i_sb->s_blocksize; + q->limits.discard_alignment = 0; - blk_queue_max_discard_sectors(q, UINT_MAX >> 9); - blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); - blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); + blk_queue_max_discard_sectors(q, UINT_MAX >> 9); + blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9); + } + + if (q->limits.max_write_zeroes_sectors) + blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); + else + blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q); } static void loop_unprepare_queue(struct loop_device *lo) -- 2.20.1