Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp802552lql; Mon, 11 Mar 2024 19:42:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUTxrrtgOlaYagqFRWENZJ0egtFkcoAEfgp+z0e3rEUzpyBk10WL07grizxzEhsCUU+yJXTGzjYDN3XU8zXi4aBR4FfjiqwUhnXV6KrZw== X-Google-Smtp-Source: AGHT+IHIbvItauiAHkfKpFCAa/icWNsAT5cahzSKZgSpQ64NnVruuHH35lZK/RE/Dty6eVLhMAg+ X-Received: by 2002:a05:6808:384d:b0:3c2:3fc6:a05a with SMTP id ej13-20020a056808384d00b003c23fc6a05amr3424174oib.33.1710211362719; Mon, 11 Mar 2024 19:42:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710211362; cv=pass; d=google.com; s=arc-20160816; b=QAAc7uFfO0tRZI67ruNi9g8PFi58xSbzxxpPqZjnp6GY35/xgcE9uoR13RViSGFlG6 E3HkhgdOppR4DdU2NyTLEguX2WnSiFG8kGDa/83vbzQg0s5FIVaL/6nZ84gheZUAxF10 MVFW24xFpLm7jg+TulqzmyjV6FCFHwOuHic3FUpNycvL4p4bV/AyvapHqhCwDsQi/10j boOorLXqYclsm9qTT2ftseXvvjLuV9JMWKEuOUgCFsmVDxQ3WgvmDXlNHD0lmmRdpIv2 WGBxp19zER1FDtUCx0W+fIiLQPNhzuoICLD1sMiPdVGOSk6hPkSZbgxFP5jUP4ELoNDC aXAw== 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=OINlgsu2YF3PvJN8OnOTR/8+uoasAh0pp34EJQtDWyM=; fh=ZhVuB2yvtvHntPH4p7uf3f5pG64YDe1R7j4l4zT5Og4=; b=vvxlt+CR7AUnvQs63T4Eni2Qv8dqnMo6mQHmd6nPHpVcxudoM4oyhdhAZSiZFSeu3c /GOxGrnGw9JpM4bsplZEtIl1t+aBCltQ6ucJE7v6GUfA+4AbpgroW9do7RAzSN5mVorr ZE01aHaTWNIqXh/kXGxFR6d+tWoqQH4NipYHK7ZdJIIWmy4ytyYmYgr18zmBPP0OX7TW 7Me28Q50zkT43PV+kPhQfzU6VRvFj3tXmHQjhYMuxJLeopS6jWGqkuoq27X8KXnhwwic qyKMC/Yy5xNUjX/r6vVmpj+CGf3o7EdDETmRc8ZLkZ8w4nEpimsnfUKvqbKEQC5+Lq1X VnXw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=YeLgFJ0Q; 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-99774-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99774-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id l24-20020a62be18000000b006e646613e33si5894775pff.251.2024.03.11.19.42.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 19:42:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99774-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=@bytedance.com header.s=google header.b=YeLgFJ0Q; 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-99774-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99774-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 3878D281BEA for ; Tue, 12 Mar 2024 02:42:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F3248F51; Tue, 12 Mar 2024 02:42:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="YeLgFJ0Q" Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 F142979C0 for ; Tue, 12 Mar 2024 02:42:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710211346; cv=none; b=lFcvsR74unkuTiEDj3Esg8x2LKf2I94ArTAztvkSPaHWvCs67qkjzWhHrgANus0BZR/J3hXTqozQkBYN5Ay+h4WdAbM2HhIq9uLGhPUbiv6Egr9/i3G2VpBTInpkBNvcd09Q2ETQCClW9DHzVbcHZzIqxattGGYumna/Q0efWDs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710211346; c=relaxed/simple; bh=A1etVzSHW+e/mqqVDMMdwEbh1AH+28mTZN4/5W0ATXM=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=F6qZ4eN8KtcIf8gG5nAIy7LEps/EAJGEimIoQnp/jnDUNbeYREwaUfKHhkyaVUz0W/AuIYi9IXH+10jB3y/KfRzZGvTbwVwstWw6XHqPGTegMW5ZiYe+DEj9QNv+CjLaVLIwNtwdiZ4k7vdVe/hctQEjlfUP/pulXIkEzqJEB94= 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=YeLgFJ0Q; arc=none smtp.client-ip=209.85.210.174 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-pf1-f174.google.com with SMTP id d2e1a72fcca58-6e6082eab17so4609250b3a.1 for ; Mon, 11 Mar 2024 19:42:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1710211343; x=1710816143; 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=OINlgsu2YF3PvJN8OnOTR/8+uoasAh0pp34EJQtDWyM=; b=YeLgFJ0QiDEotFeBmWDSLzNDjy/T7F3k8Yg0kw0dyHRBU8wmRjcp7ehr3+JQocZyKf w9bf0zJwkCCVMePgScz1MRMFPbNXPJyUk0E+UVS6wTFOfIHn1ITpjm7vgN0NTkZdRpJU tQYWn0OwnYxAuUW8p9EFRk2piZ1j4QvE3mcCGPq6JDz/5xkyA2bGndtoji0YFM9TXOyT /vRuD1Da270foUMUzszyvPbKMr+hYL0menX3tSFZl9iKITpFjakTOV8NJLW4oAKYBtxf iUSqrRQ62rcsPx4J9nvEsyyjLA/qhV9yTOlKcByIVEP2jvl1wLMx8ryInai4H6PX73Zg FTlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710211343; x=1710816143; 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=OINlgsu2YF3PvJN8OnOTR/8+uoasAh0pp34EJQtDWyM=; b=Hi25mHX7H5WXXafGiGYUGa/iwKrB69ZVrE6pTuq0w4TAtA8W+aihLX1IyORjfWVl7a CXoRuOLPKw3200uHSkHg+GsDEx926/gGsbp0cTwbYU022smwdksccpbYSHIYmQMjLmp4 MDzRoHsyvg5cc/CwXc+rmrPVUdidSvghu1UVQ9rJoUf7QdKrr31JiTVSgKBWT9FenwPq W/YbfF4L4yJ552TbKCYuHPr9n+1L0w+IosQy/O77tv5Ac+zyEqWV1Io1JvRwdqUj+cyK tXIERx+cm++XgOdU2/hRTtCoN1YOFn/dtuXFBA0gDVa1EUCxEGYrCqV6bc8HwF+s+Srs Z0ug== X-Forwarded-Encrypted: i=1; AJvYcCW69vx58lnRvRQeGnefml3ypj2MMaJmZMaQWqb0b9XY26mIV4S/YC6NXt6vedMSWLgQAerCJA2HRTXrYrQs7BjcuO0cR5ugLmpVCdyP X-Gm-Message-State: AOJu0Yz4Qc+nqBa0hvaPxBfPyh4RmcoauYi/3CVc+Rjo2fSoZ2C3N58I n6QpHSF75hZ33Kom0Tt4PYyRDSe/ePBGScEJoXWZLCpYcCAU2AM31v9vHyplnE+Zr7gG3IUA6O+ orWl4RPgK3tqEdnPUFGapXFZxi6xaGqCJxfGEIA== X-Received: by 2002:a05:6a21:32a2:b0:1a1:2b49:9f04 with SMTP id yt34-20020a056a2132a200b001a12b499f04mr2426596pzb.46.1710211343317; Mon, 11 Mar 2024 19:42:23 -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:42:12 +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: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_prog= _aux, > > > > which is used to record the accessed index of the function args in > > > > btf_ctx_access(). > > > > > > > > Meanwhile, we add the function btf_check_func_part_match() to compa= re the > > > > accessed function args of two function prototype. This function wil= l 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 subpro= g); > > > > int btf_check_type_match(struct bpf_verifier_log *log, const struc= t bpf_prog *prog, > > > > struct btf *btf, const struct btf_type *t)= ; > > > > +int btf_check_func_part_match(struct btf *btf1, const struct btf_t= ype *t1, > > > > + struct btf *btf2, const struct btf_ty= pe *t2, > > > > + u64 func_args); > > > > const char *btf_find_decl_tag_value(const struct btf *btf, const s= truct btf_type *pt, > > > > int comp_idx, const char *tag_k= ey); > > > > int btf_find_next_decl_tag(const struct btf *btf, const struct btf= _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, con= st struct btf_type *t) > > > > } > > > > > > > > static u32 get_ctx_arg_idx(struct btf *btf, const struct btf_type = *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, NU= LL); > > > > offset +=3D btf_type_is_ptr(t) ? 8 : roundup(t->siz= e, 8); > > > > if (off < offset) > > > > @@ -6207,7 +6212,7 @@ bool btf_ctx_access(int off, int size, enum b= pf_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 b= pf_access_type type, > > > > /* skip first 'void *__data' argument in btf_trace_= ##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. I'm not sure if I express my idea clearly, is this what you are asking? Thanks! Menglong Dong