[PATCH] f2fs: remove folio_detach_private() in .invalidate_folio and .release_folio
From: Chao Yu
Date: Sun Apr 09 2023 - 22:24:17 EST
We have maintain PagePrivate and page_private and page reference
w/ {set,clear}_page_private_*, it doesn't need to call
folio_detach_private() in the end of .invalidate_folio and
.release_folio, remove it and use f2fs_bug_on instead.
Signed-off-by: Chao Yu <chao@xxxxxxxxxx>
[Jaegeuk Kim: cover f2fs_delete_entry case]
Signed-off-by: Jaegeuk Kim <jaegeuk@xxxxxxxxxx>
---
fs/f2fs/data.c | 4 ++--
fs/f2fs/dir.c | 5 ++---
fs/f2fs/f2fs.h | 32 ++++++++------------------------
3 files changed, 12 insertions(+), 29 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 5a3636b70f48..8870ff630409 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -3734,7 +3734,7 @@ void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
inode->i_ino == F2FS_COMPRESS_INO(sbi))
clear_page_private_data(&folio->page);
- folio_detach_private(folio);
+ f2fs_bug_on(sbi, page_private(&folio->page));
}
bool f2fs_release_folio(struct folio *folio, gfp_t wait)
@@ -3756,7 +3756,7 @@ bool f2fs_release_folio(struct folio *folio, gfp_t wait)
clear_page_private_reference(&folio->page);
clear_page_private_gcing(&folio->page);
- folio_detach_private(folio);
+ f2fs_bug_on(sbi, page_private(&folio->page));
return true;
}
diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index d6dd8327e82d..cea179dec3b6 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -906,13 +906,12 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
clear_page_dirty_for_io(page);
ClearPageUptodate(page);
+ clear_page_private_reference(page);
clear_page_private_gcing(page);
+ f2fs_bug_on(F2FS_I_SB(dir), page_private(page));
inode_dec_dirty_pages(dir);
f2fs_remove_dirty_inode(dir);
-
- detach_page_private(page);
- set_page_private(page, 0);
}
f2fs_put_page(page, 1);
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 68eadc1ac130..1b1df9e33028 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -1408,11 +1408,8 @@ static inline bool page_private_##name(struct page *page) \
#define PAGE_PRIVATE_SET_FUNC(name, flagname) \
static inline void set_page_private_##name(struct page *page) \
{ \
- if (!PagePrivate(page)) { \
- get_page(page); \
- SetPagePrivate(page); \
- set_page_private(page, 0); \
- } \
+ if (!PagePrivate(page)) \
+ attach_page_private(page, (void *)0); \
set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
}
@@ -1421,13 +1418,8 @@ static inline void set_page_private_##name(struct page *page) \
static inline void clear_page_private_##name(struct page *page) \
{ \
clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
- if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) { \
- set_page_private(page, 0); \
- if (PagePrivate(page)) { \
- ClearPagePrivate(page); \
- put_page(page); \
- }\
- } \
+ if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) \
+ detach_page_private(page); \
}
PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
@@ -1456,11 +1448,8 @@ static inline unsigned long get_page_private_data(struct page *page)
static inline void set_page_private_data(struct page *page, unsigned long data)
{
- if (!PagePrivate(page)) {
- get_page(page);
- SetPagePrivate(page);
- set_page_private(page, 0);
- }
+ if (!PagePrivate(page))
+ attach_page_private(page, (void *)0);
set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
page_private(page) |= data << PAGE_PRIVATE_MAX;
}
@@ -1468,13 +1457,8 @@ static inline void set_page_private_data(struct page *page, unsigned long data)
static inline void clear_page_private_data(struct page *page)
{
page_private(page) &= GENMASK(PAGE_PRIVATE_MAX - 1, 0);
- if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) {
- set_page_private(page, 0);
- if (PagePrivate(page)) {
- ClearPagePrivate(page);
- put_page(page);
- }
- }
+ if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER))
+ detach_page_private(page);
}
/* For compression */
--
2.40.0.577.gac1e443424-goog
>
> With above two patches, I didn't hit any panic or use-after-free issue when testing
> xfstest until now.
>
> Thanks,
>
>
> > I feel attach/detach_page_private would look better?
> >
> > >
> > > Thanks,
> > >
> > > > 1429 put_page(page); \
> > > > 1430 }\
> > > > 1431 } \
> > > > 1432 }
> > > >
> > > > > page is now free, gets reallocated into a THP
> > > > > lookup from the f2fs file finds the new THP
> > > > > things explode messily
> > > > >
> > > > > Checking page->mapping is going to avoid the messy explosion, but
> > > > > you'll still have a page in the page cache which doesn't actually
> > > > > belong to you, and that's going to lead to subtle data corruption.
> > > > >
> > > > > This should be caught by page_expected_state(), called from
> > > > > free_page_is_bad(), called from free_pages_prepare(). Do your testers
> > > > > have CONFIG_DEBUG_VM enabled? That might give you a fighting chance at
> > > > > finding the last place which called put_page(). It won't necessarily be
> > > > > the _wrong_ place to call put_page() (that may have happened earlier),
> > > > > but it may give you a clue.
> > > > >
> > > > > > >
> > > > > > > struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,
> > > > > > > int fgp_flags, gfp_t gfp)
> > > > > > > {
> > > > > > > struct folio *folio;
> > > > > > >
> > > > > > > folio = __filemap_get_folio(mapping, index, fgp_flags, gfp);
> > > > > > > if (IS_ERR(folio))
> > > > > > > return NULL;
> > > > > > > return folio_file_page(folio, index);
> > > > > > > }
> > > > > > >
> > > > > > > Thanks,
> > > > > > >
> > > > > > > > - f2fs_put_page(page, 1);
> > > > > > > > - goto repeat;
> > > > > > > > - }
> > > > > > > > - if (unlikely(!PageUptodate(page))) {
> > > > > > > > + if (unlikely(page->mapping != mapping || !PageUptodate(page))) {
> > > > > > > > f2fs_put_page(page, 1);
> > > > > > > > return ERR_PTR(-EIO);
> > > > > > > > }