Skip to contentSkip to conversation

0: Uprobes patchset with perf probe support

PATCH Superseded Srikar Dronamraju 11 participants 124 messages Jun 7, 2011 – Jul 25, 2011

Subsystems (10)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover 0: Uprobes patchset with perf probe support

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

This patchset resolves most of the comments on the previous posting
https://lkml.org/lkml/2011/4/1/176 and inputs I got at LFCS. This patchset applies on top of tip commit 59c5f46fbe01

Uprobes Patches
This patchset implements inode based uprobes which are specified as <file>:<offset> where offset is the offset from start of the map. The probehit overhead is around 3X times the overhead from pid based patchset.

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: 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.

Here is the list of TODO Items.

- Breakpoint handling should co-exist with singlestep/blockstep from another tracer/debugger.
- Queue and dequeue signals delivered from the singlestep till completion of postprocessing.
- 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.

To try please fetch using
git fetch \
git://git.kernel.org/pub/scm/linux/kernel/git/srikar/linux-uprobes.git \
tip_inode_uprobes_070611:tip_inode_uprobes

Please refer "[RFC] [PATCH 3.0-rc2-tip 18/22] tracing: tracing: Uprobe tracer documentation" on how to use uprobe_tracer.

Please refer "[RFC] [PATCH 3.0-rc2-tip 22/22] perf: Documentation for perf uprobes" on how to use uprobe_tracer.

Please do provide your valuable comments.

Thanks in advance.
Srikar

Srikar Dronamraju (22)
0: Uprobes patchset with perf probe support
1: X86 specific breakpoint definitions.
2: uprobes: Breakground page replacement.
3: uprobes: Adding and remove a uprobe in a rb tree.
4: Uprobes: register/unregister probes.
5: x86: analyze instruction and determine fixups.
6: uprobes: store/restore original instruction.
7: uprobes: mmap and fork hooks.
8: x86: architecture specific task information.
9: uprobes: task specific information.
10: uprobes: slot allocation for uprobes
11: uprobes: get the breakpoint address.
12: x86: x86 specific probe handling
13: uprobes: Handing int3 and singlestep exception.
14: x86: uprobes exception notifier for x86.
15: uprobes: register a notifier for uprobes.
16: tracing: Extract out common code for kprobes/uprobes traceevents. 17: tracing: uprobes trace_event interface
18: tracing: Uprobe tracer documentation
19: perf: rename target_module to target
20: perf: perf interface for uprobes
21: perf: show possible probes in a given executable file or library. 22: perf: Documentation for perf uprobes

 Documentation/trace/uprobetrace.txt     |   94 ++
 arch/Kconfig                            |    4 +
 arch/x86/Kconfig                        |    3 +
 arch/x86/include/asm/thread_info.h      |    2 +
 arch/x86/include/asm/uprobes.h          |   53 ++
 arch/x86/kernel/Makefile                |    1 +
 arch/x86/kernel/signal.c                |   14 +
 arch/x86/kernel/uprobes.c               |  591 +++++++++++++
 include/linux/mm_types.h                |    9 +
 include/linux/sched.h                   |    9 +-
 include/linux/uprobes.h                 |  194 ++++
 kernel/Makefile                         |    1 +
 kernel/fork.c                           |   10 +
 kernel/trace/Kconfig                    |   20 +
 kernel/trace/Makefile                   |    2 +
 kernel/trace/trace.h                    |    5 +
 kernel/trace/trace_kprobe.c             |  860 +------------------
 kernel/trace/trace_probe.c              |  752 ++++++++++++++++
 kernel/trace/trace_probe.h              |  160 ++++
 kernel/trace/trace_uprobe.c             |  812 +++++++++++++++++
 kernel/uprobes.c                        | 1476 +++++++++++++++++++++++++++++++
 mm/mmap.c                               |    6 +
 tools/perf/Documentation/perf-probe.txt |   21 +-
 tools/perf/builtin-probe.c              |   77 ++-
 tools/perf/util/probe-event.c           |  431 ++++++++--
 tools/perf/util/probe-event.h           |   12 +-
 tools/perf/util/symbol.c                |   10 +-
 tools/perf/util/symbol.h                |    1 +
 28 files changed, 4686 insertions(+), 944 deletions(-)

--
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>

#1 lore raw

PATCH v4 1/22 1: X86 specific breakpoint definitions.

Provides definitions for the breakpoint instruction and x86 specific uprobe info structure.

---
 arch/x86/Kconfig               |    3 +++
 arch/x86/include/asm/uprobes.h |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 0 deletions(-)
 create mode 100644 arch/x86/include/asm/uprobes.h
arch/x86/Kconfig +3 -0
index 30041d8..7c843e2 100644
@@ -243,6 +243,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 +40 -0
new file mode 100644
index 0000000..192ba4a
@@ -0,0 +1,40 @@
+#ifndef _ASM_UPROBES_H
+#define _ASM_UPROBES_H
+/*
+ * Userspace Probes (UProbes) for x86
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright (C) IBM Corporation, 2008-2011
+ * Authors:
+ *	Srikar Dronamraju
+ *	Jim Keniston
+ */
+
+typedef u8 uprobe_opcode_t;
+#define MAX_UINSN_BYTES 16
+#define UPROBES_XOL_SLOT_BYTES	128	/* to keep it cache aligned */
+
+#define UPROBES_BKPT_INSN 0xcc
+#define UPROBES_BKPT_INSN_SIZE 1
+
+#ifdef CONFIG_X86_64
+struct uprobe_arch_info {
+	unsigned long rip_rela_target_address;
+};
+#else
+struct uprobe_arch_info {};
+#endif
+#endif	/* _ASM_UPROBES_H */

--
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

PATCH v4 2/22 2: uprobes: Breakground page replacement.

In reply to Srikar Dronamraju

Provides Background page replacement by
- cow the page that needs replacement.
- modify a copy of the cowed page.
- replace the cow page with the modified page
- flush the page tables.

Also provides additional routines to read an opcode from a given virtual address and for verifying if a instruction is a breakpoint instruction.

---
 arch/Kconfig            |   12 ++
 include/linux/uprobes.h |   81 ++++++++++++
 kernel/Makefile         |    1 
 kernel/uprobes.c        |  309 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 403 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/uprobes.h
 create mode 100644 kernel/uprobes.c
arch/Kconfig +12 -0
index 26b0e23..3d91687 100644
@@ -61,6 +61,18 @@ config OPTPROBES
 	depends on KPROBES && HAVE_OPTPROBES
 	depends on !PREEMPT
 
+config UPROBES
+	bool "User-space probes (EXPERIMENTAL)"
+	depends on ARCH_SUPPORTS_UPROBES
+	depends on MMU
+	select MM_OWNER
+	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
include/linux/uprobes.h new file +81 -0

81 lines not on this page · raw message

kernel/Makefile +1 -0
index 2d64cfc..2ca229a 100644
@@ -107,6 +107,7 @@ obj-$(CONFIG_PERF_EVENTS) += events/
 obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o
 obj-$(CONFIG_PADATA) += padata.o
 obj-$(CONFIG_CRASH_DUMP) += crash_dump.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 +309 -0

309 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>

maintainer
#3 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

kernel/uprobes.c line 238

Quoted 4 lines · Srikar Dronamraju
kernel/uprobes.c new file +3 -0
+       vaddr_old = kmap_atomic(old_page, KM_USER0);
+       vaddr_new = kmap_atomic(new_page, KM_USER1);
 
+       vaddr_new = kmap_atomic(page, KM_USER0);

again, drop the KM_foo bits, those are obsolete.

--
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 v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 7 lines · Peter Zijlstra

kernel/uprobes.c line 238

Quoted 4 lines · Srikar Dronamraju
kernel/uprobes.c new file +3 -0
+       vaddr_old = kmap_atomic(old_page, KM_USER0);
+       vaddr_new = kmap_atomic(new_page, KM_USER1);
 
+       vaddr_new = kmap_atomic(page, KM_USER0);

again, drop the KM_foo bits, those are obsolete.

oh okay, I had only dropped the KM_foo from kunmap_atomic. Will do the same for kmap_atomic.

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>
maintainer
#5 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

In reply to Srikar Dronamraju

kernel/uprobes.c line 188

Quoted 3 lines · Srikar Dronamraju
kernel/uprobes.c new file +3 -0
+       ret = anon_vma_prepare(vma);
+       if (ret)
+               goto unlock_out;

You just leaked new_page.

kernel/uprobes.c line 201

Quoted 13 lines
kernel/uprobes.c new file +13 -0
+
+       lock_page(new_page);
+       ret = __replace_page(vma, old_page, new_page);
+       unlock_page(new_page);
+       if (ret != 0)
+               page_cache_release(new_page);
+unlock_out:
+       unlock_page(old_page);
+
+put_out:
+       put_page(old_page); /* we did a get_page in the beginning */
+       return ret;
+} 

--
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 v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 6 lines · Peter Zijlstra

kernel/uprobes.c line 188

Quoted 3 lines · Srikar Dronamraju
kernel/uprobes.c new file +3 -0
+       ret = anon_vma_prepare(vma);
+       if (ret)
+               goto unlock_out;

You just leaked new_page.

Right, Will fix this.

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>
maintainer
#7 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

In reply to Srikar Dronamraju

kernel/uprobes.c line 295

Quoted 6 lines · Srikar Dronamraju
kernel/uprobes.c new file +6 -0
+static void print_insert_fail(struct task_struct *tsk,
+                       unsigned long vaddr, const char *why)
+{
+       pr_warn_once("Can't place breakpoint at pid %d vaddr" " %#lx: %s\n",
+                       tsk->pid, vaddr, why);
+} 

Why would we ever want to print this instead of simply failing the operation and returning an error code to userspace?

--
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
#8 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

In reply to Srikar Dronamraju
Quoted 8 lines · Srikar Dronamraju
kernel/uprobes.c new file +7 -0
+       vaddr_old = kmap_atomic(old_page, KM_USER0);
+       vaddr_new = kmap_atomic(new_page, KM_USER1);
+
+       memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
+       /* poke the new insn in, ASSUMES we don't cross page boundary */
+       addr = vaddr;
+       vaddr &= ~PAGE_MASK;

kernel/uprobes.c line 241

Quoted 10 lines
kernel/uprobes.c new file +8 -0
+       memcpy(vaddr_new + vaddr, &opcode, uprobe_opcode_sz);
+
+       kunmap_atomic(vaddr_new);
+       kunmap_atomic(vaddr_old); 
 
 
+       vaddr_new = kmap_atomic(page, KM_USER0);
+       vaddr &= ~PAGE_MASK;
+       memcpy(opcode, vaddr_new + vaddr, uprobe_opcode_sz);
+       kunmap_atomic(vaddr_new);

Both sequences in resp {write,read}_opcode() assume the opcode doesn't cross page boundaries but don't in fact have any assertions validating this assumption.

--
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 v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 22 lines · Peter Zijlstra
Quoted 8 lines · Srikar Dronamraju
kernel/uprobes.c new file +7 -0
+       vaddr_old = kmap_atomic(old_page, KM_USER0);
+       vaddr_new = kmap_atomic(new_page, KM_USER1);
+
+       memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
+       /* poke the new insn in, ASSUMES we don't cross page boundary */
+       addr = vaddr;
+       vaddr &= ~PAGE_MASK;

kernel/uprobes.c line 241

Quoted 10 lines
kernel/uprobes.c new file +8 -0
+       memcpy(vaddr_new + vaddr, &opcode, uprobe_opcode_sz);
+
+       kunmap_atomic(vaddr_new);
+       kunmap_atomic(vaddr_old); 
 
 
+       vaddr_new = kmap_atomic(page, KM_USER0);
+       vaddr &= ~PAGE_MASK;
+       memcpy(opcode, vaddr_new + vaddr, uprobe_opcode_sz);
+       kunmap_atomic(vaddr_new);
Quoted 4 lines

Both sequences in resp {write,read}_opcode() assume the opcode doesn't cross page boundaries but don't in fact have any assertions validating this assumption.

read_opcode and write_opcode reads/writes just one breakpoint instruction I had the below note just above the write_opcode definition.

/*
* NOTE:
* Expect the breakpoint instruction to be the smallest size instruction for
* the architecture. If an arch has variable length instruction and the
* breakpoint instruction is not of the smallest length instruction
* supported by that architecture then we need to modify read_opcode /
* write_opcode accordingly. This would never be a problem for archs that
* have fixed length instructions.
*/

Do we have archs which have a breakpoint instruction which isnt of the smallest instruction size for that arch. If we do have can we change the write_opcode/read_opcode while we support that architecture?

Signature · 2 lines
Thanks and Regards
Srikar
Quoted 2 lines
maintainer
#10 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 32 lines · Srikar Dronamraju
Quoted 22 lines · Peter Zijlstra
Quoted 8 lines · Srikar Dronamraju
kernel/uprobes.c new file +7 -0
+       vaddr_old = kmap_atomic(old_page, KM_USER0);
+       vaddr_new = kmap_atomic(new_page, KM_USER1);
+
+       memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
+       /* poke the new insn in, ASSUMES we don't cross page boundary */
+       addr = vaddr;
+       vaddr &= ~PAGE_MASK;

kernel/uprobes.c line 241

Quoted 10 lines
kernel/uprobes.c new file +8 -0
+       memcpy(vaddr_new + vaddr, &opcode, uprobe_opcode_sz);
+
+       kunmap_atomic(vaddr_new);
+       kunmap_atomic(vaddr_old); 
 
 
+       vaddr_new = kmap_atomic(page, KM_USER0);
+       vaddr &= ~PAGE_MASK;
+       memcpy(opcode, vaddr_new + vaddr, uprobe_opcode_sz);
+       kunmap_atomic(vaddr_new);
Quoted 4 lines

Both sequences in resp {write,read}_opcode() assume the opcode doesn't cross page boundaries but don't in fact have any assertions validating this assumption.

read_opcode and write_opcode reads/writes just one breakpoint instruction I had the below note just above the write_opcode definition.

Quoted 9 lines

/*
* NOTE:
* Expect the breakpoint instruction to be the smallest size instruction for
* the architecture. If an arch has variable length instruction and the
* breakpoint instruction is not of the smallest length instruction
* supported by that architecture then we need to modify read_opcode /
* write_opcode accordingly. This would never be a problem for archs that
* have fixed length instructions.
*/

Whoever reads comments anyway? :-)

Quoted 3 lines

Do we have archs which have a breakpoint instruction which isnt of the smallest instruction size for that arch. If we do have can we change the write_opcode/read_opcode while we support that architecture?

Why not put a simple WARN_ON_ONCE() in there that checks the assumption?

--
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

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 11 lines
Quoted 10 lines

/*
* NOTE:
* Expect the breakpoint instruction to be the smallest size instruction for
* the architecture. If an arch has variable length instruction and the
* breakpoint instruction is not of the smallest length instruction
* supported by that architecture then we need to modify read_opcode /
* write_opcode accordingly. This would never be a problem for archs that
* have fixed length instructions.
*/

Quoted 7 lines

Whoever reads comments anyway? :-)

Quoted 3 lines

Do we have archs which have a breakpoint instruction which isnt of the smallest instruction size for that arch. If we do have can we change the write_opcode/read_opcode while we support that architecture?

Why not put a simple WARN_ON_ONCE() in there that checks the assumption?

Okay, will do.

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>
maintainer
#12 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

In reply to Srikar Dronamraju

kernel/uprobes.c line 65

Quoted 12 lines · Srikar Dronamraju
kernel/uprobes.c new file +12 -0
+/**
+ * __replace_page - replace page in vma by new page.
+ * based on replace_page in mm/ksm.c
+ *
+ * @vma:      vma that holds the pte pointing to page
+ * @page:     the cowed page we are replacing by kpage
+ * @kpage:    the modified page we replace page by
+ *
+ * Returns 0 on success, -EFAULT on failure.
+ */
+static int __replace_page(struct vm_area_struct *vma, struct page *page,
+                                       struct page *kpage)

This is a verbatim copy of mm/ksm.c:replace_page(), I think I can remember why you did this, but the changelog utterly fails to mention why we need a second copy of this logic (or anything much at all).

--
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 v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 18 lines · Peter Zijlstra

kernel/uprobes.c line 65

Quoted 12 lines · Srikar Dronamraju
kernel/uprobes.c new file +12 -0
+/**
+ * __replace_page - replace page in vma by new page.
+ * based on replace_page in mm/ksm.c
+ *
+ * @vma:      vma that holds the pte pointing to page
+ * @page:     the cowed page we are replacing by kpage
+ * @kpage:    the modified page we replace page by
+ *
+ * Returns 0 on success, -EFAULT on failure.
+ */
+static int __replace_page(struct vm_area_struct *vma, struct page *page,
+                                       struct page *kpage)

This is a verbatim copy of mm/ksm.c:replace_page(), I think I can remember why you did this, but the changelog utterly fails to mention why we need a second copy of this logic (or anything much at all).

__replace_page is not exactly a copy of replace_page. Its a slightly modified copy of replace_page. Here are the reasons for having this modified copy instead of using the replace_page.

replace_page was written specifically for ksm purpose by Hugh Dickins. Also Hugh said he doesnt like replace_page to be exposed for other uses. He has plans for further modifying replace_page for ksm specific purposes which might not be aligned to what we are using.

Further for replace_page, its good enuf to call page_add_anon_rmap() However for uprobes needs we need to call page_add_new_anon_rmap(). page_add_new_anon_rmap() will add the page to the right lru list.

replace_page needs a reference to orig_pte while __replace_page doesnt need.

I can add the same to Changelog but I am not sure it makes a good reading. Hence I had skipped it.

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>
maintainer
#14 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

In reply to Srikar Dronamraju
Quoted 18 lines · Srikar Dronamraju
kernel/uprobes.c new file +17 -0
+static int __replace_page(struct vm_area_struct *vma, struct page *page,
+					struct page *kpage)
+{
+	struct mm_struct *mm = vma->vm_mm;
+	pgd_t *pgd;
+	pud_t *pud;
+	pmd_t *pmd;
+	pte_t *ptep;
+	spinlock_t *ptl;
+	unsigned long addr;
+	int err = -EFAULT;
+
+	addr = page_address_in_vma(page, vma);
+	if (addr == -EFAULT)
+		goto out;
+
+	pgd = pgd_offset(mm, addr);

kernel/uprobes.c line 90

Quoted 10 lines
kernel/uprobes.c new file +10 -0
+	if (!pgd_present(*pgd))
+		goto out;
+
+	pud = pud_offset(pgd, addr);
+	if (!pud_present(*pud))
+		goto out;
+
+	pmd = pmd_offset(pud, addr);
+	if (pmd_trans_huge(*pmd) || (!pmd_present(*pmd)))
+		goto out;

Hmm. So it doesn't work with transhuge pages? May be the caller should use __gup(FOLL_SPLIT), otherwise set_bkpt/etc can fail "mysteriously", no? OTOH, I don't really understand how pmd_trans_huge() is possible, valid_vma() checks ->vm_file != NULL and I iiuc transparent hugepages can only work with anonymous mappings. Confused...

But the real problem (afaics) is VM_HUGETLB mappings. I can't understand how __replace_page() can work in this case. Probably valid_vma() should fail if is_vm_hugetlb_page()?

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>

maintainer
#15 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

In reply to Srikar Dronamraju

kernel/uprobes.c line 147

Quoted 12 lines · Srikar Dronamraju
kernel/uprobes.c new file +11 -0
+static int write_opcode(struct task_struct *tsk, struct uprobe * uprobe,
+			unsigned long vaddr, uprobe_opcode_t opcode)
+{
+	struct page *old_page, *new_page;
+	void *vaddr_old, *vaddr_new;
+	struct vm_area_struct *vma;
+	unsigned long addr;
+	int ret;
+
+	/* Read the page with vaddr into memory */
+	ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 1, 1, &old_page, &vma);

Sorry if this was already discussed... But why we are using FOLL_WRITE here? We are not going to write into this page, and this provokes the unnecessary cow, no?

Also. This is called under down_read(mmap_sem), can't we race with access_process_vm() modifying the same memory?

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>

#16 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 7 lines
Quoted 7 lines
kernel/uprobes.c new file +7 -0
+static int write_opcode(struct task_struct *tsk, struct uprobe * uprobe,
+			unsigned long vaddr, uprobe_opcode_t opcode)
+{
+	struct page *old_page, *new_page;
+	void *vaddr_old, *vaddr_new;
+	struct vm_area_struct *vma;
+	unsigned long addr;
Quoted 8 lines

kernel/uprobes.c line 147

Quoted 4 lines
kernel/uprobes.c new file +4 -0
+	int ret;
+
+	/* Read the page with vaddr into memory */
+	ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 1, 1, &old_page, &vma);

Sorry if this was already discussed... But why we are using FOLL_WRITE here? We are not going to write into this page, and this provokes the unnecessary cow, no?

Yes, We are not going to write to the page returned by get_user_pages but a copy of that page. The idea was if we cow the page then we dont need to cow it at the replace_page time and since get_user_pages knows the right way to cow the page, we dont have to write another routine to cow the page.

I am still not clear on your concern.

Is it that we should delay cowing the page to the time we actually write into the page?

or

Is it that we dont need to cow at all if we are replacing a file backed page with anon page?

I think we have to cow the page either at page replacement time or at the beginning. I had tried the option of not cowing the page and it failed but I dont recollect why it failed but back then we used write_protect_page and replace_page from ksm.c

Quoted 3 lines

Also. This is called under down_read(mmap_sem), can't we race with access_process_vm() modifying the same memory?

Yes, we could be racing with access_process_vm on the same memory.

Do we have any other option other than making write_opcode/read_opcode being called under down_write(mmap_sem)? I know that write_opcode worked when we take down_write(mmap_sem). Just that anon_vma_prepare() documents that it should be called under read lock for mmap_sem.

Also Thomas had once asked why we were calling it under down_write. May be race with access_process_vm is a good enough reason to call it with down_write.

Signature · 13 lines
Thanks and Regards
Srikar

-- 
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>
maintainer
#17 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 17 lines · Srikar Dronamraju
Quoted 16 lines

kernel/uprobes.c line 147

Quoted 12 lines
kernel/uprobes.c new file +11 -0
+static int write_opcode(struct task_struct *tsk, struct uprobe * uprobe,
+			unsigned long vaddr, uprobe_opcode_t opcode)
+{
+	struct page *old_page, *new_page;
+	void *vaddr_old, *vaddr_new;
+	struct vm_area_struct *vma;
+	unsigned long addr;
+	int ret;
+
+	/* Read the page with vaddr into memory */
+	ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 1, 1, &old_page, &vma);

Sorry if this was already discussed... But why we are using FOLL_WRITE here? We are not going to write into this page, and this provokes the unnecessary cow, no?

Quoted 2 lines

Yes, We are not going to write to the page returned by get_user_pages but a copy of that page.

Yes I see. But the page returned by get_user_pages(write => 1) is already a cow'ed copy (this mapping should be read-only).

Quoted 2 lines

The idea was if we cow the page then we dont
need to cow it at the replace_page time

Yes, replace_page() shouldn't cow.

Quoted 3 lines

and since get_user_pages knows
the right way to cow the page, we dont have to write another routine to cow the page.

Confused. write_opcode() allocs another page and does memcpy. This is correct, but I don't understand the first cow.

Quoted 1 line

I am still not clear on your concern.

Probably I missed something... but could you please explain why we can't

- ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 1, 1, &old_page, &vma);
+ ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 0, 0, &old_page, &vma);

?

Quoted 7 lines
Quoted 2 lines

Also. This is called under down_read(mmap_sem), can't we race with access_process_vm() modifying the same memory?

Yes, we could be racing with access_process_vm on the same memory.

Do we have any other option other than making write_opcode/read_opcode being called under down_write(mmap_sem)?

I dunno. Probably we can simply ignore this issue, there are other ways to modify this memory.

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>

#18 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 17 lines
Quoted 10 lines
Quoted 7 lines

kernel/uprobes.c line 147

Quoted 3 lines
kernel/uprobes.c new file +3 -0
+
+	/* Read the page with vaddr into memory */
+	ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 1, 1, &old_page, &vma);

Sorry if this was already discussed... But why we are using FOLL_WRITE here? We are not going to write into this page, and this provokes the unnecessary cow, no?

Yes, We are not going to write to the page returned by get_user_pages but a copy of that page.

Yes I see. But the page returned by get_user_pages(write => 1) is already a cow'ed copy (this mapping should be read-only).

Quoted 2 lines

The idea was if we cow the page then we dont
need to cow it at the replace_page time

Quoted 9 lines

Yes, replace_page() shouldn't cow.

Quoted 3 lines

and since get_user_pages knows
the right way to cow the page, we dont have to write another routine to cow the page.

Confused. write_opcode() allocs another page and does memcpy. This is correct, but I don't understand the first cow.

we decided on get_user_pages(FOLL_WRITE|FOLL_FORCE) based on discussions in these threads https://lkml.org/lkml/2010/4/23/327 and
https://lkml.org/lkml/2010/5/12/119

Summary of those two sub-threads as I understand was to have get_user_pages do the "real" cow for us.

If I understand correctly, your concern is on the extra overhead added by the get_user_pages. Other than that is there any side-effect of we forcing the cow through get_user_pages.

Quoted 8 lines
Quoted 1 line

I am still not clear on your concern.

Probably I missed something... but could you please explain why we can't

- ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 1, 1, &old_page, &vma);
+ ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 0, 0, &old_page, &vma);

?

I tried the code with this change and it works for regular cases. I am not sure if it affects cases where programs do mprotect So I am okay to not force cow through get_user_pages.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Also. This is called under down_read(mmap_sem), can't we race with access_process_vm() modifying the same memory?

Quoted 8 lines
Quoted 4 lines

Yes, we could be racing with access_process_vm on the same memory.

Do we have any other option other than making write_opcode/read_opcode being called under down_write(mmap_sem)?

I dunno. Probably we can simply ignore this issue, there are other ways to modify this memory.

Okay.

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#19 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 24 lines · Srikar Dronamraju
Quoted 24 lines
Quoted 11 lines
Quoted 8 lines

kernel/uprobes.c line 147

Quoted 4 lines
kernel/uprobes.c new file +3 -0
+
+	/* Read the page with vaddr into memory */
+	ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 1, 1, &old_page, &vma);

Sorry if this was already discussed... But why we are using FOLL_WRITE here? We are not going to write into this page, and this provokes the unnecessary cow, no?

Yes, We are not going to write to the page returned by get_user_pages but a copy of that page.

Yes I see. But the page returned by get_user_pages(write => 1) is already a cow'ed copy (this mapping should be read-only).

Quoted 2 lines

The idea was if we cow the page then we dont
need to cow it at the replace_page time

Yes, replace_page() shouldn't cow.

Quoted 3 lines

and since get_user_pages knows
the right way to cow the page, we dont have to write another routine to cow the page.

Quoted 7 lines
Quoted 3 lines

Confused. write_opcode() allocs another page and does memcpy. This is correct, but I don't understand the first cow.

we decided on get_user_pages(FOLL_WRITE|FOLL_FORCE) based on discussions in these threads https://lkml.org/lkml/2010/4/23/327 and
https://lkml.org/lkml/2010/5/12/119

Failed to Connect.

Quoted 5 lines

Summary of those two sub-threads as I understand was to have get_user_pages do the "real" cow for us.

If I understand correctly, your concern is on the extra overhead added by the get_user_pages.

No. My main concern is that I do not understand why do we need an extra cow. This is fine, I am not vm expert. But I think it is not fine that you can't explain why your code needs it ;)

What this 'get_user_pages do the "real" cow for us' actually means? It does not do cow for us, __replace_page() does the actual/final cow. It re-installs the modified copy of the page returned by get_user_pages() at the same pte.

Quoted 9 lines
Quoted 6 lines

Probably I missed something... but could you please explain why we can't

- ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 1, 1, &old_page, &vma);
+ ret = get_user_pages(tsk, tsk->mm, vaddr, 1, 0, 0, &old_page, &vma);

?

I tried the code with this change and it works for regular cases. I am not sure if it affects cases where programs do mprotect

Hmm... How can mprotect make a difference? This mapping should be read only, and we are not going to do pte_mkwrite.

Quoted 1 line

So I am okay to not force cow through get_user_pages.

I am okay either way ;) But, imho, if we use FOLL_WRITE|FOLL_FORCE then it would be nice to document why it this needed.

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>

maintainer
#20 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

In reply to Oleg Nesterov
Quoted 3 lines · Oleg Nesterov

Also. This is called under down_read(mmap_sem), can't we race with access_process_vm() modifying the same memory?

Shouldn't matter COW and similar things are serialized using the pte lock.

--
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
#21 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Oleg Nesterov

Also. This is called under down_read(mmap_sem), can't we race with access_process_vm() modifying the same memory?

Shouldn't matter COW and similar things are serialized using the pte lock.

Yes, but afaics this doesn't matter. Suppose that write_opcode() is called when access_process_vm() does copy_to_user_page(). We can cow the page before memcpy() completes.

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>

maintainer
#22 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 7 lines · Oleg Nesterov
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines · Oleg Nesterov

Also. This is called under down_read(mmap_sem), can't we race with access_process_vm() modifying the same memory?

Quoted 6 lines
Quoted 2 lines

Shouldn't matter COW and similar things are serialized using the pte lock.

Yes, but afaics this doesn't matter. Suppose that write_opcode() is called when access_process_vm() does copy_to_user_page(). We can cow the page before memcpy() completes.

access_process_vm() will end up doing a FOLL_WRITE itself when copy_to_user_page() is called since write=1 in that case.

At that point we have a COW-race, someone wins, but the other will then return the same page.

At this point further PTRACE pokes can indeed race with the memcpy in write_opcode(). A possible fix would be to lock_page() around copy_to_user_page() (its already done in set_page_dirty_lock(), so pulling it out shouldn't matter much).

--
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
#23 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 16 lines · Peter Zijlstra
Quoted 7 lines · Oleg Nesterov
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines · Oleg Nesterov

Also. This is called under down_read(mmap_sem), can't we race with access_process_vm() modifying the same memory?

Quoted 6 lines
Quoted 2 lines

Shouldn't matter COW and similar things are serialized using the pte lock.

Yes, but afaics this doesn't matter. Suppose that write_opcode() is called when access_process_vm() does copy_to_user_page(). We can cow the page before memcpy() completes.

Quoted 8 lines

access_process_vm() will end up doing a FOLL_WRITE itself when copy_to_user_page() is called since write=1 in that case.

At that point we have a COW-race, someone wins, but the other will then return the same page.

At this point further PTRACE pokes can indeed race with the memcpy in write_opcode().

Currently it can't, write_opcode() does another cow. But that cow can, and this is the same, yes.

Quoted 3 lines

A possible fix would be to lock_page() around copy_to_user_page() (its already done in set_page_dirty_lock(), so pulling it out shouldn't matter much).

Yes, or write_opcode() could take mmap_sem for writing as Srikar suggests.

But do we really care? Whatever we do we can race with the other updates to this memory. Say, someone can write to vma->vm_file.

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>

maintainer
#24 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

Quoted 1 line · Oleg Nesterov

But do we really care?

probably not :-)

--
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
#25 lore raw

Re: [PATCH v4 3.0-rc2-tip 2/22] 2: uprobes: Breakground page replacement.

In reply to Srikar Dronamraju
Quoted 21 lines · Srikar Dronamraju
kernel/uprobes.c new file +21 -0
+static int __replace_page(struct vm_area_struct *vma, struct page *page,
+                                       struct page *kpage)
+{
+       struct mm_struct *mm = vma->vm_mm;
+       pgd_t *pgd;
+       pud_t *pud;
+       pmd_t *pmd;
+       pte_t *ptep;
+       spinlock_t *ptl;
+       unsigned long addr;
+       int err = -EFAULT;
+
+       addr = page_address_in_vma(page, vma);
+       if (addr == -EFAULT)
+               goto out;
+
+       pgd = pgd_offset(mm, addr);
+       if (!pgd_present(*pgd))
+               goto out;
+
+       pud = pud_offset(pgd, addr);

kernel/uprobes.c line 94

Quoted 10 lines
kernel/uprobes.c new file +10 -0
+       if (!pud_present(*pud))
+               goto out;
+
+       pmd = pmd_offset(pud, addr);
+       if (pmd_trans_huge(*pmd) || (!pmd_present(*pmd)))
+               goto out;
+
+       ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
+       if (!ptep)
+               goto out;

Shouldn't we verify that the obtained pte does indeed refer to our @page here?

Quoted 7 lines
kernel/uprobes.c new file +7 -0
+       get_page(kpage);
+       page_add_new_anon_rmap(kpage, vma, addr);
+
+       flush_cache_page(vma, addr, pte_pfn(*ptep));
+       ptep_clear_flush(vma, addr, ptep);
+       set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
+

kernel/uprobes.c line 112

Quoted 10 lines
kernel/uprobes.c new file +10 -0
+       page_remove_rmap(page);
+       if (!page_mapped(page))
+               try_to_free_swap(page);
+       put_page(page);
+       pte_unmap_unlock(ptep, ptl);
+       err = 0;
+
+out:
+       return err;
+} 

--
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

PATCH v4 3/22 3: uprobes: Adding and remove a uprobe in a rb tree.

In reply to Srikar Dronamraju

Provides interfaces to add and remove uprobes from the global rb tree. Also provides definitions for uprobe_consumer, interfaces to add and remove a consumer to a uprobe. There is a unique uprobe element in the rbtree for each unique inode:offset pair.

Uprobe gets added to the global rb tree when the first consumer for that uprobe gets registered. It gets removed from the tree only when all registered consumers are unregistered.

Multiple consumers can share the same probe. Each consumer provides a filter to limit the tasks on which the handler should run, a handler that runs on probe hit and a value which helps filter callback to limit the tasks on which the handler should run.

---
 include/linux/uprobes.h |   12 +++
 kernel/uprobes.c        |  210 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 222 insertions(+), 0 deletions(-)
include/linux/uprobes.h +12 -0
index 232ccea..9187df3 100644
@@ -23,6 +23,7 @@
  *	Jim Keniston
  */
 
+#include <linux/rbtree.h>
 #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
 #include <asm/uprobes.h>
 #else
@@ -50,6 +51,17 @@ typedef u8 uprobe_opcode_t;
 /* Unexported functions & macros for use by arch-specific code */
 #define uprobe_opcode_sz (sizeof(uprobe_opcode_t))
 
+struct uprobe_consumer {
+	int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
+	/*
+	 * filter is optional; If a filter exists, handler is run
+	 * if and only if filter returns true.
+	 */
+	bool (*filter)(struct uprobe_consumer *self, struct task_struct *task);
+
+	struct uprobe_consumer *next;
+};
+
 /*
  * Most architectures can use the default versions of @read_opcode(),
  * @set_bkpt(), @set_orig_insn(), and @is_bkpt_insn();
kernel/uprobes.c +210 -0

219 lines not on this page · raw message

#27 lore raw

Re: [PATCH v4 3.0-rc2-tip 3/22] 3: uprobes: Adding and remove a uprobe in a rb tree.

Hi Srikar,

Quoted 18 lines · Srikar Dronamraju
kernel/uprobes.c +18 -0
+/* Called with uprobes_treelock held */
+static struct uprobe *__find_uprobe(struct inode * inode,
+			 loff_t offset, struct rb_node **close_match)
+{
+	struct uprobe r = { .inode = inode, .offset = offset };
+	struct rb_node *n = uprobes_tree.rb_node;
+	struct uprobe *uprobe;
+	int match, match_inode;
+
+	while (n) {
+		uprobe = rb_entry(n, struct uprobe, rb_node);
+		match = match_uprobe(uprobe, &r, &match_inode);
+		if (close_match && match_inode)
+			*close_match = n;
+
+		if (!match) {
+			atomic_inc(&uprobe->ref);
+			return uprobe;

kernel/uprobes.c bool __weak is_bkpt_insn(u8 *insn) · line 370

Quoted 10 lines
kernel/uprobes.c +10 -0
+		}
+		if (match < 0)
+			n = n->rb_left;
+		else
+			n = n->rb_right;
+
+	}
+	return NULL;
+}
+

I think there is a simple mistake in the search logic here. In particular, I think the arguments to match_uprobe() should be swapped to give:

match = match_uprobe(&r, uprobe, NULL)

Otherwise, when we do not have an exact match, the next node to be considered is the left child of 'uprobe' even though 'uprobe' is "smaller" than r (and vice versa for the "larger" case).

Quoted 12 lines
kernel/uprobes.c +12 -0
+static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
+{
+	struct rb_node **p = &uprobes_tree.rb_node;
+	struct rb_node *parent = NULL;
+	struct uprobe *u;
+	int match;
+
+	while (*p) {
+		parent = *p;
+		u = rb_entry(parent, struct uprobe, rb_node);
+		match = match_uprobe(u, uprobe, NULL);
+		if (!match) {

kernel/uprobes.c bool __weak is_bkpt_insn(u8 *insn) · line 407

Quoted 10 lines
kernel/uprobes.c +10 -0
+			atomic_inc(&u->ref);
+			return u;
+		}
+
+		if (match < 0)
+			p = &parent->rb_left;
+		else
+			p = &parent->rb_right;
+
+	}

I think the match_uprobe() arguments should be swapped here as well for similar reasons as above.

Also, changing the argument order seems to solve the issue reported by Josh Stone where only the uprobe with the lowest address was responding (thou I did not test with perf, just lightly with the trace_event interface). In particular, iteration using rb_next() appears to work as expected, thus allowing all breakpoints to be registered in mmap_uprobe().

Quoted 7 lines
kernel/uprobes.c +7 -0
+	u = NULL;
+	rb_link_node(&uprobe->rb_node, parent, p);
+	rb_insert_color(&uprobe->rb_node, &uprobes_tree);
+	/* get access + drop ref */
+	atomic_set(&uprobe->ref, 2);
+	return u;
+}
Signature · 8 lines
steve

--
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>
#28 lore raw

Re: [PATCH v4 3.0-rc2-tip 3/22] 3: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 4 lines · Stephen Wilson

Also, changing the argument order seems to solve the issue reported by Josh Stone where only the uprobe with the lowest address was responding (thou I did not test with perf, just lightly with the trace_event interface).

Makes sense, and indeed after swapping the arguments to both calls, the perf test I gave now works as expected. Thanks!

Josh

--
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 v4 3.0-rc2-tip 3/22] 3: uprobes: Adding and remove a uprobe in a rb tree.

Quoted 10 lines · Josh Stone
Quoted 4 lines · Stephen Wilson

Also, changing the argument order seems to solve the issue reported by Josh Stone where only the uprobe with the lowest address was responding (thou I did not test with perf, just lightly with the trace_event interface).

Makes sense, and indeed after swapping the arguments to both calls, the perf test I gave now works as expected. Thanks!

Josh

Thanks Stephen for the fix and Josh for both reporting and confirming that the fix works.

Stephen, Along with the parameter interchange, I also modified the parameter name so that they dont confuse with the argument names in match_uprobe. Otherwise 'r' in __find_uprobe would correspond to 'l' in match_uprobe. The result is something like below.

I am resending the faulty patch with the fix and also checked in the fix into my git tree.

Signature · 2 lines
Thanks and Regards
Srikar
kernel/uprobes.c +3 -3
index 95c16dd..72f21db 100644
@@ -363,14 +363,14 @@ static int match_uprobe(struct uprobe *l, struct uprobe *r, int *match_inode)
 static struct uprobe *__find_uprobe(struct inode * inode,
 			 loff_t offset, struct rb_node **close_match)
 {
-	struct uprobe r = { .inode = inode, .offset = offset };
+	struct uprobe u = { .inode = inode, .offset = offset };
 	struct rb_node *n = uprobes_tree.rb_node;
 	struct uprobe *uprobe;
 	int match, match_inode;
 
 	while (n) {
 		uprobe = rb_entry(n, struct uprobe, rb_node);
-		match = match_uprobe(uprobe, &r, &match_inode);
+		match = match_uprobe(&u, uprobe, &match_inode);
 		if (close_match && match_inode)
 			*close_match = n;
 
@@ -412,7 +412,7 @@ static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
 	while (*p) {
 		parent = *p;
 		u = rb_entry(parent, struct uprobe, rb_node);
-		match = match_uprobe(u, uprobe, NULL);
+		match = match_uprobe(uprobe, u, NULL);
 		if (!match) {
 			atomic_inc(&u->ref);
 			return u;

--
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 v4 3.0-rc2-tip 3/22] 3: uprobes: Adding and remove a uprobe in a rb tree.

In reply to Srikar Dronamraju

Changelog: Including a fix suggested by Stephen Wilson, to fix a small
problem in match_uprobe, fix was to interchange the parameters Problem was reported by Josh Stone. (Thanks Josh, Stephen)

Provides interfaces to add and remove uprobes from the global rb tree. Also provides definitions for uprobe_consumer, interfaces to add and remove a consumer to a uprobe. There is a unique uprobe element in the rbtree for each unique inode:offset pair.

Uprobe gets added to the global rb tree when the first consumer for that uprobe gets registered. It gets removed from the tree only when all registered consumers are unregistered.

Multiple consumers can share the same probe. Each consumer provides a filter to limit the tasks on which the handler should run, a handler that runs on probe hit and a value which helps filter callback to limit the tasks on which the handler should run.

---
 include/linux/uprobes.h |   12 +++
 kernel/uprobes.c        |  210 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 222 insertions(+), 0 deletions(-)
include/linux/uprobes.h +12 -0
index 232ccea..9187df3 100644
@@ -23,6 +23,7 @@
  *	Jim Keniston
  */
 
+#include <linux/rbtree.h>
 #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
 #include <asm/uprobes.h>
 #else
@@ -50,6 +51,17 @@ typedef u8 uprobe_opcode_t;
 /* Unexported functions & macros for use by arch-specific code */
 #define uprobe_opcode_sz (sizeof(uprobe_opcode_t))
 
+struct uprobe_consumer {
+	int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
+	/*
+	 * filter is optional; If a filter exists, handler is run
+	 * if and only if filter returns true.
+	 */
+	bool (*filter)(struct uprobe_consumer *self, struct task_struct *task);
+
+	struct uprobe_consumer *next;
+};
+
 /*
  * Most architectures can use the default versions of @read_opcode(),
  * @set_bkpt(), @set_orig_insn(), and @is_bkpt_insn();
kernel/uprobes.c +210 -0

219 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>

#31 lore raw

PATCH v4 4/22 4: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju

A probe is specified by a file:offset. While registering, a breakpoint is inserted for the first consumer, On subsequent probes, the consumer gets appended to the existing consumers. While unregistering a breakpoint is removed if the consumer happens to be the last consumer. All other unregisterations, the consumer is deleted from the list of consumers.

Probe specifications are maintained in a rb tree. A probe specification is converted into a uprobe before store in a rb tree. A uprobe can be shared by many consumers.

Given a inode, we get a list of mm's that have mapped the inode. However we want to limit the probes to certain processes/threads. The filtering should be at thread level. To limit the probes to a certain processes/threads, we would want to walk through the list of threads whose mm member refer to a given mm.

Here are the options that I thought of:
1. Use mm->owner and walk thro the thread_group of mm->owner, siblings of mm->owner, siblings of parent of mm->owner. This should be good list to traverse. Not sure if this is an exhaustive enough list that all tasks that have a mm set to this mm_struct are walked through.

2. Install probes on all mm's that have mapped the probes and filter only at probe hit time.

3. walk thro do_each_thread; while_each_thread; I think this will catch all tasks that have a mm set to the given mm. However this might be too heavy esp if mm corresponds to a library.

4. add a list_head element to the mm struct and update the list whenever the task->mm thread gets updated. This could mean extending the current mm->owner. However there is some maintainance overhead.

Currently we use the second approach, i.e probe all mm's that have mapped the probes and filter only at probe hit.

---
 include/linux/mm_types.h |    5 +
 include/linux/uprobes.h  |   32 +++++
 kernel/uprobes.c         |  314 ++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 340 insertions(+), 11 deletions(-)
include/linux/mm_types.h +5 -0
index 027935c..7bfef2e 100644
@@ -316,6 +316,11 @@ struct mm_struct {
 #ifdef CONFIG_CPUMASK_OFFSTACK
 	struct cpumask cpumask_allocation;
 #endif
+#ifdef CONFIG_UPROBES
+	unsigned long uprobes_vaddr;
+	struct list_head uprobes_list; /* protected by uprobes_mutex */
+	atomic_t uprobes_count;
+#endif
 };
 
 static inline void mm_init_cpumask(struct mm_struct *mm)
include/linux/uprobes.h +32 -0
index 9187df3..4087cc3 100644
@@ -31,6 +31,7 @@
  * ARCH_SUPPORTS_UPROBES is not defined.
  */
 typedef u8 uprobe_opcode_t;
+struct uprobe_arch_info	{};		/* arch specific info*/
 #endif /* CONFIG_ARCH_SUPPORTS_UPROBES */
 
 /* Post-execution fixups.  Some architectures may define others. */
@@ -62,6 +63,19 @@ struct uprobe_consumer {
 	struct uprobe_consumer *next;
 };
 
+struct uprobe {
+	struct rb_node		rb_node;	/* node in the rb tree */
+	atomic_t		ref;
+	struct rw_semaphore	consumer_rwsem;
+	struct uprobe_arch_info	arch_info;	/* arch specific info if any */
+	struct uprobe_consumer	*consumers;
+	struct inode		*inode;		/* Also hold a ref to inode */
+	loff_t			offset;
+	u8			insn[MAX_UINSN_BYTES];	/* orig instruction */
+	u16			fixups;
+	int			copy;
+};
+
 /*
  * Most architectures can use the default versions of @read_opcode(),
  * @set_bkpt(), @set_orig_insn(), and @is_bkpt_insn();
@@ -90,4 +104,22 @@ struct uprobe_consumer {
  *	the probed instruction stream.  @tskinfo is as for @pre_xol().
  *	You must provide this function.
  */
+
+#ifdef CONFIG_UPROBES
+extern int register_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer);
+extern void unregister_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer);
+#else /* CONFIG_UPROBES is not defined */
+static inline int register_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
+	return -ENOSYS;
+}
+static inline void unregister_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
+}
+
+#endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/uprobes.c +303 -11

323 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>

#32 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Hi Srikar,

Just a few questions/comments inline below.

Quoted 38 lines · Srikar Dronamraju
kernel/uprobes.c +38 -0
+int register_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
+	struct prio_tree_iter iter;
+	struct list_head try_list, success_list;
+	struct address_space *mapping;
+	struct mm_struct *mm, *tmpmm;
+	struct vm_area_struct *vma;
+	struct uprobe *uprobe;
+	int ret = -1;
+
+	if (!inode || !consumer || consumer->next)
+		return -EINVAL;
+
+	if (offset > inode->i_size)
+		return -EINVAL;
+
+	uprobe = alloc_uprobe(inode, offset);
+	if (!uprobe)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&try_list);
+	INIT_LIST_HEAD(&success_list);
+	mapping = inode->i_mapping;
+
+	mutex_lock(&uprobes_mutex);
+	if (uprobe->consumers) {
+		ret = 0;
+		goto consumers_add;
+	}
+
+	mutex_lock(&mapping->i_mmap_mutex);
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+		loff_t vaddr;
+		struct task_struct *tsk;
+
+		if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
+			continue;

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 640

Quoted 10 lines
kernel/uprobes.c +10 -0
+
+		mm = vma->vm_mm;
+		if (!valid_vma(vma)) {
+			mmput(mm);
+			continue;
+		}
+
+		vaddr = vma->vm_start + offset;
+		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
+		if (vaddr < vma->vm_start || vaddr > vma->vm_end) {

This check looks like it is off by one? vma->vm_end is already one byte past the last valid address in the vma, so we should compare using ">=" here I think.

Quoted 102 lines
kernel/uprobes.c +102 -0
+			/* Not in this vma */
+			mmput(mm);
+			continue;
+		}
+		tsk = get_mm_owner(mm);
+		if (tsk && vaddr > TASK_SIZE_OF(tsk)) {
+			/*
+			 * We cannot have a virtual address that is
+			 * greater than TASK_SIZE_OF(tsk)
+			 */
+			put_task_struct(tsk);
+			mmput(mm);
+			continue;
+		}
+		put_task_struct(tsk);
+		mm->uprobes_vaddr = (unsigned long) vaddr;
+		list_add(&mm->uprobes_list, &try_list);
+	}
+	mutex_unlock(&mapping->i_mmap_mutex);
+
+	if (list_empty(&try_list)) {
+		ret = 0;
+		goto consumers_add;
+	}
+	list_for_each_entry_safe(mm, tmpmm, &try_list, uprobes_list) {
+		down_read(&mm->mmap_sem);
+		ret = install_breakpoint(mm, uprobe);
+
+		if (ret && (ret != -ESRCH || ret != -EEXIST)) {
+			up_read(&mm->mmap_sem);
+			break;
+		}
+		if (!ret)
+			list_move(&mm->uprobes_list, &success_list);
+		else {
+			/*
+			 * install_breakpoint failed as there are no active
+			 * threads for the mm; ignore the error.
+			 */
+			list_del(&mm->uprobes_list);
+			mmput(mm);
+		}
+		up_read(&mm->mmap_sem);
+	}
+
+	if (list_empty(&try_list)) {
+		/*
+		 * All install_breakpoints were successful;
+		 * cleanup successful entries.
+		 */
+		ret = 0;
+		list_for_each_entry_safe(mm, tmpmm, &success_list,
+						uprobes_list) {
+			list_del(&mm->uprobes_list);
+			mmput(mm);
+		}
+		goto consumers_add;
+	}
+
+	/*
+	 * Atleast one unsuccessful install_breakpoint;
+	 * remove successful probes and cleanup untried entries.
+	 */
+	list_for_each_entry_safe(mm, tmpmm, &success_list, uprobes_list)
+		remove_breakpoint(mm, uprobe);
+	list_for_each_entry_safe(mm, tmpmm, &try_list, uprobes_list) {
+		list_del(&mm->uprobes_list);
+		mmput(mm);
+	}
+	delete_uprobe(uprobe);
+	goto put_unlock;
+
+consumers_add:
+	add_consumer(uprobe, consumer);
+
+put_unlock:
+	mutex_unlock(&uprobes_mutex);
+	put_uprobe(uprobe); /* drop access ref */
+	return ret;
+}
+
+/*
+ * unregister_uprobe - unregister a already registered probe.
+ * @inode: the file in which the probe has to be removed.
+ * @offset: offset from the start of the file.
+ * @consumer: identify which probe if multiple probes are colocated.
+ */
+void unregister_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
+	struct prio_tree_iter iter;
+	struct list_head tmp_list;
+	struct address_space *mapping;
+	struct mm_struct *mm, *tmpmm;
+	struct vm_area_struct *vma;
+	struct uprobe *uprobe;
+
+	if (!inode || !consumer)
+		return;
+
+	uprobe = find_uprobe(inode, offset);
+	if (!uprobe) {

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 752

Quoted 10 lines
kernel/uprobes.c +10 -0
+		pr_debug("No uprobe found with inode:offset %p %lld\n",
+				inode, offset);
+		return;
+	}
+
+	if (!del_consumer(uprobe, consumer)) {
+		pr_debug("No uprobe found with consumer %p\n",
+				consumer);
+		return;
+	}

When del_consumer() fails dont we still need to do a put_uprobe(uprobe) to drop the extra access ref?

Quoted 19 lines
kernel/uprobes.c +19 -0
+
+	INIT_LIST_HEAD(&tmp_list);
+
+	mapping = inode->i_mapping;
+
+	mutex_lock(&uprobes_mutex);
+	if (uprobe->consumers)
+		goto put_unlock;
+
+	mutex_lock(&mapping->i_mmap_mutex);
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+		struct task_struct *tsk;
+
+		if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
+			continue;
+
+		mm = vma->vm_mm;
+
+		if (!atomic_read(&mm->uprobes_count)) {

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 781

Quoted 10 lines
kernel/uprobes.c +10 -0
+			mmput(mm);
+			continue;
+		}
+
+		if (valid_vma(vma)) {
+			loff_t vaddr;
+
+			vaddr = vma->vm_start + offset;
+			vaddr -= vma->vm_pgoff << PAGE_SHIFT;
+			if (vaddr < vma->vm_start || vaddr > vma->vm_end) {

Same issue with the comparison against vma->vm_end here as well.

Thanks,

Quoted 30 lines
kernel/uprobes.c +30 -0
+				/* Not in this vma */
+				mmput(mm);
+				continue;
+			}
+			tsk = get_mm_owner(mm);
+			if (tsk && vaddr > TASK_SIZE_OF(tsk)) {
+				/*
+				 * We cannot have a virtual address that is
+				 * greater than TASK_SIZE_OF(tsk)
+				 */
+				put_task_struct(tsk);
+				mmput(mm);
+				continue;
+			}
+			put_task_struct(tsk);
+			mm->uprobes_vaddr = (unsigned long) vaddr;
+			list_add(&mm->uprobes_list, &tmp_list);
+		} else
+			mmput(mm);
+	}
+	mutex_unlock(&mapping->i_mmap_mutex);
+	list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list)
+		remove_breakpoint(mm, uprobe);
+
+	delete_uprobe(uprobe);
+
+put_unlock:
+	mutex_unlock(&uprobes_mutex);
+	put_uprobe(uprobe); /* drop access ref */
+}
Signature · 8 lines
steve

--
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 v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 6 lines
Quoted 6 lines
kernel/uprobes.c +6 -0
+
+		mm = vma->vm_mm;
+		if (!valid_vma(vma)) {
+			mmput(mm);
+			continue;
+		}
Quoted 8 lines

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 640

