Skip to contentSkip to conversation

mmiotrace full patch, preview 1

RFC In review Pekka Paalanen 8 participants 13 messages Feb 24, 2008 – Feb 27, 2008

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 mmiotrace full patch, preview 1

Hi all,

I have finally got kmmio.c into shape so that it is built in when mmiotrace is built. I'd like to hear your comments on kmmio.c.

kmmio.c handles the list of mmio probes with callbacks, list of traced pages, and attaching into the page fault handler and die notifier. It arms, traps and disarms the given pages, this is the core of mmiotrace.

mmio-mod.c is a user interface, hooking into ioremap functions and registering the mmio probes. It also decodes the required information from trapped mmio accesses via the pre and post callbacks in each probe. Currently, hooking into ioremap functions works by redefining the symbols of the target (binary) kernel module, so that it calls the traced versions of the functions.

The most notable changes done since the last discussion are:
- kmmio.c is a built-in, not part of the module
- direct call from fault.c to kmmio.c, removing all dynamic hooks
- prepare for unregistering probes at any time
- make kmmio re-initializable and accessible to more than one user
- rewrite kmmio locking to remove all spinlocks from page fault path

Can I abuse call_rcu() like I do in kmmio.c:unregister_kmmio_probe() or is there a better way?

The function called via call_rcu() itself calls call_rcu() again, will this work or break? There I need a second grace period for RCU after the first grace period for page faults.

Mmiotrace itself (mmio-mod.c) is still a module, I am going to attack that next. At some point I will start looking into how to make mmiotrace a tracer component of ftrace (thanks for the hint, Ingo). Ftrace should make the user space part of mmiotracing as simple as
'cat /debug/trace/mmio > dump.txt'.

Ftrace lives in sched-devel
http://people.redhat.com/mingo/sched-devel.git/README
and I have nothing for that yet.

This patch is against torvalds/linux-2.6.git master.

Home page of the out-of-tree version:
http://nouveau.freedesktop.org/wiki/MmioTrace

Thanks, Pekka.

 arch/x86/Kconfig.debug      |   33 +++
 arch/x86/mm/Makefile        |    5 +
 arch/x86/mm/fault.c         |   13 +
 arch/x86/mm/kmmio.c         |  541 +++++++++++++++++++++++++++++++++++++++++++
 arch/x86/mm/mmio-mod.c      |  541 +++++++++++++++++++++++++++++++++++++++++++
 arch/x86/mm/pageattr.c      |    1 +
 arch/x86/mm/pf_in.c         |  489 ++++++++++++++++++++++++++++++++++++++
 arch/x86/mm/pf_in.h         |   39 +++
 arch/x86/mm/testmmiotrace.c |   76 ++++++
 include/linux/mmiotrace.h   |  104 +++++++++
 10 files changed, 1842 insertions(+), 0 deletions(-)
arch/x86/Kconfig.debug +33 -0
index 702eb39..ef73cdd 100644
@@ -134,6 +134,39 @@ config IOMMU_LEAK
 	  Add a simple leak tracer to the IOMMU code. This is useful when you
 	  are debugging a buggy device driver that leaks IOMMU mappings.
 
+config MMIOTRACE_HOOKS
+	bool
+	default n
+
+config MMIOTRACE
+	tristate "Memory mapped IO tracing"
+	depends on DEBUG_KERNEL && RELAY && DEBUG_FS
+	select MMIOTRACE_HOOKS
+	default n
+	help
+	  This will build a kernel module called mmiotrace.
+	  Making this a built-in is heavily discouraged.
+
+	  Mmiotrace traces Memory Mapped I/O access and is meant for debugging
+	  and reverse engineering. The kernel module offers wrapped
+	  versions of the ioremap family of functions. The driver to be traced
+	  must be modified to call these wrappers. A user space program is
+	  required to collect the MMIO data.
+
+	  See http://nouveau.freedesktop.org/wiki/MmioTrace
+	  If you are not helping to develop drivers, say N.
+
+config MMIOTRACE_TEST
+	tristate "Test module for mmiotrace"
+	depends on MMIOTRACE && m
+	default n
+	help
+	  This is a dumb module for testing mmiotrace. It is very dangerous
+	  as it will write garbage to IO memory starting at a given address.
+	  However, it should be safe to use on e.g. unused portion of VRAM.
+
+	  Say N, unless you absolutely know what you are doing.
+
 #
 # IO delay types:
 #
arch/x86/mm/Makefile +5 -0
index 9832910..e182f6d 100644
@@ -3,3 +3,8 @@ include ${srctree}/arch/x86/mm/Makefile_32
 else
 include ${srctree}/arch/x86/mm/Makefile_64
 endif
+
+obj-$(CONFIG_MMIOTRACE_HOOKS)	+= kmmio.o
+obj-$(CONFIG_MMIOTRACE) 	+= mmiotrace.o
+mmiotrace-objs			:= pf_in.o mmio-mod.o
+obj-$(CONFIG_MMIOTRACE_TEST) 	+= testmmiotrace.o
arch/x86/mm/fault.c +13 -0
index fdc6674..ba6d4c5 100644
@@ -25,6 +25,7 @@
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
 #include <linux/kdebug.h>
+#include <linux/mmiotrace.h>
 
 #include <asm/system.h>
 #include <asm/desc.h>
@@ -49,6 +50,16 @@
 #define PF_RSVD		(1<<3)
 #define PF_INSTR	(1<<4)
 
+static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
+{
+#ifdef CONFIG_MMIOTRACE_HOOKS
+	if (unlikely(is_kmmio_active()))
+		if (kmmio_handler(regs, addr) == 1)
+			return -1;
+#endif
+	return 0;
+}
+
 static inline int notify_page_fault(struct pt_regs *regs)
 {
 #ifdef CONFIG_KPROBES
@@ -603,6 +614,8 @@ void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
 
 	if (notify_page_fault(regs))
 		return;
+	if (unlikely(kmmio_fault(regs, address)))
+		return;
 
 	/*
 	 * We fault-in kernel-space virtual memory on-demand. The
arch/x86/mm/kmmio.c new file +541 -0

541 lines not on this page · raw message

arch/x86/mm/mmio-mod.c new file +541 -0

541 lines not on this page · raw message

arch/x86/mm/pageattr.c +1 -0
index 464d8fc..6279f8c 100644
@@ -217,6 +217,7 @@ pte_t *lookup_address(unsigned long address, unsigned int *level)
 
 	return pte_offset_kernel(pmd, address);
 }
+EXPORT_SYMBOL_GPL(lookup_address);
 
 /*
  * Set the new pmd in all the pgds we know about:
arch/x86/mm/pf_in.c new file +489 -0

489 lines not on this page · raw message

arch/x86/mm/pf_in.h new file +39 -0
new file mode 100644
index 0000000..e05341a
@@ -0,0 +1,39 @@
+/*
+ *  Fault Injection Test harness (FI)
+ *  Copyright (C) Intel Crop.
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; either version 2
+ *  of the License, or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
+ *  USA.
+ *
+ */
+
+#ifndef __PF_H_
+#define __PF_H_
+
+enum reason_type {
+	NOT_ME,	/* page fault is not in regions */
+	NOTHING,	/* access others point in regions */
+	REG_READ,	/* read from addr to reg */
+	REG_WRITE,	/* write from reg to addr */
+	IMM_WRITE,	/* write from imm to addr */
+	OTHERS	/* Other instructions can not intercept */
+};
+
+enum reason_type get_ins_type(unsigned long ins_addr);
+unsigned int get_ins_mem_width(unsigned long ins_addr);
+unsigned long get_ins_reg_val(unsigned long ins_addr, struct pt_regs *regs);
+unsigned long get_ins_imm_val(unsigned long ins_addr);
+
+#endif /* __PF_H_ */
arch/x86/mm/testmmiotrace.c new file +76 -0

76 lines not on this page · raw message

include/linux/mmiotrace.h new file +104 -0

104 lines not on this page · raw message

#1 lore raw

Hi Pekka.

A small nitpick..

arch/x86/Kconfig.debug config IOMMU_LEAK · line 139

Quoted 4 lines
arch/x86/Kconfig.debug +3 -0
+config MMIOTRACE_HOOKS
+	bool
+	default n

n is default so no need to be explicit.
If you prefer being explcit then use:

def_bool n

arch/x86/Kconfig.debug config IOMMU_LEAK · line 145

Quoted 5 lines
arch/x86/Kconfig.debug +5 -0
+config MMIOTRACE
+	tristate "Memory mapped IO tracing"
+	depends on DEBUG_KERNEL && RELAY && DEBUG_FS
+	select MMIOTRACE_HOOKS
+	default n

Same here.

Quoted 32 lines
arch/x86/Kconfig.debug +24 -0
+	help
+	  This will build a kernel module called mmiotrace.
+	  Making this a built-in is heavily discouraged.
+
+	  Mmiotrace traces Memory Mapped I/O access and is meant for debugging
+	  and reverse engineering. The kernel module offers wrapped
+	  versions of the ioremap family of functions. The driver to be traced
+	  must be modified to call these wrappers. A user space program is
+	  required to collect the MMIO data.
+
+	  See http://nouveau.freedesktop.org/wiki/MmioTrace
+	  If you are not helping to develop drivers, say N.
+
+config MMIOTRACE_TEST
+	tristate "Test module for mmiotrace"
+	depends on MMIOTRACE && m
+	default n
+	help
+	  This is a dumb module for testing mmiotrace. It is very dangerous
+	  as it will write garbage to IO memory starting at a given address.
+	  However, it should be safe to use on e.g. unused portion of VRAM.
+
+	  Say N, unless you absolutely know what you are doing.
+
 #
 # IO delay types:
 #
arch/x86/mm/Makefile +0 -0
index 9832910..e182f6d 100644

arch/x86/mm/Makefile include ${srctree}/arch/x86/mm/Makefile_32 · line 9

Quoted 7 lines
arch/x86/mm/Makefile +4 -0
@@ -3,3 +3,8 @@ include ${srctree}/arch/x86/mm/Makefile_32
 else
 include ${srctree}/arch/x86/mm/Makefile_64
 endif
+
+obj-$(CONFIG_MMIOTRACE_HOOKS)	+= kmmio.o
+obj-$(CONFIG_MMIOTRACE) 	+= mmiotrace.o
+mmiotrace-objs			:= pf_in.o mmio-mod.o

Do not use '-objs' - '-y' is preferred as below:

arch/x86/mm/Makefile include ${srctree}/arch/x86/mm/Makefile_32 · line 10

Quoted 1 line
arch/x86/mm/Makefile +1 -0
+mmiotrace-y				:= pf_in.o mmio-mod.o

Sam

#2 lore raw
In reply to Pekka Paalanen
Quoted 10 lines · On Sun, 24 Feb 2008 19:03:23 +0200 Pekka Paalanen <[email protected]> wrote:
 arch/x86/Kconfig.debug      |   33 +++
 arch/x86/mm/Makefile        |    5 +
 arch/x86/mm/fault.c         |   13 +
 arch/x86/mm/kmmio.c         |  541 +++++++++++++++++++++++++++++++++++++++++++
 arch/x86/mm/mmio-mod.c      |  541 +++++++++++++++++++++++++++++++++++++++++++
 arch/x86/mm/pageattr.c      |    1 +
 arch/x86/mm/pf_in.c         |  489 ++++++++++++++++++++++++++++++++++++++
 arch/x86/mm/pf_in.h         |   39 +++
 arch/x86/mm/testmmiotrace.c |   76 ++++++
 include/linux/mmiotrace.h   |  104 +++++++++

Please feed the diff through scritps/checkpatch.pl and consider addressing the things which it finds.

Quoted 1 line

+static DECLARE_MUTEX(kmmio_init_mutex);

That's not a mutex.

Review

Quoted 1 line
(quoted diff) +1 -0
+	down(&kmmio_init_mutex);

It's a semaphore. Please do convert it to a mutex.

Andy, I'd say that addition of new semaphores is worth a warning - they're rarely legitimate.

#3 lore raw
Quoted 4 lines · Andrew Morton

the things which it finds.

Quoted 1 line

+static DECLARE_MUTEX(kmmio_init_mutex);

Quoted 8 lines

That's not a mutex.

Review

Quoted 1 line
(quoted diff) +1 -0
+	down(&kmmio_init_mutex);

It's a semaphore. Please do convert it to a mutex.

Andy, I'd say that addition of new semaphores is worth a warning - they're rarely legitimate.

I'm not sure that any semaphore should be a warning, but the initializer for semaphore used as binary mutex (DECLARE_MUTEX and init_MUTEX) are worth it.

#4 lore raw

Quoting Christoph Hellwig <[email protected]>:

Quoted 11 lines · Andrew Morton
Quoted 10 lines

the things which it finds.

Quoted 1 line

+static DECLARE_MUTEX(kmmio_init_mutex);

That's not a mutex.

Review

Quoted 1 line
(quoted diff) +1 -0
+	down(&kmmio_init_mutex);

It's a semaphore. Please do convert it to a mutex.

Quoted 6 lines
Quoted 2 lines

Andy, I'd say that addition of new semaphores is worth a warning - they're rarely legitimate.

I'm not sure that any semaphore should be a warning, but the initializer for semaphore used as binary mutex (DECLARE_MUTEX and init_MUTEX) are worth it.

It looks like a mutex, it acts like a mutex, but it isn't a mutex, it's a trap for the unwary. Weird. I was annoyed by it before; now I see a fellow developer actually getting into that trap.

I'd say, rename DECLARE_MUTEX to DECLARE_SEMAPHORE and let external code be fixed one way or another (i.e. stick with the "mutex" name or stick with the semaphore functionality if it's really needed).

Signature · 2 lines
Regards,
Pavel Roskin
#5 lore raw
Quoted 20 lines · Pavel Roskin

Quoting Christoph Hellwig <[email protected]>:

Quoted 11 lines · Andrew Morton
Quoted 10 lines

the things which it finds.

Quoted 1 line

+static DECLARE_MUTEX(kmmio_init_mutex);

That's not a mutex.

Review

Quoted 1 line
(quoted diff) +1 -0
+	down(&kmmio_init_mutex);

It's a semaphore. Please do convert it to a mutex.

Quoted 6 lines
Quoted 2 lines

Andy, I'd say that addition of new semaphores is worth a warning - they're rarely legitimate.

I'm not sure that any semaphore should be a warning, but the initializer for semaphore used as binary mutex (DECLARE_MUTEX and init_MUTEX) are worth it.

Quoted 7 lines

It looks like a mutex, it acts like a mutex, but it isn't a mutex, it's a trap for the unwary. Weird. I was annoyed by it before; now I see a fellow developer actually getting into that trap.

I'd say, rename DECLARE_MUTEX to DECLARE_SEMAPHORE and let external code be fixed one way or another (i.e. stick with the "mutex" name or stick with the semaphore functionality if it's really needed).

I like the fact that in evey architecture its defined as:

	#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)

-apw

#6 lore raw
In reply to Pavel Roskin
Quoted 7 lines · Pavel Roskin

It looks like a mutex, it acts like a mutex, but it isn't a mutex, it's a trap for the unwary. Weird. I was annoyed by it before; now I see a fellow developer actually getting into that trap.

I'd say, rename DECLARE_MUTEX to DECLARE_SEMAPHORE and let external code be fixed one way or another (i.e. stick with the "mutex" name or stick with the semaphore functionality if it's really needed).

It's a semaphore used as mutex. Until we got struct mutex this was perfectly fine and now we're phasing it out and will hopefully get rid of it soon. It just takes some time to convert all users.

#7 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Andrew Morton

the things which it finds.

Quoted 1 line

+static DECLARE_MUTEX(kmmio_init_mutex);

That's not a mutex.

Review

Quoted 1 line
(quoted diff) +1 -0
+	down(&kmmio_init_mutex);

It's a semaphore. Please do convert it to a mutex.

Quoted 6 lines
Quoted 2 lines

Andy, I'd say that addition of new semaphores is worth a warning - they're rarely legitimate.

I'm not sure that any semaphore should be a warning, but the initializer for semaphore used as binary mutex (DECLARE_MUTEX and init_MUTEX) are worth it.

Ok, so that would be the following, work for everyone?

