diff options
| author | Oleg Nesterov <oleg@redhat.com> | 2013-07-08 19:00:58 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-07-09 13:33:26 -0400 |
| commit | 29a55513414187b50d3cebb99884955a78d97283 (patch) | |
| tree | b02319f6fbfeae97165ba7fcf606b5ef7791d73b | |
| parent | e6a7d6077106e5c72f0519ec113d986df67ee001 (diff) | |
ptrace/x86: dont delay "disable" till second pass in ptrace_write_dr7()
ptrace_write_dr7() skips ptrace_modify_breakpoint(disabled => true)
unless second_pass, this buys nothing but complicates the code and means
that we always do the main loop twice even if "disabled" was never true.
The comment says:
Don't unregister the breakpoints right-away,
unless all register_user_hw_breakpoint()
requests have succeeded.
Firstly, we do not do register_user_hw_breakpoint(), it was removed by
commit 24f1e32c60c4 ("hw-breakpoints: Rewrite the hw-breakpoints layer
on top of perf events").
We are going to restore register_user_hw_breakpoint() (see the next
patch) but this doesn't matter: after commit 44234adcdce3
("hw-breakpoints: Modify breakpoints without unregistering them")
perf_event_disable() can not hurt, hw_breakpoint_del() does not free the
slot.
Remove the "second_pass" check from the main loop and simplify the code.
Since we have to check "bp != NULL" anyway, the patch also removes the
same check in ptrace_modify_breakpoint() and moves the comment into
ptrace_write_dr7().
With this patch the second pass is only needed to restore the saved
old_dr7. This should never fail, so the patch adds WARN_ON() to catch
the potential problems as Frederic suggested.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Acked-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jan Kratochvil <jan.kratochvil@redhat.com>
Cc: Michael Neuling <mikey@neuling.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Paul Mundt <lethal@linux-sh.org>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Prasad <prasad@linux.vnet.ibm.com>
Cc: Russell King <linux@arm.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
| -rw-r--r-- | arch/x86/kernel/ptrace.c | 53 |
1 files changed, 20 insertions, 33 deletions
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c index 0649f166d7c6..98b0a2ccc33c 100644 --- a/arch/x86/kernel/ptrace.c +++ b/arch/x86/kernel/ptrace.c | |||
| @@ -609,14 +609,6 @@ ptrace_modify_breakpoint(struct perf_event *bp, int len, int type, | |||
| 609 | int gen_len, gen_type; | 609 | int gen_len, gen_type; |
| 610 | struct perf_event_attr attr; | 610 | struct perf_event_attr attr; |
| 611 | 611 | ||
| 612 | /* | ||
| 613 | * We should have at least an inactive breakpoint at this | ||
| 614 | * slot. It means the user is writing dr7 without having | ||
| 615 | * written the address register first | ||
| 616 | */ | ||
| 617 | if (!bp) | ||
| 618 | return -EINVAL; | ||
| 619 | |||
| 620 | err = arch_bp_generic_fields(len, type, &gen_len, &gen_type); | 612 | err = arch_bp_generic_fields(len, type, &gen_len, &gen_type); |
| 621 | if (err) | 613 | if (err) |
| 622 | return err; | 614 | return err; |
| @@ -634,52 +626,47 @@ ptrace_modify_breakpoint(struct perf_event *bp, int len, int type, | |||
| 634 | */ | 626 | */ |
| 635 | static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data) | 627 | static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data) |
| 636 | { | 628 | { |
| 637 | struct thread_struct *thread = &(tsk->thread); | 629 | struct thread_struct *thread = &tsk->thread; |
| 638 | unsigned long old_dr7; | 630 | unsigned long old_dr7; |
| 639 | int i, orig_ret = 0, rc = 0; | 631 | bool second_pass = false; |
| 640 | int second_pass = 0; | 632 | int i, rc, ret = 0; |
| 641 | 633 | ||
| 642 | data &= ~DR_CONTROL_RESERVED; | 634 | data &= ~DR_CONTROL_RESERVED; |
| 643 | old_dr7 = ptrace_get_dr7(thread->ptrace_bps); | 635 | old_dr7 = ptrace_get_dr7(thread->ptrace_bps); |
| 636 | |||
| 644 | restore: | 637 | restore: |
| 645 | /* | 638 | rc = 0; |
| 646 | * Loop through all the hardware breakpoints, making the | ||
| 647 | * appropriate changes to each. | ||
| 648 | */ | ||
| 649 | for (i = 0; i < HBP_NUM; i++) { | 639 | for (i = 0; i < HBP_NUM; i++) { |
| 650 | unsigned len, type; | 640 | unsigned len, type; |
| 651 | bool disabled = !decode_dr7(data, i, &len, &type); | 641 | bool disabled = !decode_dr7(data, i, &len, &type); |
| 652 | struct perf_event *bp = thread->ptrace_bps[i]; | 642 | struct perf_event *bp = thread->ptrace_bps[i]; |
| 653 | 643 | ||
| 654 | if (disabled) { | 644 | if (!bp) { |
| 645 | if (disabled) | ||
| 646 | continue; | ||
| 655 | /* | 647 | /* |
| 656 | * Don't unregister the breakpoints right-away, unless | 648 | * We should have at least an inactive breakpoint at |
| 657 | * all register_user_hw_breakpoint() requests have | 649 | * this slot. It means the user is writing dr7 without |
| 658 | * succeeded. This prevents any window of opportunity | 650 | * having written the address register first. |
| 659 | * for debug register grabbing by other users. | ||
| 660 | */ | 651 | */ |
| 661 | if (!bp || !second_pass) | 652 | rc = -EINVAL; |
| 662 | continue; | 653 | break; |
| 663 | } | 654 | } |
| 664 | 655 | ||
| 665 | rc = ptrace_modify_breakpoint(bp, len, type, tsk, disabled); | 656 | rc = ptrace_modify_breakpoint(bp, len, type, tsk, disabled); |
| 666 | if (rc) | 657 | if (rc) |
| 667 | break; | 658 | break; |
| 668 | } | 659 | } |
| 669 | /* | 660 | |
| 670 | * Make a second pass to free the remaining unused breakpoints | 661 | /* Restore if the first pass failed, second_pass shouldn't fail. */ |
| 671 | * or to restore the original breakpoints if an error occurred. | 662 | if (rc && !WARN_ON(second_pass)) { |
| 672 | */ | 663 | ret = rc; |
| 673 | if (!second_pass) { | 664 | data = old_dr7; |
| 674 | second_pass = 1; | 665 | second_pass = true; |
| 675 | if (rc < 0) { | ||
| 676 | orig_ret = rc; | ||
| 677 | data = old_dr7; | ||
| 678 | } | ||
| 679 | goto restore; | 666 | goto restore; |
| 680 | } | 667 | } |
| 681 | 668 | ||
| 682 | return orig_ret < 0 ? orig_ret : rc; | 669 | return ret; |
| 683 | } | 670 | } |
| 684 | 671 | ||
| 685 | /* | 672 | /* |
