diff options
Diffstat (limited to 'kernel/kprobes.c')
| -rw-r--r-- | kernel/kprobes.c | 648 |
1 files changed, 558 insertions, 90 deletions
diff --git a/kernel/kprobes.c b/kernel/kprobes.c index ccec774c716d..0ed46f3e51e9 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c | |||
| @@ -42,9 +42,11 @@ | |||
| 42 | #include <linux/freezer.h> | 42 | #include <linux/freezer.h> |
| 43 | #include <linux/seq_file.h> | 43 | #include <linux/seq_file.h> |
| 44 | #include <linux/debugfs.h> | 44 | #include <linux/debugfs.h> |
| 45 | #include <linux/sysctl.h> | ||
| 45 | #include <linux/kdebug.h> | 46 | #include <linux/kdebug.h> |
| 46 | #include <linux/memory.h> | 47 | #include <linux/memory.h> |
| 47 | #include <linux/ftrace.h> | 48 | #include <linux/ftrace.h> |
| 49 | #include <linux/cpu.h> | ||
| 48 | 50 | ||
| 49 | #include <asm-generic/sections.h> | 51 | #include <asm-generic/sections.h> |
| 50 | #include <asm/cacheflush.h> | 52 | #include <asm/cacheflush.h> |
| @@ -105,57 +107,74 @@ static struct kprobe_blackpoint kprobe_blacklist[] = { | |||
| 105 | * stepping on the instruction on a vmalloced/kmalloced/data page | 107 | * stepping on the instruction on a vmalloced/kmalloced/data page |
| 106 | * is a recipe for disaster | 108 | * is a recipe for disaster |
| 107 | */ | 109 | */ |
| 108 | #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t))) | ||
| 109 | |||
| 110 | struct kprobe_insn_page { | 110 | struct kprobe_insn_page { |
| 111 | struct list_head list; | 111 | struct list_head list; |
| 112 | kprobe_opcode_t *insns; /* Page of instruction slots */ | 112 | kprobe_opcode_t *insns; /* Page of instruction slots */ |
| 113 | char slot_used[INSNS_PER_PAGE]; | ||
| 114 | int nused; | 113 | int nused; |
| 115 | int ngarbage; | 114 | int ngarbage; |
| 115 | char slot_used[]; | ||
| 116 | }; | ||
| 117 | |||
| 118 | #define KPROBE_INSN_PAGE_SIZE(slots) \ | ||
| 119 | (offsetof(struct kprobe_insn_page, slot_used) + \ | ||
| 120 | (sizeof(char) * (slots))) | ||
| 121 | |||
| 122 | struct kprobe_insn_cache { | ||
| 123 | struct list_head pages; /* list of kprobe_insn_page */ | ||
| 124 | size_t insn_size; /* size of instruction slot */ | ||
| 125 | int nr_garbage; | ||
| 116 | }; | 126 | }; |
| 117 | 127 | ||
| 128 | static int slots_per_page(struct kprobe_insn_cache *c) | ||
| 129 | { | ||
| 130 | return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t)); | ||
| 131 | } | ||
| 132 | |||
| 118 | enum kprobe_slot_state { | 133 | enum kprobe_slot_state { |
| 119 | SLOT_CLEAN = 0, | 134 | SLOT_CLEAN = 0, |
| 120 | SLOT_DIRTY = 1, | 135 | SLOT_DIRTY = 1, |
| 121 | SLOT_USED = 2, | 136 | SLOT_USED = 2, |
| 122 | }; | 137 | }; |
| 123 | 138 | ||
| 124 | static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_pages */ | 139 | static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_slots */ |
| 125 | static LIST_HEAD(kprobe_insn_pages); | 140 | static struct kprobe_insn_cache kprobe_insn_slots = { |
| 126 | static int kprobe_garbage_slots; | 141 | .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages), |
| 127 | static int collect_garbage_slots(void); | 142 | .insn_size = MAX_INSN_SIZE, |
| 143 | .nr_garbage = 0, | ||
| 144 | }; | ||
| 145 | static int __kprobes collect_garbage_slots(struct kprobe_insn_cache *c); | ||
| 128 | 146 | ||
| 129 | /** | 147 | /** |
| 130 | * __get_insn_slot() - Find a slot on an executable page for an instruction. | 148 | * __get_insn_slot() - Find a slot on an executable page for an instruction. |
| 131 | * We allocate an executable page if there's no room on existing ones. | 149 | * We allocate an executable page if there's no room on existing ones. |
| 132 | */ | 150 | */ |
| 133 | static kprobe_opcode_t __kprobes *__get_insn_slot(void) | 151 | static kprobe_opcode_t __kprobes *__get_insn_slot(struct kprobe_insn_cache *c) |
| 134 | { | 152 | { |
| 135 | struct kprobe_insn_page *kip; | 153 | struct kprobe_insn_page *kip; |
| 136 | 154 | ||
| 137 | retry: | 155 | retry: |
| 138 | list_for_each_entry(kip, &kprobe_insn_pages, list) { | 156 | list_for_each_entry(kip, &c->pages, list) { |
| 139 | if (kip->nused < INSNS_PER_PAGE) { | 157 | if (kip->nused < slots_per_page(c)) { |
| 140 | int i; | 158 | int i; |
| 141 | for (i = 0; i < INSNS_PER_PAGE; i++) { | 159 | for (i = 0; i < slots_per_page(c); i++) { |
| 142 | if (kip->slot_used[i] == SLOT_CLEAN) { | 160 | if (kip->slot_used[i] == SLOT_CLEAN) { |
| 143 | kip->slot_used[i] = SLOT_USED; | 161 | kip->slot_used[i] = SLOT_USED; |
| 144 | kip->nused++; | 162 | kip->nused++; |
| 145 | return kip->insns + (i * MAX_INSN_SIZE); | 163 | return kip->insns + (i * c->insn_size); |
| 146 | } | 164 | } |
| 147 | } | 165 | } |
| 148 | /* Surprise! No unused slots. Fix kip->nused. */ | 166 | /* kip->nused is broken. Fix it. */ |
| 149 | kip->nused = INSNS_PER_PAGE; | 167 | kip->nused = slots_per_page(c); |
| 168 | WARN_ON(1); | ||
| 150 | } | 169 | } |
| 151 | } | 170 | } |
| 152 | 171 | ||
| 153 | /* If there are any garbage slots, collect it and try again. */ | 172 | /* If there are any garbage slots, collect it and try again. */ |
| 154 | if (kprobe_garbage_slots && collect_garbage_slots() == 0) { | 173 | if (c->nr_garbage && collect_garbage_slots(c) == 0) |
| 155 | goto retry; | 174 | goto retry; |
| 156 | } | 175 | |
| 157 | /* All out of space. Need to allocate a new page. Use slot 0. */ | 176 | /* All out of space. Need to allocate a new page. */ |
| 158 | kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL); | 177 | kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL); |
| 159 | if (!kip) | 178 | if (!kip) |
| 160 | return NULL; | 179 | return NULL; |
| 161 | 180 | ||
| @@ -170,20 +189,23 @@ static kprobe_opcode_t __kprobes *__get_insn_slot(void) | |||
| 170 | return NULL; | 189 | return NULL; |
| 171 | } | 190 | } |
| 172 | INIT_LIST_HEAD(&kip->list); | 191 | INIT_LIST_HEAD(&kip->list); |
| 173 | list_add(&kip->list, &kprobe_insn_pages); | 192 | memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c)); |
| 174 | memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE); | ||
| 175 | kip->slot_used[0] = SLOT_USED; | 193 | kip->slot_used[0] = SLOT_USED; |
| 176 | kip->nused = 1; | 194 | kip->nused = 1; |
| 177 | kip->ngarbage = 0; | 195 | kip->ngarbage = 0; |
| 196 | list_add(&kip->list, &c->pages); | ||
| 178 | return kip->insns; | 197 | return kip->insns; |
| 179 | } | 198 | } |
| 180 | 199 | ||
| 200 | |||
| 181 | kprobe_opcode_t __kprobes *get_insn_slot(void) | 201 | kprobe_opcode_t __kprobes *get_insn_slot(void) |
| 182 | { | 202 | { |
| 183 | kprobe_opcode_t *ret; | 203 | kprobe_opcode_t *ret = NULL; |
| 204 | |||
| 184 | mutex_lock(&kprobe_insn_mutex); | 205 | mutex_lock(&kprobe_insn_mutex); |
| 185 | ret = __get_insn_slot(); | 206 | ret = __get_insn_slot(&kprobe_insn_slots); |
| 186 | mutex_unlock(&kprobe_insn_mutex); | 207 | mutex_unlock(&kprobe_insn_mutex); |
| 208 | |||
| 187 | return ret; | 209 | return ret; |
| 188 | } | 210 | } |
| 189 | 211 | ||
| @@ -199,7 +221,7 @@ static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx) | |||
| 199 | * so as not to have to set it up again the | 221 | * so as not to have to set it up again the |
| 200 | * next time somebody inserts a probe. | 222 | * next time somebody inserts a probe. |
| 201 | */ | 223 | */ |
| 202 | if (!list_is_singular(&kprobe_insn_pages)) { | 224 | if (!list_is_singular(&kip->list)) { |
| 203 | list_del(&kip->list); | 225 | list_del(&kip->list); |
| 204 | module_free(NULL, kip->insns); | 226 | module_free(NULL, kip->insns); |
| 205 | kfree(kip); | 227 | kfree(kip); |
| @@ -209,51 +231,85 @@ static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx) | |||
| 209 | return 0; | 231 | return 0; |
| 210 | } | 232 | } |
| 211 | 233 | ||
| 212 | static int __kprobes collect_garbage_slots(void) | 234 | static int __kprobes collect_garbage_slots(struct kprobe_insn_cache *c) |
| 213 | { | 235 | { |
| 214 | struct kprobe_insn_page *kip, *next; | 236 | struct kprobe_insn_page *kip, *next; |
| 215 | 237 | ||
| 216 | /* Ensure no-one is interrupted on the garbages */ | 238 | /* Ensure no-one is interrupted on the garbages */ |
| 217 | synchronize_sched(); | 239 | synchronize_sched(); |
| 218 | 240 | ||
| 219 | list_for_each_entry_safe(kip, next, &kprobe_insn_pages, list) { | 241 | list_for_each_entry_safe(kip, next, &c->pages, list) { |
| 220 | int i; | 242 | int i; |
| 221 | if (kip->ngarbage == 0) | 243 | if (kip->ngarbage == 0) |
| 222 | continue; | 244 | continue; |
| 223 | kip->ngarbage = 0; /* we will collect all garbages */ | 245 | kip->ngarbage = 0; /* we will collect all garbages */ |
| 224 | for (i = 0; i < INSNS_PER_PAGE; i++) { | 246 | for (i = 0; i < slots_per_page(c); i++) { |
| 225 | if (kip->slot_used[i] == SLOT_DIRTY && | 247 | if (kip->slot_used[i] == SLOT_DIRTY && |
| 226 | collect_one_slot(kip, i)) | 248 | collect_one_slot(kip, i)) |
| 227 | break; | 249 | break; |
| 228 | } | 250 | } |
| 229 | } | 251 | } |
| 230 | kprobe_garbage_slots = 0; | 252 | c->nr_garbage = 0; |
| 231 | return 0; | 253 | return 0; |
| 232 | } | 254 | } |
| 233 | 255 | ||
| 234 | void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty) | 256 | static void __kprobes __free_insn_slot(struct kprobe_insn_cache *c, |
| 257 | kprobe_opcode_t *slot, int dirty) | ||
| 235 | { | 258 | { |
| 236 | struct kprobe_insn_page *kip; | 259 | struct kprobe_insn_page *kip; |
| 237 | 260 | ||
| 238 | mutex_lock(&kprobe_insn_mutex); | 261 | list_for_each_entry(kip, &c->pages, list) { |
| 239 | list_for_each_entry(kip, &kprobe_insn_pages, list) { | 262 | long idx = ((long)slot - (long)kip->insns) / |
| 240 | if (kip->insns <= slot && | 263 | (c->insn_size * sizeof(kprobe_opcode_t)); |
| 241 | slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) { | 264 | if (idx >= 0 && idx < slots_per_page(c)) { |
| 242 | int i = (slot - kip->insns) / MAX_INSN_SIZE; | 265 | WARN_ON(kip->slot_used[idx] != SLOT_USED); |
| 243 | if (dirty) { | 266 | if (dirty) { |
| 244 | kip->slot_used[i] = SLOT_DIRTY; | 267 | kip->slot_used[idx] = SLOT_DIRTY; |
| 245 | kip->ngarbage++; | 268 | kip->ngarbage++; |
| 269 | if (++c->nr_garbage > slots_per_page(c)) | ||
| 270 | collect_garbage_slots(c); | ||
| 246 | } else | 271 | } else |
| 247 | collect_one_slot(kip, i); | 272 | collect_one_slot(kip, idx); |
| 248 | break; | 273 | return; |
| 249 | } | 274 | } |
| 250 | } | 275 | } |
| 276 | /* Could not free this slot. */ | ||
| 277 | WARN_ON(1); | ||
| 278 | } | ||
| 251 | 279 | ||
| 252 | if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE) | 280 | void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty) |
| 253 | collect_garbage_slots(); | 281 | { |
| 254 | 282 | mutex_lock(&kprobe_insn_mutex); | |
| 283 | __free_insn_slot(&kprobe_insn_slots, slot, dirty); | ||
| 255 | mutex_unlock(&kprobe_insn_mutex); | 284 | mutex_unlock(&kprobe_insn_mutex); |
| 256 | } | 285 | } |
| 286 | #ifdef CONFIG_OPTPROBES | ||
| 287 | /* For optimized_kprobe buffer */ | ||
| 288 | static DEFINE_MUTEX(kprobe_optinsn_mutex); /* Protects kprobe_optinsn_slots */ | ||
| 289 | static struct kprobe_insn_cache kprobe_optinsn_slots = { | ||
| 290 | .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages), | ||
| 291 | /* .insn_size is initialized later */ | ||
| 292 | .nr_garbage = 0, | ||
| 293 | }; | ||
| 294 | /* Get a slot for optimized_kprobe buffer */ | ||
| 295 | kprobe_opcode_t __kprobes *get_optinsn_slot(void) | ||
| 296 | { | ||
| 297 | kprobe_opcode_t *ret = NULL; | ||
| 298 | |||
| 299 | mutex_lock(&kprobe_optinsn_mutex); | ||
| 300 | ret = __get_insn_slot(&kprobe_optinsn_slots); | ||
| 301 | mutex_unlock(&kprobe_optinsn_mutex); | ||
| 302 | |||
| 303 | return ret; | ||
| 304 | } | ||
| 305 | |||
| 306 | void __kprobes free_optinsn_slot(kprobe_opcode_t * slot, int dirty) | ||
| 307 | { | ||
| 308 | mutex_lock(&kprobe_optinsn_mutex); | ||
| 309 | __free_insn_slot(&kprobe_optinsn_slots, slot, dirty); | ||
| 310 | mutex_unlock(&kprobe_optinsn_mutex); | ||
| 311 | } | ||
| 312 | #endif | ||
| 257 | #endif | 313 | #endif |
| 258 | 314 | ||
| 259 | /* We have preemption disabled.. so it is safe to use __ versions */ | 315 | /* We have preemption disabled.. so it is safe to use __ versions */ |
| @@ -284,23 +340,401 @@ struct kprobe __kprobes *get_kprobe(void *addr) | |||
| 284 | if (p->addr == addr) | 340 | if (p->addr == addr) |
| 285 | return p; | 341 | return p; |
| 286 | } | 342 | } |
| 343 | |||
| 344 | return NULL; | ||
| 345 | } | ||
| 346 | |||
| 347 | static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs); | ||
| 348 | |||
| 349 | /* Return true if the kprobe is an aggregator */ | ||
| 350 | static inline int kprobe_aggrprobe(struct kprobe *p) | ||
| 351 | { | ||
| 352 | return p->pre_handler == aggr_pre_handler; | ||
| 353 | } | ||
| 354 | |||
| 355 | /* | ||
| 356 | * Keep all fields in the kprobe consistent | ||
| 357 | */ | ||
| 358 | static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p) | ||
| 359 | { | ||
| 360 | memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t)); | ||
| 361 | memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn)); | ||
| 362 | } | ||
| 363 | |||
| 364 | #ifdef CONFIG_OPTPROBES | ||
| 365 | /* NOTE: change this value only with kprobe_mutex held */ | ||
| 366 | static bool kprobes_allow_optimization; | ||
| 367 | |||
| 368 | /* | ||
| 369 | * Call all pre_handler on the list, but ignores its return value. | ||
| 370 | * This must be called from arch-dep optimized caller. | ||
| 371 | */ | ||
| 372 | void __kprobes opt_pre_handler(struct kprobe *p, struct pt_regs *regs) | ||
| 373 | { | ||
| 374 | struct kprobe *kp; | ||
| 375 | |||
| 376 | list_for_each_entry_rcu(kp, &p->list, list) { | ||
| 377 | if (kp->pre_handler && likely(!kprobe_disabled(kp))) { | ||
| 378 | set_kprobe_instance(kp); | ||
| 379 | kp->pre_handler(kp, regs); | ||
| 380 | } | ||
| 381 | reset_kprobe_instance(); | ||
| 382 | } | ||
| 383 | } | ||
| 384 | |||
| 385 | /* Return true(!0) if the kprobe is ready for optimization. */ | ||
| 386 | static inline int kprobe_optready(struct kprobe *p) | ||
| 387 | { | ||
| 388 | struct optimized_kprobe *op; | ||
| 389 | |||
| 390 | if (kprobe_aggrprobe(p)) { | ||
| 391 | op = container_of(p, struct optimized_kprobe, kp); | ||
| 392 | return arch_prepared_optinsn(&op->optinsn); | ||
| 393 | } | ||
| 394 | |||
| 395 | return 0; | ||
| 396 | } | ||
| 397 | |||
| 398 | /* | ||
| 399 | * Return an optimized kprobe whose optimizing code replaces | ||
| 400 | * instructions including addr (exclude breakpoint). | ||
| 401 | */ | ||
| 402 | struct kprobe *__kprobes get_optimized_kprobe(unsigned long addr) | ||
| 403 | { | ||
| 404 | int i; | ||
| 405 | struct kprobe *p = NULL; | ||
| 406 | struct optimized_kprobe *op; | ||
| 407 | |||
| 408 | /* Don't check i == 0, since that is a breakpoint case. */ | ||
| 409 | for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++) | ||
| 410 | p = get_kprobe((void *)(addr - i)); | ||
| 411 | |||
| 412 | if (p && kprobe_optready(p)) { | ||
| 413 | op = container_of(p, struct optimized_kprobe, kp); | ||
| 414 | if (arch_within_optimized_kprobe(op, addr)) | ||
| 415 | return p; | ||
| 416 | } | ||
| 417 | |||
| 287 | return NULL; | 418 | return NULL; |
| 288 | } | 419 | } |
| 289 | 420 | ||
| 421 | /* Optimization staging list, protected by kprobe_mutex */ | ||
| 422 | static LIST_HEAD(optimizing_list); | ||
| 423 | |||
| 424 | static void kprobe_optimizer(struct work_struct *work); | ||
| 425 | static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer); | ||
| 426 | #define OPTIMIZE_DELAY 5 | ||
| 427 | |||
| 428 | /* Kprobe jump optimizer */ | ||
| 429 | static __kprobes void kprobe_optimizer(struct work_struct *work) | ||
| 430 | { | ||
| 431 | struct optimized_kprobe *op, *tmp; | ||
| 432 | |||
| 433 | /* Lock modules while optimizing kprobes */ | ||
| 434 | mutex_lock(&module_mutex); | ||
| 435 | mutex_lock(&kprobe_mutex); | ||
| 436 | if (kprobes_all_disarmed || !kprobes_allow_optimization) | ||
| 437 | goto end; | ||
| 438 | |||
| 439 | /* | ||
| 440 | * Wait for quiesence period to ensure all running interrupts | ||
| 441 | * are done. Because optprobe may modify multiple instructions | ||
| 442 | * there is a chance that Nth instruction is interrupted. In that | ||
| 443 | * case, running interrupt can return to 2nd-Nth byte of jump | ||
| 444 | * instruction. This wait is for avoiding it. | ||
| 445 | */ | ||
| 446 | synchronize_sched(); | ||
| 447 | |||
| 448 | /* | ||
| 449 | * The optimization/unoptimization refers online_cpus via | ||
| 450 | * stop_machine() and cpu-hotplug modifies online_cpus. | ||
| 451 | * And same time, text_mutex will be held in cpu-hotplug and here. | ||
| 452 | * This combination can cause a deadlock (cpu-hotplug try to lock | ||
| 453 | * text_mutex but stop_machine can not be done because online_cpus | ||
| 454 | * has been changed) | ||
| 455 | * To avoid this deadlock, we need to call get_online_cpus() | ||
| 456 | * for preventing cpu-hotplug outside of text_mutex locking. | ||
| 457 | */ | ||
| 458 | get_online_cpus(); | ||
| 459 | mutex_lock(&text_mutex); | ||
| 460 | list_for_each_entry_safe(op, tmp, &optimizing_list, list) { | ||
| 461 | WARN_ON(kprobe_disabled(&op->kp)); | ||
| 462 | if (arch_optimize_kprobe(op) < 0) | ||
| 463 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; | ||
| 464 | list_del_init(&op->list); | ||
| 465 | } | ||
| 466 | mutex_unlock(&text_mutex); | ||
| 467 | put_online_cpus(); | ||
| 468 | end: | ||
| 469 | mutex_unlock(&kprobe_mutex); | ||
| 470 | mutex_unlock(&module_mutex); | ||
| 471 | } | ||
| 472 | |||
| 473 | /* Optimize kprobe if p is ready to be optimized */ | ||
| 474 | static __kprobes void optimize_kprobe(struct kprobe *p) | ||
| 475 | { | ||
| 476 | struct optimized_kprobe *op; | ||
| 477 | |||
| 478 | /* Check if the kprobe is disabled or not ready for optimization. */ | ||
| 479 | if (!kprobe_optready(p) || !kprobes_allow_optimization || | ||
| 480 | (kprobe_disabled(p) || kprobes_all_disarmed)) | ||
| 481 | return; | ||
| 482 | |||
| 483 | /* Both of break_handler and post_handler are not supported. */ | ||
| 484 | if (p->break_handler || p->post_handler) | ||
| 485 | return; | ||
| 486 | |||
| 487 | op = container_of(p, struct optimized_kprobe, kp); | ||
| 488 | |||
| 489 | /* Check there is no other kprobes at the optimized instructions */ | ||
| 490 | if (arch_check_optimized_kprobe(op) < 0) | ||
| 491 | return; | ||
| 492 | |||
| 493 | /* Check if it is already optimized. */ | ||
| 494 | if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) | ||
| 495 | return; | ||
| 496 | |||
| 497 | op->kp.flags |= KPROBE_FLAG_OPTIMIZED; | ||
| 498 | list_add(&op->list, &optimizing_list); | ||
| 499 | if (!delayed_work_pending(&optimizing_work)) | ||
| 500 | schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY); | ||
| 501 | } | ||
| 502 | |||
| 503 | /* Unoptimize a kprobe if p is optimized */ | ||
| 504 | static __kprobes void unoptimize_kprobe(struct kprobe *p) | ||
| 505 | { | ||
| 506 | struct optimized_kprobe *op; | ||
| 507 | |||
| 508 | if ((p->flags & KPROBE_FLAG_OPTIMIZED) && kprobe_aggrprobe(p)) { | ||
| 509 | op = container_of(p, struct optimized_kprobe, kp); | ||
| 510 | if (!list_empty(&op->list)) | ||
| 511 | /* Dequeue from the optimization queue */ | ||
| 512 | list_del_init(&op->list); | ||
| 513 | else | ||
| 514 | /* Replace jump with break */ | ||
| 515 | arch_unoptimize_kprobe(op); | ||
| 516 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 | /* Remove optimized instructions */ | ||
| 521 | static void __kprobes kill_optimized_kprobe(struct kprobe *p) | ||
| 522 | { | ||
| 523 | struct optimized_kprobe *op; | ||
| 524 | |||
| 525 | op = container_of(p, struct optimized_kprobe, kp); | ||
| 526 | if (!list_empty(&op->list)) { | ||
| 527 | /* Dequeue from the optimization queue */ | ||
| 528 | list_del_init(&op->list); | ||
| 529 | op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED; | ||
| 530 | } | ||
| 531 | /* Don't unoptimize, because the target code will be freed. */ | ||
| 532 | arch_remove_optimized_kprobe(op); | ||
| 533 | } | ||
| 534 | |||
| 535 | /* Try to prepare optimized instructions */ | ||
| 536 | static __kprobes void prepare_optimized_kprobe(struct kprobe *p) | ||
| 537 | { | ||
| 538 | struct optimized_kprobe *op; | ||
| 539 | |||
| 540 | op = container_of(p, struct optimized_kprobe, kp); | ||
| 541 | arch_prepare_optimized_kprobe(op); | ||
| 542 | } | ||
| 543 | |||
| 544 | /* Free optimized instructions and optimized_kprobe */ | ||
| 545 | static __kprobes void free_aggr_kprobe(struct kprobe *p) | ||
| 546 | { | ||
| 547 | struct optimized_kprobe *op; | ||
| 548 | |||
| 549 | op = container_of(p, struct optimized_kprobe, kp); | ||
| 550 | arch_remove_optimized_kprobe(op); | ||
| 551 | kfree(op); | ||
| 552 | } | ||
| 553 | |||
| 554 | /* Allocate new optimized_kprobe and try to prepare optimized instructions */ | ||
| 555 | static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p) | ||
| 556 | { | ||
| 557 | struct optimized_kprobe *op; | ||
| 558 | |||
| 559 | op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL); | ||
| 560 | if (!op) | ||
| 561 | return NULL; | ||
| 562 | |||
| 563 | INIT_LIST_HEAD(&op->list); | ||
| 564 | op->kp.addr = p->addr; | ||
| 565 | arch_prepare_optimized_kprobe(op); | ||
| 566 | |||
| 567 | return &op->kp; | ||
| 568 | } | ||
| 569 | |||
| 570 | static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p); | ||
| 571 | |||
| 572 | /* | ||
| 573 | * Prepare an optimized_kprobe and optimize it | ||
| 574 | * NOTE: p must be a normal registered kprobe | ||
| 575 | */ | ||
| 576 | static __kprobes void try_to_optimize_kprobe(struct kprobe *p) | ||
| 577 | { | ||
| 578 | struct kprobe *ap; | ||
| 579 | struct optimized_kprobe *op; | ||
| 580 | |||
| 581 | ap = alloc_aggr_kprobe(p); | ||
| 582 | if (!ap) | ||
| 583 | return; | ||
| 584 | |||
| 585 | op = container_of(ap, struct optimized_kprobe, kp); | ||
| 586 | if (!arch_prepared_optinsn(&op->optinsn)) { | ||
| 587 | /* If failed to setup optimizing, fallback to kprobe */ | ||
| 588 | free_aggr_kprobe(ap); | ||
| 589 | return; | ||
| 590 | } | ||
| 591 | |||
| 592 | init_aggr_kprobe(ap, p); | ||
| 593 | optimize_kprobe(ap); | ||
| 594 | } | ||
| 595 | |||
| 596 | #ifdef CONFIG_SYSCTL | ||
| 597 | static void __kprobes optimize_all_kprobes(void) | ||
| 598 | { | ||
| 599 | struct hlist_head *head; | ||
| 600 | struct hlist_node *node; | ||
| 601 | struct kprobe *p; | ||
| 602 | unsigned int i; | ||
| 603 | |||
| 604 | /* If optimization is already allowed, just return */ | ||
| 605 | if (kprobes_allow_optimization) | ||
| 606 | return; | ||
| 607 | |||
| 608 | kprobes_allow_optimization = true; | ||
| 609 | mutex_lock(&text_mutex); | ||
| 610 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { | ||
| 611 | head = &kprobe_table[i]; | ||
| 612 | hlist_for_each_entry_rcu(p, node, head, hlist) | ||
| 613 | if (!kprobe_disabled(p)) | ||
| 614 | optimize_kprobe(p); | ||
| 615 | } | ||
| 616 | mutex_unlock(&text_mutex); | ||
| 617 | printk(KERN_INFO "Kprobes globally optimized\n"); | ||
| 618 | } | ||
| 619 | |||
| 620 | static void __kprobes unoptimize_all_kprobes(void) | ||
| 621 | { | ||
| 622 | struct hlist_head *head; | ||
| 623 | struct hlist_node *node; | ||
| 624 | struct kprobe *p; | ||
| 625 | unsigned int i; | ||
| 626 | |||
| 627 | /* If optimization is already prohibited, just return */ | ||
| 628 | if (!kprobes_allow_optimization) | ||
| 629 | return; | ||
| 630 | |||
| 631 | kprobes_allow_optimization = false; | ||
| 632 | printk(KERN_INFO "Kprobes globally unoptimized\n"); | ||
| 633 | get_online_cpus(); /* For avoiding text_mutex deadlock */ | ||
| 634 | mutex_lock(&text_mutex); | ||
| 635 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { | ||
| 636 | head = &kprobe_table[i]; | ||
| 637 | hlist_for_each_entry_rcu(p, node, head, hlist) { | ||
| 638 | if (!kprobe_disabled(p)) | ||
| 639 | unoptimize_kprobe(p); | ||
| 640 | } | ||
| 641 | } | ||
| 642 | |||
| 643 | mutex_unlock(&text_mutex); | ||
| 644 | put_online_cpus(); | ||
| 645 | /* Allow all currently running kprobes to complete */ | ||
| 646 | synchronize_sched(); | ||
| 647 | } | ||
| 648 | |||
| 649 | int sysctl_kprobes_optimization; | ||
| 650 | int proc_kprobes_optimization_handler(struct ctl_table *table, int write, | ||
| 651 | void __user *buffer, size_t *length, | ||
| 652 | loff_t *ppos) | ||
| 653 | { | ||
| 654 | int ret; | ||
| 655 | |||
| 656 | mutex_lock(&kprobe_mutex); | ||
| 657 | sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0; | ||
| 658 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); | ||
| 659 | |||
| 660 | if (sysctl_kprobes_optimization) | ||
| 661 | optimize_all_kprobes(); | ||
| 662 | else | ||
| 663 | unoptimize_all_kprobes(); | ||
| 664 | mutex_unlock(&kprobe_mutex); | ||
| 665 | |||
| 666 | return ret; | ||
| 667 | } | ||
| 668 | #endif /* CONFIG_SYSCTL */ | ||
| 669 | |||
| 670 | static void __kprobes __arm_kprobe(struct kprobe *p) | ||
| 671 | { | ||
| 672 | struct kprobe *old_p; | ||
| 673 | |||
| 674 | /* Check collision with other optimized kprobes */ | ||
| 675 | old_p = get_optimized_kprobe((unsigned long)p->addr); | ||
| 676 | if (unlikely(old_p)) | ||
| 677 | unoptimize_kprobe(old_p); /* Fallback to unoptimized kprobe */ | ||
| 678 | |||
| 679 | arch_arm_kprobe(p); | ||
| 680 | optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */ | ||
| 681 | } | ||
| 682 | |||
| 683 | static void __kprobes __disarm_kprobe(struct kprobe *p) | ||
| 684 | { | ||
| 685 | struct kprobe *old_p; | ||
| 686 | |||
| 687 | unoptimize_kprobe(p); /* Try to unoptimize */ | ||
| 688 | arch_disarm_kprobe(p); | ||
| 689 | |||
| 690 | /* If another kprobe was blocked, optimize it. */ | ||
| 691 | old_p = get_optimized_kprobe((unsigned long)p->addr); | ||
| 692 | if (unlikely(old_p)) | ||
| 693 | optimize_kprobe(old_p); | ||
| 694 | } | ||
| 695 | |||
| 696 | #else /* !CONFIG_OPTPROBES */ | ||
| 697 | |||
| 698 | #define optimize_kprobe(p) do {} while (0) | ||
| 699 | #define unoptimize_kprobe(p) do {} while (0) | ||
| 700 | #define kill_optimized_kprobe(p) do {} while (0) | ||
| 701 | #define prepare_optimized_kprobe(p) do {} while (0) | ||
| 702 | #define try_to_optimize_kprobe(p) do {} while (0) | ||
| 703 | #define __arm_kprobe(p) arch_arm_kprobe(p) | ||
| 704 | #define __disarm_kprobe(p) arch_disarm_kprobe(p) | ||
| 705 | |||
| 706 | static __kprobes void free_aggr_kprobe(struct kprobe *p) | ||
| 707 | { | ||
| 708 | kfree(p); | ||
| 709 | } | ||
| 710 | |||
| 711 | static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p) | ||
| 712 | { | ||
| 713 | return kzalloc(sizeof(struct kprobe), GFP_KERNEL); | ||
| 714 | } | ||
| 715 | #endif /* CONFIG_OPTPROBES */ | ||
| 716 | |||
| 290 | /* Arm a kprobe with text_mutex */ | 717 | /* Arm a kprobe with text_mutex */ |
| 291 | static void __kprobes arm_kprobe(struct kprobe *kp) | 718 | static void __kprobes arm_kprobe(struct kprobe *kp) |
| 292 | { | 719 | { |
| 720 | /* | ||
| 721 | * Here, since __arm_kprobe() doesn't use stop_machine(), | ||
| 722 | * this doesn't cause deadlock on text_mutex. So, we don't | ||
| 723 | * need get_online_cpus(). | ||
| 724 | */ | ||
| 293 | mutex_lock(&text_mutex); | 725 | mutex_lock(&text_mutex); |
| 294 | arch_arm_kprobe(kp); | 726 | __arm_kprobe(kp); |
| 295 | mutex_unlock(&text_mutex); | 727 | mutex_unlock(&text_mutex); |
| 296 | } | 728 | } |
| 297 | 729 | ||
| 298 | /* Disarm a kprobe with text_mutex */ | 730 | /* Disarm a kprobe with text_mutex */ |
| 299 | static void __kprobes disarm_kprobe(struct kprobe *kp) | 731 | static void __kprobes disarm_kprobe(struct kprobe *kp) |
| 300 | { | 732 | { |
| 733 | get_online_cpus(); /* For avoiding text_mutex deadlock */ | ||
| 301 | mutex_lock(&text_mutex); | 734 | mutex_lock(&text_mutex); |
| 302 | arch_disarm_kprobe(kp); | 735 | __disarm_kprobe(kp); |
| 303 | mutex_unlock(&text_mutex); | 736 | mutex_unlock(&text_mutex); |
| 737 | put_online_cpus(); | ||
| 304 | } | 738 | } |
| 305 | 739 | ||
| 306 | /* | 740 | /* |
| @@ -369,7 +803,7 @@ static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs) | |||
| 369 | void __kprobes kprobes_inc_nmissed_count(struct kprobe *p) | 803 | void __kprobes kprobes_inc_nmissed_count(struct kprobe *p) |
| 370 | { | 804 | { |
| 371 | struct kprobe *kp; | 805 | struct kprobe *kp; |
| 372 | if (p->pre_handler != aggr_pre_handler) { | 806 | if (!kprobe_aggrprobe(p)) { |
| 373 | p->nmissed++; | 807 | p->nmissed++; |
| 374 | } else { | 808 | } else { |
| 375 | list_for_each_entry_rcu(kp, &p->list, list) | 809 | list_for_each_entry_rcu(kp, &p->list, list) |
| @@ -493,21 +927,16 @@ static void __kprobes cleanup_rp_inst(struct kretprobe *rp) | |||
| 493 | } | 927 | } |
| 494 | 928 | ||
| 495 | /* | 929 | /* |
| 496 | * Keep all fields in the kprobe consistent | ||
| 497 | */ | ||
| 498 | static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p) | ||
| 499 | { | ||
| 500 | memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t)); | ||
| 501 | memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn)); | ||
| 502 | } | ||
| 503 | |||
| 504 | /* | ||
| 505 | * Add the new probe to ap->list. Fail if this is the | 930 | * Add the new probe to ap->list. Fail if this is the |
| 506 | * second jprobe at the address - two jprobes can't coexist | 931 | * second jprobe at the address - two jprobes can't coexist |
| 507 | */ | 932 | */ |
| 508 | static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p) | 933 | static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p) |
| 509 | { | 934 | { |
| 510 | BUG_ON(kprobe_gone(ap) || kprobe_gone(p)); | 935 | BUG_ON(kprobe_gone(ap) || kprobe_gone(p)); |
| 936 | |||
| 937 | if (p->break_handler || p->post_handler) | ||
| 938 | unoptimize_kprobe(ap); /* Fall back to normal kprobe */ | ||
| 939 | |||
| 511 | if (p->break_handler) { | 940 | if (p->break_handler) { |
| 512 | if (ap->break_handler) | 941 | if (ap->break_handler) |
| 513 | return -EEXIST; | 942 | return -EEXIST; |
| @@ -522,7 +951,7 @@ static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p) | |||
| 522 | ap->flags &= ~KPROBE_FLAG_DISABLED; | 951 | ap->flags &= ~KPROBE_FLAG_DISABLED; |
| 523 | if (!kprobes_all_disarmed) | 952 | if (!kprobes_all_disarmed) |
| 524 | /* Arm the breakpoint again. */ | 953 | /* Arm the breakpoint again. */ |
| 525 | arm_kprobe(ap); | 954 | __arm_kprobe(ap); |
| 526 | } | 955 | } |
| 527 | return 0; | 956 | return 0; |
| 528 | } | 957 | } |
| @@ -531,12 +960,13 @@ static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p) | |||
| 531 | * Fill in the required fields of the "manager kprobe". Replace the | 960 | * Fill in the required fields of the "manager kprobe". Replace the |
| 532 | * earlier kprobe in the hlist with the manager kprobe | 961 | * earlier kprobe in the hlist with the manager kprobe |
| 533 | */ | 962 | */ |
| 534 | static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p) | 963 | static void __kprobes init_aggr_kprobe(struct kprobe *ap, struct kprobe *p) |
| 535 | { | 964 | { |
| 965 | /* Copy p's insn slot to ap */ | ||
| 536 | copy_kprobe(p, ap); | 966 | copy_kprobe(p, ap); |
| 537 | flush_insn_slot(ap); | 967 | flush_insn_slot(ap); |
| 538 | ap->addr = p->addr; | 968 | ap->addr = p->addr; |
| 539 | ap->flags = p->flags; | 969 | ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED; |
| 540 | ap->pre_handler = aggr_pre_handler; | 970 | ap->pre_handler = aggr_pre_handler; |
| 541 | ap->fault_handler = aggr_fault_handler; | 971 | ap->fault_handler = aggr_fault_handler; |
| 542 | /* We don't care the kprobe which has gone. */ | 972 | /* We don't care the kprobe which has gone. */ |
| @@ -546,8 +976,9 @@ static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p) | |||
| 546 | ap->break_handler = aggr_break_handler; | 976 | ap->break_handler = aggr_break_handler; |
| 547 | 977 | ||
| 548 | INIT_LIST_HEAD(&ap->list); | 978 | INIT_LIST_HEAD(&ap->list); |
| 549 | list_add_rcu(&p->list, &ap->list); | 979 | INIT_HLIST_NODE(&ap->hlist); |
| 550 | 980 | ||
| 981 | list_add_rcu(&p->list, &ap->list); | ||
| 551 | hlist_replace_rcu(&p->hlist, &ap->hlist); | 982 | hlist_replace_rcu(&p->hlist, &ap->hlist); |
| 552 | } | 983 | } |
| 553 | 984 | ||
| @@ -561,12 +992,12 @@ static int __kprobes register_aggr_kprobe(struct kprobe *old_p, | |||
| 561 | int ret = 0; | 992 | int ret = 0; |
| 562 | struct kprobe *ap = old_p; | 993 | struct kprobe *ap = old_p; |
| 563 | 994 | ||
| 564 | if (old_p->pre_handler != aggr_pre_handler) { | 995 | if (!kprobe_aggrprobe(old_p)) { |
| 565 | /* If old_p is not an aggr_probe, create new aggr_kprobe. */ | 996 | /* If old_p is not an aggr_kprobe, create new aggr_kprobe. */ |
| 566 | ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL); | 997 | ap = alloc_aggr_kprobe(old_p); |
| 567 | if (!ap) | 998 | if (!ap) |
| 568 | return -ENOMEM; | 999 | return -ENOMEM; |
| 569 | add_aggr_kprobe(ap, old_p); | 1000 | init_aggr_kprobe(ap, old_p); |
| 570 | } | 1001 | } |
| 571 | 1002 | ||
| 572 | if (kprobe_gone(ap)) { | 1003 | if (kprobe_gone(ap)) { |
| @@ -585,6 +1016,9 @@ static int __kprobes register_aggr_kprobe(struct kprobe *old_p, | |||
| 585 | */ | 1016 | */ |
| 586 | return ret; | 1017 | return ret; |
| 587 | 1018 | ||
| 1019 | /* Prepare optimized instructions if possible. */ | ||
| 1020 | prepare_optimized_kprobe(ap); | ||
| 1021 | |||
| 588 | /* | 1022 | /* |
| 589 | * Clear gone flag to prevent allocating new slot again, and | 1023 | * Clear gone flag to prevent allocating new slot again, and |
| 590 | * set disabled flag because it is not armed yet. | 1024 | * set disabled flag because it is not armed yet. |
| @@ -593,6 +1027,7 @@ static int __kprobes register_aggr_kprobe(struct kprobe *old_p, | |||
| 593 | | KPROBE_FLAG_DISABLED; | 1027 | | KPROBE_FLAG_DISABLED; |
| 594 | } | 1028 | } |
| 595 | 1029 | ||
| 1030 | /* Copy ap's insn slot to p */ | ||
| 596 | copy_kprobe(ap, p); | 1031 | copy_kprobe(ap, p); |
| 597 | return add_new_kprobe(ap, p); | 1032 | return add_new_kprobe(ap, p); |
| 598 | } | 1033 | } |
| @@ -743,27 +1178,34 @@ int __kprobes register_kprobe(struct kprobe *p) | |||
| 743 | p->nmissed = 0; | 1178 | p->nmissed = 0; |
| 744 | INIT_LIST_HEAD(&p->list); | 1179 | INIT_LIST_HEAD(&p->list); |
| 745 | mutex_lock(&kprobe_mutex); | 1180 | mutex_lock(&kprobe_mutex); |
| 1181 | |||
| 1182 | get_online_cpus(); /* For avoiding text_mutex deadlock. */ | ||
| 1183 | mutex_lock(&text_mutex); | ||
| 1184 | |||
| 746 | old_p = get_kprobe(p->addr); | 1185 | old_p = get_kprobe(p->addr); |
| 747 | if (old_p) { | 1186 | if (old_p) { |
| 1187 | /* Since this may unoptimize old_p, locking text_mutex. */ | ||
| 748 | ret = register_aggr_kprobe(old_p, p); | 1188 | ret = register_aggr_kprobe(old_p, p); |
| 749 | goto out; | 1189 | goto out; |
| 750 | } | 1190 | } |
| 751 | 1191 | ||
| 752 | mutex_lock(&text_mutex); | ||
| 753 | ret = arch_prepare_kprobe(p); | 1192 | ret = arch_prepare_kprobe(p); |
| 754 | if (ret) | 1193 | if (ret) |
| 755 | goto out_unlock_text; | 1194 | goto out; |
| 756 | 1195 | ||
| 757 | INIT_HLIST_NODE(&p->hlist); | 1196 | INIT_HLIST_NODE(&p->hlist); |
| 758 | hlist_add_head_rcu(&p->hlist, | 1197 | hlist_add_head_rcu(&p->hlist, |
| 759 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); | 1198 | &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]); |
| 760 | 1199 | ||
| 761 | if (!kprobes_all_disarmed && !kprobe_disabled(p)) | 1200 | if (!kprobes_all_disarmed && !kprobe_disabled(p)) |
| 762 | arch_arm_kprobe(p); | 1201 | __arm_kprobe(p); |
| 1202 | |||
| 1203 | /* Try to optimize kprobe */ | ||
| 1204 | try_to_optimize_kprobe(p); | ||
| 763 | 1205 | ||
| 764 | out_unlock_text: | ||
| 765 | mutex_unlock(&text_mutex); | ||
| 766 | out: | 1206 | out: |
| 1207 | mutex_unlock(&text_mutex); | ||
| 1208 | put_online_cpus(); | ||
| 767 | mutex_unlock(&kprobe_mutex); | 1209 | mutex_unlock(&kprobe_mutex); |
| 768 | 1210 | ||
| 769 | if (probed_mod) | 1211 | if (probed_mod) |
| @@ -785,7 +1227,7 @@ static int __kprobes __unregister_kprobe_top(struct kprobe *p) | |||
| 785 | return -EINVAL; | 1227 | return -EINVAL; |
| 786 | 1228 | ||
| 787 | if (old_p == p || | 1229 | if (old_p == p || |
| 788 | (old_p->pre_handler == aggr_pre_handler && | 1230 | (kprobe_aggrprobe(old_p) && |
| 789 | list_is_singular(&old_p->list))) { | 1231 | list_is_singular(&old_p->list))) { |
| 790 | /* | 1232 | /* |
| 791 | * Only probe on the hash list. Disarm only if kprobes are | 1233 | * Only probe on the hash list. Disarm only if kprobes are |
| @@ -793,7 +1235,7 @@ static int __kprobes __unregister_kprobe_top(struct kprobe *p) | |||
| 793 | * already have been removed. We save on flushing icache. | 1235 | * already have been removed. We save on flushing icache. |
| 794 | */ | 1236 | */ |
| 795 | if (!kprobes_all_disarmed && !kprobe_disabled(old_p)) | 1237 | if (!kprobes_all_disarmed && !kprobe_disabled(old_p)) |
| 796 | disarm_kprobe(p); | 1238 | disarm_kprobe(old_p); |
| 797 | hlist_del_rcu(&old_p->hlist); | 1239 | hlist_del_rcu(&old_p->hlist); |
| 798 | } else { | 1240 | } else { |
| 799 | if (p->break_handler && !kprobe_gone(p)) | 1241 | if (p->break_handler && !kprobe_gone(p)) |
| @@ -809,8 +1251,13 @@ noclean: | |||
| 809 | list_del_rcu(&p->list); | 1251 | list_del_rcu(&p->list); |
| 810 | if (!kprobe_disabled(old_p)) { | 1252 | if (!kprobe_disabled(old_p)) { |
| 811 | try_to_disable_aggr_kprobe(old_p); | 1253 | try_to_disable_aggr_kprobe(old_p); |
| 812 | if (!kprobes_all_disarmed && kprobe_disabled(old_p)) | 1254 | if (!kprobes_all_disarmed) { |
| 813 | disarm_kprobe(old_p); | 1255 | if (kprobe_disabled(old_p)) |
| 1256 | disarm_kprobe(old_p); | ||
| 1257 | else | ||
| 1258 | /* Try to optimize this probe again */ | ||
| 1259 | optimize_kprobe(old_p); | ||
| 1260 | } | ||
| 814 | } | 1261 | } |
| 815 | } | 1262 | } |
| 816 | return 0; | 1263 | return 0; |
| @@ -827,7 +1274,7 @@ static void __kprobes __unregister_kprobe_bottom(struct kprobe *p) | |||
| 827 | old_p = list_entry(p->list.next, struct kprobe, list); | 1274 | old_p = list_entry(p->list.next, struct kprobe, list); |
| 828 | list_del(&p->list); | 1275 | list_del(&p->list); |
| 829 | arch_remove_kprobe(old_p); | 1276 | arch_remove_kprobe(old_p); |
| 830 | kfree(old_p); | 1277 | free_aggr_kprobe(old_p); |
| 831 | } | 1278 | } |
| 832 | } | 1279 | } |
| 833 | 1280 | ||
| @@ -1123,7 +1570,7 @@ static void __kprobes kill_kprobe(struct kprobe *p) | |||
| 1123 | struct kprobe *kp; | 1570 | struct kprobe *kp; |
| 1124 | 1571 | ||
| 1125 | p->flags |= KPROBE_FLAG_GONE; | 1572 | p->flags |= KPROBE_FLAG_GONE; |
| 1126 | if (p->pre_handler == aggr_pre_handler) { | 1573 | if (kprobe_aggrprobe(p)) { |
| 1127 | /* | 1574 | /* |
| 1128 | * If this is an aggr_kprobe, we have to list all the | 1575 | * If this is an aggr_kprobe, we have to list all the |
| 1129 | * chained probes and mark them GONE. | 1576 | * chained probes and mark them GONE. |
| @@ -1132,6 +1579,7 @@ static void __kprobes kill_kprobe(struct kprobe *p) | |||
| 1132 | kp->flags |= KPROBE_FLAG_GONE; | 1579 | kp->flags |= KPROBE_FLAG_GONE; |
| 1133 | p->post_handler = NULL; | 1580 | p->post_handler = NULL; |
| 1134 | p->break_handler = NULL; | 1581 | p->break_handler = NULL; |
| 1582 | kill_optimized_kprobe(p); | ||
| 1135 | } | 1583 | } |
| 1136 | /* | 1584 | /* |
| 1137 | * Here, we can remove insn_slot safely, because no thread calls | 1585 | * Here, we can remove insn_slot safely, because no thread calls |
| @@ -1241,6 +1689,15 @@ static int __init init_kprobes(void) | |||
| 1241 | } | 1689 | } |
| 1242 | } | 1690 | } |
| 1243 | 1691 | ||
| 1692 | #if defined(CONFIG_OPTPROBES) | ||
| 1693 | #if defined(__ARCH_WANT_KPROBES_INSN_SLOT) | ||
| 1694 | /* Init kprobe_optinsn_slots */ | ||
| 1695 | kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE; | ||
| 1696 | #endif | ||
| 1697 | /* By default, kprobes can be optimized */ | ||
| 1698 | kprobes_allow_optimization = true; | ||
| 1699 | #endif | ||
| 1700 | |||
| 1244 | /* By default, kprobes are armed */ | 1701 | /* By default, kprobes are armed */ |
| 1245 | kprobes_all_disarmed = false; | 1702 | kprobes_all_disarmed = false; |
| 1246 | 1703 | ||
| @@ -1259,7 +1716,7 @@ static int __init init_kprobes(void) | |||
| 1259 | 1716 | ||
| 1260 | #ifdef CONFIG_DEBUG_FS | 1717 | #ifdef CONFIG_DEBUG_FS |
| 1261 | static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p, | 1718 | static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p, |
| 1262 | const char *sym, int offset,char *modname) | 1719 | const char *sym, int offset, char *modname, struct kprobe *pp) |
| 1263 | { | 1720 | { |
| 1264 | char *kprobe_type; | 1721 | char *kprobe_type; |
| 1265 | 1722 | ||
| @@ -1269,19 +1726,21 @@ static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p, | |||
| 1269 | kprobe_type = "j"; | 1726 | kprobe_type = "j"; |
| 1270 | else | 1727 | else |
| 1271 | kprobe_type = "k"; | 1728 | kprobe_type = "k"; |
| 1729 | |||
| 1272 | if (sym) | 1730 | if (sym) |
| 1273 | seq_printf(pi, "%p %s %s+0x%x %s %s%s\n", | 1731 | seq_printf(pi, "%p %s %s+0x%x %s ", |
| 1274 | p->addr, kprobe_type, sym, offset, | 1732 | p->addr, kprobe_type, sym, offset, |
| 1275 | (modname ? modname : " "), | 1733 | (modname ? modname : " ")); |
| 1276 | (kprobe_gone(p) ? "[GONE]" : ""), | ||
| 1277 | ((kprobe_disabled(p) && !kprobe_gone(p)) ? | ||
| 1278 | "[DISABLED]" : "")); | ||
| 1279 | else | 1734 | else |
| 1280 | seq_printf(pi, "%p %s %p %s%s\n", | 1735 | seq_printf(pi, "%p %s %p ", |
| 1281 | p->addr, kprobe_type, p->addr, | 1736 | p->addr, kprobe_type, p->addr); |
| 1282 | (kprobe_gone(p) ? "[GONE]" : ""), | 1737 | |
| 1283 | ((kprobe_disabled(p) && !kprobe_gone(p)) ? | 1738 | if (!pp) |
| 1284 | "[DISABLED]" : "")); | 1739 | pp = p; |
| 1740 | seq_printf(pi, "%s%s%s\n", | ||
| 1741 | (kprobe_gone(p) ? "[GONE]" : ""), | ||
| 1742 | ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""), | ||
| 1743 | (kprobe_optimized(pp) ? "[OPTIMIZED]" : "")); | ||
| 1285 | } | 1744 | } |
| 1286 | 1745 | ||
| 1287 | static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos) | 1746 | static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos) |
| @@ -1317,11 +1776,11 @@ static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v) | |||
| 1317 | hlist_for_each_entry_rcu(p, node, head, hlist) { | 1776 | hlist_for_each_entry_rcu(p, node, head, hlist) { |
| 1318 | sym = kallsyms_lookup((unsigned long)p->addr, NULL, | 1777 | sym = kallsyms_lookup((unsigned long)p->addr, NULL, |
| 1319 | &offset, &modname, namebuf); | 1778 | &offset, &modname, namebuf); |
| 1320 | if (p->pre_handler == aggr_pre_handler) { | 1779 | if (kprobe_aggrprobe(p)) { |
| 1321 | list_for_each_entry_rcu(kp, &p->list, list) | 1780 | list_for_each_entry_rcu(kp, &p->list, list) |
| 1322 | report_probe(pi, kp, sym, offset, modname); | 1781 | report_probe(pi, kp, sym, offset, modname, p); |
| 1323 | } else | 1782 | } else |
| 1324 | report_probe(pi, p, sym, offset, modname); | 1783 | report_probe(pi, p, sym, offset, modname, NULL); |
| 1325 | } | 1784 | } |
| 1326 | preempt_enable(); | 1785 | preempt_enable(); |
| 1327 | return 0; | 1786 | return 0; |
| @@ -1399,12 +1858,13 @@ int __kprobes enable_kprobe(struct kprobe *kp) | |||
| 1399 | goto out; | 1858 | goto out; |
| 1400 | } | 1859 | } |
| 1401 | 1860 | ||
| 1402 | if (!kprobes_all_disarmed && kprobe_disabled(p)) | ||
| 1403 | arm_kprobe(p); | ||
| 1404 | |||
| 1405 | p->flags &= ~KPROBE_FLAG_DISABLED; | ||
| 1406 | if (p != kp) | 1861 | if (p != kp) |
| 1407 | kp->flags &= ~KPROBE_FLAG_DISABLED; | 1862 | kp->flags &= ~KPROBE_FLAG_DISABLED; |
| 1863 | |||
| 1864 | if (!kprobes_all_disarmed && kprobe_disabled(p)) { | ||
| 1865 | p->flags &= ~KPROBE_FLAG_DISABLED; | ||
| 1866 | arm_kprobe(p); | ||
| 1867 | } | ||
| 1408 | out: | 1868 | out: |
| 1409 | mutex_unlock(&kprobe_mutex); | 1869 | mutex_unlock(&kprobe_mutex); |
| 1410 | return ret; | 1870 | return ret; |
| @@ -1424,12 +1884,13 @@ static void __kprobes arm_all_kprobes(void) | |||
| 1424 | if (!kprobes_all_disarmed) | 1884 | if (!kprobes_all_disarmed) |
| 1425 | goto already_enabled; | 1885 | goto already_enabled; |
| 1426 | 1886 | ||
| 1887 | /* Arming kprobes doesn't optimize kprobe itself */ | ||
| 1427 | mutex_lock(&text_mutex); | 1888 | mutex_lock(&text_mutex); |
| 1428 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { | 1889 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 1429 | head = &kprobe_table[i]; | 1890 | head = &kprobe_table[i]; |
| 1430 | hlist_for_each_entry_rcu(p, node, head, hlist) | 1891 | hlist_for_each_entry_rcu(p, node, head, hlist) |
| 1431 | if (!kprobe_disabled(p)) | 1892 | if (!kprobe_disabled(p)) |
| 1432 | arch_arm_kprobe(p); | 1893 | __arm_kprobe(p); |
| 1433 | } | 1894 | } |
| 1434 | mutex_unlock(&text_mutex); | 1895 | mutex_unlock(&text_mutex); |
| 1435 | 1896 | ||
| @@ -1456,16 +1917,23 @@ static void __kprobes disarm_all_kprobes(void) | |||
| 1456 | 1917 | ||
| 1457 | kprobes_all_disarmed = true; | 1918 | kprobes_all_disarmed = true; |
| 1458 | printk(KERN_INFO "Kprobes globally disabled\n"); | 1919 | printk(KERN_INFO "Kprobes globally disabled\n"); |
| 1920 | |||
| 1921 | /* | ||
| 1922 | * Here we call get_online_cpus() for avoiding text_mutex deadlock, | ||
| 1923 | * because disarming may also unoptimize kprobes. | ||
| 1924 | */ | ||
| 1925 | get_online_cpus(); | ||
| 1459 | mutex_lock(&text_mutex); | 1926 | mutex_lock(&text_mutex); |
| 1460 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { | 1927 | for (i = 0; i < KPROBE_TABLE_SIZE; i++) { |
| 1461 | head = &kprobe_table[i]; | 1928 | head = &kprobe_table[i]; |
| 1462 | hlist_for_each_entry_rcu(p, node, head, hlist) { | 1929 | hlist_for_each_entry_rcu(p, node, head, hlist) { |
| 1463 | if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) | 1930 | if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) |
| 1464 | arch_disarm_kprobe(p); | 1931 | __disarm_kprobe(p); |
| 1465 | } | 1932 | } |
| 1466 | } | 1933 | } |
| 1467 | 1934 | ||
| 1468 | mutex_unlock(&text_mutex); | 1935 | mutex_unlock(&text_mutex); |
| 1936 | put_online_cpus(); | ||
| 1469 | mutex_unlock(&kprobe_mutex); | 1937 | mutex_unlock(&kprobe_mutex); |
| 1470 | /* Allow all currently running kprobes to complete */ | 1938 | /* Allow all currently running kprobes to complete */ |
| 1471 | synchronize_sched(); | 1939 | synchronize_sched(); |
