From e5c2a8ab88f06b6d1259dbc9df3dad6fbcd5cdfa Mon Sep 17 00:00:00 2001 From: Sultan Alsawaf Date: Mon, 26 Apr 2021 16:17:54 -0700 Subject: [PATCH] simple_lmk: Pass a custom swap function to sort() When sort() isn't provided with a custom swap function, it falls back onto its generic implementation of just swapping one byte at a time, which is quite slow. Since we know the type of the objects being sorted, we can provide our own swap function which simply uses the swap() macro. Signed-off-by: Sultan Alsawaf --- drivers/android/simple_lmk.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/android/simple_lmk.c b/drivers/android/simple_lmk.c index a84bf545d41e..5e9ea2a14d9c 100644 --- a/drivers/android/simple_lmk.c +++ b/drivers/android/simple_lmk.c @@ -62,6 +62,14 @@ static int victim_cmp(const void *lhs_ptr, const void *rhs_ptr) return rhs->size - lhs->size; } +static void victim_swap(void *lhs_ptr, void *rhs_ptr, int size) +{ + struct victim_info *lhs = (typeof(lhs))lhs_ptr; + struct victim_info *rhs = (typeof(rhs))rhs_ptr; + + swap(*lhs, *rhs); +} + static bool vtsk_is_duplicate(int vlen, struct task_struct *vtsk) { int i; @@ -137,7 +145,7 @@ static unsigned long find_victims(int *vindex, unsigned short target_adj_min, */ if (pages_found) sort(&victims[old_vindex], *vindex - old_vindex, - sizeof(*victims), victim_cmp, NULL); + sizeof(*victims), victim_cmp, victim_swap); return pages_found; } @@ -198,7 +206,8 @@ static void scan_and_kill(void) * victims that have a lower adj can be killed in place of * smaller victims with a high adj. */ - sort(victims, nr_to_kill, sizeof(*victims), victim_cmp, NULL); + sort(victims, nr_to_kill, sizeof(*victims), victim_cmp, + victim_swap); /* Second round of processing to finally select the victims */ nr_to_kill = process_victims(nr_to_kill);