stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Eryu Guan <eguan@redhat.com>,
	Brian Foster <bfoster@redhat.com>,
	Dave Chinner <dchinner@redhat.com>,
	"Darrick J. Wong" <darrick.wong@oracle.com>
Subject: [PATCH 4.13 84/85] xfs: trim writepage mapping to within eof
Date: Tue, 24 Oct 2017 15:07:58 +0200	[thread overview]
Message-ID: <20171024125657.539724418@linuxfoundation.org> (raw)
In-Reply-To: <20171024125654.028122623@linuxfoundation.org>

4.13-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Brian Foster <bfoster@redhat.com>

commit 40214d128e07dd21bb07a8ed6a7fe2f911281ab2 upstream.

The writeback rework in commit fbcc02561359 ("xfs: Introduce
writeback context for writepages") introduced a subtle change in
behavior with regard to the block mapping used across the
->writepages() sequence. The previous xfs_cluster_write() code would
only flush pages up to EOF at the time of the writepage, thus
ensuring that any pages due to file-extending writes would be
handled on a separate cycle and with a new, updated block mapping.

The updated code establishes a block mapping in xfs_writepage_map()
that could extend beyond EOF if the file has post-eof preallocation.
Because we now use the generic writeback infrastructure and pass the
cached mapping to each writepage call, there is no implicit EOF
limit in place. If eofblocks trimming occurs during ->writepages(),
any post-eof portion of the cached mapping becomes invalid. The
eofblocks code has no means to serialize against writeback because
there are no pages associated with post-eof blocks. Therefore if an
eofblocks trim occurs and is followed by a file-extending buffered
write, not only has the mapping become invalid, but we could end up
writing a page to disk based on the invalid mapping.

Consider the following sequence of events:

- A buffered write creates a delalloc extent and post-eof
  speculative preallocation.
- Writeback starts and on the first writepage cycle, the delalloc
  extent is converted to real blocks (including the post-eof blocks)
  and the mapping is cached.
- The file is closed and xfs_release() trims post-eof blocks. The
  cached writeback mapping is now invalid.
- Another buffered write appends the file with a delalloc extent.
- The concurrent writeback cycle picks up the just written page
  because the writeback range end is LLONG_MAX. xfs_writepage_map()
  attributes it to the (now invalid) cached mapping and writes the
  data to an incorrect location on disk (and where the file offset is
  still backed by a delalloc extent).

This problem is reproduced by xfstests test generic/464, which
triggers racing writes, appends, open/closes and writeback requests.

To address this problem, trim the mapping used during writeback to
within EOF when the mapping is validated. This ensures the mapping
is revalidated for any pages encountered beyond EOF as of the time
the current mapping was cached or last validated.

Reported-by: Eryu Guan <eguan@redhat.com>
Diagnosed-by: Eryu Guan <eguan@redhat.com>
Signed-off-by: Brian Foster <bfoster@redhat.com>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 fs/xfs/libxfs/xfs_bmap.c |   11 +++++++++++
 fs/xfs/libxfs/xfs_bmap.h |    1 +
 fs/xfs/xfs_aops.c        |   13 +++++++++++++
 3 files changed, 25 insertions(+)

--- a/fs/xfs/libxfs/xfs_bmap.c
+++ b/fs/xfs/libxfs/xfs_bmap.c
@@ -3860,6 +3860,17 @@ xfs_trim_extent(
 	}
 }
 
+/* trim extent to within eof */
+void
+xfs_trim_extent_eof(
+	struct xfs_bmbt_irec	*irec,
+	struct xfs_inode	*ip)
+
+{
+	xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
+					      i_size_read(VFS_I(ip))));
+}
+
 /*
  * Trim the returned map to the required bounds
  */
--- a/fs/xfs/libxfs/xfs_bmap.h
+++ b/fs/xfs/libxfs/xfs_bmap.h
@@ -208,6 +208,7 @@ void	xfs_bmap_trace_exlist(struct xfs_in
 
 void	xfs_trim_extent(struct xfs_bmbt_irec *irec, xfs_fileoff_t bno,
 		xfs_filblks_t len);
+void	xfs_trim_extent_eof(struct xfs_bmbt_irec *, struct xfs_inode *);
 int	xfs_bmap_add_attrfork(struct xfs_inode *ip, int size, int rsvd);
 void	xfs_bmap_local_to_extents_empty(struct xfs_inode *ip, int whichfork);
 void	xfs_bmap_add_free(struct xfs_mount *mp, struct xfs_defer_ops *dfops,
--- a/fs/xfs/xfs_aops.c
+++ b/fs/xfs/xfs_aops.c
@@ -433,6 +433,19 @@ xfs_imap_valid(
 {
 	offset >>= inode->i_blkbits;
 
+	/*
+	 * We have to make sure the cached mapping is within EOF to protect
+	 * against eofblocks trimming on file release leaving us with a stale
+	 * mapping. Otherwise, a page for a subsequent file extending buffered
+	 * write could get picked up by this writeback cycle and written to the
+	 * wrong blocks.
+	 *
+	 * Note that what we really want here is a generic mapping invalidation
+	 * mechanism to protect us from arbitrary extent modifying contexts, not
+	 * just eofblocks.
+	 */
+	xfs_trim_extent_eof(imap, XFS_I(inode));
+
 	return offset >= imap->br_startoff &&
 		offset < imap->br_startoff + imap->br_blockcount;
 }

  parent reply	other threads:[~2017-10-24 13:21 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-24 13:06 [PATCH 4.13 00/85] 4.13.10-stable review Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 01/85] staging: bcm2835-audio: Fix memory corruption Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 02/85] USB: devio: Revert "USB: devio: Dont corrupt user memory" Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 03/85] USB: core: fix out-of-bounds access bug in usb_get_bos_descriptor() Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 04/85] USB: serial: metro-usb: add MS7820 device id Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 05/85] usb: cdc_acm: Add quirk for Elatec TWN3 Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 07/85] usb: hub: Allow reset retry for USB2 devices on connect bounce Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 08/85] ALSA: usb-audio: Add native DSD support for Pro-Ject Pre Box S2 Digital Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 09/85] can: gs_usb: fix busy loop if no more TX context is available Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 10/85] scsi: qla2xxx: Fix uninitialized work element Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 11/85] nbd: dont set the device size until were connected Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 12/85] s390/cputime: fix guest/irq/softirq times after CPU hotplug Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 13/85] parisc: Fix double-word compare and exchange in LWS code on 32-bit kernels Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 14/85] parisc: Fix detection of nonsynchronous cr16 cycle counters Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 15/85] iio: dummy: events: Add missing break Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 16/85] usb: musb: sunxi: Explicitly release USB PHY on exit Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 17/85] USB: musb: fix session-bit runtime-PM quirk Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 18/85] USB: musb: fix late external abort on suspend Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 19/85] usb: musb: musb_cppi41: Fix the address of teardown and autoreq registers Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 20/85] usb: musb: musb_cppi41: Fix cppi41_set_dma_mode() for DA8xx Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 21/85] usb: musb: musb_cppi41: Configure the number of channels " Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 22/85] usb: musb: Check for host-mode using is_host_active() on reset interrupt Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 23/85] xhci: Identify USB 3.1 capable hosts by their port protocol capability Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 24/85] xhci: Cleanup current_cmd in xhci_cleanup_command_queue() Greg Kroah-Hartman
