From f3b22c78684096edf57aff04e5113c4ddabffcaa Mon Sep 17 00:00:00 2001 From: Qais Yousef Date: Tue, 11 Jul 2023 22:37:40 +0000 Subject: [PATCH] ANDROID: fs/proc: Perform priority inheritance around access_remote_vm() It holds mmap_sem lock which is a hot path. Some debug daemons can end up holding this lock to get the cmdline of a process, which can result in slowing down this process. Add hooks around the calls to allow vendors to implement a simple prio inheritance scheme to deal with this. Bug: 344826816 Bug: 289412815 Signed-off-by: Qais Yousef (cherry picked from commit b821a3c8fc1efe12782061993b4feef026f8ce58) [Trivial conflict in include/trace/hooks/sched.h due to new code added. And in vendor_hooks.c due to code ordering] Signed-off-by: Qais Yousef Change-Id: I160637b30e5bd58d5978b25be8a21ce025175ec3 Signed-off-by: liulu liu --- fs/proc/base.c | 12 ++++++++++++ include/trace/hooks/sched.h | 9 +++++++++ kernel/sched/vendor_hooks.c | 2 ++ 3 files changed, 23 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index 5c8e1e3e4629..8017a774c366 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -101,6 +101,7 @@ #include #include #include +#include #include "internal.h" #include "fd.h" @@ -381,13 +382,24 @@ static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf, size_t count, loff_t *pos) { struct mm_struct *mm; + bool prio_inherited = false; + int saved_prio; ssize_t ret; mm = get_task_mm(tsk); if (!mm) return 0; + /* + * access_remote_vm() holds the hot mmap_sem lock which can cause the + * task for which we read cmdline etc for by some debug deamon to slow + * down and suffer a performance hit. Especially if the reader task has + * a low nice value. + */ + trace_android_vh_prio_inheritance(tsk, &saved_prio, &prio_inherited); ret = get_mm_cmdline(mm, buf, count, pos); + if (prio_inherited) + trace_android_vh_prio_restore(saved_prio); mmput(mm); return ret; } diff --git a/include/trace/hooks/sched.h b/include/trace/hooks/sched.h index e61f4959f3cf..5819b90dc99e 100644 --- a/include/trace/hooks/sched.h +++ b/include/trace/hooks/sched.h @@ -485,6 +485,15 @@ DECLARE_HOOK(android_vh_tick_nohz_idle_stop_tick, DECLARE_HOOK(android_vh_mmput, TP_PROTO(struct mm_struct *mm), TP_ARGS(mm)); + +DECLARE_HOOK(android_vh_prio_inheritance, + TP_PROTO(struct task_struct *p, int *saved_prio, bool *prio_inherited), + TP_ARGS(p, saved_prio, prio_inherited)); + +DECLARE_HOOK(android_vh_prio_restore, + TP_PROTO(int saved_prio), + TP_ARGS(saved_prio)); + /* macro versions of hooks are no longer required */ #endif /* _TRACE_HOOK_SCHED_H */ diff --git a/kernel/sched/vendor_hooks.c b/kernel/sched/vendor_hooks.c index 6d37912d62b9..c510ad83f49b 100644 --- a/kernel/sched/vendor_hooks.c +++ b/kernel/sched/vendor_hooks.c @@ -126,3 +126,5 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_cpu_cgroup_css_alloc); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_cpu_cgroup_css_free); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_dequeue_entity_delayed); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_mmput); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_prio_inheritance); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_prio_restore);