Skip to contentSkip to conversation

Uprobes patchset with perf probe support

PATCH Superseded Srikar Dronamraju 9 participants 45 messages Jan 10, 2012 – Jan 30, 2012

Subsystems (10)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v9 cover Uprobes patchset with perf probe support

This patchset implements Uprobes which enables you to dynamically probe any routine in a user space application and collect information non-disruptively.

This patchset resolves most of the comments on the previous posting () patchset applies on top of
commit (805a6af8dba Linux 3.2)

This patchset depends on Paul McKenney's "rcu: Introduce raw SRCU read-side primitives" - 0c53dd8b3; and my patches "x86: Clean up and extend do_int3()" - cc3a1bf52; "x86: Call do_notify_resume() with interrupts enabled" - 3596ff4e6b
All three commits in -tip tree.

uprobes git is hosted at git://github.com/srikard/linux.git
with branch inode_uprobes_v32. Branch for-next is also updated with these changes.

Uprobes Patches
This patchset implements inode based uprobes which are specified as <file>:<offset> where offset is the offset from start of the map.

When a uprobe is registered, Uprobes makes a copy of the probed instruction, replaces the first byte(s) of the probed instruction with a breakpoint instruction. (Uprobes uses background page replacement mechanism and ensures that the breakpoint affects only that process.)

When a CPU hits the breakpoint instruction, Uprobes gets notified of trap and finds the associated uprobe. It then executes the associated handler. Uprobes single-steps its copy of the probed instruction and resumes execution of the probed process at the instruction following the probepoint. Instruction copies to be single-stepped are stored in a per-mm "execution out of line (XOL) area". Currently XOL area is allocated as one page vma.

For previous postings: please refer: https://lkml.org/lkml/2011/11/18/149
https://lkml.org/lkml/2011/11/10/408 https://lkml.org/lkml/2011/9/20/123
https://lkml.org/lkml/2011/6/7/232 https://lkml.org/lkml/2011/4/1/176
http://lkml.org/lkml/2011/3/14/171/ http://lkml.org/lkml/2010/12/16/65
http://lkml.org/lkml/2010/8/25/165 http://lkml.org/lkml/2010/7/27/121
http://lkml.org/lkml/2010/7/12/67 http://lkml.org/lkml/2010/7/8/239
http://lkml.org/lkml/2010/6/29/299 http://lkml.org/lkml/2010/6/14/41
http://lkml.org/lkml/2010/3/20/107 and http://lkml.org/lkml/2010/5/18/307

This patchset is a rework based on suggestions from discussions on lkml in September, March and January 2010 (http://lkml.org/lkml/2010/1/11/92,
http://lkml.org/lkml/2010/1/27/19, http://lkml.org/lkml/2010/3/20/107
and http://lkml.org/lkml/2010/3/31/199 ). This implementation of uprobes doesnt depend on utrace.

Advantages of uprobes over conventional debugging include:

1. Non-disruptive.
Unlike current ptrace based mechanisms, uprobes tracing wouldnt involve signals, stopping threads and context switching between the tracer and tracee.

2. Much better handling of multithreaded programs because of XOL. Current ptrace based mechanisms use single stepping inline, i.e they copy back the original instruction on hitting a breakpoint. In such mechanisms tracers have to stop all the threads on a breakpoint hit or tracers will not be able to handle all hits to the location of interest. Uprobes uses execution out of line, where the instruction to be traced is analysed at the time of breakpoint insertion and a copy of instruction is stored at a different location. On breakpoint hit, uprobes jumps to that copied location and singlesteps the same instruction and does the necessary fixups post singlestepping.

3. Multiple tracers for an application.
Multiple uprobes based tracer could work in unison to trace an application. There could one tracer that could be interested in generic events for a particular set of process. While there could be another tracer that is just interested in one specific event of a particular process thats part of the previous set of process.

4. Corelating events from kernels and userspace.
Uprobes could be used with other tools like kprobes, tracepoints or as part of higher level tools like perf to give a consolidated set of events from kernel and userspace. In future we could look at a single backtrace showing application, library and kernel calls.

Changes from last patchset:
- Rebased to 805a6af8dba Linux 3.2
- Handled comments from Masami on 'perf probe'
- Verify vma returned from find_vma as suggested by Oleg.

Here is the list of TODO Items.

- worker thread if unregister_uprobe were to fail.
- Prefiltering (i.e filtering at the time of probe insertion)
- Return probes.
- Support for other architectures.
- Uprobes booster.
- replace macro W with bits in inat table.

Please refer "[PATCH 3.2 7/9] tracing: uprobes trace_event interface".

Please refer "[PATCH 3.2 9/9] perf: perf interface for uprobes".

Please do provide your valuable comments.

Thanks in advance.
Srikar

Srikar Dronamraju (9)
0: Uprobes patchset with perf probe support
1: uprobes: Install and remove breakpoints.
2: uprobes: handle breakpoint and signal step exception. 3: uprobes: slot allocation.
4: uprobes: counter to optimize probe hits.
5: tracing: modify is_delete, is_return from ints to bool. 6: tracing: Extract out common code for kprobes/uprobes traceevents. 7: tracing: uprobes trace_event interface
8: perf: rename target_module to target
9: perf: perf interface for uprobes

 Documentation/trace/uprobetracer.txt    |   93 ++
 arch/Kconfig                            |    3 +
 arch/x86/Kconfig                        |    5 +-
 arch/x86/include/asm/thread_info.h      |    2 +
 arch/x86/include/asm/uprobes.h          |   59 ++
 arch/x86/kernel/Makefile                |    1 +
 arch/x86/kernel/signal.c                |    6 +
 arch/x86/kernel/uprobes.c               |  675 ++++++++++++++
 include/linux/mm_types.h                |    5 +
 include/linux/sched.h                   |    4 +
 include/linux/uprobes.h                 |  171 ++++
 kernel/Makefile                         |    1 +
 kernel/fork.c                           |   15 +
 kernel/signal.c                         |    3 +
 kernel/trace/Kconfig                    |   20 +
 kernel/trace/Makefile                   |    2 +
 kernel/trace/trace.h                    |    5 +
 kernel/trace/trace_kprobe.c             |  899 +-----------------
 kernel/trace/trace_probe.c              |  786 ++++++++++++++++
 kernel/trace/trace_probe.h              |  162 ++++
 kernel/trace/trace_uprobe.c             |  768 +++++++++++++++
 kernel/uprobes.c                        | 1546 +++++++++++++++++++++++++++++++
 mm/mmap.c                               |   33 +-
 tools/perf/Documentation/perf-probe.txt |   14 +
 tools/perf/builtin-probe.c              |   49 +-
 tools/perf/util/probe-event.c           |  430 +++++++--
 tools/perf/util/probe-event.h           |   12 +-
 tools/perf/util/symbol.c                |    8 +
 tools/perf/util/symbol.h                |    1 +
 29 files changed, 4790 insertions(+), 988 deletions(-)
#1 lore raw

PATCH v9 1/9 uprobes: Install and remove breakpoints.

Uprobes are maintained in an rb-tree indexed by inode and offset (the offset here is from the start of the mapping). For a unique (inode, offset) tuple, there can be atmost one uprobe in the rb-tree.

Since the (inode, offset) tuple identifies a unique uprobe, more than one user may be interested in the same uprobe. Provides the ability to hook multiple 'consumers' for the same uprobe.

Each consumer defines a handler and a filter (optional). The 'handler' is run every time the uprobe is hit, if it matches the 'filter' criteria.

The first consumer of a uprobe causes the breakpoint to be inserted at the specified address and subsequent consumers are appended to this list. On subsequent probes, the consumer gets appended to the existing list of consumers. The breakpoint is removed when the last consumer unregisters. For all other unregisterations, the consumer is removed from the list of consumers.

Given a inode, we get a list of the mms that have mapped the inode. Do the actual registration if mm maps the page where a probe needs to be inserted/removed.

We use a temporary list to walk thro the vmas that map the inode.
- The number of maps that map the inode, is not known before we walk the rmap and keeps changing.
- extending vm_area_struct wasnt recommended.
- There can be more than one maps of the inode in the same mm.

Hook mmap to keep an eye on text vmas that are of interest to uprobes. When a vma of interest is mapped, insert the breakpoint at the right address.

Uprobe works by replacing the instruction at the address defined by (inode, offset) with the arch specific breakpoint instruction. Save a copy of the original instruction at the uprobed address.

This is needed for:
a. executing the instruction out-of-line (xol).
b. instruction analysis for any subsequent fixups.
c. restoring the instruction back when the uprobe is unregistered.

We insert or delete a breakpoint instruction, and this breakpoint instruction is assumed to be the smallest instruction available on the platform. For fixed size instruction platforms this is trivially true, for variable size instruction platforms the breakpoint instruction is typically the smallest (often a single byte).

Writing the instruction is done by COWing the page and changing the instruction during the copy, this even though most platforms allow atomic writes of the breakpoint instruction. This also mirrors the behaviour of a ptrace() memory write to a PRIVATE file map.

The core worker is derived from ksm's replace_page() logic.

In essence, similar to ksm:
a. allocate a new page and copy over contents of the page that has the
uprobed vaddr
b. modify the copy and insert the breakpoint at the required address c. switch the original page with the copy containing the breakpoint d. flush page tables.

replace_page is being replicated here because of some minor changes in the type of pages and also because Hugh Dickins had plans to improve replace_page for ksm specific work.

Instruction analysis on x86 is based on instruction decoder and determines if an instruction can be probed and determines the necessary fixups after singlestep. Instruction analysis is done at probe insertion time so that we avoid having to repeat the same analysis every time a probe is hit.

A lot of code here is due to the improvement/suggestions/inputs from Peter Zijlstra.

---

Changelog:(Since v5)
- Modified del_consumer as per comments from Peter.
- Drop reference to inode before dropping reference to uprobe.
- Use i_size_read(inode) instead of inode->i_size.
- Ensure uprobe->consumers is NULL, before __unregister_uprobe() is called.
- Includes errno.h as recommended by Stephen Rothwell to fix a build issue on sparc defconfig
- Remove restrictions while unregistering.
- Earlier code leaked inode references under some conditions while registering/unregistering.
- Continue the vma-rmap walk even if the intermediate vma doesnt meet the requirements.
- Validate the vma found by find_vma before inserting/removing the breakpoint
- Call del_consumer under mutex_lock.
- Use hash locks.
- Handle mremap.
- Introduce find_least_offset_node() instead of close match logic in find_uprobe
- Uprobes no more depends on MM_OWNER; No reference to task_structs while inserting/removing a probe.
- Uses read_mapping_page instead of grab_cache_page so that the pages have valid content.
- pass NULL to get_user_pages for the task parameter.
- call SetPageUptodate on the new page allocated in write_opcode.
- fix leaking a reference to the new page under certain conditions.
- Include Instruction Decoder if Uprobes gets defined.
- Remove const attributes for instruction prefix arrays.
- Uses mm_context to know if the application is 32 bit.

Changelog: (Since v7) : Handle comments from Peter Zijlstra
- Dont take reference to inode. (expect inode to register_uprobe to be sane).
- Use PTR_ERR to set the return value.
- No need to take reference to inode.
- use PTR_ERR to return error value.
- register and unregister_uprobe share code.

 arch/Kconfig                   |   11 
 arch/x86/Kconfig               |    5 
 arch/x86/include/asm/uprobes.h |   42 ++
 arch/x86/kernel/Makefile       |    1 
 arch/x86/kernel/uprobes.c      |  403 +++++++++++++++++
 include/linux/uprobes.h        |   98 ++++
 kernel/Makefile                |    1 
 kernel/uprobes.c               |  976 ++++++++++++++++++++++++++++++++++++++++
 mm/mmap.c                      |   23 +
 9 files changed, 1559 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/include/asm/uprobes.h
 create mode 100644 arch/x86/kernel/uprobes.c
 create mode 100644 include/linux/uprobes.h
 create mode 100644 kernel/uprobes.c
arch/Kconfig +11 -0
index 4b0669c..6328b49 100644
@@ -61,6 +61,17 @@ config OPTPROBES
 	depends on KPROBES && HAVE_OPTPROBES
 	depends on !PREEMPT
 
+config UPROBES
+	bool "User-space probes (EXPERIMENTAL)"
+	depends on ARCH_SUPPORTS_UPROBES
+	default n
+	help
+	  Uprobes enables kernel subsystems to establish probepoints
+	  in user applications and execute handler functions when
+	  the probepoints are hit.
+
+	  If in doubt, say "N".
+
 config HAVE_EFFICIENT_UNALIGNED_ACCESS
 	bool
 	help
arch/x86/Kconfig +4 -1
index efb4294..9c821ee 100644
@@ -77,7 +77,7 @@ config X86
 	select ARCH_HAVE_NMI_SAFE_CMPXCHG
 
 config INSTRUCTION_DECODER
-	def_bool (KPROBES || PERF_EVENTS)
+	def_bool (KPROBES || PERF_EVENTS || UPROBES)
 
 config OUTPUT_FORMAT
 	string
@@ -249,6 +249,9 @@ config ARCH_CPU_PROBE_RELEASE
 	def_bool y
 	depends on HOTPLUG_CPU
 
+config ARCH_SUPPORTS_UPROBES
+	def_bool y
+
 source "init/Kconfig"
 source "kernel/Kconfig.freezer"
 
arch/x86/include/asm/uprobes.h new file +42 -0

42 lines not on this page · raw message

arch/x86/kernel/Makefile +1 -0
index 8baca3c..8f28be8 100644
@@ -98,6 +98,7 @@ obj-$(CONFIG_X86_CHECK_BIOS_CORRUPTION) += check.o
 
 obj-$(CONFIG_SWIOTLB)			+= pci-swiotlb.o
 obj-$(CONFIG_OF)			+= devicetree.o
+obj-$(CONFIG_UPROBES)			+= uprobes.o
 
 ###
 # 64 bit specific files
arch/x86/kernel/uprobes.c new file +403 -0

403 lines not on this page · raw message

include/linux/uprobes.h new file +98 -0

98 lines not on this page · raw message

kernel/Makefile +1 -0
index e898c5b..9fb670d 100644
@@ -109,6 +109,7 @@ obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o
 obj-$(CONFIG_PADATA) += padata.o
 obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
 obj-$(CONFIG_JUMP_LABEL) += jump_label.o
+obj-$(CONFIG_UPROBES) += uprobes.o
 
 ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER),y)
 # According to Alan Modra <[email protected]>, the -fno-omit-frame-pointer is
kernel/uprobes.c new file +976 -0

976 lines not on this page · raw message

mm/mmap.c +23 -0
index eae90af..0966418 100644
@@ -30,6 +30,7 @@
 #include <linux/perf_event.h>
 #include <linux/audit.h>
 #include <linux/khugepaged.h>
+#include <linux/uprobes.h>
 
 #include <asm/uaccess.h>
 #include <asm/cacheflush.h>
@@ -616,6 +617,13 @@ again:			remove_next = 1 + (end > next->vm_end);
 	if (mapping)
 		mutex_unlock(&mapping->i_mmap_mutex);
 
+	if (root) {
+		mmap_uprobe(vma);
+
+		if (adjust_next)
+			mmap_uprobe(next);
+	}
+
 	if (remove_next) {
 		if (file) {
 			fput(file);
@@ -637,6 +645,8 @@ again:			remove_next = 1 + (end > next->vm_end);
 			goto again;
 		}
 	}
