mm: slowtier page promotion based on PTE A bit
Subsystems (9)
v3 cover mm: slowtier page promotion based on PTE A bit
The current series has additional enhancements and comments' incorporation on top of RFC V2.
This is an additional source of hot page generator to NUMAB, IBS [4], KMGLRUD [5].
Introduction:
=============
In the current hot page promotion, all the activities including the process address space scanning, NUMA hint fault handling and page migration is performed in the process context. i.e., scanning overhead is borne by applications.
This RFC V2 patch series does slow-tier page promotion by using PTE Accessed bit scanning. Scanning is done by a global kernel thread which routinely scans all the processes' address spaces and checks for accesses by reading the PTE A bit.
A separate migration thread migrates/promotes the pages to the top-tier node based on a simple heuristic that uses top-tier scan/access information of the mm.
Additionally based on the feedback, a prctl knob with a scalar value is provided to control per task scanning.
Changes Since RFC V2:
===================
- Enhanced logic to migrate on second access.
- Using prctl scalar value to further tune the scanning efficiency.
- Using of PFN instead of folio to record hot pages for easy integration
with kpromoted/kmigrated [4].
- Rebasing on top of fork/exec changes in v6.16.
- Revisiting mm_walk logic and folio validation based on Harry's comments.
- Feedback from migration system to slowdown scanning when more migration failures happen.
- Masami's comment on trace patch.
- Bug fix to overnight idle system crash due to incorrect kmemcache usage.
- Enhanced target node finding logic to further obtain fallback nodes to migrate.
(TBD: This needs followup patch that actually does migration to fallback target nodes)
Changes since RFC V1:
=====================
- Addressing the review comments by Jonathan (Thank you for your closer reviews).
- Per mm migration list with separate lock to resolve race conditions/softlockups reported by Davidlohr.
- Add one more filter before migration for LRU_GEN case to check whether folio is still hot.
- Rename kmmscand ==> kscand kmmmigrated ==> kmigrated (hopefully this gets merged into Bharat's upcoming migration thread)
Changes since RFC V0:
======================
- A separate migration thread is used for migration, thus alleviating need for multi-threaded scanning (at least as per tracing).
- A simple heuristic for target node calculation is added.
- prctl (David R) interface with scalar value is added to control per task scanning.
- Steve's comment on tracing incorporated.
- Davidlohr's reported bugfix.
- Initial scan delay similar to NUMAB1 mode added.
- Got rid of migration lock during mm_walk.
A note on per mm migration list using mm_slot:
=============================================
Using per mm migration list (mm_slot) has helped to reduce contention
and thus easing mm teardown during process exit.
It also helps to tie PFN/folio with mm to make heuristics work better
and further it would help to throttle migration per mm (OR process) (TBD).
A note on PTE A bit scanning:
============================
Major positive: Current patchset is able to cover all the process address
space scanning effectively with simple algorithms to tune scan_size and scan_period.
Thanks to Jonathan, Davidlohr, David, Harry, Masami Steve for review feedback on RFCs.
Future plans:
================
Evaluate how integration with hotness monitoring subsystem works, OR as a standalone integration with kmigrated API* of [4]
Results:
=======
Benchmark Cbench (by Bharata) to evaluate performance promotion in slowtier system.
Benchmark allocates memory on both regular NUMA node and slowtier node, then does continuous access.
Goal: Finishing fixed numaber of access in less time
SUT: Genoa+ EPYC system
base 6.16 NUMAB2 (because this has the best perf)
patched 6.16 + current series
Time taken in sec (lower is better)
base patched 8GB 228 206 32GB 547 534 128GB 1100 920
Links:
[1] RFC V0: https://lore.kernel.org/all/[email protected]/
[2] RFC V1: https://lore.kernel.org/linux-mm/[email protected]/
[3] RFC V2: https://lore.kernel.org/linux-mm/[email protected]/
[4] Hotpage detection and promotion: https://lore.kernel.org/linux-mm/[email protected]/T/#t
[5] MGLRU: https://lkml.org/lkml/2025/3/24/1458
Patch organization:
patch 1-5 initial skeleton for scanning and migration patch 6: migration
patch 7-9: scanning optimizations
patch 10: target_node heuristic
patch 11: Migration failure feedback
patch 12-14: sysfs, vmstat and tracing
patch 15-16: prctl implementation and enhancements to scanning.
patch17: Fallback target node finding
Raghavendra K T (17):
mm: Add kscand kthread for PTE A bit scan
mm: Maintain mm_struct list in the system
mm: Scan the mm and create a migration list mm/kscand: Add only hot pages to migration list
mm: Create a separate kthread for migration mm/migration: migrate accessed folios to toptier node
mm: Add throttling of mm scanning using scan_period
mm: Add throttling of mm scanning using scan_size
mm: Add initial scan delay
mm: Add a heuristic to calculate target node mm/kscand: Implement migration failure feedback
sysfs: Add sysfs support to tune scanning mm/vmstat: Add vmstat counters
trace/kscand: Add tracing of scanning and migration
prctl: Introduce new prctl to control scanning
prctl: Fine tune scan_period with prctl scale param
mm: Create a list of fallback target nodes
Documentation/filesystems/proc.rst | 2 + fs/proc/task_mmu.c | 4 + include/linux/kscand.h | 30 + include/linux/migrate.h | 2 + include/linux/mm.h | 13 + include/linux/mm_types.h | 7 + include/linux/vm_event_item.h | 12 + include/trace/events/kmem.h | 99 ++ include/uapi/linux/prctl.h | 7 + kernel/fork.c | 6 + kernel/sys.c | 25 + mm/Kconfig | 8 + mm/Makefile | 1 + mm/internal.h | 1 + mm/kscand.c | 1754 ++++++++++++++++++++++++++++ mm/migrate.c | 2 +- mm/mmap.c | 2 + mm/vma_exec.c | 3 + mm/vmstat.c | 12 + 19 files changed, 1989 insertions(+), 1 deletion(-) create mode 100644 include/linux/kscand.h create mode 100644 mm/kscand.c
base-commit: 038d61fd642278bab63ee8ef722c50d10ab01e8f
Signature · 1 line
2.34.1
RFC PATCH v3 1/17 mm: Add kscand kthread for PTE A bit scan
Also add a config option for the same.
High level design:
While (1):
Scan the slowtier pages belonging to VMAs of a task. Add to migation list.
A separate thread:
Migrate scanned pages to a toptier node based on heuristics.
The overall code is influenced by khugepaged design.
--- mm/Kconfig | 8 +++ mm/Makefile | 1 + mm/kscand.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 mm/kscand.c
mm/Kconfig +8 -0
@@ -750,6 +750,14 @@ config KSM until a program has madvised that an area is MADV_MERGEABLE, and root has set /sys/kernel/mm/ksm/run to 1 (if CONFIG_SYSFS is set). +config KSCAND + bool "Enable PTE A bit scanning and Migration" + depends on NUMA_BALANCING + help + Enable PTE A bit scanning of page. The option creates a separate + kthread for scanning and migration. Accessed slow-tier pages are + migrated to a regular NUMA node to reduce hot page access latency. + config DEFAULT_MMAP_MIN_ADDR int "Low address space to protect from user allocation" depends on MMU
mm/Makefile +1 -0
@@ -97,6 +97,7 @@ obj-$(CONFIG_FAIL_PAGE_ALLOC) += fail_page_alloc.o obj-$(CONFIG_MEMTEST) += memtest.o obj-$(CONFIG_MIGRATION) += migrate.o obj-$(CONFIG_NUMA) += memory-tiers.o +obj-$(CONFIG_KSCAND) += kscand.o obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o obj-$(CONFIG_PAGE_COUNTER) += page_counter.o
mm/kscand.c new file +163 -0
163 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 01/17] mm: Add kscand kthread for PTE A bit scan
Quoted 8 lines · On Thu, 14 Aug 2025 15:32:51 +0000 Raghavendra K T <[email protected]>…
Also add a config option for the same.
High level design:
While (1):
Scan the slowtier pages belonging to VMAs of a task. Add to migation list.
Quoted 6 lines
A separate thread:
Migrate scanned pages to a toptier node based on heuristics.The overall code is influenced by khugepaged design.
Signed-off-by Raghavendra K T <[email protected]>
Hi Raghavendra,
A few comments inline.
Thanks,
Jonathan
mm/kscand.c line 2
Quoted 8 lines
mm/kscand.c new file +2
-0@@ -0,0 +1,163 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/mm.h>
Good to pick an order for includes. Various options, my personal
preference is alphabetical with groups of linux/* asm/* and local headers.
Quoted 13 lines
mm/kscand.c new file +13
-0+#include <linux/mm_types.h> +#include <linux/sched.h> +#include <linux/sched/mm.h> +#include <linux/mmu_notifier.h> +#include <linux/swap.h> +#include <linux/mm_inline.h> +#include <linux/kthread.h> +#include <linux/string.h> +#include <linux/delay.h> +#include <linux/cleanup.h> + +#include <asm/pgalloc.h> +#include "internal.h"
...
Quoted 4 lines
mm/kscand.c new file +4
-0+static void kscand_wait_work (void ) +{ +const unsigned long scan_sleep_jiffies = +msecs_to_jiffies (kscand_scan_sleep_ms);
mm/kscand.c line 71
Quoted 10 lines
mm/kscand.c new file +10
-0+ +if (!scan_sleep_jiffies) +return ; + + kscand_sleep_expire = jiffies + scan_sleep_jiffies; + +/* Allows kthread to pause scanning */ +wait_event_timeout (kscand_wait,kscand_should_wakeup (), + scan_sleep_jiffies); +}
Trivial but more consistent I think to have a blank line here.
mm/kscand.c line 76
Quoted 5 lines
mm/kscand.c new file +5
-0+static void kscand_do_scan (void ) +{ +unsigned long iter =0 , mms_to_scan; + + mms_to_scan =READ_ONCE (kscand_mms_to_scan);
Slightly nicer perhaps as
unsigned long iter = 0; unsigned long mms_to_scan = READ_ONCE(kscand_mms_to_scan);
Quoted 20 lines
mm/kscand.c new file +19
-0+ +while (true) { +if (unlikely (kthread_should_stop ()) || + !READ_ONCE (kscand_scan_enabled)) +break ; + +if (kscand_has_work ()) +msleep (100 ); + + iter++; + +if (iter >= mms_to_scan) +break ; +cond_resched (); + } +} + + +static int start_kscand (void )
mm/kscand.c line 122
Quoted 10 lines
mm/kscand.c new file +10
-0+{ +struct task_struct *kthread; + +guard (mutex)(&kscand_mutex); + +if (kscand_thread) +return 0 ; + + kthread =kthread_run (kscand,NULL ,"kscand" ); +if (IS_ERR (kscand_thread)) {
That looks like the wrong variable to check.
mm/kscand.c line 130
Quoted 8 lines
mm/kscand.c new file +8
-0+pr_err ("kscand: kthread_run(kscand) failed\n" ); +return PTR_ERR (kthread); + } + + kscand_thread = kthread; +pr_info ("kscand: Successfully started kscand" ); + +if (!list_empty (&kscand_scan.mm_head))
You have kscand_has_work() for this.
Quoted 14 lines
mm/kscand.c new file +14
-0+wake_up_interruptible (&kscand_wait); + +return 0 ; +} + +static int stop_kscand (void ) +{ +guard (mutex)(&kscand_mutex); + +if (kscand_thread) { +kthread_stop (kscand_thread); + kscand_thread =NULL ; + } +
mm/kscand.c line 154
Quoted 10 lines
mm/kscand.c new file +10
-0+return 0 ; +} + +static int __init kscand_init (void ) +{ +int err; + + err =start_kscand (); +if (err) + goto err_kscand;
This breaks the nice principle of side effect free calls on error. If start_kscand() has failed, I'd not really expect to have
have stop explicitly. I'm not sure you actually do need to do so.
Quoted 9 lines
mm/kscand.c new file +9
-0+ +return 0 ; + +err_kscand: +stop_kscand (); + +return err; +} +subsys_initcall (kscand_init);
RFC PATCH v3 2/17 mm: Maintain mm_struct list in the system
In reply to Raghavendra K TThe list is used to iterate over all the mm and do PTE A bit scanning. mm_slot infrastructure is reused to aid insert and lookup of mm_struct.
--- include/linux/kscand.h | 30 +++++++++++++++ kernel/fork.c | 2 + mm/internal.h | 1 + mm/kscand.c | 86 ++++++++++++++++++++++++++++++++++++++++++ mm/mmap.c | 2 + mm/vma_exec.c | 3 ++ 6 files changed, 124 insertions(+) create mode 100644 include/linux/kscand.h
include/linux/kscand.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_KSCAND_H_ +#define _LINUX_KSCAND_H_ + +#ifdef CONFIG_KSCAND +extern void __kscand_enter (struct mm_struct *mm); +extern void __kscand_exit (struct mm_struct *mm); + +static inline void kscand_execve (struct mm_struct *mm) +{ +__kscand_enter (mm); +} + +static inline void kscand_fork (struct mm_struct *mm,struct mm_struct *oldmm) +{ +__kscand_enter (mm); +} + +static inline void kscand_exit (struct mm_struct *mm) +{ +__kscand_exit (mm); +} +#else /* !CONFIG_KSCAND */ +static inline void __kscand_enter (struct mm_struct *mm) {} +static inline void __kscand_exit (struct mm_struct *mm) {} +static inline void kscand_execve (struct mm_struct *mm) {} +static inline void kscand_fork (struct mm_struct *mm,struct mm_struct *oldmm) {} +static inline void kscand_exit (struct mm_struct *mm) {} +#endif +#endif /* _LINUX_KSCAND_H_ */
kernel/fork.c +2 -0
@@ -85,6 +85,7 @@#include <linux/user-return-notifier.h> #include <linux/oom.h> #include <linux/khugepaged.h> +#include <linux/kscand.h> #include <linux/signalfd.h> #include <linux/uprobes.h> #include <linux/aio.h>
@@ -1116,6 +1117,7 @@ static inline void __mmput(struct mm_struct *mm)uprobe_clear_state (mm);exit_aio (mm); +kscand_exit (mm);ksm_exit (mm);khugepaged_exit (mm);/* must run before exit_mmap */ exit_mmap (mm);
mm/internal.h +1 -0
@@ -8,6 +8,7 @@#define __MM_INTERNAL_H#include <linux/fs.h> +#include <linux/kscand.h> #include <linux/khugepaged.h> #include <linux/mm.h> #include <linux/mm_inline.h>
mm/kscand.c +86 -0
133 lines not on this page · raw message
mm/mmap.c +2 -0
@@ -37,6 +37,7 @@#include <linux/perf_event.h> #include <linux/audit.h> #include <linux/khugepaged.h> +#include <linux/kscand.h> #include <linux/uprobes.h> #include <linux/notifier.h> #include <linux/memory.h>
@@ -1849,6 +1850,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)if (!retval) {mt_set_in_rcu (vmi.mas.tree);ksm_fork (mm, oldmm); +kscand_fork (mm, oldmm);khugepaged_fork (mm, oldmm); }else {
mm/vma_exec.c +3 -0
@@ -128,6 +128,8 @@ int create_init_stack_vma(struct mm_struct *mm, struct vm_area_struct **vmap,if (err) goto err_ksm; +kscand_execve (mm); +/* * Place the stack at the largest stack address the architecture * supports. Later, we'll move this to an appropriate place. We don't
@@ -151,6 +153,7 @@ int create_init_stack_vma(struct mm_struct *mm, struct vm_area_struct **vmap,return 0 ; err: +kscand_exit (mm);ksm_exit (mm); err_ksm:mmap_write_unlock (mm);
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 02/17] mm: Maintain mm_struct list in the system
Quoted 4 lines · On Thu, 14 Aug 2025 15:32:52 +0000 Raghavendra K T <[email protected]>…
The list is used to iterate over all the mm and do PTE A bit scanning.
mm_slotinfrastructure is reused to aid insert and lookup ofmm_struct.
This is part of the tags block. Some tools moan if you have blank lines in that. Alternatively push it below the --- and it won't end up in the git commit but tooling will still add the +CC.
Quoted 3 lines
Suggested-by Bharata B Rao <[email protected]>Signed-off-by Raghavendra K T <[email protected]>
A few minor comments on formatting.
Quoted 6 lines
mm/kscand.c +1
-0+void __kscand_enter (struct mm_struct *mm)
mm/kscand.c static int kscand(void *none) · line 147
Quoted 10 lines
mm/kscand.c +10
-0+{ +struct kscand_mm_slot *kscand_slot; +struct mm_slot *slot; +int wakeup; + +/* __kscand_exit() must not run from under us */ +VM_BUG_ON_MM (kscand_test_exit (mm), mm); + + kscand_slot =mm_slot_alloc (kscand_slot_cache); +
Similar to below. I'd keep call and error check more closely coupled visually by dropping this blank line.
mm/kscand.c static int kscand(void *none) · line 156
Quoted 9 lines
mm/kscand.c +9
-0+if (!kscand_slot) +return ; + + slot = &kscand_slot->slot; + +spin_lock (&kscand_mm_lock); +mm_slot_insert (kscand_slots_hash, mm, slot); + + wakeup =list_empty (&kscand_scan.mm_head);
Looks familiar.
wakeup = kscand_has_work()
Or maybe just get rid of that helper and check it explicitly like this.
Quoted 13 lines
mm/kscand.c +8
-0+list_add_tail (&slot->mm_node, &kscand_scan.mm_head); +spin_unlock (&kscand_mm_lock); + +mmgrab (mm); +if (wakeup) +wake_up_interruptible (&kscand_wait); +} +static int start_kscand (void ) {struct task_struct *kthread;
mm/kscand.c static int __init kscand_init(void) · line 232
Quoted 5 lines
mm/kscand.c +2
-0@@ -149,6 +228,12 @@ static int __init kscand_init(void) {int err; + kscand_slot_cache =KMEM_CACHE (kscand_mm_slot,0 ); +
I'd drop this blank line. Keep the call and the error check tightly coupled in one block of code.
Quoted 15 lines
mm/kscand.c +5
-0+if (!kscand_slot_cache) { +pr_err ("kscand: kmem_cache error" ); +return -ENOMEM ; + } err =start_kscand ();if (err) goto err_kscand;@@ -157,6 +242,7 @@ static int __init kscand_init(void) err_kscand:stop_kscand (); +kscand_destroy ();return err; }
RFC PATCH v3 3/17 mm: Scan the mm and create a migration list
In reply to Raghavendra K TSince we already have the list of mm_struct in the system, add a module to scan each mm that walks VMAs of each mm_struct and scan all the pages associated with that.
In the scan path: Check for the recently acccessed pages (PFNs) belonging
to slowtier nodes. Add all those to a list.
--- mm/kscand.c | 321 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 320 insertions(+), 1 deletion(-)
mm/kscand.c +320 -1
@@ -4,10 +4,18 @@#include <linux/sched.h> #include <linux/sched/mm.h> #include <linux/mmu_notifier.h> +#include <linux/rmap.h> +#include <linux/pagewalk.h> +#include <linux/page_ext.h> +#include <linux/page_idle.h> +#include <linux/page_table_check.h> +#include <linux/pagemap.h> #include <linux/swap.h> #include <linux/mm_inline.h> #include <linux/kthread.h> #include <linux/kscand.h> +#include <linux/memory-tiers.h> +#include <linux/mempolicy.h> #include <linux/string.h> #include <linux/delay.h> #include <linux/cleanup.h>
@@ -18,6 +26,11 @@static struct task_struct *kscand_thread__read_mostly ;static DEFINE_MUTEX (kscand_mutex); +/* +* Total VMA size to cover during scan. +*/ +#define KSCAND_SCAN_SIZE (1 * 1024 * 1024 * 1024UL) +static unsigned long kscand_scan_size__read_mostly =KSCAND_SCAN_SIZE ;/* How long to pause between two scan cycles */ static unsigned int kscand_scan_sleep_ms__read_mostly =20 ;
@@ -42,6 +55,8 @@ static struct kmem_cache *kscand_slot_cache __read_mostly;/* Per mm information collected to control VMA scanning */ struct kscand_mm_slot {struct mm_slot slot; +long address; +bool is_scanned; };/* Data structure to keep track of current mm under scan */
@@ -54,6 +69,29 @@ struct kscand_scan kscand_scan = { .mm_head = LIST_HEAD_INIT(kscand_scan.mm_head), }; +/* +* Data structure passed to control scanning and also collect +* per memory node information +*/ +struct kscand_scanctrl { +struct list_head scan_list; +unsigned long address; +}; + +struct kscand_scanctrl kscand_scanctrl; +/* Per folio information used for migration */ +struct kscand_migrate_info { +struct list_head migrate_node; +unsigned long pfn; +unsigned long address; +}; + +static bool kscand_eligible_srcnid (int nid) +{ +/* Only promotion case is considered */ +return !node_is_toptier (nid); +} +static inline int kscand_has_work (void ) {return !list_empty (&kscand_scan.mm_head);
@@ -84,11 +122,277 @@ static void kscand_wait_work(void) scan_sleep_jiffies); } +static inline bool is_valid_folio (struct folio *folio) +{ +if (!folio || !folio_mapped (folio) || !folio_raw_mapping (folio)) +return false; + +if (folio_test_unevictable (folio) ||folio_is_zone_device (folio) || +folio_maybe_mapped_shared (folio)) +return false; + +return true; +} + + +static bool folio_idle_clear_pte_refs_one (struct folio *folio, +struct vm_area_struct *vma, +unsigned long addr, +pte_t *ptep) +{ +bool referenced = false; +struct mm_struct *mm = vma->vm_mm; +pmd_t *pmd =pmd_off (mm, addr); + +if (ptep) { +if (ptep_clear_young_notify (vma, addr, ptep)) + referenced = true; + }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { +if (!pmd_present (*pmd)) +WARN_ON_ONCE (1 ); +if (pmdp_clear_young_notify (vma, addr, pmd)) + referenced = true; + }else { +WARN_ON_ONCE (1 ); + } + +if (referenced) { +folio_clear_idle (folio); +folio_set_young (folio); + } + +return true; +} + +static void page_idle_clear_pte_refs (struct page *page,pte_t *pte,struct mm_walk *walk) +{ +bool need_lock; +struct folio *folio =page_folio (page); +unsigned long address; + +if (!folio_mapped (folio) || !folio_raw_mapping (folio)) +return ; + + need_lock = !folio_test_anon (folio) ||folio_test_ksm (folio); +if (need_lock && !folio_trylock (folio)) +return ; + address =vma_address (walk->vma,page_pgoff (folio, page),compound_nr (page)); +VM_BUG_ON_VMA (address == -EFAULT , walk->vma); +folio_idle_clear_pte_refs_one (folio, walk->vma, address, pte); + +if (need_lock) +folio_unlock (folio); +} + +static int hot_vma_idle_pte_entry (pte_t *pte, +unsigned long addr, +unsigned long next, +struct mm_walk *walk) +{ +struct page *page; +struct folio *folio; +struct mm_struct *mm; +struct vm_area_struct *vma; +struct kscand_migrate_info *info; +struct kscand_scanctrl *scanctrl = walk->private; +int srcnid; + + scanctrl->address = addr; +pte_t pteval =ptep_get (pte); + +if (!pte_present (pteval)) +return 0 ; + +if (pte_none (pteval)) +return 0 ; + + vma = walk->vma; + mm = vma->vm_mm; + + page =pte_page (*pte); + + + folio =page_folio (page); +folio_get (folio); + +if (!is_valid_folio (folio)) { +folio_put (folio); +return 0 ; + } +folio_set_idle (folio); +page_idle_clear_pte_refs (page, pte, walk); + srcnid =folio_nid (folio); + + +if (!folio_test_lru (folio)) { +folio_put (folio); +return 0 ; + } + +if (!kscand_eligible_srcnid (srcnid)) { +folio_put (folio); +return 0 ; + } +if (!folio_test_idle (folio) && + (folio_test_young (folio) ||folio_test_referenced (folio))) { + +/* XXX: Leaking memory. TBD: consume info */ + + info =kzalloc (sizeof (struct kscand_migrate_info ),GFP_NOWAIT ); +if (info && scanctrl) { + info->pfn =folio_pfn (folio); + info->address = addr; +list_add_tail (&info->migrate_node, &scanctrl->scan_list); + } + } + +folio_put (folio); +return 0 ; +} + +static const struct mm_walk_ops hot_vma_set_idle_ops = { + .pte_entry = hot_vma_idle_pte_entry, + .walk_lock =PGWALK_RDLOCK , +}; + +static void kscand_walk_page_vma (struct vm_area_struct *vma,struct kscand_scanctrl *scanctrl) +{ +if (!vma_migratable (vma) || !vma_policy_mof (vma) || +is_vm_hugetlb_page (vma) || (vma->vm_flags &VM_MIXEDMAP )) { +return ; + } +if (!vma->vm_mm || + (vma->vm_file && (vma->vm_flags & (VM_READ |VM_WRITE )) == (VM_READ ))) +return ; + +if (!vma_is_accessible (vma)) +return ; + +walk_page_vma (vma, &hot_vma_set_idle_ops, scanctrl); +} +static inline int kscand_test_exit (struct mm_struct *mm) {return atomic_read (&mm->mm_users) ==0 ; } +static void kscand_collect_mm_slot (struct kscand_mm_slot *mm_slot) +{ +struct mm_slot *slot = &mm_slot->slot; +struct mm_struct *mm = slot->mm; + +lockdep_assert_held (&kscand_mm_lock); + +if (kscand_test_exit (mm)) { +hash_del (&slot->hash); +list_del (&slot->mm_node); + +mm_slot_free (kscand_slot_cache, mm_slot); +mmdrop (mm); + } +} + +static unsigned long kscand_scan_mm_slot (void ) +{ +bool next_mm = false; +bool update_mmslot_info = false; + +unsigned long vma_scanned_size =0 ; +unsigned long address; + +struct mm_slot *slot; +struct mm_struct *mm; +struct vm_area_struct *vma =NULL ; +struct kscand_mm_slot *mm_slot; + + +spin_lock (&kscand_mm_lock); + +if (kscand_scan.mm_slot) { + mm_slot = kscand_scan.mm_slot; + slot = &mm_slot->slot; + address = mm_slot->address; + }else { + slot =list_entry (kscand_scan.mm_head.next, + struct mm_slot, mm_node); + mm_slot = mm_slot_entry(slot, struct kscand_mm_slot, slot); + address = mm_slot->address; + kscand_scan.mm_slot = mm_slot; + } + + mm = slot->mm; + mm_slot->is_scanned = true; +spin_unlock (&kscand_mm_lock); + +if (unlikely (!mmap_read_trylock (mm))) + goto outerloop_mmap_lock; + +if (unlikely (kscand_test_exit (mm))) { + next_mm = true; + goto outerloop; + } + +VMA_ITERATOR (vmi, mm, address); + +for_each_vma (vmi, vma) { +kscand_walk_page_vma (vma, &kscand_scanctrl); + vma_scanned_size += vma->vm_end - vma->vm_start; + +if (vma_scanned_size >= kscand_scan_size) { + next_mm = true; +/* TBD: Add scanned folios to migration list */ +break ; + } + } + +if (!vma) + address =0 ; +else + address = kscand_scanctrl.address +PAGE_SIZE ; + + update_mmslot_info = true; + +if (update_mmslot_info) + mm_slot->address = address; + +outerloop: +/* exit_mmap will destroy ptes after this */ +mmap_read_unlock (mm); + +outerloop_mmap_lock: +spin_lock (&kscand_mm_lock); +WARN_ON (kscand_scan.mm_slot != mm_slot); + +/* +* Release the current mm_slot if this mm is about to die, or +* if we scanned all vmas of this mm. +*/ +if (unlikely (kscand_test_exit (mm)) || !vma || next_mm) { +/* +* Make sure that if mm_users is reaching zero while +* kscand runs here, kscand_exit will find +* mm_slot not pointing to the exiting mm. +*/ +if (slot->mm_node.next != &kscand_scan.mm_head) { + slot =list_entry (slot->mm_node.next, + struct mm_slot, mm_node); + kscand_scan.mm_slot = + mm_slot_entry(slot, struct kscand_mm_slot, slot); + + }else + kscand_scan.mm_slot =NULL ; + +if (kscand_test_exit (mm)) { +kscand_collect_mm_slot (mm_slot); + goto end; + } + } + mm_slot->is_scanned = false; +end: +spin_unlock (&kscand_mm_lock); +return 0 ; +} +static void kscand_do_scan (void ) {unsigned long iter =0 , mms_to_scan;
@@ -101,7 +405,7 @@ static void kscand_do_scan(void)break ;if (kscand_has_work ()) -msleep (100 ); +kscand_scan_mm_slot (); iter++;
@@ -148,6 +452,7 @@ void __kscand_enter(struct mm_struct *mm)if (!kscand_slot)return ; + kscand_slot->address =0 ; slot = &kscand_slot->slot;spin_lock (&kscand_mm_lock);
@@ -175,6 +480,12 @@ void __kscand_exit(struct mm_struct *mm)hash_del (&slot->hash);list_del (&slot->mm_node); free =1 ; + }else if (mm_slot && kscand_scan.mm_slot == mm_slot && !mm_slot->is_scanned) { +hash_del (&slot->hash); +list_del (&slot->mm_node); + free =1 ; +/* TBD: Set the actual next slot */ + kscand_scan.mm_slot =NULL ; }spin_unlock (&kscand_mm_lock);
@@ -224,6 +535,12 @@ static int stop_kscand(void)return 0 ; } +static inline void init_list (void ) +{ +INIT_LIST_HEAD (&kscand_scanctrl.scan_list); +init_waitqueue_head (&kscand_wait); +} +static int __init kscand_init (void ) {int err;
@@ -234,6 +551,8 @@ static int __init kscand_init(void)pr_err ("kscand: kmem_cache error" );return -ENOMEM ; } + +init_list (); err =start_kscand ();if (err) goto err_kscand;
Signature · 1 line
2.34.1
kernel test robot
build botRe: [RFC PATCH V3 03/17] mm: Scan the mm and create a migration list
kernel test robot noticed the following build warnings:
Hi Raghavendra,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on 038d61fd642278bab63ee8ef722c50d10ab01e8f]
url: https://github.com/intel-lab-lkp/linux/commits/Raghavendra-K-T/mm-Add-kscand-kthread-for-PTE-A-bit-scan/20250814-233829 base: 038d61fd642278bab63ee8ef722c50d10ab01e8f patch link: https://lore.kernel.org/r/20250814153307.1553061-4-raghavendra.kt%40amd.com patch subject: [RFC PATCH V3 03/17] mm: Scan the mm and create a migration list config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20250816/[email protected]/config) compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250816/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> mm/kscand.c:194:20: warning: variable 'mm' set but not used [-Wunused-but-set-variable]
194 | struct mm_struct *mm;
| ^1 warning generated.
vim +/mm +194 mm/kscand.c
186
187 static int hot_vma_idle_pte_entry(pte_t *pte,
188 unsigned long addr,
189 unsigned long next,
190 struct mm_walk *walk)
191 {
192 struct page *page;
193 struct folio *folio;
> 194 struct mm_struct *mm;
195 struct vm_area_struct *vma;
196 struct kscand_migrate_info *info;
197 struct kscand_scanctrl *scanctrl = walk->private;
198 int srcnid;
199
200 scanctrl->address = addr;
201 pte_t pteval = ptep_get(pte);
202
203 if (!pte_present(pteval))
204 return 0;
205
206 if (pte_none(pteval))
207 return 0;
208
209 vma = walk->vma;
210 mm = vma->vm_mm;
211
212 page = pte_page(*pte);
213
214
215 folio = page_folio(page);
216 folio_get(folio);
217
218 if (!is_valid_folio(folio)) {
219 folio_put(folio);
220 return 0;
221 }
222 folio_set_idle(folio);
223 page_idle_clear_pte_refs(page, pte, walk);
224 srcnid = folio_nid(folio);
225
226
227 if (!folio_test_lru(folio)) {
228 folio_put(folio);
229 return 0;
230 }
231
232 if (!kscand_eligible_srcnid(srcnid)) {
233 folio_put(folio);
234 return 0;
235 }
236 if (!folio_test_idle(folio) &&
237 (folio_test_young(folio) || folio_test_referenced(folio))) {
238
239 /* XXX: Leaking memory. TBD: consume info */
240
241 info = kzalloc(sizeof(struct kscand_migrate_info), GFP_NOWAIT);
242 if (info && scanctrl) {
243 info->pfn = folio_pfn(folio);
244 info->address = addr;
245 list_add_tail(&info->migrate_node, &scanctrl->scan_list);
246 }
247 }
248
249 folio_put(folio);
250 return 0;
251 }
252
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [RFC PATCH V3 03/17] mm: Scan the mm and create a migration list
[AMD Official Use Only - AMD Internal Distribution Only]
On 8/16/2025 1:11 AM, kernel test robot wrote:
> Hi Raghavendra,
>
> [This is a private test report for your RFC patch.]
> kernel test robot noticed the following build warnings:
>
> [auto build test WARNING on 038d61fd642278bab63ee8ef722c50d10ab01e8f]
>
> url: https://github.com/intel-lab-lkp/linux/commits/Raghavendra-K-T/mm-Add-kscand-kthread-for-PTE-A-bit-scan/20250814-233829
> base: 038d61fd642278bab63ee8ef722c50d10ab01e8f
> patch link: https://lore.kernel.org/r/20250814153307.1553061-4-raghavendra.kt%40amd.com
> patch subject: [RFC PATCH V3 03/17] mm: Scan the mm and create a migration list
> config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20250816/[email protected]/config)
> compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff) > reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250816/[email protected]/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot
> | Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
>
> All warnings (new ones prefixed by >>):
>
>>> mm/kscand.c:194:20: warning: variable 'mm' set but not used [-Wunused-but-set-variable]
> 194 | struct mm_struct *mm;
> | ^
> 1 warning generated.
>
>
Hello,
Thank you for the report.
will take care of this in next iteration.
Thanks and Regards
- Raghu
Re: [RFC PATCH V3 03/17] mm: Scan the mm and create a migration list
In reply to Raghavendra K TQuoted 5 lines · On Thu, 14 Aug 2025 15:32:53 +0000 Raghavendra K T <[email protected]>…
Since we already have the list of
mm_structin the system, add a module to scan each mm that walks VMAs of eachmm_structand scan all the pages associated with that.In the scan path: Check for the recently acccessed pages (PFNs) belonging
Bonus space at start of line.
Quoted 3 lines
to slowtier nodes. Add all those to a list.
Signed-off-by Raghavendra K T <[email protected]>
A few superficial comments. I'm out of time today to take a closer read though but thought I'd send these as might not get back to this for a while.
Quoted 8 lines
--- mm/kscand.c | 321 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 320 insertions(+), 1 deletion(-)mm/kscand.c +0
-0
mm/kscand.c struct kscand_scan kscand_scan = { · line 92
Quoted 6 lines
mm/kscand.c +5
-0+ +static bool kscand_eligible_srcnid (int nid) +{ +/* Only promotion case is considered */ +return !node_is_toptier (nid);
one space before ! not 2.
Quoted 11 lines
mm/kscand.c +4
-0+} +static inline int kscand_has_work (void ) {return !list_empty (&kscand_scan.mm_head);@@ -84,11 +122,277 @@ static void kscand_wait_work(void) scan_sleep_jiffies); } +static inline bool is_valid_folio (struct folio *folio) +{
mm/kscand.c static void kscand_wait_work(void) · line 136
Quoted 10 lines
mm/kscand.c +10
-0+if (!folio || !folio_mapped (folio) || !folio_raw_mapping (folio)) +return false; + +if (folio_test_unevictable (folio) ||folio_is_zone_device (folio) || +folio_maybe_mapped_shared (folio)) +return false; + +return true; +} +
One blank line only unless local convention matches this.
Quoted 9 lines
mm/kscand.c +8
-0+ +static bool folio_idle_clear_pte_refs_one (struct folio *folio, + +static int hot_vma_idle_pte_entry (pte_t *pte, +unsigned long addr, +unsigned long next, + struct mm_walk *walk) +{
mm/kscand.c static void kscand_wait_work(void) · line 201
Quoted 10 lines
mm/kscand.c +10
-0+struct page *page; +struct folio *folio; +struct mm_struct *mm; +struct vm_area_struct *vma; +struct kscand_migrate_info *info; +struct kscand_scanctrl *scanctrl = walk->private; +int srcnid; + + scanctrl->address = addr; +pte_t pteval =ptep_get (pte);
Mixing declarations and code is a bit messy.
I'd just declare pte_t pteval earlier.
mm/kscand.c static void kscand_wait_work(void) · line 213
Quoted 12 lines
mm/kscand.c +12
-0+ +if (!pte_present (pteval)) +return 0 ; + +if (pte_none (pteval)) +return 0 ; + + vma = walk->vma; + mm = vma->vm_mm; + + page =pte_page (*pte); +
One line only here.
mm/kscand.c static void kscand_wait_work(void) · line 226
Quoted 13 lines
mm/kscand.c +13
-0+ + folio =page_folio (page); +folio_get (folio); + +if (!is_valid_folio (folio)) { +folio_put (folio); +return 0 ; + } +folio_set_idle (folio); +page_idle_clear_pte_refs (page, pte, walk); + srcnid =folio_nid (folio); + +
one blank line.
mm/kscand.c static void kscand_wait_work(void) · line 227
Quoted 1 line
mm/kscand.c +1
-0+if (!folio_test_lru (folio)) {
Maybe a goto given common code in all exit paths around here.
mm/kscand.c static void kscand_wait_work(void) · line 237
Quoted 10 lines
mm/kscand.c +10
-0+folio_put (folio); +return 0 ; + } + +if (!kscand_eligible_srcnid (srcnid)) { +folio_put (folio); +return 0 ; + } +if (!folio_test_idle (folio) && + (folio_test_young (folio) ||folio_test_referenced (folio))) {
Odd looking wrapping. I'd align after ( i.e.
if (!folio_test_idle(folio) &&
(folio_test_young(folio) || folio_test_referenced(folio))) {Quoted 17 lines
mm/kscand.c +16
-0+ +/* XXX: Leaking memory. TBD: consume info */ + + info =kzalloc (sizeof (struct kscand_migrate_info ),GFP_NOWAIT ); +if (info && scanctrl) { + info->pfn =folio_pfn (folio); + info->address = addr; +list_add_tail (&info->migrate_node, &scanctrl->scan_list); + } + } + +folio_put (folio); +return 0 ; +} +static unsigned long kscand_scan_mm_slot (void ) +{
mm/kscand.c static void kscand_wait_work(void) · line 306
Quoted 10 lines
mm/kscand.c +10
-0+bool next_mm = false; +bool update_mmslot_info = false; + +unsigned long vma_scanned_size =0 ; +unsigned long address; + +struct mm_slot *slot; +struct mm_struct *mm; +struct vm_area_struct *vma =NULL ; +struct kscand_mm_slot *mm_slot;
Confusing to have a variable called mm_slot which isn't a struct mm_slot * and another variable that is.
mm/kscand.c static void kscand_wait_work(void) · line 219
Quoted 1 line
mm/kscand.c +1
-0+
One line enough.
mm/kscand.c static void kscand_wait_work(void) · line 317
Quoted 10 lines
mm/kscand.c +10
-0+ +spin_lock (&kscand_mm_lock); + +if (kscand_scan.mm_slot) { + mm_slot = kscand_scan.mm_slot; + slot = &mm_slot->slot; + address = mm_slot->address; + }else { + slot =list_entry (kscand_scan.mm_head.next, + struct mm_slot, mm_node);
Isn't that
slot = list_first_entry(&kscand_scan.mm_head, struct mm_slot, mm_node);
Makes little difference other than pointing out it is the first entry.
mm/kscand.c static void kscand_wait_work(void) · line 319
Quoted 2 lines
mm/kscand.c +2
-0+ mm_slot = mm_slot_entry(slot, struct kscand_mm_slot, slot); + address = mm_slot->address;
Could drop setting address out of the if/else given it's the same in both legs.
Alternatively treat this as a 'get the missing mm_slot' and do
if (!kscand_scan.mm_slot) {
struct mm_slot *next_slot =
list_first_entry(&kscand_scan.mm_head,
struct mm_slot, mm_node);
struct kscand_mm_slot *next_mm_slot =
mm_slot_entry(slot, struct kscand_mm_slot, slot);
kscand_scan.mm_slot = next_mm_slot;
}
mm_slot = kscand_scan.mm_slot;
slot = &mm_slot->slot;
address = mm_slot->address;mm/kscand.c static void kscand_wait_work(void) · line 330
Quoted 11 lines
mm/kscand.c +11
-0+ kscand_scan.mm_slot = mm_slot; + } + + mm = slot->mm; + mm_slot->is_scanned = true; +spin_unlock (&kscand_mm_lock); + +if (unlikely (!mmap_read_trylock (mm))) + goto outerloop_mmap_lock; + +if (unlikely (kscand_test_exit (mm))) {
Some of these unlikelys feel like things we should leave to the branch predictors to figure out.
Quoted 22 lines
mm/kscand.c +22
-0+ next_mm = true; + goto outerloop; + } + +VMA_ITERATOR (vmi, mm, address); + +for_each_vma (vmi, vma) { +kscand_walk_page_vma (vma, &kscand_scanctrl); + vma_scanned_size += vma->vm_end - vma->vm_start; + +if (vma_scanned_size >= kscand_scan_size) { + next_mm = true; +/* TBD: Add scanned folios to migration list */ +break ; + } + } + +if (!vma) + address =0 ; +else + address = kscand_scanctrl.address +PAGE_SIZE ; +
mm/kscand.c static void kscand_wait_work(void) · line 362
Quoted 10 lines
mm/kscand.c +10
-0+ update_mmslot_info = true; + +if (update_mmslot_info) + mm_slot->address = address; + +outerloop: +/* exit_mmap will destroy ptes after this */ +mmap_read_unlock (mm); + +outerloop_mmap_lock:
This flow is getting a bit too complex for my liking. Maybe factor everything above here out given we run this in all exit paths. Then that factored out code can do early returns etc, without the goto nest. Might be able to use guard() for the spin_lock as well to allow return instead of goto in next bit.
Quoted 19 lines
mm/kscand.c +19
-0+spin_lock (&kscand_mm_lock); +WARN_ON (kscand_scan.mm_slot != mm_slot); + +/* +* Release the current mm_slot if this mm is about to die, or +* if we scanned all vmas of this mm. +*/ +if (unlikely (kscand_test_exit (mm)) || !vma || next_mm) { +/* +* Make sure that if mm_users is reaching zero while +* kscand runs here, kscand_exit will find +* mm_slot not pointing to the exiting mm. +*/ +if (slot->mm_node.next != &kscand_scan.mm_head) { + slot =list_entry (slot->mm_node.next, + struct mm_slot, mm_node); + kscand_scan.mm_slot = + mm_slot_entry(slot, struct kscand_mm_slot, slot); +
mm/kscand.c static void kscand_wait_work(void) · line 391
Quoted 10 lines
mm/kscand.c +10
-0+ }else + kscand_scan.mm_slot =NULL ; + +if (kscand_test_exit (mm)) { +kscand_collect_mm_slot (mm_slot); + goto end; + } + } + mm_slot->is_scanned = false; +end:
+ spin_unlock(&kscand_mm_lock);
Quoted 37 lines
mm/kscand.c +11
-1+return 0 ; +} +static void kscand_do_scan (void ) {unsigned long iter =0 , mms_to_scan;@@ -101,7 +405,7 @@ static void kscand_do_scan(void)break ;if (kscand_has_work ()) -msleep (100 ); +kscand_scan_mm_slot (); iter++;@@ -148,6 +452,7 @@ void __kscand_enter(struct mm_struct *mm)if (!kscand_slot)return ; + kscand_slot->address =0 ; slot = &kscand_slot->slot;spin_lock (&kscand_mm_lock);@@ -175,6 +480,12 @@ void __kscand_exit(struct mm_struct *mm)hash_del (&slot->hash);list_del (&slot->mm_node); free =1 ; + }else if (mm_slot && kscand_scan.mm_slot == mm_slot && !mm_slot->is_scanned) { +hash_del (&slot->hash); +list_del (&slot->mm_node); + free =1 ; +/* TBD: Set the actual next slot */ + kscand_scan.mm_slot =NULL ; }spin_unlock (&kscand_mm_lock);
mm/kscand.c static int stop_kscand(void) · line 538
Quoted 4 lines
mm/kscand.c +1
-0@@ -224,6 +535,12 @@ static int stop_kscand(void)return 0 ; } +static inline void init_list (void )
That's a very generic name that is likely to clash with something in future. kscand_init_list() Or don't bother as not a lot in here so you could just put them inline.
Quoted 17 lines
mm/kscand.c +7
-0+{ +INIT_LIST_HEAD (&kscand_scanctrl.scan_list); +init_waitqueue_head (&kscand_wait); +} +static int __init kscand_init (void ) {int err;@@ -234,6 +551,8 @@ static int __init kscand_init(void)pr_err ("kscand: kmem_cache error" );return -ENOMEM ; } + +init_list (); err =start_kscand ();if (err) goto err_kscand;
RFC PATCH v3 4/17 mm/kscand: Add only hot pages to migration list
In reply to Raghavendra K T Previously all pages, accessed once are added.
Improve it by adding those that are accessed second time.
This logic is closer to current NUMAB implementation of spotting hot pages.
--- mm/kscand.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/kscand.c +3 -1
@@ -196,6 +196,7 @@ static int hot_vma_idle_pte_entry(pte_t *pte,struct kscand_migrate_info *info;struct kscand_scanctrl *scanctrl = walk->private;int srcnid; +bool prev_idle; scanctrl->address = addr;pte_t pteval =ptep_get (pte);
@@ -219,6 +220,7 @@ static int hot_vma_idle_pte_entry(pte_t *pte,folio_put (folio);return 0 ; } + prev_idle =folio_test_idle (folio);folio_set_idle (folio);page_idle_clear_pte_refs (page, pte, walk); srcnid =folio_nid (folio);
@@ -233,7 +235,7 @@ static int hot_vma_idle_pte_entry(pte_t *pte,folio_put (folio);return 0 ; } -if (!folio_test_idle (folio) && +if (!folio_test_idle (folio) && !prev_idle && (folio_test_young (folio) ||folio_test_referenced (folio))) {/* XXX: Leaking memory. TBD: consume info */
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 04/17] mm/kscand: Add only hot pages to migration list
Quoted 5 lines · On Thu, 14 Aug 2025 15:32:54 +0000 Raghavendra K T <[email protected]>…
Previously all pages, accessed once are added.
Improve it by adding those that are accessed second time.This logic is closer to current NUMAB implementation of spotting hot pages.
Trivial but wrap patch description longer. 75 chars it typical.
Quoted 2 lines
Signed-off-by Raghavendra K T <[email protected]>
RFC PATCH v3 5/17 mm: Create a separate kthread for migration
In reply to Raghavendra K THaving independent thread helps in:
- Alleviating the need for multiple scanning threads
- Aids to control batch migration (TBD)
- Migration throttling (TBD)
--- mm/kscand.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+)
mm/kscand.c +74 -0
@@ -4,6 +4,7 @@#include <linux/sched.h> #include <linux/sched/mm.h> #include <linux/mmu_notifier.h> +#include <linux/migrate.h> #include <linux/rmap.h> #include <linux/pagewalk.h> #include <linux/page_ext.h>
@@ -41,6 +42,15 @@ static unsigned long kscand_mms_to_scan __read_mostly = KSCAND_MMS_TO_SCAN;bool kscand_scan_enabled = true;static bool need_wakeup; +static bool migrated_need_wakeup; + +/* How long to pause between two migration cycles */ +static unsigned int kmigrate_sleep_ms__read_mostly =20 ; + +static struct task_struct *kmigrated_thread__read_mostly ; +static DEFINE_MUTEX (kmigrated_mutex); +static DECLARE_WAIT_QUEUE_HEAD (kmigrated_wait); +static unsigned long kmigrated_sleep_expire;static unsigned long kscand_sleep_expire;
@@ -79,6 +89,7 @@ struct kscand_scanctrl { };struct kscand_scanctrl kscand_scanctrl; +/* Per folio information used for migration */ struct kscand_migrate_info {struct list_head migrate_node;
@@ -134,6 +145,19 @@ static inline bool is_valid_folio(struct folio *folio)return true; } +static inline void kmigrated_wait_work (void ) +{ +const unsigned long migrate_sleep_jiffies = +msecs_to_jiffies (kmigrate_sleep_ms); + +if (!migrate_sleep_jiffies) +return ; + + kmigrated_sleep_expire = jiffies + migrate_sleep_jiffies; +wait_event_timeout (kmigrated_wait, + true, + migrate_sleep_jiffies); +} static bool folio_idle_clear_pte_refs_one(struct folio *folio, struct vm_area_struct *vma,
@@ -537,6 +561,49 @@ static int stop_kscand(void)return 0 ; } +static int kmigrated (void *arg) +{ +while (true) { +WRITE_ONCE (migrated_need_wakeup, false); +if (unlikely (kthread_should_stop ())) +break ; +msleep (20 ); +kmigrated_wait_work (); + } +return 0 ; +} + +static int start_kmigrated (void ) +{ +struct task_struct *kthread; + +guard (mutex)(&kmigrated_mutex); + +/* Someone already succeeded in starting daemon */ +if (kmigrated_thread) +return 0 ; + + kthread =kthread_run (kmigrated,NULL ,"kmigrated" ); +if (IS_ERR (kmigrated_thread)) { +pr_err ("kmigrated: kthread_run(kmigrated) failed\n" ); +return PTR_ERR (kthread); + } + + kmigrated_thread = kthread; +pr_info ("kmigrated: Successfully started kmigrated" ); + +wake_up_interruptible (&kmigrated_wait); + +return 0 ; +} + +static int stop_kmigrated (void ) +{ +guard (mutex)(&kmigrated_mutex); +kthread_stop (kmigrated_thread); +return 0 ; +} +static inline void init_list (void ) {INIT_LIST_HEAD (&kscand_scanctrl.scan_list);
@@ -559,8 +626,15 @@ static int __init kscand_init(void)if (err) goto err_kscand; + err =start_kmigrated (); +if (err) + goto err_kmigrated; +return 0 ; +err_kmigrated: +stop_kmigrated (); + err_kscand:stop_kscand ();kscand_destroy ();
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 05/17] mm: Create a separate kthread for migration
Quoted 6 lines · On Thu, 14 Aug 2025 15:32:55 +0000 Raghavendra K T <[email protected]>…
Having independent thread helps in:
- Alleviating the need for multiple scanning threads
- Aids to control batch migration (TBD)
- Migration throttling (TBD)Signed-off-by Raghavendra K T <[email protected]>
A few superficial things inline.
Quoted 33 lines
--- mm/kscand.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+)mm/kscand.c +10
-0@@ -4,6 +4,7 @@#include <linux/sched.h> #include <linux/sched/mm.h> #include <linux/mmu_notifier.h> +#include <linux/migrate.h> #include <linux/rmap.h> #include <linux/pagewalk.h> #include <linux/page_ext.h> @@ -41,6 +42,15 @@ static unsigned long kscand_mms_to_scan __read_mostly = KSCAND_MMS_TO_SCAN;bool kscand_scan_enabled = true;static bool need_wakeup; +static bool migrated_need_wakeup; + +/* How long to pause between two migration cycles */ +static unsigned int kmigrate_sleep_ms__read_mostly =20 ; + +static struct task_struct *kmigrated_thread__read_mostly ; +static DEFINE_MUTEX (kmigrated_mutex); +static DECLARE_WAIT_QUEUE_HEAD (kmigrated_wait); +static unsigned long kmigrated_sleep_expire;static unsigned long kscand_sleep_expire;
mm/kscand.c struct kscand_scanctrl { · line 92
Quoted 4 lines
mm/kscand.c +1
-0@@ -79,6 +89,7 @@ struct kscand_scanctrl { };struct kscand_scanctrl kscand_scanctrl; +
Unrelated change. If you want it make sure to push back to earlier patch.
Quoted 9 lines
mm/kscand.c +2
-0/* Per folio information used for migration */ struct kscand_migrate_info {struct list_head migrate_node;@@ -134,6 +145,19 @@ static inline bool is_valid_folio(struct folio *folio)return true; } +static inline void kmigrated_wait_work (void ) +{
mm/kscand.c static inline bool is_valid_folio(struct folio *folio) · line 159
Quoted 10 lines
mm/kscand.c +10
-0+const unsigned long migrate_sleep_jiffies = +msecs_to_jiffies (kmigrate_sleep_ms); + +if (!migrate_sleep_jiffies) +return ; + + kmigrated_sleep_expire = jiffies + migrate_sleep_jiffies; +wait_event_timeout (kmigrated_wait, + true, + migrate_sleep_jiffies);
Unusual line wrapping. I'd go nearer to 80 chars and put at least some paramters on same line.
mm/kscand.c static int __init kscand_init(void) · line 631
Quoted 12 lines
mm/kscand.c +1
-0+}static inline void init_list (void ) {mm/kscand.c +3
-0INIT_LIST_HEAD (&kscand_scanctrl.scan_list);@@ -559,8 +626,15 @@ static int __init kscand_init(void)if (err) goto err_kscand; + err =start_kmigrated (); +if (err) + goto err_kmigrated;
Similar comment to earlier on not expecting side effects of a thread to stop if the start failed.
Quoted 9 lines
mm/kscand.c +4
-0+return 0 ; +err_kmigrated: +stop_kmigrated (); + err_kscand:stop_kscand ();kscand_destroy ();
RFC PATCH v3 6/17 mm/migration: migrate accessed folios to toptier node
In reply to Raghavendra K TA per mm migration list is added and a kernel thread iterates over each of them.
For each recently accessed slowtier folio in the migration list:
- Isolate LRU pages
- Migrate to a regular node.
The rationale behind whole migration is to speedup the access to recently accessed pages.
Currently, PTE A bit scanning approach lacks the information about exact destination node to migrate to.
Reason:
PROT_NONE hint fault based scanning is done in a process context. Here
when the fault occurs, source CPU of the fault associated the task is known. Time of page access is also accurate.
With the lack of above information, migration is done to node 0 by default.
--- include/linux/migrate.h | 2 + mm/kscand.c | 448 +++++++++++++++++++++++++++++++++++++++- mm/migrate.c | 2 +- 3 files changed, 440 insertions(+), 12 deletions(-)
include/linux/migrate.h +2 -0
@@ -142,6 +142,8 @@ const struct movable_operations *page_movable_ops(struct page *page) }#ifdef CONFIG_NUMA_BALANCING +bool migrate_balanced_pgdat (struct pglist_data *pgdat, +unsigned long nr_migrate_pages);int migrate_misplaced_folio_prepare (struct folio *folio,struct vm_area_struct *vma,int node);int migrate_misplaced_folio (struct folio *folio,int node);
mm/kscand.c +437 -11
572 lines not on this page · raw message
mm/migrate.c +1 -1
@@ -2541,7 +2541,7 @@ SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,* Returns true if this is a safe migration target node for misplaced NUMA * pages. Currently it only checks the watermarks which is crude. */ -static bool migrate_balanced_pgdat (struct pglist_data *pgdat, +bool migrate_balanced_pgdat (struct pglist_data *pgdat,unsigned long nr_migrate_pages) {int z;
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 06/17] mm/migration: migrate accessed folios to toptier node
On Thu, 14 Aug 2025 15:32:56 +0000
Quoted 13 lines · On Thu, 14 Aug 2025 15:32:56 +0000 Raghavendra K T <[email protected]>…
A per mm migration list is added and a kernel thread iterates over each of them.
For each recently accessed slowtier folio in the migration list:
- Isolate LRU pages
- Migrate to a regular node.The rationale behind whole migration is to speedup the access to recently accessed pages.
Currently, PTE A bit scanning approach lacks the information about exact destination node to migrate to.
Quoted 7 lines
Reason:
PROT_NONEhint fault based scanning is done in a process context. Here
when the fault occurs, source CPU of the fault associated the task is known. Time of page access is also accurate.
With the lack of above information, migration is done to node 0 by default.Signed-off-by Raghavendra K T <[email protected]>
Some superficial stuff inline. I'm still getting my head around the overall approach.
Quoted 18 lines
mm/kscand.c +5
-1static inline bool is_valid_folio (struct folio *folio) {if (!folio || !folio_mapped (folio) || !folio_raw_mapping (folio))@@ -145,18 +272,113 @@ static inline bool is_valid_folio(struct folio *folio)return true; } -static inline void kmigrated_wait_work (void ) +enum kscand_migration_err { +KSCAND_NULL_MM =1 , +KSCAND_EXITING_MM , +KSCAND_INVALID_FOLIO , +KSCAND_NONLRU_FOLIO ,
mm/kscand.c static inline bool is_valid_folio(struct folio *folio) · line 289
Quoted 10 lines
mm/kscand.c +10
-0+KSCAND_INELIGIBLE_SRC_NODE , +KSCAND_SAME_SRC_DEST_NODE , +KSCAND_PTE_NOT_PRESENT , +KSCAND_PMD_NOT_PRESENT , +KSCAND_NO_PTE_OFFSET_MAP_LOCK , +KSCAND_NOT_HOT_PAGE , + KSCAND_LRU_ISOLATION_ERR, +}; + +
One line probably appropriate here.
Quoted 88 lines
mm/kscand.c +77
-8+static bool is_hot_page (struct folio *folio) { -const unsigned long migrate_sleep_jiffies = -msecs_to_jiffies (kmigrate_sleep_ms); +bool ret = false; -if (!migrate_sleep_jiffies) -return ; +if (!folio_test_idle (folio)) + ret =folio_test_referenced (folio) ||folio_test_young (folio); - kmigrated_sleep_expire = jiffies + migrate_sleep_jiffies; -wait_event_timeout (kmigrated_wait, - true, - migrate_sleep_jiffies); +return ret; +} + +static int kmigrated_promote_folio (struct kscand_migrate_info *info, +struct mm_struct *mm, +int destnid) +{ +unsigned long pfn; +unsigned long address; +struct page *page; +struct folio *folio =NULL ; +int ret; +pmd_t *pmd; +pte_t *pte; +spinlock_t *ptl; +pmd_t pmde; +int srcnid; + +if (mm ==NULL ) +return KSCAND_NULL_MM ; + +if (mm ==READ_ONCE (kmigrated_cur_mm) && +READ_ONCE (kmigrated_clean_list)) { +WARN_ON_ONCE (mm); +return KSCAND_EXITING_MM ; + } + + pfn = info->pfn; + address = info->address; + page =pfn_to_online_page (pfn); + +if (page) + folio =page_folio (page); + +if (!page ||PageTail (page) || !is_valid_folio (folio)) +return KSCAND_INVALID_FOLIO ; + +if (!folio_test_lru (folio)) +return KSCAND_NONLRU_FOLIO ; + +if (!is_hot_page (folio)) +return KSCAND_NOT_HOT_PAGE ; + +folio_get (folio); + + srcnid =folio_nid (folio); + +/* Do not try to promote pages from regular nodes */ +if (!kscand_eligible_srcnid (srcnid)) { +folio_put (folio); +return KSCAND_INELIGIBLE_SRC_NODE ; + } + +/* Also happen when it is already migrated */ +if (srcnid == destnid) { +folio_put (folio); +return KSCAND_SAME_SRC_DEST_NODE ; + } + + address = info->address; + pmd =pmd_off (mm, address); + pmde =pmdp_get (pmd); + +if (!pmd_present (pmde)) { +folio_put (folio); +return KSCAND_PMD_NOT_PRESENT ; + } + + pte =pte_offset_map_lock (mm, pmd, address, &ptl); +if (!pte) { +folio_put (folio); +WARN_ON_ONCE (!pte); +return KSCAND_NO_PTE_OFFSET_MAP_LOCK ;
mm/kscand.c static inline bool is_valid_folio(struct folio *folio) · line 379
Quoted 10 lines
mm/kscand.c +10
-0+ } + + ret =kscand_migrate_misplaced_folio_prepare (folio,NULL , destnid); + +folio_put (folio); +pte_unmap_unlock (pte, ptl); + +if (ret) +return KSCAND_LRU_ISOLATION_ERR ; +
One line enough.
mm/kscand.c static inline bool is_valid_folio(struct folio *folio) · line 381
Quoted 2 lines
mm/kscand.c +2
-0+ +return migrate_misplaced_folio (folio, destnid);
extra space after return.
Quoted 10 lines
mm/kscand.c +3
-0} static bool folio_idle_clear_pte_refs_one(struct folio *folio,@@ -302,6 +524,115 @@ static inline int kscand_test_exit(struct mm_struct *mm)return atomic_read (&mm->mm_users) ==0 ; } +struct destroy_list_work { +struct list_head migrate_head; +struct work_struct dwork;
mm/kscand.c static inline int kscand_test_exit(struct mm_struct *mm) · line 539
Quoted 10 lines
mm/kscand.c +10
-0+}; + +static void kmigrated_destroy_list_fn (struct work_struct *work) +{ +struct destroy_list_work *dlw; +struct kscand_migrate_info *info, *tmp; + + dlw = container_of(work, struct destroy_list_work, dwork); + +if (!list_empty (&dlw->migrate_head)) {
Similar to below. I'm not sure this check is worth having unless something else ends up under it later.
mm/kscand.c static inline int kscand_test_exit(struct mm_struct *mm) · line 552
Quoted 13 lines
mm/kscand.c +13
-0+list_for_each_entry_safe (info, tmp, &dlw->migrate_head, migrate_node) { +list_del (&info->migrate_node); +kfree (info); + } + } + +kfree (dlw); +} + +static void kmigrated_destroy_list (struct list_head *list_head) +{ +struct destroy_list_work *destroy_list_work; +
One blank line
Quoted 10 lines
mm/kscand.c +10
-0+ + destroy_list_work =kmalloc (sizeof (*destroy_list_work),GFP_KERNEL ); +if (!destroy_list_work) +return ; + +INIT_LIST_HEAD (&destroy_list_work->migrate_head); +list_splice_tail_init (list_head, &destroy_list_work->migrate_head); +INIT_WORK (&destroy_list_work->dwork, kmigrated_destroy_list_fn); +schedule_work (&destroy_list_work->dwork); +}
mm/kscand.c static inline int kscand_test_exit(struct mm_struct *mm) · line 598
Quoted 10 lines
mm/kscand.c +9
-0+ +static void kscand_cleanup_migration_list (struct mm_struct *mm) +{ +struct kmigrated_mm_slot *mm_slot; +struct mm_slot *slot; + + mm_slot =kmigrated_get_mm_slot (mm, false); + + slot = &mm_slot->mm_slot;
Maybe combine these with declrations.
struct kmigrated_mm_slot *mm_slot = kmigrated_get_mm_slot(mm, false); struct mm_slot *slot = &mm_slot->mm_slot;
seems clear enough (assuming nothing else is added later).
Quoted 25 lines
mm/kscand.c +25
-0+ +if (mm_slot && slot && slot->mm == mm) { +spin_lock (&mm_slot->migrate_lock); + +if (!list_empty (&mm_slot->migrate_head)) { +if (mm ==READ_ONCE (kmigrated_cur_mm)) { +/* A folio in this mm is being migrated. wait */ +WRITE_ONCE (kmigrated_clean_list, true); + } + +kmigrated_destroy_list (&mm_slot->migrate_head); +spin_unlock (&mm_slot->migrate_lock); +retry: +if (!spin_trylock (&mm_slot->migrate_lock)) { +cpu_relax (); + goto retry; + } + +if (mm ==READ_ONCE (kmigrated_cur_mm)) { +spin_unlock (&mm_slot->migrate_lock); + goto retry; + } + } +/* Reset migrated mm_slot if it was pointing to us */ +if (kmigrated_daemon.mm_slot == mm_slot)
mm/kscand.c static inline int kscand_test_exit(struct mm_struct *mm) · line 633
Quoted 10 lines
mm/kscand.c +10
-0+ kmigrated_daemon.mm_slot =NULL ; + +hash_del (&slot->hash); +list_del (&slot->mm_node); +mm_slot_free (kmigrated_slot_cache, mm_slot); + +WRITE_ONCE (kmigrated_clean_list, false); + +spin_unlock (&mm_slot->migrate_lock); + }
Something odd with indent here.
Quoted 19 lines
mm/kscand.c +7
-0+} +static void kscand_collect_mm_slot (struct kscand_mm_slot *mm_slot) {struct mm_slot *slot = &mm_slot->slot;@@ -313,11 +644,77 @@ static void kscand_collect_mm_slot(struct kscand_mm_slot *mm_slot)hash_del (&slot->hash);list_del (&slot->mm_node); +kscand_cleanup_migration_list (mm); +mm_slot_free (kscand_slot_cache, mm_slot);mmdrop (mm); } } +static void kmigrated_migrate_mm (struct kmigrated_mm_slot *mm_slot) +{ +int ret =0 , dest =-1 ;
mm/kscand.c static void kscand_collect_mm_slot(struct kscand_mm_slot *mm_slot) · line 666
Quoted 10 lines
mm/kscand.c +10
-0+struct mm_slot *slot; +struct mm_struct *mm; +struct kscand_migrate_info *info, *tmp; + +spin_lock (&mm_slot->migrate_lock); + + slot = &mm_slot->mm_slot; + mm = slot->mm; + +if (!list_empty (&mm_slot->migrate_head)) {
If it's empty the iterating the list will do nothing. So is this check useful? Maybe other code comes under this check later though.
mm/kscand.c static void kscand_collect_mm_slot(struct kscand_mm_slot *mm_slot) · line 670
Quoted 4 lines
mm/kscand.c +4
-0+list_for_each_entry_safe (info, tmp, &mm_slot->migrate_head, + migrate_node) { +if (READ_ONCE (kmigrated_clean_list)) + goto clean_list_handled;
Currently same a break. I assume this will change later in patch, if so ignore this comment.
Quoted 22 lines
mm/kscand.c +19
-0+ +list_del (&info->migrate_node); + +spin_unlock (&mm_slot->migrate_lock); + + dest =kscand_get_target_node (NULL ); + ret =kmigrated_promote_folio (info, mm, dest); + +kfree (info); + +cond_resched (); +spin_lock (&mm_slot->migrate_lock); + } + } +clean_list_handled: +/* Reset mm of folio entry we are migrating */ +WRITE_ONCE (kmigrated_cur_mm,NULL ); +spin_unlock (&mm_slot->migrate_lock); +}
mm/kscand.c static int __init kscand_init(void) · line 1044
Quoted 5 lines
mm/kscand.c +2
-0@@ -621,6 +1040,13 @@ static int __init kscand_init(void)return -ENOMEM ; } + kmigrated_slot_cache =KMEM_CACHE (kmigrated_mm_slot,0 ); +
Drop this blank line to keep call + error check closely associated.
Quoted 13 lines
mm/kscand.c +5
-0+if (!kmigrated_slot_cache) { +pr_err ("kmigrated: kmem_cache error" ); +return -ENOMEM ; + } +init_list (); err =start_kscand ();if (err)mm/migrate.c +0
-0
mm/migrate.c SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages, · line 2545
Quoted 6 lines
mm/migrate.c +1
-1@@ -2541,7 +2541,7 @@ SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,* Returns true if this is a safe migration target node for misplaced NUMA * pages. Currently it only checks the watermarks which is crude. */ -static bool migrate_balanced_pgdat (struct pglist_data *pgdat, +bool migrate_balanced_pgdat (struct pglist_data *pgdat,unsigned long nr_migrate_pages)
Update parameter alignment as well.
Quoted 2 lines
mm/migrate.c +0
-0{int z;
RFC PATCH v3 7/17 mm: Add throttling of mm scanning using scan_period
In reply to Raghavendra K TBefore this patch, scanning of tasks' mm is done continuously and also at the same rate.
Improve that by adding a throttling logic:
1) If there were useful pages found during last scan and current scan, decrease the scan_period (to increase scan rate) by TUNE_PERCENT (15%).
2) If there were no useful pages found in last scan, and there are candidate migration pages in the current scan decrease the scan_period aggressively by 2 power SCAN_CHANGE_SCALE (2^3 = 8 now).
Vice versa is done for the reverse case.
Scan period is clamped between MIN (600ms) and MAX (5sec).
--- mm/kscand.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-)
mm/kscand.c +109 -1
@@ -20,6 +20,7 @@#include <linux/string.h> #include <linux/delay.h> #include <linux/cleanup.h> +#include <linux/minmax.h> #include <asm/pgalloc.h> #include "internal.h"
@@ -33,6 +34,16 @@ static DEFINE_MUTEX(kscand_mutex);#define KSCAND_SCAN_SIZE (1 * 1024 * 1024 * 1024UL)static unsigned long kscand_scan_size__read_mostly =KSCAND_SCAN_SIZE ; +/* +* Scan period for each mm. +* Min: 600ms default: 2sec Max: 5sec +*/ +#define KSCAND_SCAN_PERIOD_MAX 5000U +#define KSCAND_SCAN_PERIOD_MIN 600U +#define KSCAND_SCAN_PERIOD 2000U + +static unsigned int kscand_mm_scan_period_ms__read_mostly =KSCAND_SCAN_PERIOD ; +/* How long to pause between two scan cycles */ static unsigned int kscand_scan_sleep_ms__read_mostly =20 ;
@@ -78,6 +89,11 @@ static struct kmem_cache *kmigrated_slot_cache __read_mostly;/* Per mm information collected to control VMA scanning */ struct kscand_mm_slot {struct mm_slot slot; +/* Unit: ms. Determines how aften mm scan should happen. */ +unsigned int scan_period; +unsigned long next_scan; +/* Tracks how many useful pages obtained for migration in the last scan */ +unsigned long scan_delta;long address;bool is_scanned; };
@@ -715,13 +731,92 @@ static void kmigrated_migrate_folio(void) } } +/* +* This is the normal change percentage when old and new delta remain same. +* i.e., either both positive or both zero. +*/ +#define SCAN_PERIOD_TUNE_PERCENT 15 + +/* This is to change the scan_period aggressively when deltas are different */ +#define SCAN_PERIOD_CHANGE_SCALE 3 +/* +* XXX: Hack to prevent unmigrated pages coming again and again while scanning. +* Actual fix needs to identify the type of unmigrated pages OR consider migration +* failures in next scan. +*/ +#define KSCAND_IGNORE_SCAN_THR 256 + +/* Maintains stability of scan_period by decaying last time accessed pages */ +#define SCAN_DECAY_SHIFT 4 +/* +* X : Number of useful pages in the last scan. +* Y : Number of useful pages found in current scan. +* Tuning scan_period: +* Initial scan_period is 2s. +* case 1: (X = 0, Y = 0) +* Increase scan_period by SCAN_PERIOD_TUNE_PERCENT. +* case 2: (X = 0, Y > 0) +* Decrease scan_period by (2 << SCAN_PERIOD_CHANGE_SCALE). +* case 3: (X > 0, Y = 0 ) +* Increase scan_period by (2 << SCAN_PERIOD_CHANGE_SCALE). +* case 4: (X > 0, Y > 0) +* Decrease scan_period by SCAN_PERIOD_TUNE_PERCENT. +*/ +static inline void kscand_update_mmslot_info (struct kscand_mm_slot *mm_slot, +unsigned long total) +{ +unsigned int scan_period; +unsigned long now; +unsigned long old_scan_delta; + + scan_period = mm_slot->scan_period; + old_scan_delta = mm_slot->scan_delta; + +/* decay old value */ + total = (old_scan_delta >>SCAN_DECAY_SHIFT ) + total; + +/* XXX: Hack to get rid of continuously failing/unmigrateable pages */ +if (total <KSCAND_IGNORE_SCAN_THR ) + total =0 ; + +/* +* case 1: old_scan_delta and new delta are similar, (slow) TUNE_PERCENT used. +* case 2: old_scan_delta and new delta are different. (fast) CHANGE_SCALE used. +* TBD: +* 1. Further tune scan_period based on delta between last and current scan delta. +* 2. Optimize calculation +*/ +if (!old_scan_delta && !total) { + scan_period = (100 +SCAN_PERIOD_TUNE_PERCENT ) * scan_period; + scan_period /=100 ; + }else if (old_scan_delta && total) { + scan_period = (100 -SCAN_PERIOD_TUNE_PERCENT ) * scan_period; + scan_period /=100 ; + }else if (old_scan_delta && !total) { + scan_period = scan_period <<SCAN_PERIOD_CHANGE_SCALE ; + }else { + scan_period = scan_period >>SCAN_PERIOD_CHANGE_SCALE ; + } + + scan_period =clamp (scan_period,KSCAND_SCAN_PERIOD_MIN ,KSCAND_SCAN_PERIOD_MAX ); + + now = jiffies; + mm_slot->next_scan = now +msecs_to_jiffies (scan_period); + mm_slot->scan_period = scan_period; + mm_slot->scan_delta = total; +} +static unsigned long kscand_scan_mm_slot (void ) {bool next_mm = false;bool update_mmslot_info = false; +unsigned int mm_slot_scan_period; +unsigned long now; +unsigned long mm_slot_next_scan;unsigned long vma_scanned_size =0 ;unsigned long address; +unsigned long total =0 ;struct mm_slot *slot;struct mm_struct *mm;
@@ -746,6 +841,8 @@ static unsigned long kscand_scan_mm_slot(void) mm = slot->mm; mm_slot->is_scanned = true; + mm_slot_next_scan = mm_slot->next_scan; + mm_slot_scan_period = mm_slot->scan_period;spin_unlock (&kscand_mm_lock);if (unlikely (!mmap_read_trylock (mm)))
@@ -756,6 +853,11 @@ static unsigned long kscand_scan_mm_slot(void) goto outerloop; } + now = jiffies; + +if (mm_slot_next_scan &&time_before (now, mm_slot_next_scan)) + goto outerloop; +VMA_ITERATOR (vmi, mm, address); kmigrated_mm_slot =kmigrated_get_mm_slot (mm, false);
@@ -786,8 +888,10 @@ static unsigned long kscand_scan_mm_slot(void) update_mmslot_info = true; -if (update_mmslot_info) +if (update_mmslot_info) { mm_slot->address = address; +kscand_update_mmslot_info (mm_slot, total); + } outerloop:/* exit_mmap will destroy ptes after this */
@@ -889,6 +993,10 @@ void __kscand_enter(struct mm_struct *mm)return ; kscand_slot->address =0 ; + kscand_slot->scan_period = kscand_mm_scan_period_ms; + kscand_slot->next_scan =0 ; + kscand_slot->scan_delta =0 ; + slot = &kscand_slot->slot;spin_lock (&kscand_mm_lock);
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 07/17] mm: Add throttling of mm scanning using scan_period
Quoted 10 lines · On Thu, 14 Aug 2025 15:32:57 +0000 Raghavendra K T <[email protected]>…
Before this patch, scanning of tasks' mm is done continuously and also at the same rate.
Improve that by adding a throttling logic:
1) If there were useful pages found during last scan and current scan, decrease thescan_period(to increase scan rate) byTUNE_PERCENT(15%).2) If there were no useful pages found in last scan, and there are candidate migration pages in the current scan decrease the
scan_periodaggressively by 2 powerSCAN_CHANGE_SCALE(2^3 = 8 now).
Explain why those values were chosen.
Quoted 5 lines
Vice versa is done for the reverse case.
Scan period is clamped between MIN (600ms) and MAX (5sec).Signed-off-by Raghavendra K T <[email protected]>
Various minor comments inline.
Thanks,
Jonathan
Quoted 34 lines
--- mm/kscand.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-)mm/kscand.c +11
-0@@ -20,6 +20,7 @@#include <linux/string.h> #include <linux/delay.h> #include <linux/cleanup.h> +#include <linux/minmax.h> #include <asm/pgalloc.h> #include "internal.h" @@ -33,6 +34,16 @@ static DEFINE_MUTEX(kscand_mutex);#define KSCAND_SCAN_SIZE (1 * 1024 * 1024 * 1024UL)static unsigned long kscand_scan_size__read_mostly =KSCAND_SCAN_SIZE ; +/* +* Scan period for each mm. +* Min: 600ms default: 2sec Max: 5sec +*/ +#define KSCAND_SCAN_PERIOD_MAX 5000U +#define KSCAND_SCAN_PERIOD_MIN 600U +#define KSCAND_SCAN_PERIOD 2000U + +static unsigned int kscand_mm_scan_period_ms__read_mostly =KSCAND_SCAN_PERIOD ; +/* How long to pause between two scan cycles */ static unsigned int kscand_scan_sleep_ms__read_mostly =20 ;
mm/kscand.c static struct kmem_cache *kmigrated_slot_cache __read_mostly; · line 92
Quoted 4 lines
mm/kscand.c +1
-0@@ -78,6 +89,11 @@ static struct kmem_cache *kmigrated_slot_cache __read_mostly;/* Per mm information collected to control VMA scanning */ struct kscand_mm_slot {struct mm_slot slot; +/* Unit: ms. Determines how aften mm scan should happen. */
Name it scan_period_ms and you can drop the unit comment + that info is conveyed everywhere it is used. You do that for the global so why not here as well.
Quoted 19 lines
mm/kscand.c +12
-0+unsigned int scan_period; +unsigned long next_scan; +/* Tracks how many useful pages obtained for migration in the last scan */ +unsigned long scan_delta;long address;bool is_scanned; };@@ -715,13 +731,92 @@ static void kmigrated_migrate_folio(void) } } +/* Maintains stability of scan_period by decaying last time accessed pages */ +#define SCAN_DECAY_SHIFT 4 +/* +* X : Number of useful pages in the last scan. +* Y : Number of useful pages found in current scan. +* Tuning scan_period: +* Initial scan_period is 2s. +* case 1: (X = 0, Y = 0)
mm/kscand.c static void kmigrated_migrate_folio(void) · line 751
Quoted 10 lines
mm/kscand.c +10
-0+* Increase scan_period by SCAN_PERIOD_TUNE_PERCENT. +* case 2: (X = 0, Y > 0) +* Decrease scan_period by (2 << SCAN_PERIOD_CHANGE_SCALE). +* case 3: (X > 0, Y = 0 ) +* Increase scan_period by (2 << SCAN_PERIOD_CHANGE_SCALE). +* case 4: (X > 0, Y > 0) +* Decrease scan_period by SCAN_PERIOD_TUNE_PERCENT. +*/ +static inline void kscand_update_mmslot_info (struct kscand_mm_slot *mm_slot, +unsigned long total)
As below. I'd make total a local variable for now.
mm/kscand.c static void kmigrated_migrate_folio(void) · line 770
Quoted 4 lines
mm/kscand.c +4
-0+{ +unsigned int scan_period; +unsigned long now; +unsigned long old_scan_delta;
Might as well combine these two lines.
Quoted 39 lines
mm/kscand.c +38
-0+ + scan_period = mm_slot->scan_period; + old_scan_delta = mm_slot->scan_delta; + +/* decay old value */ + total = (old_scan_delta >>SCAN_DECAY_SHIFT ) + total; + +/* XXX: Hack to get rid of continuously failing/unmigrateable pages */ +if (total <KSCAND_IGNORE_SCAN_THR ) + total =0 ; + +/* +* case 1: old_scan_delta and new delta are similar, (slow) TUNE_PERCENT used. +* case 2: old_scan_delta and new delta are different. (fast) CHANGE_SCALE used. +* TBD: +* 1. Further tune scan_period based on delta between last and current scan delta. +* 2. Optimize calculation +*/ +if (!old_scan_delta && !total) { + scan_period = (100 +SCAN_PERIOD_TUNE_PERCENT ) * scan_period; + scan_period /=100 ; + }else if (old_scan_delta && total) { + scan_period = (100 -SCAN_PERIOD_TUNE_PERCENT ) * scan_period; + scan_period /=100 ; + }else if (old_scan_delta && !total) { + scan_period = scan_period <<SCAN_PERIOD_CHANGE_SCALE ; + }else { + scan_period = scan_period >>SCAN_PERIOD_CHANGE_SCALE ; + } + + scan_period =clamp (scan_period,KSCAND_SCAN_PERIOD_MIN ,KSCAND_SCAN_PERIOD_MAX ); + + now = jiffies; + mm_slot->next_scan = now +msecs_to_jiffies (scan_period); + mm_slot->scan_period = scan_period; + mm_slot->scan_delta = total; +} +static unsigned long kscand_scan_mm_slot (void )
mm/kscand.c static void kmigrated_migrate_folio(void) · line 819
Quoted 10 lines
mm/kscand.c +4
-0{bool next_mm = false;bool update_mmslot_info = false; +unsigned int mm_slot_scan_period; +unsigned long now; +unsigned long mm_slot_next_scan;unsigned long vma_scanned_size =0 ;unsigned long address; +unsigned long total =0 ;
Given this never changes in this patch I'd drop the parameter from kscand_update_mmslot_info() and bring it back when you have code that passes in non 0.
Quoted 13 lines
mm/kscand.c +2
-0struct mm_slot *slot;struct mm_struct *mm;@@ -746,6 +841,8 @@ static unsigned long kscand_scan_mm_slot(void) mm = slot->mm; mm_slot->is_scanned = true; + mm_slot_next_scan = mm_slot->next_scan; + mm_slot_scan_period = mm_slot->scan_period;spin_unlock (&kscand_mm_lock);if (unlikely (!mmap_read_trylock (mm)))
mm/kscand.c static unsigned long kscand_scan_mm_slot(void) · line 858
Quoted 6 lines
mm/kscand.c +3
-0@@ -756,6 +853,11 @@ static unsigned long kscand_scan_mm_slot(void) goto outerloop; } + now = jiffies; + +if (mm_slot_next_scan &&time_before (now, mm_slot_next_scan))
If now is only used once, seems better just to put jiffies in the call and drop local variable.
Quoted 28 lines
mm/kscand.c +9
-1+ goto outerloop; +VMA_ITERATOR (vmi, mm, address); kmigrated_mm_slot =kmigrated_get_mm_slot (mm, false);@@ -786,8 +888,10 @@ static unsigned long kscand_scan_mm_slot(void) update_mmslot_info = true; -if (update_mmslot_info) +if (update_mmslot_info) { mm_slot->address = address; +kscand_update_mmslot_info (mm_slot, total); + } outerloop:/* exit_mmap will destroy ptes after this */ @@ -889,6 +993,10 @@ void __kscand_enter(struct mm_struct *mm)return ; kscand_slot->address =0 ; + kscand_slot->scan_period = kscand_mm_scan_period_ms; + kscand_slot->next_scan =0 ; + kscand_slot->scan_delta =0 ; + slot = &kscand_slot->slot;spin_lock (&kscand_mm_lock);
RFC PATCH v3 8/17 mm: Add throttling of mm scanning using scan_size
In reply to Raghavendra K TBefore this patch, scanning is done on entire virtual address space of all the tasks. Now the scan size is shrunk or expanded based on the useful pages found in the last scan.
This helps to quickly get out of unnecessary scanning thus burning lesser CPU.
Drawback: If a useful chunk is at the other end of the VMA space, it will delay scanning and migration.
Shrink/expand algorithm for scan_size:
X : Number of useful pages in the last scan.
Y : Number of useful pages found in current scan.
Initial scan_size is 1GB
case 1: (X = 0, Y = 0)
Decrease scan_size by 2
case 2: (X = 0, Y > 0)
Aggressively change to MAX (4GB)
case 3: (X > 0, Y = 0 )
No change
case 4: (X > 0, Y > 0)
Increase scan_size by 2
Scan size is clamped between MIN (256MB) and MAX (4GB)).
TBD: Tuning based on real workloads
--- mm/kscand.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
mm/kscand.c +29 -0
@@ -28,10 +28,15 @@static struct task_struct *kscand_thread__read_mostly ;static DEFINE_MUTEX (kscand_mutex); +/* * Total VMA size to cover during scan. +* Min: 256MB default: 1GB max: 4GB */ +#define KSCAND_SCAN_SIZE_MIN (256 * 1024 * 1024UL) +#define KSCAND_SCAN_SIZE_MAX (4 * 1024 * 1024 * 1024UL)#define KSCAND_SCAN_SIZE (1 * 1024 * 1024 * 1024UL) +static unsigned long kscand_scan_size__read_mostly =KSCAND_SCAN_SIZE ;/*
@@ -94,6 +99,8 @@ struct kscand_mm_slot {unsigned long next_scan;/* Tracks how many useful pages obtained for migration in the last scan */ unsigned long scan_delta; +/* Determines how much VMA address space to be covered in the scanning */ +unsigned long scan_size;long address;bool is_scanned; };
@@ -746,6 +753,8 @@ static void kmigrated_migrate_folio(void)*/ #define KSCAND_IGNORE_SCAN_THR 256 +#define SCAN_SIZE_CHANGE_SHIFT 1 +/* Maintains stability of scan_period by decaying last time accessed pages */ #define SCAN_DECAY_SHIFT 4/*
@@ -761,14 +770,26 @@ static void kmigrated_migrate_folio(void)* Increase scan_period by (2 << SCAN_PERIOD_CHANGE_SCALE). * case 4: (X > 0, Y > 0) * Decrease scan_period by SCAN_PERIOD_TUNE_PERCENT. +* Tuning scan_size: +* Initial scan_size is 4GB +* case 1: (X = 0, Y = 0) +* Decrease scan_size by (1 << SCAN_SIZE_CHANGE_SHIFT). +* case 2: (X = 0, Y > 0) +* scan_size = KSCAND_SCAN_SIZE_MAX +* case 3: (X > 0, Y = 0 ) +* No change +* case 4: (X > 0, Y > 0) +* Increase scan_size by (1 << SCAN_SIZE_CHANGE_SHIFT). */ static inline void kscand_update_mmslot_info (struct kscand_mm_slot *mm_slot,unsigned long total) {unsigned int scan_period;unsigned long now; +unsigned long scan_size;unsigned long old_scan_delta; + scan_size = mm_slot->scan_size; scan_period = mm_slot->scan_period; old_scan_delta = mm_slot->scan_delta;
@@ -789,20 +810,25 @@ static inline void kscand_update_mmslot_info(struct kscand_mm_slot *mm_slot,if (!old_scan_delta && !total) { scan_period = (100 +SCAN_PERIOD_TUNE_PERCENT ) * scan_period; scan_period /=100 ; + scan_size = scan_size >>SCAN_SIZE_CHANGE_SHIFT ; }else if (old_scan_delta && total) { scan_period = (100 -SCAN_PERIOD_TUNE_PERCENT ) * scan_period; scan_period /=100 ; + scan_size = scan_size <<SCAN_SIZE_CHANGE_SHIFT ; }else if (old_scan_delta && !total) { scan_period = scan_period <<SCAN_PERIOD_CHANGE_SCALE ; }else { scan_period = scan_period >>SCAN_PERIOD_CHANGE_SCALE ; + scan_size =KSCAND_SCAN_SIZE_MAX ; } scan_period =clamp (scan_period,KSCAND_SCAN_PERIOD_MIN ,KSCAND_SCAN_PERIOD_MAX ); + scan_size =clamp (scan_size,KSCAND_SCAN_SIZE_MIN ,KSCAND_SCAN_SIZE_MAX ); now = jiffies; mm_slot->next_scan = now +msecs_to_jiffies (scan_period); mm_slot->scan_period = scan_period; + mm_slot->scan_size = scan_size; mm_slot->scan_delta = total; }
@@ -814,6 +840,7 @@ static unsigned long kscand_scan_mm_slot(void)unsigned int mm_slot_scan_period;unsigned long now;unsigned long mm_slot_next_scan; +unsigned long mm_slot_scan_size;unsigned long vma_scanned_size =0 ;unsigned long address;unsigned long total =0 ;
@@ -843,6 +870,7 @@ static unsigned long kscand_scan_mm_slot(void) mm_slot->is_scanned = true; mm_slot_next_scan = mm_slot->next_scan; mm_slot_scan_period = mm_slot->scan_period; + mm_slot_scan_size = mm_slot->scan_size;spin_unlock (&kscand_mm_lock);if (unlikely (!mmap_read_trylock (mm)))
@@ -994,6 +1022,7 @@ void __kscand_enter(struct mm_struct *mm) kscand_slot->address =0 ; kscand_slot->scan_period = kscand_mm_scan_period_ms; + kscand_slot->scan_size = kscand_scan_size; kscand_slot->next_scan =0 ; kscand_slot->scan_delta =0 ;
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 08/17] mm: Add throttling of mm scanning using scan_size
Quoted 23 lines · On Thu, 14 Aug 2025 15:32:58 +0000 Raghavendra K T <[email protected]>…
Before this patch, scanning is done on entire virtual address space of all the tasks. Now the scan size is shrunk or expanded based on the useful pages found in the last scan.
This helps to quickly get out of unnecessary scanning thus burning lesser CPU.
Drawback: If a useful chunk is at the other end of the VMA space, it will delay scanning and migration.
Shrink/expand algorithm for
scan_size:
X : Number of useful pages in the last scan.
Y : Number of useful pages found in current scan.
Initialscan_sizeis 1GB
case 1: (X = 0, Y = 0)
Decreasescan_sizeby 2
case 2: (X = 0, Y > 0)
Aggressively change to MAX (4GB)
case 3: (X > 0, Y = 0 )
No change
case 4: (X > 0, Y > 0)
Increasescan_sizeby 2
Quoted 2 lines
Scan size is clamped between MIN (256MB) and MAX (4GB)).
TBD: Tuning based on real workloads
Seems like a reasonable thing to do, but as you say tuning data needed to justify how aggressive this should be and those size limits.
Trivial stuff inline.
Quoted 11 lines
Signed-off-by Raghavendra K T <[email protected]>--- mm/kscand.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)mm/kscand.c +0
-0
mm/kscand.c line 31
Quoted 4 lines
mm/kscand.c +1
-0@@ -28,10 +28,15 @@static struct task_struct *kscand_thread__read_mostly ;static DEFINE_MUTEX (kscand_mutex); +
Push that into earlier patch to cut down on churn / noise.
mm/kscand.c line 39
Quoted 8 lines
mm/kscand.c +4
-0/* * Total VMA size to cover during scan. +* Min: 256MB default: 1GB max: 4GB */ +#define KSCAND_SCAN_SIZE_MIN (256 * 1024 * 1024UL) +#define KSCAND_SCAN_SIZE_MAX (4 * 1024 * 1024 * 1024UL)#define KSCAND_SCAN_SIZE (1 * 1024 * 1024 * 1024UL) +
Likewise.
Quoted 14 lines
mm/kscand.c +2
-0static unsigned long kscand_scan_size__read_mostly =KSCAND_SCAN_SIZE ;/* @@ -94,6 +99,8 @@ struct kscand_mm_slot {unsigned long next_scan;/* Tracks how many useful pages obtained for migration in the last scan */ unsigned long scan_delta; +/* Determines how much VMA address space to be covered in the scanning */ +unsigned long scan_size;long address;bool is_scanned; };static inline void kscand_update_mmslot_info(struct
kscand_mm_slot*mm_slot,
mm/kscand.c static void kmigrated_migrate_folio(void) · line 789
Quoted 5 lines
mm/kscand.c +1
-0unsigned long total) {unsigned int scan_period;unsigned long now; +unsigned long scan_size;
Combining a few of these or assigning at declaration will reduce the code size a bit which is always nice to have if it doesn't hurt readability.
Quoted 5 lines
mm/kscand.c +1
-0unsigned long old_scan_delta; + scan_size = mm_slot->scan_size; scan_period = mm_slot->scan_period; old_scan_delta = mm_slot->scan_delta;
RFC PATCH v3 9/17 mm: Add initial scan delay
In reply to Raghavendra K T This is to prevent unnecessary scanning of short lived tasks
to reduce CPU burning.
--- mm/kscand.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
mm/kscand.c +5 -1
@@ -28,6 +28,7 @@static struct task_struct *kscand_thread__read_mostly ;static DEFINE_MUTEX (kscand_mutex); +extern unsigned int sysctl_numa_balancing_scan_delay;/* * Total VMA size to cover during scan.
@@ -1010,6 +1011,7 @@ void __kscand_enter(struct mm_struct *mm) {struct kscand_mm_slot *kscand_slot;struct mm_slot *slot; +unsigned long now;int wakeup;/* __kscand_exit() must not run from under us */
@@ -1020,10 +1022,12 @@ void __kscand_enter(struct mm_struct *mm)if (!kscand_slot)return ; + now = jiffies; kscand_slot->address =0 ; kscand_slot->scan_period = kscand_mm_scan_period_ms; kscand_slot->scan_size = kscand_scan_size; - kscand_slot->next_scan =0 ; + kscand_slot->next_scan = now + +2 *msecs_to_jiffies (sysctl_numa_balancing_scan_delay); kscand_slot->scan_delta =0 ; slot = &kscand_slot->slot;
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 09/17] mm: Add initial scan delay
Quoted 2 lines · On Thu, 14 Aug 2025 15:32:59 +0000 Raghavendra K T <[email protected]>…
This is to prevent unnecessary scanning of short lived tasks
to reduce CPU burning.
Add a tiny bit on why reusing the existing sysctl value. Also useful to add a comment here on current default of that so when considering this algorithm we know what it is.
Quoted 11 lines
Signed-off-by Raghavendra K T <[email protected]>--- mm/kscand.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)mm/kscand.c +0
-0
mm/kscand.c line 31
Quoted 4 lines
mm/kscand.c +1
-0@@ -28,6 +28,7 @@static struct task_struct *kscand_thread__read_mostly ;static DEFINE_MUTEX (kscand_mutex); +extern unsigned int sysctl_numa_balancing_scan_delay;
Maybe we need to move the declaration from kernel/sched/sched.h I'd rather see that than a somewhat hidden away extern declaration.
Quoted 12 lines
mm/kscand.c +1
-0/* * Total VMA size to cover during scan. @@ -1010,6 +1011,7 @@ void __kscand_enter(struct mm_struct *mm) {struct kscand_mm_slot *kscand_slot;struct mm_slot *slot; +unsigned long now;int wakeup;/* __kscand_exit() must not run from under us */
mm/kscand.c void __kscand_enter(struct mm_struct *mm) · line 1030
Quoted 10 lines
mm/kscand.c +3
-1@@ -1020,10 +1022,12 @@ void __kscand_enter(struct mm_struct *mm)if (!kscand_slot)return ; + now = jiffies; kscand_slot->address =0 ; kscand_slot->scan_period = kscand_mm_scan_period_ms; kscand_slot->scan_size = kscand_scan_size; - kscand_slot->next_scan =0 ; + kscand_slot->next_scan = now + +2 *msecs_to_jiffies (sysctl_numa_balancing_scan_delay);
I think one tab beyond line above is enough for indent here.
Quoted 3 lines
mm/kscand.c +0
-0kscand_slot->scan_delta =0 ; slot = &kscand_slot->slot;
RFC PATCH v3 10/17 mm: Add a heuristic to calculate target node
In reply to Raghavendra K TOne of the key challenges in PTE A bit based scanning is to find right target node to promote to.
Here is a simple heuristic based approach:
1. While scanning pages of any mm, also scan toptier pages that belong
to that mm.
2. Accumulate the insight on the distribution of active pages on
toptier nodes.
3. Walk all the top-tier nodes and pick the one with highest accesses.
This method tries to consolidate application to a single node.
TBD: Create a list of preferred nodes for fallback when highest access
node is nearly full.
--- include/linux/mm_types.h | 4 + mm/kscand.c | 198 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 192 insertions(+), 10 deletions(-)
include/linux/mm_types.h +4 -0
@@ -1148,6 +1148,10 @@ struct mm_struct {/* numa_scan_seq prevents two threads remapping PTEs. */ int numa_scan_seq;#endif +#ifdef CONFIG_KSCAND +/* Tracks promotion node. XXX: use nodemask */ +int target_node; +#endif /* * An operation with batched TLB flushing is going on. Anything * that can move process memory needs to flush the TLB when
mm/kscand.c +188 -10
324 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 10/17] mm: Add a heuristic to calculate target node
Quoted 11 lines · On Thu, 14 Aug 2025 15:33:00 +0000 Raghavendra K T <[email protected]>…
One of the key challenges in PTE A bit based scanning is to find right target node to promote to.
Here is a simple heuristic based approach:
1. While scanning pages of any mm, also scan toptier pages that belong
to that mm.
2. Accumulate the insight on the distribution of active pages on
toptier nodes.
3. Walk all the top-tier nodes and pick the one with highest accesses.This method tries to consolidate application to a single node.
Nothing new in the following comment as we've discussed it before, but just to keep everything together:
So for the pathological case of task that has moved after initial allocations are done, this is effectively relying on conventional numa balancing ensuring we don't keep promoting to the wrong node?
That makes me a little nervous. I guess the proof of this will be
in mass testing though. Maybe it works well enough - I have no idea yet!
A few comments inline
Jonathan
Quoted 7 lines
TBD: Create a list of preferred nodes for fallback when highest access
node is nearly full.Signed-off-by Raghavendra K T <[email protected]>+/* Per memory node information used to caclulate
target_nodefor migration */
calculate
mm/kscand.c struct kscand_scan kscand_scan = { · line 129
Quoted 9 lines
mm/kscand.c +7
-0+struct kscand_nodeinfo { +unsigned long nr_scanned; +unsigned long nr_accessed; +int node; +bool is_toptier; +}; +/* * Data structure passed to control scanning and also collect
I'd drop "also". The and implies that already.
Quoted 1 line
* per memory node information
Wrap closer to 80 chars. Also missing full stop.
Quoted 10 lines
*/
structkscand_scanctrl{mm/kscand.c +2
-0struct list_head scan_list; +struct kscand_nodeinfo *nodeinfo[MAX_NUMNODES ];unsigned long address; +unsigned long nr_to_scan; };struct kscand_scanctrl kscand_scanctrl;
mm/kscand.c static void kmigrated_wait_work(void) · line 223
Quoted 6 lines
mm/kscand.c +0
-3@@ -218,15 +229,129 @@ static void kmigrated_wait_work(void) migrate_sleep_jiffies); } -/* -* Do not know what info to pass in the future to make -* decision on taget node. Keep it void * now.
Wrong patch review for this comment but "target"
mm/kscand.c static void kmigrated_wait_work(void) · line 232
Quoted 2 lines
mm/kscand.c +1
-1-*/ +static unsigned long get_slowtier_accesed (struct kscand_scanctrl *scanctrl)
accessed
Quoted 20 lines
mm/kscand.c +20
-0+{ +int node; +unsigned long accessed =0 ; + +for_each_node_state (node,N_MEMORY ) { +if (!node_is_toptier (node) && scanctrl->nodeinfo[node]) + accessed += scanctrl->nodeinfo[node]->nr_accessed; + } +return accessed; +} + +static inline unsigned long get_nodeinfo_nr_accessed (struct kscand_nodeinfo *ni) +{ +return ni->nr_accessed; +} + +static inline void set_nodeinfo_nr_accessed (struct kscand_nodeinfo *ni,unsigned long val) +{ + ni->nr_accessed = val; +}
mm/kscand.c static void kmigrated_wait_work(void) · line 262
Quoted 10 lines
mm/kscand.c +10
-0+ +static inline unsigned long get_nodeinfo_nr_scanned (struct kscand_nodeinfo *ni) +{ +return ni->nr_scanned; +} + +static inline void set_nodeinfo_nr_scanned (struct kscand_nodeinfo *ni,unsigned long val) +{ + ni->nr_scanned = val; +}
These helpers seems unnecessary given they are static, so we have fully visibility of the structure where they are called anyway.
Perhaps they get more complex in later patches though (in which case ignore this comment!)
Quoted 8 lines
mm/kscand.c +8
-0+ +static inline void reset_nodeinfo_nr_scanned (struct kscand_nodeinfo *ni) +{ +set_nodeinfo_nr_scanned (ni,0 ); +} + +static inline void reset_nodeinfo (struct kscand_nodeinfo *ni) +{
mm/kscand.c static void kmigrated_wait_work(void) · line 280
Quoted 10 lines
mm/kscand.c +10
-0+set_nodeinfo_nr_scanned (ni,0 ); +set_nodeinfo_nr_accessed (ni,0 ); +} + +static void init_one_nodeinfo (struct kscand_nodeinfo *ni,int node) +{ + ni->nr_scanned =0 ; + ni->nr_accessed =0 ; + ni->node = node; + ni->is_toptier =node_is_toptier (node) ? true : false;
ni->is_toptier = node_is_toptier(node);
mm/kscand.c static void kmigrated_wait_work(void) · line 292
Quoted 12 lines
mm/kscand.c +12
-0+} + +static struct kscand_nodeinfo *alloc_one_nodeinfo (int node) +{ +struct kscand_nodeinfo *ni; + + ni =kzalloc (sizeof (*ni),GFP_KERNEL ); + +if (!ni) +return NULL ; + +init_one_nodeinfo (ni, node);
As only done in one place, I'd just do an inline
*ni = (struct kscand_node_info) {
.node = node,
.is_toptier = node_is_toptier(node),
Can set the zeros if you think that acts as useful documentation.
};
mm/kscand.c static void kmigrated_wait_work(void) · line 300
Quoted 8 lines
mm/kscand.c +8
-0+ +return ni; +} + +/* TBD: Handle errors */ +static void init_scanctrl (struct kscand_scanctrl *scanctrl) +{ +struct kscand_nodeinfo *ni;
Trivial: I'd move this into the for_each_node scope.
mm/kscand.c static void kmigrated_wait_work(void) · line 303
Quoted 3 lines
mm/kscand.c +3
-0+int node; + +for_each_node (node) {
i.e.
struct kscand_nodeinfo *ni = alloc_one_nodeinfo(node);
mm/kscand.c static void kmigrated_wait_work(void) · line 304
Quoted 1 line
mm/kscand.c +1
-0+ ni =alloc_one_nodeinfo (node);
If this isn't going to get a lot more complex, I'd squash the alloc_one_nodeinfo() code in here and drop the helper. Up to you though as this is a trade off in levels of modularity vs compact code.
Quoted 27 lines
mm/kscand.c +25
-0+if (!ni) +WARN_ON_ONCE (ni); + scanctrl->nodeinfo[node] = ni; + } +} + +static void reset_scanctrl (struct kscand_scanctrl *scanctrl) +{ +int node; + +for_each_node_state (node,N_MEMORY ) +reset_nodeinfo (scanctrl->nodeinfo[node]); + +/* XXX: Not rellay required? */ + scanctrl->nr_to_scan = kscand_scan_size; +} + +static void free_scanctrl (struct kscand_scanctrl *scanctrl) +{ +int node; + +for_each_node (node) +kfree (scanctrl->nodeinfo[node]); +} +static int kscand_get_target_node (void *data) {
mm/kscand.c static void kmigrated_wait_work(void) · line 341
Quoted 10 lines
mm/kscand.c +7
-0return kscand_target_node; } +static int get_target_node (struct kscand_scanctrl *scanctrl) +{ +int node, target_node =NUMA_NO_NODE ; +unsigned long prev =0 ; + +for_each_node (node) { +if (node_is_toptier (node) && scanctrl->nodeinfo[node]) {
Probably flip sense of one or more of the if statements just to reduce indent.
if (!node_is_toptier(node) || !scanctrl->nodeinfo[node]) continue;
etc.
mm/kscand.c static void kmigrated_wait_work(void) · line 344
Quoted 3 lines
mm/kscand.c +3
-0+/* This creates a fallback migration node list */ +if (get_nodeinfo_nr_accessed (scanctrl->nodeinfo[node]) > prev) { + prev =get_nodeinfo_nr_accessed (scanctrl->nodeinfo[node]);
Maybe a local variable given use in check and here.
Quoted 29 lines
mm/kscand.c +18
-0+ target_node = node; + } + } + } +if (target_node ==NUMA_NO_NODE ) + target_node =kscand_get_target_node (NULL ); + +return target_node; +} +extern bool migrate_balanced_pgdat (struct pglist_data *pgdat,unsigned long nr_migrate_pages);@@ -495,6 +620,14 @@ static int hot_vma_idle_pte_entry(pte_t *pte,page_idle_clear_pte_refs (page, pte, walk); srcnid =folio_nid (folio); + scanctrl->nodeinfo[srcnid]->nr_scanned++; +if (scanctrl->nr_to_scan) + scanctrl->nr_to_scan--; + +if (!scanctrl->nr_to_scan) { +folio_put (folio); +return 1 ; + }if (!folio_test_lru (folio)) {folio_put (folio);
mm/kscand.c static int hot_vma_idle_pte_entry(pte_t *pte, · line 639
Quoted 5 lines
mm/kscand.c +2
-0@@ -502,13 +635,17 @@ static int hot_vma_idle_pte_entry(pte_t *pte, }if (!kscand_eligible_srcnid (srcnid)) { +if (folio_test_young (folio) ||folio_test_referenced (folio) + ||pte_young (pteval)) {
Unusual wrap position. I'd move the || to line above and align pte_young() after the ( on the line above.
mm/kscand.c static int hot_vma_idle_pte_entry(pte_t *pte,
Quoted 6 lines
mm/kscand.c +4
-0+ scanctrl->nodeinfo[srcnid]->nr_accessed++; + }folio_put (folio); +/* Either Scan 25% of scan_size or cover vma size of scan_size */ + kscand_scanctrl.nr_to_scan = mm_slot_scan_size >>PAGE_SHIFT ;
Trivial but I'm not sure what you are forcing alignment for here. I'd stick to one space after =
mm/kscand.c static unsigned long kscand_scan_mm_slot(void) · line 1044
Quoted 2 lines
mm/kscand.c +2
-0+/* Reduce actual amount of pages scanned */ + kscand_scanctrl.nr_to_scan = mm_slot_scan_size >>1 ;
If my eyes aren't tricking me this sets the value then immediately replaces it with something else. Is that intent?
Quoted 20 lines
mm/kscand.c +6
-1+ +/* XXX: skip scanning to avoid duplicates until all migrations done? */ kmigrated_mm_slot =kmigrated_get_mm_slot (mm, false);for_each_vma (vmi, vma) {kscand_walk_page_vma (vma, &kscand_scanctrl); vma_scanned_size += vma->vm_end - vma->vm_start; -if (vma_scanned_size >= kscand_scan_size) { +if (vma_scanned_size >= mm_slot_scan_size || + !kscand_scanctrl.nr_to_scan) { next_mm = true;if (!list_empty (&kscand_scanctrl.scan_list)) {if (!kmigrated_mm_slot) kmigrated_mm_slot =kmigrated_get_mm_slot (mm, true); +/* Add scanned folios to migration list */ spin_lock (&kmigrated_mm_slot->migrate_lock); + list_splice_tail_init(&kscand_scanctrl.scan_list,
mm/kscand.c static unsigned long kscand_scan_mm_slot(void) · line 1072
Quoted 10 lines
mm/kscand.c +6
-1&kmigrated_mm_slot->migrate_head);spin_unlock (&kmigrated_mm_slot->migrate_lock); +break ; } -break ; + } +if (!list_empty (&kscand_scanctrl.scan_list)) { +if (!kmigrated_mm_slot) + kmigrated_mm_slot =kmigrated_get_mm_slot (mm, true); +spin_lock (&kmigrated_mm_slot->migrate_lock);
Use of guard() in these might be a useful readability improvement.
mm/kscand.c static unsigned long kscand_scan_mm_slot(void) · line 1075
Quoted 3 lines
mm/kscand.c +3
-0+list_splice_tail_init (&kscand_scanctrl.scan_list, + &kmigrated_mm_slot->migrate_head); +spin_unlock (&kmigrated_mm_slot->migrate_lock);
This code block is identical to the one just above and that breaks out to run this. Do we need them both? Or is there some subtle difference my eyes are jumping over?
Quoted 3 lines
(quoted diff) +0
-0} }
RFC PATCH v3 11/17 mm/kscand: Implement migration failure feedback
In reply to Raghavendra K T Before this, scanning kthread continues to scan even after
migration fails. To control migration, scanning is slowed down based on the failure/success ratio obtained from migration thread.
Decaying failure ratio is maintained for 1024 migration window.
The ratio further contributes to approximately 10% scaling of scan_period.
--- mm/kscand.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)
mm/kscand.c +55 -0
@@ -146,6 +146,8 @@ struct kmigrated_mm_slot {spinlock_t migrate_lock;/* Head of per mm migration list */ struct list_head migrate_head; +/* Indicates weighted success, failure */ +int msuccess, mfailed, fratio; };/* System wide list of mms that maintain migration list */
@@ -812,13 +814,45 @@ static void kscand_collect_mm_slot(struct kscand_mm_slot *mm_slot) } } +static int kmigrated_get_mstat_fratio (struct mm_struct *mm) +{ +int fratio =0 ; +struct kmigrated_mm_slot *mm_slot =NULL ; +struct mm_slot *slot; + +guard (spinlock)(&kscand_migrate_lock); + + slot =mm_slot_lookup (kmigrated_slots_hash, mm); + mm_slot = mm_slot_entry(slot, struct kmigrated_mm_slot, mm_slot); + +if (mm_slot) + fratio = mm_slot->fratio; + +return fratio; +} + +static void update_mstat_ratio (struct kmigrated_mm_slot *mm_slot, +int msuccess,int mfailed) +{ + mm_slot->msuccess = (mm_slot->msuccess >>2 ) + msuccess; + mm_slot->mfailed = (mm_slot->mfailed >>2 ) + mfailed; + mm_slot->fratio = mm_slot->mfailed *100 ; + mm_slot->fratio /= (mm_slot->msuccess + mm_slot->mfailed); +} + +#define MSTAT_UPDATE_FREQ 1024 +static void kmigrated_migrate_mm (struct kmigrated_mm_slot *mm_slot) { +int mfailed =0 ; +int msuccess =0 ; +int mstat_counter;int ret =0 , dest =-1 ;struct mm_slot *slot;struct mm_struct *mm;struct kscand_migrate_info *info, *tmp; + mstat_counter =MSTAT_UPDATE_FREQ ;spin_lock (&mm_slot->migrate_lock); slot = &mm_slot->mm_slot;
@@ -842,11 +876,23 @@ static void kmigrated_migrate_mm(struct kmigrated_mm_slot *mm_slot) } ret =kmigrated_promote_folio (info, mm, dest); + mstat_counter--; + +/* TBD: encode migrated count here, currently assume folio_nr_pages */ +if (!ret) + msuccess++; +else + mfailed++;kfree (info);cond_resched ();spin_lock (&mm_slot->migrate_lock); +if (!mstat_counter) { +update_mstat_ratio (mm_slot, msuccess, mfailed); + msuccess = mfailed =0 ; + mstat_counter =MSTAT_UPDATE_FREQ ; + } } } clean_list_handled:
@@ -882,6 +928,12 @@ static void kmigrated_migrate_folio(void) } } +/* Get scan_period based on migration failure statistics */ +static int kscand_mstat_scan_period (unsigned int scan_period,int fratio) +{ +return scan_period * (1 + fratio /10 ); +} +/* * This is the normal change percentage when old and new delta remain same. * i.e., either both positive or both zero.
@@ -928,6 +980,7 @@ static void kmigrated_migrate_folio(void)static inline void kscand_update_mmslot_info (struct kscand_mm_slot *mm_slot,unsigned long total,int target_node) { +int fratio;unsigned int scan_period;unsigned long now;unsigned long scan_size;
@@ -967,6 +1020,8 @@ static inline void kscand_update_mmslot_info(struct kscand_mm_slot *mm_slot, } scan_period =clamp (scan_period,KSCAND_SCAN_PERIOD_MIN ,KSCAND_SCAN_PERIOD_MAX ); + fratio =kmigrated_get_mstat_fratio ((&mm_slot->slot)->mm); + scan_period =kscand_mstat_scan_period (scan_period, fratio); scan_size =clamp (scan_size,KSCAND_SCAN_SIZE_MIN ,KSCAND_SCAN_SIZE_MAX ); now = jiffies;
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 11/17] mm/kscand: Implement migration failure feedback
Quoted 8 lines · On Thu, 14 Aug 2025 15:33:01 +0000 Raghavendra K T <[email protected]>…
Before this, scanning kthread continues to scan even after
migration fails. To control migration, scanning is slowed down based on the failure/success ratio obtained from migration thread.Decaying failure ratio is maintained for 1024 migration window.
The ratio further contributes to approximately 10% scaling ofscan_period.
Perhaps it's worth adding a cover letter section describing all the heuristics briefly so we have a central place to understand what needs tuning against workloads before this merges?
J
Quoted 26 lines
Signed-off-by Raghavendra K T <[email protected]>--- mm/kscand.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+)mm/kscand.c +5
-0@@ -146,6 +146,8 @@ struct kmigrated_mm_slot {spinlock_t migrate_lock;/* Head of per mm migration list */ struct list_head migrate_head; +/* Indicates weighted success, failure */ +int msuccess, mfailed, fratio; };/* System wide list of mms that maintain migration list */ @@ -812,13 +814,45 @@ static void kscand_collect_mm_slot(struct kscand_mm_slot *mm_slot) } } +static int kmigrated_get_mstat_fratio (struct mm_struct *mm) +{ +int fratio =0 ;
mm/kscand.c static void kscand_collect_mm_slot(struct kscand_mm_slot *mm_slot) · line 829
Quoted 10 lines
mm/kscand.c +10
-0+struct kmigrated_mm_slot *mm_slot =NULL ; +struct mm_slot *slot; + +guard (spinlock)(&kscand_migrate_lock); + + slot =mm_slot_lookup (kmigrated_slots_hash, mm); + mm_slot = mm_slot_entry(slot, struct kmigrated_mm_slot, mm_slot); + +if (mm_slot) + fratio = mm_slot->fratio;
Extra space after =
mm/kscand.c static void kscand_collect_mm_slot(struct kscand_mm_slot *mm_slot) · line 840
Quoted 11 lines
mm/kscand.c +11
-0+ +return fratio; +} + +static void update_mstat_ratio (struct kmigrated_mm_slot *mm_slot, +int msuccess,int mfailed) +{ + mm_slot->msuccess = (mm_slot->msuccess >>2 ) + msuccess; + mm_slot->mfailed = (mm_slot->mfailed >>2 ) + mfailed; + mm_slot->fratio = mm_slot->mfailed *100 ; + mm_slot->fratio /= (mm_slot->msuccess + mm_slot->mfailed);
extra space after =
Quoted 5 lines
mm/kscand.c +4
-0+} + +#define MSTAT_UPDATE_FREQ 1024 +static void kmigrated_migrate_mm (struct kmigrated_mm_slot *mm_slot)
mm/kscand.c static void kscand_collect_mm_slot(struct kscand_mm_slot *mm_slot) · line 855
Quoted 10 lines
mm/kscand.c +4
-0{ +int mfailed =0 ; +int msuccess =0 ; +int mstat_counter;int ret =0 , dest =-1 ;struct mm_slot *slot;struct mm_struct *mm;struct kscand_migrate_info *info, *tmp; + mstat_counter =MSTAT_UPDATE_FREQ ;
Might as well set at declaration above.
Quoted 12 lines
mm/kscand.c +5
-0spin_lock (&mm_slot->migrate_lock); slot = &mm_slot->mm_slot;@@ -842,11 +876,23 @@ static void kmigrated_migrate_mm(struct kmigrated_mm_slot *mm_slot) } ret =kmigrated_promote_folio (info, mm, dest); + mstat_counter--; + +/* TBD: encode migrated count here, currently assume folio_nr_pages */ +if (!ret) + msuccess++;
mm/kscand.c static void kmigrated_migrate_mm(struct kmigrated_mm_slot *mm_slot) · line 893
Quoted 10 lines
mm/kscand.c +5
-0+else + mfailed++;kfree (info);cond_resched ();spin_lock (&mm_slot->migrate_lock); +if (!mstat_counter) { +update_mstat_ratio (mm_slot, msuccess, mfailed); + msuccess = mfailed =0 ;
extra space before =
Quoted 4 lines
mm/kscand.c +2
-0+ mstat_counter =MSTAT_UPDATE_FREQ ; + } } }
RFC PATCH v3 12/17 sysfs: Add sysfs support to tune scanning
In reply to Raghavendra K TSupport below tunables:scan_enable: turn on or turn off mm_struct scanningscan_period: initial scan_period (default: 2sec)scan_sleep_ms: sleep time between two successive round of scanning and migration.mms_to_scan: total mm_struct to scan before taking a pause.target_node: default regular node to which migration of accessed pages is done (this is only fall back mechnism, otherwise target_node heuristic is used).
--- mm/kscand.c | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+)
mm/kscand.c +205 -0
@@ -21,6 +21,7 @@#include <linux/delay.h> #include <linux/cleanup.h> #include <linux/minmax.h> +#include <trace/events/kmem.h> #include <asm/pgalloc.h> #include "internal.h"
@@ -173,6 +174,171 @@ static bool kscand_eligible_srcnid(int nid)return !node_is_toptier (nid); } +#ifdef CONFIG_SYSFS +static ssize_t scan_sleep_ms_show (struct kobject *kobj, +struct kobj_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"%u\n" , kscand_scan_sleep_ms); +} + +static ssize_t scan_sleep_ms_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +unsigned int msecs; +int err; + + err =kstrtouint (buf,10 , &msecs); +if (err) +return -EINVAL ; + + kscand_scan_sleep_ms = msecs; + kscand_sleep_expire =0 ; +wake_up_interruptible (&kscand_wait); + +return count; +} + +static struct kobj_attribute scan_sleep_ms_attr = +__ATTR_RW (scan_sleep_ms); + +static ssize_t mm_scan_period_ms_show (struct kobject *kobj, +struct kobj_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"%u\n" , kscand_mm_scan_period_ms); +} + +/* If a value less than MIN or greater than MAX asked for store value is clamped */ +static ssize_t mm_scan_period_ms_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +unsigned int msecs, stored_msecs; +int err; + + err =kstrtouint (buf,10 , &msecs); +if (err) +return -EINVAL ; + + stored_msecs =clamp (msecs,KSCAND_SCAN_PERIOD_MIN ,KSCAND_SCAN_PERIOD_MAX ); + + kscand_mm_scan_period_ms = stored_msecs; + kscand_sleep_expire =0 ; +wake_up_interruptible (&kscand_wait); + +return count; +} + +static struct kobj_attribute mm_scan_period_ms_attr = +__ATTR_RW (mm_scan_period_ms); + +static ssize_t mms_to_scan_show (struct kobject *kobj, +struct kobj_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"%lu\n" , kscand_mms_to_scan); +} + +static ssize_t mms_to_scan_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +unsigned long val; +int err; + + err =kstrtoul (buf,10 , &val); +if (err) +return -EINVAL ; + + kscand_mms_to_scan = val; + kscand_sleep_expire =0 ; +wake_up_interruptible (&kscand_wait); + +return count; +} + +static struct kobj_attribute mms_to_scan_attr = +__ATTR_RW (mms_to_scan); + +static ssize_t scan_enabled_show (struct kobject *kobj, +struct kobj_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"%u\n" , kscand_scan_enabled ?1 :0 ); +} + +static ssize_t scan_enabled_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +unsigned int val; +int err; + + err =kstrtouint (buf,10 , &val); +if (err || val >1 ) +return -EINVAL ; + +if (val) { + kscand_scan_enabled = true; + need_wakeup = true; + }else + kscand_scan_enabled = false; + + kscand_sleep_expire =0 ; +wake_up_interruptible (&kscand_wait); + +return count; +} + +static struct kobj_attribute scan_enabled_attr = +__ATTR_RW (scan_enabled); + +static ssize_t target_node_show (struct kobject *kobj, +struct kobj_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"%u\n" , kscand_target_node); +} + +static ssize_t target_node_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +int err, node; + + err =kstrtoint (buf,10 , &node); +if (err) +return -EINVAL ; + + kscand_sleep_expire =0 ; +if (!node_is_toptier (node)) +return -EINVAL ; + + kscand_target_node = node; +wake_up_interruptible (&kscand_wait); + +return count; +} +static struct kobj_attribute target_node_attr = +__ATTR_RW (target_node); + +static struct attribute *kscand_attr[] = { + &scan_sleep_ms_attr.attr, + &mm_scan_period_ms_attr.attr, + &mms_to_scan_attr.attr, + &scan_enabled_attr.attr, + &target_node_attr.attr, +NULL , +}; + +struct attribute_group kscand_attr_group = { + .attrs = kscand_attr, + .name ="kscand" , +}; +#endif +static inline int kscand_has_work (void ) {return !list_empty (&kscand_scan.mm_head);
@@ -1231,11 +1397,45 @@ static int kscand(void *none)return 0 ; } +#ifdef CONFIG_SYSFS +extern struct kobject *mm_kobj; +static int __init kscand_init_sysfs (struct kobject **kobj) +{ +int err; + + err =sysfs_create_group (*kobj, &kscand_attr_group); +if (err) { +pr_err ("failed to register kscand group\n" ); + goto err_kscand_attr; + } + +return 0 ; + +err_kscand_attr: +sysfs_remove_group (*kobj, &kscand_attr_group); +return err; +} + +static void __init kscand_exit_sysfs (struct kobject *kobj) +{ +sysfs_remove_group (kobj, &kscand_attr_group); +} +#else +static inline int __init kscand_init_sysfs (struct kobject **kobj) +{ +return 0 ; +} +static inline void __init kscand_exit_sysfs (struct kobject *kobj) +{ +} +#endif +static inline void kscand_destroy (void ) {kmem_cache_destroy (kscand_slot_cache);/* XXX: move below to kmigrated thread */ kmem_cache_destroy (kmigrated_slot_cache); +kscand_exit_sysfs (mm_kobj); }void __kscand_enter (struct mm_struct *mm)
@@ -1421,6 +1621,10 @@ static int __init kscand_init(void)return -ENOMEM ; } + err =kscand_init_sysfs (&mm_kobj); +if (err) + goto err_init_sysfs; +init_list (); err =start_kscand ();if (err)
@@ -1437,6 +1641,7 @@ static int __init kscand_init(void) err_kscand:stop_kscand (); +err_init_sysfs:kscand_destroy ();return err;
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 12/17] sysfs: Add sysfs support to tune scanning
Quoted 11 lines · On Thu, 14 Aug 2025 15:33:02 +0000 Raghavendra K T <[email protected]>…
Support below tunables:
scan_enable: turn on or turn offmm_structscanningscan_period: initialscan_period(default: 2sec)scan_sleep_ms: sleep time between two successive round of scanning and migration.mms_to_scan: totalmm_structto scan before taking a pause.target_node: default regular node to which migration of accessed pages is done (this is only fall back mechnism, otherwisetarget_nodeheuristic is used).Signed-off-by Raghavendra K T <[email protected]>
I'd suggest writingDocumentation/ABI/testing/sysfs-...
doc for this as you'll need it in the end and it tends to be easier to review a doc for this stuff than the code + figuring out where the entrees end up.
Various comments inline.
J
Quoted 17 lines
--- mm/kscand.c | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+)mm/kscand.c +1
-0@@ -21,6 +21,7 @@#include <linux/delay.h> #include <linux/cleanup.h> #include <linux/minmax.h> +#include <trace/events/kmem.h> #include <asm/pgalloc.h> #include "internal.h"
mm/kscand.c static bool kscand_eligible_srcnid(int nid) · line 177
Quoted 4 lines
mm/kscand.c +1
-0@@ -173,6 +174,171 @@ static bool kscand_eligible_srcnid(int nid)return !node_is_toptier (nid); } +#ifdef CONFIG_SYSFS
See below. Should not be necessary - the compiler should be able to see these are unused after it squashes the stubs for sysfs calls in and hence remove this for us.
mm/kscand.c static bool kscand_eligible_srcnid(int nid) · line 204
Quoted 3 lines
mm/kscand.c +3
-0+ +static struct kobj_attribute scan_sleep_ms_attr = +__ATTR_RW (scan_sleep_ms);
Fits on one line under 80 chars.
mm/kscand.c static bool kscand_eligible_srcnid(int nid) · line 263
Quoted 12 lines
mm/kscand.c +10
-0+ +static ssize_t mm_scan_period_ms_show (struct kobject *kobj, +struct kobj_attribute *attr, +char *buf) +{ +return sysfs_emit (buf,"%u\n" , kscand_mm_scan_period_ms); +} + +static struct kobj_attribute mms_to_scan_attr = +__ATTR_RW (mms_to_scan);
Fits on one line.
mm/kscand.c static bool kscand_eligible_srcnid(int nid) · line 267
Quoted 4 lines
mm/kscand.c +4
-0+ +static ssize_t scan_enabled_show (struct kobject *kobj, +struct kobj_attribute *attr, +char *buf)
Odd indent.
mm/kscand.c static bool kscand_eligible_srcnid(int nid) · line 274
Quoted 7 lines
mm/kscand.c +7
-0+{ +return sysfs_emit (buf,"%u\n" , kscand_scan_enabled ?1 :0 ); +} + +static ssize_t scan_enabled_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count)
Another odd looking indent.
mm/kscand.c static bool kscand_eligible_srcnid(int nid) · line 279
Quoted 5 lines
mm/kscand.c +5
-0+{ +unsigned int val; +int err; + + err =kstrtouint (buf,10 , &val);
Maybe use kstrtobool
Quoted 7 lines
mm/kscand.c +7
-0+if (err || val >1 ) +return -EINVAL ; + +if (val) { + kscand_scan_enabled = true; + need_wakeup = true; + }else
mm/kscand.c static bool kscand_eligible_srcnid(int nid) · line 296
Quoted 10 lines
mm/kscand.c +10
-0+ kscand_scan_enabled = false; + + kscand_sleep_expire =0 ; +wake_up_interruptible (&kscand_wait); + +return count; +} + +static struct kobj_attribute scan_enabled_attr = +__ATTR_RW (scan_enabled);
One line.
mm/kscand.c static bool kscand_eligible_srcnid(int nid) · line 325
Quoted 5 lines
mm/kscand.c +4
-0+ +} +static struct kobj_attribute target_node_attr = +__ATTR_RW (target_node);
One line.
mm/kscand.c static bool kscand_eligible_srcnid(int nid) · line 333
Quoted 8 lines
mm/kscand.c +8
-0+ +static struct attribute *kscand_attr[] = { + &scan_sleep_ms_attr.attr, + &mm_scan_period_ms_attr.attr, + &mms_to_scan_attr.attr, + &scan_enabled_attr.attr, + &target_node_attr.attr, +NULL ,
No comma for terminating entries as we don't want to add anything after this.
Quoted 12 lines
mm/kscand.c +8
-0+}; + +struct attribute_group kscand_attr_group = { + .attrs = kscand_attr, + .name ="kscand" , +}; +#endif +static inline int kscand_has_work (void ) {return !list_empty (&kscand_scan.mm_head);
mm/kscand.c static int kscand(void *none) · line 1400
Quoted 4 lines
mm/kscand.c +1
-0@@ -1231,11 +1397,45 @@ static int kscand(void *none)return 0 ; } +#ifdef CONFIG_SYSFS
The functions used have stubs in sysfs.h so no need to make these conditional on CONFIG_SYSFS.
Let the compilers dead code removal get rid of the structures above etc as well.
Quoted 4 lines
mm/kscand.c +4
-0+extern struct kobject *mm_kobj; +static int __init kscand_init_sysfs (struct kobject **kobj) +{ +int err;
mm/kscand.c static int kscand(void *none) · line 1414
Quoted 10 lines
mm/kscand.c +10
-0+ + err =sysfs_create_group (*kobj, &kscand_attr_group); +if (err) { +pr_err ("failed to register kscand group\n" ); + goto err_kscand_attr; + } + +return 0 ; + +err_kscand_attr:
If create group failed, you shouldn't have anything to remove.
mm/kscand.c static int kscand(void *none) · line 1421
Quoted 7 lines
mm/kscand.c +7
-0+sysfs_remove_group (*kobj, &kscand_attr_group); +return err; +} + +static void __init kscand_exit_sysfs (struct kobject *kobj) +{ +sysfs_remove_group (kobj, &kscand_attr_group);
Odd indent.
Quoted 7 lines
mm/kscand.c +7
-0+} +#else +static inline int __init kscand_init_sysfs (struct kobject **kobj) +{ +return 0 ; +} +static inline void __init kscand_exit_sysfs (struct kobject *kobj)
mm/kscand.c static int kscand(void *none) · line 1438
Quoted 10 lines
mm/kscand.c +5
-0+{ +} +#endif +static inline void kscand_destroy (void ) {kmem_cache_destroy (kscand_slot_cache);/* XXX: move below to kmigrated thread */ kmem_cache_destroy (kmigrated_slot_cache); +kscand_exit_sysfs (mm_kobj);
As it's separate setup step, I'd expect to see this called directly in error paths in kscand_init() rather than via this helper function. That way it is easy to see it is only called in paths where it is appropriate.
Quoted 22 lines
mm/kscand.c +5
-0}void __kscand_enter (struct mm_struct *mm)@@ -1421,6 +1621,10 @@ static int __init kscand_init(void)return -ENOMEM ; } + err =kscand_init_sysfs (&mm_kobj); +if (err) + goto err_init_sysfs; +init_list (); err =start_kscand ();if (err)@@ -1437,6 +1641,7 @@ static int __init kscand_init(void) err_kscand:stop_kscand (); +err_init_sysfs:kscand_destroy ();return err;
RFC PATCH v3 13/17 mm/vmstat: Add vmstat counters
In reply to Raghavendra K TAdd vmstat counter to track scanning, migration and type of pages.
--- include/linux/mm.h | 13 ++++++++ include/linux/vm_event_item.h | 12 +++++++ mm/kscand.c | 63 +++++++++++++++++++++++++++++++++-- mm/vmstat.c | 12 +++++++ 4 files changed, 98 insertions(+), 2 deletions(-)
include/linux/mm.h +13 -0
@@ -655,6 +655,19 @@ struct vm_operations_struct { unsigned long addr); }; +#ifdef CONFIG_KSCAND +void count_kscand_mm_scans (void ); +void count_kscand_vma_scans (void ); +void count_kscand_migadded (void ); +void count_kscand_migrated (void ); +void count_kscand_migrate_failed (void ); +void count_kscand_slowtier (void ); +void count_kscand_toptier (void ); +void count_kscand_idlepage (void ); +void count_kscand_hotpage (void ); +void count_kscand_coldpage (void ); +#endif +#ifdef CONFIG_NUMA_BALANCING static inline void vma_numab_state_init (struct vm_area_struct *vma) {
include/linux/vm_event_item.h +12 -0
@@ -67,6 +67,18 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, NUMA_HINT_FAULTS_LOCAL, NUMA_PAGE_MIGRATE,#endif +#ifdef CONFIG_KSCAND +KSCAND_MM_SCANS , +KSCAND_VMA_SCANS , +KSCAND_MIGADDED , +KSCAND_MIGRATED , +KSCAND_MIGRATE_FAILED , +KSCAND_SLOWTIER , +KSCAND_TOPTIER , +KSCAND_IDLEPAGE , +KSCAND_HOTPAGE , +KSCAND_COLDPAGE , +#endif #ifdef CONFIG_MIGRATIONPGMIGRATE_SUCCESS ,PGMIGRATE_FAIL , THP_MIGRATION_SUCCESS,
mm/kscand.c +61 -2
114 lines not on this page · raw message
mm/vmstat.c +12 -0
@@ -1347,6 +1347,18 @@ const char * const vmstat_text[] = {"numa_hint_faults_local" ,"numa_pages_migrated" , #endif +#ifdef CONFIG_KSCAND +"nr_kscand_mm_scans" , +"nr_kscand_vma_scans" , +"nr_kscand_migadded" , +"nr_kscand_migrated" , +"nr_kscand_migrate_failed" , +"nr_kscand_slowtier" , +"nr_kscand_toptier" , +"nr_kscand_idlepage" , +"nr_kscand_hotpage" , +"nr_kscand_coldpage" , +#endif #ifdef CONFIG_MIGRATION"pgmigrate_success" , "pgmigrate_fail",
Signature · 1 line
2.34.1
RFC PATCH v3 14/17 trace/kscand: Add tracing of scanning and migration
In reply to Raghavendra K TAdd tracing support to track
- start and end of scanning.
- migration.
--- include/trace/events/kmem.h | 99 +++++++++++++++++++++++++++++++++++++ mm/kscand.c | 9 ++++ 2 files changed, 108 insertions(+)
include/trace/events/kmem.h +99 -0
@@ -9,6 +9,105 @@#include <linux/tracepoint.h> #include <trace/events/mmflags.h> +#ifdef CONFIG_KSCAND +DECLARE_EVENT_CLASS (kmem_mm_class, + + TP_PROTO(struct mm_struct *mm), + +TP_ARGS (mm), + +TP_STRUCT__entry ( + __field( struct mm_struct *, mm ) + ), + +TP_fast_assign ( + __entry->mm = mm; + ), + +TP_printk ("mm = %p" , __entry->mm) +); + +DEFINE_EVENT (kmem_mm_class, kmem_mm_enter, + TP_PROTO(struct mm_struct *mm), +TP_ARGS (mm) +); + +DEFINE_EVENT (kmem_mm_class, kmem_mm_exit, + TP_PROTO(struct mm_struct *mm), +TP_ARGS (mm) +); + +DEFINE_EVENT (kmem_mm_class, kmem_scan_mm_start, + TP_PROTO(struct mm_struct *mm), +TP_ARGS (mm) +); + +TRACE_EVENT (kmem_scan_mm_end, + + TP_PROTO( struct mm_struct *mm, + unsigned long start, + unsigned long total, + unsigned long scan_period, + unsigned long scan_size, + int target_node), + +TP_ARGS (mm, start, total, scan_period, scan_size, target_node), + +TP_STRUCT__entry ( + __field( struct mm_struct *, mm ) + __field( unsigned long, start ) + __field( unsigned long, total ) + __field( unsigned long, scan_period ) + __field( unsigned long, scan_size ) +__field ( int, target_node ) + ), + +TP_fast_assign ( + __entry->mm = mm; + __entry->start = start; + __entry->total = total; + __entry->scan_period = scan_period; + __entry->scan_size = scan_size; + __entry->target_node = target_node; + ), + +TP_printk ("mm=%p, start = %ld, total = %ld, scan_period = %ld, scan_size = %ld node = %d" , + __entry->mm, __entry->start, __entry->total, __entry->scan_period, + __entry->scan_size, __entry->target_node) +); + +TRACE_EVENT (kmem_scan_mm_migrate, + + TP_PROTO(struct mm_struct *mm, + int rc, + int target_node, + int msuccess, + int mfailed), + +TP_ARGS (mm, rc, target_node, msuccess, mfailed), + +TP_STRUCT__entry ( + __field( struct mm_struct *, mm ) + __field( int, rc ) + __field( int, target_node ) + __field( int, msuccess ) +__field ( int, mfailed ) + ), + +TP_fast_assign ( + __entry->mm = mm; + __entry->rc = rc; + __entry->target_node = target_node; + __entry->msuccess = msuccess; + __entry->mfailed = mfailed; + ), + +TP_printk ("mm = %p rc = %d node = %d msuccess = %d mfailed = %d " , + __entry->mm, __entry->rc, __entry->target_node, + __entry->msuccess, __entry->mfailed) +); +#endif + TRACE_EVENT(kmem_cache_alloc, TP_PROTO(unsigned long call_site,
mm/kscand.c +9 -0
@@ -1105,6 +1105,7 @@ static void kmigrated_migrate_mm(struct kmigrated_mm_slot *mm_slot)count_kscand_migrate_failed (); mfailed++; } +trace_kmem_scan_mm_migrate (mm, ret, dest, msuccess, mfailed);kfree (info);
@@ -1308,6 +1309,9 @@ static unsigned long kscand_scan_mm_slot(void) mm_target_node =READ_ONCE (mm->target_node);if (mm_target_node != mm_slot_target_node)WRITE_ONCE (mm->target_node, mm_slot_target_node); + +trace_kmem_scan_mm_start (mm); + now = jiffies;if (mm_slot_next_scan &&time_before (now, mm_slot_next_scan))
@@ -1378,6 +1382,9 @@ static unsigned long kscand_scan_mm_slot(void)kscand_update_mmslot_info (mm_slot, total, target_node); } +trace_kmem_scan_mm_end (mm, address, total, mm_slot_scan_period, + mm_slot_scan_size, target_node); + outerloop:/* exit_mmap will destroy ptes after this */ mmap_read_unlock (mm);
@@ -1530,6 +1537,7 @@ void __kscand_enter(struct mm_struct *mm)spin_unlock (&kscand_mm_lock);mmgrab (mm); +trace_kmem_mm_enter (mm);if (wakeup)wake_up_interruptible (&kscand_wait); }
@@ -1540,6 +1548,7 @@ void __kscand_exit(struct mm_struct *mm)struct mm_slot *slot;int free =0 , serialize =1 ; +trace_kmem_mm_exit (mm);spin_lock (&kscand_mm_lock); slot =mm_slot_lookup (kscand_slots_hash, mm); mm_slot = mm_slot_entry(slot, struct kscand_mm_slot, slot);
Signature · 1 line
2.34.1
Re: [RFC PATCH V3 14/17] trace/kscand: Add tracing of scanning and migration
Quoted 8 lines · On Thu, 14 Aug 2025 15:33:04 +0000 Raghavendra K T <[email protected]>…
Add tracing support to track
- start and end of scanning.
- migration.
CC s are part of tags block so no blank line.
Probably move them under the --- as I doubt we need to keep these in the git log long term.
Quoted 13 lines
Signed-off-by Raghavendra K T <[email protected]>--- include/trace/events/kmem.h | 99 +++++++++++++++++++++++++++++++++++++ mm/kscand.c | 9 ++++ 2 files changed, 108 insertions(+)include/trace/events/kmem.h +0
-0@@ -9,6 +9,105 @@#include <linux/tracepoint.h> #include <trace/events/mmflags.h>
include/trace/events/kmem.h line 20
Quoted 10 lines
include/trace/events/kmem.h +9
-0+#ifdef CONFIG_KSCAND +DECLARE_EVENT_CLASS(kmem_mm_class, + + TP_PROTO(struct mm_struct *mm), + + TP_ARGS(mm), + + TP_STRUCT__entry( + __field( struct mm_struct *, mm )
Trace header formatting is sometimes interesting. But I have no idea why you have this padded like that.
Quoted 32 lines
include/trace/events/kmem.h +32
-0+ ), + +TP_fast_assign ( + __entry->mm = mm; + ), + +TP_printk ("mm = %p" , __entry->mm) +); + +DEFINE_EVENT (kmem_mm_class, kmem_mm_enter, + TP_PROTO(struct mm_struct *mm), +TP_ARGS (mm) +); + +DEFINE_EVENT (kmem_mm_class, kmem_mm_exit, + TP_PROTO(struct mm_struct *mm), +TP_ARGS (mm) +); + +DEFINE_EVENT (kmem_mm_class, kmem_scan_mm_start, + TP_PROTO(struct mm_struct *mm), +TP_ARGS (mm) +); + +TRACE_EVENT(kmem_scan_mm_end, + + TP_PROTO( struct mm_struct *mm, + unsigned long start, + unsigned long total, + unsigned long scan_period, + unsigned long scan_size, + int target_node),
include/trace/events/kmem.h line 62
Quoted 10 lines
include/trace/events/kmem.h +10
-0+ +TP_ARGS (mm, start, total, scan_period, scan_size, target_node), + +TP_STRUCT__entry ( + __field( struct mm_struct *, mm ) + __field( unsigned long, start ) + __field( unsigned long, total ) + __field( unsigned long, scan_period ) + __field( unsigned long, scan_size ) + __field( int, target_node )
Similar. Aligning stuff might make sense but why the spacing before the type?
RFC PATCH v3 15/17 prctl: Introduce new prctl to control scanning
In reply to Raghavendra K T A new scalar value (PTEAScanScale) to control per task PTE A bit scanning
is introduced.
0 : scanning disabled
1-10 : scanning enabled.
In future PTEAScanScale could be used to control aggressiveness of scanning.
--- Documentation/filesystems/proc.rst | 2 ++ fs/proc/task_mmu.c | 4 ++++ include/linux/mm_types.h | 3 +++ include/uapi/linux/prctl.h | 7 +++++++ kernel/fork.c | 4 ++++ kernel/sys.c | 25 +++++++++++++++++++++++++ mm/kscand.c | 5 +++++ 7 files changed, 50 insertions(+)
Documentation/filesystems/proc.rst +2 -0
@@ -205,6 +205,7 @@ read the file /proc/PID/status:: VmLib: 1412 kB VmPTE: 20 kb VmSwap: 0 kB + PTEAScanScale: 0 HugetlbPages: 0 kB CoreDumping: 0 THP_enabled: 1
@@ -288,6 +289,7 @@ It's slow but very precise. VmPTE size of page table entries VmSwap amount of swap used by anonymous private data (shmem swap usage is not included) + PTEAScanScale Integer representing async PTE A bit scan agrression HugetlbPages size of hugetlb memory portions CoreDumping process's memory is currently being dumped (killing the process may lead to a corrupted core)
fs/proc/task_mmu.c +4 -0
@@ -79,6 +79,10 @@ void task_mem(struct seq_file *m, struct mm_struct *mm) " kB\nVmPTE:\t", mm_pgtables_bytes(mm) >> 10, 8);SEQ_PUT_DEC (" kB\nVmSwap:\t" , swap);seq_puts (m," kB\n" ); +#ifdef CONFIG_KSCAND +seq_put_decimal_ull_width (m,"PTEAScanScale:\t" , mm->pte_scan_scale,8 ); +seq_puts (m,"\n" ); +#endif hugetlb_report_usage (m, mm); }#undef SEQ_PUT_DEC
include/linux/mm_types.h +3 -0
@@ -1151,6 +1151,9 @@ struct mm_struct {#ifdef CONFIG_KSCAND /* Tracks promotion node. XXX: use nodemask */ int target_node; + +/* Integer representing PTE A bit scan aggression (0-10) */ +unsigned int pte_scan_scale;#endif /* * An operation with batched TLB flushing is going on. Anything
include/uapi/linux/prctl.h +7 -0
@@ -371,4 +371,11 @@ struct prctl_mm_map {# define PR_FUTEX_HASH_GET_SLOTS 2# define PR_FUTEX_HASH_GET_IMMUTABLE 3 +/* Set/get PTE A bit scan scale */ +#define PR_SET_PTE_A_SCAN_SCALE 79 +#define PR_GET_PTE_A_SCAN_SCALE 80 +# define PR_PTE_A_SCAN_SCALE_MIN 0 +# define PR_PTE_A_SCAN_SCALE_MAX 10 +# define PR_PTE_A_SCAN_SCALE_DEFAULT 8 +#endif /* _LINUX_PRCTL_H */
kernel/fork.c +4 -0
@@ -106,6 +106,7 @@#include <uapi/linux/pidfd.h> #include <linux/pidfs.h> #include <linux/tick.h> +#include <linux/prctl.h> #include <asm/pgalloc.h> #include <linux/uaccess.h>
@@ -1050,6 +1051,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,futex_mm_init (mm);#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && !defined(CONFIG_SPLIT_PMD_PTLOCKS ) mm->pmd_huge_pte =NULL ; +#endif +#ifdef CONFIG_KSCAND + mm->pte_scan_scale =PR_PTE_A_SCAN_SCALE_DEFAULT ;#endif mm_init_uprobes_state (mm);hugetlb_count_init (mm);
kernel/sys.c +25 -0
@@ -2147,6 +2147,19 @@ static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,return 0 ; } +#ifdef CONFIG_KSCAND +static int prctl_pte_scan_scale_write (unsigned int scale) +{ + scale =clamp (scale,PR_PTE_A_SCAN_SCALE_MIN ,PR_PTE_A_SCAN_SCALE_MAX ); + current->mm->pte_scan_scale = scale; +return 0 ; +} + +static unsigned int prctl_pte_scan_scale_read (void ) +{ +return current->mm->pte_scan_scale; +} +#endif static int prctl_set_mm (int opt,unsigned long addr,unsigned long arg4,unsigned long arg5)
@@ -2824,6 +2837,18 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,case PR_FUTEX_HASH : error =futex_hash_prctl (arg2, arg3, arg4);break ; +#ifdef CONFIG_KSCAND +case PR_SET_PTE_A_SCAN_SCALE : +if (arg3 || arg4 || arg5) +return -EINVAL ; + error =prctl_pte_scan_scale_write ((unsigned int ) arg2); +break ; +case PR_GET_PTE_A_SCAN_SCALE : +if (arg2 || arg3 || arg4 || arg5) +return -EINVAL ; + error =prctl_pte_scan_scale_read (); +break ; +#endif default :trace_task_prctl_unknown (option, arg2, arg3, arg4, arg5); error = -EINVAL ;
mm/kscand.c +5 -0
@@ -1306,6 +1306,11 @@ static unsigned long kscand_scan_mm_slot(void) goto outerloop; } +if (!mm->pte_scan_scale) { + next_mm = true; + goto outerloop; + } + mm_target_node =READ_ONCE (mm->target_node);if (mm_target_node != mm_slot_target_node)WRITE_ONCE (mm->target_node, mm_slot_target_node);
Signature · 1 line
2.34.1
RFC PATCH v3 16/17 prctl: Fine tune scan_period with prctl scale param
In reply to Raghavendra K T Absolute value of pte_scan_scale param further tunesscan_period by 20%.
--- mm/kscand.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-)
mm/kscand.c +25 -2
@@ -21,6 +21,7 @@#include <linux/delay.h> #include <linux/cleanup.h> #include <linux/minmax.h> +#include <linux/prctl.h> #include <trace/events/kmem.h> #include <asm/pgalloc.h>
@@ -1157,6 +1158,26 @@ static int kscand_mstat_scan_period(unsigned int scan_period, int fratio)return scan_period * (1 + fratio /10 ); } +/* +* Scanning aggression is further controlled by prctl. pte_scan_scale value +* further tunes the scan period by 20%. +* 0 => scanning disabled. +* 1 => current min max is retained. +* 2..10 => scale the scan period by 20% * scale factor. +*/ +static unsigned long kscand_get_scaled_scan_period (unsigned int scan_period, +unsigned int scale) +{ +int delta =0 ; + +if (scale) { + delta = scan_period * (scale -1 ); + delta /= (PR_PTE_A_SCAN_SCALE_MAX -1 ); + } + +return scan_period + delta; +} +/* * This is the normal change percentage when old and new delta remain same. * i.e., either both positive or both zero.
@@ -1201,7 +1222,7 @@ static int kscand_mstat_scan_period(unsigned int scan_period, int fratio)* Increase scan_size by (1 << SCAN_SIZE_CHANGE_SHIFT). */ static inline void kscand_update_mmslot_info (struct kscand_mm_slot *mm_slot, -unsigned long total,int target_node) +unsigned long total,int target_node,unsigned int scale) {int fratio;unsigned int scan_period;
@@ -1243,6 +1264,7 @@ static inline void kscand_update_mmslot_info(struct kscand_mm_slot *mm_slot, } scan_period =clamp (scan_period,KSCAND_SCAN_PERIOD_MIN ,KSCAND_SCAN_PERIOD_MAX ); + scan_period =kscand_get_scaled_scan_period (scan_period, scale); fratio =kmigrated_get_mstat_fratio ((&mm_slot->slot)->mm); scan_period =kscand_mstat_scan_period (scan_period, fratio); scan_size =clamp (scan_size,KSCAND_SCAN_SIZE_MIN ,KSCAND_SCAN_SIZE_MAX );
@@ -1384,7 +1406,8 @@ static unsigned long kscand_scan_mm_slot(void)if (update_mmslot_info) { mm_slot->address = address; -kscand_update_mmslot_info (mm_slot, total, target_node); +kscand_update_mmslot_info (mm_slot, total, + target_node, mm->pte_scan_scale); } trace_kmem_scan_mm_end(mm, address, total, mm_slot_scan_period,
Signature · 1 line
2.34.1
RFC PATCH v3 17/17 mm: Create a list of fallback target nodes
In reply to Raghavendra K TThese fallback target nodes are used as hints for migration when current target node is near full.
TBD: Implementing migration to fallback nodes
--- mm/kscand.c | 9 +++++++++ 1 file changed, 9 insertions(+)
mm/kscand.c +9 -0
@@ -136,6 +136,7 @@ struct kscand_scanctrl {struct kscand_nodeinfo *nodeinfo[MAX_NUMNODES ];unsigned long address;unsigned long nr_to_scan; +nodemask_t nmask; };struct kscand_scanctrl kscand_scanctrl;
@@ -148,6 +149,8 @@ struct kmigrated_mm_slot {spinlock_t migrate_lock;/* Head of per mm migration list */ struct list_head migrate_head; +/* Indicates set of fallback nodes to migrate. */ +nodemask_t migration_nmask;/* Indicates weighted success, failure */ int msuccess, mfailed, fratio; };
@@ -522,6 +525,7 @@ static void reset_scanctrl(struct kscand_scanctrl *scanctrl) {int node; +nodes_clear (scanctrl->nmask);for_each_node_state (node,N_MEMORY )reset_nodeinfo (scanctrl->nodeinfo[node]);
@@ -547,9 +551,11 @@ static int get_target_node(struct kscand_scanctrl *scanctrl)int node, target_node =NUMA_NO_NODE ;unsigned long prev =0 ; +nodes_clear (scanctrl->nmask);for_each_node (node) {if (node_is_toptier (node) && scanctrl->nodeinfo[node]) {/* This creates a fallback migration node list */ +node_set (node, scanctrl->nmask);if (get_nodeinfo_nr_accessed (scanctrl->nodeinfo[node]) > prev) { prev =get_nodeinfo_nr_accessed (scanctrl->nodeinfo[node]); target_node = node;
@@ -1396,6 +1402,9 @@ static unsigned long kscand_scan_mm_slot(void) total =get_slowtier_accesed (&kscand_scanctrl); target_node =get_target_node (&kscand_scanctrl); +if (kmigrated_mm_slot) +nodes_copy (kmigrated_mm_slot->migration_nmask, + kscand_scanctrl.nmask); mm_target_node =READ_ONCE (mm->target_node);
Signature · 1 line
2.34.1
v3 cover mm: slowtier page promotion based on PTE A bit
In reply to Raghavendra K TQuoted 17 lines · Raghavendra K T
The current series has additional enhancements and comments' incorporation on top of RFC V2.
This is an additional source of hot page generator to NUMAB, IBS [4], KMGLRUD [5].
Introduction:
=============
In the current hot page promotion, all the activities including the process address space scanning, NUMA hint fault handling and page migration is performed in the process context. i.e., scanning overhead is borne by applications.This RFC V2 patch series does slow-tier page promotion by using PTE Accessed bit scanning. Scanning is done by a global kernel thread which routinely scans all the processes' address spaces and checks for accesses by reading the PTE A bit.
Quoted 6 lines
A separate migration thread migrates/promotes the pages to the top-tier node based on a simple heuristic that uses top-tier scan/access information of the mm.
Additionally based on the feedback, a prctl knob with a scalar value is provided to control per task scanning.
Patches are also available in the branch:
https://github.com/AMDESE/linux-mm/tree/rkt/kscand_rfc_v3
[...]
Quoted 3 lines
base-commit:
038d61fd642278bab63ee8ef722c50d10ab01e8f