Skip to contentSkip to conversation

Fix the issue that lowmemkiller fell into a cycle that try to kill a task

PATCH In review Hui Zhu 5 participants 8 messages Sep 23, 2014 – Oct 14, 2014 Staging

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

PATCH 1/1 Fix the issue that lowmemkiller fell into a cycle that try to kill a task

The cause of this issue is when free memroy size is low and a lot of task is trying to shrink the memory, the task that is killed by lowmemkiller cannot get CPU to exit itself.

Fix this issue with change the scheduling policy to SCHED_FIFO if a task's flag is TIF_MEMDIE in lowmemkiller.

---
 drivers/staging/android/lowmemorykiller.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/staging/android/lowmemorykiller.c +4 -0
index b545d3d..ca1ffac 100644
@@ -129,6 +129,10 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
 
 		if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
 		    time_before_eq(jiffies, lowmem_deathpending_timeout)) {
+			struct sched_param param = { .sched_priority = 1 };
+
+			if (p->policy == SCHED_NORMAL)
+				sched_setscheduler(p, SCHED_FIFO, &param);
 			task_unlock(p);
 			rcu_read_unlock();
 			return 0;
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 17 lines · Hui Zhu

The cause of this issue is when free memroy size is low and a lot of task is trying to shrink the memory, the task that is killed by lowmemkiller cannot get CPU to exit itself.

Fix this issue with change the scheduling policy to SCHED_FIFO if a task's flag is TIF_MEMDIE in lowmemkiller.

---
 drivers/staging/android/lowmemorykiller.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/staging/android/lowmemorykiller.c +0 -0
index b545d3d..ca1ffac 100644

drivers/staging/android/lowmemorykiller.c static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc) · line 135

Quoted 7 lines
drivers/staging/android/lowmemorykiller.c +4 -0
@@ -129,6 +129,10 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
 
 		if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
 		    time_before_eq(jiffies, lowmem_deathpending_timeout)) {
+			struct sched_param param = { .sched_priority = 1 };
+
+			if (p->policy == SCHED_NORMAL)
+				sched_setscheduler(p, SCHED_FIFO, &param);

This seems really specific to a specific scheduler pattern now. Isn't there some other way to resolve this?

thanks,

greg k-h

author
#2 lore raw
Quoted 19 lines · Greg KH
Quoted 18 lines · Hui Zhu

The cause of this issue is when free memroy size is low and a lot of task is trying to shrink the memory, the task that is killed by lowmemkiller cannot get CPU to exit itself.

Fix this issue with change the scheduling policy to SCHED_FIFO if a task's flag is TIF_MEMDIE in lowmemkiller.

---
  drivers/staging/android/lowmemorykiller.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/staging/android/lowmemorykiller.c +0 -0
index b545d3d..ca1ffac 100644
@@ -129,6 +129,10 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
 
Quoted 9 lines

drivers/staging/android/lowmemorykiller.c static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc) · line 135