+	if (insert && file)
+		mmap_uprobe(insert);
 
 	validate_mm(mm);
 
@@ -1329,6 +1339,11 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 			mm->locked_vm += (len >> PAGE_SHIFT);
 	} else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
 		make_pages_present(addr, addr + len);
+
+	if (file && mmap_uprobe(vma))
+		/* matching probes but cannot insert */
+		goto unmap_and_free_vma;
+
 	return addr;
 
 unmap_and_free_vma:
@@ -2305,6 +2320,10 @@ int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
 	if ((vma->vm_flags & VM_ACCOUNT) &&
 	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
 		return -ENOMEM;
+
+	if (vma->vm_file && mmap_uprobe(vma))
+		return -EINVAL;
+
 	vma_link(mm, vma, prev, rb_link, rb_parent);
 	return 0;
 }
@@ -2356,6 +2375,10 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
 			new_vma->vm_pgoff = pgoff;
 			if (new_vma->vm_file) {
 				get_file(new_vma->vm_file);
+
+				if (mmap_uprobe(new_vma))
+					goto out_free_mempol;
+
 				if (vma->vm_flags & VM_EXECUTABLE)
 					added_exe_file_vma(mm);
 			}

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw

Re: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.

Quoted 5 lines · Srikar Dronamraju

+/*
+ * opcodes we'll probably never support:
+ * 6c-6d, e4-e5, ec-ed - in
+ * 6e-6f, e6-e7, ee-ef - out
+ * cc, cd - int3, int

I imagine desire to set a breakpoint on int 0x80 will be rather typical. (Same for sysenter).

Quoted 1 line

+ * cf - iret

Iret does work. Test program for 32-bit x86:

/* gcc -nostartfiles -nostdlib -o iret iret.S */
_start: .globl _start
pushf
push %cs
push $_e
iret /* will this jump to _e? Yes! */
hlt /* segv if reached */

_e:     movl    $42, %ebx
        movl    $1, %eax
        int     $0x80

I guess supporting probes in weird stuff like ancient DOS emulators (they actually use iret) is not important.

OTOH iret doesn't seem to be too hard: if it fails (bad cs/eflags on stack), then the location of iret instruction per se is not terribly important.
If it works, then you need to be careful to not mess up eip, same as you already do with ordinary [l]ret, nothing more.

Come to think of it, why do you bother checking for invalid instructions? What can happen if you would just copy and run all instructions? You already are prepared to handle exceptions, right?

Signature · 8 lines
vda

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#3 lore raw

Re: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.

Quoted 11 lines · Denys Vlasenko
Quoted 5 lines · Srikar Dronamraju

+/*
+ * opcodes we'll probably never support:
+ * 6c-6d, e4-e5, ec-ed - in
+ * 6e-6f, e6-e7, ee-ef - out
+ * cc, cd - int3, int

I imagine desire to set a breakpoint on int 0x80 will be rather typical. (Same for sysenter).

May be uprobes will support this later. But imho we should not try to do this now.

With the current code, afaics we do not want to allow the UTASK_SSTEP/TIF_SINGLESTEP task to enter the kernel mode, this state is "too special". Just for example, suppose it clones another task and the child gets the invalid uprobe state.

Oleg.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

Re: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.

In reply to Srikar Dronamraju
Quoted 68 lines · Srikar Dronamraju
arch/x86/kernel/uprobes.c new file +68 -0
+/*
+ * If uprobe->insn doesn't use rip-relative addressing, return
+ * immediately.  Otherwise, rewrite the instruction so that it accesses
+ * its memory operand indirectly through a scratch register.  Set
+ * uprobe->arch_info.fixups and uprobe->arch_info.rip_rela_target_address
+ * accordingly.  (The contents of the scratch register will be saved
+ * before we single-step the modified instruction, and restored
+ * afterward.)
+ *
+ * We do this because a rip-relative instruction can access only a
+ * relatively small area (+/- 2 GB from the instruction), and the XOL
+ * area typically lies beyond that area.  At least for instructions
+ * that store to memory, we can't execute the original instruction
+ * and "fix things up" later, because the misdirected store could be
+ * disastrous.
+ *
+ * Some useful facts about rip-relative instructions:
+ * - There's always a modrm byte.
+ * - There's never a SIB byte.
+ * - The displacement is always 4 bytes.
+ */
+static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe,
+                                                       struct insn *insn)
+{
+       u8 *cursor;
+       u8 reg;
+
+       if (mm->context.ia32_compat)
+               return;
+
+       uprobe->arch_info.rip_rela_target_address = 0x0;
+       if (!insn_rip_relative(insn))
+               return;
+
+       /*
+        * Point cursor at the modrm byte.  The next 4 bytes are the
+        * displacement.  Beyond the displacement, for some instructions,
+        * is the immediate operand.
+        */
+       cursor = uprobe->insn + insn->prefixes.nbytes
+                       + insn->rex_prefix.nbytes + insn->opcode.nbytes;
+       insn_get_length(insn);
+
+       /*
+        * Convert from rip-relative addressing to indirect addressing
+        * via a scratch register.  Change the r/m field from 0x5 (%rip)
+        * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
+        */
+       reg = MODRM_REG(insn);
+       if (reg == 0) {
+               /*
+                * The register operand (if any) is either the A register
+                * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
+                * REX prefix) %r8.  In any case, we know the C register
+                * is NOT the register operand, so we use %rcx (register
+                * #1) for the scratch register.
+                */
+               uprobe->arch_info.fixups = UPROBES_FIX_RIP_CX;
+               /* Change modrm from 00 000 101 to 00 000 001. */
+               *cursor = 0x1;
+       } else {
+               /* Use %rax (register #0) for the scratch register. */
+               uprobe->arch_info.fixups = UPROBES_FIX_RIP_AX;
+               /* Change modrm from 00 xxx 101 to 00 xxx 000 */
+               *cursor = (reg << 3);
+       }
+
+       /* Target address = address of next instruction + (signed) offset */

arch/x86/kernel/uprobes.c line 345

Quoted 10 lines
arch/x86/kernel/uprobes.c new file +10 -0
+       uprobe->arch_info.rip_rela_target_address = (long)insn->length
+                                       + insn->displacement.value;
+       /* Displacement field is gone; slide immediate field (if any) over. */
+       if (insn->immediate.nbytes) {
+               cursor++;
+               memmove(cursor, cursor + insn->displacement.nbytes,
+                                               insn->immediate.nbytes);
+       }
+       return;
+}

It seems to be possible to store RIP value *without displacement* into AX/CX and convert rip-relative instruction into AX/CX *relative* one. Example:
c7 05 78 56 34 12 2a 00 00 00 movl $0x2a,0x12345678(%rip)
converts to:
c7 81 78 56 34 12 2a 00 00 00 movl $0x2a,0x12345678(%rcx)

This way instruction size stays the same and you don't need to memmove immediate value.

Signature · 8 lines
vda

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw

Re: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.

Quoted 9 lines · Denys Vlasenko
Quoted 9 lines

+       /*
+        * Convert from rip-relative addressing to indirect addressing
+        * via a scratch register.  Change the r/m field from 0x5 (%rip)
+        * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
+        */
+       reg = MODRM_REG(insn);
+       if (reg == 0) {
+               /*
+                * The register operand (if any) is either the A register

Quoted 8 lines
Quoted 8 lines

+                * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
+                * REX prefix) %r8.  In any case, we know the C register
+                * is NOT the register operand, so we use %rcx (register
+                * #1) for the scratch register.
+                */
+               uprobe->arch_info.fixups = UPROBES_FIX_RIP_CX;
+               /* Change modrm from 00 000 101 to 00 000 001. */
+               *cursor = 0x1;

Hmm. I think we have a bug here.

What if this instruction has REX.B = 1? Granted, REX.B = 1 has no effect on rip-relative addressing and therefore normally won't be generated by gcc/as, but still. If you replace md and r/m fields as above, you are trying to convert 0x12345678(%rip) reference to (%rcx), but if REX.B = 1, then you in fact converted it to (%r9)!

Signature · 8 lines
vda

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw

Re: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.

Quoted 20 lines · Denys Vlasenko
Quoted 9 lines · Denys Vlasenko
Quoted 9 lines
arch/x86/kernel/uprobes.c new file +9 -0
+       /*
+        * Convert from rip-relative addressing to indirect addressing
+        * via a scratch register.  Change the r/m field from 0x5 (%rip)
+        * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
+        */
+       reg = MODRM_REG(insn);
+       if (reg == 0) {
+               /*
+                * The register operand (if any) is either the A register
Quoted 8 lines

arch/x86/kernel/uprobes.c line 327

Quoted 8 lines
arch/x86/kernel/uprobes.c new file +8 -0
+                * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
+                * REX prefix) %r8.  In any case, we know the C register
+                * is NOT the register operand, so we use %rcx (register
+                * #1) for the scratch register.
+                */
+               uprobe->arch_info.fixups = UPROBES_FIX_RIP_CX;
+               /* Change modrm from 00 000 101 to 00 000 001. */
+               *cursor = 0x1;
Quoted 7 lines

Hmm. I think we have a bug here.

What if this instruction has REX.B = 1? Granted, REX.B = 1 has no effect on rip-relative addressing and therefore normally won't be generated by gcc/as, but still. If you replace md and r/m fields as above, you are trying to convert 0x12345678(%rip) reference to (%rcx), but if REX.B = 1, then you in fact converted it to (%r9)!

Right, thanks for finding :)
And %rax register reference encoding has same problem, doesn't it?

Thank you,

Signature · 11 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw

Re: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.

Quoted 23 lines · Masami Hiramatsu
Quoted 22 lines · Denys Vlasenko
Quoted 9 lines · Denys Vlasenko
Quoted 9 lines

+       /*
+        * Convert from rip-relative addressing to indirect addressing
+        * via a scratch register.  Change the r/m field from 0x5 (%rip)
+        * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
+        */
+       reg = MODRM_REG(insn);
+       if (reg == 0) {
+               /*
+                * The register operand (if any) is either the A register

Quoted 8 lines
Quoted 8 lines

+                * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
+                * REX prefix) %r8.  In any case, we know the C register
+                * is NOT the register operand, so we use %rcx (register
+                * #1) for the scratch register.
+                */
+               uprobe->arch_info.fixups = UPROBES_FIX_RIP_CX;
+               /* Change modrm from 00 000 101 to 00 000 001. */
+               *cursor = 0x1;

Hmm. I think we have a bug here.

Quoted 8 lines
Quoted 5 lines

What if this instruction has REX.B = 1? Granted, REX.B = 1 has no effect on rip-relative addressing and therefore normally won't be generated by gcc/as, but still. If you replace md and r/m fields as above, you are trying to convert 0x12345678(%rip) reference to (%rcx), but if REX.B = 1, then you in fact converted it to (%r9)!

Right, thanks for finding :)
And %rax register reference encoding has same problem, doesn't it?

Yes.

The solution is trivial: "if (REX pfx exists) REX.B = 0;"

Also, I don't remember whether (%rip) addressing is affected by 0x67 (address size) prefix. If it is, then nothing needs to be done.

But if there is an exception and CPU ignores 0x67 pfx for (%rip) addressing, we will need to check for and remove this pfx.

Otherwise, it'll instruct to use 32-bit registers for addressing, thus it will turn our (%rax) operand into (%eax). Not good.

Signature · 8 lines
vda

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#8 lore raw

Re: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.

Quoted 34 lines · Denys Vlasenko
Quoted 23 lines · Masami Hiramatsu
Quoted 22 lines · Denys Vlasenko
Quoted 9 lines · Denys Vlasenko
Quoted 9 lines
arch/x86/kernel/uprobes.c new file +9 -0
+       /*
+        * Convert from rip-relative addressing to indirect addressing
+        * via a scratch register.  Change the r/m field from 0x5 (%rip)
+        * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
+        */
+       reg = MODRM_REG(insn);
+       if (reg == 0) {
+               /*
+                * The register operand (if any) is either the A register
Quoted 8 lines

arch/x86/kernel/uprobes.c line 327

Quoted 8 lines
arch/x86/kernel/uprobes.c new file +8 -0
+                * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
+                * REX prefix) %r8.  In any case, we know the C register
+                * is NOT the register operand, so we use %rcx (register
+                * #1) for the scratch register.
+                */
+               uprobe->arch_info.fixups = UPROBES_FIX_RIP_CX;
+               /* Change modrm from 00 000 101 to 00 000 001. */
+               *cursor = 0x1;

Hmm. I think we have a bug here.

Quoted 8 lines
Quoted 5 lines

What if this instruction has REX.B = 1? Granted, REX.B = 1 has no effect on rip-relative addressing and therefore normally won't be generated by gcc/as, but still. If you replace md and r/m fields as above, you are trying to convert 0x12345678(%rip) reference to (%rcx), but if REX.B = 1, then you in fact converted it to (%r9)!

Right, thanks for finding :)
And %rax register reference encoding has same problem, doesn't it?

Quoted 7 lines

Yes.

The solution is trivial: "if (REX pfx exists) REX.B = 0;"

Also, I don't remember whether (%rip) addressing is affected by 0x67 (address size) prefix. If it is, then nothing needs to be done.

In Intel SDM vol.2, "2.2.1.6 RIP-Relative Addressing" explains that as below;

---
RIP-relative addressing is enabled by 64-bit mode, not by a 64-bit address-size. The use of the address-size prefix does not disable RIP-relative addressing. The effect of the address-size prefix is to truncate and zero-extend the computed effective address to 32 bits.
---

This means 0x67 pfx can be used with RIP-relative, and effective address(EA) will be 32bit, as same as others. So, I think it should work :)

Thank you!

Signature · 11 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#9 lore raw

Re: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.

