* [PATCH] drm/i915/gem: Skip writeback of shmem folios with extra references
@ 2026-11-19 13:42 Kiryl Shutsemau (Meta)
2026-11-19 17:43 ` Andi Shyti
0 siblings, 1 reply; 5+ messages in thread
From: Kiryl Shutsemau (Meta) @ 2026-11-19 13:42 UTC (permalink / raw)
To: Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin
Cc: Kiryl Shutsemau (Meta),
David Airlie, Simona Vetter, Andi Shyti, David Hildenbrand,
Hugh Dickins, Baolin Wang, intel-gfx, dri-devel, linux-mm,
linux-kernel, kernel-team
__shmem_writeback() writes out every dirty folio that is not mapped. A
folio stops being mapped when munmap() removes its rmap, but the TLB
flush comes later. Until then another thread can still store to it
through a stale TLB entry:
CPU0 (munmap) CPU1 (same mm) CPU2 (i915 shrinker)
zap_huge_pmd()
folio_remove_rmap_pmd()
__shmem_writeback()
clear dirty
!folio_mapped()
shmem_write_folio()
store via stale TLB
tlb_finish_mmu()
The store leaves the folio clean in the swap cache. Once reclaim frees
it, the object reads back from swap without that store.
The legacy mmap ioctl maps the shmem file directly, and i915 mounts it
with huge=within_size on some platforms, so the folio can be PMD-mapped.
The core mm keeps the rmap until after the flush for PTEs, but not for
shmem PMDs: shmem has no writeback, and pageout() refuses a folio with
an extra reference.
Do the same here. The unmapping side holds its reference until the TLB
is flushed, so a folio with references beyond the page cache and
writeback_iter() is left to reclaim, as a mapped one already is.
Fixes: 2d6692e642e7 ("drm/i915: Start writeback from the shrinker")
Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
---
Found while reviewing David's fix for the same race on file PMDs, which
leaves shmem out because shmem has no writeback:
https://patch.msgid.link/20261116-dirty_file_pmd-v2-2-571883053174@kernel.org
Build-tested only.
drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
index bf9e7480f0f5..748772c358ac 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
@@ -316,9 +316,15 @@ void __shmem_writeback(size_t size, struct address_space *mapping)
* leaving only CPU mmapings around) and add those folios to the LRU
* instead of invoking writeback so they are aged and paged out
* as normal.
+ *
+ * The same goes for a folio with references beyond the page cache
+ * and writeback_iter(). A folio that was just unmapped can still be
+ * written through a stale TLB entry until the unmapping side flushes
+ * the TLB and drops its reference. pageout() checks the same.
*/
while ((folio = writeback_iter(mapping, &wbc, folio, &error))) {
- if (folio_mapped(folio))
+ if (folio_mapped(folio) ||
+ folio_ref_count(folio) != 1 + folio_nr_pages(folio))
folio_redirty_for_writepage(&wbc, folio);
else
error = shmem_write_folio(folio);
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
--
2.54.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] drm/i915/gem: Skip writeback of shmem folios with extra references
2026-11-19 13:42 [PATCH] drm/i915/gem: Skip writeback of shmem folios with extra references Kiryl Shutsemau (Meta)
@ 2026-11-19 17:43 ` Andi Shyti
2026-11-20 13:17 ` Kiryl Shutsemau (Meta)
0 siblings, 1 reply; 5+ messages in thread
From: Andi Shyti @ 2026-11-19 17:43 UTC (permalink / raw)
To: Kiryl Shutsemau (Meta)
Cc: Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
David Airlie, Simona Vetter, Andi Shyti, David Hildenbrand,
Hugh Dickins, Baolin Wang, intel-gfx, dri-devel, linux-mm,
linux-kernel, kernel-team
Hi Kiryl,
On Thu, Nov 19, 2026 at 01:42:41PM +0000, Kiryl Shutsemau (Meta) wrote:
> __shmem_writeback() writes out every dirty folio that is not mapped. A
> folio stops being mapped when munmap() removes its rmap, but the TLB
> flush comes later. Until then another thread can still store to it
> through a stale TLB entry:
>
> CPU0 (munmap) CPU1 (same mm) CPU2 (i915 shrinker)
> zap_huge_pmd()
> folio_remove_rmap_pmd()
> __shmem_writeback()
> clear dirty
> !folio_mapped()
> shmem_write_folio()
> store via stale TLB
> tlb_finish_mmu()
>
> The store leaves the folio clean in the swap cache. Once reclaim frees
> it, the object reads back from swap without that store.
>
> The legacy mmap ioctl maps the shmem file directly, and i915 mounts it
> with huge=within_size on some platforms, so the folio can be PMD-mapped.
> The core mm keeps the rmap until after the flush for PTEs, but not for
> shmem PMDs: shmem has no writeback, and pageout() refuses a folio with
> an extra reference.
>
> Do the same here. The unmapping side holds its reference until the TLB
> is flushed, so a folio with references beyond the page cache and
> writeback_iter() is left to reclaim, as a mapped one already is.
>
> Fixes: 2d6692e642e7 ("drm/i915: Start writeback from the shrinker")
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
> ---
> Found while reviewing David's fix for the same race on file PMDs, which
> leaves shmem out because shmem has no writeback:
> https://patch.msgid.link/20261116-dirty_file_pmd-v2-2-571883053174@kernel.org
>
> Build-tested only.
>
> drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> index bf9e7480f0f5..748772c358ac 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> @@ -316,9 +316,15 @@ void __shmem_writeback(size_t size, struct address_space *mapping)
> * leaving only CPU mmapings around) and add those folios to the LRU
> * instead of invoking writeback so they are aged and paged out
> * as normal.
> + *
> + * The same goes for a folio with references beyond the page cache
> + * and writeback_iter(). A folio that was just unmapped can still be
> + * written through a stale TLB entry until the unmapping side flushes
> + * the TLB and drops its reference. pageout() checks the same.
> */
> while ((folio = writeback_iter(mapping, &wbc, folio, &error))) {
> - if (folio_mapped(folio))
> + if (folio_mapped(folio) ||
> + folio_ref_count(folio) != 1 + folio_nr_pages(folio))
I don't think we need this fix in the current tree. The i915
writeback iterator already skips the shmem folio in the case you
describe, so the new refcount check doesn't change the outcome.
Thanks,
Andi
> folio_redirty_for_writepage(&wbc, folio);
> else
> error = shmem_write_folio(folio);
>
> base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
> --
> 2.54.0
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] drm/i915/gem: Skip writeback of shmem folios with extra references
2026-11-19 17:43 ` Andi Shyti
@ 2026-11-20 13:17 ` Kiryl Shutsemau (Meta)
2026-11-25 5:36 ` Christoph Hellwig
0 siblings, 1 reply; 5+ messages in thread
From: Kiryl Shutsemau (Meta) @ 2026-11-20 13:17 UTC (permalink / raw)
To: Andi Shyti
Cc: Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
David Airlie, Simona Vetter, Andi Shyti, David Hildenbrand,
Hugh Dickins, Baolin Wang, intel-gfx, dri-devel, linux-mm,
linux-kernel, kernel-team
On Thu, Nov 19, 2026 at 06:43:08PM +0100, Andi Shyti wrote:
> > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> > index bf9e7480f0f5..748772c358ac 100644
> > --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> > +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
> > @@ -316,9 +316,15 @@ void __shmem_writeback(size_t size, struct address_space *mapping)
> > * leaving only CPU mmapings around) and add those folios to the LRU
> > * instead of invoking writeback so they are aged and paged out
> > * as normal.
> > + *
> > + * The same goes for a folio with references beyond the page cache
> > + * and writeback_iter(). A folio that was just unmapped can still be
> > + * written through a stale TLB entry until the unmapping side flushes
> > + * the TLB and drops its reference. pageout() checks the same.
> > */
> > while ((folio = writeback_iter(mapping, &wbc, folio, &error))) {
> > - if (folio_mapped(folio))
> > + if (folio_mapped(folio) ||
> > + folio_ref_count(folio) != 1 + folio_nr_pages(folio))
>
> I don't think we need this fix in the current tree. The i915
> writeback iterator already skips the shmem folio in the case you
> describe, so the new refcount check doesn't change the outcome.
Oh. It took me a while to see why.
__shmem_writeback() asks writeback_iter() for PAGECACHE_TAG_DIRTY
folios, and shmem never sets that tag, its ->dirty_folio is
noop_dirty_folio(). So the loop body never runs for any folio, and
hasn't since 776a853a43c9 ("i915: Use writeback_iter()").
That makes me question the whole writeback thing. What's going on
there, and what's the plan?
--
Kiryl Shutsemau / Kirill A. Shutemov
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] drm/i915/gem: Skip writeback of shmem folios with extra references
2026-11-20 13:17 ` Kiryl Shutsemau (Meta)
@ 2026-11-25 5:36 ` Christoph Hellwig
2026-11-25 9:38 ` Joonas Lahtinen
0 siblings, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2026-11-25 5:36 UTC (permalink / raw)
To: Kiryl Shutsemau (Meta)
Cc: Andi Shyti, Jani Nikula, Joonas Lahtinen, Rodrigo Vivi,
Tvrtko Ursulin, David Airlie, Simona Vetter, Andi Shyti,
David Hildenbrand, Hugh Dickins, Baolin Wang, intel-gfx,
dri-devel, linux-mm, linux-kernel, kernel-team
On Fri, Nov 20, 2026 at 01:17:31PM +0000, Kiryl Shutsemau (Meta) wrote:
> > > while ((folio = writeback_iter(mapping, &wbc, folio, &error))) {
> > > - if (folio_mapped(folio))
> > > + if (folio_mapped(folio) ||
> > > + folio_ref_count(folio) != 1 + folio_nr_pages(folio))
> >
> > I don't think we need this fix in the current tree. The i915
> > writeback iterator already skips the shmem folio in the case you
> > describe, so the new refcount check doesn't change the outcome.
>
> Oh. It took me a while to see why.
>
> __shmem_writeback() asks writeback_iter() for PAGECACHE_TAG_DIRTY
> folios, and shmem never sets that tag, its ->dirty_folio is
> noop_dirty_folio(). So the loop body never runs for any folio, and
> hasn't since 776a853a43c9 ("i915: Use writeback_iter()").
Heh. I'm surprised no one noticed..
> That makes me question the whole writeback thing. What's going on
> there, and what's the plan?
I (and others) have been asking forever to get drm out of micro-managing
tmpfs. I guess given that this code path hasn't worked for a year and
a half is a pretty good indicator that this instance should simply
go away.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] drm/i915/gem: Skip writeback of shmem folios with extra references
2026-11-25 5:36 ` Christoph Hellwig
@ 2026-11-25 9:38 ` Joonas Lahtinen
0 siblings, 0 replies; 5+ messages in thread
From: Joonas Lahtinen @ 2026-11-25 9:38 UTC (permalink / raw)
To: Kiryl Shutsemau (Meta), Christoph Hellwig, willy, brauner, torvalds
Cc: Andi Shyti, Jani Nikula, Rodrigo Vivi, Tvrtko Ursulin,
David Airlie, Simona Vetter, Andi Shyti, David Hildenbrand,
Hugh Dickins, Baolin Wang, intel-gfx, dri-devel, linux-mm,
linux-kernel, willy
Quoting Christoph Hellwig (2026-11-25 07:36:42)
> On Fri, Nov 20, 2026 at 01:17:31PM +0000, Kiryl Shutsemau (Meta) wrote:
> > > > while ((folio = writeback_iter(mapping, &wbc, folio, &error))) {
> > > > - if (folio_mapped(folio))
> > > > + if (folio_mapped(folio) ||
> > > > + folio_ref_count(folio) != 1 + folio_nr_pages(folio))
> > >
> > > I don't think we need this fix in the current tree. The i915
> > > writeback iterator already skips the shmem folio in the case you
> > > describe, so the new refcount check doesn't change the outcome.
> >
> > Oh. It took me a while to see why.
> >
> > __shmem_writeback() asks writeback_iter() for PAGECACHE_TAG_DIRTY
> > folios, and shmem never sets that tag, its ->dirty_folio is
> > noop_dirty_folio(). So the loop body never runs for any folio, and
> > hasn't since 776a853a43c9 ("i915: Use writeback_iter()").
>
> Heh. I'm surprised no one noticed..
Not so funny to me, you folks are pushing regressions without ever
Cc'ing a single maintainer in the original patches.
I was under impression that Linux development had some rules about
not introducing regressions and also some rules about not pushing
stuff silently to other subsystems. Maybe it's just me not able to
keep up with the vibes of these times.
> > That makes me question the whole writeback thing. What's going on
> > there, and what's the plan?
>
> I (and others) have been asking forever to get drm out of micro-managing
> tmpfs. I guess given that this code path hasn't worked for a year and
> a half is a pretty good indicator that this instance should simply
> go away.
That's not a good indicator in this case.
The writeback for dirty shmem pages took multiple years and multiple
iterations to address the best we could as it's not exactly a race
that shows up frequently.
It's also more likely on the hardware from the era the fix was developed
on, and the owners of those older machines are not exactly jumping to the
latest mainline but rather more likely to run Debian oldstable.
So which one of you will be writing up a revert or fix for the offending
patch or do you expect us to clean up the mess you've created?
Regards, Joonas
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-25 9:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 13:42 [PATCH] drm/i915/gem: Skip writeback of shmem folios with extra references Kiryl Shutsemau (Meta)
2026-11-19 17:43 ` Andi Shyti
2026-11-20 13:17 ` Kiryl Shutsemau (Meta)
2026-11-25 5:36 ` Christoph Hellwig
2026-11-25 9:38 ` Joonas Lahtinen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®