From mboxrd@z Thu Jan 1 00:00:00 1970 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 241B1C4345F for ; Thu, 11 Apr 2024 15:56:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A03E96B0095; Thu, 11 Apr 2024 11:56:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B2EC6B0096; Thu, 11 Apr 2024 11:56:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 853B86B0098; Thu, 11 Apr 2024 11:56:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 66FE76B0095 for ; Thu, 11 Apr 2024 11:56:34 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2E2B580AC7 for ; Thu, 11 Apr 2024 15:56:34 +0000 (UTC) X-FDA: 81997703508.25.79C298A Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf01.hostedemail.com (Postfix) with ESMTP id C05DE40025 for ; Thu, 11 Apr 2024 15:56:31 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UShYJHoo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of mhiramat@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=mhiramat@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712850992; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=urnz0SJFo57kFQaiQmhCRU6vHt/qU0KMf1s2ItTjR+8=; b=PX7tP2s+RmSpGwsUUydlImW1s2fQpBFqhys7Ln+7PVVnKhpD6fgk9VQm+Ktn/AYoysMEeU fT/aTP2HrkS91MPajUbKmn+HDfSCGjCQGXTRj1JRGf6dBuHmDkJO7Wn2udrXmgqWlTG2DV lvEMcrJwPLTovnsmBF9z20MQHApUG3g= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UShYJHoo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of mhiramat@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=mhiramat@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712850992; a=rsa-sha256; cv=none; b=w7kZ5gCM9K9phzhwPgsfBYjirJRccz0uAYzyTsUbsiou76Xy3dK5ZliEmhMMh5VeWSl7zf J5gsTaeh1EFmFj1SxWrzja9fQqLhBl2+X0cyRWrgOGXgPxi/ViawDnIn1Y5e04pJzVOFaN LSWu2B+ZTUc2/Ft8LHXX3r3XzBllydA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id BC6EDCE11A3; Thu, 11 Apr 2024 15:56:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE3EEC113CD; Thu, 11 Apr 2024 15:56:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712850987; bh=bbnRRo7spo5+ab0ewdvfbug8lWhA1ZDBE8cVFJKpfRk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=UShYJHook8wRAmez4mDPl6cjJjQJ5JqjbzROxMgvciHBQcjxSderHpXdIEpc9FlV7 h0HoHxiABAxMVu6eEHykY992zhZQoVVD0D0zJ1X2HlKqU/dzwwarW+o9cwe6rQ4bLJ tB3aHCjGRCvZ5SA99SDcxdVGlgSKCQT1vIeqJIW4kMZNFeV5tprEwiPXbs4JuOum3e 5JnkCegGndS975r/vZauTGHhaTjep/kdOsl+/KcOBYlxPEMwuPiKMKW8JiYrCirmXB w/ZPkmn+ySBDQh3C5tA+Ykf7vHb27F0vYuyvqTd9QBDDhEbpnH927Qd3xdyAajmAlW gOUaD7+STQaRg== Date: Fri, 12 Apr 2024 00:56:21 +0900 From: Masami Hiramatsu (Google) To: Marco Elver Cc: Steven Rostedt , 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 Subject: Re: [PATCH v2] tracing: Add sched_prepare_exec tracepoint Message-Id: <20240412005621.205fd8cf208d98f2e9511ce1@kernel.org> In-Reply-To: <20240411102158.1272267-1-elver@google.com> References: <20240411102158.1272267-1-elver@google.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: C05DE40025 X-Stat-Signature: erm8i5zcs8exzb54grqctqi1rr7dxef5 X-HE-Tag: 1712850991-718040 X-HE-Meta: U2FsdGVkX1/ahhyh+qwCmEsbEtca4GtoBz3S4JUYrZvApikHzXYvMUEbQsUc6uGwcrlKNPzl9anl6KdFvWcGqcxzocaAinXC0PinD2W+g4165kAJprc9Ira4C2YM04yNpAeCdaYCNHiZ0A8Lb0uacq54pqdQVHln3MMapdTozOgaEcb1nWh98q6Px75vFGRAliEECyFvff90r20znMaTZFNtUEP2yp2ivQHEjSlMew1oh61kwzCfvMGJP+WJCzT7VPWgSmH0dHpta5UtepEg4ukLQO3MTBYXNrSkkfGRxVwt8iV/35EtphKmMaC5lviAZtIFpJfmevJUkW8OENYpi5vEJ3HR6gRD06u9Grr+leLsjMTG10y1gqC2aHFFKmg6qnRT1AQnus5dbYcDlpB91KWfCRY+omC9oNFHHCHi8qI04Kf3+BLL6GYOefsPlPHUk8jL2EgczooLldjPE5fMm2SHVjCFaiG7Q6My0JoXZxxHf76s4s+XtyxglqwzgDbUkqop0zRipqEuRELeE/m3gjMq0GOBfE5zBMePatwP048nThF8mCFYTY4sxH8/nspmHSLOk/972J/2dqhth9NZGFj4O1NLGCzOho4b+JpP2aNgVyhRV2/y4nsptX3ekRV85ZtfqeYG456fwihqTaO2nOdxkPR/PFdpZ8rFabbaes7r60oUqj3OrbCzgbfNyhn9BnTs2AhmEWuRPnNiZVnf6oNz3sBbveMCYCzugNQQ3PEQrRGwxG03grn+faWfGjajWJW9D/XePU1SANE+SWOYOEmrdQbfhDG7K/j+dVY4+fA8nV8VRVln+re6gYOBv50E+jITGaJTi7Nm/yqu1OtLmW+CH9YMF7WOR3pqWtIf+mwJftIJR4X6qpJIKyms4v2gYbSB+G3LHEsKiEstl17zHm+qqrTSfHft1LcTgOqrC17KnSnhQr3b3YTPuJoJOcmRt8qat9GKEOygom4aWcT pjOpB15O wQ8SUhWA6eSMEfcITX66Zk932TNVh1yaZi0947+ikyohV6eex1t4ZsO/knYf9oCrv4AqhcUydgTc1DbNN+IOG6r2iAmsoadbpUuNlOpHLuKQLBDNzmjXKxjw6uttzDMkZI41lLRvoXJwvwouuwp1t0y6k46kjoOQm7WPJRb5CTPXtDn9kdgJqHCMGCTBoUJfjZOWyEP1quzCnk7rQ3U313F19vqf7rsjibX0JyvD3r33HFEFn2tQpJEOFYUAjccKro6KFzZtr43KSk5eMll6HKwjirylRFYqCrbsFBe+p/zH2K8bvZC0bjjbLE4BLnCuUqHmTybyQ8thBl+PgUXh26QSy4g== 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: On Thu, 11 Apr 2024 12:20:57 +0200 Marco Elver wrote: > 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 Looks good to me. Reviewed-by: Masami Hiramatsu (Google) Thanks, > --- > 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 > -- > 2.44.0.478.gd926399ef9-goog > -- Masami Hiramatsu (Google)