From c05c6a233caff06d5c9fe07c62f6f8ca063da4d5 Mon Sep 17 00:00:00 2001 From: liuxudong5 Date: Thu, 27 Jul 2023 19:26:47 +0800 Subject: [PATCH 1/4] ANDROID: vendor_hooks:vendor hook for percpu-rwsem We need a new vendor hook for two reasons: 1.The position of the previous vendor hook is inappropriate: when the task wakes up from percpu_rwsem_wait, it will enter a long runnable state, which will cause frame loss when the application starts. In order to solve this problem, we need to let the process enter the "vip" queue when it is woken up, so we need to set a flag for the process holding the lock to prove that it is about to hold the lock. The timing of setting the flag should be at the beginning of percpu_down_read/percpu_down_write rather than the end. 2.Most of this long runnable state occurs in the cgroup_threadgroup_rwsem, so we only care cgroup_threadgroup_rwsem, and cgroup_threadgroup_rwsem should be exported. At the same time, one more parameter "struct percpu_rw_semaphore *sem", is needed for this vendor hook. Bug: 300614317 Bug: 300361495 Bug: 294496814 Change-Id: I5f014cfb68a60c29bbfd21452336e381e31e81b1 Signed-off-by: liuxudong5 --- drivers/android/vendor_hooks.c | 1 + include/linux/percpu-rwsem.h | 7 +++++++ include/trace/hooks/dtask.h | 4 ++++ kernel/locking/percpu-rwsem.c | 17 +++++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/drivers/android/vendor_hooks.c b/drivers/android/vendor_hooks.c index 229f0e712f93..3852fc7543df 100644 --- a/drivers/android/vendor_hooks.c +++ b/drivers/android/vendor_hooks.c @@ -186,6 +186,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_permit_before_init); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_permit_after_init); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_selinux_is_initialized); EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_shmem_get_folio); +EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_record_pcpu_rwsem_time_early); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_check_mmap_file); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_check_file_open); EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_check_bpf_syscall); diff --git a/include/linux/percpu-rwsem.h b/include/linux/percpu-rwsem.h index ebd81e03f3c1..7734ef905011 100644 --- a/include/linux/percpu-rwsem.h +++ b/include/linux/percpu-rwsem.h @@ -23,6 +23,9 @@ struct percpu_rw_semaphore { #endif }; +void _trace_android_vh_record_pcpu_rwsem_time_early( + unsigned long settime, struct percpu_rw_semaphore *sem); + #ifdef CONFIG_DEBUG_LOCK_ALLOC #define __PERCPU_RWSEM_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }, #else @@ -54,6 +57,8 @@ static inline void percpu_down_read(struct percpu_rw_semaphore *sem) rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); preempt_disable(); + _trace_android_vh_record_pcpu_rwsem_time_early(jiffies, sem); + /* * We are in an RCU-sched read-side critical section, so the writer * cannot both change sem->state from readers_fast and start checking @@ -93,6 +98,7 @@ static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem) */ if (ret) { + _trace_android_vh_record_pcpu_rwsem_time_early(jiffies, sem); _trace_android_vh_record_pcpu_rwsem_starttime(current, jiffies); rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_); } @@ -124,6 +130,7 @@ static inline void percpu_up_read(struct percpu_rw_semaphore *sem) this_cpu_dec(*sem->read_count); rcuwait_wake_up(&sem->writer); } + _trace_android_vh_record_pcpu_rwsem_time_early(0, sem); _trace_android_vh_record_pcpu_rwsem_starttime(current, 0); preempt_enable(); } diff --git a/include/trace/hooks/dtask.h b/include/trace/hooks/dtask.h index 1552b71c1792..f5cdfe9b04f9 100644 --- a/include/trace/hooks/dtask.h +++ b/include/trace/hooks/dtask.h @@ -15,6 +15,7 @@ struct mutex; struct rt_mutex_base; struct rw_semaphore; struct task_struct; +struct percpu_rw_semaphore; DECLARE_HOOK(android_vh_mutex_wait_start, TP_PROTO(struct mutex *lock), @@ -80,6 +81,9 @@ DECLARE_HOOK(android_vh_record_rwsem_lock_starttime, DECLARE_HOOK(android_vh_record_pcpu_rwsem_starttime, TP_PROTO(struct task_struct *tsk, unsigned long settime_jiffies), TP_ARGS(tsk, settime_jiffies)); +DECLARE_HOOK(android_vh_record_pcpu_rwsem_time_early, + TP_PROTO(unsigned long settime_jiffies, struct percpu_rw_semaphore *sem), + TP_ARGS(settime_jiffies, sem)); struct mutex_waiter; DECLARE_HOOK(android_vh_alter_mutex_list_add, diff --git a/kernel/locking/percpu-rwsem.c b/kernel/locking/percpu-rwsem.c index 084aedde3d0f..8b64155ebb1f 100644 --- a/kernel/locking/percpu-rwsem.c +++ b/kernel/locking/percpu-rwsem.c @@ -26,6 +26,20 @@ void _trace_android_vh_record_pcpu_rwsem_starttime(struct task_struct *tsk, } EXPORT_SYMBOL_GPL(_trace_android_vh_record_pcpu_rwsem_starttime); +/* + * trace_android_vh_record_pcpu_rwsem_time_early is called in + * include/linux/percpu-rwsem.h by including include/hooks/dtask.h, which + * will result to build-err. So we create + * func: _trace_android_vh_record_pcpu_rwsem_time_early for percpu-rwsem.h to call. +*/ + +void _trace_android_vh_record_pcpu_rwsem_time_early( + unsigned long settime, struct percpu_rw_semaphore *sem) +{ + trace_android_vh_record_pcpu_rwsem_time_early(settime, sem); +} +EXPORT_SYMBOL_GPL(_trace_android_vh_record_pcpu_rwsem_time_early); + int __percpu_init_rwsem(struct percpu_rw_semaphore *sem, const char *name, struct lock_class_key *key) { @@ -242,6 +256,8 @@ void __sched percpu_down_write(struct percpu_rw_semaphore *sem) rwsem_acquire(&sem->dep_map, 0, 0, _RET_IP_); trace_contention_begin(sem, LCB_F_PERCPU | LCB_F_WRITE); + trace_android_vh_record_pcpu_rwsem_time_early(jiffies, sem); + /* Notify readers to take the slow path. */ rcu_sync_enter(&sem->rss); @@ -294,6 +310,7 @@ void percpu_up_write(struct percpu_rw_semaphore *sem) * exclusive write lock because its counting. */ rcu_sync_exit(&sem->rss); + trace_android_vh_record_pcpu_rwsem_time_early(0, sem); trace_android_vh_record_pcpu_rwsem_starttime(current, 0); } EXPORT_SYMBOL_GPL(percpu_up_write); From c15fc62fa00891c63df103006d6088750f0fbf2e Mon Sep 17 00:00:00 2001 From: xiaofeng Date: Tue, 15 Aug 2023 20:57:25 +0800 Subject: [PATCH 2/4] ANDROID: GKI: Update symbol list for xiaomi 2 symbol(s) added 'int __traceiter_android_vh_record_pcpu_rwsem_time_early(unsigned long settime_jiffies, struct percpu_rw_semaphore *sem)' 'struct tracepoint __tracepoint_android_vh_record_pcpu_rwsem_time_early' Bug: 300614317 Bug: 300361495 Bug: 294496814 Change-Id: Ibff9da7be5a5f9ff9cac537ee2bbddc3d34abef8 Signed-off-by: xiaofeng --- android/abi_gki_aarch64.stg | 27 +++++++++++++++++++++++++++ android/abi_gki_aarch64_xiaomi | 4 ++++ 2 files changed, 31 insertions(+) diff --git a/android/abi_gki_aarch64.stg b/android/abi_gki_aarch64.stg index a725534e2b17..6a4879d187be 100644 --- a/android/abi_gki_aarch64.stg +++ b/android/abi_gki_aarch64.stg @@ -311039,6 +311039,13 @@ function { parameter_id: 0x17047654 parameter_id: 0xc9082b19 } +function { + id: 0x9b6602ad + return_type_id: 0x6720d32f + parameter_id: 0x18bd6530 + parameter_id: 0x33756485 + parameter_id: 0x11b57133 +} function { id: 0x9b660b2c return_type_id: 0x6720d32f @@ -329227,6 +329234,15 @@ elf_symbol { type_id: 0x9bd7019d full_name: "__traceiter_android_vh_record_pcpu_rwsem_starttime" } +elf_symbol { + id: 0x1a91ec8c + name: "__traceiter_android_vh_record_pcpu_rwsem_time_early" + is_defined: true + symbol_type: FUNCTION + crc: 0xeeef021b + type_id: 0x9b6602ad + full_name: "__traceiter_android_vh_record_pcpu_rwsem_time_early" +} elf_symbol { id: 0x92518ec5 name: "__traceiter_android_vh_record_rtmutex_lock_starttime" @@ -332485,6 +332501,15 @@ elf_symbol { type_id: 0x18ccbd2c full_name: "__tracepoint_android_vh_record_pcpu_rwsem_starttime" } +elf_symbol { + id: 0x158c4cfa + name: "__tracepoint_android_vh_record_pcpu_rwsem_time_early" + is_defined: true + symbol_type: OBJECT + crc: 0xfb2f7ea7 + type_id: 0x18ccbd2c + full_name: "__tracepoint_android_vh_record_pcpu_rwsem_time_early" +} elf_symbol { id: 0x4568ff8f name: "__tracepoint_android_vh_record_rtmutex_lock_starttime" @@ -385866,6 +385891,7 @@ interface { symbol_id: 0x93303c51 symbol_id: 0x7d069e91 symbol_id: 0x0fa39b81 + symbol_id: 0x1a91ec8c symbol_id: 0x92518ec5 symbol_id: 0x9792c22e symbol_id: 0xe2d75052 @@ -386228,6 +386254,7 @@ interface { symbol_id: 0xb0c197a3 symbol_id: 0x761f292f symbol_id: 0xef7ad117 + symbol_id: 0x158c4cfa symbol_id: 0x4568ff8f symbol_id: 0xe918e2ec symbol_id: 0x13b2fb38 diff --git a/android/abi_gki_aarch64_xiaomi b/android/abi_gki_aarch64_xiaomi index 5a0852cf19f6..bd22553da95c 100644 --- a/android/abi_gki_aarch64_xiaomi +++ b/android/abi_gki_aarch64_xiaomi @@ -332,3 +332,7 @@ #required by xm_ispv4_pcie.ko pci_ioremap_bar pci_disable_pcie_error_reporting + +#required by lock_optimization module + __traceiter_android_vh_record_pcpu_rwsem_time_early + __tracepoint_android_vh_record_pcpu_rwsem_time_early From a63af2386a93fef04c58ed3a81ee0d5bf7a05aab Mon Sep 17 00:00:00 2001 From: liuxudong5 Date: Tue, 15 Aug 2023 11:24:01 +0800 Subject: [PATCH 3/4] ANDROID: vendor_hooks: export cgroup_threadgroup_rwsem When the task wakes up from percpu_rwsem_wait, it will enter a long runnable state, which will cause frame loss when the application starts. In order to solve this problem, we need to let the process enter the "vip" queue when it is woken up, so we need to set a flag for the process holding the lock to prove that it is about to hold the lock. Most of this long runnable state occurs in the cgroup_threadgroup_rwsem, so we only care cgroup_threadgroup_rwsem, and cgroup_threadgroup_rwsem should be exported. Finally, if the semaphore is of cgroup_threadgroup_rwsem type and has a flag, then let it join the "vip" queue. Bug: 300614317 Bug: 300361495 Bug: 297785167 Signed-off-by: liuxudong Change-Id: I2297dfbc2f2681581241f85a3b4fd59415ea67db --- kernel/cgroup/cgroup.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index c827832ff7a2..52352aabef10 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -114,6 +114,7 @@ static DEFINE_SPINLOCK(cgroup_idr_lock); static DEFINE_SPINLOCK(cgroup_file_kn_lock); DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem); +EXPORT_SYMBOL_GPL(cgroup_threadgroup_rwsem); #define cgroup_assert_mutex_or_rcu_locked() \ RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ From 5c1ed513eded33d20e713b1d346e5766251ac98b Mon Sep 17 00:00:00 2001 From: xiaofeng Date: Tue, 15 Aug 2023 21:04:46 +0800 Subject: [PATCH 4/4] ANDROID: GKI: Update symbol list for xiaomi 1 symbol(s) added export cgroup_threadgroup_rwsem Bug: 300614317 Bug: 300361495 Bug: 297785167 Change-Id: I8eb493e719f218f2804bdfb5800049c30992f065 Signed-off-by: xiaofeng --- android/abi_gki_aarch64.stg | 10 ++++++++++ android/abi_gki_aarch64_xiaomi | 1 + 2 files changed, 11 insertions(+) diff --git a/android/abi_gki_aarch64.stg b/android/abi_gki_aarch64.stg index 6a4879d187be..4b4e4a11b4b7 100644 --- a/android/abi_gki_aarch64.stg +++ b/android/abi_gki_aarch64.stg @@ -337609,6 +337609,15 @@ elf_symbol { type_id: 0x751b5661 full_name: "cgroup_taskset_next" } +elf_symbol { + id: 0xb7533de5 + name: "cgroup_threadgroup_rwsem" + is_defined: true + symbol_type: OBJECT + crc: 0x4e1c73cb + type_id: 0x6c952252 + full_name: "cgroup_threadgroup_rwsem" +} elf_symbol { id: 0x7a871d1c name: "check_move_unevictable_pages" @@ -386822,6 +386831,7 @@ interface { symbol_id: 0x4ce62869 symbol_id: 0x6d77f512 symbol_id: 0xb3cbf3c8 + symbol_id: 0xb7533de5 symbol_id: 0x7a871d1c symbol_id: 0x91718d34 symbol_id: 0x65e5fa26 diff --git a/android/abi_gki_aarch64_xiaomi b/android/abi_gki_aarch64_xiaomi index bd22553da95c..5c5426861df6 100644 --- a/android/abi_gki_aarch64_xiaomi +++ b/android/abi_gki_aarch64_xiaomi @@ -336,3 +336,4 @@ #required by lock_optimization module __traceiter_android_vh_record_pcpu_rwsem_time_early __tracepoint_android_vh_record_pcpu_rwsem_time_early + cgroup_threadgroup_rwsem