public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Karol Herbst <karolherbst@gmail.com>,
	Pekka Paalanen <ppaalanen@gmail.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Andy Lutomirski <luto@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Thomas Gleixner <tglx@linutronix.de>
Subject: [for-next][PATCH 12/25] x86/mm/kmmio: Switch to arch_spin_lock()
Date: Sat, 10 Dec 2022 08:58:02 -0500	[thread overview]
Message-ID: <20221210135825.088221176@goodmis.org> (raw)
In-Reply-To: 20221210135750.425719934@goodmis.org

From: Steven Rostedt <rostedt@goodmis.org>

The mmiotrace tracer is "special". The purpose is to help reverse engineer
binary drivers by removing the memory allocated by the driver and when the
driver goes to access it, a fault occurs, the mmiotracer will record what
the driver was doing and then do the work on its behalf by single stepping
through the process.

But to achieve this ability, it must do some special things. One is it
needs to grab a lock while in the breakpoint handler. This is considered
an NMI state, and then lockdep warns that the lock is being held in both
an NMI state (really a breakpoint handler) and also in normal context.

As the breakpoint/NMI state only happens when the driver is accessing
memory, there's no concern of a race condition against the setup and
tear-down of mmiotracer.

To make lockdep and mmiotrace work together, convert the locks used in the
breakpoint handler into arch_spin_lock().

Link: https://lkml.kernel.org/r/20221206191229.656244029@goodmis.org
Link: https://lore.kernel.org/lkml/20221201213126.620b7dd3@gandalf.local.home/
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Karol Herbst <karolherbst@gmail.com>
Cc: Pekka Paalanen <ppaalanen@gmail.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 arch/x86/mm/kmmio.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/arch/x86/mm/kmmio.c b/arch/x86/mm/kmmio.c
index d3efbc5b3449..edb486450158 100644
--- a/arch/x86/mm/kmmio.c
+++ b/arch/x86/mm/kmmio.c
@@ -62,7 +62,13 @@ struct kmmio_context {
 	int active;
 };
 
-static DEFINE_SPINLOCK(kmmio_lock);
+/*
+ * The kmmio_lock is taken in int3 context, which is treated as NMI context.
+ * This causes lockdep to complain about it bein in both NMI and normal
+ * context. Hide it from lockdep, as it should not have any other locks
+ * taken under it, and this is only enabled for debugging mmio anyway.
+ */
+static arch_spinlock_t kmmio_lock = __ARCH_SPIN_LOCK_UNLOCKED;
 
 /* Protected by kmmio_lock */
 unsigned int kmmio_count;
@@ -346,10 +352,10 @@ static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
 		ctx->probe->post_handler(ctx->probe, condition, regs);
 
 	/* Prevent racing against release_kmmio_fault_page(). */
-	spin_lock(&kmmio_lock);
+	arch_spin_lock(&kmmio_lock);
 	if (ctx->fpage->count)
 		arm_kmmio_fault_page(ctx->fpage);
-	spin_unlock(&kmmio_lock);
+	arch_spin_unlock(&kmmio_lock);
 
 	regs->flags &= ~X86_EFLAGS_TF;
 	regs->flags |= ctx->saved_flags;
@@ -440,7 +446,8 @@ int register_kmmio_probe(struct kmmio_probe *p)
 	unsigned int l;
 	pte_t *pte;
 
-	spin_lock_irqsave(&kmmio_lock, flags);
+	local_irq_save(flags);
+	arch_spin_lock(&kmmio_lock);
 	if (get_kmmio_probe(addr)) {
 		ret = -EEXIST;
 		goto out;
@@ -460,7 +467,9 @@ int register_kmmio_probe(struct kmmio_probe *p)
 		size += page_level_size(l);
 	}
 out:
-	spin_unlock_irqrestore(&kmmio_lock, flags);
+	arch_spin_unlock(&kmmio_lock);
+	local_irq_restore(flags);
+
 	/*
 	 * XXX: What should I do here?
 	 * Here was a call to global_flush_tlb(), but it does not exist
@@ -494,7 +503,8 @@ static void remove_kmmio_fault_pages(struct rcu_head *head)
 	struct kmmio_fault_page **prevp = &dr->release_list;
 	unsigned long flags;
 
-	spin_lock_irqsave(&kmmio_lock, flags);
+	local_irq_save(flags);
+	arch_spin_lock(&kmmio_lock);
 	while (f) {
 		if (!f->count) {
 			list_del_rcu(&f->list);
@@ -506,7 +516,8 @@ static void remove_kmmio_fault_pages(struct rcu_head *head)
 		}
 		f = *prevp;
 	}
-	spin_unlock_irqrestore(&kmmio_lock, flags);
+	arch_spin_unlock(&kmmio_lock);
+	local_irq_restore(flags);
 
 	/* This is the real RCU destroy call. */
 	call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages);
