1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Kernel Probes (KProbes) 4 * kernel/kprobes.c 5 * 6 * Copyright (C) IBM Corporation, 2002, 2004 7 * 8 * 2002-Oct Created by Vamsi Krishna S <[email protected]> Kernel 9 * Probes initial implementation (includes suggestions from 10 * Rusty Russell). 11 * 2004-Aug Updated by Prasanna S Panchamukhi <[email protected]> with 12 * hlists and exceptions notifier as suggested by Andi Kleen. 13 * 2004-July Suparna Bhattacharya <[email protected]> added jumper probes 14 * interface to access function arguments. 15 * 2004-Sep Prasanna S Panchamukhi <[email protected]> Changed Kprobes 16 * exceptions notifier to be first on the priority list. 17 * 2005-May Hien Nguyen <[email protected]>, Jim Keniston 18 * <[email protected]> and Prasanna S Panchamukhi 19 * <[email protected]> added function-return probes. 20 */ 21 #include <linux/kprobes.h> 22 #include <linux/hash.h> 23 #include <linux/init.h> 24 #include <linux/slab.h> 25 #include <linux/stddef.h> 26 #include <linux/export.h> 27 #include <linux/moduleloader.h> 28 #include <linux/kallsyms.h> 29 #include <linux/freezer.h> 30 #include <linux/seq_file.h> 31 #include <linux/debugfs.h> 32 #include <linux/sysctl.h> 33 #include <linux/kdebug.h> 34 #include <linux/memory.h> 35 #include <linux/ftrace.h> 36 #include <linux/cpu.h> 37 #include <linux/jump_label.h> 38 #include <linux/perf_event.h> 39 #include <linux/static_call.h> 40 41 #include <asm/sections.h> 42 #include <asm/cacheflush.h> 43 #include <asm/errno.h> 44 #include <linux/uaccess.h> 45 46 #define KPROBE_HASH_BITS 6 47 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS) 48 49 50 static int kprobes_initialized; 51 /* kprobe_table can be accessed by 52 * - Normal hlist traversal and RCU add/del under kprobe_mutex is held. 53 * Or 54 * - RCU hlist traversal under disabling preempt (breakpoint handlers) 55 */ 56 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE]; 57 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE]; 58 59 /* NOTE: change this value only with kprobe_mutex held */ 60 static bool kprobes_all_disarmed; 61 62 /* This protects kprobe_table and optimizing_list */ 63 static DEFINE_MUTEX(kprobe_mutex); 64 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL; 65 static struct { 66 raw_spinlock_t lock ____cacheline_aligned_in_smp; 67 } kretprobe_table_locks[KPROBE_TABLE_SIZE]; 68 69 kprobe_opcode_t * __weak kprobe_lookup_name(const char *name, 70 unsigned int __unused) 71 { 72 return ((kprobe_opcode_t *)(kallsyms_lookup_name(name))); 73 } 74 75 static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash) 76 { 77 return &(kretprobe_table_locks[hash].lock); 78 } 79 80 /* Blacklist -- list of struct kprobe_blacklist_entry */ 81 static LIST_HEAD(kprobe_blacklist); 82 83 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT 84 /* 85 * kprobe->ainsn.insn points to the copy of the instruction to be 86 * single-stepped. x86_64, POWER4 and above have no-exec support and 87 * stepping on the instruction on a vmalloced/kmalloced/data page 88 * is a recipe for disaster 89 */ 90 struct kprobe_insn_page { 91 struct list_head list; 92 kprobe_opcode_t *insns; /* Page of instruction slots */ 93 struct kprobe_insn_cache *cache; 94 int nused; 95 int ngarbage; 96 char slot_used[]; 97 }; 98 99 #define KPROBE_INSN_PAGE_SIZE(slots) \ 100 (offsetof(struct kprobe_insn_page, slot_used) + \ 101 (sizeof(char) * (slots))) 102 103 static int slots_per_page(struct kprobe_insn_cache *c) 104 { 105 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t)); 106 } 107 108 enum kprobe_slot_state { 109 SLOT_CLEAN = 0, 110 SLOT_DIRTY = 1, 111 SLOT_USED = 2, 112 }; 113 114 void __weak *alloc_insn_page(void) 115 { 116 return module_alloc(PAGE_SIZE); 117 } 118 119 void __weak free_insn_page(void *page) 120 { 121 module_memfree(page); 122 } 123 124 struct kprobe_insn_cache kprobe_insn_slots = { 125 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex), 126 .alloc = alloc_insn_page, 127 .free = free_insn_page, 128 .sym = KPROBE_INSN_PAGE_SYM, 129 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages), 130 .insn_size = MAX_INSN_SIZE, 131 .nr_garbage = 0, 132 }; 133 static int collect_garbage_slots(struct kprobe_insn_cache *c); 134 135 /** 136 * __get_insn_slot() - Find a slot on an executable page for an instruction. 137 * We allocate an executable page if there's no room on existing ones. 138 */ 139 kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c) 140 { 141 struct kprobe_insn_page *kip; 142 kprobe_opcode_t *slot = NULL; 143 144 /* Since the slot array is not protected by rcu, we need a mutex */ 145 mutex_lock(&c->mutex); 146 retry: 147 rcu_read_lock(); 148 list_for_each_entry_rcu(kip, &c->pages, list) { 149 if (kip->nused < slots_per_page(c)) { 150 int i; 151 for (i = 0; i < slots_per_page(c); i++) { 152 if (kip->slot_used[i] == SLOT_CLEAN) { 153 kip->slot_used[i] = SLOT_USED; 154 kip->nused++; 155 slot = kip->insns + (i * c->insn_size); 156 rcu_read_unlock(); 157 goto out; 158 } 159 } 160 /* kip->nused is broken. Fix it. */ 161 kip->nused = slots_per_page(c); 162 WARN_ON(1); 163 } 164 } 165 rcu_read_unlock(); 166 167 /* If there are any garbage slots, collect it and try again. */ 168 if (c->nr_garbage && collect_garbage_slots(c) == 0) 169 goto retry; 170 171 /* All out of space. Need to allocate a new page. */ 172 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL); 173 if (!kip) 174 goto out; 175 176 /* 177 * Use module_alloc so this page is within +/- 2GB of where the 178 * kernel image and loaded module images reside. This is required 179 * so x86_64 can correctly handle the %rip-relative fixups. 180 */ 181 kip->insns = c->alloc(); 182 if (!kip->insns) { 183 kfree(kip); 184 goto out; 185 } 186 INIT_LIST_HEAD(&kip->list); 187 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c)); 188 kip->slot_used[0] = SLOT_USED; 189 kip->nused = 1; 190 kip->ngarbage = 0; 191 kip->cache = c; 192 list_add_rcu(&kip->list, &c->pages); 193 slot = kip->insns; 194 195 /* Record the perf ksymbol register event after adding the page */ 196 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insns, 197 PAGE_SIZE, false, c->sym); 198 out: 199 mutex_unlock(&c->mutex); 200 return slot; 201 } 202 203 /* Return 1 if all garbages are collected, otherwise 0. */ 204 static int collect_one_slot(struct kprobe_insn_page *kip, int idx) 205 { 206 kip->slot_used[idx] = SLOT_CLEAN; 207 kip->nused--; 208 if (kip->nused == 0) { 209 /* 210 * Page is no longer in use. Free it unless 211 * it's the last one. We keep the last one 212 * so as not to have to set it up again the 213 * next time somebody inserts a probe. 214 */ 215 if (!list_is_singular(&kip->list)) { 216 /* 217 * Record perf ksymbol unregister event before removing 218 * the page. 219 */ 220 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, 221 (unsigned long)kip->insns, PAGE_SIZE, true, 222 kip->cache->sym); 223 list_del_rcu(&kip->list); 224 synchronize_rcu(); 225 kip->cache->free(kip->insns); 226 kfree(kip); 227 } 228 return 1; 229 } 230 return 0; 231 } 232 233 static int collect_garbage_slots(struct kprobe_insn_cache *c) 234 { 235 struct kprobe_insn_page *kip, *next; 236 237 /* Ensure no-one is interrupted on the garbages */ 238 synchronize_rcu(); 239 240 list_for_each_entry_safe(kip, next, &c->pages, list) { 241 int i; 242 if (kip->ngarbage == 0) 243 continue; 244 kip->ngarbage = 0; /* we will collect all garbages */ 245 for (i = 0; i < slots_per_page(c); i++) { 246 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i)) 247 break; 248 } 249 } 250 c->nr_garbage = 0; 251 return 0; 252 } 253 254 void __free_insn_slot(struct kprobe_insn_cache *c, 255 kprobe_opcode_t *slot, int dirty) 256 { 257 struct kprobe_insn_page *kip; 258 long idx; 259 260 mutex_lock(&c->mutex); 261 rcu_read_lock(); 262 list_for_each_entry_rcu(kip, &c->pages, list) { 263 idx = ((long)slot - (long)kip->insns) / 264 (c->insn_size * sizeof(kprobe_opcode_t)); 265 if (idx >= 0 && idx < slots_per_page(c)) 266 goto out; 267 } 268 /* Could not find this slot. */ 269 WARN_ON(1); 270 kip = NULL; 271 out: 272 rcu_read_unlock(); 273 /* Mark and sweep: this may sleep */ 274 if (kip) { 275 /* Check double free */ 276 WARN_ON(kip->slot_used[idx] != SLOT_USED); 277 if (dirty) { 278 kip->slot_used[idx] = SLOT_DIRTY; 279 kip->ngarbage++; 280 if (++c->nr_garbage > slots_per_page(c)) 281 collect_garbage_slots(c); 282 } else { 283 collect_one_slot(kip, idx); 284 } 285 } 286 mutex_unlock(&c->mutex); 287 } 288 289 /* 290 * Check given address is on the page of kprobe instruction slots. 291 * This will be used for checking whether the address on a stack 292 * is on a text area or not. 293 */ 294 bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr) 295 { 296 struct kprobe_insn_page *kip; 297 bool ret = false; 298 299 rcu_read_lock(); 300 list_for_each_entry_rcu(kip, &c->pages, list) { 301 if (addr >= (unsigned long)kip->insns && 302 addr < (unsigned long)kip->insns + PAGE_SIZE) { 303 ret = true; 304 break; 305 } 306 } 307 rcu_read_unlock(); 308 309 return ret; 310 } 311 312 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum, 313 unsigned long *value, char *type, char *sym) 314 { 315 struct kprobe_insn_page *kip; 316 int ret = -ERANGE; 317 318 rcu_read_lock(); 319 list_for_each_entry_rcu(kip, &c->pages, list) { 320 if ((*symnum)--) 321 continue; 322 strlcpy(sym, c->sym, KSYM_NAME_LEN); 323 *type = 't'; 324 *value = (unsigned long)kip->insns; 325 ret = 0; 326 break; 327 } 328 rcu_read_unlock(); 329 330 return ret; 331 } 332 333 #ifdef CONFIG_OPTPROBES 334 /* For optimized_kprobe buffer */ 335 struct kprobe_insn_cache kprobe_optinsn_slots = { 336 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex), 337 .alloc = alloc_insn_page, 338 .free = free_insn_page, 339 .sym = KPROBE_OPTINSN_PAGE_SYM, 340 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages), 341 /* .insn_size is initialized later */ 342 .nr_garbage = 0, 343 }; 344 #endif 345 #endif 346 347 /* We have preemption disabled.. so it is safe to use __ versions */ 348 static inline void set_kprobe_instance(struct kprobe *kp) 349 { 350 __this_cpu_write(kprobe_instance, kp); 351 } 352 353 static inline void reset_kprobe_instance(void) 354 { 355 __this_cpu_write(kprobe_instance, NULL); 356 } 357 358 /* 359 * This routine is called either: 360 * - under the kprobe_mutex - during kprobe_[un]register() 361 * OR 362 * - with preemption disabled - from arch/xxx/kernel/kprobes.c 363 */ 364 struct kprobe *get_kprobe(void *addr) 365 { 366 struct hlist_head *head; 367 struct kprobe *p; 368 369 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)]; 370 hlist_for_each_entry_rcu(p, head, hlist, 371 lockdep_is_held(&kprobe_mutex)) { 372 if (p->addr == addr) 373 return p; 374 } 375 376 return NULL; 377 } 378 NOKPROBE_SYMBOL(get_kprobe); 379 380 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs); 381 382 /* Return true if the kprobe is an aggregator */ 383 static inline int kprobe_aggrprobe(struct kprobe *p) 384 { 385 return p->pre_handler == aggr_pre_handler; 386 } 387 388 /* Return true(!0) if the kprobe is unused */ 389 static inline int kprobe_unused(struct kprobe *p) 390 { 391 return kprobe_aggrprobe(p) && kprobe_disabled(p) && 392 list_empty(&p->list); 393 } 394 395 /* 396 * Keep all fields in the kprobe consistent 397 */ 398 static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p) 399 { 400 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t)); 401 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn)); 402 } 403 404 #ifdef CONFIG_OPTPROBES 405 /* NOTE: change this value only with kprobe_mutex held */ 406 static bool kprobes_allow_optimization; 407 408 /* 409 * Call all pre_handler on the list, but ignores its return value. 410 * This must be called from arch-dep optimized caller. 411 */ 412 void opt_pre_handler(struct kprobe *p, struct pt_regs *regs) 413 { 414 struct kprobe *kp; 415 416 list_for_each_entry_rcu(kp, &p->list, list) { 417 if (kp->pre_handler && likely(!kprobe_disabled(kp))) { 418 set_kprobe_instance(kp); 419 kp->pre_handler(kp, regs); 420 } 421 reset_kprobe_instance(); 422 } 423 } 424 NOKPROBE_SYMBOL(opt_pre_handler); 425 426 /* Free optimized instructions and optimized_kprobe */ 427 static void free_aggr_kprobe(struct kprobe *p) 428 { 429 struct optimized_kprobe *op; 430 431 op = container_of(p, struct optimized_kprobe, kp); 432 arch_remove_optimized_kprobe(op); 433 arch_remove_kprobe(p); 434 kfree(op); 435 } 436 437 /* Return true(!0) if the kprobe is ready for optimization. */ 438 static inline int kprobe_optready(struct kprobe *p) 439 { 440 struct optimized_kprobe *op; 441 442 if (kprobe_aggrprobe(p)) { 443 op = container_of(p, struct optimized_kprobe, kp); 444 return arch_prepared_optinsn(&op->optinsn); 445 } 446 447 return 0; 448 } 449 450 /* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */ 451 static inline int kprobe_disarmed(struct kprobe *p) 452 { 453 struct optimized_kprobe *op; 454 455 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */ 456 if (!kprobe_aggrprobe(p)) 457 return kprobe_disabled(p); 458 459 op = container_of(p, struct optimized_kprobe, kp); 460 461 return kprobe_disabled(p) && list_empty(&op->list); 462 } 463 464 /* Return true(!0) if the probe is queued on (un)optimizing lists */ 465 static int kprobe_queued(struct kprobe *p) 466 { 467 struct optimized_kprobe *op; 468 469 if (kprobe_aggrprobe(p)) { 470 op = container_of(p, struct optimized_kprobe, kp); 471 if (!list_empty(&op->list)) 472 return 1; 473 } 474 return 0; 475 } 476 477 /* 478 * Return an optimized kprobe whose optimizing code replaces 479 * instructions including addr (exclude breakpoint). 480 */ 481 static struct kprobe *get_optimized_kprobe(unsigned long addr) 482 { 483 int i; 484 struct kprobe *p = NULL; 485 struct optimized_kprobe *op; 486 487 /* Don't check i == 0, since that is a breakpoint case. */ 488 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++) 489 p = get_kprobe((void *)(addr - i)); 490 491 if (p && kprobe_optready(p)) { 492 op = container_of(p, struct optimized_kprobe, kp); 493 if (arch_within_optimized_kprobe(op, addr)) 494 return p; 495 } 496 497 return NULL; 498 } 499 500 /* Optimization staging list, protected by kprobe_mutex */ 501 static LIST_HEAD(optimizing_list); 502 static LIST_HEAD(unoptimizing_list); 503 static LIST_HEAD(freeing_list); 504 505 static void kprobe_optimizer(struct work_struct *work); 506 static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer); 507 #define OPTIMIZE_DELAY 5 508 509 /* 510 * Optimize (replace a breakpoint with a jump) kprobes listed on 511 * optimizing_list. 512 */ 513 static void do_optimize_kprobes(void) 514 { 515 lockdep_assert_held(&text_mutex); 516 /* 517 * The optimization/unoptimization refers online_cpus via 518 * stop_machine() and cpu-hotplug modifies online_cpus. 519 * And same time, text_mutex will be held in cpu-hotplug and here. 520 * This combination can cause a deadlock (cpu-hotplug try to lock 521 * text_mutex but stop_machine can not be done because online_cpus 522 * has been changed) 523 * To avoid this deadlock, caller must have locked cpu hotplug 524 * for preventing cpu-hotplug outside of text_mutex locking. 525 */ 526 lockdep_assert_cpus_held(); 527 528 /* Optimization never be done when disarmed */ 529 if (kprobes_all_disarmed || !kprobes_allow_optimization || 530 list_empty(&optimizing_list)) 531 return; 532 533 arch_optimize_kprobes(&optimizing_list); 534 } 535 536 /* 537 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint 538 * if need) kprobes listed on unoptimizing_list. 539 */ 540 static void do_unoptimize_kprobes(void) 541 { 542 struct optimized_kprobe *op, *tmp; 543 544 lockdep_assert_held(&text_mutex); 545 /* See comment in do_optimize_kprobes() */ 546 lockdep_assert_cpus_held(); 547 548 /* Unoptimization must be done anytime */ 549 if (list_empty(&unoptimizing_list)) 550 return; 551 552 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list); 553 /* Loop free_list for disarming */ 554 list_for_each_entry_safe(op, tmp, &freeing_list, list) { 555 /* Switching from detour code to origin */ 556 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; 557 /* Disarm probes if marked disabled */ 558 if (kprobe_disabled(&op->kp)) 559 arch_disarm_kprobe(&op->kp); 560 if (kprobe_unused(&op->kp)) { 561 /* 562 * Remove unused probes from hash list. After waiting 563 * for synchronization, these probes are reclaimed. 564 * (reclaiming is done by do_free_cleaned_kprobes.) 565 */ 566 hlist_del_rcu(&op->kp.hlist); 567 } else 568 list_del_init(&op->list); 569 } 570 } 571 572 /* Reclaim all kprobes on the free_list */ 573 static void do_free_cleaned_kprobes(void) 574 { 575 struct optimized_kprobe *op, *tmp; 576 577 list_for_each_entry_safe(op, tmp, &freeing_list, list) { 578 list_del_init(&op->list); 579 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) { 580 /* 581 * This must not happen, but if there is a kprobe 582 * still in use, keep it on kprobes hash list. 583 */ 584 continue; 585 } 586 free_aggr_kprobe(&op->kp); 587 } 588 } 589 590 /* Start optimizer after OPTIMIZE_DELAY passed */ 591 static void kick_kprobe_optimizer(void) 592 { 593 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY); 594 } 595 596 /* Kprobe jump optimizer */ 597 static void kprobe_optimizer(struct work_struct *work) 598 { 599 mutex_lock(&kprobe_mutex); 600 cpus_read_lock(); 601 mutex_lock(&text_mutex); 602 603 /* 604 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed) 605 * kprobes before waiting for quiesence period. 606 */ 607 do_unoptimize_kprobes(); 608 609 /* 610 * Step 2: Wait for quiesence period to ensure all potentially 611 * preempted tasks to have normally scheduled. Because optprobe 612 * may modify multiple instructions, there is a chance that Nth 613 * instruction is preempted. In that case, such tasks can return 614 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it. 615 * Note that on non-preemptive kernel, this is transparently converted 616 * to synchronoze_sched() to wait for all interrupts to have completed. 617 */ 618 synchronize_rcu_tasks(); 619 620 /* Step 3: Optimize kprobes after quiesence period */ 621 do_optimize_kprobes(); 622 623 /* Step 4: Free cleaned kprobes after quiesence period */ 624 do_free_cleaned_kprobes(); 625 626 mutex_unlock(&text_mutex); 627 cpus_read_unlock(); 628 629 /* Step 5: Kick optimizer again if needed */ 630 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) 631 kick_kprobe_optimizer(); 632 633 mutex_unlock(&kprobe_mutex); 634 } 635 636 /* Wait for completing optimization and unoptimization */ 637 void wait_for_kprobe_optimizer(void) 638 { 639 mutex_lock(&kprobe_mutex); 640 641 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) { 642 mutex_unlock(&kprobe_mutex); 643 644 /* this will also make optimizing_work execute immmediately */ 645 flush_delayed_work(&optimizing_work); 646 /* @optimizing_work might not have been queued yet, relax */ 647 cpu_relax(); 648 649 mutex_lock(&kprobe_mutex); 650 } 651 652 mutex_unlock(&kprobe_mutex); 653 } 654 655 static bool optprobe_queued_unopt(struct optimized_kprobe *op) 656 { 657 struct optimized_kprobe *_op; 658 659 list_for_each_entry(_op, &unoptimizing_list, list) { 660 if (op == _op) 661 return true; 662 } 663 664 return false; 665 } 666 667 /* Optimize kprobe if p is ready to be optimized */ 668 static void optimize_kprobe(struct kprobe *p) 669 { 670 struct optimized_kprobe *op; 671 672 /* Check if the kprobe is disabled or not ready for optimization. */ 673 if (!kprobe_optready(p) || !kprobes_allow_optimization || 674 (kprobe_disabled(p) || kprobes_all_disarmed)) 675 return; 676 677 /* kprobes with post_handler can not be optimized */ 678 if (p->post_handler) 679 return; 680 681 op = container_of(p, struct optimized_kprobe, kp); 682 683 /* Check there is no other kprobes at the optimized instructions */ 684 if (arch_check_optimized_kprobe(op) < 0) 685 return; 686 687 /* Check if it is already optimized. */ 688 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) { 689 if (optprobe_queued_unopt(op)) { 690 /* This is under unoptimizing. Just dequeue the probe */ 691 list_del_init(&op->list); 692 } 693 return; 694 } 695 op->kp.flags |= KPROBE_FLAG_OPTIMIZED; 696 697 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */ 698 if (WARN_ON_ONCE(!list_empty(&op->list))) 699 return; 700 701 list_add(&op->list, &optimizing_list); 702 kick_kprobe_optimizer(); 703 } 704 705 /* Short cut to direct unoptimizing */ 706 static void force_unoptimize_kprobe(struct optimized_kprobe *op) 707 { 708 lockdep_assert_cpus_held(); 709 arch_unoptimize_kprobe(op); 710 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; 711 } 712 713 /* Unoptimize a kprobe if p is optimized */ 714 static void unoptimize_kprobe(struct kprobe *p, bool force) 715 { 716 struct optimized_kprobe *op; 717 718 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p)) 719 return; /* This is not an optprobe nor optimized */ 720 721 op = container_of(p, struct optimized_kprobe, kp); 722 if (!kprobe_optimized(p)) 723 return; 724 725 if (!list_empty(&op->list)) { 726 if (optprobe_queued_unopt(op)) { 727 /* Queued in unoptimizing queue */ 728 if (force) { 729 /* 730 * Forcibly unoptimize the kprobe here, and queue it 731 * in the freeing list for release afterwards. 732 */ 733 force_unoptimize_kprobe(op); 734 list_move(&op->list, &freeing_list); 735 } 736 } else { 737 /* Dequeue from the optimizing queue */ 738 list_del_init(&op->list); 739 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; 740 } 741 return; 742 } 743 744 /* Optimized kprobe case */ 745 if (force) { 746 /* Forcibly update the code: this is a special case */ 747 force_unoptimize_kprobe(op); 748 } else { 749 list_add(&op->list, &unoptimizing_list); 750 kick_kprobe_optimizer(); 751 } 752 } 753 754 /* Cancel unoptimizing for reusing */ 755 static int reuse_unused_kprobe(struct kprobe *ap) 756 { 757 struct optimized_kprobe *op; 758 759 /* 760 * Unused kprobe MUST be on the way of delayed unoptimizing (means 761 * there is still a relative jump) and disabled. 762 */ 763 op = container_of(ap, struct optimized_kprobe, kp); 764 WARN_ON_ONCE(list_empty(&op->list)); 765 /* Enable the probe again */ 766 ap->flags &= ~KPROBE_FLAG_DISABLED; 767 /* Optimize it again (remove from op->list) */ 768 if (!kprobe_optready(ap)) 769 return -EINVAL; 770 771 optimize_kprobe(ap); 772 return 0; 773 } 774 775 /* Remove optimized instructions */ 776 static void kill_optimized_kprobe(struct kprobe *p) 777 { 778 struct optimized_kprobe *op; 779 780 op = container_of(p, struct optimized_kprobe, kp); 781 if (!list_empty(&op->list)) 782 /* Dequeue from the (un)optimization queue */ 783 list_del_init(&op->list); 784 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; 785 786 if (kprobe_unused(p)) { 787 /* Enqueue if it is unused */ 788 list_add(&op->list, &freeing_list); 789 /* 790 * Remove unused probes from the hash list. After waiting 791 * for synchronization, this probe is reclaimed. 792 * (reclaiming is done by do_free_cleaned_kprobes().) 793 */ 794 hlist_del_rcu(&op->kp.hlist); 795 } 796 797 /* Don't touch the code, because it is already freed. */ 798 arch_remove_optimized_kprobe(op); 799 } 800 801 static inline 802 void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p) 803 { 804 if (!kprobe_ftrace(p)) 805 arch_prepare_optimized_kprobe(op, p); 806 } 807 808 /* Try to prepare optimized instructions */ 809 static void prepare_optimized_kprobe(struct kprobe *p) 810 { 811 struct optimized_kprobe *op; 812 813 op = container_of(p, struct optimized_kprobe, kp); 814 __prepare_optimized_kprobe(op, p); 815 } 816 817 /* Allocate new optimized_kprobe and try to prepare optimized instructions */ 818 static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) 819 { 820 struct optimized_kprobe *op; 821 822 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL); 823 if (!op) 824 return NULL; 825 826 INIT_LIST_HEAD(&op->list); 827 op->kp.addr = p->addr; 828 __prepare_optimized_kprobe(op, p); 829 830 return &op->kp; 831 } 832 833 static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p); 834 835 /* 836 * Prepare an optimized_kprobe and optimize it 837 * NOTE: p must be a normal registered kprobe 838 */ 839 static void try_to_optimize_kprobe(struct kprobe *p) 840 { 841 struct kprobe *ap; 842 struct optimized_kprobe *op; 843 844 /* Impossible to optimize ftrace-based kprobe */ 845 if (kprobe_ftrace(p)) 846 return; 847 848 /* For preparing optimization, jump_label_text_reserved() is called */ 849 cpus_read_lock(); 850 jump_label_lock(); 851 mutex_lock(&text_mutex); 852 853 ap = alloc_aggr_kprobe(p); 854 if (!ap) 855 goto out; 856 857 op = container_of(ap, struct optimized_kprobe, kp); 858 if (!arch_prepared_optinsn(&op->optinsn)) { 859 /* If failed to setup optimizing, fallback to kprobe */ 860 arch_remove_optimized_kprobe(op); 861 kfree(op); 862 goto out; 863 } 864 865 init_aggr_kprobe(ap, p); 866 optimize_kprobe(ap); /* This just kicks optimizer thread */ 867 868 out: 869 mutex_unlock(&text_mutex); 870 jump_label_unlock(); 871 cpus_read_unlock(); 872 } 873 874 #ifdef CONFIG_SYSCTL 875 static void optimize_all_kprobes(void) 876 { 877 struct hlist_head *head; 878 struct kprobe *p; 879 unsigned int i; 880 881 mutex_lock(&kprobe_mutex); 882 /* If optimization is already allowed, just return */ 883 if (kprobes_allow_optimization) 884 goto out; 885 886 cpus_read_lock(); 887 kprobes_allow_optimization = true; 888 for (i = 0; i < KPROBE_TABLE_SIZE; i++) { 889 head = &kprobe_table[i]; 890 hlist_for_each_entry(p, head, hlist) 891 if (!kprobe_disabled(p)) 892 optimize_kprobe(p); 893 } 894 cpus_read_unlock(); 895 printk(KERN_INFO "Kprobes globally optimized\n"); 896 out: 897 mutex_unlock(&kprobe_mutex); 898 } 899 900 static void unoptimize_all_kprobes(void) 901 { 902 struct hlist_head *head; 903 struct kprobe *p; 904 unsigned int i; 905 906 mutex_lock(&kprobe_mutex); 907 /* If optimization is already prohibited, just return */ 908 if (!kprobes_allow_optimization) { 909 mutex_unlock(&kprobe_mutex); 910 return; 911 } 912 913 cpus_read_lock(); 914 kprobes_allow_optimization = false; 915 for (i = 0; i < KPROBE_TABLE_SIZE; i++) { 916 head = &kprobe_table[i]; 917 hlist_for_each_entry(p, head, hlist) { 918 if (!kprobe_disabled(p)) 919 unoptimize_kprobe(p, false); 920 } 921 } 922 cpus_read_unlock(); 923 mutex_unlock(&kprobe_mutex); 924 925 /* Wait for unoptimizing completion */ 926 wait_for_kprobe_optimizer(); 927 printk(KERN_INFO "Kprobes globally unoptimized\n"); 928 } 929 930 static DEFINE_MUTEX(kprobe_sysctl_mutex); 931 int sysctl_kprobes_optimization; 932 int proc_kprobes_optimization_handler(struct ctl_table *table, int write, 933 void *buffer, size_t *length, 934 loff_t *ppos) 935 { 936 int ret; 937 938 mutex_lock(&kprobe_sysctl_mutex); 939 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0; 940 ret = proc_dointvec_minmax(table, write, buffer, length, ppos); 941 942 if (sysctl_kprobes_optimization) 943 optimize_all_kprobes(); 944 else 945 unoptimize_all_kprobes(); 946 mutex_unlock(&kprobe_sysctl_mutex); 947 948 return ret; 949 } 950 #endif /* CONFIG_SYSCTL */ 951 952 /* Put a breakpoint for a probe. Must be called with text_mutex locked */ 953 static void __arm_kprobe(struct kprobe *p) 954 { 955 struct kprobe *_p; 956 957 /* Check collision with other optimized kprobes */ 958 _p = get_optimized_kprobe((unsigned long)p->addr); 959 if (unlikely(_p)) 960 /* Fallback to unoptimized kprobe */ 961 unoptimize_kprobe(_p, true); 962 963 arch_arm_kprobe(p); 964 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */ 965 } 966 967 /* Remove the breakpoint of a probe. Must be called with text_mutex locked */ 968 static void __disarm_kprobe(struct kprobe *p, bool reopt) 969 { 970 struct kprobe *_p; 971 972 /* Try to unoptimize */ 973 unoptimize_kprobe(p, kprobes_all_disarmed); 974 975 if (!kprobe_queued(p)) { 976 arch_disarm_kprobe(p); 977 /* If another kprobe was blocked, optimize it. */ 978 _p = get_optimized_kprobe((unsigned long)p->addr); 979 if (unlikely(_p) && reopt) 980 optimize_kprobe(_p); 981 } 982 /* TODO: reoptimize others after unoptimized this probe */ 983 } 984 985 #else /* !CONFIG_OPTPROBES */ 986 987 #define optimize_kprobe(p) do {} while (0) 988 #define unoptimize_kprobe(p, f) do {} while (0) 989 #define kill_optimized_kprobe(p) do {} while (0) 990 #define prepare_optimized_kprobe(p) do {} while (0) 991 #define try_to_optimize_kprobe(p) do {} while (0) 992 #define __arm_kprobe(p) arch_arm_kprobe(p) 993 #define __disarm_kprobe(p, o) arch_disarm_kprobe(p) 994 #define kprobe_disarmed(p) kprobe_disabled(p) 995 #define wait_for_kprobe_optimizer() do {} while (0) 996 997 static int reuse_unused_kprobe(struct kprobe *ap) 998 { 999 /* 1000 * If the optimized kprobe is NOT supported, the aggr kprobe is 1001 * released at the same time that the last aggregated kprobe is 1002 * unregistered. 1003 * Thus there should be no chance to reuse unused kprobe. 1004 */ 1005 printk(KERN_ERR "Error: There should be no unused kprobe here.\n"); 1006 return -EINVAL; 1007 } 1008 1009 static void free_aggr_kprobe(struct kprobe *p) 1010 { 1011 arch_remove_kprobe(p); 1012 kfree(p); 1013 } 1014 1015 static struct kprobe *alloc_aggr_kprobe(struct kprobe *p) 1016 { 1017 return kzalloc(sizeof(struct kprobe), GFP_KERNEL); 1018 } 1019 #endif /* CONFIG_OPTPROBES */ 1020 1021 #ifdef CONFIG_KPROBES_ON_FTRACE 1022 static struct ftrace_ops kprobe_ftrace_ops __read_mostly = { 1023 .func = kprobe_ftrace_handler, 1024 .flags = FTRACE_OPS_FL_SAVE_REGS, 1025 }; 1026 1027 static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = { 1028 .func = kprobe_ftrace_handler, 1029 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY, 1030 }; 1031 1032 static int kprobe_ipmodify_enabled; 1033 static int kprobe_ftrace_enabled; 1034 1035 /* Must ensure p->addr is really on ftrace */ 1036 static int prepare_kprobe(struct kprobe *p) 1037 { 1038 if (!kprobe_ftrace(p)) 1039 return arch_prepare_kprobe(p); 1040 1041 return arch_prepare_kprobe_ftrace(p); 1042 } 1043 1044 /* Caller must lock kprobe_mutex */ 1045 static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, 1046 int *cnt) 1047 { 1048 int ret = 0; 1049 1050 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0); 1051 if (ret) { 1052 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n", 1053 p->addr, ret); 1054 return ret; 1055 } 1056 1057 if (*cnt == 0) { 1058 ret = register_ftrace_function(ops); 1059 if (ret) { 1060 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret); 1061 goto err_ftrace; 1062 } 1063 } 1064 1065 (*cnt)++; 1066 return ret; 1067 1068 err_ftrace: 1069 /* 1070 * At this point, sinec ops is not registered, we should be sefe from 1071 * registering empty filter. 1072 */ 1073 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); 1074 return ret; 1075 } 1076 1077 static int arm_kprobe_ftrace(struct kprobe *p) 1078 { 1079 bool ipmodify = (p->post_handler != NULL); 1080 1081 return __arm_kprobe_ftrace(p, 1082 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops, 1083 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled); 1084 } 1085 1086 /* Caller must lock kprobe_mutex */ 1087 static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, 1088 int *cnt) 1089 { 1090 int ret = 0; 1091 1092 if (*cnt == 1) { 1093 ret = unregister_ftrace_function(ops); 1094 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret)) 1095 return ret; 1096 } 1097 1098 (*cnt)--; 1099 1100 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); 1101 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n", 1102 p->addr, ret); 1103 return ret; 1104 } 1105 1106 static int disarm_kprobe_ftrace(struct kprobe *p) 1107 { 1108 bool ipmodify = (p->post_handler != NULL); 1109 1110 return __disarm_kprobe_ftrace(p, 1111 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops, 1112 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled); 1113 } 1114 #else /* !CONFIG_KPROBES_ON_FTRACE */ 1115 static inline int prepare_kprobe(struct kprobe *p) 1116 { 1117 return arch_prepare_kprobe(p); 1118 } 1119 1120 static inline int arm_kprobe_ftrace(struct kprobe *p) 1121 { 1122 return -ENODEV; 1123 } 1124 1125 static inline int disarm_kprobe_ftrace(struct kprobe *p) 1126 { 1127 return -ENODEV; 1128 } 1129 #endif 1130 1131 /* Arm a kprobe with text_mutex */ 1132 static int arm_kprobe(struct kprobe *kp) 1133 { 1134 if (unlikely(kprobe_ftrace(kp))) 1135 return arm_kprobe_ftrace(kp); 1136 1137 cpus_read_lock(); 1138 mutex_lock(&text_mutex); 1139 __arm_kprobe(kp); 1140 mutex_unlock(&text_mutex); 1141 cpus_read_unlock(); 1142 1143 return 0; 1144 } 1145 1146 /* Disarm a kprobe with text_mutex */ 1147 static int disarm_kprobe(struct kprobe *kp, bool reopt) 1148 { 1149 if (unlikely(kprobe_ftrace(kp))) 1150 return disarm_kprobe_ftrace(kp); 1151 1152 cpus_read_lock(); 1153 mutex_lock(&text_mutex); 1154 __disarm_kprobe(kp, reopt); 1155 mutex_unlock(&text_mutex); 1156 cpus_read_unlock(); 1157 1158 return 0; 1159 } 1160 1161 /* 1162 * Aggregate handlers for multiple kprobes support - these handlers 1163 * take care of invoking the individual kprobe handlers on p->list 1164 */ 1165 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs) 1166 { 1167 struct kprobe *kp; 1168 1169 list_for_each_entry_rcu(kp, &p->list, list) { 1170 if (kp->pre_handler && likely(!kprobe_disabled(kp))) { 1171 set_kprobe_instance(kp); 1172 if (kp->pre_handler(kp, regs)) 1173 return 1; 1174 } 1175 reset_kprobe_instance(); 1176 } 1177 return 0; 1178 } 1179 NOKPROBE_SYMBOL(aggr_pre_handler); 1180 1181 static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs, 1182 unsigned long flags) 1183 { 1184 struct kprobe *kp; 1185 1186 list_for_each_entry_rcu(kp, &p->list, list) { 1187 if (kp->post_handler && likely(!kprobe_disabled(kp))) { 1188 set_kprobe_instance(kp); 1189 kp->post_handler(kp, regs, flags); 1190 reset_kprobe_instance(); 1191 } 1192 } 1193 } 1194 NOKPROBE_SYMBOL(aggr_post_handler); 1195 1196 static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs, 1197 int trapnr) 1198 { 1199 struct kprobe *cur = __this_cpu_read(kprobe_instance); 1200 1201 /* 1202 * if we faulted "during" the execution of a user specified 1203 * probe handler, invoke just that probe's fault handler 1204 */ 1205 if (cur && cur->fault_handler) { 1206 if (cur->fault_handler(cur, regs, trapnr)) 1207 return 1; 1208 } 1209 return 0; 1210 } 1211 NOKPROBE_SYMBOL(aggr_fault_handler); 1212 1213 /* Walks the list and increments nmissed count for multiprobe case */ 1214 void kprobes_inc_nmissed_count(struct kprobe *p) 1215 { 1216 struct kprobe *kp; 1217 if (!kprobe_aggrprobe(p)) { 1218 p->nmissed++; 1219 } else { 1220 list_for_each_entry_rcu(kp, &p->list, list) 1221 kp->nmissed++; 1222 } 1223 return; 1224 } 1225 NOKPROBE_SYMBOL(kprobes_inc_nmissed_count); 1226 1227 void recycle_rp_inst(struct kretprobe_instance *ri, 1228 struct hlist_head *head) 1229 { 1230 struct kretprobe *rp = ri->rp; 1231 1232 /* remove rp inst off the rprobe_inst_table */ 1233 hlist_del(&ri->hlist); 1234 INIT_HLIST_NODE(&ri->hlist); 1235 if (likely(rp)) { 1236 raw_spin_lock(&rp->lock); 1237 hlist_add_head(&ri->hlist, &rp->free_instances); 1238 raw_spin_unlock(&rp->lock); 1239 } else 1240 /* Unregistering */ 1241 hlist_add_head(&ri->hlist, head); 1242 } 1243 NOKPROBE_SYMBOL(recycle_rp_inst); 1244 1245 void kretprobe_hash_lock(struct task_struct *tsk, 1246 struct hlist_head **head, unsigned long *flags) 1247 __acquires(hlist_lock) 1248 { 1249 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS); 1250 raw_spinlock_t *hlist_lock; 1251 1252 *head = &kretprobe_inst_table[hash]; 1253 hlist_lock = kretprobe_table_lock_ptr(hash); 1254 raw_spin_lock_irqsave(hlist_lock, *flags); 1255 } 1256 NOKPROBE_SYMBOL(kretprobe_hash_lock); 1257 1258 static void kretprobe_table_lock(unsigned long hash, 1259 unsigned long *flags) 1260 __acquires(hlist_lock) 1261 { 1262 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash); 1263 raw_spin_lock_irqsave(hlist_lock, *flags); 1264 } 1265 NOKPROBE_SYMBOL(kretprobe_table_lock); 1266 1267 void kretprobe_hash_unlock(struct task_struct *tsk, 1268 unsigned long *flags) 1269 __releases(hlist_lock) 1270 { 1271 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS); 1272 raw_spinlock_t *hlist_lock; 1273 1274 hlist_lock = kretprobe_table_lock_ptr(hash); 1275 raw_spin_unlock_irqrestore(hlist_lock, *flags); 1276 } 1277 NOKPROBE_SYMBOL(kretprobe_hash_unlock); 1278 1279 static void kretprobe_table_unlock(unsigned long hash, 1280 unsigned long *flags) 1281 __releases(hlist_lock) 1282 { 1283 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash); 1284 raw_spin_unlock_irqrestore(hlist_lock, *flags); 1285 } 1286 NOKPROBE_SYMBOL(kretprobe_table_unlock); 1287 1288 struct kprobe kprobe_busy = { 1289 .addr = (void *) get_kprobe, 1290 }; 1291 1292 void kprobe_busy_begin(void) 1293 { 1294 struct kprobe_ctlblk *kcb; 1295 1296 preempt_disable(); 1297 __this_cpu_write(current_kprobe, &kprobe_busy); 1298 kcb = get_kprobe_ctlblk(); 1299 kcb->kprobe_status = KPROBE_HIT_ACTIVE; 1300 } 1301 1302 void kprobe_busy_end(void) 1303 { 1304 __this_cpu_write(current_kprobe, NULL); 1305 preempt_enable(); 1306 } 1307 1308 /* 1309 * This function is called from finish_task_switch when task tk becomes dead, 1310 * so that we can recycle any function-return probe instances associated 1311 * with this task. These left over instances represent probed functions 1312 * that have been called but will never return. 1313 */ 1314 void kprobe_flush_task(struct task_struct *tk) 1315 { 1316 struct kretprobe_instance *ri; 1317 struct hlist_head *head, empty_rp; 1318 struct hlist_node *tmp; 1319 unsigned long hash, flags = 0; 1320 1321 if (unlikely(!kprobes_initialized)) 1322 /* Early boot. kretprobe_table_locks not yet initialized. */ 1323 return; 1324 1325 kprobe_busy_begin(); 1326 1327 INIT_HLIST_HEAD(&empty_rp); 1328 hash = hash_ptr(tk, KPROBE_HASH_BITS); 1329 head = &kretprobe_inst_table[hash]; 1330 kretprobe_table_lock(hash, &flags); 1331 hlist_for_each_entry_safe(ri, tmp, head, hlist) { 1332 if (ri->task == tk) 1333 recycle_rp_inst(ri, &empty_rp); 1334 } 1335 kretprobe_table_unlock(hash, &flags); 1336 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) { 1337 hlist_del(&ri->hlist); 1338 kfree(ri); 1339 } 1340 1341 kprobe_busy_end(); 1342 } 1343 NOKPROBE_SYMBOL(kprobe_flush_task); 1344 1345 static inline void free_rp_inst(struct kretprobe *rp) 1346 { 1347 struct kretprobe_instance *ri; 1348 struct hlist_node *next; 1349 1350 hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) { 1351 hlist_del(&ri->hlist); 1352 kfree(ri); 1353 } 1354 } 1355 1356 static void cleanup_rp_inst(struct kretprobe *rp) 1357 { 1358 unsigned long flags, hash; 1359 struct kretprobe_instance *ri; 1360 struct hlist_node *next; 1361 struct hlist_head *head; 1362 1363 /* No race here */ 1364 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) { 1365 kretprobe_table_lock(hash, &flags); 1366 head = &kretprobe_inst_table[hash]; 1367 hlist_for_each_entry_safe(ri, next, head, hlist) { 1368 if (ri->rp == rp) 1369 ri->rp = NULL; 1370 } 1371 kretprobe_table_unlock(hash, &flags); 1372 } 1373 free_rp_inst(rp); 1374 } 1375 NOKPROBE_SYMBOL(cleanup_rp_inst); 1376 1377 /* Add the new probe to ap->list */ 1378 static int add_new_kprobe(struct kprobe *ap, struct kprobe *p) 1379 { 1380 if (p->post_handler) 1381 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */ 1382 1383 list_add_rcu(&p->list, &ap->list); 1384 if (p->post_handler && !ap->post_handler) 1385 ap->post_handler = aggr_post_handler; 1386 1387 return 0; 1388 } 1389 1390 /* 1391 * Fill in the required fields of the "manager kprobe". Replace the 1392 * earlier kprobe in the hlist with the manager kprobe 1393 */ 1394 static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p) 1395 { 1396 /* Copy p's insn slot to ap */ 1397 copy_kprobe(p, ap); 1398 flush_insn_slot(ap); 1399 ap->addr = p->addr; 1400 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED; 1401 ap->pre_handler = aggr_pre_handler; 1402 ap->fault_handler = aggr_fault_handler; 1403 /* We don't care the kprobe which has gone. */ 1404 if (p->post_handler && !kprobe_gone(p)) 1405 ap->post_handler = aggr_post_handler; 1406 1407 INIT_LIST_HEAD(&ap->list); 1408 INIT_HLIST_NODE(&ap->hlist); 1409 1410 list_add_rcu(&p->list, &ap->list); 1411 hlist_replace_rcu(&p->hlist, &ap->hlist); 1412 } 1413 1414 /* 1415 * This is the second or subsequent kprobe at the address - handle 1416 * the intricacies 1417 */ 1418 static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) 1419 { 1420 int ret = 0; 1421 struct kprobe *ap = orig_p; 1422 1423 cpus_read_lock(); 1424 1425 /* For preparing optimization, jump_label_text_reserved() is called */ 1426 jump_label_lock(); 1427 mutex_lock(&text_mutex); 1428 1429 if (!kprobe_aggrprobe(orig_p)) { 1430 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */ 1431 ap = alloc_aggr_kprobe(orig_p); 1432 if (!ap) { 1433 ret = -ENOMEM; 1434 goto out; 1435 } 1436 init_aggr_kprobe(ap, orig_p); 1437 } else if (kprobe_unused(ap)) { 1438 /* This probe is going to die. Rescue it */ 1439 ret = reuse_unused_kprobe(ap); 1440 if (ret) 1441 goto out; 1442 } 1443 1444 if (kprobe_gone(ap)) { 1445 /* 1446 * Attempting to insert new probe at the same location that 1447 * had a probe in the module vaddr area which already 1448 * freed. So, the instruction slot has already been 1449 * released. We need a new slot for the new probe. 1450 */ 1451 ret = arch_prepare_kprobe(ap); 1452 if (ret) 1453 /* 1454 * Even if fail to allocate new slot, don't need to 1455 * free aggr_probe. It will be used next time, or 1456 * freed by unregister_kprobe. 1457 */ 1458 goto out; 1459 1460 /* Prepare optimized instructions if possible. */ 1461 prepare_optimized_kprobe(ap); 1462 1463 /* 1464 * Clear gone flag to prevent allocating new slot again, and 1465 * set disabled flag because it is not armed yet. 1466 */ 1467 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE) 1468 | KPROBE_FLAG_DISABLED; 1469 } 1470 1471 /* Copy ap's insn slot to p */ 1472 copy_kprobe(ap, p); 1473 ret = add_new_kprobe(ap, p); 1474 1475 out: 1476 mutex_unlock(&text_mutex); 1477 jump_label_unlock(); 1478 cpus_read_unlock(); 1479 1480 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) { 1481 ap->flags &= ~KPROBE_FLAG_DISABLED; 1482 if (!kprobes_all_disarmed) { 1483 /* Arm the breakpoint again. */ 1484 ret = arm_kprobe(ap); 1485 if (ret) { 1486 ap->flags |= KPROBE_FLAG_DISABLED; 1487 list_del_rcu(&p->list); 1488 synchronize_rcu(); 1489 } 1490 } 1491 } 1492 return ret; 1493 } 1494 1495 bool __weak arch_within_kprobe_blacklist(unsigned long addr) 1496 { 1497 /* The __kprobes marked functions and entry code must not be probed */ 1498 return addr >= (unsigned long)__kprobes_text_start && 1499 addr < (unsigned long)__kprobes_text_end; 1500 } 1501 1502 static bool __within_kprobe_blacklist(unsigned long addr) 1503 { 1504 struct kprobe_blacklist_entry *ent; 1505 1506 if (arch_within_kprobe_blacklist(addr)) 1507 return true; 1508 /* 1509 * If there exists a kprobe_blacklist, verify and 1510 * fail any probe registration in the prohibited area 1511 */ 1512 list_for_each_entry(ent, &kprobe_blacklist, list) { 1513 if (addr >= ent->start_addr && addr < ent->end_addr) 1514 return true; 1515 } 1516 return false; 1517 } 1518 1519 bool within_kprobe_blacklist(unsigned long addr) 1520 { 1521 char symname[KSYM_NAME_LEN], *p; 1522 1523 if (__within_kprobe_blacklist(addr)) 1524 return true; 1525 1526 /* Check if the address is on a suffixed-symbol */ 1527 if (!lookup_symbol_name(addr, symname)) { 1528 p = strchr(symname, '.'); 1529 if (!p) 1530 return false; 1531 *p = '\0'; 1532 addr = (unsigned long)kprobe_lookup_name(symname, 0); 1533 if (addr) 1534 return __within_kprobe_blacklist(addr); 1535 } 1536 return false; 1537 } 1538 1539 /* 1540 * If we have a symbol_name argument, look it up and add the offset field 1541 * to it. This way, we can specify a relative address to a symbol. 1542 * This returns encoded errors if it fails to look up symbol or invalid 1543 * combination of parameters. 1544 */ 1545 static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr, 1546 const char *symbol_name, unsigned int offset) 1547 { 1548 if ((symbol_name && addr) || (!symbol_name && !addr)) 1549 goto invalid; 1550 1551 if (symbol_name) { 1552 addr = kprobe_lookup_name(symbol_name, offset); 1553 if (!addr) 1554 return ERR_PTR(-ENOENT); 1555 } 1556 1557 addr = (kprobe_opcode_t *)(((char *)addr) + offset); 1558 if (addr) 1559 return addr; 1560 1561 invalid: 1562 return ERR_PTR(-EINVAL); 1563 } 1564 1565 static kprobe_opcode_t *kprobe_addr(struct kprobe *p) 1566 { 1567 return _kprobe_addr(p->addr, p->symbol_name, p->offset); 1568 } 1569 1570 /* Check passed kprobe is valid and return kprobe in kprobe_table. */ 1571 static struct kprobe *__get_valid_kprobe(struct kprobe *p) 1572 { 1573 struct kprobe *ap, *list_p; 1574 1575 lockdep_assert_held(&kprobe_mutex); 1576 1577 ap = get_kprobe(p->addr); 1578 if (unlikely(!ap)) 1579 return NULL; 1580 1581 if (p != ap) { 1582 list_for_each_entry(list_p, &ap->list, list) 1583 if (list_p == p) 1584 /* kprobe p is a valid probe */ 1585 goto valid; 1586 return NULL; 1587 } 1588 valid: 1589 return ap; 1590 } 1591 1592 /* Return error if the kprobe is being re-registered */ 1593 static inline int check_kprobe_rereg(struct kprobe *p) 1594 { 1595 int ret = 0; 1596 1597 mutex_lock(&kprobe_mutex); 1598 if (__get_valid_kprobe(p)) 1599 ret = -EINVAL; 1600 mutex_unlock(&kprobe_mutex); 1601 1602 return ret; 1603 } 1604 1605 int __weak arch_check_ftrace_location(struct kprobe *p) 1606 { 1607 unsigned long ftrace_addr; 1608 1609 ftrace_addr = ftrace_location((unsigned long)p->addr); 1610 if (ftrace_addr) { 1611 #ifdef CONFIG_KPROBES_ON_FTRACE 1612 /* Given address is not on the instruction boundary */ 1613 if ((unsigned long)p->addr != ftrace_addr) 1614 return -EILSEQ; 1615 p->flags |= KPROBE_FLAG_FTRACE; 1616 #else /* !CONFIG_KPROBES_ON_FTRACE */ 1617 return -EINVAL; 1618 #endif 1619 } 1620 return 0; 1621 } 1622 1623 static int check_kprobe_address_safe(struct kprobe *p, 1624 struct module **probed_mod) 1625 { 1626 int ret; 1627 1628 ret = arch_check_ftrace_location(p); 1629 if (ret) 1630 return ret; 1631 jump_label_lock(); 1632 preempt_disable(); 1633 1634 /* Ensure it is not in reserved area nor out of text */ 1635 if (!kernel_text_address((unsigned long) p->addr) || 1636 within_kprobe_blacklist((unsigned long) p->addr) || 1637 jump_label_text_reserved(p->addr, p->addr) || 1638 static_call_text_reserved(p->addr, p->addr) || 1639 find_bug((unsigned long)p->addr)) { 1640 ret = -EINVAL; 1641 goto out; 1642 } 1643 1644 /* Check if are we probing a module */ 1645 *probed_mod = __module_text_address((unsigned long) p->addr); 1646 if (*probed_mod) { 1647 /* 1648 * We must hold a refcount of the probed module while updating 1649 * its code to prohibit unexpected unloading. 1650 */ 1651 if (unlikely(!try_module_get(*probed_mod))) { 1652 ret = -ENOENT; 1653 goto out; 1654 } 1655 1656 /* 1657 * If the module freed .init.text, we couldn't insert 1658 * kprobes in there. 1659 */ 1660 if (within_module_init((unsigned long)p->addr, *probed_mod) && 1661 (*probed_mod)->state != MODULE_STATE_COMING) { 1662 module_put(*probed_mod); 1663 *probed_mod = NULL; 1664 ret = -ENOENT; 1665 } 1666 } 1667 out: 1668 preempt_enable(); 1669 jump_label_unlock(); 1670 1671 return ret; 1672 } 1673 1674 int register_kprobe(struct kprobe *p) 1675 { 1676 int ret; 1677 struct kprobe *old_p; 1678 struct module *probed_mod; 1679 kprobe_opcode_t *addr; 1680 1681 /* Adjust probe address from symbol */ 1682 addr = kprobe_addr(p); 1683 if (IS_ERR(addr)) 1684 return PTR_ERR(addr); 1685 p->addr = addr; 1686 1687 ret = check_kprobe_rereg(p); 1688 if (ret) 1689 return ret; 1690 1691 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ 1692 p->flags &= KPROBE_FLAG_DISABLED; 1693 p->nmissed = 0; 1694 INIT_LIST_HEAD(&p->list); 1695 1696 ret = check_kprobe_address_safe(p, &probed_mod); 1697 if (ret) 1698 return ret; 1699 1700 mutex_lock(&kprobe_mutex); 1701 1702 old_p = get_kprobe(p->addr); 1703 if (old_p) { 1704 /* Since this may unoptimize old_p, locking text_mutex. */ 1705 ret = register_aggr_kprobe(old_p, p); 1706 goto out; 1707 } 1708 1709 cpus_read_lock(); 1710 /* Prevent text modification */ 1711 mutex_lock(&text_mutex); 1712 ret = prepare_kprobe(p); 1713 mutex_unlock(&text_mutex); 1714 cpus_read_unlock(); 1715 if (ret) 1716 goto out; 1717 1718 INIT_HLIST_NODE(&p->hlist); 1719 hlist_add_head_rcu(&p->hlist, 1720 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); 1721 1722 if (!kprobes_all_disarmed && !kprobe_disabled(p)) { 1723 ret = arm_kprobe(p); 1724 if (ret) { 1725 hlist_del_rcu(&p->hlist); 1726 synchronize_rcu(); 1727 goto out; 1728 } 1729 } 1730 1731 /* Try to optimize kprobe */ 1732 try_to_optimize_kprobe(p); 1733 out: 1734 mutex_unlock(&kprobe_mutex); 1735 1736 if (probed_mod) 1737 module_put(probed_mod); 1738 1739 return ret; 1740 } 1741 EXPORT_SYMBOL_GPL(register_kprobe); 1742 1743 /* Check if all probes on the aggrprobe are disabled */ 1744 static int aggr_kprobe_disabled(struct kprobe *ap) 1745 { 1746 struct kprobe *kp; 1747 1748 lockdep_assert_held(&kprobe_mutex); 1749 1750 list_for_each_entry(kp, &ap->list, list) 1751 if (!kprobe_disabled(kp)) 1752 /* 1753 * There is an active probe on the list. 1754 * We can't disable this ap. 1755 */ 1756 return 0; 1757 1758 return 1; 1759 } 1760 1761 /* Disable one kprobe: Make sure called under kprobe_mutex is locked */ 1762 static struct kprobe *__disable_kprobe(struct kprobe *p) 1763 { 1764 struct kprobe *orig_p; 1765 int ret; 1766 1767 /* Get an original kprobe for return */ 1768 orig_p = __get_valid_kprobe(p); 1769 if (unlikely(orig_p == NULL)) 1770 return ERR_PTR(-EINVAL); 1771 1772 if (!kprobe_disabled(p)) { 1773 /* Disable probe if it is a child probe */ 1774 if (p != orig_p) 1775 p->flags |= KPROBE_FLAG_DISABLED; 1776 1777 /* Try to disarm and disable this/parent probe */ 1778 if (p == orig_p || aggr_kprobe_disabled(orig_p)) { 1779 /* 1780 * If kprobes_all_disarmed is set, orig_p 1781 * should have already been disarmed, so 1782 * skip unneed disarming process. 1783 */ 1784 if (!kprobes_all_disarmed) { 1785 ret = disarm_kprobe(orig_p, true); 1786 if (ret) { 1787 p->flags &= ~KPROBE_FLAG_DISABLED; 1788 return ERR_PTR(ret); 1789 } 1790 } 1791 orig_p->flags |= KPROBE_FLAG_DISABLED; 1792 } 1793 } 1794 1795 return orig_p; 1796 } 1797 1798 /* 1799 * Unregister a kprobe without a scheduler synchronization. 1800 */ 1801 static int __unregister_kprobe_top(struct kprobe *p) 1802 { 1803 struct kprobe *ap, *list_p; 1804 1805 /* Disable kprobe. This will disarm it if needed. */ 1806 ap = __disable_kprobe(p); 1807 if (IS_ERR(ap)) 1808 return PTR_ERR(ap); 1809 1810 if (ap == p) 1811 /* 1812 * This probe is an independent(and non-optimized) kprobe 1813 * (not an aggrprobe). Remove from the hash list. 1814 */ 1815 goto disarmed; 1816 1817 /* Following process expects this probe is an aggrprobe */ 1818 WARN_ON(!kprobe_aggrprobe(ap)); 1819 1820 if (list_is_singular(&ap->list) && kprobe_disarmed(ap)) 1821 /* 1822 * !disarmed could be happen if the probe is under delayed 1823 * unoptimizing. 1824 */ 1825 goto disarmed; 1826 else { 1827 /* If disabling probe has special handlers, update aggrprobe */ 1828 if (p->post_handler && !kprobe_gone(p)) { 1829 list_for_each_entry(list_p, &ap->list, list) { 1830 if ((list_p != p) && (list_p->post_handler)) 1831 goto noclean; 1832 } 1833 ap->post_handler = NULL; 1834 } 1835 noclean: 1836 /* 1837 * Remove from the aggrprobe: this path will do nothing in 1838 * __unregister_kprobe_bottom(). 1839 */ 1840 list_del_rcu(&p->list); 1841 if (!kprobe_disabled(ap) && !kprobes_all_disarmed) 1842 /* 1843 * Try to optimize this probe again, because post 1844 * handler may have been changed. 1845 */ 1846 optimize_kprobe(ap); 1847 } 1848 return 0; 1849 1850 disarmed: 1851 hlist_del_rcu(&ap->hlist); 1852 return 0; 1853 } 1854 1855 static void __unregister_kprobe_bottom(struct kprobe *p) 1856 { 1857 struct kprobe *ap; 1858 1859 if (list_empty(&p->list)) 1860 /* This is an independent kprobe */ 1861 arch_remove_kprobe(p); 1862 else if (list_is_singular(&p->list)) { 1863 /* This is the last child of an aggrprobe */ 1864 ap = list_entry(p->list.next, struct kprobe, list); 1865 list_del(&p->list); 1866 free_aggr_kprobe(ap); 1867 } 1868 /* Otherwise, do nothing. */ 1869 } 1870 1871 int register_kprobes(struct kprobe **kps, int num) 1872 { 1873 int i, ret = 0; 1874 1875 if (num <= 0) 1876 return -EINVAL; 1877 for (i = 0; i < num; i++) { 1878 ret = register_kprobe(kps[i]); 1879 if (ret < 0) { 1880 if (i > 0) 1881 unregister_kprobes(kps, i); 1882 break; 1883 } 1884 } 1885 return ret; 1886 } 1887 EXPORT_SYMBOL_GPL(register_kprobes); 1888 1889 void unregister_kprobe(struct kprobe *p) 1890 { 1891 unregister_kprobes(&p, 1); 1892 } 1893 EXPORT_SYMBOL_GPL(unregister_kprobe); 1894 1895 void unregister_kprobes(struct kprobe **kps, int num) 1896 { 1897 int i; 1898 1899 if (num <= 0) 1900 return; 1901 mutex_lock(&kprobe_mutex); 1902 for (i = 0; i < num; i++) 1903 if (__unregister_kprobe_top(kps[i]) < 0) 1904 kps[i]->addr = NULL; 1905 mutex_unlock(&kprobe_mutex); 1906 1907 synchronize_rcu(); 1908 for (i = 0; i < num; i++) 1909 if (kps[i]->addr) 1910 __unregister_kprobe_bottom(kps[i]); 1911 } 1912 EXPORT_SYMBOL_GPL(unregister_kprobes); 1913 1914 int __weak kprobe_exceptions_notify(struct notifier_block *self, 1915 unsigned long val, void *data) 1916 { 1917 return NOTIFY_DONE; 1918 } 1919 NOKPROBE_SYMBOL(kprobe_exceptions_notify); 1920 1921 static struct notifier_block kprobe_exceptions_nb = { 1922 .notifier_call = kprobe_exceptions_notify, 1923 .priority = 0x7fffffff /* we need to be notified first */ 1924 }; 1925 1926 unsigned long __weak arch_deref_entry_point(void *entry) 1927 { 1928 return (unsigned long)entry; 1929 } 1930 1931 #ifdef CONFIG_KRETPROBES 1932 /* 1933 * This kprobe pre_handler is registered with every kretprobe. When probe 1934 * hits it will set up the return probe. 1935 */ 1936 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) 1937 { 1938 struct kretprobe *rp = container_of(p, struct kretprobe, kp); 1939 unsigned long hash, flags = 0; 1940 struct kretprobe_instance *ri; 1941 1942 /* 1943 * To avoid deadlocks, prohibit return probing in NMI contexts, 1944 * just skip the probe and increase the (inexact) 'nmissed' 1945 * statistical counter, so that the user is informed that 1946 * something happened: 1947 */ 1948 if (unlikely(in_nmi())) { 1949 rp->nmissed++; 1950 return 0; 1951 } 1952 1953 /* TODO: consider to only swap the RA after the last pre_handler fired */ 1954 hash = hash_ptr(current, KPROBE_HASH_BITS); 1955 raw_spin_lock_irqsave(&rp->lock, flags); 1956 if (!hlist_empty(&rp->free_instances)) { 1957 ri = hlist_entry(rp->free_instances.first, 1958 struct kretprobe_instance, hlist); 1959 hlist_del(&ri->hlist); 1960 raw_spin_unlock_irqrestore(&rp->lock, flags); 1961 1962 ri->rp = rp; 1963 ri->task = current; 1964 1965 if (rp->entry_handler && rp->entry_handler(ri, regs)) { 1966 raw_spin_lock_irqsave(&rp->lock, flags); 1967 hlist_add_head(&ri->hlist, &rp->free_instances); 1968 raw_spin_unlock_irqrestore(&rp->lock, flags); 1969 return 0; 1970 } 1971 1972 arch_prepare_kretprobe(ri, regs); 1973 1974 /* XXX(hch): why is there no hlist_move_head? */ 1975 INIT_HLIST_NODE(&ri->hlist); 1976 kretprobe_table_lock(hash, &flags); 1977 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]); 1978 kretprobe_table_unlock(hash, &flags); 1979 } else { 1980 rp->nmissed++; 1981 raw_spin_unlock_irqrestore(&rp->lock, flags); 1982 } 1983 return 0; 1984 } 1985 NOKPROBE_SYMBOL(pre_handler_kretprobe); 1986 1987 bool __weak arch_kprobe_on_func_entry(unsigned long offset) 1988 { 1989 return !offset; 1990 } 1991 1992 bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset) 1993 { 1994 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset); 1995 1996 if (IS_ERR(kp_addr)) 1997 return false; 1998 1999 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset) || 2000 !arch_kprobe_on_func_entry(offset)) 2001 return false; 2002 2003 return true; 2004 } 2005 2006 int register_kretprobe(struct kretprobe *rp) 2007 { 2008 int ret = 0; 2009 struct kretprobe_instance *inst; 2010 int i; 2011 void *addr; 2012 2013 if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset)) 2014 return -EINVAL; 2015 2016 if (kretprobe_blacklist_size) { 2017 addr = kprobe_addr(&rp->kp); 2018 if (IS_ERR(addr)) 2019 return PTR_ERR(addr); 2020 2021 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { 2022 if (kretprobe_blacklist[i].addr == addr) 2023 return -EINVAL; 2024 } 2025 } 2026 2027 rp->kp.pre_handler = pre_handler_kretprobe; 2028 rp->kp.post_handler = NULL; 2029 rp->kp.fault_handler = NULL; 2030 2031 /* Pre-allocate memory for max kretprobe instances */ 2032 if (rp->maxactive <= 0) { 2033 #ifdef CONFIG_PREEMPTION 2034 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus()); 2035 #else 2036 rp->maxactive = num_possible_cpus(); 2037 #endif 2038 } 2039 raw_spin_lock_init(&rp->lock); 2040 INIT_HLIST_HEAD(&rp->free_instances); 2041 for (i = 0; i < rp->maxactive; i++) { 2042 inst = kmalloc(sizeof(struct kretprobe_instance) + 2043 rp->data_size, GFP_KERNEL); 2044 if (inst == NULL) { 2045 free_rp_inst(rp); 2046 return -ENOMEM; 2047 } 2048 INIT_HLIST_NODE(&inst->hlist); 2049 hlist_add_head(&inst->hlist, &rp->free_instances); 2050 } 2051 2052 rp->nmissed = 0; 2053 /* Establish function entry probe point */ 2054 ret = register_kprobe(&rp->kp); 2055 if (ret != 0) 2056 free_rp_inst(rp); 2057 return ret; 2058 } 2059 EXPORT_SYMBOL_GPL(register_kretprobe); 2060 2061 int register_kretprobes(struct kretprobe **rps, int num) 2062 { 2063 int ret = 0, i; 2064 2065 if (num <= 0) 2066 return -EINVAL; 2067 for (i = 0; i < num; i++) { 2068 ret = register_kretprobe(rps[i]); 2069 if (ret < 0) { 2070 if (i > 0) 2071 unregister_kretprobes(rps, i); 2072 break; 2073 } 2074 } 2075 return ret; 2076 } 2077 EXPORT_SYMBOL_GPL(register_kretprobes); 2078 2079 void unregister_kretprobe(struct kretprobe *rp) 2080 { 2081 unregister_kretprobes(&rp, 1); 2082 } 2083 EXPORT_SYMBOL_GPL(unregister_kretprobe); 2084 2085 void unregister_kretprobes(struct kretprobe **rps, int num) 2086 { 2087 int i; 2088 2089 if (num <= 0) 2090 return; 2091 mutex_lock(&kprobe_mutex); 2092 for (i = 0; i < num; i++) 2093 if (__unregister_kprobe_top(&rps[i]->kp) < 0) 2094 rps[i]->kp.addr = NULL; 2095 mutex_unlock(&kprobe_mutex); 2096 2097 synchronize_rcu(); 2098 for (i = 0; i < num; i++) { 2099 if (rps[i]->kp.addr) { 2100 __unregister_kprobe_bottom(&rps[i]->kp); 2101 cleanup_rp_inst(rps[i]); 2102 } 2103 } 2104 } 2105 EXPORT_SYMBOL_GPL(unregister_kretprobes); 2106 2107 #else /* CONFIG_KRETPROBES */ 2108 int register_kretprobe(struct kretprobe *rp) 2109 { 2110 return -ENOSYS; 2111 } 2112 EXPORT_SYMBOL_GPL(register_kretprobe); 2113 2114 int register_kretprobes(struct kretprobe **rps, int num) 2115 { 2116 return -ENOSYS; 2117 } 2118 EXPORT_SYMBOL_GPL(register_kretprobes); 2119 2120 void unregister_kretprobe(struct kretprobe *rp) 2121 { 2122 } 2123 EXPORT_SYMBOL_GPL(unregister_kretprobe); 2124 2125 void unregister_kretprobes(struct kretprobe **rps, int num) 2126 { 2127 } 2128 EXPORT_SYMBOL_GPL(unregister_kretprobes); 2129 2130 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) 2131 { 2132 return 0; 2133 } 2134 NOKPROBE_SYMBOL(pre_handler_kretprobe); 2135 2136 #endif /* CONFIG_KRETPROBES */ 2137 2138 /* Set the kprobe gone and remove its instruction buffer. */ 2139 static void kill_kprobe(struct kprobe *p) 2140 { 2141 struct kprobe *kp; 2142 2143 lockdep_assert_held(&kprobe_mutex); 2144 2145 p->flags |= KPROBE_FLAG_GONE; 2146 if (kprobe_aggrprobe(p)) { 2147 /* 2148 * If this is an aggr_kprobe, we have to list all the 2149 * chained probes and mark them GONE. 2150 */ 2151 list_for_each_entry(kp, &p->list, list) 2152 kp->flags |= KPROBE_FLAG_GONE; 2153 p->post_handler = NULL; 2154 kill_optimized_kprobe(p); 2155 } 2156 /* 2157 * Here, we can remove insn_slot safely, because no thread calls 2158 * the original probed function (which will be freed soon) any more. 2159 */ 2160 arch_remove_kprobe(p); 2161 2162 /* 2163 * The module is going away. We should disarm the kprobe which 2164 * is using ftrace. 2165 */ 2166 if (kprobe_ftrace(p)) 2167 disarm_kprobe_ftrace(p); 2168 } 2169 2170 /* Disable one kprobe */ 2171 int disable_kprobe(struct kprobe *kp) 2172 { 2173 int ret = 0; 2174 struct kprobe *p; 2175 2176 mutex_lock(&kprobe_mutex); 2177 2178 /* Disable this kprobe */ 2179 p = __disable_kprobe(kp); 2180 if (IS_ERR(p)) 2181 ret = PTR_ERR(p); 2182 2183 mutex_unlock(&kprobe_mutex); 2184 return ret; 2185 } 2186 EXPORT_SYMBOL_GPL(disable_kprobe); 2187 2188 /* Enable one kprobe */ 2189 int enable_kprobe(struct kprobe *kp) 2190 { 2191 int ret = 0; 2192 struct kprobe *p; 2193 2194 mutex_lock(&kprobe_mutex); 2195 2196 /* Check whether specified probe is valid. */ 2197 p = __get_valid_kprobe(kp); 2198 if (unlikely(p == NULL)) { 2199 ret = -EINVAL; 2200 goto out; 2201 } 2202 2203 if (kprobe_gone(kp)) { 2204 /* This kprobe has gone, we couldn't enable it. */ 2205 ret = -EINVAL; 2206 goto out; 2207 } 2208 2209 if (p != kp) 2210 kp->flags &= ~KPROBE_FLAG_DISABLED; 2211 2212 if (!kprobes_all_disarmed && kprobe_disabled(p)) { 2213 p->flags &= ~KPROBE_FLAG_DISABLED; 2214 ret = arm_kprobe(p); 2215 if (ret) 2216 p->flags |= KPROBE_FLAG_DISABLED; 2217 } 2218 out: 2219 mutex_unlock(&kprobe_mutex); 2220 return ret; 2221 } 2222 EXPORT_SYMBOL_GPL(enable_kprobe); 2223 2224 /* Caller must NOT call this in usual path. This is only for critical case */ 2225 void dump_kprobe(struct kprobe *kp) 2226 { 2227 pr_err("Dumping kprobe:\n"); 2228 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n", 2229 kp->symbol_name, kp->offset, kp->addr); 2230 } 2231 NOKPROBE_SYMBOL(dump_kprobe); 2232 2233 int kprobe_add_ksym_blacklist(unsigned long entry) 2234 { 2235 struct kprobe_blacklist_entry *ent; 2236 unsigned long offset = 0, size = 0; 2237 2238 if (!kernel_text_address(entry) || 2239 !kallsyms_lookup_size_offset(entry, &size, &offset)) 2240 return -EINVAL; 2241 2242 ent = kmalloc(sizeof(*ent), GFP_KERNEL); 2243 if (!ent) 2244 return -ENOMEM; 2245 ent->start_addr = entry; 2246 ent->end_addr = entry + size; 2247 INIT_LIST_HEAD(&ent->list); 2248 list_add_tail(&ent->list, &kprobe_blacklist); 2249 2250 return (int)size; 2251 } 2252 2253 /* Add all symbols in given area into kprobe blacklist */ 2254 int kprobe_add_area_blacklist(unsigned long start, unsigned long end) 2255 { 2256 unsigned long entry; 2257 int ret = 0; 2258 2259 for (entry = start; entry < end; entry += ret) { 2260 ret = kprobe_add_ksym_blacklist(entry); 2261 if (ret < 0) 2262 return ret; 2263 if (ret == 0) /* In case of alias symbol */ 2264 ret = 1; 2265 } 2266 return 0; 2267 } 2268 2269 /* Remove all symbols in given area from kprobe blacklist */ 2270 static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end) 2271 { 2272 struct kprobe_blacklist_entry *ent, *n; 2273 2274 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) { 2275 if (ent->start_addr < start || ent->start_addr >= end) 2276 continue; 2277 list_del(&ent->list); 2278 kfree(ent); 2279 } 2280 } 2281 2282 static void kprobe_remove_ksym_blacklist(unsigned long entry) 2283 { 2284 kprobe_remove_area_blacklist(entry, entry + 1); 2285 } 2286 2287 int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value, 2288 char *type, char *sym) 2289 { 2290 return -ERANGE; 2291 } 2292 2293 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type, 2294 char *sym) 2295 { 2296 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT 2297 if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym)) 2298 return 0; 2299 #ifdef CONFIG_OPTPROBES 2300 if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym)) 2301 return 0; 2302 #endif 2303 #endif 2304 if (!arch_kprobe_get_kallsym(&symnum, value, type, sym)) 2305 return 0; 2306 return -ERANGE; 2307 } 2308 2309 int __init __weak arch_populate_kprobe_blacklist(void) 2310 { 2311 return 0; 2312 } 2313 2314 /* 2315 * Lookup and populate the kprobe_blacklist. 2316 * 2317 * Unlike the kretprobe blacklist, we'll need to determine 2318 * the range of addresses that belong to the said functions, 2319 * since a kprobe need not necessarily be at the beginning 2320 * of a function. 2321 */ 2322 static int __init populate_kprobe_blacklist(unsigned long *start, 2323 unsigned long *end) 2324 { 2325 unsigned long entry; 2326 unsigned long *iter; 2327 int ret; 2328 2329 for (iter = start; iter < end; iter++) { 2330 entry = arch_deref_entry_point((void *)*iter); 2331 ret = kprobe_add_ksym_blacklist(entry); 2332 if (ret == -EINVAL) 2333 continue; 2334 if (ret < 0) 2335 return ret; 2336 } 2337 2338 /* Symbols in __kprobes_text are blacklisted */ 2339 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start, 2340 (unsigned long)__kprobes_text_end); 2341 if (ret) 2342 return ret; 2343 2344 /* Symbols in noinstr section are blacklisted */ 2345 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start, 2346 (unsigned long)__noinstr_text_end); 2347 2348 return ret ? : arch_populate_kprobe_blacklist(); 2349 } 2350 2351 static void add_module_kprobe_blacklist(struct module *mod) 2352 { 2353 unsigned long start, end; 2354 int i; 2355 2356 if (mod->kprobe_blacklist) { 2357 for (i = 0; i < mod->num_kprobe_blacklist; i++) 2358 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]); 2359 } 2360 2361 start = (unsigned long)mod->kprobes_text_start; 2362 if (start) { 2363 end = start + mod->kprobes_text_size; 2364 kprobe_add_area_blacklist(start, end); 2365 } 2366 2367 start = (unsigned long)mod->noinstr_text_start; 2368 if (start) { 2369 end = start + mod->noinstr_text_size; 2370 kprobe_add_area_blacklist(start, end); 2371 } 2372 } 2373 2374 static void remove_module_kprobe_blacklist(struct module *mod) 2375 { 2376 unsigned long start, end; 2377 int i; 2378 2379 if (mod->kprobe_blacklist) { 2380 for (i = 0; i < mod->num_kprobe_blacklist; i++) 2381 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]); 2382 } 2383 2384 start = (unsigned long)mod->kprobes_text_start; 2385 if (start) { 2386 end = start + mod->kprobes_text_size; 2387 kprobe_remove_area_blacklist(start, end); 2388 } 2389 2390 start = (unsigned long)mod->noinstr_text_start; 2391 if (start) { 2392 end = start + mod->noinstr_text_size; 2393 kprobe_remove_area_blacklist(start, end); 2394 } 2395 } 2396 2397 /* Module notifier call back, checking kprobes on the module */ 2398 static int kprobes_module_callback(struct notifier_block *nb, 2399 unsigned long val, void *data) 2400 { 2401 struct module *mod = data; 2402 struct hlist_head *head; 2403 struct kprobe *p; 2404 unsigned int i; 2405 int checkcore = (val == MODULE_STATE_GOING); 2406 2407 if (val == MODULE_STATE_COMING) { 2408 mutex_lock(&kprobe_mutex); 2409 add_module_kprobe_blacklist(mod); 2410 mutex_unlock(&kprobe_mutex); 2411 } 2412 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE) 2413 return NOTIFY_DONE; 2414 2415 /* 2416 * When MODULE_STATE_GOING was notified, both of module .text and 2417 * .init.text sections would be freed. When MODULE_STATE_LIVE was 2418 * notified, only .init.text section would be freed. We need to 2419 * disable kprobes which have been inserted in the sections. 2420 */ 2421 mutex_lock(&kprobe_mutex); 2422 for (i = 0; i < KPROBE_TABLE_SIZE; i++) { 2423 head = &kprobe_table[i]; 2424 hlist_for_each_entry(p, head, hlist) 2425 if (within_module_init((unsigned long)p->addr, mod) || 2426 (checkcore && 2427 within_module_core((unsigned long)p->addr, mod))) { 2428 /* 2429 * The vaddr this probe is installed will soon 2430 * be vfreed buy not synced to disk. Hence, 2431 * disarming the breakpoint isn't needed. 2432 * 2433 * Note, this will also move any optimized probes 2434 * that are pending to be removed from their 2435 * corresponding lists to the freeing_list and 2436 * will not be touched by the delayed 2437 * kprobe_optimizer work handler. 2438 */ 2439 kill_kprobe(p); 2440 } 2441 } 2442 if (val == MODULE_STATE_GOING) 2443 remove_module_kprobe_blacklist(mod); 2444 mutex_unlock(&kprobe_mutex); 2445 return NOTIFY_DONE; 2446 } 2447 2448 static struct notifier_block kprobe_module_nb = { 2449 .notifier_call = kprobes_module_callback, 2450 .priority = 0 2451 }; 2452 2453 /* Markers of _kprobe_blacklist section */ 2454 extern unsigned long __start_kprobe_blacklist[]; 2455 extern unsigned long __stop_kprobe_blacklist[]; 2456 2457 static int __init init_kprobes(void) 2458 { 2459 int i, err = 0; 2460 2461 /* FIXME allocate the probe table, currently defined statically */ 2462 /* initialize all list heads */ 2463 for (i = 0; i < KPROBE_TABLE_SIZE; i++) { 2464 INIT_HLIST_HEAD(&kprobe_table[i]); 2465 INIT_HLIST_HEAD(&kretprobe_inst_table[i]); 2466 raw_spin_lock_init(&(kretprobe_table_locks[i].lock)); 2467 } 2468 2469 err = populate_kprobe_blacklist(__start_kprobe_blacklist, 2470 __stop_kprobe_blacklist); 2471 if (err) { 2472 pr_err("kprobes: failed to populate blacklist: %d\n", err); 2473 pr_err("Please take care of using kprobes.\n"); 2474 } 2475 2476 if (kretprobe_blacklist_size) { 2477 /* lookup the function address from its name */ 2478 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) { 2479 kretprobe_blacklist[i].addr = 2480 kprobe_lookup_name(kretprobe_blacklist[i].name, 0); 2481 if (!kretprobe_blacklist[i].addr) 2482 printk("kretprobe: lookup failed: %s\n", 2483 kretprobe_blacklist[i].name); 2484 } 2485 } 2486 2487 #if defined(CONFIG_OPTPROBES) 2488 #if defined(__ARCH_WANT_KPROBES_INSN_SLOT) 2489 /* Init kprobe_optinsn_slots */ 2490 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE; 2491 #endif 2492 /* By default, kprobes can be optimized */ 2493 kprobes_allow_optimization = true; 2494 #endif 2495 2496 /* By default, kprobes are armed */ 2497 kprobes_all_disarmed = false; 2498 2499 err = arch_init_kprobes(); 2500 if (!err) 2501 err = register_die_notifier(&kprobe_exceptions_nb); 2502 if (!err) 2503 err = register_module_notifier(&kprobe_module_nb); 2504 2505 kprobes_initialized = (err == 0); 2506 2507 if (!err) 2508 init_test_probes(); 2509 return err; 2510 } 2511 subsys_initcall(init_kprobes); 2512 2513 #ifdef CONFIG_DEBUG_FS 2514 static void report_probe(struct seq_file *pi, struct kprobe *p, 2515 const char *sym, int offset, char *modname, struct kprobe *pp) 2516 { 2517 char *kprobe_type; 2518 void *addr = p->addr; 2519 2520 if (p->pre_handler == pre_handler_kretprobe) 2521 kprobe_type = "r"; 2522 else 2523 kprobe_type = "k"; 2524 2525 if (!kallsyms_show_value(pi->file->f_cred)) 2526 addr = NULL; 2527 2528 if (sym) 2529 seq_printf(pi, "%px %s %s+0x%x %s ", 2530 addr, kprobe_type, sym, offset, 2531 (modname ? modname : " ")); 2532 else /* try to use %pS */ 2533 seq_printf(pi, "%px %s %pS ", 2534 addr, kprobe_type, p->addr); 2535 2536 if (!pp) 2537 pp = p; 2538 seq_printf(pi, "%s%s%s%s\n", 2539 (kprobe_gone(p) ? "[GONE]" : ""), 2540 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""), 2541 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""), 2542 (kprobe_ftrace(pp) ? "[FTRACE]" : "")); 2543 } 2544 2545 static void *kprobe_seq_start(struct seq_file *f, loff_t *pos) 2546 { 2547 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL; 2548 } 2549 2550 static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos) 2551 { 2552 (*pos)++; 2553 if (*pos >= KPROBE_TABLE_SIZE) 2554 return NULL; 2555 return pos; 2556 } 2557 2558 static void kprobe_seq_stop(struct seq_file *f, void *v) 2559 { 2560 /* Nothing to do */ 2561 } 2562 2563 static int show_kprobe_addr(struct seq_file *pi, void *v) 2564 { 2565 struct hlist_head *head; 2566 struct kprobe *p, *kp; 2567 const char *sym = NULL; 2568 unsigned int i = *(loff_t *) v; 2569 unsigned long offset = 0; 2570 char *modname, namebuf[KSYM_NAME_LEN]; 2571 2572 head = &kprobe_table[i]; 2573 preempt_disable(); 2574 hlist_for_each_entry_rcu(p, head, hlist) { 2575 sym = kallsyms_lookup((unsigned long)p->addr, NULL, 2576 &offset, &modname, namebuf); 2577 if (kprobe_aggrprobe(p)) { 2578 list_for_each_entry_rcu(kp, &p->list, list) 2579 report_probe(pi, kp, sym, offset, modname, p); 2580 } else 2581 report_probe(pi, p, sym, offset, modname, NULL); 2582 } 2583 preempt_enable(); 2584 return 0; 2585 } 2586 2587 static const struct seq_operations kprobes_sops = { 2588 .start = kprobe_seq_start, 2589 .next = kprobe_seq_next, 2590 .stop = kprobe_seq_stop, 2591 .show = show_kprobe_addr 2592 }; 2593 2594 DEFINE_SEQ_ATTRIBUTE(kprobes); 2595 2596 /* kprobes/blacklist -- shows which functions can not be probed */ 2597 static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos) 2598 { 2599 mutex_lock(&kprobe_mutex); 2600 return seq_list_start(&kprobe_blacklist, *pos); 2601 } 2602 2603 static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos) 2604 { 2605 return seq_list_next(v, &kprobe_blacklist, pos); 2606 } 2607 2608 static int kprobe_blacklist_seq_show(struct seq_file *m, void *v) 2609 { 2610 struct kprobe_blacklist_entry *ent = 2611 list_entry(v, struct kprobe_blacklist_entry, list); 2612 2613 /* 2614 * If /proc/kallsyms is not showing kernel address, we won't 2615 * show them here either. 2616 */ 2617 if (!kallsyms_show_value(m->file->f_cred)) 2618 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL, 2619 (void *)ent->start_addr); 2620 else 2621 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr, 2622 (void *)ent->end_addr, (void *)ent->start_addr); 2623 return 0; 2624 } 2625 2626 static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v) 2627 { 2628 mutex_unlock(&kprobe_mutex); 2629 } 2630 2631 static const struct seq_operations kprobe_blacklist_sops = { 2632 .start = kprobe_blacklist_seq_start, 2633 .next = kprobe_blacklist_seq_next, 2634 .stop = kprobe_blacklist_seq_stop, 2635 .show = kprobe_blacklist_seq_show, 2636 }; 2637 DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist); 2638 2639 static int arm_all_kprobes(void) 2640 { 2641 struct hlist_head *head; 2642 struct kprobe *p; 2643 unsigned int i, total = 0, errors = 0; 2644 int err, ret = 0; 2645 2646 mutex_lock(&kprobe_mutex); 2647 2648 /* If kprobes are armed, just return */ 2649 if (!kprobes_all_disarmed) 2650 goto already_enabled; 2651 2652 /* 2653 * optimize_kprobe() called by arm_kprobe() checks 2654 * kprobes_all_disarmed, so set kprobes_all_disarmed before 2655 * arm_kprobe. 2656 */ 2657 kprobes_all_disarmed = false; 2658 /* Arming kprobes doesn't optimize kprobe itself */ 2659 for (i = 0; i < KPROBE_TABLE_SIZE; i++) { 2660 head = &kprobe_table[i]; 2661 /* Arm all kprobes on a best-effort basis */ 2662 hlist_for_each_entry(p, head, hlist) { 2663 if (!kprobe_disabled(p)) { 2664 err = arm_kprobe(p); 2665 if (err) { 2666 errors++; 2667 ret = err; 2668 } 2669 total++; 2670 } 2671 } 2672 } 2673 2674 if (errors) 2675 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n", 2676 errors, total); 2677 else 2678 pr_info("Kprobes globally enabled\n"); 2679 2680 already_enabled: 2681 mutex_unlock(&kprobe_mutex); 2682 return ret; 2683 } 2684 2685 static int disarm_all_kprobes(void) 2686 { 2687 struct hlist_head *head; 2688 struct kprobe *p; 2689 unsigned int i, total = 0, errors = 0; 2690 int err, ret = 0; 2691 2692 mutex_lock(&kprobe_mutex); 2693 2694 /* If kprobes are already disarmed, just return */ 2695 if (kprobes_all_disarmed) { 2696 mutex_unlock(&kprobe_mutex); 2697 return 0; 2698 } 2699 2700 kprobes_all_disarmed = true; 2701 2702 for (i = 0; i < KPROBE_TABLE_SIZE; i++) { 2703 head = &kprobe_table[i]; 2704 /* Disarm all kprobes on a best-effort basis */ 2705 hlist_for_each_entry(p, head, hlist) { 2706 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) { 2707 err = disarm_kprobe(p, false); 2708 if (err) { 2709 errors++; 2710 ret = err; 2711 } 2712 total++; 2713 } 2714 } 2715 } 2716 2717 if (errors) 2718 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n", 2719 errors, total); 2720 else 2721 pr_info("Kprobes globally disabled\n"); 2722 2723 mutex_unlock(&kprobe_mutex); 2724 2725 /* Wait for disarming all kprobes by optimizer */ 2726 wait_for_kprobe_optimizer(); 2727 2728 return ret; 2729 } 2730 2731 /* 2732 * XXX: The debugfs bool file interface doesn't allow for callbacks 2733 * when the bool state is switched. We can reuse that facility when 2734 * available 2735 */ 2736 static ssize_t read_enabled_file_bool(struct file *file, 2737 char __user *user_buf, size_t count, loff_t *ppos) 2738 { 2739 char buf[3]; 2740 2741 if (!kprobes_all_disarmed) 2742 buf[0] = '1'; 2743 else 2744 buf[0] = '0'; 2745 buf[1] = '\n'; 2746 buf[2] = 0x00; 2747 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 2748 } 2749 2750 static ssize_t write_enabled_file_bool(struct file *file, 2751 const char __user *user_buf, size_t count, loff_t *ppos) 2752 { 2753 char buf[32]; 2754 size_t buf_size; 2755 int ret = 0; 2756 2757 buf_size = min(count, (sizeof(buf)-1)); 2758 if (copy_from_user(buf, user_buf, buf_size)) 2759 return -EFAULT; 2760 2761 buf[buf_size] = '\0'; 2762 switch (buf[0]) { 2763 case 'y': 2764 case 'Y': 2765 case '1': 2766 ret = arm_all_kprobes(); 2767 break; 2768 case 'n': 2769 case 'N': 2770 case '0': 2771 ret = disarm_all_kprobes(); 2772 break; 2773 default: 2774 return -EINVAL; 2775 } 2776 2777 if (ret) 2778 return ret; 2779 2780 return count; 2781 } 2782 2783 static const struct file_operations fops_kp = { 2784 .read = read_enabled_file_bool, 2785 .write = write_enabled_file_bool, 2786 .llseek = default_llseek, 2787 }; 2788 2789 static int __init debugfs_kprobe_init(void) 2790 { 2791 struct dentry *dir; 2792 unsigned int value = 1; 2793 2794 dir = debugfs_create_dir("kprobes", NULL); 2795 2796 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops); 2797 2798 debugfs_create_file("enabled", 0600, dir, &value, &fops_kp); 2799 2800 debugfs_create_file("blacklist", 0400, dir, NULL, 2801 &kprobe_blacklist_fops); 2802 2803 return 0; 2804 } 2805 2806 late_initcall(debugfs_kprobe_init); 2807 #endif /* CONFIG_DEBUG_FS */ 2808