In reply to Denys Vlasenko
Quoted 88 lines · Denys Vlasenko
Quoted 68 lines · Srikar Dronamraju
arch/x86/kernel/uprobes.c new file +68 -0
+/*
+ * If uprobe->insn doesn't use rip-relative addressing, return
+ * immediately.  Otherwise, rewrite the instruction so that it accesses
+ * its memory operand indirectly through a scratch register.  Set
+ * uprobe->arch_info.fixups and uprobe->arch_info.rip_rela_target_address
+ * accordingly.  (The contents of the scratch register will be saved
+ * before we single-step the modified instruction, and restored
+ * afterward.)
+ *
+ * We do this because a rip-relative instruction can access only a
+ * relatively small area (+/- 2 GB from the instruction), and the XOL
+ * area typically lies beyond that area.  At least for instructions
+ * that store to memory, we can't execute the original instruction
+ * and "fix things up" later, because the misdirected store could be
+ * disastrous.
+ *
+ * Some useful facts about rip-relative instructions:
+ * - There's always a modrm byte.
+ * - There's never a SIB byte.
+ * - The displacement is always 4 bytes.
+ */
+static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe,
+                                                       struct insn *insn)
+{
+       u8 *cursor;
+       u8 reg;
+
+       if (mm->context.ia32_compat)
+               return;
+
+       uprobe->arch_info.rip_rela_target_address = 0x0;
+       if (!insn_rip_relative(insn))
+               return;
+
+       /*
+        * Point cursor at the modrm byte.  The next 4 bytes are the
+        * displacement.  Beyond the displacement, for some instructions,
+        * is the immediate operand.
+        */
+       cursor = uprobe->insn + insn->prefixes.nbytes
+                       + insn->rex_prefix.nbytes + insn->opcode.nbytes;
+       insn_get_length(insn);
+
+       /*
+        * Convert from rip-relative addressing to indirect addressing
+        * via a scratch register.  Change the r/m field from 0x5 (%rip)
+        * to 0x0 (%rax) or 0x1 (%rcx), and squeeze out the offset field.
+        */
+       reg = MODRM_REG(insn);
+       if (reg == 0) {
+               /*
+                * The register operand (if any) is either the A register
+                * (%rax, %eax, etc.) or (if the 0x4 bit is set in the
+                * REX prefix) %r8.  In any case, we know the C register
+                * is NOT the register operand, so we use %rcx (register
+                * #1) for the scratch register.
+                */
+               uprobe->arch_info.fixups = UPROBES_FIX_RIP_CX;
+               /* Change modrm from 00 000 101 to 00 000 001. */
+               *cursor = 0x1;
+       } else {
+               /* Use %rax (register #0) for the scratch register. */
+               uprobe->arch_info.fixups = UPROBES_FIX_RIP_AX;
+               /* Change modrm from 00 xxx 101 to 00 xxx 000 */
+               *cursor = (reg << 3);
+       }
+
+       /* Target address = address of next instruction + (signed) offset */

arch/x86/kernel/uprobes.c line 345

Quoted 10 lines
arch/x86/kernel/uprobes.c new file +10 -0
+       uprobe->arch_info.rip_rela_target_address = (long)insn->length
+                                       + insn->displacement.value;
+       /* Displacement field is gone; slide immediate field (if any) over. */
+       if (insn->immediate.nbytes) {
+               cursor++;
+               memmove(cursor, cursor + insn->displacement.nbytes,
+                                               insn->immediate.nbytes);
+       }
+       return;
+}

It seems to be possible to store RIP value *without displacement* into AX/CX and convert rip-relative instruction into AX/CX *relative* one. Example:
c7 05 78 56 34 12 2a 00 00 00 movl $0x2a,0x12345678(%rip)
converts to:
c7 81 78 56 34 12 2a 00 00 00 movl $0x2a,0x12345678(%rcx)

Quoted 2 lines

This way instruction size stays the same and you don't need to memmove immediate value.

Right, I agree there is a possibility of optimizing. However, for ease of review, I think it's better to be a separate patch.

Thank you,

Signature · 11 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#10 lore raw

Re: [PATCH v9 3.2 1/9] uprobes: Install and remove breakpoints.

