Skip to content

Commit fd4aed8

Browse files
mjkravetzakpm00
authored andcommitted
hugetlb: revert use of page_cache_next_miss()
Ackerley Tng reported an issue with hugetlbfs fallocate as noted in the Closes tag. The issue showed up after the conversion of hugetlb page cache lookup code to use page_cache_next_miss. User visible effects are: - hugetlbfs fallocate incorrectly returns -EEXIST if pages are presnet in the file. - hugetlb pages will not be included in core dumps if they need to be brought in via GUP. - userfaultfd UFFDIO_COPY will not notice pages already present in the cache. It may try to allocate a new page and potentially return ENOMEM as opposed to EEXIST. Revert the use page_cache_next_miss() in hugetlb code. IMPORTANT NOTE FOR STABLE BACKPORTS: This patch will apply cleanly to v6.3. However, due to the change of filemap_get_folio() return values, it will not function correctly. This patch must be modified for stable backports. [dan.carpenter@linaro.org: fix hugetlbfs_pagecache_present()] Link: https://lkml.kernel.org/r/efa86091-6a2c-4064-8f55-9b44e1313015@moroto.mountain Link: https://lkml.kernel.org/r/20230621212403.174710-2-mike.kravetz@oracle.com Fixes: d0ce0e4 ("mm/hugetlb: convert hugetlb fault paths to use alloc_hugetlb_folio()") Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org> Reported-by: Ackerley Tng <ackerleytng@google.com> Closes: https://lore.kernel.org/linux-mm/cover.1683069252.git.ackerleytng@google.com Reviewed-by: Sidhartha Kumar <sidhartha.kumar@oracle.com> Cc: Erdem Aktas <erdemaktas@google.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Matthew Wilcox <willy@infradead.org> Cc: Muchun Song <songmuchun@bytedance.com> Cc: Vishal Annapurve <vannapurve@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 16f8eb3 commit fd4aed8

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

fs/hugetlbfs/inode.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,6 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
821821
*/
822822
struct folio *folio;
823823
unsigned long addr;
824-
bool present;
825824

826825
cond_resched();
827826

@@ -842,10 +841,9 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
842841
mutex_lock(&hugetlb_fault_mutex_table[hash]);
843842

844843
/* See if already present in mapping to avoid alloc/free */
845-
rcu_read_lock();
846-
present = page_cache_next_miss(mapping, index, 1) != index;
847-
rcu_read_unlock();
848-
if (present) {
844+
folio = filemap_get_folio(mapping, index);
845+
if (!IS_ERR(folio)) {
846+
folio_put(folio);
849847
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
850848
continue;
851849
}

mm/hugetlb.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5728,13 +5728,13 @@ static bool hugetlbfs_pagecache_present(struct hstate *h,
57285728
{
57295729
struct address_space *mapping = vma->vm_file->f_mapping;
57305730
pgoff_t idx = vma_hugecache_offset(h, vma, address);
5731-
bool present;
5732-
5733-
rcu_read_lock();
5734-
present = page_cache_next_miss(mapping, idx, 1) != idx;
5735-
rcu_read_unlock();
5731+
struct folio *folio;
57365732

5737-
return present;
5733+
folio = filemap_get_folio(mapping, idx);
5734+
if (IS_ERR(folio))
5735+
return false;
5736+
folio_put(folio);
5737+
return true;
57385738
}
57395739

57405740
int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping,

0 commit comments

Comments
 (0)