From: Jonathan Cameron <jonathan.cameron@huawei.com>
To: Raghavendra K T <raghavendra.kt@amd.com>
Cc: <AneeshKumar.KizhakeVeetil@arm.com>, <Michael.Day@amd.com>,
<akpm@linux-foundation.org>, <bharata@amd.com>,
<dave.hansen@intel.com>, <david@redhat.com>,
<dongjoo.linux.dev@gmail.com>, <feng.tang@intel.com>,
<gourry@gourry.net>, <hannes@cmpxchg.org>, <honggyu.kim@sk.com>,
<hughd@google.com>, <jhubbard@nvidia.com>, <jon.grimm@amd.com>,
<k.shutemov@gmail.com>, <kbusch@meta.com>,
<kmanaouil.dev@gmail.com>, <leesuyeon0506@gmail.com>,
<leillc@google.com>, <liam.howlett@oracle.com>,
<linux-kernel@vger.kernel.org>, <linux-mm@kvack.org>,
<mgorman@techsingularity.net>, <mingo@redhat.com>,
<nadav.amit@gmail.com>, <nphamcs@gmail.com>,
<peterz@infradead.org>, <riel@surriel.com>, <rientjes@google.com>,
<rppt@kernel.org>, <santosh.shukla@amd.com>, <shivankg@amd.com>,
<shy828301@gmail.com>, <sj@kernel.org>, <vbabka@suse.cz>,
<weixugc@google.com>, <willy@infradead.org>,
<ying.huang@linux.alibaba.com>, <ziy@nvidia.com>,
<dave@stgolabs.net>, <yuanchu@google.com>, <kinseyho@google.com>,
<hdanton@sina.com>, <harry.yoo@oracle.com>
Subject: Re: [RFC PATCH V3 03/17] mm: Scan the mm and create a migration list
Date: Thu, 2 Oct 2025 14:53:32 +0100 [thread overview]
Message-ID: <20251002145332.00003f63@huawei.com> (raw)
In-Reply-To: <20250814153307.1553061-4-raghavendra.kt@amd.com>
On Thu, 14 Aug 2025 15:32:53 +0000
Raghavendra K T <raghavendra.kt@amd.com> wrote:
> Since 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
Bonus space at start of line.
> to slowtier nodes. Add all those to a list.
>
> Signed-off-by: Raghavendra K T <raghavendra.kt@amd.com>
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.
> ---
> mm/kscand.c | 321 +++++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 320 insertions(+), 1 deletion(-)
>
> diff --git a/mm/kscand.c b/mm/kscand.c
> index d5b0d3041b0f..1d883d411664 100644
> --- a/mm/kscand.c
> +++ b/mm/kscand.c
> +
> +static bool kscand_eligible_srcnid(int nid)
> +{
> + /* Only promotion case is considered */
> + return !node_is_toptier(nid);
one space before ! not 2.
> +}
> +
> 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;
> +}
> +
One blank line only unless local convention matches this.
> +
> +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)
> +{
> + 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.
> +
> + 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.
> +
> + 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.
> + if (!folio_test_lru(folio)) {
Maybe a goto given common code in all exit paths around here.
> + 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))) {
> +
> + /* 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)
> +{
> + 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.
> +
One line enough.
> +
> + 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_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;
> + 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.
> + 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:
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.
> + 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)
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.
> +{
> + 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;
next prev parent reply other threads:[~2025-10-02 13:53 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-14 15:32 [RFC PATCH V3 00/17] mm: slowtier page promotion based on PTE A bit Raghavendra K T
2025-08-14 15:32 ` [RFC PATCH V3 01/17] mm: Add kscand kthread for PTE A bit scan Raghavendra K T
2025-10-02 13:12 ` Jonathan Cameron
2025-08-14 15:32 ` [RFC PATCH V3 02/17] mm: Maintain mm_struct list in the system Raghavendra K T
2025-10-02 13:23 ` Jonathan Cameron
2025-08-14 15:32 ` [RFC PATCH V3 03/17] mm: Scan the mm and create a migration list Raghavendra K T
2025-10-02 13:53 ` Jonathan Cameron [this message]
2025-08-14 15:32 ` [RFC PATCH V3 04/17] mm/kscand: Add only hot pages to " Raghavendra K T
2025-10-02 16:00 ` Jonathan Cameron
2025-08-14 15:32 ` [RFC PATCH V3 05/17] mm: Create a separate kthread for migration Raghavendra K T
2025-10-02 16:03 ` Jonathan Cameron
2025-08-14 15:32 ` [RFC PATCH V3 06/17] mm/migration: migrate accessed folios to toptier node Raghavendra K T
2025-10-02 16:17 ` Jonathan Cameron
2025-08-14 15:32 ` [RFC PATCH V3 07/17] mm: Add throttling of mm scanning using scan_period Raghavendra K T
2025-10-02 16:24 ` Jonathan Cameron
2025-08-14 15:32 ` [RFC PATCH V3 08/17] mm: Add throttling of mm scanning using scan_size Raghavendra K T
2025-10-03 9:35 ` Jonathan Cameron
2025-08-14 15:32 ` [RFC PATCH V3 09/17] mm: Add initial scan delay Raghavendra K T
2025-10-03 9:41 ` Jonathan Cameron
2025-08-14 15:33 ` [RFC PATCH V3 10/17] mm: Add a heuristic to calculate target node Raghavendra K T
2025-10-03 10:04 ` Jonathan Cameron
2025-08-14 15:33 ` [RFC PATCH V3 11/17] mm/kscand: Implement migration failure feedback Raghavendra K T
2025-10-03 10:10 ` Jonathan Cameron
2025-08-14 15:33 ` [RFC PATCH V3 12/17] sysfs: Add sysfs support to tune scanning Raghavendra K T
2025-10-03 10:25 ` Jonathan Cameron
2025-08-14 15:33 ` [RFC PATCH V3 13/17] mm/vmstat: Add vmstat counters Raghavendra K T
2025-08-14 15:33 ` [RFC PATCH V3 14/17] trace/kscand: Add tracing of scanning and migration Raghavendra K T
2025-10-03 10:28 ` Jonathan Cameron
2025-08-14 15:33 ` [RFC PATCH V3 15/17] prctl: Introduce new prctl to control scanning Raghavendra K T
2025-08-14 15:33 ` [RFC PATCH V3 16/17] prctl: Fine tune scan_period with prctl scale param Raghavendra K T
2025-08-14 15:33 ` [RFC PATCH V3 17/17] mm: Create a list of fallback target nodes Raghavendra K T
2025-08-21 15:24 ` [RFC PATCH V3 00/17] mm: slowtier page promotion based on PTE A bit Raghavendra K T
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251002145332.00003f63@huawei.com \
--to=jonathan.cameron@huawei.com \
--cc=AneeshKumar.KizhakeVeetil@arm.com \
--cc=Michael.Day@amd.com \
--cc=akpm@linux-foundation.org \
--cc=bharata@amd.com \
--cc=dave.hansen@intel.com \
--cc=dave@stgolabs.net \
--cc=david@redhat.com \
--cc=dongjoo.linux.dev@gmail.com \
--cc=feng.tang@intel.com \
--cc=gourry@gourry.net \
--cc=hannes@cmpxchg.org \
--cc=harry.yoo@oracle.com \
--cc=hdanton@sina.com \
--cc=honggyu.kim@sk.com \
--cc=hughd@google.com \
--cc=jhubbard@nvidia.com \
--cc=jon.grimm@amd.com \
--cc=k.shutemov@gmail.com \
--cc=kbusch@meta.com \
--cc=kinseyho@google.com \
--cc=kmanaouil.dev@gmail.com \
--cc=leesuyeon0506@gmail.com \
--cc=leillc@google.com \
--cc=liam.howlett@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mgorman@techsingularity.net \
--cc=mingo@redhat.com \
--cc=nadav.amit@gmail.com \
--cc=nphamcs@gmail.com \
--cc=peterz@infradead.org \
--cc=raghavendra.kt@amd.com \
--cc=riel@surriel.com \
--cc=rientjes@google.com \
--cc=rppt@kernel.org \
--cc=santosh.shukla@amd.com \
--cc=shivankg@amd.com \
--cc=shy828301@gmail.com \
--cc=sj@kernel.org \
--cc=vbabka@suse.cz \
--cc=weixugc@google.com \
--cc=willy@infradead.org \
--cc=ying.huang@linux.alibaba.com \
--cc=yuanchu@google.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox