Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1109436lqp; Fri, 22 Mar 2024 06:04:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWmTI0SOfJJlYY2Fmvu5n5RL7q1jLCsm6ROWqXs4Z8t3+r0Y1MMsUoX0VgKdO6+/01J8laC5dUhwvuoCzkulRSt5ocKXgiG3mYwJ5Tywg== X-Google-Smtp-Source: AGHT+IFiTRbm+65kVKM4wUGcUCbKWjSnBv73SqGhDJIzba5BuqjvO7Jn+0Ev4pGXaMihOI+7Ak6v X-Received: by 2002:a0d:edc1:0:b0:610:e424:4dc2 with SMTP id w184-20020a0dedc1000000b00610e4244dc2mr1020101ywe.3.1711112692794; Fri, 22 Mar 2024 06:04:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711112692; cv=pass; d=google.com; s=arc-20160816; b=WPublByILXoXOE1uSaoy1h9gyKFo6pHKwkzebFE6VH6IMxtFOX3ntgsTY/PzaBMTsy ma4v20u4jO0DLMhjovOaSHosuA2jgHV+8TcApU8gVNuyCO9ERQI2ojJvEGOe/A5QuAx7 3Vs29QC6G70WXhU/qQ2xl4SRoWN+FwZ4SK78PprigctqZylf53dcfWX/foVWJwNDGPXE WlFfVpmsBZ2XK4G2KzNGkrTb/4SxIiiQ71c1IZAQT6RDyuzh4Wf9egAm11IF11VKBn3M 2w1TGew0SStvN5drY5cWV6SYXcqIRZRiwlMdQpLTKUlRXebKDZrHdW6u4Gh1oYbs9E6z +xNQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=odSLnu7xUxWBor7ynbXB0TYJ9j35wk7IeT4HPsgSXKA=; fh=qmyFNe8t+IUXUkrxfdZt+VGwiwY4q6x1nzoI5t8Y/1U=; b=woUw24RkeyPXYo4FU6b57V0bbdt9+5bRGddtorqEXySu+D5sU+8dVD+W2j6BUN2Bva /ggEcEPadXQWEO3q6RbQNFhxsZLW//MUlPwc06hECrMjfkCUV+nXvR5nQdK56RNoY5re nkCnL3uNM+P/ntPkxpRQHJNAL97+vKMskh2NcqhybyEPHiaVGIyUUM/aj66Qjg7zNc80 daY6cSrd8x6hEBIJyDlx/8tpN6Rt+wYraSjPgxVuObgRowWkxLm70355kjU9zKMxyToN FyJDKa4pDd3oB1IAZ7WrAUmhvS28NeZPJqE4drUe0P9jSeefvD3lmZgOZtSCXuPDyRqN vqGw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-111450-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111450-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id w10-20020a81a20a000000b00610e7249907si815530ywg.227.2024.03.22.06.04.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 06:04:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111450-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-111450-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111450-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 725561C208E5 for ; Fri, 22 Mar 2024 13:04:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9034C46439; Fri, 22 Mar 2024 13:04:35 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BD3AC4438E; Fri, 22 Mar 2024 13:04:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711112674; cv=none; b=uqVGPAoCjdgPYhb1ICVswbQ/E3jBbv5yEK6PJ99mj0vPEH7nO4eAIxhyT9TvD2pEhk4bBt7HVJ/5/0Pae5SxSfgkzhHzdwJ61VOSDE6m4lh/o2XfBHII4s7TuNfridU6eGFThqD52yWRaSQ2qnHpaOqKNqWSSNmFeR4hHmpVXpU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711112674; c=relaxed/simple; bh=EK8nvo4TRweQgyliYjiBd+AvfZpo6ofH+nQNVcwGxD8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VTkrbOTfIhexpMjB1l0q8Uk70I+ZLxY2SEzAdZvjTA1aQMJjb7PxEp0y1IqTOVPcZLxnnbdn0JLZMnuivML+lXct/wmL9YR1CIce0/K6jcf/XUu1BW3KJrhC0xVE71gx5SdKS7UpN1+vwHkXUff4wLBKHvj5UMubx4JTH9VkqWw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 14FD61042; Fri, 22 Mar 2024 06:05:06 -0700 (PDT) Received: from e126817.cambridge.arm.com (e126817.cambridge.arm.com [10.2.3.5]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 3831D3F762; Fri, 22 Mar 2024 06:04:30 -0700 (PDT) From: Ben Gainey To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org Cc: james.clark@arm.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gainey Subject: [PATCH v3 1/4] perf: Support PERF_SAMPLE_READ with inherit_stat Date: Fri, 22 Mar 2024 13:04:11 +0000 Message-ID: <20240322130414.70186-2-ben.gainey@arm.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240322130414.70186-1-ben.gainey@arm.com> References: <20240322130414.70186-1-ben.gainey@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This change allows events to use PERF_SAMPLE READ with inherit so long as both inherit_stat and PERF_SAMPLE_TID are set. In this configuration, an event will be inherited into any child processes / threads, allowing convenient profiling of a multiprocess or multithreaded application, whilst allowing profiling tools to collect per-thread samples, in particular of groups of counters. Signed-off-by: Ben Gainey --- include/linux/perf_event.h | 1 + kernel/events/core.c | 62 +++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index d2a15c0c6f8a..7d405dff6694 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -932,6 +932,7 @@ struct perf_event_context { int nr_task_data; int nr_stat; + int nr_stat_read; int nr_freq; int rotate_disable; diff --git a/kernel/events/core.c b/kernel/events/core.c index f0f0f71213a1..870a7dbd8d5f 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1767,6 +1767,18 @@ perf_event_groups_next(struct perf_event *event, struct pmu *pmu) event = rb_entry_safe(rb_next(&event->group_node), \ typeof(*event), group_node)) +/* + * Does the event attribute request inherit with PERF_SAMPLE_READ + */ +#define perf_attr_has_inherit_and_sample_read(attr) \ + ((attr)->inherit && ((attr)->sample_type & PERF_SAMPLE_READ)) + +/* + * Does the event request an attribte that requests inherit with PERF_SAMPLE_READ + */ +#define perf_event_has_inherit_and_sample_read(event) \ + perf_attr_has_inherit_and_sample_read(&((event)->attr)) + /* * Add an event from the lists for its context. * Must be called with ctx->mutex and ctx->lock held. @@ -1795,8 +1807,11 @@ list_add_event(struct perf_event *event, struct perf_event_context *ctx) ctx->nr_events++; if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT) ctx->nr_user++; - if (event->attr.inherit_stat) + if (event->attr.inherit_stat) { ctx->nr_stat++; + if (perf_event_has_inherit_and_sample_read(event)) + ctx->nr_stat_read++; + } if (event->state > PERF_EVENT_STATE_OFF) perf_cgroup_event_enable(event, ctx); @@ -2019,8 +2034,11 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx) ctx->nr_events--; if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT) ctx->nr_user--; - if (event->attr.inherit_stat) + if (event->attr.inherit_stat) { ctx->nr_stat--; + if (perf_event_has_inherit_and_sample_read(event)) + ctx->nr_stat_read--; + } list_del_rcu(&event->event_entry); @@ -3529,11 +3547,19 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) perf_ctx_disable(ctx, false); /* PMIs are disabled; ctx->nr_pending is stable. */ - if (local_read(&ctx->nr_pending) || + if (ctx->nr_stat_read || + next_ctx->nr_stat_read || + local_read(&ctx->nr_pending) || local_read(&next_ctx->nr_pending)) { /* * Must not swap out ctx when there's pending * events that rely on the ctx->task relation. + * + * Likewise, when a context contains inherit + + * inherit_stat + SAMPLE_READ events they + * should be switched out using the slow path + * so that they are treated as if they were + * distinct contexts. */ raw_spin_unlock(&next_ctx->lock); rcu_read_unlock(); @@ -4533,8 +4559,11 @@ static void __perf_event_read(void *info) raw_spin_unlock(&ctx->lock); } -static inline u64 perf_event_count(struct perf_event *event) +static inline u64 perf_event_count(struct perf_event *event, bool self_value_only) { + if (self_value_only && perf_event_has_inherit_and_sample_read(event)) + return local64_read(&event->count); + return local64_read(&event->count) + atomic64_read(&event->child_count); } @@ -5454,7 +5483,7 @@ static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 * mutex_lock(&event->child_mutex); (void)perf_event_read(event, false); - total += perf_event_count(event); + total += perf_event_count(event, false); *enabled += event->total_time_enabled + atomic64_read(&event->child_total_time_enabled); @@ -5463,7 +5492,7 @@ static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 * list_for_each_entry(child, &event->child_list, child_list) { (void)perf_event_read(child, false); - total += perf_event_count(child); + total += perf_event_count(child, false); *enabled += child->total_time_enabled; *running += child->total_time_running; } @@ -5545,14 +5574,14 @@ static int __perf_read_group_add(struct perf_event *leader, /* * Write {count,id} tuples for every sibling. */ - values[n++] += perf_event_count(leader); + values[n++] += perf_event_count(leader, false); if (read_format & PERF_FORMAT_ID) values[n++] = primary_event_id(leader); if (read_format & PERF_FORMAT_LOST) values[n++] = atomic64_read(&leader->lost_samples); for_each_sibling_event(sub, leader) { - values[n++] += perf_event_count(sub); + values[n++] += perf_event_count(sub, false); if (read_format & PERF_FORMAT_ID) values[n++] = primary_event_id(sub); if (read_format & PERF_FORMAT_LOST) @@ -6132,7 +6161,7 @@ void perf_event_update_userpage(struct perf_event *event) ++userpg->lock; barrier(); userpg->index = perf_event_index(event); - userpg->offset = perf_event_count(event); + userpg->offset = perf_event_count(event, false); if (userpg->index) userpg->offset -= local64_read(&event->hw.prev_count); @@ -7200,7 +7229,7 @@ static void perf_output_read_one(struct perf_output_handle *handle, u64 values[5]; int n = 0; - values[n++] = perf_event_count(event); + values[n++] = perf_event_count(event, true); if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { values[n++] = enabled + atomic64_read(&event->child_total_time_enabled); @@ -7245,7 +7274,7 @@ static void perf_output_read_group(struct perf_output_handle *handle, (leader->state == PERF_EVENT_STATE_ACTIVE)) leader->pmu->read(leader); - values[n++] = perf_event_count(leader); + values[n++] = perf_event_count(leader, true); if (read_format & PERF_FORMAT_ID) values[n++] = primary_event_id(leader); if (read_format & PERF_FORMAT_LOST) @@ -7260,7 +7289,7 @@ static void perf_output_read_group(struct perf_output_handle *handle, (sub->state == PERF_EVENT_STATE_ACTIVE)) sub->pmu->read(sub); - values[n++] = perf_event_count(sub); + values[n++] = perf_event_count(sub, false); if (read_format & PERF_FORMAT_ID) values[n++] = primary_event_id(sub); if (read_format & PERF_FORMAT_LOST) @@ -12010,10 +12039,13 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, local64_set(&hwc->period_left, hwc->sample_period); /* - * We currently do not support PERF_SAMPLE_READ on inherited events. + * We do not support PERF_SAMPLE_READ on inherited events unless + * inherit_stat and PERF_SAMPLE_TID are also selected, which allows + * inherited events to collect per-thread samples. * See perf_output_read(). */ - if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ)) + if (perf_attr_has_inherit_and_sample_read(attr) + && !(attr->inherit_stat && (attr->sample_type & PERF_SAMPLE_TID))) goto err_ns; if (!has_branch_stack(event)) @@ -13037,7 +13069,7 @@ static void sync_child_event(struct perf_event *child_event) perf_event_read_event(child_event, task); } - child_val = perf_event_count(child_event); + child_val = perf_event_count(child_event, false); /* * Add back the child's count to the parent's count: -- 2.44.0