Commit 68f03208 authored by David Hildenbrand's avatar David Hildenbrand Committed by Andrew Morton

mm/rmap: convert folio_add_file_rmap_range() into folio_add_file_rmap_[pte|ptes|pmd]()

Let's get rid of the compound parameter and instead define explicitly
which mappings we're adding.  That is more future proof, easier to read
and harder to mess up.

Use an enum to express the granularity internally.  Make the compiler
always special-case on the granularity by using __always_inline.  Replace
the "compound" check by a switch-case that will be removed by the compiler
completely.

Add plenty of sanity checks with CONFIG_DEBUG_VM.  Replace the
folio_test_pmd_mappable() check by a config check in the caller and sanity
checks.  Convert the single user of folio_add_file_rmap_range().

While at it, consistently use "int" instead of "unisgned int" in rmap code
when dealing with mapcounts and the number of pages.

This function design can later easily be extended to PUDs and to batch
PMDs.  Note that for now we don't support anything bigger than PMD-sized
folios (as we cleanly separated hugetlb handling).  Sanity checks will
catch if that ever changes.

Next up is removing page_remove_rmap() along with its "compound" parameter
and smilarly converting all other rmap functions.

Link: https://lkml.kernel.org/r/20231220224504.646757-8-david@redhat.comSigned-off-by: default avatarDavid Hildenbrand <david@redhat.com>
Reviewed-by: default avatarYin Fengwei <fengwei.yin@intel.com>
Reviewed-by: default avatarRyan Roberts <ryan.roberts@arm.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Muchun Song <songmuchun@bytedance.com>
Cc: Peter Xu <peterx@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent a4ea1864
...@@ -191,6 +191,44 @@ typedef int __bitwise rmap_t; ...@@ -191,6 +191,44 @@ typedef int __bitwise rmap_t;
*/ */
#define RMAP_COMPOUND ((__force rmap_t)BIT(1)) #define RMAP_COMPOUND ((__force rmap_t)BIT(1))
/*
* Internally, we're using an enum to specify the granularity. We make the
* compiler emit specialized code for each granularity.
*/
enum rmap_level {
RMAP_LEVEL_PTE = 0,
RMAP_LEVEL_PMD,
};
static inline void __folio_rmap_sanity_checks(struct folio *folio,
struct page *page, int nr_pages, enum rmap_level level)
{
/* hugetlb folios are handled separately. */
VM_WARN_ON_FOLIO(folio_test_hugetlb(folio), folio);
VM_WARN_ON_FOLIO(folio_test_large(folio) &&
!folio_test_large_rmappable(folio), folio);
VM_WARN_ON_ONCE(nr_pages <= 0);
VM_WARN_ON_FOLIO(page_folio(page) != folio, folio);
VM_WARN_ON_FOLIO(page_folio(page + nr_pages - 1) != folio, folio);
switch (level) {
case RMAP_LEVEL_PTE:
break;
case RMAP_LEVEL_PMD:
/*
* We don't support folios larger than a single PMD yet. So
* when RMAP_LEVEL_PMD is set, we assume that we are creating
* a single "entire" mapping of the folio.
*/
VM_WARN_ON_FOLIO(folio_nr_pages(folio) != HPAGE_PMD_NR, folio);
VM_WARN_ON_FOLIO(nr_pages != HPAGE_PMD_NR, folio);
break;
default:
VM_WARN_ON_ONCE(true);
}
}
/* /*
* rmap interfaces called when adding or removing pte of page * rmap interfaces called when adding or removing pte of page
*/ */
...@@ -201,8 +239,12 @@ void folio_add_new_anon_rmap(struct folio *, struct vm_area_struct *, ...@@ -201,8 +239,12 @@ void folio_add_new_anon_rmap(struct folio *, struct vm_area_struct *,
unsigned long address); unsigned long address);
void page_add_file_rmap(struct page *, struct vm_area_struct *, void page_add_file_rmap(struct page *, struct vm_area_struct *,
bool compound); bool compound);
void folio_add_file_rmap_range(struct folio *, struct page *, unsigned int nr, void folio_add_file_rmap_ptes(struct folio *, struct page *, int nr_pages,
struct vm_area_struct *, bool compound); struct vm_area_struct *);
#define folio_add_file_rmap_pte(folio, page, vma) \
folio_add_file_rmap_ptes(folio, page, 1, vma)
void folio_add_file_rmap_pmd(struct folio *, struct page *,
struct vm_area_struct *);
void page_remove_rmap(struct page *, struct vm_area_struct *, void page_remove_rmap(struct page *, struct vm_area_struct *,
bool compound); bool compound);
......
...@@ -4516,7 +4516,7 @@ void set_pte_range(struct vm_fault *vmf, struct folio *folio, ...@@ -4516,7 +4516,7 @@ void set_pte_range(struct vm_fault *vmf, struct folio *folio,
folio_add_lru_vma(folio, vma); folio_add_lru_vma(folio, vma);
} else { } else {
add_mm_counter(vma->vm_mm, mm_counter_file(page), nr); add_mm_counter(vma->vm_mm, mm_counter_file(page), nr);
folio_add_file_rmap_range(folio, page, nr, vma, false); folio_add_file_rmap_ptes(folio, page, nr, vma);
} }
set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr); set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr);
......
...@@ -1378,31 +1378,18 @@ void folio_add_new_anon_rmap(struct folio *folio, struct vm_area_struct *vma, ...@@ -1378,31 +1378,18 @@ void folio_add_new_anon_rmap(struct folio *folio, struct vm_area_struct *vma,
__lruvec_stat_mod_folio(folio, NR_ANON_MAPPED, nr); __lruvec_stat_mod_folio(folio, NR_ANON_MAPPED, nr);
} }
/** static __always_inline void __folio_add_file_rmap(struct folio *folio,
* folio_add_file_rmap_range - add pte mapping to page range of a folio struct page *page, int nr_pages, struct vm_area_struct *vma,
* @folio: The folio to add the mapping to enum rmap_level level)
* @page: The first page to add
* @nr_pages: The number of pages which will be mapped
* @vma: the vm area in which the mapping is added
* @compound: charge the page as compound or small page
*
* The page range of folio is defined by [first_page, first_page + nr_pages)
*
* The caller needs to hold the pte lock.
*/
void folio_add_file_rmap_range(struct folio *folio, struct page *page,
unsigned int nr_pages, struct vm_area_struct *vma,
bool compound)
{ {
atomic_t *mapped = &folio->_nr_pages_mapped; atomic_t *mapped = &folio->_nr_pages_mapped;
unsigned int nr_pmdmapped = 0, first; int nr = 0, nr_pmdmapped = 0, first;
int nr = 0;
VM_WARN_ON_FOLIO(folio_test_hugetlb(folio), folio); VM_WARN_ON_FOLIO(folio_test_anon(folio), folio);
VM_WARN_ON_FOLIO(compound && !folio_test_pmd_mappable(folio), folio); __folio_rmap_sanity_checks(folio, page, nr_pages, level);
/* Is page being mapped by PTE? Is this its first map to be added? */ switch (level) {
if (likely(!compound)) { case RMAP_LEVEL_PTE:
do { do {
first = atomic_inc_and_test(&page->_mapcount); first = atomic_inc_and_test(&page->_mapcount);
if (first && folio_test_large(folio)) { if (first && folio_test_large(folio)) {
...@@ -1413,9 +1400,8 @@ void folio_add_file_rmap_range(struct folio *folio, struct page *page, ...@@ -1413,9 +1400,8 @@ void folio_add_file_rmap_range(struct folio *folio, struct page *page,
if (first) if (first)
nr++; nr++;
} while (page++, --nr_pages > 0); } while (page++, --nr_pages > 0);
} else if (folio_test_pmd_mappable(folio)) { break;
/* That test is redundant: it's for safety or to optimize out */ case RMAP_LEVEL_PMD:
first = atomic_inc_and_test(&folio->_entire_mapcount); first = atomic_inc_and_test(&folio->_entire_mapcount);
if (first) { if (first) {
nr = atomic_add_return_relaxed(COMPOUND_MAPPED, mapped); nr = atomic_add_return_relaxed(COMPOUND_MAPPED, mapped);
...@@ -1430,6 +1416,7 @@ void folio_add_file_rmap_range(struct folio *folio, struct page *page, ...@@ -1430,6 +1416,7 @@ void folio_add_file_rmap_range(struct folio *folio, struct page *page,
nr = 0; nr = 0;
} }
} }
break;
} }
if (nr_pmdmapped) if (nr_pmdmapped)
...@@ -1443,6 +1430,43 @@ void folio_add_file_rmap_range(struct folio *folio, struct page *page, ...@@ -1443,6 +1430,43 @@ void folio_add_file_rmap_range(struct folio *folio, struct page *page,
mlock_vma_folio(folio, vma); mlock_vma_folio(folio, vma);
} }
/**
* folio_add_file_rmap_ptes - add PTE mappings to a page range of a folio
* @folio: The folio to add the mappings to
* @page: The first page to add
* @nr_pages: The number of pages that will be mapped using PTEs
* @vma: The vm area in which the mappings are added
*
* The page range of the folio is defined by [page, page + nr_pages)
*
* The caller needs to hold the page table lock.
*/
void folio_add_file_rmap_ptes(struct folio *folio, struct page *page,
int nr_pages, struct vm_area_struct *vma)
{
__folio_add_file_rmap(folio, page, nr_pages, vma, RMAP_LEVEL_PTE);
}
/**
* folio_add_file_rmap_pmd - add a PMD mapping to a page range of a folio
* @folio: The folio to add the mapping to
* @page: The first page to add
* @vma: The vm area in which the mapping is added
*
* The page range of the folio is defined by [page, page + HPAGE_PMD_NR)
*
* The caller needs to hold the page table lock.
*/
void folio_add_file_rmap_pmd(struct folio *folio, struct page *page,
struct vm_area_struct *vma)
{
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
__folio_add_file_rmap(folio, page, HPAGE_PMD_NR, vma, RMAP_LEVEL_PMD);
#else
WARN_ON_ONCE(true);
#endif
}
/** /**
* page_add_file_rmap - add pte mapping to a file page * page_add_file_rmap - add pte mapping to a file page
* @page: the page to add the mapping to * @page: the page to add the mapping to
...@@ -1455,16 +1479,13 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, ...@@ -1455,16 +1479,13 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,
bool compound) bool compound)
{ {
struct folio *folio = page_folio(page); struct folio *folio = page_folio(page);
unsigned int nr_pages;
VM_WARN_ON_ONCE_PAGE(compound && !PageTransHuge(page), page); VM_WARN_ON_ONCE_PAGE(compound && !PageTransHuge(page), page);
if (likely(!compound)) if (likely(!compound))
nr_pages = 1; folio_add_file_rmap_pte(folio, page, vma);
else else
nr_pages = folio_nr_pages(folio); folio_add_file_rmap_pmd(folio, page, vma);
folio_add_file_rmap_range(folio, page, nr_pages, vma, compound);
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment