hw-breakpoints allocation constraints updates
cover hw-breakpoints allocation constraints updates
Hi,
These patches tend to extend the breakpoint allocation constraints to be more non-x86 friendly.
It can separate the data and instruction breakpoint space and handle address ranges or other kind of tricky things that makes a breakpoint use more than one address register.
There is still one kind of constraint it can't handle, something I've found on ARMv7 implementation: linking an breakpoint to a watchpoint. But if this needs arises, I will be glad to add that support.
Beware, it's only compile-tested for now.
Tell me what you think.
Thanks.
Frederic Weisbecker (2):
hw-breakpoints: Separate constraint space for data and instruction
breakpoints
hw-breakpoints: Handle breakpoint weight in allocation constraints
arch/Kconfig | 11 ++++ arch/x86/Kconfig | 1 + include/linux/hw_breakpoint.h | 9 ++- kernel/hw_breakpoint.c | 129 ++++++++++++++++++++++++++++++----------- 4 files changed, 112 insertions(+), 38 deletions(-)
RFC PATCH 1/2 hw-breakpoints: Separate constraint space for data and instruction breakpoints
There are two outstanding fashions for archs to implement hardware breakpoints.
The first is to separate breakpoint address pattern definition space between data and instruction breakpoints. We then have typically distinct instruction address breakpoint registers and data address breakpoint registers, delivered with separate control registers for data and instruction breakpoints as well. This is the case of PowerPc and ARM for example.
The second consists in having merged breakpoint address space definition between data and instruction breakpoint. Address registers can host either instruction or data address and the access mode for the breakpoint is defined in a control register. This is the case of x86.
This patch adds a new CONFIG_HAVE_MIXED_BREAKPOINTS_REGS config that archs can select if they belong to the second case. Those will have their slot allocation merged for instructions and data breakpoints.
The others will have a separate slot tracking between data and instruction breakpoints.
--- arch/Kconfig | 11 +++++ arch/x86/Kconfig | 1 + include/linux/hw_breakpoint.h | 9 +++- kernel/hw_breakpoint.c | 86 ++++++++++++++++++++++++++++------------- 4 files changed, 77 insertions(+), 30 deletions(-)
arch/Kconfig +11 -0
@@ -137,6 +137,17 @@ config HAVE_HW_BREAKPOINT bool depends on PERF_EVENTS +config HAVE_MIXED_BREAKPOINTS_REGS + bool + depends on HAVE_HW_BREAKPOINT + help + Depending on the arch implementation of hardware breakpoints, + some of them have separate registers for data and instruction + breakpoints addresses, others have mixed registers to store + them but define the access type in a control register. + Select this option if your arch implements breakpoints under the + latter fashion. + config HAVE_USER_RETURN_NOTIFIER bool
arch/x86/Kconfig +1 -0
@@ -53,6 +53,7 @@ config X86 select HAVE_KERNEL_LZMA select HAVE_KERNEL_LZO select HAVE_HW_BREAKPOINT + select HAVE_MIXED_BREAKPOINTS_REGS select PERF_EVENTS select ANON_INODES select HAVE_ARCH_KMEMCHECK
include/linux/hw_breakpoint.h +6 -3
@@ -9,9 +9,12 @@ enum { };enum { -HW_BREAKPOINT_R =1 , -HW_BREAKPOINT_W =2 , -HW_BREAKPOINT_X =4 , +HW_BREAKPOINT_EMPTY =0 , +HW_BREAKPOINT_R =1 , +HW_BREAKPOINT_W =2 , +HW_BREAKPOINT_RW =HW_BREAKPOINT_R |HW_BREAKPOINT_W , +HW_BREAKPOINT_X =4 , +HW_BREAKPOINT_INVALID =HW_BREAKPOINT_RW |HW_BREAKPOINT_X , };#ifdef __KERNEL__
kernel/hw_breakpoint.c +59 -27
201 lines not on this page · raw message
Signature · 1 line
1.6.2.3
Re: [RFC PATCH 1/2] hw-breakpoints: Separate constraint space for data and instruction breakpoints
Hello Frederic,
Thanks for this.
Quoted 16 lines · Frederic Weisbecker
There are two outstanding fashions for archs to implement hardware breakpoints.
The first is to separate breakpoint address pattern definition space between data and instruction breakpoints. We then have typically distinct instruction address breakpoint registers and data address breakpoint registers, delivered with separate control registers for data and instruction breakpoints as well. This is the case of PowerPc and ARM for example.
The second consists in having merged breakpoint address space definition between data and instruction breakpoint. Address registers can host either instruction or data address and the access mode for the breakpoint is defined in a control register. This is the case of x86.
Quoted 7 lines
This patch adds a new
CONFIG_HAVE_MIXED_BREAKPOINTS_REGSconfig that archs can select if they belong to the second case. Those will have their slot allocation merged for instructions and data breakpoints.The others will have a separate slot tracking between data and instruction breakpoints.
This looks useful for supporting architectures with separate data/instruction breakpoints.
A couple of points:
1.) Will this affect the arch backend at all?
2.) On ARM, it is possible to have different numbers of breakpoint registers
and watchpoint registers [which we do not know until runtime]. This means that we have to define HBP_NUM as a potential upper bound, which seems a bit wasteful. Perhaps there could be a mechanism to register the available resources at runtime?
Thanks,
Will
Re: [RFC PATCH 1/2] hw-breakpoints: Separate constraint space for data and instruction breakpoints
Quoted 29 lines · Will Deacon
Hello Frederic,
Thanks for this.
Quoted 16 lines · Frederic Weisbecker
There are two outstanding fashions for archs to implement hardware breakpoints.
The first is to separate breakpoint address pattern definition space between data and instruction breakpoints. We then have typically distinct instruction address breakpoint registers and data address breakpoint registers, delivered with separate control registers for data and instruction breakpoints as well. This is the case of PowerPc and ARM for example.
The second consists in having merged breakpoint address space definition between data and instruction breakpoint. Address registers can host either instruction or data address and the access mode for the breakpoint is defined in a control register. This is the case of x86.
Quoted 7 lines
This patch adds a new
CONFIG_HAVE_MIXED_BREAKPOINTS_REGSconfig that archs can select if they belong to the second case. Those will have their slot allocation merged for instructions and data breakpoints.The others will have a separate slot tracking between data and instruction breakpoints.
Quoted 6 lines
This looks useful for supporting architectures with separate data/instruction breakpoints.
A couple of points:
1.) Will this affect the arch backend at all?
No change is required from the backend. In fact this config probably only fits for x86, unless there are other archs that have shared register addresses... Archs that only support data or inst breakpoints should also select it, but only to save a bit of memory, there would be no functional changes.
Quoted 5 lines
2.) On ARM, it is possible to have different numbers of breakpoint registers
and watchpoint registers [which we do not know until runtime]. This means that we have to defineHBP_NUMas a potential upper bound, which seems a bit wasteful. Perhaps there could be a mechanism to register the available resources at runtime?
Ah that's interesting. Ok, I will update my patch to integrate that.
RFC PATCH 2/2 hw-breakpoints: Handle breakpoint weight in allocation constraints
In reply to Frederic WeisbeckerDepending on their nature and on what an arch supports, breakpoints may consume more than one address register. For example a simple absolute address match usually only requires one address register. But an address range match may consume two registers.
Currently our slot allocation constraints, that tend to reflect the limited arch's resources, always consider that a breakpoint consumes one slot.
Then provide a way for archs to tell us the weight of a breakpoint through a new hw_breakpoint_weight() helper. This weight will be computed against the generic allocation constraints instead of a constant value.
--- kernel/hw_breakpoint.c | 63 ++++++++++++++++++++++++++++++++++------------- 1 files changed, 45 insertions(+), 18 deletions(-)
kernel/hw_breakpoint.c +45 -18
@@ -77,6 +77,11 @@ struct bp_busy_slots {/* Serialize accesses to the above constraints */ static DEFINE_MUTEX (nr_bp_mutex); +__weak int hw_breakpoint_weight (struct perf_event *bp) +{ +return 1 ; +} +static inline enum bp_type_idx find_slot_idx (struct perf_event *bp) {if (bp->attr.bp_type &HW_BREAKPOINT_RW )
@@ -124,7 +129,7 @@ static int task_bp_pinned(struct task_struct *tsk, enum bp_type_idx type)list_for_each_entry (bp, list, event_entry) {if (bp->attr.type ==PERF_TYPE_BREAKPOINT )if (find_slot_idx (bp) == type) - count++; + count +=hw_breakpoint_weight (bp); }raw_spin_unlock_irqrestore (&ctx->lock, flags);
@@ -174,25 +179,40 @@ fetch_bp_busy_slots(struct bp_busy_slots *slots, struct perf_event *bp, }/* +* For now, continue to consider flexible as pinned, until we can +* ensure no flexible event can ever be scheduled before a pinned event +* in a same cpu. +*/ +static void +fetch_this_slot (struct bp_busy_slots *slots,int weight) +{ + slots->pinned += weight; +} + +/* * Add a pinned breakpoint for the given task in our constraint table */ static void toggle_bp_task_slot (struct task_struct *tsk,int cpu,bool enable, -enum bp_type_idx type) +enum bp_type_idx type,int weight) {unsigned int *tsk_pinned; -int count =0 ; +int old_count =0 ; +int old_idx =0 ; +int idx =0 ; - count =task_bp_pinned (tsk, type); + old_count =task_bp_pinned (tsk, type); + old_idx = old_count -1 ; + idx = old_idx + weight; tsk_pinned =per_cpu (nr_task_bp_pinned[type], cpu);if (enable) { - tsk_pinned[count]++; -if (count >0 ) - tsk_pinned[count-1 ]--; + tsk_pinned[idx]++; +if (old_count >0 ) + tsk_pinned[old_idx]--; }else { - tsk_pinned[count]--; -if (count >0 ) - tsk_pinned[count-1 ]++; + tsk_pinned[idx]--; +if (old_count >0 ) + tsk_pinned[old_idx]++; } }
@@ -200,7 +220,8 @@ static void toggle_bp_task_slot(struct task_struct *tsk, int cpu, bool enable,* Add/remove the given breakpoint in our constraint table */ static void -toggle_bp_slot (struct perf_event *bp,bool enable,enum bp_type_idx type) +toggle_bp_slot (struct perf_event *bp,bool enable,enum bp_type_idx type, +int weight) {int cpu = bp->cpu;struct task_struct *tsk = bp->ctx->task;
@@ -208,20 +229,20 @@ toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type)/* Pinned counter task profiling */ if (tsk) {if (cpu >=0 ) { -toggle_bp_task_slot (tsk, cpu, enable, type); +toggle_bp_task_slot (tsk, cpu, enable, type, weight);return ; }for_each_online_cpu (cpu) -toggle_bp_task_slot (tsk, cpu, enable, type); +toggle_bp_task_slot (tsk, cpu, enable, type, weight);return ; }/* Pinned counter cpu profiling */ if (enable) -per_cpu (nr_cpu_bp_pinned[type], bp->cpu)++; +per_cpu (nr_cpu_bp_pinned[type], bp->cpu) += weight;else -per_cpu (nr_cpu_bp_pinned[type], bp->cpu)--; +per_cpu (nr_cpu_bp_pinned[type], bp->cpu) -= weight; }/*
@@ -269,6 +290,7 @@ static int __reserve_bp_slot(struct perf_event *bp) {struct bp_busy_slots slots = {0 };enum bp_type_idx type; +int weight;/* Basic checks */ if (bp->attr.bp_type == HW_BREAKPOINT_EMPTY ||
@@ -276,13 +298,16 @@ static int __reserve_bp_slot(struct perf_event *bp)return -EINVAL ; type =find_slot_idx (bp); + weight =hw_breakpoint_weight (bp); +fetch_bp_busy_slots (&slots, bp, type); +fetch_this_slot (&slots, weight);/* Flexible counters need to keep at least one slot */ -if (slots.pinned + (!!slots.flexible) ==HBP_NUM ) +if (slots.pinned + (!!slots.flexible) >HBP_NUM )return -ENOSPC ; -toggle_bp_slot (bp, true, type); +toggle_bp_slot (bp, true, type, weight);return 0 ; }
@@ -303,9 +328,11 @@ int reserve_bp_slot(struct perf_event *bp)static void __release_bp_slot (struct perf_event *bp) {enum bp_type_idx type; +int weight; type =find_slot_idx (bp); -toggle_bp_slot (bp, false, type); + weight =hw_breakpoint_weight (bp); +toggle_bp_slot (bp, false, type, weight); }void release_bp_slot (struct perf_event *bp)
Signature · 1 line
1.6.2.3