From patchwork Mon Apr 8 09:01:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13620825 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 C4974CD128A for ; Mon, 8 Apr 2024 09:02:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2FA9E6B0083; Mon, 8 Apr 2024 05:02:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2AD2A6B0085; Mon, 8 Apr 2024 05:02:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 173AF6B0087; Mon, 8 Apr 2024 05:02:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id ED9DF6B0083 for ; Mon, 8 Apr 2024 05:02:50 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7B176808BD for ; Mon, 8 Apr 2024 09:02:50 +0000 (UTC) X-FDA: 81985774500.17.F50FC89 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf13.hostedemail.com (Postfix) with ESMTP id C145020017 for ; Mon, 8 Apr 2024 09:02:48 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=n82mI9XU; spf=pass (imf13.hostedemail.com: domain of 3t7ITZgUKCKIGNXGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--elver.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3t7ITZgUKCKIGNXGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712566968; 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=dRUfOG22gFtzSxXckMYpwWSYVbuVuH++MvjmfSoLS8Y=; b=Qnplhql6gy7MZRpYyDnyBpf0wtN3vSXwjyihv4PVcAuHu6hBFJEqg8RahvoRlUds5sUgWu Z1o0OgR2i0n5v1xePeVFVoo1lw07dNTrIRKdMGID2CRYZAVuaYDHpSCgsZ7BPV8hZrPBuw uRWjKLpaQXu2PXbTE/hinBuqRpJY8oU= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=n82mI9XU; spf=pass (imf13.hostedemail.com: domain of 3t7ITZgUKCKIGNXGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--elver.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3t7ITZgUKCKIGNXGTIQQING.EQONKPWZ-OOMXCEM.QTI@flex--elver.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712566968; a=rsa-sha256; cv=none; b=jBJESYHDCpbY6f3rHOx6Co9+6DA7QFtilBX4rOLzrEku7tcm6bb1cDCZw+NpDzU2pWRNYA XbWcgDI1iwUrMuKW3e4ipnoGemA3IodROeMc1POrx520YoRf2rwW+KNdFGviMObnhJsUGY WfRSoJsB5N/L0uPsfabwRg3WJFOVeLQ= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dbe9e13775aso6839463276.1 for ; Mon, 08 Apr 2024 02:02:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712566968; x=1713171768; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=dRUfOG22gFtzSxXckMYpwWSYVbuVuH++MvjmfSoLS8Y=; b=n82mI9XUOcCbeVdCoHugztU7DeE/jguQse0Um7fkUL0VYQorEUcFS4wlYceK0ORZ9g 6IvCHT8G39G1AxB1zxFFse94yE+DuoPr6wiYlIr2EvrKft0OslbJPq6nRFiUOcPwI21Z cuCJLE5S/AplMO7NvPxOUR5DEEL/gV8CKJ7nVeFqKVg6RG0qShF9kllOOJcqd6ydkFx4 RePdt72cFXZEDqTDtZQ0bOFmmz1DC1VcyJaNBYAqyEK3qk1fVWBSsRhCOio1DboQH7Zb 8RUMI7TqO+kdqcdqx8c0DpNq7KEwxR+v2tDdO1W9ftuwpJIKKlMANt2tTBXOfEUKHPVZ Bcew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712566968; x=1713171768; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=dRUfOG22gFtzSxXckMYpwWSYVbuVuH++MvjmfSoLS8Y=; b=htbfNCvVu9fF2HseIufyin73PFAMeHPszhBw5i2XUP7xfb4Ixh3CP2DAlcrOssnlBH 11vRFYsvtIWZRgrg8BDAKG4/Y2cuhp+b/sm8ehFm3UwE4vGjNm+MwIMu5INUxp+knhCU yZ9XYwZ9NldUPx1y46SJEA4m/KenKppq8XOgi45rx2fIAIZIoeHGHVs9nAhqoQJNsXnM yFNl5InvsYl3ym5z2s7k6iFCnfnjmA8CeeTpfZBLUub92+fgl6F/L7ohhmZ99iaRpMao jmkwsYkOCLeY/ER138Z4izoDmkVq+xgE1shOmYSQ7sNdSBDu5WIUXwc/3Tj9vlG4gsGC LTfQ== X-Forwarded-Encrypted: i=1; AJvYcCXFTVBQ6Q2GSMyr3HNJtrH9eUHNulGm2cI7YftW1yKAIndHbN7CrMrHSaJ9SIFVHhaMozBjQtdkLpVn5uJv3nHl1oE= X-Gm-Message-State: AOJu0YxmeLNqlR19iVYBpsHhpwfHUsnbbQtqLzeJvhYMLgFXI2Tx+rCX JwgYO5t5MfSPxKrOjRpjfwozSO+AeUYRBqLlaXswsI1POkkIHGh2rk9rTOfUvjJSM95OYAUAqA= = X-Google-Smtp-Source: AGHT+IF7fNc9CfA3rN0FPE5cCB/FJRCFpa71D/Uyw6NdHdjHjjwDmKUZCoP0gpqMqi3bqfDBxO1+PAF/cA== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:ea4d:dc81:62c5:eeb9]) (user=elver job=sendgmr) by 2002:a05:6902:c09:b0:dcd:b431:7f5b with SMTP id fs9-20020a0569020c0900b00dcdb4317f5bmr2823872ybb.0.1712566967683; Mon, 08 Apr 2024 02:02:47 -0700 (PDT) Date: Mon, 8 Apr 2024 11:01:54 +0200 Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240408090205.3714934-1-elver@google.com> Subject: [PATCH] tracing: Add new_exec tracepoint From: Marco Elver To: elver@google.com, Steven Rostedt Cc: Eric Biederman , Kees Cook , Alexander Viro , Christian Brauner , Jan Kara , Masami Hiramatsu , Mathieu Desnoyers , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, Dmitry Vyukov X-Rspamd-Queue-Id: C145020017 X-Rspam-User: X-Stat-Signature: 6co8o4bu8t6hckwqrne8jpi84c5hm33e X-Rspamd-Server: rspam01 X-HE-Tag: 1712566968-629129 X-HE-Meta: U2FsdGVkX19rdRBDRJFpBN0eSli03zMzAtD9BAblHUuDDHieCIeGa88OrgZ2HK0c4aRo5HpkmHuUMjuBwOBYxZxpAtrxhPhivah/152JMFLikNJiowMMQrNUQiLlisv4Q5NKi6FBy0C1Yx1zhV85X6sIUtkmdwklzVNyBjd29aJpTXgCSgQoQLcgNKZQIoMrkRy+HyTG5N/OxjCUBItHDjisZoUPvZR/T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UXblM4fA8cWf2P6VyZwMPvyeP2U2pI20Xa5bWTtx/CmgsV1pnd9bXA2Tc+K1tmfO+83OvvUnFdA7TiDnBDl5ztN0w5TyW0KlxSgwvSO5Zm5Dfp7NNwvoIO9LPP5C8DfyP++CndACNRccuhkUUUu+Iw3wutyIUHJTvVmwlWOM8+kBZnWMSb5OUuKohFh7WdNV38ACLiTGHsfVhK/WH73ziUYL7CkkCqFWAshEFM3dE/4ha189zJdwM0GJ7N58Rm8NDZw7cWufMNw8ued7gCUWnhn4y8HtZ6PL1xXOstur+3b3SKAPD0SLTCguplR1BjpFem3j/ZVvQAWzf3yodaxzQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 "new_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 "new_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 ("new_exec" to "sched_process_exec"). Example of tracing output ("new_exec" and "sched_process_exec"): $ cat /sys/kernel/debug/tracing/trace_pipe <...>-379 [003] ..... 179.626921: new_exec: filename=/usr/bin/sshd pid=379 comm=sshd <...>-379 [003] ..... 179.629131: sched_process_exec: filename=/usr/bin/sshd pid=379 old_pid=379 <...>-381 [002] ..... 180.048580: new_exec: filename=/bin/bash pid=381 comm=sshd <...>-381 [002] ..... 180.053122: sched_process_exec: filename=/bin/bash pid=381 old_pid=381 <...>-385 [001] ..... 180.068277: new_exec: filename=/usr/bin/tty pid=385 comm=bash <...>-385 [001] ..... 180.069485: sched_process_exec: filename=/usr/bin/tty pid=385 old_pid=385 <...>-389 [006] ..... 192.020147: new_exec: filename=/usr/bin/dmesg pid=389 comm=bash bash-389 [006] ..... 192.021377: sched_process_exec: filename=/usr/bin/dmesg pid=389 old_pid=389 Signed-off-by: Marco Elver --- fs/exec.c | 2 ++ include/trace/events/task.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index 38bf71cbdf5e..ab778ae1fc06 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1268,6 +1268,8 @@ int begin_new_exec(struct linux_binprm * bprm) if (retval) return retval; + trace_new_exec(current, bprm); + /* * Ensure all future errors are fatal. */ diff --git a/include/trace/events/task.h b/include/trace/events/task.h index 47b527464d1a..8853dc44783d 100644 --- a/include/trace/events/task.h +++ b/include/trace/events/task.h @@ -56,6 +56,36 @@ TRACE_EVENT(task_rename, __entry->newcomm, __entry->oom_score_adj) ); +/** + * new_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, but at the point of no return during + * switching to the new exec. + */ +TRACE_EVENT(new_exec, + + TP_PROTO(struct task_struct *task, struct linux_binprm *bprm), + + TP_ARGS(task, bprm), + + TP_STRUCT__entry( + __string( filename, bprm->filename ) + __field( pid_t, pid ) + __string( comm, task->comm ) + ), + + TP_fast_assign( + __assign_str(filename, bprm->filename); + __entry->pid = task->pid; + __assign_str(comm, task->comm); + ), + + TP_printk("filename=%s pid=%d comm=%s", + __get_str(filename), __entry->pid, __get_str(comm)) +); + #endif /* This part must be outside protection */