public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Andreas Gruenbacher <agruenba@redhat.com>,
	Jan Kara <jack@suse.cz>, Sasha Levin <sashal@kernel.org>,
	rpeterso@redhat.com, cluster-devel@redhat.com
Subject: [PATCH AUTOSEL 6.1 13/26] gfs2: Don't get stuck writing page onto itself under direct I/O
Date: Fri, 16 Jun 2023 06:26:10 -0400	[thread overview]
Message-ID: <20230616102625.673454-13-sashal@kernel.org> (raw)
In-Reply-To: <20230616102625.673454-1-sashal@kernel.org>

From: Andreas Gruenbacher <agruenba@redhat.com>

[ Upstream commit fa58cc888d67e640e354d8b3ceef877ea167b0cf ]

When a direct I/O write is performed, iomap_dio_rw() invalidates the
part of the page cache which the write is going to before carrying out
the write.  In the odd case, the direct I/O write will be reading from
the same page it is writing to.  gfs2 carries out writes with page
faults disabled, so it should have been obvious that this page
invalidation can cause iomap_dio_rw() to never make any progress.
Currently, gfs2 will end up in an endless retry loop in
gfs2_file_direct_write() instead, though.

Break this endless loop by limiting the number of retries and falling
back to buffered I/O after that.

Also simplify should_fault_in_pages() sightly and add a comment to make
the above case easier to understand.

Reported-by: Jan Kara <jack@suse.cz>
Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 fs/gfs2/file.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c
index 60c6fb91fb589..bc6cd5f4b1077 100644
--- a/fs/gfs2/file.c
+++ b/fs/gfs2/file.c
@@ -783,9 +783,13 @@ static inline bool should_fault_in_pages(struct iov_iter *i,
 	if (!user_backed_iter(i))
 		return false;
 
+	/*
+	 * Try to fault in multiple pages initially.  When that doesn't result
+	 * in any progress, fall back to a single page.
+	 */
 	size = PAGE_SIZE;
 	offs = offset_in_page(iocb->ki_pos);
-	if (*prev_count != count || !*window_size) {
+	if (*prev_count != count) {
 		size_t nr_dirtied;
 
 		nr_dirtied = max(current->nr_dirtied_pause -
@@ -869,6 +873,7 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
 	struct gfs2_inode *ip = GFS2_I(inode);
 	size_t prev_count = 0, window_size = 0;
 	size_t written = 0;
+	bool enough_retries;
 	ssize_t ret;
 
 	/*
@@ -912,11 +917,17 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
 	if (ret > 0)
 		written = ret;
 
+	enough_retries = prev_count == iov_iter_count(from) &&
+			 window_size <= PAGE_SIZE;
 	if (should_fault_in_pages(from, iocb, &prev_count, &window_size)) {
 		gfs2_glock_dq(gh);
 		window_size -= fault_in_iov_iter_readable(from, window_size);
-		if (window_size)
-			goto retry;
+		if (window_size) {
+			if (!enough_retries)
+				goto retry;
+			/* fall back to buffered I/O */
+			ret = 0;
+		}
 	}
 out_unlock:
 	if (gfs2_holder_queued(gh))
-- 
2.39.2


  parent reply	other threads:[~2023-06-16 10:30 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-16 10:25 [PATCH AUTOSEL 6.1 01/26] Input: soc_button_array - add invalid acpi_index DMI quirk handling Sasha Levin
2023-06-16 10:25 ` [PATCH AUTOSEL 6.1 02/26] arm64: dts: qcom: sc7280-idp: drop incorrect dai-cells from WCD938x SDW Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 03/26] arm64: dts: qcom: sc7280-qcard: " Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 04/26] s390/cio: unregister device when the only path is gone Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 05/26] spi: lpspi: disable lpspi module irq in DMA mode Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 06/26] ASoC: codecs: wcd938x-sdw: do not set can_multi_write flag Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 07/26] ASoC: simple-card: Add missing of_node_put() in case of error Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 08/26] soundwire: dmi-quirks: add new mapping for HP Spectre x360 Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 09/26] soundwire: qcom: add proper error paths in qcom_swrm_startup() Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 10/26] ASoC: nau8824: Add quirk to active-high jack-detect Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 11/26] ASoC: amd: yc: Add Thinkpad Neo14 to quirks list for acp6x Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 12/26] drm/ast: Fix modeset failed on DisplayPort Sasha Levin
2023-06-16 10:26 ` Sasha Levin [this message]
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 14/26] s390/purgatory: disable branch profiling Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 15/26] ASoC: fsl_sai: Enable BCI bit if SAI works on synchronous mode with BYP asserted Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 16/26] ALSA: hda/realtek: Add "Intel Reference board" and "NUC 13" SSID in the ALC256 Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 17/26] i2c: mchp-pci1xxxx: Avoid cast to incompatible function type Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 18/26] ARM: dts: Fix erroneous ADS touchscreen polarities Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 19/26] null_blk: Fix: memory release when memory_backed=1 Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 20/26] drm/exynos: vidi: fix a wrong error return Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 21/26] drm/exynos: fix race condition UAF in exynos_g2d_exec_ioctl Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 22/26] drm/radeon: fix race condition UAF in radeon_gem_set_domain_ioctl Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 23/26] Revert "ext4: don't clear SB_RDONLY when remounting r/w until quota is re-enabled" Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 24/26] ext4: only check dquot_initialize_needed() when debugging Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 25/26] vhost_vdpa: tell vqs about the negotiated Sasha Levin
2023-06-16 10:26 ` [PATCH AUTOSEL 6.1 26/26] vhost_net: revert upend_idx only on retriable error 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=20230616102625.673454-13-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=agruenba@redhat.com \
    --cc=cluster-devel@redhat.com \
    --cc=jack@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rpeterso@redhat.com \
    --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