Replace mutex with spinlock for tracepoint registration (#2882)

This commit is contained in:
Wang Han
2025-11-07 17:40:49 +08:00
committed by ShirkNeko
parent 53d763cdf9
commit da0e16bd26

View File

@@ -13,6 +13,7 @@
#include <linux/types.h> #include <linux/types.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/version.h> #include <linux/version.h>
#include <linux/spinlock.h>
#include <linux/sched/task_stack.h> #include <linux/sched/task_stack.h>
#include <asm/syscall.h> #include <asm/syscall.h>
#include <trace/events/syscalls.h> #include <trace/events/syscalls.h>
@@ -440,11 +441,12 @@ static struct kprobe *pts_kp = NULL;
#ifdef CONFIG_KRETPROBES #ifdef CONFIG_KRETPROBES
static int tracepoint_reg_count = 0; static int tracepoint_reg_count = 0;
static DEFINE_MUTEX(tracepoint_reg_mutex); static DEFINE_SPINLOCK(tracepoint_reg_lock);
static int syscall_regfunc_handler(struct kretprobe_instance *ri, struct pt_regs *regs) static int syscall_regfunc_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
{ {
mutex_lock(&tracepoint_reg_mutex); unsigned long flags;
spin_lock_irqsave(&tracepoint_reg_lock, flags);
if (tracepoint_reg_count < 1) { if (tracepoint_reg_count < 1) {
// while install our tracepoint, mark our processes // while install our tracepoint, mark our processes
unmark_all_process(); unmark_all_process();
@@ -454,13 +456,14 @@ static int syscall_regfunc_handler(struct kretprobe_instance *ri, struct pt_regs
mark_all_process(); mark_all_process();
} }
tracepoint_reg_count++; tracepoint_reg_count++;
mutex_unlock(&tracepoint_reg_mutex); spin_unlock_irqrestore(&tracepoint_reg_lock, flags);
return 0; return 0;
} }
static int syscall_unregfunc_handler(struct kretprobe_instance *ri, struct pt_regs *regs) static int syscall_unregfunc_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
{ {
mutex_lock(&tracepoint_reg_mutex); unsigned long flags;
spin_lock_irqsave(&tracepoint_reg_lock, flags);
if (tracepoint_reg_count <= 1) { if (tracepoint_reg_count <= 1) {
// while uninstall our tracepoint, unmark all processes // while uninstall our tracepoint, unmark all processes
unmark_all_process(); unmark_all_process();
@@ -470,7 +473,7 @@ static int syscall_unregfunc_handler(struct kretprobe_instance *ri, struct pt_re
ksu_mark_running_process(); ksu_mark_running_process();
} }
tracepoint_reg_count--; tracepoint_reg_count--;
mutex_unlock(&tracepoint_reg_mutex); spin_unlock_irqrestore(&tracepoint_reg_lock, flags);
return 0; return 0;
} }