From patchwork Thu Apr 11 10:20:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13625676 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B6EBCD1284 for ; Thu, 11 Apr 2024 10:22:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C73E6B0087; Thu, 11 Apr 2024 06:22:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 976D06B008A; Thu, 11 Apr 2024 06:22:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 83DC56B008C; Thu, 11 Apr 2024 06:22:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 671D26B0087 for ; Thu, 11 Apr 2024 06:22:06 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id ED6A1160B3D for ; Thu, 11 Apr 2024 10:22:05 +0000 (UTC) X-FDA: 81996860610.26.176AA7C Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf28.hostedemail.com (Postfix) with ESMTP id 396B5C000B for ; Thu, 11 Apr 2024 10:22:04 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=c5HdSir+; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 3yrkXZgUKCNM3AK3G5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--elver.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3yrkXZgUKCNM3AK3G5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--elver.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712830924; a=rsa-sha256; cv=none; b=wJ2QqVS9lMhc7lBjbobSfm+zfCXr3Q8R9eo6u4oB1PhjESkiiEpQIWRkDmAfQFaPit/TMO didr+EN5bTTnTJiEIgBCBLWxToPTntoQCEL6ksUWac6P2j0E2qbevBYZxjIyHDZJshmTYA GsO34vYhfd085eZRy5Vgbfjs1NZaQ8Q= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=c5HdSir+; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 3yrkXZgUKCNM3AK3G5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--elver.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3yrkXZgUKCNM3AK3G5DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--elver.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712830924; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=+rR9HIwFgg/0oSpb0w3lX+KECeC+/l7rF3yPb/nkNJ0=; b=f7t+yTbYh5IJAi/u6uxdecvfTyNmJJ5reX0ZqiF33PBKsUXm7rGr0tp4zL4fokdSZSVioS EoyJ1s0pZ4DGCex9483CpmMOcfDt78VdUNrQuP6o6JW+OCJCHjY9GusTwhX5f8sT4KlF7a XZhA6vxInqwA5IZjYe8/BF2fOP+0saU= Received: by mail-ej1-f73.google.com with SMTP id a640c23a62f3a-a51abd0d7c6so433069666b.3 for ; Thu, 11 Apr 2024 03:22:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712830923; x=1713435723; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=+rR9HIwFgg/0oSpb0w3lX+KECeC+/l7rF3yPb/nkNJ0=; b=c5HdSir+ql5KUXigwYH0O2FZlexN/kp3XuiCAse2d8Ceq4p7Jt1HVKgzGLxecAC/rj Tvjkjz3Wd8LV9FhOCP9SH0Unw3ZRpgDwplj/+wGv6C1YQRZTb4ZyeSiQS4wmhjY/R7i3 gcDpbqdlwLTQnsDvGBCmYI561WqDTsSlGEf7oPA4H6VAdD+frAUsugLiE81cja833azX 41kyK+DrlNJ2L/LeZJpiiUgOySd0ax5uCf51gXj2UNFs/wJIAHVdm99dvx51jGSnD/zX vmuuhnnDU3+L7NoxTilw22fBnh/rcektKFGEo6VhYoEPqKnJnVkYSoGTCj6wa/2waO9P 7/xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712830923; x=1713435723; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=+rR9HIwFgg/0oSpb0w3lX+KECeC+/l7rF3yPb/nkNJ0=; b=fsfgLENNcFh7XR2nV3+FQ9DLFAO/E4yu9s8y8R869o5x0kZcO9HTPcHqkaKE2QsXFw VDq55qmxvZ9YSiwHkl+JRVUxd0N7HrkS/g9Eexb67RHhlsU19xzsfebYfXRxKFFMLvSK E47cQXqHJ9UapiB6bHlBLQqqnXRiQt+BWxiVql/ZOnvdZ6lzh8a/ZrvVnGItJdoPsjED avpwia6jVgc+mIRy8daLkRrQpXEeA6rDa55Xzm5Iii10I3xNak3ddlQ78ZJe5ZYL+usU BP6P30ZVtiirzyIc/R9UX7i26tPaId0S3ZLiXlHPDXDykrOws4JdP7gCT3g8rl24/bFq u10Q== X-Forwarded-Encrypted: i=1; AJvYcCUzymExyvbnAaPu7S0kV9GPTh523IaXpCN89ymhV6y3mB3CkHa11TLEwJyh82Sk3rpUBnQQh0q+Z/5piFDyfpPaBCw= X-Gm-Message-State: AOJu0Yxzy60D3Ul70hK7TeRylcbs71RY3zX31qHrUzVMaKKUy5HoGekZ 1mssbUDZD1nqMzukF2Y6mconUWiDxUu1SJghWo4AXbgd16kJF6ypYfP61l95J4xKlGGz/NE29A= = X-Google-Smtp-Source: AGHT+IFXG6iOFjgVBIzBx0eWBP5cDueeQuL6me8jmVrtbOPiz1Cb0gwJMrq43P1HgLFWtX3oaOdfw2/1NQ== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:5b29:b86f:ece8:1df5]) (user=elver job=sendgmr) by 2002:a17:906:857:b0:a51:abd7:85a5 with SMTP id f23-20020a170906085700b00a51abd785a5mr4613ejd.15.1712830922433; Thu, 11 Apr 2024 03:22:02 -0700 (PDT) Date: Thu, 11 Apr 2024 12:20:57 +0200 Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240411102158.1272267-1-elver@google.com> Subject: [PATCH v2] tracing: Add sched_prepare_exec tracepoint From: Marco Elver To: elver@google.com, Steven Rostedt Cc: Alexander Viro , Christian Brauner , Jan Kara , Eric Biederman , Kees Cook , Masami Hiramatsu , Mathieu Desnoyers , Azeem Shaikh , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Dmitry Vyukov X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 396B5C000B X-Stat-Signature: 435xgo4i1brh646uqmxydzhuwyo7s6gz X-HE-Tag: 1712830924-796290 X-HE-Meta: U2FsdGVkX18vV+3cvjqJJVrfBpx0TDcVqTG63/Hp+OE71alNKegXg7kYrGcEqdyITrmPhLJyw0CGudxetLDSU4CDar1o0X17yR5JMLpnAqZDDOcY5EtA+HAsA3NhEPx+s36W3VSEpg2m1XVqWao5TLQTv3E6hngIsXkJZ/+m528TaGFlLCAT621jLPIGi3rHq3TkB5vYuZuVGzTZa7CCmmNs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48tji9wRRr6SFFs+h6lfSnf61lZE7dxvZG8ReM6zLhJdPHUuiOSEuqb2h0w6/NOrxhPtRgpqCfYXlNj+BxmK9wbZibbrA/0T1eJTgkMnfL0P6FqGvHYEdWh3hv9/sgI4crrrju9zKx7I0mEgUxgX5Y35qOT4jmufMVI6lcPKc0D62bUiHtxNS3BgSAutzqt1qpQgeS5RKZZH8Gx6oNWF+c9qs+YSQY4wx6AcR05PPKWPkCtLZdQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add "sched_prepare_exec" tracepoint, which is run right after the point of no return but before the current task assumes its new exec identity. Unlike the tracepoint "sched_process_exec", the "sched_prepare_exec" tracepoint runs before flushing the old exec, i.e. while the task still has the original state (such as original MM), but when the new exec either succeeds or crashes (but never returns to the original exec). Being able to trace this event can be helpful in a number of use cases: * allowing tracing eBPF programs access to the original MM on exec, before current->mm is replaced; * counting exec in the original task (via perf event); * profiling flush time ("sched_prepare_exec" to "sched_process_exec"). Example of tracing output: $ cat /sys/kernel/debug/tracing/trace_pipe <...>-379 [003] ..... 179.626921: sched_prepare_exec: interp=/usr/bin/sshd filename=/usr/bin/sshd pid=379 comm=sshd <...>-381 [002] ..... 180.048580: sched_prepare_exec: interp=/bin/bash filename=/bin/bash pid=381 comm=sshd <...>-385 [001] ..... 180.068277: sched_prepare_exec: interp=/usr/bin/tty filename=/usr/bin/tty pid=385 comm=bash <...>-389 [006] ..... 192.020147: sched_prepare_exec: interp=/usr/bin/dmesg filename=/usr/bin/dmesg pid=389 comm=bash Signed-off-by: Marco Elver Acked-by: Kees Cook Acked-by: Steven Rostedt (Google) Reviewed-by: Masami Hiramatsu (Google) --- v2: * Add more documentation. * Also show bprm->interp in trace. * Rename to sched_prepare_exec. --- fs/exec.c | 8 ++++++++ include/trace/events/sched.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index 38bf71cbdf5e..57fee729dd92 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1268,6 +1268,14 @@ int begin_new_exec(struct linux_binprm * bprm) if (retval) return retval; + /* + * This tracepoint marks the point before flushing the old exec where + * the current task is still unchanged, but errors are fatal (point of + * no return). The later "sched_process_exec" tracepoint is called after + * the current task has successfully switched to the new exec. + */ + trace_sched_prepare_exec(current, bprm); + /* * Ensure all future errors are fatal. */ diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h index dbb01b4b7451..226f47c6939c 100644 --- a/include/trace/events/sched.h +++ b/include/trace/events/sched.h @@ -420,6 +420,41 @@ TRACE_EVENT(sched_process_exec, __entry->pid, __entry->old_pid) ); +/** + * sched_prepare_exec - called before setting up new exec + * @task: pointer to the current task + * @bprm: pointer to linux_binprm used for new exec + * + * Called before flushing the old exec, where @task is still unchanged, but at + * the point of no return during switching to the new exec. At the point it is + * called the exec will either succeed, or on failure terminate the task. Also + * see the "sched_process_exec" tracepoint, which is called right after @task + * has successfully switched to the new exec. + */ +TRACE_EVENT(sched_prepare_exec, + + TP_PROTO(struct task_struct *task, struct linux_binprm *bprm), + + TP_ARGS(task, bprm), + + TP_STRUCT__entry( + __string( interp, bprm->interp ) + __string( filename, bprm->filename ) + __field( pid_t, pid ) + __string( comm, task->comm ) + ), + + TP_fast_assign( + __assign_str(interp, bprm->interp); + __assign_str(filename, bprm->filename); + __entry->pid = task->pid; + __assign_str(comm, task->comm); + ), + + TP_printk("interp=%s filename=%s pid=%d comm=%s", + __get_str(interp), __get_str(filename), + __entry->pid, __get_str(comm)) +); #ifdef CONFIG_SCHEDSTATS #define DEFINE_EVENT_SCHEDSTAT DEFINE_EVENT