public inbox for systemtap@sourceware.org
 help / color / mirror / Atom feed
From: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
To: Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@elte.hu>
Cc: Steven Rostedt <rostedt@goodmis.org>,
	       Srikar Dronamraju <srikar@linux.vnet.ibm.com>,
	       Linux-mm <linux-mm@kvack.org>,
	       Arnaldo Carvalho de Melo <acme@infradead.org>,
	       Linus Torvalds <torvalds@linux-foundation.org>,
	       Christoph Hellwig <hch@infradead.org>,
	       Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	       Ananth N Mavinakayanahalli <ananth@in.ibm.com>,
	       Oleg Nesterov <oleg@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>,
	       SystemTap <systemtap@sources.redhat.com>,
	       Jim Keniston <jkenisto@linux.vnet.ibm.com>,
	       Roland McGrath <roland@hack.frob.com>,
	       Andi Kleen <andi@firstfloor.org>,
	       Andrew Morton <akpm@linux-foundation.org>,
	       "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Subject: [PATCH v2 2.6.38-rc8-tip 5/20]  5: Uprobes: register/unregister probes.
Date: Mon, 14 Mar 2011 13:40:00 -0000	[thread overview]
Message-ID: <20110314133454.27435.81020.sendpatchset@localhost6.localdomain6> (raw)
In-Reply-To: <20110314133403.27435.7901.sendpatchset@localhost6.localdomain6>


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.

Also would be interested to know if there are ways to call
replace_page without having to take mmap_sem.

Signed-off-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
---
 include/linux/mm_types.h |    5 +
 include/linux/uprobes.h  |   32 ++++++++
 kernel/uprobes.c         |  195 +++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 221 insertions(+), 11 deletions(-)

diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 26bc4e2..96e4a77 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -315,6 +315,11 @@ struct mm_struct {
 #endif
 	/* How many tasks sharing this mm are OOM_DISABLE */
 	atomic_t oom_disable_count;
+#ifdef CONFIG_UPROBES
+	unsigned long uprobes_vaddr;
+	struct list_head uprobes_list;
+	atomic_t uprobes_count;
+#endif
 };
 
 /* Future-safe accessor for struct mm_struct's cpu_vm_mask. */
diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
index f422bc6..8654a06 100644
--- a/include/linux/uprobes.h
+++ b/include/linux/uprobes.h
@@ -31,6 +31,7 @@
  * ARCH_SUPPORTS_UPROBES is not defined.
  */
 typedef u8 uprobe_opcode_t;
+struct uprobe_arch_info	{};		/* arch specific info*/
 
 /* Post-execution fixups.  Some architectures may define others. */
 #endif /* CONFIG_ARCH_SUPPORTS_UPROBES */
@@ -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();
@@ -79,4 +93,22 @@ struct uprobe_consumer {
  *	You may modify @user_bkpt->insn (e.g., the x86_64 port does this
  *	for rip-relative instructions).
  */
+
+#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 */
diff --git a/kernel/uprobes.c b/kernel/uprobes.c
index 6e692a8..4dbb90f 100644
--- a/kernel/uprobes.c
+++ b/kernel/uprobes.c
@@ -32,17 +32,6 @@
 #include <linux/uprobes.h>
 #include <linux/rmap.h> /* needed for anon_vma_prepare */
 
-struct uprobe {
-	struct rb_node		rb_node;	/* node in the rb tree */
-	atomic_t		ref;		/* lifetime muck */
-	struct rw_semaphore	consumer_rwsem;
-	struct uprobe_consumer	*consumers;
-	struct inode		*inode;		/* we hold a ref */
-	loff_t			offset;
-	u8			insn[MAX_UINSN_BYTES];
-	u16			fixups;
-};
-
 static int valid_vma(struct vm_area_struct *vma)
 {
 	if (!vma->vm_file)
@@ -445,3 +434,187 @@ static int del_consumer(struct uprobe *uprobe,
 	up_write(&uprobe->consumer_rwsem);
 	return ret;
 }
+
+static int install_uprobe(struct mm_struct *mm, struct uprobe *uprobe)
+{
+	int ret = 0;
+
+	/*TODO: install breakpoint */
+	if (!ret)
+		atomic_inc(&mm->uprobes_count);
+	return ret;
+}
+
+static int remove_uprobe(struct mm_struct *mm, struct uprobe *uprobe)
+{
+	int ret = 0;
+
+	/*TODO: remove breakpoint */
+	if (!ret)
+		atomic_dec(&mm->uprobes_count);
+
+	return ret;
+}
+
+/* Returns 0 if it can install one probe */
+int register_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;
+	int ret = -1;
+
+	if (!inode || !consumer || consumer->next)
+		return -EINVAL;
+	uprobe = uprobes_add(inode, offset);
+	INIT_LIST_HEAD(&tmp_list);
+
+	mapping = inode->i_mapping;
+
+	mutex_lock(&uprobes_mutex);
+	if (uprobe->consumers) {
+		ret = 0;
+		goto consumers_add;
+	}
+
+	spin_lock(&mapping->i_mmap_lock);
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+		loff_t vaddr;
+
+		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 > ULONG_MAX) {
+			/*
+			 * We cannot have a virtual address that is
+			 * greater than ULONG_MAX
+			 */
+			mmput(mm);
+			continue;
+		}
+		mm->uprobes_vaddr = (unsigned long) vaddr;
+		list_add(&mm->uprobes_list, &tmp_list);
+	}
+	spin_unlock(&mapping->i_mmap_lock);
+
+	if (list_empty(&tmp_list)) {
+		ret = 0;
+		goto consumers_add;
+	}
+	list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list) {
+		down_read(&mm->mmap_sem);
+		if (!install_uprobe(mm, uprobe))
+			ret = 0;
+		list_del(&mm->uprobes_list);
+		up_read(&mm->mmap_sem);
+		mmput(mm);
+	}
+
+consumers_add:
+	add_consumer(uprobe, consumer);
+	mutex_unlock(&uprobes_mutex);
+	put_uprobe(uprobe);
+	return ret;
+}
+
+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;
+	unsigned long flags;
+
+	if (!inode || !consumer)
+		return;
+
+	uprobe = find_uprobe(inode, offset);
+	if (!uprobe) {
+		printk(KERN_ERR "No uprobe found with inode:offset %p %lld\n",
+				inode, offset);
+		return;
+	}
+
+	if (!del_consumer(uprobe, consumer)) {
+		printk(KERN_ERR "No uprobe found with consumer %p\n",
+				consumer);
+		return;
+	}
+
+	INIT_LIST_HEAD(&tmp_list);
+
+	mapping = inode->i_mapping;
+
+	mutex_lock(&uprobes_mutex);
+	if (uprobe->consumers)
+		goto put_unlock;
+
+	spin_lock(&mapping->i_mmap_lock);
+	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, 0) {
+		if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
+			continue;
+
+		mm = vma->vm_mm;
+
+		if (!atomic_read(&mm->uprobes_count)) {
+			mmput(mm);
+			continue;
+		}
+
+		if (valid_vma(vma)) {
+			loff_t vaddr;
+
+			vaddr = vma->vm_start + offset;
+			vaddr -= vma->vm_pgoff << PAGE_SHIFT;
+			if (vaddr > ULONG_MAX) {
+				/*
+				 * We cannot have a virtual address that is
+				 * greater than ULONG_MAX
+				 */
+				mmput(mm);
+				continue;
+			}
+			mm->uprobes_vaddr = (unsigned long) vaddr;
+			list_add(&mm->uprobes_list, &tmp_list);
+		} else
+			mmput(mm);
+	}
+	spin_unlock(&mapping->i_mmap_lock);
+	list_for_each_entry_safe(mm, tmpmm, &tmp_list, uprobes_list) {
+		down_read(&mm->mmap_sem);
+		remove_uprobe(mm, uprobe);
+		list_del(&mm->uprobes_list);
+		up_read(&mm->mmap_sem);
+		mmput(mm);
+	}
+
+	/*
+	 * There could be other threads that could be spinning on
+	 * treelock; some of these threads could be interested in this
+	 * uprobe.  Give these threads a chance to run.
+	 */
+	synchronize_sched();
+	spin_lock_irqsave(&treelock, flags);
+	rb_erase(&uprobe->rb_node, &uprobes_tree);
+	spin_unlock_irqrestore(&treelock, flags);
+	iput(uprobe->inode);
+
+put_unlock:
+	mutex_unlock(&uprobes_mutex);
+	put_uprobe(uprobe);
+}

  parent reply	other threads:[~2011-03-14 13:40 UTC|newest]

Thread overview: 100+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-14 13:40 [PATCH v2 2.6.38-rc8-tip 0/20] 0: Inode based uprobes Srikar Dronamraju
2011-03-14 13:40 ` [PATCH v2 2.6.38-rc8-tip 1/20] 1: mm: Move replace_page() to mm/memory.c Srikar Dronamraju
2011-03-14 14:16   ` Steven Rostedt
2011-03-14 17:08     ` Srikar Dronamraju
2011-03-14 17:14       ` Steven Rostedt
2011-03-14 17:35         ` Srikar Dronamraju
2011-03-14 13:40 ` [PATCH v2 2.6.38-rc8-tip 3/20] 3: uprobes: Breakground page replacement Srikar Dronamraju
2011-03-14 15:29   ` Steven Rostedt
2011-03-14 15:39   ` Steven Rostedt
2011-03-14 17:30     ` Srikar Dronamraju
2011-03-14 18:21       ` Steven Rostedt
2011-03-14 18:22       ` Steven Rostedt
2011-03-14 16:59   ` Stephen Wilson
2011-03-14 17:36     ` Srikar Dronamraju
2011-03-15 13:23   ` Thomas Gleixner
2011-03-15 17:57     ` Srikar Dronamraju
2011-03-15 18:04       ` Thomas Gleixner
2011-03-15 18:13         ` Srikar Dronamraju
2011-03-14 13:40 ` [PATCH v2 2.6.38-rc8-tip 2/20] 2: X86 specific breakpoint definitions Srikar Dronamraju
2011-03-14 13:40 ` [PATCH v2 2.6.38-rc8-tip 4/20] 4: uprobes: Adding and remove a uprobe in a rb tree Srikar Dronamraju
2011-03-15 13:40   ` Thomas Gleixner
2011-03-15 13:43     ` Steven Rostedt
2011-03-15 17:36     ` Srikar Dronamraju
2011-03-15 19:41       ` Thomas Gleixner
2011-03-15 19:46         ` Peter Zijlstra
2011-03-15 22:42           ` Eric Dumazet
2011-03-16  7:52             ` Peter Zijlstra
2011-03-14 13:40 ` Srikar Dronamraju [this message]
2011-03-14 16:00   ` [PATCH v2 2.6.38-rc8-tip 5/20] 5: Uprobes: register/unregister probes Steven Rostedt
2011-03-14 17:38     ` Srikar Dronamraju
2011-03-15 14:30   ` Thomas Gleixner
2011-03-15 17:21     ` Srikar Dronamraju
2011-03-15 17:47       ` Steven Rostedt
2011-03-15 17:51         ` Peter Zijlstra
2011-03-15 18:10           ` Srikar Dronamraju
2011-03-15 18:16             ` Peter Zijlstra
2011-03-18 19:00               ` Srikar Dronamraju
2011-03-15 18:17       ` Thomas Gleixner
2011-03-14 13:41 ` [PATCH v2 2.6.38-rc8-tip 7/20] 7: uprobes: store/restore original instruction Srikar Dronamraju
2011-03-14 18:10   ` Stephen Wilson
2011-03-15  9:29     ` Srikar Dronamraju
2011-03-15 13:48       ` Steven Rostedt
2011-03-15 16:17         ` Stephen Wilson
2011-03-15 16:21           ` Steven Rostedt
2011-03-15 16:24           ` Srikar Dronamraju
2011-03-15 17:58       ` Peter Zijlstra
2011-03-15 18:59         ` Balbir Singh
2011-03-15 19:28           ` Peter Zijlstra
2011-03-15 19:32             ` Steven Rostedt
2011-03-16  5:51               ` Balbir Singh
2011-03-16 17:40                 ` Steven Rostedt
2011-03-15 14:43   ` Thomas Gleixner
2011-03-15 16:31     ` Srikar Dronamraju
2011-03-14 13:41 ` [PATCH v2 2.6.38-rc8-tip 8/20] 8: uprobes: mmap and fork hooks Srikar Dronamraju
2011-03-14 13:41 ` [PATCH v2 2.6.38-rc8-tip 9/20] 9: x86: architecture specific task information Srikar Dronamraju
2011-03-14 13:41 ` [PATCH v2 2.6.38-rc8-tip 6/20] 6: x86: analyze instruction and determine fixups Srikar Dronamraju
2011-03-15 14:38   ` Thomas Gleixner
2011-03-18 18:31     ` Srikar Dronamraju
2011-03-18 18:36       ` Roland McGrath
2011-03-18 18:56         ` Srikar Dronamraju
2011-03-18 19:10           ` Roland McGrath
2011-03-18 19:13           ` Srikar Dronamraju
2011-03-14 13:41 ` [PATCH v2 2.6.38-rc8-tip 10/20] 10: uprobes: task specific information Srikar Dronamraju
2011-03-14 13:42 ` [PATCH v2 2.6.38-rc8-tip 16/20] 16: uprobes: register a notifier for uprobes Srikar Dronamraju
2011-03-15 19:57   ` Stephen Wilson
2011-03-18 19:35     ` Srikar Dronamraju
2011-03-14 13:42 ` [PATCH v2 2.6.38-rc8-tip 11/20] 11: uprobes: slot allocation " Srikar Dronamraju
2011-03-15 19:10   ` Jonathan Corbet
2011-03-16  5:04     ` Srikar Dronamraju
2011-03-15 20:32   ` Stephen Wilson
2011-03-16  4:57     ` Srikar Dronamraju
2011-03-14 13:42 ` [PATCH v2 2.6.38-rc8-tip 12/20] 12: uprobes: get the breakpoint address Srikar Dronamraju
2011-03-14 13:42 ` [PATCH v2 2.6.38-rc8-tip 15/20] 15: x86: uprobes exception notifier for x86 Srikar Dronamraju
2011-03-14 13:42 ` [PATCH v2 2.6.38-rc8-tip 13/20] 13: x86: x86 specific probe handling Srikar Dronamraju
2011-03-14 13:42 ` [PATCH v2 2.6.38-rc8-tip 14/20] 14: uprobes: Handing int3 and singlestep exception Srikar Dronamraju
2011-03-14 13:43 ` [PATCH v2 2.6.38-rc8-tip 17/20] 17: uprobes: filter chain Srikar Dronamraju
2011-03-15 19:51   ` Stephen Wilson
2011-03-18 19:23     ` Srikar Dronamraju
2011-03-18 22:11       ` Stephen Wilson
2011-03-14 13:43 ` [PATCH v2 2.6.38-rc8-tip 19/20] 19: tracing: Extract out common code for kprobes/uprobes traceevents Srikar Dronamraju
2011-03-14 13:43 ` [PATCH v2 2.6.38-rc8-tip 20/20] 20: tracing: uprobes trace_event interface Srikar Dronamraju
2011-03-14 13:43 ` [PATCH v2 2.6.38-rc8-tip 18/20] 18: uprobes: commonly used filters Srikar Dronamraju
2011-03-14 23:31 ` [PATCH v2 2.6.38-rc8-tip 0/20] 0: Inode based uprobes Andrew Morton
2011-03-14 23:48   ` Andi Kleen
2011-03-15  0:23     ` Thomas Gleixner
2011-03-15 18:06       ` Andi Kleen
2011-03-15 20:02         ` Thomas Gleixner
2011-03-15 20:01           ` Steven Rostedt
2011-03-15 20:25             ` Thomas Gleixner
2011-03-15 20:44               ` Steven Rostedt
2011-03-16 17:32                 ` Tom Tromey
2011-03-16 17:44                   ` Steven Rostedt
2011-03-16 17:27               ` Tom Tromey
2011-03-15  1:13   ` Frank Ch. Eigler
2011-03-15  1:36     ` Thomas Gleixner
2011-03-15  5:27       ` Srikar Dronamraju
2011-03-15 11:04         ` Thomas Gleixner
2011-03-15 11:44           ` Srikar Dronamraju
2011-03-15  2:03   ` Srikar Dronamraju
2011-03-15  2:52   ` Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110314133454.27435.81020.sendpatchset@localhost6.localdomain6 \
    --to=srikar@linux.vnet.ibm.com \
    --cc=acme@infradead.org \
    --cc=akpm@linux-foundation.org \
    --cc=ananth@in.ibm.com \
    --cc=andi@firstfloor.org \
    --cc=hch@infradead.org \
    --cc=jkenisto@linux.vnet.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@elte.hu \
    --cc=oleg@redhat.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=roland@hack.frob.com \
    --cc=rostedt@goodmis.org \
    --cc=systemtap@sources.redhat.com \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).