From patchwork Fri Mar 17 20:19:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13179412 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 335C8C74A5B for ; Fri, 17 Mar 2023 20:19:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230075AbjCQUTd (ORCPT ); Fri, 17 Mar 2023 16:19:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229601AbjCQUTa (ORCPT ); Fri, 17 Mar 2023 16:19:30 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F12203B20E; Fri, 17 Mar 2023 13:19:28 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id lr16-20020a17090b4b9000b0023f187954acso6452143pjb.2; Fri, 17 Mar 2023 13:19:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679084368; 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=JN9cBNt3h3eIh0OXmvX38/fGd4Tx0MODI3Ugpo8F75o=; b=GmxUn1U4n3wS6krTdx/OdDtgbVO7VbB71sT1m9gfx+34+syc12nqNFLhg6HW1eX6Z/ m6/cBB7SSxJ22qydBZ4I7X58HASdli7jgOU+KCOYVoo1f8NiTIG50M93d95hPTkCms2m AmdFlQJGNODafqSMFUSFdXPNTKQ1L5i+cmhCUYXUpyJ5Czh7v420f0fGZ1Ng6MUA846k 7u5oGcRPHJn5ukv3Flkw88nGCE+/YzEAWXBzBzbnLYJq3fwQCc7JDGKWq9UsQA/KLnf6 ddSME8JSPajGlhkrVWT8UTvcz3Atg2wbvHUQ+xGn6g7m5zHsPEKR7+EbVvqVyFyZinKE /mHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679084368; 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=JN9cBNt3h3eIh0OXmvX38/fGd4Tx0MODI3Ugpo8F75o=; b=4kS2xtcWVCynGAx3FxBJr41kjbyAARGfw7wQI4VQ2pQaY0/KmZIlBMsooLs5EYjyGt mqPthszVI0wdZNWcr6PY1Yi+R8VCNFj+JKgjxuUM0kHFNV79W4xY+8UISvs7ipS3j20l k3NgD7gd5byYqv4Fgxy3D1MM31Q+6f6VYoAIWgdt/JYditNX+Omn5wVXV/80+iFNCoZZ FeEiNqasJLWlv1n42SgPJP6+kBwu9rvDepPNFpKjAFaBC0dHGHn9LCPHPs8HKCo/tvah 4DLWUrlbUR//r/n+TUSbCCoLZ7r2nDMHK4b3NCQaqCjSVFqbBSjEbO4Shfh752hBednm vqcw== X-Gm-Message-State: AO0yUKWY7deYbfVGShSpTrFczTR9KCe4oV3/RzSZBWC8tBy4i3TDK/Gs 10XjYQizuPBzMtzl2jIohSKpK0IR5NM= X-Google-Smtp-Source: AK7set+5G5gszHJ74ll+JFQF3HwLPjJ5P/pooksNjyG6Pzi7hqu0h/un0ga4n7eMvIEJSKGbp/KELg== X-Received: by 2002:a17:90b:3a8f:b0:23f:63d5:c10f with SMTP id om15-20020a17090b3a8f00b0023f63d5c10fmr2735385pjb.25.1679084368394; Fri, 17 Mar 2023 13:19:28 -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 q6-20020a17090a1b0600b002311ae14a01sm5339849pjq.11.2023.03.17.13.19.26 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 17 Mar 2023 13:19:28 -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 v2 bpf-next 1/4] bpf: Allow ld_imm64 instruction to point to kfunc. Date: Fri, 17 Mar 2023 13:19:17 -0700 Message-Id: <20230317201920.62030-2-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230317201920.62030-1-alexei.starovoitov@gmail.com> References: <20230317201920.62030-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. The ld_imm64 pointing to a valid kfunc will be seen as non-null PTR_TO_MEM by is_branch_taken() logic of the verifier, while libbpf will resolve address to unknown kfunc as ld_imm64 reg, 0 which will also be recognized by is_branch_taken() and the verifier will proceed dead code elimination. BPF programs can use this logic to detect at load time whether kfunc is present in the kernel with bpf_ksym_exists() macro that is introduced in the next patches. Signed-off-by: Alexei Starovoitov Reviewed-by: Martin KaFai Lau Acked-by: John Fastabend Reviewed-by: Toke Høiland-Jørgensen --- kernel/bpf/verifier.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d62b7127ff2a..1bc8a6d6fdd2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -15952,8 +15952,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; } @@ -15966,6 +15966,14 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, err = -ENOENT; goto err_put; } + insn[0].imm = (u32)addr; + insn[1].imm = addr >> 32; + + if (btf_type_is_func(t)) { + aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY; + aux->btf_var.mem_size = 0; + goto check_btf; + } datasec_id = find_btf_percpu_datasec(btf); if (datasec_id > 0) { @@ -15978,9 +15986,6 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, } } - insn[0].imm = (u32)addr; - insn[1].imm = addr >> 32; - type = t->type; t = btf_type_skip_modifiers(btf, type, NULL); if (percpu) { @@ -16008,7 +16013,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, aux->btf_var.btf = btf; aux->btf_var.btf_id = type; } - +check_btf: /* check whether we recorded this BTF (and maybe module) already */ for (i = 0; i < env->used_btf_cnt; i++) { if (env->used_btfs[i].btf == btf) { From patchwork Fri Mar 17 20:19:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13179413 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 97348C7618B for ; Fri, 17 Mar 2023 20:19:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229601AbjCQUTf (ORCPT ); Fri, 17 Mar 2023 16:19:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230076AbjCQUTe (ORCPT ); Fri, 17 Mar 2023 16:19:34 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 389532B2BA; Fri, 17 Mar 2023 13:19:33 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id gp15-20020a17090adf0f00b0023d1bbd9f9eso10410530pjb.0; Fri, 17 Mar 2023 13:19:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679084372; 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=DykUx1HbeAhX8oTWNoXUh40nOPiSPk2JHLd6IpEyXHY=; b=Vf+UbayJaUiQOpIPXNNFQUgOsPPFc/l7/DrK7qRj8UWgOQ6pHn5m/faFjpfDMY9DIZ gcjk3VHH6HL9zT1ewO9CAPwGzI8wpiJn03EGCxeB0mmHrlVWKhN6dOitzb+7h8roPC3Y Y9HiVsp0Xv/VyS01lkdfUZ/NWYKV/4j8FnXdYAqMWW0WU8YKP16VzFPWYyhqC0xxcWl3 1DLNYk2bnORorDSHdYGPJgH9pQW8dPXq27rj0ihlj/tmUHAtdj8kMgGTCkGwOC02eN7h fiyTPkrH1s2kOHhxpFJk72WUvk0iHgncrPo9wZfF7bxyktMQ68/sgpp0Xd0WOP3r83Jk CtPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679084372; 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=DykUx1HbeAhX8oTWNoXUh40nOPiSPk2JHLd6IpEyXHY=; b=fdKdl9yehdeScX+1DjuD2UPoVtqkaJwMONQa8zW4Zr3fUpiFVb1I7en9LmfTjxGacn Rr6Ci5umXi2shDz/oZOlu5/mZYhBhDcqU/56dyag+uZDpJ8VL8UE4aNIbpJrjbe/g1vH VQOQyU1VAN2F9PtPcn/OKTwnXw5BJvN82qsAOr53yM4OdLixZHj+QgWDOrDhivWwU6Mn FWMQLfPpCllWBN3AsJH8kdSihWMYpemAEsbTS3rI4zzhBt09Uw6SdVKYEI8HOFeocJWP nPLRhPGwe1rAXRg57dc6i5gQANNpRG9HBUQQnIxR4WnXW2dJIZTZXALxf+TdPOW7KSba LCmA== X-Gm-Message-State: AO0yUKX6kWERmH5F88rM97E/8/eB656GcAV7S8my88pv+StR1IyXUsu+ NbhjWpx0/K/lxXNk/yndTyM= X-Google-Smtp-Source: AK7set962yR1KyvWKVSeJx0w7rgHEDOvHLLTb1nLv8MVTTI2NJXKOlCewJN1iF8yuKYQ2DO4b/I7Wg== X-Received: by 2002:a17:903:1247:b0:19e:d60a:e9e with SMTP id u7-20020a170903124700b0019ed60a0e9emr11540603plh.42.1679084372534; Fri, 17 Mar 2023 13:19:32 -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 y9-20020a1709029b8900b0019e6f3112b6sm1924298plp.91.2023.03.17.13.19.30 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 17 Mar 2023 13:19:32 -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 v2 bpf-next 2/4] libbpf: Fix relocation of kfunc ksym in ld_imm64 insn. Date: Fri, 17 Mar 2023 13:19:18 -0700 Message-Id: <20230317201920.62030-3-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230317201920.62030-1-alexei.starovoitov@gmail.com> References: <20230317201920.62030-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 void *p = kfunc; -> generates ld_imm64 insn. kfunc() -> generates bpf_call insn. libbpf patches bpf_call insn correctly while only btf_id part of ld_imm64 is set in the former case. Which means that pointers to kfuncs in modules are not patched correctly and the verifier rejects load of such programs due to btf_id being out of range. Fix libbpf to patch ld_imm64 for kfunc. Signed-off-by: Alexei Starovoitov --- tools/lib/bpf/libbpf.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index a557718401e4..4c34fbd7b5be 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -7533,6 +7533,12 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, ext->is_set = true; ext->ksym.kernel_btf_id = kfunc_id; ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0; + /* Also set kernel_btf_obj_fd to make sure that bpf_object__relocate_data() + * populates FD into ld_imm64 insn when it's used to point to kfunc. + * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call. + * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64. + */ + ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0; pr_debug("extern (func ksym) '%s': resolved to kernel [%d]\n", ext->name, kfunc_id); From patchwork Fri Mar 17 20:19:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13179414 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 07FC1C74A5B for ; Fri, 17 Mar 2023 20:19:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230058AbjCQUTn (ORCPT ); Fri, 17 Mar 2023 16:19:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230044AbjCQUTm (ORCPT ); Fri, 17 Mar 2023 16:19:42 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CC733B20E; Fri, 17 Mar 2023 13:19:37 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id h8so6490038plf.10; Fri, 17 Mar 2023 13:19:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679084376; 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=MXegsicfeEQTJRNtz+ivUG8LHA5872wbv9Jz/Z3VRk4=; b=QaBqPDPgXJ7sCxCDL13tYT/aZRro2hmKYgUpuMZTSOMlmfGZrK/jmpCJTjpAUIrZdX JgLQ3p8Suu2e0WWCYa6xIFI3HB1OunRBy6pXb41C/Qe6xkO9FSJOYZuNssoVCbmEJ/3Q pR60r4ft/p0R+xYlcu3uhRIyiw7FyLnizmjKGwQQJv4jSxN0eLgmT1kJoCc4VcD2f1C7 VFhMMHxis0fbOdNMU0iWcYARly7cMEBVNxK1pRi1lmeZXfBbPZLUGw8i6aLotBwGXvzo rw0l+ah6ZxXJaCKlTApwT2oW31lkeEOG6Z/Z1IgDHq3TlD4GlgMxqh/k5ZGA36rdZUGM KTxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679084376; 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=MXegsicfeEQTJRNtz+ivUG8LHA5872wbv9Jz/Z3VRk4=; b=tLA+VbKYHym1m+XDvoOakTWAAoQcq5zETvGfTPo7b0px8wkqM0Qm5cUJgcL5XVzTFy WD8ebMrkUv4gCvpFCI66G0b91oLGr8oQNDtT2VXiunh0I+GFlEyPE5PghF5bIjMLwm2S 11gb3CvwLrOUEmnAg9aI1vuip19zzZ8L/UZpVrBt8E3Hy9H77WNtW7QQV5s6NOmqgGh+ Bz9o0XPV6Yj9DUoELZI7Jz9gmlddOvdBNptoY+UywnYMcqtDOE2IWWyzPnxrfhifeJU2 qqNHuWNannPsIFrcIq3dmGiW5KHQ67Y+qs1fmReA6kxsJuTd1sj6So450oTMBYjCv0Mx tJHQ== X-Gm-Message-State: AO0yUKVtsIdhOtROIfrmPH7zkeczH3M3EKTcb+S6JaP+96yb3caBtvJX iA7ol3KKVznERFby09ehfG0= X-Google-Smtp-Source: AK7set/hdeU7pRtByU9ErrjsWFkNnP9G6CGmJifh9C4m5JQa9a9NXvgyR14f9zHdOUs4Wn3TlUSZSw== X-Received: by 2002:a17:902:ce81:b0:1a0:7602:589e with SMTP id f1-20020a170902ce8100b001a07602589emr10830122plg.40.1679084376627; Fri, 17 Mar 2023 13:19:36 -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 i20-20020a63cd14000000b0050a0227a4bcsm1851520pgg.57.2023.03.17.13.19.35 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 17 Mar 2023 13:19:36 -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 v2 bpf-next 3/4] libbpf: Introduce bpf_ksym_exists() macro. Date: Fri, 17 Mar 2023 13:19:19 -0700 Message-Id: <20230317201920.62030-4-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230317201920.62030-1-alexei.starovoitov@gmail.com> References: <20230317201920.62030-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 Introduce bpf_ksym_exists() macro that can be used by BPF programs to detect at load time whether particular ksym (either variable or kfunc) is present in the kernel. Signed-off-by: Alexei Starovoitov --- tools/lib/bpf/bpf_helpers.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h index 7d12d3e620cc..b49823117dae 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"))) +#define bpf_ksym_exists(sym) \ + ({ _Static_assert(!__builtin_constant_p(!!sym), #sym " should be marked as __weak"); !!sym; }) + #ifndef ___bpf_concat #define ___bpf_concat(a, b) a ## b #endif From patchwork Fri Mar 17 20:19:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13179415 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 76841C6FD1D for ; Fri, 17 Mar 2023 20:19:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229928AbjCQUTv (ORCPT ); Fri, 17 Mar 2023 16:19:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230039AbjCQUTo (ORCPT ); Fri, 17 Mar 2023 16:19:44 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 888A213511; Fri, 17 Mar 2023 13:19:41 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id ix20so6516794plb.3; Fri, 17 Mar 2023 13:19:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679084381; 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=ixtplnJ1knk+7+mvFxZ3DGrp1i26A6gmObViNwYENi0=; b=OnaUNDIoPkA27oKj0zLHLo7oUXb0LQu0ub1mpJi4fa/uSN+DHkf8umcTnFtw3phnwo 6CtlANkPHEjCVnAdEpt775L0k3apMEjICaYKFPZGMkRi4c98kLoWqfgYfQvL2bEjR0I6 SzvkmzoQ2bJmmRFWxTq2doQp1uB66aJ2Ww5awFb9t8Jx48twTD36PCtY5buqNitlLfZB l04FDgI56jt86igLrqeVGwoh9e29qfXF+0x7BUHlcNxpBygjCO41GoZ/6qqPvLiV9mZW WGIzLZEH+ikdnbD9Xx3Ggpc5Q9zApx6g8Sy1OxtVqXitOlE4IPeIUizNXpE94F1qTqx/ fJdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679084381; 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=ixtplnJ1knk+7+mvFxZ3DGrp1i26A6gmObViNwYENi0=; b=Qx774ywx/c2dFXJCGA+asCXTBdK7Jos3sIIWkzSECTrcvIXkmhuFpCAUfs35FhadPZ qbPXtBiSSAyUx1yd9vtanIlthH94MFK3KpkycHnmFbUt9zUSYoOLW0Tw2XTugVquxUPk AEXp7r20Sgnvme0oJ9k+2US1j50FLfNM9WImbyk8P7Rdrb8DzlG66B4Q01ADxVNmUJzM VOW7ZuImyWGzZwSMJZLbre8WKN8t163B83vKwDmql9ABXrkQ25kMEhp2jqHLX9832+xe +EnvsKijrbR2pWdbn7WWGIm4Bkrnqe/oFKwfecNE5qPKPTaBtBIiHqpzXBJUmgEwbMF1 WrCA== X-Gm-Message-State: AO0yUKVc708Urzt3Zp/N1yo7mvWUuvtj/X5xgxRzYUKyD/aA4DmUOchO SJCmt0neUcRJxPFkxbe0omQ= X-Google-Smtp-Source: AK7set/H1xf3NCZFp/2Gw5oKu6Gw+bapcdqigIrfbCgPchvYGpAjvd1rsBY81Y4VgwGeS/vn5N0FFw== X-Received: by 2002:a17:902:e38b:b0:19f:3b86:4715 with SMTP id g11-20020a170902e38b00b0019f3b864715mr6861037ple.8.1679084380795; Fri, 17 Mar 2023 13:19:40 -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 l9-20020a170902d34900b001a064282b11sm1927976plk.151.2023.03.17.13.19.39 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 17 Mar 2023 13:19:40 -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 v2 bpf-next 4/4] selftests/bpf: Add test for bpf_ksym_exists(). Date: Fri, 17 Mar 2023 13:19:20 -0700 Message-Id: <20230317201920.62030-5-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230317201920.62030-1-alexei.starovoitov@gmail.com> References: <20230317201920.62030-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_ksym_exists() and check that the verifier performs dead code elimination for non-existing kfunc. Signed-off-by: Alexei Starovoitov Reviewed-by: Martin KaFai Lau Acked-by: John Fastabend Reviewed-by: Toke Høiland-Jørgensen --- .../selftests/bpf/progs/task_kfunc_success.c | 20 ++++++++++++++++++- 1 file changed, 19 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..cfa7f12b84e8 100644 --- a/tools/testing/selftests/bpf/progs/task_kfunc_success.c +++ b/tools/testing/selftests/bpf/progs/task_kfunc_success.c @@ -17,6 +17,10 @@ int err, pid; * TP_PROTO(struct task_struct *p, u64 clone_flags) */ +struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym __weak; +void invalid_kfunc(void) __ksym __weak; +void bpf_testmod_test_mod_kfunc(int i) __ksym __weak; + static bool is_test_kfunc_task(void) { int cur_pid = bpf_get_current_pid_tgid() >> 32; @@ -26,7 +30,21 @@ 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_ksym_exists(bpf_task_acquire)) { + err = 3; + return 0; + } + if (!bpf_ksym_exists(bpf_testmod_test_mod_kfunc)) { + err = 4; + return 0; + } + if (bpf_ksym_exists(invalid_kfunc)) { + /* the verifier's dead code elimination should remove this */ + err = 5; + asm volatile ("goto -1"); /* for (;;); */ + } acquired = bpf_task_acquire(task); bpf_task_release(acquired);