linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/8] mm: convert mm counter to take a folio
@ 2024-01-11 11:12 Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 1/8] mm: swap: introduce pfn_swap_entry_to_folio() Kefeng Wang
                   ` (7 more replies)
  0 siblings, 8 replies; 12+ messages in thread
From: Kefeng Wang @ 2024-01-11 11:12 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: willy, david, linux-s390, linux-perf-users, Kefeng Wang

Make all mm_counter() and mm_counter_file() callers to use a folio,
then convert mm counter functions to take a folio, which saves some
of compound_head() calls.

v2:
- rebase on v6.7, since most of mm counter callers already with a folio,
  drop some unneeded changes.

v1:
- rename should_zap_page() to should_zap_folio(), per Matthew Wilcox 
- pass page to page_remove_rmap/page_try_dup_anon_rmap for
  device private page, per Matthew Wilcox

Kefeng Wang (8):
  mm: swap: introduce pfn_swap_entry_to_folio()
  s390: pgtable: use pfn_swap_entry_to_folio() in ptep_zap_swap_entry()
  mm: huge_memory: use pfn_swap_entry_to_folio() in
    __split_huge_pmd_locked()
  mm: huge_memory: use pfn_swap_entry_to_folio() in zap_huge_pmd()
  mm: memory: use pfn_swap_entry_to_folio() in copy_nonpresent_pte()
  mm: memory: convert to should_zap_folio()
  mm: convert mm_counter() to take a folio
  mm: convert mm_counter_file() to take a folio

 arch/s390/mm/pgtable.c  |  4 ++--
 include/linux/mm.h      | 12 +++++-----
 include/linux/swapops.h | 13 +++++++++++
 kernel/events/uprobes.c |  2 +-
 mm/huge_memory.c        | 21 ++++++++++--------
 mm/khugepaged.c         |  4 ++--
 mm/memory.c             | 49 ++++++++++++++++++++++-------------------
 mm/rmap.c               | 10 ++++-----
 mm/userfaultfd.c        |  2 +-
 9 files changed, 68 insertions(+), 49 deletions(-)

-- 
2.27.0



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

* [PATCH v2 1/8] mm: swap: introduce pfn_swap_entry_to_folio()
  2024-01-11 11:12 [PATCH v2 0/8] mm: convert mm counter to take a folio Kefeng Wang
@ 2024-01-11 11:12 ` Kefeng Wang
  2024-01-11 14:37   ` Matthew Wilcox
  2024-01-11 11:12 ` [PATCH v2 2/8] s390: pgtable: use pfn_swap_entry_to_folio() in ptep_zap_swap_entry() Kefeng Wang
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 12+ messages in thread
From: Kefeng Wang @ 2024-01-11 11:12 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: willy, david, linux-s390, linux-perf-users, Kefeng Wang

Introduce a new pfn_swap_entry_to_folio(), it is similar to
pfn_swap_entry_to_page(), but return a folio, which allow us
to completely replace the struct page variables with struct
folio variables.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 include/linux/swapops.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/include/linux/swapops.h b/include/linux/swapops.h
index bff1e8d97de0..85cb84e4be95 100644
--- a/include/linux/swapops.h
+++ b/include/linux/swapops.h
@@ -468,6 +468,19 @@ static inline struct page *pfn_swap_entry_to_page(swp_entry_t entry)
 	return p;
 }
 
