From 3e2dc32f5930c3928c4a241ad11ef752f13b196b Mon Sep 17 00:00:00 2001 From: Dezhi Huang Date: Fri, 26 May 2023 16:33:47 +0800 Subject: [PATCH] ANDROID: mm: create vendor hooks for memory reclaim we try to adjust page reclaim operations based on the running task and kernel memory pressure. Thus, we want to create some vendor hooks into kernel6.1. Firstly, we add ADNRROID_VENDOR_DATA into the struct scan_control, special operations would be performed based on this special scan option. We measure the importance of the current process in the system and obtain its weight, which is recorded in ANDROID_VENDOR_DATA. The hook function: trace_android_vh_modify_scan_control is added inside of the function modify_scan_control() to adjust reclaim operations based on memory pressure. The hook function: trace_android_vh_should_continue_reclaim is added inside of the function shrink_node() to decide if page_reclaim would continue or not based on memory pressure. The hook function: trace_android_vh_file_is_tiny_bypass is added into the function prepare_scan_count() to decide if the file pages should be skipped in condition to file refualts and memory pressure. Bug: 279793370 Change-Id: I1efe9d3e866f37b0295c7cd94ec8ca0117a9bd4a Signed-off-by: Dezhi Huang --- drivers/android/vendor_hooks.c | 3 +++ include/trace/hooks/vmscan.h | 12 ++++++++++++ mm/vmscan.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/drivers/android/vendor_hooks.c b/drivers/android/vendor_hooks.c index 8d209492daf04..3a9010b7bc92f 100644 --- a/drivers/android/vendor_hooks.c +++ b/drivers/android/vendor_hooks.c @@ -266,6 +266,9 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_psci_cpu_suspend); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_psi_event); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_psi_group); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_cpufreq_acct_update_power); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_modify_scan_control); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_should_continue_reclaim); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_file_is_tiny_bypass); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_mmc_resume); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_mmc_suspend); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_exit_signal); diff --git a/include/trace/hooks/vmscan.h b/include/trace/hooks/vmscan.h index 05065be1eb113..0896b1134de51 100644 --- a/include/trace/hooks/vmscan.h +++ b/include/trace/hooks/vmscan.h @@ -24,6 +24,18 @@ DECLARE_HOOK(android_vh_do_shrink_slab, DECLARE_HOOK(android_vh_shrink_node_memcgs, TP_PROTO(struct mem_cgroup *memcg, bool *skip), TP_ARGS(memcg, skip)); +DECLARE_HOOK(android_vh_modify_scan_control, + TP_PROTO(u64 *ext, unsigned long *nr_to_reclaim, + struct mem_cgroup *target_mem_cgroup, + bool *file_is_tiny, bool *may_writepage), + TP_ARGS(ext, nr_to_reclaim, target_mem_cgroup, file_is_tiny, may_writepage)); +DECLARE_HOOK(android_vh_should_continue_reclaim, + TP_PROTO(u64 *ext, unsigned long *nr_to_reclaim, + unsigned long *nr_reclaimed, bool *continue_reclaim), + TP_ARGS(ext, nr_to_reclaim, nr_reclaimed, continue_reclaim)); +DECLARE_HOOK(android_vh_file_is_tiny_bypass, + TP_PROTO(bool file_is_tiny, bool *bypass), + TP_ARGS(file_is_tiny, bypass)); #endif /* _TRACE_HOOK_VMSCAN_H */ /* This part must be outside protection */ #include diff --git a/mm/vmscan.c b/mm/vmscan.c index 1c289d07255f5..e32c7f5328262 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -169,6 +169,7 @@ struct scan_control { /* for recording the reclaimed slab by now */ struct reclaim_state reclaim_state; + ANDROID_VENDOR_DATA(1); }; #ifdef ARCH_HAS_PREFETCHW @@ -2805,6 +2806,7 @@ static void prepare_scan_count(pg_data_t *pgdat, struct scan_control *sc) { unsigned long file; struct lruvec *target_lruvec; + bool bypass = false; if (lru_gen_enabled()) return; @@ -2866,6 +2868,11 @@ static void prepare_scan_count(pg_data_t *pgdat, struct scan_control *sc) else sc->cache_trim_mode = 0; + + trace_android_vh_file_is_tiny_bypass(sc->file_is_tiny, &bypass); + if (bypass) + return; + /* * Prevent the reclaimer from falling into the cache trap: as * cache pages start out inactive, every cache fault will tip @@ -6330,6 +6337,7 @@ static inline bool should_continue_reclaim(struct pglist_data *pgdat, unsigned long pages_for_compaction; unsigned long inactive_lru_pages; int z; + bool continue_reclaim = true; /* If not in reclaim/compaction mode, stop */ if (!in_reclaim_compaction(sc)) @@ -6373,6 +6381,11 @@ static inline bool should_continue_reclaim(struct pglist_data *pgdat, if (can_reclaim_anon_pages(NULL, pgdat->node_id, sc)) inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON); + trace_android_vh_should_continue_reclaim(&sc->android_vendor_data1, + &sc->nr_to_reclaim, &sc->nr_reclaimed, &continue_reclaim); + if (!continue_reclaim) + return false; + return inactive_lru_pages > pages_for_compaction; } @@ -6727,6 +6740,20 @@ static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat) target_lruvec->refaults[WORKINGSET_FILE] = refaults; } +static void modify_scan_control(struct scan_control *sc) +{ + bool file_is_tiny = false, may_writepage = true; + + trace_android_vh_modify_scan_control(&sc->android_vendor_data1, + &sc->nr_to_reclaim, sc->target_mem_cgroup, &file_is_tiny, + &may_writepage); + + if (file_is_tiny) + sc->file_is_tiny = true; + if (!may_writepage) + sc->may_writepage = false; +} + /* * This is the main entry point to direct page reclaim. * @@ -6750,6 +6777,8 @@ static unsigned long do_try_to_free_pages(struct zonelist *zonelist, pg_data_t *last_pgdat; struct zoneref *z; struct zone *zone; + + modify_scan_control(sc); retry: delayacct_freepages_start();