From: Adrian Hunter <adrian.hunter@intel.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
Ian Rogers <irogers@google.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH RFC 5/5] perf: Add mmap to the sideband ioctl
Date: Fri, 14 Apr 2023 11:23:00 +0300 [thread overview]
Message-ID: <20230414082300.34798-6-adrian.hunter@intel.com> (raw)
In-Reply-To: <20230414082300.34798-1-adrian.hunter@intel.com>
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.
Set the mmap pid/tid from the appropriate task.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
kernel/events/core.c | 91 +++++++++++++++++++++++++++++++++++---------
1 file changed, 73 insertions(+), 18 deletions(-)
diff --git a/kernel/events/core.c b/kernel/events/core.c
index cddc02c2e411..317bdf5f919a 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -8584,6 +8584,7 @@ static void perf_event_cgroup(struct cgroup *cgrp)
struct perf_mmap_event {
struct vm_area_struct *vma;
+ struct task_struct *task;
const char *file_name;
int file_size;
@@ -8605,19 +8606,25 @@ struct perf_mmap_event {
} event_id;
};
+static int perf_event_mmap_match_vma(struct perf_event *event,
+ struct vm_area_struct *vma)
+{
+ int executable = vma->vm_flags & VM_EXEC;
+
+ return (!executable && event->attr.mmap_data) ||
+ (executable && (event->attr.mmap || event->attr.mmap2));
+}
+
static int perf_event_mmap_match(struct perf_event *event,
void *data)
{
struct perf_mmap_event *mmap_event = data;
struct vm_area_struct *vma = mmap_event->vma;
- int executable = vma->vm_flags & VM_EXEC;
- return (!executable && event->attr.mmap_data) ||
- (executable && (event->attr.mmap || event->attr.mmap2));
+ return perf_event_mmap_match_vma(event, vma);
}
-static void perf_event_mmap_output(struct perf_event *event,
- void *data)
+static int perf_event_mmap_output(struct perf_event *event, void *data)
{
struct perf_mmap_event *mmap_event = data;
struct perf_output_handle handle;
@@ -8628,7 +8635,7 @@ static void perf_event_mmap_output(struct perf_event *event,
int ret;
if (!perf_event_mmap_match(event, data))
- return;
+ return -ENOENT;
if (event->attr.mmap2) {
mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
@@ -8646,8 +8653,8 @@ static void perf_event_mmap_output(struct perf_event *event,
if (ret)
goto out;
- mmap_event->event_id.pid = perf_event_pid(event, current);
- mmap_event->event_id.tid = perf_event_tid(event, current);
+ mmap_event->event_id.pid = perf_event_pid(event, mmap_event->task);
+ mmap_event->event_id.tid = perf_event_tid(event, mmap_event->task);
use_build_id = event->attr.build_id && mmap_event->build_id_size;
@@ -8681,9 +8688,10 @@ static void perf_event_mmap_output(struct perf_event *event,
out:
mmap_event->event_id.header.size = size;
mmap_event->event_id.header.type = type;
+ return ret;
}
-static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
+static int perf_event_mmap_event(struct perf_mmap_event *mmap_event, struct perf_event *event)
{
struct vm_area_struct *vma = mmap_event->vma;
struct file *file = vma->vm_file;
@@ -8694,6 +8702,7 @@ static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
char tmp[16];
char *buf = NULL;
char *name;
+ int ret;
if (vma->vm_flags & VM_READ)
prot |= PROT_READ;
@@ -8795,11 +8804,10 @@ static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
if (atomic_read(&nr_build_id_events))
build_id_parse(vma, mmap_event->build_id, &mmap_event->build_id_size);
- perf_iterate_sb(perf_event_mmap_output,
- mmap_event,
- NULL);
+ ret = perf_output_sb(perf_event_mmap_output, mmap_event, NULL, event);
kfree(buf);
+ return ret;
}
/*
@@ -8899,21 +8907,25 @@ static void perf_addr_filters_adjust(struct vm_area_struct *vma)
rcu_read_unlock();
}
-void perf_event_mmap(struct vm_area_struct *vma)
+static int __perf_event_mmap(struct vm_area_struct *vma,
+ struct perf_event *event,
+ struct task_struct *task)
{
struct perf_mmap_event mmap_event;
if (!atomic_read(&nr_mmap_events))
- return;
+ return -ENOENT;
mmap_event = (struct perf_mmap_event){
.vma = vma,
+ .task = task ?: current,
/* .file_name */
/* .file_size */
.event_id = {
.header = {
.type = PERF_RECORD_MMAP,
- .misc = PERF_RECORD_MISC_USER,
+ .misc = PERF_RECORD_MISC_USER |
+ (event ? PERF_RECORD_MISC_STATUS_ONLY : 0),
/* .size */
},
/* .pid */
@@ -8930,8 +8942,14 @@ void perf_event_mmap(struct vm_area_struct *vma)
/* .flags (attr_mmap2 only) */
};
- perf_addr_filters_adjust(vma);
- perf_event_mmap_event(&mmap_event);
+ if (!event)
+ perf_addr_filters_adjust(vma);
+ return perf_event_mmap_event(&mmap_event, event);
+}
+
+void perf_event_mmap(struct vm_area_struct *vma)
+{
+ __perf_event_mmap(vma, NULL, NULL);
}
void perf_event_aux_event(struct perf_event *event, unsigned long head,
@@ -12901,9 +12919,46 @@ static int perf_event_emit_comm(struct perf_event *event, struct task_struct *ta
return __perf_event_comm(task, false, event);
}
+static int perf_event_mm_emit_mmap(struct perf_event *event,
+ struct task_struct *task,
+ struct mm_struct *mm)
+{
+ struct vm_area_struct *vma;
+ VMA_ITERATOR(vmi, mm, 0);
+ int err;
+
+ for_each_vma(vmi, vma) {
+ if (!perf_event_mmap_match_vma(event, vma))
+ continue;
+ err = __perf_event_mmap(vma, event, task);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
static int perf_event_emit_mmap(struct perf_event *event, struct task_struct *task)
{
- return -EINVAL;
+ struct mm_struct *mm;
+ int err;
+
+ if (!event->attr.mmap_data && !event->attr.mmap && !event->attr.mmap2)
+ return -EINVAL;
+
+ mm = get_task_mm(task);
+ if (!mm)
+ return 0;
+
+ mmap_read_lock(mm);
+
+ err = perf_event_mm_emit_mmap(event, task, mm);
+
+ mmap_read_unlock(mm);
+
+ mmput(mm);
+
+ return err;
}
static int perf_event_emit_sideband(struct perf_event *event, void __user *arg)
--
2.34.1
next prev parent reply other threads:[~2023-04-14 8:24 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-14 8:22 [PATCH RFC 0/5] perf: Add ioctl to emit sideband events Adrian Hunter
2023-04-14 8:22 ` [PATCH RFC 1/5] " Adrian Hunter
2023-04-17 10:57 ` Peter Zijlstra
2023-04-18 6:29 ` Adrian Hunter
2023-04-14 8:22 ` [PATCH RFC 2/5] perf: Add fork to the sideband ioctl Adrian Hunter
2023-04-14 8:22 ` [PATCH RFC 3/5] perf: Add namespaces " Adrian Hunter
2023-04-14 8:22 ` [PATCH RFC 4/5] perf: Add comm " Adrian Hunter
2023-04-14 8:23 ` Adrian Hunter [this message]
2023-04-17 11:02 ` [PATCH RFC 0/5] perf: Add ioctl to emit sideband events Peter Zijlstra
2023-04-17 16:37 ` Ian Rogers
2023-04-18 7:03 ` Adrian Hunter
2023-04-18 6:18 ` Adrian Hunter
2023-04-18 13:36 ` Adrian Hunter
2023-04-18 15:51 ` Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230414082300.34798-6-adrian.hunter@intel.com \
--to=adrian.hunter@intel.com \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).