In reply to Srikar Dronamraju
Quoted 10 lines · Srikar Dronamraju
arch/x86/kernel/uprobes.c new file +10 -0
+static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe,
+							struct insn *insn)
+{
+	u8 *cursor;
+	u8 reg;
+
+	if (mm->context.ia32_compat)
+		return;
+
+	uprobe->arch_info.rip_rela_target_address = 0x0;

arch/x86/kernel/uprobes.c line 308

Quoted 10 lines
arch/x86/kernel/uprobes.c new file +10 -0
+	if (!insn_rip_relative(insn))
+		return;
+
+	/*
+	 * Point cursor at the modrm byte.  The next 4 bytes are the
+	 * displacement.  Beyond the displacement, for some instructions,
+	 * is the immediate operand.
+	 */
+	cursor = uprobe->insn + insn->prefixes.nbytes
+			+ insn->rex_prefix.nbytes + insn->opcode.nbytes;

FYI, insn.h already provide a macro for this purpose. You can write this as below;

	cursor = uprobe->insn + insn_offset_modrm(insn);

Thank you,

Signature · 11 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#11 lore raw

PATCH v9 2/9 uprobes: handle breakpoint and signal step exception.

In reply to Srikar Dronamraju

Uprobes uses exception notifiers to get to know if a thread hit a breakpoint or singlestep exception.

When a thread hits a uprobe or is singlestepping post a uprobe hit, the uprobe exception notifier, sets its TIF_UPROBE bit, which will then be checked on its return to userspace path (do_notify_resume() ->uprobe_notify_resume()), where the consumers handlers are run (in task context) based on the defined filters.

Uprobe hits are thread specific and hence we need to maintain information about if a task hit a uprobe, what uprobe was hit, the slot where the original instruction was copied for xol so that it can be singlestepped with appropriate fixups.

In some cases, special care is needed for instructions that are executed out of line (xol). These are architecture specific artefacts, such as handling RIP relative instructions on x86_64.

Since the instruction at which the uprobe was inserted is executed out of line, architecture specific fixups are added so that the thread continues normal execution in the presence of a uprobe.

Postpone the signals until we execute the probed insn. post_xol() path does a recalc_sigpending() before return to user-mode, this ensures the signal can't be lost.

Uprobes relies on DIE_DEBUG notification to notify if a singlestep is complete.

Adds x86 specific uprobe exception notifiers and appropriate hooks needed to determine a uprobe hit and subsequent post processing.

Add requisite x86 fixups for xol for uprobes. Specific cases needing fixups include relative jumps (x86_64), calls, etc.

Where possible, we check and skip singlestepping the breakpointed instructions. For now we skip single byte as well as few multibyte nop instructions. However this can be extended to other instructions too.

Credits to Oleg Nesterov for suggestions/patches related to signal, breakpoint, singlestep handling code.

---
(Changelog (since v8))
- verify the vma returned by find_vma (suggested by Oleg).

(Changelog (since v7)): Resolve comments from Dan Carpenter.
- add_utask returns NULL on error.
- Handles architecture agnostic parts of a uprobe breakpoint hit and subsequent xol singlestepping.

Changelog (since v6)
- added x86 specific hook for aborting xol.

Changelog (since v5)
- Use srcu_raw instead of synchronize_sched
- Introduce per task srcu_id to store the srcu_id
- Modified comments.
- No more do a i386 specific enable interrupts. (Its not part of another patch posted separately)

 arch/x86/include/asm/thread_info.h |    2 
 arch/x86/include/asm/uprobes.h     |   17 ++
 arch/x86/kernel/signal.c           |    6 +
 arch/x86/kernel/uprobes.c          |  272 ++++++++++++++++++++++++++++++++++++
 include/linux/sched.h              |    4 +
 include/linux/uprobes.h            |   46 ++++++
 kernel/fork.c                      |    7 +
 kernel/signal.c                    |    3 
 kernel/uprobes.c                   |  275 ++++++++++++++++++++++++++++++++++++
 9 files changed, 630 insertions(+), 2 deletions(-)
arch/x86/include/asm/thread_info.h +2 -0
index a1fe5c1..aeb3e04 100644
@@ -84,6 +84,7 @@ struct thread_info {
 #define TIF_SECCOMP		8	/* secure computing */
 #define TIF_MCE_NOTIFY		10	/* notify userspace of an MCE */
 #define TIF_USER_RETURN_NOTIFY	11	/* notify kernel of userspace return */
+#define TIF_UPROBE		12	/* breakpointed or singlestepping */
 #define TIF_NOTSC		16	/* TSC is not accessible in userland */
 #define TIF_IA32		17	/* 32bit process */
 #define TIF_FORK		18	/* ret_from_fork */
@@ -107,6 +108,7 @@ struct thread_info {
 #define _TIF_SECCOMP		(1 << TIF_SECCOMP)
 #define _TIF_MCE_NOTIFY		(1 << TIF_MCE_NOTIFY)
 #define _TIF_USER_RETURN_NOTIFY	(1 << TIF_USER_RETURN_NOTIFY)
+#define _TIF_UPROBE		(1 << TIF_UPROBE)
 #define _TIF_NOTSC		(1 << TIF_NOTSC)
 #define _TIF_IA32		(1 << TIF_IA32)
 #define _TIF_FORK		(1 << TIF_FORK)
arch/x86/include/asm/uprobes.h +17 -0
index 8208234..475563b 100644
@@ -23,6 +23,8 @@
  *	Jim Keniston
  */
 
+#include <linux/notifier.h>
+
 typedef u8 uprobe_opcode_t;
 #define MAX_UINSN_BYTES 16
 #define UPROBES_XOL_SLOT_BYTES	128	/* to keep it cache aligned */
@@ -37,6 +39,21 @@ struct uprobe_arch_info {
 #endif
 };
 
+struct uprobe_task_arch_info {
+	unsigned long saved_trap_no;
+#ifdef CONFIG_X86_64
+	unsigned long saved_scratch_register;
+#endif
+};
+
 struct uprobe;
+
 extern int analyze_insn(struct mm_struct *mm, struct uprobe *uprobe);
+extern void set_instruction_pointer(struct pt_regs *regs, unsigned long vaddr);
+extern int pre_xol(struct uprobe *uprobe, struct pt_regs *regs);
+extern int post_xol(struct uprobe *uprobe, struct pt_regs *regs);
+extern bool xol_was_trapped(struct task_struct *tsk);
+extern int uprobe_exception_notify(struct notifier_block *self,
+				       unsigned long val, void *data);
+extern void abort_xol(struct pt_regs *regs, struct uprobe *uprobe);
 #endif	/* _ASM_UPROBES_H */
arch/x86/kernel/signal.c +6 -0
index 54ddaeb..4fdf470 100644
@@ -20,6 +20,7 @@
 #include <linux/personality.h>
 #include <linux/uaccess.h>
 #include <linux/user-return-notifier.h>
+#include <linux/uprobes.h>
 
 #include <asm/processor.h>
 #include <asm/ucontext.h>
@@ -820,6 +821,11 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
 		mce_notify_process();
 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
 
+	if (thread_info_flags & _TIF_UPROBE) {
+		clear_thread_flag(TIF_UPROBE);
+		uprobe_notify_resume(regs);
+	}
+
 	/* deal with pending signal delivery */
 	if (thread_info_flags & _TIF_SIGPENDING)
 		do_signal(regs);
arch/x86/kernel/uprobes.c +272 -0

285 lines not on this page · raw message

include/linux/sched.h +4 -0
index 1c4f3e9..bab3da7 100644
@@ -1572,6 +1572,10 @@ struct task_struct {
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
 	atomic_t ptrace_bp_refcnt;
 #endif
+#ifdef CONFIG_UPROBES
+	struct uprobe_task *utask;
+	int uprobes_srcu_id;
+#endif
 };
 
 /* Future-safe accessor for struct task_struct's cpus_allowed. */
include/linux/uprobes.h +45 -1

76 lines not on this page · raw message

kernel/fork.c +7 -0
index da4a6a1..7a0b7d7 100644
@@ -66,6 +66,7 @@
 #include <linux/user-return-notifier.h>
 #include <linux/oom.h>
 #include <linux/khugepaged.h>
+#include <linux/uprobes.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -677,6 +678,8 @@ void mm_release(struct task_struct *tsk, struct mm_struct *mm)
 		exit_pi_state_list(tsk);
 #endif
 
+	free_uprobe_utask(tsk);
+
 	/* Get rid of any cached register state */
 	deactivate_mm(tsk, mm);
 
@@ -1272,6 +1275,10 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	INIT_LIST_HEAD(&p->pi_state_list);
 	p->pi_state_cache = NULL;
 #endif
+#ifdef CONFIG_UPROBES
+	p->utask = NULL;
+	p->uprobes_srcu_id = -1;
+#endif
 	/*
 	 * sigaltstack should be cleared when sharing the same VM
 	 */
kernel/signal.c +3 -0
index 2065515..a9d8a50 100644
@@ -2147,6 +2147,9 @@ int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
 	struct signal_struct *signal = current->signal;
 	int signr;
 
+	if (unlikely(uprobe_deny_signal()))
+		return 0;
+
 relock:
 	/*
 	 * We'll jump back here after any time we were stopped in TASK_STOPPED.
kernel/uprobes.c +274 -1

317 lines not on this page · raw message

#12 lore raw

Re: [PATCH v9 3.2 2/9] uprobes: handle breakpoint and signal step exception.

On Tue, Jan 10, 2012 at 05:18:42PM +0530, Srikar Dronamraju wrote:
[snip]

Quoted 4 lines
arch/x86/include/asm/uprobes.h +0 -0
index 8208234..475563b 100644

[snip]

Quoted 5 lines
arch/x86/include/asm/uprobes.h +1 -0
@@ -37,6 +39,21 @@ struct uprobe_arch_info {
 #endif
 };
 
+struct uprobe_task_arch_info {

arch/x86/include/asm/uprobes.h struct uprobe_arch_info { · line 52

Quoted 10 lines
arch/x86/include/asm/uprobes.h +8 -0
+	unsigned long saved_trap_no;
+#ifdef CONFIG_X86_64
+	unsigned long saved_scratch_register;
+#endif
+};
+
 struct uprobe;
+
 extern int analyze_insn(struct mm_struct *mm, struct uprobe *uprobe);
+extern void set_instruction_pointer(struct pt_regs *regs, unsigned long vaddr);

Srikar,

Can we use existing SET_IP() instead of set_instruction_pointer() ?

[snip]

arch/x86/include/asm/uprobes.h struct uprobe_arch_info { · line 53

Quoted 2 lines
arch/x86/include/asm/uprobes.h +0 -0
 static void __exit exit_uprobes(void)

===
[PATCH] uprobes: cleanup, eliminate set_instruction_pointer(), use existing SET_IP() instead

Use SET_IP() available in include/asm-generic/ptrace.h

---
 arch/x86/include/asm/uprobes.h |    1 -
 arch/x86/kernel/uprobes.c      |   12 +-----------
 kernel/uprobes.c               |    2 +-
 3 files changed, 2 insertions(+), 13 deletions(-)
arch/x86/include/asm/uprobes.h +0 -1
index 475563b..88df7ec 100644
@@ -49,7 +49,6 @@ struct uprobe_task_arch_info {
 struct uprobe;
 
 extern int analyze_insn(struct mm_struct *mm, struct uprobe *uprobe);
-extern void set_instruction_pointer(struct pt_regs *regs, unsigned long vaddr);
 extern int pre_xol(struct uprobe *uprobe, struct pt_regs *regs);
 extern int post_xol(struct uprobe *uprobe, struct pt_regs *regs);
 extern bool xol_was_trapped(struct task_struct *tsk);
arch/x86/kernel/uprobes.c +1 -11
index e4e0dfd..08b633f 100644
@@ -409,16 +409,6 @@ int analyze_insn(struct mm_struct *mm, struct uprobe *uprobe)
 	return 0;
 }
 
-/*
- * @reg: reflects the saved state of the task
- * @vaddr: the virtual address to jump to.
- * Return 0 on success or a -ve number on error.
- */
-void set_instruction_pointer(struct pt_regs *regs, unsigned long vaddr)
-{
-	regs->ip = vaddr;
-}
-
 #define	UPROBE_TRAP_NO		UINT_MAX
 
 /*
@@ -624,7 +614,7 @@ void abort_xol(struct pt_regs *regs, struct uprobe *uprobe)
 
 	current->thread.trap_no = utask->tskinfo.saved_trap_no;
 	handle_riprel_post_xol(uprobe, regs, NULL);
-	set_instruction_pointer(regs, utask->vaddr);
+	SET_IP(regs, utask->vaddr);
 }
 
 /*
kernel/uprobes.c +1 -1
index 0918448..b0db46b 100644
@@ -1479,7 +1479,7 @@ cleanup_ret:
 	}
 	if (u) {
 		if (!(u->flags & UPROBES_SKIP_SSTEP))
-			set_instruction_pointer(regs, probept);
+			SET_IP(regs, probept);
 
 		put_uprobe(u);
 	} else
Signature · 8 lines
1.7.7.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#13 lore raw

Re: [PATCH v9 3.2 2/9] uprobes: handle breakpoint and signal step exception.

Quoted 4 lines

Srikar,

Can we use existing SET_IP() instead of set_instruction_pointer() ?

Oleg had already commented about this in one his uprobes reviews.

The GET_IP/SET_IP available in include/asm-generic/ptrace.h doesnt work on all archs. Atleast it doesnt work on powerpc when I tried it.

Also most archs define instruction_pointer(). So I thought (rather Peter Zijlstra suggested the name set_instruction_pointer()) set_instruction_pointer was a better bet than SET_IP. I

Also I dont see any usage for SET_IP/GET_IP.

May be we should have something like this in
include/asm-generic/ptrace.h

#ifdef instruction_pointer
#define GET_IP(regs) (instruction_pointer(regs))

#define set_instruction_pointer(regs, val) (instruction_pointer(regs) = (val))

#define SET_IP(regs, val) (set_instruction_pointer(regs,val))

#endif

or should we do away with GET_IP/SET_IP esp since there are no many users?

Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#14 lore raw

Re: [PATCH v9 3.2 2/9] uprobes: handle breakpoint and signal step exception.

Quoted 6 lines · On Wednesday 18 January 2012 04:02:32 Srikar Dronamraju wrote:
Quoted 1 line

Can we use existing SET_IP() instead of set_instruction_pointer() ?

Oleg had already commented about this in one his uprobes reviews.

The GET_IP/SET_IP available in include/asm-generic/ptrace.h doesnt work on all archs. Atleast it doesnt work on powerpc when I tried it.

so migrate the arches you need over to it.

Quoted 3 lines

Also most archs define instruction_pointer(). So I thought (rather Peter Zijlstra suggested the name set_instruction_pointer()) set_instruction_pointer was a better bet than SET_IP. I

asm-generic/ptrace.h already has instruction_pointer_set()

Quoted 1 line

Also I dont see any usage for SET_IP/GET_IP.

i think you mean "users" here ? the usage should be fairly obvious. both macros are used by asm-generic/ptrace.h internally, but (currently) rarely defined by arches themselves (by design). the funcs that are based on these GET/SET helpers though do get used in many places.

simply grep arch/*/include/asm/ptrace.h

Quoted 10 lines

May be we should have something like this in
include/asm-generic/ptrace.h

#ifdef instruction_pointer
#define GET_IP(regs) (instruction_pointer(regs))
#define set_instruction_pointer(regs, val) (instruction_pointer(regs) = (val))
#define SET_IP(regs, val) (set_instruction_pointer(regs,val))
#endif

what you propose here won't work on all arches which is the whole point of {G,S}ET_IP in the first place. i proposed a similar idea before and was shot down for exactly that reason. look at ia64 for an obvious example.

Quoted 2 lines

or should we do away with GET_IP/SET_IP esp since there are no many users?

no, the point is to migrate to asm-generic/ptrace.h, not away from it. -mike

#15 lore raw

Re: [PATCH v9 3.2 2/9] uprobes: handle breakpoint and signal step exception.

Quoted 10 lines · On Wednesday 18 January 2012 04:02:32 Srikar Dronamraju wrote:
Quoted 6 lines
Quoted 1 line

Can we use existing SET_IP() instead of set_instruction_pointer() ?

Oleg had already commented about this in one his uprobes reviews.

The GET_IP/SET_IP available in include/asm-generic/ptrace.h doesnt work on all archs. Atleast it doesnt work on powerpc when I tried it.

so migrate the arches you need over to it.

One question that could be asked is why arent we using instruction_pointer instead of GET_IP since instruction_pointer is being defined in 25 places and with references in 120 places.

Quoted 7 lines
Quoted 4 lines

Also most archs define instruction_pointer(). So I thought (rather Peter Zijlstra suggested the name set_instruction_pointer()) set_instruction_pointer was a better bet than SET_IP. I

asm-generic/ptrace.h already has instruction_pointer_set()

Quoted 8 lines
Quoted 1 line

Also I dont see any usage for SET_IP/GET_IP.

i think you mean "users" here ? the usage should be fairly obvious. both macros are used by asm-generic/ptrace.h internally, but (currently) rarely defined by arches themselves (by design). the funcs that are based on these GET/SET helpers though do get used in many places.

simply grep arch/*/include/asm/ptrace.h

here are the stats

$ grep -r -w GET_IP * | wc -l 
5
$ grep -r -w SET_IP * | wc -l 
3
$ grep -r -w instruction_pointer * | wc -l 
120
$ grep -r -w instruction_pointer_set * | wc -l
3

The only place I saw GET_IP was used was to define SET_IP
The only place I saw SET_IP was used was to define instruction_pointer_set.
The only place I saw instruction_pointer_set being used is drivers/misc/kgdbts.c

instruction_pointer was defined in close to 25 places.

Quoted 12 lines
Quoted 11 lines

May be we should have something like this in
include/asm-generic/ptrace.h

#ifdef instruction_pointer
#define GET_IP(regs) (instruction_pointer(regs))
#define set_instruction_pointer(regs, val) (instruction_pointer(regs) = (val))
#define SET_IP(regs, val) (set_instruction_pointer(regs,val))
#endif

Quoted 3 lines

what you propose here won't work on all arches which is the whole point of {G,S}ET_IP in the first place. i proposed a similar idea before and was shot down for exactly that reason. look at ia64 for an obvious example.

Sorry, I didnt quite understand this.
Was it that people objected to instruction_pointer or
Is it that instruction_pointer and GET_IP will work differently on few architectures or
Is it people had an objection to defining instruction_pointer.

So let me rephrase here. Initially we used set_ip. But Peter suggested that the name be changed to set_instruction_pointer so that it goes with instruction_pointer. I also felt that set_instruction_pointer was better. However I am okay with any other name including SET_IP/instruction_pointer_set. I have no issues in moving the set_instruction_pointer to arch/*/ptrace.h files it it helps (including include/asm-generic/ptrace.h).

But I think we should either have GET_IP or instruction_pointer. Similarly either SET_IP/set_instruction_pointer{_set}. Since instruction_pointer is more widely used, I would side by the instruction_pointer.

Quoted 5 lines
Quoted 3 lines

or should we do away with GET_IP/SET_IP esp since there are no many users?

no, the point is to migrate to asm-generic/ptrace.h, not away from it.

I think the rational for having asm-generic/ptrace.h was to have define a way to get the instruction_pointer such that the each archs dont have to define their own definition unless and untill its necessary.

If yes, then why did we choose the names GET_IP/SET_IP instead of instruction_pointer and the like.

Signature · 10 lines
Thanks and Regards
Srikar


--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#16 lore raw

Re: [PATCH v9 3.2 2/9] uprobes: handle breakpoint and signal step exception.

Quoted 5 lines · On Wednesday 18 January 2012 05:47:49 Srikar Dronamraju wrote:
Quoted 5 lines · On Wednesday 18 January 2012 04:02:32 Srikar Dronamraju wrote:
Quoted 4 lines
Quoted 1 line

Can we use existing SET_IP() instead of set_instruction_pointer() ?

Oleg had already commented about this in one his uprobes reviews.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The GET_IP/SET_IP available in include/asm-generic/ptrace.h doesnt work on all archs. Atleast it doesnt work on powerpc when I tried it.

so migrate the arches you need over to it.

One question that could be asked is why arent we using instruction_pointer instead of GET_IP since instruction_pointer is being defined in 25 places and with references in 120 places.

i think you misunderstand the point. {G,S}ET_IP() is the glue between the arch's pt_regs struct and the public facing API. the only people who should be touching those macros are the ptrace core. instruction_pointer() and instruction_pointer_set() are the API that asm/ptrace.h exports to the rest of the tree.
-mike

#17 lore raw

Re: [PATCH v9 3.2 2/9] uprobes: handle breakpoint and signal step exception.

Quoted 15 lines · Mike Frysinger
Quoted 5 lines · On Wednesday 18 January 2012 05:47:49 Srikar Dronamraju wrote:
Quoted 5 lines · On Wednesday 18 January 2012 04:02:32 Srikar Dronamraju wrote:
Quoted 4 lines
Quoted 1 line

  Can we use existing SET_IP() instead of set_instruction_pointer() ?

Oleg had already commented about this in one his uprobes reviews.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The GET_IP/SET_IP available in include/asm-generic/ptrace.h doesnt work on all archs. Atleast it doesnt work on powerpc when I tried it.

so migrate the arches you need over to it.

One question that could be asked is why arent we using instruction_pointer instead of GET_IP since instruction_pointer is being defined in 25 places and with references in 120 places.

Quoted 5 lines

i think you misunderstand the point.  {G,S}ET_IP() is the glue between the arch's pt_regs struct and the public facing API.  the only people who should be touching those macros are the ptrace core.  instruction_pointer() and instruction_pointer_set() are the API that asm/ptrace.h exports to the rest of the tree.

Srikar: does that make sense ? i'm happy to help with improving asm-generic/ptrace.h.
-mike

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#18 lore raw

Re: [PATCH v9 3.2 2/9] uprobes: handle breakpoint and signal step exception.

Quoted 11 lines
Quoted 10 lines
Quoted 4 lines

One question that could be asked is why arent we using instruction_pointer instead of GET_IP since instruction_pointer is being defined in 25 places and with references in 120 places.

i think you misunderstand the point. {G,S}ET_IP() is the glue between the arch's pt_regs struct and the public facing API. the only people who should be touching those macros are the ptrace core. instruction_pointer() and instruction_pointer_set() are the API that asm/ptrace.h exports to the rest of the tree.

Quoted 4 lines

Srikar: does that make sense ? i'm happy to help with improving asm-generic/ptrace.h.
-mike

Yes, I think it makes sense. I have modified the code to use instruction_pointer_set instead of set_instruction_pointer.

Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#19 lore raw

PATCH v9 3/9 uprobes: slot allocation.

In reply to Srikar Dronamraju

Uprobes executes the original instruction at a probed location out of line. For this, we allocate a page (per mm) upon the first uprobe hit, in the process' user address space, divide it into slots that are used to store the actual instructions to be singlestepped.

Care is taken to ensure that the allocation is in an unmapped area as close to the top of the user address space as possible, with appropriate permission settings to keep selinux like frameworks happy.

Upon a uprobe hit, a free slot is acquired, and is released after the singlestep completes.

[ Folded a fix for build issue on powerpc fixed and reported by Stephen Rothwell]

Lots of improvements courtesy suggestions/inputs from Peter and Oleg.

---
Changelog (since v5)
- no more spin lock needed for slot allocation.
- use install_special_mapping to add a vma. (previous approach used init_creds)
- set uprobes_xol_area while holding map_sem exclusively.

 include/linux/mm_types.h |    4 +
 include/linux/uprobes.h  |   25 ++++++
 kernel/fork.c            |    4 +
 kernel/uprobes.c         |  202 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 235 insertions(+), 0 deletions(-)
include/linux/mm_types.h +4 -0
index 5b42f1b..e9eac38 100644
@@ -12,6 +12,7 @@
 #include <linux/completion.h>
 #include <linux/cpumask.h>
 #include <linux/page-debug-flags.h>
+#include <linux/uprobes.h>
 #include <asm/page.h>
 #include <asm/mmu.h>
 
@@ -389,6 +390,9 @@ struct mm_struct {
 #ifdef CONFIG_CPUMASK_OFFSTACK
 	struct cpumask cpumask_allocation;
 #endif
+#ifdef CONFIG_UPROBES
+	struct uprobes_xol_area *uprobes_xol_area;
+#endif
 };
 
 static inline void mm_init_cpumask(struct mm_struct *mm)
include/linux/uprobes.h +25 -0
index 41037e9..9e25c41 100644
@@ -27,6 +27,7 @@
 #include <linux/rbtree.h>
 
 struct vm_area_struct;
+struct mm_struct;
 #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
 #include <asm/uprobes.h>
 #else
@@ -92,6 +93,26 @@ struct uprobe_task {
 	struct uprobe *active_uprobe;
 };
 
+/*
+ * On a breakpoint hit, thread contests for a slot.  It free the
+ * slot after singlestep.  Only definite number of slots are
+ * allocated.
+ */
+
+struct uprobes_xol_area {
+	wait_queue_head_t wq;	/* if all slots are busy */
+	atomic_t slot_count;	/* currently in use slots */
+	unsigned long *bitmap;	/* 0 = free slot */
+	struct page *page;
+
+	/*
+	 * We keep the vma's vm_start rather than a pointer to the vma
+	 * itself.  The probed process or a naughty kernel module could make
+	 * the vma go away, and we must handle that reasonably gracefully.
+	 */
+	unsigned long vaddr;		/* Page(s) of instruction slots */
+};
+
 #ifdef CONFIG_UPROBES
 extern int __weak set_bkpt(struct mm_struct *mm, struct uprobe *uprobe,
 							unsigned long vaddr);
@@ -103,6 +124,7 @@ extern int register_uprobe(struct inode *inode, loff_t offset,
 extern void unregister_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer);
 extern void free_uprobe_utask(struct task_struct *tsk);
+extern void free_uprobes_xol_area(struct mm_struct *mm);
 extern int mmap_uprobe(struct vm_area_struct *vma);
 extern unsigned long __weak get_uprobe_bkpt_addr(struct pt_regs *regs);
 extern int uprobe_post_notifier(struct pt_regs *regs);
@@ -138,5 +160,8 @@ static inline unsigned long get_uprobe_bkpt_addr(struct pt_regs *regs)
 static inline void free_uprobe_utask(struct task_struct *tsk)
 {
 }
+static inline void free_uprobes_xol_area(struct mm_struct *mm)
+{
+}
 #endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/fork.c +4 -0
index 7a0b7d7..2da316e 100644
@@ -550,6 +550,7 @@ void mmput(struct mm_struct *mm)
 	might_sleep();
 
 	if (atomic_dec_and_test(&mm->mm_users)) {
+		free_uprobes_xol_area(mm);
 		exit_aio(mm);
 		ksm_exit(mm);
 		khugepaged_exit(mm); /* must run before exit_mmap */
@@ -736,6 +737,9 @@ struct mm_struct *dup_mm(struct task_struct *tsk)
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	mm->pmd_huge_pte = NULL;
 #endif
+#ifdef CONFIG_UPROBES
+	mm->uprobes_xol_area = NULL;
+#endif
 
 	if (!mm_init(mm, tsk))
 		goto fail_nomem;
kernel/uprobes.c +202 -0

232 lines not on this page · raw message

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#20 lore raw

PATCH v9 4/9 uprobes: counter to optimize probe hits.

In reply to Srikar Dronamraju

Maintain a per-mm counter (number of uprobes that are inserted on this process address space). This counter can be used at probe hit time to determine if we need to do a uprobe lookup in the uprobes rbtree. Everytime a probe gets inserted successfully, the probe count is incremented and everytime a probe gets removed successfully the probe count is removed.

A new hook munmap_uprobe is added to keep the counter to be correct even when a region is unmapped or remapped. This patch expects that once a munmap_uprobe() is called, the vma either go away or a subsequent mmap_uprobe gets called before a removal of a probe from unregister_uprobe in the same address space.

On every executable vma thats cowed at fork, mmap_uprobe is called so that the mm_uprobes_count is in sync.

When a vma of interest is mapped, insert the breakpoint at the right address. Upon munmap, just make sure the data structures are adjusted/cleaned up.

On process creation, make sure the probes count in the child is set correctly.

Special cases that are taken care include:
a. mremap()
b. VM_DONTCOPY vmas on fork()
c. insertion/removal races in the parent during fork().

---
Changelog (since v5)
- While forking, handle vma's that have VM_DONTCOPY.
- While forking, handle race of new breakpoints being inserted / removed in the parent process.

Changelog (since v7)
- Separate this patch from the patch that implements mmap_uprobe.
- Increment counter only after verifying that the probe lies underneath.

 include/linux/mm_types.h |    1 
 include/linux/uprobes.h  |    4 ++
 kernel/fork.c            |    4 ++
 kernel/uprobes.c         |  103 ++++++++++++++++++++++++++++++++++++++++++++--
 mm/mmap.c                |   10 ++++
 5 files changed, 117 insertions(+), 5 deletions(-)
include/linux/mm_types.h +1 -0
index e9eac38..2595c9c 100644
@@ -391,6 +391,7 @@ struct mm_struct {
 	struct cpumask cpumask_allocation;
 #endif
 #ifdef CONFIG_UPROBES
+	atomic_t mm_uprobes_count;
 	struct uprobes_xol_area *uprobes_xol_area;
 #endif
 };
include/linux/uprobes.h +4 -0
index 9e25c41..adaa8d3 100644
@@ -126,6 +126,7 @@ extern void unregister_uprobe(struct inode *inode, loff_t offset,
 extern void free_uprobe_utask(struct task_struct *tsk);
 extern void free_uprobes_xol_area(struct mm_struct *mm);
 extern int mmap_uprobe(struct vm_area_struct *vma);
+extern void munmap_uprobe(struct vm_area_struct *vma);
 extern unsigned long __weak get_uprobe_bkpt_addr(struct pt_regs *regs);
 extern int uprobe_post_notifier(struct pt_regs *regs);
 extern int uprobe_bkpt_notifier(struct pt_regs *regs);
@@ -146,6 +147,9 @@ static inline int mmap_uprobe(struct vm_area_struct *vma)
 {
 	return 0;
 }
+static inline void munmap_uprobe(struct vm_area_struct *vma)
+{
+}
 static inline void uprobe_notify_resume(struct pt_regs *regs)
 {
 }
kernel/fork.c +4 -0
index 2da316e..2b3a5e0 100644
@@ -417,6 +417,9 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
 
 		if (retval)
 			goto out;
+
+		if (file && mmap_uprobe(tmp))
+			goto out;
 	}
 	/* a new mm has just been created */
 	arch_dup_mmap(oldmm, mm);
@@ -738,6 +741,7 @@ struct mm_struct *dup_mm(struct task_struct *tsk)
 	mm->pmd_huge_pte = NULL;
 #endif
 #ifdef CONFIG_UPROBES
+	atomic_set(&mm->mm_uprobes_count, 0);
 	mm->uprobes_xol_area = NULL;
 #endif
 
kernel/uprobes.c +99 -4

150 lines not on this page · raw message

mm/mmap.c +9 -1
index 0966418..83813fa 100644
@@ -218,6 +218,7 @@ void unlink_file_vma(struct vm_area_struct *vma)
 		mutex_lock(&mapping->i_mmap_mutex);
 		__remove_shared_vm_struct(vma, file, mapping);
 		mutex_unlock(&mapping->i_mmap_mutex);
+		munmap_uprobe(vma);
 	}
 }
 
@@ -546,8 +547,14 @@ again:			remove_next = 1 + (end > next->vm_end);
 
 	if (file) {
 		mapping = file->f_mapping;
-		if (!(vma->vm_flags & VM_NONLINEAR))
+		if (!(vma->vm_flags & VM_NONLINEAR)) {
 			root = &mapping->i_mmap;
+			munmap_uprobe(vma);
+
+			if (adjust_next)
+				munmap_uprobe(next);
+		}
+
 		mutex_lock(&mapping->i_mmap_mutex);
 		if (insert) {
 			/*
@@ -626,6 +633,7 @@ again:			remove_next = 1 + (end > next->vm_end);
 
 	if (remove_next) {
 		if (file) {
+			munmap_uprobe(next);
 			fput(file);
 			if (next->vm_flags & VM_EXECUTABLE)
 				removed_exe_file_vma(mm);

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#21 lore raw

PATCH v9 5/9 tracing: modify is_delete, is_return from ints to bool.

In reply to Srikar Dronamraju

is_delete and is_return can take atmost 2 values and are better of being a boolean than a int.

---

Changelog (since v5):
- extracted from the next patch on Masami's suggestion.

 kernel/trace/trace_kprobe.c |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)
kernel/trace/trace_kprobe.c +8 -8
index 00d527c..2490dd1 100644
@@ -651,7 +651,7 @@ static struct trace_probe *alloc_trace_probe(const char *group,
 					     void *addr,
 					     const char *symbol,
 					     unsigned long offs,
-					     int nargs, int is_return)
+					     int nargs, bool is_return)
 {
 	struct trace_probe *tp;
 	int ret = -ENOMEM;
@@ -944,7 +944,7 @@ static int split_symbol_offset(char *symbol, unsigned long *offset)
 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
 
 static int parse_probe_vars(char *arg, const struct fetch_type *t,
-			    struct fetch_param *f, int is_return)
+			    struct fetch_param *f, bool is_return)
 {
 	int ret = 0;
 	unsigned long param;
@@ -977,7 +977,7 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t,
 
 /* Recursive argument parser */
 static int __parse_probe_arg(char *arg, const struct fetch_type *t,
-			     struct fetch_param *f, int is_return)
+			     struct fetch_param *f, bool is_return)
 {
 	int ret = 0;
 	unsigned long param;
@@ -1089,7 +1089,7 @@ static int __parse_bitfield_probe_arg(const char *bf,
 
 /* String length checking wrapper */
 static int parse_probe_arg(char *arg, struct trace_probe *tp,
-			   struct probe_arg *parg, int is_return)
+			   struct probe_arg *parg, bool is_return)
 {
 	const char *t;
 	int ret;
@@ -1162,7 +1162,7 @@ static int create_trace_probe(int argc, char **argv)
 	 */
 	struct trace_probe *tp;
 	int i, ret = 0;
-	int is_return = 0, is_delete = 0;
+	bool is_return = false, is_delete = false;
 	char *symbol = NULL, *event = NULL, *group = NULL;
 	char *arg;
 	unsigned long offset = 0;
@@ -1171,11 +1171,11 @@ static int create_trace_probe(int argc, char **argv)
 
 	/* argc must be >= 1 */
 	if (argv[0][0] == 'p')
-		is_return = 0;
+		is_return = false;
 	else if (argv[0][0] == 'r')
-		is_return = 1;
+		is_return = true;
 	else if (argv[0][0] == '-')
-		is_delete = 1;
+		is_delete = true;
 	else {
 		pr_info("Probe definition must be started with 'p', 'r' or"
 			" '-'.\n");

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#22 lore raw

PATCH v9 6/9 tracing: Extract out common code for kprobes/uprobes traceevents.

In reply to Srikar Dronamraju

Move parts of trace_kprobe.c that can be shared with upcoming trace_uprobe.c. Common code to kernel/trace/trace_probe.h and kernel/trace/trace_probe.c.

---
Changelog (since v5)
- Extracted out int to bool changes to a separate patch.
- Fix a bug in kprobe_trace_self_tests_init that was introduced in previous patchset.

Changelog (since v7)
- Modified Copyright as suggested by Steven Rostedt.

 kernel/trace/Kconfig        |    4 
 kernel/trace/Makefile       |    1 
 kernel/trace/trace_kprobe.c |  889 +------------------------------------------
 kernel/trace/trace_probe.c  |  780 ++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_probe.h  |  161 ++++++++
 5 files changed, 964 insertions(+), 871 deletions(-)
 create mode 100644 kernel/trace/trace_probe.c
 create mode 100644 kernel/trace/trace_probe.h
kernel/trace/Kconfig +4 -0
index cd31345..520106a 100644
@@ -373,6 +373,7 @@ config KPROBE_EVENT
 	depends on HAVE_REGS_AND_STACK_ACCESS_API
 	bool "Enable kprobes-based dynamic events"
 	select TRACING
+	select PROBE_EVENTS
 	default y
 	help
 	  This allows the user to add tracing events (similar to tracepoints)
@@ -385,6 +386,9 @@ config KPROBE_EVENT
 	  This option is also required by perf-probe subcommand of perf tools.
 	  If you want to use perf tools, this option is strongly recommended.
 
+config PROBE_EVENTS
+	def_bool n
+
 config DYNAMIC_FTRACE
 	bool "enable/disable ftrace tracepoints dynamically"
 	depends on FUNCTION_TRACER
kernel/trace/Makefile +1 -0
index 5f39a07..fa10d5c 100644
@@ -61,5 +61,6 @@ endif
 ifeq ($(CONFIG_TRACING),y)
 obj-$(CONFIG_KGDB_KDB) += trace_kdb.o
 endif
+obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o
 
 libftrace-y := ftrace.o
kernel/trace/trace_kprobe.c +18 -871

987 lines not on this page · raw message

kernel/trace/trace_probe.c new file +780 -0

780 lines not on this page · raw message

kernel/trace/trace_probe.h new file +161 -0

161 lines not on this page · raw message

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#23 lore raw

PATCH v9 7/9 tracing: uprobes trace_event interface

In reply to Srikar Dronamraju

Implements trace_event support for uprobes. In its current form it can be used to put probes at a specified offset in a file and dump the required registers when the code flow reaches the probed address.

The following example shows how to dump the instruction pointer and %ax a register at the probed text address. Here we are trying to probe zfree in /bin/zsh

# cd /sys/kernel/debug/tracing/
# cat /proc/`pgrep  zsh`/maps | grep /bin/zsh | grep r-xp
00400000-0048a000 r-xp 00000000 08:03 130904 /bin/zsh
# objdump -T /bin/zsh | grep -w zfree
0000000000446420 g    DF .text  0000000000000012  Base        zfree
# echo 'p /bin/zsh:0x46420 %ip %ax' > uprobe_events
# cat uprobe_events
p:uprobes/p_zsh_0x46420 /bin/zsh:0x0000000000046420
# echo 1 > events/uprobes/enable
# sleep 20
# echo 0 > events/uprobes/enable
# cat trace
# tracer: nop
#
#           TASK-PID    CPU#    TIMESTAMP  FUNCTION
#              | |       |          |         |
             zsh-24842 [006] 258544.995456: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
             zsh-24842 [007] 258545.000270: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
             zsh-24842 [002] 258545.043929: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
             zsh-24842 [004] 258547.046129: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79

TODO: Connect a filter to a consumer.

---

Changelog (since v5)
- Added uprobe tracer documentation to this patch.

 Documentation/trace/uprobetracer.txt |   93 ++++
 arch/Kconfig                         |   10 
 kernel/trace/Kconfig                 |   16 +
 kernel/trace/Makefile                |    1 
 kernel/trace/trace.h                 |    5 
 kernel/trace/trace_kprobe.c          |    4 
 kernel/trace/trace_probe.c           |   14 -
 kernel/trace/trace_probe.h           |    3 
 kernel/trace/trace_uprobe.c          |  768 ++++++++++++++++++++++++++++++++++
 9 files changed, 898 insertions(+), 16 deletions(-)
 create mode 100644 Documentation/trace/uprobetracer.txt
 create mode 100644 kernel/trace/trace_uprobe.c
Documentation/trace/uprobetracer.txt new file +93 -0
new file mode 100644
index 0000000..457932f
@@ -0,0 +1,93 @@
+		Uprobe-tracer: Uprobe-based Event Tracing
+		=========================================
+                 Documentation is written by Srikar Dronamraju
+
+Overview
+--------
+These events are similar to kprobe based events.
+To enable this feature, build your kernel with CONFIG_UPROBE_EVENTS=y.
+
+Similar to the kprobe-event tracer, this doesn't need to be activated via
+current_tracer. Instead of that, add probe points via
+/sys/kernel/debug/tracing/uprobe_events, and enable it via
+/sys/kernel/debug/tracing/events/uprobes/<EVENT>/enabled.
+
+
+Synopsis of uprobe_tracer
+-------------------------
+  p[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS]	: Set a probe
+
+ GRP		: Group name. If omitted, use "uprobes" for it.
+ EVENT		: Event name. If omitted, the event name is generated
+		  based on SYMBOL+offs.
+ PATH		: path to an executable or a library.
+ SYMBOL[+offs]	: Symbol+offset where the probe is inserted.
+
+ FETCHARGS	: Arguments. Each probe can have up to 128 args.
+  %REG		: Fetch register REG
+
+Event Profiling
+---------------
+ You can check the total number of probe hits and probe miss-hits via
+/sys/kernel/debug/tracing/uprobe_profile.
+ The first column is event name, the second is the number of probe hits,
+the third is the number of probe miss-hits.
+
+Usage examples
+--------------
+To add a probe as a new event, write a new definition to uprobe_events
+as below.
+
+  echo 'p: /bin/bash:0x4245c0' > /sys/kernel/debug/tracing/uprobe_events
+
+ This sets a uprobe at an offset of 0x4245c0 in the executable /bin/bash
+
+
+  echo > /sys/kernel/debug/tracing/uprobe_events
+
+ This clears all probe points.
+
+The following example shows how to dump the instruction pointer and %ax
+a register at the probed text address.  Here we are trying to probe
+function zfree in /bin/zsh
+
+    # cd /sys/kernel/debug/tracing/
+    # cat /proc/`pgrep  zsh`/maps | grep /bin/zsh | grep r-xp
+    00400000-0048a000 r-xp 00000000 08:03 130904 /bin/zsh
+    # objdump -T /bin/zsh | grep -w zfree
+    0000000000446420 g    DF .text  0000000000000012  Base        zfree
+
+0x46420 is the offset of zfree in object /bin/zsh that is loaded at
+0x00400000. Hence the command to probe would be :
+
+    # echo 'p /bin/zsh:0x46420 %ip %ax' > uprobe_events
+
+We can see the events that are registered by looking at the uprobe_events
+file.
+
+    # cat uprobe_events
+    p:uprobes/p_zsh_0x46420 /bin/zsh:0x0000000000046420
+
+Right after definition, each event is disabled by default. For tracing these
+events, you need to enable it by:
+
+    # echo 1 > events/uprobes/enable
+
+Lets disable the event after sleeping for some time.
+    # sleep 20
+    # echo 0 > events/uprobes/enable
+
+And you can see the traced information via /sys/kernel/debug/tracing/trace.
+
+    # cat trace
+    # tracer: nop
+    #
+    #           TASK-PID    CPU#    TIMESTAMP  FUNCTION
+    #              | |       |          |         |
+                 zsh-24842 [006] 258544.995456: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
+                 zsh-24842 [007] 258545.000270: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
+                 zsh-24842 [002] 258545.043929: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
+                 zsh-24842 [004] 258547.046129: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
+
+Each line shows us probes were triggered for a pid 24842 with ip being
+0x446421 and contents of ax register being 79.
arch/Kconfig +1 -9
index 6328b49..6c6df9f 100644
@@ -62,15 +62,7 @@ config OPTPROBES
 	depends on !PREEMPT
 
 config UPROBES
-	bool "User-space probes (EXPERIMENTAL)"
-	depends on ARCH_SUPPORTS_UPROBES
-	default n
-	help
-	  Uprobes enables kernel subsystems to establish probepoints
-	  in user applications and execute handler functions when
-	  the probepoints are hit.
-
-	  If in doubt, say "N".
+	def_bool n
 
 config HAVE_EFFICIENT_UNALIGNED_ACCESS
 	bool
kernel/trace/Kconfig +16 -0
index 520106a..b001fb1 100644
@@ -386,6 +386,22 @@ config KPROBE_EVENT
 	  This option is also required by perf-probe subcommand of perf tools.
 	  If you want to use perf tools, this option is strongly recommended.
 
+config UPROBE_EVENT
+	bool "Enable uprobes-based dynamic events"
+	depends on ARCH_SUPPORTS_UPROBES
+	depends on MMU
+	select UPROBES
+	select PROBE_EVENTS
+	select TRACING
+	default n
+	help
+	  This allows the user to add tracing events on top of userspace dynamic
+	  events (similar to tracepoints) on the fly via the traceevents interface.
+	  Those events can be inserted wherever uprobes can probe, and record
+	  various registers.
+	  This option is required if you plan to use perf-probe subcommand of perf
+	  tools on user space applications.
+
 config PROBE_EVENTS
 	def_bool n
 
kernel/trace/Makefile +1 -0
index fa10d5c..1734c03 100644
@@ -62,5 +62,6 @@ ifeq ($(CONFIG_TRACING),y)
 obj-$(CONFIG_KGDB_KDB) += trace_kdb.o
 endif
 obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o
+obj-$(CONFIG_UPROBE_EVENT) += trace_uprobe.o
 
 libftrace-y := ftrace.o
kernel/trace/trace.h +5 -0
index 092e1f8..f5f7bb3 100644
@@ -97,6 +97,11 @@ struct kretprobe_trace_entry_head {
 	unsigned long		ret_ip;
 };
 
+struct uprobe_trace_entry_head {
+	struct trace_entry	ent;
+	unsigned long		ip;
+};
+
 /*
  * trace_flag_type is an enumeration that holds different
  * states when a trace occurs. These are:
kernel/trace/trace_kprobe.c +2 -2
index 967e634..60384df 100644
@@ -524,8 +524,8 @@ static int create_trace_probe(int argc, char **argv)
 		}
 
 		/* Parse fetch argument */
-		ret = traceprobe_parse_probe_arg(arg, &tp->size, &tp->args[i],
-								is_return);
+		ret = traceprobe_parse_probe_arg(arg, &tp->size,
+					&tp->args[i], is_return, true);
 		if (ret) {
 			pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
 			goto error;
kernel/trace/trace_probe.c +10 -4
index e79a9e5..c0a98e6 100644
@@ -529,13 +529,17 @@ static int parse_probe_vars(char *arg, const struct fetch_type *t,
 
 /* Recursive argument parser */
 static int parse_probe_arg(char *arg, const struct fetch_type *t,
-		     struct fetch_param *f, bool is_return)
+		     struct fetch_param *f, bool is_return, bool is_kprobe)
 {
 	int ret = 0;
 	unsigned long param;
 	long offset;
 	char *tmp;
 
+	/* Until uprobe_events supports only reg arguments */
+	if (!is_kprobe && arg[0] != '%')
+		return -EINVAL;
+
 	switch (arg[0]) {
 	case '$':
 		ret = parse_probe_vars(arg + 1, t, f, is_return);
@@ -585,7 +589,8 @@ static int parse_probe_arg(char *arg, const struct fetch_type *t,
 			if (!dprm)
 				return -ENOMEM;
 			dprm->offset = offset;
-			ret = parse_probe_arg(arg, t2, &dprm->orig, is_return);
+			ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
+							is_kprobe);
 			if (ret)
 				kfree(dprm);
 			else {
@@ -640,7 +645,7 @@ static int __parse_bitfield_probe_arg(const char *bf,
 
 /* String length checking wrapper */
 int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
-		struct probe_arg *parg, bool is_return)
+		struct probe_arg *parg, bool is_return, bool is_kprobe)
 {
 	const char *t;
 	int ret;
@@ -666,7 +671,8 @@ int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
 	}
 	parg->offset = *size;
 	*size += parg->type->size;
-	ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return);
+	ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return,
+							is_kprobe);
 	if (ret >= 0 && t != NULL)
 		ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
 	if (ret >= 0) {
kernel/trace/trace_probe.h +2 -1
index 84eef0a..31e635e 100644
@@ -66,6 +66,7 @@
 #define TP_FLAG_TRACE	1
 #define TP_FLAG_PROFILE	2
 #define TP_FLAG_REGISTERED 4
+#define TP_FLAG_UPROBE	8
 
 
 /* data_rloc: data relative location, compatible with u32 */
@@ -144,7 +145,7 @@ static inline int is_good_name(const char *name)
 }
 
 extern int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
-		   struct probe_arg *parg, bool is_return);
+		   struct probe_arg *parg, bool is_return, bool is_kprobe);
 
 extern int traceprobe_conflict_field_name(const char *name,
 			       struct probe_arg *args, int narg);
kernel/trace/trace_uprobe.c new file +768 -0

768 lines not on this page · raw message

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#24 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

Quoted 30 lines · Srikar Dronamraju

Implements trace_event support for uprobes. In its current form it can be used to put probes at a specified offset in a file and dump the required registers when the code flow reaches the probed address.

The following example shows how to dump the instruction pointer and %ax a register at the probed text address. Here we are trying to probe zfree in /bin/zsh

# cd /sys/kernel/debug/tracing/
# cat /proc/`pgrep  zsh`/maps | grep /bin/zsh | grep r-xp
00400000-0048a000 r-xp 00000000 08:03 130904 /bin/zsh
# objdump -T /bin/zsh | grep -w zfree
0000000000446420 g    DF .text  0000000000000012  Base        zfree
# echo 'p /bin/zsh:0x46420 %ip %ax' > uprobe_events
# cat uprobe_events
p:uprobes/p_zsh_0x46420 /bin/zsh:0x0000000000046420
# echo 1 > events/uprobes/enable
# sleep 20
# echo 0 > events/uprobes/enable
# cat trace
# tracer: nop
#
#           TASK-PID    CPU#    TIMESTAMP  FUNCTION
#              | |       |          |         |
             zsh-24842 [006] 258544.995456: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
             zsh-24842 [007] 258545.000270: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
             zsh-24842 [002] 258545.043929: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
             zsh-24842 [004] 258547.046129: p_zsh_0x46420: (0x446420) arg1=446421 arg2=79
Quoted 1 line

TODO: Connect a filter to a consumer.

hi,

It looks to me the perf/trace filtering is already working with the small fix attached.

In another email discussion I think you suggested to use the 'filter property of uprobe consumer' for the perf/trace filtering. The current code does the filtering in the handler itself, and it's same for other perf/trace based events.

I discussed the 'filter property of uprobe consumer' with Oleg, and he is not sure why it was added.. was it only for the perf/trace filtering?

I've tested following event:

        echo "p:probe_libc/free /lib64/libc-2.13.so:0x7a4f0 %ax" > ./uprobe_events

and commands like:

        perf record -a -e probe_libc:free  --filter "common_pid == 1127"
        perf record -e probe_libc:free --filter "arg1 == 0xa" ls

got me proper results.

thanks,
jirka

---
The preemption needs to be disabled when submitting data into perf.

---
 kernel/trace/trace_uprobe.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
kernel/trace/trace_uprobe.c +5 -1
index af29368..4d3857c 100644
@@ -653,9 +653,11 @@ static void uprobe_perf_func(struct trace_uprobe *tp, struct pt_regs *regs)
 		     "profile buffer not large enough"))
 		return;
 
+	preempt_disable();
+
 	entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
 	if (!entry)
-		return;
+		goto out;
 
 	entry->ip = get_uprobe_bkpt_addr(task_pt_regs(current));
 	data = (u8 *)&entry[1];
@@ -665,6 +667,8 @@ static void uprobe_perf_func(struct trace_uprobe *tp, struct pt_regs *regs)
 
 	head = this_cpu_ptr(call->perf_events);
 	perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
+ out:
+	preempt_enable();
 }
 #endif	/* CONFIG_PERF_EVENTS */
 
Signature · 8 lines
1.7.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#25 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

Quoted 10 lines

I've tested following event:

        echo "p:probe_libc/free /lib64/libc-2.13.so:0x7a4f0 %ax" > ./uprobe_events

and commands like:

        perf record -a -e probe_libc:free  --filter "common_pid == 1127"
        perf record -e probe_libc:free --filter "arg1 == 0xa" ls

got me proper results.

Okay thanks for the inputs.

Quoted 5 lines

thanks,
jirka

---
The preemption needs to be disabled when submitting data into perf.

I actually looked at other places where perf_trace_buf_prepare and perf_trace_buf_submit are being called. for example perf_syscall_enter and perf_syscall_exit both call the above routines and they didnt seem to be called with premption disabled. Is that the way perf probe is called in our case that needs us to call pre-emption here? Did you see a case where calling these without preemption disabled caused a problem?

Quoted 30 lines
---
 kernel/trace/trace_uprobe.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
kernel/trace/trace_uprobe.c +5 -1
index af29368..4d3857c 100644
@@ -653,9 +653,11 @@ static void uprobe_perf_func(struct trace_uprobe *tp, struct pt_regs *regs)
 		     "profile buffer not large enough"))
 		return;
 
