Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp808833rdb; Fri, 8 Sep 2023 18:18:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEwc547fpmP4du2mLWVP7MgMB/ZI5wiU9ESq5S4l2wiGppnb/6YeKCJK9gpQsOVkzjqhCfy X-Received: by 2002:ac2:446f:0:b0:500:a08e:2fcf with SMTP id y15-20020ac2446f000000b00500a08e2fcfmr2679062lfl.47.1694222290947; Fri, 08 Sep 2023 18:18:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694222290; cv=none; d=google.com; s=arc-20160816; b=g37l/2tfvwJxN600UYMRc6RlhYLUgwMOgYDcffgwxjHDauzjynWcxm3K0x0s+f+n0b tMy/i1HLUus18ELmiqUbg6d9B6/JxR+qR8JCHuNB1QSP0tgLIAQZ+v9Fk6GxgHYvkKGo JxTVH4VJOH1DW9o9RikAkN6EblR6lCV/P7f14u6YsYmQIV6F2yH+vGTH5tjgvO9NIfP1 STXAfEpDdrZ3cvCU6ujeK2BXObB4HWameQI8A1RDOV63MhtP8lHbclWOKAhkzyySqhAF 6Ii42FA6TfFeBWuujEhyuqZ+/8F+1pgp4xPCyhjmHl+FGJsm0ynwOY+jDStFVUaQ60Zr TMjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=jrm9nFM+dmT+wC8EAx2G6BoiUrbJkZ2V45kBkAqBx/k=; fh=TijIssVzKHJFRKJKDj7Rwb0PHGe20IitS1Z1or/+geg=; b=0x4MpOJ7GsZmJ4so3cTWUsp3OsSfdhNpUVkSvaZ3sO5aSeTxVMRkK9nsX7GLF3ZAXZ r/9ceSWiJIXRdHbzKWq3FgCWHrq4PwqM8mQR676Pkbf0VE7xzqK4jHy1zF0SL6keA5d6 JU4BbT19L8OXEKEHaVExDlgfpIyLlqNxMso/XCsWpYf2FjZfos1b4JaTnfSBm3UaIop8 jEox8ExFqNzINZHb1Z8TlpXYILjO2vxnF9z8fajLbk8Pi+ZqS117Oaft9UYG5SDpsvZD 6whHHYKuVotZK1tSnqhl9Yl3OBECuBeKkroWqQs3QyCa6qr5OFP2wS4jmwOAs6YNmzSw S5pA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=e8bMNBiZ; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b4-20020aa7df84000000b00527d44d0456si2530830edy.456.2023.09.08.18.17.42; Fri, 08 Sep 2023 18:18:10 -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=@gmail.com header.s=20221208 header.b=e8bMNBiZ; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343565AbjIHW5F (ORCPT + 99 others); Fri, 8 Sep 2023 18:57:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243112AbjIHW5D (ORCPT ); Fri, 8 Sep 2023 18:57:03 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1F8E1FEF; Fri, 8 Sep 2023 15:56:57 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id ffacd0b85a97d-31c73c21113so2321658f8f.1; Fri, 08 Sep 2023 15:56:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694213816; x=1694818616; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=jrm9nFM+dmT+wC8EAx2G6BoiUrbJkZ2V45kBkAqBx/k=; b=e8bMNBiZ0MCbrs6is6UVebdOVV4R19ua5m/ut+GrbWvgDQmHzjLqQOj8lVdKNO8D2Z 8T5JQd4JCGAG5sQBXbE2kdPZVmlnoNlw3MriH0yMpH8kfvOPYyJRnhWMJvL+gMw2pC7u 4d2NalH7kB4vRujc9uu//E/t0p+ZY8ynT/where2blNZ7QBIBt7NsLZ184/JOHVZFjYr 34OIpfRmjYh+kpr/fN3dAt9WT2toTgxjfjJbbxE37bNE2Bb4+gAykXxU+iIQE29hX9Vp Dc5hUN6QaiS0PMHdUMiMuknrfIODKVIrbZCe3pAIwnHmfOeK58YJFNsUJe0AcPU6Bw/7 CXdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694213816; x=1694818616; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jrm9nFM+dmT+wC8EAx2G6BoiUrbJkZ2V45kBkAqBx/k=; b=dd0XwtGZDKATNHTyeryoZsLJvba8OlpxGKPSse6jCD3M/y5HeDQC3CQ3zyREUVkNVj I74jmLtMxlZhvMK+7soy6TMLISFCtIo6wLmXEecL9rZ7T8Z/oNcVHFNIfZaGKzTRAGbv z3CNXdmapFRynHIZHpzFKfk7+H2MWd3zrHIYuOZjWOzkMorlLWI/ZRpE9UkUSmUrr3EZ 76lqXhDquVjkW9npIe4OO5DtEnyknybwoWB2KE0S/nK1PjAWQy9ZxFP3mqEedYf6dwqc e/FS88ZXOibyn1dDsBqPeuSc+gUW5E113oIAHfVw3Er132m4d3j9kze8JRCAejQcVQT2 Vy5Q== X-Gm-Message-State: AOJu0Yx4M0xACtBKgX4GKw1Z0Xf9E147CzKCg8rga1KKjfQQaBidj1it H3JwPX0FCCeSH7I8kfTyDFKolgd0jmxmn/YL+tQ= X-Received: by 2002:adf:fa4f:0:b0:317:e1fb:d56a with SMTP id y15-20020adffa4f000000b00317e1fbd56amr2936307wrr.40.1694213815837; Fri, 08 Sep 2023 15:56:55 -0700 (PDT) MIME-Version: 1.0 References: <169280372795.282662.9784422934484459769.stgit@devnote2> <169280378611.282662.4078983611827223131.stgit@devnote2> <20230826105632.e3eb35fc69a65ebaf11c7741@kernel.org> <20230906092845.ce5ff494d379c73cef58cb08@kernel.org> In-Reply-To: <20230906092845.ce5ff494d379c73cef58cb08@kernel.org> From: Andrii Nakryiko Date: Fri, 8 Sep 2023 15:56:43 -0700 Message-ID: Subject: Re: [PATCH v4 5/9] ftrace: Add ftrace_partial_regs() for converting ftrace_regs to pt_regs To: Masami Hiramatsu Cc: Alexei Starovoitov , Steven Rostedt , Florent Revest , linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov , Jiri Olsa , Arnaldo Carvalho de Melo , Daniel Borkmann , Alan Maguire , Mark Rutland , Peter Zijlstra , Thomas Gleixner Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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 On Tue, Sep 5, 2023 at 5:28=E2=80=AFPM Masami Hiramatsu wrote: > > On Tue, 5 Sep 2023 12:50:28 -0700 > Andrii Nakryiko wrote: > > > On Fri, Aug 25, 2023 at 6:56=E2=80=AFPM Masami Hiramatsu wrote: > > > > > > On Fri, 25 Aug 2023 14:49:48 -0700 > > > Andrii Nakryiko wrote: > > > > > > > On Wed, Aug 23, 2023 at 8:16=E2=80=AFAM Masami Hiramatsu (Google) > > > > wrote: > > > > > > > > > > From: Masami Hiramatsu (Google) > > > > > > > > > > Add ftrace_partial_regs() which converts the ftrace_regs to pt_re= gs. > > > > > If the architecture defines its own ftrace_regs, this copies part= ial > > > > > registers to pt_regs and returns it. If not, ftrace_regs is the s= ame as > > > > > pt_regs and ftrace_partial_regs() will return ftrace_regs::regs. > > > > > > > > > > Signed-off-by: Masami Hiramatsu (Google) > > > > > Acked-by: Florent Revest > > > > > --- > > > > > Changes in v3: > > > > > - Fix to use pt_regs::regs instead of x. > > > > > - Return ftrace_regs::regs forcibly if HAVE_PT_REGS_COMPAT_FTRA= CE_REGS=3Dy. > > > > > - Fix typo. > > > > > - Fix to copy correct registers to the pt_regs on arm64. > > > > > Changes in v4: > > > > > - Change the patch order in the series so that fprobe event can= use this. > > > > > --- > > > > > arch/arm64/include/asm/ftrace.h | 11 +++++++++++ > > > > > include/linux/ftrace.h | 17 +++++++++++++++++ > > > > > 2 files changed, 28 insertions(+) > > > > > > > > > > diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include= /asm/ftrace.h > > > > > index ab158196480c..5ad24f315d52 100644 > > > > > --- a/arch/arm64/include/asm/ftrace.h > > > > > +++ b/arch/arm64/include/asm/ftrace.h > > > > > @@ -137,6 +137,17 @@ ftrace_override_function_with_return(struct = ftrace_regs *fregs) > > > > > fregs->pc =3D fregs->lr; > > > > > } > > > > > > > > > > +static __always_inline struct pt_regs * > > > > > +ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_r= egs *regs) > > > > > +{ > > > > > + memcpy(regs->regs, fregs->regs, sizeof(u64) * 9); > > > > > + regs->sp =3D fregs->sp; > > > > > + regs->pc =3D fregs->pc; > > > > > + regs->regs[29] =3D fregs->fp; > > > > > + regs->regs[30] =3D fregs->lr; > > > > > > > > I see that orig_x0 from pt_regs is used on arm64 to get syscall's > > > > first parameter. And it seems like this ftrace_regs to pt_regs > > > > conversion doesn't touch orig_x0 at all. Is it maintained/preserved > > > > somewhere else, or will we lose the ability to get the first syscal= l's > > > > argument? > > > > > > Thanks for checking it! > > > > > > Does BPF uses kprobe probe to trace syscalls? Since we have raw_sysca= ll > > > trace events, no need to use kprobe to do that. (and I don't recommen= d to > > > use kprobe to do such fixed event) > > > > Yeah, lots of tools and projects actually trace syscalls with kprobes. > > I don't think there is anything we can do to quickly change that, so > > we should avoid breaking all of them. > > Yes, ah, but anyway, this is the fprobe case, not kprobe. Do you use > multi_kprobes for tracing syscalls? > > Jiri, do you know when the multi-kprobe feature is used? Is that used > implicitly or explicitly? ah, ok, so all this fprobe machinery is not used for (single-)kprobes? This makes it a bit less painful for end users, I believe most syscall tracing kprobes right now are not multi-kprobes. > > > > > So getting back to my original question, is it possible to preserve ori= g_x0? > > I'm curious that the orig_x0 is stored to pt_regs of the kprobes itself > because it is not a real register. There is no way to access it. You can > use regs->x0 instead of that. > > I think the orig_x0 is stored when the syscall happened because it has > another user pt_regs on the stack, right? > > If so, we don't need to save orig_x0 on the pt_regs for kprobes, but user= can > dig the stack to find the orig_x0. Here the arm64, syscall entry handler, > > static void el0_svc_common(struct pt_regs *regs, int scno, int sc_nr, > const syscall_fn_t syscall_table[]) > { > unsigned long flags =3D read_thread_flags(); > > regs->orig_x0 =3D regs->regs[0]; > regs->syscallno =3D scno; > > (BTW, it seems syscall number is saved on regs->syscallno.) > > It seems that if you probe the el0_svc_common() for tracing syscall, > all you need is tracing $arg1 =3D=3D pt_regs and $arg2 =3D=3D syscall num= ber. > > Thank you, > > > > > > > > > > > > > > Looking at libbpf's bpf_tracing.h, other than orig_x0, I think all = the > > > > other registers are still preserved, so this seems to be the only > > > > potential problem. > > > > > > Great! > > > > > > Thank you, > > > > > > > > > > > > > > > > + return regs; > > > > > +} > > > > > + > > > > > int ftrace_regs_query_register_offset(const char *name); > > > > > > > > > > int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec); > > > > > diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h > > > > > index c0a42d0860b8..a6ed2aa71efc 100644 > > > > > --- a/include/linux/ftrace.h > > > > > +++ b/include/linux/ftrace.h > > > > > @@ -165,6 +165,23 @@ static __always_inline struct pt_regs *ftrac= e_get_regs(struct ftrace_regs *fregs > > > > > return arch_ftrace_get_regs(fregs); > > > > > } > > > > > > > > > > +#if !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS) || \ > > > > > + defined(CONFIG_HAVE_PT_REGS_TO_FTRACE_REGS_CAST) > > > > > + > > > > > +static __always_inline struct pt_regs * > > > > > +ftrace_partial_regs(struct ftrace_regs *fregs, struct pt_regs *r= egs) > > > > > +{ > > > > > + /* > > > > > + * If CONFIG_HAVE_PT_REGS_TO_FTRACE_REGS_CAST=3Dy, ftrace= _regs memory > > > > > + * layout is the same as pt_regs. So always returns that = address. > > > > > + * Since arch_ftrace_get_regs() will check some members a= nd may return > > > > > + * NULL, we can not use it. > > > > > + */ > > > > > + return &fregs->regs; > > > > > +} > > > > > + > > > > > +#endif /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS || CONFIG_HAVE_P= T_REGS_TO_FTRACE_REGS_CAST */ > > > > > + > > > > > /* > > > > > * When true, the ftrace_regs_{get,set}_*() functions may be use= d on fregs. > > > > > * Note: this can be true even when ftrace_get_regs() cannot pro= vide a pt_regs. > > > > > > > > > > > > > > > > > > > -- > > > Masami Hiramatsu (Google) > > > -- > Masami Hiramatsu (Google)