Skip to contentSkip to conversation

sched: Rate limit migrations

RFC PATCH In review Mathieu Desnoyers 2 participants 2 messages Apr 11, 2023 – Apr 12, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 sched: Rate limit migrations

This WIP patch rate-limits migrations to 32 migrations per 10ms window for each task.

The specific migration count and window size can be changed with the following defines in kernel/sched/sched.h:

- SCHED_MIGRATION_WINDOW_NS
- SCHED_MIGRATION_LIMIT

Testing is welcome, especially to see if it helps with Aaron's migration-heavy workload wrt rseq concurrency id performance regression.

---
 include/linux/sched.h |  9 +++++++++
 kernel/fork.c         |  3 +++
 kernel/sched/core.c   | 42 ++++++++++++++++++++++++++++++++++++++----
 kernel/sched/sched.h  |  7 +++++++
 4 files changed, 57 insertions(+), 4 deletions(-)
include/linux/sched.h +9 -0
index 48d48b2c73a5..bfd5e268900c 100644
@@ -1316,6 +1316,15 @@ struct task_struct {
 	int				last_mm_cid;	/* Most recent cid in mm */
 	int				mm_cid_active;	/* Whether cid bitmap is active */
 #endif
+	/*
+	 * Keep track of last migration time to compare sched_clock
+	 * locally from a single CPU perspective.
+	 */
+	u64				last_migration_time;
+	/* Time slice used in current migration window. */
+	u64				migration_window_time_slice;
+	/* Number of migrations in current migration window. */
+	u32				migration_count;
 
 	struct tlbflush_unmap_batch	tlb_ubc;
 
kernel/fork.c +3 -0
index 3832bea713c4..791792a218f6 100644
@@ -1061,6 +1061,9 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
 	tsk->last_mm_cid = -1;
 	tsk->mm_cid_active = 0;
 #endif
+	tsk->last_migration_time = 0;
+	tsk->migration_window_time_slice = 0;
+	tsk->migration_count = 0;
 	return tsk;
 
 free_stack:
kernel/sched/core.c +38 -4

67 lines not on this page · raw message

kernel/sched/sched.h +7 -0
index 64220134fb45..e52cc38f10fc 100644
@@ -104,6 +104,13 @@ struct cpuidle_state;
 #define TASK_ON_RQ_QUEUED	1
 #define TASK_ON_RQ_MIGRATING	2
 
+/*
+ * A task can be migrated at most SCHED_MIGRATION_WINDOW_LIMIT times per
+ * sched-migration window.
+ */
+#define SCHED_MIGRATION_WINDOW_NS	(10ULL * 1000000)	/* 10 ms */
+#define SCHED_MIGRATION_LIMIT		32
+
 extern __read_mostly int scheduler_running;
 
 extern unsigned long calc_load_update;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · Mathieu Desnoyers

This WIP patch rate-limits migrations to 32 migrations per 10ms window for each task.

The specific migration count and window size can be changed with the following defines in kernel/sched/sched.h:

Quoted 6 lines

- SCHED_MIGRATION_WINDOW_NS
- SCHED_MIGRATION_LIMIT

Testing is welcome, especially to see if it helps with Aaron's migration-heavy workload wrt rseq concurrency id performance regression.

Initial test shows the migration number for a 5s window is still in some millions and profile wise, contention didn't change much, still in the range of single digit to 20% something during 3 minutes run.

If this limit works, then the total migration number should be less
than: 224 * 2 * 3200 * 5 = 7million so hard to say if the limit works as expected since the number I captured is indeed less than 7 million. 224 * 2 means totally there are 448 tasks, 224 client and 224 server. 3200 is the theoretical migration number for a task in a 1s window and 5 means 5 seconds.

I'll play with it more to see how things change.