Use generic_file_read_iter() in hugetlbfs
Subsystems (15)
Matthew Wilcox (Oracle)
maintainerv5 cover Use generic_file_read_iter() in hugetlbfs
This set of patches are separated out from Jane's earlier series.
https://lore.kernel.org/linux-mm/[email protected]/
v4 here:
https://lore.kernel.org/all/[email protected]/
Changes from v4:
- Drop patch "Test the page is hwpoison before taking the mutex". It's not necessary for this series, and Sashiko points out a bad possibility.
- Fix try_to_unmap() checking of HWPoison for hugetlb folios
- Protect the folio->hugetlb_hwpoison list with its own spinlock
- Fold the "Remove raw_hwp_list_head" patch into the "Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage" patch
- Make folio_test_huge_poison() always false if CONFIG_MEMORY_FAILURE not set
- Fix CONFIG_FILEMAP_LARGE_FOLIO -> CONFIG_LARGE_FOLIO conversion
- Fix hugetlb_clear_poison() to clear has_hwpoisoned last
- Add tags from Ackerley & Sean for guest_memfd patch
Jane Chu (2):
filemap: Add hwpoison handling to filemap_read()
hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter()
Matthew Wilcox (Oracle) (10):
memory-failure: Fix hardware poison check in unpoison_memory() again
mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()
hugetlb: Mark some function arguments as const
guest_memfd: Use folio_has_hwpoisoned_page()
hugetlb: Use the has_hwpoisoned flag
mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()
mm: Check individual hugetlb pages for poison
filemap: Remove checks in mapping_set_folio_order_range()
hugetlb: Set mapping folio order
filemap: Add support for authoritative mappings
fs/Kconfig | 2 +- fs/hugetlbfs/inode.c | 123 +++--------------------- include/linux/hugetlb.h | 24 ++--- include/linux/mm_types.h | 4 +- include/linux/page-flags.h | 63 +++++++++++-- include/linux/pagemap.h | 45 +++++---- mm/Kconfig | 6 +- mm/filemap.c | 51 +++++++++- mm/huge_memory.c | 2 +- mm/hugetlb.c | 16 ++-- mm/memory-failure.c | 186 ++++++++++++++++++++++++------------- mm/memory_hotplug.c | 2 +- mm/rmap.c | 43 +++++---- mm/shmem.c | 2 +- mm/vmscan.c | 2 +- virt/kvm/guest_memfd.c | 2 +- 16 files changed, 321 insertions(+), 252 deletions(-)
Signature · 1 line
2.47.3
Matthew Wilcox (Oracle)
maintainerPATCH v5 1/12 memory-failure: Fix hardware poison check in unpoison_memory() again
The earlier patch (6c54312f9689) that attempted to fix unpoison_memory() was incorrect for hugetlb folios. Before a6fddef49eef, we checked the head page for poison (which was correct for hugetlb and incorrect for THP). We are currently incapable of unpoisoning pages (other than the first page) in a hugetlb folio.
Use is_page_hwpoison() which handles hugetlb pages sufficiently well for this purpose. This is racy as we don't have a reference on the folio at this point, but fixing that properly requires deeper surgery and this is a CAP_SYS_ADMIN path only so this will do for the moment and can be easily backported. It's no worse than the situation before a6fddef49eef.
The other bug in a6fddef49eef is that we currently clear the HWPoison flag on the precise page, which is wrong for hugetlb. Fix that too.
--- mm/memory-failure.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
mm/memory-failure.c +3 -2
@@ -2695,8 +2695,8 @@ int unpoison_memory(unsigned long pfn) goto unlock_mutex; } -if (!PageHWPoison (p)) { -unpoison_pr_info ("%#lx: page was already unpoisoned\n" , +if (!is_page_hwpoison (p)) { +unpoison_pr_info ("%#lx: page is not poisoned\n" , pfn, &unpoison_rs); goto unlock_mutex; }
@@ -2748,6 +2748,7 @@ int unpoison_memory(unsigned long pfn)folio_put (folio); goto unlock_mutex; } + p = &folio->page; }folio_put (folio);
Signature · 1 line
2.47.3
Matthew Wilcox
maintainerRe: [PATCH v5 01/12] memory-failure: Fix hardware poison check in unpoison_memory() again
Sashiko:
Quoted 25 lines
--- Patch [1]: [PATCH v5 01/12] memory-failure: Fix hardware poison check in
unpoison_memory()again --- commit4beed8e27f2afa68261389ac2133cfe324d37d0e
Author: Matthew Wilcox (Oracle) <[email protected]>memory-failure: Fix hardware poison check in
unpoison_memory()againThis commit modifies
unpoison_memory()to useis_page_hwpoison()to better support hugetlb folios and ensures the HWPoison flag is cleared on the correct head page.mm/memory-failure.c int unpoison_memory(unsigned long pfn) · line 2699
Quoted 12 lines
mm/memory-failure.c +2
-2@@ -2695,8 +2695,8 @@ int unpoison_memory(unsigned long pfn) goto unlock_mutex; } -if (!PageHWPoison (p)) { - unpoison_pr_info("%#lx: page was already unpoisoned\n", +if (!is_page_hwpoison (p)) { + unpoison_pr_info("%#lx: page is not poisoned\n",Can this speculative evaluation of
is_page_hwpoison()trigger a panic?
Quoted 4 lines
Since this is evaluated without a reference on the page, the underlying page could be concurrently freed and reallocated as a tail page. Looking at
is_page_hwpoison()ininclude/linux/page-flags.h, it callsfolio_test_hugetlb(folio), which usesconst_folio_flags(). That function
No it doesn't. folio_test_hugetlb() is one of th FOLIO_TYPE_OPS which is defined as return data_race(folio->page.page_type >> 24) == PGTY_##lname; and no asserts. I mean, there probably should be! But this is irrelevant; this whole function contains many such folio tests and the same problem applies to all of them. This patch doesn't make it worse.
Quoted 25 lines
enforces a strict assertion:
VM_BUG_ON_PGFLAGS(PageTail(&folio->page))
If the page is concurrently reallocated as a tail page while
is_page_hwpoison()executes, won't this crash the kernel on debug builds?Quoted 3 lines
mm/memory-failure.c +0
-0pfn, &unpoison_rs); goto unlock_mutex; }[ ... ]
mm/memory-failure.c int unpoison_memory(unsigned long pfn) · line 2754
Quoted 8 lines
mm/memory-failure.c +1
-0@@ -2748,6 +2748,7 @@ int unpoison_memory(unsigned long pfn)folio_put (folio); goto unlock_mutex; } + p = &folio->page; }folio_put (folio);This isn't a bug introduced by this patch, but does this lead to a double
folio_put()and use-after-free for hugetlb folios?
Quoted 6 lines
If
memory_failure()handles an in-use hugetlb folio,me_huge_page()might fail to dissolve it, meaning no extra poison reference is taken but the folio remains marked as hwpoisoned. If the folio is later unmapped and holds a single transient reference,unpoison_memory()will acquire a reference (ghp = 1) and perform thefolio_put(folio)seen just below this reassignment.
I'm not an expert in the memory-poison field, but this also seems wrong. Don't all hugetlb folios have a folio_mapping()? And thus we'll never take the dissolve path in me_huge_page().
At any rate, it says "not introduced by this patch", so I'm not going to take any action.
Quoted 14 lines
By reassigning p =
&folio->pagehere, the subsequent call to TestClearPageHWPoison(p) will be tested on the head page and return true, triggering a second unconditionalfolio_put(folio). Does this underflow the transient pin and cause a use-after-free?This is a pre-existing issue, but does this same logic underflow the refcount for THP folios that previously failed to split?
If
memory_failure()handles a THP andtry_to_split_thp_page()fails, it explicitly drops the reference acquired viaget_hwpoison_page(p), leaving the THP with no extra poison pin. However, TestSetPageHWPoison(p) was already called.
Quoted 5 lines
If the THP later reaches a state with a single transient reference and no mappings,
unpoison_memory()acquires a reference (ghp = 1), callsfolio_put(folio)once, and then TestClearPageHWPoison(p) clears the flag and returns true. This triggers the secondfolio_put(folio), dropping a non-existent poison pin.
I don't know, and I'm losing patience with the Sashiko rabbit holes. This is all getting very far afield from the problem I actually want to fix.
Andrew Morton
maintainerRe: [PATCH v5 01/12] memory-failure: Fix hardware poison check in unpoison_memory() again
Quoted 9 lines · On Mon, 27 Jul 2026 15:43:53 +0100 Matthew Wilcox <[email protected]> wrote:
Quoted 5 lines
If the THP later reaches a state with a single transient reference and no mappings,
unpoison_memory()acquires a reference (ghp = 1), callsfolio_put(folio)once, and then TestClearPageHWPoison(p) clears the flag and returns true. This triggers the secondfolio_put(folio), dropping a non-existent poison pin.I don't know, and I'm losing patience with the Sashiko rabbit holes. This is all getting very far afield from the problem I actually want to fix.
That's fair enough - you aren't a memory-failure maintainer. If you want to take a shot at the pre-existing things then great, but otherwise then let's ensure that the relevant maintainers were cc'ed and move on.
One relatively minor issue here is that if Sashiko's pre-exiting thing is serious, we may decide to fix it with a cc:stable and such a fix should be staged ahead of your series for backportability reasons. If we end up upstreaming these things in the "wrong" order then the backportable-fix developers will be asked to prepare a backportable version of their patch, which is a bit cumbersome, but not the end of the world.
Matthew Wilcox (Oracle)
maintainerPATCH v5 2/12 mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()
In reply to Matthew Wilcox (Oracle)This is a better name for this predicate. Also make the argument const to allow other callers to be made const. No functional change.
--- include/linux/page-flags.h | 4 ++-- mm/huge_memory.c | 2 +- mm/memory_hotplug.c | 2 +- mm/shmem.c | 2 +- mm/vmscan.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
include/linux/page-flags.h +2 -2
@@ -1083,10 +1083,10 @@ static inline bool is_page_hwpoison(const struct page *page)return folio_test_hugetlb (folio) &&PageHWPoison (&folio->page); } -static inline bool folio_contain_hwpoisoned_page (struct folio *folio) +static inline bool folio_has_hwpoisoned_page (const struct folio *folio) {return folio_test_hwpoison (folio) || - (folio_test_large (folio) &&folio_test_has_hwpoisoned (folio)); + (folio_test_large (folio) &&folio_test_has_hwpoisoned (folio)); }bool is_free_buddy_page (const struct page *page);
mm/huge_memory.c +1 -1
@@ -4413,7 +4413,7 @@ static bool thp_underused(struct folio *folio)if (khugepaged_max_ptes_none ==HPAGE_PMD_NR -1 )return false; -if (folio_contain_hwpoisoned_page (folio)) +if (folio_has_hwpoisoned_page (folio))return false;for (i =0 ; i <folio_nr_pages (folio); i++) {
mm/memory_hotplug.c +1 -1
@@ -1813,7 +1813,7 @@ static void do_migrate_range(unsigned long start_pfn, unsigned long end_pfn)if (folio_test_large (folio)) pfn =folio_pfn (folio) +folio_nr_pages (folio) -1 ; -if (folio_contain_hwpoisoned_page (folio)) { +if (folio_has_hwpoisoned_page (folio)) {/* * unmap_poisoned_folio() cannot handle large folios * in all cases yet.
mm/shmem.c +1 -1
@@ -3240,7 +3240,7 @@ shmem_write_begin(const struct kiocb *iocb, struct address_space *mapping,if (ret)return ret; -if (folio_contain_hwpoisoned_page (folio)) { +if (folio_has_hwpoisoned_page (folio)) {folio_unlock (folio);folio_put (folio);return -EIO ;
mm/vmscan.c +1 -1
@@ -1086,7 +1086,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,if (!folio_trylock (folio)) goto keep; -if (folio_contain_hwpoisoned_page (folio)) { +if (folio_has_hwpoisoned_page (folio)) {/* * unmap_poisoned_folio() can't handle large * folio, just skip it. memory_failure() will
Signature · 1 line
2.47.3
Matthew Wilcox
maintainerRe: [PATCH v5 02/12] mm: Rename folio_contain_hwpoison_page() to folio_has_hwpoison_page()
Sashiko review:
Quoted 17 lines
--- Patch [2]: [PATCH v5 02/12] mm: Rename
folio_contain_hwpoison_page()tofolio_has_hwpoison_page()--- commit8fadf59bc3118e11dab2620f6cc376507c16b29a
Author: Matthew Wilcox (Oracle) <[email protected]>mm: Rename
folio_contain_hwpoison_page()tofolio_has_hwpoison_page()This is a better name for this predicate. Also make the argument const to allow other callers to be made const. No functional change.
Quoted 8 lines
include/linux/page-flags.h +0
-0@@ -1083,10 +1083,10 @@ static inline bool is_page_hwpoison(const struct page *page)return folio_test_hugetlb (folio) &&PageHWPoison (&folio->page); }
Quoted 10 lines
include/linux/page-flags.h static inline bool is_page_hwpoison(const struct page *page) · line 1090
Quoted 7 lines
include/linux/page-flags.h +2
-2-static inline bool folio_contain_hwpoisoned_page (struct folio *folio) +static inline bool folio_has_hwpoisoned_page (const struct folio *folio) {return folio_test_hwpoison (folio) || - (folio_test_large (folio) &&folio_test_has_hwpoisoned (folio)); + (folio_test_large (folio) &&folio_test_has_hwpoisoned (folio)); }This isn't a bug introduced by this patch, but can KSM trigger a machine check exception when scanning memory?
Good lord. I renamed a function. Which apparently causes Sashiko to look for bugs in every caller
Quoted 18 lines
Looking at
cmp_and_merge_page()inmm/ksm.c, the KSM scanner initiates page hash calculation:mm/ksm.c:cmp_and_merge_page() { ... checksum = calc_checksum(page); ... }which then maps and reads the page content:
mm/ksm.c:calc_checksum() { ... void *addr = kmap_local_page(page); checksum = xxhash(addr, PAGE_SIZE, 0); ... }
Quoted 7 lines
If a page becomes hardware-poisoned before KSM scans it, shouldn't there be a PageHWPoison(page) or
folio_has_hwpoisoned_page(folio)check before the read to prevent a fatal MCE?This is a pre-existing issue, but does migration risk panicking on poisoned memory?
Yes, lots of things risk panicking on poisoned memory. It's never been a priority for Linux.
Quoted 39 lines
During page migration, pages are passed to
__migrate_folio()which copies the contents without validation:mm/migrate.c:__migrate_folio() { ... rc = folio_mc_copy(dst, src); if (unlikely(rc)) return rc; ... }This executes the page copy in
folio_mc_copy():mm/util.c:folio_mc_copy() { ... if (copy_mc_highpage(folio_page(dst, i), folio_page(src, i))) return -EHWPOISON; ... }On architectures lacking
copy_mcsupport, this falls back tocopy_highpage(). Without a priorfolio_has_hwpoisoned_page(src)check, could migrating a poisoned page viambind()ormove_pages()read poisoned memory and trigger a kernel panic?This is a pre-existing issue, but does
kill_accessing_process()return the wrong error code when a task lacks an mm?In memory failure handling, if the task is exiting or is a kernel thread,
p->mmis NULL:mm/memory-failure.c:kill_accessing_process() { ... if (!p->mm) return -EFAULT; ... }
Quoted 5 lines
Since the
memory_failure()API protocol requires returning 0 for a successfully recovered state (no user signal needed), does returning-EFAULTcause the architecture MCE handler, such askill_me_maybe(), to erroneously conclude recovery failed? Could this trigger unnecessary warnings and incorrectly attempt to force a SIGBUS on the dying task?
Dunno, and I'm done with side-quests. Somebody else can decide if this is a real problem that needs to be fixed.
Matthew Wilcox (Oracle)
maintainerPATCH v5 3/12 hugetlb: Mark some function arguments as const
In reply to Matthew Wilcox (Oracle)These functions do not modify their folio argument, so we can mark them as being const which will allow for some minor optimisations and let us mark other function arguments as being const in the future.
--- include/linux/hugetlb.h | 19 +++++++++---------- mm/hugetlb.c | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-)
include/linux/hugetlb.h +9 -10
@@ -165,7 +165,7 @@ bool hugetlbfs_pagecache_present(struct hstate *h,struct vm_area_struct *vma, unsigned long address); -struct address_space *hugetlb_folio_mapping_lock_write (struct folio *folio); +struct address_space *hugetlb_folio_mapping_lock_write (const struct folio *folio);extern int movable_gigantic_pages__read_mostly ;extern int sysctl_hugetlb_shm_group__read_mostly ;
@@ -292,8 +292,7 @@ static inline unsigned long hugetlb_total_pages(void)return 0 ; } -static inline struct address_space *hugetlb_folio_mapping_lock_write ( -struct folio *folio) +static inline struct address_space *hugetlb_folio_mapping_lock_write (const struct folio *folio) {return NULL ; }
@@ -595,8 +594,8 @@ enum hugetlb_page_flags {#ifdef CONFIG_HUGETLB_PAGE #define TESTHPAGEFLAG (uname, flname) \ static __always_inline \ -bool folio_test_hugetlb_##flname(struct folio *folio) \ - { void *private = &folio->private; \ +bool folio_test_hugetlb_##flname(const struct folio *folio) \ + { const void *private = &folio->private; \ return test_bit(HPG_##flname, private); \ }
@@ -616,7 +615,7 @@ void folio_clear_hugetlb_##flname(struct folio *folio) \#else #define TESTHPAGEFLAG (uname, flname) \ static inline bool \ -folio_test_hugetlb_##flname(struct folio *folio) \ +folio_test_hugetlb_##flname(const struct folio *folio) \ { return 0; }#define SETHPAGEFLAG (uname, flname) \
@@ -727,7 +726,7 @@ static inline struct hugepage_subpool *subpool_inode(struct inode *inode)return HUGETLBFS_SB (inode->i_sb)->spool; } -static inline struct hugepage_subpool *hugetlb_folio_subpool (struct folio *folio) +static inline struct hugepage_subpool *hugetlb_folio_subpool (const struct folio *folio) {return folio->_hugetlb_subpool; }
@@ -857,7 +856,7 @@ static inline bool arch_has_huge_bootmem_alloc(void) }#endif -static inline struct hstate *folio_hstate (struct folio *folio) +static inline struct hstate *folio_hstate (const struct folio *folio) {VM_BUG_ON_FOLIO (!folio_test_hugetlb (folio), folio);return size_to_hstate (folio_size (folio));
@@ -1088,7 +1087,7 @@ static inline unsigned long huge_page_mask_align(struct file *file)return 0 ; } -static inline struct hugepage_subpool *hugetlb_folio_subpool (struct folio *folio) +static inline struct hugepage_subpool *hugetlb_folio_subpool (const struct folio *folio) {return NULL ; }
@@ -1157,7 +1156,7 @@ static inline struct hstate *hstate_vma(struct vm_area_struct *vma)return NULL ; } -static inline struct hstate *folio_hstate (struct folio *folio) +static inline struct hstate *folio_hstate (const struct folio *folio) {return NULL ; }
mm/hugetlb.c +1 -1
@@ -1775,7 +1775,7 @@ void init_new_hugetlb_folio(struct folio *folio)* stable. Due to locking order, we can only trylock_write. If we can * not get the lock, simply return NULL to caller. */ -struct address_space *hugetlb_folio_mapping_lock_write (struct folio *folio) +struct address_space *hugetlb_folio_mapping_lock_write (const struct folio *folio) {struct address_space *mapping =folio_mapping (folio);
Signature · 1 line
2.47.3
Matthew Wilcox (Oracle)
maintainerPATCH v5 4/12 guest_memfd: Use folio_has_hwpoisoned_page()
In reply to Matthew Wilcox (Oracle)folio_test_hwpoison() only works on hugetlb folios today and will give the wrong answer for other large folios. folio_has_hwpoisoned_page() works on both.
--- virt/kvm/guest_memfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/guest_memfd.c +1 -1
@@ -774,7 +774,7 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,if (IS_ERR (folio))return folio; -if (folio_test_hwpoison (folio)) { +if (folio_has_hwpoisoned_page (folio)) {folio_unlock (folio);folio_put (folio);return ERR_PTR (-EHWPOISON );
Signature · 1 line
2.47.3
Matthew Wilcox (Oracle)
maintainerPATCH v5 5/12 hugetlb: Use the has_hwpoisoned flag
In reply to Matthew Wilcox (Oracle)Other large folios use the has_hwpoisoned flag. Convert hugetlb to match. This will help us use the per-page hwpoison flag in the future.
Also introduce a folio_test_huge_poison(). This has exactly the same meaning as folio_test_has_hwpoisoned() but can be used when we don't have a reference to the folio containing the page. folio_test_has_hwpoisoned() can race with folio splitting / reallocation and trip the assertions in const_folio_flags().
This closes a gap where a page in a previously-poisoned hugetlb folio could be observed to not have the hwpoison bit set.
--- fs/Kconfig | 2 +- fs/hugetlbfs/inode.c | 2 +- include/linux/page-flags.h | 46 +++++++++++++++++++++++++------ mm/Kconfig | 6 +++- mm/hugetlb.c | 14 +++++----- mm/memory-failure.c | 56 +++++++++++++++++++++++++++----------- mm/rmap.c | 43 ++++++++++++++++++----------- 7 files changed, 119 insertions(+), 50 deletions(-)
fs/Kconfig +1 -1
@@ -272,7 +272,7 @@ endif # HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS - select XARRAY_MULTI + select LARGE_FOLIO config HUGETLB_PAGE_OPTIMIZE_VMEMMAP def_bool HUGETLB_PAGE
fs/hugetlbfs/inode.c +1 -1
@@ -258,7 +258,7 @@ static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to) }else {folio_unlock (folio); -if (!folio_test_hwpoison (folio)) +if (!folio_test_has_hwpoisoned (folio)) want = nr;else {/*
include/linux/page-flags.h +38 -8
81 lines not on this page · raw message
mm/Kconfig +5 -1
@@ -843,11 +843,15 @@ config PERSISTENT_HUGE_ZERO_FOLIO config MM_ID def_bool n +config LARGE_FOLIO + def_bool n + select XARRAY_MULTI + menuconfig TRANSPARENT_HUGEPAGE bool "Transparent Hugepage Support" depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT select COMPACTION - select XARRAY_MULTI + select LARGE_FOLIO select MM_ID help Transparent Hugepages allows the kernel to use huge pages and
mm/hugetlb.c +7 -7
@@ -1255,7 +1255,7 @@ static struct folio *dequeue_hugetlb_folio_node_exact(struct hstate *h,if (pin && !folio_is_longterm_pinnable (folio))continue ; -if (folio_test_hwpoison (folio)) +if (folio_test_has_hwpoisoned (folio))continue ;if (is_migrate_isolate_page (&folio->page))
@@ -1498,10 +1498,10 @@ static void __update_and_free_hugetlb_folio(struct hstate *h, }/* -* Move PageHWPoison flag from head page to the raw error pages, -* which makes any healthy subpages reusable. +* Move HWPoison flag to each error page +* which makes any healthy pages reusable. */ -if (unlikely (folio_test_hwpoison (folio))) +if (unlikely (folio_test_has_hwpoisoned (folio)))folio_clear_hugetlb_hwpoison (folio);VM_BUG_ON_FOLIO (folio_ref_count (folio), folio);
@@ -3998,7 +3998,7 @@ long demote_pool_huge_page(struct hstate *src, nodemask_t *nodes_allowed,struct folio *folio, *next;list_for_each_entry_safe (folio, next, &src->hugepage_freelists[node], lru) { -if (folio_test_hwpoison (folio)) +if (folio_test_has_hwpoisoned (folio))continue ;remove_hugetlb_folio (src, folio, false);
@@ -5809,7 +5809,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,* don't have hwpoisoned swap entry for errored virtual address. * So we need to block hugepage fault by PG_hwpoison bit check. */ -if (unlikely (folio_test_hwpoison (folio))) { +if (unlikely (folio_test_has_hwpoisoned (folio))) { ret =VM_FAULT_HWPOISON_LARGE |VM_FAULT_SET_HINDEX (hstate_index (h)); goto backout_unlocked;
@@ -6318,7 +6318,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, ptl =huge_pte_lock (h, dst_mm, dst_pte); ret = -EIO ; -if (folio_test_hwpoison (folio)) +if (folio_test_has_hwpoisoned (folio)) goto out_release_unlock; ret = -EEXIST ;
mm/memory-failure.c +40 -16
104 lines not on this page · raw message
mm/rmap.c +27 -16
119 lines not on this page · raw message
Signature · 1 line
2.47.3
Matthew Wilcox
maintainerRe: [PATCH v5 05/12] hugetlb: Use the has_hwpoisoned flag
Sashiko:
Quoted 30 lines
--- Patch [5]: [PATCH v5 05/12] hugetlb: Use the
has_hwpoisonedflag --- commitcca87d250a7a597186556cb9d897807edae0b82f
Author: Matthew Wilcox (Oracle) <[email protected]>hugetlb: Use the
has_hwpoisonedflagThis commit converts hugetlb to use the
has_hwpoisonedflag to match other large folios. It introducesfolio_test_huge_poison()for lockless checks to close a gap where a poisoned hugetlb folio could be observed without the hwpoison bit set during teardown.Quoted 3 lines
include/linux/page-flags.h +0
-0[ ... ]
include/linux/page-flags.h PAGE_TYPE_OPS(Slab, slab, slab) · line 1055
Quoted 11 lines
include/linux/page-flags.h +7
-0@@ -1041,8 +1046,29 @@ PAGE_TYPE_OPS(Slab, slab, slab)#ifdef CONFIG_HUGETLB_PAGE FOLIO_TYPE_OPS(hugetlb, hugetlb) + +#ifdef CONFIG_MEMORY_FAILURE +static inline bool folio_test_huge_poison (const struct folio *folio) +{ +return (READ_ONCE (folio->page.page_type) >>23 ) == + ((PGTY_hugetlb <<1 ) |1 ); +}This is a pre-existing issue, but does this logic fully close the race window?
Quoted 8 lines
The
folio_test_huge_poison()function assumes thePGTY_hugetlbbits remain intact during teardown. However,__ClearPagehugetlb()unconditionally sets the entirepage_typetoUINT_MAX.When
__ClearPagehugetlb()is called,folio_test_huge_poison()will evaluate to false at the exact same momentfolio_test_hugetlb()evaluates to false. Would this allow a concurrentis_page_hwpoison()check to miss the poison state?
Um, no? Here's how is_page_hwpoison() looks:
static inline bool is_page_hwpoison(const struct page *page)
{
const struct folio *folio;
if (PageHWPoison(page))
return true;
folio = page_folio(page);
if (folio_test_huge_poison(folio))
return true;
/* In case we raced with hugetlb transferring flags */
return PageHWPoison(page);
}We're relying on ordering; if we see a false folio_test_huge_poison(), we recheck that the flag hasn't already been moved to the page. Since we always move the poison to the pages before clearing the page type, I don't see how we can miss anything.
Quoted 18 lines
include/linux/page-flags.h PAGE_TYPE_OPS(Slab, slab, slab) · line 1065
Quoted 9 lines
include/linux/page-flags.h +9
-0+static inline void folio_set_huge_poison (struct folio *folio) +{ + folio->page.page_type |= (1 <<23 ); +} + +static inline void folio_clear_huge_poison (struct folio *folio) +{ + folio->page.page_type &= ~(1 <<23 ); +}Can this cause a data race on the
page_typefield?In
folio_clear_huge_poison(), the bitwise operation modifies thepage_typefield non-atomically. Ifunpoison_memory()calls this on a free hugetlb folio while a concurrent sysfs pool shrink calls__ClearPagehugetlb()under thehugetlb_lock, theUINT_MAXvalue could be overwritten with0xFF7FFFFF.
Quoted 3 lines
Since
unpoison_memory()relies onmf_mutexrather thanhugetlb_lock, would this corruptedpage_typetrigger aVM_BUG_ON_PAGEin the buddy allocator when the page reachesfree_pages_prepare()?
Hmm ... if we just move the hugetlb_clear_poison() call before the call to folio_put(), that'll take care of this race, right? Oh, but there's two calls to folio_free_raw_hwp(), and the other one is when (ugh), we don't have a reference to the folio. So I think we wrap the whole thing in hugetlb_lock to prevent this?
ghp = get_hwpoison_page(p, MF_UNPOISON);
if (!ghp) {
spin_lock_irq(&hugetlb_lock);
if (folio_test_hugetlb(folio)) {
huge = true;
count = folio_free_raw_hwp(folio, false);
if (count == 0)
goto unlock_mutex;
ret = hugetlb_clear_poison(folio);
} else {
ret = TestClearPageHWPoison(p) ? 0 : -EBUSY;
}
spin_unlock_irq(&hugetlb_lock);(this also includes the fix to the problem discussed below)
Quoted 26 lines
Quoted 3 lines
mm/hugetlb.c +0
-0[ ... ]
Quoted 4 lines
mm/hugetlb.c +0
-0@@ -1498,10 +1498,10 @@ static void __update_and_free_hugetlb_folio(struct hstate *h, }/* mm/hugetlb.c static void __update_and_free_hugetlb_folio(struct hstate *h, · line 1507
Quoted 10 lines
mm/hugetlb.c +3
-3-* Move PageHWPoison flag from head page to the raw error pages, -* which makes any healthy subpages reusable. +* Move HWPoison flag to each error page +* which makes any healthy pages reusable. */ -if (unlikely (folio_test_hwpoison (folio))) +if (unlikely (folio_test_has_hwpoisoned (folio)))folio_clear_hugetlb_hwpoison (folio);VM_BUG_ON_FOLIO (folio_ref_count (folio), folio);This is a pre-existing issue, but is there a risk of a double free in the hwpoison list teardown?
Both
__update_and_free_hugetlb_folio()(viafolio_clear_hugetlb_hwpoison()) andunpoison_memory()can invoke__folio_free_raw_hwp()on the same folio concurrently.
Quoted 5 lines
Looking at
__folio_free_raw_hwp()inmm/memory-failure.c, it readsfolio->hugetlb_hwpoisonand sets it to NULL without any synchronization locks. If both threads read the pointer before either sets it to NULL, wouldn't they concurrently iterate the linked list and callkfree()on the samehwp_pageelements?
I think that's right, but I also think it goes away later in this series when hwp_page_lock serialises the calls to __folio_free_raw_hwp().
Quoted 21 lines
Quoted 3 lines
mm/memory-failure.c +0
-0[ ... ]
Quoted 11 lines
mm/memory-failure.c +1
-1@@ -2730,8 +2753,8 @@ int unpoison_memory(unsigned long pfn) count =folio_free_raw_hwp (folio, false);if (count ==0 ) goto unlock_mutex; + ret =hugetlb_clear_poison (folio); } - ret =folio_test_clear_hwpoison (folio) ?0 : -EBUSY ; }else if (ghp <0 ) {if (ghp == -EHWPOISON ) { ret =put_page_back_buddy (p) ?0 : -EBUSY ;mm/memory-failure.c int unpoison_memory(unsigned long pfn) · line 2765
Quoted 3 lines
mm/memory-failure.c +0
-0@@ -2740,17 +2763,18 @@ int unpoison_memory(unsigned long pfn)unpoison_pr_info ("%#lx: failed to grab page\n" , pfn, &unpoison_rs); }Does this logic change drop the ability to unpoison free non-hugetlb pages?
Quoted 7 lines
The refactoring of the if (!ghp) block removed the fallback logic that handled non-hugetlb pages. If
get_hwpoison_page()returns 0 for a free buddy page, execution enters the if (!ghp) block.Because
folio_test_hugetlb(folio)is false, it does nothing, drops through the entire if/else chain, and returns-EBUSY. Could this leave free buddy pages permanently poisoned?
Oh, I see. I hadn't realised that case was also being handled here. I'll rework it.
Matthew Wilcox (Oracle)
maintainerPATCH v5 6/12 mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()
In reply to Matthew Wilcox (Oracle)Sleeping in this kind of predicate is unexpected. Add a new spinlock to protect access to the list, and turn it into a normal singly linked list now that it doesn't need to be a lockless list.
Rename is_raw_hwpoison_page_in_hugepage() to hugetlb_page_hwpoison() and make it take the folio (since the callers naturally have the folio). Also remove the handling of non-hugetlb folios and make the arguments const.
--- fs/hugetlbfs/inode.c | 4 +- include/linux/hugetlb.h | 5 +- include/linux/mm_types.h | 4 +- mm/memory-failure.c | 98 ++++++++++++++++++++++------------------ 4 files changed, 58 insertions(+), 53 deletions(-)
fs/hugetlbfs/inode.c +2 -2
@@ -198,7 +198,7 @@ static size_t adjust_range_hwpoison(struct folio *folio, size_t offset,struct page *page =folio_page (folio, offset /PAGE_SIZE );size_t safe_bytes; -if (is_raw_hwpoison_page_in_hugepage (page)) +if (hugetlb_page_hwpoison (folio, page))return 0 ;/* Safe to read the remaining bytes in this page. */ safe_bytes =PAGE_SIZE - (offset %PAGE_SIZE );
@@ -206,7 +206,7 @@ static size_t adjust_range_hwpoison(struct folio *folio, size_t offset,/* Check each remaining page as long as we are not done yet. */ for (; safe_bytes < bytes; safe_bytes +=PAGE_SIZE , page++) -if (is_raw_hwpoison_page_in_hugepage (page)) +if (hugetlb_page_hwpoison (folio, page))break ;return min (safe_bytes, bytes);
include/linux/hugetlb.h +1 -4
@@ -1069,10 +1069,7 @@ void hugetlb_register_node(struct node *node);void hugetlb_unregister_node (struct node *node);#endif -/* -* Check if a given raw @page in a hugepage is HWPOISON. -*/ -bool is_raw_hwpoison_page_in_hugepage (struct page *page); +bool hugetlb_page_hwpoison (const struct folio *folio,const struct page *page);static inline unsigned long huge_page_mask_align (struct file *file) {
include/linux/mm_types.h +2 -2
@@ -387,7 +387,7 @@ typedef unsigned short mm_id_t;* @_hugetlb_subpool: Do not use directly, use accessor in hugetlb.h. * @_hugetlb_cgroup: Do not use directly, use accessor in hugetlb_cgroup.h. * @_hugetlb_cgroup_rsvd: Do not use directly, use accessor in hugetlb_cgroup.h. -* @_hugetlb_hwpoison: Do not use directly, call raw_hwp_list_head(). +* @hugetlb_hwpoison: List of pages with hwpoison. * @_deferred_list: Folios to be split under memory pressure. * @_unused_slab_obj_exts: Placeholder to match obj_exts in struct slab. *
@@ -499,7 +499,7 @@ struct folio {void *_hugetlb_subpool;void *_hugetlb_cgroup;void *_hugetlb_cgroup_rsvd; -void *_hugetlb_hwpoison; +struct hwp_page *hugetlb_hwpoison;/* private: the union with struct page is transitional */ };struct page __page_3;
mm/memory-failure.c +53 -45
164 lines not on this page · raw message
Signature · 1 line
2.47.3
Matthew Wilcox
maintainerRe: [PATCH v5 06/12] mm: Remove locking mf_mutex in is_raw_hwpoison_page_in_hugepage()
Sashiko:
Quoted 57 lines
--- Patch [6]: [PATCH v5 06/12] mm: Remove locking
mf_mutexinis_raw_hwpoison_page_in_hugepage()--- commit5aafc5635bbbdae616864594ab6ced23fdde474f
Author: Matthew Wilcox (Oracle) <[email protected]>mm: Remove locking
mf_mutexinis_raw_hwpoison_page_in_hugepage()This commit replaces
mf_mutexwith a new spinlock to protect the hwpoison list in hugetlb folios, preventing unexpected sleeping in predicate functions. It also renames the function tohugetlb_page_hwpoisonand simplifies it to use a standard singly linked list.Quoted 4 lines
mm/memory-failure.c +0
-0[ ... ]
Quoted 2 lines
+bool
hugetlb_page_hwpoison(const struct folio *folio, const struct page *page)
{[ ... ]
Quoted 6 lines
mm/memory-failure.c +2
-2/* -* When RawHwpUnreliable is set, kernel lost track of which subpages -* are HWPOISON. So return as if ALL subpages are HWPOISONed. +* When RawHwpUnreliable is set, kernel lost track of which pages +* are HWPOISON. So return as if ALL pages are HWPOISONed. */ mm/memory-failure.c EXPORT_SYMBOL_GPL(mf_dax_kill_procs); · line 1841
Quoted 10 lines
mm/memory-failure.c +1
-6if (folio_test_hugetlb_raw_hwp_unreliable (folio))return true; -mutex_lock (&mf_mutex); - - raw_hwp_head =raw_hwp_list_head (folio); -llist_for_each_entry (p, raw_hwp_head->first, node) { -if (page == p->page) { - ret = true; +spin_lock_irqsave (&hwp_page_lock, flags);This isn't a bug introduced by this patch, but is it possible for a race to occur if the unreliable flag is checked without holding the lock?
If CPU1 calls
hugetlb_page_hwpoison()and checks the unreliable flag locklessly, it might read false.Concurrently, CPU2 executing
hugetlb_update_hwpoison()might fail to allocate a list entry, acquirehwp_page_lock, set the unreliable flag, and clear thehugetlb_hwpoisonlist:
hugetlb_update_hwpoison()... p = kmalloc_obj(*p, GFP_ATOMIC); if (p) { ... } else { folio_set_hugetlb_raw_hwp_unreliable(folio); __folio_free_raw_hwp(folio, false); } spin_unlock_irqrestore(&hwp_page_lock, flags);
Quoted 6 lines
CPU1 would then acquire
hwp_page_lock, scan the now-empty list, and incorrectly conclude the page is safe. This could potentially allow poisoned memory to be accessed.Would it be safer to check
folio_test_hugetlb_raw_hwp_unreliable()inside thehwp_page_lockcritical section?
Hm, yes, that's true. I'll fold the fix into this patch:
@@ -1831,14 +1831,17 @@ bool hugetlb_page_hwpoison(const struct folio *folio, const struct page *page)
if (!folio_test_has_hwpoisoned(folio))
return false;+ spin_lock_irqsave(&hwp_page_lock, flags);
+
/*
* When RawHwpUnreliable is set, kernel lost track of which pages
* are HWPOISON. So return as if ALL pages are HWPOISONed. */
- if (folio_test_hugetlb_raw_hwp_unreliable(folio))
+ if (folio_test_hugetlb_raw_hwp_unreliable(folio)) {
+ spin_unlock_irqrestore(&hwp_page_lock, flags);
return true;+ }
- spin_lock_irqsave(&hwp_page_lock, flags);
for (p = folio->hugetlb_hwpoison; p; p = p->next) {
if (page == p->page)
break;Quoted 12 lines
Quoted 12 lines
mm/memory-failure.c +4
-4+for (p = folio->hugetlb_hwpoison; p; p = p->next) { +if (page == p->page)break ; - } } +spin_unlock_irqrestore (&hwp_page_lock, flags); -mutex_unlock (&mf_mutex); - -return ret; +return p !=NULL ; }
Matthew Wilcox (Oracle)
maintainerPATCH v5 7/12 mm: Check individual hugetlb pages for poison
In reply to Matthew Wilcox (Oracle)So far, is_page_hwpoison() has returned true for the entire poisoned hugetlb folio, but we can usually allow access to the pages which aren't poisoned. It's slightly tricky due to not having a reference to the folio, but the hugetlb lock can save us here.
--- include/linux/page-flags.h | 4 +++- mm/memory-failure.c | 43 +++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 8 deletions(-)
include/linux/page-flags.h +3 -1
@@ -1094,6 +1094,8 @@ static inline bool PageHuge(const struct page *page)return folio_test_hugetlb (page_folio (page)); } +bool hugetlb_unref_page_hwpoison (const struct page *page); +/* * Check if a page is currently marked HWPoisoned. This check is best * effort only and inherently racy: there is no way to synchronize with
@@ -1108,7 +1110,7 @@ static inline bool is_page_hwpoison(const struct page *page)return true; folio =page_folio (page);if (folio_test_huge_poison (folio)) -return true; +return hugetlb_unref_page_hwpoison (page);/* In case we raced with hugetlb transferring flags */ return PageHWPoison (page); }
mm/memory-failure.c +36 -7
59 lines not on this page · raw message
Signature · 1 line
2.47.3
Matthew Wilcox (Oracle)
maintainerPATCH v5 8/12 filemap: Add hwpoison handling to filemap_read()
In reply to Matthew Wilcox (Oracle)From: Jane Chu <[email protected]>
Add hwpoison handling to filemap_read() such that .read_iter() could make best effort copying data out of clean pages without risking MCE in case page cache contains HWpoison.
--- include/linux/hugetlb.h | 2 -- include/linux/page-flags.h | 11 +++++++++++ mm/filemap.c | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-)
include/linux/hugetlb.h +0 -2
@@ -1069,8 +1069,6 @@ void hugetlb_register_node(struct node *node);void hugetlb_unregister_node (struct node *node);#endif -bool hugetlb_page_hwpoison (const struct folio *folio,const struct page *page); -static inline unsigned long huge_page_mask_align (struct file *file) {return PAGE_MASK & ~huge_page_mask (hstate_file (file));
include/linux/page-flags.h +11 -0
@@ -1094,6 +1094,7 @@ static inline bool PageHuge(const struct page *page)return folio_test_hugetlb (page_folio (page)); } +bool hugetlb_page_hwpoison (const struct folio *folio,const struct page *page);bool hugetlb_unref_page_hwpoison (const struct page *page);/*
@@ -1115,6 +1116,16 @@ static inline bool is_page_hwpoison(const struct page *page)return PageHWPoison (page); } +static inline bool is_ref_page_hwpoison (const struct folio *folio, +const struct page *page) +{ +if (PageHWPoison (page)) +return true; +if (folio_test_hugetlb (folio)) +return hugetlb_page_hwpoison (folio, page); +return false; +} +static inline bool folio_has_hwpoisoned_page (const struct folio *folio) { return PageHWPoison(&folio->page) ||
mm/filemap.c +31 -2
@@ -2483,6 +2483,8 @@ static void filemap_get_read_batch(struct address_space *mapping,if (!folio_batch_add (fbatch, folio))break ; +if (folio_has_hwpoisoned_page (folio)) +break ;if (!folio_test_uptodate (folio))break ;if (folio_test_readahead (folio))
@@ -2749,6 +2751,29 @@ static inline bool pos_same_folio(loff_t pos1, loff_t pos2, struct folio *folio)return (pos1 >> shift == pos2 >> shift); } +static size_t adjust_range_hwpoison (const struct folio *folio,size_t offset, +size_t bytes) +{ +const struct page *page =folio_page (folio, offset /PAGE_SIZE ); +size_t safe_bytes; + +if (!folio_has_hwpoisoned_page (folio)) +return bytes; +if (is_ref_page_hwpoison (folio, page)) +return 0 ; + +/* Safe to read the remaining bytes in this page. */ + safe_bytes =PAGE_SIZE - (offset %PAGE_SIZE ); + page++; + +/* Check each remaining page as long as we are not done yet. */ +for (; safe_bytes < bytes; safe_bytes +=PAGE_SIZE , page++) +if (is_ref_page_hwpoison (folio, page)) +break ; + +return min (safe_bytes, bytes); +} +static void filemap_end_dropbehind_read (struct folio *folio) {if (!folio_test_dropbehind (folio))
@@ -2862,14 +2887,18 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter,if (writably_mapped)flush_dcache_folio (folio); - copied =copy_folio_to_iter (folio, offset, bytes, iter); + copied =adjust_range_hwpoison (folio, offset, bytes); +if (copied < bytes) + error = -EIO ; + copied =copy_folio_to_iter (folio, offset, copied, iter); already_read += copied; iocb->ki_pos += copied; last_pos = iocb->ki_pos;if (copied < bytes) { - error = -EFAULT ; +if (!error) + error = -EFAULT ;break ; } }
Signature · 1 line
2.47.3
Matthew Wilcox (Oracle)
maintainerPATCH v5 9/12 filemap: Remove checks in mapping_set_folio_order_range()
In reply to Matthew Wilcox (Oracle)These checks make it impossible to use mapping_set_folio_order_range() in hugetlb. They add very little value and can simply be removed.
--- include/linux/pagemap.h | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-)
include/linux/pagemap.h +14 -20
@@ -373,7 +373,7 @@ static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)* assumptions about maximum order if THP are disabled, but 8 seems like * a good order (that's 1MB if you're using 4kB pages) */ -#ifdef CONFIG_TRANSPARENT_HUGEPAGE +#ifdef CONFIG_LARGE_FOLIO #define PREFERRED_MAX_PAGECACHE_ORDER HPAGE_PMD_ORDER#else #define PREFERRED_MAX_PAGECACHE_ORDER 8
@@ -394,7 +394,7 @@ static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)*/ static inline size_t mapping_max_folio_size_supported (void ) { -if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) +if (IS_ENABLED (CONFIG_LARGE_FOLIO ))return 1U << (PAGE_SHIFT +MAX_PAGECACHE_ORDER );return PAGE_SIZE ; }
@@ -405,29 +405,23 @@ static inline size_t mapping_max_folio_size_supported(void)* @min: Minimum folio order (between 0-MAX_PAGECACHE_ORDER inclusive). * @max: Maximum folio order (between @min-MAX_PAGECACHE_ORDER inclusive). * -* The filesystem should call this function in its inode constructor to -* indicate which base size (min) and maximum size (max) of folio the VFS -* can use to cache the contents of the file. This should only be used -* if the filesystem needs special handling of folio sizes (ie there is -* something the core cannot know). +* The filesystem should call this function in its inode constructor +* to indicate which size folios can be used to cache the contents of +* the inode. This should only be used if the filesystem needs special +* handling of folio sizes (ie there is something the core cannot know). * Do not tune it based on, eg, i_size. * +* hugetlb calls this with orders larger than MAX_PAGECACHE_ORDER. +* Normal filesystems should not do this. +* * Context: This should not be called while the inode is active as it * is non-atomic. */ static inline void mapping_set_folio_order_range (struct address_space *mapping, -unsigned int min, -unsigned int max) +unsigned int min,unsigned int max) { -if (!IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) +if (!IS_ENABLED (CONFIG_LARGE_FOLIO ))return ; - -if (min >MAX_PAGECACHE_ORDER ) - min =MAX_PAGECACHE_ORDER ; - -if (max >MAX_PAGECACHE_ORDER ) - max =MAX_PAGECACHE_ORDER ; -if (max < min) max = min;
@@ -460,7 +454,7 @@ static inline void mapping_set_large_folios(struct address_space *mapping)static inline unsigned int mapping_max_folio_order (const struct address_space *mapping) { -if (!IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) +if (!IS_ENABLED (CONFIG_LARGE_FOLIO ))return 0 ;return (mapping->flags &AS_FOLIO_ORDER_MAX_MASK ) >>AS_FOLIO_ORDER_MAX ; }
@@ -468,7 +462,7 @@ mapping_max_folio_order(const struct address_space *mapping)static inline unsigned int mapping_min_folio_order (const struct address_space *mapping) { -if (!IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) +if (!IS_ENABLED (CONFIG_LARGE_FOLIO ))return 0 ;return (mapping->flags &AS_FOLIO_ORDER_MIN_MASK ) >>AS_FOLIO_ORDER_MIN ; }
@@ -524,7 +518,7 @@ static inline bool mapping_large_folio_support(const struct address_space *mappi* * Return: True if PMD-sized folios are supported, otherwise false. */ -#ifdef CONFIG_TRANSPARENT_HUGEPAGE +#ifdef CONFIG_LARGE_FOLIO static inline bool mapping_pmd_folio_support (const struct address_space *mapping) {/* AS_FOLIO_ORDER is only reasonable for pagecache folios */
Signature · 1 line
2.47.3
Matthew Wilcox (Oracle)
maintainerPATCH v5 10/12 hugetlb: Set mapping folio order
In reply to Matthew Wilcox (Oracle)hugetlbfs currently uses an fs-specific way to determine the size of the folios in its page cache. Thanks to the support of block sizes larger than page sizes, we now support this as part of the page cache. It's somewhat more efficient as we have this information directly in the mapping rather than going from inode->i_sb->s_fs_info. We can convert the rest of the hugetlb code to use this at our leisure; this is needed now so that we can use filemap_get_pages() unmodified.
--- fs/hugetlbfs/inode.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
fs/hugetlbfs/inode.c +11 -2
@@ -883,6 +883,16 @@ static struct inode *hugetlbfs_get_root(struct super_block *sb,return inode; } +static void hugetlbfs_init_regular_inode (struct inode *inode) +{ +struct hstate *hstate =hstate_inode (inode); +unsigned int order =huge_page_order (hstate); + + inode->i_op = &hugetlbfs_inode_operations; + inode->i_fop = &hugetlbfs_file_operations; +mapping_set_folio_order_range (inode->i_mapping, order, order); +} +/* * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never * be taken from reclaim -- unlike regular filesystems. This needs an
@@ -926,8 +936,7 @@ static struct inode *hugetlbfs_get_inode(struct super_block *sb,init_special_inode (inode, mode, dev);break ;case S_IFREG : - inode->i_op = &hugetlbfs_inode_operations; - inode->i_fop = &hugetlbfs_file_operations; +hugetlbfs_init_regular_inode (inode);break ;case S_IFDIR : inode->i_op = &hugetlbfs_dir_inode_operations;
Signature · 1 line
2.47.3
Matthew Wilcox (Oracle)
maintainerPATCH v5 11/12 filemap: Add support for authoritative mappings
In reply to Matthew Wilcox (Oracle)An authoritative mapping knows about all the folios in the mapping. If read() finds a missing folio, there's no reason to allocate one and try to read it because we know it's a zero region of the file. We can just call iov_iter_zero() instead.
--- include/linux/pagemap.h | 11 +++++++++++ mm/filemap.c | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+)
include/linux/pagemap.h +11 -0
@@ -210,6 +210,7 @@ enum mapping_flags {AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM =9 ,AS_KERNEL_FILE =10 ,/* mapping for a fake kernel file that shouldn't account usage to user cgroups */ +AS_AUTHORITATIVE =11 ,/* If we miss in the page cache, it's a hole */ /* Bits 16-25 are used for FOLIO_ORDER */ AS_FOLIO_ORDER_BITS =5 , AS_FOLIO_ORDER_MIN = 16,
@@ -345,6 +346,16 @@ static inline bool mapping_writeback_may_deadlock_on_reclaim(const struct addresreturn test_bit (AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM , &mapping->flags); } +static inline void mapping_set_authoritative (struct address_space *mapping) +{ +set_bit (AS_AUTHORITATIVE , &mapping->flags); +} + +static inline bool mapping_is_authoritative (const struct address_space *mapping) +{ +return test_bit (AS_AUTHORITATIVE , &mapping->flags); +} +static inline gfp_t mapping_gfp_mask (const struct address_space *mapping) {return mapping->gfp_mask;
mm/filemap.c +18 -0
@@ -2697,6 +2697,8 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count,if (!folio_batch_count (fbatch)) {DEFINE_READAHEAD (ractl, filp, &filp->f_ra, mapping, index); +if (mapping_is_authoritative (mapping)) +return 0 ;if (iocb->ki_flags &IOCB_NOIO )return -EAGAIN ;if (iocb->ki_flags &IOCB_NOWAIT )
@@ -2853,6 +2855,22 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, goto put_folios; end_offset =min_t (loff_t, isize, iocb->ki_pos + iter->count); +if (!folio_batch_count (&fbatch)) { +size_t fsize =mapping_min_folio_nrbytes (mapping); +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 =iov_iter_zero (bytes, iter); + + already_read += copied; + iocb->ki_pos += copied; + last_pos = iocb->ki_pos; + +if (copied < bytes) + error = -EFAULT ; +continue ; + } +/* * Once we start copying data, we don't want to be touching any * cachelines that might be contended:
Signature · 1 line
2.47.3
Matthew Wilcox
maintainerRe: [PATCH v5 11/12] filemap: Add support for authoritative mappings
Sashiko:
Quoted 19 lines
--- Patch [11]: [PATCH v5 11/12] filemap: Add support for authoritative mappings --- commit
7b270a787d853107f7a64dd3127d799a28d3142e
Author: Matthew Wilcox (Oracle) <[email protected]>filemap: Add support for authoritative mappings
An authoritative mapping knows about all the folios in the mapping. If
read()finds a missing folio, there's no reason to allocate one and try to read it because we know it's a zero region of the file. We can just calliov_iter_zero()instead.Quoted 8 lines
mm/filemap.c +0
-0@@ -2697,6 +2697,8 @@ static int filemap_get_pages(struct kiocb *iocb, size_t count,if (!folio_batch_count (fbatch)) {DEFINE_READAHEAD (ractl, filp, &filp->f_ra, mapping, index);
Quoted 4 lines
mm/filemap.c static int filemap_get_pages(struct kiocb *iocb, size_t count, · line 2701
Quoted 2 lines
mm/filemap.c +2
-0+if (mapping_is_authoritative (mapping)) +return 0 ;Does returning 0 here cause an infinite loop in
filemap_splice_read()?
As before, hugetlb doesn't use splice_read() so adding support would be dead untestable code. We'll add support here when we need it.
Quoted 13 lines
Quoted 13 lines
mm/filemap.c +9
-0@@ -2853,6 +2855,22 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, goto put_folios; end_offset =min_t (loff_t, isize, iocb->ki_pos + iter->count); +if (!folio_batch_count (&fbatch)) { +size_t fsize =mapping_min_folio_nrbytes (mapping); +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 =iov_iter_zero (bytes, iter); + + already_read += copied; + iocb->ki_pos += copied;
Quoted 8 lines
mm/filemap.c ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, · line 2872
Quoted 6 lines
mm/filemap.c +6
-0+ last_pos = iocb->ki_pos; + +if (copied < bytes) + error = -EFAULT ; +continue ; + }Should
filemap_fault()also be updated to handle authoritative mappings?
Eventually, if hugetlbfs is ever converted to use filemap_fault(). As above, it'd be dead code.
That's it for interesting Sashiko reviews. I've omitted some which are suplicates, either within this patch series or have been reported against previous patch series. Feels like we're close!
Matthew Wilcox (Oracle)
maintainerPATCH v5 12/12 hugetlb: replace hugetlbfs_read_iter() with generic_file_read_iter()
In reply to Matthew Wilcox (Oracle)From: Jane Chu <[email protected]>
generic_file_read_iter() now handles hugetlb poison, hugetlb page size and missing folios correctly, so we no longer need a separate hugetlbfs_read_iter().
--- fs/hugetlbfs/inode.c | 110 +------------------------------------------ 1 file changed, 2 insertions(+), 108 deletions(-)
fs/hugetlbfs/inode.c +2 -108
@@ -187,113 +187,6 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,return mm_get_unmapped_area_vmflags (file, addr0, len, pgoff, flags,0 ); } -/* -* Someone wants to read @bytes from a HWPOISON hugetlb @folio from @offset. -* Returns the maximum number of bytes one can read without touching the 1st raw -* HWPOISON page. -*/ -static size_t adjust_range_hwpoison (struct folio *folio,size_t offset, -size_t bytes) -{ -struct page *page =folio_page (folio, offset /PAGE_SIZE ); -size_t safe_bytes; - -if (hugetlb_page_hwpoison (folio, page)) -return 0 ; -/* Safe to read the remaining bytes in this page. */ - safe_bytes =PAGE_SIZE - (offset %PAGE_SIZE ); - page++; - -/* Check each remaining page as long as we are not done yet. */ -for (; safe_bytes < bytes; safe_bytes +=PAGE_SIZE , page++) -if (hugetlb_page_hwpoison (folio, page)) -break ; - -return min (safe_bytes, bytes); -} - -/* -* Support for read() - Find the page attached to f_mapping and copy out the -* data. This provides functionality similar to filemap_read(). -*/ -static ssize_t hugetlbfs_read_iter (struct kiocb *iocb,struct iov_iter *to) -{ -struct file *file = iocb->ki_filp; -struct hstate *h =hstate_file (file); -struct address_space *mapping = file->f_mapping; -struct inode *inode = mapping->host; -unsigned long index = iocb->ki_pos >>huge_page_shift (h); -unsigned long offset = iocb->ki_pos & ~huge_page_mask (h); -unsigned long end_index; -loff_t isize; -ssize_t retval =0 ; - -while (iov_iter_count (to)) { -struct folio *folio; -size_t nr, copied, want; - -/* nr is the maximum number of bytes to copy from this page */ - nr =huge_page_size (h); - isize =i_size_read (inode); -if (!isize) -break ; - end_index = (isize -1 ) >>huge_page_shift (h); -if (index > end_index) -break ; -if (index == end_index) { - nr = ((isize -1 ) & ~huge_page_mask (h)) +1 ; -if (nr <= offset) -break ; - } - nr = nr - offset; - -/* Find the folio */ - folio =filemap_lock_hugetlb_folio (h, mapping, index); -if (IS_ERR (folio)) { -/* -* We have a HOLE, zero out the user-buffer for the -* length of the hole or request. -*/ - copied =iov_iter_zero (nr, to); - }else { -folio_unlock (folio); - -if (!folio_test_has_hwpoisoned (folio)) - want = nr; -else { -/* -* Adjust how many bytes safe to read without -* touching the 1st raw HWPOISON page after -* offset. -*/ - want =adjust_range_hwpoison (folio, offset, nr); -if (want ==0 ) { -folio_put (folio); - retval = -EIO ; -break ; - } - } - -/* -* We have the folio, copy it to user space buffer. -*/ - copied =copy_folio_to_iter (folio, offset, want, to); -folio_put (folio); - } - offset += copied; - retval += copied; -if (copied != nr &&iov_iter_count (to)) { -if (!retval) - retval = -EFAULT ; -break ; - } - index += offset >>huge_page_shift (h); - offset &= ~huge_page_mask (h); - } - iocb->ki_pos = ((loff_t )index <<huge_page_shift (h)) + offset; -return retval; -} - static int hugetlbfs_write_begin(const struct kiocb *iocb, struct address_space *mapping, loff_t pos, unsigned len,
@@ -891,6 +784,7 @@ static void hugetlbfs_init_regular_inode(struct inode *inode) inode->i_op = &hugetlbfs_inode_operations; inode->i_fop = &hugetlbfs_file_operations;mapping_set_folio_order_range (inode->i_mapping, order, order); +mapping_set_authoritative (inode->i_mapping); }/*
@@ -1215,7 +1109,7 @@ static void init_once(void *foo) }static const struct file_operations hugetlbfs_file_operations = { - .read_iter = hugetlbfs_read_iter, + .read_iter = generic_file_read_iter, .mmap = hugetlbfs_file_mmap, .fsync = noop_fsync, .get_unmapped_area = hugetlb_get_unmapped_area,
Signature · 1 line
2.47.3