From: Raghavendra K T <raghavendra.kt@amd.com>
To: <linux-mm@kvack.org>, <linux-kernel@vger.kernel.org>,
<gourry@gourry.net>, <nehagholkar@meta.com>, <abhishekd@meta.com>,
<david@redhat.com>, <ying.huang@intel.com>, <nphamcs@gmail.com>,
<akpm@linux-foundation.org>, <hannes@cmpxchg.org>,
<feng.tang@intel.com>, <kbusch@meta.com>, <bharata@amd.com>,
<Hasan.Maruf@amd.com>, <sj@kernel.org>
Cc: <willy@infradead.org>, <kirill.shutemov@linux.intel.com>,
<mgorman@techsingularity.net>, <vbabka@suse.cz>,
<hughd@google.com>, <rientjes@google.com>, <shy828301@gmail.com>,
<Liam.Howlett@Oracle.com>, <peterz@infradead.org>,
<mingo@redhat.com>, Raghavendra K T <raghavendra.kt@amd.com>
Subject: [RFC PATCH V0 05/10] mm: Add throttling of mm scanning using scan_period
Date: Sun, 1 Dec 2024 15:38:13 +0000 [thread overview]
Message-ID: <20241201153818.2633616-6-raghavendra.kt@amd.com> (raw)
In-Reply-To: <20241201153818.2633616-1-raghavendra.kt@amd.com>
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 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 (400ms) and MAX (5sec).
Signed-off-by: Raghavendra K T <raghavendra.kt@amd.com>
---
include/linux/mm_types.h | 4 ++
mm/kmmscand.c | 123 ++++++++++++++++++++++++++++++++++++++-
2 files changed, 125 insertions(+), 2 deletions(-)
Future improvements:
1. Consider the slope of useful pages found in last
scan and current scan for finer tuning.
2. Use migration failure information.
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 7361a8f3ab68..620b360b06fe 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -978,6 +978,10 @@ struct mm_struct {
/* numa_scan_seq prevents two threads remapping PTEs. */
int numa_scan_seq;
+#endif
+#ifdef CONFIG_KMMSCAND
+ /* Tracks number of pages with PTE A bit set after scanning. */
+ atomic_long_t nr_accessed;
#endif
/*
* An operation with batched TLB flushing is going on. Anything
diff --git a/mm/kmmscand.c b/mm/kmmscand.c
index 3b4453b053f4..589aed604cd6 100644
--- a/mm/kmmscand.c
+++ b/mm/kmmscand.c
@@ -19,6 +19,7 @@
#include <linux/mempolicy.h>
#include <linux/string.h>
#include <linux/cleanup.h>
+#include <linux/minmax.h>
#include <asm/pgalloc.h>
#include "internal.h"
@@ -27,6 +28,16 @@
static struct task_struct *kmmscand_thread __read_mostly;
static DEFINE_MUTEX(kmmscand_mutex);
+/*
+ * Scan period for each mm.
+ * Min: 400ms default: 2sec Max: 5sec
+ */
+#define KMMSCAND_SCAN_PERIOD_MAX 5000U
+#define KMMSCAND_SCAN_PERIOD_MIN 400U
+#define KMMSCAND_SCAN_PERIOD 2000U
+
+static unsigned int kmmscand_mm_scan_period_ms __read_mostly = KMMSCAND_SCAN_PERIOD;
+
/* How long to pause between two scan and migration cycle */
static unsigned int kmmscand_scan_sleep_ms __read_mostly = 16;
@@ -58,6 +69,11 @@ static struct kmem_cache *kmmscand_slot_cache __read_mostly;
struct kmmscand_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;
};
@@ -85,6 +101,7 @@ struct kmmscand_migrate_info {
struct folio *folio;
unsigned long address;
};
+
static int kmmscand_has_work(void)
{
return !list_empty(&kmmscand_scan.mm_head);
@@ -324,6 +341,12 @@ static int hot_vma_idle_pte_entry(pte_t *pte,
spin_lock(&kmmscand_migrate_lock);
list_add_tail(&info->migrate_node, &migrate_list->migrate_head);
spin_unlock(&kmmscand_migrate_lock);
+
+ /*
+ * XXX: Should nr_accessed be per vma for finer control?
+ * XXX: We are increamenting atomic var under mmap_readlock
+ */
+ atomic_long_inc(&mm->nr_accessed);
}
}
end:
@@ -446,11 +469,85 @@ static void kmmscand_migrate_folio(void)
spin_unlock(&kmmscand_migrate_lock);
}
+/*
+ * 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 KMMSCAND_IGNORE_SCAN_THR 100
+
+/*
+ * 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 kmmscand_update_mmslot_info(struct kmmscand_mm_slot *mm_slot, unsigned long total)
+{
+ unsigned int scan_period;
+ unsigned long now;
+ unsigned long old_scan_delta;
+
+ /* XXX: Hack to get rid of continuously failing/unmigrateable pages */
+ if (total < KMMSCAND_IGNORE_SCAN_THR)
+ total = 0;
+
+ scan_period = mm_slot->scan_period;
+
+ old_scan_delta = mm_slot->scan_delta;
+
+ /*
+ * 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, KMMSCAND_SCAN_PERIOD_MIN, KMMSCAND_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 kmmscand_scan_mm_slot(void)
{
bool update_mmslot_info = false;
+ unsigned int mm_slot_scan_period;
+ unsigned long now;
+ unsigned long mm_slot_next_scan;
unsigned long address;
+ unsigned long folio_nr_access_s, folio_nr_access_e, total = 0;
struct mm_slot *slot;
struct mm_struct *mm;
@@ -473,6 +570,8 @@ static unsigned long kmmscand_scan_mm_slot(void)
kmmscand_scan.mm_slot = mm_slot;
}
+ mm_slot_next_scan = mm_slot->next_scan;
+ mm_slot_scan_period = mm_slot->scan_period;
mm = slot->mm;
spin_unlock(&kmmscand_mm_lock);
@@ -483,6 +582,16 @@ static unsigned long kmmscand_scan_mm_slot(void)
if (unlikely(kmmscand_test_exit(mm)))
goto outerloop;
+ now = jiffies;
+ /*
+ * Dont scan if :
+ * This is not a first scan AND
+ * Reaching here before designated next_scan time.
+ */
+ if (mm_slot_next_scan && time_before(now, mm_slot_next_scan))
+ goto outerloop;
+
+ folio_nr_access_s = atomic_long_read(&mm->nr_accessed);
vma_iter_init(&vmi, mm, address);
@@ -492,6 +601,8 @@ static unsigned long kmmscand_scan_mm_slot(void)
address = vma->vm_end;
}
+ folio_nr_access_e = atomic_long_read(&mm->nr_accessed);
+ total = folio_nr_access_e - folio_nr_access_s;
if (!vma)
address = 0;
@@ -506,8 +617,12 @@ static unsigned long kmmscand_scan_mm_slot(void)
spin_lock(&kmmscand_mm_lock);
VM_BUG_ON(kmmscand_scan.mm_slot != mm_slot);
- if (update_mmslot_info)
+
+ if (update_mmslot_info) {
mm_slot->address = address;
+ kmmscand_update_mmslot_info(mm_slot, total);
+ }
+
/*
* Release the current mm_slot if this mm is about to die, or
* if we scanned all vmas of this mm.
@@ -532,7 +647,7 @@ static unsigned long kmmscand_scan_mm_slot(void)
}
spin_unlock(&kmmscand_mm_lock);
- return 0;
+ return total;
}
static void kmmscand_do_scan(void)
@@ -595,6 +710,10 @@ void __kmmscand_enter(struct mm_struct *mm)
return;
kmmscand_slot->address = 0;
+ kmmscand_slot->scan_period = kmmscand_mm_scan_period_ms;
+ kmmscand_slot->next_scan = 0;
+ kmmscand_slot->scan_delta = 0;
+
slot = &kmmscand_slot->slot;
spin_lock(&kmmscand_mm_lock);
--
2.39.3
next prev parent reply other threads:[~2024-12-01 15:39 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-01 15:38 [RFC PATCH V0 0/10] mm: slowtier page promotion based on PTE A bit Raghavendra K T
2024-12-01 15:38 ` [RFC PATCH V0 01/10] mm: Add kmmscand kernel daemon Raghavendra K T
2024-12-01 15:38 ` [RFC PATCH V0 02/10] mm: Maintain mm_struct list in the system Raghavendra K T
2024-12-01 15:38 ` [RFC PATCH V0 03/10] mm: Scan the mm and create a migration list Raghavendra K T
2024-12-01 15:38 ` [RFC PATCH V0 04/10] mm/migration: Migrate accessed folios to toptier node Raghavendra K T
2024-12-01 15:38 ` Raghavendra K T [this message]
2024-12-01 15:38 ` [RFC PATCH V0 06/10] mm: Add throttling of mm scanning using scan_size Raghavendra K T
2024-12-01 15:38 ` [RFC PATCH V0 07/10] sysfs: Add sysfs support to tune scanning Raghavendra K T
2024-12-01 15:38 ` [RFC PATCH V0 08/10] vmstat: Add vmstat counters Raghavendra K T
2024-12-01 15:38 ` [RFC PATCH V0 09/10] trace/kmmscand: Add tracing of scanning and migration Raghavendra K T
2024-12-05 17:46 ` Steven Rostedt
2024-12-06 6:33 ` Raghavendra K T
2024-12-06 14:49 ` Steven Rostedt
2024-12-01 15:38 ` [RFC PATCH V0 DO NOT MERGE 10/10] kmmscand: Add scanning Raghavendra K T
2024-12-10 18:53 ` [RFC PATCH V0 0/10] mm: slowtier page promotion based on PTE A bit SeongJae Park
2024-12-20 6:30 ` Raghavendra K T
2025-02-12 17:02 ` Davidlohr Bueso
2025-02-13 5:39 ` 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=20241201153818.2633616-6-raghavendra.kt@amd.com \
--to=raghavendra.kt@amd.com \
--cc=Hasan.Maruf@amd.com \
--cc=Liam.Howlett@Oracle.com \
--cc=abhishekd@meta.com \
--cc=akpm@linux-foundation.org \
--cc=bharata@amd.com \
--cc=david@redhat.com \
--cc=feng.tang@intel.com \
--cc=gourry@gourry.net \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=kbusch@meta.com \
--cc=kirill.shutemov@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mgorman@techsingularity.net \
--cc=mingo@redhat.com \
--cc=nehagholkar@meta.com \
--cc=nphamcs@gmail.com \
--cc=peterz@infradead.org \
--cc=rientjes@google.com \
--cc=shy828301@gmail.com \
--cc=sj@kernel.org \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
--cc=ying.huang@intel.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