+	preempt_disable();
+
 	entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
 	if (!entry)
-		return;
+		goto out;
 
 	entry->ip = get_uprobe_bkpt_addr(task_pt_regs(current));
 	data = (u8 *)&entry[1];
@@ -665,6 +667,8 @@ static void uprobe_perf_func(struct trace_uprobe *tp, struct pt_regs *regs)
 
 	head = this_cpu_ptr(call->perf_events);
 	perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
+ out:
+	preempt_enable();
 }
 #endif	/* CONFIG_PERF_EVENTS */
 
Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#26 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

Quoted 19 lines · Srikar Dronamraju
Quoted 10 lines

I've tested following event:

        echo "p:probe_libc/free /lib64/libc-2.13.so:0x7a4f0 %ax" > ./uprobe_events

and commands like:

        perf record -a -e probe_libc:free  --filter "common_pid == 1127"
        perf record -e probe_libc:free --filter "arg1 == 0xa" ls

got me proper results.

Okay thanks for the inputs.

Quoted 5 lines

thanks,
jirka

---
The preemption needs to be disabled when submitting data into perf.

Quoted 6 lines

I actually looked at other places where perf_trace_buf_prepare and perf_trace_buf_submit are being called. for example perf_syscall_enter and perf_syscall_exit both call the above routines and they didnt seem to be called with premption disabled. Is that the way perf probe is called in our case that needs us to call pre-emption here? Did you see a case where calling these without preemption disabled caused a problem?

