From patchwork Mon Feb 24 16:59:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Chen X-Patchwork-Id: 13988520 X-Patchwork-Delegate: bpf@iogearbox.net Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDB93264A7B for ; Mon, 24 Feb 2025 17:02:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416577; cv=none; b=PTKmgRr5wqRGbf2zcjdmgW3p/TGp+QwcJ3IINuhkktnZpweXGkLe97UDiiWm7AI/EUuV8eVhBGV6sFYH5liFiKJHkec5KToF+mNONyARBJXlr7J419xxRaFZsVUQV/4fw5/1PWAl0JHY8IntpFPHypCsyKksRZ1h7OV3b7lU2dA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740416577; c=relaxed/simple; bh=e1HU03QaEH/WJqwMrSHwDYP8oWlp/FRk7I/D+9cATTo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=anxNUEGVZL/kse+IrteziPjAHBRpyWytHcAi2Ealcb8MPCuZz1jfvLfcsl5uhgExYlslW0xsoi1uyhuiqF4q2ADY4ILiBZuFLp/YaRU+I7jQnTzoO99szVOLZx6NO5jH4ESQ9lw7as2fZrwwo3nlH/njZaU3tMcrOunss5CZhSk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=lpDm5H4s; arc=none smtp.client-ip=95.215.58.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="lpDm5H4s" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1740416573; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qiQKTOacPogCTZt2sF84QiGnZvXEsGlrdkRpybzF4+0=; b=lpDm5H4sJdnzN0FVGveCI+7jrxzFfbnhmXhpxW3Nnuhh6TdXNlMAdIcXDFOXlU/7+yYqRC kg744NhFn8Epzua9iaUK3upeY8Ab4LxungmSXp5/ewmkDWRmAHr4VDpyOQurVCcO/LyXAP P5CH5gjfz8nr0fvspiPu0PQ08IjrGzs= From: Tao Chen To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, haoluo@google.com, jolsa@kernel.org, qmo@kernel.org Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, chen.dylane@gmail.com, Tao Chen , Tao Chen Subject: [PATCH bpf-next v8 5/5] selftests/bpf: Add libbpf_probe_bpf_kfunc API selftests Date: Tue, 25 Feb 2025 00:59:12 +0800 Message-Id: <20250224165912.599068-6-chen.dylane@linux.dev> In-Reply-To: <20250224165912.599068-1-chen.dylane@linux.dev> References: <20250224165912.599068-1-chen.dylane@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Patchwork-Delegate: bpf@iogearbox.net Add selftests for prog_kfunc feature probing. ./test_progs -t libbpf_probe_kfuncs #153 libbpf_probe_kfuncs:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Cc: Tao Chen Reviewed-by: Jiri Olsa Reviewed-by: Eduard Zingerman Signed-off-by: Tao Chen --- .../selftests/bpf/prog_tests/libbpf_probes.c | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c index 4ed46ed58a7b..d8f5475a94ce 100644 --- a/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +++ b/tools/testing/selftests/bpf/prog_tests/libbpf_probes.c @@ -126,3 +126,114 @@ void test_libbpf_probe_helpers(void) ASSERT_EQ(res, d->supported, buf); } } + +static int module_btf_fd(char *module) +{ + int fd, err; + __u32 id = 0, len; + struct bpf_btf_info info; + char name[64]; + + while (true) { + err = bpf_btf_get_next_id(id, &id); + if (err) + return -1; + + fd = bpf_btf_get_fd_by_id(id); + if (fd < 0) { + if (errno == ENOENT) + continue; + return -1; + } + len = sizeof(info); + memset(&info, 0, sizeof(info)); + info.name = ptr_to_u64(name); + info.name_len = sizeof(name); + err = bpf_btf_get_info_by_fd(fd, &info, &len); + if (err) { + close(fd); + return -1; + } + /* find target module BTF */ + if (!strcmp(name, module)) + break; + + close(fd); + } + + return fd; +} + +void test_libbpf_probe_kfuncs(void) +{ + int ret, kfunc_id, fd; + char *kfunc = "bpf_cpumask_create"; + struct btf *vmlinux_btf = NULL; + struct btf *module_btf = NULL; + + vmlinux_btf = btf__parse("/sys/kernel/btf/vmlinux", NULL); + if (!ASSERT_OK_PTR(vmlinux_btf, "btf_parse")) + return; + + kfunc_id = btf__find_by_name_kind(vmlinux_btf, kfunc, BTF_KIND_FUNC); + if (!ASSERT_GT(kfunc_id, 0, kfunc)) + goto cleanup; + + /* prog BPF_PROG_TYPE_SYSCALL supports kfunc bpf_cpumask_create */ + ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, -1, NULL); + if (!ASSERT_EQ(ret, 1, "kfunc in vmlinux support")) + goto cleanup; + + /* prog BPF_PROG_TYPE_KPROBE does not support kfunc bpf_cpumask_create */ + ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, kfunc_id, -1, NULL); + if (!ASSERT_EQ(ret, 0, "kfunc in vmlinux not suuport")) + goto cleanup; + + ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, -1, -1, NULL); + if (!ASSERT_EQ(ret, 0, "invalid kfunc id:-1")) + goto cleanup; + + ret = libbpf_probe_bpf_kfunc(100000, kfunc_id, -1, NULL); + if (!ASSERT_ERR(ret, "invalid prog type:100000")) + goto cleanup; + + if (!env.has_testmod) + goto cleanup; + + module_btf = btf__load_module_btf("bpf_testmod", vmlinux_btf); + if (!ASSERT_OK_PTR(module_btf, "load module BTF")) + goto cleanup; + + kfunc_id = btf__find_by_name(module_btf, "bpf_kfunc_call_test1"); + if (!ASSERT_GT(kfunc_id, 0, "func not found")) + goto cleanup; + + fd = module_btf_fd("bpf_testmod"); + if (!ASSERT_GE(fd, 0, "module BTF fd")) + goto cleanup; + + /* prog BPF_PROG_TYPE_SYSCALL supports kfunc bpf_kfunc_call_test1 in bpf_testmod */ + ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, fd, NULL); + if (!ASSERT_EQ(ret, 1, "kfunc in module BTF support")) + goto cleanup_fd; + + /* prog BPF_PROG_TYPE_KPROBE does not support kfunc bpf_kfunc_call_test1 + * in bpf_testmod + */ + ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_KPROBE, kfunc_id, fd, NULL); + if (!ASSERT_EQ(ret, 0, "kfunc in module BTF not support")) + goto cleanup_fd; + + ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, -1, fd, NULL); + if (!ASSERT_EQ(ret, 0, "invalid kfunc id in module BTF")) + goto cleanup_fd; + + ret = libbpf_probe_bpf_kfunc(BPF_PROG_TYPE_SYSCALL, kfunc_id, 100, NULL); + ASSERT_EQ(ret, 0, "invalid BTF fd in module BTF"); + +cleanup_fd: + close(fd); +cleanup: + btf__free(vmlinux_btf); + btf__free(module_btf); +}