ANDROID: signal: Add vendor hook for memory reaping

Add vendor hook to determine if the memory of a process that received
the SIGKILL can be reaped.

Bug: 189803002
Bug: 236430563
Change-Id: Ie6802b9bf93ddffb0ceef615d7cca40c23219e57
Signed-off-by: Charan Teja Reddy <charante@codeaurora.org>
Signed-off-by: Isaac J. Manjarres <quic_isaacm@quicinc.com>
This commit is contained in:
Charan Teja Reddy
2021-06-02 17:33:03 +05:30
committed by Suren Baghdasaryan
parent 8deaa6958e
commit 561fe20b66
5 changed files with 55 additions and 10 deletions

View File

@@ -103,6 +103,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_alter_futex_plist_add);
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);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_process_killed);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_rtmutex_wait_start);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_rtmutex_wait_finish);
EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_rwsem_read_wait_start);

View File

@@ -127,4 +127,7 @@ extern struct task_struct *find_lock_task_mm(struct task_struct *p);
extern int sysctl_oom_dump_tasks;
extern int sysctl_oom_kill_allocating_task;
extern int sysctl_panic_on_oom;
/* call for adding killed process to reaper. */
extern void add_to_oom_reaper(struct task_struct *p);
#endif /* _INCLUDE_LINUX_OOM_H */

View File

@@ -12,6 +12,9 @@ struct task_struct;
DECLARE_HOOK(android_vh_do_send_sig_info,
TP_PROTO(int sig, struct task_struct *killer, struct task_struct *dst),
TP_ARGS(sig, killer, dst));
DECLARE_HOOK(android_vh_process_killed,
TP_PROTO(struct task_struct *task, bool *reap),
TP_ARGS(task, reap));
#endif /* _TRACE_HOOK_SIGNAL_H */
/* This part must be outside protection */
#include <trace/define_trace.h>

View File

@@ -45,6 +45,7 @@
#include <linux/posix-timers.h>
#include <linux/cgroup.h>
#include <linux/audit.h>
#include <linux/oom.h>
#define CREATE_TRACE_POINTS
#include <trace/events/signal.h>
@@ -1440,8 +1441,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_process_killed(current, &reap);
if (reap)
add_to_oom_reaper(p);
}
}
return ret;
}

View File

@@ -662,10 +662,8 @@ static int oom_reaper(void *unused)
return 0;
}
static void wake_oom_reaper(struct timer_list *timer)
static void __wake_oom_reaper(struct task_struct *tsk)
{
struct task_struct *tsk = container_of(timer, struct task_struct,
oom_reaper_timer);
struct mm_struct *mm = tsk->signal->oom_mm;
unsigned long flags;
@@ -683,6 +681,13 @@ static void wake_oom_reaper(struct timer_list *timer)
wake_up(&oom_reaper_wait);
}
static void wake_oom_reaper(struct timer_list *timer)
{
struct task_struct *tsk = container_of(timer, struct task_struct,
oom_reaper_timer);
__wake_oom_reaper(tsk);
}
/*
* Give the OOM victim time to exit naturally before invoking the oom_reaping.
* The timers timeout is arbitrary... the longer it is, the longer the worst
@@ -716,6 +721,20 @@ 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);
set_bit(MMF_OOM_VICTIM, &mm->flags);
}
}
/**
* mark_oom_victim - mark the given task as OOM victim
* @tsk: task to mark
@@ -728,18 +747,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);
set_bit(MMF_OOM_VICTIM, &mm->flags);
}
__mark_oom_victim(tsk);
/*
* Make sure that the task is woken up from uninterruptible sleep
@@ -1253,3 +1267,18 @@ put_pid:
return -ENOSYS;
#endif /* CONFIG_MMU */
}
void add_to_oom_reaper(struct task_struct *p)
{
p = find_lock_task_mm(p);
if (!p)
return;
get_task_struct(p);
if (task_will_free_mem(p)) {
__mark_oom_victim(p);
__wake_oom_reaper(p);
}
task_unlock(p);
put_task_struct(p);
}