+static inline struct folio *pfn_swap_entry_to_folio(swp_entry_t entry)
+{
+	struct folio *folio = pfn_folio(swp_offset_pfn(entry));
+
+	/*
+	 * Any use of migration entries may only occur while the
+	 * corresponding folio is locked
+	 */
+	BUG_ON(is_migration_entry(entry) && !folio_test_locked(folio));
+
+	return folio;
+}
+
 /*
  * A pfn swap entry is a special type of swap entry that always has a pfn stored
  * in the swap offset. They are used to represent unaddressable device memory
-- 
2.27.0



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

* [PATCH v2 2/8] s390: pgtable: use pfn_swap_entry_to_folio() in ptep_zap_swap_entry()
  2024-01-11 11:12 [PATCH v2 0/8] mm: convert mm counter to take a folio Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 1/8] mm: swap: introduce pfn_swap_entry_to_folio() Kefeng Wang
@ 2024-01-11 11:12 ` Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 3/8] mm: huge_memory: use pfn_swap_entry_to_folio() in __split_huge_pmd_locked() Kefeng Wang
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2024-01-11 11:12 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: willy, david, linux-s390, linux-perf-users, Kefeng Wang

Using pfn_swap_entry_to_folio() in ptep_zap_swap_entry(), which is
preparetion for converting mm counter functions to take a folio.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 arch/s390/mm/pgtable.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/s390/mm/pgtable.c b/arch/s390/mm/pgtable.c
index 99422926efe1..e8fc5c55968e 100644
--- a/arch/s390/mm/pgtable.c
+++ b/arch/s390/mm/pgtable.c
@@ -721,9 +721,9 @@ static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
 	if (!non_swap_entry(entry))
 		dec_mm_counter(mm, MM_SWAPENTS);
 	else if (is_migration_entry(entry)) {
-		struct page *page = pfn_swap_entry_to_page(entry);
+		struct folio *folio = pfn_swap_entry_to_folio(entry);
 
-		dec_mm_counter(mm, mm_counter(page));
+		dec_mm_counter(mm, mm_counter(&folio->page));
 	}
 	free_swap_and_cache(entry);
 }
-- 
2.27.0



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

* [PATCH v2 3/8] mm: huge_memory: use pfn_swap_entry_to_folio() in __split_huge_pmd_locked()
  2024-01-11 11:12 [PATCH v2 0/8] mm: convert mm counter to take a folio Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 1/8] mm: swap: introduce pfn_swap_entry_to_folio() Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 2/8] s390: pgtable: use pfn_swap_entry_to_folio() in ptep_zap_swap_entry() Kefeng Wang
@ 2024-01-11 11:12 ` Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 4/8] mm: huge_memory: use pfn_swap_entry_to_folio() in zap_huge_pmd() Kefeng Wang
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2024-01-11 11:12 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: willy, david, linux-s390, linux-perf-users, Kefeng Wang

Using pfn_swap_entry_to_folio() in __split_huge_pmd_locked() which is
preparetion for converting mm counter functions to take a folio.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 mm/huge_memory.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 94ef5c02b459..6139887933a7 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2432,7 +2432,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 			swp_entry_t entry;
 
 			entry = pmd_to_swp_entry(old_pmd);
-			page = pfn_swap_entry_to_page(entry);
+			folio = pfn_swap_entry_to_folio(entry);
 		} else {
 			page = pmd_page(old_pmd);
 			folio = page_folio(page);
@@ -2443,7 +2443,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 			folio_remove_rmap_pmd(folio, page, vma);
 			folio_put(folio);
 		}
-		add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR);
+		add_mm_counter(mm, mm_counter_file(&folio->page), -HPAGE_PMD_NR);
 		return;
 	}
 
-- 
2.27.0



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

* [PATCH v2 4/8] mm: huge_memory: use pfn_swap_entry_to_folio() in zap_huge_pmd()
  2024-01-11 11:12 [PATCH v2 0/8] mm: convert mm counter to take a folio Kefeng Wang
                   ` (2 preceding siblings ...)
  2024-01-11 11:12 ` [PATCH v2 3/8] mm: huge_memory: use pfn_swap_entry_to_folio() in __split_huge_pmd_locked() Kefeng Wang
@ 2024-01-11 11:12 ` Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 5/8] mm: memory: use pfn_swap_entry_to_folio() in copy_nonpresent_pte() Kefeng Wang
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2024-01-11 11:12 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: willy, david, linux-s390, linux-perf-users, Kefeng Wang

Using pfn_swap_entry_to_folio() in zap_huge_pmd(), which is a
preparetion for converting mm counter functions to take a folio.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 mm/huge_memory.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 6139887933a7..b024edb55855 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1895,12 +1895,14 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		zap_deposited_table(tlb->mm, pmd);
 		spin_unlock(ptl);
 	} else {
-		struct page *page = NULL;
+		struct folio *folio = NULL;
 		int flush_needed = 1;
 
 		if (pmd_present(orig_pmd)) {
-			page = pmd_page(orig_pmd);
-			folio_remove_rmap_pmd(page_folio(page), page, vma);
+			struct page *page = pmd_page(orig_pmd);
+
+			folio = page_folio(page);
+			folio_remove_rmap_pmd(folio, page, vma);
 			VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
 			VM_BUG_ON_PAGE(!PageHead(page), page);
 		} else if (thp_migration_supported()) {
@@ -1908,23 +1910,24 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 
 			VM_BUG_ON(!is_pmd_migration_entry(orig_pmd));
 			entry = pmd_to_swp_entry(orig_pmd);
-			page = pfn_swap_entry_to_page(entry);
+			folio = pfn_swap_entry_to_folio(entry);
 			flush_needed = 0;
 		} else
 			WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
 
-		if (PageAnon(page)) {
+		if (folio_test_anon(folio)) {
 			zap_deposited_table(tlb->mm, pmd);
 			add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
 		} else {
 			if (arch_needs_pgtable_deposit())
 				zap_deposited_table(tlb->mm, pmd);
-			add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR);
+			add_mm_counter(tlb->mm, mm_counter_file(&folio->page),
+				       -HPAGE_PMD_NR);
 		}
 
 		spin_unlock(ptl);
 		if (flush_needed)
-			tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
+			tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE);
 	}
 	return 1;
 }
-- 
2.27.0



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

* [PATCH v2 5/8] mm: memory: use pfn_swap_entry_to_folio() in copy_nonpresent_pte()
  2024-01-11 11:12 [PATCH v2 0/8] mm: convert mm counter to take a folio Kefeng Wang
                   ` (3 preceding siblings ...)
  2024-01-11 11:12 ` [PATCH v2 4/8] mm: huge_memory: use pfn_swap_entry_to_folio() in zap_huge_pmd() Kefeng Wang
@ 2024-01-11 11:12 ` Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 6/8] mm: memory: convert to should_zap_folio() Kefeng Wang
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2024-01-11 11:12 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: willy, david, linux-s390, linux-perf-users, Kefeng Wang

Using pfn_swap_entry_to_folio() which is preparetion for
converting mm counter functions to take a folio.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 mm/memory.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index c66af4520958..7b3fbeb21534 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -806,9 +806,9 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 		}
 		rss[MM_SWAPENTS]++;
 	} else if (is_migration_entry(entry)) {
-		page = pfn_swap_entry_to_page(entry);
+		folio = pfn_swap_entry_to_folio(entry);
 
-		rss[mm_counter(page)]++;
+		rss[mm_counter(&folio->page)]++;
 
 		if (!is_readable_migration_entry(entry) &&
 				is_cow_mapping(vm_flags)) {
-- 
2.27.0



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

* [PATCH v2 6/8] mm: memory: convert to should_zap_folio()
  2024-01-11 11:12 [PATCH v2 0/8] mm: convert mm counter to take a folio Kefeng Wang
                   ` (4 preceding siblings ...)
  2024-01-11 11:12 ` [PATCH v2 5/8] mm: memory: use pfn_swap_entry_to_folio() in copy_nonpresent_pte() Kefeng Wang
@ 2024-01-11 11:12 ` Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 7/8] mm: convert mm_counter() to take a folio Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 8/8] mm: convert mm_counter_file() " Kefeng Wang
  7 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2024-01-11 11:12 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: willy, david, linux-s390, linux-perf-users, Kefeng Wang

Make should_zap_page() to take a folio and convert it to
should_zap_folio() which is preparetion for converting mm
counter functions to take a folio.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 mm/memory.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 7b3fbeb21534..afba8b156457 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1369,19 +1369,20 @@ static inline bool should_zap_cows(struct zap_details *details)
 	return details->even_cows;
 }
 
-/* Decides whether we should zap this page with the page pointer specified */
-static inline bool should_zap_page(struct zap_details *details, struct page *page)
+/* Decides whether we should zap this folio with the folio pointer specified */
+static inline bool should_zap_folio(struct zap_details *details,
+				    struct folio *folio)
 {
-	/* If we can make a decision without *page.. */
+	/* If we can make a decision without *folio.. */
 	if (should_zap_cows(details))
 		return true;
 
-	/* E.g. the caller passes NULL for the case of a zero page */
-	if (!page)
+	/* E.g. the caller passes NULL for the case of a zero folio */
+	if (!folio)
 		return true;
 
-	/* Otherwise we should only zap non-anon pages */
-	return !PageAnon(page);
+	/* Otherwise we should only zap non-anon folios */
+	return !folio_test_anon(folio);
 }
 
 static inline bool zap_drop_file_uffd_wp(struct zap_details *details)
