Skip to contentSkip to conversation

mmiotrace full patch, preview 3

RFC In review Pekka Paalanen 2 participants 12 messages Apr 28, 2008 – May 1, 2008

Subsystems (6)
Documentation x86 Tracing Arm STM32 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

RFC 1/3 mmiotrace full patch, preview 3

Hi all,

this is the third full mmiotrace patch set for review (a bit late, sorry). These patches do not apply to any git tree that I know of, but after Ingo pushes a new version of his sched-devel/latest tree, these patches should be subtractable from there.

I created these three patches by reorganizing patches in the sched-devel/latest tree to get all the mmiotrace patches to the top, and the end result is identical to the original, though I had to solve some conflicts along the way. I included my last 5+1 patches, too.

This first patch adds the bulk of the new code, only introducing new files. The second patch establishes the interfaces to the ftrace framework, and the third patch hooks everything up into the Kbuild system, page fault handler, and ioremap functions.

All three patches went through checkpatch.pl, getting complaints only about the use of 'volatile', which is used to duplicate the parameter signature of iounmap() for the functions mmiotrace_iounmap() and iounmap_trace_core() in the first patch.

New development since preview 2, March 9th:
- Abandoning the binary user space interface that used relay, and
- moving to ftrace framework interfaces, which
- produces the trace log in text format rather than binary, and
- made the special user space logging program obsolete.
- lots of cleanups, e.g. reference_kmmio() removed
- refactor dis/arm_kmmio_fault_page() and add check for page levels
- work around recursive probe hits on SMP, trying to survive
- added user documentation
- If HOTPLUG_CPU, disable all but one CPU when mmiotrace is activated,
- and also restore the CPUs when mmiotrace is deactivated.
- detect and print buffer overrun counts (losing events)

Some of the new code depends on the new features in the ftrace framework, many thanks to Steven for those.

The current state is that mmiotrace seems to work fine, also on SMP, but on SMP there's a chance to miss events due to CPUs racing. At last the log produced via ftrace framework is up-to-spec. Inserting user comments (markers in mmiotrace-parlance) into the log is not yet supported. All in all, after some more testing on my part, IMHO this is in a mergeable state.

Thanks,
Pekka Paalanen

 Documentation/tracers/mmiotrace.txt |  164 +++++++++++
 arch/x86/mm/kmmio.c                 |  500 ++++++++++++++++++++++++++++++++++
 arch/x86/mm/mmio-mod.c              |  513 +++++++++++++++++++++++++++++++++++
 arch/x86/mm/pf_in.c                 |  489 +++++++++++++++++++++++++++++++++
 arch/x86/mm/pf_in.h                 |   39 +++
 arch/x86/mm/testmmiotrace.c         |   71 +++++
 include/linux/mmiotrace.h           |   85 ++++++
 7 files changed, 1861 insertions(+), 0 deletions(-)
