uprobes/core: Move insn to arch specific structure

Few cleanups suggested by Ingo Molnar.

- Rename struct uprobe_arch_info to struct arch_uprobe.
- Move insn from struct uprobe to struct arch_uprobe.
- Make arch specific uprobe functions to accept struct arch_uprobe
  instead of  struct uprobe.
- Move struct uprobe to kernel/uprobes.c from include/linux/uprobes.h

Signed-off-by: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Christoph Hellwig <hch@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Anton Arapov <anton@redhat.com>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Jim Keniston <jkenisto@linux.vnet.ibm.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Josh Stone <jistone@redhat.com>
Link: http://lkml.kernel.org/r/20120222091602.15880.40249.sendpatchset@srdronam.in.ibm.com
[ Made various small improvements ]
Signed-off-by: Ingo Molnar <mingo@elte.hu>
This commit is contained in:
Srikar Dronamraju 2012-02-22 14:46:02 +05:30 коммит произвёл Ingo Molnar
Родитель 96379f6007
Коммит 3ff54efdfa
4 изменённых файлов: 61 добавлений и 66 удалений

Просмотреть файл

@ -31,13 +31,13 @@ typedef u8 uprobe_opcode_t;
#define UPROBES_BKPT_INSN 0xcc #define UPROBES_BKPT_INSN 0xcc
#define UPROBES_BKPT_INSN_SIZE 1 #define UPROBES_BKPT_INSN_SIZE 1
struct uprobe_arch_info { struct arch_uprobe {
u16 fixups; u16 fixups;
u8 insn[MAX_UINSN_BYTES];
#ifdef CONFIG_X86_64 #ifdef CONFIG_X86_64
unsigned long rip_rela_target_address; unsigned long rip_rela_target_address;
#endif #endif
}; };
struct uprobe; extern int arch_uprobes_analyze_insn(struct mm_struct *mm, struct arch_uprobe *arch_uprobe);
extern int arch_uprobes_analyze_insn(struct mm_struct *mm, struct uprobe *uprobe);
#endif /* _ASM_UPROBES_H */ #endif /* _ASM_UPROBES_H */

Просмотреть файл

@ -200,9 +200,9 @@ static bool is_prefix_bad(struct insn *insn)
return false; return false;
} }
static int validate_insn_32bits(struct uprobe *uprobe, struct insn *insn) static int validate_insn_32bits(struct arch_uprobe *auprobe, struct insn *insn)
{ {
insn_init(insn, uprobe->insn, false); insn_init(insn, auprobe->insn, false);
/* Skip good instruction prefixes; reject "bad" ones. */ /* Skip good instruction prefixes; reject "bad" ones. */
insn_get_opcode(insn); insn_get_opcode(insn);
@ -222,11 +222,11 @@ static int validate_insn_32bits(struct uprobe *uprobe, struct insn *insn)
/* /*
* Figure out which fixups post_xol() will need to perform, and annotate * Figure out which fixups post_xol() will need to perform, and annotate
* uprobe->arch_info.fixups accordingly. To start with, * arch_uprobe->fixups accordingly. To start with,
* uprobe->arch_info.fixups is either zero or it reflects rip-related * arch_uprobe->fixups is either zero or it reflects rip-related
* fixups. * fixups.
*/ */
static void prepare_fixups(struct uprobe *uprobe, struct insn *insn) static void prepare_fixups(struct arch_uprobe *auprobe, struct insn *insn)
{ {
bool fix_ip = true, fix_call = false; /* defaults */ bool fix_ip = true, fix_call = false; /* defaults */
int reg; int reg;
@ -269,17 +269,17 @@ static void prepare_fixups(struct uprobe *uprobe, struct insn *insn)
break; break;
} }
if (fix_ip) if (fix_ip)
uprobe->arch_info.fixups |= UPROBES_FIX_IP; auprobe->fixups |= UPROBES_FIX_IP;
if (fix_call) if (fix_call)
uprobe->arch_info.fixups |= UPROBES_FIX_CALL; auprobe->fixups |= UPROBES_FIX_CALL;
} }
#ifdef CONFIG_X86_64 #ifdef CONFIG_X86_64
/* /*
* If uprobe->insn doesn't use rip-relative addressing, return * If arch_uprobe->insn doesn't use rip-relative addressing, return
* immediately. Otherwise, rewrite the instruction so that it accesses * immediately. Otherwise, rewrite the instruction so that it accesses
* its memory operand indirectly through a scratch register. Set * its memory operand indirectly through a scratch register. Set
* uprobe->arch_info.fixups and uprobe->arch_info.rip_rela_target_address * arch_uprobe->fixups and arch_uprobe->rip_rela_target_address
* accordingly. (The contents of the scratch register will be saved * accordingly. (The contents of the scratch register will be saved
* before we single-step the modified instruction, and restored * before we single-step the modified instruction, and restored
* afterward.) * afterward.)
@ -297,7 +297,7 @@ static void prepare_fixups(struct uprobe *uprobe, struct insn *insn)
* - There's never a SIB byte. * - There's never a SIB byte.
* - The displacement is always 4 bytes. * - The displacement is always 4 bytes.
*/ */
static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe, struct insn *insn) static void handle_riprel_insn(struct mm_struct *mm, struct arch_uprobe *auprobe, struct insn *insn)
{ {
u8 *cursor; u8 *cursor;
u8 reg; u8 reg;
@ -305,7 +305,7 @@ static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe, stru
if (mm->context.ia32_compat) if (mm->context.ia32_compat)
return; return;
uprobe->arch_info.rip_rela_target_address = 0x0; auprobe->rip_rela_target_address = 0x0;
if (!insn_rip_relative(insn)) if (!insn_rip_relative(insn))
return; return;
@ -315,7 +315,7 @@ static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe, stru
* we want to encode rax/rcx, not r8/r9. * we want to encode rax/rcx, not r8/r9.
*/ */
if (insn->rex_prefix.nbytes) { if (insn->rex_prefix.nbytes) {
cursor = uprobe->insn + insn_offset_rex_prefix(insn); cursor = auprobe->insn + insn_offset_rex_prefix(insn);
*cursor &= 0xfe; /* Clearing REX.B bit */ *cursor &= 0xfe; /* Clearing REX.B bit */
} }
@ -324,7 +324,7 @@ static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe, stru
* displacement. Beyond the displacement, for some instructions, * displacement. Beyond the displacement, for some instructions,
* is the immediate operand. * is the immediate operand.
*/ */
cursor = uprobe->insn + insn_offset_modrm(insn); cursor = auprobe->insn + insn_offset_modrm(insn);
insn_get_length(insn); insn_get_length(insn);
/* /*
@ -341,18 +341,18 @@ static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe, stru
* is NOT the register operand, so we use %rcx (register * is NOT the register operand, so we use %rcx (register
* #1) for the scratch register. * #1) for the scratch register.
*/ */
uprobe->arch_info.fixups = UPROBES_FIX_RIP_CX; auprobe->fixups = UPROBES_FIX_RIP_CX;
/* Change modrm from 00 000 101 to 00 000 001. */ /* Change modrm from 00 000 101 to 00 000 001. */
*cursor = 0x1; *cursor = 0x1;
} else { } else {
/* Use %rax (register #0) for the scratch register. */ /* Use %rax (register #0) for the scratch register. */
uprobe->arch_info.fixups = UPROBES_FIX_RIP_AX; auprobe->fixups = UPROBES_FIX_RIP_AX;
/* Change modrm from 00 xxx 101 to 00 xxx 000 */ /* Change modrm from 00 xxx 101 to 00 xxx 000 */
*cursor = (reg << 3); *cursor = (reg << 3);
} }
/* Target address = address of next instruction + (signed) offset */ /* Target address = address of next instruction + (signed) offset */
uprobe->arch_info.rip_rela_target_address = (long)insn->length + insn->displacement.value; auprobe->rip_rela_target_address = (long)insn->length + insn->displacement.value;
/* Displacement field is gone; slide immediate field (if any) over. */ /* Displacement field is gone; slide immediate field (if any) over. */
if (insn->immediate.nbytes) { if (insn->immediate.nbytes) {
@ -362,9 +362,9 @@ static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe, stru
return; return;
} }
static int validate_insn_64bits(struct uprobe *uprobe, struct insn *insn) static int validate_insn_64bits(struct arch_uprobe *auprobe, struct insn *insn)
{ {
insn_init(insn, uprobe->insn, true); insn_init(insn, auprobe->insn, true);
/* Skip good instruction prefixes; reject "bad" ones. */ /* Skip good instruction prefixes; reject "bad" ones. */
insn_get_opcode(insn); insn_get_opcode(insn);
@ -381,42 +381,42 @@ static int validate_insn_64bits(struct uprobe *uprobe, struct insn *insn)
return -ENOTSUPP; return -ENOTSUPP;
} }
static int validate_insn_bits(struct mm_struct *mm, struct uprobe *uprobe, struct insn *insn) static int validate_insn_bits(struct mm_struct *mm, struct arch_uprobe *auprobe, struct insn *insn)
{ {
if (mm->context.ia32_compat) if (mm->context.ia32_compat)
return validate_insn_32bits(uprobe, insn); return validate_insn_32bits(auprobe, insn);
return validate_insn_64bits(uprobe, insn); return validate_insn_64bits(auprobe, insn);
} }
#else /* 32-bit: */ #else /* 32-bit: */
static void handle_riprel_insn(struct mm_struct *mm, struct uprobe *uprobe, struct insn *insn) static void handle_riprel_insn(struct mm_struct *mm, struct arch_uprobe *auprobe, struct insn *insn)
{ {
/* No RIP-relative addressing on 32-bit */ /* No RIP-relative addressing on 32-bit */
} }
static int validate_insn_bits(struct mm_struct *mm, struct uprobe *uprobe, struct insn *insn) static int validate_insn_bits(struct mm_struct *mm, struct arch_uprobe *auprobe, struct insn *insn)
{ {
return validate_insn_32bits(uprobe, insn); return validate_insn_32bits(auprobe, insn);
} }
#endif /* CONFIG_X86_64 */ #endif /* CONFIG_X86_64 */
/** /**
* arch_uprobes_analyze_insn - instruction analysis including validity and fixups. * arch_uprobes_analyze_insn - instruction analysis including validity and fixups.
* @mm: the probed address space. * @mm: the probed address space.
* @uprobe: the probepoint information. * @arch_uprobe: the probepoint information.
* Return 0 on success or a -ve number on error. * Return 0 on success or a -ve number on error.
*/ */
int arch_uprobes_analyze_insn(struct mm_struct *mm, struct uprobe *uprobe) int arch_uprobes_analyze_insn(struct mm_struct *mm, struct arch_uprobe *auprobe)
{ {
int ret; int ret;
struct insn insn; struct insn insn;
uprobe->arch_info.fixups = 0; auprobe->fixups = 0;
ret = validate_insn_bits(mm, uprobe, &insn); ret = validate_insn_bits(mm, auprobe, &insn);
if (ret != 0) if (ret != 0)
return ret; return ret;
handle_riprel_insn(mm, uprobe, &insn); handle_riprel_insn(mm, auprobe, &insn);
prepare_fixups(uprobe, &insn); prepare_fixups(auprobe, &insn);
return 0; return 0;
} }

Просмотреть файл

@ -29,12 +29,6 @@
struct vm_area_struct; struct vm_area_struct;
#ifdef CONFIG_ARCH_SUPPORTS_UPROBES #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
#include <asm/uprobes.h> #include <asm/uprobes.h>
#else
typedef u8 uprobe_opcode_t;
struct uprobe_arch_info {};
#define MAX_UINSN_BYTES 4
#endif #endif
/* flags that denote/change uprobes behaviour */ /* flags that denote/change uprobes behaviour */
@ -56,22 +50,9 @@ struct uprobe_consumer {
struct uprobe_consumer *next; 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 list_head pending_list;
struct uprobe_arch_info arch_info;
struct uprobe_consumer *consumers;
struct inode *inode; /* Also hold a ref to inode */
loff_t offset;
int flags;
u8 insn[MAX_UINSN_BYTES];
};
#ifdef CONFIG_UPROBES #ifdef CONFIG_UPROBES
extern int __weak set_bkpt(struct mm_struct *mm, struct uprobe *uprobe, unsigned long vaddr); extern int __weak set_bkpt(struct mm_struct *mm, struct arch_uprobe *auprobe, unsigned long vaddr);
extern int __weak set_orig_insn(struct mm_struct *mm, struct uprobe *uprobe, unsigned long vaddr, bool verify); extern int __weak set_orig_insn(struct mm_struct *mm, struct arch_uprobe *auprobe, unsigned long vaddr, bool verify);
extern bool __weak is_bkpt_insn(uprobe_opcode_t *insn); extern bool __weak is_bkpt_insn(uprobe_opcode_t *insn);
extern int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *consumer); extern int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *consumer);
extern void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *consumer); extern void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *consumer);

Просмотреть файл

@ -65,6 +65,18 @@ struct vma_info {
loff_t vaddr; loff_t vaddr;
}; };
struct uprobe {
struct rb_node rb_node; /* node in the rb tree */
atomic_t ref;
struct rw_semaphore consumer_rwsem;
struct list_head pending_list;
struct uprobe_consumer *consumers;
struct inode *inode; /* Also hold a ref to inode */
loff_t offset;
int flags;
struct arch_uprobe arch;
};
/* /*
* valid_vma: Verify if the specified vma is an executable vma * valid_vma: Verify if the specified vma is an executable vma
* Relax restrictions while unregistering: vm_flags might have * Relax restrictions while unregistering: vm_flags might have
@ -180,7 +192,7 @@ bool __weak is_bkpt_insn(uprobe_opcode_t *insn)
/* /*
* write_opcode - write the opcode at a given virtual address. * write_opcode - write the opcode at a given virtual address.
* @mm: the probed process address space. * @mm: the probed process address space.
* @uprobe: the breakpointing information. * @arch_uprobe: the breakpointing information.
* @vaddr: the virtual address to store the opcode. * @vaddr: the virtual address to store the opcode.
* @opcode: opcode to be written at @vaddr. * @opcode: opcode to be written at @vaddr.
* *
@ -190,13 +202,14 @@ bool __weak is_bkpt_insn(uprobe_opcode_t *insn)
* For mm @mm, write the opcode at @vaddr. * For mm @mm, write the opcode at @vaddr.
* Return 0 (success) or a negative errno. * Return 0 (success) or a negative errno.
*/ */
static int write_opcode(struct mm_struct *mm, struct uprobe *uprobe, static int write_opcode(struct mm_struct *mm, struct arch_uprobe *auprobe,
unsigned long vaddr, uprobe_opcode_t opcode) unsigned long vaddr, uprobe_opcode_t opcode)
{ {
struct page *old_page, *new_page; struct page *old_page, *new_page;
struct address_space *mapping; struct address_space *mapping;
void *vaddr_old, *vaddr_new; void *vaddr_old, *vaddr_new;
struct vm_area_struct *vma; struct vm_area_struct *vma;
struct uprobe *uprobe;
loff_t addr; loff_t addr;
int ret; int ret;
@ -216,6 +229,7 @@ static int write_opcode(struct mm_struct *mm, struct uprobe *uprobe,
if (!valid_vma(vma, is_bkpt_insn(&opcode))) if (!valid_vma(vma, is_bkpt_insn(&opcode)))
goto put_out; goto put_out;
uprobe = container_of(auprobe, struct uprobe, arch);
mapping = uprobe->inode->i_mapping; mapping = uprobe->inode->i_mapping;
if (mapping != vma->vm_file->f_mapping) if (mapping != vma->vm_file->f_mapping)
goto put_out; goto put_out;
@ -326,7 +340,7 @@ static int is_bkpt_at_addr(struct mm_struct *mm, unsigned long vaddr)
* For mm @mm, store the breakpoint instruction at @vaddr. * For mm @mm, store the breakpoint instruction at @vaddr.
* Return 0 (success) or a negative errno. * Return 0 (success) or a negative errno.
*/ */
int __weak set_bkpt(struct mm_struct *mm, struct uprobe *uprobe, unsigned long vaddr) int __weak set_bkpt(struct mm_struct *mm, struct arch_uprobe *auprobe, unsigned long vaddr)
{ {
int result; int result;
@ -337,7 +351,7 @@ int __weak set_bkpt(struct mm_struct *mm, struct uprobe *uprobe, unsigned long v
if (result) if (result)
return result; return result;
return write_opcode(mm, uprobe, vaddr, UPROBES_BKPT_INSN); return write_opcode(mm, auprobe, vaddr, UPROBES_BKPT_INSN);
} }
/** /**
@ -351,7 +365,7 @@ int __weak set_bkpt(struct mm_struct *mm, struct uprobe *uprobe, unsigned long v
* Return 0 (success) or a negative errno. * Return 0 (success) or a negative errno.
*/ */
int __weak int __weak
set_orig_insn(struct mm_struct *mm, struct uprobe *uprobe, unsigned long vaddr, bool verify) set_orig_insn(struct mm_struct *mm, struct arch_uprobe *auprobe, unsigned long vaddr, bool verify)
{ {
if (verify) { if (verify) {
int result; int result;
@ -363,7 +377,7 @@ set_orig_insn(struct mm_struct *mm, struct uprobe *uprobe, unsigned long vaddr,
if (result != 1) if (result != 1)
return result; return result;
} }
return write_opcode(mm, uprobe, vaddr, *(uprobe_opcode_t *)uprobe->insn); return write_opcode(mm, auprobe, vaddr, *(uprobe_opcode_t *)auprobe->insn);
} }
static int match_uprobe(struct uprobe *l, struct uprobe *r) static int match_uprobe(struct uprobe *l, struct uprobe *r)
@ -593,13 +607,13 @@ static int copy_insn(struct uprobe *uprobe, struct vm_area_struct *vma, unsigned
/* Instruction at the page-boundary; copy bytes in second page */ /* Instruction at the page-boundary; copy bytes in second page */
if (nbytes < bytes) { if (nbytes < bytes) {
if (__copy_insn(mapping, vma, uprobe->insn + nbytes, if (__copy_insn(mapping, vma, uprobe->arch.insn + nbytes,
bytes - nbytes, uprobe->offset + nbytes)) bytes - nbytes, uprobe->offset + nbytes))
return -ENOMEM; return -ENOMEM;
bytes = nbytes; bytes = nbytes;
} }
return __copy_insn(mapping, vma, uprobe->insn, bytes, uprobe->offset); return __copy_insn(mapping, vma, uprobe->arch.insn, bytes, uprobe->offset);
} }
static int install_breakpoint(struct mm_struct *mm, struct uprobe *uprobe, static int install_breakpoint(struct mm_struct *mm, struct uprobe *uprobe,
@ -625,23 +639,23 @@ static int install_breakpoint(struct mm_struct *mm, struct uprobe *uprobe,
if (ret) if (ret)
return ret; return ret;
if (is_bkpt_insn((uprobe_opcode_t *)uprobe->insn)) if (is_bkpt_insn((uprobe_opcode_t *)uprobe->arch.insn))
return -EEXIST; return -EEXIST;
ret = arch_uprobes_analyze_insn(mm, uprobe); ret = arch_uprobes_analyze_insn(mm, &uprobe->arch);
if (ret) if (ret)
return ret; return ret;
uprobe->flags |= UPROBES_COPY_INSN; uprobe->flags |= UPROBES_COPY_INSN;
} }
ret = set_bkpt(mm, uprobe, addr); ret = set_bkpt(mm, &uprobe->arch, addr);
return ret; return ret;
} }
static void remove_breakpoint(struct mm_struct *mm, struct uprobe *uprobe, loff_t vaddr) static void remove_breakpoint(struct mm_struct *mm, struct uprobe *uprobe, loff_t vaddr)
{ {
set_orig_insn(mm, uprobe, (unsigned long)vaddr, true); set_orig_insn(mm, &uprobe->arch, (unsigned long)vaddr, true);
} }
static void delete_uprobe(struct uprobe *uprobe) static void delete_uprobe(struct uprobe *uprobe)