From 347882f665464bb3cbd2e6921711b95bb7a5fc2c Mon Sep 17 00:00:00 2001 From: huangzq2 Date: Tue, 3 Nov 2020 20:03:19 +0800 Subject: [PATCH] limit ion cache size We often see lost ram is still very high even we close the camera app. That's because camera's ion memory goes into ion cache after use, not buddy system. And this cache was not counted in proc/meminfo/memAvailable, so it can not reflect the true memAvailable in this state. It may affects some components l(CR) camera or lmkd which need evaluate true memAvailable in system. Take camera as example, while launching camera, camera app needs calcuate the available memory then call framework api to kill processes, without this patch, we will see overkill. This patch is only for 4.14 & 4.19, on kernel 5.4, ion cache will be counted in memAvailable, so memAvailable is always trustable. Change-Id: I4a9678530d387bd8da404d797f7d6e33bd258ac4 Signed-off-by: huangzq2 Reviewed-on: https://gerrit.mot.com/1792299 SLTApproved: Slta Waiver SME-Granted: SME Approvals Granted Tested-by: Jira Key Reviewed-by: Xiangpo Zhao Submit-Approved: Jira Key (cherry picked from commit a9acaa732c7fcb5e9ec9eea08bb6bb390c316722) Reviewed-on: https://gerrit.mot.com/1795665 --- drivers/staging/android/ion/ion_system_heap.c | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/drivers/staging/android/ion/ion_system_heap.c b/drivers/staging/android/ion/ion_system_heap.c index 6754d1367359..d2d91fe6f1bf 100644 --- a/drivers/staging/android/ion/ion_system_heap.c +++ b/drivers/staging/android/ion/ion_system_heap.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "ion_system_heap.h" #include "ion.h" @@ -440,6 +441,23 @@ err: return ret; } +/* + * Moto huangzq2: limit the system ion cache size. + * We often see lost ram is still very high even we close the camera app. + * That's because camera's ion memory goes into ion cache after use, not + * buddy system. And this cache was not counted in proc/meminfo/memAvailable, + * so it can not reflect the true memAvailable in this state. It may affects + * some components like camera or lmkd which need evaluate true memAvailable + * in system. Take camera as example, while launching camera, camera app needs + * calcuate the available memory then call framework api to kill processes, + * without this patch, we will see overkill. + * + * This patch is only for 4.14 & 4.19, on kernel 5.4, ion cache will + * be counted in memAvailable, so memAvailable is always trustable. + */ +static int max_page_pool_size = 24300; +module_param(max_page_pool_size, int, 0600); + void ion_system_heap_free(struct ion_buffer *buffer) { struct ion_heap *heap = buffer->heap; @@ -450,6 +468,16 @@ void ion_system_heap_free(struct ion_buffer *buffer) struct scatterlist *sg; int i; int vmid = get_secure_vmid(buffer->flags); + unsigned int count = 0; + + for (i = 0; i < NUM_ORDERS; i++) { + count += ion_page_pool_total(sys_heap->cached_pools[i], true); + count += ion_page_pool_total(sys_heap->uncached_pools[i], true); + } + + if (count > max_page_pool_size) + buffer->private_flags |= ION_PRIV_FLAG_SHRINKER_FREE; + if (!(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE) && !(buffer->flags & ION_FLAG_POOL_FORCE_ALLOC)) {