From 6e5f1821281fd8b67e7033f3a07d14d4d18588f9 Mon Sep 17 00:00:00 2001 From: zhengtangquan Date: Wed, 6 Sep 2023 10:39:36 +0800 Subject: [PATCH] ANDROID: signal: Add vendor hook for memory reap Add vendor hook to determine if the memory of a process that received the SIGKILL can be reaped. Partial cherry-pick of aosp/1724512 & aosp/2093626. Bug: 232062955 Change-Id: I75072bd264df33caff67d083821ee6f33ca83af9 Signed-off-by: Tangquan Zheng --- drivers/android/vendor_hooks.c | 1 + include/linux/oom.h | 2 ++ include/trace/hooks/signal.h | 3 +++ kernel/signal.c | 11 ++++++++++- mm/oom_kill.c | 31 +++++++++++++++++++++++++++---- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/drivers/android/vendor_hooks.c b/drivers/android/vendor_hooks.c index 0c754ba4dd5d..104e4eb48ff9 100644 --- a/drivers/android/vendor_hooks.c +++ b/drivers/android/vendor_hooks.c @@ -85,6 +85,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_binder_set_priority); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_binder_restore_priority); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_binder_wakeup_ilocked); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_do_send_sig_info); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_killed_process); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_mutex_wait_start); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_mutex_wait_finish); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_mutex_init); diff --git a/include/linux/oom.h b/include/linux/oom.h index 7d0c9c48a0c5..f008e23d9b41 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -112,4 +112,6 @@ extern void oom_killer_enable(void); extern struct task_struct *find_lock_task_mm(struct task_struct *p); +/* call for adding killed process to reaper. */ +extern void add_to_oom_reaper(struct task_struct *p); #endif /* _INCLUDE_LINUX_OOM_H */ diff --git a/include/trace/hooks/signal.h b/include/trace/hooks/signal.h index 4e61006cb145..c845aec359d7 100644 --- a/include/trace/hooks/signal.h +++ b/include/trace/hooks/signal.h @@ -14,6 +14,9 @@ DECLARE_HOOK(android_vh_do_send_sig_info, DECLARE_HOOK(android_vh_exit_signal, TP_PROTO(struct task_struct *task), TP_ARGS(task)); +DECLARE_HOOK(android_vh_killed_process, + TP_PROTO(struct task_struct *killer, struct task_struct *dst, bool *reap), + TP_ARGS(killer, dst, reap)); #endif /* _TRACE_HOOK_SIGNAL_H */ /* This part must be outside protection */ #include diff --git a/kernel/signal.c b/kernel/signal.c index 3b3204c26641..2f90f315ec1b 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -45,6 +45,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -1448,8 +1449,16 @@ int group_send_sig_info(int sig, struct kernel_siginfo *info, ret = check_kill_permission(sig, info, p); rcu_read_unlock(); - if (!ret && sig) + if (!ret && sig) { ret = do_send_sig_info(sig, info, p, type); + if (!ret && sig == SIGKILL) { + bool reap = false; + + trace_android_vh_killed_process(current, p, &reap); + if (reap) + add_to_oom_reaper(p); + } + } return ret; } diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 1276e49b31b0..2c5b854f767b 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -745,6 +745,19 @@ static inline void queue_oom_reaper(struct task_struct *tsk) } #endif /* CONFIG_MMU */ +/** + * tsk->mm has to be non NULL and caller has to guarantee it is stable (either + * under task_lock or operate on the current). + */ +static void __mark_oom_victim(struct task_struct *tsk) +{ + struct mm_struct *mm = tsk->mm; + + if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm)) { + mmgrab(tsk->signal->oom_mm); + } +} + /** * mark_oom_victim - mark the given task as OOM victim * @tsk: task to mark @@ -757,16 +770,13 @@ static inline void queue_oom_reaper(struct task_struct *tsk) */ static void mark_oom_victim(struct task_struct *tsk) { - struct mm_struct *mm = tsk->mm; - WARN_ON(oom_killer_disabled); /* OOM killer might race with memcg OOM */ if (test_and_set_tsk_thread_flag(tsk, TIF_MEMDIE)) return; /* oom_mm is bound to the signal struct life time. */ - if (!cmpxchg(&tsk->signal->oom_mm, NULL, mm)) - mmgrab(tsk->signal->oom_mm); + __mark_oom_victim(tsk); /* * Make sure that the task is woken up from uninterruptible sleep @@ -1260,3 +1270,16 @@ put_task: return -ENOSYS; #endif /* CONFIG_MMU */ } + +void add_to_oom_reaper(struct task_struct *p) +{ + p = find_lock_task_mm(p); + if (!p) + return; + + if (task_will_free_mem(p)) { + __mark_oom_victim(p); + queue_oom_reaper(p); + } + task_unlock(p); +}