Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp307503pxf; Thu, 8 Apr 2021 03:38:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwYmplyXfpopBrGJuJejEpuE+rbGhjOWsNLVpRGBRVBT5jN7dVpy69MOW1NaWWP+BjTc2Qe X-Received: by 2002:a17:90b:1e0f:: with SMTP id pg15mr7801416pjb.51.1617878294020; Thu, 08 Apr 2021 03:38:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617878294; cv=none; d=google.com; s=arc-20160816; b=ElOq4EmepdLAkzZNZP+WD27lqF9p/uGnp2TClr75KFWhPCCgq99WhQ/uV3pzxqSAZW vLY9AF5hYNOIdNL0FVQRpY9UWndCJMrXEYoVhD+atq1lOAqZj2Ooc51YjtACFEuMmjLV uEpJHLSesVvHqxYNsUDUyaWQtXrIVgIYA4EQ8WNz6TXeB1rlcYty9p21WiL4ZTQRraFX wriaEKAasoVx/ECTtjMSiKYIqb4istcSb0eAX38BQOKrEw3arsVXS8ihca2b0nFgQkMY gRNTdUa6iKsPR3WjtI11mgNT0ufCxJYBeCTQZGdumIz7oFHHAs2WOKwZphc0jXhJBq1Y ZreQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=WFLySSLyk1OHGUPgGWpvFgqBO4Z7Oek9XA/23wsFKPA=; b=bdfGVLpUDi+xY4qDTlv6GPVZb1drVncqrV3k0gksWfh3XyRYaV0WLZwlZIFMs4xACz al+VjUUyLLHbzpv2QDaARASXrx2akGfYqhidCTc+vwWfx7mmXzjvhUAlxG3i08+dCauo bqgN5rFRAx5t4fKj1DL77G2dfbOGZuTUlzvYnZjCujb4vT+UWtYaylVW/myAH0whSz6K fIggUspFyV0XoQvnXTF4iuk77rM4oB9TSgIdo8oq2XN3eYasrDqkFrL/lrjX6G6eRsCJ O3s2aZzBLw10Y95n+I8Px0zJeTE+wAUhSZYoorlyjZHScMaUT5+CKrV9jhvIRavnkJOB eNyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ed4EtZeH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t17si26508960plg.272.2021.04.08.03.38.02; Thu, 08 Apr 2021 03:38:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ed4EtZeH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231342AbhDHKha (ORCPT + 99 others); Thu, 8 Apr 2021 06:37:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231187AbhDHKhH (ORCPT ); Thu, 8 Apr 2021 06:37:07 -0400 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92793C0613DB for ; Thu, 8 Apr 2021 03:36:56 -0700 (PDT) Received: by mail-wr1-x44a.google.com with SMTP id s10so802459wre.0 for ; Thu, 08 Apr 2021 03:36:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=WFLySSLyk1OHGUPgGWpvFgqBO4Z7Oek9XA/23wsFKPA=; b=ed4EtZeH+APnBbaShhnDpikA39d84+dcE7IgXMK84L7kEZSQBPgxTO2+LQh5y0qWLz 82c7Fsh+3ZLwQjCKQEME1QA3Rf6/RT8xfrkYjHplUvyo3q0N9WgXSLmqMZzpLnhiqcda +jDiLb/12s1VV/qQlD68zutU5E6w94rhN3r3sguOptzw1MYQdorpvdmicHzbjXgtPQk3 3Gh/UBqKmlqBW8Ar7DUfsaZpFpk1CPkgwBzBHEPqUrli3Xh0PwGq/pQT+qNL6Rw9/3D5 snAy2QmgPXPX911JiFcSyUu5Aj+bAaehI4+5XKXStQHEQKGWUsTDAfM+ZgWlihrdJKqW 2F6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=WFLySSLyk1OHGUPgGWpvFgqBO4Z7Oek9XA/23wsFKPA=; b=F2SH8A5fO/abVLiYMeW+RnE8AZayZtkC1qZqZnNuj8xVU9jhFm+62Eb8zj4TsPUObd GiZwSzwYrS5JH9REb0SQlmFkbBwfuUp8+IcDQPGqiHRBXoK5O1RMle8tGJmeuAOUXBqS UJEQKuBEZeyAVDSEkc/0giTU5beZzXr98vRiG9nCzx0ebm4BspQZEi8uLj7r2G61Qbpq wt9UM+VkMp3y0+qVmgT7pri6xBaJAKinCwLvoZ61jD0ygdvgSAVRYL9SUD8mFeDTUkZu 0sLHucRazm0bxquBnMja0eSIvErfG6TrznFGahcd4I/WkKxSYhfetqlxHYvG8mhjBLk+ Od8g== X-Gm-Message-State: AOAM533ZeTRWY0n+QTfSB6nLq7b41BTA1DTOtD2DPk8suaHRpA2mYLMP dz7UZq4Fbf7VJNUm+uhTG8f2Y01DsA== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:9038:bbd3:4a12:abda]) (user=elver job=sendgmr) by 2002:a1c:9a02:: with SMTP id c2mr7815998wme.131.1617878215147; Thu, 08 Apr 2021 03:36:55 -0700 (PDT) Date: Thu, 8 Apr 2021 12:35:59 +0200 In-Reply-To: <20210408103605.1676875-1-elver@google.com> Message-Id: <20210408103605.1676875-5-elver@google.com> Mime-Version: 1.0 References: <20210408103605.1676875-1-elver@google.com> X-Mailer: git-send-email 2.31.0.208.g409f899ff0-goog Subject: [PATCH v4 04/10] perf: Add support for event removal on exec From: Marco Elver To: elver@google.com, peterz@infradead.org, alexander.shishkin@linux.intel.com, acme@kernel.org, mingo@redhat.com, jolsa@redhat.com, mark.rutland@arm.com, namhyung@kernel.org, tglx@linutronix.de Cc: glider@google.com, viro@zeniv.linux.org.uk, arnd@arndb.de, christian@brauner.io, dvyukov@google.com, jannh@google.com, axboe@kernel.dk, mascasa@google.com, pcc@google.com, irogers@google.com, oleg@redhat.com, kasan-dev@googlegroups.com, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: Marco Elver --- v3: * Rework based on Peter's "perf: Rework perf_event_exit_event()" added to the beginning of the series. Intermediate attempts between v2 and this v3 can be found here: https://lkml.kernel.org/r/YFm6aakSRlF2nWtu@elver.google.com v2: * Add patch to series. --- include/uapi/linux/perf_event.h | 3 +- kernel/events/core.c | 70 +++++++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 813efb65fea8..8c5b9f5ad63f 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -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 */ diff --git a/kernel/events/core.c b/kernel/events/core.c index de2917b3c59e..19c045ff2b9c 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -4247,6 +4247,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; @@ -7559,18 +7610,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 { @@ -11652,6 +11703,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; -- 2.31.0.208.g409f899ff0-goog