From patchwork Thu Jan 27 07:15:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Tao X-Patchwork-Id: 12728135 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C66ACC433F5 for ; Fri, 28 Jan 2022 08:43:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9gLTGlFRGGjIBiXMT6GyG7Rd0k4HvHSNFiQ5pgm/90E=; b=VGGE7sPIbKybbR GMqolbEwZEM8wQAEhSXXMT4zG7e/c44pvK47IWKXPp33KedairYuSpCvw9NzSISGpk+72vPJ0t2NA OQRK5EcAo6iUmHEGARck5oFcTwIYDKYG8cxu9YN5IeYOynm5trScp2rQ/G3mZXvBKC9sYR3dVH+vz I+n4mc0Rvdu5D+KEY+CZZqdNySeMm3dL+p8MLBp0Grh+QhaxT8W0z1+kyiQbJzqutoHDGkCZyH/2a KPg//KpH68iL7h5MyCjLB4RAmH9apdtXw5v/heHCjYVR2qqyFnwtMB8amKX/Hv6C7V5q1y68g8JCv gdC/wGLmhq8KwZflmJGg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nDMpZ-0012X9-Dv; Fri, 28 Jan 2022 08:42:05 +0000 Received: from szxga02-in.huawei.com ([45.249.212.188]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nDMpU-0012UL-Ls for linux-arm-kernel@lists.infradead.org; Fri, 28 Jan 2022 08:42:02 +0000 Received: from dggpeml500025.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4JlWB846Tyz9sbn; Fri, 28 Jan 2022 16:40:32 +0800 (CST) Received: from huawei.com (10.175.124.27) by dggpeml500025.china.huawei.com (7.185.36.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Fri, 28 Jan 2022 16:41:49 +0800 From: Hou Tao To: Daniel Borkmann CC: Alexei Starovoitov , Martin KaFai Lau , Yonghong Song , Andrii Nakryiko , "David S . Miller" , Jakub Kicinski , John Fastabend , , , , Zi Shen Lim , Catalin Marinas , Will Deacon , Ard Biesheuvel , Subject: [PATCH bpf-next v2 2/2] selftests/bpf: check whether s32 is sufficient for kfunc offset Date: Thu, 27 Jan 2022 15:15:32 +0800 Message-ID: <20220127071532.384888-3-houtao1@huawei.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220127071532.384888-1-houtao1@huawei.com> References: <20220127071532.384888-1-houtao1@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500025.china.huawei.com (7.185.36.35) X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220128_004201_082646_D4FBF2BD X-CRM114-Status: GOOD ( 13.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In add_kfunc_call(), bpf_kfunc_desc->imm with type s32 is used to represent the offset of called kfunc from __bpf_call_base, so add a test to ensure that the offset will not be overflowed. Signed-off-by: Hou Tao --- .../selftests/bpf/prog_tests/ksyms_module.c | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c index d490ad80eccb..ce0cd3446931 100644 --- a/tools/testing/selftests/bpf/prog_tests/ksyms_module.c +++ b/tools/testing/selftests/bpf/prog_tests/ksyms_module.c @@ -6,6 +6,76 @@ #include "test_ksyms_module.lskel.h" #include "test_ksyms_module.skel.h" +/* Most logic comes from bpf_object__read_kallsyms_file() */ +static int test_find_func_in_kallsyms(const char *func, unsigned long *addr) +{ + /* Same as KSYM_NAME_LEN */ + char sym_name[128]; + char sym_type; + unsigned long sym_addr; + int ret, err; + FILE *f; + + f = fopen("/proc/kallsyms", "r"); + if (!f) + return -errno; + + err = -ENOENT; + while (true) { + ret = fscanf(f, "%lx %c %127s%*[^\n]\n", + &sym_addr, &sym_type, sym_name); + if (ret == EOF && feof(f)) + break; + + if (ret != 3) { + err = -EINVAL; + break; + } + + if ((sym_type == 't' || sym_type == 'T') && + !strcmp(sym_name, func)) { + *addr = sym_addr; + err = 0; + break; + } + } + + fclose(f); + return err; +} + +/* + * Check whether or not s32 in bpf_kfunc_desc is sufficient + * to represent the offset between bpf_testmod_test_mod_kfunc + * and __bpf_call_base. + */ +void test_ksyms_module_valid_offset(void) +{ + unsigned long kfunc_addr; + unsigned long base_addr; + int used_offset; + long actual_offset; + int err; + + if (!env.has_testmod) { + test__skip(); + return; + } + + err = test_find_func_in_kallsyms("bpf_testmod_test_mod_kfunc", + &kfunc_addr); + if (!ASSERT_OK(err, "find kfunc addr")) + return; + + err = test_find_func_in_kallsyms("__bpf_call_base", &base_addr); + if (!ASSERT_OK(err, "find base addr")) + return; + + used_offset = kfunc_addr - base_addr; + actual_offset = kfunc_addr - base_addr; + ASSERT_EQ((long)used_offset, actual_offset, "kfunc offset overflowed"); +} + void test_ksyms_module_lskel(void) { struct test_ksyms_module_lskel *skel; @@ -55,6 +125,8 @@ void test_ksyms_module_libbpf(void) void test_ksyms_module(void) { + if (test__start_subtest("valid_offset")) + test_ksyms_module_valid_offset(); if (test__start_subtest("lskel")) test_ksyms_module_lskel(); if (test__start_subtest("libbpf"))