Documentation/tracers/mmiotrace.txt new file +164 -0
new file mode 100644
index 0000000..a4afb56
@@ -0,0 +1,164 @@
+		In-kernel memory-mapped I/O tracing
+
+
+Home page and links to optional user space tools:
+
+	http://nouveau.freedesktop.org/wiki/MmioTrace
+
+MMIO tracing was originally developed by Intel around 2003 for their Fault
+Injection Test Harness. In Dec 2006 - Jan 2007, using the code from Intel,
+Jeff Muizelaar created a tool for tracing MMIO accesses with the Nouveau
+project in mind. Since then many people have contributed.
+
+Mmiotrace was built for reverse engineering any memory-mapped IO device with
+the Nouveau project as the first real user. Only x86 and x86_64 architectures
+are supported.
+
+Out-of-tree mmiotrace was originally modified for mainline inclusion and
+ftrace framework by Pekka Paalanen <[email protected]>.
+
+
+Preparation
+-----------
+
+Mmiotrace feature is compiled in by the CONFIG_MMIOTRACE option. Tracing is
+disabled by default, so it is safe to have this set to yes. SMP systems are
+supported, but tracing is unreliable and may miss events if more than one CPU
+is on-line, therefore mmiotrace takes all but one CPU off-line during run-time
+activation. You can re-enable CPUs by hand, but you have been warned, there
+is no way to automatically detect if you are losing events due to CPUs racing.
+
+
+Usage Quick Reference
+---------------------
+
+$ mount -t debugfs debugfs /debug
+$ echo mmiotrace > /debug/tracing/current_tracer
+$ cat /debug/tracing/trace_pipe > mydump.txt &
+Start X or whatever.
+$ echo "X is up" > /debug/tracing/marker
+$ echo none > /debug/tracing/current_tracer
+Check for lost events.
+
+
+Usage
+-----
+
+Make sure debugfs is mounted to /debug. If not, (requires root privileges)
+$ mount -t debugfs debugfs /debug
+
+Check that the driver you are about to trace is not loaded.
+
+Activate mmiotrace (requires root privileges):
+$ echo mmiotrace > /debug/tracing/current_tracer
+
+Start storing the trace:
+$ cat /debug/tracing/trace_pipe > mydump.txt &
+The 'cat' process should stay running (sleeping) in the background.
+
+Load the driver you want to trace and use it. Mmiotrace will only catch MMIO
+accesses to areas that are ioremapped while mmiotrace is active.
+
+[Unimplemented feature:]
+During tracing you can place comments (markers) into the trace by
+$ echo "X is up" > /debug/tracing/marker
+This makes it easier to see which part of the (huge) trace corresponds to
+which action. It is recommended to place descriptive markers about what you
+do.
+
+Shut down mmiotrace (requires root privileges):
+$ echo none > /debug/tracing/current_tracer
+The 'cat' process exits. If it does not, kill it by issuing 'fg' command and
+pressing ctrl+c.
+
+Check that mmiotrace did not lose events due to a buffer filling up. Either
+$ grep -i lost mydump.txt
+which tells you exactly how many events were lost, or use
+$ dmesg
+to view your kernel log and look for "mmiotrace has lost events" warning. If
+events were lost, the trace is incomplete. You should enlarge the buffers and
+try again. Buffers are enlarged by first seeing how large the current buffers
+are:
+$ cat /debug/tracing/trace_entries
+gives you a number. Approximately double this number and write it back, for
+instance:
+$ echo 128000 > /debug/tracing/trace_entries
+Then start again from the top.
+
+If you are doing a trace for a driver project, e.g. Nouveau, you should also
+do the following before sending your results:
+$ lspci -vvv > lspci.txt
+$ dmesg > dmesg.txt
+$ tar zcf pciid-nick-mmiotrace.tar.gz mydump.txt lspci.txt dmesg.txt
+and then send the .tar.gz file. The trace compresses considerably. Replace
+"pciid" and "nick" with the PCI ID or model name of your piece of hardware
+under investigation and your nick name.
+
+
+How Mmiotrace Works
+-------------------
+
+Access to hardware IO-memory is gained by mapping addresses from PCI bus by
+calling one of the ioremap_*() functions. Mmiotrace is hooked into the
+__ioremap() function and gets called whenever a mapping is created. Mapping is
+an event that is recorded into the trace log. Note, that ISA range mappings
+are not caught, since the mapping always exists and is returned directly.
+
+MMIO accesses are recorded via page faults. Just before __ioremap() returns,
+the mapped pages are marked as not present. Any access to the pages causes a
+fault. The page fault handler calls mmiotrace to handle the fault. Mmiotrace
+marks the page present, sets TF flag to achieve single stepping and exits the
+fault handler. The instruction that faulted is executed and debug trap is
+entered. Here mmiotrace again marks the page as not present. The instruction
+is decoded to get the type of operation (read/write), data width and the value
+read or written. These are stored to the trace log.
+
+Setting the page present in the page fault handler has a race condition on SMP
+machines. During the single stepping other CPUs may run freely on that page
+and events can be missed without a notice. Re-enabling other CPUs during
+tracing is discouraged.
+
+
+Trace Log Format
+----------------
+
+The raw log is text and easily filtered with e.g. grep and awk. One record is
+one line in the log. A record starts with a keyword, followed by keyword
+dependant arguments. Arguments are separated by a space, or continue until the
+end of line. The format for version 20070824 is as follows:
+
+Explanation	Keyword	Space separated arguments
+---------------------------------------------------------------------------
+
+read event	R	width, timestamp, map id, physical, value, PC, PID
+write event	W	width, timestamp, map id, physical, value, PC, PID
+ioremap event	MAP	timestamp, map id, physical, virtual, length, PC, PID
+iounmap event	UNMAP	timestamp, map id, PC, PID
+marker		MARK	timestamp, text
+version		VERSION	the string "20070824"
+info for reader	LSPCI	one line from lspci -v
+PCI address map	PCIDEV	space separated /proc/bus/pci/devices data
+unk. opcode	UNKNOWN	timestamp, map id, physical, data, PC, PID
+
+Timestamp is in seconds with decimals. Physical is a PCI bus address, virtual
+is a kernel virtual address. Width is the data width in bytes and value is the
+data value. Map id is an arbitrary id number identifying the mapping that was
+used in an operation. PC is the program counter and PID is process id. PC is
+zero if it is not recorded. PID is always zero as tracing MMIO accesses
+originating in user space memory is not yet supported.
+
+For instance, the following awk filter will pass all 32-bit writes that target
+physical addresses in the range [0xfb73ce40, 0xfb800000[
+
+$ awk '/W 4 / { adr=strtonum($5); if (adr >= 0xfb73ce40 &&
+adr < 0xfb800000) print; }'
+
+
+Tools for Developers
+--------------------
+
+The user space tools include utilities for:
+- replacing numeric addresses and values with hardware register names
+- replaying MMIO logs, i.e., re-executing the recorded writes
+
+
arch/x86/mm/kmmio.c new file +500 -0

500 lines not on this page · raw message

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

513 lines not on this page · raw message

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 +71 -0

71 lines not on this page · raw message

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

85 lines not on this page · raw message

#1 lore raw

RFC 2/3 mmiotrace full patch, preview 3

Quoted 6 lines · On Mon, 28 Apr 2008 21:45:04 +0300 Pekka Paalanen <[email protected]> wrote:

Hi all,

this is the third full mmiotrace patch set for review (a bit late, sorry). These patches do not apply to any git tree that I know of, but after Ingo pushes a new version of his sched-devel/latest tree, these patches should be subtractable from there.

...

Quoted 4 lines

