diff options
| author | Jiri Kosina <jkosina@suse.cz> | 2017-05-01 15:49:28 -0400 |
|---|---|---|
| committer | Jiri Kosina <jkosina@suse.cz> | 2017-05-01 15:49:28 -0400 |
| commit | a0841609f658c77f066af9c61a2e13143564fcb4 (patch) | |
| tree | 0f0df468b6f852501cd4ed1570701e695b9f5d56 /kernel/livepatch | |
| parent | 77f8f39a2e463eca89a19b916189d0e4e38f75d8 (diff) | |
| parent | e679af627fe875a51d40b9a2b17f08fbde36e0e2 (diff) | |
Merge branches 'for-4.12/upstream' and 'for-4.12/klp-hybrid-consistency-model' into for-linus
Diffstat (limited to 'kernel/livepatch')
| -rw-r--r-- | kernel/livepatch/Makefile | 2 | ||||
| -rw-r--r-- | kernel/livepatch/core.c | 437 | ||||
| -rw-r--r-- | kernel/livepatch/core.h | 6 | ||||
| -rw-r--r-- | kernel/livepatch/patch.c | 272 | ||||
| -rw-r--r-- | kernel/livepatch/patch.h | 33 | ||||
| -rw-r--r-- | kernel/livepatch/transition.c | 553 | ||||
| -rw-r--r-- | kernel/livepatch/transition.h | 14 |
7 files changed, 1043 insertions, 274 deletions
diff --git a/kernel/livepatch/Makefile b/kernel/livepatch/Makefile index e8780c0901d9..2b8bdb1925da 100644 --- a/kernel/livepatch/Makefile +++ b/kernel/livepatch/Makefile | |||
| @@ -1,3 +1,3 @@ | |||
| 1 | obj-$(CONFIG_LIVEPATCH) += livepatch.o | 1 | obj-$(CONFIG_LIVEPATCH) += livepatch.o |
| 2 | 2 | ||
| 3 | livepatch-objs := core.o | 3 | livepatch-objs := core.o patch.o transition.o |
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 8739e9e0bdb8..b9628e43c78f 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c | |||
| @@ -24,61 +24,31 @@ | |||
| 24 | #include <linux/kernel.h> | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/mutex.h> | 25 | #include <linux/mutex.h> |
| 26 | #include <linux/slab.h> | 26 | #include <linux/slab.h> |
| 27 | #include <linux/ftrace.h> | ||
| 28 | #include <linux/list.h> | 27 | #include <linux/list.h> |
| 29 | #include <linux/kallsyms.h> | 28 | #include <linux/kallsyms.h> |
| 30 | #include <linux/livepatch.h> | 29 | #include <linux/livepatch.h> |
| 31 | #include <linux/elf.h> | 30 | #include <linux/elf.h> |
| 32 | #include <linux/moduleloader.h> | 31 | #include <linux/moduleloader.h> |
| 32 | #include <linux/completion.h> | ||
| 33 | #include <asm/cacheflush.h> | 33 | #include <asm/cacheflush.h> |
| 34 | 34 | #include "core.h" | |
| 35 | /** | 35 | #include "patch.h" |
| 36 | * struct klp_ops - structure for tracking registered ftrace ops structs | 36 | #include "transition.h" |
| 37 | * | ||
| 38 | * A single ftrace_ops is shared between all enabled replacement functions | ||
| 39 | * (klp_func structs) which have the same old_addr. This allows the switch | ||
| 40 | * between function versions to happen instantaneously by updating the klp_ops | ||
| 41 | * struct's func_stack list. The winner is the klp_func at the top of the | ||
| 42 | * func_stack (front of the list). | ||
| 43 | * | ||
| 44 | * @node: node for the global klp_ops list | ||
| 45 | * @func_stack: list head for the stack of klp_func's (active func is on top) | ||
| 46 | * @fops: registered ftrace ops struct | ||
| 47 | */ | ||
| 48 | struct klp_ops { | ||
| 49 | struct list_head node; | ||
| 50 | struct list_head func_stack; | ||
| 51 | struct ftrace_ops fops; | ||
| 52 | }; | ||
| 53 | 37 | ||
| 54 | /* | 38 | /* |
| 55 | * The klp_mutex protects the global lists and state transitions of any | 39 | * klp_mutex is a coarse lock which serializes access to klp data. All |
| 56 | * structure reachable from them. References to any structure must be obtained | 40 | * accesses to klp-related variables and structures must have mutex protection, |
| 57 | * under mutex protection (except in klp_ftrace_handler(), which uses RCU to | 41 | * except within the following functions which carefully avoid the need for it: |
| 58 | * ensure it gets consistent data). | 42 | * |
| 43 | * - klp_ftrace_handler() | ||
| 44 | * - klp_update_patch_state() | ||
| 59 | */ | 45 | */ |
| 60 | static DEFINE_MUTEX(klp_mutex); | 46 | DEFINE_MUTEX(klp_mutex); |
| 61 | 47 | ||
| 62 | static LIST_HEAD(klp_patches); | 48 | static LIST_HEAD(klp_patches); |
| 63 | static LIST_HEAD(klp_ops); | ||
| 64 | 49 | ||
| 65 | static struct kobject *klp_root_kobj; | 50 | static struct kobject *klp_root_kobj; |
| 66 | 51 | ||
| 67 | static struct klp_ops *klp_find_ops(unsigned long old_addr) | ||
| 68 | { | ||
| 69 | struct klp_ops *ops; | ||
| 70 | struct klp_func *func; | ||
| 71 | |||
| 72 | list_for_each_entry(ops, &klp_ops, node) { | ||
| 73 | func = list_first_entry(&ops->func_stack, struct klp_func, | ||
| 74 | stack_node); | ||
| 75 | if (func->old_addr == old_addr) | ||
| 76 | return ops; | ||
| 77 | } | ||
| 78 | |||
| 79 | return NULL; | ||
| 80 | } | ||
| 81 | |||
| 82 | static bool klp_is_module(struct klp_object *obj) | 52 | static bool klp_is_module(struct klp_object *obj) |
| 83 | { | 53 | { |
| 84 | return obj->name; | 54 | return obj->name; |
| @@ -117,7 +87,6 @@ static void klp_find_object_module(struct klp_object *obj) | |||
| 117 | mutex_unlock(&module_mutex); | 87 | mutex_unlock(&module_mutex); |
| 118 | } | 88 | } |
| 119 | 89 | ||
| 120 | /* klp_mutex must be held by caller */ | ||
| 121 | static bool klp_is_patch_registered(struct klp_patch *patch) | 90 | static bool klp_is_patch_registered(struct klp_patch *patch) |
| 122 | { | 91 | { |
| 123 | struct klp_patch *mypatch; | 92 | struct klp_patch *mypatch; |
| @@ -314,191 +283,30 @@ static int klp_write_object_relocations(struct module *pmod, | |||
| 314 | return ret; | 283 | return ret; |
| 315 | } | 284 | } |
| 316 | 285 | ||
| 317 | static void notrace klp_ftrace_handler(unsigned long ip, | ||
| 318 | unsigned long parent_ip, | ||
| 319 | struct ftrace_ops *fops, | ||
| 320 | struct pt_regs *regs) | ||
| 321 | { | ||
| 322 | struct klp_ops *ops; | ||
| 323 | struct klp_func *func; | ||
| 324 | |||
| 325 | ops = container_of(fops, struct klp_ops, fops); | ||
| 326 | |||
| 327 | rcu_read_lock(); | ||
| 328 | func = list_first_or_null_rcu(&ops->func_stack, struct klp_func, | ||
| 329 | stack_node); | ||
| 330 | if (WARN_ON_ONCE(!func)) | ||
| 331 | goto unlock; | ||
| 332 | |||
| 333 | klp_arch_set_pc(regs, (unsigned long)func->new_func); | ||
| 334 | unlock: | ||
| 335 | rcu_read_unlock(); | ||
| 336 | } | ||
| 337 | |||
| 338 | /* | ||
| 339 | * Convert a function address into the appropriate ftrace location. | ||
| 340 | * | ||
| 341 | * Usually this is just the address of the function, but on some architectures | ||
| 342 | * it's more complicated so allow them to provide a custom behaviour. | ||
| 343 | */ | ||
| 344 | #ifndef klp_get_ftrace_location | ||
| 345 | static unsigned long klp_get_ftrace_location(unsigned long faddr) | ||
| 346 | { | ||
| 347 | return faddr; | ||
| 348 | } | ||
| 349 | #endif | ||
| 350 | |||
| 351 | static void klp_disable_func(struct klp_func *func) | ||
| 352 | { | ||
| 353 | struct klp_ops *ops; | ||
| 354 | |||
| 355 | if (WARN_ON(func->state != KLP_ENABLED)) | ||
| 356 | return; | ||
| 357 | if (WARN_ON(!func->old_addr)) | ||
| 358 | return; | ||
| 359 | |||
| 360 | ops = klp_find_ops(func->old_addr); | ||
| 361 | if (WARN_ON(!ops)) | ||
| 362 | return; | ||
| 363 | |||
| 364 | if (list_is_singular(&ops->func_stack)) { | ||
| 365 | unsigned long ftrace_loc; | ||
| 366 | |||
| 367 | ftrace_loc = klp_get_ftrace_location(func->old_addr); | ||
| 368 | if (WARN_ON(!ftrace_loc)) | ||
| 369 | return; | ||
| 370 | |||
| 371 | WARN_ON(unregister_ftrace_function(&ops->fops)); | ||
| 372 | WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0)); | ||
| 373 | |||
| 374 | list_del_rcu(&func->stack_node); | ||
| 375 | list_del(&ops->node); | ||
| 376 | kfree(ops); | ||
| 377 | } else { | ||
| 378 | list_del_rcu(&func->stack_node); | ||
| 379 | } | ||
| 380 | |||
| 381 | func->state = KLP_DISABLED; | ||
| 382 | } | ||
| 383 | |||
| 384 | static int klp_enable_func(struct klp_func *func) | ||
| 385 | { | ||
| 386 | struct klp_ops *ops; | ||
| 387 | int ret; | ||
| 388 | |||
| 389 | if (WARN_ON(!func->old_addr)) | ||
| 390 | return -EINVAL; | ||
| 391 | |||
| 392 | if (WARN_ON(func->state != KLP_DISABLED)) | ||
| 393 | return -EINVAL; | ||
| 394 | |||
| 395 | ops = klp_find_ops(func->old_addr); | ||
| 396 | if (!ops) { | ||
| 397 | unsigned long ftrace_loc; | ||
| 398 | |||
| 399 | ftrace_loc = klp_get_ftrace_location(func->old_addr); | ||
| 400 | if (!ftrace_loc) { | ||
| 401 | pr_err("failed to find location for function '%s'\n", | ||
| 402 | func->old_name); | ||
| 403 | return -EINVAL; | ||
| 404 | } | ||
| 405 | |||
| 406 | ops = kzalloc(sizeof(*ops), GFP_KERNEL); | ||
| 407 | if (!ops) | ||
| 408 | return -ENOMEM; | ||
| 409 | |||
| 410 | ops->fops.func = klp_ftrace_handler; | ||
| 411 | ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS | | ||
| 412 | FTRACE_OPS_FL_DYNAMIC | | ||
| 413 | FTRACE_OPS_FL_IPMODIFY; | ||
| 414 | |||
| 415 | list_add(&ops->node, &klp_ops); | ||
| 416 | |||
| 417 | INIT_LIST_HEAD(&ops->func_stack); | ||
| 418 | list_add_rcu(&func->stack_node, &ops->func_stack); | ||
| 419 | |||
| 420 | ret = ftrace_set_filter_ip(&ops->fops, ftrace_loc, 0, 0); | ||
| 421 | if (ret) { | ||
| 422 | pr_err("failed to set ftrace filter for function '%s' (%d)\n", | ||
| 423 | func->old_name, ret); | ||
| 424 | goto err; | ||
| 425 | } | ||
| 426 | |||
| 427 | ret = register_ftrace_function(&ops->fops); | ||
| 428 | if (ret) { | ||
| 429 | pr_err("failed to register ftrace handler for function '%s' (%d)\n", | ||
| 430 | func->old_name, ret); | ||
| 431 | ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0); | ||
| 432 | goto err; | ||
| 433 | } | ||
| 434 | |||
| 435 | |||
| 436 | } else { | ||
| 437 | list_add_rcu(&func->stack_node, &ops->func_stack); | ||
| 438 | } | ||
| 439 | |||
| 440 | func->state = KLP_ENABLED; | ||
| 441 | |||
| 442 | return 0; | ||
| 443 | |||
| 444 | err: | ||
| 445 | list_del_rcu(&func->stack_node); | ||
| 446 | list_del(&ops->node); | ||
| 447 | kfree(ops); | ||
| 448 | return ret; | ||
| 449 | } | ||
| 450 | |||
| 451 | static void klp_disable_object(struct klp_object *obj) | ||
| 452 | { | ||
| 453 | struct klp_func *func; | ||
| 454 | |||
| 455 | klp_for_each_func(obj, func) | ||
| 456 | if (func->state == KLP_ENABLED) | ||
| 457 | klp_disable_func(func); | ||
| 458 | |||
| 459 | obj->state = KLP_DISABLED; | ||
| 460 | } | ||
| 461 | |||
| 462 | static int klp_enable_object(struct klp_object *obj) | ||
| 463 | { | ||
| 464 | struct klp_func *func; | ||
| 465 | int ret; | ||
| 466 | |||
| 467 | if (WARN_ON(obj->state != KLP_DISABLED)) | ||
| 468 | return -EINVAL; | ||
| 469 | |||
| 470 | if (WARN_ON(!klp_is_object_loaded(obj))) | ||
| 471 | return -EINVAL; | ||
| 472 | |||
| 473 | klp_for_each_func(obj, func) { | ||
| 474 | ret = klp_enable_func(func); | ||
| 475 | if (ret) { | ||
| 476 | klp_disable_object(obj); | ||
| 477 | return ret; | ||
| 478 | } | ||
| 479 | } | ||
| 480 | obj->state = KLP_ENABLED; | ||
| 481 | |||
| 482 | return 0; | ||
| 483 | } | ||
| 484 | |||
| 485 | static int __klp_disable_patch(struct klp_patch *patch) | 286 | static int __klp_disable_patch(struct klp_patch *patch) |
| 486 | { | 287 | { |
| 487 | struct klp_object *obj; | 288 | if (klp_transition_patch) |
| 289 | return -EBUSY; | ||
| 488 | 290 | ||
| 489 | /* enforce stacking: only the last enabled patch can be disabled */ | 291 | /* enforce stacking: only the last enabled patch can be disabled */ |
| 490 | if (!list_is_last(&patch->list, &klp_patches) && | 292 | if (!list_is_last(&patch->list, &klp_patches) && |
| 491 | list_next_entry(patch, list)->state == KLP_ENABLED) | 293 | list_next_entry(patch, list)->enabled) |
| 492 | return -EBUSY; | 294 | return -EBUSY; |
| 493 | 295 | ||
| 494 | pr_notice("disabling patch '%s'\n", patch->mod->name); | 296 | klp_init_transition(patch, KLP_UNPATCHED); |
| 495 | 297 | ||
| 496 | klp_for_each_object(patch, obj) { | 298 | /* |
| 497 | if (obj->state == KLP_ENABLED) | 299 | * Enforce the order of the func->transition writes in |
| 498 | klp_disable_object(obj); | 300 | * klp_init_transition() and the TIF_PATCH_PENDING writes in |
| 499 | } | 301 | * klp_start_transition(). In the rare case where klp_ftrace_handler() |
| 302 | * is called shortly after klp_update_patch_state() switches the task, | ||
| 303 | * this ensures the handler sees that func->transition is set. | ||
| 304 | */ | ||
| 305 | smp_wmb(); | ||
| 500 | 306 | ||
| 501 | patch->state = KLP_DISABLED; | 307 | klp_start_transition(); |
| 308 | klp_try_complete_transition(); | ||
| 309 | patch->enabled = false; | ||
| 502 | 310 | ||
| 503 | return 0; | 311 | return 0; |
| 504 | } | 312 | } |
| @@ -522,7 +330,7 @@ int klp_disable_patch(struct klp_patch *patch) | |||
| 522 | goto err; | 330 | goto err; |
| 523 | } | 331 | } |
| 524 | 332 | ||
| 525 | if (patch->state == KLP_DISABLED) { | 333 | if (!patch->enabled) { |
| 526 | ret = -EINVAL; | 334 | ret = -EINVAL; |
| 527 | goto err; | 335 | goto err; |
| 528 | } | 336 | } |
| @@ -540,32 +348,61 @@ static int __klp_enable_patch(struct klp_patch *patch) | |||
| 540 | struct klp_object *obj; | 348 | struct klp_object *obj; |
| 541 | int ret; | 349 | int ret; |
| 542 | 350 | ||
| 543 | if (WARN_ON(patch->state != KLP_DISABLED)) | 351 | if (klp_transition_patch) |
| 352 | return -EBUSY; | ||
| 353 | |||
| 354 | if (WARN_ON(patch->enabled)) | ||
| 544 | return -EINVAL; | 355 | return -EINVAL; |
| 545 | 356 | ||
| 546 | /* enforce stacking: only the first disabled patch can be enabled */ | 357 | /* enforce stacking: only the first disabled patch can be enabled */ |
| 547 | if (patch->list.prev != &klp_patches && | 358 | if (patch->list.prev != &klp_patches && |
| 548 | list_prev_entry(patch, list)->state == KLP_DISABLED) | 359 | !list_prev_entry(patch, list)->enabled) |
| 549 | return -EBUSY; | 360 | return -EBUSY; |
| 550 | 361 | ||
| 362 | /* | ||
| 363 | * A reference is taken on the patch module to prevent it from being | ||
| 364 | * unloaded. | ||
| 365 | * | ||
| 366 | * Note: For immediate (no consistency model) patches we don't allow | ||
| 367 | * patch modules to unload since there is no safe/sane method to | ||
| 368 | * determine if a thread is still running in the patched code contained | ||
| 369 | * in the patch module once the ftrace registration is successful. | ||
| 370 | */ | ||
| 371 | if (!try_module_get(patch->mod)) | ||
| 372 | return -ENODEV; | ||
| 373 | |||
| 551 | pr_notice("enabling patch '%s'\n", patch->mod->name); | 374 | pr_notice("enabling patch '%s'\n", patch->mod->name); |
| 552 | 375 | ||
| 376 | klp_init_transition(patch, KLP_PATCHED); | ||
| 377 | |||
| 378 | /* | ||
| 379 | * Enforce the order of the func->transition writes in | ||
| 380 | * klp_init_transition() and the ops->func_stack writes in | ||
| 381 | * klp_patch_object(), so that klp_ftrace_handler() will see the | ||
| 382 | * func->transition updates before the handler is registered and the | ||
| 383 | * new funcs become visible to the handler. | ||
| 384 | */ | ||
| 385 | smp_wmb(); | ||
| 386 | |||
| 553 | klp_for_each_object(patch, obj) { | 387 | klp_for_each_object(patch, obj) { |
| 554 | if (!klp_is_object_loaded(obj)) | 388 | if (!klp_is_object_loaded(obj)) |
| 555 | continue; | 389 | continue; |
| 556 | 390 | ||
| 557 | ret = klp_enable_object(obj); | 391 | ret = klp_patch_object(obj); |
| 558 | if (ret) | 392 | if (ret) { |
| 559 | goto unregister; | 393 | pr_warn("failed to enable patch '%s'\n", |
| 394 | patch->mod->name); | ||
| 395 | |||
| 396 | klp_cancel_transition(); | ||
| 397 | return ret; | ||
| 398 | } | ||
| 560 | } | 399 | } |
| 561 | 400 | ||
| 562 | patch->state = KLP_ENABLED; | 401 | klp_start_transition(); |
| 402 | klp_try_complete_transition(); | ||
| 403 | patch->enabled = true; | ||
| 563 | 404 | ||
| 564 | return 0; | 405 | return 0; |
| 565 | |||
| 566 | unregister: | ||
| 567 | WARN_ON(__klp_disable_patch(patch)); | ||
| 568 | return ret; | ||
| 569 | } | 406 | } |
| 570 | 407 | ||
| 571 | /** | 408 | /** |
| @@ -602,6 +439,7 @@ EXPORT_SYMBOL_GPL(klp_enable_patch); | |||
| 602 | * /sys/kernel/livepatch | 439 | * /sys/kernel/livepatch |
| 603 | * /sys/kernel/livepatch/<patch> | 440 | * /sys/kernel/livepatch/<patch> |
| 604 | * /sys/kernel/livepatch/<patch>/enabled | 441 | * /sys/kernel/livepatch/<patch>/enabled |
| 442 | * /sys/kernel/livepatch/<patch>/transition | ||
| 605 | * /sys/kernel/livepatch/<patch>/<object> | 443 | * /sys/kernel/livepatch/<patch>/<object> |
| 606 | * /sys/kernel/livepatch/<patch>/<object>/<function,sympos> | 444 | * /sys/kernel/livepatch/<patch>/<object>/<function,sympos> |
| 607 | */ | 445 | */ |
| @@ -611,26 +449,34 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, | |||
| 611 | { | 449 | { |
| 612 | struct klp_patch *patch; | 450 | struct klp_patch *patch; |
| 613 | int ret; | 451 | int ret; |
| 614 | unsigned long val; | 452 | bool enabled; |
| 615 | 453 | ||
| 616 | ret = kstrtoul(buf, 10, &val); | 454 | ret = kstrtobool(buf, &enabled); |
| 617 | if (ret) | 455 | if (ret) |
| 618 | return -EINVAL; | 456 | return ret; |
| 619 | |||
| 620 | if (val != KLP_DISABLED && val != KLP_ENABLED) | ||
| 621 | return -EINVAL; | ||
| 622 | 457 | ||
| 623 | patch = container_of(kobj, struct klp_patch, kobj); | 458 | patch = container_of(kobj, struct klp_patch, kobj); |
| 624 | 459 | ||
| 625 | mutex_lock(&klp_mutex); | 460 | mutex_lock(&klp_mutex); |
| 626 | 461 | ||
| 627 | if (val == patch->state) { | 462 | if (!klp_is_patch_registered(patch)) { |
| 463 | /* | ||
| 464 | * Module with the patch could either disappear meanwhile or is | ||
| 465 | * not properly initialized yet. | ||
| 466 | */ | ||
| 467 | ret = -EINVAL; | ||
| 468 | goto err; | ||
| 469 | } | ||
| 470 | |||
| 471 | if (patch->enabled == enabled) { | ||
| 628 | /* already in requested state */ | 472 | /* already in requested state */ |
| 629 | ret = -EINVAL; | 473 | ret = -EINVAL; |
| 630 | goto err; | 474 | goto err; |
| 631 | } | 475 | } |
| 632 | 476 | ||
| 633 | if (val == KLP_ENABLED) { | 477 | if (patch == klp_transition_patch) { |
| 478 | klp_reverse_transition(); | ||
| 479 | } else if (enabled) { | ||
| 634 | ret = __klp_enable_patch(patch); | 480 | ret = __klp_enable_patch(patch); |
| 635 | if (ret) | 481 | if (ret) |
| 636 | goto err; | 482 | goto err; |
| @@ -655,21 +501,33 @@ static ssize_t enabled_show(struct kobject *kobj, | |||
| 655 | struct klp_patch *patch; | 501 | struct klp_patch *patch; |
| 656 | 502 | ||
| 657 | patch = container_of(kobj, struct klp_patch, kobj); | 503 | patch = container_of(kobj, struct klp_patch, kobj); |
| 658 | return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state); | 504 | return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled); |
| 505 | } | ||
| 506 | |||
| 507 | static ssize_t transition_show(struct kobject *kobj, | ||
| 508 | struct kobj_attribute *attr, char *buf) | ||
| 509 | { | ||
| 510 | struct klp_patch *patch; | ||
| 511 | |||
| 512 | patch = container_of(kobj, struct klp_patch, kobj); | ||
| 513 | return snprintf(buf, PAGE_SIZE-1, "%d\n", | ||
| 514 | patch == klp_transition_patch); | ||
| 659 | } | 515 | } |
| 660 | 516 | ||
| 661 | static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled); | 517 | static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled); |
| 518 | static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition); | ||
| 662 | static struct attribute *klp_patch_attrs[] = { | 519 | static struct attribute *klp_patch_attrs[] = { |
| 663 | &enabled_kobj_attr.attr, | 520 | &enabled_kobj_attr.attr, |
| 521 | &transition_kobj_attr.attr, | ||
| 664 | NULL | 522 | NULL |
| 665 | }; | 523 | }; |
| 666 | 524 | ||
| 667 | static void klp_kobj_release_patch(struct kobject *kobj) | 525 | static void klp_kobj_release_patch(struct kobject *kobj) |
| 668 | { | 526 | { |
| 669 | /* | 527 | struct klp_patch *patch; |
| 670 | * Once we have a consistency model we'll need to module_put() the | 528 | |
| 671 | * patch module here. See klp_register_patch() for more details. | 529 | patch = container_of(kobj, struct klp_patch, kobj); |
| 672 | */ | 530 | complete(&patch->finish); |
| 673 | } | 531 | } |
| 674 | 532 | ||
| 675 | static struct kobj_type klp_ktype_patch = { | 533 | static struct kobj_type klp_ktype_patch = { |
| @@ -740,7 +598,6 @@ static void klp_free_patch(struct klp_patch *patch) | |||
| 740 | klp_free_objects_limited(patch, NULL); | 598 | klp_free_objects_limited(patch, NULL); |
| 741 | if (!list_empty(&patch->list)) | 599 | if (!list_empty(&patch->list)) |
| 742 | list_del(&patch->list); | 600 | list_del(&patch->list); |
| 743 | kobject_put(&patch->kobj); | ||
| 744 | } | 601 | } |
| 745 | 602 | ||
| 746 | static int klp_init_func(struct klp_object *obj, struct klp_func *func) | 603 | static int klp_init_func(struct klp_object *obj, struct klp_func *func) |
| @@ -749,7 +606,8 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func) | |||
| 749 | return -EINVAL; | 606 | return -EINVAL; |
| 750 | 607 | ||
| 751 | INIT_LIST_HEAD(&func->stack_node); | 608 | INIT_LIST_HEAD(&func->stack_node); |
| 752 | func->state = KLP_DISABLED; | 609 | func->patched = false; |
| 610 | func->transition = false; | ||
| 753 | 611 | ||
| 754 | /* The format for the sysfs directory is <function,sympos> where sympos | 612 | /* The format for the sysfs directory is <function,sympos> where sympos |
| 755 | * is the nth occurrence of this symbol in kallsyms for the patched | 613 | * is the nth occurrence of this symbol in kallsyms for the patched |
| @@ -790,6 +648,22 @@ static int klp_init_object_loaded(struct klp_patch *patch, | |||
| 790 | &func->old_addr); | 648 | &func->old_addr); |
| 791 | if (ret) | 649 | if (ret) |
| 792 | return ret; | 650 | return ret; |
| 651 | |||
| 652 | ret = kallsyms_lookup_size_offset(func->old_addr, | ||
| 653 | &func->old_size, NULL); | ||
| 654 | if (!ret) { | ||
| 655 | pr_err("kallsyms size lookup failed for '%s'\n", | ||
| 656 | func->old_name); | ||
| 657 | return -ENOENT; | ||
| 658 | } | ||
| 659 | |||
| 660 | ret = kallsyms_lookup_size_offset((unsigned long)func->new_func, | ||
| 661 | &func->new_size, NULL); | ||
| 662 | if (!ret) { | ||
| 663 | pr_err("kallsyms size lookup failed for '%s' replacement\n", | ||
| 664 | func->old_name); | ||
| 665 | return -ENOENT; | ||
| 666 | } | ||
| 793 | } | 667 | } |
| 794 | 668 | ||
| 795 | return 0; | 669 | return 0; |
| @@ -804,7 +678,7 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) | |||
| 804 | if (!obj->funcs) | 678 | if (!obj->funcs) |
| 805 | return -EINVAL; | 679 | return -EINVAL; |
| 806 | 680 | ||
| 807 | obj->state = KLP_DISABLED; | 681 | obj->patched = false; |
| 808 | obj->mod = NULL; | 682 | obj->mod = NULL; |
| 809 | 683 | ||
| 810 | klp_find_object_module(obj); | 684 | klp_find_object_module(obj); |
| @@ -845,12 +719,15 @@ static int klp_init_patch(struct klp_patch *patch) | |||
| 845 | 719 | ||
| 846 | mutex_lock(&klp_mutex); | 720 | mutex_lock(&klp_mutex); |
| 847 | 721 | ||
| 848 | patch->state = KLP_DISABLED; | 722 | patch->enabled = false; |
| 723 | init_completion(&patch->finish); | ||
| 849 | 724 | ||
| 850 | ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch, | 725 | ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch, |
| 851 | klp_root_kobj, "%s", patch->mod->name); | 726 | klp_root_kobj, "%s", patch->mod->name); |
| 852 | if (ret) | 727 | if (ret) { |
| 853 | goto unlock; | 728 | mutex_unlock(&klp_mutex); |
| 729 | return ret; | ||
| 730 | } | ||
| 854 | 731 | ||
| 855 | klp_for_each_object(patch, obj) { | 732 | klp_for_each_object(patch, obj) { |
| 856 | ret = klp_init_object(patch, obj); | 733 | ret = klp_init_object(patch, obj); |
| @@ -866,9 +743,12 @@ static int klp_init_patch(struct klp_patch *patch) | |||
| 866 | 743 | ||
| 867 | free: | 744 | free: |
| 868 | klp_free_objects_limited(patch, obj); | 745 | klp_free_objects_limited(patch, obj); |
| 869 | kobject_put(&patch->kobj); | 746 | |
| 870 | unlock: | ||
| 871 | mutex_unlock(&klp_mutex); | 747 | mutex_unlock(&klp_mutex); |
| 748 | |||
| 749 | kobject_put(&patch->kobj); | ||
| 750 | wait_for_completion(&patch->finish); | ||
| 751 | |||
| 872 | return ret; | 752 | return ret; |
| 873 | } | 753 | } |
| 874 | 754 | ||
| @@ -882,23 +762,29 @@ unlock: | |||
| 882 | */ | 762 | */ |
| 883 | int klp_unregister_patch(struct klp_patch *patch) | 763 | int klp_unregister_patch(struct klp_patch *patch) |
| 884 | { | 764 | { |
| 885 | int ret = 0; | 765 | int ret; |
| 886 | 766 | ||
| 887 | mutex_lock(&klp_mutex); | 767 | mutex_lock(&klp_mutex); |
| 888 | 768 | ||
| 889 | if (!klp_is_patch_registered(patch)) { | 769 | if (!klp_is_patch_registered(patch)) { |
| 890 | ret = -EINVAL; | 770 | ret = -EINVAL; |
| 891 | goto out; | 771 | goto err; |
| 892 | } | 772 | } |
| 893 | 773 | ||
| 894 | if (patch->state == KLP_ENABLED) { | 774 | if (patch->enabled) { |
| 895 | ret = -EBUSY; | 775 | ret = -EBUSY; |
| 896 | goto out; | 776 | goto err; |
| 897 | } | 777 | } |
| 898 | 778 | ||
| 899 | klp_free_patch(patch); | 779 | klp_free_patch(patch); |
| 900 | 780 | ||
| 901 | out: | 781 | mutex_unlock(&klp_mutex); |
| 782 | |||
| 783 | kobject_put(&patch->kobj); | ||
| 784 | wait_for_completion(&patch->finish); | ||
| 785 | |||
| 786 | return 0; | ||
| 787 | err: | ||
| 902 | mutex_unlock(&klp_mutex); | 788 | mutex_unlock(&klp_mutex); |
| 903 | return ret; | 789 | return ret; |
| 904 | } | 790 | } |
| @@ -911,12 +797,13 @@ EXPORT_SYMBOL_GPL(klp_unregister_patch); | |||
| 911 | * Initializes the data structure associated with the patch and | 797 | * Initializes the data structure associated with the patch and |
| 912 | * creates the sysfs interface. | 798 | * creates the sysfs interface. |
| 913 | * | 799 | * |
| 800 | * There is no need to take the reference on the patch module here. It is done | ||
| 801 | * later when the patch is enabled. | ||
| 802 | * | ||
| 914 | * Return: 0 on success, otherwise error | 803 | * Return: 0 on success, otherwise error |
| 915 | */ | 804 | */ |
| 916 | int klp_register_patch(struct klp_patch *patch) | 805 | int klp_register_patch(struct klp_patch *patch) |
| 917 | { | 806 | { |
| 918 | int ret; | ||
| 919 | |||
| 920 | if (!patch || !patch->mod) | 807 | if (!patch || !patch->mod) |
| 921 | return -EINVAL; | 808 | return -EINVAL; |
| 922 | 809 | ||
| @@ -930,20 +817,16 @@ int klp_register_patch(struct klp_patch *patch) | |||
| 930 | return -ENODEV; | 817 | return -ENODEV; |
| 931 | 818 | ||
| 932 | /* | 819 | /* |
| 933 | * A reference is taken on the patch module to prevent it from being | 820 | * Architectures without reliable stack traces have to set |
| 934 | * unloaded. Right now, we don't allow patch modules to unload since | 821 | * patch->immediate because there's currently no way to patch kthreads |
| 935 | * there is currently no method to determine if a thread is still | 822 | * with the consistency model. |
| 936 | * running in the patched code contained in the patch module once | ||
| 937 | * the ftrace registration is successful. | ||
| 938 | */ | 823 | */ |
| 939 | if (!try_module_get(patch->mod)) | 824 | if (!klp_have_reliable_stack() && !patch->immediate) { |
| 940 | return -ENODEV; | 825 | pr_err("This architecture doesn't have support for the livepatch consistency model.\n"); |
| 941 | 826 | return -ENOSYS; | |
| 942 | ret = klp_init_patch(patch); | 827 | } |
| 943 | if (ret) | ||
| 944 | module_put(patch->mod); | ||
| 945 | 828 | ||
| 946 | return ret; | 829 | return klp_init_patch(patch); |
| 947 | } | 830 | } |
| 948 | EXPORT_SYMBOL_GPL(klp_register_patch); | 831 | EXPORT_SYMBOL_GPL(klp_register_patch); |
| 949 | 832 | ||
| @@ -978,13 +861,17 @@ int klp_module_coming(struct module *mod) | |||
| 978 | goto err; | 861 | goto err; |
| 979 | } | 862 | } |
| 980 | 863 | ||
| 981 | if (patch->state == KLP_DISABLED) | 864 | /* |
| 865 | * Only patch the module if the patch is enabled or is | ||
| 866 | * in transition. | ||
| 867 | */ | ||
| 868 | if (!patch->enabled && patch != klp_transition_patch) | ||
| 982 | break; | 869 | break; |
| 983 | 870 | ||
| 984 | pr_notice("applying patch '%s' to loading module '%s'\n", | 871 | pr_notice("applying patch '%s' to loading module '%s'\n", |
| 985 | patch->mod->name, obj->mod->name); | 872 | patch->mod->name, obj->mod->name); |
| 986 | 873 | ||
| 987 | ret = klp_enable_object(obj); | 874 | ret = klp_patch_object(obj); |
| 988 | if (ret) { | 875 | if (ret) { |
| 989 | pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", | 876 | pr_warn("failed to apply patch '%s' to module '%s' (%d)\n", |
| 990 | patch->mod->name, obj->mod->name, ret); | 877 | patch->mod->name, obj->mod->name, ret); |
| @@ -1035,10 +922,14 @@ void klp_module_going(struct module *mod) | |||
| 1035 | if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) | 922 | if (!klp_is_module(obj) || strcmp(obj->name, mod->name)) |
| 1036 | continue; | 923 | continue; |
| 1037 | 924 | ||
| 1038 | if (patch->state != KLP_DISABLED) { | 925 | /* |
| 926 | * Only unpatch the module if the patch is enabled or | ||
| 927 | * is in transition. | ||
| 928 | */ | ||
| 929 | if (patch->enabled || patch == klp_transition_patch) { | ||
| 1039 | pr_notice("reverting patch '%s' on unloading module '%s'\n", | 930 | pr_notice("reverting patch '%s' on unloading module '%s'\n", |
| 1040 | patch->mod->name, obj->mod->name); | 931 | patch->mod->name, obj->mod->name); |
| 1041 | klp_disable_object(obj); | 932 | klp_unpatch_object(obj); |
| 1042 | } | 933 | } |
| 1043 | 934 | ||
| 1044 | klp_free_object_loaded(obj); | 935 | klp_free_object_loaded(obj); |
diff --git a/kernel/livepatch/core.h b/kernel/livepatch/core.h new file mode 100644 index 000000000000..c74f24c47837 --- /dev/null +++ b/kernel/livepatch/core.h | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | #ifndef _LIVEPATCH_CORE_H | ||
| 2 | #define _LIVEPATCH_CORE_H | ||
| 3 | |||
| 4 | extern struct mutex klp_mutex; | ||
| 5 | |||
| 6 | #endif /* _LIVEPATCH_CORE_H */ | ||
diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c new file mode 100644 index 000000000000..f8269036bf0b --- /dev/null +++ b/kernel/livepatch/patch.c | |||
| @@ -0,0 +1,272 @@ | |||
| 1 | /* | ||
| 2 | * patch.c - livepatch patching functions | ||
| 3 | * | ||
| 4 | * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com> | ||
| 5 | * Copyright (C) 2014 SUSE | ||
| 6 | * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com> | ||
| 7 | * | ||
| 8 | * This program is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU General Public License | ||
| 10 | * as published by the Free Software Foundation; either version 2 | ||
| 11 | * of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This program is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | * GNU General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU General Public License | ||
| 19 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
| 20 | */ | ||
| 21 | |||
| 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
| 23 | |||
| 24 | #include <linux/livepatch.h> | ||
| 25 | #include <linux/list.h> | ||
| 26 | #include <linux/ftrace.h> | ||
| 27 | #include <linux/rculist.h> | ||
| 28 | #include <linux/slab.h> | ||
| 29 | #include <linux/bug.h> | ||
| 30 | #include <linux/printk.h> | ||
| 31 | #include "patch.h" | ||
| 32 | #include "transition.h" | ||
| 33 | |||
| 34 | static LIST_HEAD(klp_ops); | ||
| 35 | |||
| 36 | struct klp_ops *klp_find_ops(unsigned long old_addr) | ||
| 37 | { | ||
| 38 | struct klp_ops *ops; | ||
| 39 | struct klp_func *func; | ||
| 40 | |||
| 41 | list_for_each_entry(ops, &klp_ops, node) { | ||
| 42 | func = list_first_entry(&ops->func_stack, struct klp_func, | ||
| 43 | stack_node); | ||
| 44 | if (func->old_addr == old_addr) | ||
| 45 | return ops; | ||
| 46 | } | ||
| 47 | |||
| 48 | return NULL; | ||
| 49 | } | ||
| 50 | |||
| 51 | static void notrace klp_ftrace_handler(unsigned long ip, | ||
| 52 | unsigned long parent_ip, | ||
| 53 | struct ftrace_ops *fops, | ||
| 54 | struct pt_regs *regs) | ||
| 55 | { | ||
| 56 | struct klp_ops *ops; | ||
| 57 | struct klp_func *func; | ||
| 58 | int patch_state; | ||
| 59 | |||
| 60 | ops = container_of(fops, struct klp_ops, fops); | ||
| 61 | |||
| 62 | rcu_read_lock(); | ||
| 63 | |||
| 64 | func = list_first_or_null_rcu(&ops->func_stack, struct klp_func, | ||
| 65 | stack_node); | ||
| 66 | |||
| 67 | /* | ||
| 68 | * func should never be NULL because preemption should be disabled here | ||
| 69 | * and unregister_ftrace_function() does the equivalent of a | ||
| 70 | * synchronize_sched() before the func_stack removal. | ||
| 71 | */ | ||
| 72 | if (WARN_ON_ONCE(!func)) | ||
| 73 | goto unlock; | ||
| 74 | |||
| 75 | /* | ||
| 76 | * In the enable path, enforce the order of the ops->func_stack and | ||
| 77 | * func->transition reads. The corresponding write barrier is in | ||
| 78 | * __klp_enable_patch(). | ||
| 79 | * | ||
| 80 | * (Note that this barrier technically isn't needed in the disable | ||
| 81 | * path. In the rare case where klp_update_patch_state() runs before | ||
| 82 | * this handler, its TIF_PATCH_PENDING read and this func->transition | ||
| 83 | * read need to be ordered. But klp_update_patch_state() already | ||
| 84 | * enforces that.) | ||
| 85 | */ | ||
| 86 | smp_rmb(); | ||
| 87 | |||
| 88 | if (unlikely(func->transition)) { | ||
| 89 | |||
| 90 | /* | ||
| 91 | * Enforce the order of the func->transition and | ||
| 92 | * current->patch_state reads. Otherwise we could read an | ||
| 93 | * out-of-date task state and pick the wrong function. The | ||
| 94 | * corresponding write barrier is in klp_init_transition(). | ||
| 95 | */ | ||
| 96 | smp_rmb(); | ||
| 97 | |||
| 98 | patch_state = current->patch_state; | ||
| 99 | |||
| 100 | WARN_ON_ONCE(patch_state == KLP_UNDEFINED); | ||
| 101 | |||
| 102 | if (patch_state == KLP_UNPATCHED) { | ||
| 103 | /* | ||
| 104 | * Use the previously patched version of the function. | ||
| 105 | * If no previous patches exist, continue with the | ||
| 106 | * original function. | ||
| 107 | */ | ||
| 108 | func = list_entry_rcu(func->stack_node.next, | ||
| 109 | struct klp_func, stack_node); | ||
| 110 | |||
| 111 | if (&func->stack_node == &ops->func_stack) | ||
| 112 | goto unlock; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | klp_arch_set_pc(regs, (unsigned long)func->new_func); | ||
| 117 | unlock: | ||
| 118 | rcu_read_unlock(); | ||
| 119 | } | ||
| 120 | |||
| 121 | /* | ||
| 122 | * Convert a function address into the appropriate ftrace location. | ||
| 123 | * | ||
| 124 | * Usually this is just the address of the function, but on some architectures | ||
| 125 | * it's more complicated so allow them to provide a custom behaviour. | ||
| 126 | */ | ||
| 127 | #ifndef klp_get_ftrace_location | ||
| 128 | static unsigned long klp_get_ftrace_location(unsigned long faddr) | ||
| 129 | { | ||
| 130 | return faddr; | ||
| 131 | } | ||
| 132 | #endif | ||
| 133 | |||
| 134 | static void klp_unpatch_func(struct klp_func *func) | ||
| 135 | { | ||
| 136 | struct klp_ops *ops; | ||
| 137 | |||
| 138 | if (WARN_ON(!func->patched)) | ||
| 139 | return; | ||
| 140 | if (WARN_ON(!func->old_addr)) | ||
| 141 | return; | ||
| 142 | |||
| 143 | ops = klp_find_ops(func->old_addr); | ||
| 144 | if (WARN_ON(!ops)) | ||
| 145 | return; | ||
| 146 | |||
| 147 | if (list_is_singular(&ops->func_stack)) { | ||
| 148 | unsigned long ftrace_loc; | ||
| 149 | |||
| 150 | ftrace_loc = klp_get_ftrace_location(func->old_addr); | ||
| 151 | if (WARN_ON(!ftrace_loc)) | ||
| 152 | return; | ||
| 153 | |||
| 154 | WARN_ON(unregister_ftrace_function(&ops->fops)); | ||
| 155 | WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0)); | ||
| 156 | |||
| 157 | list_del_rcu(&func->stack_node); | ||
| 158 | list_del(&ops->node); | ||
| 159 | kfree(ops); | ||
| 160 | } else { | ||
| 161 | list_del_rcu(&func->stack_node); | ||
| 162 | } | ||
| 163 | |||
| 164 | func->patched = false; | ||
| 165 | } | ||
| 166 | |||
| 167 | static int klp_patch_func(struct klp_func *func) | ||
| 168 | { | ||
| 169 | struct klp_ops *ops; | ||
| 170 | int ret; | ||
| 171 | |||
| 172 | if (WARN_ON(!func->old_addr)) | ||
| 173 | return -EINVAL; | ||
| 174 | |||
| 175 | if (WARN_ON(func->patched)) | ||
| 176 | return -EINVAL; | ||
| 177 | |||
| 178 | ops = klp_find_ops(func->old_addr); | ||
| 179 | if (!ops) { | ||
| 180 | unsigned long ftrace_loc; | ||
| 181 | |||
| 182 | ftrace_loc = klp_get_ftrace_location(func->old_addr); | ||
| 183 | if (!ftrace_loc) { | ||
| 184 | pr_err("failed to find location for function '%s'\n", | ||
| 185 | func->old_name); | ||
| 186 | return -EINVAL; | ||
| 187 | } | ||
| 188 | |||
| 189 | ops = kzalloc(sizeof(*ops), GFP_KERNEL); | ||
| 190 | if (!ops) | ||
| 191 | return -ENOMEM; | ||
| 192 | |||
| 193 | ops->fops.func = klp_ftrace_handler; | ||
| 194 | ops->fops.flags = FTRACE_OPS_FL_SAVE_REGS | | ||
| 195 | FTRACE_OPS_FL_DYNAMIC | | ||
| 196 | FTRACE_OPS_FL_IPMODIFY; | ||
| 197 | |||
| 198 | list_add(&ops->node, &klp_ops); | ||
| 199 | |||
| 200 | INIT_LIST_HEAD(&ops->func_stack); | ||
| 201 | list_add_rcu(&func->stack_node, &ops->func_stack); | ||
| 202 | |||
| 203 | ret = ftrace_set_filter_ip(&ops->fops, ftrace_loc, 0, 0); | ||
| 204 | if (ret) { | ||
| 205 | pr_err("failed to set ftrace filter for function '%s' (%d)\n", | ||
| 206 | func->old_name, ret); | ||
| 207 | goto err; | ||
| 208 | } | ||
| 209 | |||
| 210 | ret = register_ftrace_function(&ops->fops); | ||
| 211 | if (ret) { | ||
| 212 | pr_err("failed to register ftrace handler for function '%s' (%d)\n", | ||
| 213 | func->old_name, ret); | ||
| 214 | ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0); | ||
| 215 | goto err; | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | } else { | ||
| 220 | list_add_rcu(&func->stack_node, &ops->func_stack); | ||
| 221 | } | ||
| 222 | |||
| 223 | func->patched = true; | ||
| 224 | |||
| 225 | return 0; | ||
| 226 | |||
| 227 | err: | ||
| 228 | list_del_rcu(&func->stack_node); | ||
| 229 | list_del(&ops->node); | ||
| 230 | kfree(ops); | ||
| 231 | return ret; | ||
| 232 | } | ||
| 233 | |||
| 234 | void klp_unpatch_object(struct klp_object *obj) | ||
| 235 | { | ||
| 236 | struct klp_func *func; | ||
| 237 | |||
| 238 | klp_for_each_func(obj, func) | ||
| 239 | if (func->patched) | ||
| 240 | klp_unpatch_func(func); | ||
| 241 | |||
| 242 | obj->patched = false; | ||
| 243 | } | ||
| 244 | |||
| 245 | int klp_patch_object(struct klp_object *obj) | ||
| 246 | { | ||
| 247 | struct klp_func *func; | ||
| 248 | int ret; | ||
| 249 | |||
| 250 | if (WARN_ON(obj->patched)) | ||
| 251 | return -EINVAL; | ||
| 252 | |||
| 253 | klp_for_each_func(obj, func) { | ||
| 254 | ret = klp_patch_func(func); | ||
| 255 | if (ret) { | ||
| 256 | klp_unpatch_object(obj); | ||
| 257 | return ret; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | obj->patched = true; | ||
| 261 | |||
| 262 | return 0; | ||
| 263 | } | ||
| 264 | |||
| 265 | void klp_unpatch_objects(struct klp_patch *patch) | ||
| 266 | { | ||
| 267 | struct klp_object *obj; | ||
| 268 | |||
| 269 | klp_for_each_object(patch, obj) | ||
| 270 | if (obj->patched) | ||
| 271 | klp_unpatch_object(obj); | ||
| 272 | } | ||
diff --git a/kernel/livepatch/patch.h b/kernel/livepatch/patch.h new file mode 100644 index 000000000000..0db227170c36 --- /dev/null +++ b/kernel/livepatch/patch.h | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | #ifndef _LIVEPATCH_PATCH_H | ||
| 2 | #define _LIVEPATCH_PATCH_H | ||
| 3 | |||
| 4 | #include <linux/livepatch.h> | ||
| 5 | #include <linux/list.h> | ||
| 6 | #include <linux/ftrace.h> | ||
| 7 | |||
| 8 | /** | ||
| 9 | * struct klp_ops - structure for tracking registered ftrace ops structs | ||
| 10 | * | ||
| 11 | * A single ftrace_ops is shared between all enabled replacement functions | ||
| 12 | * (klp_func structs) which have the same old_addr. This allows the switch | ||
| 13 | * between function versions to happen instantaneously by updating the klp_ops | ||
| 14 | * struct's func_stack list. The winner is the klp_func at the top of the | ||
| 15 | * func_stack (front of the list). | ||
| 16 | * | ||
| 17 | * @node: node for the global klp_ops list | ||
| 18 | * @func_stack: list head for the stack of klp_func's (active func is on top) | ||
| 19 | * @fops: registered ftrace ops struct | ||
| 20 | */ | ||
| 21 | struct klp_ops { | ||
| 22 | struct list_head node; | ||
| 23 | struct list_head func_stack; | ||
| 24 | struct ftrace_ops fops; | ||
| 25 | }; | ||
| 26 | |||
| 27 | struct klp_ops *klp_find_ops(unsigned long old_addr); | ||
| 28 | |||
| 29 | int klp_patch_object(struct klp_object *obj); | ||
| 30 | void klp_unpatch_object(struct klp_object *obj); | ||
| 31 | void klp_unpatch_objects(struct klp_patch *patch); | ||
| 32 | |||
| 33 | #endif /* _LIVEPATCH_PATCH_H */ | ||
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c new file mode 100644 index 000000000000..adc0cc64aa4b --- /dev/null +++ b/kernel/livepatch/transition.c | |||
| @@ -0,0 +1,553 @@ | |||
| 1 | /* | ||
| 2 | * transition.c - Kernel Live Patching transition functions | ||
| 3 | * | ||
| 4 | * Copyright (C) 2015-2016 Josh Poimboeuf <jpoimboe@redhat.com> | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU General Public License | ||
| 8 | * as published by the Free Software Foundation; either version 2 | ||
| 9 | * of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
| 18 | */ | ||
| 19 | |||
| 20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
| 21 | |||
| 22 | #include <linux/cpu.h> | ||
| 23 | #include <linux/stacktrace.h> | ||
| 24 | #include "core.h" | ||
| 25 | #include "patch.h" | ||
| 26 | #include "transition.h" | ||
| 27 | #include "../sched/sched.h" | ||
| 28 | |||
| 29 | #define MAX_STACK_ENTRIES 100 | ||
| 30 | #define STACK_ERR_BUF_SIZE 128 | ||
| 31 | |||
| 32 | struct klp_patch *klp_transition_patch; | ||
| 33 | |||
| 34 | static int klp_target_state = KLP_UNDEFINED; | ||
| 35 | |||
| 36 | /* | ||
| 37 | * This work can be performed periodically to finish patching or unpatching any | ||
| 38 | * "straggler" tasks which failed to transition in the first attempt. | ||
| 39 | */ | ||
| 40 | static void klp_transition_work_fn(struct work_struct *work) | ||
| 41 | { | ||
| 42 | mutex_lock(&klp_mutex); | ||
| 43 | |||
| 44 | if (klp_transition_patch) | ||
| 45 | klp_try_complete_transition(); | ||
| 46 | |||
| 47 | mutex_unlock(&klp_mutex); | ||
| 48 | } | ||
| 49 | static DECLARE_DELAYED_WORK(klp_transition_work, klp_transition_work_fn); | ||
| 50 | |||
| 51 | /* | ||
| 52 | * The transition to the target patch state is complete. Clean up the data | ||
| 53 | * structures. | ||
| 54 | */ | ||
| 55 | static void klp_complete_transition(void) | ||
| 56 | { | ||
| 57 | struct klp_object *obj; | ||
| 58 | struct klp_func *func; | ||
| 59 | struct task_struct *g, *task; | ||
| 60 | unsigned int cpu; | ||
| 61 | bool immediate_func = false; | ||
| 62 | |||
| 63 | if (klp_target_state == KLP_UNPATCHED) { | ||
| 64 | /* | ||
| 65 | * All tasks have transitioned to KLP_UNPATCHED so we can now | ||
| 66 | * remove the new functions from the func_stack. | ||
| 67 | */ | ||
| 68 | klp_unpatch_objects(klp_transition_patch); | ||
| 69 | |||
| 70 | /* | ||
| 71 | * Make sure klp_ftrace_handler() can no longer see functions | ||
| 72 | * from this patch on the ops->func_stack. Otherwise, after | ||
| 73 | * func->transition gets cleared, the handler may choose a | ||
| 74 | * removed function. | ||
| 75 | */ | ||
| 76 | synchronize_rcu(); | ||
| 77 | } | ||
| 78 | |||
| 79 | if (klp_transition_patch->immediate) | ||
| 80 | goto done; | ||
| 81 | |||
| 82 | klp_for_each_object(klp_transition_patch, obj) { | ||
| 83 | klp_for_each_func(obj, func) { | ||
| 84 | func->transition = false; | ||
| 85 | if (func->immediate) | ||
| 86 | immediate_func = true; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | if (klp_target_state == KLP_UNPATCHED && !immediate_func) | ||
| 91 | module_put(klp_transition_patch->mod); | ||
| 92 | |||
| 93 | /* Prevent klp_ftrace_handler() from seeing KLP_UNDEFINED state */ | ||
| 94 | if (klp_target_state == KLP_PATCHED) | ||
| 95 | synchronize_rcu(); | ||
| 96 | |||
| 97 | read_lock(&tasklist_lock); | ||
| 98 | for_each_process_thread(g, task) { | ||
| 99 | WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING)); | ||
| 100 | task->patch_state = KLP_UNDEFINED; | ||
| 101 | } | ||
| 102 | read_unlock(&tasklist_lock); | ||
| 103 | |||
| 104 | for_each_possible_cpu(cpu) { | ||
| 105 | task = idle_task(cpu); | ||
| 106 | WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING)); | ||
| 107 | task->patch_state = KLP_UNDEFINED; | ||
| 108 | } | ||
| 109 | |||
| 110 | done: | ||
| 111 | klp_target_state = KLP_UNDEFINED; | ||
| 112 | klp_transition_patch = NULL; | ||
| 113 | } | ||
| 114 | |||
| 115 | /* | ||
| 116 | * This is called in the error path, to cancel a transition before it has | ||
| 117 | * started, i.e. klp_init_transition() has been called but | ||
| 118 | * klp_start_transition() hasn't. If the transition *has* been started, | ||
| 119 | * klp_reverse_transition() should be used instead. | ||
| 120 | */ | ||
| 121 | void klp_cancel_transition(void) | ||
| 122 | { | ||
| 123 | if (WARN_ON_ONCE(klp_target_state != KLP_PATCHED)) | ||
| 124 | return; | ||
| 125 | |||
| 126 | klp_target_state = KLP_UNPATCHED; | ||
| 127 | klp_complete_transition(); | ||
| 128 | } | ||
| 129 | |||
| 130 | /* | ||
| 131 | * Switch the patched state of the task to the set of functions in the target | ||
| 132 | * patch state. | ||
| 133 | * | ||
| 134 | * NOTE: If task is not 'current', the caller must ensure the task is inactive. | ||
| 135 | * Otherwise klp_ftrace_handler() might read the wrong 'patch_state' value. | ||
| 136 | */ | ||
| 137 | void klp_update_patch_state(struct task_struct *task) | ||
| 138 | { | ||
| 139 | rcu_read_lock(); | ||
| 140 | |||
| 141 | /* | ||
| 142 | * This test_and_clear_tsk_thread_flag() call also serves as a read | ||
| 143 | * barrier (smp_rmb) for two cases: | ||
| 144 | * | ||
| 145 | * 1) Enforce the order of the TIF_PATCH_PENDING read and the | ||
| 146 | * klp_target_state read. The corresponding write barrier is in | ||
| 147 | * klp_init_transition(). | ||
| 148 | * | ||
| 149 | * 2) Enforce the order of the TIF_PATCH_PENDING read and a future read | ||
| 150 | * of func->transition, if klp_ftrace_handler() is called later on | ||
| 151 | * the same CPU. See __klp_disable_patch(). | ||
| 152 | */ | ||
| 153 | if (test_and_clear_tsk_thread_flag(task, TIF_PATCH_PENDING)) | ||
| 154 | task->patch_state = READ_ONCE(klp_target_state); | ||
| 155 | |||
| 156 | rcu_read_unlock(); | ||
| 157 | } | ||
| 158 | |||
| 159 | /* | ||
| 160 | * Determine whether the given stack trace includes any references to a | ||
| 161 | * to-be-patched or to-be-unpatched function. | ||
| 162 | */ | ||
| 163 | static int klp_check_stack_func(struct klp_func *func, | ||
| 164 | struct stack_trace *trace) | ||
| 165 | { | ||
| 166 | unsigned long func_addr, func_size, address; | ||
| 167 | struct klp_ops *ops; | ||
| 168 | int i; | ||
| 169 | |||
| 170 | if (func->immediate) | ||
| 171 | return 0; | ||
| 172 | |||
| 173 | for (i = 0; i < trace->nr_entries; i++) { | ||
| 174 | address = trace->entries[i]; | ||
| 175 | |||
| 176 | if (klp_target_state == KLP_UNPATCHED) { | ||
| 177 | /* | ||
| 178 | * Check for the to-be-unpatched function | ||
| 179 | * (the func itself). | ||
| 180 | */ | ||
| 181 | func_addr = (unsigned long)func->new_func; | ||
| 182 | func_size = func->new_size; | ||
| 183 | } else { | ||
| 184 | /* | ||
| 185 | * Check for the to-be-patched function | ||
| 186 | * (the previous func). | ||
| 187 | */ | ||
| 188 | ops = klp_find_ops(func->old_addr); | ||
| 189 | |||
| 190 | if (list_is_singular(&ops->func_stack)) { | ||
| 191 | /* original function */ | ||
| 192 | func_addr = func->old_addr; | ||
| 193 | func_size = func->old_size; | ||
| 194 | } else { | ||
| 195 | /* previously patched function */ | ||
| 196 | struct klp_func *prev; | ||
| 197 | |||
| 198 | prev = list_next_entry(func, stack_node); | ||
| 199 | func_addr = (unsigned long)prev->new_func; | ||
| 200 | func_size = prev->new_size; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | if (address >= func_addr && address < func_addr + func_size) | ||
| 205 | return -EAGAIN; | ||
| 206 | } | ||
| 207 | |||
| 208 | return 0; | ||
| 209 | } | ||
| 210 | |||
| 211 | /* | ||
| 212 | * Determine whether it's safe to transition the task to the target patch state | ||
| 213 | * by looking for any to-be-patched or to-be-unpatched functions on its stack. | ||
| 214 | */ | ||
| 215 | static int klp_check_stack(struct task_struct *task, char *err_buf) | ||
| 216 | { | ||
| 217 | static unsigned long entries[MAX_STACK_ENTRIES]; | ||
| 218 | struct stack_trace trace; | ||
| 219 | struct klp_object *obj; | ||
| 220 | struct klp_func *func; | ||
| 221 | int ret; | ||
| 222 | |||
| 223 | trace.skip = 0; | ||
| 224 | trace.nr_entries = 0; | ||
| 225 | trace.max_entries = MAX_STACK_ENTRIES; | ||
| 226 | trace.entries = entries; | ||
| 227 | ret = save_stack_trace_tsk_reliable(task, &trace); | ||
| 228 | WARN_ON_ONCE(ret == -ENOSYS); | ||
| 229 | if (ret) { | ||
| 230 | snprintf(err_buf, STACK_ERR_BUF_SIZE, | ||
| 231 | "%s: %s:%d has an unreliable stack\n", | ||
| 232 | __func__, task->comm, task->pid); | ||
| 233 | return ret; | ||
| 234 | } | ||
| 235 | |||
| 236 | klp_for_each_object(klp_transition_patch, obj) { | ||
| 237 | if (!obj->patched) | ||
| 238 | continue; | ||
| 239 | klp_for_each_func(obj, func) { | ||
| 240 | ret = klp_check_stack_func(func, &trace); | ||
| 241 | if (ret) { | ||
| 242 | snprintf(err_buf, STACK_ERR_BUF_SIZE, | ||
| 243 | "%s: %s:%d is sleeping on function %s\n", | ||
| 244 | __func__, task->comm, task->pid, | ||
| 245 | func->old_name); | ||
| 246 | return ret; | ||
| 247 | } | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | return 0; | ||
| 252 | } | ||
| 253 | |||
| 254 | /* | ||
| 255 | * Try to safely switch a task to the target patch state. If it's currently | ||
| 256 | * running, or it's sleeping on a to-be-patched or to-be-unpatched function, or | ||
| 257 | * if the stack is unreliable, return false. | ||
| 258 | */ | ||
| 259 | static bool klp_try_switch_task(struct task_struct *task) | ||
| 260 | { | ||
| 261 | struct rq *rq; | ||
| 262 | struct rq_flags flags; | ||
| 263 | int ret; | ||
| 264 | bool success = false; | ||
| 265 | char err_buf[STACK_ERR_BUF_SIZE]; | ||
| 266 | |||
| 267 | err_buf[0] = '\0'; | ||
| 268 | |||
| 269 | /* check if this task has already switched over */ | ||
| 270 | if (task->patch_state == klp_target_state) | ||
| 271 | return true; | ||
| 272 | |||
| 273 | /* | ||
| 274 | * For arches which don't have reliable stack traces, we have to rely | ||
| 275 | * on other methods (e.g., switching tasks at kernel exit). | ||
| 276 | */ | ||
| 277 | if (!klp_have_reliable_stack()) | ||
| 278 | return false; | ||
| 279 | |||
| 280 | /* | ||
| 281 | * Now try to check the stack for any to-be-patched or to-be-unpatched | ||
| 282 | * functions. If all goes well, switch the task to the target patch | ||
| 283 | * state. | ||
| 284 | */ | ||
| 285 | rq = task_rq_lock(task, &flags); | ||
| 286 | |||
| 287 | if (task_running(rq, task) && task != current) { | ||
| 288 | snprintf(err_buf, STACK_ERR_BUF_SIZE, | ||
| 289 | "%s: %s:%d is running\n", __func__, task->comm, | ||
| 290 | task->pid); | ||
| 291 | goto done; | ||
| 292 | } | ||
| 293 | |||
| 294 | ret = klp_check_stack(task, err_buf); | ||
| 295 | if (ret) | ||
| 296 | goto done; | ||
| 297 | |||
| 298 | success = true; | ||
| 299 | |||
| 300 | clear_tsk_thread_flag(task, TIF_PATCH_PENDING); | ||
| 301 | task->patch_state = klp_target_state; | ||
| 302 | |||
| 303 | done: | ||
| 304 | task_rq_unlock(rq, task, &flags); | ||
| 305 | |||
| 306 | /* | ||
| 307 | * Due to console deadlock issues, pr_debug() can't be used while | ||
| 308 | * holding the task rq lock. Instead we have to use a temporary buffer | ||
| 309 | * and print the debug message after releasing the lock. | ||
| 310 | */ | ||
| 311 | if (err_buf[0] != '\0') | ||
| 312 | pr_debug("%s", err_buf); | ||
| 313 | |||
| 314 | return success; | ||
| 315 | |||
| 316 | } | ||
| 317 | |||
| 318 | /* | ||
| 319 | * Try to switch all remaining tasks to the target patch state by walking the | ||
| 320 | * stacks of sleeping tasks and looking for any to-be-patched or | ||
| 321 | * to-be-unpatched functions. If such functions are found, the task can't be | ||
| 322 | * switched yet. | ||
| 323 | * | ||
| 324 | * If any tasks are still stuck in the initial patch state, schedule a retry. | ||
| 325 | */ | ||
| 326 | void klp_try_complete_transition(void) | ||
| 327 | { | ||
| 328 | unsigned int cpu; | ||
| 329 | struct task_struct *g, *task; | ||
| 330 | bool complete = true; | ||
| 331 | |||
| 332 | WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED); | ||
| 333 | |||
| 334 | /* | ||
| 335 | * If the patch can be applied or reverted immediately, skip the | ||
| 336 | * per-task transitions. | ||
| 337 | */ | ||
| 338 | if (klp_transition_patch->immediate) | ||
| 339 | goto success; | ||
| 340 | |||
| 341 | /* | ||
| 342 | * Try to switch the tasks to the target patch state by walking their | ||
| 343 | * stacks and looking for any to-be-patched or to-be-unpatched | ||
| 344 | * functions. If such functions are found on a stack, or if the stack | ||
| 345 | * is deemed unreliable, the task can't be switched yet. | ||
| 346 | * | ||
| 347 | * Usually this will transition most (or all) of the tasks on a system | ||
| 348 | * unless the patch includes changes to a very common function. | ||
| 349 | */ | ||
| 350 | read_lock(&tasklist_lock); | ||
| 351 | for_each_process_thread(g, task) | ||
| 352 | if (!klp_try_switch_task(task)) | ||
| 353 | complete = false; | ||
| 354 | read_unlock(&tasklist_lock); | ||
| 355 | |||
| 356 | /* | ||
| 357 | * Ditto for the idle "swapper" tasks. | ||
| 358 | */ | ||
| 359 | get_online_cpus(); | ||
| 360 | for_each_possible_cpu(cpu) { | ||
| 361 | task = idle_task(cpu); | ||
| 362 | if (cpu_online(cpu)) { | ||
| 363 | if (!klp_try_switch_task(task)) | ||
| 364 | complete = false; | ||
| 365 | } else if (task->patch_state != klp_target_state) { | ||
| 366 | /* offline idle tasks can be switched immediately */ | ||
| 367 | clear_tsk_thread_flag(task, TIF_PATCH_PENDING); | ||
| 368 | task->patch_state = klp_target_state; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | put_online_cpus(); | ||
| 372 | |||
| 373 | if (!complete) { | ||
| 374 | /* | ||
| 375 | * Some tasks weren't able to be switched over. Try again | ||
| 376 | * later and/or wait for other methods like kernel exit | ||
| 377 | * switching. | ||
| 378 | */ | ||
| 379 | schedule_delayed_work(&klp_transition_work, | ||
| 380 | round_jiffies_relative(HZ)); | ||
| 381 | return; | ||
| 382 | } | ||
| 383 | |||
| 384 | success: | ||
| 385 | pr_notice("'%s': %s complete\n", klp_transition_patch->mod->name, | ||
| 386 | klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); | ||
| 387 | |||
| 388 | /* we're done, now cleanup the data structures */ | ||
| 389 | klp_complete_transition(); | ||
| 390 | } | ||
| 391 | |||
| 392 | /* | ||
| 393 | * Start the transition to the specified target patch state so tasks can begin | ||
| 394 | * switching to it. | ||
| 395 | */ | ||
| 396 | void klp_start_transition(void) | ||
| 397 | { | ||
| 398 | struct task_struct *g, *task; | ||
| 399 | unsigned int cpu; | ||
| 400 | |||
| 401 | WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED); | ||
| 402 | |||
| 403 | pr_notice("'%s': %s...\n", klp_transition_patch->mod->name, | ||
| 404 | klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); | ||
| 405 | |||
| 406 | /* | ||
| 407 | * If the patch can be applied or reverted immediately, skip the | ||
| 408 | * per-task transitions. | ||
| 409 | */ | ||
| 410 | if (klp_transition_patch->immediate) | ||
| 411 | return; | ||
| 412 | |||
| 413 | /* | ||
| 414 | * Mark all normal tasks as needing a patch state update. They'll | ||
| 415 | * switch either in klp_try_complete_transition() or as they exit the | ||
| 416 | * kernel. | ||
| 417 | */ | ||
| 418 | read_lock(&tasklist_lock); | ||
| 419 | for_each_process_thread(g, task) | ||
| 420 | if (task->patch_state != klp_target_state) | ||
| 421 | set_tsk_thread_flag(task, TIF_PATCH_PENDING); | ||
| 422 | read_unlock(&tasklist_lock); | ||
| 423 | |||
| 424 | /* | ||
| 425 | * Mark all idle tasks as needing a patch state update. They'll switch | ||
| 426 | * either in klp_try_complete_transition() or at the idle loop switch | ||
| 427 | * point. | ||
| 428 | */ | ||
| 429 | for_each_possible_cpu(cpu) { | ||
| 430 | task = idle_task(cpu); | ||
| 431 | if (task->patch_state != klp_target_state) | ||
| 432 | set_tsk_thread_flag(task, TIF_PATCH_PENDING); | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | /* | ||
| 437 | * Initialize the global target patch state and all tasks to the initial patch | ||
| 438 | * state, and initialize all function transition states to true in preparation | ||
| 439 | * for patching or unpatching. | ||
| 440 | */ | ||
| 441 | void klp_init_transition(struct klp_patch *patch, int state) | ||
| 442 | { | ||
| 443 | struct task_struct *g, *task; | ||
| 444 | unsigned int cpu; | ||
| 445 | struct klp_object *obj; | ||
| 446 | struct klp_func *func; | ||
| 447 | int initial_state = !state; | ||
| 448 | |||
| 449 | WARN_ON_ONCE(klp_target_state != KLP_UNDEFINED); | ||
| 450 | |||
| 451 | klp_transition_patch = patch; | ||
| 452 | |||
| 453 | /* | ||
| 454 | * Set the global target patch state which tasks will switch to. This | ||
| 455 | * has no effect until the TIF_PATCH_PENDING flags get set later. | ||
| 456 | */ | ||
| 457 | klp_target_state = state; | ||
| 458 | |||
| 459 | /* | ||
| 460 | * If the patch can be applied or reverted immediately, skip the | ||
| 461 | * per-task transitions. | ||
| 462 | */ | ||
| 463 | if (patch->immediate) | ||
| 464 | return; | ||
| 465 | |||
| 466 | /* | ||
| 467 | * Initialize all tasks to the initial patch state to prepare them for | ||
| 468 | * switching to the target state. | ||
| 469 | */ | ||
| 470 | read_lock(&tasklist_lock); | ||
| 471 | for_each_process_thread(g, task) { | ||
| 472 | WARN_ON_ONCE(task->patch_state != KLP_UNDEFINED); | ||
| 473 | task->patch_state = initial_state; | ||
| 474 | } | ||
| 475 | read_unlock(&tasklist_lock); | ||
| 476 | |||
| 477 | /* | ||
| 478 | * Ditto for the idle "swapper" tasks. | ||
| 479 | */ | ||
| 480 | for_each_possible_cpu(cpu) { | ||
| 481 | task = idle_task(cpu); | ||
| 482 | WARN_ON_ONCE(task->patch_state != KLP_UNDEFINED); | ||
| 483 | task->patch_state = initial_state; | ||
| 484 | } | ||
| 485 | |||
| 486 | /* | ||
| 487 | * Enforce the order of the task->patch_state initializations and the | ||
| 488 | * func->transition updates to ensure that klp_ftrace_handler() doesn't | ||
| 489 | * see a func in transition with a task->patch_state of KLP_UNDEFINED. | ||
| 490 | * | ||
| 491 | * Also enforce the order of the klp_target_state write and future | ||
| 492 | * TIF_PATCH_PENDING writes to ensure klp_update_patch_state() doesn't | ||
| 493 | * set a task->patch_state to KLP_UNDEFINED. | ||
| 494 | */ | ||
| 495 | smp_wmb(); | ||
| 496 | |||
| 497 | /* | ||
| 498 | * Set the func transition states so klp_ftrace_handler() will know to | ||
| 499 | * switch to the transition logic. | ||
| 500 | * | ||
| 501 | * When patching, the funcs aren't yet in the func_stack and will be | ||
| 502 | * made visible to the ftrace handler shortly by the calls to | ||
| 503 | * klp_patch_object(). | ||
| 504 | * | ||
| 505 | * When unpatching, the funcs are already in the func_stack and so are | ||
| 506 | * already visible to the ftrace handler. | ||
| 507 | */ | ||
| 508 | klp_for_each_object(patch, obj) | ||
| 509 | klp_for_each_func(obj, func) | ||
| 510 | func->transition = true; | ||
| 511 | } | ||
| 512 | |||
| 513 | /* | ||
| 514 | * This function can be called in the middle of an existing transition to | ||
| 515 | * reverse the direction of the target patch state. This can be done to | ||
| 516 | * effectively cancel an existing enable or disable operation if there are any | ||
| 517 | * tasks which are stuck in the initial patch state. | ||
| 518 | */ | ||
| 519 | void klp_reverse_transition(void) | ||
| 520 | { | ||
| 521 | unsigned int cpu; | ||
| 522 | struct task_struct *g, *task; | ||
| 523 | |||
| 524 | klp_transition_patch->enabled = !klp_transition_patch->enabled; | ||
| 525 | |||
| 526 | klp_target_state = !klp_target_state; | ||
| 527 | |||
| 528 | /* | ||
| 529 | * Clear all TIF_PATCH_PENDING flags to prevent races caused by | ||
| 530 | * klp_update_patch_state() running in parallel with | ||
| 531 | * klp_start_transition(). | ||
| 532 | */ | ||
| 533 | read_lock(&tasklist_lock); | ||
| 534 | for_each_process_thread(g, task) | ||
| 535 | clear_tsk_thread_flag(task, TIF_PATCH_PENDING); | ||
| 536 | read_unlock(&tasklist_lock); | ||
| 537 | |||
| 538 | for_each_possible_cpu(cpu) | ||
| 539 | clear_tsk_thread_flag(idle_task(cpu), TIF_PATCH_PENDING); | ||
| 540 | |||
| 541 | /* Let any remaining calls to klp_update_patch_state() complete */ | ||
| 542 | synchronize_rcu(); | ||
| 543 | |||
| 544 | klp_start_transition(); | ||
| 545 | } | ||
| 546 | |||
| 547 | /* Called from copy_process() during fork */ | ||
| 548 | void klp_copy_process(struct task_struct *child) | ||
| 549 | { | ||
| 550 | child->patch_state = current->patch_state; | ||
| 551 | |||
| 552 | /* TIF_PATCH_PENDING gets copied in setup_thread_stack() */ | ||
| 553 | } | ||
diff --git a/kernel/livepatch/transition.h b/kernel/livepatch/transition.h new file mode 100644 index 000000000000..ce09b326546c --- /dev/null +++ b/kernel/livepatch/transition.h | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | #ifndef _LIVEPATCH_TRANSITION_H | ||
| 2 | #define _LIVEPATCH_TRANSITION_H | ||
| 3 | |||
| 4 | #include <linux/livepatch.h> | ||
| 5 | |||
| 6 | extern struct klp_patch *klp_transition_patch; | ||
| 7 | |||
| 8 | void klp_init_transition(struct klp_patch *patch, int state); | ||
| 9 | void klp_cancel_transition(void); | ||
| 10 | void klp_start_transition(void); | ||
| 11 | void klp_try_complete_transition(void); | ||
| 12 | void klp_reverse_transition(void); | ||
| 13 | |||
| 14 | #endif /* _LIVEPATCH_TRANSITION_H */ | ||
