2023-10-10 06:47:35

by Kefeng Wang

[permalink] [raw]
Subject: [PATCH -next 0/7] mm: convert page cpupid functions to folios

The cpupid(or access time) used by numa balancing is stored in flags
or _last_cpupid(if LAST_CPUPID_NOT_IN_PAGE_FLAGS) of page, this is to
convert page cpupid to folio cpupid, a new _last_cpupid is added into
folio, which make us to use folio->_last_cpupid directly, and the
page_cpupid_xchg_last(), xchg_page_access_time() and page_cpupid_last()
are converted to folio ones.

v1:
- drop inappropriate page_cpupid_reset_last convertion from RFC
- rebased on next-20231009

Kefeng Wang (7):
mm_types: add _last_cpupid into folio
mm: mprotect: use a folio in change_pte_range()
mm: huge_memory: use a folio in change_huge_pmd()
mm: convert xchg_page_access_time to xchg_folio_access_time()
mm: convert page_cpupid_last() to folio_cpupid_last()
mm: make wp_page_reuse() and finish_mkwrite_fault() to take a folio
mm: convert page_cpupid_xchg_last() to folio_cpupid_xchg_last()

include/linux/mm.h | 30 +++++++++++++++---------------
include/linux/mm_types.h | 13 +++++++++----
kernel/sched/fair.c | 4 ++--
mm/huge_memory.c | 17 +++++++++--------
mm/memory.c | 39 +++++++++++++++++++++------------------
mm/migrate.c | 4 ++--
mm/mmzone.c | 6 +++---
mm/mprotect.c | 16 +++++++++-------
8 files changed, 70 insertions(+), 59 deletions(-)

--
2.27.0


2023-10-10 06:47:48

by Kefeng Wang

[permalink] [raw]
Subject: [PATCH -next 7/7] mm: convert page_cpupid_xchg_last() to folio_cpupid_xchg_last()

Make page_cpupid_xchg_last() to take a folio, and rename it to
olio_cpupid_xchg_last() since all callers with a folio.

Signed-off-by: Kefeng Wang <[email protected]>
---
include/linux/mm.h | 14 +++++++-------
kernel/sched/fair.c | 2 +-
mm/huge_memory.c | 2 +-
mm/memory.c | 2 +-
mm/migrate.c | 4 ++--
mm/mmzone.c | 6 +++---
6 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 3d59455626fa..e761642e1c00 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1683,9 +1683,9 @@ static inline bool __cpupid_match_pid(pid_t task_pid, int cpupid)

#define cpupid_match_pid(task, cpupid) __cpupid_match_pid(task->pid, cpupid)
#ifdef LAST_CPUPID_NOT_IN_PAGE_FLAGS
-static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
+static inline int folio_cpupid_xchg_last(struct folio *folio, int cpupid)
{
- return xchg(&page->_last_cpupid, cpupid & LAST_CPUPID_MASK);
+ return xchg(&folio->_last_cpupid, cpupid & LAST_CPUPID_MASK);
}

static inline int folio_cpupid_last(struct folio *folio)
@@ -1702,7 +1702,7 @@ static inline int folio_cpupid_last(struct folio *folio)
return (folio->flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;
}

-extern int page_cpupid_xchg_last(struct page *page, int cpupid);
+extern int folio_cpupid_xchg_last(struct folio *folio, int cpupid);

static inline void page_cpupid_reset_last(struct page *page)
{
@@ -1714,8 +1714,8 @@ static inline int xchg_folio_access_time(struct folio *folio, int time)
{
int last_time;

- last_time = page_cpupid_xchg_last(&folio->page,
- time >> PAGE_ACCESS_TIME_BUCKETS);
+ last_time = folio_cpupid_xchg_last(folio,
+ time >> PAGE_ACCESS_TIME_BUCKETS);
return last_time << PAGE_ACCESS_TIME_BUCKETS;
}

@@ -1729,9 +1729,9 @@ static inline void vma_set_access_pid_bit(struct vm_area_struct *vma)
}
}
#else /* !CONFIG_NUMA_BALANCING */
-static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
+static inline int folio_cpupid_xchg_last(struct folio *folio, int cpupid)
{
- return page_to_nid(page); /* XXX */
+ return folio_nid(folio); /* XXX */
}

static inline int xchg_folio_access_time(struct folio *folio, int time)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 50b9f63099fb..5d4c7cedc6d1 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1818,7 +1818,7 @@ bool should_numa_migrate_memory(struct task_struct *p, struct folio *folio,
}

this_cpupid = cpu_pid_to_cpupid(dst_cpu, current->pid);
- last_cpupid = page_cpupid_xchg_last(&folio->page, this_cpupid);
+ last_cpupid = folio_cpupid_xchg_last(folio, this_cpupid);

if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) &&
!node_is_toptier(src_nid) && !cpupid_valid(last_cpupid))
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 3b37367eaeff..2163b1d0dad5 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2515,7 +2515,7 @@ static void __split_huge_page_tail(struct folio *folio, int tail,
if (page_is_idle(head))
set_page_idle(page_tail);

- page_cpupid_xchg_last(page_tail, folio_cpupid_last(folio));
+ folio_cpupid_xchg_last(new_folio, folio_cpupid_last(folio));

/*
* always add to the tail because some iterators expect new
diff --git a/mm/memory.c b/mm/memory.c
index 1a1a6a6ccd58..9f3b359b46db 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3034,7 +3034,7 @@ static inline void wp_page_reuse(struct vm_fault *vmf, struct folio *folio)
* information potentially belongs to a now completely
* unrelated process.
*/
- page_cpupid_xchg_last(vmf->page, (1 << LAST_CPUPID_SHIFT) - 1);
+ folio_cpupid_xchg_last(folio, (1 << LAST_CPUPID_SHIFT) - 1);
}

flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte));
diff --git a/mm/migrate.c b/mm/migrate.c
index c602bf6dec97..5642e9572d80 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -588,7 +588,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
* Copy NUMA information to the new page, to prevent over-eager
* future migrations of this same page.
*/
- cpupid = page_cpupid_xchg_last(&folio->page, -1);
+ cpupid = folio_cpupid_xchg_last(folio, -1);
/*
* For memory tiering mode, when migrate between slow and fast
* memory node, reset cpupid, because that is used to record
@@ -601,7 +601,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
if (f_toptier != t_toptier)
cpupid = -1;
}
- page_cpupid_xchg_last(&newfolio->page, cpupid);
+ folio_cpupid_xchg_last(newfolio, cpupid);

folio_migrate_ksm(newfolio, folio);
/*
diff --git a/mm/mmzone.c b/mm/mmzone.c
index 68e1511be12d..cd473f82b647 100644
--- a/mm/mmzone.c
+++ b/mm/mmzone.c
@@ -93,19 +93,19 @@ void lruvec_init(struct lruvec *lruvec)
}

#if defined(CONFIG_NUMA_BALANCING) && !defined(LAST_CPUPID_NOT_IN_PAGE_FLAGS)
-int page_cpupid_xchg_last(struct page *page, int cpupid)
+int folio_cpupid_xchg_last(struct folio *folio, int cpupid)
{
unsigned long old_flags, flags;
int last_cpupid;

- old_flags = READ_ONCE(page->flags);
+ old_flags = READ_ONCE(folio->flags);
do {
flags = old_flags;
last_cpupid = (flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;

flags &= ~(LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT);
flags |= (cpupid & LAST_CPUPID_MASK) << LAST_CPUPID_PGSHIFT;
- } while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags)));
+ } while (unlikely(!try_cmpxchg(&folio->flags, &old_flags, flags)));

return last_cpupid;
}
--
2.27.0

2023-10-10 06:47:57

by Kefeng Wang

[permalink] [raw]
Subject: [PATCH -next 5/7] mm: convert page_cpupid_last() to folio_cpupid_last()

Make page_cpupid_last() to take a folio, and rename it to
folio_cpupid_last() since all callers with a folio.

Signed-off-by: Kefeng Wang <[email protected]>
---
include/linux/mm.h | 12 ++++++------
mm/huge_memory.c | 4 ++--
mm/memory.c | 2 +-
3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 13ca63efacf7..e0bd8abae6c6 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1689,18 +1689,18 @@ static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
return xchg(&page->_last_cpupid, cpupid & LAST_CPUPID_MASK);
}

-static inline int page_cpupid_last(struct page *page)
+static inline int folio_cpupid_last(struct folio *folio)
{
- return page->_last_cpupid;
+ return folio->_last_cpupid;
}
static inline void page_cpupid_reset_last(struct page *page)
{
page->_last_cpupid = -1 & LAST_CPUPID_MASK;
}
#else
-static inline int page_cpupid_last(struct page *page)
+static inline int folio_cpupid_last(struct folio *folio)
{
- return (page->flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;
+ return (folio->flags >> LAST_CPUPID_PGSHIFT) & LAST_CPUPID_MASK;
}

extern int page_cpupid_xchg_last(struct page *page, int cpupid);
@@ -1740,9 +1740,9 @@ static inline int xchg_folio_access_time(struct folio *folio, int time)
return 0;
}

-static inline int page_cpupid_last(struct page *page)
+static inline int folio_cpupid_last(struct folio *folio)
{
- return page_to_nid(page); /* XXX */
+ return folio_nid(folio); /* XXX */
}