This first patch adds the bulk of the new code, only introducing new files. The second patch establishes the interfaces to the ftrace framework, and the third patch hooks everything up into the Kbuild system, page fault handler, and ioremap functions.

 kernel/trace/Makefile          |    1 +
 kernel/trace/trace.c           |   66 +++++++---
 kernel/trace/trace.h           |   29 ++++
 kernel/trace/trace_mmiotrace.c |  291 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 370 insertions(+), 17 deletions(-)
kernel/trace/Makefile +1 -0
index 7aec123..71d17de 100644
@@ -19,5 +19,6 @@ obj-$(CONFIG_FTRACE) += trace_functions.o
 obj-$(CONFIG_IRQSOFF_TRACER) += trace_irqsoff.o
 obj-$(CONFIG_PREEMPT_TRACER) += trace_irqsoff.o
 obj-$(CONFIG_SCHED_TRACER) += trace_sched_wakeup.o
+obj-$(CONFIG_MMIOTRACE) += trace_mmiotrace.o
 
 libftrace-y := ftrace.o
kernel/trace/trace.c +49 -17

114 lines not on this page · raw message

kernel/trace/trace.h +29 -0
index f2ca481..3e15c23 100644
@@ -5,6 +5,21 @@
 #include <asm/atomic.h>
 #include <linux/sched.h>
 #include <linux/clocksource.h>
+#include <linux/mmiotrace.h>
+
+enum trace_type {
+	__TRACE_FIRST_TYPE = 0,
+
+	TRACE_FN,
+	TRACE_CTX,
+	TRACE_WAKE,
+	TRACE_STACK,
+	TRACE_SPECIAL,
+	TRACE_MMIO_RW,
+	TRACE_MMIO_MAP,
+
+	__TRACE_LAST_TYPE
+};
 
 /*
  * Function trace entry - function address and parent function addres:
@@ -63,6 +78,8 @@ struct trace_entry {
 		struct ctx_switch_entry		ctx;
 		struct special_entry		special;
 		struct stack_entry		stack;
+		struct mmiotrace_rw		mmiorw;
+		struct mmiotrace_map		mmiomap;
 	};
 };
 
@@ -135,6 +152,7 @@ struct tracer {
 	int			(*selftest)(struct tracer *trace,
 					    struct trace_array *tr);
 #endif
+	int			(*print_line)(struct trace_iterator *iter);
 	struct tracer		*next;
 	int			print_max;
 };
@@ -244,6 +262,15 @@ extern unsigned long ftrace_update_tot_cnt;
 extern int DYN_FTRACE_TEST_NAME(void);
 #endif
 
+#ifdef CONFIG_MMIOTRACE
+extern void __trace_mmiotrace_rw(struct trace_array *tr,
+				struct trace_array_cpu *data,
+				struct mmiotrace_rw *rw);
+extern void __trace_mmiotrace_map(struct trace_array *tr,
+				struct trace_array_cpu *data,
+				struct mmiotrace_map *map);
+#endif
+
 #ifdef CONFIG_FTRACE_STARTUP_TEST
 #ifdef CONFIG_FTRACE
 extern int trace_selftest_startup_function(struct tracer *trace,
@@ -276,8 +303,10 @@ extern int trace_selftest_startup_sysprof(struct tracer *trace,
 #endif /* CONFIG_FTRACE_STARTUP_TEST */
 
 extern void *head_page(struct trace_array_cpu *data);
+extern int trace_seq_printf(struct trace_seq *s, const char *fmt, ...);
 extern ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
 				 size_t cnt);
+extern long ns2usecs(cycle_t nsec);
 
 extern unsigned long trace_flags;
 
kernel/trace/trace_mmiotrace.c new file +291 -0

291 lines not on this page · raw message

#2 lore raw
In reply to Pekka Paalanen
Quoted 6 lines · On Mon, 28 Apr 2008 21:45:04 +0300 Pekka Paalanen <[email protected]> wrote:

Hi all,

this is the third full mmiotrace patch set for review (a bit late, sorry). These patches do not apply to any git tree that I know of, but after Ingo pushes a new version of his sched-devel/latest tree, these patches should be subtractable from there.

...

Quoted 4 lines

This first patch adds the bulk of the new code, only introducing new files. The second patch establishes the interfaces to the ftrace framework, and the third patch hooks everything up into the Kbuild system, page fault handler, and ioremap functions.

 arch/x86/Kconfig.debug |   28 ++++++++++++++++++++++++++++
 arch/x86/mm/Makefile   |    5 +++++
 arch/x86/mm/fault.c    |   13 +++++++++++++
 arch/x86/mm/ioremap.c  |    9 ++++++++-
 arch/x86/mm/pageattr.c |    1 +
 5 files changed, 55 insertions(+), 1 deletions(-)
arch/x86/Kconfig.debug +28 -0
index 1f237d2..fb0794e 100644
@@ -170,6 +170,34 @@ 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
+
+config MMIOTRACE
+	bool "Memory mapped IO tracing"
+	depends on DEBUG_KERNEL && PCI
+	select TRACING
+	select MMIOTRACE_HOOKS
+	default y
+	help
+	  Mmiotrace traces Memory Mapped I/O access and is meant for
+	  debugging and reverse engineering. It is called from the ioremap
+	  implementation and works via page faults. Tracing is disabled by
+	  default and can be enabled at run-time.
+
+	  See Documentation/tracers/mmiotrace.txt.
+	  If you are not helping to develop drivers, say N.
+
+config MMIOTRACE_TEST
+	tristate "Test module for mmiotrace"
+	depends on MMIOTRACE && m
+	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 b7b3e4c..07dab50 100644
@@ -8,6 +8,11 @@ obj-$(CONFIG_X86_PTDUMP)	+= dump_pagetables.o
 
 obj-$(CONFIG_HIGHMEM)		+= highmem_32.o
 
