Linux Kernel Markers 0.13 for 2.6.17
Subsystems (14)
PATCH 1/1 Linux Kernel Markers 0.13 for 2.6.17
Hi,
Here is a corrected version. Following Jeremy Fitzhardinge's advices :
- I now use memory constraints on an extern symbol to keep inline assembly in the right order.
- I declare the symbols around and inside the 2 bytes jump in assembly to make sure that gcc will not insert code, padding, etc... All the assembly alignment+instruction is all in one inline asm now.
- Use variable argument list in the probe (safe on every architecture). Thanks to Frank and Jeremy for pointing this out.
Thanks for all your comments,
Mathieu
---BEGIN---
Makefile +2 -1
@@ -301,7 +301,8 @@ # Use LINUXINCLUDE when you must referen # Needed to be compatible with the O= option LINUXINCLUDE := -Iinclude \ $(if $(KBUILD_SRC),-Iinclude2 -I$(srctree)/include) \ - -include include/linux/autoconf.h + -include include/linux/autoconf.h \ + -include include/linux/marker.h CPPFLAGS := -D__KERNEL__ $(LINUXINCLUDE)
arch/alpha/Kconfig +6 -0
@@ -630,6 +630,12 @@ source "fs/Kconfig" source "arch/alpha/oprofile/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/alpha/Kconfig.debug" source "security/Kconfig"
arch/arm/Kconfig +6 -0
@@ -871,6 +871,12 @@ source "fs/Kconfig" source "arch/arm/oprofile/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/arm/Kconfig.debug" source "security/Kconfig"
arch/arm26/Kconfig +6 -0
@@ -232,6 +232,12 @@ source "drivers/misc/Kconfig" source "drivers/usb/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/arm26/Kconfig.debug" source "security/Kconfig"
arch/cris/Kconfig +6 -0
@@ -179,6 +179,12 @@ source "sound/Kconfig" source "drivers/usb/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/cris/Kconfig.debug" source "security/Kconfig"
arch/frv/Kconfig +6 -0
@@ -345,6 +345,12 @@ source "drivers/Kconfig" source "fs/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/frv/Kconfig.debug" source "security/Kconfig"
arch/h8300/Kconfig +6 -0
@@ -197,6 +197,12 @@ endmenu source "fs/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/h8300/Kconfig.debug" source "security/Kconfig"
arch/i386/Kconfig +3 -0
@@ -1081,6 +1081,9 @@ config KPROBES a probepoint and specifies the callback. Kprobes is useful for kernel debugging, non-intrusive instrumentation and testing. If in doubt, say "N". + +source "kernel/Kconfig.marker" + endmenu source "arch/i386/Kconfig.debug"
arch/ia64/Kconfig +3 -0
@@ -499,6 +499,9 @@ config KPROBES a probepoint and specifies the callback. Kprobes is useful for kernel debugging, non-intrusive instrumentation and testing. If in doubt, say "N". + +source "kernel/Kconfig.marker" + endmenu source "arch/ia64/Kconfig.debug"
arch/m32r/Kconfig +6 -0
@@ -386,6 +386,12 @@ source "fs/Kconfig" source "arch/m32r/oprofile/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/m32r/Kconfig.debug" source "security/Kconfig"
arch/m68k/Kconfig +6 -0
@@ -652,6 +652,12 @@ endmenu source "fs/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/m68k/Kconfig.debug" source "security/Kconfig"
arch/m68knommu/Kconfig +6 -0
@@ -652,6 +652,12 @@ source "drivers/Kconfig" source "fs/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/m68knommu/Kconfig.debug" source "security/Kconfig"
arch/mips/Kconfig +6 -0
@@ -1850,6 +1850,12 @@ source "fs/Kconfig" source "arch/mips/oprofile/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/mips/Kconfig.debug" source "security/Kconfig"
arch/parisc/Kconfig +6 -0
@@ -251,6 +251,12 @@ source "fs/Kconfig" source "arch/parisc/oprofile/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/parisc/Kconfig.debug" source "security/Kconfig"
arch/powerpc/Kconfig +3 -0
@@ -1011,6 +1011,9 @@ config KPROBES a probepoint and specifies the callback. Kprobes is useful for kernel debugging, non-intrusive instrumentation and testing. If in doubt, say "N". + +source "kernel/Kconfig.marker" + endmenu source "arch/powerpc/Kconfig.debug"
arch/ppc/Kconfig +6 -0
@@ -1412,6 +1412,12 @@ source "lib/Kconfig" source "arch/powerpc/oprofile/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/ppc/Kconfig.debug" source "security/Kconfig"
arch/s390/Kconfig +6 -0
@@ -476,6 +476,12 @@ source "fs/Kconfig" source "arch/s390/oprofile/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/s390/Kconfig.debug" source "security/Kconfig"
arch/sh/Kconfig +6 -0
@@ -644,6 +644,12 @@ source "fs/Kconfig" source "arch/sh/oprofile/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/sh/Kconfig.debug" source "security/Kconfig"
arch/sh64/Kconfig +6 -0
@@ -276,6 +276,12 @@ source "fs/Kconfig" source "arch/sh64/oprofile/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/sh64/Kconfig.debug" source "security/Kconfig"
arch/sparc/Kconfig +6 -0
@@ -289,6 +289,12 @@ endmenu source "fs/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/sparc/Kconfig.debug" source "security/Kconfig"
arch/sparc64/Kconfig +3 -0
@@ -419,6 +419,9 @@ config KPROBES a probepoint and specifies the callback. Kprobes is useful for kernel debugging, non-intrusive instrumentation and testing. If in doubt, say "N". + +source "kernel/Kconfig.marker" + endmenu source "arch/sparc64/Kconfig.debug"
arch/um/Kconfig +6 -0
@@ -312,4 +312,10 @@ config INPUT bool default n +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/um/Kconfig.debug"
arch/v850/Kconfig +6 -0
@@ -324,6 +324,12 @@ source "sound/Kconfig" source "drivers/usb/Kconfig" +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/v850/Kconfig.debug" source "security/Kconfig"
arch/x86_64/Kconfig +3 -0
@@ -611,6 +611,9 @@ config KPROBES a probepoint and specifies the callback. Kprobes is useful for kernel debugging, non-intrusive instrumentation and testing. If in doubt, say "N". + +source "kernel/Kconfig.marker" + endmenu source "arch/x86_64/Kconfig.debug"
arch/xtensa/Kconfig +6 -0
@@ -255,6 +255,12 @@ config EMBEDDED_RAMDISK_IMAGE provide one yourself. endmenu +menu "Instrumentation Support" + +source "kernel/Kconfig.marker" + +endmenu + source "arch/xtensa/Kconfig.debug" source "security/Kconfig"
include/asm-alpha/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-arm/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-arm26/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-cris/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-frv/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-generic/marker.h new file +22 -0
@@ -0,0 +1,22 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Generic header. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#define MARK_CALL(name, format, args...) \ + do { \ + static marker_probe_func *__mark_call_##name = \ + __mark_empty_function; \ + asm volatile( ".section .markers, \"a\";\n\t" \ + ".long %1, %2;\n\t" \ + ".previous;\n\t" : "+m" (__marker_sequencer) : \ + "m" (__mark_call_##name), \ + "m" (*format)); \ + preempt_disable(); \ + (*__mark_call_##name)(format, ## args); \ + preempt_enable_no_resched(); \ + } while(0)
include/asm-generic/vmlinux.lds.h +6 -0
@@ -90,6 +90,12 @@ #define RODATA \ __ksymtab_strings : AT(ADDR(__ksymtab_strings) - LOAD_OFFSET) { \ *(__ksymtab_strings) \ } \ +/* Kernel markers : pointers */ \ + .markers : AT(ADDR(.markers) - LOAD_OFFSET) { \ + VMLINUX_SYMBOL(__start___markers) = .; \ + *(.markers) \ + VMLINUX_SYMBOL(__stop___markers) = .; \ + } \ __end_rodata = .; \ . = ALIGN(4096); \ \
include/asm-h8300/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-i386/marker.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. i386 architecture optimisations. +* +* (C) Copyright 2006 Mathieu Desnoyers <[email protected]> +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h> + +#define ARCH_HAS_MARK_JUMP + +/* Note : max 256 bytes between over_label and near_jump */ +#define MARK_JUMP(name, format, args...) \ + do { \ + asm volatile( ".section .markers, \"a\";\n\t" \ + ".long 0f, 1f, 2f;\n\t" \ + ".previous;\n\t" \ + ".align 16;\n\t" \ + ".byte 0xeb;\n\t" \ + "0:\n\t" \ + ".byte 2f-1f;\n\t" \ + "1:\n\t" \ + : "+m" (__marker_sequencer) : ); \ + MARK_CALL(name, format, ## args); \ + asm volatile ( "2:\n\t" : "+m" (__marker_sequencer) : ); \ + } while(0)
include/asm-ia64/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-m32r/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-m68k/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-m68knommu/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-mips/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-parisc/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-powerpc/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-ppc/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-ppc64/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-s390/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-sh/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-sh64/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-sparc/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-sparc64/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-um/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-v850/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-x86_64/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/asm-xtensa/marker.h new file +13 -0
@@ -0,0 +1,13 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Architecture specific +* optimisations. +* +* No optimisation implemented. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h>
include/linux/marker.h new file +90 -0
90 lines not on this page · raw message
include/linux/module.h +2 -0
@@ -254,6 +254,8 @@ struct moduleconst struct kernel_symbol *syms;unsigned int num_syms;const unsigned long *crcs; +const struct __mark_marker *markers; +unsigned int num_markers;/* GPL-only exported symbols. */ const struct kernel_symbol *gpl_syms;
kernel/Kconfig.marker new file +18 -0
@@ -0,0 +1,18 @@ +# Code markers configuration + +config MARKERS + bool "Activate markers" + select MODULES + default n + help + Place an empty function call at each marker site. Can by + dynamically changed for a probe function. + +config MARKERS_FORCE_DIRECT_CALL + bool "Disable markers jump optimisations" + depends EMBEDDED + default n + help + Disable code replacement jump optimisations. Especially useful if your + code is in a read-only rom/flash. +
kernel/module.c +161 -1
198 lines not on this page · raw message
---END---
---BEGIN---
/* probe.c
*
* Loads a function at a marker call site.
*
* (C) Copyright 2006 Mathieu Desnoyers <[email protected]> *
* This file is released under the GPLv2.
* See the file COPYING for more details.
*/
#include <linux/marker.h>
#include <linux/module.h>
#include <linux/kallsyms.h>
/* function to install */
#define DO_MARK1_FORMAT "%d"
void do_mark1(const char *format, ...)
{
va_list ap;
int value;
va_start(ap, format);
value = va_arg(ap, int);
printk("value is %d\n", value);
va_end(ap);
}void do_mark2(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vprintk(format, ap);
va_end(ap);
printk("\n");
}#define DO_MARK3_FORMAT "%d %s %s"
void do_mark3(const char *format, ...)
{
va_list ap;
int value;
const char *s1, *s2;
va_start(ap, format);
value = va_arg(ap, int);
s1 = va_arg(ap, const char*);
s2 = va_arg(ap, const char *);
printk("value is %d %s %s\n",
value, s1, s2);
va_end(ap);
}int init_module(void)
{
int result;
result = marker_set_probe("subsys_mark1", DO_MARK1_FORMAT,
(marker_probe_func*)do_mark1);
if(!result) goto end;
result = marker_set_probe("subsys_mark2", NULL,
(marker_probe_func*)do_mark2);
if(!result) goto cleanup1;
result = marker_set_probe("subsys_mark3", DO_MARK3_FORMAT,
(marker_probe_func*)do_mark3);
if(!result) goto cleanup2;
return 0;cleanup2:
marker_remove_probe((marker_probe_func*)do_mark2); cleanup1: marker_remove_probe((marker_probe_func*)do_mark1); end: return -EPERM; }
void cleanup_module(void)
{
marker_remove_probe((marker_probe_func*)do_mark1);
marker_remove_probe((marker_probe_func*)do_mark2);
marker_remove_probe((marker_probe_func*)do_mark3);
}MODULE_LICENSE("GPL");MODULE_AUTHOR("Mathieu Desnoyers");MODULE_DESCRIPTION("Probe");
---END---
---BEGIN---
/* test-mark.c
*
*/
#include <linux/marker.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <asm/ptrace.h>
volatile int x = 7;
struct proc_dir_entry *pentry = NULL;
static inline void test(struct pt_regs * regs)
{
MARK(kernel_debug_test, "%d %ld %p", 2, regs->eip, regs);
}static int my_open(struct inode *inode, struct file *file)
{
unsigned int i;
for(i=0; i<2; i++) {
MARK(subsys_mark1, "%d", 1);
}
MARK(subsys_mark2, "%d %s %s", 2, "blah2", "blahx");
MARK(subsys_mark3, "%d %s %s", x, "blah3", "blah5");
test(NULL);
test(NULL);
return -EPERM;
}static struct file_operations my_operations = {
.open = my_open,
};
int init_module(void)
{
pentry = create_proc_entry("testmark", 0444, NULL);
if (pentry)
pentry->proc_fops = &my_operations;
marker_list_probe(NULL);
return 0;
}void cleanup_module(void)
{
remove_proc_entry("testmark", NULL);
}MODULE_LICENSE("GPL");MODULE_AUTHOR("Mathieu Desnoyers");MODULE_DESCRIPTION("Marker Test");
---END---
OpenPGP public key: http://krystal.dyndns.org:8080/key/compudj.gpg Key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Just as a precision :
The following sequence (please refer to the code below for local symbols 1 and 2) :
1:preempt_disable()
call (*__mark_call_##name)(format, ## args);preempt_enable_no_resched()
2:
is insured because :
1 is part of an inline assembly with rw dependency on __marker_sequencer the call is surrounded by memory barriers.
2 is part of an inline assembly with rw dependency on __marker_sequencer
Mathieu
Quoted 48 lines · * Mathieu Desnoyers ([email protected]) wrote:
+++
b/include/asm-generic/marker.hinclude/asm-generic/marker.h new file +22
-0@@ -0,0 +1,22 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. Generic header. +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#define MARK_CALL(name, format, args...) \ + do { \ + static marker_probe_func *__mark_call_##name = \ + __mark_empty_function; \ + asm volatile( ".section .markers, \"a\";\n\t" \ + ".long %1, %2;\n\t" \ + ".previous;\n\t" : "+m" (__marker_sequencer) : \ + "m" (__mark_call_##name), \ + "m" (*format)); \ + preempt_disable(); \ + (*__mark_call_##name)(format, ## args); \ + preempt_enable_no_resched(); \ + } while(0)include/asm-i386/marker.h new file +20
-0@@ -0,0 +1,30 @@ +/* +* marker.h +* +* Code markup for dynamic and static tracing. i386 architecture optimisations. +* +* (C) Copyright 2006 Mathieu Desnoyers <[email protected]> +* +* This file is released under the GPLv2. +* See the file COPYING for more details. +*/ + +#include <asm-generic/marker.h> + +#define ARCH_HAS_MARK_JUMP + +/* Note : max 256 bytes between over_label and near_jump */ +#define MARK_JUMP(name, format, args...) \ + do { \ + asm volatile( ".section .markers, \"a\";\n\t" \ + ".long 0f, 1f, 2f;\n\t" \
include/asm-i386/marker.h line 30
Quoted 10 lines
include/asm-i386/marker.h new file +10
-0+".previous;\n\t" \ +".align 16;\n\t" \ +".byte 0xeb;\n\t" \ +"0:\n\t" \ +".byte 2f-1f;\n\t" \ +"1:\n\t" \ + : "+m" (__marker_sequencer) : ); \ +MARK_CALL(name, format, ## args); \ + asm volatile ( "2:\n\t" : "+m" (__marker_sequencer) : ); \ + } while(0)
OpenPGP public key: http://krystal.dyndns.org:8080/key/compudj.gpg Key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Quoted 5 lines · Mathieu Desnoyers
include/asm-i386/marker.h new file +4
-0+/* Note : max 256 bytes between over_label and near_jump */ +#define MARK_JUMP(name, format, args...) \ + do { \ + asm volatile( ".section .markers, \"a\";\n\t" \
include/asm-i386/marker.h line 29
Quoted 10 lines
include/asm-i386/marker.h new file +10
-0+".long 0f, 1f, 2f;\n\t" \ +".previous;\n\t" \ +".align 16;\n\t" \ +".byte 0xeb;\n\t" \ +"0:\n\t" \ +".byte 2f-1f;\n\t" \ +"1:\n\t" \ + : "+m" (__marker_sequencer) : ); \ +MARK_CALL(name, format, ## args); \ + asm volatile ( "2:\n\t" : "+m" (__marker_sequencer) : ); \
Unfortunately this doesn't work either. The two asms are ordered with respect to each other, but there's nothing to 1) stop the MARK_CALL from being moved out between them, 2) something else being moved in between them. I don't really see a way out of this without implementing the whole call in assembler as well, which is a big pain.
J
Quoted 11 lines · Mathieu Desnoyers
Just as a precision :
The following sequence (please refer to the code below for local symbols 1 and 2) :
1:
preempt_disable()
call (*__mark_call_##name)(format, ## args);preempt_enable_no_resched()
2:
Quoted 6 lines
is insured because :
1 is part of an inline assembly with rw dependency on
__marker_sequencerthe call is surrounded by memory barriers.
2 is part of an inline assembly with rw dependency on__marker_sequencer
What do you mean the call is surrounded by memory barriers? Do you mean a call has an implicit barrier, or something else?
Either way, this doesn't prevent some otherwise unrelated non-memory-using code from being scheduled in there, which would not be executed. The gcc manual really strongly discourages jumping between inline asms, because they have basically unpredictable results.
J
Quoted 14 lines · * Jeremy Fitzhardinge ([email protected]) wrote:
Quoted 13 lines · Mathieu Desnoyers
Just as a precision :
The following sequence (please refer to the code below for local symbols 1 and 2) :
1:
preempt_disable()
call (*__mark_call_##name)(format, ## args);preempt_enable_no_resched()
2:is insured because :
Quoted 8 lines
Quoted 4 lines
1 is part of an inline assembly with rw dependency on
__marker_sequencerthe call is surrounded by memory barriers.
2 is part of an inline assembly with rw dependency on__marker_sequencerWhat do you mean the call is surrounded by memory barriers? Do you mean a call has an implicit barrier, or something else?
Yes, preempt_disable() has a barrier(), on gcc :__asm__ __volatile__("": : :"memory").
Quoted 5 lines
Either way, this doesn't prevent some otherwise unrelated non-memory-using code from being scheduled in there, which would not be executed. The gcc manual really strongly discourages jumping between inline asms, because they have basically unpredictable results.
Ok, I will do the call in assembly then.
Mathieu
OpenPGP public key: http://krystal.dyndns.org:8080/key/compudj.gpg Key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Quoted 4 lines · * Mathieu Desnoyers ([email protected]) wrote:
Yes,
preempt_disable()has abarrier(), on gcc :__asm____volatile__("": : :"memory").
Quoted 8 lines
Quoted 5 lines
Either way, this doesn't prevent some otherwise unrelated non-memory-using code from being scheduled in there, which would not be executed. The gcc manual really strongly discourages jumping between inline asms, because they have basically unpredictable results.
Ok, I will do the call in assembly then.
Before I rush on a solution too fast... I have a question for you :
To protect code from being preempted, the macros preempt_disable and preempt_enable must normally be used. Logically, this macro must make sure gcc doesn't interleave preemptible code and non-preemptible code.
Starting with this hypothesis, what makes gcc aware of this ? If we check preempt_disable (the disable call is almost symmetric) :
linux/preempt.h:
define add_preempt_count(val) do { preempt_count() += (val); } while (0)
#define inc_preempt_count() add_preempt_count(1)
#define preempt_disable() \
do { \
inc_preempt_count(); \
barrier(); \
} while (0)So the magic must be in the barrier() macro :
linux/compiler-gcc.h:
/* Optimization barrier */
/* The "volatile" is due to gcc bugs */ #define barrier() __asm__ __volatile__("": : :"memory")
Which makes me think that if I put barriers around my asm, call, asm trio, no other code will be interleaved. Is it right ?
Mathieu
OpenPGP public key: http://krystal.dyndns.org:8080/key/compudj.gpg Key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Quoted 4 lines · Mathieu Desnoyers
To protect code from being preempted, the macros
preempt_disableandpreempt_enablemust normally be used. Logically, this macro must make sure gcc doesn't interleave preemptible code and non-preemptible code.
No, it only needs to prevent globally visible side-effects from being moved into/out of preemptable blocks. In practice that means memory updates (including the implicit ones that calls to external functions are assumed to make).
Quoted 3 lines
Which makes me think that if I put barriers around my asm, call, asm trio, no other code will be interleaved. Is it right ?
No global side effects, but code with local side effects could be moved around without changing the meaning of preempt.
For example:
int foo; extern int global; foo = some_function(); foo += 42; preempt_disable(); // stuff preempt_enable(); global = foo; foo += other_thing();
Assume here that some_function and other_function are extern, and so gcc has no insight into their behaviour and therefore conservatively assumes they have global side-effects.
The memory barriers in preempt_disable/enable will prevent gcc from moving any of the function calls into the non-preemptable region. But because "foo" is local and isn't visible to any other code, there's no reason why the "foo += 42" couldn't move into the preempt region. Likewise, the assignment to "global" can't move out of the range between the preempt_enable and the call to other_thing().
So in your case, if your equivalent of the non-preemptable block is the call to the marker function, then there's a good chance that the compiler might decide to move some other code in there.
Now it might be possible to take the addresses of labels to inhibit code motion into a particular range:
{
__label__ before, after;
asm volatile("" : : "m" (*&&before), "m" (*&&after)); // gcc can't know what we're doing with the labels
before: ;
// stuff
after: ;
}but that might be risky for several reasons: I don't know of any particular promises gcc makes in this circumstance; I suspect taking the address of a label will have a pretty severe inhibition on what optimisations gcc's is willing to use (it may prevent inlining altogether); and this looks pretty unusual, so there could be bugs.
J
First of all, yes, gcc seems not to allow a programmer to jump between asm labels without painfully verifying everything, as stated in
http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/Extended-Asm.html
"Speaking of labels, jumps from one asm to another are not supported. The compiler's optimizers do not know about these jumps, and therefore they cannot take account of them when deciding how to optimize."
however... (see below)
Quoted 36 lines · * Jeremy Fitzhardinge ([email protected]) wrote:
Quoted 5 lines · Mathieu Desnoyers
To protect code from being preempted, the macros
preempt_disableandpreempt_enablemust normally be used. Logically, this macro must make sure gcc
doesn't interleave preemptible code and non-preemptible code.No, it only needs to prevent globally visible side-effects from being moved into/out of preemptable blocks. In practice that means memory updates (including the implicit ones that calls to external functions are assumed to make).
Quoted 4 lines
Which makes me think that if I put barriers around my asm, call, asm trio, no
other code will be interleaved. Is it right ?No global side effects, but code with local side effects could be moved around without changing the meaning of preempt.
For example:
int foo; extern int global; foo = some_function(); foo += 42; preempt_disable(); // stuff preempt_enable(); global = foo; foo += other_thing();
Quoted 8 lines
Assume here that
some_functionandother_functionare extern, and so gcc has no insight into their behaviour and therefore conservatively assumes they have global side-effects.The memory barriers in preempt_disable/enable will prevent gcc from moving any of the function calls into the non-preemptable region. But because "foo" is local and isn't visible to any other code, there's no reason why the "foo += 42" couldn't move into the preempt region.
I am not sure about this last statement. The same reference :
http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/Extended-Asm.html
States :
"If your assembler instructions access memory in an unpredictable fashion, add memory to the list of clobbered registers. This will cause GCC to not keep memory values cached in registers across the assembler instruction and not optimize stores or loads to that memory. You will also want to add the volatile keyword if the memory affected is not listed in the inputs or outputs of the asm, as the memory clobber does not count as a side-effect of the asm. If you know how large the accessed memory is, you can add it as input or output but if this is not known, you should add memory. As an example, if you access ten bytes of a string, you can use a memory input like:
{"m"( ({ struct { char x[10]; } *p = (void *)ptr ; *p; }) )} "
I am just wondering how gcc can assume that I will not modify variables on the stack from within a function with a memory clobber. If I would like to do some nasty things in my assembly code, like accessing directly to a local variable by using an offset from the stack pointer, I would expect gcc not to relocate this local variable around my asm volatile memory clobbered statement, as it falls under the category "access memory in an unpredictable fashion".
Mathieu
OpenPGP public key: http://krystal.dyndns.org:8080/key/compudj.gpg Key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Quoted 32 lines · Mathieu Desnoyers
Quoted 32 lines
Quoted 5 lines · Mathieu Desnoyers
To protect code from being preempted, the macros
preempt_disableandpreempt_enablemust normally be used. Logically, this macro must make sure gcc doesn't interleave preemptible code and non-preemptible code.No, it only needs to prevent globally visible side-effects from being moved into/out of preemptable blocks. In practice that means memory updates (including the implicit ones that calls to external functions are assumed to make).
Quoted 4 lines
Which makes me think that if I put barriers around my asm, call, asm trio, no other code will be interleaved. Is it right ?
No global side effects, but code with local side effects could be moved around without changing the meaning of preempt.
For example:
int foo; extern int global; foo = some_function(); foo += 42; preempt_disable(); // stuff preempt_enable(); global = foo; foo += other_thing();
Quoted 8 lines
Quoted 4 lines
Assume here that
some_functionandother_functionare extern, and so gcc has no insight into their behaviour and therefore conservatively assumes they have global side-effects.The memory barriers in preempt_disable/enable will prevent gcc from moving any of the function calls into the non-preemptable region. But because "foo" is local and isn't visible to any other code, there's no reason why the "foo += 42" couldn't move into the preempt region.
I am not sure about this last statement. The same reference :
http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/Extended-Asm.html
(This is pretty old, and this is an area which changes quite a lot. You should refer to something more recent;
http://www.cims.nyu.edu/cgi-systems/info2html?/usr/local/info(gcc)Top for example, though in this case the quoted text looks the same.)
Quoted 7 lines
I am just wondering how gcc can assume that I will not modify variables on the stack from within a function with a memory clobber. If I would like to do some nasty things in my assembly code, like accessing directly to a local variable by using an offset from the stack pointer, I would expect gcc not to relocate this local variable around my asm volatile memory clobbered statement, as it falls under the category "access memory in an unpredictable fashion".
That not really what it means. gcc is free to put local variables in memory or register, and unless you pass the local to your asm as a parameter, your code has no way of knowing how to find the current location of the local. You could trash your stack frame from within the asm if you like, but I don't think gcc is under any obligation to behave in a deterministic way if you do.
"Unpredictable" in this case means that the memory modified isn't easily specified as a normal asm parameter. For example, if you have an asm which does a memset(), the modified memory's size is a runtime variable rather than a compile-time constant. Or perhaps your asm follows a linked list and modifies memory as it traverses the list.
J
Hi,
Ok, so as far as I can see, we can only control the execution flow by modifying values in the output list of the asm.
Do you think the following would work ?
#define MARK_JUMP(name, format, args...) \
do { \
char condition; \
asm volatile( ".section .markers, \"a\";\n\t" \
".long 0f;\n\t" \
".previous;\n\t" \
"0:\n\t" \
"movb $0,%1;\n\t" \
: "+m" (__marker_sequencer), \
"=r" (condition) : ); \
if(unlikely(condition)) { \
MARK_CALL(name, format, ## args); \
} \
} while(0)The jump is left to gcc, we only modify an immediate value (a byte) to change the selection. The is no memory load involved on the fast path :
...
6: b0 00 mov $0x0,%al 8: 84 c0 test %al,%al a: 75 07 jne 13 <my_open+0x13> c: b8 ff ff ff ff mov $0xffffffff,%eax 11: c9 leave 12: c3 ret 13: b8 01 00 00 00 mov $0x1,%eax 18: e8 fc ff ff ff call 19 <my_open+0x19> 1d: c7 44 24 0c 00 00 00 movl $0x0,0xc(%esp) 24: 00 25: c7 44 24 08 06 00 00 movl $0x6,0x8(%esp) 2c: 00 2d: c7 44 24 04 02 00 00 movl $0x2,0x4(%esp) 34: 00 35: c7 04 24 0c 00 00 00 movl $0xc,(%esp) 3c: ff 15 94 00 00 00 call *0x94 42: b8 01 00 00 00 mov $0x1,%eax 47: e8 fc ff ff ff call 48 <my_open+0x48> 4c: eb be jmp c <my_open+0xc>
Mathieu
Quoted 77 lines · * Jeremy Fitzhardinge ([email protected]) wrote:
Quoted 42 lines · Mathieu Desnoyers
Quoted 42 lines
Quoted 6 lines · Mathieu Desnoyers
To protect code from being preempted, the macros
preempt_disableandpreempt_enablemust normally be used. Logically, this macro must make sure gcc
doesn't interleave preemptible code and non-preemptible code.No, it only needs to prevent globally visible side-effects from being moved into/out of preemptable blocks. In practice that means memory updates (including the implicit ones that calls to external functions are assumed to make).
Quoted 5 lines
Which makes me think that if I put barriers around my asm, call, asm trio, no
other code will be interleaved. Is it right ?No global side effects, but code with local side effects could be moved around without changing the meaning of preempt.
For example:
int foo; extern int global; foo = some_function(); foo += 42; preempt_disable(); // stuff preempt_enable(); global = foo; foo += other_thing();Assume here that
some_functionandother_functionare extern, and so gcc has no insight into their behaviour and therefore conservatively assumes they have global side-effects.Quoted 9 lines
Quoted 5 lines
The memory barriers in preempt_disable/enable will prevent gcc from moving any of the function calls into the non-preemptable region. But because "foo" is local and isn't visible to any other code, there's no reason why the "foo += 42" couldn't move into the preempt region.
I am not sure about this last statement. The same reference :
http://developer.apple.com/documentation/DeveloperTools/gcc-4.0.1/gcc/Extended-Asm.html(This is pretty old, and this is an area which changes quite a lot. You should refer to something more recent;
http://www.cims.nyu.edu/cgi-systems/info2html?/usr/local/info(gcc)Top
for example, though in this case the quoted text looks the same.)Quoted 12 lines
I am just wondering how gcc can assume that I will not modify variables on the
stack from within a function with a memory clobber. If I would like to do some
nasty things in my assembly code, like accessing directly to a local variable by
using an offset from the stack pointer, I would expect gcc not to relocate this
local variable around my asm volatile memory clobbered statement, as it falls
under the category "access memory in an unpredictable fashion".That not really what it means. gcc is free to put local variables in memory or register, and unless you pass the local to your asm as a parameter, your code has no way of knowing how to find the current location of the local. You could trash your stack frame from within the asm if you like, but I don't think gcc is under any obligation to behave in a deterministic way if you do.
Quoted 9 lines
"Unpredictable" in this case means that the memory modified isn't easily specified as a normal asm parameter. For example, if you have an asm which does a
memset(), the modified memory's size is a runtime variable rather than a compile-time constant. Or perhaps your asm follows a linked list and modifies memory as it traverses the list.J
OpenPGP public key: http://krystal.dyndns.org:8080/key/compudj.gpg Key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Quoted 8 lines · Mathieu Desnoyers
Hi,
Ok, so as far as I can see, we can only control the execution flow by modifying values in the output list of the asm.
Do you think the following would work ?
Quoted 15 lines
#define MARK_JUMP(name, format, args...) \
do { \
char condition; \
asm volatile( ".section .markers, \"a\";\n\t" \
".long 0f;\n\t" \
".previous;\n\t" \
"0:\n\t" \
"movb $0,%1;\n\t" \
: "+m" (__marker_sequencer), \
"=r" (condition) : ); \
if(unlikely(condition)) { \
MARK_CALL(name, format, ## args); \
} \
} while(0)Yep, that looks reasonable. Though you could just directly test a per-marker enable flag, rather than using "condition"...
J
Quoted 26 lines · * Jeremy Fitzhardinge ([email protected]) wrote:
Quoted 9 lines · Mathieu Desnoyers
Hi,
Ok, so as far as I can see, we can only control the execution flow by modifying
values in the output list of the asm.Do you think the following would work ?
Quoted 15 lines
#define MARK_JUMP(name, format, args...) \ do { \ char condition; \ asm volatile( ".section .markers, \"a\";\n\t" \ ".long 0f;\n\t" \ ".previous;\n\t" \ "0:\n\t" \ "movb $0,%1;\n\t" \ : "+m" (__marker_sequencer), \ "=r" (condition) : ); \ if(unlikely(condition)) { \ MARK_CALL(name, format, ## args); \ } \ } while(0)
Quoted 3 lines
Yep, that looks reasonable. Though you could just directly test a per-marker enable flag, rather than using "condition"...
The goal of the local variable "condition" here is that it will be forced to be a register (=r in asm output), so there is no memory load involved (immediate value).
I am not sure I understand your suggestion correctly.. do you mean having a per-marker flag that would be loaded and tested at every marker site ?
Mathieu
OpenPGP public key: http://krystal.dyndns.org:8080/key/compudj.gpg Key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68
Quoted 6 lines · Mathieu Desnoyers
[...]
Quoted 2 lines
Yep, that looks reasonable. Though you could just directly test a per-marker enable flag, rather than using "condition"...
[...]
I am not sure I understand your suggestion correctly.. do you mean having a per-marker flag that would be loaded and tested at every marker site ?
I gather that one reason for working so hard with the inline assembly is a race condition problem with the plain STAP_MARK style of marker disconnection:
if (pointer) (*pointer)(args ...);
Granted, but this problem could almost certainly be dealt with simpler than that. How about a compxchg or other atomic-fetch of the static pointer with a local variable? That should solve the worry of an (*NULL) call.
If we then become concerned with a valid pointer become obsolete (the probe handler function wanting to unload), we might be able to use some RCU-type deferral mechanism and/or preempt controls to ensure that this does not happen.
- FChE
Quoted 13 lines · * Frank Ch. Eigler ([email protected]) wrote:
Quoted 6 lines · Mathieu Desnoyers
[...]
Quoted 2 lines
Yep, that looks reasonable. Though you could just directly test a per-marker enable flag, rather than using "condition"...
[...]
I am not sure I understand your suggestion correctly.. do you mean having a per-marker flag that would be loaded and tested at every marker site ?I gather that one reason for working so hard with the inline assembly is a race condition problem with the plain
STAP_MARKstyle of marker disconnection:
Quoted 7 lines
if (pointer) (*pointer)(args ...);Granted, but this problem could almost certainly be dealt with simpler than that. How about a compxchg or other atomic-fetch of the static pointer with a local variable? That should solve the worry of an (*NULL) call.
I don't really see how cmpxchg might be needed here.
Atomic fetch of a static variable is how I will do it in my next version for the non optimized case :
volatile static var = 0;
if(var) {
preempt disable
call
preempt_enable_no_resched
}
But, still, in the optimized case, the if(var) will depend on an immediate value, therefore saving the memory read.
Quoted 5 lines
If we then become concerned with a valid pointer become obsolete (the probe handler function wanting to unload), we might be able to use some RCU-type deferral mechanism and/or preempt controls to ensure that this does not happen.
This is exactly why the preemption is disabled around the call. However, RCU must always _see_ a coherent version of the structure in memory.
Calling an empty function, disabling preemption around the call and calling synchronize_sched() before deleting the removed function looks very much like a RCU-style protection (actually, that's what it is).
Mathieu
OpenPGP public key: http://krystal.dyndns.org:8080/key/compudj.gpg Key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68