the perf_trace_buf_prepare touches per cpu variables, hence the preemption disabling

the perf_trace_buf_prepare code is used by syscalls, kprobes, and trace events

- both syscalls and trace events are implemented by tracepoints which disable preemption before calling the probe (see __DO_TRACE macro in include/linux/tracepoint.h)

- kprobes disable preemption as well
(kprobe_handler in arch/x86/kernel/kprobes.c)
haven't checked the optimalized kprobes,
but should be the same case

jirka

Quoted 25 lines
Quoted 25 lines

---
 kernel/trace/trace_uprobe.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
kernel/trace/trace_uprobe.c +3 -1
index af29368..4d3857c 100644
@@ -653,9 +653,11 @@ static void uprobe_perf_func(struct trace_uprobe *tp, struct pt_regs *regs)
 		     "profile buffer not large enough"))
 		return;
 
+	preempt_disable();
+
 	entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
 	if (!entry)
-		return;
+		goto out;
 
 	entry->ip = get_uprobe_bkpt_addr(task_pt_regs(current));
 	data = (u8 *)&entry[1];
@@ -665,6 +667,8 @@ static void uprobe_perf_func(struct trace_uprobe *tp, struct pt_regs *regs)
 
Quoted 12 lines
Quoted 7 lines
kernel/trace/trace_uprobe.c +2 -0
 	head = this_cpu_ptr(call->perf_events);
 	perf_trace_buf_submit(entry, size, rctx, entry->ip, 1, regs, head);
+ out:
+	preempt_enable();
 }
 #endif	/* CONFIG_PERF_EVENTS */
 
Signature · 2 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#27 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

Quoted 37 lines · Jiri Olsa
Quoted 19 lines · Srikar Dronamraju
Quoted 10 lines

I've tested following event:

        echo "p:probe_libc/free /lib64/libc-2.13.so:0x7a4f0 %ax" > ./uprobe_events

and commands like:

        perf record -a -e probe_libc:free  --filter "common_pid == 1127"
        perf record -e probe_libc:free --filter "arg1 == 0xa" ls

got me proper results.

Okay thanks for the inputs.

Quoted 5 lines

thanks,
jirka

---
The preemption needs to be disabled when submitting data into perf.

Quoted 6 lines

I actually looked at other places where perf_trace_buf_prepare and perf_trace_buf_submit are being called. for example perf_syscall_enter and perf_syscall_exit both call the above routines and they didnt seem to be called with premption disabled. Is that the way perf probe is called in our case that needs us to call pre-emption here? Did you see a case where calling these without preemption disabled caused a problem?

the perf_trace_buf_prepare touches per cpu variables, hence the preemption disabling

the perf_trace_buf_prepare code is used by syscalls, kprobes, and trace events

- both syscalls and trace events are implemented by tracepoints which disable preemption before calling the probe (see __DO_TRACE macro in include/linux/tracepoint.h)

Quoted 7 lines

- kprobes disable preemption as well
(kprobe_handler in arch/x86/kernel/kprobes.c)
haven't checked the optimalized kprobes,
but should be the same case

jirka

Okay, thanks for the clarification, I will pick the fix.

--
Thanks and Regards
Srikar

#28 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

In reply to Jiri Olsa
Quoted 8 lines · Jiri Olsa

I've tested following event:

        echo "p:probe_libc/free /lib64/libc-2.13.so:0x7a4f0 %ax" > ./uprobe_events

and commands like:

        perf record -a -e probe_libc:free  --filter "common_pid == 1127"
        perf record -e probe_libc:free --filter "arg1 == 0xa" ls

got me proper results.

Btw., Srikar, if that's the primary UI today then we'll need to make it a *lot* more user-friendly than the above usage workflow.

In particular this line:

Quoted 1 line
        echo "p:probe_libc/free /lib64/libc-2.13.so:0x7a4f0 %ax" > ./uprobe_events

is not something a mere mortal will be able to figure out.

