[PATCH perf/core 05/22] uprobes: Add nbytes argument to uprobe_write

Jiri Olsa
2025-04-21T21:45:33Z

Adding nbytes argument to uprobe_write and related functions as
preparation for writing whole instructions in following changes.

Also renaming opcode arguments to insn, which seems to fit better.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
include/linux/uprobes.h | 6 +++---
kernel/events/uprobes.c | 27 ++++++++++++++-------------
2 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/include/linux/uprobes.h b/include/linux/uprobes.h
index 09fe93816173..b86a2f0475a4 100644
--- a/include/linux/uprobes.h
+++ b/include/linux/uprobes.h
@@ -188,7 +188,7 @@ struct uprobes_state {
};

typedef int (*uprobe_write_verify_t)(struct page *page, unsigned long vaddr,
- uprobe_opcode_t *opcode);
+ uprobe_opcode_t *insn, int nbytes);

extern void __init uprobes_init(void);
extern int set_swbp(struct arch_uprobe *aup, struct vm_area_struct *vma, unsigned long vaddr);
@@ -198,8 +198,8 @@ extern bool is_trap_insn(uprobe_opcode_t *insn);
extern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs);
extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs);
extern int uprobe_write_opcode(struct vm_area_struct *vma, unsigned long vaddr, uprobe_opcode_t opcode);
-extern int uprobe_write(struct vm_area_struct *vma, const unsigned long opcode_vaddr,
- uprobe_opcode_t opcode, uprobe_write_verify_t verify);
+extern int uprobe_write(struct vm_area_struct *vma, const unsigned long insn_vaddr,
+ uprobe_opcode_t *insn, int nbytes, uprobe_write_verify_t verify);
extern struct uprobe *uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc);
extern int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool);
extern void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc);
diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index 3c5dc86bfe65..6dc7f0b2756d 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -191,7 +191,8 @@ static void copy_to_page(struct page *page, unsigned long vaddr, const void *src
kunmap_atomic(kaddr);
}

-static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
+static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *insn,
+ int nbytes)
{
uprobe_opcode_t old_opcode;
bool is_swbp;
@@ -208,7 +209,7 @@ static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t
uprobe_copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE);
is_swbp = is_swbp_insn(&old_opcode);

- if (is_swbp_insn(new_opcode)) {
+ if (is_swbp_insn(insn)) {
if (is_swbp) /* register: already installed? */
return 0;
} else {
@@ -401,10 +402,10 @@ static bool orig_page_is_identical(struct vm_area_struct *vma,

static int __uprobe_write(struct vm_area_struct *vma,
struct folio_walk *fw, struct folio *folio,
- unsigned long opcode_vaddr, uprobe_opcode_t opcode)
+ unsigned long insn_vaddr, uprobe_opcode_t *insn, int nbytes)
{
- const unsigned long vaddr = opcode_vaddr & PAGE_MASK;
- const bool is_register = !!is_swbp_insn(&opcode);
+ const unsigned long vaddr = insn_vaddr & PAGE_MASK;
+ const bool is_register = !!is_swbp_insn(insn);
bool pmd_mappable;

/* For now, we'll only handle PTE-mapped folios. */
@@ -429,7 +430,7 @@ static int __uprobe_write(struct vm_area_struct *vma,
*/
flush_cache_page(vma, vaddr, pte_pfn(fw->pte));
fw->pte = ptep_clear_flush(vma, vaddr, fw->ptep);
- copy_to_page(fw->page, opcode_vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
+ copy_to_page(fw->page, insn_vaddr, insn, nbytes);

/*
* When unregistering, we may only zap a PTE if uffd is disabled and
@@ -489,13 +490,13 @@ static int __uprobe_write(struct vm_area_struct *vma,
int uprobe_write_opcode(struct vm_area_struct *vma, const unsigned long opcode_vaddr,
uprobe_opcode_t opcode)
{
- return uprobe_write(vma, opcode_vaddr, opcode, verify_opcode);
+ return uprobe_write(vma, opcode_vaddr, &opcode, UPROBE_SWBP_INSN_SIZE, verify_opcode);
}

-int uprobe_write(struct vm_area_struct *vma, const unsigned long opcode_vaddr,
- uprobe_opcode_t opcode, uprobe_write_verify_t verify)
+int uprobe_write(struct vm_area_struct *vma, const unsigned long insn_vaddr,
+ uprobe_opcode_t *insn, int nbytes, uprobe_write_verify_t verify)
{
- const unsigned long vaddr = opcode_vaddr & PAGE_MASK;
+ const unsigned long vaddr = insn_vaddr & PAGE_MASK;
struct mm_struct *mm = vma->vm_mm;
int ret, is_register;
unsigned int gup_flags = FOLL_FORCE;
@@ -504,7 +505,7 @@ int uprobe_write(struct vm_area_struct *vma, const unsigned long opcode_vaddr,
struct folio *folio;
struct page *page;

- is_register = is_swbp_insn(&opcode);
+ is_register = is_swbp_insn(insn);

if (WARN_ON_ONCE(!is_cow_mapping(vma->vm_flags)))
return -EINVAL;
@@ -526,7 +527,7 @@ int uprobe_write(struct vm_area_struct *vma, const unsigned long opcode_vaddr,
goto out;
folio = page_folio(page);

- ret = verify(page, opcode_vaddr, &opcode);
+ ret = verify(page, insn_vaddr, insn, nbytes);
if (ret <= 0) {
folio_put(folio);
goto out;
@@ -554,7 +555,7 @@ int uprobe_write(struct vm_area_struct *vma, const unsigned long opcode_vaddr,
/* Walk the page tables again, to perform the actual update. */
if (folio_walk_start(&fw, vma, vaddr, 0)) {
if (fw.page == page)
- ret = __uprobe_write(vma, &fw, folio, opcode_vaddr, opcode);
+ ret = __uprobe_write(vma, &fw, folio, insn_vaddr, insn, nbytes);
folio_walk_end(&fw, vma);
}

--
2.49.0