@@ -1447,7 +1448,10 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 			unsigned int delay_rmap;
 
 			page = vm_normal_page(vma, addr, ptent);
-			if (unlikely(!should_zap_page(details, page)))
+			if (page)
+				folio = page_folio(page);
+
+			if (unlikely(!should_zap_folio(details, folio)))
 				continue;
 			ptent = ptep_get_and_clear_full(mm, addr, pte,
 							tlb->fullmm);
@@ -1460,7 +1464,6 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 				continue;
 			}
 
-			folio = page_folio(page);
 			delay_rmap = 0;
 			if (!folio_test_anon(folio)) {
 				if (pte_dirty(ptent)) {
@@ -1492,7 +1495,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 		    is_device_exclusive_entry(entry)) {
 			page = pfn_swap_entry_to_page(entry);
 			folio = page_folio(page);
-			if (unlikely(!should_zap_page(details, page)))
+			if (unlikely(!should_zap_folio(details, folio)))
 				continue;
 			/*
 			 * Both device private/exclusive mappings should only
@@ -1513,10 +1516,10 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 			if (unlikely(!free_swap_and_cache(entry)))
 				print_bad_pte(vma, addr, ptent, NULL);
 		} else if (is_migration_entry(entry)) {
-			page = pfn_swap_entry_to_page(entry);
-			if (!should_zap_page(details, page))
+			folio = pfn_swap_entry_to_folio(entry);
+			if (!should_zap_folio(details, folio))
 				continue;
-			rss[mm_counter(page)]--;
+			rss[mm_counter(&folio->page)]--;
 		} else if (pte_marker_entry_uffd_wp(entry)) {
 			/*
 			 * For anon: always drop the marker; for file: only
-- 
2.27.0



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

* [PATCH v2 7/8] mm: convert mm_counter() to take a folio
  2024-01-11 11:12 [PATCH v2 0/8] mm: convert mm counter to take a folio Kefeng Wang
                   ` (5 preceding siblings ...)
  2024-01-11 11:12 ` [PATCH v2 6/8] mm: memory: convert to should_zap_folio() Kefeng Wang
@ 2024-01-11 11:12 ` Kefeng Wang
  2024-01-11 11:12 ` [PATCH v2 8/8] mm: convert mm_counter_file() " Kefeng Wang
  7 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2024-01-11 11:12 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: willy, david, linux-s390, linux-perf-users, Kefeng Wang

Since all mm_counter() callers with a folio, let's convert
mm_counter() to take a folio.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 arch/s390/mm/pgtable.c |  2 +-
 include/linux/mm.h     |  6 +++---
 mm/memory.c            | 10 +++++-----
 mm/rmap.c              |  8 ++++----
 mm/userfaultfd.c       |  2 +-
 5 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/arch/s390/mm/pgtable.c b/arch/s390/mm/pgtable.c
index e8fc5c55968e..4c92b08e3c59 100644
--- a/arch/s390/mm/pgtable.c
+++ b/arch/s390/mm/pgtable.c
@@ -723,7 +723,7 @@ static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
 	else if (is_migration_entry(entry)) {
 		struct folio *folio = pfn_swap_entry_to_folio(entry);
 
-		dec_mm_counter(mm, mm_counter(&folio->page));
+		dec_mm_counter(mm, mm_counter(folio));
 	}
 	free_swap_and_cache(entry);
 }
diff --git a/include/linux/mm.h b/include/linux/mm.h
index f5a97dec5169..22e597b36b38 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2603,11 +2603,11 @@ static inline int mm_counter_file(struct page *page)
 	return MM_FILEPAGES;
 }
 
-static inline int mm_counter(struct page *page)
+static inline int mm_counter(struct folio *folio)
 {
-	if (PageAnon(page))
+	if (folio_test_anon(folio))
 		return MM_ANONPAGES;
-	return mm_counter_file(page);
+	return mm_counter_file(&folio->page);
 }
 
 static inline unsigned long get_mm_rss(struct mm_struct *mm)
diff --git a/mm/memory.c b/mm/memory.c
index afba8b156457..2f858263e5a2 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -808,7 +808,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 	} else if (is_migration_entry(entry)) {
 		folio = pfn_swap_entry_to_folio(entry);
 
-		rss[mm_counter(&folio->page)]++;
+		rss[mm_counter(folio)]++;
 
 		if (!is_readable_migration_entry(entry) &&
 				is_cow_mapping(vm_flags)) {
@@ -840,7 +840,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 		 * keep things as they are.
 		 */
 		folio_get(folio);
-		rss[mm_counter(page)]++;
+		rss[mm_counter(folio)]++;
 		/* Cannot fail as these pages cannot get pinned. */
 		folio_try_dup_anon_rmap_pte(folio, page, src_vma);
 
@@ -1476,7 +1476,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 				if (pte_young(ptent) && likely(vma_has_recency(vma)))
 					folio_mark_accessed(folio);
 			}
-			rss[mm_counter(page)]--;
+			rss[mm_counter(folio)]--;
 			if (!delay_rmap) {
 				folio_remove_rmap_pte(folio, page, vma);
 				if (unlikely(page_mapcount(page) < 0))
@@ -1504,7 +1504,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 			 * see zap_install_uffd_wp_if_needed().
 			 */
 			WARN_ON_ONCE(!vma_is_anonymous(vma));
-			rss[mm_counter(page)]--;
+			rss[mm_counter(folio)]--;
 			if (is_device_private_entry(entry))
 				folio_remove_rmap_pte(folio, page, vma);
 			folio_put(folio);
@@ -1519,7 +1519,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
 			folio = pfn_swap_entry_to_folio(entry);
 			if (!should_zap_folio(details, folio))
 				continue;
-			rss[mm_counter(&folio->page)]--;
+			rss[mm_counter(folio)]--;
 		} else if (pte_marker_entry_uffd_wp(entry)) {
 			/*
 			 * For anon: always drop the marker; for file: only
diff --git a/mm/rmap.c b/mm/rmap.c
index f5d43edad529..4648cf1d8178 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1780,7 +1780,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 				set_huge_pte_at(mm, address, pvmw.pte, pteval,
 						hsz);
 			} else {
-				dec_mm_counter(mm, mm_counter(&folio->page));
+				dec_mm_counter(mm, mm_counter(folio));
 				set_pte_at(mm, address, pvmw.pte, pteval);
 			}
 
@@ -1795,7 +1795,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			 * migration) will not expect userfaults on already
 			 * copied pages.
 			 */
-			dec_mm_counter(mm, mm_counter(&folio->page));
+			dec_mm_counter(mm, mm_counter(folio));
 		} else if (folio_test_anon(folio)) {
 			swp_entry_t entry = page_swap_entry(subpage);
 			pte_t swp_pte;
@@ -2181,7 +2181,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 				set_huge_pte_at(mm, address, pvmw.pte, pteval,
 						hsz);
 			} else {
-				dec_mm_counter(mm, mm_counter(&folio->page));
+				dec_mm_counter(mm, mm_counter(folio));
 				set_pte_at(mm, address, pvmw.pte, pteval);
 			}
 
@@ -2196,7 +2196,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 			 * migration) will not expect userfaults on already
 			 * copied pages.
 			 */