+obj-$(CONFIG_MMIOTRACE_HOOKS)	+= kmmio.o
+obj-$(CONFIG_MMIOTRACE)		+= mmiotrace.o
+mmiotrace-y			:= pf_in.o mmio-mod.o
+obj-$(CONFIG_MMIOTRACE_TEST)	+= testmmiotrace.o
+
 ifeq ($(CONFIG_X86_32),y)
 obj-$(CONFIG_NUMA)		+= discontig_32.o
 else
arch/x86/mm/fault.c +13 -0
index 4145597..0440dce 100644
@@ -26,6 +26,7 @@
 #include <linux/uaccess.h>
 #include <linux/kdebug.h>
 #include <linux/magic.h>
+#include <linux/mmiotrace.h>
 
 #include <asm/system.h>
 #include <asm/desc.h>
@@ -51,6 +52,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
@@ -623,6 +634,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/ioremap.c +8 -1
index d176b23..68b287d 100644
@@ -12,6 +12,7 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
+#include <linux/mmiotrace.h>
 
 #include <asm/cacheflush.h>
 #include <asm/e820.h>
@@ -126,6 +127,7 @@ static void __iomem *__ioremap(resource_size_t phys_addr, unsigned long size,
 	unsigned long new_prot_val;
 	pgprot_t prot;
 	int retval;
+	void __iomem *ret_addr;
 
 	/* Don't allow wraparound or zero size */
 	last_addr = phys_addr + size - 1;
@@ -229,7 +231,10 @@ static void __iomem *__ioremap(resource_size_t phys_addr, unsigned long size,
 		return NULL;
 	}
 
-	return (void __iomem *) (vaddr + offset);
+	ret_addr = (void __iomem *) (vaddr + offset);
+	mmiotrace_ioremap(phys_addr, size, ret_addr);
+
+	return ret_addr;
 }
 
 /**
@@ -306,6 +311,8 @@ void iounmap(volatile void __iomem *addr)
 	    addr < phys_to_virt(ISA_END_ADDRESS))
 		return;
 
+	mmiotrace_iounmap(addr);
+
 	addr = (volatile void __iomem *)
 		(PAGE_MASK & (unsigned long __force)addr);
 
arch/x86/mm/pageattr.c +1 -0
index bd5e05c..099374b 100644
@@ -227,6 +227,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:
#3 lore raw

RFC 3/3 mmiotrace full patch, preview 3

Whoops, sorry, this is of course titled RFC 3/3.

Quoted 169 lines · On Mon, 28 Apr 2008 21:58:54 +0300 Pekka Paalanen <[email protected]> wrote:
Quoted 6 lines · On Mon, 28 Apr 2008 21:45:04 +0300 Pekka Paalanen <[email protected]> wrote:

Hi all,

this is the third full mmiotrace patch set for review (a bit late, sorry). These patches do not apply to any git tree that I know of, but after Ingo pushes a new version of his sched-devel/latest tree, these patches should be subtractable from there.

...

Quoted 4 lines

This first patch adds the bulk of the new code, only introducing new files. The second patch establishes the interfaces to the ftrace framework, and the third patch hooks everything up into the Kbuild system, page fault handler, and ioremap functions.

 arch/x86/Kconfig.debug |   28 ++++++++++++++++++++++++++++
 arch/x86/mm/Makefile   |    5 +++++
 arch/x86/mm/fault.c    |   13 +++++++++++++
 arch/x86/mm/ioremap.c  |    9 ++++++++-
 arch/x86/mm/pageattr.c |    1 +
 5 files changed, 55 insertions(+), 1 deletions(-)
arch/x86/Kconfig.debug +28 -0
index 1f237d2..fb0794e 100644
@@ -170,6 +170,34 @@ 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
+
+config MMIOTRACE
+	bool "Memory mapped IO tracing"
+	depends on DEBUG_KERNEL && PCI
+	select TRACING
+	select MMIOTRACE_HOOKS
+	default y
+	help
+	  Mmiotrace traces Memory Mapped I/O access and is meant for
+	  debugging and reverse engineering. It is called from the ioremap
+	  implementation and works via page faults. Tracing is disabled by
+	  default and can be enabled at run-time.
+
+	  See Documentation/tracers/mmiotrace.txt.
+	  If you are not helping to develop drivers, say N.
+
+config MMIOTRACE_TEST
+	tristate "Test module for mmiotrace"
+	depends on MMIOTRACE && m
+	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 b7b3e4c..07dab50 100644
@@ -8,6 +8,11 @@ obj-$(CONFIG_X86_PTDUMP)	+= dump_pagetables.o
 
 obj-$(CONFIG_HIGHMEM)		+= highmem_32.o
 
+obj-$(CONFIG_MMIOTRACE_HOOKS)	+= kmmio.o
+obj-$(CONFIG_MMIOTRACE)		+= mmiotrace.o
+mmiotrace-y			:= pf_in.o mmio-mod.o
+obj-$(CONFIG_MMIOTRACE_TEST)	+= testmmiotrace.o
+
 ifeq ($(CONFIG_X86_32),y)
 obj-$(CONFIG_NUMA)		+= discontig_32.o
 else
arch/x86/mm/fault.c +13 -0
index 4145597..0440dce 100644
@@ -26,6 +26,7 @@
 #include <linux/uaccess.h>
 #include <linux/kdebug.h>
 #include <linux/magic.h>
+#include <linux/mmiotrace.h>
 
 #include <asm/system.h>
 #include <asm/desc.h>
@@ -51,6 +52,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
@@ -623,6 +634,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/ioremap.c +8 -1
index d176b23..68b287d 100644
@@ -12,6 +12,7 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
+#include <linux/mmiotrace.h>
 
 #include <asm/cacheflush.h>
 #include <asm/e820.h>
@@ -126,6 +127,7 @@ static void __iomem *__ioremap(resource_size_t phys_addr, unsigned long size,
 	unsigned long new_prot_val;
 	pgprot_t prot;
 	int retval;
+	void __iomem *ret_addr;
 
 	/* Don't allow wraparound or zero size */
 	last_addr = phys_addr + size - 1;
