Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 11a71a36 authored by Liam Mark's avatar Liam Mark Committed by Vinayak Menon
Browse files

android/lowmemorykiller: Wait for memory to be freed



The memory reclaim code needs to give time to the system to
return the memory from a killed process otherwise the memory
reclaim code could run continuously, in multiple threads,
which could starve both the watchdog thread and the thread
which is responsible for returning the memory from the
killed process.

Change-Id: Ieded4bfe038ca936247fa4b638070e979b02eaa1
Signed-off-by: default avatarLiam Mark <lmark@codeaurora.org>
Signed-off-by: default avatarVinayak Menon <vinmenon@codeaurora.org>
parent 302cef78
Loading
Loading
Loading
Loading
+22 −5
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@
#include <linux/rcupdate.h>
#include <linux/profile.h>
#include <linux/notifier.h>
#include <linux/mutex.h>
#include <linux/delay.h>

#define CREATE_TRACE_POINTS
#include "trace/lowmemorykiller.h"
@@ -97,6 +99,8 @@ static int test_task_lmk_waiting(struct task_struct *p)
	return 0;
}

static DEFINE_MUTEX(scan_mutex);

static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
{
	struct task_struct *tsk;
@@ -109,8 +113,14 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
	int selected_tasksize = 0;
	short selected_oom_score_adj;
	int array_size = ARRAY_SIZE(lowmem_adj);
	int other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages;
	int other_file = global_node_page_state(NR_FILE_PAGES) -
	int other_free;
	int other_file;

	if (mutex_lock_interruptible(&scan_mutex) < 0)
		return 0;

	other_free = global_page_state(NR_FREE_PAGES) - totalreserve_pages;
	other_file = global_node_page_state(NR_FILE_PAGES) -
					global_node_page_state(NR_SHMEM) -
					global_node_page_state(NR_UNEVICTABLE) -
					total_swapcache_pages();
@@ -134,6 +144,7 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
	if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
		lowmem_print(5, "lowmem_scan %lu, %x, return 0\n",
			     sc->nr_to_scan, sc->gfp_mask);
		mutex_unlock(&scan_mutex);
		return 0;
	}

@@ -150,6 +161,9 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
		if (time_before_eq(jiffies, lowmem_deathpending_timeout)) {
			if (test_task_lmk_waiting(tsk)) {
				rcu_read_unlock();
				/* give the system time to free up the memory */
				msleep_interruptible(20);
				mutex_unlock(&scan_mutex);
				return 0;
			}
		}
@@ -204,11 +218,14 @@ static unsigned long lowmem_scan(struct shrinker *s, struct shrink_control *sc)
			     free);
		lowmem_deathpending_timeout = jiffies + HZ;
		rem += selected_tasksize;
		/* give the system time to free up the memory */
		msleep_interruptible(20);
	}

	lowmem_print(4, "lowmem_scan %lu, %x, return %lu\n",
		     sc->nr_to_scan, sc->gfp_mask, rem);
	rcu_read_unlock();
	mutex_unlock(&scan_mutex);
	return rem;
}