Quoted 6 lines
drivers/staging/android/lowmemorykiller.c +4 -0
  		if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
  		    time_before_eq(jiffies, lowmem_deathpending_timeout)) {
+			struct sched_param param = { .sched_priority = 1 };
+
+			if (p->policy == SCHED_NORMAL)
+				sched_setscheduler(p, SCHED_FIFO, &param);

This seems really specific to a specific scheduler pattern now. Isn't there some other way to resolve this?

I tried to let the task that call lowmemkiller sleep some time when it try to kill same task. But it doesn't work.
I think the issue is that the free memroy size is too low to make more and more tasks come to call lowmemkiller.

Thanks,
Hui

Quoted 5 lines

thanks,

greg k-h

�翳簕.n�+壏煯壄�+%娝�遍荻w�簕.n�+壏{炳G珴�妠ay�蕠跈�,jf"穐殢飦�戧鐉_璁(殠娸"濟�m���G珴�⒏?櫒璀�&x忈秈O曟瑉窔v豝�m��� �鹅�⒏?朓�

#3 lore raw
Quoted 22 lines · 朱辉
Quoted 19 lines · Greg KH
Quoted 18 lines · Hui Zhu

The cause of this issue is when free memroy size is low and a lot of task is trying to shrink the memory, the task that is killed by lowmemkiller cannot get CPU to exit itself.

Fix this issue with change the scheduling policy to SCHED_FIFO if a task's flag is TIF_MEMDIE in lowmemkiller.

---
  drivers/staging/android/lowmemorykiller.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/staging/android/lowmemorykiller.c +0 -0
index b545d3d..ca1ffac 100644
@@ -129,6 +129,10 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
 
Quoted 9 lines
Quoted 9 lines

drivers/staging/android/lowmemorykiller.c static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc) · line 135

Quoted 6 lines
drivers/staging/android/lowmemorykiller.c +4 -0
             if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
                 time_before_eq(jiffies, lowmem_deathpending_timeout)) {
+                    struct sched_param param = { .sched_priority = 1 };
+
+                    if (p->policy == SCHED_NORMAL)
+                            sched_setscheduler(p, SCHED_FIFO, &param);

This seems really specific to a specific scheduler pattern now. Isn't there some other way to resolve this?

hui, how about modify lowmem_deathpending_timeout if we don't touch scheduler pattern?

Quoted 4 lines

I tried to let the task that call lowmemkiller sleep some time when it try to kill same task. But it doesn't work.
I think the issue is that the free memroy size is too low to make more and more tasks come to call lowmemkiller.

I am not opposed to the idea that the task which is selected to be killed should exit ASAP.

I want to make it clear, what is problem for the existing code and which effect we can get by applying this patch.
1. LMK count is increased, which can be reduced by applying this patch?
2. app become more sluggish?

By the way, whether we need to modify out_of_memory() which also try to kill task?

Quoted 8 lines

Thanks,
Hui

Quoted 5 lines

thanks,

greg k-h

author
#4 lore raw
Quoted 33 lines · Weijie Yang
Quoted 22 lines · 朱辉
Quoted 19 lines · Greg KH
Quoted 18 lines · Hui Zhu

The cause of this issue is when free memroy size is low and a lot of task is trying to shrink the memory, the task that is killed by lowmemkiller cannot get CPU to exit itself.

Fix this issue with change the scheduling policy to SCHED_FIFO if a task's flag is TIF_MEMDIE in lowmemkiller.

---
   drivers/staging/android/lowmemorykiller.c | 4 ++++
   1 file changed, 4 insertions(+)
drivers/staging/android/lowmemorykiller.c +0 -0
index b545d3d..ca1ffac 100644
@@ -129,6 +129,10 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
 
Quoted 9 lines
Quoted 9 lines

drivers/staging/android/lowmemorykiller.c static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc) · line 135