@@ -540,14 +551,16 @@ void unregister_kmmio_probe(struct kmmio_probe *p)
 	if (!pte)
 		return;
 
-	spin_lock_irqsave(&kmmio_lock, flags);
+	local_irq_save(flags);
+	arch_spin_lock(&kmmio_lock);
 	while (size < size_lim) {
 		release_kmmio_fault_page(addr + size, &release_list);
 		size += page_level_size(l);
 	}
 	list_del_rcu(&p->list);
 	kmmio_count--;
-	spin_unlock_irqrestore(&kmmio_lock, flags);
+	arch_spin_unlock(&kmmio_lock);
+	local_irq_restore(flags);
 
 	if (!release_list)
 		return;
-- 
2.35.1



  parent reply	other threads:[~2022-12-10 13:59 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-10 13:57 [for-next][PATCH 00/25] tracing: Updates for 6.2 Steven Rostedt
2022-12-10 13:57 ` [for-next][PATCH 01/25] tracing/user_events: Fix call print_fmt leak Steven Rostedt
2022-12-10 13:57 ` [for-next][PATCH 02/25] tracing: Update MAINTAINERS file for new patchwork and mailing list Steven Rostedt
2022-12-10 13:57 ` [for-next][PATCH 03/25] ftrace/x86: Add back ftrace_expected for ftrace bug reports Steven Rostedt
2022-12-10 13:57 ` [for-next][PATCH 04/25] tracing: Allow multiple hitcount values in histograms Steven Rostedt
2022-12-10 13:57 ` [for-next][PATCH 05/25] tracing: Add .percent suffix option to histogram values Steven Rostedt
2022-12-10 13:57 ` [for-next][PATCH 06/25] tracing: Add .graph suffix option to histogram value Steven Rostedt
2022-12-10 13:57 ` [for-next][PATCH 07/25] tracing: Add nohitcount option for suppressing display of raw hitcount Steven Rostedt
2022-12-10 13:57 ` [for-next][PATCH 08/25] tracing: docs: Update histogram doc for .percent/.graph and nohitcount Steven Rostedt
2022-12-10 13:57 ` [for-next][PATCH 09/25] trace/kprobe: remove duplicated calls of ring_buffer_event_data Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 10/25] tracing/probes: Handle system names with hyphens Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 11/25] tracing: Fix complicated dependency of CONFIG_TRACER_MAX_TRACE Steven Rostedt
2022-12-10 13:58 ` Steven Rostedt [this message]
2022-12-10 13:58 ` [for-next][PATCH 13/25] x86/mm/kmmio: Use rcu_read_lock_sched_notrace() Steven Rostedt
2022-12-10 17:47   ` Paul E. McKenney
2022-12-10 18:34     ` Steven Rostedt
2022-12-10 21:34       ` Paul E. McKenney
2022-12-10 22:32         ` Steven Rostedt
2022-12-11  5:52           ` Paul E. McKenney
2022-12-10 23:30       ` Thomas Gleixner
2022-12-10 23:55         ` Steven Rostedt
2022-12-12 10:51           ` Thomas Gleixner
2022-12-12 15:42             ` Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 14/25] tracing/hist: Fix wrong return value in parse_action_params() Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 15/25] tracing/hist: Fix out-of-bound write on action_data.var_ref_idx Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 16/25] tracing: Fix issue of missing one synthetic field Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 17/25] tracing/hist: Fix issue of losting command info in error_log Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 18/25] ring-buffer: Handle resize in early boot up Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 19/25] tracing: remove unnecessary trace_trigger ifdef Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 20/25] tracing/osnoise: Make osnoise_options static Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 21/25] tracing: Fix some checker warnings Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 22/25] Documentation/osnoise: Escape underscore of NO_ prefix Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 23/25] tracing/osnoise: Add PANIC_ON_STOP option Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 24/25] tracing/osnoise: Add preempt and/or irq disabled options Steven Rostedt
2022-12-10 13:58 ` [for-next][PATCH 25/25] Documentation/osnoise: Add osnoise/options documentation Steven Rostedt

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=20221210135825.088221176@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=karolherbst@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=ppaalanen@gmail.com \
    --cc=tglx@linutronix.de \
    /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