2017-10-24 13:06 ` [PATCH 4.13 25/85] usb: xhci: Reset halted endpoint if trb is noop Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 26/85] usb: xhci: Handle error condition in xhci_stop_device() Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 28/85] can: af_can: can_pernet_init(): add missing error handling for kzalloc returning NULL Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 29/85] can: flexcan: fix state transition regression Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 30/85] can: flexcan: rename legacy error state quirk Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 31/85] can: flexcan: implement error passive " Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 32/85] can: flexcan: fix i.MX6 state transition issue Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 33/85] can: flexcan: fix i.MX28 " Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 34/85] can: flexcan: fix p1010 " Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 35/85] KEYS: encrypted: fix dereference of NULL user_key_payload Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 36/85] mmc: sdhci-pci: Fix default d3_retune for Intel host controllers Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 38/85] drm/nouveau/kms/nv50: fix oops during DP IRQ handling on non-MST boards Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 39/85] drm/nouveau/bsp/g92: disable by default Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 40/85] drm/nouveau/mmu: flush tlbs before deleting page tables Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 41/85] media: s5p-cec: add NACK detection support Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 42/85] media: cec: Respond to unregistered initiators, when applicable Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 43/85] media: dvb: i2c transfers over usb cannot be done from stack Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 44/85] tracing/samples: Fix creation and deletion of simple_thread_fn creation Greg Kroah-Hartman
2017-10-30 19:57   ` Steven Rostedt
2017-10-30 21:17     ` Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 45/85] ALSA: seq: Enable use locking in all configurations Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 46/85] ALSA: hda: Remove superfluous - added by printk conversion Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 47/85] ALSA: hda: Abort capability probe at invalid register read Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 48/85] i2c: ismt: Separate I2C block read from SMBus block read Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 50/85] Revert "tools/power turbostat: stop migrating, unless -m" Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 51/85] Input: stmfts - fix setting ABS_MT_POSITION_* maximum size Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 52/85] brcmfmac: Add check for short event packets Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 53/85] brcmsmac: make some local variables static const to reduce stack size Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 54/85] ARM: dts: sun6i: Fix endpoint IDs in second display pipeline Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 55/85] bus: mbus: fix window size calculation for 4GB windows Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 56/85] clockevents/drivers/cs5535: Improve resilience to spurious interrupts Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 57/85] rtlwifi: rtl8821ae: Fix connection lost problem Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 58/85] x86/microcode/intel: Disable late loading on model 79 Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 59/85] lib/digsig: fix dereference of NULL user_key_payload Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 60/85] fscrypt: " Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 61/85] ecryptfs: " Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 62/85] KEYS: Fix race between updating and finding a negative key Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 63/85] FS-Cache: fix dereference of NULL user_key_payload Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 64/85] KEYS: dont let add_key() update an uninstantiated key Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 65/85] pkcs7: Prevent NULL pointer dereference, since sinfo is not always set Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 66/85] arm64: dts: rockchip: correct vqmmc voltage for rk3399 platforms Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 67/85] ALSA: hda - Fix incorrect TLV callback check introduced during set_fs() removal Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 68/85] iomap_dio_rw: Allocate AIO completion queue before submitting dio Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 69/85] xfs: dont unconditionally clear the reflink flag on zero-block files Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 70/85] xfs: evict CoW fork extents when performing finsert/fcollapse Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 71/85] fs/xfs: Use %pS printk format for direct addresses Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 72/85] xfs: report zeroed or not correctly in xfs_zero_range() Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 73/85] xfs: update i_size after unwritten conversion in dio completion Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 74/85] xfs: perag initialization should only touch m_ag_max_usable for AG 0 Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 75/85] xfs: Capture state of the right inode in xfs_iflush_done Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 76/85] xfs: always swap the cow forks when swapping extents Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 77/85] xfs: handle racy AIO in xfs_reflink_end_cow Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 78/85] xfs: Dont log uninitialised fields in inode structures Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 79/85] xfs: move more RT specific code under CONFIG_XFS_RT Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 80/85] xfs: dont change inode mode if ACL update fails Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 81/85] xfs: reinit btree pointer on attr tree inactivation walk Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 82/85] xfs: handle error if xfs_btree_get_bufs fails Greg Kroah-Hartman
2017-10-24 13:07 ` [PATCH 4.13 83/85] xfs: cancel dirty pages on invalidation Greg Kroah-Hartman
2017-10-24 13:07 ` Greg Kroah-Hartman [this message]
2017-10-24 13:07 ` [PATCH 4.13 85/85] xfs: move two more RT specific functions into CONFIG_XFS_RT Greg Kroah-Hartman
2017-10-25 21:09   ` Arnd Bergmann
2017-10-26  7:09     ` Greg Kroah-Hartman
2017-10-24 21:28 ` [PATCH 4.13 00/85] 4.13.10-stable review Guenter Roeck
2017-10-25  6:48   ` Greg Kroah-Hartman

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=20171024125657.539724418@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=bfoster@redhat.com \
    --cc=darrick.wong@oracle.com \
    --cc=dchinner@redhat.com \
    --cc=eguan@redhat.com \
    --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).