uprobes/core: Allocate xol slots for uprobes use
Subsystems (7)
PATCH 1/2 uprobes/core: Allocate xol slots for uprobes use
From: Srikar Dronamraju <[email protected]>
Uprobes executes the original instruction at a probed location out of line. For this, we allocate a page (per mm) upon the first uprobe hit, in the process user address space, divide it into slots that are used to store the actual instructions to be singlestepped. These slots are known as xol (execution out of line) slots.
Care is taken to ensure that the allocation is in an unmapped area as close to the top of the user address space as possible, with appropriate permission settings to keep selinux like frameworks happy.
Upon a uprobe hit, a free slot is acquired, and is released after the singlestep completes.
[ Folded a fix for build issue on powerpc fixed and reported by Stephen Rothwell]
Lots of improvements courtesy suggestions/inputs from Peter and Oleg.
---
Changelog:
(v5)
- uprobes_xol_area moved from mm to uprobes_state. Also uprobes_xol_area is now known as xol_area.
- no more spin lock needed for slot allocation.
- use install_special_mapping to add a vma. (previous approach used init_creds)
- set xol_area while holding map_sem exclusively.
include/linux/mm_types.h | 2 include/linux/uprobes.h | 34 +++++++ kernel/events/uprobes.c | 215 ++++++++++++++++++++++++++++++++++++++++++++++ kernel/fork.c | 2 4 files changed, 253 insertions(+), 0 deletions(-)
include/linux/mm_types.h +2 -0
@@ -12,6 +12,7 @@#include <linux/completion.h> #include <linux/cpumask.h> #include <linux/page-debug-flags.h> +#include <linux/uprobes.h> #include <asm/page.h> #include <asm/mmu.h>
@@ -388,6 +389,7 @@ struct mm_struct {#ifdef CONFIG_CPUMASK_OFFSTACK struct cpumask cpumask_allocation;#endif +struct uprobes_state uprobes_state; };static inline void mm_init_cpumask (struct mm_struct *mm)
include/linux/uprobes.h +34 -0
@@ -28,6 +28,8 @@#include <linux/rbtree.h> struct vm_area_struct ; +struct mm_struct ; +struct inode ;#ifdef CONFIG_ARCH_SUPPORTS_UPROBES # include <asm/uprobes.h>
@@ -76,6 +78,28 @@ struct uprobe_task {unsigned long vaddr; }; +/* +* On a breakpoint hit, thread contests for a slot. It frees the +* slot after singlestep. Currently a fixed number of slots are +* allocated. +*/ +struct xol_area { +wait_queue_head_t wq;/* if all slots are busy */ +atomic_t slot_count;/* number of 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 */ +}; + +struct uprobes_state { +struct xol_area *xol_area; +};extern int __weak set_swbp (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long vaddr);extern int __weak set_orig_insn (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long vaddr,bool verify);extern bool __weak is_swbp_insn (uprobe_opcode_t *insn);
@@ -90,7 +114,11 @@ extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);extern void uprobe_notify_resume (struct pt_regs *regs);extern bool uprobe_deny_signal (void );extern bool __weak arch_uprobe_skip_sstep (struct arch_uprobe *aup,struct pt_regs *regs); +extern void uprobe_clear_state (struct mm_struct *mm); +extern void uprobe_reset_state (struct mm_struct *mm);#else /* !CONFIG_UPROBES */ +struct uprobes_state { +};static inline int uprobe_register (struct inode *inode,loff_t offset,struct uprobe_consumer *uc) {
@@ -121,5 +149,11 @@ static inline void uprobe_free_utask(struct task_struct *t)static inline void uprobe_copy_process (struct task_struct *t) { } +static inline void uprobe_clear_state (struct mm_struct *mm) +{ +} +static inline void uprobe_reset_state (struct mm_struct *mm) +{ +}#endif /* !CONFIG_UPROBES */ #endif /* _LINUX_UPROBES_H */
kernel/events/uprobes.c +215 -0
245 lines not on this page · raw message
kernel/fork.c +2 -0
@@ -570,6 +570,7 @@ void mmput(struct mm_struct *mm)might_sleep ();if (atomic_dec_and_test (&mm->mm_users)) { +uprobe_clear_state (mm);exit_aio (mm);ksm_exit (mm);khugepaged_exit (mm);/* must run before exit_mmap */
@@ -804,6 +805,7 @@ struct mm_struct *dup_mm(struct task_struct *tsk)#ifdef CONFIG_TRANSPARENT_HUGEPAGE mm->pmd_huge_pte =NULL ;#endif +uprobe_reset_state (mm);if (!mm_init (mm, tsk)) goto fail_nomem;
PATCH 2/2 uprobes/core: Optimize probe hits with help of counter
From: Srikar Dronamraju <[email protected]>
Maintain a per-mm counter (number of uprobes that are inserted on this process address space). This counter can be used at probe hit time to determine if we need a lookup in the uprobes rbtree. Everytime a probe gets inserted successfully, the probe count is incremented and everytime a probe gets removed, the probe count is decremented.
The new uprobe_munmap hook ensures the count is correct on a unmap or remap of a region. We expect that once a uprobe_munmap() is called, the vma goes away. So uprobe_unregister() finding a probe to unregister would either mean unmap event hasnt occurred yet or a mmap event on the same executable file occured after a unmap event.
Additionally, uprobe_mmap hook now also gets called:
a. on every executable vma that is COWed at fork.
b. a vma of interest is newly mapped; breakpoint insertion also
happens at the required address.
On process creation, make sure the probes count in the child is set correctly.
Special cases that are taken care include:
a. mremap
b. VM_DONTCOPY vmas on fork()
c. insertion/removal races in the parent during fork().
---
Changelog:
(v7)
- Move mm->mm_uprobes_count to mm->uprobes_state.count.
- Separate this patch from the patch that implements uprobe_mmap.
- Increment counter only after verifying that the probe lies underneath.
(v5)
- While forking, handle vma's that have VM_DONTCOPY.
- While forking, handle race of new breakpoints being inserted / removed in the parent process.
include/linux/uprobes.h | 5 ++ kernel/events/uprobes.c | 119 ++++++++++++++++++++++++++++++++++++++++++++--- kernel/fork.c | 3 + mm/mmap.c | 10 ++++ 4 files changed, 128 insertions(+), 9 deletions(-)
include/linux/uprobes.h +5 -0
@@ -99,6 +99,7 @@ struct xol_area {struct uprobes_state {struct xol_area *xol_area; +atomic_t count; };extern int __weak set_swbp (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long vaddr);extern int __weak set_orig_insn (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long vaddr,bool verify);
@@ -106,6 +107,7 @@ extern bool __weak is_swbp_insn(uprobe_opcode_t *insn);extern int uprobe_register (struct inode *inode,loff_t offset,struct uprobe_consumer *uc);extern void uprobe_unregister (struct inode *inode,loff_t offset,struct uprobe_consumer *uc);extern int uprobe_mmap (struct vm_area_struct *vma); +extern void uprobe_munmap (struct vm_area_struct *vma);extern void uprobe_free_utask (struct task_struct *t);extern void uprobe_copy_process (struct task_struct *t);extern unsigned long __weak uprobe_get_swbp_addr (struct pt_regs *regs);
@@ -132,6 +134,9 @@ static inline int uprobe_mmap(struct vm_area_struct *vma) {return 0 ; } +static inline void uprobe_munmap (struct vm_area_struct *vma) +{ +}static inline void uprobe_notify_resume (struct pt_regs *regs) { }
kernel/events/uprobes.c +111 -8
178 lines not on this page · raw message
kernel/fork.c +3 -0
@@ -422,6 +422,9 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)if (retval) goto out; + +if (file &&uprobe_mmap (tmp)) + goto out; }/* a new mm has just been created */ arch_dup_mmap (oldmm, mm);
mm/mmap.c +9 -1
@@ -218,6 +218,7 @@ void unlink_file_vma(struct vm_area_struct *vma)mutex_lock (&mapping->i_mmap_mutex);__remove_shared_vm_struct (vma, file, mapping);mutex_unlock (&mapping->i_mmap_mutex); +uprobe_munmap (vma); } }
@@ -545,8 +546,14 @@ again: remove_next = 1 + (end > next->vm_end);if (file) { mapping = file->f_mapping; -if (!(vma->vm_flags &VM_NONLINEAR )) +if (!(vma->vm_flags &VM_NONLINEAR )) { root = &mapping->i_mmap; +uprobe_munmap (vma); + +if (adjust_next) +uprobe_munmap (next); + } +mutex_lock (&mapping->i_mmap_mutex);if (insert) {/*
@@ -625,6 +632,7 @@ again: remove_next = 1 + (end > next->vm_end);if (remove_next) {if (file) { +uprobe_munmap (next);fput (file);if (next->vm_flags &VM_EXECUTABLE )removed_exe_file_vma (mm);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
[tip:perf/uprobes] uprobes/core: Optimize probe hits with the help of a counter
Commit-ID: 682968e0c425c60f0dde37977e5beb2b12ddc4cc Gitweb: http://git.kernel.org/tip/682968e0c425c60f0dde37977e5beb2b12ddc4cc Author: Srikar Dronamraju <[email protected]> AuthorDate: Fri, 30 Mar 2012 23:56:46 +0530 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 31 Mar 2012 11:50:02 +0200
uprobes/core: Optimize probe hits with the help of a counter
Maintain a per-mm counter: number of uprobes that are inserted on this process address space.
This counter can be used at probe hit time to determine if we need a lookup in the uprobes rbtree. Everytime a probe gets inserted successfully, the probe count is incremented and everytime a probe gets removed, the probe count is decremented.
The new uprobe_munmap hook ensures the count is correct on a unmap or remap of a region. We expect that once a uprobe_munmap() is called, the vma goes away. So uprobe_unregister() finding a probe to unregister would either mean unmap event hasnt occurred yet or a mmap event on the same executable file occured after a unmap event.
Additionally, uprobe_mmap hook now also gets called:
a. on every executable vma that is COWed at fork.
b. a vma of interest is newly mapped; breakpoint insertion also
happens at the required address.
On process creation, make sure the probes count in the child is set correctly.
Special cases that are taken care include:
a. mremap
b. VM_DONTCOPY vmas on fork()
c. insertion/removal races in the parent during fork().
--- include/linux/uprobes.h | 5 ++ kernel/events/uprobes.c | 119 +++++++++++++++++++++++++++++++++++++++++++--- kernel/fork.c | 3 + mm/mmap.c | 10 ++++- 4 files changed, 128 insertions(+), 9 deletions(-)
include/linux/uprobes.h +5 -0
@@ -99,6 +99,7 @@ struct xol_area {struct uprobes_state {struct xol_area *xol_area; +atomic_t count; };extern int __weak set_swbp (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long vaddr);extern int __weak set_orig_insn (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long vaddr,bool verify);
@@ -106,6 +107,7 @@ extern bool __weak is_swbp_insn(uprobe_opcode_t *insn);extern int uprobe_register (struct inode *inode,loff_t offset,struct uprobe_consumer *uc);extern void uprobe_unregister (struct inode *inode,loff_t offset,struct uprobe_consumer *uc);extern int uprobe_mmap (struct vm_area_struct *vma); +extern void uprobe_munmap (struct vm_area_struct *vma);extern void uprobe_free_utask (struct task_struct *t);extern void uprobe_copy_process (struct task_struct *t);extern unsigned long __weak uprobe_get_swbp_addr (struct pt_regs *regs);
@@ -132,6 +134,9 @@ static inline int uprobe_mmap(struct vm_area_struct *vma) {return 0 ; } +static inline void uprobe_munmap (struct vm_area_struct *vma) +{ +}static inline void uprobe_notify_resume (struct pt_regs *regs) { }
kernel/events/uprobes.c +111 -8
178 lines not on this page · raw message
kernel/fork.c +3 -0
@@ -421,6 +421,9 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)if (retval) goto out; + +if (file &&uprobe_mmap (tmp)) + goto out; }/* a new mm has just been created */ arch_dup_mmap (oldmm, mm);
mm/mmap.c +9 -1
@@ -218,6 +218,7 @@ void unlink_file_vma(struct vm_area_struct *vma)mutex_lock (&mapping->i_mmap_mutex);__remove_shared_vm_struct (vma, file, mapping);mutex_unlock (&mapping->i_mmap_mutex); +uprobe_munmap (vma); } }
@@ -546,8 +547,14 @@ again: remove_next = 1 + (end > next->vm_end);if (file) { mapping = file->f_mapping; -if (!(vma->vm_flags &VM_NONLINEAR )) +if (!(vma->vm_flags &VM_NONLINEAR )) { root = &mapping->i_mmap; +uprobe_munmap (vma); + +if (adjust_next) +uprobe_munmap (next); + } +mutex_lock (&mapping->i_mmap_mutex);if (insert) {/*
@@ -626,6 +633,7 @@ again: remove_next = 1 + (end > next->vm_end);if (remove_next) {if (file) { +uprobe_munmap (next);fput (file);if (next->vm_flags &VM_EXECUTABLE )removed_exe_file_vma (mm);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
[tip:perf/uprobes] uprobes/core: Allocate XOL slots for uprobes use
In reply to Srikar DronamrajuCommit-ID: d4b3b6384f98f8692ad0209891ccdbc7e78bbefe Gitweb: http://git.kernel.org/tip/d4b3b6384f98f8692ad0209891ccdbc7e78bbefe Author: Srikar Dronamraju <[email protected]> AuthorDate: Fri, 30 Mar 2012 23:56:31 +0530 Committer: Ingo Molnar <[email protected]> CommitDate: Sat, 31 Mar 2012 11:50:01 +0200
uprobes/core: Allocate XOL slots for uprobes use
Uprobes executes the original instruction at a probed location out of line. For this, we allocate a page (per mm) upon the first uprobe hit, in the process user address space, divide it into slots that are used to store the actual instructions to be singlestepped. These slots are known as xol (execution out of line) slots.
Care is taken to ensure that the allocation is in an unmapped area as close to the top of the user address space as possible, with appropriate permission settings to keep selinux like frameworks happy.
Upon a uprobe hit, a free slot is acquired, and is released after the singlestep completes.
Lots of improvements courtesy suggestions/inputs from Peter and Oleg.
[ Folded a fix for build issue on powerpc fixed and reported by
Stephen Rothwell. ]
--- include/linux/mm_types.h | 2 + include/linux/uprobes.h | 34 +++++++ kernel/events/uprobes.c | 215 ++++++++++++++++++++++++++++++++++++++++++++++ kernel/fork.c | 2 + 4 files changed, 253 insertions(+), 0 deletions(-)
include/linux/mm_types.h +2 -0
@@ -12,6 +12,7 @@#include <linux/completion.h> #include <linux/cpumask.h> #include <linux/page-debug-flags.h> +#include <linux/uprobes.h> #include <asm/page.h> #include <asm/mmu.h>
@@ -388,6 +389,7 @@ struct mm_struct {#ifdef CONFIG_CPUMASK_OFFSTACK struct cpumask cpumask_allocation;#endif +struct uprobes_state uprobes_state; };static inline void mm_init_cpumask (struct mm_struct *mm)
include/linux/uprobes.h +34 -0
@@ -28,6 +28,8 @@#include <linux/rbtree.h> struct vm_area_struct ; +struct mm_struct ; +struct inode ;#ifdef CONFIG_ARCH_SUPPORTS_UPROBES # include <asm/uprobes.h>
@@ -76,6 +78,28 @@ struct uprobe_task {unsigned long vaddr; }; +/* +* On a breakpoint hit, thread contests for a slot. It frees the +* slot after singlestep. Currently a fixed number of slots are +* allocated. +*/ +struct xol_area { +wait_queue_head_t wq;/* if all slots are busy */ +atomic_t slot_count;/* number of 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 */ +}; + +struct uprobes_state { +struct xol_area *xol_area; +};extern int __weak set_swbp (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long vaddr);extern int __weak set_orig_insn (struct arch_uprobe *aup,struct mm_struct *mm,unsigned long vaddr,bool verify);extern bool __weak is_swbp_insn (uprobe_opcode_t *insn);
@@ -90,7 +114,11 @@ extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);extern void uprobe_notify_resume (struct pt_regs *regs);extern bool uprobe_deny_signal (void );extern bool __weak arch_uprobe_skip_sstep (struct arch_uprobe *aup,struct pt_regs *regs); +extern void uprobe_clear_state (struct mm_struct *mm); +extern void uprobe_reset_state (struct mm_struct *mm);#else /* !CONFIG_UPROBES */ +struct uprobes_state { +};static inline int uprobe_register (struct inode *inode,loff_t offset,struct uprobe_consumer *uc) {
@@ -121,5 +149,11 @@ static inline void uprobe_free_utask(struct task_struct *t)static inline void uprobe_copy_process (struct task_struct *t) { } +static inline void uprobe_clear_state (struct mm_struct *mm) +{ +} +static inline void uprobe_reset_state (struct mm_struct *mm) +{ +}#endif /* !CONFIG_UPROBES */ #endif /* _LINUX_UPROBES_H */
kernel/events/uprobes.c +215 -0
245 lines not on this page · raw message
kernel/fork.c +2 -0
@@ -554,6 +554,7 @@ void mmput(struct mm_struct *mm)might_sleep ();if (atomic_dec_and_test (&mm->mm_users)) { +uprobe_clear_state (mm);exit_aio (mm);ksm_exit (mm);khugepaged_exit (mm);/* must run before exit_mmap */
@@ -760,6 +761,7 @@ struct mm_struct *dup_mm(struct task_struct *tsk)#ifdef CONFIG_TRANSPARENT_HUGEPAGE mm->pmd_huge_pte =NULL ;#endif +uprobe_reset_state (mm);if (!mm_init (mm, tsk)) goto fail_nomem;
--
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>