[PATCH RFC 4/5] perf: Add comm to the sideband ioctl

From: Adrian Hunter
Date: Fri Apr 14 2023 - 04:24:01 EST


Support the case of output to an active event, and return an error if
output is not possible in that case. Set PERF_RECORD_MISC_STATUS_ONLY to
differentiate the ioctl status-only sideband event from a "real" sideband
event.

Signed-off-by: Adrian Hunter <adrian.hunter@xxxxxxxxx>
---
kernel/events/core.c | 31 +++++++++++++++++++------------
1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index ed4af231853a..cddc02c2e411 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -8265,8 +8265,7 @@ static int perf_event_comm_match(struct perf_event *event)
return event->attr.comm;
}

-static void perf_event_comm_output(struct perf_event *event,
- void *data)
+static int perf_event_comm_output(struct perf_event *event, void *data)
{
struct perf_comm_event *comm_event = data;
struct perf_output_handle handle;
@@ -8275,7 +8274,7 @@ static void perf_event_comm_output(struct perf_event *event,
int ret;

if (!perf_event_comm_match(event))
- return;
+ return -ENOENT;

perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
ret = perf_output_begin(&handle, &sample, event,
@@ -8296,9 +8295,10 @@ static void perf_event_comm_output(struct perf_event *event,
perf_output_end(&handle);
out:
comm_event->event_id.header.size = size;
+ return ret;
}

-static void perf_event_comm_event(struct perf_comm_event *comm_event)
+static int perf_event_comm_event(struct perf_comm_event *comm_event, struct perf_event *event)
{
char comm[TASK_COMM_LEN];
unsigned int size;
@@ -8312,17 +8312,15 @@ static void perf_event_comm_event(struct perf_comm_event *comm_event)

comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;

- perf_iterate_sb(perf_event_comm_output,
- comm_event,
- NULL);
+ return perf_output_sb(perf_event_comm_output, comm_event, NULL, event);
}

-void perf_event_comm(struct task_struct *task, bool exec)
+static int __perf_event_comm(struct task_struct *task, bool exec, struct perf_event *event)
{
struct perf_comm_event comm_event;

if (!atomic_read(&nr_comm_events))
- return;
+ return -ENOENT;

comm_event = (struct perf_comm_event){
.task = task,
@@ -8331,7 +8329,8 @@ void perf_event_comm(struct task_struct *task, bool exec)
.event_id = {
.header = {
.type = PERF_RECORD_COMM,
- .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
+ .misc = (exec ? PERF_RECORD_MISC_COMM_EXEC : 0) |
+ (event ? PERF_RECORD_MISC_STATUS_ONLY : 0),
/* .size */
},
/* .pid */
@@ -8339,7 +8338,12 @@ void perf_event_comm(struct task_struct *task, bool exec)
},
};

- perf_event_comm_event(&comm_event);
+ return perf_event_comm_event(&comm_event, event);
+}
+
+void perf_event_comm(struct task_struct *task, bool exec)
+{
+ __perf_event_comm(task, exec, NULL);
}

/*
@@ -12891,7 +12895,10 @@ static int perf_event_emit_namespaces(struct perf_event *event, struct task_stru

static int perf_event_emit_comm(struct perf_event *event, struct task_struct *task)
{
- return -EINVAL;
+ if (!event->attr.comm)
+ return -EINVAL;
+
+ return __perf_event_comm(task, false, event);
}

static int perf_event_emit_mmap(struct perf_event *event, struct task_struct *task)
--
2.34.1