Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp804762lql; Mon, 11 Mar 2024 19:49:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWXv+DwngQiUGATkzXyJ1zK1PTkFrb6hWJOHtEpjP82GelvVF4vJjyjut2Izb82KVViTsRUcBd9lVtNFuPMHPlSV4CWdMC6/5VBOgMDHw== X-Google-Smtp-Source: AGHT+IGJM8v61b3NBM9zL1geW/mIIng2t66jC7sbjyL/yM6ItXZZorPeIuZ+34Y28AAGehTVSK0q X-Received: by 2002:a17:902:d586:b0:1dc:fc86:2e7a with SMTP id k6-20020a170902d58600b001dcfc862e7amr9280692plh.59.1710211781813; Mon, 11 Mar 2024 19:49:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710211781; cv=pass; d=google.com; s=arc-20160816; b=davrHh4ooUpO02UG7iJfiJrOz89YidPNpCeETN7AW663SY2behznEs8KHfLKa55OQF 3gz2efJLrhxOx5qscfOuiiEXcwlkubzS36ckhP/CaSVLPwrH3Lla9HB1VpiPuAHW2InU 1rFS43dYDQZXPaQm9WEHERWBfE/gBSbvdcen6wmUDtdBhJVmgnueHUfv6ygOlJ0V4REG 3QDzfOhTPjSRwQLS4o1509uHeRpQjABiyqSR7gkqg4xJtfPZFAgoGhyTshsajesqjuNx Eb/f7LYwkJxqxgJw2cf00ZshVbYoDi380v2X/T28fGAozJ39hDUTmOcfv5v5vcfRbnY7 Xmmw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=kp8FDAIHogCGtdOtPeOiQYNfgQ9IDPgMIvR+XSfvvHk=; fh=nHE0Rh8CxQSKQpFzbuCplgY6idyzSn1NeYattq8fD2o=; b=kGyuuX4EoqhUJQ0C5VGqshMQAlpeLxxavxEyYe+skAv1gKHg3ju+W4Ll00g0ch6Buu 1GILJMJBkHhYRHEk/s6idJXMRy7nDgZdad1ZvpO8mKbnFOIsWHXDCOXr7qkCGNO76KgR ksOsJos+LHQmzF34VFuKgLpiPjD+4IZTTU3c1RviGVd2hhT19h2uz9T9jAZqAfmKasAh eMVQ1Z/6/AhuOgpYt/wx4bIaZSj1gLwu9J28s8ilqNu+hp818OjungpJEOhNTll0BUHj /VhXsI8UN6pH6q6JQjOcFKJxiSzTxWpnTFn9dFNW1kujeam8BsNhwGJpm9GHcXYDxvRJ KGBw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=WfkGS4Xb; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-99779-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99779-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id s15-20020a17090302cf00b001dc6cd28614si6316103plk.231.2024.03.11.19.49.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 19:49:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99779-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=WfkGS4Xb; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-99779-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99779-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 1E0C8B21F0E for ; Tue, 12 Mar 2024 02:49:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AFC49AD4C; Tue, 12 Mar 2024 02:49:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="WfkGS4Xb" Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D36B6883D for ; Tue, 12 Mar 2024 02:49:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710211764; cv=none; b=aPt+k+m1K2iDTR1mLCpJnhxlh0WuYb31dMiY6ccPKsiw7kj9ov0sPQ8U1bhRoNIwd6+F5aSuzE8smFVP24Cx4cqqEnNmPdfCBaCB6x74sbDCyO7vBCo+7y8yCQwUKlOxhV7ZkYdqZuDfxoWovZXHQ7wu19FeOF29D+tZ48fDzlc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710211764; c=relaxed/simple; bh=LG8tjvBnuxxg899SAnLnow23FrwucP99leAXJLBMDyk=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=H+/GHw46SYxrEncL5eZhjzoHsBHBoaI/nrssl6gJaI/IbAh54poQijXCQL+nYt+PfhcDiopZSSdxABnNWRaENqA3Jw48++nArbyIgEH5UPQglag7FV9xV+/UqrGU0ELvv/zQTv0XDnbPlWDOxq4jvJZE77TYbdsRbELDQJYXagw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=WfkGS4Xb; arc=none smtp.client-ip=209.85.216.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-29bb5bec0e4so2546312a91.1 for ; Mon, 11 Mar 2024 19:49:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1710211761; x=1710816561; 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=kp8FDAIHogCGtdOtPeOiQYNfgQ9IDPgMIvR+XSfvvHk=; b=WfkGS4XbLbTYwVv62QrPovHbbApLL4JUmXpJ5WtAN8jlZ2e8FnXxGgwOUWChToIaXF UTkx+bcDlDJ3zvesem2GDR1fhYpTNdBGRYj9WOriUb2b6kaxoFs+5gzBpoayhAxb/k8V 2HIdfu88t+uPRer8yK8vOV1F0L5MT4BOx8P5fs3ZFRD5RaxiwHGmmIYnwfYymT4ObgHz Ka4IpN7Cdus8aQWbxWdB4rd2WfaAK8ZAtlDE1a0xPGJkU4jPulzyvaBi6TxawtDx5Bfv jA9dmIT37wn27bNd06h99C34fj/XvJmYxvT4C2Z9aiomAiV9nAdCuA5El4iCK8d+/YAB Xscg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710211761; x=1710816561; 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=kp8FDAIHogCGtdOtPeOiQYNfgQ9IDPgMIvR+XSfvvHk=; b=SRxfc1oiiXMmPFNLFVPa997bAALCtz4EocBcY6gpZVeUrPgGVEL3pllH3yGkbtn03r N1W0Aw1XuH060By27PZyj4axgzgwQmdkK5eTxP4G+mz7nqrcs/aXCnoMYRZ7DyD7dVeL 9GZs6VZlf9+CvCSlN1iMVmOxuCiHVWhqOhA6QzhyD6WMhgd13C1LRzba22EbFzA2Du8q wR5F5soQkgrMVU36TBIib8ATWZrMNae5JrUa9BoCYNFDCzXynruNPTfMhqAtxXaSWGZe x4iHlZsMJZ0yqnihcct9O4mH3CIjX/eenEhNcqI2k+SHwDJ5IsOJuZbf8PgnnBYZp0VR 73rg== X-Forwarded-Encrypted: i=1; AJvYcCWI8o5iXyEK9Es2zosBuSxEs1cFw0cgDjW3jPEwt/2uQShxhz2rjA8LQ9/PqyJyYOTvcWqXkNfgmIJ60u+3A6ldF7tBxOUv6yL/eAMM X-Gm-Message-State: AOJu0YzjdkLGbrv63rPCB9fjr3r5CeIqOsLbuURRrHbK/19TIp6O2unc EQQgaBn6keV18ifc6RAp4+t2rc9EtZCD+Hm77FjFO6DcW6fgNsoFwMht/VfcmShCadywPjp3UBi w7Ekchl0dpUq8OHNy5w3HuwNPZGD4GjgrxetaQg== X-Received: by 2002:a17:90a:5d8c:b0:29a:2860:28b9 with SMTP id t12-20020a17090a5d8c00b0029a286028b9mr6080591pji.48.1710211761009; Mon, 11 Mar 2024 19:49:21 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240311093526.1010158-1-dongmenglong.8@bytedance.com> <20240311093526.1010158-2-dongmenglong.8@bytedance.com> In-Reply-To: From: =?UTF-8?B?5qKm6b6Z6JGj?= Date: Tue, 12 Mar 2024 10:49:10 +0800 Message-ID: Subject: Re: [External] Re: [PATCH bpf-next v2 1/9] bpf: tracing: add support to record and check the accessed args To: Alexei Starovoitov Cc: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Eddy Z , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "David S. Miller" , David Ahern , Dave Hansen , X86 ML , Steven Rostedt , Mathieu Desnoyers , Quentin Monnet , bpf , linux-arm-kernel , LKML , linux-riscv , linux-s390 , Network Development , linux-trace-kernel@vger.kernel.org, "open list:KERNEL SELFTEST FRAMEWORK" , linux-stm32@st-md-mailman.stormreply.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Mar 12, 2024 at 10:42=E2=80=AFAM =E6=A2=A6=E9=BE=99=E8=91=A3 wrote: > > On Tue, Mar 12, 2024 at 10:09=E2=80=AFAM Alexei Starovoitov > wrote: > > > > On Mon, Mar 11, 2024 at 7:01=E2=80=AFPM =E6=A2=A6=E9=BE=99=E8=91=A3 wrote: > > > > > > On Tue, Mar 12, 2024 at 9:46=E2=80=AFAM Alexei Starovoitov > > > wrote: > > > > > > > > On Mon, Mar 11, 2024 at 2:34=E2=80=AFAM Menglong Dong > > > > wrote: > > > > > > > > > > In this commit, we add the 'accessed_args' field to struct bpf_pr= og_aux, > > > > > which is used to record the accessed index of the function args i= n > > > > > btf_ctx_access(). > > > > > > > > > > Meanwhile, we add the function btf_check_func_part_match() to com= pare the > > > > > accessed function args of two function prototype. This function w= ill be > > > > > used in the following commit. > > > > > > > > > > Signed-off-by: Menglong Dong > > > > > --- > > > > > include/linux/bpf.h | 4 ++ > > > > > kernel/bpf/btf.c | 108 ++++++++++++++++++++++++++++++++++++++= +++++- > > > > > 2 files changed, 110 insertions(+), 2 deletions(-) > > > > > > > > > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > > > > > index 95e07673cdc1..0f677fdcfcc7 100644 > > > > > --- a/include/linux/bpf.h > > > > > +++ b/include/linux/bpf.h > > > > > @@ -1461,6 +1461,7 @@ struct bpf_prog_aux { > > > > > const struct btf_type *attach_func_proto; > > > > > /* function name for valid attach_btf_id */ > > > > > const char *attach_func_name; > > > > > + u64 accessed_args; > > > > > struct bpf_prog **func; > > > > > void *jit_data; /* JIT specific data. arch dependent */ > > > > > struct bpf_jit_poke_descriptor *poke_tab; > > > > > @@ -2565,6 +2566,9 @@ struct bpf_reg_state; > > > > > int btf_prepare_func_args(struct bpf_verifier_env *env, int subp= rog); > > > > > int btf_check_type_match(struct bpf_verifier_log *log, const str= uct bpf_prog *prog, > > > > > struct btf *btf, const struct btf_type *= t); > > > > > +int btf_check_func_part_match(struct btf *btf1, const struct btf= _type *t1, > > > > > + struct btf *btf2, const struct btf_= type *t2, > > > > > + u64 func_args); > > > > > const char *btf_find_decl_tag_value(const struct btf *btf, const= struct btf_type *pt, > > > > > int comp_idx, const char *tag= _key); > > > > > int btf_find_next_decl_tag(const struct btf *btf, const struct b= tf_type *pt, > > > > > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c > > > > > index 170d017e8e4a..c2a0299d4358 100644 > > > > > --- a/kernel/bpf/btf.c > > > > > +++ b/kernel/bpf/btf.c > > > > > @@ -6125,19 +6125,24 @@ static bool is_int_ptr(struct btf *btf, c= onst struct btf_type *t) > > > > > } > > > > > > > > > > static u32 get_ctx_arg_idx(struct btf *btf, const struct btf_typ= e *func_proto, > > > > > - int off) > > > > > + int off, int *aligned_idx) > > > > > { > > > > > const struct btf_param *args; > > > > > const struct btf_type *t; > > > > > u32 offset =3D 0, nr_args; > > > > > int i; > > > > > > > > > > + if (aligned_idx) > > > > > + *aligned_idx =3D -ENOENT; > > > > > + > > > > > if (!func_proto) > > > > > return off / 8; > > > > > > > > > > nr_args =3D btf_type_vlen(func_proto); > > > > > args =3D (const struct btf_param *)(func_proto + 1); > > > > > for (i =3D 0; i < nr_args; i++) { > > > > > + if (aligned_idx && offset =3D=3D off) > > > > > + *aligned_idx =3D i; > > > > > t =3D btf_type_skip_modifiers(btf, args[i].type, = NULL); > > > > > offset +=3D btf_type_is_ptr(t) ? 8 : roundup(t->s= ize, 8); > > > > > if (off < offset) > > > > > @@ -6207,7 +6212,7 @@ bool btf_ctx_access(int off, int size, enum= bpf_access_type type, > > > > > tname, off); > > > > > return false; > > > > > } > > > > > - arg =3D get_ctx_arg_idx(btf, t, off); > > > > > + arg =3D get_ctx_arg_idx(btf, t, off, NULL); > > > > > args =3D (const struct btf_param *)(t + 1); > > > > > /* if (t =3D=3D NULL) Fall back to default BPF prog with > > > > > * MAX_BPF_FUNC_REG_ARGS u64 arguments. > > > > > @@ -6217,6 +6222,9 @@ bool btf_ctx_access(int off, int size, enum= bpf_access_type type, > > > > > /* skip first 'void *__data' argument in btf_trac= e_##name typedef */ > > > > > args++; > > > > > nr_args--; > > > > > + prog->aux->accessed_args |=3D (1 << (arg + 1)); > > > > > + } else { > > > > > + prog->aux->accessed_args |=3D (1 << arg); > > > > > > > > What do you need this aligned_idx for ? > > > > I'd expect that above "accessed_args |=3D (1 << arg);" is enough. > > > > > > > > > > Which aligned_idx? No aligned_idx in the btf_ctx_access(), and > > > aligned_idx is only used in the btf_check_func_part_match(). > > > > > > In the btf_check_func_part_match(), I need to compare the > > > t1->args[i] and t2->args[j], which have the same offset. And > > > the aligned_idx is to find the "j" according to the offset of > > > t1->args[i]. > > > > And that's my question. > > Why you don't do the max of accessed_args across all attach > > points and do btf_check_func_type_match() to that argno > > instead of nargs1. > > This 'offset +=3D btf_type_is_ptr(t1) ? 8 : roundup... > > is odd. > > Hi, I'm trying to make the bpf flexible enough. Let's take an example, > now we have the bpf program: > > int test1_result =3D 0; > int BPF_PROG(test1, int a, long b, char c) > { > test1_result =3D a + c; > return 0; > } > > In this program, only the 1st and 3rd arg is accessed. So all kernel > functions whose 1st arg is int and 3rd arg is char can be attached > by this bpf program, even if their 2nd arg is different. > > And let's take another example for struct. This is our bpf program: > > int test1_result =3D 0; > int BPF_PROG(test1, long a, long b, char c) > { > test1_result =3D c; > return 0; > } > > Only the 3rd arg is accessed. And we have following kernel function: > > int kernel_function1(long a, long b, char c) > { > xxx > } > > struct test1 { > long a; > long b; > }; > int kernel_function2(struct test1 a, char b) > { > xxx > } > > The kernel_function1 and kernel_function2 should be compatible, > as the bpf program only accessed the ctx[2], whose offset is 16. > And the arg in kernel_function1() with offset 16 is "char c", the > arg in kernel_function2() with offset 16 is "char b", which is > compatible. > > That's why we need to check the consistency of accessed args > by offset instead of function arg index. > And that's why I didn't share the code with btf_check_func_type_match(). In btf_check_func_part_match(), I'm trying to check the "real" accessed args of t1 and t2, not by the function index, which has quite a difference with btf_check_func_type_match(). > I'm not sure if I express my idea clearly, is this what you are > asking? > > Thanks! > Menglong Dong