Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp144165pxf; Wed, 10 Mar 2021 02:43:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJxb7B0tK6s7nMuACJr3pLL2OLtyiGSn6dg9BgQJZubEUwYRXc78vUYLwAB4xqrhLroLG6uh X-Received: by 2002:aa7:c5c4:: with SMTP id h4mr2373130eds.375.1615373034289; Wed, 10 Mar 2021 02:43:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615373034; cv=none; d=google.com; s=arc-20160816; b=rBVFC6bORY0Mu9Qsrjua1RXaHSXI6YNcHwZFiappod3dQvD/byFTaO6lVLSldEcqrU peOcFSE/jKUQuN0MYMWj7Hs4N+vRgXh2ThhPCyDDS9pS6wicjPjuSPDTpd2A+SLzVVUt 5XEysdwAosHrMjGVwGvBUeynwfadq9XFHlv4mGP0YbM2ypKSKhduZwHQsupI3I7Iiq78 O2eyLTpWuNWsXGxEK/9ugcVO3zYjj1CNZ9jh0r3eiH+OV5BlHAdEh4dPl9fyJT4CzKOB tILRjoqzcRTCmqRyIdaPiEmiEEvlU7DbPKx5fH34vcEI5r4zce9Yqy5DKWbSfSO2K+6s Gxtg== 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=LX0emKbgsHYtGpYyL1x1FnLQ5ewlS0gEm+vlZ3nascQ=; b=mSIXKzlP13ZQ6RFqJWtw/KvoFDbHa3JNTvtvh7UgJdM9c44w7AM4X6FUmxghWHMOrd R9IvjoLKm+TrV5e0oF6PTnDcalZ5MesncnryvdQpwXIdjYBwH5PhwmGYUmVAbRS25lFN s3xaTwfAhvIbfsRPgODObqJ/WFoJJaEpw7A/pj6t3fjUuLzau/7Pym8wZSnJJaydJyrU EfWZpOQUG4J1OqIuKMocuOeQyoZBa4wSC1vrhKHxxDSnoArRPQaeSjQimu82saH1pP0I ymQk4/CEE98Q4ojKHqBMxGqalP5ClzdJ3v6Ip0YsTtjCvRsJC0RxAAInrFZKcKFocZXC q5wA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="N/u3p5qk"; 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 l1si11452174ejh.597.2021.03.10.02.43.32; Wed, 10 Mar 2021 02:43:54 -0800 (PST) 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="N/u3p5qk"; 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 S232623AbhCJKm1 (ORCPT + 99 others); Wed, 10 Mar 2021 05:42:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232544AbhCJKl5 (ORCPT ); Wed, 10 Mar 2021 05:41:57 -0500 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1AB2C06174A for ; Wed, 10 Mar 2021 02:41:56 -0800 (PST) Received: by mail-qk1-x749.google.com with SMTP id g18so12407934qki.15 for ; Wed, 10 Mar 2021 02:41:56 -0800 (PST) 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=LX0emKbgsHYtGpYyL1x1FnLQ5ewlS0gEm+vlZ3nascQ=; b=N/u3p5qkgkVCIorblwjlo0ALkDKW6OemYeWX+fR+AT8w6i0Gh/XvradEyaRC0Nmutk j3AGpbLlTACkVr5oi/De9Cf9L/VxHIbruSwuU7HfD6sKWBig/HP45lCrnKDX1aRSOhp6 ZgdsDxtOtDhrvcdiYBVzGE0Yz/2W3v0FmAEpbD1N+n6ggnT8qnl47gG7JmqqPv3PrAko qGwATz9jmqBUcMVHnIzIDgnkvckIW8Sp98YOu5u5o5EwKc4jVHB3aVNz+K9uwfv9Y1rQ WjLGhhIrjQcVepjxDJ+LjtV/n+fiN6L3yTzeRv74Ev9+ZDMb8KBoXkY739owMIXjSb+n Ursw== 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=LX0emKbgsHYtGpYyL1x1FnLQ5ewlS0gEm+vlZ3nascQ=; b=GrOKhmIjLij4poItAxYqtMtNw/4CfrBGz9nIVaGKCrch7x4NSP50dPkJ7eFRSQ2PlK d89/UX/ryCO4J+/4NLYP0KnIQbUBlXe1+WmF+i0B3A8LwgqNUrwFBRKe3Y7R1iO5ZyCM q/oBNpk1ws0J7OU9geoHDCX3MoTNnbzDuHiInGq7mB+iFBbxUjutKySSsh+7Yuu4sjGq PMpK7sy/8Pva0nxQxECjJBID1B6SkFzZ/hUmCRqUSsgbLqpGHRRHHngcxtSS6gr9q8EO puDxSeIVBvwIcNkj+ajqgnzmLiACklS85XPgiw9AsGPPADK7rHFf+4F7zlGdCslyKBQX VZ/g== X-Gm-Message-State: AOAM530/CZnNZbr9a64i1zoSdMPiGJsVfaHhta4JF6DQBjNzAzEPEvNv vNVUI2gjzpJBhX3b7JmGDqQEha2uBQ== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:e995:ac0b:b57c:49a4]) (user=elver job=sendgmr) by 2002:ad4:4d82:: with SMTP id cv2mr2356657qvb.6.1615372915311; Wed, 10 Mar 2021 02:41:55 -0800 (PST) Date: Wed, 10 Mar 2021 11:41:33 +0100 In-Reply-To: <20210310104139.679618-1-elver@google.com> Message-Id: <20210310104139.679618-3-elver@google.com> Mime-Version: 1.0 References: <20210310104139.679618-1-elver@google.com> X-Mailer: git-send-email 2.30.1.766.gb4fecdf3b7-goog Subject: [PATCH RFC v2 2/8] perf/core: Support only inheriting events if cloned with CLONE_THREAD 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, 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::inherit_thread, to restricting inheriting events only if the child was cloned with CLONE_THREAD. This option supports the case where an event is supposed to be process-wide only (including subthreads), but should not propagate beyond the current process's shared environment. Link: https://lore.kernel.org/lkml/YBvj6eJR%2FDY2TsEB@hirez.programming.kicks-ass.net/ Suggested-by: Peter Zijlstra Signed-off-by: Marco Elver --- v2: * Add patch to series. --- include/linux/perf_event.h | 5 +++-- include/uapi/linux/perf_event.h | 3 ++- kernel/events/core.c | 21 ++++++++++++++------- kernel/fork.c | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index fab42cfbd350..982ad61c653a 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -955,7 +955,7 @@ extern void __perf_event_task_sched_in(struct task_struct *prev, struct task_struct *task); extern void __perf_event_task_sched_out(struct task_struct *prev, struct task_struct *next); -extern int perf_event_init_task(struct task_struct *child); +extern int perf_event_init_task(struct task_struct *child, u64 clone_flags); extern void perf_event_exit_task(struct task_struct *child); extern void perf_event_free_task(struct task_struct *task); extern void perf_event_delayed_put(struct task_struct *task); @@ -1446,7 +1446,8 @@ perf_event_task_sched_in(struct task_struct *prev, static inline void perf_event_task_sched_out(struct task_struct *prev, struct task_struct *next) { } -static inline int perf_event_init_task(struct task_struct *child) { return 0; } +static inline int perf_event_init_task(struct task_struct *child, + u64 clone_flags) { return 0; } static inline void perf_event_exit_task(struct task_struct *child) { } static inline void perf_event_free_task(struct task_struct *task) { } static inline void perf_event_delayed_put(struct task_struct *task) { } diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index ad15e40d7f5d..813efb65fea8 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -389,7 +389,8 @@ struct perf_event_attr { cgroup : 1, /* include cgroup events */ text_poke : 1, /* include text poke events */ build_id : 1, /* use build id in mmap2 events */ - __reserved_1 : 29; + inherit_thread : 1, /* children only inherit if cloned with CLONE_THREAD */ + __reserved_1 : 28; union { __u32 wakeup_events; /* wakeup every n events */ diff --git a/kernel/events/core.c b/kernel/events/core.c index bff498766065..a8382e6c907c 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -11597,6 +11597,9 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr, (attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) return -EINVAL; + if (!attr->inherit && attr->inherit_thread) + return -EINVAL; + out: return ret; @@ -12820,12 +12823,13 @@ static int inherit_task_group(struct perf_event *event, struct task_struct *parent, struct perf_event_context *parent_ctx, struct task_struct *child, int ctxn, - int *inherited_all) + u64 clone_flags, int *inherited_all) { int ret; struct perf_event_context *child_ctx; - if (!event->attr.inherit) { + if (!event->attr.inherit || + (event->attr.inherit_thread && !(clone_flags & CLONE_THREAD))) { *inherited_all = 0; return 0; } @@ -12857,7 +12861,8 @@ inherit_task_group(struct perf_event *event, struct task_struct *parent, /* * Initialize the perf_event context in task_struct */ -static int perf_event_init_context(struct task_struct *child, int ctxn) +static int perf_event_init_context(struct task_struct *child, int ctxn, + u64 clone_flags) { struct perf_event_context *child_ctx, *parent_ctx; struct perf_event_context *cloned_ctx; @@ -12897,7 +12902,8 @@ static int perf_event_init_context(struct task_struct *child, int ctxn) */ perf_event_groups_for_each(event, &parent_ctx->pinned_groups) { ret = inherit_task_group(event, parent, parent_ctx, - child, ctxn, &inherited_all); + child, ctxn, clone_flags, + &inherited_all); if (ret) goto out_unlock; } @@ -12913,7 +12919,8 @@ static int perf_event_init_context(struct task_struct *child, int ctxn) perf_event_groups_for_each(event, &parent_ctx->flexible_groups) { ret = inherit_task_group(event, parent, parent_ctx, - child, ctxn, &inherited_all); + child, ctxn, clone_flags, + &inherited_all); if (ret) goto out_unlock; } @@ -12955,7 +12962,7 @@ static int perf_event_init_context(struct task_struct *child, int ctxn) /* * Initialize the perf_event context in task_struct */ -int perf_event_init_task(struct task_struct *child) +int perf_event_init_task(struct task_struct *child, u64 clone_flags) { int ctxn, ret; @@ -12964,7 +12971,7 @@ int perf_event_init_task(struct task_struct *child) INIT_LIST_HEAD(&child->perf_event_list); for_each_task_context_nr(ctxn) { - ret = perf_event_init_context(child, ctxn); + ret = perf_event_init_context(child, ctxn, clone_flags); if (ret) { perf_event_free_task(child); return ret; diff --git a/kernel/fork.c b/kernel/fork.c index d3171e8e88e5..d090366d1206 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2070,7 +2070,7 @@ static __latent_entropy struct task_struct *copy_process( if (retval) goto bad_fork_cleanup_policy; - retval = perf_event_init_task(p); + retval = perf_event_init_task(p, clone_flags); if (retval) goto bad_fork_cleanup_policy; retval = audit_alloc(p); -- 2.30.1.766.gb4fecdf3b7-goog