Skip to contentSkip to conversation

perf/core: fast breakpoint modification via _IOC_MODIFY_BREAKPOINT.

PATCH Superseded Milind Chabbi 4 participants 12 messages Nov 6, 2017 – Nov 8, 2017 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf/core: fast breakpoint modification via _IOC_MODIFY_BREAKPOINT.

Problem and motivation: Once a breakpoint perf event (PERF_TYPE_BREAKPOINT) is created, there is no flexibility to change the breakpoint type (bp_type), breakpoint address (bp_addr), or breakpoint length (bp_len). The only option is to close the perf event and configure a new breakpoint event. This inflexibility has a significant performance overhead. For example, sampling-based, lightweight performance profilers (and also concurrency bug detection tools), monitor different addresses for a short duration using PERF_TYPE_BREAKPOINT and change the address (bp_addr) to another address or change the kind of breakpoint (bp_type) from "write" to a "read" or vice-versa or change the length (bp_len) of the address being monitored. The cost of these modifications is prohibitive since it involves unmapping the circular buffer associated with the perf event, closing the perf event, opening another perf event and mmaping another circular buffer.

Solution: The new ioctl flag for perf events, PERF_EVENT_IOC_MODIFY_BREAKPOINT, introduced in this patch takes a pointer to a struct perf_event_attr as an argument to update an old breakpoint event with new address, type, and size. This facility allows retaining a previous mmaped perf events ring buffer and avoids having to close and reopen another perf event. The patch replicates some of its functionality of modify_user_hw_breakpoint() in kernel/events/hw_breakpoint.c. modify_user_hw_breakpoint cannot be called directed since perf_event_ctx_lock() is already held in _perf_ioctl().

Evidence: Experiments show that the baseline (not able to modify an already created breakpoint) costs an order of magnitude (~10x) more than the suggested optimization (having the ability to dynamically modifying a configured breakpoint via ioctl). When the breakpoints typically do not trap, the speedup due to the suggested optimization is ~10x; even when the breakpoints always trap, the speedup is ~4x due to the suggested optimization.

Testing: tests posted at
https://github.com/linux-contrib/perf_event_modify_bp demonstrate the performance significance of this patch. Tests also check the functional correctness of the patch.

---
 include/uapi/linux/perf_event.h       |  2 ++
 kernel/events/core.c                  | 46 +++++++++++++++++++++++++++++++++++
 kernel/events/hw_breakpoint.c         |  2 +-
 kernel/events/internal.h              |  1 +
 tools/include/uapi/linux/perf_event.h |  2 ++
 5 files changed, 52 insertions(+), 1 deletion(-)
