public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@kernel.org>
To: Ingo Molnar <mingo@kernel.org>, Steven Rostedt <rostedt@goodmis.org>
Cc: "Naveen N . Rao" <naveen.n.rao@linux.vnet.ibm.com>,
	Ananth N Mavinakayanahalli <ananth@linux.ibm.com>,
	linux-kernel@vger.kernel.org, mhiramat@kernel.org,
	x86@kernel.org
Subject: [BUGFIX PATCH -tip] x86/kprobes: Fix to call previous kprobe's fault handler
Date: Mon, 24 May 2021 12:25:36 +0900	[thread overview]
Message-ID: <162182673618.114649.7393137495689996180.stgit@devnote2> (raw)

Fix to call the previous kprobe's fault handler when a page fault
occurred in reentered kprobes.
This may happen if kprobes handler calls a function which can cause
a page fault (e.g. access user page) and another kprobe probes that
instruction.
Without the 2nd kprobe, the 1st kprobe can handle the page fault,
but with the 2nd kprobe, the 1st one can not handle it.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
 arch/x86/kernel/kprobes/core.c |   19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 7c4d0736a998..ac2514f1e195 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -1098,12 +1098,21 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
 		 */
 		regs->flags |= kcb->kprobe_old_flags;
 
-		if (kcb->kprobe_status == KPROBE_REENTER)
-			restore_previous_kprobe(kcb);
-		else
+		if (kcb->kprobe_status != KPROBE_REENTER) {
 			reset_current_kprobe();
-	} else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
-		   kcb->kprobe_status == KPROBE_HIT_SSDONE) {
+			return 0;
+		}
+		restore_previous_kprobe(kcb);
+		/*
+		 * If reentered kprobes caused a page fault, it must be
+		 * handled by the previous kprobe too. But we don't bother
+		 * checking KPROBE_HIT_SS again because kprobes can not
+		 * probe another kprobe's single stepping buffer.
+		 */
+	}
+
+	if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
+	    kcb->kprobe_status == KPROBE_HIT_SSDONE) {
 		/*
 		 * We increment the nmissed count for accounting,
 		 * we can also use npre/npostfault count for accounting


             reply	other threads:[~2021-05-24  3:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-24  3:25 Masami Hiramatsu [this message]
2021-05-24  5:30 ` [BUGFIX PATCH -tip] x86/kprobes: Fix to call previous kprobe's fault handler Masami Hiramatsu
2021-05-25  7:24   ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=162182673618.114649.7393137495689996180.stgit@devnote2 \
    --to=mhiramat@kernel.org \
    --cc=ananth@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=naveen.n.rao@linux.vnet.ibm.com \
    --cc=rostedt@goodmis.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox