Skip to contentSkip to conversation

sched/core: Don't use dying mm as active_mm of kthreads

PATCH In review Waiman Long 4 participants 14 messages Jul 29, 2019 – Jul 31, 2019

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

PATCH v3 1/1 sched/core: Don't use dying mm as active_mm of kthreads

It was found that a dying mm_struct where the owning task has exited can stay on as active_mm of kernel threads as long as no other user tasks run on those CPUs that use it as active_mm. This prolongs the life time of dying mm holding up some resources that cannot be freed on a mostly idle system.

Fix that by forcing the kernel threads to use init_mm as the active_mm during a kernel thread to kernel thread transition if the previous active_mm is dying (!mm_users). This will allows the freeing of resources associated with the dying mm ASAP.

The presence of a kernel-to-kernel thread transition indicates that the cpu is probably idling with no higher priority user task to run. So the overhead of loading the mm_users cacheline should not really matter in this case.

My testing on an x86 system showed that the mm_struct was freed within seconds after the task exited instead of staying alive for minutes or even longer on a mostly idle system before this patch.

---
 kernel/sched/core.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/sched/core.c +19 -2
index 795077af4f1a..41997e676251 100644
@@ -3214,6 +3214,8 @@ static __always_inline struct rq *
 context_switch(struct rq *rq, struct task_struct *prev,
 	       struct task_struct *next, struct rq_flags *rf)
 {
+	struct mm_struct *next_mm = next->mm;
+
 	prepare_task_switch(rq, prev, next);
 
 	/*
@@ -3229,8 +3231,22 @@ context_switch(struct rq *rq, struct task_struct *prev,
 	 *
 	 * kernel ->   user   switch + mmdrop() active
 	 *   user ->   user   switch
+	 *
+	 * kernel -> kernel and !prev->active_mm->mm_users:
+	 *   switch to init_mm + mmgrab() + mmdrop()
 	 */
-	if (!next->mm) {                                // to kernel
+	if (!next_mm) {					// to kernel
+		/*
+		 * Checking is only done on kernel -> kernel transition
+		 * to avoid any performance overhead while user tasks
+		 * are running.
+		 */
+		if (unlikely(!prev->mm &&
+			     !atomic_read(&prev->active_mm->mm_users))) {
+			next_mm = next->active_mm = &init_mm;
+			mmgrab(next_mm);
+			goto mm_switch;
+		}
 		enter_lazy_tlb(prev->active_mm, next);
 
 		next->active_mm = prev->active_mm;
@@ -3239,6 +3255,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		else
 			prev->active_mm = NULL;
 	} else {                                        // to user
+mm_switch:
 		/*
 		 * sys_membarrier() requires an smp_mb() between setting
 		 * rq->curr and returning to userspace.
@@ -3248,7 +3265,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		 * finish_task_switch()'s mmdrop().
 		 */
 
-		switch_mm_irqs_off(prev->active_mm, next->mm, next);
+		switch_mm_irqs_off(prev->active_mm, next_mm, next);
 
 		if (!prev->mm) {                        // from kernel
 			/* will mmdrop() in finish_task_switch(). */
Signature · 1 line
2.18.1
#1 lore raw
Quoted 71 lines · Waiman Long

It was found that a dying mm_struct where the owning task has exited can stay on as active_mm of kernel threads as long as no other user tasks run on those CPUs that use it as active_mm. This prolongs the life time of dying mm holding up some resources that cannot be freed on a mostly idle system.

Fix that by forcing the kernel threads to use init_mm as the active_mm during a kernel thread to kernel thread transition if the previous active_mm is dying (!mm_users). This will allows the freeing of resources associated with the dying mm ASAP.

The presence of a kernel-to-kernel thread transition indicates that the cpu is probably idling with no higher priority user task to run. So the overhead of loading the mm_users cacheline should not really matter in this case.

My testing on an x86 system showed that the mm_struct was freed within seconds after the task exited instead of staying alive for minutes or even longer on a mostly idle system before this patch.

---
 kernel/sched/core.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/sched/core.c +18 -1
index 795077af4f1a..41997e676251 100644
@@ -3214,6 +3214,8 @@ static __always_inline struct rq *
 context_switch(struct rq *rq, struct task_struct *prev,
 	       struct task_struct *next, struct rq_flags *rf)
 {
+	struct mm_struct *next_mm = next->mm;
+
 	prepare_task_switch(rq, prev, next);
 
 	/*
@@ -3229,8 +3231,22 @@ context_switch(struct rq *rq, struct task_struct *prev,
 	 *
 	 * kernel ->   user   switch + mmdrop() active
 	 *   user ->   user   switch
+	 *
+	 * kernel -> kernel and !prev->active_mm->mm_users:
+	 *   switch to init_mm + mmgrab() + mmdrop()
 	 */
-	if (!next->mm) {                                // to kernel
+	if (!next_mm) {					// to kernel
+		/*
+		 * Checking is only done on kernel -> kernel transition
+		 * to avoid any performance overhead while user tasks
+		 * are running.
+		 */
+		if (unlikely(!prev->mm &&
+			     !atomic_read(&prev->active_mm->mm_users))) {
+			next_mm = next->active_mm = &init_mm;
+			mmgrab(next_mm);
+			goto mm_switch;
+		}
 		enter_lazy_tlb(prev->active_mm, next);
 
 		next->active_mm = prev->active_mm;
@@ -3239,6 +3255,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		else
 			prev->active_mm = NULL;
 	} else {                                        // to user
+mm_switch:
 		/*
 		 * sys_membarrier() requires an smp_mb() between setting
 		 * rq->curr and returning to userspace.

kernel/sched/core.c context_switch(struct rq *rq, struct task_struct *prev, · line 3271

Quoted 8 lines
kernel/sched/core.c +1 -1
@@ -3248,7 +3265,7 @@ context_switch(struct rq *rq, struct task_struct *prev,
 		 * finish_task_switch()'s mmdrop().
 		 */
 
-		switch_mm_irqs_off(prev->active_mm, next->mm, next);
+		switch_mm_irqs_off(prev->active_mm, next_mm, next);
 
 		if (!prev->mm) {                        // from kernel
 			/* will mmdrop() in finish_task_switch(). */

OK, this is my final push.

My previous statements are not totally correct. Many of the resources are indeed freed when mm_users reaches 0. However, I still think it is an issue to let the a dying mm structure to stay alive for minutes or even longer. I am totally fine if you think it is not worth doing.

Thanks,
Longman

#2 lore raw
In reply to Waiman Long
Quoted 5 lines · Waiman Long

It was found that a dying mm_struct where the owning task has exited can stay on as active_mm of kernel threads as long as no other user tasks run on those CPUs that use it as active_mm. This prolongs the life time of dying mm holding up some resources that cannot be freed on a mostly idle system.

On what kernels does this happen?

Don't we explicitly flush all lazy TLB CPUs at exit time, when we are about to free page tables?

Does this happen only on the CPU where the task in question is exiting, or also on other CPUs?

If it is only on the CPU where the task is exiting, would the TASK_DEAD handling in finish_task_switch() be a better place to handle this?

Quoted 81 lines

Fix that by forcing the kernel threads to use init_mm as the active_mm
during a kernel thread to kernel thread transition if the previous active_mm is dying (!mm_users). This will allows the freeing of resources
associated with the dying mm ASAP.

The presence of a kernel-to-kernel thread transition indicates that the cpu is probably idling with no higher priority user task to run. So the overhead of loading the mm_users cacheline should not really matter in this case.

My testing on an x86 system showed that the mm_struct was freed within
seconds after the task exited instead of staying alive for minutes or even longer on a mostly idle system before this patch.

---
 kernel/sched/core.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/sched/core.c +17 -1
index 795077af4f1a..41997e676251 100644
@@ -3214,6 +3214,8 @@ static __always_inline struct rq *
 context_switch(struct rq *rq, struct task_struct *prev,
 	       struct task_struct *next, struct rq_flags *rf)
 {
+	struct mm_struct *next_mm = next->mm;
+
 	prepare_task_switch(rq, prev, next);
 
 	/*
@@ -3229,8 +3231,22 @@ context_switch(struct rq *rq, struct
 task_struct *prev,
 	 *
 	 * kernel ->   user   switch + mmdrop() active
 	 *   user ->   user   switch
+	 *
+	 * kernel -> kernel and !prev->active_mm->mm_users:
+	 *   switch to init_mm + mmgrab() + mmdrop()
 	 */
-	if (!next->mm) {                                // to kernel
+	if (!next_mm) {					// to kernel
+		/*
+		 * Checking is only done on kernel -> kernel transition
+		 * to avoid any performance overhead while user tasks
+		 * are running.
+		 */
+		if (unlikely(!prev->mm &&
+			     !atomic_read(&prev->active_mm->mm_users))) 
 {
+			next_mm = next->active_mm = &init_mm;
+			mmgrab(next_mm);
+			goto mm_switch;
+		}
 		enter_lazy_tlb(prev->active_mm, next);
kernel/sched/core.c +1 -0
 		next->active_mm = prev->active_mm;
@@ -3239,6 +3255,7 @@ context_switch(struct rq *rq, struct
 task_struct *prev,
 		else
 			prev->active_mm = NULL;
 	} else {                                        // to user
+mm_switch:
 		/*
 		 * sys_membarrier() requires an smp_mb() between

setting

kernel/sched/core.c +1 -1
 		 * rq->curr and returning to userspace.
@@ -3248,7 +3265,7 @@ context_switch(struct rq *rq, struct
 task_struct *prev,
 		 * finish_task_switch()'s mmdrop().
 		 */
 
-		switch_mm_irqs_off(prev->active_mm, next->mm, next);
+		switch_mm_irqs_off(prev->active_mm, next_mm, next);
 
 		if (!prev->mm) {                        // from kernel

/* will mmdrop() in finish_task_switch(). */

Signature · 1 line
All Rights Reversed.
#3 lore raw
Quoted 10 lines · Rik van Riel
Quoted 5 lines · Waiman Long

It was found that a dying mm_struct where the owning task has exited can stay on as active_mm of kernel threads as long as no other user tasks run on those CPUs that use it as active_mm. This prolongs the life time of dying mm holding up some resources that cannot be freed on a mostly idle system.

On what kernels does this happen?

Don't we explicitly flush all lazy TLB CPUs at exit time, when we are about to free page tables?

There are still a couple of calls that will be done until mm_count reaches 0:

- mm_free_pgd(mm);
- destroy_context(mm);
- mmu_notifier_mm_destroy(mm);
- check_mm(mm);
- put_user_ns(mm->user_ns);

These are not big items, but holding it off for a long time is still not a good thing.

Quoted 2 lines

Does this happen only on the CPU where the task in question is exiting, or also on other CPUs?

What I have found is that a long running process on a mostly idle system with many CPUs is likely to cycle through a lot of the CPUs during its lifetime and leave behind its mm in the active_mm of those CPUs.  My 2-socket test system have 96 logical CPUs. After running the test program for a minute or so, it leaves behind its mm in about half of the CPUs with a mm_count of 45 after exit. So the dying mm will stay until all those 45 CPUs get new user tasks to run.

Quoted 4 lines

If it is only on the CPU where the task is exiting, would the TASK_DEAD handling in finish_task_switch() be a better place to handle this?

I need to switch the mm off the dying one. mm switching is only done in context_switch(). I don't think finish_task_switch() is the right place.

-Longman

#4 lore raw
Quoted 11 lines · Waiman Long

What I have found is that a long running process on a mostly idle system
with many CPUs is likely to cycle through a lot of the CPUs during its
lifetime and leave behind its mm in the active_mm of those CPUs. My 2-socket test system have 96 logical CPUs. After running the test program for a minute or so, it leaves behind its mm in about half of the
CPUs with a mm_count of 45 after exit. So the dying mm will stay until
all those 45 CPUs get new user tasks to run.

OK. On what kernel are you seeing this?

On current upstream, the code in native_flush_tlb_others() will send a TLB flush to every CPU in mm_cpumask() if page table pages have been freed.

That should cause the lazy TLB CPUs to switch to init_mm when the exit->zap_page_range path gets to the point where it frees page tables.

Quoted 8 lines
Quoted 3 lines

If it is only on the CPU where the task is exiting, would the TASK_DEAD handling in finish_task_switch() be a better place to handle this?

I need to switch the mm off the dying one. mm switching is only done in
context_switch(). I don't think finish_task_switch() is the right place.

mm switching is also done in flush_tlb_func_common, if the CPU received a TLB shootdown IPI while in lazy TLB mode.

Signature · 1 line
All Rights Reversed.
#5 lore raw
Quoted 15 lines · Rik van Riel
Quoted 11 lines · Waiman Long

What I have found is that a long running process on a mostly idle system
with many CPUs is likely to cycle through a lot of the CPUs during its
lifetime and leave behind its mm in the active_mm of those CPUs. My 2-socket test system have 96 logical CPUs. After running the test program for a minute or so, it leaves behind its mm in about half of the
CPUs with a mm_count of 45 after exit. So the dying mm will stay until
all those 45 CPUs get new user tasks to run.

OK. On what kernel are you seeing this?

Quoted 8 lines

On current upstream, the code in native_flush_tlb_others() will send a TLB flush to every CPU in mm_cpumask() if page table pages have been freed.

That should cause the lazy TLB CPUs to switch to init_mm when the exit->zap_page_range path gets to the point where it frees page tables.

I was using the latest upstream 5.3-rc2 kernel. It may be the case that the mm has been switched, but the mm_count field of the active_mm of the kthread is not being decremented until a user task runs on a CPU.

Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

If it is only on the CPU where the task is exiting, would the TASK_DEAD handling in finish_task_switch() be a better place to handle this?

I need to switch the mm off the dying one. mm switching is only done in
context_switch(). I don't think finish_task_switch() is the right place.

mm switching is also done in flush_tlb_func_common, if the CPU received a TLB shootdown IPI while in lazy TLB mode.

I see.

Cheers,
Longman

#6 lore raw
Quoted 27 lines · Waiman Long
Quoted 18 lines · Rik van Riel
Quoted 6 lines · Waiman Long

What I have found is that a long running process on a mostly idle system
with many CPUs is likely to cycle through a lot of the CPUs during
its
lifetime and leave behind its mm in the active_mm of those

Quoted 8 lines

CPUs. My
2-socket test system have 96 logical CPUs. After running the test program for a minute or so, it leaves behind its mm in about half of
the
CPUs with a mm_count of 45 after exit. So the dying mm will stay until
all those 45 CPUs get new user tasks to run.

OK. On what kernel are you seeing this?

Quoted 8 lines

On current upstream, the code in native_flush_tlb_others() will send a TLB flush to every CPU in mm_cpumask() if page table pages have been freed.

That should cause the lazy TLB CPUs to switch to init_mm when the exit->zap_page_range path gets to the point where it frees page tables.

Quoted 5 lines

I was using the latest upstream 5.3-rc2 kernel. It may be the case that
the mm has been switched, but the mm_count field of the active_mm of the
kthread is not being decremented until a user task runs on a CPU.

Is that something we could fix from the TLB flushing code?

When switching to init_mm, drop the refcount on the lazy mm?

That way that overhead is not added to the context switching code.

Signature · 1 line
All Rights Reversed.
#7 lore raw
Quoted 33 lines · Rik van Riel
Quoted 27 lines · Waiman Long
Quoted 18 lines · Rik van Riel
Quoted 6 lines · Waiman Long

What I have found is that a long running process on a mostly idle system
with many CPUs is likely to cycle through a lot of the CPUs during
its
lifetime and leave behind its mm in the active_mm of those

Quoted 8 lines

CPUs. My
2-socket test system have 96 logical CPUs. After running the test program for a minute or so, it leaves behind its mm in about half of
the
CPUs with a mm_count of 45 after exit. So the dying mm will stay until
all those 45 CPUs get new user tasks to run.

OK. On what kernel are you seeing this?

Quoted 8 lines

On current upstream, the code in native_flush_tlb_others() will send a TLB flush to every CPU in mm_cpumask() if page table pages have been freed.

That should cause the lazy TLB CPUs to switch to init_mm when the exit->zap_page_range path gets to the point where it frees page tables.

Quoted 5 lines

I was using the latest upstream 5.3-rc2 kernel. It may be the case that
the mm has been switched, but the mm_count field of the active_mm of the
kthread is not being decremented until a user task runs on a CPU.

Quoted 8 lines

Is that something we could fix from the TLB flushing code?

When switching to init_mm, drop the refcount on the lazy mm?

That way that overhead is not added to the context switching code.

I have thought about that. That will require changing the active_mm of the current task to point to init_mm, for example. Since TLB flush is done in interrupt context, proper coordination between interrupt and process context will require some atomic instruction which will defect the purpose.

Cheers,
Longman

#8 lore raw
Quoted 49 lines · Waiman Long
Quoted 39 lines · Rik van Riel
Quoted 31 lines · Waiman Long
Quoted 22 lines · Rik van Riel
Quoted 10 lines · Waiman Long

What I have found is that a long running process on a mostly idle
system
with many CPUs is likely to cycle through a lot of the CPUs during
its
lifetime and leave behind its mm in the active_mm of those CPUs. My
2-socket test system have 96 logical CPUs. After running the test

Quoted 8 lines

program for a minute or so, it leaves behind its mm in about half
of
the
CPUs with a mm_count of 45 after exit. So the dying mm will stay
until
all those 45 CPUs get new user tasks to run.

OK. On what kernel are you seeing this?

Quoted 8 lines

On current upstream, the code in native_flush_tlb_others() will send a TLB flush to every CPU in mm_cpumask() if page table pages have been freed.

That should cause the lazy TLB CPUs to switch to init_mm when the exit->zap_page_range path gets to the point where it frees page tables.

Quoted 7 lines

I was using the latest upstream 5.3-rc2 kernel. It may be the case
that
the mm has been switched, but the mm_count field of the active_mm of
the
kthread is not being decremented until a user task runs on a CPU.

Quoted 8 lines

Is that something we could fix from the TLB flushing code?

When switching to init_mm, drop the refcount on the lazy mm?

That way that overhead is not added to the context switching code.

Quoted 7 lines

I have thought about that. That will require changing the active_mm of
the current task to point to init_mm, for example. Since TLB flush is done in interrupt context, proper coordination between interrupt and process context will require some atomic instruction which will defect
the purpose.

Would it be possible to work around that by scheduling a work item that drops the active_mm?

After all, a work item runs in a kernel thread, so by the time the work item is run, either the kernel will still be running the mm you want to get rid of as active_mm, or it will have already gotten rid of it earlier.

Signature · 1 line
All Rights Reversed.
#9 lore raw
Quoted 56 lines · Rik van Riel
Quoted 48 lines · Waiman Long
Quoted 39 lines · Rik van Riel
Quoted 31 lines · Waiman Long
Quoted 22 lines · Rik van Riel
Quoted 10 lines · Waiman Long

What I have found is that a long running process on a mostly idle
system
with many CPUs is likely to cycle through a lot of the CPUs during
its
lifetime and leave behind its mm in the active_mm of those CPUs. My
2-socket test system have 96 logical CPUs. After running the test

Quoted 8 lines

program for a minute or so, it leaves behind its mm in about half
of
the
CPUs with a mm_count of 45 after exit. So the dying mm will stay
until
all those 45 CPUs get new user tasks to run.

OK. On what kernel are you seeing this?

Quoted 8 lines

On current upstream, the code in native_flush_tlb_others() will send a TLB flush to every CPU in mm_cpumask() if page table pages have been freed.

That should cause the lazy TLB CPUs to switch to init_mm when the exit->zap_page_range path gets to the point where it frees page tables.

Quoted 7 lines

I was using the latest upstream 5.3-rc2 kernel. It may be the case
that
the mm has been switched, but the mm_count field of the active_mm of
the
kthread is not being decremented until a user task runs on a CPU.

Quoted 8 lines

Is that something we could fix from the TLB flushing code?

When switching to init_mm, drop the refcount on the lazy mm?

That way that overhead is not added to the context switching code.

Quoted 7 lines

I have thought about that. That will require changing the active_mm of
the current task to point to init_mm, for example. Since TLB flush is done in interrupt context, proper coordination between interrupt and process context will require some atomic instruction which will defect
the purpose.

Quoted 8 lines

Would it be possible to work around that by scheduling a work item that drops the active_mm?

After all, a work item runs in a kernel thread, so by the time the work item is run, either the kernel will still be running the mm you want to get rid of as active_mm, or it will have already gotten rid of it earlier.

Yes, that may work.

Thanks,
Longman

#10 lore raw
In reply to Waiman Long
Quoted 15 lines · Waiman Long
Quoted 10 lines · Rik van Riel
Quoted 5 lines · Waiman Long

It was found that a dying mm_struct where the owning task has exited can stay on as active_mm of kernel threads as long as no other user tasks run on those CPUs that use it as active_mm. This prolongs the life time of dying mm holding up some resources that cannot be freed on a mostly idle system.

On what kernels does this happen?

Don't we explicitly flush all lazy TLB CPUs at exit time, when we are about to free page tables?

There are still a couple of calls that will be done until mm_count reaches 0:

Quoted 8 lines

- mm_free_pgd(mm);
- destroy_context(mm);
- mmu_notifier_mm_destroy(mm);
- check_mm(mm);
- put_user_ns(mm->user_ns);

These are not big items, but holding it off for a long time is still not a good thing.

It would be helpful to give a ball park estimation of how much that actually is. If we are talking about few pages worth of pages per idle cpu in the worst case then I am not sure we want to find an elaborate way around that. We are quite likely having more in per-cpu caches in different subsystems already. It is also quite likely that large machines with many CPUs will have a lot of memory as well.

Signature · 2 lines
Michal Hocko
SUSE Labs
#11 lore raw
Quoted 21 lines · Michal Hocko
Quoted 20 lines · Waiman Long
Quoted 10 lines · Rik van Riel
Quoted 5 lines · Waiman Long

It was found that a dying mm_struct where the owning task has exited can stay on as active_mm of kernel threads as long as no other user tasks run on those CPUs that use it as active_mm. This prolongs the life time of dying mm holding up some resources that cannot be freed on a mostly idle system.

On what kernels does this happen?

Don't we explicitly flush all lazy TLB CPUs at exit time, when we are about to free page tables?

There are still a couple of calls that will be done until mm_count reaches 0:

- mm_free_pgd(mm);
- destroy_context(mm);
- mmu_notifier_mm_destroy(mm);
- check_mm(mm);
- put_user_ns(mm->user_ns);

Quoted 8 lines
Quoted 2 lines

These are not big items, but holding it off for a long time is still not a good thing.

It would be helpful to give a ball park estimation of how much that actually is. If we are talking about few pages worth of pages per idle cpu in the worst case then I am not sure we want to find an elaborate way around that. We are quite likely having more in per-cpu caches in different subsystems already. It is also quite likely that large machines with many CPUs will have a lot of memory as well.

I think they are relatively small. So I am not going to pursue it further at this point.

Cheers,
Longman

maintainer
#12 lore raw
In reply to Waiman Long
Quoted 52 lines · Waiman Long

It was found that a dying mm_struct where the owning task has exited can stay on as active_mm of kernel threads as long as no other user tasks run on those CPUs that use it as active_mm. This prolongs the life time of dying mm holding up some resources that cannot be freed on a mostly idle system.

Fix that by forcing the kernel threads to use init_mm as the active_mm during a kernel thread to kernel thread transition if the previous active_mm is dying (!mm_users). This will allows the freeing of resources associated with the dying mm ASAP.

The presence of a kernel-to-kernel thread transition indicates that the cpu is probably idling with no higher priority user task to run. So the overhead of loading the mm_users cacheline should not really matter in this case.

My testing on an x86 system showed that the mm_struct was freed within seconds after the task exited instead of staying alive for minutes or even longer on a mostly idle system before this patch.

---
 kernel/sched/core.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/sched/core.c +10 -1
index 795077af4f1a..41997e676251 100644
@@ -3214,6 +3214,8 @@ static __always_inline struct rq *
 context_switch(struct rq *rq, struct task_struct *prev,
 	       struct task_struct *next, struct rq_flags *rf)
 {
+	struct mm_struct *next_mm = next->mm;
+
 	prepare_task_switch(rq, prev, next);
 
 	/*
@@ -3229,8 +3231,22 @@ context_switch(struct rq *rq, struct task_struct *prev,
 	 *
 	 * kernel ->   user   switch + mmdrop() active
 	 *   user ->   user   switch
+	 *
+	 * kernel -> kernel and !prev->active_mm->mm_users:
+	 *   switch to init_mm + mmgrab() + mmdrop()
 	 */
-	if (!next->mm) {                                // to kernel
+	if (!next_mm) {					// to kernel
+		/*
+		 * Checking is only done on kernel -> kernel transition
+		 * to avoid any performance overhead while user tasks
+		 * are running.

kernel/sched/core.c context_switch(struct rq *rq, struct task_struct *prev, · line 3252

Quoted 10 lines
kernel/sched/core.c +7 -0
+		 */
+		if (unlikely(!prev->mm &&
+			     !atomic_read(&prev->active_mm->mm_users))) {
+			next_mm = next->active_mm = &init_mm;
+			mmgrab(next_mm);
+			goto mm_switch;
+		}
 		enter_lazy_tlb(prev->active_mm, next);
 
 		next->active_mm = prev->active_mm;

So I _really_ hate this complication. I'm thinking if you really care about this the time is much better spend getting rid of the active_mm tracking for x86 entirely.

#13 lore raw
Quoted 62 lines · Peter Zijlstra
Quoted 61 lines · Waiman Long

It was found that a dying mm_struct where the owning task has exited can stay on as active_mm of kernel threads as long as no other user tasks run on those CPUs that use it as active_mm. This prolongs the life time of dying mm holding up some resources that cannot be freed on a mostly idle system.

Fix that by forcing the kernel threads to use init_mm as the active_mm during a kernel thread to kernel thread transition if the previous active_mm is dying (!mm_users). This will allows the freeing of resources associated with the dying mm ASAP.

The presence of a kernel-to-kernel thread transition indicates that the cpu is probably idling with no higher priority user task to run. So the overhead of loading the mm_users cacheline should not really matter in this case.

My testing on an x86 system showed that the mm_struct was freed within seconds after the task exited instead of staying alive for minutes or even longer on a mostly idle system before this patch.

---
 kernel/sched/core.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/sched/core.c +17 -1
index 795077af4f1a..41997e676251 100644
@@ -3214,6 +3214,8 @@ static __always_inline struct rq *
 context_switch(struct rq *rq, struct task_struct *prev,
 	       struct task_struct *next, struct rq_flags *rf)
 {
+	struct mm_struct *next_mm = next->mm;
+
 	prepare_task_switch(rq, prev, next);
 
 	/*
@@ -3229,8 +3231,22 @@ context_switch(struct rq *rq, struct task_struct *prev,
 	 *
 	 * kernel ->   user   switch + mmdrop() active
 	 *   user ->   user   switch
+	 *
+	 * kernel -> kernel and !prev->active_mm->mm_users:
+	 *   switch to init_mm + mmgrab() + mmdrop()
 	 */
-	if (!next->mm) {                                // to kernel
+	if (!next_mm) {					// to kernel
+		/*
+		 * Checking is only done on kernel -> kernel transition
+		 * to avoid any performance overhead while user tasks
+		 * are running.
+		 */
+		if (unlikely(!prev->mm &&
+			     !atomic_read(&prev->active_mm->mm_users))) {
+			next_mm = next->active_mm = &init_mm;
+			mmgrab(next_mm);
+			goto mm_switch;
+		}
 		enter_lazy_tlb(prev->active_mm, next);
 
Quoted 5 lines

kernel/sched/core.c context_switch(struct rq *rq, struct task_struct *prev, · line 3252

Quoted 1 line
kernel/sched/core.c +0 -0
 		next->active_mm = prev->active_mm;

So I _really_ hate this complication. I'm thinking if you really care about this the time is much better spend getting rid of the active_mm tracking for x86 entirely.

That is fine. I won't pursue further. I will take a look at your suggestion when I have time, but it will probably be a while :-)

Cheers,
Longman