Quoted 6 lines
drivers/staging/android/lowmemorykiller.c +4 -0
              if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
                  time_before_eq(jiffies, lowmem_deathpending_timeout)) {
+                    struct sched_param param = { .sched_priority = 1 };
+
+                    if (p->policy == SCHED_NORMAL)
+                            sched_setscheduler(p, SCHED_FIFO, &param);

This seems really specific to a specific scheduler pattern now. Isn't there some other way to resolve this?

Quoted 2 lines

hui, how about modify lowmem_deathpending_timeout if we don't touch scheduler pattern?

I tried to change line "lowmem_deathpending_timeout = jiffies + HZ" to "lowmem_deathpending_timeout = jiffies + HZ * 10".
But the issue can be reproduced sometimes.
Could you give me some comments on this part?

Quoted 6 lines
Quoted 5 lines

I tried to let the task that call lowmemkiller sleep some time when it try to kill same task. But it doesn't work.
I think the issue is that the free memroy size is too low to make more and more tasks come to call lowmemkiller.

Quoted 6 lines

I am not opposed to the idea that the task which is selected to be killed should exit ASAP.

I want to make it clear, what is problem for the existing code and which effect we can get by applying this patch.
1. LMK count is increased, which can be reduced by applying this patch?

I think the free mem size will grow faster than without this patch.

Quoted 1 line

2. app become more sluggish?

I didn't get that in my part.

Quoted 3 lines

By the way, whether we need to modify out_of_memory() which also try to kill task?

I am not sure because LMK handle the memory issue early than OOM.
But I think this issue will not affect OOM because OOM has oom_zonelist_trylock and oom_zonelist_unlock.

Thanks,
Hui

Quoted 10 lines
Quoted 9 lines

Thanks,
Hui

Quoted 5 lines

thanks,

greg k-h

�翳簕.n�+壏煯壄�+%娝�遍荻w�簕.n�+壏{炳G珴�妠ay�蕠跈�,jf"穐殢飦�戧鐉_璁(殠娸"濟�m���G珴�⒏?櫒璀�&x忈秈O曟瑉窔v豝�m��� �鹅�⒏?朓�

#5 lore raw
Quoted 6 lines · 朱辉
Quoted 2 lines

By the way, whether we need to modify out_of_memory() which also try to kill task?

I am not sure because LMK handle the memory issue early than OOM.
But I think this issue will not affect OOM because OOM has oom_zonelist_trylock and oom_zonelist_unlock.

The oom killer tries to schedule other processes that exit the oom killer without being a victim itself and any process that cannot get the zonelist lock will do the same in the page allocator. The low memory killer case is somewhat different.

#6 lore raw
In reply to Hui Zhu
Quoted 6 lines · Hui Zhu

The cause of this issue is when free memroy size is low and a lot of task is trying to shrink the memory, the task that is killed by lowmemkiller cannot get CPU to exit itself.

Fix this issue with change the scheduling policy to SCHED_FIFO if a task's flag is TIF_MEMDIE in lowmemkiller.

Is it actually true that the task that was killed by lowmemkiller cannot get CPU time?

It is also possible that the task is busy in the kernel, for example in the reclaim code, and is not breaking out of some loop fast enough, despite the TIF_MEMDIE flag being set.

I suspect SCHED_FIFO simply papers over that kind of issue, by not letting anything else run until the task is gone, instead of fixing the root cause of the problem.

author
#7 lore raw

2014 09 24 23:36, Rik van Riel:

Quoted 10 lines · Hui Zhu
Quoted 6 lines

The cause of this issue is when free memroy size is low and a lot of task is trying to shrink the memory, the task that is killed by lowmemkiller cannot get CPU to exit itself.

Fix this issue with change the scheduling policy to SCHED_FIFO if a task's flag is TIF_MEMDIE in lowmemkiller.

Is it actually true that the task that was killed by lowmemkiller cannot get CPU time?

I am so sorry that answer this mail late because I tried to do more test around it.
But this issue is really hard to reproduce the issue. I got a special app that can reproduce this issue easyly. But I still need retry a lot of times to repdroduce this issue.

And I found that most of time, the task cannot be killed because it is blocked by binder_lock.
It looks like there are something wrong with a task that get binder_lock and it is blocked by another thing.

So I make a patch that change a binder_lock to binder_lock_killable to handle this issue.(I will post it later)
It work sometime but I am not sure it is right.
And I just met one time, the kernel with the binder patch and without the lowmemkiller SCHED_FIFO patch, a task that didn't blocked by a lock.
And different tasks call lowmemkiller tried to kill this task.
I think the root cause of this issue is killed task cannot get cpu. But I just got this issue one time.

Quoted 10 lines

It is also possible that the task is busy in the kernel, for example in the reclaim code, and is not breaking out of some loop fast enough, despite the TIF_MEMDIE flag being set.

I suspect SCHED_FIFO simply papers over that kind of issue, by not letting anything else run until the task is gone, instead of fixing the root cause of the problem.

According to I introduction, I think lowmemkiller SCHED_FIFO patch maybe can handle some issue.

Thanks,
Hui �翳簕.n�+壏煯壄�+%娝�遍荻w�簕.n�+壏{炳G珴�妠ay�蕠跈�,jf"穐殢飦�戧鐉_璁(殠娸"濟�m���G珴�⒏?櫒璀�&x忈秈O曟瑉窔v豝�m��� �鹅�⒏?朓