There needs to be perf probe integration, that allows intuitive usage, such as:

perf probe add libc:free

Using the perf symbols code it should first search a libc*so DSO in the system, finding say /lib64/libc-2.15.so. The 'free' symbol is readily available there:

aldebaran:~> eu-readelf -s /lib64/libc-2.15.so | grep ' free$'
7186: 00000039ff47f080 224 FUNC GLOBAL DEFAULT 12 free

then the tool can automatically turn that symbol information into the specific probe.

Will it all work with DSO randomization, prelinking and default placement as well?

Users should not be expected to enter magic hexa numbers to get a trivial usecase going ...

this bit:

Quoted 2 lines
        perf record -a -e probe_libc:free  --filter "common_pid == 1127"
        perf record -e probe_libc:free --filter "arg1 == 0xa" ls

looks good and intuitive and 'perf list' should list all the available uprobes.

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#29 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

Quoted 11 lines
Quoted 6 lines

and commands like:

        perf record -a -e probe_libc:free  --filter "common_pid == 1127"
        perf record -e probe_libc:free --filter "arg1 == 0xa" ls

got me proper results.

Btw., Srikar, if that's the primary UI today then we'll need to make it a *lot* more user-friendly than the above usage workflow.

Quoted 5 lines

In particular this line:

Quoted 1 line
        echo "p:probe_libc/free /lib64/libc-2.13.so:0x7a4f0 %ax" > ./uprobe_events

is not something a mere mortal will be able to figure out.

Agree, perf probe is the primary interface to use uprobes.

Quoted 5 lines

There needs to be perf probe integration, that allows intuitive usage, such as:

perf probe add libc:free

Current usage is like perf probe -x <executable> -a <func1> -a <func2> So we could use
perf probe -x /lib64/libc.so.6 free

or
perf probe -x /lib64/libc.so.6 -a free -a malloc -a strcpy

The -x option helps perf to identify that its a user space based probing. This currently restricts that all probes defined per "perf probe" invocation to just one executable. This usage was suggested by Masami.

Earlier we used perf probe free@/lib/libc.so.6 malloc@/lib/libc.so.6 The objection for this was that perf was already using @ to signify source file. Similarly : is already used for Relative line number.

This also goes with perf probe -F -x /lib64/libc.so to list the available probes in libc.

Quoted 4 lines

Using the perf symbols code it should first search a libc*so DSO in the system, finding say /lib64/libc-2.15.so. The 'free' symbol is readily available there:

While I understand the ease of using a libc instead of the full path, I think it does have few issues.
- Do we need to keep checking if the new files created in the system match the pattern and if they match the pattern, dynamically add the files?

- Also the current model helps if the user wants to restrict his trace to particular executable where there are more that one executable with the same name.

Quoted 7 lines

aldebaran:~> eu-readelf -s /lib64/libc-2.15.so | grep ' free$'
7186: 00000039ff47f080 224 FUNC GLOBAL DEFAULT 12 free

then the tool can automatically turn that symbol information into the specific probe.

Given a function in an executable, we are

Quoted 2 lines

Will it all work with DSO randomization, prelinking and default placement as well?

Works with DSO randomization, I havent tried with prelinking. did you mean http://en.wikipedia.org/wiki/Placement_syntax#Default_placement
when you said default placement?

Quoted 3 lines

Users should not be expected to enter magic hexa numbers to get a trivial usecase going ...

yes, that why we dont allow perf probe -x /lib/libc.so.6 0x1234

Quoted 8 lines

this bit:

Quoted 2 lines
        perf record -a -e probe_libc:free  --filter "common_pid == 1127"
        perf record -e probe_libc:free --filter "arg1 == 0xa" ls

looks good and intuitive and 'perf list' should list all the available uprobes.

Currently "perf probe -F -x /bin/zsh" lists all available functions in zsh. perf probe --list has been enhanced to show uprobes that are already registered.

Similar to kprobes based probes, available user space probes are not part of "perf list".

Quoted 5 lines

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#30 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

btw., i'd write the CONFIG_UPROBE_EVENT .config help text:

This allows the user to add tracing events on top of userspace dynamic events (similar to tracepoints) on the fly via the traceevents interface. Those events can be inserted wherever uprobes can probe, and record various registers.

This option is required if you plan to use perf-probe subcommand of perf tools on user space applications.

In a clearer form:

This allows the user to add user-space dynamic events on the fly, similar to tracepoints. These events can be inserted into just about any user-space code transparently, and can record register state there on the fly, which is then used by instrumentation tools.

This option is required if you plan to use the "perf probe" subcommand of the perf tool on user space applications.

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#31 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

Quoted 13 lines · Ingo Molnar

btw., i'd write the CONFIG_UPROBE_EVENT .config help text:

This allows the user to add tracing events on top of userspace dynamic events (similar to tracepoints) on the fly via the traceevents interface. Those events can be inserted wherever uprobes can probe, and record various registers.

This option is required if you plan to use perf-probe subcommand of perf tools on user space applications.

In a clearer form:

Quoted 9 lines

This allows the user to add user-space dynamic events on the fly, similar to tracepoints. These events can be inserted into just about any user-space code transparently, and can record register state there on the fly, which is then used by instrumentation tools.

This option is required if you plan to use the "perf probe" subcommand of the perf tool on user space applications.

Okay, Will change as suggested.

Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Tested-by Srikar Dronamraju
#32 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

In reply to Srikar Dronamraju

Srikar, rebased commits like this one:

c5af743: rcu: Introduce raw SRCU read-side primitives

are absolutely inacceptable:

commit c5af7439f322db86e347991e184b95dd80676967
Author: Paul E. McKenney <[email protected]>
Date: Sun Oct 9 15:13:11 2011 -0700

rcu: Introduce raw SRCU read-side primitives

...

If you pull a commit from Paul into your tree then you must never rebase it. If you applied Paul's commit out of email then you should add a Signed-off-by, not a Tested-by tag at the end.

Also, please test whether it works when merged to latest -tip, there's been various RCU changes in this area. AFAICS these read-side primitives are already upstream via:

0c53dd8b3140: rcu: Introduce raw SRCU read-side primitives

so pulling your tree would break the build.

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#33 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

Quoted 6 lines

Srikar, rebased commits like this one:

c5af743: rcu: Introduce raw SRCU read-side primitives

are absolutely inacceptable:

Okay, I wasnt sure whats the best way for me to expose a tree such that people who just use my tree to try (without basing on any other trees except Linus) can build and use uprobes but people who already have this commit arent affected by me having this commit.

So couple of people did tell me that git was intelligent enuf that if the commit was already in, it would create a empty commit.

do you have suggestions on how to work around such situations in future?

Quoted 8 lines

commit c5af7439f322db86e347991e184b95dd80676967
Author: Paul E. McKenney <[email protected]>
Date: Sun Oct 9 15:13:11 2011 -0700

rcu: Introduce raw SRCU read-side primitives

...

Quoted 7 lines

If you pull a commit from Paul into your tree then you must never rebase it. If you applied Paul's commit out of email then you should add a Signed-off-by, not a Tested-by tag at the end.

I actually cherry-picked commit from your tree i.e -tip.
The Tested-by tag was added by Paul after I confirmed to him that it works. Even the commit 0c53dd8b3140: that you refer has the same tags.

Quoted 8 lines

Also, please test whether it works when merged to latest -tip, there's been various RCU changes in this area. AFAICS these read-side primitives are already upstream via:

0c53dd8b3140: rcu: Introduce raw SRCU read-side primitives

so pulling your tree would break the build.

I had used v3.2 as a base for my tree and 0c53dd8b3140: was not part of that tag. Now that its gone into mainline, I will drop the commit.

Signature · 2 lines
Thanks and Regards
Srikar
#34 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

Quoted 13 lines · Srikar Dronamraju
Quoted 6 lines

Srikar, rebased commits like this one:

c5af743: rcu: Introduce raw SRCU read-side primitives

are absolutely inacceptable:

Okay, I wasnt sure whats the best way for me to expose a tree such that people who just use my tree to try (without basing on any other trees except Linus) can build and use uprobes but people who already have this commit arent affected by me having this commit.

Quoted 6 lines

So couple of people did tell me that git was intelligent enuf that if the commit was already in, it would create a empty commit.

do you have suggestions on how to work around such situations in future?

You can just apply it out of email, as a regular commit.

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#35 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

a couple of other 'perf probe' suggestions:

i think the 'perf probe' UI should be streamlined.

Right now we have kprobes assumptions like this syntax:

perf probe <kernel_symbol>

will add a probe.

That is nonsensical once we merge uprobes, as i'd expect much more developers to be interested in user-space symbols than kernel-space symbols.

So we could change this syntax and add the more intuitive:

perf probe add ...
perf probe del ...
perf probe list

Variants. Also, the -x syntax isnt particularly intuitive either
- it should be a 'perf probe add ...' variant.

If someone uses the old syntax it will produce a meaningful error message.

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#36 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

In reply to Srikar Dronamraju

A couple of 'perf probe' usability issues.

When running it as unprivileged user right now it fails with:

 $ perf probe -x /lib64/libc.so.6 free
 Failed to open uprobe_events file: Permission denied

That error message should reference the full file name in question, i.e. /sys/kernel/debug/tracing/uprobe_events - that way the user can make that file writable if it's secure to do that on that system.

The other thing is the text that gets printed:

 $ perf probe --del free
 Remove event: probe_libc:free

that's not how tools generally communicate - it should be something like:

 $ perf probe --del free
 Removed event: probe_libc:free

Note the past tense - this tells the user that the action has been performed successfully.

Likewise, 'perf probe --add' should talk in past tense as well, to indicate success. So it should say something like:

 $ perf probe -x /lib64/libc.so.6 free
 Added new event:
  probe_libc:free      (on 0x7f080)

You can now use it in all perf tools, such as:

perf record -e probe_libc:free -aR sleep 1

(Also note the s/on/in change in the other text.)

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#37 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

In reply to Srikar Dronamraju

Have you tried to use 'perf probe' to achieve any useful instrumentation on a real app?

I just tried out the 'glibc:free' usecase and it's barely usable.

Firstly, the recording very frequently produces overruns:

 $ perf record -e probe_libc:free -aR sleep 1
 [ perf record: Woken up 169 times to write data ]
 [ perf record: Captured and wrote 89.674 MB perf.data (~3917919 samples) ]
 Warning:Processed 1349133 events and lost 1 chunks!

Using -m 4096 made it work better.

Adding -g for call-graph profiling caused 'perf report' to lock up:

perf record -m 4096 -e probe_libc:free -agR sleep 1 perf report
[ loops forever ]

I've sent a testcase to Arnaldo separately. Note that perf report --stdio appears to work.

Regular '-e cycles -g' works fine, so this is a uprobes specific bug.

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#38 lore raw

Re: [PATCH v9 3.2 7/9] tracing: uprobes trace_event interface

Quoted 7 lines

Have you tried to use 'perf probe' to achieve any useful instrumentation on a real app?

I just tried out the 'glibc:free' usecase and it's barely usable.

Thanks for trying.

Yes, I have actually place probes on free and malloc while building a kernel and it has worked for me.

Quoted 7 lines

Firstly, the recording very frequently produces overruns:

 $ perf record -e probe_libc:free -aR sleep 1
 [ perf record: Woken up 169 times to write data ]
 [ perf record: Captured and wrote 89.674 MB perf.data (~3917919 samples) ]
 Warning:Processed 1349133 events and lost 1 chunks!

I have seen the "lost chunks" but thats not always and mostly when I have run perf record for a very long time. But I have seen this even with just kernel probes too. So I didnt debug that.

Quoted 9 lines

Using -m 4096 made it work better.

Adding -g for call-graph profiling caused 'perf report' to lock up:

perf record -m 4096 -e probe_libc:free -agR sleep 1 perf report
[ loops forever ]

This actually works for me.
I had CONFIG_PREEMPT not set. So I set it and tried with and without Jiri's fix. It still worked for me. Can you pass me your config that shows this hang so that I can try and fix it.

Quoted 2 lines

I've sent a testcase to Arnaldo separately. Note that perf report --stdio appears to work.

There are no changes in perf report because of uprobes. So the data collected from uprobes could trigger the hang. I was speculating that not disabling preemption that Jiri pointed out could be causing this. So I tried with and without but couldnt reproduce. Can I request you to see if Jiri's patch helps?

Today with -g option, on one machine I do see a

$ sudo perf report
perf: Floating point exception

I will take a look at this too. Other machines dont seem to show this error.

Quoted 3 lines

Regular '-e cycles -g' works fine, so this is a uprobes specific bug.

Signature · 9 lines
Thanks and Regards
Srikar

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#39 lore raw

PATCH v9 8/9 perf: rename target_module to target

In reply to Srikar Dronamraju

This is a precursor patch that modifies names that refer to kernel/module to also refer to user space names.

---
 tools/perf/builtin-probe.c    |   12 ++++++------
 tools/perf/util/probe-event.c |   26 +++++++++++++-------------
 2 files changed, 19 insertions(+), 19 deletions(-)
tools/perf/builtin-probe.c +6 -6
index 710ae3d..93d5171 100644
@@ -61,7 +61,7 @@ static struct {
 	struct perf_probe_event events[MAX_PROBES];
 	struct strlist *dellist;
 	struct line_range line_range;
-	const char *target_module;
+	const char *target;
 	int max_probe_points;
 	struct strfilter *filter;
 } params;
@@ -249,7 +249,7 @@ static const struct option options[] = {
 		   "file", "vmlinux pathname"),
 	OPT_STRING('s', "source", &symbol_conf.source_prefix,
 		   "directory", "path to kernel source"),