include/uapi/linux/perf_event.h +2 -0
index 362493a..d458214 100644
@@ -433,6 +433,8 @@ struct perf_event_attr {
 #define PERF_EVENT_IOC_ID		_IOR('$', 7, __u64 *)
 #define PERF_EVENT_IOC_SET_BPF		_IOW('$', 8, __u32)
 #define PERF_EVENT_IOC_PAUSE_OUTPUT	_IOW('$', 9, __u32)
+#define PERF_EVENT_IOC_MODIFY_BREAKPOINT \
+			_IOW('$', 10, struct perf_event_attr *)
 
 enum perf_event_ioc_flags {
 	PERF_IOC_FLAG_GROUP		= 1U << 0,
kernel/events/core.c +46 -0

64 lines not on this page · raw message

kernel/events/hw_breakpoint.c +1 -1
index 3f8cb1e..fde596c 100644
@@ -367,7 +367,7 @@ int dbg_release_bp_slot(struct perf_event *bp)
 	return 0;
 }
 
-static int validate_hw_breakpoint(struct perf_event *bp)
+int validate_hw_breakpoint(struct perf_event *bp)
 {
 	int ret;
 
kernel/events/internal.h +1 -0
index 09b1537..acb2b8b 100644
@@ -82,6 +82,7 @@ extern int rb_alloc_aux(struct ring_buffer *rb, struct perf_event *event,
 extern void rb_free_aux(struct ring_buffer *rb);
 extern struct ring_buffer *ring_buffer_get(struct perf_event *event);
 extern void ring_buffer_put(struct ring_buffer *rb);
+extern int validate_hw_breakpoint(struct perf_event *bp);
 
 static inline bool rb_has_aux(struct ring_buffer *rb)
 {
tools/include/uapi/linux/perf_event.h +2 -0
index 140ae63..5c1dcd3 100644
@@ -432,6 +432,8 @@ struct perf_event_attr {
 #define PERF_EVENT_IOC_ID		_IOR('$', 7, __u64 *)
 #define PERF_EVENT_IOC_SET_BPF		_IOW('$', 8, __u32)
 #define PERF_EVENT_IOC_PAUSE_OUTPUT	_IOW('$', 9, __u32)
+#define PERF_EVENT_IOC_MODIFY_BREAKPOINT	\
+				_IOW('$', 10, struct perf_event_attr *)
 
 enum perf_event_ioc_flags {
 	PERF_IOC_FLAG_GROUP		= 1U << 0,
Signature · 1 line
1.8.3.1
#1 lore raw

kernel/events/core.c int perf_event_refresh(struct perf_event *event, int refresh) · line 2761

Quoted 13 lines
kernel/events/core.c +13 -0
+static int _perf_event_modify_breakpoint(struct perf_event *bp,
+					 struct perf_event_attr *attr)
+{
+	u64 old_addr = bp->attr.bp_addr;
+	u64 old_len = bp->attr.bp_len;
+	int old_type = bp->attr.bp_type;
+	int err = 0;
+
+	_perf_event_disable(bp);
+
+	bp->attr.bp_addr = attr->bp_addr;
+	bp->attr.bp_type = attr->bp_type;
+	bp->attr.bp_len = attr->bp_len;

You don't check any of the other fields, so user space is free to fill in junk. That means they can never be used for anything. It would be better to check at least some of them for being zero, and also that the type matches the break point.

-Andi

maintainer
#2 lore raw
Quoted 10 lines · Andi Kleen
Quoted 10 lines
kernel/events/core.c +10 -0
+static int _perf_event_modify_breakpoint(struct perf_event *bp,
+					 struct perf_event_attr *attr)
+{
+	u64 old_addr = bp->attr.bp_addr;
+	u64 old_len = bp->attr.bp_len;
+	int old_type = bp->attr.bp_type;
+	int err = 0;
+
+	_perf_event_disable(bp);
+
Quoted 8 lines

kernel/events/core.c int perf_event_refresh(struct perf_event *event, int refresh) · line 2761

Quoted 3 lines
kernel/events/core.c +3 -0
+	bp->attr.bp_addr = attr->bp_addr;
+	bp->attr.bp_type = attr->bp_type;
+	bp->attr.bp_len = attr->bp_len;

You don't check any of the other fields, so user space is free to fill in junk. That means they can never be used for anything. It would be better to check at least some of them for being zero, and also that the type matches the break point.

Yes, the values should at the very least get the exact same validation they would get on creating an event with those values.

#3 lore raw
Quoted 20 lines · Peter Zijlstra
Quoted 10 lines · Andi Kleen
Quoted 10 lines
kernel/events/core.c +10 -0
+static int _perf_event_modify_breakpoint(struct perf_event *bp,
+					 struct perf_event_attr *attr)
+{
+	u64 old_addr = bp->attr.bp_addr;
+	u64 old_len = bp->attr.bp_len;
+	int old_type = bp->attr.bp_type;
+	int err = 0;
+
+	_perf_event_disable(bp);
+
Quoted 8 lines

kernel/events/core.c int perf_event_refresh(struct perf_event *event, int refresh) · line 2761

Quoted 3 lines
kernel/events/core.c +3 -0
+	bp->attr.bp_addr = attr->bp_addr;
+	bp->attr.bp_type = attr->bp_type;
+	bp->attr.bp_len = attr->bp_len;

You don't check any of the other fields, so user space is free to fill in junk. That means they can never be used for anything. It would be better to check at least some of them for being zero, and also that the type matches the break point.

Quoted 2 lines

Yes, the values should at the very least get the exact same validation they would get on creating an event with those values.

In this case the ioctl could be also generalized. Not call it _BREAKPOINT, just _MODIFY. Just for now it would be only implemented for break points, but that could be potentially extended later.

-Andi

maintainer
#4 lore raw
In reply to Milind Chabbi

include/uapi/linux/perf_event.h struct perf_event_attr { · line 437

Quoted 10 lines · Milind Chabbi
include/uapi/linux/perf_event.h +2 -0
index 362493a..d458214 100644
@@ -433,6 +433,8 @@ struct perf_event_attr {
 #define PERF_EVENT_IOC_ID		_IOR('$', 7, __u64 *)
 #define PERF_EVENT_IOC_SET_BPF		_IOW('$', 8, __u32)
 #define PERF_EVENT_IOC_PAUSE_OUTPUT	_IOW('$', 9, __u32)
+#define PERF_EVENT_IOC_MODIFY_BREAKPOINT \
+			_IOW('$', 10, struct perf_event_attr *)

I really hate this thing. I would much rather see a more generic update perf_event_attr interface. Where we allow modifying some of the perf_event_attr fields.

#5 lore raw

Peter,

Generic update perf_event_attr interface is noble but impractical. It will cause a validation nightmare.
Many of the behaviors or choices will become hard to reason.

If somebody changes "sample_period"
it is unclear what to do it the number of samples collected so far exceeds the newly configured N events.

If somebody changes sample_type, the type of data recorded in the mmap ring buffer will be a mix of two (or more) different kinds of data, which make it untenable.

Uncommon but possible: somebody may like to change the "type" itself.

The list goes on.

When you proposed generic update perf_event_attr interface, do you have a clear use case in mind with measured performance impact? If so, one can consider that path, which is an entirely different project. I believe your proposal is to introduce a new system call perf_event_update_attr().

The changes proposed in the patch are motivated by a clear use case with a clear performance impact. Changing the address monitored by a breakpoint is a common operation by profilers, and hence it need not go through the whole process of unmapping the ring buffer, closing the fd, re-opening a perf event and remapping the ring buffer.

-Milind

Quoted 17 lines · Peter Zijlstra

include/uapi/linux/perf_event.h struct perf_event_attr { · line 437

Quoted 10 lines · Milind Chabbi
include/uapi/linux/perf_event.h +2 -0
index 362493a..d458214 100644
@@ -433,6 +433,8 @@ struct perf_event_attr {
 #define PERF_EVENT_IOC_ID            _IOR('$', 7, __u64 *)
 #define PERF_EVENT_IOC_SET_BPF               _IOW('$', 8, __u32)
 #define PERF_EVENT_IOC_PAUSE_OUTPUT  _IOW('$', 9, __u32)
+#define PERF_EVENT_IOC_MODIFY_BREAKPOINT \
+                     _IOW('$', 10, struct perf_event_attr *)

I really hate this thing. I would much rather see a more generic update perf_event_attr interface. Where we allow modifying some of the perf_event_attr fields.

#6 lore raw
Quoted 5 lines · Milind Chabbi

Peter,

Generic update perf_event_attr interface is noble but impractical. It will cause a validation nightmare.
Many of the behaviors or choices will become hard to reason.

I don't think you would necessarily need to support modifying all of this. Just define a general interface that could be used to modify these things, but right now it would be only implemented for the special case of breakpoints.

Your ioctl is very near it anyways, just need to change the name and do more sanity checking on the input values.

-Andi

#7 lore raw

I am fine with Andi's suggestion. In summary,

1. I will introduce an ioctl flag _IOC_MODIFY_ATTRIBUTES. (Yes, plural ATTRIBUTES not ATTRIBUTE)
2. Currently, implement only updates to breakpoints and all others will fail with -EOPNOTSUPP.
3. The implementation of breakpoint update shall check the following before modifying:

     (event->attr.type == PERF_TYPE_BREAKPOINT) && (new_attr.type ==

PERF_TYPE_BREAKPOINT)
This ensures that both the passed in fd's event and the new_attr
are PERF_TYPE_BREAKPOINT.

Can we have a consensus on this?

Now the question is what other attribute values to check in the implementation of the breakpoint update.

Do you expect all fields other than the ones that we allow modification remain unchanged from the original creation time? and if anything changes should we fail with -EOPNOTSUPP? I think that is too strict. Expecting them to be zeros can be seen as a change from the original values, hence zero is not the right expectation.
I am open to suggestions here and your help in listing a few attribute fields that need
validation will be valuable.

-Milind

Quoted 16 lines · Andi Kleen
Quoted 5 lines · Milind Chabbi

Peter,

Generic update perf_event_attr interface is noble but impractical. It will cause a validation nightmare.
Many of the behaviors or choices will become hard to reason.

I don't think you would necessarily need to support modifying all of this. Just define a general interface that could be used to modify these things, but right now it would be only implemented for the special case of breakpoints.

Your ioctl is very near it anyways, just need to change the name and do more sanity checking on the input values.

-Andi

maintainer
#8 lore raw
Quoted 1 line · Milind Chabbi

I am fine with Andi's suggestion. In summary,

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

#9 lore raw
Quoted 7 lines · Peter Zijlstra
Quoted 1 line · Milind Chabbi

I am fine with Andi's suggestion. In summary,

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

Point taken Peter. Posted below this time.

#10 lore raw
In reply to Milind Chabbi
Thank you for the patch! Yet something to improve:

Hi Milind,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tip/perf/core]
[also build test ERROR on v4.14-rc8 next-20171108]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Milind-Chabbi/perf-core-fast-breakpoint-modification-via-_IOC_MODIFY_BREAKPOINT/20171108-184959
config: parisc-c3000_defconfig (attached as .config)
compiler: hppa-linux-gnu-gcc (Debian 6.1.1-9) 6.1.1 20160705
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=parisc

All errors (new ones prefixed by >>):

kernel/events/core.o: In function `_perf_ioctl':

>> (.text._perf_ioctl+0x2c8): undefined reference to `validate_hw_breakpoint'

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#11 lore raw
In reply to Milind Chabbi
Thank you for the patch! Yet something to improve:

Hi Milind,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tip/perf/core]
[also build test ERROR on v4.14-rc8 next-20171108]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Milind-Chabbi/perf-core-fast-breakpoint-modification-via-_IOC_MODIFY_BREAKPOINT/20171108-184959
config: arm-multi_v5_defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (Debian 6.1.1-9) 6.1.1 20160705
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=arm

All errors (new ones prefixed by >>):

kernel/events/core.o: In function `_perf_ioctl':

>> core.c:(.text+0xa644): undefined reference to `validate_hw_breakpoint'

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation