Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

uprobes: Add is_register argument to uprobe_write and uprobe_write_opcode

The uprobe_write has special path to restore the original page when we
write original instruction back. This happens when uprobe_write detects
that we want to write anything else but breakpoint instruction.

Moving the detection away and passing it to uprobe_write as argument,
so it's possible to write different instructions (other than just
breakpoint and rest).

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Link: https://lore.kernel.org/r/20250720112133.244369-7-jolsa@kernel.org

authored by

Jiri Olsa and committed by
Peter Zijlstra
ec46350f f8b7c528

+15 -13
+1 -1
arch/arm/probes/uprobes/core.c
··· 30 30 unsigned long vaddr) 31 31 { 32 32 return uprobe_write_opcode(auprobe, vma, vaddr, 33 - __opcode_to_mem_arm(auprobe->bpinsn)); 33 + __opcode_to_mem_arm(auprobe->bpinsn), true); 34 34 } 35 35 36 36 bool arch_uprobe_ignore(struct arch_uprobe *auprobe, struct pt_regs *regs)
+3 -2
include/linux/uprobes.h
··· 197 197 extern bool is_trap_insn(uprobe_opcode_t *insn); 198 198 extern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs); 199 199 extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs); 200 - extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, unsigned long vaddr, uprobe_opcode_t); 200 + extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, unsigned long vaddr, uprobe_opcode_t, 201 + bool is_register); 201 202 extern int uprobe_write(struct arch_uprobe *auprobe, struct vm_area_struct *vma, const unsigned long opcode_vaddr, 202 - uprobe_opcode_t *insn, int nbytes, uprobe_write_verify_t verify); 203 + uprobe_opcode_t *insn, int nbytes, uprobe_write_verify_t verify, bool is_register); 203 204 extern struct uprobe *uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc); 204 205 extern int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool); 205 206 extern void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc);
+11 -10
kernel/events/uprobes.c
··· 402 402 403 403 static int __uprobe_write(struct vm_area_struct *vma, 404 404 struct folio_walk *fw, struct folio *folio, 405 - unsigned long insn_vaddr, uprobe_opcode_t *insn, int nbytes) 405 + unsigned long insn_vaddr, uprobe_opcode_t *insn, int nbytes, 406 + bool is_register) 406 407 { 407 408 const unsigned long vaddr = insn_vaddr & PAGE_MASK; 408 - const bool is_register = !!is_swbp_insn(insn); 409 409 bool pmd_mappable; 410 410 411 411 /* For now, we'll only handle PTE-mapped folios. */ ··· 487 487 * Return 0 (success) or a negative errno. 488 488 */ 489 489 int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, 490 - const unsigned long opcode_vaddr, uprobe_opcode_t opcode) 490 + const unsigned long opcode_vaddr, uprobe_opcode_t opcode, 491 + bool is_register) 491 492 { 492 - return uprobe_write(auprobe, vma, opcode_vaddr, &opcode, UPROBE_SWBP_INSN_SIZE, verify_opcode); 493 + return uprobe_write(auprobe, vma, opcode_vaddr, &opcode, UPROBE_SWBP_INSN_SIZE, 494 + verify_opcode, is_register); 493 495 } 494 496 495 497 int uprobe_write(struct arch_uprobe *auprobe, struct vm_area_struct *vma, 496 498 const unsigned long insn_vaddr, uprobe_opcode_t *insn, int nbytes, 497 - uprobe_write_verify_t verify) 499 + uprobe_write_verify_t verify, bool is_register) 498 500 { 499 501 const unsigned long vaddr = insn_vaddr & PAGE_MASK; 500 502 struct mm_struct *mm = vma->vm_mm; 501 503 struct uprobe *uprobe; 502 - int ret, is_register, ref_ctr_updated = 0; 504 + int ret, ref_ctr_updated = 0; 503 505 unsigned int gup_flags = FOLL_FORCE; 504 506 struct mmu_notifier_range range; 505 507 struct folio_walk fw; 506 508 struct folio *folio; 507 509 struct page *page; 508 510 509 - is_register = is_swbp_insn(insn); 510 511 uprobe = container_of(auprobe, struct uprobe, arch); 511 512 512 513 if (WARN_ON_ONCE(!is_cow_mapping(vma->vm_flags))) ··· 569 568 /* Walk the page tables again, to perform the actual update. */ 570 569 if (folio_walk_start(&fw, vma, vaddr, 0)) { 571 570 if (fw.page == page) 572 - ret = __uprobe_write(vma, &fw, folio, insn_vaddr, insn, nbytes); 571 + ret = __uprobe_write(vma, &fw, folio, insn_vaddr, insn, nbytes, is_register); 573 572 folio_walk_end(&fw, vma); 574 573 } 575 574 ··· 611 610 int __weak set_swbp(struct arch_uprobe *auprobe, struct vm_area_struct *vma, 612 611 unsigned long vaddr) 613 612 { 614 - return uprobe_write_opcode(auprobe, vma, vaddr, UPROBE_SWBP_INSN); 613 + return uprobe_write_opcode(auprobe, vma, vaddr, UPROBE_SWBP_INSN, true); 615 614 } 616 615 617 616 /** ··· 627 626 struct vm_area_struct *vma, unsigned long vaddr) 628 627 { 629 628 return uprobe_write_opcode(auprobe, vma, vaddr, 630 - *(uprobe_opcode_t *)&auprobe->insn); 629 + *(uprobe_opcode_t *)&auprobe->insn, false); 631 630 } 632 631 633 632 /* uprobe should have guaranteed positive refcount */