sched: Restructure task_mm_cid_work for predictability
Subsystems (5)
v7 cover sched: Restructure task_mm_cid_work for predictability
This patchset moves the task_mm_cid_work to a preemptible and migratable context. This reduces the impact of this work to the scheduling latency of real time tasks.
The change makes the recurrence of the task a bit more predictable.
The behaviour causing latency was introduced in commit 223baf9d17f2 ("sched: Fix performance regression introduced by mm_cid") which introduced a task work tied to the scheduler tick.
That approach presents two possible issues:
* the task work runs before returning to user and causes, in fact, a scheduling latency (with order of magnitude significant in PREEMPT_RT)
* periodic tasks with short runtime are less likely to run during the tick, hence they might not run the task work at all
Patch 1 contains the main changes, removing the task_work on the scheduler tick and using a work_struct scheduled more reliably during __rseq_handle_notify_resume.
Patch 2 adds a selftest to validate the functionality of the task_mm_cid_work (i.e. to compact the mm_cids). The test fails if patch 1 of V6 ("sched: Compact RSEQ concurrency IDs with reduced threads and affinity", applied separately) is not applied and is flaky without patch 1 of this series. We expect it to always pass with both applied.
Changes since V6 [1]:
* Switch to a simple work_struct instead of a delayed work
* Schedule the work_struct in __rseq_handle_notify_resume
* Asynchronously disable the work but make sure mm is there while we run
* Remove first patch as merged independently
* Fix commit tag for test
Changes since V5:
* Punctuation
Changes since V4 [2]:
* Fixes on the selftest
* Polished memory allocation and cleanup
* Handle the test failure in main
Changes since V3 [3]:
* Fixes on the selftest
* Minor style issues in comments and indentation
* Use of perror where possible
* Add a barrier to align threads execution
* Improve test failure and error handling
Changes since V2 [4]:
* Change the order of the patches
* Merge patches changing the main delayed_work logic
* Improved self-test to spawn 1 less thread and use the main one instead
Changes since V1 [5]:
* Re-arm the delayed_work at each invocation
* Cancel the work synchronously at mmdrop
* Remove next scan fields and completely rely on the delayed_work
* Shrink mm_cid allocation with nr thread/affinity (Mathieu Desnoyers)
* Add self test
[1] - https://lore.kernel.org/lkml/[email protected]
[2] - https://lore.kernel.org/lkml/[email protected]
[3] - https://lore.kernel.org/lkml/[email protected]
[4] - https://lore.kernel.org/lkml/[email protected]
[5] - https://lore.kernel.org/lkml/[email protected]
To: Mathieu Desnoyers <[email protected]>
To: Peter Zijlstra <[email protected]>
To: Ingo Molnar <[email protected]>
To: Paul E. McKenney <[email protected]>
To: Shuah Khan <[email protected]>
Gabriele Monaco (2):
sched: Move task_mm_cid_work to mm work_struct
selftests/rseq: Add test for mm_cid compaction
include/linux/mm_types.h | 8 + include/linux/sched.h | 7 +- kernel/rseq.c | 1 + kernel/sched/core.c | 33 ++- kernel/sched/sched.h | 2 - tools/testing/selftests/rseq/.gitignore | 1 + tools/testing/selftests/rseq/Makefile | 2 +- .../selftests/rseq/mm_cid_compaction_test.c | 200 ++++++++++++++++++ 8 files changed, 229 insertions(+), 25 deletions(-) create mode 100644 tools/testing/selftests/rseq/mm_cid_compaction_test.c
base-commit: 0ad2507d5d93f39619fc42372c347d6006b64319
prerequisite-patch-id: 6888a889ab5dcd50c3874005018576fdced3a5e5
Signature · 1 line
2.48.1
PATCH v7 1/2 sched: Move task_mm_cid_work to mm work_struct
Currently, the task_mm_cid_work function is called in a task work triggered by a scheduler tick to frequently compact the mm_cids of each process. This can delay the execution of the corresponding thread for the entire duration of the function, negatively affecting the response in case of real time tasks. In practice, we observe task_mm_cid_work increasing the latency of 30-35us on a 128 cores system, this order of magnitude is meaningful under PREEMPT_RT.
Run the task_mm_cid_work in a new work_struct connected to the mm_struct rather than in the task context before returning to userspace.
This work_struct is initialised with the mm and disabled before freeing it. Its execution is no longer triggered by scheduler ticks: the queuing of the work happens while returning to userspace in __rseq_handle_notify_resume, maintaining the checks to avoid running more frequently than MM_CID_SCAN_DELAY.
The main advantage of this change is that the function can be offloaded to a different CPU and even preempted by RT tasks.
Moreover, this new behaviour is more predictable with periodic tasks with short runtime, which may rarely run during a scheduler tick. Now, the work is always scheduled when the task returns to userspace.
The work is disabled during mmdrop, since the function cannot sleep in all kernel configurations, we cannot wait for possibly running work items to terminate. We make sure the mm is valid in case the task is terminating by reserving it with mmgrab/mmdrop, returning prematurely if we are really the last user before mmgrab.
This situation is unlikely since we don't schedule the work for exiting tasks, but we cannot rule it out.
--- include/linux/mm_types.h | 8 ++++++++ include/linux/sched.h | 7 ++++++- kernel/rseq.c | 1 + kernel/sched/core.c | 33 ++++++++++++--------------------- kernel/sched/sched.h | 2 -- 5 files changed, 27 insertions(+), 24 deletions(-)
include/linux/mm_types.h +8 -0
@@ -889,6 +889,10 @@ struct mm_struct {* mm nr_cpus_allowed updates. */ raw_spinlock_t cpus_allowed_lock; +/* +* @cid_work: Work item to run the mm_cid scan. +*/ +struct work_struct cid_work;#endif #ifdef CONFIG_MMU atomic_long_t pgtables_bytes;/* size of all page tables */
@@ -1185,6 +1189,8 @@ enum mm_cid_state { MM_CID_LAZY_PUT = (1U << 31), }; +extern void task_mm_cid_work (struct work_struct *work); +static inline bool mm_cid_is_unset (int cid) {return cid ==MM_CID_UNSET ;
@@ -1257,12 +1263,14 @@ static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *if (!mm->pcpu_cid)return -ENOMEM ;mm_init_cid (mm, p); +INIT_WORK (&mm->cid_work, task_mm_cid_work);return 0 ; }#define mm_alloc_cid (...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__))static inline void mm_destroy_cid (struct mm_struct *mm) { +disable_work (&mm->cid_work);free_percpu (mm->pcpu_cid); mm->pcpu_cid =NULL ; }
include/linux/sched.h +6 -1
@@ -1397,7 +1397,6 @@ struct task_struct {int last_mm_cid;/* Most recent cid in mm */ int migrate_from_cpu;int mm_cid_active;/* Whether cid bitmap is active */ -struct callback_head cid_work;#endif struct tlbflush_unmap_batch tlb_ubc;
@@ -2254,4 +2253,10 @@ static __always_inline void alloc_tag_restore(struct alloc_tag *tag, struct allo#define alloc_tag_restore (_tag, _old) do {} while (0)#endif +#ifdef CONFIG_SCHED_MM_CID +extern void task_queue_mm_cid (struct task_struct *curr); +#else +static inline void task_queue_mm_cid (struct task_struct *curr) { } +#endif +#endif
kernel/rseq.c +1 -0
@@ -419,6 +419,7 @@ void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs) }if (unlikely (rseq_update_cpu_node_id (t))) goto error; +task_queue_mm_cid (t);return ; error:
kernel/sched/core.c +12 -21
@@ -5663,7 +5663,6 @@ void sched_tick(void) resched_latency =cpu_resched_latency (rq);calc_global_load_tick (rq);sched_core_tick (rq); -task_tick_mm_cid (rq, donor);scx_tick (rq);rq_unlock (rq, &rf);
@@ -10530,22 +10529,16 @@ static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu,sched_mm_cid_remote_clear (mm, pcpu_cid, cpu); } -static void task_mm_cid_work (struct callback_head *work) +void task_mm_cid_work (struct work_struct *work) {unsigned long now = jiffies, old_scan, next_scan; -struct task_struct *t = current;struct cpumask *cidmask; -struct mm_struct *mm; + struct mm_struct *mm = container_of(work, struct mm_struct, cid_work);int weight, cpu; - SCHED_WARN_ON(t != container_of(work, struct task_struct, cid_work)); - - work->next = work;/* Prevent double-add */ -if (t->flags &PF_EXITING ) -return ; - mm = t->mm; -if (!mm) +if (!atomic_read (&mm->mm_count))return ; +mmgrab (mm); old_scan =READ_ONCE (mm->mm_cid_next_scan); next_scan = now +msecs_to_jiffies (MM_CID_SCAN_DELAY );if (!old_scan) {
@@ -10558,9 +10551,9 @@ static void task_mm_cid_work(struct callback_head *work) old_scan = next_scan; }if (time_before (now, old_scan)) -return ; + goto out_drop;if (!try_cmpxchg (&mm->mm_cid_next_scan, &old_scan, next_scan)) -return ; + goto out_drop; cidmask =mm_cidmask (mm);/* Clear cids that were not recently used. */ for_each_possible_cpu (cpu)
@@ -10572,6 +10565,8 @@ static void task_mm_cid_work(struct callback_head *work)*/ for_each_possible_cpu (cpu)sched_mm_cid_remote_clear_weight (mm, cpu, weight); +out_drop: +mmdrop (mm); }void init_sched_mm_cid (struct task_struct *t)
@@ -10584,23 +10579,19 @@ void init_sched_mm_cid(struct task_struct *t)if (mm_users ==1 ) mm->mm_cid_next_scan = jiffies +msecs_to_jiffies (MM_CID_SCAN_DELAY ); } - t->cid_work.next = &t->cid_work;/* Protect against double add */ -init_task_work (&t->cid_work, task_mm_cid_work); } -void task_tick_mm_cid (struct rq *rq,struct task_struct *curr) +void task_queue_mm_cid (struct task_struct *curr) { -struct callback_head *work = &curr->cid_work; +struct work_struct *work = &curr->mm->cid_work;unsigned long now = jiffies; -if (!curr->mm || (curr->flags & (PF_EXITING |PF_KTHREAD )) || - work->next != work) +if (!curr->mm || (curr->flags & (PF_EXITING |PF_KTHREAD )))return ;if (time_before (now,READ_ONCE (curr->mm->mm_cid_next_scan)))return ; -/* No page allocation under rq lock */ -task_work_add (curr, work,TWA_RESUME ); +schedule_work (work); }void sched_mm_cid_exit_signals (struct task_struct *t)
kernel/sched/sched.h +0 -2
@@ -3630,7 +3630,6 @@ extern int use_cid_lock;extern void sched_mm_cid_migrate_from (struct task_struct *t);extern void sched_mm_cid_migrate_to (struct rq *dst_rq,struct task_struct *t); -extern void task_tick_mm_cid (struct rq *rq,struct task_struct *curr);extern void init_sched_mm_cid (struct task_struct *t);static inline void __mm_cid_put (struct mm_struct *mm,int cid)
@@ -3899,7 +3898,6 @@ static inline void switch_mm_cid(struct rq *rq,static inline void switch_mm_cid (struct rq *rq,struct task_struct *prev,struct task_struct *next) { }static inline void sched_mm_cid_migrate_from (struct task_struct *t) { }static inline void sched_mm_cid_migrate_to (struct rq *dst_rq,struct task_struct *t) { } -static inline void task_tick_mm_cid (struct rq *rq,struct task_struct *curr) { }static inline void init_sched_mm_cid (struct task_struct *t) { }#endif /* !CONFIG_SCHED_MM_CID */
Signature · 1 line
2.48.1
Mathieu Desnoyers
maintainerRe: [PATCH v7 1/2] sched: Move task_mm_cid_work to mm work_struct
Quoted 122 lines · Gabriele Monaco
Currently, the
task_mm_cid_workfunction is called in a task work triggered by a scheduler tick to frequently compact themm_cidsof each process. This can delay the execution of the corresponding thread for the entire duration of the function, negatively affecting the response in case of real time tasks. In practice, we observetask_mm_cid_workincreasing the latency of 30-35us on a 128 cores system, this order of magnitude is meaningful underPREEMPT_RT.Run the
task_mm_cid_workin a newwork_structconnected to themm_structrather than in the task context before returning to userspace.This
work_structis initialised with the mm and disabled before freeing it. Its execution is no longer triggered by scheduler ticks: the queuing of the work happens while returning to userspace in__rseq_handle_notify_resume, maintaining the checks to avoid running more frequently thanMM_CID_SCAN_DELAY.The main advantage of this change is that the function can be offloaded to a different CPU and even preempted by RT tasks.
Moreover, this new behaviour is more predictable with periodic tasks with short runtime, which may rarely run during a scheduler tick. Now, the work is always scheduled when the task returns to userspace.
The work is disabled during mmdrop, since the function cannot sleep in all kernel configurations, we cannot wait for possibly running work items to terminate. We make sure the mm is valid in case the task is terminating by reserving it with mmgrab/mmdrop, returning prematurely if we are really the last user before mmgrab.
This situation is unlikely since we don't schedule the work for exiting tasks, but we cannot rule it out.Fixes 223baf9d17f2 ("sched: Fix performance regression introduced by mm_cid")Signed-off-by Gabriele Monaco <[email protected]>--- include/linux/mm_types.h | 8 ++++++++ include/linux/sched.h | 7 ++++++- kernel/rseq.c | 1 + kernel/sched/core.c | 33 ++++++++++++--------------------- kernel/sched/sched.h | 2 -- 5 files changed, 27 insertions(+), 24 deletions(-)include/linux/mm_types.h +8
-0@@ -889,6 +889,10 @@ struct mm_struct {* mm nr_cpus_allowed updates. */ raw_spinlock_t cpus_allowed_lock; +/* +* @cid_work: Work item to run the mm_cid scan. +*/ +struct work_struct cid_work;#endif #ifdef CONFIG_MMU atomic_long_t pgtables_bytes;/* size of all page tables */ @@ -1185,6 +1189,8 @@ enum mm_cid_state { MM_CID_LAZY_PUT = (1U << 31), }; +extern void task_mm_cid_work (struct work_struct *work); +static inline bool mm_cid_is_unset (int cid) {return cid ==MM_CID_UNSET ;@@ -1257,12 +1263,14 @@ static inline int mm_alloc_cid_noprof(struct mm_struct *mm, struct task_struct *if (!mm->pcpu_cid)return -ENOMEM ;mm_init_cid (mm, p); +INIT_WORK (&mm->cid_work, task_mm_cid_work);return 0 ; }#define mm_alloc_cid (...) alloc_hooks(mm_alloc_cid_noprof(__VA_ARGS__))static inline void mm_destroy_cid (struct mm_struct *mm) { +disable_work (&mm->cid_work);free_percpu (mm->pcpu_cid); mm->pcpu_cid =NULL ; }include/linux/sched.h +6
-1@@ -1397,7 +1397,6 @@ struct task_struct {int last_mm_cid;/* Most recent cid in mm */ int migrate_from_cpu;int mm_cid_active;/* Whether cid bitmap is active */ -struct callback_head cid_work;#endif struct tlbflush_unmap_batch tlb_ubc;@@ -2254,4 +2253,10 @@ static __always_inline void alloc_tag_restore(struct alloc_tag *tag, struct allo#define alloc_tag_restore (_tag, _old) do {} while (0)#endif +#ifdef CONFIG_SCHED_MM_CID +extern void task_queue_mm_cid (struct task_struct *curr); +#else +static inline void task_queue_mm_cid (struct task_struct *curr) { } +#endif +#endif kernel/rseq.c +1
-0@@ -419,6 +419,7 @@ void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs) }if (unlikely (rseq_update_cpu_node_id (t))) goto error; +task_queue_mm_cid (t);return ; error:kernel/sched/core.c +0
-0
kernel/sched/core.c void sched_tick(void) · line 5666
Quoted 4 lines
kernel/sched/core.c +0
-1@@ -5663,7 +5663,6 @@ void sched_tick(void) resched_latency =cpu_resched_latency (rq);calc_global_load_tick (rq);sched_core_tick (rq); -task_tick_mm_cid (rq, donor);
I agree that this approach is promising, however I am concerned about the fact that a task running alone on its runqueue (thus without preemption) for a long time will never recompact mm_cid, and also will never update its mm_cid field.
So I am tempted to insert this in the sched_tick to cover that scenario:
rseq_preempt(current);
This would ensure that the task runs __rseq_handle_notify_resume() at least each tick.
Quoted 17 lines
kernel/sched/core.c +2
-3scx_tick (rq);rq_unlock (rq, &rf);@@ -10530,22 +10529,16 @@ static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu,sched_mm_cid_remote_clear (mm, pcpu_cid, cpu); } -static void task_mm_cid_work (struct callback_head *work) +void task_mm_cid_work (struct work_struct *work) {unsigned long now = jiffies, old_scan, next_scan; -struct task_struct *t = current;struct cpumask *cidmask; -struct mm_struct *mm; + struct mm_struct *mm = container_of(work, struct mm_struct, cid_work);int weight, cpu;
kernel/sched/core.c static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu, · line 10541
Quoted 10 lines
kernel/sched/core.c +2
-7- SCHED_WARN_ON(t != container_of(work, struct task_struct, cid_work)); - - work->next = work;/* Prevent double-add */ -if (t->flags &PF_EXITING ) -return ; - mm = t->mm; -if (!mm) +if (!atomic_read (&mm->mm_count))return ; +mmgrab (mm);
AFAIU this is racy with respect to re-use of mm struct.
I recommend that you move mmgrab() to task_queue_mm_cid() just before invoking schedule_work. That way you ensure that the mm count never reaches 0 while there is work in flight (and therefore guarantee that the mm is not re-used).
Thanks,
Mathieu
Quoted 71 lines
kernel/sched/core.c +7
-10old_scan =READ_ONCE (mm->mm_cid_next_scan); next_scan = now +msecs_to_jiffies (MM_CID_SCAN_DELAY );if (!old_scan) {@@ -10558,9 +10551,9 @@ static void task_mm_cid_work(struct callback_head *work) old_scan = next_scan; }if (time_before (now, old_scan)) -return ; + goto out_drop;if (!try_cmpxchg (&mm->mm_cid_next_scan, &old_scan, next_scan)) -return ; + goto out_drop; cidmask =mm_cidmask (mm);/* Clear cids that were not recently used. */ for_each_possible_cpu (cpu)@@ -10572,6 +10565,8 @@ static void task_mm_cid_work(struct callback_head *work)*/ for_each_possible_cpu (cpu)sched_mm_cid_remote_clear_weight (mm, cpu, weight); +out_drop: +mmdrop (mm); }void init_sched_mm_cid (struct task_struct *t)@@ -10584,23 +10579,19 @@ void init_sched_mm_cid(struct task_struct *t)if (mm_users ==1 ) mm->mm_cid_next_scan = jiffies +msecs_to_jiffies (MM_CID_SCAN_DELAY ); } - t->cid_work.next = &t->cid_work;/* Protect against double add */ -init_task_work (&t->cid_work, task_mm_cid_work); } -void task_tick_mm_cid (struct rq *rq,struct task_struct *curr) +void task_queue_mm_cid (struct task_struct *curr) { -struct callback_head *work = &curr->cid_work; +struct work_struct *work = &curr->mm->cid_work;unsigned long now = jiffies; -if (!curr->mm || (curr->flags & (PF_EXITING |PF_KTHREAD )) || - work->next != work) +if (!curr->mm || (curr->flags & (PF_EXITING |PF_KTHREAD )))return ;if (time_before (now,READ_ONCE (curr->mm->mm_cid_next_scan)))return ; -/* No page allocation under rq lock */ - task_work_add(curr, work, TWA_RESUME);> + schedule_work(work); }void sched_mm_cid_exit_signals (struct task_struct *t)kernel/sched/sched.h +0
-2@@ -3630,7 +3630,6 @@ extern int use_cid_lock;extern void sched_mm_cid_migrate_from (struct task_struct *t);extern void sched_mm_cid_migrate_to (struct rq *dst_rq,struct task_struct *t); -extern void task_tick_mm_cid (struct rq *rq,struct task_struct *curr);extern void init_sched_mm_cid (struct task_struct *t);static inline void __mm_cid_put (struct mm_struct *mm,int cid)@@ -3899,7 +3898,6 @@ static inline void switch_mm_cid(struct rq *rq,static inline void switch_mm_cid (struct rq *rq,struct task_struct *prev,struct task_struct *next) { }static inline void sched_mm_cid_migrate_from (struct task_struct *t) { }static inline void sched_mm_cid_migrate_to (struct rq *dst_rq,struct task_struct *t) { } -static inline void task_tick_mm_cid (struct rq *rq,struct task_struct *curr) { }static inline void init_sched_mm_cid (struct task_struct *t) { }#endif /* !CONFIG_SCHED_MM_CID */
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Re: [PATCH v7 1/2] sched: Move task_mm_cid_work to mm work_struct
Quoted 81 lines · Mathieu Desnoyers
Quoted 81 lines · Gabriele Monaco
Quoted 66 lines
Quoted 66 lines
Currently, the
task_mm_cid_workfunction is called in a task work triggered by a scheduler tick to frequently compact themm_cidsof each
process. This can delay the execution of the corresponding thread for
the entire duration of the function, negatively affecting the response
in case of real time tasks. In practice, we observetask_mm_cid_work
increasing the latency of 30-35us on a 128 cores system, this order of
magnitude is meaningful underPREEMPT_RT.Run the
task_mm_cid_workin a newwork_structconnected to themm_structrather than in the task context before returning to userspace.This
work_structis initialised with the mm and disabled before freeing
it. Its execution is no longer triggered by scheduler ticks: the queuing
of the work happens while returning to userspace in__rseq_handle_notify_resume, maintaining the checks to avoid running
more frequently thanMM_CID_SCAN_DELAY.The main advantage of this change is that the function can be offloaded
to a different CPU and even preempted by RT tasks.Moreover, this new behaviour is more predictable with periodic tasks
with short runtime, which may rarely run during a scheduler tick. Now, the work is always scheduled when the task returns to userspace.The work is disabled during mmdrop, since the function cannot sleep in
all kernel configurations, we cannot wait for possibly running work items to terminate. We make sure the mm is valid in case the task is
terminating by reserving it with mmgrab/mmdrop, returning prematurely if
we are really the last user before mmgrab.
This situation is unlikely since we don't schedule the work for exiting
tasks, but we cannot rule it out.Fixes 223baf9d17f2 ("sched: Fix performance regression introducedby
mm_cid")Signed-off-by Gabriele Monaco <[email protected]>---
kernel/rseq.c +1
-0@@ -419,6 +419,7 @@ void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs) } if (unlikely(rseq_update_cpu_node_id(t))) goto error; +task_queue_mm_cid (t); return;
error:kernel/sched/core.c +0
-0Quoted 8 lines
kernel/sched/core.c void sched_tick(void) · line 5666
Quoted 8 lines
index
9aecd914ac691..ee35f9962444b100644kernel/sched/core.c +0
-1@@ -5663,7 +5663,6 @@ void sched_tick(void) resched_latency = cpu_resched_latency(rq); calc_global_load_tick(rq); sched_core_tick(rq); -task_tick_mm_cid (rq, donor);I agree that this approach is promising, however I am concerned about the fact that a task running alone on its runqueue (thus without preemption) for a long time will never recompact
mm_cid, and also will never update itsmm_cidfield.
Quoted 8 lines
Quoted 8 lines
So I am tempted to insert this in the
sched_tickto cover that scenario:
rseq_preempt(current);This would ensure that the task runs
__rseq_handle_notify_resume()at least each tick.
Right, I thought about this scenario but forgot to add it in the final patch. We could have a test doing that: instead of sleeping, the task busy waits.
Does __rseq_handle_notify_resume need to run in this case, besides for the cid compaction, I mean? Otherwise we could again just enqueu the work from there.
I'll give a shot for both.
Quoted 33 lines
Quoted 33 lines
Quoted 22 lines
Quoted 22 lines
kernel/sched/core.c +0
-0scx_tick(rq);
rq_unlock(rq, &rf);kernel/sched/core.c +0
-0@@ -10530,22 +10529,16 @@ static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu, sched_mm_cid_remote_clear(mm, pcpu_cid, cpu); }kernel/sched/core.c +1
-1-static void task_mm_cid_work (struct callback_head *work) +void task_mm_cid_work (struct work_struct *work){
unsigned long now = jiffies,old_scan,next_scan;
- structtask_struct*t = current;
struct cpumask *cidmask;
- structmm_struct*mm;
+ structmm_struct*mm =container_of(work, struct mm_struct, cid_work);
int weight, cpu;
-SCHED_WARN_ON(t != container_of(work, struct task_struct, cid_work));
-Quoted 8 lines
Quoted 8 lines
-
work->next= work; /* Prevent double-add */
- if (t->flags&PF_EXITING)
- return;
- mm =t->mm;
- if (!mm)
+ if (!atomic_read(&mm->mm_count))
return;
+ mmgrab(mm);AFAIU this is racy with respect to re-use of mm struct.
Quoted 5 lines
Quoted 5 lines
I recommend that you move
mmgrab()totask_queue_mm_cid()just before invokingschedule_work. That way you ensure that the mm count never reaches 0 while there is work in flight (and therefore guarantee that the mm is not re-used).
Mmh good point, in that case I think we can still keep on testing the mm_count and return prematurely if it's 1 (we are the only user and the task exited before the work got scheduled). That would be a safe assumption if we don't get to 0, wouldn't it?
Thanks,
Gabriele
Mathieu Desnoyers
maintainerRe: [PATCH v7 1/2] sched: Move task_mm_cid_work to mm work_struct
Quoted 93 lines · Gabriele Monaco
Quoted 81 lines · Mathieu Desnoyers
Quoted 81 lines · Gabriele Monaco
Quoted 64 lines
Quoted 64 lines
Currently, the
task_mm_cid_workfunction is called in a task work triggered by a scheduler tick to frequently compact themm_cidsof each
process. This can delay the execution of the corresponding thread for
the entire duration of the function, negatively affecting the response
in case of real time tasks. In practice, we observetask_mm_cid_work
increasing the latency of 30-35us on a 128 cores system, this order of
magnitude is meaningful underPREEMPT_RT.Run the
task_mm_cid_workin a newwork_structconnected to themm_structrather than in the task context before returning to userspace.This
work_structis initialised with the mm and disabled before freeing
it. Its execution is no longer triggered by scheduler ticks: the queuing
of the work happens while returning to userspace in__rseq_handle_notify_resume, maintaining the checks to avoid running
more frequently thanMM_CID_SCAN_DELAY.The main advantage of this change is that the function can be offloaded
to a different CPU and even preempted by RT tasks.Moreover, this new behaviour is more predictable with periodic tasks
with short runtime, which may rarely run during a scheduler tick. Now, the work is always scheduled when the task returns to userspace.The work is disabled during mmdrop, since the function cannot sleep in
all kernel configurations, we cannot wait for possibly running work items to terminate. We make sure the mm is valid in case the task is
terminating by reserving it with mmgrab/mmdrop, returning prematurely if
we are really the last user before mmgrab.
This situation is unlikely since we don't schedule the work for exiting
tasks, but we cannot rule it out.Fixes 223baf9d17f2 ("sched: Fix performance regression introducedby
mm_cid")Signed-off-by Gabriele Monaco <[email protected]>---
kernel/rseq.c +1
-0@@ -419,6 +419,7 @@ void __rseq_handle_notify_resume(struct ksignal *ksig, struct pt_regs *regs) } if (unlikely(rseq_update_cpu_node_id(t))) goto error; +task_queue_mm_cid (t); return;Quoted 10 lines
kernel/sched/core.c void sched_tick(void) · line 5666
Quoted 10 lines
error:
kernel/sched/core.c +0
-1@@ -5663,7 +5663,6 @@ void sched_tick(void) resched_latency = cpu_resched_latency(rq); calc_global_load_tick(rq); sched_core_tick(rq); -task_tick_mm_cid (rq, donor);I agree that this approach is promising, however I am concerned about the fact that a task running alone on its runqueue (thus without preemption) for a long time will never recompact
mm_cid, and also will never update itsmm_cidfield.Quoted 8 lines
Quoted 8 lines
So I am tempted to insert this in the
sched_tickto cover that scenario:
rseq_preempt(current);This would ensure that the task runs
__rseq_handle_notify_resume()at least each tick.
Quoted 5 lines
Right, I thought about this scenario but forgot to add it in the final patch. We could have a test doing that: instead of sleeping, the task busy waits.
Does
__rseq_handle_notify_resumeneed to run in this case, besides for the cid compaction, I mean? Otherwise we could again just enqueu the work from there.
Yes we need to do both:
- compact cid,
- run __rseq_handle_notify_resume to update the mm_cid.
We we don't care much if compacting the cid is done at some point and __rseq_handle_notify_resume only updates the mm_cid field on the following tick.
So enqueuing the work is not sufficient there, I would really issue rseq_preempt(current) which makes sure a busy thread both triggers cid compaction *and* gets its mm_cid updated.
Quoted 44 lines
I'll give a shot for both.
Quoted 33 lines
Quoted 33 lines
Quoted 19 lines
Quoted 19 lines
kernel/sched/core.c +2
-3scx_tick(rq); rq_unlock(rq, &rf);@@ -10530,22 +10529,16 @@ static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu, sched_mm_cid_remote_clear(mm, pcpu_cid, cpu); } -static void task_mm_cid_work (struct callback_head *work) +void task_mm_cid_work (struct work_struct *work) { unsigned long now = jiffies, old_scan, next_scan; -struct task_struct *t = current; struct cpumask *cidmask; -struct mm_struct *mm; + struct mm_struct *mm = container_of(work, struct mm_struct, cid_work); int weight, cpu;Quoted 11 lines
kernel/sched/core.c static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu, · line 10541
Quoted 11 lines
kernel/sched/core.c +2
-7- SCHED_WARN_ON(t != container_of(work, struct task_struct, cid_work)); - - work->next = work;/* Prevent double-add */ -if (t->flags &PF_EXITING ) -return ; - mm = t->mm; -if (!mm) +if (!atomic_read (&mm->mm_count)) return; +mmgrab (mm);AFAIU this is racy with respect to re-use of mm struct.
Quoted 5 lines
Quoted 5 lines
I recommend that you move
mmgrab()totask_queue_mm_cid()just before invokingschedule_work. That way you ensure that the mm count never reaches 0 while there is work in flight (and therefore guarantee that the mm is not re-used).
Quoted 2 lines
Mmh good point, in that case I think we can still keep on testing the
mm_countand return prematurely if it's 1 (we are the only user and the task exited before the work got scheduled). That would be a safe assumption if we don't get to 0, wouldn't it?
Yes, although don't forget the mmdrop in that case ;)
Thanks,
Mathieu
Quoted 4 lines
Thanks,
Gabriele
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Re: [PATCH v7 1/2] sched: Move task_mm_cid_work to mm work_struct
Quoted 132 lines · Mathieu Desnoyers
Quoted 121 lines · Gabriele Monaco
Quoted 106 lines · Mathieu Desnoyers
Quoted 106 lines · Gabriele Monaco
Quoted 85 lines
Quoted 85 lines
Currently, the
task_mm_cid_workfunction is called in a task work
triggered by a scheduler tick to frequently compact themm_cidsof
each
process. This can delay the execution of the corresponding thread
for
the entire duration of the function, negatively affecting the
response
in case of real time tasks. In practice, we observetask_mm_cid_work
increasing the latency of 30-35us on a 128 cores system, this order
of
magnitude is meaningful underPREEMPT_RT.Run the
task_mm_cid_workin a newwork_structconnected to themm_structrather than in the task context before returning to
userspace.This
work_structis initialised with the mm and disabled before
freeing
it. Its execution is no longer triggered by scheduler
ticks: the
queuing
of the work happens while returning to userspace in__rseq_handle_notify_resume, maintaining the checks to avoid
running
more frequently thanMM_CID_SCAN_DELAY.The main advantage of this change is that the function can be
offloaded
to a different CPU and even preempted by RT tasks.Moreover, this new behaviour is more predictable with periodic
tasks
with short runtime, which may rarely run during a scheduler tick.
Now, the work is always scheduled when the task returns to userspace.The work is disabled during mmdrop, since the function cannot sleep
in
all kernel configurations, we cannot wait for possibly running work
items to terminate. We make sure the mm is valid in case the task
is
terminating by reserving it with mmgrab/mmdrop, returning prematurely if
we are really the last user before mmgrab.
This situation is unlikely since we don't schedule the work for
exiting
tasks, but we cannot rule it out.Fixes 223baf9d17f2 ("sched: Fix performance regressionintroduced
bymm_cid")Signed-off-by Gabriele Monaco <[email protected]>---
kernel/rseq.c +0
-0@@ -419,6 +419,7 @@ void __rseq_handle_notify_resume(structksignal
*ksig, structpt_regs*regs)
}
if (unlikely(rseq_update_cpu_node_id(t)))
goto error;
+task_queue_mm_cid(t);
return;
error:kernel/sched/core.c +0
-0Quoted 8 lines
kernel/sched/core.c void sched_tick(void) · line 5666
Quoted 8 lines
index
9aecd914ac691..ee35f9962444b100644kernel/sched/core.c +0
-1@@ -5663,7 +5663,6 @@ void sched_tick(void) resched_latency = cpu_resched_latency(rq); calc_global_load_tick(rq); sched_core_tick(rq); -task_tick_mm_cid (rq, donor);I agree that this approach is promising, however I am concerned about
the fact that a task running alone on its runqueue (thus without
preemption) for a long time will never recompactmm_cid, and also
will never update itsmm_cidfield.So I am tempted to insert this in the
sched_tickto cover that scenario:Quoted 6 lines
Quoted 6 lines
rseq_preempt(current);This would ensure that the task runs
__rseq_handle_notify_resume()at
least each tick.Right, I thought about this scenario but forgot to add it in the final patch.
We could have a test doing that: instead of sleeping, the task busy waits.Quoted 4 lines
Does
__rseq_handle_notify_resumeneed to run in this case, besides for the cid compaction, I mean? Otherwise we could again just enqueu
the work from there.Yes we need to do both:
- compact cid,
- run__rseq_handle_notify_resumeto update themm_cid.
Quoted 8 lines
We we don't care much if compacting the cid is done at some point and
__rseq_handle_notify_resumeonly updates themm_cidfield on the following tick.So enqueuing the work is not sufficient there, I would really issue
rseq_preempt(current)which makes sure a busy thread both triggers cid compaction *and* gets itsmm_cidupdated.
Sure, will do.
I've been trying to test this scenario but it's quite hard to achieve. I set all threads to FIFO and highest priority, removed all system calls from the leader thread (even the ones to wait for other threads) and replaced the usleep with a busy wait, running on a VM so not sure if interrupts can bother. The test still passes..
Anyway it seems a reasonable situation to happens and I guess it won't hurt to just run an rseq_preempt in the tick.
Testing and sending V8 without touching the selftest.
Thanks,
Gabriele
PATCH v7 2/2 selftests/rseq: Add test for mm_cid compaction
In reply to Gabriele MonacoA task in the kernel (task_mm_cid_work) runs somewhat periodically to compact the mm_cid for each process. Add a test to validate that it runs correctly and timely.
The test spawns 1 thread pinned to each CPU, then each thread, including the main one, runs in short bursts for some time. During this period, the mm_cids should be spanning all numbers between 0 and nproc.
At the end of this phase, a thread with high enough mm_cid (>= nproc/2) is selected to be the new leader, all other threads terminate.
After some time, the only running thread should see 0 as mm_cid, if that doesn't happen, the compaction mechanism didn't work and the test fails.
The test never fails if only 1 core is available, in which case, we cannot test anything as the only available mm_cid is 0.
--- tools/testing/selftests/rseq/.gitignore | 1 + tools/testing/selftests/rseq/Makefile | 2 +- .../selftests/rseq/mm_cid_compaction_test.c | 200 ++++++++++++++++++ 3 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/rseq/mm_cid_compaction_test.c
tools/testing/selftests/rseq/.gitignore +1 -0
@@ -3,6 +3,7 @@ basic_percpu_ops_test basic_percpu_ops_mm_cid_test basic_test basic_rseq_op_test +mm_cid_compaction_test param_test param_test_benchmark param_test_compare_twice
tools/testing/selftests/rseq/Makefile +1 -1
@@ -16,7 +16,7 @@ OVERRIDE_TARGETS = 1 TEST_GEN_PROGS = basic_test basic_percpu_ops_test basic_percpu_ops_mm_cid_test param_test \ param_test_benchmark param_test_compare_twice param_test_mm_cid \ - param_test_mm_cid_benchmark param_test_mm_cid_compare_twice + param_test_mm_cid_benchmark param_test_mm_cid_compare_twice mm_cid_compaction_test TEST_GEN_PROGS_EXTENDED = librseq.so
tools/testing/selftests/rseq/mm_cid_compaction_test.c new file +200 -0
200 lines not on this page · raw message
Signature · 1 line
2.48.1