WARNING: mutexes are preferred for single holder semaphores #1: FILE: Z95.c:1:
+ DECLARE_MUTEX(&foo);

WARNING: mutexes are preferred for single holder semaphores #3: FILE: Z95.c:3:
+ init_MUTEX(&foo);

-apw

Acked-by
#8 lore raw
Quoted 9 lines · Andy Whitcroft

Ok, so that would be the following, work for everyone?

WARNING: mutexes are preferred for single holder semaphores #1: FILE: Z95.c:1:
+ DECLARE_MUTEX(&foo);

WARNING: mutexes are preferred for single holder semaphores #3: FILE: Z95.c:3:
+ init_MUTEX(&foo);

yeah.

also i guess init_MUTEX_LOCKED() should emit a "this should be a completion" warning.

i guess non-DEFINE_SPINLOCK old-style spinlock definition:

  spinlock_t lock = SPIN_LOCK_UNLOCKED;

should emit a 'use DEFINE_SPINLOCK' warning as well?

Ingo

#9 lore raw
Quoted 13 lines · Ingo Molnar
Quoted 9 lines · Andy Whitcroft

Ok, so that would be the following, work for everyone?

WARNING: mutexes are preferred for single holder semaphores #1: FILE: Z95.c:1:
+ DECLARE_MUTEX(&foo);

WARNING: mutexes are preferred for single holder semaphores #3: FILE: Z95.c:3:
+ init_MUTEX(&foo);

Quoted 6 lines

yeah.

also i guess init_MUTEX_LOCKED() should emit a "this should be a completion" warning.

Thats easy enough. Though your tone here implies its less definatly wrong than the other use forms. Do we want gentle language here?

"consider using a completion"

Quoted 5 lines

i guess non-DEFINE_SPINLOCK old-style spinlock definition:

  spinlock_t lock = SPIN_LOCK_UNLOCKED;

should emit a 'use DEFINE_SPINLOCK' warning as well?

Those (SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED) we already pick up and indicate are deprecated.

-apw

#10 lore raw
In reply to Andrew Morton
Quoted 2 lines · On Mon, 25 Feb 2008 14:49:22 -0800 Andrew Morton <[email protected]>…

Please feed the diff through scritps/checkpatch.pl and consider addressing the things which it finds.

I checked that, but I didn't think any of them were worth fixing. And since this is a work in progress and a in a state which I do not want committed yet, I did not sign this patch. mmio-mod.c is still a mess.

Quoted 3 lines

WARNING: Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt #1097: FILE: arch/x86/mm/mmio-mod.c:437:
+void iounmap_trace(volatile void __iomem *addr)

I believe the 'volatile' belongs here, it is also in the declaration of iounmap() in arch/x86/mm/ioremap.c.

Quoted 11 lines

