linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Evan Green <evgreen@chromium.org>,
	Gwendal Grignou <gwendal@chromium.org>,
	Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>,
	Andrzej Pietrasiewicz <andrzej.p@collabora.com>,
	Christoph Hellwig <hch@lst.de>, Jens Axboe <axboe@kernel.dk>,
	Sasha Levin <sashal@kernel.org>,
	linux-block@vger.kernel.org
Subject: [PATCH AUTOSEL 5.4 33/78] loop: Better discard support for block devices
Date: Sat, 18 Apr 2020 10:40:02 -0400	[thread overview]
Message-ID: <20200418144047.9013-33-sashal@kernel.org> (raw)
In-Reply-To: <20200418144047.9013-1-sashal@kernel.org>

From: Evan Green <evgreen@chromium.org>

[ 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 <evgreen@chromium.org>
Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
[used updated version of Evan's comment in loop_config_discard()]
[moved backingq to local scope, removed redundant braces]
Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 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


  parent reply	other threads:[~2020-04-18 14:41 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-18 14:39 [PATCH AUTOSEL 5.4 01/78] iommu/amd: Fix the configuration of GCR3 table root pointer Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 02/78] tools/testing/nvdimm: Fix compilation failure without CONFIG_DEV_DAX_PMEM_COMPAT Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 03/78] watchdog: reset last_hw_keepalive time at start Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 04/78] ovl: fix value of i_ino for lower hardlink corner case Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 05/78] iommu/vt-d: Fix page request descriptor size Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 06/78] acpi/nfit: improve bounds checking for 'func' Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 07/78] iommu/vt-d: Fix mm reference leak Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 08/78] scsi: lpfc: Fix kasan slab-out-of-bounds error in lpfc_unreg_login Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 09/78] scsi: lpfc: Fix crash after handling a pci error Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 10/78] scsi: lpfc: Fix crash in target side cable pulls hitting WAIT_FOR_UNREG Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 11/78] scsi: libfc: If PRLI rejected, move rport to PLOGI state Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 12/78] ceph: return ceph_mdsc_do_request() errors from __get_parent() Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 13/78] ceph: don't skip updating wanted caps when cap is stale Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 14/78] pwm: rcar: Fix late Runtime PM enablement Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 15/78] nvme-tcp: fix possible crash in write_zeroes processing Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 16/78] ASoC: dpcm: allow start or stop during pause for backend Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 17/78] scsi: iscsi: Report unbind session event when the target has been removed Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 18/78] tools/test/nvdimm: Fix out of tree build Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 19/78] ASoC: Intel: atom: Take the drv->lock mutex before calling sst_send_slot_map() Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 20/78] nvme: fix deadlock caused by ANA update wrong locking Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 21/78] drm/amd/display: Update stream adjust in dc_stream_adjust_vmin_vmax Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 22/78] dma-direct: fix data truncation in dma_direct_get_required_mask() Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 23/78] dma-debug: fix displaying of dma allocation type Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 24/78] kernel/gcov/fs.c: gcov_seq_next() should increase position index Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 25/78] selftests: kmod: fix handling test numbers above 9 Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 26/78] ipc/util.c: sysvipc_find_ipc() should increase position index Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 27/78] kconfig: qconf: Fix a few alignment issues Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 28/78] lib/raid6/test: fix build on distros whose /bin/sh is not bash Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 29/78] KVM: s390: vsie: Fix possible race when shadowing region 3 tables Sasha Levin
2020-04-18 14:39 ` [PATCH AUTOSEL 5.4 30/78] drm/nouveau: workaround runpm fail by disabling PCI power management on certain intel bridges Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 31/78] s390/cio: generate delayed uevent for vfio-ccw subchannels Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 32/78] s390/cio: avoid duplicated 'ADD' uevents Sasha Levin
2020-04-18 14:40 ` Sasha Levin [this message]
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 34/78] pwm: pca9685: Fix PWM/GPIO inter-operation Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 35/78] Revert "powerpc/64: irq_work avoid interrupt when called with hardware irqs enabled" Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 36/78] powerpc/pseries: Fix MCE handling on pseries Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 37/78] drm/amdkfd: kfree the wrong pointer Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 38/78] nvme: fix compat address handling in several ioctls Sasha Levin
2020-04-28  4:53   ` Naresh Kamboju
2020-04-28  5:23     ` Nick Bowler
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 39/78] pwm: renesas-tpu: Fix late Runtime PM enablement Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 40/78] pwm: bcm2835: Dynamically allocate base Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 41/78] drm/vc4: Fix HDMI mode validation Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 42/78] iommu/virtio: Fix freeing of incomplete domains Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 43/78] iommu/vt-d: Silence RCU-list debugging warning in dmar_find_atsr() Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 44/78] platform/chrome: cros_ec_rpmsg: Fix race with host event Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 45/78] libnvdimm: Out of bounds read in __nd_ioctl() Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 46/78] ocfs2: no need try to truncate file beyond i_size Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 47/78] hfsplus: fix crash and filesystem corruption when deleting files Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 48/78] ALSA: hda: Add driver blacklist Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 49/78] ALSA: hda/realtek - Add quirk for MSI GL63 Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 50/78] perf/core: Disable page faults when getting phys address Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 51/78] libata: Return correct status in sata_pmp_eh_recover_pm() when ATA_DFLAG_DETACH is set Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 52/78] drm/amd/display: Calculate scaling ratios on every medium/full update Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 53/78] ALSA: ice1724: Fix invalid access for enumerated ctl items Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 54/78] ALSA: hda: Fix potential access overflow in beep helper Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 55/78] KVM: s390: vsie: Fix delivery of addressing exceptions Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 56/78] ASoC: Intel: bytcr_rt5640: Add quirk for MPMAN MPWIN895CL tablet Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 57/78] ipmi: fix hung processes in __get_guid() Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 58/78] ALSA: usb-audio: Add Pioneer DJ DJM-250MK2 quirk Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 59/78] ALSA: hda/realtek - Add quirk for Lenovo Carbon X1 8th gen Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 60/78] scsi: mpt3sas: Fix kernel panic observed on soft HBA unplug Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 61/78] xhci: Ensure link state is U3 after setting USB_SS_PORT_LS_U3 Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 62/78] xhci: Wait until link state trainsits to U0 after setting USB_SS_PORT_LS_U0 Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 63/78] xhci: Finetune host initiated USB3 rootport link suspend and resume Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 64/78] drm/amd/display: Not doing optimize bandwidth if flip pending Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 65/78] PCI/PM: Add pcie_wait_for_link_delay() Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 66/78] libbpf: Fix readelf output parsing on powerpc with recent binutils Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 67/78] PCI: pciehp: Prevent deadlock on disconnect Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 68/78] ASoC: SOF: trace: fix unconditional free in trace release Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 69/78] powerpc/powernv/ioda: Fix ref count for devices with their own PE Sasha Levin
2020-04-21 11:02   ` Frederic Barrat
2020-04-25 15:00     ` Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 70/78] pci/hotplug/pnv-php: Remove erroneous warning Sasha Levin
2020-04-21 11:03   ` Frederic Barrat
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 71/78] ocxl: Add PCI hotplug dependency to Kconfig Sasha Levin
2020-04-21 11:05   ` Frederic Barrat
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 72/78] tracing/selftests: Turn off timeout setting Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 73/78] virtio-blk: improve virtqueue error to BLK_STS Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 74/78] scsi: smartpqi: fix controller lockup observed during force reboot Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 75/78] scsi: smartpqi: fix call trace in device discovery Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 76/78] scsi: smartpqi: fix problem with unique ID for physical device Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 77/78] PCI/ASPM: Allow re-enabling Clock PM Sasha Levin
2020-04-18 14:40 ` [PATCH AUTOSEL 5.4 78/78] PCI/PM: Add missing link delays required by the PCIe spec Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200418144047.9013-33-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=andrzej.p@collabora.com \
    --cc=axboe@kernel.dk \
    --cc=chaitanya.kulkarni@wdc.com \
    --cc=evgreen@chromium.org \
    --cc=gwendal@chromium.org \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).