From patchwork Fri Apr 14 08:22:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 83290 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp221825vqo; Fri, 14 Apr 2023 01:33:03 -0700 (PDT) X-Google-Smtp-Source: AKy350YPOh473Ul1HnOzOhwKViQeXFYNHaYa387C7Uv1L78FGmrufEp9PtuAzyq+xGMP32mOkoLU X-Received: by 2002:a17:903:443:b0:1a2:8c7e:f310 with SMTP id iw3-20020a170903044300b001a28c7ef310mr1985119plb.35.1681461183226; Fri, 14 Apr 2023 01:33:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681461183; cv=none; d=google.com; s=arc-20160816; b=Nyn4WVriytBx8VSyxcbTtiaGXNbgb5yn5Nem0OYkwM6O/f+Ta7HF8jJ8DnrhRjcwcb Jals997iK9JBi4cxtXdrhdBkxFpD3r5Rieweb6ajhpbTN23obsJsEarh4zgiNQfgyZzH c2C0eYCBLNgNdMBmvM9aq9GqSC+YVSa157f4FYvYnBlG0Ic3I10VoN3cJ0Xo0JH5rsnO oAFwCHTAzOl9ABCTID7xbKTZCgG2box1gwvoDE5B8E9MUIl1wVs5G9her+XBosJ0iuT8 cWYHRiipymrytXrMQbGf3+asMa8gpSFymzW08lkL15CXQQd9H7bJ4xErWvNWkS3RH46U 6OZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3j71A+4V6kNSAVGoZGtJJUExUe0l9HNzqQ0nkWcXBpM=; b=A8Nt5Q+ZW7aZ9gl46xxup7FFWsZ/xsCd0O1Kjw1P3je1fGppLtmnZnuyj9QnJbvM9M pCFYy/ZK1Zk5FbW7zSCXXwfh0OFjdsthFf/UMn7FZHQKE7mSD1XIh5omgc1cFIHrkbwA XqQrK5UdIq/z+4Tl7o0raGz7r4P2j2w8rXlrQknLfePHJRsCuadtiu+u9jF7bgyBUT5r t9kVcewg+ReP6kYm86NyPJbp86lz0Lbf8L9ddsqP2TS0djk0+6oyK6xUaTU9+KcvCdDJ gSvIptw8OS0VI8Ios/ioR1P5QLJLQgdbosp2dHwVsGgj5sHoHH8Il32aL///h789IaON rIlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=C3us+nhn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m15-20020a170902db0f00b001a22560151asi4248018plx.325.2023.04.14.01.32.51; Fri, 14 Apr 2023 01:33:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=C3us+nhn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230182AbjDNIXd (ORCPT + 99 others); Fri, 14 Apr 2023 04:23:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230110AbjDNIXZ (ORCPT ); Fri, 14 Apr 2023 04:23:25 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E408E30FD; Fri, 14 Apr 2023 01:23:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681460603; x=1712996603; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=lKhCyQDrP3SWVP+Mn5U2yqr9K7pd2gf6DvLZAJNWAYw=; b=C3us+nhnfk6/hNhlG/JKbd6WhQY7SlftA2qG84jea3PzKO6PBZOro3t7 CTpxnRp0Myxx28XiTcFjz8AeHDjB1Iit49Ar4GNCBHzjxeADod5FNVWK/ JrSV3q754ooJ03E5CuqxCUnwZZwauXiw9uOXgZt4MQeW2a4v5WhlnpUP7 d759Twsp25xR73lwo8kBGU5DNXNbEdwwPSXULeA/sIE2C8Exxev+zPTxA dUA4JAcDqBxOaM+U/4S/WErqZ+XZSmlUGgnLcTl811De0HR6e9sprLE97 6tR4YleFLbvHkZ1xw8914v9Mp8GLqXD0AafSD4Nec2qW7RUa0GlKCeDkN A==; X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="430708091" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="430708091" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="683267456" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="683267456" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.249.34.252]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:20 -0700 From: Adrian Hunter To: Peter Zijlstra Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC 1/5] perf: Add ioctl to emit sideband events Date: Fri, 14 Apr 2023 11:22:56 +0300 Message-Id: <20230414082300.34798-2-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230414082300.34798-1-adrian.hunter@intel.com> References: <20230414082300.34798-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763139828019123338?= X-GMAIL-MSGID: =?utf-8?q?1763139841877604223?= perf tools currently read /proc to get this information, but that races with changes made by the kernel. Add an ioctl to output status-only sideband events for a currently active event on the current CPU. Using timestamps, these status-only sideband events will be correctly ordered with respect to "real" sideband events. The assumption is a user will: - open and enable a dummy event to track sideband events - call the new ioctl to get sideband information for currently running processes as needed - enable the remaining selected events The initial sideband events to be supported will be: fork, namespaces, comm and mmap. Add a new misc flag PERF_RECORD_MISC_STATUS_ONLY to differentiate "real" sideband events from status-only sideband events. The limitation that the event must be active is significant. The ioctl caller must either: i) For a CPU context, set CPU affinity to the correct CPU. Note, obviously that would not need to be done for system-wide tracing on all CPUs. It would also only need to be done for the period of tracing when the ioctl is to be used. ii) Use an event opened for the current process on all CPUs. Note, if such an additional event is needed, it would also use additional memory from the user's perf_event_mlock_kb / RLIMIT_MEMLOCK limit. Signed-off-by: Adrian Hunter --- include/uapi/linux/perf_event.h | 19 ++++++- kernel/events/core.c | 87 ++++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index 37675437b768..d44fb0f65484 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -541,6 +541,18 @@ struct perf_event_query_bpf { __u32 ids[]; }; +enum perf_event_emit_flag { + PERF_EVENT_EMIT_FORK = 1U << 0, + PERF_EVENT_EMIT_NAMESPACES = 1U << 1, + PERF_EVENT_EMIT_COMM = 1U << 2, + PERF_EVENT_EMIT_MMAP = 1U << 3, +}; + +struct perf_event_pid_sb { + __u32 pid; + __u32 emit_flags; /* Refer perf_event_emit_flag */ +}; + /* * Ioctls that can be done on a perf event fd: */ @@ -556,6 +568,7 @@ struct perf_event_query_bpf { #define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32) #define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *) #define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_EMIT_SIDEBAND _IOW('$', 12, struct perf_event_pid_sb *) enum perf_event_ioc_flags { PERF_IOC_FLAG_GROUP = 1U << 0, @@ -743,12 +756,13 @@ struct perf_event_mmap_page { * The current state of perf_event_header::misc bits usage: * ('|' used bit, '-' unused bit) * - * 012 CDEF - * |||---------|||| + * 012 BCDEF + * |||--------||||| * * Where: * 0-2 CPUMODE_MASK * + * B STATUS_ONLY * C PROC_MAP_PARSE_TIMEOUT * D MMAP_DATA / COMM_EXEC / FORK_EXEC / SWITCH_OUT * E MMAP_BUILD_ID / EXACT_IP / SCHED_OUT_PREEMPT @@ -763,6 +777,7 @@ struct perf_event_mmap_page { #define PERF_RECORD_MISC_GUEST_KERNEL (4 << 0) #define PERF_RECORD_MISC_GUEST_USER (5 << 0) +#define PERF_RECORD_MISC_STATUS_ONLY (1 << 11) /* * Indicates that /proc/PID/maps parsing are truncated by time out. */ diff --git a/kernel/events/core.c b/kernel/events/core.c index fb3e436bcd4a..5cbcc6851587 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5797,6 +5797,7 @@ static int perf_event_set_output(struct perf_event *event, static int perf_event_set_filter(struct perf_event *event, void __user *arg); static int perf_copy_attr(struct perf_event_attr __user *uattr, struct perf_event_attr *attr); +static int perf_event_emit_sideband(struct perf_event *event, void __user *arg); static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) { @@ -5924,6 +5925,9 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (ret) return ret; + if (cmd == PERF_EVENT_IOC_EMIT_SIDEBAND) + return perf_event_emit_sideband(event, (void __user *)arg); + ctx = perf_event_ctx_lock(event); ret = _perf_ioctl(event, cmd, arg); perf_event_ctx_unlock(event, ctx); @@ -5940,6 +5944,7 @@ static long perf_compat_ioctl(struct file *file, unsigned int cmd, case _IOC_NR(PERF_EVENT_IOC_ID): case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF): case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES): + case _IOC_NR(PERF_EVENT_IOC_EMIT_SIDEBAND): /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { cmd &= ~IOCSIZE_MASK; @@ -12277,7 +12282,7 @@ perf_check_permission(struct perf_event_attr *attr, struct task_struct *task) unsigned int ptrace_mode = PTRACE_MODE_READ_REALCREDS; bool is_capable = perfmon_capable(); - if (attr->sigtrap) { + if (attr && attr->sigtrap) { /* * perf_event_attr::sigtrap sends signals to the other task. * Require the current task to also have CAP_KILL. @@ -12810,6 +12815,86 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, } EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); +static int perf_event_emit_fork(struct perf_event *event, struct task_struct *task) +{ + return -EINVAL; +} + +static int perf_event_emit_namespaces(struct perf_event *event, struct task_struct *task) +{ + return -EINVAL; +} + +static int perf_event_emit_comm(struct perf_event *event, struct task_struct *task) +{ + return -EINVAL; +} + +static int perf_event_emit_mmap(struct perf_event *event, struct task_struct *task) +{ + return -EINVAL; +} + +static int perf_event_emit_sideband(struct perf_event *event, void __user *arg) +{ + struct perf_event_pid_sb pid_sb; + struct perf_event_context *ctx; + struct task_struct *task; + int err; + + if (copy_from_user(&pid_sb, arg, sizeof(pid_sb))) + return -EFAULT; + + if (pid_sb.emit_flags & ~(PERF_EVENT_EMIT_FORK | + PERF_EVENT_EMIT_NAMESPACES | + PERF_EVENT_EMIT_COMM | + PERF_EVENT_EMIT_MMAP)) + return -EINVAL; + + task = find_lively_task_by_vpid(pid_sb.pid); + if (IS_ERR(task)) + return PTR_ERR(task); + + err = down_read_interruptible(&task->signal->exec_update_lock); + if (err) + goto out_put_task; + + /* Validate access to pid (same as perf_event_open) */ + err = -EACCES; + if (!perf_check_permission(NULL, task)) + goto out_cred; + + ctx = perf_event_ctx_lock(event); + + if (pid_sb.emit_flags & PERF_EVENT_EMIT_FORK) { + err = perf_event_emit_fork(event, task); + if (err) + goto out_ctx; + } + if (pid_sb.emit_flags & PERF_EVENT_EMIT_NAMESPACES) { + err = perf_event_emit_namespaces(event, task); + if (err) + goto out_ctx; + } + if (pid_sb.emit_flags & PERF_EVENT_EMIT_COMM) { + err = perf_event_emit_comm(event, task); + if (err) + goto out_ctx; + } + if (pid_sb.emit_flags & PERF_EVENT_EMIT_MMAP) { + err = perf_event_emit_mmap(event, task); + if (err) + goto out_ctx; + } +out_ctx: + perf_event_ctx_unlock(event, ctx); +out_cred: + up_read(&task->signal->exec_update_lock); +out_put_task: + put_task_struct(task); + return err; +} + static void __perf_pmu_remove(struct perf_event_context *ctx, int cpu, struct pmu *pmu, struct perf_event_groups *groups, From patchwork Fri Apr 14 08:22:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 83289 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp221616vqo; Fri, 14 Apr 2023 01:32:35 -0700 (PDT) X-Google-Smtp-Source: AKy350Y9z5IJJZ1/S3pD3/OP4whO6phSQWRaGYo41tAZ2vSOSZBi4BDQRiu0/3Nn2MrZDGcSxEbU X-Received: by 2002:a17:90a:8904:b0:23f:d7ea:6212 with SMTP id u4-20020a17090a890400b0023fd7ea6212mr4479014pjn.38.1681461155316; Fri, 14 Apr 2023 01:32:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681461155; cv=none; d=google.com; s=arc-20160816; b=O2AJ3XWEnlMxQTv29XYZadYUGDM/KgpkxnQzXBUrBcjyNi3x/bGtWq0GgN2xTfVP45 s4YaYiQlJgmZ4C9mOAb/1R/nDNdBiSlkKaL3ermHHcwBOSScjQo4fButEQGTL1LM9l/N wlbEpN4WStnnD4s/P5itqSah50gWTa0ES8FRiJh/zhm/8NR3KTsSaJM7JG91o557AfdB 1w+Thc+bomeeU0DZBEoiXXNemZnUGldjw5+c0FPXYiLTZp0XKoBQQuwdS4dIi/P8rRaL YRz9UMDkVoaAv7D2SZpZy7UDLWnNc6cMzfmADLiN2H2PPOEMDc4AAIYzn3G7v04hg5Sq cyOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=qF+THty94EGXAom7r7CwfrxD58ZOO4PuNQhPC9d3Veo=; b=fCf3UiPcOaW5+6HqyGLEiNYaQeW1hMdnCWnAZgYaSNpu1Kk9dOb9gD2ccqHue4myNP 9JeP95Q8h2AQP8GF299S0iBMkruc/JiW96xSwQaciyHxTzsVufiCrmW0gWPL7dT7H+In 1PC6b1Rt3Z2QwhS2lrdcZxTMxDWeMT9Q883PympDEznKCM6sFFXbjGfMVIKBBN73pPd8 nVsv27hNXy445WoIpuH8mC+snHN4Y5D37WMBZkBHzehccOsYQ5oWhHPtXTnXyhguT/zw PP22VIENrS++Zmqc88Z4Pg9ewwzxhOCXD4+FViN9qgYt4cdtbjqQkPzZG/ECGIOdegDn kT2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ahgM5qkA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a32-20020a631a20000000b0051b554df1c2si3042172pga.497.2023.04.14.01.32.23; Fri, 14 Apr 2023 01:32:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ahgM5qkA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230188AbjDNIXk (ORCPT + 99 others); Fri, 14 Apr 2023 04:23:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230003AbjDNIX2 (ORCPT ); Fri, 14 Apr 2023 04:23:28 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EF6B6E9D; Fri, 14 Apr 2023 01:23:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681460607; x=1712996607; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Xhw2PFI44xhxatWgOY9IkkDtUpBt0VWfQQBb/Lx2E8k=; b=ahgM5qkA4p9/HBmOgo2SAuiCLd3kduResrfoGooNywsBgx5l1JM8Q9i1 xjQyu2bcqAUEQO8YfujHtZ7omMlZNBB/l+43gAv7mww2ICgi8nA49vh4a D3bQWHbdWm2SWI02cjBeVq2e3jnnPDFyFBOGk18P8jjrQ72/jr32sYIHi HhRZP5SjH3DAg8l0UOuPc+loFyRXBfX3DzdVTHXyZOsz00VzywDwKCkWR mrHGFH84mR167vx+mm5WhcqDOHZGDaqTaZwYLkigygkxH101+cZ6RINcr uGPJ7jXBSkWjw8s8A/iID5IwkabT0UAwOjIqRbk+6pGBN2JiUCEorDUV6 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="430708106" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="430708106" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="683267482" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="683267482" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.249.34.252]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:23 -0700 From: Adrian Hunter To: Peter Zijlstra Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC 2/5] perf: Add fork to the sideband ioctl Date: Fri, 14 Apr 2023 11:22:57 +0300 Message-Id: <20230414082300.34798-3-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230414082300.34798-1-adrian.hunter@intel.com> References: <20230414082300.34798-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763139812102785825?= X-GMAIL-MSGID: =?utf-8?q?1763139812102785825?= 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 fork parent pid/tid to the real parent for a thread group leader, or to the thread group leader otherwise. Signed-off-by: Adrian Hunter --- kernel/events/core.c | 88 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 15 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 5cbcc6851587..4e76596d3bfb 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7948,6 +7948,54 @@ perf_iterate_sb(perf_iterate_f output, void *data, rcu_read_unlock(); } +typedef int (perf_output_f)(struct perf_event *event, void *data); + +static int perf_event_output_sb(struct perf_event *event, perf_output_f output, void *data) +{ + int err = -ENOENT; + + preempt_disable(); + + if (event->state != PERF_EVENT_STATE_ACTIVE || + !event_filter_match(event) || + READ_ONCE(event->oncpu) != smp_processor_id()) + goto out; + + err = output(event, data); +out: + preempt_enable(); + return err; +} + +struct perf_output_f_data { + perf_output_f *func; + void *data; +}; + +void perf_output_f_wrapper(struct perf_event *event, void *data) +{ + struct perf_output_f_data *f_data = data; + + f_data->func(event, f_data->data); +} + +static int perf_output_sb(perf_output_f output, void *data, + struct perf_event_context *task_ctx, + struct perf_event *event) +{ + struct perf_output_f_data f_data = { + .func = output, + .data = data, + }; + + if (event) + return perf_event_output_sb(event, output, data); + + perf_iterate_sb(perf_output_f_wrapper, &f_data, task_ctx); + + return 0; +} + /* * Clear all file-based filters at exec, they'll have to be * re-instated when/if these objects are mmapped again. @@ -8107,8 +8155,7 @@ static int perf_event_task_match(struct perf_event *event) event->attr.task; } -static void perf_event_task_output(struct perf_event *event, - void *data) +static int perf_event_task_output(struct perf_event *event, void *data) { struct perf_task_event *task_event = data; struct perf_output_handle handle; @@ -8117,7 +8164,7 @@ static void perf_event_task_output(struct perf_event *event, int ret, size = task_event->event_id.header.size; if (!perf_event_task_match(event)) - return; + return -ENOENT; perf_event_header__init_id(&task_event->event_id.header, &sample, event); @@ -8134,6 +8181,14 @@ static void perf_event_task_output(struct perf_event *event, task->real_parent); task_event->event_id.ptid = perf_event_pid(event, task->real_parent); + } else if (task_event->event_id.header.misc & PERF_RECORD_MISC_STATUS_ONLY) { + if (thread_group_leader(task)) { + task_event->event_id.ppid = perf_event_pid(event, task->real_parent); + task_event->event_id.ptid = perf_event_tid(event, task->real_parent); + } else { + task_event->event_id.ppid = perf_event_pid(event, task); + task_event->event_id.ptid = perf_event_pid(event, task); + } } else { /* PERF_RECORD_FORK */ task_event->event_id.ppid = perf_event_pid(event, current); task_event->event_id.ptid = perf_event_tid(event, current); @@ -8148,18 +8203,19 @@ static void perf_event_task_output(struct perf_event *event, perf_output_end(&handle); out: task_event->event_id.header.size = size; + return ret; } -static void perf_event_task(struct task_struct *task, - struct perf_event_context *task_ctx, - int new) +static int perf_event_task(struct task_struct *task, + struct perf_event_context *task_ctx, + int new, struct perf_event *event) { struct perf_task_event task_event; if (!atomic_read(&nr_comm_events) && !atomic_read(&nr_mmap_events) && !atomic_read(&nr_task_events)) - return; + return -ENOENT; task_event = (struct perf_task_event){ .task = task, @@ -8167,7 +8223,7 @@ static void perf_event_task(struct task_struct *task, .event_id = { .header = { .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, - .misc = 0, + .misc = event ? PERF_RECORD_MISC_STATUS_ONLY : 0, .size = sizeof(task_event.event_id), }, /* .pid */ @@ -8178,14 +8234,12 @@ static void perf_event_task(struct task_struct *task, }, }; - perf_iterate_sb(perf_event_task_output, - &task_event, - task_ctx); + return perf_output_sb(perf_event_task_output, &task_event, task_ctx, event); } void perf_event_fork(struct task_struct *task) { - perf_event_task(task, NULL, 1); + perf_event_task(task, NULL, 1, NULL); perf_event_namespaces(task); } @@ -12817,7 +12871,11 @@ EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); static int perf_event_emit_fork(struct perf_event *event, struct task_struct *task) { - return -EINVAL; + if (!event->attr.comm && !event->attr.mmap && !event->attr.mmap2 && + !event->attr.mmap_data && !event->attr.task) + return -EINVAL; + + return perf_event_task(task, NULL, 1, event); } static int perf_event_emit_namespaces(struct perf_event *event, struct task_struct *task) @@ -13115,7 +13173,7 @@ static void perf_event_exit_task_context(struct task_struct *child) * won't get any samples after PERF_RECORD_EXIT. We can however still * get a few PERF_RECORD_READ events. */ - perf_event_task(child, child_ctx, 0); + perf_event_task(child, child_ctx, 0, NULL); list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) perf_event_exit_event(child_event, child_ctx); @@ -13157,7 +13215,7 @@ void perf_event_exit_task(struct task_struct *child) * child contexts and sets child->perf_event_ctxp[] to NULL. * At this point we need to send EXIT events to cpu contexts. */ - perf_event_task(child, NULL, 0); + perf_event_task(child, NULL, 0, NULL); } static void perf_free_event(struct perf_event *event, From patchwork Fri Apr 14 08:22:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 83292 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp222021vqo; Fri, 14 Apr 2023 01:33:32 -0700 (PDT) X-Google-Smtp-Source: AKy350an1UPlYLAipUZHYsTO3BVKJcR8jvnQqObfbbDKYQAHDjPJ6zUKn8Ud+9hWHpiSL3XcMQe2 X-Received: by 2002:a17:90a:670e:b0:247:2d9d:4722 with SMTP id n14-20020a17090a670e00b002472d9d4722mr3187830pjj.0.1681461212027; Fri, 14 Apr 2023 01:33:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681461212; cv=none; d=google.com; s=arc-20160816; b=Y6HUETkRAhu6rcXwkJNG6AXvpSgLGwWbEptu/DZq20xbpkdMh0aacvjX4rgj8L7Xqa 4ca2CeLg0FTVYlFVzufFtLdLiYH88BH7w6GWMlhgTwknBt9e90TsONHFpTvgIYGslDL8 uCFI5n9AUxQPMRlT0dA8x5bkWbADHsr6xl2I0UUwEXJ70xVOgSjkovEIfLKBGA/XSCPe IU2IZIFXOsd7DejwbVfNE6ePufvho0b/IUwV9X6BIdWXxKOH3H2suxjxxgltEDbfqthh xcDx1MI9BqltrytN0NAkM5C6xiGOpzjQE9o/IH4COdCnmyB8dZjwqNUo1w1rrbdV01qp KErw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fGLUiIx/GYGnfIBiBSGVJqQHVu8kigGpgwqJl7y9fgs=; b=xXb+29Df+c+uTSv/qYRzDl4G7mi5T/RpzvITLuIw73h+mshpaH4SE5pCleeDN6B3ip bNaVc4X7aoXgZPN2dV2n8LsaY4bqasSgE5MugS/ERtGEOB9KsH8Jy+qc4BVPZ5RUfWhS x0BmO8J/uva4DXuin6i9wC06rdwgmK2u3N2UzZOIr+kleJQ0ZaUE8dpFjqEMVDpy00o2 27qP4xW7YGgkaC8ZXUNreViNFdAKT30Uo95B0hGShqV4tODVf5gEfmpqVPgQ4Euc1J+q JavgbMI+NTL4PO7fdoJRbgkGRFopmVtmFygvOaPvk03fzeG6w6doM67Jx6euw9w5ecAs AG0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nXPByUpy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u64-20020a638543000000b005023dfa3c68si4338103pgd.145.2023.04.14.01.33.20; Fri, 14 Apr 2023 01:33:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nXPByUpy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230257AbjDNIXw (ORCPT + 99 others); Fri, 14 Apr 2023 04:23:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230200AbjDNIXr (ORCPT ); Fri, 14 Apr 2023 04:23:47 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38EB78F; Fri, 14 Apr 2023 01:23:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681460611; x=1712996611; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=k9xICirPTs+XLNZnMSTUNHHk49e/SSv9s64WeLS7yIQ=; b=nXPByUpyyio0vmdQQDD/xoorBkijXOofEqiFlchUd+d7oB/vbHItpkr/ dL9M7252bMA2lCSt4dc+ZacK4OYyiifgTiLBRdz1iTm+swkAspdHcYsdT bpH5h5RLNM1/JUO650C60fQlzqRHUwc3WmyPhgoc+JySyOS79T7u26YiK ynR1ClorVNFEqgvh+hZOddvfGJPPiXllnrqGbPkbcz2k7jO+cqk6HZBuN 3Nnhwv+kYRPcP8khjYjv+sjnd/H2PS8tsLhZwjqLVX8sj2O9fEA6kPqjB 2iiLE+qfAPD7VtgW8RefJsBd5H2dtkB5gPAogw8xZaT9lYQr10w886GsN Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="430708125" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="430708125" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="683267520" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="683267520" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.249.34.252]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:27 -0700 From: Adrian Hunter To: Peter Zijlstra Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC 3/5] perf: Add namespaces to the sideband ioctl Date: Fri, 14 Apr 2023 11:22:58 +0300 Message-Id: <20230414082300.34798-4-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230414082300.34798-1-adrian.hunter@intel.com> References: <20230414082300.34798-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763139871985190936?= X-GMAIL-MSGID: =?utf-8?q?1763139871985190936?= 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. Signed-off-by: Adrian Hunter --- kernel/events/core.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 4e76596d3bfb..ed4af231853a 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8364,8 +8364,7 @@ static int perf_event_namespaces_match(struct perf_event *event) return event->attr.namespaces; } -static void perf_event_namespaces_output(struct perf_event *event, - void *data) +static int perf_event_namespaces_output(struct perf_event *event, void *data) { struct perf_namespaces_event *namespaces_event = data; struct perf_output_handle handle; @@ -8374,7 +8373,7 @@ static void perf_event_namespaces_output(struct perf_event *event, int ret; if (!perf_event_namespaces_match(event)) - return; + return -ENOENT; perf_event_header__init_id(&namespaces_event->event_id.header, &sample, event); @@ -8395,6 +8394,7 @@ static void perf_event_namespaces_output(struct perf_event *event, perf_output_end(&handle); out: namespaces_event->event_id.header.size = header_size; + return ret; } static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info, @@ -8414,20 +8414,20 @@ static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info, } } -void perf_event_namespaces(struct task_struct *task) +static int __perf_event_namespaces(struct task_struct *task, struct perf_event *event) { struct perf_namespaces_event namespaces_event; struct perf_ns_link_info *ns_link_info; if (!atomic_read(&nr_namespaces_events)) - return; + return -ENOENT; namespaces_event = (struct perf_namespaces_event){ .task = task, .event_id = { .header = { .type = PERF_RECORD_NAMESPACES, - .misc = 0, + .misc = event ? PERF_RECORD_MISC_STATUS_ONLY : 0, .size = sizeof(namespaces_event.event_id), }, /* .pid */ @@ -8467,9 +8467,12 @@ void perf_event_namespaces(struct task_struct *task) task, &cgroupns_operations); #endif - perf_iterate_sb(perf_event_namespaces_output, - &namespaces_event, - NULL); + return perf_output_sb(perf_event_namespaces_output, &namespaces_event, NULL, event); +} + +void perf_event_namespaces(struct task_struct *task) +{ + __perf_event_namespaces(task, NULL); } /* @@ -12880,7 +12883,10 @@ static int perf_event_emit_fork(struct perf_event *event, struct task_struct *ta static int perf_event_emit_namespaces(struct perf_event *event, struct task_struct *task) { - return -EINVAL; + if (!event->attr.namespaces) + return -EINVAL; + + return __perf_event_namespaces(task, event); } static int perf_event_emit_comm(struct perf_event *event, struct task_struct *task) From patchwork Fri Apr 14 08:22:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 83285 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp218836vqo; Fri, 14 Apr 2023 01:25:41 -0700 (PDT) X-Google-Smtp-Source: AKy350bKtYXyzoIx7C9KsFnOqOQUqTVYXJ8zxinoFnpnFjXpxCLXxVGhirzfJo0/9UNIFxUn/ZY5 X-Received: by 2002:a17:902:db10:b0:1a6:494c:f723 with SMTP id m16-20020a170902db1000b001a6494cf723mr2140634plx.54.1681460741307; Fri, 14 Apr 2023 01:25:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681460741; cv=none; d=google.com; s=arc-20160816; b=WJinp7V1NOFkzgX5IywwmjGS4ydj15WgwlH0EpPMm2pd+SCRBPWmprUBG+yaxg9BqL zjP2xA6ymZrHKaCr2yLSY1j/Gyy8c96tRVMLKYXZyYgMZoGmqaXizDOucrXnO+E9t+rq QLNRp5hNa9wmtqJ03coM1/C02dqdm5sZNoK0P6PSI1LtKnj4er9SdvKQrnmziRBrSeWG lwm/lnQWXCPuHpHk87c1WjRQOyFfrexOLTFG6y2BVre1vRkcTRx84koQIY/joWb9ZWJw KQ9qMoEVrUFYOuMP8hr1fG1Atlt4HDT+yWFNcPfgygSt0bHKm5ONt5HZu3w+6pALwheD qzAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=iD7v+IQFV0HmhxCjWzW6/hN2SiJquhiNnXd4fWTOzRY=; b=gKrcm/wH9O6ZxdCHSkE4TdsDX4myerbuU3if9e5dXwaSuWpBXzmGsf2Tvce97sTKzb Fxw62cvYzU+MsgPZqtFkmZdg/7haBHGGPUTrqZeAZAWn5sfABWTlfuKWGXCkSEzvgcU/ h2j0yvyJArARi/CZtj2Gy1K6hl2eEnu56h+7GpbMxtHva7wp6cvcENN+YvqHGJt9BNtR G8VwVL2ZObBpYnEwqUkcpRKTEonFKrOUb9+7vs9QB0f0aBLnx7G7B3R18coxCMX6mA4Q /sa8BmxVDe26sNl2PpqdNJ6bfynw7uYeq9cTgdKbd/srTEPuNFFKz7wlIDk+faDOm1r5 Puyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dglHk3ST; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b11-20020a170903228b00b001a1e4051da5si4470397plh.498.2023.04.14.01.25.28; Fri, 14 Apr 2023 01:25:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dglHk3ST; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229749AbjDNIYA (ORCPT + 99 others); Fri, 14 Apr 2023 04:24:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230220AbjDNIXt (ORCPT ); Fri, 14 Apr 2023 04:23:49 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFECF7EF5; Fri, 14 Apr 2023 01:23:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681460614; x=1712996614; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3nHcjAW5hAXD3jrHcmKICBKZPcBY+tB2vS9Jj26IxgQ=; b=dglHk3STL+XoTEAcBm1p7GlojlLHnTIFjlP3TzcNoD4YIrk5PVTglCaZ SwruLGdz3Hu7q6wALVUpVLYliszc61pmLDesKZI7Pm1wQ+ekgNYYANhhn ySRb9EbGuXDjW1OMjH1Ehw8IfOc9vINvTkfs72oyoDiY269wpvFR0JPFX JCquoLQOybIx4JVDHz6YquGmWZhKnJiX2z7np+a3fpTs53UY2ZkZyXjir fhYkfwE6L0C4179o5b249s8pKg19hGBJmN/jCUGYsGxoxUlSgqUomXSgL nOR3O0QuFWfpeMAfV5dF5hcqk/kp1j7ZCVYjRM36YPbkI+DSxtdsvEMKf Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="430708139" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="430708139" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="683267542" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="683267542" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.249.34.252]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:31 -0700 From: Adrian Hunter To: Peter Zijlstra Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC 4/5] perf: Add comm to the sideband ioctl Date: Fri, 14 Apr 2023 11:22:59 +0300 Message-Id: <20230414082300.34798-5-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230414082300.34798-1-adrian.hunter@intel.com> References: <20230414082300.34798-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763139378129614148?= X-GMAIL-MSGID: =?utf-8?q?1763139378129614148?= 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. Signed-off-by: Adrian Hunter --- kernel/events/core.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index ed4af231853a..cddc02c2e411 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8265,8 +8265,7 @@ static int perf_event_comm_match(struct perf_event *event) return event->attr.comm; } -static void perf_event_comm_output(struct perf_event *event, - void *data) +static int perf_event_comm_output(struct perf_event *event, void *data) { struct perf_comm_event *comm_event = data; struct perf_output_handle handle; @@ -8275,7 +8274,7 @@ static void perf_event_comm_output(struct perf_event *event, int ret; if (!perf_event_comm_match(event)) - return; + return -ENOENT; perf_event_header__init_id(&comm_event->event_id.header, &sample, event); ret = perf_output_begin(&handle, &sample, event, @@ -8296,9 +8295,10 @@ static void perf_event_comm_output(struct perf_event *event, perf_output_end(&handle); out: comm_event->event_id.header.size = size; + return ret; } -static void perf_event_comm_event(struct perf_comm_event *comm_event) +static int perf_event_comm_event(struct perf_comm_event *comm_event, struct perf_event *event) { char comm[TASK_COMM_LEN]; unsigned int size; @@ -8312,17 +8312,15 @@ static void perf_event_comm_event(struct perf_comm_event *comm_event) comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; - perf_iterate_sb(perf_event_comm_output, - comm_event, - NULL); + return perf_output_sb(perf_event_comm_output, comm_event, NULL, event); } -void perf_event_comm(struct task_struct *task, bool exec) +static int __perf_event_comm(struct task_struct *task, bool exec, struct perf_event *event) { struct perf_comm_event comm_event; if (!atomic_read(&nr_comm_events)) - return; + return -ENOENT; comm_event = (struct perf_comm_event){ .task = task, @@ -8331,7 +8329,8 @@ void perf_event_comm(struct task_struct *task, bool exec) .event_id = { .header = { .type = PERF_RECORD_COMM, - .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, + .misc = (exec ? PERF_RECORD_MISC_COMM_EXEC : 0) | + (event ? PERF_RECORD_MISC_STATUS_ONLY : 0), /* .size */ }, /* .pid */ @@ -8339,7 +8338,12 @@ void perf_event_comm(struct task_struct *task, bool exec) }, }; - perf_event_comm_event(&comm_event); + return perf_event_comm_event(&comm_event, event); +} + +void perf_event_comm(struct task_struct *task, bool exec) +{ + __perf_event_comm(task, exec, NULL); } /* @@ -12891,7 +12895,10 @@ static int perf_event_emit_namespaces(struct perf_event *event, struct task_stru static int perf_event_emit_comm(struct perf_event *event, struct task_struct *task) { - return -EINVAL; + if (!event->attr.comm) + return -EINVAL; + + return __perf_event_comm(task, false, event); } static int perf_event_emit_mmap(struct perf_event *event, struct task_struct *task) From patchwork Fri Apr 14 08:23:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 83286 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp218854vqo; Fri, 14 Apr 2023 01:25:45 -0700 (PDT) X-Google-Smtp-Source: AKy350bXb+m79UpTPBU+fx3+X3KAEZC+5d2tVMfRZnAtKOqjS8OSj8dGz+grIAbryPVQSHj/8Yjo X-Received: by 2002:a05:6a20:4e22:b0:ec:40f:871c with SMTP id gk34-20020a056a204e2200b000ec040f871cmr4994720pzb.30.1681460745671; Fri, 14 Apr 2023 01:25:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681460745; cv=none; d=google.com; s=arc-20160816; b=Lw4L3dH7YqSMcdPIdoAIuLkobODuI8xVOHtumtlo/jomtKA+cLSl6e72I33+XAiYyo OkFJW7i1Xbz3wxWl7kNRDRxM3LbBVylnk4ePtvRYhYDSfn6N9xVe0wa2+IimOS0Ez1Zj OcGJvuCL8K+FgxHstHuJ3Q/BRTSbJS3e5YWusH1OUoD1Th3/Zn9QT1ue7GPIjirIlU0U 5O9vrAm2hTcywMKEbuFwvpvAeStu4qeWiFtIl2+je6hHoB5jxKzMy2J1G58cWBay88v8 n5idG6zzGtMQ67TQFPi+4Yv8l85NUBM3ylWCFjSCXMvKgqCdAv3AnKfXTAtQSbDH30xW 7nYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=gfYV3cuh1k9RP1hUJlh3/10pEkeT7HKlUamEzMwMoL4=; b=0RnLv+nuCXqp5o3Cfk5tth1SfHNMzcg+qrLIQBDu8AJ5kSJhf6Y6lK9YZd+EmBHjgR 5b1fLKDOzNvfi0ZFa3vJdirUrLzzBRyFMpj4sraGeYeD6uc4jeOOsDb4AfGtyr9p0x3b PvaRKl6Q1uUhzPGj21YvCBY78F2/mB/9OUVJlmK6tDauhljTIOdB4yyPrLwG+uSlEUnw GCs8ybULHi9UWbHbmaPvoPpJxfMSYeL3amAzGMFT5MrexXtjDPl0dINF7ylJ54rxv7vN vlqEj88nIzgHCrCYimZmVpTCQHip3/2p/W0U2uB+1TBYU9+BeMpiQ7K6E0M9gVwF6RcP EMog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GCDCeErI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r18-20020a63ce52000000b0051324096be5si4190607pgi.47.2023.04.14.01.25.33; Fri, 14 Apr 2023 01:25:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GCDCeErI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230220AbjDNIYD (ORCPT + 99 others); Fri, 14 Apr 2023 04:24:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230177AbjDNIXv (ORCPT ); Fri, 14 Apr 2023 04:23:51 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BBEA83F6; Fri, 14 Apr 2023 01:23:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681460618; x=1712996618; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KlLl00c1BkbXcaNS+veMVCoA8VHv6YdBHnPGvXsCuZw=; b=GCDCeErIVi60cfi8c5dY0oJLqOLNo1xxN5TNGX0GdZQWxZZNnbR2F/eq JTrJ0P7HUaEQtt+FfZ/VTDCRLMMxs53/YJL6WN4/3/M66gDo9E/ldDQC8 sOTeWzBPONk5TsUWGgSPQLMnbMXpsgo+A9l8W7Z0g8U1Aeh2mOYKvg8YV baSO2n0LknY1XLaMmPgMqMeAmDTh0sCjE27RFlMof/fMYxVdlDYutZYln O3TfRSE97yak4jwRFPy48pO1q0GN/cCSFtDNJFOUSfr3WaJSfZC0BLLyi BzTsKpwwt4+1LCxmktTqqDQvahaQ8uA4URbjLelJES0i6FV++gkaNWoaG Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="430708155" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="430708155" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10679"; a="683267565" X-IronPort-AV: E=Sophos;i="5.99,195,1677571200"; d="scan'208";a="683267565" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.249.34.252]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Apr 2023 01:23:34 -0700 From: Adrian Hunter To: Peter Zijlstra Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , 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 Message-Id: <20230414082300.34798-6-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230414082300.34798-1-adrian.hunter@intel.com> References: <20230414082300.34798-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763139382404947333?= X-GMAIL-MSGID: =?utf-8?q?1763139382404947333?= 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 --- 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)