Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp948190rdd; Wed, 10 Jan 2024 04:33:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IGSTwqN4897s09e8NQsIzAFdFsUTQZDHIPXXNdPUog1jdeWn1NYNA8KH8a1YkDpSJsTlyG+ X-Received: by 2002:a05:6358:78c:b0:175:6b16:5902 with SMTP id n12-20020a056358078c00b001756b165902mr1051761rwj.46.1704890025908; Wed, 10 Jan 2024 04:33:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704890025; cv=none; d=google.com; s=arc-20160816; b=vn3IWQ6RxJffzjMWzsGZBJ3FZ6labd3T8WuuOVMPJBuO026otPUj14WLquV8JgEN6u 05xyoxDoxJ3qSL6vSwMGEvqz7HujBiq/LTgxX4hyVTJuu40GG1Sh2MLlzj+wl0t7wQfy MGvg8Ei/TdJPqGlJ0Yx9cqUorydtZnVkfnGLfqTHEcmyIDax3n5oSqYagctxZx7YOEUk /a0SrCC30TK5tr2Zd2GvY9muBD547ChAgGJoMY5ULW6AbSr1shphR/AS/l0gzucj8bfD 3/Z7Hd4kMREslJPBffGfa+w/GCJF/zT1VbNce1wTNLEYyScw8RHLIF9gkjv/n/v0spUE 4HJQ== ARC-Message-Signature: i=1; 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 :subject:cc:to:from:date:dkim-signature; bh=eUqoUJnUPIaONs5tjG2SKGSpOdCsFBO89qkFZq/Qi9A=; fh=ddlSF86e4sNrL5lUSeOUPUysN2FpiEdPu9GlN994glw=; b=bCbm4w1zp5qK1YoAQF3QeShUHG2Npxpf3/vIQmyc8U3R/Kf8JzOo+K4Dka/w3zngF3 VxzzObPvjzwfpzN3LLqkoK2NxChv0mQgI0qiBKIbgk3SlSQ25tuYkSQ5nqu0hAtai8sJ 8fN4IdnsJ2esFpNS3wq94WRo0fWVp6Gl3N5Opkho8fmWLMNku33b8olB3vGWORrlqz1G 7aRnA5JW++nEtB3DU63I1CilNqyGoYncnOyo1se7VHp/7LnHh36R7Fkzbi6jDxP8+RSf ey2UVPlGjEfGT5JU/d0wjWQFg/uw7BfET+LaJWgQ6jFErJs0Ck8iL3mG9yUeLO6pT+/C 7uhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bQz2aoF1; spf=pass (google.com: domain of linux-kernel+bounces-22173-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-22173-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id r6-20020a632b06000000b005cdc07fbef4si3655875pgr.736.2024.01.10.04.33.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 04:33:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-22173-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bQz2aoF1; spf=pass (google.com: domain of linux-kernel+bounces-22173-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-22173-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 874122830CD for ; Wed, 10 Jan 2024 12:33:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 14DA845BEE; Wed, 10 Jan 2024 12:33:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bQz2aoF1" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EBA048780; Wed, 10 Jan 2024 12:33:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 18BDBC433F1; Wed, 10 Jan 2024 12:33:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1704890011; bh=u/jcroJ0nISOYgVRITd+AfjQp+CSDwFhB6tqs0dV3r0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=bQz2aoF1ehdjk3NbLgfPHDxdj7bXfHJUb+s60KfeD8+9OTicnDmT2OdZIVG6xo0me uBpMKEz9E328rN1WhNk6AtIPV4DOxSqIwc9GBb0cq5sDv5BT6Trh9QGDmgu/WhkE2L fB2fR6cKg+3IAjKuynnpFPFMW+HrJoQphgd3YRlAsvJd28rG6dj6+6ta8JbbtbihjI PAnCuw7W3DyjA97Taa/eYS3p2KZZjt558G0gfp5IN2JYThy1UuNhoHVPCDgYPNJv+1 4xuB0PGc7L2/j8/2zSHOfWTMas/5E6h2ifQnmj/TBlPQDBWhPSjIjaMKLPByj4OBRf xp8+XS+VaZylw== Date: Wed, 10 Jan 2024 21:33:27 +0900 From: Masami Hiramatsu (Google) To: Mark Rutland Cc: Steven Rostedt , Jiri Olsa , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, Florent Revest Subject: Re: [PATCH] ftrace: Fix DIRECT_CALLS to use SAVE_REGS by default Message-Id: <20240110213327.9f3fa220e3d6b5222d2892e7@kernel.org> In-Reply-To: References: <170484558617.178953.1590516949390270842.stgit@devnote2> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 10 Jan 2024 12:20:21 +0000 Mark Rutland wrote: > On Wed, Jan 10, 2024 at 09:13:06AM +0900, Masami Hiramatsu (Google) wrote: > > From: Masami Hiramatsu (Google) > > > > The commit 60c8971899f3 ("ftrace: Make DIRECT_CALLS work WITH_ARGS > > and !WITH_REGS") changed DIRECT_CALLS to use SAVE_ARGS when there > > are multiple ftrace_ops at the same function, but since the x86 only > > support to jump to direct_call from ftrace_regs_caller, when we set > > the function tracer on the same target function on x86, ftrace-direct > > does not work as below (this actually works on arm64.) > > > > At first, insmod ftrace-direct.ko to put a direct_call on > > 'wake_up_process()'. > > > > # insmod kernel/samples/ftrace/ftrace-direct.ko > > # less trace > > ... > > -0 [006] ..s1. 564.686958: my_direct_func: waking up rcu_preempt-17 > > -0 [007] ..s1. 564.687836: my_direct_func: waking up kcompactd0-63 > > -0 [006] ..s1. 564.690926: my_direct_func: waking up rcu_preempt-17 > > -0 [006] ..s1. 564.696872: my_direct_func: waking up rcu_preempt-17 > > -0 [007] ..s1. 565.191982: my_direct_func: waking up kcompactd0-63 > > > > Setup a function filter to the 'wake_up_process' too, and enable it. > > > > # cd /sys/kernel/tracing/ > > # echo wake_up_process > set_ftrace_filter > > # echo function > current_tracer > > # less trace > > ... > > -0 [006] ..s3. 686.180972: wake_up_process <-call_timer_fn > > -0 [006] ..s3. 686.186919: wake_up_process <-call_timer_fn > > -0 [002] ..s3. 686.264049: wake_up_process <-call_timer_fn > > -0 [002] d.h6. 686.515216: wake_up_process <-kick_pool > > -0 [002] d.h6. 686.691386: wake_up_process <-kick_pool > > > > Then, only function tracer is shown on x86. > > But if you enable 'kprobe on ftrace' event (which uses SAVE_REGS flag) > > on the same function, it is shown again. > > > > # echo 'p wake_up_process' >> dynamic_events > > # echo 1 > events/kprobes/p_wake_up_process_0/enable > > # echo > trace > > # less trace > > ... > > -0 [006] ..s2. 2710.345919: p_wake_up_process_0: (wake_up_process+0x4/0x20) > > -0 [006] ..s3. 2710.345923: wake_up_process <-call_timer_fn > > -0 [006] ..s1. 2710.345928: my_direct_func: waking up rcu_preempt-17 > > -0 [006] ..s2. 2710.349931: p_wake_up_process_0: (wake_up_process+0x4/0x20) > > -0 [006] ..s3. 2710.349934: wake_up_process <-call_timer_fn > > -0 [006] ..s1. 2710.349937: my_direct_func: waking up rcu_preempt-17 > > > > To fix this issue, use SAVE_REGS flag for multiple ftrace_ops flag of > > direct_call by default. > > > > Fixes: 60c8971899f3 ("ftrace: Make DIRECT_CALLS work WITH_ARGS and !WITH_REGS") > > Cc: stable@vger.kernel.org > > Signed-off-by: Masami Hiramatsu (Google) > > Sorry about this; I hadn't realised that x86 only supported direct calls when > SAVE_REGS was requested. Yeah, it is hard to find without my fprobe on fgraph series because all probes (kprobe/fprobe) and function-graph tracer uses SAVE_REGS. Only function tracer hits this issue, but usually ftrace-direct user will not use it with function tracer. So we were lucky to find it :) > > The patch looks good to me. I applied it atop v6.7 and double-checked that this > still works on arm64 as per your examples above, and everything looks good: > > # mount -t tracefs none /sys/kernel/tracing/ > # insmod ftrace-direct.ko > # echo wake_up_process > /sys/kernel/tracing/set_ftrace_filter > # echo function > /sys/kernel/tracing/current_tracer > # less /sys/kernel/tracing/trace > .. > -0 [007] ..s3. 172.932840: wake_up_process <-process_timeout > -0 [007] ..s1. 172.932842: my_direct_func: waking up kcompactd0-62 > -0 [007] ..s3. 173.444836: wake_up_process <-process_timeout > -0 [007] ..s1. 173.444838: my_direct_func: waking up kcompactd0-62 > -0 [001] d.h5. 173.471116: wake_up_process <-kick_pool > -0 [001] d.h3. 173.471118: my_direct_func: waking up kworker/1:1-58 > > Reviewed-by: Mark Rutland > Tested-by: Mark Rutland [arm64] Thank you! > > Thanks, > Mark. > > > --- > > kernel/trace/ftrace.c | 10 ++++++++++ > > 1 file changed, 10 insertions(+) > > > > diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c > > index b01ae7d36021..c060d5b47910 100644 > > --- a/kernel/trace/ftrace.c > > +++ b/kernel/trace/ftrace.c > > @@ -5325,7 +5325,17 @@ static LIST_HEAD(ftrace_direct_funcs); > > > > static int register_ftrace_function_nolock(struct ftrace_ops *ops); > > > > +/* > > + * If there are multiple ftrace_ops, use SAVE_REGS by default, so that direct > > + * call will be jumped from ftrace_regs_caller. Only if the architecture does > > + * not support ftrace_regs_caller but direct_call, use SAVE_ARGS so that it > > + * jumps from ftrace_caller for multiple ftrace_ops. > > + */ > > +#ifndef HAVE_DYNAMIC_FTRACE_WITH_REGS > > #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_ARGS) > > +#else > > +#define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS) > > +#endif > > > > static int check_direct_multi(struct ftrace_ops *ops) > > { > > > -- Masami Hiramatsu (Google)