-			dec_mm_counter(mm, mm_counter(&folio->page));
+			dec_mm_counter(mm, mm_counter(folio));
 		} else {
 			swp_entry_t entry;
 			pte_t swp_pte;
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 216ab4c8621f..662ab304cca3 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -124,7 +124,7 @@ int mfill_atomic_install_pte(pmd_t *dst_pmd,
 	 * Must happen after rmap, as mm_counter() checks mapping (via
 	 * PageAnon()), which is set by __page_set_anon_rmap().
 	 */
-	inc_mm_counter(dst_mm, mm_counter(page));
+	inc_mm_counter(dst_mm, mm_counter(folio));
 
 	set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
 
-- 
2.27.0



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

* [PATCH v2 8/8] mm: convert mm_counter_file() to take a folio
  2024-01-11 11:12 [PATCH v2 0/8] mm: convert mm counter to take a folio Kefeng Wang
                   ` (6 preceding siblings ...)
  2024-01-11 11:12 ` [PATCH v2 7/8] mm: convert mm_counter() to take a folio Kefeng Wang
@ 2024-01-11 11:12 ` Kefeng Wang
  7 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2024-01-11 11:12 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: willy, david, linux-s390, linux-perf-users, Kefeng Wang

Since all mm_counter_file() callers with a folio, let's convert
mm_counter_file() to take a folio.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 include/linux/mm.h      |  8 ++++----
 kernel/events/uprobes.c |  2 +-
 mm/huge_memory.c        |  4 ++--
 mm/khugepaged.c         |  4 ++--
 mm/memory.c             | 10 +++++-----
 mm/rmap.c               |  2 +-
 6 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 22e597b36b38..ac6b71cbdffb 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2595,10 +2595,10 @@ static inline void dec_mm_counter(struct mm_struct *mm, int member)
 	mm_trace_rss_stat(mm, member);
 }
 
-/* Optimized variant when page is already known not to be PageAnon */
-static inline int mm_counter_file(struct page *page)
+/* Optimized variant when folio is already known not to be anon */
+static inline int mm_counter_file(struct folio *folio)
 {
-	if (PageSwapBacked(page))
+	if (folio_test_swapbacked(folio))
 		return MM_SHMEMPAGES;
 	return MM_FILEPAGES;
 }
@@ -2607,7 +2607,7 @@ static inline int mm_counter(struct folio *folio)
 {
 	if (folio_test_anon(folio))
 		return MM_ANONPAGES;
-	return mm_counter_file(&folio->page);
+	return mm_counter_file(folio);
 }
 
 static inline unsigned long get_mm_rss(struct mm_struct *mm)
diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 485bb0389b48..948c2e064ca3 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -188,7 +188,7 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
 		dec_mm_counter(mm, MM_ANONPAGES);
 
 	if (!folio_test_anon(old_folio)) {
-		dec_mm_counter(mm, mm_counter_file(old_page));
+		dec_mm_counter(mm, mm_counter_file(old_folio));
 		inc_mm_counter(mm, MM_ANONPAGES);
 	}
 
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index b024edb55855..f40137f927dd 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1921,7 +1921,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		} else {
 			if (arch_needs_pgtable_deposit())
 				zap_deposited_table(tlb->mm, pmd);
-			add_mm_counter(tlb->mm, mm_counter_file(&folio->page),
+			add_mm_counter(tlb->mm, mm_counter_file(folio),
 				       -HPAGE_PMD_NR);
 		}
 
@@ -2446,7 +2446,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 			folio_remove_rmap_pmd(folio, page, vma);
 			folio_put(folio);
 		}
-		add_mm_counter(mm, mm_counter_file(&folio->page), -HPAGE_PMD_NR);
+		add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
 		return;
 	}
 
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 2b219acb528e..fe43fbc44525 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1634,7 +1634,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	/* step 3: set proper refcount and mm_counters. */
 	if (nr_ptes) {
 		folio_ref_sub(folio, nr_ptes);
-		add_mm_counter(mm, mm_counter_file(&folio->page), -nr_ptes);
+		add_mm_counter(mm, mm_counter_file(folio), -nr_ptes);
 	}
 
 	/* step 4: remove empty page table */
@@ -1665,7 +1665,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	if (nr_ptes) {
 		flush_tlb_mm(mm);
 		folio_ref_sub(folio, nr_ptes);
-		add_mm_counter(mm, mm_counter_file(&folio->page), -nr_ptes);
+		add_mm_counter(mm, mm_counter_file(folio), -nr_ptes);
 	}
 	if (start_pte)
 		pte_unmap_unlock(start_pte, ptl);
diff --git a/mm/memory.c b/mm/memory.c
index 2f858263e5a2..033035c58b8d 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -966,7 +966,7 @@ copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
 	} else if (page) {
 		folio_get(folio);
 		folio_dup_file_rmap_pte(folio, page);
-		rss[mm_counter_file(page)]++;
+		rss[mm_counter_file(folio)]++;
 	}
 
 	/*
@@ -1873,7 +1873,7 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte,
 		return -EBUSY;
 	/* Ok, finally just insert the thing.. */
 	folio_get(folio);
-	inc_mm_counter(vma->vm_mm, mm_counter_file(page));
+	inc_mm_counter(vma->vm_mm, mm_counter_file(folio));
 	folio_add_file_rmap_pte(folio, page, vma);
 	set_pte_at(vma->vm_mm, addr, pte, mk_pte(page, prot));
 	return 0;
@@ -3178,7 +3178,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
 	if (likely(vmf->pte && pte_same(ptep_get(vmf->pte), vmf->orig_pte))) {
 		if (old_folio) {
 			if (!folio_test_anon(old_folio)) {
-				dec_mm_counter(mm, mm_counter_file(&old_folio->page));
+				dec_mm_counter(mm, mm_counter_file(old_folio));
 				inc_mm_counter(mm, MM_ANONPAGES);
 			}
 		} else {
@@ -4463,7 +4463,7 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
 	if (write)
 		entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
 
-	add_mm_counter(vma->vm_mm, mm_counter_file(page), HPAGE_PMD_NR);
+	add_mm_counter(vma->vm_mm, mm_counter_file(folio), HPAGE_PMD_NR);
 	folio_add_file_rmap_pmd(folio, page, vma);
 
 	/*
@@ -4526,7 +4526,7 @@ void set_pte_range(struct vm_fault *vmf, struct folio *folio,
 		folio_add_new_anon_rmap(folio, vma, addr);
 		folio_add_lru_vma(folio, vma);
 	} else {
-		add_mm_counter(vma->vm_mm, mm_counter_file(page), nr);
+		add_mm_counter(vma->vm_mm, mm_counter_file(folio), nr);
 		folio_add_file_rmap_ptes(folio, page, nr, vma);
 	}
 	set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr);
diff --git a/mm/rmap.c b/mm/rmap.c
index 4648cf1d8178..1cf2bffa48ed 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1903,7 +1903,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			 *
 			 * See Documentation/mm/mmu_notifier.rst
 			 */
-			dec_mm_counter(mm, mm_counter_file(&folio->page));
+			dec_mm_counter(mm, mm_counter_file(folio));
 		}
 discard:
 		if (unlikely(folio_test_hugetlb(folio)))
-- 
2.27.0



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

* Re: [PATCH v2 1/8] mm: swap: introduce pfn_swap_entry_to_folio()
  2024-01-11 11:12 ` [PATCH v2 1/8] mm: swap: introduce pfn_swap_entry_to_folio() Kefeng Wang
@ 2024-01-11 14:37   ` Matthew Wilcox
  2024-01-11 14:39     ` Matthew Wilcox
  0 siblings, 1 reply; 12+ messages in thread
From: Matthew Wilcox @ 2024-01-11 14:37 UTC (permalink / raw)
  To: Kefeng Wang; +Cc: Andrew Morton, linux-mm, david, linux-s390, linux-perf-users

On Thu, Jan 11, 2024 at 07:12:32PM +0800, Kefeng Wang wrote:
> Introduce a new pfn_swap_entry_to_folio(), it is similar to
> pfn_swap_entry_to_page(), but return a folio, which allow us
> to completely replace the struct page variables with struct
> folio variables.

I have the exact same patch in my tree except I called it
pfn_swap_entry_folio().  I also noted:

    Thanks to the compound_head() hidden inside PageLocked(), this saves a
    call to compound_head() over calling page_folio(pfn_swap_entry_to_page())

> Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>

Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>


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

* Re: [PATCH v2 1/8] mm: swap: introduce pfn_swap_entry_to_folio()
  2024-01-11 14:37   ` Matthew Wilcox
@ 2024-01-11 14:39     ` Matthew Wilcox
  2024-01-12  4:17       ` Kefeng Wang
  0 siblings, 1 reply; 12+ messages in thread
From: Matthew Wilcox @ 2024-01-11 14:39 UTC (permalink / raw)
  To: Kefeng Wang; +Cc: Andrew Morton, linux-mm, david, linux-s390, linux-perf-users

On Thu, Jan 11, 2024 at 02:37:41PM +0000, Matthew Wilcox wrote:
> On Thu, Jan 11, 2024 at 07:12:32PM +0800, Kefeng Wang wrote:
> > Introduce a new pfn_swap_entry_to_folio(), it is similar to
> > pfn_swap_entry_to_page(), but return a folio, which allow us
> > to completely replace the struct page variables with struct
> > folio variables.
> 
> I have the exact same patch in my tree except I called it
> pfn_swap_entry_folio().  I also noted:
> 
>     Thanks to the compound_head() hidden inside PageLocked(), this saves a
>     call to compound_head() over calling page_folio(pfn_swap_entry_to_page())
> 
> > Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
> 
> Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>

Oh, and I converted a couple of callers ...
migration_entry_wait_on_locked and change_huge_pmd.  Those should
probably be added to this patch.


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

* Re: [PATCH v2 1/8] mm: swap: introduce pfn_swap_entry_to_folio()
  2024-01-11 14:39     ` Matthew Wilcox
@ 2024-01-12  4:17       ` Kefeng Wang
  0 siblings, 0 replies; 12+ messages in thread
From: Kefeng Wang @ 2024-01-12  4:17 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Andrew Morton, linux-mm, david, linux-s390, linux-perf-users



On 2024/1/11 22:39, Matthew Wilcox wrote:
> On Thu, Jan 11, 2024 at 02:37:41PM +0000, Matthew Wilcox wrote:
>> On Thu, Jan 11, 2024 at 07:12:32PM +0800, Kefeng Wang wrote:
>>> Introduce a new pfn_swap_entry_to_folio(), it is similar to
>>> pfn_swap_entry_to_page(), but return a folio, which allow us
>>> to completely replace the struct page variables with struct
>>> folio variables.
>>
>> I have the exact same patch in my tree except I called it
>> pfn_swap_entry_folio().  I also noted:
>>
>>      Thanks to the compound_head() hidden inside PageLocked(), this saves a
>>      call to compound_head() over calling page_folio(pfn_swap_entry_to_page())
>>
>>> Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
>>
>> Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> 
> Oh, and I converted a couple of callers ...
> migration_entry_wait_on_locked and change_huge_pmd.  Those should
> probably be added to this patch.
> 

Thanks for your update, and all above conversion in your v3, and I check
more callers,  eg, hugetlb_change_protection,break_ksm_pmd_entry could
be converted too, but we could send separate patch.


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

end of thread, other threads:[~2024-01-12  4:17 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-11 11:12 [PATCH v2 0/8] mm: convert mm counter to take a folio Kefeng Wang
2024-01-11 11:12 ` [PATCH v2 1/8] mm: swap: introduce pfn_swap_entry_to_folio() Kefeng Wang
2024-01-11 14:37   ` Matthew Wilcox
2024-01-11 14:39     ` Matthew Wilcox
2024-01-12  4:17       ` Kefeng Wang
2024-01-11 11:12 ` [PATCH v2 2/8] s390: pgtable: use pfn_swap_entry_to_folio() in ptep_zap_swap_entry() Kefeng Wang
2024-01-11 11:12 ` [PATCH v2 3/8] mm: huge_memory: use pfn_swap_entry_to_folio() in __split_huge_pmd_locked() Kefeng Wang
2024-01-11 11:12 ` [PATCH v2 4/8] mm: huge_memory: use pfn_swap_entry_to_folio() in zap_huge_pmd() Kefeng Wang
2024-01-11 11:12 ` [PATCH v2 5/8] mm: memory: use pfn_swap_entry_to_folio() in copy_nonpresent_pte() Kefeng Wang
2024-01-11 11:12 ` [PATCH v2 6/8] mm: memory: convert to should_zap_folio() Kefeng Wang
2024-01-11 11:12 ` [PATCH v2 7/8] mm: convert mm_counter() to take a folio Kefeng Wang
2024-01-11 11:12 ` [PATCH v2 8/8] mm: convert mm_counter_file() " Kefeng Wang

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