linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] mm/filemap: don't call folio_test_locked() without a reference in next_uptodate_folio()
@ 2024-11-29 12:53 David Hildenbrand
  2024-11-29 13:40 ` Kirill A. Shutemov
  0 siblings, 1 reply; 2+ messages in thread
From: David Hildenbrand @ 2024-11-29 12:53 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-fsdevel, linux-mm, David Hildenbrand,
	syzbot+9f9a7f73fb079b2387a6, Matthew Wilcox (Oracle),
	Andrew Morton, Kirill A. Shutemov, Hillf Danton

The folio can get freed + buddy-merged + reallocated in the meantime,
resulting in us calling folio_test_locked() possibly on a tail page.

This makes const_folio_flags VM_BUG_ON_PGFLAGS() when stumbling over
the tail page.

Could this result in other issues? Doesn't look like it. False positives
and false negatives don't really matter, because this folio would get
skipped either way when detecting that they have been reallocated in
the meantime.

Fix it by performing the folio_test_locked() checked after grabbing a
reference. If this ever becomes a real problem, we could add a special
helper that racily checks if the bit is set even on tail pages ... but
let's hope that's not required so we can just handle it cleaner:
work on the folio after we hold a reference.

Do we really need the folio_test_locked() check if we are going to
trylock briefly after? Well, we can at least avoid a xas_reload().

It's a bit unclear which exact change introduced that issue. Likely,
ever since we made PG_locked obey to the PF_NO_TAIL policy it could have
been triggered in some way.

Reported-by: syzbot+9f9a7f73fb079b2387a6@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/lkml/674184c9.050a0220.1cc393.0001.GAE@google.com/
Fixes: 48c935ad88f5 ("page-flags: define PG_locked behavior on compound pages")
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Cc: Hillf Danton <hdanton@sina.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
---
 mm/filemap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 7c76a123ba18b..f61cf51c22389 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3501,10 +3501,10 @@ static struct folio *next_uptodate_folio(struct xa_state *xas,
 			continue;
 		if (xa_is_value(folio))
 			continue;
-		if (folio_test_locked(folio))
-			continue;
 		if (!folio_try_get(folio))
 			continue;
+		if (folio_test_locked(folio))
+			goto skip;
 		/* Has the page moved or been split? */
 		if (unlikely(folio != xas_reload(xas)))
 			goto skip;
-- 
2.47.1



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v1] mm/filemap: don't call folio_test_locked() without a reference in next_uptodate_folio()
  2024-11-29 12:53 [PATCH v1] mm/filemap: don't call folio_test_locked() without a reference in next_uptodate_folio() David Hildenbrand
@ 2024-11-29 13:40 ` Kirill A. Shutemov
  0 siblings, 0 replies; 2+ messages in thread
From: Kirill A. Shutemov @ 2024-11-29 13:40 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: linux-kernel, linux-fsdevel, linux-mm,
	syzbot+9f9a7f73fb079b2387a6, Matthew Wilcox (Oracle),
	Andrew Morton, Kirill A. Shutemov, Hillf Danton

On Fri, Nov 29, 2024 at 01:53:03PM +0100, David Hildenbrand wrote:
> The folio can get freed + buddy-merged + reallocated in the meantime,
> resulting in us calling folio_test_locked() possibly on a tail page.
> 
> This makes const_folio_flags VM_BUG_ON_PGFLAGS() when stumbling over
> the tail page.
> 
> Could this result in other issues? Doesn't look like it. False positives
> and false negatives don't really matter, because this folio would get
> skipped either way when detecting that they have been reallocated in
> the meantime.
> 
> Fix it by performing the folio_test_locked() checked after grabbing a
> reference. If this ever becomes a real problem, we could add a special
> helper that racily checks if the bit is set even on tail pages ... but
> let's hope that's not required so we can just handle it cleaner:
> work on the folio after we hold a reference.
> 
> Do we really need the folio_test_locked() check if we are going to
> trylock briefly after? Well, we can at least avoid a xas_reload().
> 
> It's a bit unclear which exact change introduced that issue. Likely,
> ever since we made PG_locked obey to the PF_NO_TAIL policy it could have
> been triggered in some way.
> 
> Reported-by: syzbot+9f9a7f73fb079b2387a6@syzkaller.appspotmail.com
> Closes: https://lore.kernel.org/lkml/674184c9.050a0220.1cc393.0001.GAE@google.com/
> Fixes: 48c935ad88f5 ("page-flags: define PG_locked behavior on compound pages")
> Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
> Cc: Hillf Danton <hdanton@sina.com>
> Signed-off-by: David Hildenbrand <david@redhat.com>

Looks reasonable:

Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

-- 
  Kiryl Shutsemau / Kirill A. Shutemov


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-12-05 15:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-29 12:53 [PATCH v1] mm/filemap: don't call folio_test_locked() without a reference in next_uptodate_folio() David Hildenbrand
2024-11-29 13:40 ` Kirill A. Shutemov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox