All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shakeel Butt" <shakeel.butt@linux.dev>,
	"Tejun Heo" <tj@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: "Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Koutny" <mkoutny@suse.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Muchun Song" <muchun.song@linux.dev>, "Jan Kara" <jack@suse.cz>,
	"Matthew Wilcox" <willy@infradead.org>,
	"David Hildenbrand" <david@kernel.org>,
	"Vlastimil Babka" <vbabka@kernel.org>, "Zi Yan" <ziy@nvidia.com>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Nico Pache" <nico.pache@linux.dev>,
	"Usama Arif" <usama.arif@linux.dev>,
	"Kiryl Shutsemau" <kas@kernel.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Meta kernel team" <kernel-team@meta.com>,
	linux-mm@kvack.org, dsterba@suse.cz
Subject: Re: [RFC PATCH 3/7] mm, fs: handle memory.high overage in buffered reads and writes
Date: Sun, 22 Nov 2026 03:06:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-227658@kernel.org> (raw)
In-Reply-To: <20261122024737.2741569-4-shakeel.butt@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] mm/filemap: read corruption in filemap_read() if i_size grows
  during throttling
--

commit 0c9715c2d32c8a6aa3086d8505b1f86c7ebcf8cb
Author: Shakeel Butt <shakeel.butt@linux.dev>

mm, fs: handle memory.high overage in buffered reads and writes

This commit adds memory.high throttling hooks to buffered read and write
copy loops so overage is handled outside the memcg charge path.

> diff --git a/mm/filemap.c b/mm/filemap.c
> index dc0ceaa58a0f7..b0cefe71c1fdd 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c

[ ... ]

> @@ -2913,6 +2914,28 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,
>  				error = -EFAULT;
>  				break;
>  			}
> +
> +			/*
> +			 * A large read can charge a lot, for the page cache and
> +			 * for faulting in the buffer. Handle memory.high
> +			 * overage after each folio, as a batch of large folios
> +			 * can be big. Keep the batch, or reclaim could free
> +			 * folios not copied yet and they would be read again.
> +			 * The file may change meanwhile, so check its size and
> +			 * mappings again. Reads into kernel buffers skip this:
> +			 * their callers may hold other locks.
> +			 */
> +			if (user_backed_iter(iter) &&
> +			    !(iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ)) &&
> +			    mem_cgroup_over_high_pending()) {
> +				mem_cgroup_handle_over_high(high_gfp);
> +				isize = i_size_read(inode);
> +				if (iocb->ki_pos >= isize)
> +					break;
> +				end_offset = min_t(loff_t, isize,
> +						   iocb->ki_pos + iter->count);
> +				writably_mapped = mapping_writably_mapped(mapping);
> +			}
>  		}

[Severity: High]
Can refreshing isize and end_offset here corrupt the read buffer if
i_size grows while the reader is throttled?

filemap_get_pages() does not clamp the batch to i_size, so fbatch can
already hold uptodate folios past the initial end_offset (for example,
from a concurrent append that marked fbatch.folios[i + 1] uptodate
before calling i_size_write(), or after fallocate(FALLOC_FL_KEEP_SIZE)):

mm/filemap.c:filemap_read() {
    ...
		for (i = 0; i < folio_batch_count(&fbatch); i++) {
			struct folio *folio = fbatch.folios[i];
			size_t fsize = folio_size(folio);
			size_t offset = iocb->ki_pos & (fsize - 1);
			size_t bytes = min_t(loff_t, end_offset - iocb->ki_pos,
					     fsize - offset);
			size_t copied;

			if (end_offset < folio_pos(folio))
				break;
    ...
}

When end_offset lies inside fbatch.folios[i], iteration i copies only up
to end_offset (offset + copied < fsize), leaving iocb->ki_pos inside
fbatch.folios[i].  Normally the next iteration immediately breaks out of
the loop because end_offset < folio_pos(fbatch.folios[i + 1]).

If mem_cgroup_handle_over_high() runs at the end of iteration i and a
concurrent writer extends i_size past folio_pos(fbatch.folios[i + 1]),
end_offset is raised past folio_pos(fbatch.folios[i + 1]).  The loop
then advances to fbatch.folios[i + 1] while iocb->ki_pos still points
into fbatch.folios[i], skipping the rest of fbatch.folios[i] and copying
from fbatch.folios[i + 1] at the wrong file offset.

Breaking out of the fbatch loop when offset + copied < fsize (or before
re-evaluating a larger end_offset) would keep iocb->ki_pos aligned with
the current folio.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122024737.2741569-1-shakeel.butt@linux.dev?part=3


  reply	other threads:[~2026-11-22  3:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22  2:47 [RFC PATCH 0/7] memcg: effective high limit that avoids isolation issues Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 1/7] memcg: add helpers to handle memory.high overage in long operations Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 2/7] mm/gup: handle memory.high overage while populating Shakeel Butt
2026-11-22  3:06   ` sashiko-bot
2026-11-22  2:47 ` [RFC PATCH 3/7] mm, fs: handle memory.high overage in buffered reads and writes Shakeel Butt
2026-11-22  3:06   ` sashiko-bot [this message]
2026-11-22  2:47 ` [RFC PATCH 4/7] mm/shmem: handle memory.high overage in fallocate and reads Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 5/7] mm/khugepaged: handle memory.high overage in MADV_COLLAPSE Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 6/7] memcg: remove synchronous memory.high enforcement Shakeel Butt
2026-11-22  3:06   ` sashiko-bot
2026-11-23 17:15     ` Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 7/7] selftests/cgroup: make test_memcg_high_sync check that throttling holds Shakeel Butt

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-227658@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=brauner@kernel.org \
    --cc=david@kernel.org \
    --cc=dsterba@suse.cz \
    --cc=hannes@cmpxchg.org \
    --cc=jack@suse.cz \
    --cc=kas@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=nico.pache@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=tj@kernel.org \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=willy@infradead.org \
    --cc=ziy@nvidia.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.