-	OPT_STRING('m', "module", &params.target_module,
+	OPT_STRING('m', "module", &params.target,
 		   "modname|path",
 		   "target module name (for online) or path (for offline)"),
 #endif
@@ -336,7 +336,7 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
 		if (!params.filter)
 			params.filter = strfilter__new(DEFAULT_FUNC_FILTER,
 						       NULL);
-		ret = show_available_funcs(params.target_module,
+		ret = show_available_funcs(params.target,
 					   params.filter);
 		strfilter__delete(params.filter);
 		if (ret < 0)
@@ -357,7 +357,7 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
 			usage_with_options(probe_usage, options);
 		}
 
-		ret = show_line_range(&params.line_range, params.target_module);
+		ret = show_line_range(&params.line_range, params.target);
 		if (ret < 0)
 			pr_err("  Error: Failed to show lines. (%d)\n", ret);
 		return ret;
@@ -374,7 +374,7 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
 
 		ret = show_available_vars(params.events, params.nevents,
 					  params.max_probe_points,
-					  params.target_module,
+					  params.target,
 					  params.filter,
 					  params.show_ext_vars);
 		strfilter__delete(params.filter);
@@ -396,7 +396,7 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
 	if (params.nevents) {
 		ret = add_perf_probe_events(params.events, params.nevents,
 					    params.max_probe_points,
-					    params.target_module,
+					    params.target,
 					    params.force_add);
 		if (ret < 0) {
 			pr_err("  Error: Failed to add events. (%d)\n", ret);
tools/perf/util/probe-event.c +13 -13
index eb25900..d54eefb 100644
@@ -275,10 +275,10 @@ static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
 /* Try to find perf_probe_event with debuginfo */
 static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
 					  struct probe_trace_event **tevs,
-					  int max_tevs, const char *module)
+					  int max_tevs, const char *target)
 {
 	bool need_dwarf = perf_probe_event_need_dwarf(pev);
-	struct debuginfo *dinfo = open_debuginfo(module);
+	struct debuginfo *dinfo = open_debuginfo(target);
 	int ntevs, ret = 0;
 
 	if (!dinfo) {
@@ -297,9 +297,9 @@ static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
 
 	if (ntevs > 0) {	/* Succeeded to find trace events */
 		pr_debug("find %d probe_trace_events.\n", ntevs);
-		if (module)
+		if (target)
 			ret = add_module_to_probe_trace_events(*tevs, ntevs,
-							       module);
+							       target);
 		return ret < 0 ? ret : ntevs;
 	}
 
@@ -1798,14 +1798,14 @@ static int __add_probe_trace_events(struct perf_probe_event *pev,
 
 static int convert_to_probe_trace_events(struct perf_probe_event *pev,
 					  struct probe_trace_event **tevs,
-					  int max_tevs, const char *module)
+					  int max_tevs, const char *target)
 {
 	struct symbol *sym;
 	int ret = 0, i;
 	struct probe_trace_event *tev;
 
 	/* Convert perf_probe_event with debuginfo */
-	ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
+	ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, target);
 	if (ret != 0)
 		return ret;	/* Found in debuginfo or got an error */
 
@@ -1821,8 +1821,8 @@ static int convert_to_probe_trace_events(struct perf_probe_event *pev,
 		goto error;
 	}
 
-	if (module) {
-		tev->point.module = strdup(module);
+	if (target) {
+		tev->point.module = strdup(target);
 		if (tev->point.module == NULL) {
 			ret = -ENOMEM;
 			goto error;
@@ -1886,7 +1886,7 @@ struct __event_package {
 };
 
 int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
-			  int max_tevs, const char *module, bool force_add)
+			  int max_tevs, const char *target, bool force_add)
 {
 	int i, j, ret;
 	struct __event_package *pkgs;
@@ -1909,7 +1909,7 @@ int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
 		ret  = convert_to_probe_trace_events(pkgs[i].pev,
 						     &pkgs[i].tevs,
 						     max_tevs,
-						     module);
+						     target);
 		if (ret < 0)
 			goto end;
 		pkgs[i].ntevs = ret;
@@ -2065,7 +2065,7 @@ static int filter_available_functions(struct map *map __unused,
 	return 1;
 }
 
-int show_available_funcs(const char *module, struct strfilter *_filter)
+int show_available_funcs(const char *target, struct strfilter *_filter)
 {
 	struct map *map;
 	int ret;
@@ -2076,9 +2076,9 @@ int show_available_funcs(const char *module, struct strfilter *_filter)
 	if (ret < 0)
 		return ret;
 
-	map = kernel_get_module_map(module);
+	map = kernel_get_module_map(target);
 	if (!map) {
-		pr_err("Failed to find %s map.\n", (module) ? : "kernel");
+		pr_err("Failed to find %s map.\n", (target) ? : "kernel");
 		return -EINVAL;
 	}
 	available_func_filter = _filter;
#40 lore raw
In reply to Srikar Dronamraju
Quoted 3 lines · Srikar Dronamraju

This patchset implements Uprobes which enables you to dynamically probe any routine in a user space application and collect information non-disruptively.

Did all review feedback get addressed in your latest tree?

If yes then it would be nice to hear the opinion of Andrew about this bit:

Quoted 1 line
 mm/mmap.c                               |   33 +-

The relevant portion of the patch is:

Quoted 57 lines
mm/mmap.c +19 -0
@@ -30,6 +30,7 @@
 #include <linux/perf_event.h>
 #include <linux/audit.h>
 #include <linux/khugepaged.h>
+#include <linux/uprobes.h>
 
 #include <asm/uaccess.h>
 #include <asm/cacheflush.h>
@@ -616,6 +617,13 @@ again:			remove_next = 1 + (end > next->vm_end);
 	if (mapping)
 		mutex_unlock(&mapping->i_mmap_mutex);
 
+	if (root) {
+		mmap_uprobe(vma);
+
+		if (adjust_next)
+			mmap_uprobe(next);
+	}
+
 	if (remove_next) {
 		if (file) {
 			fput(file);
@@ -637,6 +645,8 @@ again:			remove_next = 1 + (end > next->vm_end);
 			goto again;
 		}
 	}
+	if (insert && file)
+		mmap_uprobe(insert);
 
 	validate_mm(mm);
 
@@ -1329,6 +1339,11 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 			mm->locked_vm += (len >> PAGE_SHIFT);
 	} else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
 		make_pages_present(addr, addr + len);
+
+	if (file && mmap_uprobe(vma))
+		/* matching probes but cannot insert */
+		goto unmap_and_free_vma;
+
 	return addr;
 
 unmap_and_free_vma:
@@ -2305,6 +2320,10 @@ int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
 	if ((vma->vm_flags & VM_ACCOUNT) &&
 	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
 		return -ENOMEM;
+
+	if (vma->vm_file && mmap_uprobe(vma))
+		return -EINVAL;
+
 	vma_link(mm, vma, prev, rb_link, rb_parent);
 	return 0;
 }

mm/mmap.c struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, · line 2384

Quoted 10 lines
mm/mmap.c +4 -0
@@ -2356,6 +2375,10 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
 			new_vma->vm_pgoff = pgoff;
 			if (new_vma->vm_file) {
 				get_file(new_vma->vm_file);
+
+				if (mmap_uprobe(new_vma))
+					goto out_free_mempol;
+
 				if (vma->vm_flags & VM_EXECUTABLE)
 					added_exe_file_vma(mm);
 			}

it's named mmap_uprobe(), which makes it rather single-purpose. The uprobes code wants to track vma life-time so that it can manage uprobes breakpoints installed here, correct?

We already have some other vma tracking goodies in perf itself (see perf_event_mmap() et al) - would it make sense to merge the two vma instrumentation facilities and not burden mm/ with two separate sets of callbacks?

If all such issues are resolved then i guess we could queue up uprobes in -tip, conditional on it remaining sufficiently regression-, problem- and NAK-free.

Also, it would be nice to hear Arnaldo's opinion about the tools/perf/ bits.

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#41 lore raw
Quoted 8 lines · Ingo Molnar
Quoted 3 lines · Srikar Dronamraju

This patchset implements Uprobes which enables you to dynamically probe any routine in a user space application and collect information non-disruptively.

Did all review feedback get addressed in your latest tree?

I think this question would be better answered by Peter, Oleg and Masami. For my part, I have fixed all comments till now. Also uprobes has been part of -next for quite sometime.

Quoted 71 lines

If yes then it would be nice to hear the opinion of Andrew about this bit:

Quoted 1 line
 mm/mmap.c                               |   33 +-

The relevant portion of the patch is:

Quoted 63 lines
mm/mmap.c +22 -0
@@ -30,6 +30,7 @@
 #include <linux/perf_event.h>
 #include <linux/audit.h>
 #include <linux/khugepaged.h>
+#include <linux/uprobes.h>
 
 #include <asm/uaccess.h>
 #include <asm/cacheflush.h>
@@ -616,6 +617,13 @@ again:			remove_next = 1 + (end > next->vm_end);
 	if (mapping)
 		mutex_unlock(&mapping->i_mmap_mutex);
 
+	if (root) {
+		mmap_uprobe(vma);
+
+		if (adjust_next)
+			mmap_uprobe(next);
+	}
+
 	if (remove_next) {
 		if (file) {
 			fput(file);
@@ -637,6 +645,8 @@ again:			remove_next = 1 + (end > next->vm_end);
 			goto again;
 		}
 	}
+	if (insert && file)
+		mmap_uprobe(insert);
 
 	validate_mm(mm);
 
@@ -1329,6 +1339,11 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 			mm->locked_vm += (len >> PAGE_SHIFT);
 	} else if ((flags & MAP_POPULATE) && !(flags & MAP_NONBLOCK))
 		make_pages_present(addr, addr + len);
+
+	if (file && mmap_uprobe(vma))
+		/* matching probes but cannot insert */
+		goto unmap_and_free_vma;
+
 	return addr;
 
 unmap_and_free_vma:
@@ -2305,6 +2320,10 @@ int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
 	if ((vma->vm_flags & VM_ACCOUNT) &&
 	     security_vm_enough_memory_mm(mm, vma_pages(vma)))
 		return -ENOMEM;
+
+	if (vma->vm_file && mmap_uprobe(vma))
+		return -EINVAL;
+
 	vma_link(mm, vma, prev, rb_link, rb_parent);
 	return 0;
 }
@@ -2356,6 +2375,10 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
 			new_vma->vm_pgoff = pgoff;
 			if (new_vma->vm_file) {
 				get_file(new_vma->vm_file);
+
+				if (mmap_uprobe(new_vma))
+					goto out_free_mempol;
Quoted 9 lines

mm/mmap.c struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, · line 2384

Quoted 4 lines
mm/mmap.c +1 -0
+
 				if (vma->vm_flags & VM_EXECUTABLE)
 					added_exe_file_vma(mm);
 			}

it's named mmap_uprobe(), which makes it rather single-purpose. The uprobes code wants to track vma life-time so that it can manage uprobes breakpoints installed here, correct?

Yes,

Quoted 4 lines

We already have some other vma tracking goodies in perf itself (see perf_event_mmap() et al) - would it make sense to merge the two vma instrumentation facilities and not burden mm/ with two separate sets of callbacks?

Atleast for file backed vmas, perf_event_mmap seems to be interested in just the new vma creations. Uprobes would also be interested in the size changes like the vma growing/shrinking/remap. Is perf_event_mmap interested in such changes? From what i could see, perf_event_mmap seems to be interested in stack vma size changes but not file vma size changes.

Also mmap_uprobe gets called in fork path. Currently we have a hook in copy_mm/dup_mm so that we get to know the context of each vma that gets added to the child and add its breakpoints. At dup_mm/dup_mmap we would have taken mmap_sem for both parent and child so there is no way we could have missed a register/unregister in the parent not reflected in the child.

I see the perf_event_fork but that would have to enhanced to do a lot more to help us do a mmap_uprobe.

Quoted 4 lines

If all such issues are resolved then i guess we could queue up uprobes in -tip, conditional on it remaining sufficiently regression-, problem- and NAK-free.

Okay. Accepting uprobes into tip, would provide more testing/feedback.

Quoted 3 lines

Also, it would be nice to hear Arnaldo's opinion about the tools/perf/ bits.

Whatever comments Arnaldo/Masami have given till now have been resolved.

Signature · 2 lines
Thanks and Regards
Srikar
#42 lore raw
Quoted 5 lines · Srikar Dronamraju
Quoted 4 lines

We already have some other vma tracking goodies in perf itself (see perf_event_mmap() et al) - would it make sense to merge the two vma instrumentation facilities and not burden mm/ with two separate sets of callbacks?

Quoted 7 lines

Atleast for file backed vmas, perf_event_mmap seems to be interested in just the new vma creations. Uprobes would also be interested in the size changes like the vma growing/shrinking/remap. Is perf_event_mmap interested in such changes? From what i could see, perf_event_mmap seems to be interested in stack vma size changes but not file vma size changes.

I don't think perf_event_mmap() would be hurt from getting more callbacks, as long as there's enough metadata to differentiate the various events from each other.

( In the long run we really want all such callbacks to be
tracepoints so that we can make them even less intrusive via jump label patching optimizations, etc. - but we are not there yet. )

Quoted 9 lines

Also mmap_uprobe gets called in fork path. Currently we have a hook in copy_mm/dup_mm so that we get to know the context of each vma that gets added to the child and add its breakpoints. At dup_mm/dup_mmap we would have taken mmap_sem for both parent and child so there is no way we could have missed a register/unregister in the parent not reflected in the child.

I see the perf_event_fork but that would have to enhanced to do a lot more to help us do a mmap_uprobe.

Andrew's call i guess.

I did not suggest anything complex or intrusive: just basically unify the namespace, have a single set of callbacks, and call into the uprobes and perf code from those callbacks - out of the sight of MM code.

That unified namespace could be called:

    event_mmap(...);
    event_fork(...);

etc. - and from event_mmap() you could do a simple:

	perf_event_mmap(...)
	uprobes_event_mmap(...)

[ Once all this is updated to use tracepoints it would turn into
a notification callback chain kind of thing. ]

Quoted 4 lines
Quoted 3 lines

If all such issues are resolved then i guess we could queue up uprobes in -tip, conditional on it remaining sufficiently regression-, problem- and NAK-free.

Quoted 8 lines

Okay. Accepting uprobes into tip, would provide more testing/feedback.

Quoted 2 lines

Also, it would be nice to hear Arnaldo's opinion about the tools/perf/ bits.

Whatever comments Arnaldo/Masami have given till now have been resolved.

Please see the probe installation syntax feedback i've sent in the previous mail - that needs to be addressed too.

You should think with the head of an ordinary user-space developer who wants to say debug a library and wants to use uprobes for that. What would he have to type to achieve that and is what he types the minimum number of characters to reasonably achieve that goal?

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#43 lore raw
Quoted 10 lines · Ingo Molnar

I did not suggest anything complex or intrusive: just basically unify the namespace, have a single set of callbacks, and call into the uprobes and perf code from those callbacks - out of the sight of MM code.

That unified namespace could be called:

    event_mmap(...);
    event_fork(...);
Quoted 7 lines

etc. - and from event_mmap() you could do a simple:

	perf_event_mmap(...)
	uprobes_event_mmap(...)

[ Once all this is updated to use tracepoints it would turn into
a notification callback chain kind of thing. ]

We keep disagreeing on this. I utterly loathe hiding stuff in notifier lists. It makes it completely non-obvious who all does what.

Another very good reason to not do what you suggest is that perf_event_mmap() is a pure consumer, it doesn't have a return value, whereas uprobes_mmap() can actually fail the mmap.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#44 lore raw
Quoted 17 lines · Peter Zijlstra
Quoted 15 lines · Ingo Molnar

I did not suggest anything complex or intrusive: just basically unify the namespace, have a single set of callbacks, and call into the uprobes and perf code from those callbacks - out of the sight of MM code.

That unified namespace could be called:

    event_mmap(...);
    event_fork(...);

etc. - and from event_mmap() you could do a simple:

	perf_event_mmap(...)
	uprobes_event_mmap(...)
Quoted 6 lines
Quoted 2 lines

[ Once all this is updated to use tracepoints it would turn into
a notification callback chain kind of thing. ]

We keep disagreeing on this. I utterly loathe hiding stuff in notifier lists. It makes it completely non-obvious who all does what.

My immediate suggestion was not a notifier list but an open-coded list of function calls done in helper inline functions - to minimize the impact of the callbacks on mm/.

Quoted 3 lines

Another very good reason to not do what you suggest is that perf_event_mmap() is a pure consumer, it doesn't have a return value, whereas uprobes_mmap() can actually fail the mmap.

You know that i disagree with that, there is no fundamental reason why event callbacks couldnt participate in program logic, as long as the call site explicitly wants such side effects. It avoids senseless duplication of callbacks.

Anyway, if Andrew is fine with the current callbacks as-is then it's fine to me as well.

Thanks,

Ingo

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>