[RFC PATCH V1 1/2] sched/numa: Introduce per vma scan counter

From: Raghavendra K T
Date: Tue May 02 2023 - 22:08:08 EST


With the recent numa scan enhancements, only the tasks which had
previously accessed vma are allowed to scan.

While this has improved significant system time overhead, there are
corner cases, which genuinely needs some relaxation for e.g., concern
raised by PeterZ where unfairness amongst the theread belonging to
disjoint set of VMSs can potentially amplify the side effects of vma
regions belonging to some of the tasks being left unscanned.

To address this, allow scanning for first few times with a per vma
counter.

Signed-off-by: Raghavendra K T <raghavendra.kt@xxxxxxx>
---
include/linux/mm_types.h | 1 +
kernel/sched/fair.c | 30 +++++++++++++++++++++++++++---
2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 3fc9e680f174..f66e6b4e0620 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -479,6 +479,7 @@ struct vma_numab_state {
unsigned long next_scan;
unsigned long next_pid_reset;
unsigned long access_pids[2];
+ unsigned int scan_counter;
};

/*
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index a29ca11bead2..3c50dc3893eb 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -2928,19 +2928,38 @@ static void reset_ptenuma_scan(struct task_struct *p)
p->mm->numa_scan_offset = 0;
}

+/* Scan 1GB or 4 * scan_size */
+#define VMA_DISJOINT_SET_ACCESS_THRESH 4U
+
static bool vma_is_accessed(struct vm_area_struct *vma)
{
unsigned long pids;
+ unsigned int windows;
+ unsigned int scan_size = READ_ONCE(sysctl_numa_balancing_scan_size);
+
+ if (scan_size < MAX_SCAN_WINDOW)
+ windows = MAX_SCAN_WINDOW / scan_size;
+
+ /* Allow only half of the windows for disjoint set cases */
+ windows /= 2;
+
+ windows = max(VMA_DISJOINT_SET_ACCESS_THRESH, windows);
+
/*
- * Allow unconditional access first two times, so that all the (pages)
- * of VMAs get prot_none fault introduced irrespective of accesses.
+ * Make sure to allow scanning of disjoint vma set for the first
+ * few times.
+ * OR At mm level allow unconditional access first two times, so that
+ * all the (pages) of VMAs get prot_none fault introduced irrespective
+ * of accesses.
* This is also done to avoid any side effect of task scanning
* amplifying the unfairness of disjoint set of VMAs' access.
*/
- if (READ_ONCE(current->mm->numa_scan_seq) < 2)
+ if (READ_ONCE(vma->numab_state->scan_counter) < windows ||
+ READ_ONCE(current->mm->numa_scan_seq) < 2)
return true;

pids = vma->numab_state->access_pids[0] | vma->numab_state->access_pids[1];
+
return test_bit(hash_32(current->pid, ilog2(BITS_PER_LONG)), &pids);
}

@@ -3058,6 +3077,8 @@ static void task_numa_work(struct callback_head *work)
/* Reset happens after 4 times scan delay of scan start */
vma->numab_state->next_pid_reset = vma->numab_state->next_scan +
msecs_to_jiffies(VMA_PID_RESET_PERIOD);
+
+ WRITE_ONCE(vma->numab_state->scan_counter, 0);
}

/*
@@ -3084,6 +3105,9 @@ static void task_numa_work(struct callback_head *work)
vma->numab_state->access_pids[1] = 0;
}

+ WRITE_ONCE(vma->numab_state->scan_counter,
+ READ_ONCE(vma->numab_state->scan_counter) + 1);
+
do {
start = max(start, vma->vm_start);
end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
--
2.34.1