mm: slowtier page promotion based on PTE A bit
Subsystems (10)
v2 cover mm: slowtier page promotion based on PTE A bit
The current series is:
a) RFC V1 [2] with review comments incorporated.
b) Design change to stabilize the solution.
c) Rebased to v6.15.
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 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.
What is not addressed yet:
===========================
- Patchset can use PFN based list instead of folios for migration. This will be done in the next iteration or so when integrating with kpromoted [3] /enhanced kmigrated APIs.
- Still using old target node heuristic that chooses only a single target. Hillf had suggested promotion to next tier nodes. we are not there yet.
- Jonathan, Davidlohr had raised comments on microbenchmark and migration based on first access. Now, filtering of folios is done by checking whether it is still hot using lru_gen_is_active() (for LRU_GEN case). But it is still first access for !LRU_GEN config.
- Davidlohr suggested using NUMAB2 along with scanning. Need more thoughts/implementation based on that (without relying on NUMAB2 timestamps??).
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 for review feedback on RFC V1.
Results:
=======
Microbenchmark gave similar improvements (8%+) as in RFC V1. But more benchmarking TBD with redis memtier etc (perhaps tuning based on that).
The patchset is also available here
Links:
[1] RFC V0: https://lore.kernel.org/all/[email protected]/
[2] RFC V1: https://lore.kernel.org/linux-mm/[email protected]/
[3] Kpromoted: https://lore.kernel.org/linux-mm/[email protected]/
Patch organization:
patch 1-4 initial skeleton for scanning and migration patch 5: migration
patch 6-8: scanning optimizations
patch 9: target_node heuristic
patch 10-12: sysfs, vmstat and tracing
patch 13: A basic prctl implementation.
Raghavendra K T (13):
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: 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
sysfs: Add sysfs support to tune scanning
vmstat: Add vmstat counters
trace/kscand: Add tracing of scanning and migration
prctl: Introduce new prctl to control scanning
Documentation/filesystems/proc.rst | 2 + fs/exec.c | 4 + fs/proc/task_mmu.c | 4 + include/linux/kscand.h | 30 + include/linux/migrate.h | 2 + include/linux/mm.h | 11 + include/linux/mm_types.h | 7 + include/linux/vm_event_item.h | 10 + include/trace/events/kmem.h | 90 ++ include/uapi/linux/prctl.h | 7 + kernel/fork.c | 8 + kernel/sys.c | 25 + mm/Kconfig | 8 + mm/Makefile | 1 + mm/kscand.c | 1644 ++++++++++++++++++++++++++++ mm/migrate.c | 2 +- mm/vmstat.c | 10 + 17 files changed, 1864 insertions(+), 1 deletion(-) create mode 100644 include/linux/kscand.h create mode 100644 mm/kscand.c
base-commit: 0ff41df1cb268fc69e703a08a57ee14ae967d0ca
Signature · 1 line
2.34.1
RFC PATCH v2 1/13 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
@@ -746,6 +746,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
RFC PATCH v2 2/13 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.
--- fs/exec.c | 4 ++ include/linux/kscand.h | 30 +++++++++++++++ kernel/fork.c | 4 ++ mm/kscand.c | 86 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 include/linux/kscand.h
fs/exec.c +4 -0
@@ -68,6 +68,7 @@#include <linux/user_events.h> #include <linux/rseq.h> #include <linux/ksm.h> +#include <linux/kscand.h> #include <linux/uaccess.h> #include <asm/mmu_context.h>
@@ -266,6 +267,8 @@ static int __bprm_mm_init(struct linux_binprm *bprm)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
@@ -288,6 +291,7 @@ static int __bprm_mm_init(struct linux_binprm *bprm)return 0 ; err:ksm_exit (mm); +kscand_exit (mm); err_ksm:mmap_write_unlock (mm); err_free:
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 +4 -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>
@@ -630,6 +631,8 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, mm->exec_vm = oldmm->exec_vm; mm->stack_vm = oldmm->stack_vm; +kscand_fork (mm, oldmm); +/* Use __mt_dup() to efficiently build an identical maple tree. */ retval =__mt_dup (&oldmm->mm_mt, &mm->mm_mt,GFP_KERNEL );if (unlikely (retval))
@@ -1377,6 +1380,7 @@ static inline void __mmput(struct mm_struct *mm)exit_aio (mm);ksm_exit (mm);khugepaged_exit (mm);/* must run before exit_mmap */ +kscand_exit (mm);exit_mmap (mm);mm_put_huge_zero_folio (mm);set_mm_exe_file (mm,NULL );
mm/kscand.c +86 -0
133 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH v2 3/13 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 (folios) belonging
to slowtier nodes. Add all those folios to a list.
--- mm/kscand.c | 319 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 318 insertions(+), 1 deletion(-)
mm/kscand.c +318 -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; +struct folio *folio; +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,275 @@ static void kscand_wait_work(void) scan_sleep_jiffies); } +static inline bool is_valid_folio (struct folio *folio) +{ +if (!folio ||folio_test_unevictable (folio) || !folio_mapped (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); + +page_idle_clear_pte_refs (page, pte, walk); + + folio =page_folio (page); +folio_get (folio); + +if (!is_valid_folio (folio)) { +folio_put (folio); +return 0 ; + } + srcnid =folio_nid (folio); + + +if (!folio_test_lru (folio)) { +folio_put (folio); +return 0 ; + } + +if (!folio_test_idle (folio) ||folio_test_young (folio) || +mmu_notifier_test_young (mm, addr) || +folio_test_referenced (folio) ||pte_young (pteval)) { + +if (!kscand_eligible_srcnid (srcnid)) { +folio_put (folio); +return 0 ; + } +/* XXX: Leaking memory. TBD: consume info */ + + info =kzalloc (sizeof (struct kscand_migrate_info ),GFP_NOWAIT ); +if (info && scanctrl) { + info->address = addr; + info->folio = folio; +list_add_tail (&info->migrate_node, &scanctrl->scan_list); + } + } + +folio_set_idle (folio); +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 +403,7 @@ static void kscand_do_scan(void)break ;if (kscand_has_work ()) -msleep (100 ); +kscand_scan_mm_slot (); iter++;
@@ -148,6 +450,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 +478,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 +533,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 +549,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
Re: [RFC PATCH V2 03/13] mm: Scan the mm and create a migration list
Quoted 9 lines · Raghavendra K T
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 (folios) belonging
to slowtier nodes. Add all those folios to a list.Signed-off-by Raghavendra K T <[email protected]>---
Hi, just taking a quick look...
Quoted 10 lines
mm/kscand.c | 319 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 318 insertions(+), 1 deletion(-)mm/kscand.c +0
-0@@ -42,6 +55,8 @@ static struct kmem_cache *kscand_slot_cache __read_mostly;@@ -84,11 +122,275 @@ static void kscand_wait_work(void) scan_sleep_jiffies);
mm/kscand.c static void kscand_wait_work(void) · line 132
Quoted 10 lines
mm/kscand.c +8
-0} +static inline bool is_valid_folio (struct folio *folio) +{ +if (!folio ||folio_test_unevictable (folio) || !folio_mapped (folio) || +folio_is_zone_device (folio) ||folio_maybe_mapped_shared (folio)) +return false; + +return true; +}
What makes it undesirable to migrate shared folios?
Quoted 10 lines
mm/kscand.c +10
-0+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) {
mm/kscand.c static void kscand_wait_work(void) · line 154
Quoted 10 lines
mm/kscand.c +10
-0+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 ); + }
This does not look good.
I think pmd entry handling should be handled in
mm_walk_ops.pmd_entry callback?
Quoted 11 lines
mm/kscand.c +11
-0+ +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) +{
mm/kscand.c static void kscand_wait_work(void) · line 175
Quoted 10 lines
mm/kscand.c +10
-0+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 ;
Why acquire folio lock here?
And I'm not even sure if it's safe to acquire it?
The locking order is folio_lock -> pte_lock
page walk should have already acquired pte_lock before calling ->pte_entry() callback.
Quoted 12 lines
mm/kscand.c +12
-0+ 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 const struct mm_walk_ops hot_vma_set_idle_ops = { + .pte_entry = hot_vma_idle_pte_entry, + .walk_lock =PGWALK_RDLOCK , +};
mm/kscand.c static void kscand_wait_work(void) · line 264
Quoted 10 lines
mm/kscand.c +10
-0+ +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 ;
Why not walk writable file VMAs?
Quoted 5 lines
mm/kscand.c +5
-0+if (!vma_is_accessible (vma)) +return ; + +walk_page_vma (vma, &hot_vma_set_idle_ops, scanctrl); +}
Signature · 2 lines
Cheers, Harry / Hyeonggon
Re: [RFC PATCH V2 03/13] mm: Scan the mm and create a migration list
Quoted 86 lines · Harry Yoo
Quoted 9 lines · Raghavendra K T
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 (folios) belonging
to slowtier nodes. Add all those folios to a list.Signed-off-by Raghavendra K T <[email protected]>---
Hi, just taking a quick look...
Quoted 10 lines
mm/kscand.c | 319 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 318 insertions(+), 1 deletion(-)mm/kscand.c +0
-0@@ -42,6 +55,8 @@ static struct kmem_cache *kscand_slot_cache __read_mostly;@@ -84,11 +122,275 @@ static void kscand_wait_work(void) scan_sleep_jiffies);mm/kscand.c static void kscand_wait_work(void) · line 132
Quoted 10 lines
mm/kscand.c +8
-0} +static inline bool is_valid_folio (struct folio *folio) +{ +if (!folio ||folio_test_unevictable (folio) || !folio_mapped (folio) || +folio_is_zone_device (folio) ||folio_maybe_mapped_shared (folio)) +return false; + +return true; +}What makes it undesirable to migrate shared folios?
Quoted 10 lines
mm/kscand.c +10
-0+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) {mm/kscand.c static void kscand_wait_work(void) · line 154
Quoted 10 lines
mm/kscand.c +10
-0+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 ); + }This does not look good.
I think pmd entry handling should be handled in
mm_walk_ops.pmd_entry callback?Quoted 11 lines
mm/kscand.c +11
-0+ +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) +{mm/kscand.c static void kscand_wait_work(void) · line 175
Quoted 10 lines
mm/kscand.c +10
-0+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 ;Why acquire folio lock here?
Quoted 5 lines
And I'm not even sure if it's safe to acquire it?
The locking order isfolio_lock->pte_lockpage walk should have already acquired
pte_lockbefore calling ->pte_entry() callback.
Oops, it's trylock. Nevermind.
Needed more coffee in the morning :)
Quoted 34 lines
Quoted 12 lines
mm/kscand.c +12
-0+ 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 const struct mm_walk_ops hot_vma_set_idle_ops = { + .pte_entry = hot_vma_idle_pte_entry, + .walk_lock =PGWALK_RDLOCK , +};mm/kscand.c static void kscand_wait_work(void) · line 264
Quoted 10 lines
mm/kscand.c +10
-0+ +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 ;Why not walk writable file VMAs?
Quoted 5 lines
mm/kscand.c +5
-0+if (!vma_is_accessible (vma)) +return ; + +walk_page_vma (vma, &hot_vma_set_idle_ops, scanctrl); +}Signature · 2 lines
Cheers, Harry / Hyeonggon
Signature · 2 lines
Cheers, Harry / Hyeonggon
Re: [RFC PATCH V2 03/13] mm: Scan the mm and create a migration list
In reply to Harry YooQuoted 5 lines · Harry Yoo
Quoted 4 lines · Raghavendra K T
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.
Quoted 7 lines
Quoted 5 lines
In the scan path: Check for the recently acccessed pages (folios) belonging
to slowtier nodes. Add all those folios to a list.Signed-off-by Raghavendra K T <[email protected]>---
Hi, just taking a quick look...
Hello Harry,
Thanks for taking a look at the patches.
Quoted 13 lines
Quoted 13 lines
mm/kscand.c | 319 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 318 insertions(+), 1 deletion(-)mm/kscand.c +0
-0@@ -42,6 +55,8 @@ static struct kmem_cache *kscand_slot_cache __read_mostly;@@ -84,11 +122,275 @@ static void kscand_wait_work(void) scan_sleep_jiffies); }
Quoted 10 lines
mm/kscand.c static void kscand_wait_work(void) · line 132
Quoted 8 lines
mm/kscand.c +8
-0+static inline bool is_valid_folio (struct folio *folio) +{ +if (!folio ||folio_test_unevictable (folio) || !folio_mapped (folio) || +folio_is_zone_device (folio) ||folio_maybe_mapped_shared (folio)) +return false; + +return true; +}What makes it undesirable to migrate shared folios?
This was mostly to avoid shared libraries, but yes this also should have accompanied with EXEC flag to refine further. This also avoids moving around shared data. I will experiment more and add additional filters OR remove the check.
Quoted 23 lines
Quoted 10 lines
mm/kscand.c +10
-0+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) {mm/kscand.c static void kscand_wait_work(void) · line 154
Quoted 10 lines
mm/kscand.c +10
-0+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 ); + }This does not look good.
Quoted 3 lines
I think pmd entry handling should be handled in
mm_walk_ops.pmd_entry callback?
Thanks, Let me check on this. Some part came when I was referring to idle page tracking.
Quoted 24 lines
Quoted 11 lines
mm/kscand.c +11
-0+ +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) +{mm/kscand.c static void kscand_wait_work(void) · line 175
Quoted 10 lines
mm/kscand.c +10
-0+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 ;Why acquire folio lock here?
Quoted 6 lines
And I'm not even sure if it's safe to acquire it?
The locking order isfolio_lock->pte_lockpage walk should have already acquired
pte_lockbefore calling ->pte_entry() callback.
I saw you clarified later.
Quoted 16 lines
Quoted 16 lines
mm/kscand.c +16
-0+ 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 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) ||
Quoted 9 lines
mm/kscand.c static void kscand_wait_work(void) · line 264
Quoted 6 lines
mm/kscand.c +6
-0+ 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 ;Why not walk writable file VMAs?
This is mainly followed from numa balancing logic
" Avoid hinting faults in read-only file-backed mappings or the vDSO
as migrating the pages will be of marginal benefit. "
But I have not measured benefits either way. Let me try once.
Quoted 6 lines
mm/kscand.c static void kscand_wait_work(void) · line 270
Quoted 5 lines
mm/kscand.c +5
-0+if (!vma_is_accessible (vma)) +return ; + +walk_page_vma (vma, &hot_vma_set_idle_ops, scanctrl); +}
Regards
- Raghu
Re: [RFC PATCH V2 03/13] mm: Scan the mm and create a migration list
Quoted 43 lines · Raghavendra K T
Quoted 5 lines · Harry Yoo
Quoted 4 lines · Raghavendra K T
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.Quoted 7 lines
Quoted 5 lines
In the scan path: Check for the recently acccessed pages (folios) belonging to slowtier nodes. Add all those folios to a list.
Signed-off-by Raghavendra K T <[email protected]>---
Hi, just taking a quick look...
Hello Harry,
Thanks for taking a look at the patches.Quoted 17 lines
Quoted 17 lines
mm/kscand.c| 319 +++++++++++++++++++++++++++++++++++++++++++++++++++-1 file changed, 318 insertions(+), 1 deletion(-)mm/kscand.c +3
-0@@ -42,6 +55,8 @@ static struct kmem_cache *kscand_slot_cache__read_mostly ;@@ -84,11 +122,275 @@ static void kscand_wait_work(void) scan_sleep_jiffies); } +static inline bool is_valid_folio (struct folio *folio) +{ + if (!folio || folio_test_unevictable(folio) || !
folio_mapped(folio)||Quoted 8 lines
mm/kscand.c static void kscand_wait_work(void) · line 132
Quoted 6 lines
+
folio_is_zone_device(folio)||folio_maybe_mapped_shared(folio))
+ return false;mm/kscand.c +3
-0+ + return true; +}What makes it undesirable to migrate shared folios?
Quoted 5 lines
This was mostly to avoid shared libraries, but yes this also should have accompanied with EXEC flag to refine further. This also avoids moving around shared data. I will experiment more and add additional filters OR remove the check.
Atleast experiment with microbenchmark gives a positive benefit if I remove check for folio_maybe_mapped_shared() folios. Will respin accordingly.
(also after checking some real workloads).
- Raghu
RFC PATCH v2 4/13 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;
@@ -131,6 +142,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,
@@ -533,6 +557,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);
@@ -555,8 +622,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
RFC PATCH v2 5/13 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 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 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 | 452 ++++++++++++++++++++++++++++++++++++++-- mm/migrate.c | 2 +- 3 files changed, 443 insertions(+), 13 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 +440 -12
582 lines not on this page · raw message
mm/migrate.c +1 -1
@@ -2598,7 +2598,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
RFC PATCH v2 6/13 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; };
@@ -713,13 +729,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;
@@ -744,6 +839,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)))
@@ -754,6 +851,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);
@@ -784,8 +886,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 */
@@ -887,6 +991,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
RFC PATCH v2 7/13 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; };
@@ -744,6 +751,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/*
@@ -759,14 +768,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;
@@ -787,20 +808,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; }
@@ -812,6 +838,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 ;
@@ -841,6 +868,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)))
@@ -992,6 +1020,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
RFC PATCH v2 8/13 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.
@@ -1008,6 +1009,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 */
@@ -1018,10 +1020,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 + +msecs_to_jiffies (sysctl_numa_balancing_scan_delay); kscand_slot->scan_delta =0 ; slot = &kscand_slot->slot;
Signature · 1 line
2.34.1
RFC PATCH v2 9/13 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 | 186 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 181 insertions(+), 9 deletions(-)
TBD: Also maintain nodemask instead of single target node to handle failed migrations?.
include/linux/mm_types.h +4 -0
@@ -1109,6 +1109,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 +177 -9
306 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH v2 10/13 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"
@@ -171,6 +172,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);
@@ -1164,11 +1330,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)
@@ -1354,6 +1554,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)
@@ -1370,6 +1574,7 @@ static int __init kscand_init(void) err_kscand:stop_kscand (); +err_init_sysfs:kscand_destroy ();return err;
Signature · 1 line
2.34.1
RFC PATCH v2 11/13 vmstat: Add vmstat counters
In reply to Raghavendra K TAdd vmstat counter to track scanning, migration and type of pages.
--- include/linux/mm.h | 11 ++++++++ include/linux/vm_event_item.h | 10 +++++++ mm/kscand.c | 51 ++++++++++++++++++++++++++++++++++- mm/vmstat.c | 10 +++++++ 4 files changed, 81 insertions(+), 1 deletion(-)
This implementation will change with upcoming changes in vmstat.
include/linux/mm.h +11 -0
@@ -656,6 +656,17 @@ 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 ); +#endif +#ifdef CONFIG_NUMA_BALANCING static inline void vma_numab_state_init (struct vm_area_struct *vma) {
include/linux/vm_event_item.h +10 -0
@@ -67,6 +67,16 @@ 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 , +#endif #ifdef CONFIG_MIGRATIONPGMIGRATE_SUCCESS ,PGMIGRATE_FAIL , THP_MIGRATION_SUCCESS,
mm/kscand.c +50 -1
97 lines not on this page · raw message
mm/vmstat.c +10 -0
@@ -1348,6 +1348,16 @@ 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" , +#endif #ifdef CONFIG_MIGRATION"pgmigrate_success" , "pgmigrate_fail",
Signature · 1 line
2.34.1
RFC PATCH v2 12/13 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 | 90 +++++++++++++++++++++++++++++++++++++ mm/kscand.c | 8 ++++ 2 files changed, 98 insertions(+)
include/trace/events/kmem.h +90 -0
@@ -9,6 +9,96 @@#include <linux/tracepoint.h> #include <trace/events/mmflags.h> +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), + +TP_ARGS (mm, rc, target_node), + +TP_STRUCT__entry ( + __field( struct mm_struct *, mm ) + __field( int, rc ) +__field ( int, target_node ) + ), + +TP_fast_assign ( + __entry->mm = mm; + __entry->rc = rc; + __entry->target_node = target_node; + ), + +TP_printk ("mm = %p rc = %d node = %d" , + __entry->mm, __entry->rc, __entry->target_node) +); + TRACE_EVENT(kmem_cache_alloc, TP_PROTO(unsigned long call_site,
mm/kscand.c +8 -0
@@ -1035,6 +1035,7 @@ static void kmigrated_migrate_mm(struct kmigrated_mm_slot *mm_slot) } ret =kmigrated_promote_folio (info, mm, dest); +trace_kmem_scan_mm_migrate (mm, ret, dest);/* TBD: encode migrated count here, currently assume folio_nr_pages */ if (!ret)
@@ -1230,6 +1231,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))
@@ -1300,6 +1304,8 @@ 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 */
@@ -1453,6 +1459,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); }
@@ -1463,6 +1470,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
Masami Hiramatsu (Google)
maintainerRe: [RFC PATCH V2 12/13] trace/kscand: Add tracing of scanning and migration
Quoted 19 lines · On Tue, 24 Jun 2025 05:56:16 +0000 Raghavendra K T <[email protected]>…
Add tracing support to track
- start and end of scanning.
- migration.Signed-off-by Raghavendra K T <[email protected]>--- include/trace/events/kmem.h | 90 +++++++++++++++++++++++++++++++++++++ mm/kscand.c | 8 ++++ 2 files changed, 98 insertions(+)include/trace/events/kmem.h +0
-0
include/trace/events/kmem.h line 11
Quoted 3 lines
include/trace/events/kmem.h +0
-0@@ -9,6 +9,96 @@#include <linux/tracepoint.h> #include <trace/events/mmflags.h>
Please make sure the event is not exposed when it is not used.
#ifdef CONFIG_KSCAND
Thank you,
Quoted 144 lines
include/trace/events/kmem.h +90
-0+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), + +TP_ARGS (mm, rc, target_node), + +TP_STRUCT__entry ( + __field( struct mm_struct *, mm ) + __field( int, rc ) +__field ( int, target_node ) + ), + +TP_fast_assign ( + __entry->mm = mm; + __entry->rc = rc; + __entry->target_node = target_node; + ), + +TP_printk ("mm = %p rc = %d node = %d" , + __entry->mm, __entry->rc, __entry->target_node) +); + TRACE_EVENT(kmem_cache_alloc, TP_PROTO(unsigned long call_site,mm/kscand.c +8
-0@@ -1035,6 +1035,7 @@ static void kmigrated_migrate_mm(struct kmigrated_mm_slot *mm_slot) } ret =kmigrated_promote_folio (info, mm, dest); +trace_kmem_scan_mm_migrate (mm, ret, dest);/* TBD: encode migrated count here, currently assume folio_nr_pages */ if (!ret)@@ -1230,6 +1231,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))@@ -1300,6 +1304,8 @@ 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 */ @@ -1453,6 +1459,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); }@@ -1463,6 +1470,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
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [RFC PATCH V2 12/13] trace/kscand: Add tracing of scanning and migration
Quoted 28 lines · Masami Hiramatsu (Google)
Quoted 19 lines · On Tue, 24 Jun 2025 05:56:16 +0000 Raghavendra K T <[email protected]>…
Add tracing support to track
- start and end of scanning.
- migration.Signed-off-by Raghavendra K T <[email protected]>--- include/trace/events/kmem.h | 90 +++++++++++++++++++++++++++++++++++++ mm/kscand.c | 8 ++++ 2 files changed, 98 insertions(+)include/trace/events/kmem.h +0
-0include/trace/events/kmem.h line 11
Quoted 3 lines
include/trace/events/kmem.h +0
-0@@ -9,6 +9,96 @@#include <linux/tracepoint.h> #include <trace/events/mmflags.h> Please make sure the event is not exposed when it is not used.
Quoted 4 lines
#ifdef
CONFIG_KSCANDThank you,
[...]
Sure. Noted. Thank you :)
RFC PATCH v2 13/13 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(+)
Rebasing to upstream tree will change prctl number.
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
@@ -1112,6 +1112,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
@@ -364,4 +364,11 @@ struct prctl_mm_map {# define PR_TIMER_CREATE_RESTORE_IDS_ON 1# define PR_TIMER_CREATE_RESTORE_IDS_GET 2 +/* Set/get PTE A bit scan scale */ +#define PR_SET_PTE_A_SCAN_SCALE 78 +#define PR_GET_PTE_A_SCAN_SCALE 79 +# define PR_PTE_A_SCAN_SCALE_MIN 0 +# define PR_PTE_A_SCAN_SCALE_MAX 10 +# define PR_PTE_A_SCAN_SCALE_DEFAULT 1 +#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>
@@ -1311,6 +1312,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,init_tlb_flush_pending (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
@@ -2146,6 +2146,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)
@@ -2820,6 +2833,18 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,return -EINVAL ; error =posixtimer_create_prctl (arg2);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
@@ -1228,6 +1228,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