Quoted 4 lines
kernel/uprobes.c +4 -0
+
+		vaddr = vma->vm_start + offset;
+		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
+		if (vaddr < vma->vm_start || vaddr > vma->vm_end) {

This check looks like it is off by one? vma->vm_end is already one byte past the last valid address in the vma, so we should compare using ">=" here I think.

Right, we are off-by one.
Will correct in the next patchset.
Will also correct the other places where we check for vm_end.

Quoted 10 lines

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 752

Quoted 6 lines
kernel/uprobes.c +6 -0
+
+	if (!del_consumer(uprobe, consumer)) {
+		pr_debug("No uprobe found with consumer %p\n",
+				consumer);
+		return;
+	}

When del_consumer() fails dont we still need to do a put_uprobe(uprobe) to drop the extra access ref?

Yes, we need to check and drop the reference.
Will correct in the next patchset.

Quoted 4 lines
Quoted 4 lines
kernel/uprobes.c +4 -0
+
+	INIT_LIST_HEAD(&tmp_list);
+
+	mapping = inode->i_mapping;
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>
maintainer
#34 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju
Quoted 24 lines · Srikar Dronamraju
kernel/uprobes.c +24 -0
+       vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+               loff_t vaddr;
+               struct task_struct *tsk;
+
+               if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
+                       continue;
+
+               mm = vma->vm_mm;
+               if (!valid_vma(vma)) {
+                       mmput(mm);
+                       continue;
+               }
+
+               vaddr = vma->vm_start + offset;
+               vaddr -= vma->vm_pgoff << PAGE_SHIFT;
+               if (vaddr < vma->vm_start || vaddr > vma->vm_end) {
+                       /* Not in this vma */
+                       mmput(mm);
+                       continue;
+               }
+               tsk = get_mm_owner(mm);
+               if (tsk && vaddr > TASK_SIZE_OF(tsk)) {
+                       /*
+                        * We cannot have a virtual address that is

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 658

Quoted 10 lines
kernel/uprobes.c +10 -0
+                        * greater than TASK_SIZE_OF(tsk)
+                        */
+                       put_task_struct(tsk);
+                       mmput(mm);
+                       continue;
+               }
+               put_task_struct(tsk);
+               mm->uprobes_vaddr = (unsigned long) vaddr;
+               list_add(&mm->uprobes_list, &try_list);
+       } 

This still falls flat on its face when there's multiple maps of the same text in one 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>

#35 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 30 lines · Peter Zijlstra
Quoted 29 lines · Srikar Dronamraju
kernel/uprobes.c +29 -0
+       vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+               loff_t vaddr;
+               struct task_struct *tsk;
+
+               if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
+                       continue;
+
+               mm = vma->vm_mm;
+               if (!valid_vma(vma)) {
+                       mmput(mm);
+                       continue;
+               }
+
+               vaddr = vma->vm_start + offset;
+               vaddr -= vma->vm_pgoff << PAGE_SHIFT;
+               if (vaddr < vma->vm_start || vaddr > vma->vm_end) {
+                       /* Not in this vma */
+                       mmput(mm);
+                       continue;
+               }
+               tsk = get_mm_owner(mm);
+               if (tsk && vaddr > TASK_SIZE_OF(tsk)) {
+                       /*
+                        * We cannot have a virtual address that is
+                        * greater than TASK_SIZE_OF(tsk)
+                        */
+                       put_task_struct(tsk);
+                       mmput(mm);
+                       continue;
Quoted 9 lines

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 658

Quoted 5 lines
kernel/uprobes.c +5 -0
+               }
+               put_task_struct(tsk);
+               mm->uprobes_vaddr = (unsigned long) vaddr;
+               list_add(&mm->uprobes_list, &try_list);
+       } 

This still falls flat on its face when there's multiple maps of the same text in one mm.

To address this we will use a uprobe_info structure.

struct uprobe_info {
        unsigned long uprobes_vaddr;
        struct mm_struct *mm;
        struct list_head uprobes_list;
};

and remove the uprobes_list and uprobes_vaddr entries from mm structure.

the uprobe_info structures will be created in the vma_prio_tree_foreach loop as and when required. Since we now have i_mmap_mutex, allocating this uprobe_info structure as when required should be okay.

Agree?

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>
maintainer
#36 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju
Quoted 11 lines · Srikar Dronamraju

+/*
+ * There could be threads that have hit the breakpoint and are entering the
+ * notifier code and trying to acquire the uprobes_treelock. The thread
+ * calling delete_uprobe() that is removing the uprobe from the rb_tree can
+ * race with these threads and might acquire the uprobes_treelock compared
+ * to some of the breakpoint hit threads. In such a case, the breakpoint hit
+ * threads will not find the uprobe. Finding if a "trap" instruction was
+ * present at the interrupting address is racy. Hence provide some extra
+ * time (by way of synchronize_sched() for breakpoint hit threads to acquire
+ * the uprobes_treelock before the uprobe is removed from the rbtree.
+ */

'some' extra time doesn't really sound convincing to me. Either it is sufficient to avoid the race or it is not. It reads to me like: we add a delay so that the race mostly doesn't occur. Not good ;-)

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 572

Quoted 10 lines
kernel/uprobes.c +10 -0
+static void delete_uprobe(struct uprobe *uprobe)
+{
+       unsigned long flags;
+
+       synchronize_sched();
+       spin_lock_irqsave(&uprobes_treelock, flags);
+       rb_erase(&uprobe->rb_node, &uprobes_tree);
+       spin_unlock_irqrestore(&uprobes_treelock, flags);
+       iput(uprobe->inode);
+} 

Also what are the uprobe lifetime rules here? Does it still exist after this returns?

The comment in del_consumer() that says: 'drop creation ref' worries me and makes me thing that is the last reference around and the uprobe will be freed right there, which clearly cannot happen since its not yet removed from the RB-tree.

--
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
#37 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 4 lines · Peter Zijlstra

The comment in del_consumer() that says: 'drop creation ref' worries me and makes me thing that is the last reference around and the uprobe will be freed right there, which clearly cannot happen since its not yet removed from the RB-tree.

I agree about that comment. It scared me too, not only about the RB tree, but the uprobe is used later in that function to drop the write_rwsem.

I think that comment needs to be changed to something like:

/* Have caller drop the creation ref */

-- Steve

--
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 v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

In reply to Peter Zijlstra
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Srikar Dronamraju

+/*
+ * There could be threads that have hit the breakpoint and are entering the
+ * notifier code and trying to acquire the uprobes_treelock. The thread
+ * calling delete_uprobe() that is removing the uprobe from the rb_tree can
+ * race with these threads and might acquire the uprobes_treelock compared
+ * to some of the breakpoint hit threads. In such a case, the breakpoint hit
+ * threads will not find the uprobe. Finding if a "trap" instruction was
+ * present at the interrupting address is racy. Hence provide some extra
+ * time (by way of synchronize_sched() for breakpoint hit threads to acquire
+ * the uprobes_treelock before the uprobe is removed from the rbtree.
+ */

Quoted 3 lines

'some' extra time doesn't really sound convincing to me. Either it is sufficient to avoid the race or it is not. It reads to me like: we add a delay so that the race mostly doesn't occur. Not good ;-)

The extra time provided is sufficient to avoid the race. So will modify it to mean "sufficient" instead of "some".

Quoted 15 lines

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 572

Quoted 11 lines
kernel/uprobes.c +10 -0
+static void delete_uprobe(struct uprobe *uprobe)
+{
+       unsigned long flags;
+
+       synchronize_sched();
+       spin_lock_irqsave(&uprobes_treelock, flags);
+       rb_erase(&uprobe->rb_node, &uprobes_tree);
+       spin_unlock_irqrestore(&uprobes_treelock, flags);
+       iput(uprobe->inode);
+} 

Also what are the uprobe lifetime rules here? Does it still exist after this returns?

Quoted 5 lines

The comment in del_consumer() that says: 'drop creation ref' worries me and makes me thing that is the last reference around and the uprobe will be freed right there, which clearly cannot happen since its not yet removed from the RB-tree.

When del_consumer() is called in unregister_uprobe() it has atleast two (or more if the uprobe is hit) references. One at the creation time and the other thro find_uprobe() called in unregister_uprobe before del_consumer. So the reference lost in del_consumer is never the last reference. I added a commented this as creation reference so that the find_uprobe and the put_uprobe() before return would match.

If the comment is confusing I can delete it or reword it as suggested by Steven Rostedt which is /* Have caller drop the creation ref */

I would prefer to delete the comment.

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>
maintainer
#39 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 4 lines · Srikar Dronamraju

If the comment is confusing I can delete it or reword it as suggested by Steven Rostedt which is /* Have caller drop the creation ref */

I would prefer to delete the comment.

Yeah, just drop the thing.

--
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
#40 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju
Quoted 24 lines · Srikar Dronamraju
kernel/uprobes.c +23 -0
+int register_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
+	struct prio_tree_iter iter;
+	struct list_head try_list, success_list;
+	struct address_space *mapping;
+	struct mm_struct *mm, *tmpmm;
+	struct vm_area_struct *vma;
+	struct uprobe *uprobe;
+	int ret = -1;
+
+	if (!inode || !consumer || consumer->next)
+		return -EINVAL;
+
+	if (offset > inode->i_size)
+		return -EINVAL;
+
+	uprobe = alloc_uprobe(inode, offset);
+	if (!uprobe)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&try_list);
+	INIT_LIST_HEAD(&success_list);

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 625

Quoted 10 lines
kernel/uprobes.c +10 -0
+	mapping = inode->i_mapping;
+
+	mutex_lock(&uprobes_mutex);
+	if (uprobe->consumers) {
+		ret = 0;
+		goto consumers_add;
+	}
+
+	mutex_lock(&mapping->i_mmap_mutex);
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {

I didn't actually read this patch yet, but this looks suspicious. Why begin == end == 0? Doesn't this mean we are ignoring the mappings with vm_pgoff != 0 ?

Perhaps this should be offset >> PAGE_SIZE?

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>

#41 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 40 lines · Oleg Nesterov
Quoted 24 lines · Srikar Dronamraju
kernel/uprobes.c +23 -0
+int register_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
+	struct prio_tree_iter iter;
+	struct list_head try_list, success_list;
+	struct address_space *mapping;
+	struct mm_struct *mm, *tmpmm;
+	struct vm_area_struct *vma;
+	struct uprobe *uprobe;
+	int ret = -1;
+
+	if (!inode || !consumer || consumer->next)
+		return -EINVAL;
+
+	if (offset > inode->i_size)
+		return -EINVAL;
+
+	uprobe = alloc_uprobe(inode, offset);
+	if (!uprobe)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&try_list);
+	INIT_LIST_HEAD(&success_list);

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 625