@@ -229,7 +231,10 @@ static void __iomem *__ioremap(resource_size_t phys_addr, unsigned long size,
 		return NULL;
 	}
 
-	return (void __iomem *) (vaddr + offset);
+	ret_addr = (void __iomem *) (vaddr + offset);
+	mmiotrace_ioremap(phys_addr, size, ret_addr);
+
+	return ret_addr;
 }
 
 /**
@@ -306,6 +311,8 @@ void iounmap(volatile void __iomem *addr)
 	    addr < phys_to_virt(ISA_END_ADDRESS))
 		return;
 
+	mmiotrace_iounmap(addr);
+
 	addr = (volatile void __iomem *)
 		(PAGE_MASK & (unsigned long __force)addr);
 
arch/x86/mm/pageattr.c +1 -0
index bd5e05c..099374b 100644
@@ -227,6 +227,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:
Signature · 2 lines
Pekka Paalanen
http://www.iki.fi/pq/
#4 lore raw
In reply to Pekka Paalanen
Quoted 6 lines · Pekka Paalanen

Hi all,

this is the third full mmiotrace patch set for review (a bit late, sorry). These patches do not apply to any git tree that I know of, but after Ingo pushes a new version of his sched-devel/latest tree, these patches should be subtractable from there.

might not be able to release a new sched-devel.git/x86.git tonight - had to filter out lots of badness today from new patches and i dont trust the quality of the current lot yet. (nothing mmiotrace related for that matter)

Ingo

#5 lore raw
In reply to Pekka Paalanen
Quoted 6 lines · On Mon, 28 Apr 2008 21:45:04 +0300 Pekka Paalanen <[email protected]> wrote:

The current state is that mmiotrace seems to work fine, also on SMP, but on SMP there's a chance to miss events due to CPUs racing. At last the log produced via ftrace framework is up-to-spec. Inserting user comments (markers in mmiotrace-parlance) into the log is not yet supported. All in all, after some more testing on my part, IMHO this is in a mergeable state.

Ok, looks like I won't be able to do more testing than what I did today. The sched-devel/latest tree has changes that break the out-of-tree DRM, so I cannot test with Nouveau, and most likely the proprietary driver will not work either. A very nice test would be to enable mmiotracing as early in boot as convinient, but I don't know how to achieve that with the ftrace framework.

I did notice two bugs in mmiotrace while looking at ioremap.c, so I will follow here with three patches.

Tested with the patches and sched-devel/latest of
Tue, 29 Apr 2008 11:47:55 +0000
using testmmiotrace.ko and things looked fine.

Thanks.

Signature · 2 lines
Pekka Paalanen
http://www.iki.fi/pq/
#6 lore raw

PATCH 1/3 x86 mmiotrace: fix page-unaligned ioremaps

Quoted 1 line

From c306ab1bc1f40e74f7015a498784e1a39ec6229b Mon Sep 17 00:00:00 2001

From: Pekka Paalanen <[email protected]>
Date: Tue, 29 Apr 2008 22:39:40 +0300
Subject: [PATCH] x86 mmiotrace: fix page-unaligned ioremaps

mmiotrace_ioremap() expects to receive the original unaligned map phys address and size. Also fix {un,}register_kmmio_probe() to deal properly with unaligned size.

---
 arch/x86/mm/ioremap.c  |    4 +++-
 arch/x86/mm/kmmio.c    |   13 +++++++++++--
 arch/x86/mm/mmio-mod.c |    1 +
 3 files changed, 15 insertions(+), 3 deletions(-)
arch/x86/mm/ioremap.c +3 -1
index ce73980..923621d 100644
@@ -123,6 +123,8 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr,
 {
 	unsigned long pfn, offset, vaddr;
 	resource_size_t last_addr;
+	const resource_size_t unaligned_phys_addr = phys_addr;
+	const unsigned long unaligned_size = size;
 	struct vm_struct *area;
 	unsigned long new_prot_val;
 	pgprot_t prot;
@@ -235,7 +237,7 @@ static void __iomem *__ioremap_caller(resource_size_t phys_addr,
 	}
 
 	ret_addr = (void __iomem *) (vaddr + offset);
-	mmiotrace_ioremap(phys_addr, size, ret_addr);
+	mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
 
 	return ret_addr;
 }
arch/x86/mm/kmmio.c +11 -2
index 40567d8..c12be07 100644
@@ -352,11 +352,19 @@ static void release_kmmio_fault_page(unsigned long page,
 	}
 }
 
+/*
+ * With page-unaligned ioremaps, one or two armed pages may contain
+ * addresses from outside the intended mapping. Events for these addresses
+ * are currently silently dropped. The events may result only from programming
+ * mistakes by accessing addresses before the beginning or past the end of a
+ * mapping.
+ */
 int register_kmmio_probe(struct kmmio_probe *p)
 {
 	unsigned long flags;
 	int ret = 0;
 	unsigned long size = 0;
+	const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
 
 	spin_lock_irqsave(&kmmio_lock, flags);
 	if (get_kmmio_probe(p->addr)) {
@@ -365,7 +373,7 @@ int register_kmmio_probe(struct kmmio_probe *p)
 	}
 	kmmio_count++;
 	list_add_rcu(&p->list, &kmmio_probes);
-	while (size < p->len) {
+	while (size < size_lim) {
 		if (add_kmmio_fault_page(p->addr + size))
 			pr_err("kmmio: Unable to set page fault.\n");
 		size += PAGE_SIZE;
@@ -437,11 +445,12 @@ void unregister_kmmio_probe(struct kmmio_probe *p)
 {
 	unsigned long flags;
 	unsigned long size = 0;
+	const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
 	struct kmmio_fault_page *release_list = NULL;
 	struct kmmio_delayed_release *drelease;
 
 	spin_lock_irqsave(&kmmio_lock, flags);
-	while (size < p->len) {
+	while (size < size_lim) {
 		release_kmmio_fault_page(p->addr + size, &release_list);
 		size += PAGE_SIZE;
 	}
arch/x86/mm/mmio-mod.c +1 -0
index a8d2a00..278998c 100644
@@ -280,6 +280,7 @@ static void ioremap_trace_core(unsigned long offset, unsigned long size,
 {
 	static atomic_t next_id;
 	struct remap_trace *trace = kmalloc(sizeof(*trace), GFP_KERNEL);
+	/* These are page-unaligned. */
 	struct mmiotrace_map map = {
 		.phys = offset,
 		.virt = (unsigned long)addr,
Signature · 1 line
1.5.3.7
#7 lore raw

PATCH 2/3 x86 mmiotrace: use resource_size_t for phys addresses

In reply to Pekka Paalanen
Quoted 1 line

From 921daf84218f4d2b35007f7d867f3301bb59173d Mon Sep 17 00:00:00 2001

From: Pekka Paalanen <[email protected]>
Date: Tue, 29 Apr 2008 22:52:31 +0300
Subject: [PATCH] x86 mmiotrace: use resource_size_t for phys addresses

---
 arch/x86/mm/mmio-mod.c         |   11 ++++++-----
 include/linux/mmiotrace.h      |   14 +++++++-------
 kernel/trace/trace_mmiotrace.c |   20 ++++++++++++--------
 3 files changed, 25 insertions(+), 20 deletions(-)
arch/x86/mm/mmio-mod.c +6 -5
index 278998c..3b04a01 100644
@@ -48,7 +48,7 @@ struct trap_reason {
 struct remap_trace {
 	struct list_head list;
 	struct kmmio_probe probe;
-	unsigned long phys;
+	resource_size_t phys;
 	unsigned long id;
 };
 
@@ -275,7 +275,7 @@ static void post(struct kmmio_probe *p, unsigned long condition,
 	put_cpu_var(pf_reason);
 }
 
-static void ioremap_trace_core(unsigned long offset, unsigned long size,
+static void ioremap_trace_core(resource_size_t offset, unsigned long size,
 							void __iomem *addr)
 {
 	static atomic_t next_id;
@@ -319,13 +319,14 @@ not_enabled:
 	spin_unlock_irq(&trace_lock);
 }
 
-void
-mmiotrace_ioremap(unsigned long offset, unsigned long size, void __iomem *addr)
+void mmiotrace_ioremap(resource_size_t offset, unsigned long size,
+						void __iomem *addr)
 {
 	if (!is_enabled()) /* recheck and proper locking in *_core() */
 		return;
 
-	pr_debug(NAME "ioremap_*(0x%lx, 0x%lx) = %p\n", offset, size, addr);
+	pr_debug(NAME "ioremap_*(0x%llx, 0x%lx) = %p\n",
+				(unsigned long long)offset, size, addr);
 	if ((filter_offset) && (offset != filter_offset))
 		return;
 	ioremap_trace_core(offset, size, addr);
include/linux/mmiotrace.h +7 -7
index de8e912..5cbbc37 100644
@@ -2,7 +2,6 @@
 #define MMIOTRACE_H
 
 #include <linux/types.h>
-
 #include <linux/list.h>
 
 struct kmmio_probe;
@@ -37,14 +36,15 @@ extern int kmmio_handler(struct pt_regs *regs, unsigned long addr);
 
 /* Called from ioremap.c */
 #ifdef CONFIG_MMIOTRACE
-extern void
-mmiotrace_ioremap(unsigned long offset, unsigned long size, void __iomem *addr);
+extern void mmiotrace_ioremap(resource_size_t offset, unsigned long size,
+							void __iomem *addr);
 extern void mmiotrace_iounmap(volatile void __iomem *addr);
 #else
-static inline void
-mmiotrace_ioremap(unsigned long offset, unsigned long size, void __iomem *addr)
+static inline void mmiotrace_ioremap(resource_size_t offset,
+					unsigned long size, void __iomem *addr)
 {
 }
+
 static inline void mmiotrace_iounmap(volatile void __iomem *addr)
 {
 }
@@ -60,7 +60,7 @@ enum mm_io_opcode {
 };
 
 struct mmiotrace_rw {
-	unsigned long phys;	/* PCI address of register */
+	resource_size_t phys;	/* PCI address of register */
 	unsigned long value;
 	unsigned long pc;	/* optional program counter */
 	int map_id;
@@ -69,7 +69,7 @@ struct mmiotrace_rw {
 };
 
 struct mmiotrace_map {
-	unsigned long phys;	/* base address in PCI space */
+	resource_size_t phys;	/* base address in PCI space */
 	unsigned long virt;	/* base virtual address */
 	unsigned long len;	/* mapping size */
 	int map_id;
kernel/trace/trace_mmiotrace.c +12 -8
index 3c1dacd..b13dc19 100644
@@ -184,20 +184,23 @@ static int mmio_print_rw(struct trace_iterator *iter)
 	switch (entry->mmiorw.opcode) {
 	case MMIO_READ:
 		ret = trace_seq_printf(s,
-			"R %d %lu.%06lu %d 0x%lx 0x%lx 0x%lx %d\n",
-			rw->width, secs, usec_rem, rw->map_id, rw->phys,
+			"R %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
+			rw->width, secs, usec_rem, rw->map_id,
+			(unsigned long long)rw->phys,
 			rw->value, rw->pc, 0);
 		break;
 	case MMIO_WRITE:
 		ret = trace_seq_printf(s,
-			"W %d %lu.%06lu %d 0x%lx 0x%lx 0x%lx %d\n",
-			rw->width, secs, usec_rem, rw->map_id, rw->phys,
+			"W %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
+			rw->width, secs, usec_rem, rw->map_id,
+			(unsigned long long)rw->phys,
 			rw->value, rw->pc, 0);
 		break;
 	case MMIO_UNKNOWN_OP:
 		ret = trace_seq_printf(s,
-			"UNKNOWN %lu.%06lu %d 0x%lx %02x,%02x,%02x 0x%lx %d\n",
-			secs, usec_rem, rw->map_id, rw->phys,
+			"UNKNOWN %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
+			secs, usec_rem, rw->map_id,
+			(unsigned long long)rw->phys,
 			(rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
 			(rw->value >> 0) & 0xff, rw->pc, 0);
 		break;
@@ -223,8 +226,9 @@ static int mmio_print_map(struct trace_iterator *iter)
 	switch (entry->mmiorw.opcode) {
 	case MMIO_PROBE:
 		ret = trace_seq_printf(s,
-			"MAP %lu.%06lu %d 0x%lx 0x%lx 0x%lx 0x%lx %d\n",
-			secs, usec_rem, m->map_id, m->phys, m->virt, m->len,
+			"MAP %lu.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
+			secs, usec_rem, m->map_id,
+			(unsigned long long)m->phys, m->virt, m->len,
 			0UL, 0);
 		break;
 	case MMIO_UNPROBE:
Signature · 1 line
1.5.3.7
#8 lore raw

PATCH 3/3 mmiotrace: rename kmmio_probe::user_data to :private.

In reply to Pekka Paalanen
Quoted 1 line

From ecd0591bcefb2bbd934de25cadd0f7cb5dd2b7e1 Mon Sep 17 00:00:00 2001

From: Pekka Paalanen <[email protected]>
Date: Tue, 29 Apr 2008 22:56:26 +0300
Subject: [PATCH] mmiotrace: rename kmmio_probe::user_data to :private.

---
 arch/x86/mm/mmio-mod.c    |    4 ++--
 include/linux/mmiotrace.h |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/mm/mmio-mod.c +2 -2
index 3b04a01..ed0e0e9 100644
@@ -191,7 +191,7 @@ static void pre(struct kmmio_probe *p, struct pt_regs *regs,
 	struct mmiotrace_rw *my_trace = &get_cpu_var(cpu_trace);
 	const unsigned long instptr = instruction_pointer(regs);
 	const enum reason_type type = get_ins_type(instptr);
-	struct remap_trace *trace = p->user_data;
+	struct remap_trace *trace = p->private;
 
 	/* it doesn't make sense to have more than one active trace per cpu */
 	if (my_reason->active_traces)
@@ -299,7 +299,7 @@ static void ioremap_trace_core(resource_size_t offset, unsigned long size,
 			.len = size,
 			.pre_handler = pre,
 			.post_handler = post,
-			.user_data = trace
+			.private = trace
 		},
 		.phys = offset,
 		.id = atomic_inc_return(&next_id)
include/linux/mmiotrace.h +1 -1
index 5cbbc37..61d19e1 100644
@@ -18,7 +18,7 @@ struct kmmio_probe {
 	unsigned long len; /* length of the probe region */
 	kmmio_pre_handler_t pre_handler; /* Called before addr is executed. */
 	kmmio_post_handler_t post_handler; /* Called after addr is executed */
-	void *user_data;
+	void *private;
 };
 
 /* kmmio is active by some kmmio_probes? */
Signature · 1 line
1.5.3.7
#9 lore raw

Re: [PATCH 3/3] mmiotrace: rename kmmio_probe::user_data to :private.

thanks Pekka, applied all 3 patches.

Ingo

#10 lore raw

PATCH 1/1 x86 mmiotrace: page level is unsigned

Quoted 1 line

From 8261c3eeead01739d18875797816637621d17c2d Mon Sep 17 00:00:00 2001

From: Pekka Paalanen <[email protected]>
Date: Thu, 1 May 2008 16:15:11 +0300
Subject: [PATCH] x86 mmiotrace: page level is unsigned

Fixes some sparse warnings.

---

There are three other sparse warnings, too, but two are false positives and one is a code style issue. I do not know how to annotate those away, or if it is even possible, so I will probably write a proper question some time after the merge window closes about how to deal with those.

 arch/x86/mm/kmmio.c    |   13 +++++++------
 arch/x86/mm/mmio-mod.c |    2 +-
 2 files changed, 8 insertions(+), 7 deletions(-)
arch/x86/mm/kmmio.c +7 -6
index c12be07..93d8203 100644
@@ -105,11 +105,12 @@ static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long page)
 	return NULL;
 }
 
-static void set_page_present(unsigned long addr, bool present, int *pglevel)
+static void set_page_present(unsigned long addr, bool present,
+							unsigned int *pglevel)
 {
 	pteval_t pteval;
 	pmdval_t pmdval;
-	int level;
+	unsigned int level;
 	pmd_t *pmd;
 	pte_t *pte = lookup_address(addr, &level);
 
@@ -146,15 +147,15 @@ static void set_page_present(unsigned long addr, bool present, int *pglevel)
 }
 
 /** Mark the given page as not present. Access to it will trigger a fault. */
-static void arm_kmmio_fault_page(unsigned long page, int *page_level)
+static void arm_kmmio_fault_page(unsigned long page, unsigned int *pglevel)
 {
-	set_page_present(page & PAGE_MASK, false, page_level);
+	set_page_present(page & PAGE_MASK, false, pglevel);
 }
 
 /** Mark the given page as present. */
-static void disarm_kmmio_fault_page(unsigned long page, int *page_level)
+static void disarm_kmmio_fault_page(unsigned long page, unsigned int *pglevel)
 {
-	set_page_present(page & PAGE_MASK, true, page_level);
+	set_page_present(page & PAGE_MASK, true, pglevel);
 }
 
 /*
arch/x86/mm/mmio-mod.c +1 -1
index ed0e0e9..e7397e1 100644
@@ -137,7 +137,7 @@ static ssize_t write_marker(struct file *file, const char __user *buffer,
 
 static void print_pte(unsigned long address)
 {
-	int level;
+	unsigned int level;
 	pte_t *pte = lookup_address(address, &level);
 
 	if (!pte) {
Signature · 1 line
1.5.3.7
#11 lore raw
In reply to Pekka Paalanen
Quoted 11 lines · On Tue, 29 Apr 2008 23:40:21 +0300 Pekka Paalanen <[email protected]> wrote:
Quoted 6 lines · On Mon, 28 Apr 2008 21:45:04 +0300 Pekka Paalanen <[email protected]> wrote:

The current state is that mmiotrace seems to work fine, also on SMP, but on SMP there's a chance to miss events due to CPUs racing. At last the log produced via ftrace framework is up-to-spec. Inserting user comments (markers in mmiotrace-parlance) into the log is not yet supported. All in all, after some more testing on my part, IMHO this is in a mergeable state.

Ok, looks like I won't be able to do more testing than what I did today.

...

Quoted 4 lines

Tested with the patches and sched-devel/latest of
Tue, 29 Apr 2008 11:47:55 +0000
using testmmiotrace.ko and things looked fine.

Hi,

I did get the nvidia blob to work, with some help, and tried mmiotrace on it. Worked perfectly. The machine is Thinkpad T61 with a Core 2 Duo. Mmiotrace disabled and enabled the extra core without a glitch, and did not even drop any events with the default ftrace buffer size. Previously while tracing Nouveau, I had to increase the buffer size, which worked well, too.

I did a second trace run after doing
echo 1 > /debug/tracing/trace_entries
and as expected, it lost a lot of events, but showed no problems. Then I disabled mmiotrace while in X, the 2nd cpu core came back online, and everything was good. Losing events is logged into the kernel log once per mmiotrace activation, and into the trace whenever noticed.

(On athlon64 3000+, x86_64:)
Earlier I tried unaligned accesses with testmmiotrace.ko, by mapping from an odd start address and doing 8, 16 and 32 bit reads and writes. There were no crashes, and at a page boundary it resulted in a recursive probe hit, which was resolved by disarming the both pages. Only one page is re-armed, so the other page is left "blind", but did not seem to harm stability.

Conclusion: green light from me!

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