static inline int cpupid_to_nid(int cpupid)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index e85238ac1d5c..3b37367eaeff 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1562,7 +1562,7 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
* to record page access time. So use default value.
*/
if (node_is_toptier(nid))
- last_cpupid = page_cpupid_last(&folio->page);
+ last_cpupid = folio_cpupid_last(folio);
target_nid = numa_migrate_prep(folio, vma, haddr, nid, &flags);
if (target_nid == NUMA_NO_NODE) {
folio_put(folio);
@@ -2515,7 +2515,7 @@ static void __split_huge_page_tail(struct folio *folio, int tail,
if (page_is_idle(head))
set_page_idle(page_tail);

- page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
+ page_cpupid_xchg_last(page_tail, folio_cpupid_last(folio));

/*
* always add to the tail because some iterators expect new
diff --git a/mm/memory.c b/mm/memory.c
index c4b4aa4c1180..7566955d88e3 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4861,7 +4861,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf)
!node_is_toptier(nid))
last_cpupid = (-1 & LAST_CPUPID_MASK);
else
- last_cpupid = page_cpupid_last(&folio->page);
+ last_cpupid = folio_cpupid_last(folio);
target_nid = numa_migrate_prep(folio, vma, vmf->address, nid, &flags);
if (target_nid == NUMA_NO_NODE) {
folio_put(folio);
--
2.27.0

2023-10-10 06:48:01

by Kefeng Wang

[permalink] [raw]
Subject: [PATCH -next 4/7] mm: convert xchg_page_access_time to xchg_folio_access_time()

Make xchg_page_access_time to take a folio, and rename it to
xchg_folio_access_time() since all callers with a folio.

Signed-off-by: Kefeng Wang <[email protected]>
---
include/linux/mm.h | 7 ++++---
kernel/sched/fair.c | 2 +-
mm/huge_memory.c | 4 ++--
mm/mprotect.c | 2 +-
4 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index a10b8774cc6f..13ca63efacf7 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1711,11 +1711,12 @@ static inline void page_cpupid_reset_last(struct page *page)
}
#endif /* LAST_CPUPID_NOT_IN_PAGE_FLAGS */

-static inline int xchg_page_access_time(struct page *page, int time)
+static inline int xchg_folio_access_time(struct folio *folio, int time)
{
int last_time;

- last_time = page_cpupid_xchg_last(page, time >> PAGE_ACCESS_TIME_BUCKETS);
+ last_time = page_cpupid_xchg_last(&folio->page,
+ time >> PAGE_ACCESS_TIME_BUCKETS);
return last_time << PAGE_ACCESS_TIME_BUCKETS;
}

@@ -1734,7 +1735,7 @@ static inline int page_cpupid_xchg_last(struct page *page, int cpupid)
return page_to_nid(page); /* XXX */
}

-static inline int xchg_page_access_time(struct page *page, int time)
+static inline int xchg_folio_access_time(struct folio *folio, int time)
{
return 0;
}
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 682067c545d1..50b9f63099fb 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1722,7 +1722,7 @@ static int numa_hint_fault_latency(struct folio *folio)
int last_time, time;

time = jiffies_to_msecs(jiffies);
- last_time = xchg_page_access_time(&folio->page, time);
+ last_time = xchg_folio_access_time(folio, time);

return (time - last_time) & PAGE_ACCESS_TIME_MASK;
}
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 344c8db904e1..e85238ac1d5c 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1912,8 +1912,8 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,

