Skip to content

Commit 83dbbdb

Browse files
rientjestorvalds
authored andcommitted
android, lowmemorykiller: remove task handoff notifier
The task handoff notifier leaks task_struct since it never gets freed after the callback returns NOTIFY_OK, which means it is responsible for doing so. It turns out the lowmemorykiller actually doesn't need this notifier at all. It's used to prevent unnecessary killing by waiting for a thread to exit as a result of lowmem_shrink(), however, it's possible to do this in the same way the kernel oom killer works by setting TIF_MEMDIE and avoid killing if we're still waiting for it to exit. The kernel oom killer will already automatically set TIF_MEMDIE for threads that are attempting to allocate memory that have a fatal signal. The thread selected by lowmem_shrink() will have such a signal after the lowmemorykiller sends it a SIGKILL, so this won't result in an unnecessary use of memory reserves for the thread to exit. This has the added benefit that we don't have to rely on CONFIG_PROFILING to prevent needlessly killing tasks. Reported-by: Werner Landgraf <w.landgraf@ru.ru> Cc: stable@vger.kernel.org Signed-off-by: David Rientjes <rientjes@google.com> Acked-by: Colin Cross <ccross@android.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0034102 commit 83dbbdb

File tree

1 file changed

+7
-41
lines changed

1 file changed

+7
-41
lines changed

drivers/staging/android/lowmemorykiller.c

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ static int lowmem_minfree[6] = {
5555
};
5656
static int lowmem_minfree_size = 4;
5757

58-
static struct task_struct *lowmem_deathpending;
5958
static unsigned long lowmem_deathpending_timeout;
6059

6160
#define lowmem_print(level, x...) \
@@ -64,24 +63,6 @@ static unsigned long lowmem_deathpending_timeout;
6463
printk(x); \
6564
} while (0)
6665

67-
static int
68-
task_notify_func(struct notifier_block *self, unsigned long val, void *data);
69-
70-
static struct notifier_block task_nb = {
71-
.notifier_call = task_notify_func,
72-
};
73-
74-
static int
75-
task_notify_func(struct notifier_block *self, unsigned long val, void *data)
76-
{
77-
struct task_struct *task = data;
78-
79-
if (task == lowmem_deathpending)
80-
lowmem_deathpending = NULL;
81-
82-
return NOTIFY_OK;
83-
}
84-
8566
static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
8667
{
8768
struct task_struct *tsk;
@@ -97,19 +78,6 @@ static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
9778
int other_file = global_page_state(NR_FILE_PAGES) -
9879
global_page_state(NR_SHMEM);
9980

100-
/*
101-
* If we already have a death outstanding, then
102-
* bail out right away; indicating to vmscan
103-
* that we have nothing further to offer on
104-
* this pass.
105-
*
106-
* Note: Currently you need CONFIG_PROFILING
107-
* for this to work correctly.
108-
*/
109-
if (lowmem_deathpending &&
110-
time_before_eq(jiffies, lowmem_deathpending_timeout))
111-
return 0;
112-
11381
if (lowmem_adj_size < array_size)
11482
array_size = lowmem_adj_size;
11583
if (lowmem_minfree_size < array_size)
@@ -148,6 +116,12 @@ static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
148116
if (!p)
149117
continue;
150118

119+
if (test_tsk_thread_flag(p, TIF_MEMDIE) &&
120+
time_before_eq(jiffies, lowmem_deathpending_timeout)) {
121+
task_unlock(p);
122+
rcu_read_unlock();
123+
return 0;
124+
}
151125
oom_score_adj = p->signal->oom_score_adj;
152126
if (oom_score_adj < min_score_adj) {
153127
task_unlock(p);
@@ -174,15 +148,9 @@ static int lowmem_shrink(struct shrinker *s, struct shrink_control *sc)
174148
lowmem_print(1, "send sigkill to %d (%s), adj %d, size %d\n",
175149
selected->pid, selected->comm,
176150
selected_oom_score_adj, selected_tasksize);
177-
/*
178-
* If CONFIG_PROFILING is off, then we don't want to stall
179-
* the killer by setting lowmem_deathpending.
180-
*/
181-
#ifdef CONFIG_PROFILING
182-
lowmem_deathpending = selected;
183151
lowmem_deathpending_timeout = jiffies + HZ;
184-
#endif
185152
send_sig(SIGKILL, selected, 0);
153+
set_tsk_thread_flag(selected, TIF_MEMDIE);
186154
rem -= selected_tasksize;
187155
}
188156
lowmem_print(4, "lowmem_shrink %lu, %x, return %d\n",
@@ -198,15 +166,13 @@ static struct shrinker lowmem_shrinker = {
198166

199167
static int __init lowmem_init(void)
200168
{
201-
task_handoff_register(&task_nb);
202169
register_shrinker(&lowmem_shrinker);
203170
return 0;
204171
}
205172

206173
static void __exit lowmem_exit(void)
207174
{
208175
unregister_shrinker(&lowmem_shrinker);
209-
task_handoff_unregister(&task_nb);
210176
}
211177

212178
module_param_named(cost, lowmem_shrinker.seeks, int, S_IRUGO | S_IWUSR);

0 commit comments

Comments
 (0)