Skip to content

Commit

Permalink
perf: Add support for event removal on exec
Browse files Browse the repository at this point in the history
Adds bit perf_event_attr::remove_on_exec, to support removing an event
from a task on exec.

This option supports the case where an event is supposed to be
process-wide only, and should not propagate beyond exec, to limit
monitoring to the original process image only.

Suggested-by: Peter Zijlstra <[email protected]>
Signed-off-by: Marco Elver <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
  • Loading branch information
melver authored and Peter Zijlstra committed Apr 16, 2021
1 parent 2b26f0a commit 2e498d0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/uapi/linux/perf_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ struct perf_event_attr {
text_poke : 1, /* include text poke events */
build_id : 1, /* use build id in mmap2 events */
inherit_thread : 1, /* children only inherit if cloned with CLONE_THREAD */
__reserved_1 : 28;
remove_on_exec : 1, /* event is removed from task on exec */
__reserved_1 : 27;

union {
__u32 wakeup_events; /* wakeup every n events */
Expand Down
70 changes: 62 additions & 8 deletions kernel/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -4248,6 +4248,57 @@ static void perf_event_enable_on_exec(int ctxn)
put_ctx(clone_ctx);
}

static void perf_remove_from_owner(struct perf_event *event);
static void perf_event_exit_event(struct perf_event *event,
struct perf_event_context *ctx);

/*
* Removes all events from the current task that have been marked
* remove-on-exec, and feeds their values back to parent events.
*/
static void perf_event_remove_on_exec(int ctxn)
{
struct perf_event_context *ctx, *clone_ctx = NULL;
struct perf_event *event, *next;
LIST_HEAD(free_list);
unsigned long flags;
bool modified = false;

ctx = perf_pin_task_context(current, ctxn);
if (!ctx)
return;

mutex_lock(&ctx->mutex);

if (WARN_ON_ONCE(ctx->task != current))
goto unlock;

list_for_each_entry_safe(event, next, &ctx->event_list, event_entry) {
if (!event->attr.remove_on_exec)
continue;

if (!is_kernel_event(event))
perf_remove_from_owner(event);

modified = true;

perf_event_exit_event(event, ctx);
}

raw_spin_lock_irqsave(&ctx->lock, flags);
if (modified)
clone_ctx = unclone_ctx(ctx);
--ctx->pin_count;
raw_spin_unlock_irqrestore(&ctx->lock, flags);

unlock:
mutex_unlock(&ctx->mutex);

put_ctx(ctx);
if (clone_ctx)
put_ctx(clone_ctx);
}

struct perf_read_data {
struct perf_event *event;
bool group;
Expand Down Expand Up @@ -7560,18 +7611,18 @@ void perf_event_exec(void)
struct perf_event_context *ctx;
int ctxn;

rcu_read_lock();
for_each_task_context_nr(ctxn) {
ctx = current->perf_event_ctxp[ctxn];
if (!ctx)
continue;

perf_event_enable_on_exec(ctxn);
perf_event_remove_on_exec(ctxn);

perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
true);
rcu_read_lock();
ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
if (ctx) {
perf_iterate_ctx(ctx, perf_event_addr_filters_exec,
NULL, true);
}
rcu_read_unlock();
}
rcu_read_unlock();
}

struct remote_output {
Expand Down Expand Up @@ -11656,6 +11707,9 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
if (!attr->inherit && attr->inherit_thread)
return -EINVAL;

if (attr->remove_on_exec && attr->enable_on_exec)
return -EINVAL;

out:
return ret;

Expand Down

0 comments on commit 2e498d0

Please sign in to comment.