From patchwork Wed Mar 15 22:36:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13176704 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 904CEC7618A for ; Wed, 15 Mar 2023 22:36:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230124AbjCOWgZ (ORCPT ); Wed, 15 Mar 2023 18:36:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232345AbjCOWgU (ORCPT ); Wed, 15 Mar 2023 18:36:20 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D80E1A977; Wed, 15 Mar 2023 15:36:17 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id iw3so4856539plb.6; Wed, 15 Mar 2023 15:36:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678919776; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pFibuvC+RuNZKj+JLga00XkPB0Kr+WsV2xstr+uagnQ=; b=h0haTo//ELwMPl2fT3k9nvU568eQVR51q2APdsiJD3zYE0rDec1Hy8ln4HRh0mbGMh AG3mmF59IO4omU+FM/2a9YOAbWgznfOuMxOqG6P+HyOdO1NcF2DALdnm7DvcgE2uXM3L gyqvy/ZU4ybXl1gCcJ/0C8uEPjAizW3hT/d/vCh53wbeX8XR2vc8+2ACcIdFUWCwE/q1 eNWEBBJY/GIZTCNs+KtyDFgdYgex8UggPqiwOk5V8qXOOqK+jyBxbQsJeWWLOPRuFe1e 4JConX4Wb2IqqaopMWkNzhJ2/uSDv3D97QghGhm2zWBbeuvS1wANNS64L1lGEfkMKSyh bPFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678919776; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pFibuvC+RuNZKj+JLga00XkPB0Kr+WsV2xstr+uagnQ=; b=O5Ud7uGC1m82ZwBt4oXRJL45GDRnLXXf3fQU+DYMDWjipWC1EuoJ5DTAjI9HqnRkkd cTWeRHoYlRuzceK6UoHmMxWDYHdxX8P8dGVEqw/POOIFyholeTVwGYg7XoC+06Cbp/up iY9z58l73ai1GFH4tXJE470sXxh/8WoWTYkhw0KL1TayVFF0bGjseU+3hHoxT9qQESIq uUH7Nz99E41eGGMLYRd+Zq7wEyqdE6WtJH6ks4wb0/YvLvV/WnoJWQ7zyvNu7a3T50q2 d59sLnv7lLT6Zq4/I0gLQcCHBzX/cY1DN433b7DuxyZf+GYkhRH4dfcclixzrOQEnDd0 5Ihw== X-Gm-Message-State: AO0yUKUN8HHZczoeynsyNl6SbXQ2YIHqJXECO3c41YZaEq3xbrbjEA+U XeOYHG3rNKDy7SkEeavSLIc= X-Google-Smtp-Source: AK7set+87adcux6kwUtVk1knr3VfYqDva3hLOySZ4ScmqM3jW3CDBYpQh0jg3vtMqxUSaK/kk7rrKg== X-Received: by 2002:a05:6a20:699b:b0:d5:a28c:43ef with SMTP id t27-20020a056a20699b00b000d5a28c43efmr1592672pzk.43.1678919776484; Wed, 15 Mar 2023 15:36:16 -0700 (PDT) Received: from dhcp-172-26-102-232.DHCP.thefacebook.com ([2620:10d:c090:400::5:2bcf]) by smtp.gmail.com with ESMTPSA id h11-20020a63df4b000000b0050b19d24c3bsm3904254pgj.37.2023.03.15.15.36.14 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 15 Mar 2023 15:36:15 -0700 (PDT) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, void@manifault.com, davemarchevsky@meta.com, tj@kernel.org, memxor@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH bpf-next 1/2] bpf: Allow ld_imm64 instruction to point to kfunc. Date: Wed, 15 Mar 2023 15:36:06 -0700 Message-Id: <20230315223607.50803-2-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230315223607.50803-1-alexei.starovoitov@gmail.com> References: <20230315223607.50803-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Allow ld_imm64 insn with BPF_PSEUDO_BTF_ID to hold the address of kfunc. PTR_MEM is already recognized for NULL-ness by is_branch_taken(), so unresolved kfuncs will be seen as zero. This allows BPF programs to detect at load time whether kfunc is present in the kernel with bpf_kfunc_exists() macro. Signed-off-by: Alexei Starovoitov --- kernel/bpf/verifier.c | 7 +++++-- tools/lib/bpf/bpf_helpers.h | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 60793f793ca6..4e49d34d8cd6 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -15955,8 +15955,8 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, goto err_put; } - if (!btf_type_is_var(t)) { - verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); + if (!btf_type_is_var(t) && !btf_type_is_func(t)) { + verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR or KIND_FUNC\n", id); err = -EINVAL; goto err_put; } @@ -15990,6 +15990,9 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, aux->btf_var.reg_type = PTR_TO_BTF_ID | MEM_PERCPU; aux->btf_var.btf = btf; aux->btf_var.btf_id = type; + } else if (!btf_type_is_func(t)) { + aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; + aux->btf_var.mem_size = 0; } else if (!btf_type_is_struct(t)) { const struct btf_type *ret; const char *tname; diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h index 7d12d3e620cc..43abe4c29409 100644 --- a/tools/lib/bpf/bpf_helpers.h +++ b/tools/lib/bpf/bpf_helpers.h @@ -177,6 +177,9 @@ enum libbpf_tristate { #define __kptr_untrusted __attribute__((btf_type_tag("kptr_untrusted"))) #define __kptr __attribute__((btf_type_tag("kptr"))) +/* pass function pointer through asm otherwise compiler assumes that any function != 0 */ +#define bpf_kfunc_exists(fn) ({ void *__p = fn; asm ("" : "+r"(__p)); __p; }) + #ifndef ___bpf_concat #define ___bpf_concat(a, b) a ## b #endif From patchwork Wed Mar 15 22:36:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13176705 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 AB5ECC6FD1D for ; Wed, 15 Mar 2023 22:36:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229459AbjCOWg0 (ORCPT ); Wed, 15 Mar 2023 18:36:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230059AbjCOWgW (ORCPT ); Wed, 15 Mar 2023 18:36:22 -0400 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 337041ACFC; Wed, 15 Mar 2023 15:36:21 -0700 (PDT) Received: by mail-pl1-x630.google.com with SMTP id ja10so12030677plb.5; Wed, 15 Mar 2023 15:36:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1678919780; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zMJOIKge1TNhWbJLbClPoucTcHqzq/T/IvQd51ijcoU=; b=J1JEvl4Y5znFBEkdgVQu+oLdwBCIK6uyLbt4IR4qnV9DbidY8uWSx+lpRdnWTto05V ZGvuKtY6pvmulkSWJJ1Yc2NBEhzDTmenB3gEXuUlX0WWrD9D6FBqHKWBUoP75Hyn/Dd0 jYx9C9ld7aYKB+BclWjPQYZc6uCCDD/77R62giMFnUcQOcmMcCyUpBceEdgcDr1yNLY3 qooFW3EFc8WUZu2LXYMwmHT2jffFFhGixzLySXABDsNYtqw6ejNvkeTRBvjrgT+yygyG bZEASVWbHJ5brDmeLSPhXW1IRWfkAbjxg65eyR2/KXnXbQ182s9ZeatQ7xXved/QpWWc NuGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678919780; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zMJOIKge1TNhWbJLbClPoucTcHqzq/T/IvQd51ijcoU=; b=MW+U70MM2Ed45WEExjarUIJj6CK7zjlTFVwf8UefMv+cbwU3eN0ErnYrryoQz/0NRw 4mJiaN7tRNpag6ek7bU2K1uuzaTy4Kyuya9mPtEEFgFvYV/hsNGQogCEIa4Ohc/b4jzI vxdWYeLow6mtxLlLqq+S5BMMA0B6lzSbeMbr/8yRvPR5h++kgpeUjyaT6eGhmdfkvvKo hwFmqieoFBI2f/lt1ybjP3ejxG9TRWTq+Yi18o4h1H+nUR4FrSmunA7JCGWD4H1llWkT i+6xiXfWLh+PHiHflKzmKjhe/De+ImuwR2LEd2KQ8AxU/tGVzKh4AjFHA4PoPP1kWDLB r7kA== X-Gm-Message-State: AO0yUKU1cRnzLDH/GqrMcKZRXOtXSLTDAewXhVHhawcJOrwwygIv3E95 PpCtyen5VYU8H5NBgCNipJU= X-Google-Smtp-Source: AK7set+rlBr5yqQVnIIOS7FHB9vkF6sZoe/yJJd0Gzdqw4rzuIkSzw3SIA7ubTKdbx1X6oK5DoJ2QQ== X-Received: by 2002:a05:6a20:8e06:b0:d6:8c70:85ce with SMTP id y6-20020a056a208e0600b000d68c7085cemr1481621pzj.54.1678919780425; Wed, 15 Mar 2023 15:36:20 -0700 (PDT) Received: from dhcp-172-26-102-232.DHCP.thefacebook.com ([2620:10d:c090:400::5:2bcf]) by smtp.gmail.com with ESMTPSA id s5-20020aa78285000000b005d296facfa3sm4008964pfm.36.2023.03.15.15.36.18 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 15 Mar 2023 15:36:20 -0700 (PDT) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, void@manifault.com, davemarchevsky@meta.com, tj@kernel.org, memxor@gmail.com, netdev@vger.kernel.org, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH bpf-next 2/2] selftests/bpf: Add test for bpf_kfunc_exists(). Date: Wed, 15 Mar 2023 15:36:07 -0700 Message-Id: <20230315223607.50803-3-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230315223607.50803-1-alexei.starovoitov@gmail.com> References: <20230315223607.50803-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Add load and run time test for bpf_kfunc_exists() and check that the verifier performs dead code elimination for non-existing kfunc. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/progs/task_kfunc_success.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_success.c b/tools/testing/selftests/bpf/progs/task_kfunc_success.c index 4f61596b0242..c0a7774e0c79 100644 --- a/tools/testing/selftests/bpf/progs/task_kfunc_success.c +++ b/tools/testing/selftests/bpf/progs/task_kfunc_success.c @@ -17,6 +17,8 @@ int err, pid; * TP_PROTO(struct task_struct *p, u64 clone_flags) */ +void invalid_kfunc(void) __ksym __weak; + static bool is_test_kfunc_task(void) { int cur_pid = bpf_get_current_pid_tgid() >> 32; @@ -26,7 +28,17 @@ static bool is_test_kfunc_task(void) static int test_acquire_release(struct task_struct *task) { - struct task_struct *acquired; + struct task_struct *acquired = NULL; + + if (!bpf_kfunc_exists(bpf_task_acquire)) { + err = 3; + return 0; + } + if (bpf_kfunc_exists(invalid_kfunc)) { + /* the verifier's dead code elimination should remove this */ + err = 4; + asm volatile ("goto -1"); /* for (;;); */ + } acquired = bpf_task_acquire(task); bpf_task_release(acquired);