Quoted 10 lines
kernel/uprobes.c +10 -0
+	mapping = inode->i_mapping;
+
+	mutex_lock(&uprobes_mutex);
+	if (uprobe->consumers) {
+		ret = 0;
+		goto consumers_add;
+	}
+
+	mutex_lock(&mapping->i_mmap_mutex);
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {

I didn't actually read this patch yet, but this looks suspicious. Why begin == end == 0? Doesn't this mean we are ignoring the mappings with vm_pgoff != 0 ?

Quoted 2 lines

Perhaps this should be offset >> PAGE_SIZE?

Okay,
I guess you meant something like this.

	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {

where pgoff == offset >> PAGE_SIZE
Right?

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>
maintainer
#42 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 13 lines · Srikar Dronamraju
Quoted 9 lines · Oleg Nesterov

Review

Quoted 2 lines
(quoted diff) +2 -0
+	mutex_lock(&mapping->i_mmap_mutex);
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {

I didn't actually read this patch yet, but this looks suspicious. Why begin == end == 0? Doesn't this mean we are ignoring the mappings with vm_pgoff != 0 ?

Perhaps this should be offset >> PAGE_SIZE?

Quoted 7 lines

Okay,
I guess you meant something like this.

	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {

where pgoff == offset >> PAGE_SIZE
Right?

Yes, modulo s/PAGE_SIZE/PAGE_SHIFT. But please double check, I can be easily wrong ;)

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>

maintainer
#43 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju

I still didn't actually read this/next patches, but

include/linux/mm_types.h struct mm_struct { · line 320

Quoted 3 lines · Srikar Dronamraju
include/linux/mm_types.h +2 -0
+#ifdef CONFIG_UPROBES
+	unsigned long uprobes_vaddr;

Srikar, I know it is very easy to blame the patches ;) But why does this patch add mm->uprobes_vaddr ? Look, it is write-only, register/unregister do

	mm->uprobes_vaddr = (unsigned long) vaddr;

and it is not used otherwise. It is not possible to understand its purpose without reading the next patches. And the code above looks very strange, the next vma can overwrite uprobes_vaddr.

If possible, please try to re-split this series. If uprobes_vaddr is used in 6/22, then this patch should introduce this member. Note that this is only one particular example, there are a lot more.

Quoted 5 lines
kernel/uprobes.c +4 -0
+int register_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
...
+	mutex_lock(&mapping->i_mmap_mutex);

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 634

Quoted 10 lines
kernel/uprobes.c +10 -0
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+		loff_t vaddr;
+		struct task_struct *tsk;
+
+		if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
+			continue;
+
+		mm = vma->vm_mm;
+		if (!valid_vma(vma)) {
+			mmput(mm);

This looks deadlockable. If mmput()->atomic_dec_and_test() succeeds unlink_file_vma() needs the same ->i_mmap_mutex, no?

I think you can simply remove mmput(). Why do you increment ->mm_users in advance? I think you can do this right before list_add(), after all valid_vma/etc checks.

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 644

Quoted 7 lines
kernel/uprobes.c +7 -0
+		vaddr = vma->vm_start + offset;
+		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
+		if (vaddr < vma->vm_start || vaddr > vma->vm_end) {
+			/* Not in this vma */
+			mmput(mm);
+			continue;
+		}

Not sure that "Not in this vma" is possible if we pass the correct pgoff to vma_prio_tree_foreach()... but OK, I forgot everything I knew about vma prio_tree.

So, we verified that vaddr is valid. Then,

Review

Quoted 2 lines
(quoted diff) +2 -0
+		tsk = get_mm_owner(mm);
+		if (tsk && vaddr > TASK_SIZE_OF(tsk)) {

how it it possible to map ->vm_file above TASK_SIZE ?

And why do you need get/put_task_struct? You could simply read TASK_SIZE_OF(tsk) under rcu_read_lock.

Quoted 6 lines
kernel/uprobes.c +5 -0
+void unregister_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
...
+
+	mutex_lock(&mapping->i_mmap_mutex);

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 772

Quoted 10 lines
kernel/uprobes.c +10 -0
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+		struct task_struct *tsk;
+
+		if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
+			continue;
+
+		mm = vma->vm_mm;
+
+		if (!atomic_read(&mm->uprobes_count)) {
+			mmput(mm);

Again, mmput() doesn't look safe.

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 804

Quoted 2 lines
kernel/uprobes.c +2 -0
+	list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list)
+		remove_breakpoint(mm, uprobe);

What if the application, say, unmaps the vma with bkpt before unregister_uprobe() ? Or it can do mprotect(PROT_WRITE), then valid_vma() fails. Probably this is fine, but mm->uprobes_count becomes wrong, no?

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>

#44 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 11 lines

include/linux/mm_types.h struct mm_struct { · line 320

Quoted 3 lines
include/linux/mm_types.h +2 -0
+#ifdef CONFIG_UPROBES
+	unsigned long uprobes_vaddr;

Srikar, I know it is very easy to blame the patches ;) But why does this patch add mm->uprobes_vaddr ? Look, it is write-only, register/unregister do

	mm->uprobes_vaddr = (unsigned long) vaddr;

and it is not used otherwise. It is not possible to understand its purpose

mm->uprobes_vaddr is used in helper routines insert(remove)_breakpoint routines which are just stubs here. mm->uprobes_vaddr caches the vaddr for subsequent use in insert_breakpoint.

I could have moved the mm->uprobes_vaddr to the 6th patch that implemented the insert_breakpoint routine. However at that time I felt that people would comment back saying we do all the checks and get the correct vaddr, but we dont cache it for subsequent use.

I will move adding the uprobes_vaddr initialization to the next patch. Infact I might remove mm->uprobes_vaddr in the subsequent posting.

In one of the previous postings, I had the patches that used the helper routines (like insert_breakpoint) first and then patches for wrapper routines (like register/unregister) followed in the next patch. I was told that it was tough to understand the context in which these helper routines would be called. So I moved to having the wrapper routines with stubs and implementing the stubs later.

Quoted 2 lines

without reading the next patches. And the code above looks very strange, the next vma can overwrite uprobes_vaddr.

For this posting, handling two vmas for the same inode in the same mm
was a TODO. Since you and Peter have raised this I will handle it in the next posting. I will give a brief description of how I plan to implement this in my response to Peter's comments. Please do review and comment to it.

Quoted 15 lines

If possible, please try to re-split this series. If uprobes_vaddr is used in 6/22, then this patch should introduce this member. Note that this is only one particular example, there are a lot more.

Quoted 10 lines
kernel/uprobes.c +9 -0
+int register_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
...
+	mutex_lock(&mapping->i_mmap_mutex);
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+		loff_t vaddr;
+		struct task_struct *tsk;
+
+		if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
Quoted 8 lines

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 634

Quoted 5 lines
kernel/uprobes.c +5 -0
+			continue;
+
+		mm = vma->vm_mm;
+		if (!valid_vma(vma)) {
+			mmput(mm);

This looks deadlockable. If mmput()->atomic_dec_and_test() succeeds unlink_file_vma() needs the same ->i_mmap_mutex, no?

okay,

Quoted 4 lines

I think you can simply remove mmput(). Why do you increment ->mm_users in advance? I think you can do this right before list_add(), after all valid_vma/etc checks.

Okay, will modify as suggested.

Quoted 14 lines

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 644

Quoted 8 lines
kernel/uprobes.c +7 -0
+		vaddr = vma->vm_start + offset;
+		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
+		if (vaddr < vma->vm_start || vaddr > vma->vm_end) {
+			/* Not in this vma */
+			mmput(mm);
+			continue;
+		}

Not sure that "Not in this vma" is possible if we pass the correct pgoff to vma_prio_tree_foreach()... but OK, I forgot everything I knew about vma prio_tree.

I was asked what if the arithmetic to arrive at vaddr would end up not being in the range.

Quoted 6 lines

So, we verified that vaddr is valid. Then,

Review

Quoted 2 lines
(quoted diff) +2 -0
+		tsk = get_mm_owner(mm);
+		if (tsk && vaddr > TASK_SIZE_OF(tsk)) {

how it it possible to map ->vm_file above TASK_SIZE ?

Same as above. I will do a rethink on both of these checks.

Quoted 3 lines

And why do you need get/put_task_struct? You could simply read TASK_SIZE_OF(tsk) under rcu_read_lock.

Yes, for register/unregister case I could have just done the check under rcu_read_lock instead of doing a get/put_task_struct. Since I needed get_mm_owner() for insert/remove_breakpoint, I thought I will reuse it here.

Quoted 11 lines
Quoted 11 lines
kernel/uprobes.c +9 -0
+void unregister_uprobe(struct inode *inode, loff_t offset,
+				struct uprobe_consumer *consumer)
+{
...
+
+	mutex_lock(&mapping->i_mmap_mutex);
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+		struct task_struct *tsk;
+
+		if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
Quoted 8 lines

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 772

Quoted 6 lines
kernel/uprobes.c +6 -0
+			continue;
+
+		mm = vma->vm_mm;
+
+		if (!atomic_read(&mm->uprobes_count)) {
+			mmput(mm);

Again, mmput() doesn't look safe.

Okay, I will increment the mm_users while adding to the list.

Quoted 7 lines

kernel/uprobes.c static bool del_consumer(struct uprobe *uprobe, · line 804

Quoted 3 lines
kernel/uprobes.c +2 -0
+	list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list)
+		remove_breakpoint(mm, uprobe);

What if the application, say, unmaps the vma with bkpt before unregister_uprobe() ? Or it can do mprotect(PROT_WRITE), then valid_vma() fails. Probably this is fine, but mm->uprobes_count becomes wrong, no?

Okay, will add a hook in unmap to keep the mm->uprobes_count sane.

Quoted 3 lines

Oleg.

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#45 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju
Quoted 5 lines · Srikar Dronamraju

1. Use mm->owner and walk thro the thread_group of mm->owner, siblings of mm->owner, siblings of parent of mm->owner. This should be good list to traverse. Not sure if this is an exhaustive enough list that all tasks that have a mm set to this mm_struct are walked through.

As per copy_process():

	/*
	 * Thread groups must share signals as well, and detached threads
	 * can only be started up within the thread group.
	 */
	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
		return ERR_PTR(-EINVAL);

	/*
	 * Shared signal handlers imply shared VM. By way of the above,
	 * thread groups also imply shared VM. Blocking this case allows
	 * for various simplifications in other code.
	 */
	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
		return ERR_PTR(-EINVAL);

CLONE_THREAD implies CLONE_VM, but not the other way around, we therefore would be able to CLONE_VM and not be part of the primary owner's thread group.

This is of course all terribly sad..

--
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>

#46 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 24 lines · Peter Zijlstra
Quoted 5 lines · Srikar Dronamraju

1. Use mm->owner and walk thro the thread_group of mm->owner, siblings of mm->owner, siblings of parent of mm->owner. This should be good list to traverse. Not sure if this is an exhaustive enough list that all tasks that have a mm set to this mm_struct are walked through.

As per copy_process():

	/*
	 * Thread groups must share signals as well, and detached threads
	 * can only be started up within the thread group.
	 */
	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
		return ERR_PTR(-EINVAL);

	/*
	 * Shared signal handlers imply shared VM. By way of the above,
	 * thread groups also imply shared VM. Blocking this case allows
	 * for various simplifications in other code.
	 */
	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
		return ERR_PTR(-EINVAL);
Quoted 5 lines

CLONE_THREAD implies CLONE_VM, but not the other way around, we therefore would be able to CLONE_VM and not be part of the primary owner's thread group.

This is of course all terribly sad..

Agree,

If clone(CLONE_VM) were to be done by a thread_group leader, we can walk thro the siblings of parent of mm->owner.

However if clone(CLONE_VM) were to be done by non thread_group_leader thread, then we dont even seem to add it to the init_task. i.e I dont think we can refer to such a thread even when we walk thro do_each_thread(g,t) { .. } while_each_thread(g,t);

right?

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>
maintainer
#47 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 37 lines · Srikar Dronamraju
Quoted 24 lines · Peter Zijlstra
Quoted 5 lines · Srikar Dronamraju

1. Use mm->owner and walk thro the thread_group of mm->owner, siblings of mm->owner, siblings of parent of mm->owner. This should be good list to traverse. Not sure if this is an exhaustive enough list that all tasks that have a mm set to this mm_struct are walked through.

As per copy_process():

	/*
	 * Thread groups must share signals as well, and detached threads
	 * can only be started up within the thread group.
	 */
	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
		return ERR_PTR(-EINVAL);

	/*
	 * Shared signal handlers imply shared VM. By way of the above,
	 * thread groups also imply shared VM. Blocking this case allows
	 * for various simplifications in other code.
	 */
	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
		return ERR_PTR(-EINVAL);
Quoted 5 lines

CLONE_THREAD implies CLONE_VM, but not the other way around, we therefore would be able to CLONE_VM and not be part of the primary owner's thread group.

This is of course all terribly sad..

Agree,

If clone(CLONE_VM) were to be done by a thread_group leader, we can walk thro the siblings of parent of mm->owner.

Quoted 6 lines

However if clone(CLONE_VM) were to be done by non thread_group_leader thread, then we dont even seem to add it to the init_task. i.e I dont think we can refer to such a thread even when we walk thro do_each_thread(g,t) { .. } while_each_thread(g,t);

right?

No, we initialize p->group_leader = p; and only change that for CLONE_THREAD, so a clone without CLONE_THREAD always results in a new thread group leader, which are always added to the init_task list.

Or I'm now confused, which isn't at all impossible with that code ;-)

--
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>

#48 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 43 lines · Peter Zijlstra
Quoted 42 lines · Srikar Dronamraju
Quoted 24 lines · Peter Zijlstra
Quoted 5 lines · Srikar Dronamraju

1. Use mm->owner and walk thro the thread_group of mm->owner, siblings of mm->owner, siblings of parent of mm->owner. This should be good list to traverse. Not sure if this is an exhaustive enough list that all tasks that have a mm set to this mm_struct are walked through.

As per copy_process():

	/*
	 * Thread groups must share signals as well, and detached threads
	 * can only be started up within the thread group.
	 */
	if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
		return ERR_PTR(-EINVAL);

	/*
	 * Shared signal handlers imply shared VM. By way of the above,
	 * thread groups also imply shared VM. Blocking this case allows
	 * for various simplifications in other code.
	 */
	if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
		return ERR_PTR(-EINVAL);
Quoted 5 lines

CLONE_THREAD implies CLONE_VM, but not the other way around, we therefore would be able to CLONE_VM and not be part of the primary owner's thread group.

This is of course all terribly sad..

Agree,

If clone(CLONE_VM) were to be done by a thread_group leader, we can walk thro the siblings of parent of mm->owner.

However if clone(CLONE_VM) were to be done by non thread_group_leader thread, then we dont even seem to add it to the init_task. i.e I dont think we can refer to such a thread even when we walk thro do_each_thread(g,t) { .. } while_each_thread(g,t);

Quoted 6 lines
Quoted 1 line

right?

No, we initialize p->group_leader = p; and only change that for CLONE_THREAD, so a clone without CLONE_THREAD always results in a new thread group leader, which are always added to the init_task list.

Ahh .. I missed the p->group_leader = p thing.

In which case, shouldnt traversing all the tasks of all siblings of parent of mm->owner should provide us all the the tasks that have linked to mm. Right?

Agree that we can bother about this a little later.

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>
maintainer
#49 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 6 lines · Srikar Dronamraju

Ahh .. I missed the p->group_leader = p thing.

In which case, shouldnt traversing all the tasks of all siblings of parent of mm->owner should provide us all the the tasks that have linked to mm. Right?

Yes, I think so, stopping the hierarchy walk when we find a sibling/child with a different mm.

Quoted 1 line

Agree that we can bother about this a little later.

*nod*

--
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
#50 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju
Quoted 4 lines · Srikar Dronamraju

In which case, shouldnt traversing all the tasks of all siblings of parent of mm->owner should provide us all the the tasks that have linked to mm. Right?

I don't think so.

Even if the initial mm->ovner never exits (iow, mm->owner is never changed), the "deep" CLONE_VM child can be reparented to init if its parent exits.

Quoted 1 line

Agree that we can bother about this a little later.

Agreed.

Oh. We should move ->mm from task_struct to signal_struct, but we need to change the code like get_task_mm(). And then instead of mm->owner we can have mm->processes list. Perhaps. This can be used by zap_threads() too.

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>

#51 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 11 lines · Oleg Nesterov
Quoted 4 lines · Srikar Dronamraju

In which case, shouldnt traversing all the tasks of all siblings of parent of mm->owner should provide us all the the tasks that have linked to mm. Right?

I don't think so.

Even if the initial mm->ovner never exits (iow, mm->owner is never changed), the "deep" CLONE_VM child can be reparented to init if its parent exits.

oh right.

Quoted 9 lines
Quoted 1 line

Agree that we can bother about this a little later.

Agreed.

Oh. We should move ->mm from task_struct to signal_struct, but we need to change the code like get_task_mm(). And then instead of mm->owner we can have mm->processes list. Perhaps. This can be used by zap_threads() too.

Okay.. thats a nice idea.

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>
maintainer
#52 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju
Quoted 3 lines · Srikar Dronamraju

2. Install probes on all mm's that have mapped the probes and filter only at probe hit time.

Right, seems sensible given the alternatives, we could always look at optimizing this later.

--
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
#53 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

In reply to Srikar Dronamraju

Hi Srikar,

I still hope some day I'll find the time to read the whole series ;) Trying to continue from where I have stopped, and it seems that this patch has a couple more problems.

Quoted 11 lines · Srikar Dronamraju

A probe is specified by a file:offset. While registering, a breakpoint is inserted for the first consumer, On subsequent probes, the consumer gets appended to the existing consumers. While unregistering a breakpoint is removed if the consumer happens to be the last consumer. All other unregisterations, the consumer is deleted from the list of consumers.

Probe specifications are maintained in a rb tree. A probe specification is converted into a uprobe before store in a rb tree. A uprobe can be shared by many consumers.

register/unregister logic looks racy...

Supose that uprobe U has a single consumer C and register_uprobe() is called with the same inode/offset, while another thread does unregister(U,C).

- register() calls alloc_uprobe(), finds the entry in rb tree, and increments U->ref. But this doesn't add the new consumer.

- uregister() does del_consumer(), and removes the single consumer C.

then it takes uprobes_mutex, sees uprobe->consumers == NULL and calls delete_uprobe()->rb_erase()

- register() continues, takes uprobes_mutex, re-inserts the breakpoints, finds the new consumer and succeeds.

However, this uprobe is not in rb-tree, it was deleted by unregister.

OTOH. Suppose we add the new uprobe. register()->alloc_uprobe() sets new_uprobe->ref == 2. If something goes wrong after that, register() does delete_uprobe() + put_uprobe(), new_uprobe->ref becomes 1 and we leak this uprobe.

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>

#54 lore raw

Re: [PATCH v4 3.0-rc2-tip 4/22] 4: Uprobes: register/unregister probes.

Quoted 5 lines · Oleg Nesterov

Hi Srikar,

I still hope some day I'll find the time to read the whole series ;) Trying to continue from where I have stopped, and it seems that this patch has a couple more problems.

Thanks for the review and I sincerely hope you find time and that too at the earliest.

Quoted 26 lines · Srikar Dronamraju
Quoted 11 lines

A probe is specified by a file:offset. While registering, a breakpoint is inserted for the first consumer, On subsequent probes, the consumer gets appended to the existing consumers. While unregistering a breakpoint is removed if the consumer happens to be the last consumer. All other unregisterations, the consumer is deleted from the list of consumers.

Probe specifications are maintained in a rb tree. A probe specification is converted into a uprobe before store in a rb tree. A uprobe can be shared by many consumers.

register/unregister logic looks racy...

Supose that uprobe U has a single consumer C and register_uprobe() is called with the same inode/offset, while another thread does unregister(U,C).

- register() calls alloc_uprobe(), finds the entry in rb tree, and increments U->ref. But this doesn't add the new consumer.

- uregister() does del_consumer(), and removes the single consumer C.

Quoted 9 lines

then it takes uprobes_mutex, sees uprobe->consumers == NULL and calls delete_uprobe()->rb_erase()

- register() continues, takes uprobes_mutex, re-inserts the breakpoints, finds the new consumer and succeeds.

However, this uprobe is not in rb-tree, it was deleted by unregister.

Agree,
I will move the alloc_uprobe under the mutex_lock.

On a side_note: As per the current discussions in this thread, I plan to use inode->i_mutex so that we could serialize register/unregister if they are for two different files.

Quoted 7 lines

OTOH. Suppose we add the new uprobe. register()->alloc_uprobe() sets new_uprobe->ref == 2. If something goes wrong after that, register() does delete_uprobe() + put_uprobe(), new_uprobe->ref becomes 1 and we leak this uprobe.

Agree:
yes I will add a put_uprobe() just after delete_uprobe() but just before the goto.
sidenote: Even this code will change based on the discussions we had on this topic. But I will ensure to make the appropriate changes are taken care of.

Signature · 2 lines
Thanks and Regards
Srikar
Quoted 1 line

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>

#55 lore raw

PATCH v4 5/22 5: x86: analyze instruction and determine fixups.

In reply to Srikar Dronamraju

The instruction analysis is based on x86 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.

---
 arch/x86/include/asm/uprobes.h |    2 
 arch/x86/kernel/Makefile       |    1 
 arch/x86/kernel/uprobes.c      |  414 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 417 insertions(+), 0 deletions(-)
 create mode 100644 arch/x86/kernel/uprobes.c
arch/x86/include/asm/uprobes.h +2 -0
index 192ba4a..4295ce0 100644
@@ -37,4 +37,6 @@ struct uprobe_arch_info {
 #else
 struct uprobe_arch_info {};
 #endif
+struct uprobe;
+extern int analyze_insn(struct task_struct *tsk, struct uprobe *uprobe);
 #endif	/* _ASM_UPROBES_H */
arch/x86/kernel/Makefile +1 -0
index cc0469a..8be00b9 100644
@@ -116,6 +116,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 +414 -0

414 lines not on this page · raw message

maintainer
#56 lore raw

Re: [PATCH v4 3.0-rc2-tip 5/22] 5: x86: analyze instruction and determine fixups.

Quoted 9 lines · Srikar Dronamraju
arch/x86/kernel/uprobes.c new file +9 -0
+static void report_bad_prefix(void)
+{
+       pr_warn_once("uprobes does not currently support probing "
+               "instructions with any of the following prefixes: "
+               "cs:, ds:, es:, ss:, lock:\n");
+}
+
+static void report_bad_1byte_opcode(int mode, uprobe_opcode_t op)
+{

arch/x86/kernel/uprobes.c line 209

Quoted 10 lines
arch/x86/kernel/uprobes.c new file +10 -0
+       pr_warn_once("In %d-bit apps, "
+               "uprobes does not currently support probing "
+               "instructions whose first byte is 0x%2.2x\n", mode, op);
+}
+
+static void report_bad_2byte_opcode(uprobe_opcode_t op)
+{
+       pr_warn_once("uprobes does not currently support probing "
+               "instructions with the 2-byte opcode 0x0f 0x%2.2x\n", op);
+} 

I really don't like all that dmesg muck, why not simply fail the op?

This _once stuff is pretty useless too, once you've had them all subsequent probe attempts will not say anything and leave you in the dark anyway.

--
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>

#57 lore raw

PATCH v4 6/22 6: uprobes: store/restore original instruction.

In reply to Srikar Dronamraju

On the first probe insertion, copy the original instruction and opcode. If multiple vmas map the same text area corresponding to an inode, we only need to copy the instruction just once.
The copied instruction is further copied to a designated slot on probe hit. Its also used at the time of probe removal to restore the original instruction.
opcode is used to analyze the instruction and determine the fixups. Determining fixups at probe hit time would result in doing the same operation on every probe hit. Hence Instruction analysis using the opcode is done at probe insertion time.

---
 kernel/uprobes.c |  121 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 114 insertions(+), 7 deletions(-)
kernel/uprobes.c +114 -7
index c6c2f5e..9564a78 100644
@@ -133,6 +133,7 @@ static int write_opcode(struct task_struct *tsk, struct uprobe * uprobe,
 			unsigned long vaddr, uprobe_opcode_t opcode)
 {
 	struct page *old_page, *new_page;
+	struct address_space *mapping;
 	void *vaddr_old, *vaddr_new;
 	struct vm_area_struct *vma;
 	unsigned long addr;
@@ -153,6 +154,18 @@ static int write_opcode(struct task_struct *tsk, struct uprobe * uprobe,
 	if (!valid_vma(vma))
 		goto put_out;
 
+	mapping = uprobe->inode->i_mapping;
+	if (mapping != vma->vm_file->f_mapping)
+		goto put_out;
+
+	addr = vma->vm_start + uprobe->offset;
+	addr -= vma->vm_pgoff << PAGE_SHIFT;
+	if (addr > TASK_SIZE_OF(tsk))
+		goto put_out;
+
+	if (vaddr != (unsigned long) addr)
+		goto put_out;
+
 	/* Allocate a page */
 	new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
 	if (!new_page) {
@@ -171,7 +184,6 @@ static int write_opcode(struct task_struct *tsk, struct uprobe * uprobe,
 
 	memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
 	/* poke the new insn in, ASSUMES we don't cross page boundary */
-	addr = vaddr;
 	vaddr &= ~PAGE_MASK;
 	memcpy(vaddr_new + vaddr, &opcode, uprobe_opcode_sz);
 
@@ -507,6 +519,66 @@ static bool del_consumer(struct uprobe *uprobe,
 	return ret;
 }
 
+static int __copy_insn(struct address_space *mapping,
+			struct vm_area_struct *vma, char *insn,
+			unsigned long nbytes, unsigned long offset)
+{
+	struct file *filp = vma->vm_file;
+	struct page *page;
+	void *vaddr;
+	unsigned long off1;
+	unsigned long idx;
+
+	if (!filp)
+		return -EINVAL;
+
+	idx = (unsigned long) (offset >> PAGE_CACHE_SHIFT);
+	off1 = offset &= ~PAGE_MASK;
+
+	/*
+	 * Ensure that the page that has the original instruction is
+	 * populated and in page-cache.
+	 */
+	page_cache_sync_readahead(mapping, &filp->f_ra, filp, idx, 1);
+	page = grab_cache_page(mapping, idx);
+	if (!page)
+		return -ENOMEM;
+
+	vaddr = kmap_atomic(page, KM_USER0);
+	memcpy(insn, vaddr + off1, nbytes);
+	kunmap_atomic(vaddr);
+	unlock_page(page);
+	page_cache_release(page);
+	return 0;
+}
+
+static int copy_insn(struct uprobe *uprobe, struct vm_area_struct *vma,
+					unsigned long addr)
+{
+	struct address_space *mapping;
+	int bytes;
+	unsigned long nbytes;
+
+	addr &= ~PAGE_MASK;
+	nbytes = PAGE_SIZE - addr;
+	mapping = uprobe->inode->i_mapping;
+
+	/* Instruction at end of binary; copy only available bytes */
+	if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
+		bytes = uprobe->inode->i_size - uprobe->offset;
+	else
+		bytes = MAX_UINSN_BYTES;
+
+	/* Instruction at the page-boundary; copy bytes in second page */
+	if (nbytes < bytes) {
+		if (__copy_insn(mapping, vma, uprobe->insn + nbytes,
+				bytes - nbytes, uprobe->offset + nbytes))
+			return -ENOMEM;
+		bytes = nbytes;
+	}
+	return __copy_insn(mapping, vma, uprobe->insn, bytes, uprobe->offset);
+}
+
 static struct task_struct *get_mm_owner(struct mm_struct *mm)
 {
 	struct task_struct *tsk;
@@ -521,22 +593,57 @@ static struct task_struct *get_mm_owner(struct mm_struct *mm)
 
 static int install_breakpoint(struct mm_struct *mm, struct uprobe *uprobe)
 {
-	int ret = 0;
+	struct task_struct *tsk = get_mm_owner(mm);
+	int ret;
 
-	/*TODO: install breakpoint */
-	if (!ret)
+	if (!tsk)	/* task is probably exiting; bail-out */
+		return -ESRCH;
+
+	if (!uprobe->copy) {
+		struct vm_area_struct *vma = find_vma(mm, mm->uprobes_vaddr);
+
+		ret = copy_insn(uprobe, vma, mm->uprobes_vaddr);
+		if (ret)
+			goto put_return;
+		if (is_bkpt_insn(uprobe->insn)) {
+			print_insert_fail(tsk, mm->uprobes_vaddr,
+				"breakpoint instruction already exists");
+			ret = -EEXIST;
+			goto put_return;
+		}
+		ret = analyze_insn(tsk, uprobe);
+		if (ret) {
+			print_insert_fail(tsk, mm->uprobes_vaddr,
+					"instruction type cannot be probed");
+			goto put_return;
+		}
+		uprobe->copy = 1;
+	}
+
+	ret = set_bkpt(tsk, uprobe, mm->uprobes_vaddr);
+	if (ret < 0)
+		print_insert_fail(tsk, mm->uprobes_vaddr,
+					"failed to insert bkpt instruction");
+	else
 		atomic_inc(&mm->uprobes_count);
+
+put_return:
+	put_task_struct(tsk);
 	return ret;
 }
 
 static int __remove_breakpoint(struct mm_struct *mm, struct uprobe *uprobe)
 {
-	int ret = 0;
+	struct task_struct *tsk = get_mm_owner(mm);
+	int ret;
 
-	/*TODO: remove breakpoint */
+	if (!tsk)	/* task is probably exiting; bail-out */
+		return -ESRCH;
+
+	ret = set_orig_insn(tsk, uprobe, mm->uprobes_vaddr, true);
 	if (!ret)
 		atomic_dec(&mm->uprobes_count);
-
+	put_task_struct(tsk);
 	return ret;
 }
 
#58 lore raw

PATCH v4 7/22 7: uprobes: mmap and fork hooks.

In reply to Srikar Dronamraju

Provides hooks in mmap and fork.

On fork, after the new mm is created, we need to set the count of uprobes. On mmap, check if the mmap region is an executable page and if its a executable page, walk through the rbtree and insert actual breakpoints for already registered probes corresponding to this inode.

---
 include/linux/uprobes.h |   14 ++++++
 kernel/fork.c           |    2 +
 kernel/uprobes.c        |  106 +++++++++++++++++++++++++++++++++++++++++++++++
 mm/mmap.c               |    6 +++
 4 files changed, 127 insertions(+), 1 deletions(-)
include/linux/uprobes.h +13 -1
index 4087cc3..fc2f9d2 100644
@@ -66,6 +66,7 @@ struct uprobe_consumer {
 struct uprobe {
 	struct rb_node		rb_node;	/* node in the rb tree */
 	atomic_t		ref;
+	struct list_head	pending_list;
 	struct rw_semaphore	consumer_rwsem;
 	struct uprobe_arch_info	arch_info;	/* arch specific info if any */
 	struct uprobe_consumer	*consumers;
@@ -110,6 +111,10 @@ extern int register_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer);
 extern void unregister_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer);
+
+struct vm_area_struct;
+extern int mmap_uprobe(struct vm_area_struct *vma);
+extern void dup_mmap_uprobe(struct mm_struct *old_mm, struct mm_struct *mm);
 #else /* CONFIG_UPROBES is not defined */
 static inline int register_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer)
@@ -120,6 +125,13 @@ static inline void unregister_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer)
 {
 }
-
+static inline void dup_mmap_uprobe(struct mm_struct *old_mm,
+		struct mm_struct *mm)
+{
+}
+static inline int mmap_uprobe(struct vm_area_struct *vma)
+{
+	return 0;
+}
 #endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/fork.c +2 -0
index 0276c30..f6c7cb1 100644
@@ -67,6 +67,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>
@@ -423,6 +424,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
 	}
 	/* a new mm has just been created */
 	arch_dup_mmap(oldmm, mm);
+	dup_mmap_uprobe(oldmm, mm);
 	retval = 0;
 out:
 	up_write(&mm->mmap_sem);
kernel/uprobes.c +106 -0

115 lines not on this page · raw message

mm/mmap.c +6 -0
index bbdc9af..3ff312f 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>
@@ -1344,6 +1345,11 @@ out:
 			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:

--
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>

#59 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

kernel/uprobes.c put_unlock: · line 930

Quoted 10 lines · Srikar Dronamraju
kernel/uprobes.c +10 -0
+static void add_to_temp_list(struct vm_area_struct *vma, struct inode *inode,
+		struct list_head *tmp_list)
+{
+	struct uprobe *uprobe;
+	struct rb_node *n;
+	unsigned long flags;
+
+	n = uprobes_tree.rb_node;
+	spin_lock_irqsave(&uprobes_treelock, flags);
+	uprobe = __find_uprobe(inode, 0, &n);

It is valid for a uprobe offset to be zero I guess, so perhaps we need to do a put_uprobe() here when the result of __find_uprobe() is non-null.

Quoted 63 lines
kernel/uprobes.c +63 -0
+	for (; n; n = rb_next(n)) {
+		uprobe = rb_entry(n, struct uprobe, rb_node);
+		if (uprobe->inode != inode)
+			break;
+		list_add(&uprobe->pending_list, tmp_list);
+		continue;
+	}
+	spin_unlock_irqrestore(&uprobes_treelock, flags);
+}
+
+/*
+ * Called from dup_mmap.
+ * called with mm->mmap_sem and old_mm->mmap_sem acquired.
+ */
+void dup_mmap_uprobe(struct mm_struct *old_mm, struct mm_struct *mm)
+{
+	atomic_set(&old_mm->uprobes_count,
+			atomic_read(&mm->uprobes_count));
+}
+
+/*
+ * Called from mmap_region.
+ * called with mm->mmap_sem acquired.
+ *
+ * Return -ve no if we fail to insert probes and we cannot
+ * bail-out.
+ * Return 0 otherwise. i.e :
+ *	- successful insertion of probes
+ *	- no possible probes to be inserted.
+ *	- insertion of probes failed but we can bail-out.
+ */
+int mmap_uprobe(struct vm_area_struct *vma)
+{
+	struct list_head tmp_list;
+	struct uprobe *uprobe, *u;
+	struct mm_struct *mm;
+	struct inode *inode;
+	unsigned long start, pgoff;
+	int ret = 0;
+
+	if (!valid_vma(vma))
+		return ret;	/* Bail-out */
+
+	INIT_LIST_HEAD(&tmp_list);
+
+	mm = vma->vm_mm;
+	inode = vma->vm_file->f_mapping->host;
+	start = vma->vm_start;
+	pgoff = vma->vm_pgoff;
+	__iget(inode);
+
+	up_write(&mm->mmap_sem);
+	mutex_lock(&uprobes_mutex);
+	down_read(&mm->mmap_sem);
+
+	vma = find_vma(mm, start);
+	/* Not the same vma */
+	if (!vma || vma->vm_start != start ||
+			vma->vm_pgoff != pgoff || !valid_vma(vma) ||
+			inode->i_mapping != vma->vm_file->f_mapping)
+		goto mmap_out;
+
+	add_to_temp_list(vma, inode, &tmp_list);

kernel/uprobes.c put_unlock: · line 1003

Quoted 10 lines
kernel/uprobes.c +10 -0
+	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
+		loff_t vaddr;
+
+		list_del(&uprobe->pending_list);
+		if (ret)
+			continue;
+
+		vaddr = vma->vm_start + uprobe->offset;
+		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
+		if (vaddr < vma->vm_start || vaddr > vma->vm_end)

Another place where the check should be "vaddr >= vma->vm_end" I think?

Thanks,

Quoted 21 lines
kernel/uprobes.c +21 -0
+			/* Not in this vma */
+			continue;
+		if (vaddr > TASK_SIZE)
+			/*
+			 * We cannot have a virtual address that is
+			 * greater than TASK_SIZE
+			 */
+			continue;
+		mm->uprobes_vaddr = (unsigned long)vaddr;
+		ret = install_breakpoint(mm, uprobe);
+		if (ret && (ret == -ESRCH || ret == -EEXIST))
+			ret = 0;
+	}
+
+mmap_out:
+	mutex_unlock(&uprobes_mutex);
+	iput(inode);
+	up_read(&mm->mmap_sem);
+	down_write(&mm->mmap_sem);
+	return ret;
+}
Signature · 8 lines
steve

--
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>
#60 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 6 lines
Quoted 6 lines
kernel/uprobes.c +6 -0
+static void add_to_temp_list(struct vm_area_struct *vma, struct inode *inode,
+		struct list_head *tmp_list)
+{
+	struct uprobe *uprobe;
+	struct rb_node *n;
+	unsigned long flags;
Quoted 8 lines

kernel/uprobes.c put_unlock: · line 930

Quoted 4 lines
kernel/uprobes.c +4 -0
+
+	n = uprobes_tree.rb_node;
+	spin_lock_irqsave(&uprobes_treelock, flags);
+	uprobe = __find_uprobe(inode, 0, &n);

It is valid for a uprobe offset to be zero I guess, so perhaps we need to do a put_uprobe() here when the result of __find_uprobe() is non-null.

Right, will check for the result of __find_uprobe and do a put_uprobe(). Will correct this in the next patchset.

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>
maintainer
#61 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

In reply to Srikar Dronamraju

kernel/uprobes.c put_unlock: · line 984

Quoted 3 lines · Srikar Dronamraju
kernel/uprobes.c +3 -0
+       up_write(&mm->mmap_sem);
+       mutex_lock(&uprobes_mutex);
+       down_read(&mm->mmap_sem); 

egads, and all that without a comment explaining why you think that is even remotely sane.

I'm not at all convinced, it would expose the mmap() even though you could still decide to tear it down if this function were to fail, I bet there's some funnies there.

--
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>

#62 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 11 lines · Peter Zijlstra

kernel/uprobes.c put_unlock: · line 984

Quoted 3 lines · Srikar Dronamraju
kernel/uprobes.c +3 -0
+       up_write(&mm->mmap_sem);
+       mutex_lock(&uprobes_mutex);
+       down_read(&mm->mmap_sem); 

egads, and all that without a comment explaining why you think that is even remotely sane.

I'm not at all convinced, it would expose the mmap() even though you could still decide to tear it down if this function were to fail, I bet there's some funnies there.

The problem is with lock ordering. register/unregister operations acquire uprobes_mutex (which serializes register unregister and the mmap_hook) and then holds mmap_sem for read before they insert a breakpoint.

But the mmap hook would be called with mmap_sem held for write. So acquiring uprobes_mutex can result in deadlock. Hence we release the mmap_sem, take the uprobes_mutex and then again hold the mmap_sem.

After we re-acquire the mmap_sem, we do check if the vma is valid.

Do we have better solutions?

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#63 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 14 lines · Srikar Dronamraju
Quoted 11 lines · Peter Zijlstra

kernel/uprobes.c put_unlock: · line 984

Quoted 3 lines · Srikar Dronamraju
kernel/uprobes.c +3 -0
+       up_write(&mm->mmap_sem);
+       mutex_lock(&uprobes_mutex);
+       down_read(&mm->mmap_sem); 

egads, and all that without a comment explaining why you think that is even remotely sane.

I'm not at all convinced, it would expose the mmap() even though you could still decide to tear it down if this function were to fail, I bet there's some funnies there.

Quoted 8 lines

The problem is with lock ordering. register/unregister operations acquire uprobes_mutex (which serializes register unregister and the mmap_hook) and then holds mmap_sem for read before they insert a breakpoint.

But the mmap hook would be called with mmap_sem held for write. So acquiring uprobes_mutex can result in deadlock. Hence we release the mmap_sem, take the uprobes_mutex and then again hold the mmap_sem.

Sure, I saw why you wanted to do it, I'm just not quite convinced its safe to do and something like this definitely wants a comment explaining why its safe to drop mmap_sem.

Quoted 1 line

After we re-acquire the mmap_sem, we do check if the vma is valid.

But you don't on the return path, and if !ret mmap_region():unmap_and_free_vma will be touching vma again to remove it.

Quoted 1 line

Do we have better solutions?

/me kicks the brain into gear and walks off to get a fresh cup of tea.

So the reason we take uprobes_mutex there is to avoid probes from going away while you're installing them, right?

So we start by doing this add_to_temp_list() thing (horrid name), which iterates the probes on this inode under uprobes_treelock and adds them to a list.

Then we iterate the list, installing the probles.

How about we make the initial pass under uprobes_treelock take a references on the probe, and then after install_breakpoint() succeeds we again take uprobes_treelock and validate the uprobe still exists in the tree and drop the extra reference, if not we simply remove the breakpoint again and continue like it never existed.

That should avoid the need to take uprobes_mutex and not require dropping mmap_sem, right?

--
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>

#64 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 28 lines · Peter Zijlstra
Quoted 14 lines · Srikar Dronamraju
Quoted 11 lines · Peter Zijlstra

kernel/uprobes.c put_unlock: · line 984

Quoted 3 lines · Srikar Dronamraju
kernel/uprobes.c +3 -0
+       up_write(&mm->mmap_sem);
+       mutex_lock(&uprobes_mutex);
+       down_read(&mm->mmap_sem); 

egads, and all that without a comment explaining why you think that is even remotely sane.

I'm not at all convinced, it would expose the mmap() even though you could still decide to tear it down if this function were to fail, I bet there's some funnies there.

Quoted 8 lines

The problem is with lock ordering. register/unregister operations acquire uprobes_mutex (which serializes register unregister and the mmap_hook) and then holds mmap_sem for read before they insert a breakpoint.

But the mmap hook would be called with mmap_sem held for write. So acquiring uprobes_mutex can result in deadlock. Hence we release the mmap_sem, take the uprobes_mutex and then again hold the mmap_sem.

Sure, I saw why you wanted to do it, I'm just not quite convinced its safe to do and something like this definitely wants a comment explaining why its safe to drop mmap_sem.

Quoted 6 lines
Quoted 1 line

After we re-acquire the mmap_sem, we do check if the vma is valid.

But you don't on the return path, and if !ret mmap_region():unmap_and_free_vma will be touching vma again to remove it.

Agree.

Quoted 6 lines
Quoted 1 line

Do we have better solutions?

/me kicks the brain into gear and walks off to get a fresh cup of tea.

So the reason we take uprobes_mutex there is to avoid probes from going away while you're installing them, right?

It serializes register/unregister/mmap operations.

Quoted 7 lines

So we start by doing this add_to_temp_list() thing (horrid name), which iterates the probes on this inode under uprobes_treelock and adds them to a list.

Then we iterate the list, installing the probles.

Quoted 8 lines

How about we make the initial pass under uprobes_treelock take a references on the probe, and then after install_breakpoint() succeeds we again take uprobes_treelock and validate the uprobe still exists in the tree and drop the extra reference, if not we simply remove the breakpoint again and continue like it never existed.

That should avoid the need to take uprobes_mutex and not require dropping mmap_sem, right?

Now since a register and mmap operations can run in parallel, we could have subtle race conditions like this:

1. register_uprobe inserts the uprobe in RB tree.
2. register_uprobe loops thro vmas and inserts breakpoints.

3. mmap is called for same inode, mmap_uprobe() takes reference;
4. mmap completes insertion and releases reference.

5. register uprobe tries to install breakpoint on one vma fails and not due to -ESRCH or -EEXIST.
6. register_uprobe rolls back all install breakpoints except the one inserted by mmap.

We end up with breakpoints that we have inserted by havent cleared.

Similarly unregister_uprobe might be looping to remove the breakpoints when mmap comes in installs the breakpoint and returns. unregister_uprobe might erase the uprobe from rbtree after mmap is done.

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>
maintainer
#65 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 14 lines · Srikar Dronamraju

Now since a register and mmap operations can run in parallel, we could have subtle race conditions like this:

1. register_uprobe inserts the uprobe in RB tree.
2. register_uprobe loops thro vmas and inserts breakpoints.

3. mmap is called for same inode, mmap_uprobe() takes reference;
4. mmap completes insertion and releases reference.

5. register uprobe tries to install breakpoint on one vma fails and not due to -ESRCH or -EEXIST.
6. register_uprobe rolls back all install breakpoints except the one inserted by mmap.

Quoted 5 lines

We end up with breakpoints that we have inserted by havent cleared.

Similarly unregister_uprobe might be looping to remove the breakpoints when mmap comes in installs the breakpoint and returns. unregister_uprobe might erase the uprobe from rbtree after mmap is done.

Well yes, but that's mostly because of how you use those lists.

int __register_uprobe(...)
{
uprobe = alloc_uprobe(...); // find or insert in tree

  vma_prio_tree_foreach(..) {
    // get mm ref, add to list blah blah
  }

  list_for_each_entry_safe() {
    // del from list etc..
    down_read(mm->mmap_sem);
    ret = install_breakpoint();
    if (ret && (ret != -ESRCH || ret != -EEXIST)) {
      up_read(..);
      goto fail;
  }

return 0;

fail:

  list_for_each_entry_safe() {
    // del from list, put mm

}

return ret;
}

void __unregister_uprobe(...)
{
uprobe = find_uprobe(); // ref++
if (delete_consumer(...)); // includes tree removal on last consumer
// implies we own the last ref
return; // consumers

  vma_prio_tree_foreach() {
     // create list
  }
  list_for_each_entry_safe() {
    // remove from list
    remove_breakpoint(); // unconditional, if it wasn't there
                         // its a nop anyway, can't get any new
                         // new probes on account of holding
                         // uprobes_mutex and mmap() doesn't see
                         // it due to tree removal.

}
}

int register_uprobe(...)
{
int ret;

  mutex_lock(&uprobes_mutex);
  ret = __register_uprobe(...);
  if (!ret)
    __unregister_uprobe(...);
  mutex_unlock(&uprobes_mutex);

ret;
}

int mmap_uprobe(...)
{
spin_lock(&uprobes_treelock);

  for_each_probe_in_inode() {
    // create list;

}
spin_unlock(..);

  list_for_each_entry_safe() {
    // remove from list
    ret = install_breakpoint();
    if (ret)
      goto fail;
    if (!uprobe_still_there()) // takes treelock
      remove_breakpoint();
  }

return 0;

fail:

  list_for_each_entry_safe() {
    // destroy list

}
return ret;
}

Should work I think, no?

--
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
#66 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 38 lines · Peter Zijlstra

int __register_uprobe(...)
{
uprobe = alloc_uprobe(...); // find or insert in tree

  vma_prio_tree_foreach(..) {
    // get mm ref, add to list blah blah
  }

  list_for_each_entry_safe() {
    // del from list etc..
    down_read(mm->mmap_sem);
    ret = install_breakpoint();
    if (ret && (ret != -ESRCH || ret != -EEXIST)) {
      up_read(..);
      goto fail;
  }

return 0;

fail:

  list_for_each_entry_safe() {
    // del from list, put mm

}

return ret;
}

void __unregister_uprobe(...)
{
uprobe = find_uprobe(); // ref++
if (delete_consumer(...)); // includes tree removal on last consumer
// implies we own the last ref
return; // consumers

  vma_prio_tree_foreach() {
     // create list
  }
Quoted 8 lines
  list_for_each_entry_safe() {
    // remove from list
    remove_breakpoint(); // unconditional, if it wasn't there
                         // its a nop anyway, can't get any new
                         // new probes on account of holding
                         // uprobes_mutex and mmap() doesn't see
                         // it due to tree removal.

}

put_uprobe(); // last ref, *poof*

Quoted 32 lines

}

int register_uprobe(...)
{
int ret;

  mutex_lock(&uprobes_mutex);
  ret = __register_uprobe(...);
  if (!ret)
    __unregister_uprobe(...);
  mutex_unlock(&uprobes_mutex);

ret;
}

int mmap_uprobe(...)
{
spin_lock(&uprobes_treelock);

  for_each_probe_in_inode() {
    // create list;

}
spin_unlock(..);

  list_for_each_entry_safe() {
    // remove from list
    ret = install_breakpoint();
    if (ret)
      goto fail;
    if (!uprobe_still_there()) // takes treelock
      remove_breakpoint();
  }
Quoted 8 lines

return 0;

fail:

  list_for_each_entry_safe() {
    // destroy list

}
return ret;
}

--
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>

#67 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

In reply to Peter Zijlstra
Quoted 19 lines

void __unregister_uprobe(...)
{
uprobe = find_uprobe(); // ref++
if (delete_consumer(...)); // includes tree removal on last consumer
// implies we own the last ref
return; // consumers

  vma_prio_tree_foreach() {
     // create list
  }
  list_for_each_entry_safe() {
    // remove from list
    remove_breakpoint(); // unconditional, if it wasn't there
                         // its a nop anyway, can't get any new
                         // new probes on account of holding
                         // uprobes_mutex and mmap() doesn't see
                         // it due to tree removal.
Quoted 3 lines

}
}

This would have a bigger race.
A breakpoint might be hit by which time the node is removed and we have no way to find out the uprobe. So we deliver an extra TRAP to the app.

Quoted 17 lines

int mmap_uprobe(...)
{
spin_lock(&uprobes_treelock);

  for_each_probe_in_inode() {
    // create list;

}
spin_unlock(..);

  list_for_each_entry_safe() {
    // remove from list
    ret = install_breakpoint();
    if (ret)
      goto fail;
    if (!uprobe_still_there()) // takes treelock
      remove_breakpoint();
  }
Quoted 9 lines

return 0;

fail:

  list_for_each_entry_safe() {
    // destroy list

}
return ret;
}

register_uprobe will race with mmap_uprobe's first pass. So we might end up with a vma that doesnot have a breakpoint inserted but inserted in all other vma that map to the same inode.

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>
maintainer
#68 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 23 lines · Srikar Dronamraju
Quoted 19 lines

void __unregister_uprobe(...)
{
uprobe = find_uprobe(); // ref++
if (delete_consumer(...)); // includes tree removal on last consumer
// implies we own the last ref
return; // consumers

  vma_prio_tree_foreach() {
     // create list
  }
  list_for_each_entry_safe() {
    // remove from list
    remove_breakpoint(); // unconditional, if it wasn't there
                         // its a nop anyway, can't get any new
                         // new probes on account of holding
                         // uprobes_mutex and mmap() doesn't see
                         // it due to tree removal.
Quoted 3 lines

}
}

Quoted 4 lines

This would have a bigger race.
A breakpoint might be hit by which time the node is removed and we have no way to find out the uprobe. So we deliver an extra TRAP to the app.

Gah indeed. Back to the drawing board for me.

Quoted 28 lines
Quoted 17 lines

int mmap_uprobe(...)
{
spin_lock(&uprobes_treelock);

  for_each_probe_in_inode() {
    // create list;

}
spin_unlock(..);

  list_for_each_entry_safe() {
    // remove from list
    ret = install_breakpoint();
    if (ret)
      goto fail;
    if (!uprobe_still_there()) // takes treelock
      remove_breakpoint();
  }
Quoted 9 lines

return 0;

fail:

  list_for_each_entry_safe() {
    // destroy list

}
return ret;
}

Quoted 3 lines

register_uprobe will race with mmap_uprobe's first pass. So we might end up with a vma that doesnot have a breakpoint inserted but inserted in all other vma that map to the same inode.

I'm not seeing this though, if mmap_uprobe() is before register_uprobe() inserts the probe in the tree, the vma is already in the rmap and register_uprobe() will find it in its vma walk. If its after, mmap_uprobe() will find it and install, if a concurrent register_uprobe()'s vma walk also finds it, it will -EEXISTS and ignore the error.

--
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>

#69 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 24 lines · Peter Zijlstra
Quoted 23 lines · Srikar Dronamraju
Quoted 22 lines

void __unregister_uprobe(...)
{
uprobe = find_uprobe(); // ref++
if (delete_consumer(...)); // includes tree removal on last consumer
// implies we own the last ref
return; // consumers

  vma_prio_tree_foreach() {
     // create list
  }
  list_for_each_entry_safe() {
    // remove from list
    remove_breakpoint(); // unconditional, if it wasn't there
                         // its a nop anyway, can't get any new
                         // new probes on account of holding
                         // uprobes_mutex and mmap() doesn't see
                         // it due to tree removal.

}
}

Quoted 12 lines
Quoted 4 lines

This would have a bigger race.
A breakpoint might be hit by which time the node is removed and we have no way to find out the uprobe. So we deliver an extra TRAP to the app.

Gah indeed. Back to the drawing board for me.

Quoted 5 lines
Quoted 5 lines

int mmap_uprobe(...)
{
spin_lock(&uprobes_treelock);

  for_each_probe_in_inode() {
    // create list;

Here again if we have multiple mmaps for the same inode occuring on two process contexts (I mean two different mm's), we have to manage how we add the same uprobe to more than one list. Atleast my current uprobe->pending_list wouldnt work.

Quoted 27 lines
Quoted 23 lines
Quoted 12 lines
  }
  spin_unlock(..);

  list_for_each_entry_safe() {
    // remove from list
    ret = install_breakpoint();
    if (ret)
      goto fail;
    if (!uprobe_still_there()) // takes treelock
      remove_breakpoint();
  }
Quoted 9 lines

return 0;

fail:

  list_for_each_entry_safe() {
    // destroy list

}
return ret;
}

Quoted 3 lines

register_uprobe will race with mmap_uprobe's first pass. So we might end up with a vma that doesnot have a breakpoint inserted but inserted in all other vma that map to the same inode.

Quoted 7 lines

I'm not seeing this though, if mmap_uprobe() is before register_uprobe() inserts the probe in the tree, the vma is already in the rmap and register_uprobe() will find it in its vma walk. If its after, mmap_uprobe() will find it and install, if a concurrent register_uprobe()'s vma walk also finds it, it will -EEXISTS and ignore the error.

You are right here.

What happens if the register_uprobe comes first and walks around the vmas, Between mmap comes in does the insertion including the second pass and returns. register_uprobe now finds that it cannot insert breakpoint on one of the vmas and hence has to roll-back. The vma on which mmap_uprobe inserted will not be in the list of vmas from which we try to remove the breakpoint.

How about something like this:

/* Change from previous time:
* - add a atomic counter to inode (this is optional)
* - trylock first.
* - take down_write instead of down_read if we drop mmap_sem
* - no releasing mmap_sem second time since we take a down_write.
*/

int mmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	unsigned long start, pgoff;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;     /* Bail-out */

	inode = vma->vm_file->f_mapping->host;
	if (!atomic_read(&inode->uprobes_count))
		return ret;

	INIT_LIST_HEAD(&tmp_list);

	mm = vma->vm_mm;
	start = vma->vm_start;
	pgoff = vma->vm_pgoff;
	__iget(inode);

	if (!mutex_trylock(uprobes_mutex)) {

		/*
		 * Unable to get uprobes_mutex; Probably contending with
		 * someother thread. Drop mmap_sem; acquire uprobes_mutex
		 * and mmap_sem and then verify vma.
		 */

		up_write(&mm->mmap_sem);
		mutex_lock&(uprobes_mutex);
		down_write(&mm->mmap_sem);
		vma = find_vma(mm, start);
		/* Not the same vma */
		if (!vma || vma->vm_start != start ||
				vma->vm_pgoff != pgoff || !valid_vma(vma) ||
				inode->i_mapping != vma->vm_file->f_mapping)
			goto mmap_out;
	}

	add_to_temp_list(vma, inode, &tmp_list);
	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
		loff_t vaddr;

		list_del(&uprobe->pending_list);
		if (ret)
			continue;

		vaddr = vma->vm_start + uprobe->offset;
		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
		if (vaddr < vma->vm_start || vaddr >= vma->vm_end)
			/* Not in this vma */
			continue;
		if (vaddr > TASK_SIZE)
			/*
			 * We cannot have a virtual address that is
			 * greater than TASK_SIZE
			 */
			continue;
		ret = install_breakpoint(mm, uprobe, vaddr);

		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
        }

mmap_out:

	mutex_unlock(&uprobes_mutex);
	iput(inode);
	return ret;
}
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>
maintainer
#70 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 10 lines · Srikar Dronamraju
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

int mmap_uprobe(...)
{
spin_lock(&uprobes_treelock);

  for_each_probe_in_inode() {
    // create list;

Here again if we have multiple mmaps for the same inode occuring on two process contexts (I mean two different mm's), we have to manage how we add the same uprobe to more than one list. Atleast my current uprobe->pending_list wouldnt work.

Sure, wasn't concerned about that particular problem.

Quoted 35 lines
Quoted 27 lines
Quoted 23 lines
Quoted 12 lines
  }
  spin_unlock(..);

  list_for_each_entry_safe() {
    // remove from list
    ret = install_breakpoint();
    if (ret)
      goto fail;
    if (!uprobe_still_there()) // takes treelock
      remove_breakpoint();
  }
Quoted 9 lines

return 0;

fail:

  list_for_each_entry_safe() {
    // destroy list

}
return ret;
}

Quoted 3 lines

register_uprobe will race with mmap_uprobe's first pass. So we might end up with a vma that doesnot have a breakpoint inserted but inserted in all other vma that map to the same inode.

Quoted 7 lines

I'm not seeing this though, if mmap_uprobe() is before register_uprobe() inserts the probe in the tree, the vma is already in the rmap and register_uprobe() will find it in its vma walk. If its after, mmap_uprobe() will find it and install, if a concurrent register_uprobe()'s vma walk also finds it, it will -EEXISTS and ignore the error.

Quoted 8 lines

You are right here.

What happens if the register_uprobe comes first and walks around the vmas, Between mmap comes in does the insertion including the second pass and returns. register_uprobe now finds that it cannot insert breakpoint on one of the vmas and hence has to roll-back. The vma on which mmap_uprobe inserted will not be in the list of vmas from which we try to remove the breakpoint.

Yes it will, remember __register_uprobe() will call __unregister_uprobe() on fail, which does a new vma-rmap walk which will then see the newly added mmap.

Quoted 12 lines

How about something like this:

	if (!mutex_trylock(uprobes_mutex)) {

		/*
		 * Unable to get uprobes_mutex; Probably contending with
		 * someother thread. Drop mmap_sem; acquire uprobes_mutex
		 * and mmap_sem and then verify vma.
		 */

		up_write(&mm->mmap_sem);
		mutex_lock&(uprobes_mutex);
Quoted 8 lines
		down_write(&mm->mmap_sem);
		vma = find_vma(mm, start);
		/* Not the same vma */
		if (!vma || vma->vm_start != start ||
				vma->vm_pgoff != pgoff || !valid_vma(vma) ||
				inode->i_mapping != vma->vm_file->f_mapping)
			goto mmap_out;
	}

Only if we have to, I really don't like dropping mmap_sem in the middle of mmap. I'm fairly sure we can come up with some ordering scheme that ought to make mmap_uprobe() work without the uprobes_mutex.

On thing I was thinking of to fix that initial problem of spurious traps was to leave the uprobe in the tree but skip all probes without consumers in mmap_uprobe().

--
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
#71 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 4 lines · Peter Zijlstra

On thing I was thinking of to fix that initial problem of spurious traps was to leave the uprobe in the tree but skip all probes without consumers in mmap_uprobe().

Can you find fault with using __unregister_uprobe() as a cleanup path for __register_uprobe() so that we do a second vma-rmap walk, and ignoring empty probes on uprobe_mmap()?

We won't get spurious traps because the empty (no consumers) uprobe is still in the tree, we won't get any 'lost' probe insn because the cleanup does a second vma-rmap walk which will include the new mmap(). And double probe insertion is harmless.

--
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>

#72 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Peter Zijlstra

On thing I was thinking of to fix that initial problem of spurious traps was to leave the uprobe in the tree but skip all probes without consumers in mmap_uprobe().

Can you find fault with using __unregister_uprobe() as a cleanup path for __register_uprobe() so that we do a second vma-rmap walk, and ignoring empty probes on uprobe_mmap()?

It gets a little complicated to handle simultaneous mmaps of the same inode/file on different processes.

- Same uprobe cannot be in two different temporary lists at the same time. So we have to serialize the mmap_uprobe hook.

- If we use auxillary structures that refers to uprobes as nodes of tmplist, we dont know how many of them to preallocate. We cannot allocate on demand since we traverse RB tree with uprobes_treelock.

Quoted 6 lines

We won't get spurious traps because the empty (no consumers) uprobe is still in the tree, we won't get any 'lost' probe insn because the cleanup does a second vma-rmap walk which will include the new mmap(). And double probe insertion is harmless.

so I am thinking of a solution that includes most of your ideas along with using i_mmap_mutex in mmap_uprobe path.

/*
Changes:
1. Uses inode->i_mutex instead of uprobes_mutex. (This is optional).
2. Now along with vma rma walk, i_mmap_mutex is even held when we do deletion of uprobes into RB tree.
3. mmap_uprobe takes i_mmap_mutex.
4. inode->uprobes_count ( Again this is optional.)

Advantages:
1. No need to drop mmap_sem.
2. Now register/unregister can run in parallel. (iff we use i_mutex);
3. No need to take extra reference to uprobe in mmap_uprobe(). */

void _unregister_uprobe(...)

{
	if (!del_consumer(...)) {	// includes tree removal on last consumer
		return;
	}
	if (uprobe->consumers)
		return;

	mutex_lock(&inode->i_map_mutex);	//sync with mmap.
	vma_prio_tree_foreach() {
		// create list
	}

	mutex_unlock(&inode->i_map_mutex);

	list_for_each_entry_safe() {
		// remove from list
		down_read(&mm->mmap_sem);
		remove_breakpoint();	// unconditional, if it wasn't there
		up_read(&mm->mmap_sem);
	}

	mutex_lock(&inode->i_mmap_mutex);
	delete_uprobe(uprobe);
	mutex_unlock(&inode->i_mmap_mutex);

	inode->uprobes_count --;
	mutex_unlock(&inode->i_mutex);
}

int register_uprobe(...)

{
	uprobe = alloc_uprobe(...);	// find or insert in tree

	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	if (uprobe->consumers) {
		add_consumer();
		goto put_unlock;
	}
	add_consumer();
	inode->uprobes_count ++;
	mutex_lock(&inode->i_map_mutex);	//sync with mmap.
	vma_prio_tree_foreach(..) {
		// get mm ref, add to list blah blah
	}

	mutex_unlock(&inode->i_map_mutex);
	list_for_each_entry_safe() {
		if (ret) {
			// del from list etc..
			//
			continue;
		}
		down_read(mm->mmap_sem);
		ret = install_breakpoint();
		up_read(..);
		// del from list etc..
		//
		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
	}

	if (ret) {
		_unregister_uprobe();

put_unlock:

	mutex_unlock(&inode->i_mutex);
	put_uprobe(uprobe);
	return ret;
}

void unregister_uprobe(...)

{
	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	uprobe = find_uprobe();	// ref++
	_unregister_uprobe();
	mutex_unlock(&inode->i_mutex);
	put_uprobe(uprobe);
}

int mmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;	/* Bail-out */

	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (inode->uprobes_count)
		return ret;
	__iget(inode);

	INIT_LIST_HEAD(&tmp_list);

	mutex_lock(&inode->i_map_mutex);
	add_to_temp_list(vma, inode, &tmp_list);
	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
		loff_t vaddr;

		list_del(&uprobe->pending_list);
		if (ret)
			continue;

		vaddr = vma->vm_start + uprobe->offset;
		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
		ret = install_breakpoint(mm, uprobe, vaddr);

		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
	}

	mutex_unlock(&inode->i_map_mutex);
	iput(inode);
	return ret;
}

int munmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;	/* Bail-out */

	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (inode->uprobes_count)
		return ret;

// walk thro RB tree and decrement mm->uprobes_count

	walk_rbtree_and_dec_uprobes_count(); //hold treelock.

	return ret;
}
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>
#73 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 4 lines

so I am thinking of a solution that includes most of your ideas along with using i_mmap_mutex in mmap_uprobe path.

Addressing Peter's comments given on irc wrt i_mmap_mutex.

/*
Changes:
1. Uses inode->i_mutex instead of uprobes_mutex.
2. Now along with vma rma walk, i_mmap_mutex is even held when we do deletion of uprobes into RB tree.
3. mmap_uprobe takes i_mmap_mutex.

Advantages:
1. No need to drop mmap_sem.
2. Now register/unregister can run in parallel.
3.
*/

void _unregister_uprobe(...)

{
	if (!del_consumer(...)) {	// includes tree removal on last consumer
		return;
	}
	if (uprobe->consumers)
		return;

	mutex_lock(&mapping->i_mmap_mutex);	//sync with mmap.
	vma_prio_tree_foreach() {
		// create list
	}

	mutex_unlock(&mapping->i_mmap_mutex);

	list_for_each_entry_safe() {
		// remove from list
		down_read(&mm->mmap_sem);
		remove_breakpoint();	// unconditional, if it wasn't there
		up_read(&mm->mmap_sem);
	}

	mutex_lock(&mapping->i_mmap_mutex);
	delete_uprobe(uprobe);
	mutex_unlock(&mapping->i_mmap_mutex);

	inode->uprobes_count--;
	mutex_unlock(&inode->i_mutex);
}

int register_uprobe(...)

{
	uprobe = alloc_uprobe(...);	// find or insert in tree

	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	if (uprobe->consumers) {
		add_consumer();
		goto put_unlock;
	}
	add_consumer();
	inode->uprobes_count++;
	mutex_lock(&mapping->i_mmap_mutex);	//sync with mmap.
	vma_prio_tree_foreach(..) {
		// get mm ref, add to list blah blah
	}

	mutex_unlock(&mapping->i_mmap_mutex);
	list_for_each_entry_safe() {
		if (ret) {
			// del from list etc..
			//
			continue;
		}
		down_read(mm->mmap_sem);
		ret = install_breakpoint();
		up_read(..);
		// del from list etc..
		//
		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
	}

	if (ret)
		_unregister_uprobe();

      put_unlock:
	mutex_unlock(&inode->i_mutex);
	put_uprobe(uprobe);
	return ret;
}

void unregister_uprobe(...)

{
	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	uprobe = find_uprobe();	// ref++
	_unregister_uprobe();
	mutex_unlock(&inode->i_mutex);
	put_uprobe(uprobe);
}

int mmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;	/* Bail-out */

	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (inode->uprobes_count)
		return ret;
	__iget(inode);

	INIT_LIST_HEAD(&tmp_list);

	mutex_lock(&mapping->i_mmap_mutex);
	add_to_temp_list(vma, inode, &tmp_list);
	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
		loff_t vaddr;

		list_del(&uprobe->pending_list);
		if (ret)
			continue;

		vaddr = vma->vm_start + uprobe->offset;
		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
		ret = install_breakpoint(mm, uprobe, vaddr);

		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
	}

	mutex_unlock(&mapping->i_mmap_mutex);
	iput(inode);
	return ret;
}

int munmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;	/* Bail-out */

	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (inode->uprobes_count)
		return ret;

// walk thro RB tree and decrement mm->uprobes_count

	walk_rbtree_and_dec_uprobes_count();	//hold treelock.

	return ret;
}
maintainer
#74 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 28 lines · Srikar Dronamraju
Quoted 4 lines

so I am thinking of a solution that includes most of your ideas along with using i_mmap_mutex in mmap_uprobe path.

Addressing Peter's comments given on irc wrt i_mmap_mutex.

void _unregister_uprobe(...)

{
	if (!del_consumer(...)) {	// includes tree removal on last consumer
		return;
	}
	if (uprobe->consumers)
		return;

	mutex_lock(&mapping->i_mmap_mutex);	//sync with mmap.
	vma_prio_tree_foreach() {
		// create list
	}

	mutex_unlock(&mapping->i_mmap_mutex);

	list_for_each_entry_safe() {
		// remove from list
		down_read(&mm->mmap_sem);
		remove_breakpoint();	// unconditional, if it wasn't there
		up_read(&mm->mmap_sem);
Quoted 8 lines
	}

	mutex_lock(&mapping->i_mmap_mutex);
	delete_uprobe(uprobe);
	mutex_unlock(&mapping->i_mmap_mutex);

	inode->uprobes_count--;
	mutex_unlock(&inode->i_mutex);

Right, so this lonesome unlock got me puzzled for a while, I always find it best not to do asymmetric locking like this, keep the lock and unlock in the same function.

Quoted 31 lines

}

int register_uprobe(...)

{
	uprobe = alloc_uprobe(...);	// find or insert in tree

	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	if (uprobe->consumers) {
		add_consumer();
		goto put_unlock;
	}
	add_consumer();
	inode->uprobes_count++;
	mutex_lock(&mapping->i_mmap_mutex);	//sync with mmap.
	vma_prio_tree_foreach(..) {
		// get mm ref, add to list blah blah
	}

	mutex_unlock(&mapping->i_mmap_mutex);
	list_for_each_entry_safe() {
		if (ret) {
			// del from list etc..
			//
			continue;
		}
		down_read(mm->mmap_sem);
		ret = install_breakpoint();
		up_read(..);
		// del from list etc..
		//
		if (ret && (ret == -ESRCH || ret == -EEXIST))
Quoted 8 lines
			ret = 0;
	}

	if (ret)
		_unregister_uprobe();

      put_unlock:
	mutex_unlock(&inode->i_mutex);

You see, now this is a double unlock

Quoted 10 lines
	put_uprobe(uprobe);
	return ret;
}

void unregister_uprobe(...)

{
	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	uprobe = find_uprobe();	// ref++
	_unregister_uprobe();
	mutex_unlock(&inode->i_mutex);

idem

Quoted 27 lines
	put_uprobe(uprobe);
}

int mmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;	/* Bail-out */

	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (inode->uprobes_count)
		return ret;
	__iget(inode);

	INIT_LIST_HEAD(&tmp_list);

	mutex_lock(&mapping->i_mmap_mutex);
	add_to_temp_list(vma, inode, &tmp_list);
	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
		loff_t vaddr;
Quoted 7 lines
		list_del(&uprobe->pending_list);
		if (ret)
			continue;

		vaddr = vma->vm_start + uprobe->offset;
		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
		ret = install_breakpoint(mm, uprobe, vaddr);

Right, so this is the problem, you cannot do allocations under i_mmap_mutex, however I think you can under i_mutex.

Quoted 10 lines
		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
	}

	mutex_unlock(&mapping->i_mmap_mutex);
	iput(inode);
	return ret;
}

int munmap_uprobe(struct vm_area_struct *vma)

Quoted 14 lines
{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;	/* Bail-out */

	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (inode->uprobes_count)
		return ret;

Should that be !->uprobes_count?

Quoted 5 lines

// walk thro RB tree and decrement mm->uprobes_count

	walk_rbtree_and_dec_uprobes_count();	//hold treelock.

	return ret;
}

--
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>

#75 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 11 lines
Quoted 6 lines
	mutex_lock(&mapping->i_mmap_mutex);
	delete_uprobe(uprobe);
	mutex_unlock(&mapping->i_mmap_mutex);

	inode->uprobes_count--;
	mutex_unlock(&inode->i_mutex);

Right, so this lonesome unlock got me puzzled for a while, I always find it best not to do asymmetric locking like this, keep the lock and unlock in the same function.

Okay, will do.

Quoted 34 lines
Quoted 34 lines

}

int register_uprobe(...)

{
	uprobe = alloc_uprobe(...);	// find or insert in tree

	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	if (uprobe->consumers) {
		add_consumer();
		goto put_unlock;
	}
	add_consumer();
	inode->uprobes_count++;
	mutex_lock(&mapping->i_mmap_mutex);	//sync with mmap.
	vma_prio_tree_foreach(..) {
		// get mm ref, add to list blah blah
	}

	mutex_unlock(&mapping->i_mmap_mutex);
	list_for_each_entry_safe() {
		if (ret) {
			// del from list etc..
			//
			continue;
		}
		down_read(mm->mmap_sem);
		ret = install_breakpoint();
		up_read(..);
		// del from list etc..
		//
		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
	}
Quoted 7 lines
Quoted 5 lines
	if (ret)
		_unregister_uprobe();

      put_unlock:
	mutex_unlock(&inode->i_mutex);

You see, now this is a double unlock

hmm . .will correct this.

Quoted 45 lines
Quoted 11 lines
	put_uprobe(uprobe);
	return ret;
}

void unregister_uprobe(...)

{
	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	uprobe = find_uprobe();	// ref++
	_unregister_uprobe();
	mutex_unlock(&inode->i_mutex);

idem

Quoted 31 lines
	put_uprobe(uprobe);
}

int mmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;	/* Bail-out */

	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (inode->uprobes_count)
		return ret;
	__iget(inode);

	INIT_LIST_HEAD(&tmp_list);

	mutex_lock(&mapping->i_mmap_mutex);
	add_to_temp_list(vma, inode, &tmp_list);
	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
		loff_t vaddr;

		list_del(&uprobe->pending_list);
		if (ret)
			continue;
Quoted 6 lines
Quoted 3 lines
		vaddr = vma->vm_start + uprobe->offset;
		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
		ret = install_breakpoint(mm, uprobe, vaddr);

Right, so this is the problem, you cannot do allocations under i_mmap_mutex, however I think you can under i_mutex.

I didnt know that we cannot do allocations under i_mmap_mutex. Why is this?

I cant take i_mutex, because we would have already held down_write(mmap_sem) here.

Quoted 21 lines
Quoted 21 lines
		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
	}

	mutex_unlock(&mapping->i_mmap_mutex);
	iput(inode);
	return ret;
}

int munmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;	/* Bail-out */
Quoted 6 lines
Quoted 4 lines
	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (inode->uprobes_count)
		return ret;

Should that be !->uprobes_count?

Yes it should be !inode->uprobes_count.
(both here and in mmap_uprobe)

Quoted 2 lines
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>
maintainer
#76 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 13 lines · Srikar Dronamraju
Quoted 13 lines
Quoted 12 lines
	mutex_lock(&mapping->i_mmap_mutex);
	add_to_temp_list(vma, inode, &tmp_list);
	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
		loff_t vaddr;

		list_del(&uprobe->pending_list);
		if (ret)
			continue;

		vaddr = vma->vm_start + uprobe->offset;
		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
		ret = install_breakpoint(mm, uprobe, vaddr);
Quoted 5 lines
Quoted 2 lines

Right, so this is the problem, you cannot do allocations under i_mmap_mutex, however I think you can under i_mutex.

I didnt know that we cannot do allocations under i_mmap_mutex. Why is this?

Because we try to take i_mmap_mutex during reclaim, trying to unmap pages. So suppose we do an allocation while holding i_mmap_mutex, find there's no free memory, try and unmap a page in order to free it, and we're stuck.

Quoted 2 lines

I cant take i_mutex, because we would have already held down_write(mmap_sem) here.

Right. So can we add a lock in the uprobe? All we need is per uprobe serialization, right?

--
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>

#77 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Here is another take at locking for uprobes.

/*
* Register/Unregister:
* - walks thro vma-rmap creates a temp list of vmas.
* - Cannot sleep while walking thro vma-rmap; hence temp list.
* - Iterate thro the temp list and insert/delete breakpoints.
* - Insertion/deletion of breakpoint needs mmap_sem to be held and
* dropped in the loop.
*
* mmap/munmap:
* - called with down_write(mmap_sem)
* - walk thro the uprobe rbtree and create a temp list of uprobes.
* - Cannot sleep while walking thro rbtree; hence temp list.
* - Iterate thro the temp list and insert/delete breakpoints.
*
* Issues:
* - Lock ordering issues since mmap/munmap are called with mmap_sem
* held for write unlike register/unregister where they need to
* held just before insertion of breakpoints.
* - Cannot allocate when i_mmap_mutex is held.
* - Vma objects can vanish after munmap.
* - Multiple vmas mapping to same text area of an inode in the same mm
* struct.
*
* Changes:
*
* - Introduce uprobes_list and uprobes_vaddr in vm_area_struct.

 *    uprobes_list is a node in the temp list of vmas while
 *    registering/unregistering uprobes. uprobes_vaddr caches the vaddr to
 *    insert/remove the breakpoint.
 *
 *  - Introduce srcu to synchronize vma deletion with walking the list of
 *    vma in register/unregister_uprobe.
 *
 *  - Introduce uprobes_mmap_mutex to synchronize uprobe deletion and
 *    mmap_uprobe().
 *
 * Locking:
 *   hierarcy:
 *    --> inode->i_mutex
 *    		--> mm->mmap_sem
 *    			--> mapping->i_mmap_mutex
 *    			--> uprobes_mmap_mutex
 *    				--> treelock
 *
 *   i_mutex: serializes register/unregister on the same inode.
 *
 *   i_mmap_mutex: allows to walk the vma-rmap but cannot be held across
 *  		kzalloc.
 *
 *   mmap_sem : insert an instruction into the address-space.
 *
 *   uprobes_mmap_mutex : serializes multiple mmap_uprobes since the
 *			  same uprobe cannot be in more than one list.
 *   			: serializes walking the uprobe list with deleting
 *   			  the uprobe. (this can be achieved by taking a
 *   			  reference to uprobe while we walk).
 *
 *   treelock (spinlock) : modification/walking thro the rb tree.
 *
 *   srcu : synchronize walking the vma list with munmap.
 *
 *  Assumptions:
 *  Can sleep including sleeping for page allocations/copying original
 *  instructions while holding the srcu.

*
* Advantages:
* 1. No need to drop mmap_sem.
* 2. Now register/unregister can run in parallel except unless when uprobe
* gets deleted.
*
* Disadvantages:
* 1. All calls to mmap_uprobe are serialized.
* 2. Locking gets complicated with 3 mutexs, mmap_sem, treelock and srcu. *
* Note:
* Can avoid uprobes_mmap_mutex/srcu/serializing the mmap_uprobes by
* try_mutex_lock(i_mutex) followed by dropping mmap_sem; acquiring
* i_mutex and mmap_sem in order and verifying the vma in both
* mmap_uprobe and munmap_uprobe but Peter Zijlstra not happy with that
* approach.
*/

void _unregister_uprobe(...)

{
	if (!del_consumer(...)) {	// includes tree removal on last consumer
		return;
	}
	if (uprobe->consumers)
		return;		// Not the last consumer.

	INIT_LIST_HEAD(&tmp_list);

	mutex_lock(&mapping->i_mmap_mutex);	//sync with mmap.
	vma_prio_tree_foreach() {
		list_add(&vma->uprobes_list, &tmplist);
		// set vma->uprobes_vaddr;
	}

	mutex_unlock(&mapping->i_mmap_mutex);

	srcu_read_lock(&uprobes_srcu);	// synch with munmap_uprobe.
	list_for_each_entry_safe() {
		mm = vma->vm_mm;
		vaddr = vma->uprobes_vaddr;
		// remove from list
		down_read(&mm->mmap_sem);
		remove_breakpoint();	// unconditional, if it wasn't there
		up_read(&mm->mmap_sem);
	}
	srcu_read_unlock(&uprobes_srcu, ..);

	inode->uprobes_count--;

	mutex_lock(&uprobe_mmap_mutex);	// sync with mmap_uprobe.
	delete_uprobe(uprobe);
	mutex_unlock(&uprobe_mmap_mutex);
}

int register_uprobe(...)

{
	uprobe = alloc_uprobe(...);	// find or insert in tree

	INIT_LIST_HEAD(&tmp_list);

	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	if (uprobe->consumers) {
		add_consumer();
		goto put_unlock;
	}
	add_consumer();
	inode->uprobes_count++;
	mutex_lock(&mapping->i_mmap_mutex);	//sync with mmap_uprobe
	vma_prio_tree_foreach(..) {
		list_add(&vma->uprobes_list, &tmplist);
		// get mm ref, add to list blah blah
		// set vma->uprobes_vaddr;
	}

	mutex_unlock(&mapping->i_mmap_mutex);

	srcu_read_lock(&uprobes_srcu);	// synch with munmap_uprobe.
	list_for_each_entry_safe() {
		mm = vma->vm_mm;
		vaddr = vma->uprobes_vaddr;
		if (ret) {
			// del from list etc..
			//
			continue;
		}
		down_read(mm->mmap_sem);
		ret = install_breakpoint();
		up_read(..);
		// del from list etc..
		//
		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
	}
	srcu_read_unlock(&uprobes_srcu, ..);

	if (ret)
		_unregister_uprobe();

      put_unlock:
	mutex_unlock(&inode->i_mutex);
	put_uprobe(uprobe);
	return ret;
}

void unregister_uprobe(...)

{
	mutex_lock(&inode->i_mutex);	// sync with register/unregister
	uprobe = find_uprobe();	// ref++
	_unregister_uprobe();
	mutex_unlock(&inode->i_mutex);
	put_uprobe(uprobe);
}

int mmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;
	int ret = 0;

	if (!valid_vma(vma))
		return ret;	/* Bail-out */

	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (!inode->uprobes_count)
		return ret;
	__iget(inode);

	INIT_LIST_HEAD(&tmp_list);

	mutex_lock(&uprobes_mmap_mutex);
	add_to_temp_list(vma, inode, &tmp_list);
	list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
		loff_t vaddr;
		if (!uprobe->consumer)
			continue;

		list_del(&uprobe->pending_list);
		if (ret)
			continue;

		vaddr = vma->vm_start + uprobe->offset;
		vaddr -= vma->vm_pgoff << PAGE_SHIFT;
		ret = install_breakpoint(mm, uprobe, vaddr);

		if (ret && (ret == -ESRCH || ret == -EEXIST))
			ret = 0;
	}

	mutex_unlock(&uprobes_mmap_mutex);
	iput(inode);
	return ret;
}

void munmap_uprobe(struct vm_area_struct *vma)

{
	struct list_head tmp_list;
	struct uprobe *uprobe, *u;
	struct mm_struct *mm;
	struct inode *inode;

	if (!valid_vma(vma))
		return;	/* Bail-out */

	mm = vma->vm_mm;
	inode = vma->vm_file->f_mapping->host;
	if (!inode->uprobes_count)
		return;

	list_del(&vma->uprobes_list);
	synchronize_srcu(..);	// synchronize with (un)register_uprobe

	if (!mm->uprobes_count)
		return;

// walk thro RB tree and decrement mm->uprobes_count

	walk_rbtree_and_dec_uprobes_count();	//hold treelock.

	return;
}

--
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
#78 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 7 lines · Srikar Dronamraju

* - Introduce uprobes_list and uprobes_vaddr in vm_area_struct.

 *    uprobes_list is a node in the temp list of vmas while
 *    registering/unregistering uprobes. uprobes_vaddr caches the vaddr to
 *    insert/remove the breakpoint.

*
* - Introduce srcu to synchronize vma deletion with walking the list of
* vma in register/unregister_uprobe.

I don't think you can sell this, that'll make munmap() horridly slow.

Quoted 2 lines

* - Introduce uprobes_mmap_mutex to synchronize uprobe deletion and
* mmap_uprobe().

Yes, that'll work I think.

--
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>

#79 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 10 lines · Peter Zijlstra
Quoted 7 lines · Srikar Dronamraju

* - Introduce uprobes_list and uprobes_vaddr in vm_area_struct.

 *    uprobes_list is a node in the temp list of vmas while
 *    registering/unregistering uprobes. uprobes_vaddr caches the vaddr to
 *    insert/remove the breakpoint.

*
* - Introduce srcu to synchronize vma deletion with walking the list of
* vma in register/unregister_uprobe.

I don't think you can sell this, that'll make munmap() horridly slow.

Okay,

How about using a counter and a wq in each vma.
Based on the counter, I can wait in the munmap() and since this is per vma, this should be faster than srcu.

Counter would be incremented when we do a vma-rmap walk. decremented when after insertion/deletion.
read in munmap().

Quoted 5 lines
Quoted 3 lines

* - Introduce uprobes_mmap_mutex to synchronize uprobe deletion and
* mmap_uprobe().

Yes, that'll work I think.

Here another possibility is to have a per uprobe mutex, that way we would not have to serialize mmap_uprobe. But this optimization can be done later too.

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#80 lore raw

Re: [PATCH v4 3.0-rc2-tip 7/22] 7: uprobes: mmap and fork hooks.

Quoted 11 lines · Srikar Dronamraju
Quoted 1 line

I don't think you can sell this, that'll make munmap() horridly slow.

Okay,

How about using a counter and a wq in each vma.
Based on the counter, I can wait in the munmap() and since this is per vma, this should be faster than srcu.

Counter would be incremented when we do a vma-rmap walk. decremented when after insertion/deletion.
read in munmap().

I know I would feel somewhat uneasy about growing struct vm_area_struct for just uprobes, but what do other people think?

Andrew, Christoph?

--
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>

#81 lore raw

PATCH v4 8/22 8: x86: architecture specific task information.

In reply to Srikar Dronamraju

On X86_64, we need to support rip relative instructions. Rip relative instructions are handled by saving the scratch register on probe hit and then retrieving the previously saved scratch register after single-step. This value stored at probe hit is specific to each task. Hence this is implemented as part of uprobe_task_arch_info.

Since x86_32 has no support for rip relative instructions, we dont need to bother for x86_32.

---
 arch/x86/include/asm/uprobes.h |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
arch/x86/include/asm/uprobes.h +5 -0
index 4295ce0..2f3c64d 100644
@@ -34,8 +34,13 @@ typedef u8 uprobe_opcode_t;
 struct uprobe_arch_info {
 	unsigned long rip_rela_target_address;
 };
+
+struct uprobe_task_arch_info {
+	unsigned long saved_scratch_register;
+};
 #else
 struct uprobe_arch_info {};
+struct uprobe_task_arch_info {};
 #endif
 struct uprobe;
 extern int analyze_insn(struct task_struct *tsk, struct uprobe *uprobe);

--
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>

#82 lore raw

PATCH v4 9/22 9: uprobes: task specific information.

In reply to Srikar Dronamraju

Uprobes needs to maintain some task specific information include if a task is currently uprobed, the currently handing uprobe, any arch specific information (for example to handle rip relative instructions), the per-task slot where the original instruction is copied to before single-stepping.

Provides routines to create/manage and free the task specific information.

---
 include/linux/sched.h   |    9 ++++++---
 include/linux/uprobes.h |   25 +++++++++++++++++++++++++
 kernel/fork.c           |    4 ++++
 kernel/uprobes.c        |   38 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 73 insertions(+), 3 deletions(-)
include/linux/sched.h +6 -3
index a837b20..9af9c99 100644
@@ -1304,9 +1304,9 @@ struct task_struct {
 	unsigned long stack_canary;
 #endif
 
-	/* 
+	/*
 	 * pointers to (original) parent process, youngest child, younger sibling,
-	 * older sibling, respectively.  (p->father can be replaced with 
+	 * older sibling, respectively.  (p->father can be replaced with
 	 * p->real_parent->pid)
 	 */
 	struct task_struct *real_parent; /* real parent process */
@@ -1561,6 +1561,9 @@ struct task_struct {
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
 	atomic_t ptrace_bp_refcnt;
 #endif
+#ifdef CONFIG_UPROBES
+	struct uprobe_task *utask;
+#endif
 };
 
 /* Future-safe accessor for struct task_struct's cpus_allowed. */
@@ -2127,7 +2130,7 @@ static inline int dequeue_signal_lock(struct task_struct *tsk, sigset_t *mask, s
 	spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
 
 	return ret;
-}	
+}
 
 extern void block_all_signals(int (*notifier)(void *priv), void *priv,
 			      sigset_t *mask);
include/linux/uprobes.h +25 -0
index fc2f9d2..821e000 100644
@@ -26,12 +26,14 @@
 #include <linux/rbtree.h>
 #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
 #include <asm/uprobes.h>
+struct uprobe_task_arch_info;	/* arch specific task info */
 #else
 /*
  * ARCH_SUPPORTS_UPROBES is not defined.
  */
 typedef u8 uprobe_opcode_t;
 struct uprobe_arch_info	{};		/* arch specific info*/
+struct uprobe_task_arch_info {};	/* arch specific task info */
 #endif /* CONFIG_ARCH_SUPPORTS_UPROBES */
 
 /* Post-execution fixups.  Some architectures may define others. */
@@ -77,6 +79,27 @@ struct uprobe {
 	int			copy;
 };
 
+enum uprobe_task_state {
+	UTASK_RUNNING,
+	UTASK_BP_HIT,
+	UTASK_SSTEP
+};
+
+/*
+ * uprobe_utask -- not a user-visible struct.
+ * Corresponds to a thread in a probed process.
+ * Guarded by uproc->mutex.
+ */
+struct uprobe_task {
+	unsigned long xol_vaddr;
+	unsigned long vaddr;
+
+	enum uprobe_task_state state;
+	struct uprobe_task_arch_info tskinfo;
+
+	struct uprobe *active_uprobe;
+};
+
 /*
  * Most architectures can use the default versions of @read_opcode(),
  * @set_bkpt(), @set_orig_insn(), and @is_bkpt_insn();
@@ -111,6 +134,7 @@ extern int register_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer);
 extern void unregister_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer);
+extern void free_uprobe_utask(struct task_struct *tsk);
 
 struct vm_area_struct;
 extern int mmap_uprobe(struct vm_area_struct *vma);
@@ -133,5 +157,6 @@ static inline int mmap_uprobe(struct vm_area_struct *vma)
 {
 	return 0;
 }
+static inline void free_uprobe_utask(struct task_struct *tsk) {}
 #endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/fork.c +4 -0
index f6c7cb1..bf5999b 100644
@@ -196,6 +196,7 @@ void __put_task_struct(struct task_struct *tsk)
 	delayacct_tsk_free(tsk);
 	put_signal_struct(tsk->signal);
 
+	free_uprobe_utask(tsk);
 	if (!profile_handoff_task(tsk))
 		free_task(tsk);
 }
@@ -1268,6 +1269,9 @@ 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;
+#endif
 	/*
 	 * sigaltstack should be cleared when sharing the same VM
 	 */
kernel/uprobes.c +38 -0
index 93a53c0..2bb2bd7 100644
@@ -1022,3 +1022,41 @@ mmap_out:
 	down_write(&mm->mmap_sem);
 	return ret;
 }
+
+/*
+ * Called with no locks held.
+ * Called in context of a exiting or a exec-ing thread.
+ */
+void free_uprobe_utask(struct task_struct *tsk)
+{
+	struct uprobe_task *utask = tsk->utask;
+
+	if (!utask)
+		return;
+
+	if (utask->active_uprobe)
+		put_uprobe(utask->active_uprobe);
+	kfree(utask);
+	tsk->utask = NULL;
+}
+
+/*
+ * Allocate a uprobe_task object for the task.
+ * Called when the thread hits a breakpoint for the first time.
+ *
+ * Returns:
+ * - pointer to new uprobe_task on success
+ * - negative errno otherwise
+ */
+static struct uprobe_task *add_utask(void)
+{
+	struct uprobe_task *utask;
+
+	utask = kzalloc(sizeof *utask, GFP_KERNEL);
+	if (unlikely(utask == NULL))
+		return ERR_PTR(-ENOMEM);
+
+	utask->active_uprobe = NULL;
+	current->utask = utask;
+	return utask;
+}

--
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>

#83 lore raw

PATCH v4 10/22 10: uprobes: slot allocation for uprobes

In reply to Srikar Dronamraju

Slots are allocated at probe hit time and released after singlestep. When a probe is hit, the original instruction corresponding to the probe hit is copied to allocated slot. Currently we allocate one page of slots for each mm. Bitmaps are used to know which slots are free. Each slot is made of 128 bytes so that its cache aligned.

---
 include/linux/mm_types.h |    4 +
 include/linux/uprobes.h  |   23 ++++
 kernel/fork.c            |    4 +
 kernel/uprobes.c         |  242 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 273 insertions(+), 0 deletions(-)
include/linux/mm_types.h +4 -0
index 7bfef2e..e016ac7 100644
@@ -12,6 +12,9 @@
 #include <linux/completion.h>
 #include <linux/cpumask.h>
 #include <linux/page-debug-flags.h>
+#ifdef CONFIG_UPROBES
+#include <linux/uprobes.h>
+#endif
 #include <asm/page.h>
 #include <asm/mmu.h>
 
@@ -320,6 +323,7 @@ struct mm_struct {
 	unsigned long uprobes_vaddr;
 	struct list_head uprobes_list; /* protected by uprobes_mutex */
 	atomic_t uprobes_count;
+	struct uprobes_xol_area *uprobes_xol_area;
 #endif
 };
 
include/linux/uprobes.h +23 -0
index 821e000..4590e9a 100644
@@ -101,6 +101,27 @@ struct uprobe_task {
 };
 
 /*
+ * 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 {
+	spinlock_t slot_lock;	/* protects bitmap and slot (de)allocation*/
+	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 */
+};
+
+/*
  * Most architectures can use the default versions of @read_opcode(),
  * @set_bkpt(), @set_orig_insn(), and @is_bkpt_insn();
  *
@@ -139,6 +160,7 @@ extern void free_uprobe_utask(struct task_struct *tsk);
 struct vm_area_struct;
 extern int mmap_uprobe(struct vm_area_struct *vma);
 extern void dup_mmap_uprobe(struct mm_struct *old_mm, struct mm_struct *mm);
+extern void free_uprobes_xol_area(struct mm_struct *mm);
 #else /* CONFIG_UPROBES is not defined */
 static inline int register_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer)
@@ -158,5 +180,6 @@ static inline int mmap_uprobe(struct vm_area_struct *vma)
 	return 0;
 }
 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 bf5999b..c2790b5 100644
@@ -557,6 +557,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 */
@@ -741,6 +742,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 +242 -0

266 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>

#84 lore raw

PATCH v4 11/22 11: uprobes: get the breakpoint address.

In reply to Srikar Dronamraju

On a breakpoint hit, return the address where the breakpoint was hit.

---
 include/linux/uprobes.h |    5 +++++
 kernel/uprobes.c        |   11 +++++++++++
 2 files changed, 16 insertions(+), 0 deletions(-)
include/linux/uprobes.h +5 -0
index 4590e9a..838fbaa 100644
@@ -161,6 +161,7 @@ struct vm_area_struct;
 extern int mmap_uprobe(struct vm_area_struct *vma);
 extern void dup_mmap_uprobe(struct mm_struct *old_mm, struct mm_struct *mm);
 extern void free_uprobes_xol_area(struct mm_struct *mm);
+extern unsigned long __weak get_uprobe_bkpt_addr(struct pt_regs *regs);
 #else /* CONFIG_UPROBES is not defined */
 static inline int register_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer)
@@ -181,5 +182,9 @@ static inline int mmap_uprobe(struct vm_area_struct *vma)
 }
 static inline void free_uprobe_utask(struct task_struct *tsk) {}
 static inline void free_uprobes_xol_area(struct mm_struct *mm) {}
+static inline unsigned long get_uprobe_bkpt_addr(struct pt_regs *regs)
+{
+	return 0;
+}
 #endif /* CONFIG_UPROBES */
 #endif	/* _LINUX_UPROBES_H */
kernel/uprobes.c +11 -0
index d19c3b0..fa9e9ba 100644
@@ -1263,6 +1263,17 @@ static void xol_free_insn_slot(struct task_struct *tsk)
 						__func__, slot_addr);
 }
 
+/**
+ * get_uprobe_bkpt_addr - compute address of bkpt given post-bkpt regs
+ * @regs: Reflects the saved state of the task after it has hit a breakpoint
+ * instruction.
+ * Return the address of the breakpoint instruction.
+ */
+unsigned long __weak get_uprobe_bkpt_addr(struct pt_regs *regs)
+{
+	return instruction_pointer(regs) - UPROBES_BKPT_INSN_SIZE;
+}
+
 /*
  * Called with no locks held.
  * Called in context of a exiting or a exec-ing thread.

--
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>

#85 lore raw

PATCH v4 12/22 12: x86: x86 specific probe handling

In reply to Srikar Dronamraju

Provides x86 specific implementations for setting the current instruction pointer, pre single-step and post-singlestep handling, enabling and disabling singlestep.

This patch also introduces TIF_UPROBE which is set by uprobes notifier code. TIF_UPROBE indicates that there is pending work that needs to be done at do_notify_resume time.

---
 arch/x86/include/asm/thread_info.h |    2 
 arch/x86/include/asm/uprobes.h     |    3 +
 arch/x86/kernel/uprobes.c          |  148 ++++++++++++++++++++++++++++++++++++
 3 files changed, 153 insertions(+), 0 deletions(-)
arch/x86/include/asm/thread_info.h +2 -0
index 1f2e61e..2dc1921 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 +3 -0
index 2f3c64d..3a7833c 100644
@@ -44,4 +44,7 @@ struct uprobe_task_arch_info {};
 #endif
 struct uprobe;
 extern int analyze_insn(struct task_struct *tsk, 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);
 #endif	/* _ASM_UPROBES_H */
arch/x86/kernel/uprobes.c +148 -0

157 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>

#86 lore raw

PATCH v4 13/22 13: uprobes: Handing int3 and singlestep exception.

In reply to Srikar Dronamraju

On int3, set the TIF_UPROBE flag and if a task specific info is available, indicate the task state as breakpoint hit. Setting the TIF_UPROBE flag results in uprobe_notify_resume being called. uprobe_notify_resume walks thro the list of vmas and then matches the inode and offset corresponding to the instruction pointer to enteries in rbtree. Once a matcing uprobes is found, run the handlers for all the consumers that have registered.

On singlestep exception, perform the necessary fixups and allow the process to continue. The necessary fixups are determined at instruction analysis time.

TODO: If there is no matching uprobe, signal a trap to the process.

---
 include/linux/uprobes.h |    4 +
 kernel/uprobes.c        |  143 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 147 insertions(+), 0 deletions(-)
include/linux/uprobes.h +4 -0
index 838fbaa..8581723 100644
@@ -162,6 +162,9 @@ extern int mmap_uprobe(struct vm_area_struct *vma);
 extern void dup_mmap_uprobe(struct mm_struct *old_mm, struct mm_struct *mm);
 extern void free_uprobes_xol_area(struct mm_struct *mm);
 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);
+extern void uprobe_notify_resume(struct pt_regs *regs);
 #else /* CONFIG_UPROBES is not defined */
 static inline int register_uprobe(struct inode *inode, loff_t offset,
 				struct uprobe_consumer *consumer)
@@ -182,6 +185,7 @@ static inline int mmap_uprobe(struct vm_area_struct *vma)
 }
 static inline void free_uprobe_utask(struct task_struct *tsk) {}
 static inline void free_uprobes_xol_area(struct mm_struct *mm) {}
+static inline void uprobe_notify_resume(struct pt_regs *regs) {}
 static inline unsigned long get_uprobe_bkpt_addr(struct pt_regs *regs)
 {
 	return 0;
kernel/uprobes.c +143 -0

146 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>

#87 lore raw

Re: [PATCH v4 3.0-rc2-tip 13/22] 13: uprobes: Handing int3 and singlestep exception.

Quoted 11 lines · Srikar Dronamraju
kernel/uprobes.c +11 -0
+/*
+ * uprobe_post_notifier gets called in interrupt context.
+ * It completes the single step operation.
+ */
+int uprobe_post_notifier(struct pt_regs *regs)
+{
+	struct uprobe *uprobe;
+	struct uprobe_task *utask;
+
+	if (!current->mm || !current->utask || !current->utask->active_uprobe)
+		/* task is currently not uprobed */

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1458

Quoted 10 lines
kernel/uprobes.c +10 -0
+		return 0;
+
+	utask = current->utask;
+	uprobe = utask->active_uprobe;
+	if (!uprobe)
+		return 0;
+
+	set_thread_flag(TIF_UPROBE);
+	return 1;
+}

Looks like this can be simplified. If current->utask->active_uprobe is non-null then surely the assignment to uprobe will be too?

Signature · 8 lines
steve

--
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>
#88 lore raw

Re: [PATCH v4 3.0-rc2-tip 13/22] 13: uprobes: Handing int3 and singlestep exception.

Quoted 13 lines
Quoted 13 lines

+ */

kernel/uprobes.c +12 -0
+int uprobe_post_notifier(struct pt_regs *regs)
+{
+	struct uprobe *uprobe;
+	struct uprobe_task *utask;
+
+	if (!current->mm || !current->utask || !current->utask->active_uprobe)
+		/* task is currently not uprobed */
+		return 0;
+
+	utask = current->utask;
+	uprobe = utask->active_uprobe;
+	if (!uprobe)
Quoted 9 lines

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1436

Quoted 5 lines
kernel/uprobes.c +5 -0
+		return 0;
+
+	set_thread_flag(TIF_UPROBE);
+	return 1;
+}

Looks like this can be simplified. If current->utask->active_uprobe is non-null then surely the assignment to uprobe will be too?

Yes, the two lines where we check for !uprobe and return are redundant and can be removed. Will be corrected in the next patchset.

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>
maintainer
#89 lore raw

Re: [PATCH v4 3.0-rc2-tip 13/22] 13: uprobes: Handing int3 and singlestep exception.

In reply to Srikar Dronamraju
Quoted 15 lines · Srikar Dronamraju
kernel/uprobes.c +15 -0
+void uprobe_notify_resume(struct pt_regs *regs)
+{
+       struct vm_area_struct *vma;
+       struct uprobe_task *utask;
+       struct mm_struct *mm;
+       struct uprobe *u = NULL;
+       unsigned long probept;
+
+       utask = current->utask;
+       mm = current->mm;
+       if (!utask || utask->state == UTASK_BP_HIT) {
+               probept = get_uprobe_bkpt_addr(regs);
+               down_read(&mm->mmap_sem);
+               vma = find_vma(mm, probept);
+               if (vma && valid_vma(vma))

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1378

Quoted 10 lines
kernel/uprobes.c +10 -0
+                       u = find_uprobe(vma->vm_file->f_mapping->host,
+                                       probept - vma->vm_start +
+                                       (vma->vm_pgoff << PAGE_SHIFT));
+               up_read(&mm->mmap_sem);
+               if (!u)
+                       goto cleanup_ret;
+               if (!utask) {
+                       utask = add_utask();
+                       if (!utask)
+                               goto cleanup_ret;

So if we fail to allocate task state,..

Quoted 17 lines
kernel/uprobes.c +17 -0
+               }
+               /* TODO Start queueing signals. */
+               utask->active_uprobe = u;
+               handler_chain(u, regs);
+               utask->state = UTASK_SSTEP;
+               if (!pre_ssout(u, regs, probept))
+                       user_enable_single_step(current);
+               else
+                       goto cleanup_ret;
+       } else if (utask->state == UTASK_SSTEP) {
+               u = utask->active_uprobe;
+               if (sstep_complete(u, regs)) {
+                       put_uprobe(u);
+                       utask->active_uprobe = NULL;
+                       utask->state = UTASK_RUNNING;
+                       user_disable_single_step(current);
+                       xol_free_insn_slot(current);

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1405

Quoted 10 lines
kernel/uprobes.c +10 -0
+
+                       /* TODO Stop queueing signals. */
+               }
+       }
+       return;
+
+cleanup_ret:
+       if (u) {
+               down_read(&mm->mmap_sem);
+               if (!set_orig_insn(current, u, probept, true))

we try to undo the probe? That doesn't make any sense. I thought you meant to return to userspace, let it re-take the trap and try again until you do manage to allocate the user resource.

This behaviour makes probes totally unreliable under memory pressure.

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1417

Quoted 12 lines
kernel/uprobes.c +12 -0
+                       atomic_dec(&mm->uprobes_count);
+               up_read(&mm->mmap_sem);
+               put_uprobe(u);
+       } else {
+       /*TODO Return SIGTRAP signal */
+       }
+       if (utask) {
+               utask->active_uprobe = NULL;
+               utask->state = UTASK_RUNNING;
+       }
+       set_instruction_pointer(regs, probept);
+} 

Also, there's a scary amount of TODO in there...

--
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>

#90 lore raw

Re: [PATCH v4 3.0-rc2-tip 13/22] 13: uprobes: Handing int3 and singlestep exception.

Quoted 9 lines

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1405

Quoted 5 lines
kernel/uprobes.c +5 -0
+
+cleanup_ret:
+       if (u) {
+               down_read(&mm->mmap_sem);
+               if (!set_orig_insn(current, u, probept, true))

we try to undo the probe? That doesn't make any sense. I thought you meant to return to userspace, let it re-take the trap and try again until you do manage to allocate the user resource.

I meant removing the probe itself
https://lkml.org/lkml/2011/4/21/279

We could try reseting and retrying the trap. Just that we might end up looping under memory pressure.

Quoted 2 lines

This behaviour makes probes totally unreliable under memory pressure.

Under memory pressure we could be unreliable.

Quoted 14 lines

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1417

Quoted 13 lines
kernel/uprobes.c +12 -0
+                       atomic_dec(&mm->uprobes_count);
+               up_read(&mm->mmap_sem);
+               put_uprobe(u);
+       } else {
+       /*TODO Return SIGTRAP signal */
+       }
+       if (utask) {
+               utask->active_uprobe = NULL;
+               utask->state = UTASK_RUNNING;
+       }
+       set_instruction_pointer(regs, probept);
+} 
Quoted 1 line

Also, there's a scary amount of TODO in there...

All of those deal with delaying the signals. I am working on it at this moment.

Signature · 2 lines
Thanks and Regards
Srikar
maintainer
#91 lore raw

Re: [PATCH v4 3.0-rc2-tip 13/22] 13: uprobes: Handing int3 and singlestep exception.

Quoted 13 lines · Srikar Dronamraju
Quoted 9 lines

kernel/uprobes.c static struct uprobe_task *add_utask(void) · line 1405

Quoted 5 lines
kernel/uprobes.c +5 -0
+
+cleanup_ret:
+       if (u) {
+               down_read(&mm->mmap_sem);
+               if (!set_orig_insn(current, u, probept, true))

we try to undo the probe? That doesn't make any sense. I thought you meant to return to userspace, let it re-take the trap and try again until you do manage to allocate the user resource.

I meant removing the probe itself
https://lkml.org/lkml/2011/4/21/279

Quoted 7 lines

We could try reseting and retrying the trap. Just that we might end up looping under memory pressure.

Quoted 2 lines

This behaviour makes probes totally unreliable under memory pressure.

Under memory pressure we could be unreliable.

But that is total crap, there's nothing worse than unreliable debug tools.

--
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>

#92 lore raw

PATCH v4 14/22 14: x86: uprobes exception notifier for x86.

In reply to Srikar Dronamraju

Provides a uprobes exception notifier for x86. This uprobe_exception notifier gets called in interrupt context and routes int3 and singlestep exception when a uprobed process encounters a INT3 or a singlestep exception.

---
 arch/x86/include/asm/uprobes.h |    3 +++
 arch/x86/kernel/signal.c       |   14 ++++++++++++++
 arch/x86/kernel/uprobes.c      |   29 +++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 0 deletions(-)
arch/x86/include/asm/uprobes.h +3 -0
index 3a7833c..a5d9480 100644
@@ -22,6 +22,7 @@
  *	Srikar Dronamraju
  *	Jim Keniston
  */
+#include <linux/notifier.h>
 
 typedef u8 uprobe_opcode_t;
 #define MAX_UINSN_BYTES 16
@@ -47,4 +48,6 @@ extern int analyze_insn(struct task_struct *tsk, 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 int uprobe_exception_notify(struct notifier_block *self,
+				       unsigned long val, void *data);
 #endif	/* _ASM_UPROBES_H */
arch/x86/kernel/signal.c +14 -0
index 40a2493..55db9f5 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>
@@ -844,6 +845,19 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
 	if (thread_info_flags & _TIF_SIGPENDING)
 		do_signal(regs);
 
+	if (thread_info_flags & _TIF_UPROBE) {
+		clear_thread_flag(TIF_UPROBE);
+#ifdef CONFIG_X86_32
+		/*
+		 * On x86_32, do_notify_resume() gets called with
+		 * interrupts disabled. Hence enable interrupts if they
+		 * are still disabled.
+		 */
+		local_irq_enable();
+#endif
+		uprobe_notify_resume(regs);
+	}
+
 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
 		clear_thread_flag(TIF_NOTIFY_RESUME);
 		tracehook_notify_resume(regs);
arch/x86/kernel/uprobes.c +29 -0
index 8d90ff3..a323631 100644
@@ -560,3 +560,32 @@ int post_xol(struct uprobe *uprobe, struct pt_regs *regs)
 		result = adjust_ret_addr(regs->sp, correction);
 	return result;
 }
+
+/*
+ * Wrapper routine for handling exceptions.
+ */
+int uprobe_exception_notify(struct notifier_block *self,
+				       unsigned long val, void *data)
+{
+	struct die_args *args = data;
+	struct pt_regs *regs = args->regs;
+	int ret = NOTIFY_DONE;
+
+	/* We are only interested in userspace traps */
+	if (regs && !user_mode_vm(regs))
+		return NOTIFY_DONE;
+
+	switch (val) {
+	case DIE_INT3:
+		/* Run your handler here */
+		if (uprobe_bkpt_notifier(regs))
+			ret = NOTIFY_STOP;
+		break;
+	case DIE_DEBUG:
+		if (uprobe_post_notifier(regs))
+			ret = NOTIFY_STOP;
+	default:
+		break;
+	}
+	return ret;
+}

--
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
#93 lore raw

Re: [PATCH v4 3.0-rc2-tip 14/22] 14: x86: uprobes exception notifier for x86.

Quoted 6 lines · Srikar Dronamraju
arch/x86/kernel/signal.c +2 -0
@@ -844,6 +845,19 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
        if (thread_info_flags & _TIF_SIGPENDING)
                do_signal(regs);
 
+       if (thread_info_flags & _TIF_UPROBE) {
+               clear_thread_flag(TIF_UPROBE);

arch/x86/kernel/signal.c do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags) · line 859

Quoted 10 lines
arch/x86/kernel/signal.c +10 -0
+#ifdef CONFIG_X86_32
+               /*
+                * On x86_32, do_notify_resume() gets called with
+                * interrupts disabled. Hence enable interrupts if they
+                * are still disabled.
+                */
+               local_irq_enable();
+#endif
+               uprobe_notify_resume(regs);
+       } 

Would it make sense to do TIF_UPROBE before TIF_SIGPENDING? That way when uprobe decides it ought to have send a signal we don't have to do another loop through all this.

--
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
#94 lore raw

Re: [PATCH v4 3.0-rc2-tip 14/22] 14: x86: uprobes exception notifier for x86.

Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Srikar Dronamraju
arch/x86/kernel/signal.c +0 -0
@@ -844,6 +845,19 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
        if (thread_info_flags & _TIF_SIGPENDING)
                do_signal(regs);
 
Quoted 16 lines

arch/x86/kernel/signal.c do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags) · line 859

Quoted 12 lines
arch/x86/kernel/signal.c +12 -0
+       if (thread_info_flags & _TIF_UPROBE) {
+               clear_thread_flag(TIF_UPROBE);
+#ifdef CONFIG_X86_32
+               /*
+                * On x86_32, do_notify_resume() gets called with
+                * interrupts disabled. Hence enable interrupts if they
+                * are still disabled.
+                */
+               local_irq_enable();
+#endif
+               uprobe_notify_resume(regs);
+       } 

Would it make sense to do TIF_UPROBE before TIF_SIGPENDING? That way when uprobe decides it ought to have send a signal we don't have to do another loop through all this.

Also, it might be good to unify x86_86 and i386 on the interrupt thing, instead of propagating this difference (unless of course there's a good reason they're different, but I really don't know this code well).

--
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>

#95 lore raw

Re: [PATCH v4 3.0-rc2-tip 14/22] 14: x86: uprobes exception notifier for x86.

Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Srikar Dronamraju
arch/x86/kernel/signal.c +0 -0
@@ -844,6 +845,19 @@ do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
        if (thread_info_flags & _TIF_SIGPENDING)
                do_signal(regs);
 
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/signal.c +8 -0
+       if (thread_info_flags & _TIF_UPROBE) {
+               clear_thread_flag(TIF_UPROBE);
+#ifdef CONFIG_X86_32
+               /*
+                * On x86_32, do_notify_resume() gets called with
+                * interrupts disabled. Hence enable interrupts if they
+                * are still disabled.
+                */
Quoted 8 lines

arch/x86/kernel/signal.c do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags) · line 859

Quoted 4 lines
arch/x86/kernel/signal.c +4 -0
+               local_irq_enable();
+#endif
+               uprobe_notify_resume(regs);
+       } 

Would it make sense to do TIF_UPROBE before TIF_SIGPENDING? That way when uprobe decides it ought to have send a signal we don't have to do another loop through all this.

Okay,

Quoted 4 lines

Also, it might be good to unify x86_86 and i386 on the interrupt thing, instead of propagating this difference (unless of course there's a good reason they're different, but I really don't know this code well).

I am not sure if this has changed lately. So I will try removing the local_irq_enable.

Oleg, Roland, do you know why do_notify_resume() gets called with interrupts disabled on i386?

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>
#96 lore raw

Re: [PATCH v4 3.0-rc2-tip 14/22] 14: x86: uprobes exception notifier for x86.

Quoted 2 lines

Oleg, Roland, do you know why do_notify_resume() gets called with interrupts disabled on i386?

It was that way for a long time. My impression was that it was just not bothering to reenable before do_signal->get_signal_to_deliver would shortly disable (via spin_lock_irq) anyway. It's possible there was something more to it, but I don't know of anything.

Thanks,
Roland

--
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>

#97 lore raw

PATCH v4 15/22 15: uprobes: register a notifier for uprobes.

In reply to Srikar Dronamraju

Uprobe needs to be intimated on int3 and singlestep exceptions. Hence uprobes registers a die notifier so that its notified of the events.

---
 kernel/uprobes.c |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
kernel/uprobes.c +18 -0
index 1e88d64..95c16dd 100644
@@ -36,6 +36,7 @@
 #include <linux/mman.h>	/* needed for PROT_EXEC, MAP_PRIVATE */
 #include <linux/file.h> /* needed for fput() */
 #include <linux/init_task.h> /* init_cred */
+#include <linux/kdebug.h> /* for notifier mechanism */
 
 #define UINSNS_PER_PAGE	(PAGE_SIZE/UPROBES_XOL_SLOT_BYTES)
 #define MAX_UPROBES_XOL_SLOTS UINSNS_PER_PAGE
@@ -1456,3 +1457,20 @@ int uprobe_post_notifier(struct pt_regs *regs)
 	set_thread_flag(TIF_UPROBE);
 	return 1;
 }
+
+struct notifier_block uprobe_exception_nb = {
+	.notifier_call = uprobe_exception_notify,
+	.priority = INT_MAX - 1,	/* notified after kprobes, kgdb */
+};
+
+static int __init init_uprobes(void)
+{
+	return register_die_notifier(&uprobe_exception_nb);
+}
+
+static void __exit exit_uprobes(void)
+{
+}
+
+module_init(init_uprobes);
+module_exit(exit_uprobes);

--
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>

#98 lore raw

PATCH v4 16/22 16: 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.

---
 kernel/trace/Kconfig        |    4 
 kernel/trace/Makefile       |    1 
 kernel/trace/trace_kprobe.c |  860 +------------------------------------------
 kernel/trace/trace_probe.c  |  746 +++++++++++++++++++++++++++++++++++++
 kernel/trace/trace_probe.h  |  158 ++++++++
 5 files changed, 928 insertions(+), 841 deletions(-)
 create mode 100644 kernel/trace/trace_probe.c
 create mode 100644 kernel/trace/trace_probe.h
kernel/trace/Kconfig +4 -0
index 2ad39e5..f6bc439 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 761c510..692223a 100644
@@ -56,5 +56,6 @@ obj-$(CONFIG_TRACEPOINTS) += power-traces.o
 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 +19 -841

972 lines not on this page · raw message

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

746 lines not on this page · raw message

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

158 lines not on this page · raw message

#99 lore raw

PATCH v4 17/22 17: 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.

---
 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  |    6 
 kernel/trace/trace_uprobe.c |  812 +++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 851 insertions(+), 17 deletions(-)
 create mode 100644 kernel/trace/trace_uprobe.c
arch/Kconfig +1 -9
index 3d91687..9ba05e1 100644
@@ -62,16 +62,8 @@ config OPTPROBES
 	depends on !PREEMPT
 
 config UPROBES
-	bool "User-space probes (EXPERIMENTAL)"
-	depends on ARCH_SUPPORTS_UPROBES
-	depends on MMU
 	select MM_OWNER
-	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 f6bc439..84783ea 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 692223a..bb3d3ff 100644
@@ -57,5 +57,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 229f859..19f4f23 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 704c04d..55fa2a6 100644
@@ -374,8 +374,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 d3828b2..673ca69 100644
@@ -507,13 +507,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);
@@ -563,7 +567,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 {
@@ -617,7 +622,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;
@@ -643,7 +648,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 +4 -2
index 487514b..f625d8d 100644
@@ -48,6 +48,7 @@
 #define FIELD_STRING_IP "__probe_ip"
 #define FIELD_STRING_RETIP "__probe_ret_ip"
 #define FIELD_STRING_FUNC "__probe_func"
+#define FIELD_STRING_PID "__probe_pid"
 
 #undef DEFINE_FIELD
 #define DEFINE_FIELD(type, item, name, is_signed)			\
@@ -64,6 +65,7 @@
 /* Flags for trace_probe */
 #define TP_FLAG_TRACE	1
 #define TP_FLAG_PROFILE	2
+#define TP_FLAG_UPROBE	4
 
 
 /* data_rloc: data relative location, compatible with u32 */
@@ -130,7 +132,7 @@ static inline __kprobes void call_fetch(struct fetch_param *fprm,
 }
 
 /* Check the name is good for event/group/fields */
-static int is_good_name(const char *name)
+static inline int is_good_name(const char *name)
 {
 	if (!isalpha(*name) && *name != '_')
 		return 0;
@@ -142,7 +144,7 @@ static 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 +812 -0

812 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>

#100 lore raw

PATCH v4 18/22 18: tracing: Uprobe tracer documentation

In reply to Srikar Dronamraju
---
 Documentation/trace/uprobetrace.txt |   94 +++++++++++++++++++++++++++++++++++
 1 files changed, 94 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/trace/uprobetrace.txt
Documentation/trace/uprobetrace.txt new file +94 -0
new file mode 100644
index 0000000..6c18ffe
@@ -0,0 +1,94 @@
+		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.
+

--
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>

#101 lore raw

PATCH v4 19/22 19: 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 |    6 +++---
 2 files changed, 9 insertions(+), 9 deletions(-)
tools/perf/builtin-probe.c +6 -6
index 2c0e64d..98db08f 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;
@@ -241,7 +241,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", "target module name"),
 #endif
 	OPT__DRY_RUN(&probe_event_dry_run),
@@ -327,7 +327,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)
@@ -348,7 +348,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;
@@ -365,7 +365,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);
@@ -387,7 +387,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 +3 -3
index f022316..153e860 100644
@@ -1976,7 +1976,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 *elfobject, struct strfilter *_filter)
 {
 	struct map *map;
 	int ret;
@@ -1987,9 +1987,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(elfobject);
 	if (!map) {
-		pr_err("Failed to find %s map.\n", (module) ? : "kernel");
+		pr_err("Failed to find %s map.\n", (elfobject) ? : "kernel");
 		return -EINVAL;
 	}
 	available_func_filter = _filter;

--
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>

#102 lore raw

PATCH v4 20/22 20: perf: perf interface for uprobes

In reply to Srikar Dronamraju

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

[ Probing a function in the executable using function name ]
-------------------------------------------------------------
[root@localhost ~]# perf probe -u zfree@/bin/zsh Add new event:
probe_zsh:zfree (on /bin/zsh:0x45400)

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

perf record -e probe_zsh:zfree -aR sleep 1

[root@localhost ~]# perf record -e probe_zsh:zfree -aR sleep 15 [ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.314 MB perf.data (~13715 samples) ] [root@localhost ~]# perf report --stdio
# Events: 3K probe_zsh:zfree
#

# Overhead  Command  Shared Object  Symbol
# ........  .......  .............  ......

#
100.00% zsh zsh [.] zfree

#
# (For a higher level overview, try: perf report --sort comm,dso) #
[root@localhost ~]

[ Probing a library function using function name ]
--------------------------------------------------
[root@localhost]#

[root@localhost]# perf probe -u malloc@/lib64/libc.so.6
Add new event:
  probe_libc:malloc    (on /lib64/libc-2.5.so:0x74dc0)

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

perf record -e probe_libc:malloc -aR sleep 1

[root@localhost]#

[root@localhost]# perf probe --list
  probe_libc:malloc    (on /lib64/libc-2.5.so:0x0000000000074dc0)
---
 tools/perf/builtin-probe.c    |   10 +
 tools/perf/util/probe-event.c |  387 +++++++++++++++++++++++++++++++++--------
 tools/perf/util/probe-event.h |    8 +
 tools/perf/util/symbol.c      |   10 +
 tools/perf/util/symbol.h      |    1 
 5 files changed, 334 insertions(+), 82 deletions(-)
tools/perf/builtin-probe.c +9 -1
index 98db08f..a90ee01 100644
@@ -57,6 +57,7 @@ static struct {
 	bool show_ext_vars;
 	bool show_funcs;
 	bool mod_events;
+	bool uprobes;
 	int nevents;
 	struct perf_probe_event events[MAX_PROBES];
 	struct strlist *dellist;
@@ -78,6 +79,7 @@ static int parse_probe_event(const char *str)
 		return -1;
 	}
 
+	pev->uprobes = params.uprobes;
 	/* Parse a perf-probe command into event */
 	ret = parse_perf_probe_command(str, pev);
 	pr_debug("%d arguments\n", pev->nargs);
@@ -249,6 +251,8 @@ static const struct option options[] = {
 		 "Set how many probe points can be found for a probe."),
 	OPT_BOOLEAN('F', "funcs", &params.show_funcs,
 		    "Show potential probe-able functions."),
+	OPT_BOOLEAN('u', "uprobes", &params.uprobes,
+		    "user space probe events"),
 	OPT_CALLBACK('\0', "filter", NULL,
 		     "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
 		     "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
@@ -304,6 +308,10 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
 			pr_err("  Error: Don't use --list with --funcs.\n");
 			usage_with_options(probe_usage, options);
 		}
+		if (params.uprobes) {
+			pr_warning("  Error: Don't use --list with --uprobes.\n");
+			usage_with_options(probe_usage, options);
+		}
 		ret = show_perf_probe_events();
 		if (ret < 0)
 			pr_err("  Error: Failed to show event list. (%d)\n",
@@ -337,7 +345,7 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
 	}
 
 #ifdef DWARF_SUPPORT
-	if (params.show_lines) {
+	if (params.show_lines && !params.uprobes) {
 		if (params.mod_events) {
 			pr_err("  Error: Don't use --line with"
 			       " --add/--del.\n");
tools/perf/util/probe-event.c +310 -77

616 lines not on this page · raw message

tools/perf/util/probe-event.h +5 -3
index 3434fc9..365e016 100644
@@ -7,7 +7,7 @@
 
 extern bool probe_event_dry_run;
 
-/* kprobe-tracer tracing point */
+/* kprobe-tracer and uprobe-tracer tracing point */
 struct probe_trace_point {
 	char		*symbol;	/* Base symbol */
 	unsigned long	offset;		/* Offset from symbol */
@@ -20,7 +20,7 @@ struct probe_trace_arg_ref {
 	long				offset;	/* Offset value */
 };
 
-/* kprobe-tracer tracing argument */
+/* kprobe-tracer and uprobe-tracer tracing argument */
 struct probe_trace_arg {
 	char				*name;	/* Argument name */
 	char				*value;	/* Base value */
@@ -28,12 +28,13 @@ struct probe_trace_arg {
 	struct probe_trace_arg_ref	*ref;	/* Referencing offset */
 };
 
-/* kprobe-tracer tracing event (point + arg) */
+/* kprobe-tracer and uprobe-tracer tracing event (point + arg) */
 struct probe_trace_event {
 	char				*event;	/* Event name */
 	char				*group;	/* Group name */
 	struct probe_trace_point	point;	/* Trace point */
 	int				nargs;	/* Number of args */
+	bool				uprobes;	/* uprobes only */
 	struct probe_trace_arg		*args;	/* Arguments */
 };
 
@@ -69,6 +70,7 @@ struct perf_probe_event {
 	char			*group;	/* Group name */
 	struct perf_probe_point	point;	/* Probe point */
 	int			nargs;	/* Number of arguments */
+	bool			uprobes;
 	struct perf_probe_arg	*args;	/* Arguments */
 };
 
tools/perf/util/symbol.c +9 -1
index eec1963..6a52022 100644
@@ -565,7 +565,7 @@ static int dso__split_kallsyms(struct dso *dso, struct map *map,
 	struct machine *machine = kmaps->machine;
 	struct map *curr_map = map;
 	struct symbol *pos;
-	int count = 0, moved = 0;	
+	int count = 0, moved = 0;
 	struct rb_root *root = &dso->symbols[map->type];
 	struct rb_node *next = rb_first(root);
 	int kernel_range = 0;
@@ -2665,3 +2665,11 @@ int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
 
 	return ret;
 }
+
+struct map *dso__new_map(const char *name)
+{
+	struct dso *dso = dso__new(name);
+	struct map *map = map__new2(0, dso, MAP__FUNCTION);
+
+	return map;
+}
tools/perf/util/symbol.h +1 -0
index 325ee36..8824d57 100644
@@ -215,6 +215,7 @@ void dso__set_long_name(struct dso *dso, char *name);
 void dso__set_build_id(struct dso *dso, void *build_id);
 void dso__read_running_kernel_build_id(struct dso *dso,
 				       struct machine *machine);
+struct map *dso__new_map(const char *name);
 struct symbol *dso__find_symbol(struct dso *dso, enum map_type type,
 				u64 addr);
 struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,

--
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>

#103 lore raw

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

Quoted 3 lines · Srikar Dronamraju

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

Nice. Does this require full debug info for symbolic probes, or can it also work with simple symbolc information?

--
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>

#104 lore raw

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Srikar Dronamraju

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

Nice. Does this require full debug info for symbolic probes, or can it also work with simple symbolc information?

It works only with symbol information for now.
It doesn't (yet) know how to use debuginfo :-)

Ananth

--
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>

#105 lore raw

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

Em Tue, Jun 07, 2011 at 07:08:53PM +0530, Ananth N Mavinakayanahalli escreveu:

Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Srikar Dronamraju
Quoted 2 lines

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

Nice. Does this require full debug info for symbolic probes, or can it also work with simple symbolc information?

It works only with symbol information for now.
It doesn't (yet) know how to use debuginfo :-)

'perf probe' uses perf symbol library, so it really don't have to know from where symbol resolution information is obtained, only if it needs things that are _just_ on debuginfo, such as line information, etc.

But then that is also already supported in 'perf probe'.

Or is there something else in particular you're thinking?

- Arnaldo

--
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>

#106 lore raw

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

Quoted 12 lines · Arnaldo Carvalho de Melo

Em Tue, Jun 07, 2011 at 07:08:53PM +0530, Ananth N Mavinakayanahalli escreveu:

Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Srikar Dronamraju
Quoted 2 lines

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

Nice. Does this require full debug info for symbolic probes, or can it also work with simple symbolc information?

It works only with symbol information for now.
It doesn't (yet) know how to use debuginfo :-)

Quoted 8 lines

'perf probe' uses perf symbol library, so it really don't have to know from where symbol resolution information is obtained, only if it needs things that are _just_ on debuginfo, such as line information, etc.

But then that is also already supported in 'perf probe'.

Or is there something else in particular you're thinking?

What Ananth was saying was that perf probe for uprobes still has to take
advantage of tracing using line number; Also it is still restricted to
showing only register contents and we still have to add support for variables. I know that perf symbol library has support for that just that we have to enable it for uprobes.

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>
#107 lore raw

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

In reply to Ananth N Mavinakayanahalli
Quoted 11 lines · Ananth N Mavinakayanahalli
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Srikar Dronamraju

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

Nice. Does this require full debug info for symbolic probes, or can it also work with simple symbolc information?

It works only with symbol information for now.
It doesn't (yet) know how to use debuginfo :-)

So we can enhance it to use debuginfo for obtaining variables or line numbers from debuginfo. since probe-finder already has the analysis part, I think it's not hard to support userspace apps.

Thanks!

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>
#108 lore raw

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

In reply to Srikar Dronamraju
Quoted 2 lines · Srikar Dronamraju

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

Hi Srikar,

This seems to have an issue with multiple active uprobes, whereas the v3 patchset handled this fine. I haven't tracked down the exact code difference yet, but here's an example transcript of what I'm seeing:

# perf probe -l
  probe_zsh:main       (on /bin/zsh:0x000000000000e3f0)
  probe_zsh:zalloc     (on /bin/zsh:0x0000000000051120)
  probe_zsh:zfree      (on /bin/zsh:0x0000000000051c70)
# perf stat -e probe_zsh:main zsh -c true

Performance counter stats for 'zsh -c true':

                 1 probe_zsh:main

       0.029387785 seconds time elapsed
# perf stat -e probe_zsh:zalloc zsh -c true

Performance counter stats for 'zsh -c true':

               605 probe_zsh:zalloc

       0.043836002 seconds time elapsed
# perf stat -e probe_zsh:zfree zsh -c true

Performance counter stats for 'zsh -c true':

                36 probe_zsh:zfree

       0.029445890 seconds time elapsed
# perf stat -e probe_zsh:* zsh -c true

Performance counter stats for 'zsh -c true':

                 0 probe_zsh:zalloc
                 1 probe_zsh:main
                 0 probe_zsh:zfree

       0.030912587 seconds time elapsed
# perf stat -e probe_zsh:z* zsh -c true

Performance counter stats for 'zsh -c true':

               605 probe_zsh:zalloc
                 0 probe_zsh:zfree

       0.043774671 seconds time elapsed

It seems like among the selected probes, only one with the lowest offset ever gets hit. Any ideas?

Thanks,
Josh

--
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>

#109 lore raw

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

Quoted 6 lines

Hi Srikar,

This seems to have an issue with multiple active uprobes, whereas the v3 patchset handled this fine. I haven't tracked down the exact code difference yet, but here's an example transcript of what I'm seeing:

Okay, Thanks for taking a look and trying it out. Since you have given a way to reproduce the problem, I will try and post the fix.

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>
#110 lore raw

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

In reply to Srikar Dronamraju
Quoted 6 lines · Srikar Dronamraju

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

[ Probing a function in the executable using function name ]
-------------------------------------------------------------
[root@localhost ~]# perf probe -u zfree@/bin/zsh

Hmm, here, I have concern about the interface inconsistency of the probe point syntax.

Since perf probe already supports debuginfo analysis, it accepts following syntax;

[EVENT=]FUNC[@SRC][:RLN|+OFFS|%return|;PTN] [ARG ...]

Thus, The "@" should take a source file path, not binary path.

I think -u option should have a path of the target binary, as below

# perf probe -u /bin/zsh -a zfree

This will allow perf-probe to support user-space debuginfo analysis. With it, we can do as below;

# perf probe -u /bin/zsh -a zfree@foo/bar.c:10

Please try to update tools/perf/Documentation/perf-probe.txt too, then you'll see how the new syntax is different from current one :)

Thanks,

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>
#111 lore raw

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

Hey Masami,
[ I have restricted the number of people in CC to those people who are closely following perf. Please feel free to add people].

Thanks for the review.

Quoted 10 lines
Quoted 6 lines

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

[ Probing a function in the executable using function name ]
-------------------------------------------------------------
[root@localhost ~]# perf probe -u zfree@/bin/zsh

Hmm, here, I have concern about the interface inconsistency of the probe point syntax.

Quoted 7 lines

Since perf probe already supports debuginfo analysis, it accepts following syntax;

[EVENT=]FUNC[@SRC][:RLN|+OFFS|%return|;PTN] [ARG ...]

Thus, The "@" should take a source file path, not binary path.

All the discussions in this forum happened with respect to <function>@<executable> form (esp since we started with the inode based uprobes approach). Thats why I chose the
perf probe -u func@execfile. This also gave us a flexibility to define two probes in two different executables in the same session/command. Something like
perf probe -u func1@exec1 func2@exec2.

However I am okay to change to your suggested syntax which is "perf probe -u /bin/zsh -a zfree"

Quoted 3 lines

I think -u option should have a path of the target binary, as below

# perf probe -u /bin/zsh -a zfree

Will --uprobe work as the long name option for -u or do you suggest something else?

Quoted 4 lines

This will allow perf-probe to support user-space debuginfo analysis. With it, we can do as below;

While we discuss this I would also like to get your thoughts on couple of issues.

1. Its okay to use a switch like "-u" that restricts the probe definition session to just the user space tracing. i.e We wont be able to define a probe in user space executable and also a kernel probe in one single session.

2. Its okay to have just "target" and a flag to identify if the session is a userspace probing session, instead of having separate fields for userspace executable path and the module name.
i.e you are okay with patch "perf: rename target_module to target"

3. Currently perf_probe_point has one field "file" that was only used to refer to source file. Uprobes overrides it to use it for executable file too. One approach would be to add another field something like "execfile" that we use to refer to executable files and use the current field "file" for source files.
Also do you suggest that we rename the current
file field to "srcfile?

Based on the outcomes of this discussion, I will modify the code and Documentation.

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

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

Quoted 24 lines · Srikar Dronamraju

Hey Masami,
[ I have restricted the number of people in CC to those people who are closely following perf. Please feel free to add people].

Thanks for the review.

Quoted 10 lines
Quoted 6 lines

Enhances perf probe to user space executables and libraries. Provides very basic support for uprobes.

[ Probing a function in the executable using function name ]
-------------------------------------------------------------
[root@localhost ~]# perf probe -u zfree@/bin/zsh

Hmm, here, I have concern about the interface inconsistency of the probe point syntax.

Quoted 7 lines

Since perf probe already supports debuginfo analysis, it accepts following syntax;

[EVENT=]FUNC[@SRC][:RLN|+OFFS|%return|;PTN] [ARG ...]

Thus, The "@" should take a source file path, not binary path.

Quoted 7 lines

All the discussions in this forum happened with respect to <function>@<executable> form (esp since we started with the inode based uprobes approach). Thats why I chose the
perf probe -u func@execfile. This also gave us a flexibility to define two probes in two different executables in the same session/command. Something like
perf probe -u func1@exec1 func2@exec2.

I see, and sorry for changing my mind, but I think '-u execname symbol' style finally be good also for uprobe users when perf probe -u supports debuginfo, since they don't need to learn a different syntax.

Quoted 3 lines

However I am okay to change to your suggested syntax which is "perf probe -u /bin/zsh -a zfree"

Thanks!

Quoted 7 lines
Quoted 4 lines

I think -u option should have a path of the target binary, as below

# perf probe -u /bin/zsh -a zfree

Will --uprobe work as the long name option for -u or do you suggest something else?

Hmm, good question. Maybe we can use -x|--exec to define a uprobe event, because there is no need to give an executable file for kprobes events.

# so that -x implies user space event on given execfile
Quoted 11 lines
Quoted 3 lines

This will allow perf-probe to support user-space debuginfo analysis. With it, we can do as below;

While we discuss this I would also like to get your thoughts on couple of issues.

1. Its okay to use a switch like "-u" that restricts the probe definition session to just the user space tracing. i.e We wont be able to define a probe in user space executable and also a kernel probe in one single session.

I'm OK for that. The usage of perf probe is different from systemtap; perf probe do one thing at a time, systemtap do everything at a time.

If someone want to define various probes in the kernel, they may have to call perf probe several times. And I'm OK for that, because all probe definition should be done before recording.

So with perf probe, tracing is done following below 3 steps.
1. Definition
2. Recording
3. Analysis

And each phase is done separately.

Quoted 4 lines

2. Its okay to have just "target" and a flag to identify if the session is a userspace probing session, instead of having separate fields for userspace executable path and the module name.
i.e you are okay with patch "perf: rename target_module to target"

Ah, that's good to me. Actually, I'm now trying to expand "target_module" to receive a path of offline module. It'll be better for that too.:-)

Quoted 7 lines

3. Currently perf_probe_point has one field "file" that was only used to refer to source file. Uprobes overrides it to use it for executable file too. One approach would be to add another field something like "execfile" that we use to refer to executable files and use the current field "file" for source files.
Also do you suggest that we rename the current
file field to "srcfile?

Ah, I see. From the viewpoint of implementation, we just introduce a bool flag, which indicates user-probe, and a union, which has a "char *module" and "char *exec". :)

However, Maybe we'd better look this more carefully. Here, we have a problem with listing userspace probes (I mean how perf probe can list up the probes which is on a user app)

Currently, it just ignores module name if a probe on a module.
probe:fuse_do_open (on fuse_do_open@ksrc/linux-2.6/fs/fuse/file.c with isdir)

One possible solution is to show the module name right before the symbol as same as the kernel does.

probe:fuse_do_open (on fuse:fuse_do_open@ksrc/linux-2.6/fs/fuse/file.c with
isdir)

It seems that current your proposal doing same thing

probe_zsh:zfree (on /bin/zsh:0x45400)

Another way is to show it more verbosely, like below.

probe:fuse_do_open (at fuse_do_open@ksrc/linux-2.6/fs/fuse/file.c with isdir
on fuse)
probe_zsh:zfree (at 0x45400 on /bin/zsh)

Any thought?

Quoted 4 lines

Based on the outcomes of this discussion, I will modify the code and Documentation.

Thank you!

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

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

Quoted 16 lines
Quoted 8 lines

All the discussions in this forum happened with respect to <function>@<executable> form (esp since we started with the inode based uprobes approach). Thats why I chose the
perf probe -u func@execfile. This also gave us a flexibility to define two probes in two different executables in the same session/command. Something like
perf probe -u func1@exec1 func2@exec2.

I see, and sorry for changing my mind, but I think '-u execname symbol' style finally be good also for uprobe users when perf probe -u supports debuginfo, since they don't need to learn a different syntax.

Quoted 3 lines
Quoted 3 lines

I think -u option should have a path of the target binary, as below

Quoted 9 lines
Quoted 4 lines
Quoted 1 line
# perf probe -u /bin/zsh -a zfree

Will --uprobe work as the long name option for -u or do you suggest something else?

Hmm, good question. Maybe we can use -x|--exec to define a uprobe event, because there is no need to give an executable file for kprobes events.

# so that -x implies user space event on given execfile

Okay, then lets stick with perf probe -x executable <function-name> then.

Quoted 6 lines
Quoted 5 lines

1. Its okay to use a switch like "-u" that restricts the probe definition session to just the user space tracing. i.e We wont be able to define a probe in user space executable and also a kernel probe in one single session.

Quoted 6 lines

I'm OK for that. The usage of perf probe is different from systemtap; perf probe do one thing at a time, systemtap do everything at a time.

If someone want to define various probes in the kernel, they may have to call perf probe several times. And I'm OK for that, because all probe definition should be done before recording.

Right.

Quoted 8 lines

So with perf probe, tracing is done following below 3 steps.
1. Definition
2. Recording
3. Analysis

And each phase is done separately.

Quoted 7 lines
Quoted 4 lines

2. Its okay to have just "target" and a flag to identify if the session is a userspace probing session, instead of having separate fields for userspace executable path and the module name.
i.e you are okay with patch "perf: rename target_module to target"

Ah, that's good to me. Actually, I'm now trying to expand "target_module" to receive a path of offline module. It'll be better for that too.:-)

Okay, works.

Quoted 9 lines
Quoted 8 lines

3. Currently perf_probe_point has one field "file" that was only used to refer to source file. Uprobes overrides it to use it for executable file too. One approach would be to add another field something like "execfile" that we use to refer to executable files and use the current field "file" for source files.
Also do you suggest that we rename the current
file field to "srcfile?

Quoted 3 lines

Ah, I see. From the viewpoint of implementation, we just introduce a bool flag, which indicates user-probe, and a union, which has a "char *module" and "char *exec". :)

Okay,

Quoted 5 lines

However, Maybe we'd better look this more carefully. Here, we have a problem with listing userspace probes (I mean how perf probe can list up the probes which is on a user app)

Quoted 8 lines

Currently, it just ignores module name if a probe on a module.
probe:fuse_do_open (on fuse_do_open@ksrc/linux-2.6/fs/fuse/file.c with isdir)

One possible solution is to show the module name right before the symbol as same as the kernel does.

probe:fuse_do_open (on fuse:fuse_do_open@ksrc/linux-2.6/fs/fuse/file.c with
isdir)

This looks better to me.

Quoted 11 lines

It seems that current your proposal doing same thing

probe_zsh:zfree (on /bin/zsh:0x45400)

Another way is to show it more verbosely, like below.

probe:fuse_do_open (at fuse_do_open@ksrc/linux-2.6/fs/fuse/file.c with isdir
on fuse)
probe_zsh:zfree (at 0x45400 on /bin/zsh)

But I am okay with changing to this format too.

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

Re: [PATCH v4 3.0-rc2-tip 20/22] 20: perf: perf interface for uprobes

Quoted 7 lines · Srikar Dronamraju
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

I think -u option should have a path of the target binary, as below

# perf probe -u /bin/zsh -a zfree

Will --uprobe work as the long name option for -u or do you suggest something else?

Quoted 7 lines
Quoted 4 lines

Hmm, good question. Maybe we can use -x|--exec to define a uprobe event, because there is no need to give an executable file for kprobes events.

# so that -x implies user space event on given execfile

Okay, then lets stick with perf probe -x executable <function-name> then.

Thanks:-)

Quoted 7 lines
Quoted 7 lines

However, Maybe we'd better look this more carefully. Here, we have a problem with listing userspace probes (I mean how perf probe can list up the probes which is on a user app)

Currently, it just ignores module name if a probe on a module.
probe:fuse_do_open (on fuse_do_open@ksrc/linux-2.6/fs/fuse/file.c with isdir)

Quoted 7 lines
Quoted 5 lines

One possible solution is to show the module name right before the symbol as same as the kernel does.

probe:fuse_do_open (on fuse:fuse_do_open@ksrc/linux-2.6/fs/fuse/file.c with
isdir)

This looks better to me.

OK, this will be good for starting point. If someone complains, we can switch to below format.

Quoted 9 lines
Quoted 6 lines

Another way is to show it more verbosely, like below.

probe:fuse_do_open (at fuse_do_open@ksrc/linux-2.6/fs/fuse/file.c with isdir
on fuse)
probe_zsh:zfree (at 0x45400 on /bin/zsh)

But I am okay with changing to this format too.

Thanks!

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

PATCH v4 21/22 21: perf: show possible probes in a given executable file or library.

In reply to Srikar Dronamraju

Enhances -F/--funcs option of "perf probe" to list possible probe points in an executable file or library. A new option -e/--exe specifies the path of the executable or library.

Show last 10 functions in /bin/zsh.

# perf probe -F -u -e /bin/zsh | tail
zstrtol
ztrcmp
ztrdup
ztrduppfx
ztrftime
ztrlen
ztrncpy
ztrsub
zwarn
zwarnnam

Show first 10 functions in /lib/libc.so.6

# perf probe -u -F -e /lib/libc.so.6 | head
_IO_adjust_column
_IO_adjust_wcolumn
_IO_default_doallocate
_IO_default_finish
_IO_default_pbackfail
_IO_default_uflow
_IO_default_xsgetn
_IO_default_xsputn
_IO_do_write@@GLIBC_2.2.5
_IO_doallocbuf
---
 tools/perf/builtin-probe.c    |   63 +++++++++++++++++++++++++++++++++++++----
 tools/perf/util/probe-event.c |   56 ++++++++++++++++++++++++++++--------
 tools/perf/util/probe-event.h |    4 +--
 3 files changed, 102 insertions(+), 21 deletions(-)
tools/perf/builtin-probe.c +57 -6
index a90ee01..6aff85c 100644
@@ -185,6 +185,55 @@ static int opt_set_filter(const struct option *opt __used,
 	return 0;
 }
 
+static int opt_set_executable(const struct option *opt __used,
+			  const char *str, int unset __used)
+{
+	if (params.target || !str)
+		return -EINVAL;
+
+	if (params.uprobes) {
+		pr_err("  Error: Don't use -m with --uprobes.\n");
+		return -EINVAL;
+	}
+
+	if (str) {
+		params.target = str;
+		params.uprobes = true;
+	}
+	return 0;
+}
+
+#ifdef DWARF_SUPPORT
+static int opt_set_module(const struct option *opt __used,
+			  const char *str, int unset __used)
+{
+	if (params.target || !str)
+		return -EINVAL;
+
+	if (params.uprobes) {
+		pr_err("  Error: Don't use -m with --uprobes.\n");
+		return -EINVAL;
+	}
+
+	if (str)
+		params.target = str;
+
+	return 0;
+}
+#endif
+
+static int opt_set_uprobes(const struct option *opt __used,
+			  const char *str __used, int unset __used)
+{
+	if (params.target) {
+		pr_err("  Error: Don't use --uprobes with -x/-m.\n");
+		return -EINVAL;
+	}
+
+	params.uprobes = true;
+	return 0;
+}
+
 static const char * const probe_usage[] = {
 	"perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
 	"perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
@@ -243,16 +292,18 @@ 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,
-		   "modname", "target module name"),
+	OPT_CALLBACK('m', "module", NULL, "modname", "target module name",
+		   opt_set_module),
 #endif
 	OPT__DRY_RUN(&probe_event_dry_run),
 	OPT_INTEGER('\0', "max-probes", &params.max_probe_points,
 		 "Set how many probe points can be found for a probe."),
 	OPT_BOOLEAN('F', "funcs", &params.show_funcs,
 		    "Show potential probe-able functions."),
-	OPT_BOOLEAN('u', "uprobes", &params.uprobes,
-		    "user space probe events"),
+	OPT_CALLBACK_NOOPT('u', "uprobes", NULL, NULL,
+		    "user space probe events", opt_set_uprobes),
+	OPT_CALLBACK('x', "exe", NULL, "/path/to/absolute/relative/file",
+		    "target executable name", opt_set_executable),
 	OPT_CALLBACK('\0', "filter", NULL,
 		     "[!]FILTER", "Set a filter (with --vars/funcs only)\n"
 		     "\t\t\t(default: \"" DEFAULT_VAR_FILTER "\" for --vars,\n"
@@ -335,8 +386,8 @@ 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,
-					   params.filter);
+		ret = show_available_funcs(params.target, params.filter,
+					params.uprobes);
 		strfilter__delete(params.filter);
 		if (ret < 0)
 			pr_err("  Error: Failed to show functions."
tools/perf/util/probe-event.c +43 -13

87 lines not on this page · raw message

tools/perf/util/probe-event.h +2 -2
index 365e016..5199df4 100644
@@ -130,8 +130,8 @@ extern int show_line_range(struct line_range *lr, const char *module);
 extern int show_available_vars(struct perf_probe_event *pevs, int npevs,
 			       int max_probe_points, const char *module,
 			       struct strfilter *filter, bool externs);
-extern int show_available_funcs(const char *module, struct strfilter *filter);
-
+extern int show_available_funcs(const char *module, struct strfilter *filter,
+				bool user);
 
 /* Maximum index number of event-name postfix */
 #define MAX_EVENT_INDEX	1024

--
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>

#116 lore raw

PATCH v4 22/22 22: perf: Documentation for perf uprobes

In reply to Srikar Dronamraju

Modify perf-probe.txt to include uprobe documentation

---
 tools/perf/Documentation/perf-probe.txt |   21 ++++++++++++++++++++-
 1 files changed, 20 insertions(+), 1 deletions(-)
tools/perf/Documentation/perf-probe.txt +20 -1
index 02bafce..3db8d25 100644
@@ -76,6 +76,8 @@ OPTIONS
 -F::
 --funcs::
 	Show available functions in given module or kernel.
+	With -x/--exe, can also list functions in a user space executable /
+	shared library.
 
 --filter=FILTER::
 	(Only for --vars and --funcs) Set filter. FILTER is a combination of glob
@@ -96,12 +98,21 @@ OPTIONS
 --max-probes::
 	Set the maximum number of probe points for an event. Default is 128.
 
+-u::
+--uprobes::
+	Specify a uprobe based probe point.
+
+-x::
+--exe=PATH::
+	Specify path to the executable or shared library file for user
+	space tracing. Used with --funcs option only.
+
 PROBE SYNTAX
 ------------
 Probe points are defined by following syntax.
 
     1) Define event based on function name
-     [EVENT=]FUNC[@SRC][:RLN|+OFFS|%return|;PTN] [ARG ...]
+     [EVENT=]FUNC[@EXE][@SRC][:RLN|+OFFS|%return|;PTN] [ARG ...]
 
     2) Define event based on source file with line number
      [EVENT=]SRC:ALN [ARG ...]
@@ -112,6 +123,7 @@ Probe points are defined by following syntax.
 
 'EVENT' specifies the name of new event, if omitted, it will be set the name of the probed function. Currently, event group name is set as 'probe'.
 'FUNC' specifies a probed function name, and it may have one of the following options; '+OFFS' is the offset from function entry address in bytes, ':RLN' is the relative-line number from function entry line, and '%return' means that it probes function return. And ';PTN' means lazy matching pattern (see LAZY MATCHING). Note that ';PTN' must be the end of the probe point definition.  In addition, '@SRC' specifies a source file which has that function.
+'EXE' specifies the absolute or relative path of the user space executable or user space shared library.
 It is also possible to specify a probe point by the source line number or lazy matching by using 'SRC:ALN' or 'SRC;PTN' syntax, where 'SRC' is the source file path, ':ALN' is the line number and ';PTN' is the lazy matching pattern.
 'ARG' specifies the arguments of this probe point, (see PROBE ARGUMENT).
 
@@ -180,6 +192,13 @@ Delete all probes on schedule().
 
  ./perf probe --del='schedule*'
 
+Add probes at zfree() function on /bin/zsh
+
+ ./perf probe -u zfree@/bin/zsh
+
+Add probes at malloc() function on libc
+
+ ./perf probe -u malloc@/lib/libc.so.6
 
 SEE ALSO
 --------

--
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>

#117 lore raw

Re: [PATCH v4 3.0-rc2-tip 22/22] 22: perf: Documentation for perf uprobes

Quoted 1 line · Srikar Dronamraju

Modify perf-probe.txt to include uprobe documentation

Sorry, Nak this. I hope to see better uniformed interface for both of uprobe and kprobe, even after we support debuginfo for uprobe.

I mean, if uprobe always requires a user-space binary, it should be specified with -u option instead of --exe and @EXE.

I think this simplifies perf probe usage, it implies;

# perf probe [-k KERN] [...]  // Control probes in kernel
# perf probe -m MOD [...]     // Control probes in MOD module
# perf probe -u EXE [...]     // Control probes on EXE file

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>
maintainer
#118 lore raw
In reply to Srikar Dronamraju
Quoted 4 lines · Srikar Dronamraju

- Breakpoint handling should co-exist with singlestep/blockstep from another tracer/debugger.
- Queue and dequeue signals delivered from the singlestep till completion of postprocessing.

These two are important to sort before we can think of merging this right?

--
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>

#119 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 4 lines · Srikar Dronamraju

- Breakpoint handling should co-exist with singlestep/blockstep from another tracer/debugger.
- Queue and dequeue signals delivered from the singlestep till completion of postprocessing.

These two are important to sort before we can think of merging this right?

Yup.

Guess Srikar missed updating this part, but the first of the issues (sstep/blockstep) is now fixed. Signal queueing is a work-in-progress.

Ananth

--
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>

#120 lore raw
In reply to Peter Zijlstra
Quoted 3 lines · Peter Zijlstra
Quoted 2 lines · Srikar Dronamraju

- Breakpoint handling should co-exist with singlestep/blockstep from another tracer/debugger.

We can remove this now.
Previous to this patchset the post notifier would run in interrupt context hence we couldnt call user_disable_single_step

However from this patchset, (due to changes to do away with per task slot), we run the post notifier in task context. Hence we can now call user_enable_single_step/user_disable_single_step which does the right thing.

Please correct me if I am missing.

Quoted 3 lines
Quoted 2 lines

- Queue and dequeue signals delivered from the singlestep till completion of postprocessing.

I am working towards this.

Quoted 3 lines

These two are important to sort before we can think of merging this right?

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>
maintainer
#121 lore raw
In reply to Srikar Dronamraju
Quoted 1 line · Srikar Dronamraju

Please do provide your valuable comments.

Your patch split-up is complete crap. I'm about to simply fold all of them just to be able to read anything.

The split-up appears to do its best to make it absolutely impossible to get a sane overview of things, tons of things are out of order, either it relies on future patches filling out things or modifies stuff in previous patches.

Its a complete pain to read..

--
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>

#122 lore raw
Quoted 6 lines · Peter Zijlstra
Quoted 1 line · Srikar Dronamraju

Please do provide your valuable comments.

Your patch split-up is complete crap. I'm about to simply fold all of them just to be able to read anything.

Quoted 6 lines

The split-up appears to do its best to make it absolutely impossible to get a sane overview of things, tons of things are out of order, either it relies on future patches filling out things or modifies stuff in previous patches.

Its a complete pain to read..

Maybe for the part of uprobe itself, you are right. But I hope tracing/perf parts are separated from that.

Thanks,

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>
#123 lore raw
In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 1 line · Srikar Dronamraju

Please do provide your valuable comments.

Your patch split-up is complete crap. I'm about to simply fold all of them just to be able to read anything.

Quoted 7 lines

The split-up appears to do its best to make it absolutely impossible to get a sane overview of things, tons of things are out of order, either it relies on future patches filling out things or modifies stuff in previous patches.

Its a complete pain to read..

We have two options,

Combine patches 2,3,4 and patches 6-7 and patches 9-10-11 or
Combine patches from 1 to 15 into one patch.

Please let me know your preference.

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>