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 <zhengtangquan@oppo.com>
This commit is contained in:
zhengtangquan 2023-09-06 10:39:36 +08:00 committed by Carlos Llamas
parent a94ba5ab28
commit d21d656201
5 changed files with 43 additions and 4 deletions

View File

@ -99,6 +99,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_udp_enqueue_schedule_skb);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_build_skb_around);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_refrigerator);
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);

View File

@ -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 */

View File

@ -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 <trace/define_trace.h>

View File

@ -47,6 +47,7 @@
#include <linux/cgroup.h>
#include <linux/audit.h>
#include <linux/sysctl.h>
#include <linux/oom.h>
#define CREATE_TRACE_POINTS
#include <trace/events/signal.h>
@ -1465,8 +1466,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;
}

View File

@ -748,6 +748,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
@ -761,7 +774,6 @@ static inline void queue_oom_reaper(struct task_struct *tsk)
static void mark_oom_victim(struct task_struct *tsk)
{
const struct cred *cred;
struct mm_struct *mm = tsk->mm;
WARN_ON(oom_killer_disabled);
/* OOM killer might race with memcg OOM */
@ -769,8 +781,7 @@ static void mark_oom_victim(struct task_struct *tsk)
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
@ -1263,3 +1274,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);
}