Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp4481434ybz; Tue, 28 Apr 2020 12:02:56 -0700 (PDT) X-Google-Smtp-Source: APiQypKzbZSu8b+RGpr7XUD9t3ed00+r8MqrrkfGWGnky9M1DhXfB3Sk5qy7x7u+9EZzcpr/iWqK X-Received: by 2002:a05:6402:1a49:: with SMTP id bf9mr14661566edb.189.1588100576368; Tue, 28 Apr 2020 12:02:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588100576; cv=none; d=google.com; s=arc-20160816; b=L5d0u4yOClo32cFWgL8eVh3r4f+OWKmx/R9/7FRwLFOk6ARPR6XKRHh2qEJooI2MA5 C1IjpHOeJ1n+cikmYBkMQm48s1m5fIbnIxG+JM6XvgD8eMkQRdvyzV1/h0n4XlOj0mMm kWw/lQ0rhlpVDpVQ3WJEazSYfkMYpth2mnM8bqcAr1WHHCennJzgt9SXfdfTdVo9kxhe JoegUUYk0B22YSObx5Za8aajTb1hNcJNfir46O4lLjTxuBSP5MREnRL4LP/2QKkmex9h bL+FUNTVewITGJMrvIyg1FxrvU/ei6GuuQTWfbpcsa4d3vDsDZvIueoqBoBJ+RBTGONn fRyQ== 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=cPd8qG3j5OpNQ7R7sdUU3/9SL/eD3HoDcYStJSV/QrE=; b=YnAkWyGudn+liMtHVfGrddGW/1a9Lq2K9qcfGyTZASz46PRWqIAFwE7Hm8DMIdMm8F Ni7Jyj4TPg4W7nySeSfwTnqqvnZcOqLxxQNDYtcX4C2TWM0NeylpOQ1FnI07OkjzjXJ2 ceMGfspHlgOso2vaLwz0LSpylw+7enmAQGOWdcYX5MTPX7L7Xfz8xyelPq1QLbib+VV0 ybrDLY7l5Fojsm+EbFcwb3F5uvNK7plZfr8Z5udFSSdhsqSfbndWoXTqBZP2toJaEGCL w5Al8GcOCJXJa9ZfH/iyW8cYMBNF3/eTeBQzYGKvsDKJFU78Fq9Wxhycp8wNMeppjHLG X/gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oibexXFv; 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 oz15si2504864ejb.24.2020.04.28.12.02.31; Tue, 28 Apr 2020 12:02:56 -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=oibexXFv; 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 S1728915AbgD1TBA (ORCPT + 99 others); Tue, 28 Apr 2020 15:01:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:38686 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728840AbgD1S0w (ORCPT ); Tue, 28 Apr 2020 14:26:52 -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 9E48920730; Tue, 28 Apr 2020 18:26:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588098412; bh=Ivk+dFE3vBDfPp2V+SICdUuKd5Bd1SZYW4iiGwIWm9I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oibexXFvFYXUPBveNQ2DUZJmCjgzsVA6JDjd0hwslz+F3o1KRU3BofHdIMCIaZ9Bv 6YJn2EJKEvKrX574HYlcC/qfnhKaBIzgFjOevdGA9a7Z9ta8sFiaUr+HXFdpx5Pszw niZjxdIDshAiRPvt95R1ZCeXFU19WQOYHY/aTd8Q= 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.6 029/167] loop: Better discard support for block devices Date: Tue, 28 Apr 2020 20:23:25 +0200 Message-Id: <20200428182228.906846015@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200428182225.451225420@linuxfoundation.org> References: <20200428182225.451225420@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 739b372a51128..d943e713d5e34 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; } @@ -865,28 +866,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