Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Baolin Wang" <baolin.wang@linux.alibaba.com>,
	akpm@linux-foundation.org, david@kernel.org, hughd@google.com
Cc: linux-mm@kvack.org
Subject: Re: [RFC PATCH] mm: shmem: fix discarded data left intact after truncation or hole punching
Date: Tue, 24 Nov 2026 12:26:56 +0000	[thread overview]
Message-ID: <sashiko-outbox-231134@kernel.org> (raw)
In-Reply-To: <6fcc15c466667f6943c34e130212b891b7471c83.1795436423.git.baolin.wang@linux.alibaba.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mm: shmem: shmem_get_partial_folio() swaps in whole
  non-straddling entries
- [High] mm: shmem: missing boundary zeroing when hole is faulted in
  during truncate
--

commit 44fc5f1a0ce8be88e4d2e1f2173306d27462ff3d
Author: Baolin Wang <baolin.wang@linux.alibaba.com>

mm: shmem: fix discarded data left intact after truncation or hole punching

This commit swaps in and locks boundary folios before updating i_size or
unmapping the range during truncation and hole punching, propagating swapin
errors to the syscall callers and passing the locked boundary folios to
shmem_undo_range().

> diff --git a/mm/shmem.c b/mm/shmem.c
> index 2b2b78dd24975..1e1d741e50dc2 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -1306,6 +1306,7 @@ static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index)
>  {
>  	const gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
>  	struct folio *folio;
> +	int error;
>  
>  retry:
>  	folio = filemap_get_entry(inode->i_mapping, index);
> @@ -1324,17 +1325,22 @@ static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index)
>  	 * Truncation can look up a large swap entry at or beyond i_size.
>  	 * Swap it in directly to bypass the size check in shmem_get_folio().
>  	 */
> -	if (shmem_swapin_folio(inode, index, &folio, SGP_READ, gfp, NULL, NULL) == -EEXIST)
> +	error = shmem_swapin_folio(inode, index, &folio, SGP_READ, gfp, NULL, NULL);
> +	if (error == -EEXIST)
>  		goto retry;
> +	if (error)
> +		return ERR_PTR(error);
>  	return folio;
>  }
>  
>  /*
>   * Remove range of pages and swap entries from page cache, and free them.
>   * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
> + * Boundary folios are locked and referenced. Zero them, split large ones,
> + * then unlock and put them. NULL means there is no boundary folio.
>   */
>  static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend,
> -								 bool unfalloc)
> +		struct folio *start_folio, struct folio *end_folio, bool unfalloc)
>  {
>  	struct address_space *mapping = inode->i_mapping;
>  	struct shmem_inode_info *info = SHMEM_I(inode);
> @@ -1392,25 +1398,19 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend,
>  	if (unfalloc)
>  		goto whole_folios;
>  
> -	same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT);
> -	folio = shmem_get_partial_folio(inode, lstart >> PAGE_SHIFT);
> -	if (folio) {
> -		same_folio = lend < folio_next_pos(folio);
> -		folio_mark_dirty(folio);
> -		truncate_inode_partial_folio(folio, lstart, lend, &start,
> +	if (start_folio) {
> +		same_folio = lend < folio_next_pos(start_folio);
> +		folio_mark_dirty(start_folio);
> +		truncate_inode_partial_folio(start_folio, lstart, lend, &start,
>  					     same_folio ? &end : NULL);
> -		folio_unlock(folio);
> -		folio_put(folio);
> -		folio = NULL;
> +		folio_unlock(start_folio);
> +		folio_put(start_folio);
>  	}
> -
> -	if (!same_folio)
> -		folio = shmem_get_partial_folio(inode, lend >> PAGE_SHIFT);
> -	if (folio) {
> -		folio_mark_dirty(folio);
> -		truncate_inode_partial_folio(folio, lstart, lend, NULL, &end);
> -		folio_unlock(folio);
> -		folio_put(folio);
> +	if (end_folio && end_folio != start_folio) {
> +		folio_mark_dirty(end_folio);
> +		truncate_inode_partial_folio(end_folio, lstart, lend, NULL, &end);
> +		folio_unlock(end_folio);
> +		folio_put(end_folio);
>  	}
>  
>  whole_folios:
> @@ -1513,12 +1513,39 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend,
>  	shmem_recalc_inode(inode, 0, -nr_swaps_freed);
>  }
>  
> -void shmem_truncate_range(struct inode *inode, loff_t lstart, uoff_t lend)
> +/* Check whether the end page of [lstart, lend] lies in the start folio. */
> +static bool is_same_boundary_folio(struct folio *start_folio,
> +		loff_t lstart, uoff_t lend)
>  {
> -	shmem_undo_range(inode, lstart, lend, false);
> +	if (start_folio)
> +		return folio_next_index(start_folio) > lend >> PAGE_SHIFT;
> +	return (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT);
> +}
> +
> +static void shmem_truncate_range_partial(struct inode *inode, loff_t lstart, loff_t lend,
> +		struct folio *start_folio, struct folio *end_folio)
> +{
> +	shmem_undo_range(inode, lstart, lend, start_folio, end_folio, false);
>  	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
>  	inode_inc_iversion(inode);
>  }
> +
> +void shmem_truncate_range(struct inode *inode, loff_t lstart, uoff_t lend)
> +{
> +	struct folio *start_folio = NULL, *end_folio = NULL;
> +
> +	/* This void interface retains best-effort handling of boundary errors. */
> +	start_folio = shmem_get_partial_folio(inode, lstart >> PAGE_SHIFT);
> +	if (IS_ERR(start_folio))
> +		start_folio = NULL;
> +
> +	if (lend != -1 && !is_same_boundary_folio(start_folio, lstart, lend)) {
> +		end_folio = shmem_get_partial_folio(inode, lend >> PAGE_SHIFT);
> +		if (IS_ERR(end_folio))
> +			end_folio = NULL;
> +	}
> +	shmem_truncate_range_partial(inode, lstart, lend, start_folio, end_folio);
> +}
>  EXPORT_SYMBOL_GPL(shmem_truncate_range);
>  
>  static int shmem_getattr(const struct mnt_idmap *idmap,
> @@ -1578,19 +1605,30 @@ static int shmem_setattr(const struct mnt_idmap *idmap,
>  	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
>  		loff_t oldsize = inode->i_size;
>  		loff_t newsize = attr->ia_size;
> +		struct folio *folio = NULL;
>  
>  		/* protected by i_rwsem */
>  		if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
>  		    (newsize > oldsize && (info->seals & F_SEAL_GROW)))
>  			return -EPERM;
>  
> +		if (newsize != oldsize && (info->flags & SHMEM_F_MAPPING_FROZEN))
> +			return -EPERM;
> +		if (newsize <= oldsize) {
> +			folio = shmem_get_partial_folio(inode, newsize >> PAGE_SHIFT);
> +			if (IS_ERR(folio))
> +				return PTR_ERR(folio);
> +		}

[Severity: High]
Does calling shmem_get_partial_folio() before shmem_undo_range() swap in
whole swap entries at lstart >> PAGE_SHIFT and lend >> PAGE_SHIFT even when
they do not straddle the range boundaries?

Previously, shmem_get_partial_folio() ran after the first
find_lock_entries() pass in shmem_undo_range(), which had already freed
non-straddling swap entries in [start, end - 1] via shmem_free_swap().
Looking up the boundary indices upfront means a page-aligned truncate (such
as ftruncate(fd, 0)), page-aligned hole punch, or shmem_truncate_range()
swaps in the entry at the boundary index only for
truncate_inode_partial_folio() to immediately discard the entire folio.

As a result, page-aligned ftruncate() and fallocate(FALLOC_FL_PUNCH_HOLE)
fail with -ENOMEM when a cgroup at its memory limit tries to free
swapped-out pages, and fail with -EIO if the boundary index holds a
swapin-error poison marker (regressing 59511d630a69 ("mm/shmem: fix
truncation of files containing only swapin-error markers")).

[ ... ]

> @@ -3911,7 +3949,9 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
>  		struct address_space *mapping = file->f_mapping;
>  		loff_t unmap_start = round_up(offset, PAGE_SIZE);
>  		loff_t unmap_end = round_down(offset + len, PAGE_SIZE);
> +		loff_t lend = offset + len - 1;
>  		DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
> +		struct folio *start_folio = NULL, *end_folio = NULL;
>  
>  		/* protected by i_rwsem */
>  		if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
> @@ -3919,6 +3959,23 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
>  			goto out;
>  		}
>  
> +		start_folio = shmem_get_partial_folio(inode, offset >> PAGE_SHIFT);
> +		if (IS_ERR(start_folio)) {
> +			error = PTR_ERR(start_folio);
> +			goto out;
> +		}
> +		if (!is_same_boundary_folio(start_folio, offset, lend)) {
> +			end_folio = shmem_get_partial_folio(inode, lend >> PAGE_SHIFT);
> +			if (IS_ERR(end_folio)) {
> +				error = PTR_ERR(end_folio);
> +				if (start_folio) {
> +					folio_unlock(start_folio);
> +					folio_put(start_folio);
> +				}
> +				goto out;
> +			}
> +		}
> +
>  		shmem_falloc.waitq = &shmem_falloc_waitq;
>  		shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
>  		shmem_falloc.next = unmap_end >> PAGE_SHIFT;
> @@ -3929,7 +3986,8 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset,
>  		if ((u64)unmap_end > (u64)unmap_start)
>  			unmap_mapping_range(mapping, unmap_start,
>  					    unmap_end - unmap_start, 0);
> -		shmem_truncate_range(inode, offset, offset + len - 1);
> +		shmem_truncate_range_partial(inode, offset, lend,
> +					     start_folio, end_folio);

[Severity: High]
Can a concurrent page fault populate an initially empty boundary page after
shmem_get_partial_folio() returns NULL, causing shmem_undo_range() to skip
zeroing it?

CPU0                                    CPU1
shmem_setattr() / shmem_fallocate()
  start_folio = shmem_get_partial_folio()
    -> NULL (hole)
                                        shmem_fault()
                                          allocates and maps folio at
                                          boundary index, writes data
  i_size_write() / unmap_mapping_range()
  shmem_undo_range(..., NULL, ...)
    find_lock_entries(mapping, start, ...)
    if (start_folio) -> skipped         <-- unaligned boundary not zeroed

When the boundary index starts as a hole, start_folio (or end_folio) is
NULL and no folio lock is held across the subsequent swapin of end_folio,
i_size_write(), unmap_mapping_range(), or the first find_lock_entries()
loop in shmem_undo_range().  Because both loops in shmem_undo_range() scan
only [start, end - 1] (where start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT
and end = (lend + 1) >> PAGE_SHIFT) and shmem_undo_range() no longer
re-checks the boundary indices when start_folio or end_folio is NULL, a
folio faulted into an unaligned boundary page during that window is left
with non-zero bytes in the truncated tail or punched hole.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/6fcc15c466667f6943c34e130212b891b7471c83.1795436423.git.baolin.wang@linux.alibaba.com?part=1


      reply	other threads:[~2026-11-24 12:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 12:26 [RFC PATCH] mm: shmem: fix discarded data left intact after truncation or hole punching Baolin Wang
2026-11-24 12:26 ` sashiko-bot [this message]

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=sashiko-outbox-231134@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=hughd@google.com \
    --cc=linux-mm@kvack.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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