From patchwork Fri Aug 12 05:24:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 12941903 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2864AC00140 for ; Fri, 12 Aug 2022 05:24:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235200AbiHLFYx (ORCPT ); Fri, 12 Aug 2022 01:24:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229524AbiHLFYw (ORCPT ); Fri, 12 Aug 2022 01:24:52 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6BFBA00E4 for ; Thu, 11 Aug 2022 22:24:51 -0700 (PDT) Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 27BLdcHA013963 for ; Thu, 11 Aug 2022 22:24:51 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=W2kitG9IvK8m8vHLDcDAZekjFmUhHc//Sf6vO4wswF4=; b=Tml43IBcRtF4rNYLR9Cb91abwTkh5ln3Q5dx5i8aJ+wL2+XA6ryeYMkn1XgH4T1ypfm1 3iSgYjUgU8buTbaYtRtumB0Dh6AA9S5Id5VWF6/tVL36ExePdUyx3WuICKJFfUPl2b8A kdh7EKNK72D8E1fn7ilHXWCr7QNoTd2559k= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3hw9vgsx3y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 11 Aug 2022 22:24:51 -0700 Received: from twshared0646.06.ash9.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 11 Aug 2022 22:24:49 -0700 Received: by devbig309.ftw3.facebook.com (Postfix, from userid 128203) id DCFCDDF8C486; Thu, 11 Aug 2022 22:24:45 -0700 (PDT) From: Yonghong Song To: CC: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Subject: [PATCH bpf-next v2 5/6] bpf: Populate struct argument info in btf_func_model Date: Thu, 11 Aug 2022 22:24:45 -0700 Message-ID: <20220812052445.524459-1-yhs@fb.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220812052419.520522-1-yhs@fb.com> References: <20220812052419.520522-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-ORIG-GUID: aw88Uq-LH8qvDMD22MMOIMoCFwd7toBj X-Proofpoint-GUID: aw88Uq-LH8qvDMD22MMOIMoCFwd7toBj X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-12_03,2022-08-11_01,2022-06-22_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add struct argument support in btf_ctx_access() and btf_distill_func_proto(). The arch-specific code will handle whether and how such struct arguments are supported. Signed-off-by: Yonghong Song --- kernel/bpf/btf.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 903719b89238..f38ae0e908fd 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -5339,7 +5339,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, struct bpf_verifier_log *log = info->log; const struct btf_param *args; const char *tag_value; - u32 nr_args, arg; + u32 nr_args, arg, curr_tid = 0; int i, ret; if (off % 8) { @@ -5385,6 +5385,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, */ if (!t) return true; + curr_tid = t->type; t = btf_type_by_id(btf, t->type); break; case BPF_MODIFY_RETURN: @@ -5394,7 +5395,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, if (!t) return false; - t = btf_type_skip_modifiers(btf, t->type, NULL); + t = btf_type_skip_modifiers(btf, t->type, &curr_tid); if (!btf_type_is_small_int(t)) { bpf_log(log, "ret type %s not allowed for fmod_ret\n", @@ -5411,15 +5412,25 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, if (!t) /* Default prog with MAX_BPF_FUNC_REG_ARGS args */ return true; + curr_tid = args[arg].type; t = btf_type_by_id(btf, args[arg].type); } /* skip modifiers */ - while (btf_type_is_modifier(t)) + while (btf_type_is_modifier(t)) { + curr_tid = t->type; t = btf_type_by_id(btf, t->type); + } if (btf_type_is_small_int(t) || btf_is_any_enum(t)) /* accessing a scalar */ return true; + if (__btf_type_is_struct(t) && curr_tid) { + info->reg_type = PTR_TO_BTF_ID; + info->btf = btf; + info->btf_id = curr_tid; + return true; + } + if (!btf_type_is_ptr(t)) { bpf_log(log, "func '%s' arg%d '%s' has type %s. Only pointer access is allowed\n", @@ -5878,7 +5889,7 @@ static int __get_type_size(struct btf *btf, u32 btf_id, if (!t) return -EINVAL; *ret_type = t; - if (btf_type_is_ptr(t)) + if (btf_type_is_ptr(t) || __btf_type_is_struct(t)) /* kernel size of pointer. Not BPF's size of pointer*/ return sizeof(void *); if (btf_type_is_int(t) || btf_is_any_enum(t)) @@ -5901,8 +5912,10 @@ int btf_distill_func_proto(struct bpf_verifier_log *log, /* BTF function prototype doesn't match the verifier types. * Fall back to MAX_BPF_FUNC_REG_ARGS u64 args. */ - for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) + for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { m->arg_size[i] = 8; + m->arg_flags[i] = 0; + } m->ret_size = 8; m->nr_args = MAX_BPF_FUNC_REG_ARGS; return 0; @@ -5944,7 +5957,12 @@ int btf_distill_func_proto(struct bpf_verifier_log *log, tname); return -EINVAL; } - m->arg_size[i] = ret; + if (__btf_type_is_struct(t)) { + m->arg_flags[i] = BTF_FMODEL_STRUCT_ARG; + m->arg_size[i] = t->size; + } else { + m->arg_size[i] = ret; + } } m->nr_args = nargs; return 0;