kprobe: use preempt_{disable, enable}_notrace in kprobe_busy_{begin, end}

Message ID 20230513090548.376522-1-zegao@tencent.com
State New
Headers
Series kprobe: use preempt_{disable, enable}_notrace in kprobe_busy_{begin, end} |

Commit Message

Ze Gao May 13, 2023, 9:05 a.m. UTC
  Replacing preempt_{disable, enable} with its corresponding notrace
version in kprobe_busy_{begin, end} because they are called by
fprobe_kprobe_handler. Such changes would resolve the potential
stack recursion or overflow problem introduced by preempt_count
_{add, sub} when these functions themselves accidentally traced
by fprobe+rethook, and does not break the original semantics.

Signed-off-by: Ze Gao <zegao@tencent.com>
---
 kernel/kprobes.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Masami Hiramatsu (Google) May 31, 2023, 9:44 a.m. UTC | #1
Hi,

On Sat, 13 May 2023 17:05:48 +0800
Ze Gao <zegao2021@gmail.com> wrote:

> Replacing preempt_{disable, enable} with its corresponding notrace
> version in kprobe_busy_{begin, end} because they are called by
> fprobe_kprobe_handler. Such changes would resolve the potential
> stack recursion or overflow problem introduced by preempt_count
> _{add, sub} when these functions themselves accidentally traced
> by fprobe+rethook, and does not break the original semantics.

As I explained below;

https://lore.kernel.org/all/20230517010311.f46db3f78b11cf9d92193527@kernel.org/

we don't need this for kprobes because kprobes already prohibits
probing on preempt_count_{add,sub}, and we don't want to miss
tracing the preempt-off period by kprobes itself.

Thank you,

> 
> Signed-off-by: Ze Gao <zegao@tencent.com>
> ---
>  kernel/kprobes.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index 00e177de91cc..94d01b5d0646 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1242,7 +1242,7 @@ void kprobe_busy_begin(void)
>  {
>  	struct kprobe_ctlblk *kcb;
>  
> -	preempt_disable();
> +	preempt_disable_notrace();
>  	__this_cpu_write(current_kprobe, &kprobe_busy);
>  	kcb = get_kprobe_ctlblk();
>  	kcb->kprobe_status = KPROBE_HIT_ACTIVE;
> @@ -1251,7 +1251,7 @@ void kprobe_busy_begin(void)
>  void kprobe_busy_end(void)
>  {
>  	__this_cpu_write(current_kprobe, NULL);
> -	preempt_enable();
> +	preempt_enable_notrace();
>  }
>  
>  /* Add the new probe to 'ap->list'. */
> -- 
> 2.40.1
>
  

Patch

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index 00e177de91cc..94d01b5d0646 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1242,7 +1242,7 @@  void kprobe_busy_begin(void)
 {
 	struct kprobe_ctlblk *kcb;
 
-	preempt_disable();
+	preempt_disable_notrace();
 	__this_cpu_write(current_kprobe, &kprobe_busy);
 	kcb = get_kprobe_ctlblk();
 	kcb->kprobe_status = KPROBE_HIT_ACTIVE;
@@ -1251,7 +1251,7 @@  void kprobe_busy_begin(void)
 void kprobe_busy_end(void)
 {
 	__this_cpu_write(current_kprobe, NULL);
-	preempt_enable();
+	preempt_enable_notrace();
 }
 
 /* Add the new probe to 'ap->list'. */