WARNING: externs should be avoided in .c files
#1766: FILE: arch/x86/mm/testmmiotrace.c:7:
+extern void __iomem *ioremap_nocache_trace(unsigned long offset,

WARNING: Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt #1768: FILE: arch/x86/mm/testmmiotrace.c:9:
+extern void iounmap_trace(volatile void __iomem *addr);

WARNING: externs should be avoided in .c files #1768: FILE: arch/x86/mm/testmmiotrace.c:9:
+extern void iounmap_trace(volatile void __iomem *addr);

These are all in testmmiotrace.c which calls the traced ioremap functions directly. These direct calls will go away and it will call the normal ioremap functions.

Quoted 3 lines

WARNING: Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt #1790: FILE: arch/x86/mm/testmmiotrace.c:31:
+ volatile unsigned int v;

Since testmmiotrace does not use v for anything other than return value of ioread8/16/32(), I wanted to prevent the compiler from optimizing it away. Now thinking it again, ioread*() must guarantee that the read is always executed. I'll remove v altogether.

Patch for the other issues you mentioned is brewing.

Thanks.

Signature · 2 lines
Pekka Paalanen
http://www.iki.fi/pq/
#11 lore raw
In reply to Pekka Paalanen

Hey, Pekka,

A couple of little things I noticed...

arch/x86/mm/kmmio.c line 321

Quoted 9 lines
arch/x86/mm/kmmio.c new file +9 -0
+static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
+{
+	int ret = 0;
+	struct kmmio_probe *probe;
+	struct kmmio_fault_page *faultpage;
+	struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx);
+
+	if (!ctx->active)
+		goto out;

Should that text read something like:

	if (condition != DIE_TRAP || !ctx->active)

Presumably you won't be active if something else is going wrong, but one never knows.

arch/x86/mm/kmmio.c line 422

Quoted 11 lines
arch/x86/mm/kmmio.c new file +11 -0
+int register_kmmio_probe(struct kmmio_probe *p)
+{
+	int ret = 0;
+	unsigned long size = 0;
+
+	spin_lock_irq(&kmmio_lock);
+	kmmio_count++;
+	if (get_kmmio_probe(p->addr)) {
+		ret = -EEXIST;
+		goto out;
+	}

That only checks the first page; if the probed region partially overlaps another one found later in memory, the registration will succeed.

Maybe you want to decrement kmmio_count if you decide to return -EEXIST (or hold off on the increment until after the test)?

In general, I worry about what happens if an interrupt handler generates traced MMIO traffic while a fault handler is active. It looks a lot like the "all hell breaks loose" scenario mentioned in the comments. Is there some way of preventing that which I missed? Otherwise, maybe, should the ioremap() wrappers take an additional argument, being an IRQ to disable while trace handlers are active?

jon

#12 lore raw
Quoted 14 lines · On Tue, 26 Feb 2008 10:20:08 -0700 [email protected] (Jonathan Corbet) wrote:

A couple of little things I noticed...

arch/x86/mm/kmmio.c line 321

Quoted 9 lines
arch/x86/mm/kmmio.c new file +9 -0
+static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
+{
+	int ret = 0;
+	struct kmmio_probe *probe;
+	struct kmmio_fault_page *faultpage;
+	struct kmmio_context *ctx = &get_cpu_var(kmmio_ctx);
+
+	if (!ctx->active)
+		goto out;

Should that text read something like:

Quoted 1 line
	if (condition != DIE_TRAP || !ctx->active)

The last thing in kmmio.c is:

static int kmmio_die_notifier(struct notifier_block *nb, unsigned long val,
void *args)

{
	struct die_args *arg = args;

	if (val == DIE_DEBUG)
		if (post_kmmio_handler(arg->err, arg->regs) == 1)
			return NOTIFY_STOP;

	return NOTIFY_DONE;
}

so post_kmmio_handler() is not the die_notifier callback, and the handler is limited to debug calls, which I assume can only come from single-stepping. The variable 'condition' is something that on x86_64 comes from get_debugreg(condition, 6); in arch/x86/kernel/traps_64.c do_debug(). I don't know what 'condition' is and kmmio is not using it.

Quoted 17 lines

Presumably you won't be active if something else is going wrong, but one never knows.

arch/x86/mm/kmmio.c line 422

Quoted 11 lines
arch/x86/mm/kmmio.c new file +11 -0
+int register_kmmio_probe(struct kmmio_probe *p)
+{
+	int ret = 0;
+	unsigned long size = 0;
+
+	spin_lock_irq(&kmmio_lock);
+	kmmio_count++;
+	if (get_kmmio_probe(p->addr)) {
+		ret = -EEXIST;
+		goto out;
+	}

That only checks the first page; if the probed region partially overlaps another one found later in memory, the registration will succeed.

True. I am assuming ioremap*() cannot return regions that overlap in their kernel virtual addresses. Clearly this does not hold for the ISA region, but tracing the ISA region has triggered hard freezes, so we specifically exclude that.

Even if ioremap*() would return overlapping areas, I don't think it would break things. It just means there are two probes on the same area. If an MMIO access hits that area, the "first" probe gets selected and executed. The only thing lost is which probe was actually hit, but then there's no way to know that, anyway.

kmmio_fault_page objects are shared and reference-counted, so they should not be a problem, either.

Quoted 2 lines

Maybe you want to decrement kmmio_count if you decide to return -EEXIST (or hold off on the increment until after the test)?

Yes, good catch. Then again, maybe I would not even need the failure case at all. I think I'll keep it there, though, maybe it catches something stupid I will do.

Quoted 6 lines

In general, I worry about what happens if an interrupt handler generates traced MMIO traffic while a fault handler is active. It looks a lot like the "all hell breaks loose" scenario mentioned in the comments. Is there some way of preventing that which I missed? Otherwise, maybe, should the ioremap() wrappers take an additional argument, being an IRQ to disable while trace handlers are active?

I have thought of something like this, how could it happen. My current understanding is the following.

kmmio_handler(), where-ever it was triggered, executes with interrupts disabled. It modifies the saved CPU state: disable interrupts and enable single-stepping (what's the correct term?). After disarming the page, it returns. The faulted instruction is single-stepped, all the time the interrupts are disabled. It hits the debug trap and we end up in post_kmmio_handler(), still interrupts disabled. The post handler does its thing, restores the CPU state changed in kmmio_handler(), and returns.

Therefore, on this CPU, no interrupt except NMI may occur during the handling of a probe hit. Of course, I am assuming the execution flow will stay on this CPU, but there's no chance it could jump to a different CPU, is there?

Which leads to the question, is preempt_disable() in kmmio_handler() and the respective preempt_enable_no_resched() in post_kmmio_handler() required, or does disabling interrupts guarantee that?

And also, the following code is probably useless in post_kmmio_handler() because of the interrupts being disabled all the time:

	faultpage = get_kmmio_fault_page(ctx->addr);
	probe = get_kmmio_probe(ctx->addr);
	if (faultpage != ctx->fpage || probe != ctx->probe) {
		/*
		 * The trace setup changed after kmmio_handler() and before
		 * running this respective post handler. User does not want
		 * the result anymore.
		 */
		ctx->probe = NULL;
		ctx->fpage = NULL;
	}

For the if to trigger, the probe list or the kmmio_fault_page list would have to change during a probe hit or the single-stepping. The lists are protected by RCU, so this is impossible. Hmm, no, wait. The lists could be modified, but the pointers would still stay valid because RCU has not had a chance run the freeing function. OTOH, rcu_read_lock() is not held over single-stepping.

Maybe I should replace the preempt_disable() with rcu_read_lock() that would be held over single-stepping?

Are there any locks or anything I should *not* hold over the single- stepping, i.e., lock in kmmio_handler() and unlock only in post_kmmio_handler()?

Is there any way post_kmmio_handler() would not get called after kmmio_handler() was called?

You might get the impression that I don't know what I'm doing. You would be correct! ;-)

Thanks, I appreciate all feedback.

Signature · 2 lines
Pekka Paalanen
http://www.iki.fi/pq/