if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
!toptier)
- xchg_page_access_time(&folio->page,
- jiffies_to_msecs(jiffies));
+ xchg_folio_access_time(folio,
+ jiffies_to_msecs(jiffies));
}
/*
* In case prot_numa, we are under mmap_read_lock(mm). It's critical
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 459daa987131..1c556651888a 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -159,7 +159,7 @@ static long change_pte_range(struct mmu_gather *tlb,
continue;
if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
!toptier)
- xchg_page_access_time(&folio->page,
+ xchg_folio_access_time(folio,
jiffies_to_msecs(jiffies));
}

--
2.27.0

2023-10-10 12:27:38

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH -next 4/7] mm: convert xchg_page_access_time to xchg_folio_access_time()

On Tue, Oct 10, 2023 at 02:45:41PM +0800, Kefeng Wang wrote:
> Make xchg_page_access_time to take a folio, and rename it to
> xchg_folio_access_time() since all callers with a folio.

You're doing this the hard way, which makes life hard for the reviewrs.

patch 1. Introduce folio->_last_cpupid
patch 2: Add

static inline int folio_xchg_access_time(struct folio *folio, int time)
{
return xchg_page_access_time(&folio->page, time);
}

patch 3-n: Convert callers
Patch n+1: Remove xchg_page_access_time(), folding it into
folio_xchg_access_time().

Similarly for page_cpupid_xchg_last / folio_cpupid_xchg_last().
(why is this not called folio_xchg_last_cpupid?)

2023-10-11 03:03:37

by Kefeng Wang

[permalink] [raw]
Subject: Re: [PATCH -next 4/7] mm: convert xchg_page_access_time to xchg_folio_access_time()



On 2023/10/10 20:27, Matthew Wilcox wrote:
> On Tue, Oct 10, 2023 at 02:45:41PM +0800, Kefeng Wang wrote:
>> Make xchg_page_access_time to take a folio, and rename it to
>> xchg_folio_access_time() since all callers with a folio.
>
> You're doing this the hard way, which makes life hard for the reviewrs.
>
> patch 1. Introduce folio->_last_cpupid
> patch 2: Add
>
> static inline int folio_xchg_access_time(struct folio *folio, int time)
> {
> return xchg_page_access_time(&folio->page, time);
> }
>
> patch 3-n: Convert callers
> Patch n+1: Remove xchg_page_access_time(), folding it into
> folio_xchg_access_time().

Ok, I will follow this way, thanks for your advise.
>
> Similarly for page_cpupid_xchg_last / folio_cpupid_xchg_last().
> (why is this not called folio_xchg_last_cpupid?)

Fine with me, will update.

Thanks.

>
>