From patchwork Tue Jun 16 10:05:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 217719 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4F2CC433E1 for ; Tue, 16 Jun 2020 10:05:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7DF7520734 for ; Tue, 16 Jun 2020 10:05:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592301939; bh=2C7Uc9uVWFtK0tSjDTSUSqLwwkPzsQ/UQ+F4UE4oujo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=GWw/3lz7yuWbwbNyU4gueSe01IT0Tr2Z7yxYQNbEobWAGkmDHRD7JJxaC7p6J7ul5 qfvL4OR726H6Pp4tIiWPpOq27e22nVNWOjmvATO8eh7CqyMpkRpaX4F4Omq5p2yduX xfGkchY6HVj8MkPE7GRmFnCeCoxBaTq9Dwlfrhnc= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728274AbgFPKFi convert rfc822-to-8bit (ORCPT ); Tue, 16 Jun 2020 06:05:38 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:46345 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726261AbgFPKFf (ORCPT ); Tue, 16 Jun 2020 06:05:35 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-146-pjeXWtzxNaaRByx-W77-7A-1; Tue, 16 Jun 2020 06:05:32 -0400 X-MC-Unique: pjeXWtzxNaaRByx-W77-7A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E3669193F587; Tue, 16 Jun 2020 10:05:29 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.195.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 42F3E5D9DD; Tue, 16 Jun 2020 10:05:23 +0000 (UTC) From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Song Liu , Yonghong Song , Martin KaFai Lau , David Miller , John Fastabend , Wenbo Zhang , KP Singh , Andrii Nakryiko , Brendan Gregg , Florent Revest , Al Viro Subject: [PATCH 02/11] bpf: Compile btfid tool at kernel compilation start Date: Tue, 16 Jun 2020 12:05:03 +0200 Message-Id: <20200616100512.2168860-3-jolsa@kernel.org> In-Reply-To: <20200616100512.2168860-1-jolsa@kernel.org> References: <20200616100512.2168860-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kernel.org Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The btfid tool will be used during the vmlinux linking, so it's necessary it's ready for it. Signed-off-by: Jiri Olsa --- Makefile | 22 ++++++++++++++++++---- tools/Makefile | 3 +++ tools/bpf/Makefile | 5 ++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 839f9fee22cb..b190d502d7d7 100644 --- a/Makefile +++ b/Makefile @@ -1066,9 +1066,10 @@ export mod_sign_cmd HOST_LIBELF_LIBS = $(shell pkg-config libelf --libs 2>/dev/null || echo -lelf) +has_libelf = $(call try-run,\ + echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0) + ifdef CONFIG_STACK_VALIDATION - has_libelf := $(call try-run,\ - echo "int main() {}" | $(HOSTCC) -xc -o /dev/null $(HOST_LIBELF_LIBS) -,1,0) ifeq ($(has_libelf),1) objtool_target := tools/objtool FORCE else @@ -1077,6 +1078,14 @@ ifdef CONFIG_STACK_VALIDATION endif endif +ifdef CONFIG_DEBUG_INFO_BTF + ifeq ($(has_libelf),1) + btfid_target := tools/bpf/btfid FORCE + else + ERROR_BTF_IDS_RESOLVE := 1 + endif +endif + PHONY += prepare0 export MODORDER := $(extmod-prefix)modules.order @@ -1188,7 +1197,7 @@ prepare0: archprepare $(Q)$(MAKE) $(build)=. # All the preparing.. -prepare: prepare0 prepare-objtool +prepare: prepare0 prepare-objtool prepare-btfid # Support for using generic headers in asm-generic asm-generic := -f $(srctree)/scripts/Makefile.asm-generic obj @@ -1201,7 +1210,7 @@ uapi-asm-generic: $(Q)$(MAKE) $(asm-generic)=arch/$(SRCARCH)/include/generated/uapi/asm \ generic=include/uapi/asm-generic -PHONY += prepare-objtool +PHONY += prepare-objtool prepare-btfid prepare-objtool: $(objtool_target) ifeq ($(SKIP_STACK_VALIDATION),1) ifdef CONFIG_UNWINDER_ORC @@ -1212,6 +1221,11 @@ else endif endif +prepare-btfid: $(btfid_target) +ifeq ($(ERROR_BTF_IDS_RESOLVE),1) + @echo "error: Cannot resolve BTF IDs for CONFIG_DEBUG_INFO_BTF, please install libelf-dev, libelf-devel or elfutils-libelf-devel" >&2 + @false +endif # Generate some files # --------------------------------------------------------------------------- diff --git a/tools/Makefile b/tools/Makefile index bd778812e915..85af6ebbce91 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -67,6 +67,9 @@ cpupower: FORCE cgroup firewire hv guest bootconfig spi usb virtio vm bpf iio gpio objtool leds wmi pci firmware debugging: FORCE $(call descend,$@) +bpf/%: FORCE + $(call descend,$@) + liblockdep: FORCE $(call descend,lib/lockdep) diff --git a/tools/bpf/Makefile b/tools/bpf/Makefile index 77472e28c8fd..d8bbe7ef264f 100644 --- a/tools/bpf/Makefile +++ b/tools/bpf/Makefile @@ -124,5 +124,8 @@ runqslower_install: runqslower_clean: $(call descend,runqslower,clean) +btfid: + $(call descend,btfid) + .PHONY: all install clean bpftool bpftool_install bpftool_clean \ - runqslower runqslower_install runqslower_clean + runqslower runqslower_install runqslower_clean btfid From patchwork Tue Jun 16 10:05:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 217718 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF7E6C433E0 for ; Tue, 16 Jun 2020 10:05:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 974D12078E for ; Tue, 16 Jun 2020 10:05:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592301952; bh=giwX+2ZdBVf6YYXrb1PkJLZYGeMbN+hGfX/N1vbvSD4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=q7hWP2nYy7NBgEtYDmyABlEXKOryVkZ7hvcdlEn39EOMIysz56KF3k60OvQ3Sxqk4 sh8lthdDd1J/u2DknbjnV9aVIpgD6JsfFlLQC0AZGN83DctAB6fNcXGfMXDAhan7xb bbKpq2oGPuyyB4F62DNOzRNFTQyZE1d//fq95ZPs= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728322AbgFPKFv convert rfc822-to-8bit (ORCPT ); Tue, 16 Jun 2020 06:05:51 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:24096 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728264AbgFPKFv (ORCPT ); Tue, 16 Jun 2020 06:05:51 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-90-DMGxkoffOPON-Fih67QkUw-1; Tue, 16 Jun 2020 06:05:42 -0400 X-MC-Unique: DMGxkoffOPON-Fih67QkUw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 99EE618A2671; Tue, 16 Jun 2020 10:05:40 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.195.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id AFBC65D994; Tue, 16 Jun 2020 10:05:35 +0000 (UTC) From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Song Liu , Yonghong Song , Martin KaFai Lau , David Miller , John Fastabend , Wenbo Zhang , KP Singh , Andrii Nakryiko , Brendan Gregg , Florent Revest , Al Viro Subject: [PATCH 04/11] bpf: Resolve BTF IDs in vmlinux image Date: Tue, 16 Jun 2020 12:05:05 +0200 Message-Id: <20200616100512.2168860-5-jolsa@kernel.org> In-Reply-To: <20200616100512.2168860-1-jolsa@kernel.org> References: <20200616100512.2168860-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kernel.org Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Run btfid on vmlinux object during linking, so the .BTF_ids section is processed and IDs are resolved. Signed-off-by: Jiri Olsa --- Makefile | 3 ++- include/linux/bpf.h | 5 +++++ kernel/bpf/btf_ids.c | 12 ++++++++++++ kernel/trace/bpf_trace.c | 2 -- net/core/filter.c | 2 -- scripts/link-vmlinux.sh | 6 ++++++ 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index b190d502d7d7..889d909fd71a 100644 --- a/Makefile +++ b/Makefile @@ -448,6 +448,7 @@ OBJSIZE = $(CROSS_COMPILE)size STRIP = $(CROSS_COMPILE)strip endif PAHOLE = pahole +BTFID = $(srctree)/tools/bpf/btfid/btfid LEX = flex YACC = bison AWK = awk @@ -524,7 +525,7 @@ GCC_PLUGINS_CFLAGS := CLANG_FLAGS := export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC -export CPP AR NM STRIP OBJCOPY OBJDUMP OBJSIZE READELF PAHOLE LEX YACC AWK INSTALLKERNEL +export CPP AR NM STRIP OBJCOPY OBJDUMP OBJSIZE READELF PAHOLE BTFID LEX YACC AWK INSTALLKERNEL export PERL PYTHON PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX export _GZIP _BZIP2 _LZOP LZMA LZ4 XZ export KBUILD_HOSTCXXFLAGS KBUILD_HOSTLDFLAGS KBUILD_HOSTLDLIBS LDFLAGS_MODULE diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 07052d44bca1..f18c23dcc858 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1743,4 +1743,9 @@ enum bpf_text_poke_type { int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t, void *addr1, void *addr2); +extern int bpf_skb_output_btf_ids[]; +extern int bpf_seq_printf_btf_ids[]; +extern int bpf_seq_write_btf_ids[]; +extern int bpf_xdp_output_btf_ids[]; + #endif /* _LINUX_BPF_H */ diff --git a/kernel/bpf/btf_ids.c b/kernel/bpf/btf_ids.c index e7f9d94ad293..d8d0df162f04 100644 --- a/kernel/bpf/btf_ids.c +++ b/kernel/bpf/btf_ids.c @@ -1,3 +1,15 @@ // SPDX-License-Identifier: GPL-2.0-only #include "btf_ids.h" + +BTF_ID_LIST(bpf_skb_output_btf_ids) +BTF_ID(struct, sk_buff) + +BTF_ID_LIST(bpf_seq_printf_btf_ids) +BTF_ID(struct, seq_file) + +BTF_ID_LIST(bpf_seq_write_btf_ids) +BTF_ID(struct, seq_file) + +BTF_ID_LIST(bpf_xdp_output_btf_ids) +BTF_ID(struct, xdp_buff) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 3744372a24e2..c1866d76041f 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -667,7 +667,6 @@ BPF_CALL_5(bpf_seq_printf, struct seq_file *, m, char *, fmt, u32, fmt_size, return err; } -static int bpf_seq_printf_btf_ids[5]; static const struct bpf_func_proto bpf_seq_printf_proto = { .func = bpf_seq_printf, .gpl_only = true, @@ -685,7 +684,6 @@ BPF_CALL_3(bpf_seq_write, struct seq_file *, m, const void *, data, u32, len) return seq_write(m, data, len) ? -EOVERFLOW : 0; } -static int bpf_seq_write_btf_ids[5]; static const struct bpf_func_proto bpf_seq_write_proto = { .func = bpf_seq_write, .gpl_only = true, diff --git a/net/core/filter.c b/net/core/filter.c index 209482a4eaa2..440e52061be8 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -3775,7 +3775,6 @@ static const struct bpf_func_proto bpf_skb_event_output_proto = { .arg5_type = ARG_CONST_SIZE_OR_ZERO, }; -static int bpf_skb_output_btf_ids[5]; const struct bpf_func_proto bpf_skb_output_proto = { .func = bpf_skb_event_output, .gpl_only = true, @@ -4169,7 +4168,6 @@ static const struct bpf_func_proto bpf_xdp_event_output_proto = { .arg5_type = ARG_CONST_SIZE_OR_ZERO, }; -static int bpf_xdp_output_btf_ids[5]; const struct bpf_func_proto bpf_xdp_output_proto = { .func = bpf_xdp_event_output, .gpl_only = true, diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 57cb14bd8925..99a3f8c65e84 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -336,6 +336,12 @@ fi vmlinux_link vmlinux "${kallsymso}" ${btf_vmlinux_bin_o} +# fill in BTF IDs +if [ -n "${CONFIG_DEBUG_INFO_BTF}" ]; then +info BTFID vmlinux +${BTFID} vmlinux +fi + if [ -n "${CONFIG_BUILDTIME_TABLE_SORT}" ]; then info SORTTAB vmlinux if ! sorttable vmlinux; then From patchwork Tue Jun 16 10:05:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 217717 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 28D3EC433E0 for ; Tue, 16 Jun 2020 10:06:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 010F920707 for ; Tue, 16 Jun 2020 10:06:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592301967; bh=m0VD0bw8O9CbKHRF/BXQinLeV5L6c1OoHxc7pmqmjuo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=jtvkuvWHibPB34YfiKYjUNV5TNGhfycshkQOZI0jMg/69968t6UadBfqkvOkTeOhX flmu4CJnREt/m2+8m34DMBchyjpz3u/9oQ1w3DmsjBgMiLdbEK6i2YvpoifU3m8hd0 FBAuJ3sGBPgiPsCk83WFki3cnQaJGaQhYXJ2cqsM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728401AbgFPKGF convert rfc822-to-8bit (ORCPT ); Tue, 16 Jun 2020 06:06:05 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:41127 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728329AbgFPKFz (ORCPT ); Tue, 16 Jun 2020 06:05:55 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-260-pmrSLxL7Mt-zGYQIo-_dIQ-1; Tue, 16 Jun 2020 06:05:48 -0400 X-MC-Unique: pmrSLxL7Mt-zGYQIo-_dIQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2F7FF107AD98; Tue, 16 Jun 2020 10:05:45 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.195.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 011B35D9DA; Tue, 16 Jun 2020 10:05:40 +0000 (UTC) From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Song Liu , Yonghong Song , Martin KaFai Lau , David Miller , John Fastabend , Wenbo Zhang , KP Singh , Andrii Nakryiko , Brendan Gregg , Florent Revest , Al Viro Subject: [PATCH 05/11] bpf: Remove btf_id helpers resolving Date: Tue, 16 Jun 2020 12:05:06 +0200 Message-Id: <20200616100512.2168860-6-jolsa@kernel.org> In-Reply-To: <20200616100512.2168860-1-jolsa@kernel.org> References: <20200616100512.2168860-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kernel.org Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Now when we moved the helpers btf_id into .BTF_ids section, we can remove the code that resolve those IDs in runtime. Signed-off-by: Jiri Olsa --- kernel/bpf/btf.c | 88 +++--------------------------------------------- 1 file changed, 4 insertions(+), 84 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 58c9af1d4808..aea7b2cc8d26 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -4049,96 +4049,16 @@ int btf_struct_access(struct bpf_verifier_log *log, return -EINVAL; } -static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn, - int arg) -{ - char fnname[KSYM_SYMBOL_LEN + 4] = "btf_"; - const struct btf_param *args; - const struct btf_type *t; - const char *tname, *sym; - u32 btf_id, i; - - if (IS_ERR(btf_vmlinux)) { - bpf_log(log, "btf_vmlinux is malformed\n"); - return -EINVAL; - } - - sym = kallsyms_lookup((long)fn, NULL, NULL, NULL, fnname + 4); - if (!sym) { - bpf_log(log, "kernel doesn't have kallsyms\n"); - return -EFAULT; - } - - for (i = 1; i <= btf_vmlinux->nr_types; i++) { - t = btf_type_by_id(btf_vmlinux, i); - if (BTF_INFO_KIND(t->info) != BTF_KIND_TYPEDEF) - continue; - tname = __btf_name_by_offset(btf_vmlinux, t->name_off); - if (!strcmp(tname, fnname)) - break; - } - if (i > btf_vmlinux->nr_types) { - bpf_log(log, "helper %s type is not found\n", fnname); - return -ENOENT; - } - - t = btf_type_by_id(btf_vmlinux, t->type); - if (!btf_type_is_ptr(t)) - return -EFAULT; - t = btf_type_by_id(btf_vmlinux, t->type); - if (!btf_type_is_func_proto(t)) - return -EFAULT; - - args = (const struct btf_param *)(t + 1); - if (arg >= btf_type_vlen(t)) { - bpf_log(log, "bpf helper %s doesn't have %d-th argument\n", - fnname, arg); - return -EINVAL; - } - - t = btf_type_by_id(btf_vmlinux, args[arg].type); - if (!btf_type_is_ptr(t) || !t->type) { - /* anything but the pointer to struct is a helper config bug */ - bpf_log(log, "ARG_PTR_TO_BTF is misconfigured\n"); - return -EFAULT; - } - btf_id = t->type; - t = btf_type_by_id(btf_vmlinux, t->type); - /* skip modifiers */ - while (btf_type_is_modifier(t)) { - btf_id = t->type; - t = btf_type_by_id(btf_vmlinux, t->type); - } - if (!btf_type_is_struct(t)) { - bpf_log(log, "ARG_PTR_TO_BTF is not a struct\n"); - return -EFAULT; - } - bpf_log(log, "helper %s arg%d has btf_id %d struct %s\n", fnname + 4, - arg, btf_id, __btf_name_by_offset(btf_vmlinux, t->name_off)); - return btf_id; -} - int btf_resolve_helper_id(struct bpf_verifier_log *log, const struct bpf_func_proto *fn, int arg) { - int *btf_id = &fn->btf_id[arg]; - int ret; - if (fn->arg_type[arg] != ARG_PTR_TO_BTF_ID) return -EINVAL; - ret = READ_ONCE(*btf_id); - if (ret) - return ret; - /* ok to race the search. The result is the same */ - ret = __btf_resolve_helper_id(log, fn->func, arg); - if (!ret) { - /* Function argument cannot be type 'void' */ - bpf_log(log, "BTF resolution bug\n"); - return -EFAULT; - } - WRITE_ONCE(*btf_id, ret); - return ret; + if (WARN_ON_ONCE(!fn->btf_id)) + return -EINVAL; + + return fn->btf_id[arg]; } static int __get_type_size(struct btf *btf, u32 btf_id, From patchwork Tue Jun 16 10:05:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 217716 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59318C433DF for ; Tue, 16 Jun 2020 10:06:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3315020734 for ; Tue, 16 Jun 2020 10:06:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592301974; bh=0ywlkednVggh9I7+TBEKsl++LHT7iCbm2pWBu5zCdGQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=IWypccwWUcdiozm7tYGd3gGdgRMS1E/cbsKzhtcPs4Wc8fUYY4TZV5rn0PHF026sz ViL8529aZOrOA7FNglgOj1vac+Ltx9JL5dy8WNRwqezU+FwNT0vcqHdKk/GqCdmZOY b5C6DoIcWW5sLZu3zx+5pflP3gSrvibZcGKkaUsA= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728411AbgFPKGM convert rfc822-to-8bit (ORCPT ); Tue, 16 Jun 2020 06:06:12 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:31088 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728326AbgFPKFz (ORCPT ); Tue, 16 Jun 2020 06:05:55 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-469-pX9VizinNoG_56EMAEfUlQ-1; Tue, 16 Jun 2020 06:05:51 -0400 X-MC-Unique: pX9VizinNoG_56EMAEfUlQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4DD1681EE58; Tue, 16 Jun 2020 10:05:49 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.195.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 805765D747; Tue, 16 Jun 2020 10:05:45 +0000 (UTC) From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Song Liu , Yonghong Song , Martin KaFai Lau , David Miller , John Fastabend , Wenbo Zhang , KP Singh , Andrii Nakryiko , Brendan Gregg , Florent Revest , Al Viro Subject: [PATCH 06/11] bpf: Do not pass enum bpf_access_type to btf_struct_access Date: Tue, 16 Jun 2020 12:05:07 +0200 Message-Id: <20200616100512.2168860-7-jolsa@kernel.org> In-Reply-To: <20200616100512.2168860-1-jolsa@kernel.org> References: <20200616100512.2168860-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kernel.org Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org There's no need for it. Signed-off-by: Jiri Olsa --- include/linux/bpf.h | 1 - kernel/bpf/btf.c | 3 +-- kernel/bpf/verifier.c | 2 +- net/ipv4/bpf_tcp_ca.c | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f18c23dcc858..b7d3b5f3dc09 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1282,7 +1282,6 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, struct bpf_insn_access_aux *info); int btf_struct_access(struct bpf_verifier_log *log, const struct btf_type *t, int off, int size, - enum bpf_access_type atype, u32 *next_btf_id); int btf_resolve_helper_id(struct bpf_verifier_log *log, const struct bpf_func_proto *fn, int); diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index aea7b2cc8d26..304369a4c2e2 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -3831,7 +3831,6 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, int btf_struct_access(struct bpf_verifier_log *log, const struct btf_type *t, int off, int size, - enum bpf_access_type atype, u32 *next_btf_id) { u32 i, moff, mtrue_end, msize = 0, total_nelems = 0; @@ -3880,7 +3879,7 @@ int btf_struct_access(struct bpf_verifier_log *log, goto error; off = (off - moff) % elem_type->size; - return btf_struct_access(log, elem_type, off, size, atype, + return btf_struct_access(log, elem_type, off, size, next_btf_id); error: diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 5c7bbaac81ef..b553e4523bd3 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3175,7 +3175,7 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env, return -EACCES; } - ret = btf_struct_access(&env->log, t, off, size, atype, + ret = btf_struct_access(&env->log, t, off, size, &btf_id); } diff --git a/net/ipv4/bpf_tcp_ca.c b/net/ipv4/bpf_tcp_ca.c index e3939f76b024..c6aab9389ac4 100644 --- a/net/ipv4/bpf_tcp_ca.c +++ b/net/ipv4/bpf_tcp_ca.c @@ -130,7 +130,7 @@ static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log, size_t end; if (atype == BPF_READ) - return btf_struct_access(log, t, off, size, atype, next_btf_id); + return btf_struct_access(log, t, off, size, next_btf_id); if (t != tcp_sock_type) { bpf_log(log, "only read is supported\n"); From patchwork Tue Jun 16 10:05:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 217715 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A73D6C433E0 for ; Tue, 16 Jun 2020 10:06:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 82F6820707 for ; Tue, 16 Jun 2020 10:06:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592301984; bh=s5AIqPLMYjaiTPQsq7x7wZzFuQIUauowM5NMNehpPMM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=0ujReB7FSVrcR6nTEwJ4qx1QAwrPiTIXkKr/5adXFQJ03lW19X12OlZLA8unKu+Ue zwby4BJcyV8GB/ZRU54zGPC/CfRqDcsGtU8owUm3Wbo0FapgvfqnxhzgTucjspB7XG qNXOJSB1WLnHo/T18KGgj7rw2nAGJ3PxcAYpn8Jc= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728427AbgFPKGW convert rfc822-to-8bit (ORCPT ); Tue, 16 Jun 2020 06:06:22 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:22179 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728420AbgFPKGV (ORCPT ); Tue, 16 Jun 2020 06:06:21 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-294-1j_u1g3XMPmpIZEatOc1uQ-1; Tue, 16 Jun 2020 06:06:14 -0400 X-MC-Unique: 1j_u1g3XMPmpIZEatOc1uQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9DE191030C53; Tue, 16 Jun 2020 10:06:11 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.195.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 495195D9D5; Tue, 16 Jun 2020 10:06:06 +0000 (UTC) From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, Song Liu , Yonghong Song , Martin KaFai Lau , David Miller , John Fastabend , Wenbo Zhang , KP Singh , Andrii Nakryiko , Brendan Gregg , Florent Revest , Al Viro Subject: [PATCH 10/11] selftests/bpf: Add verifier test for d_path helper Date: Tue, 16 Jun 2020 12:05:11 +0200 Message-Id: <20200616100512.2168860-11-jolsa@kernel.org> In-Reply-To: <20200616100512.2168860-1-jolsa@kernel.org> References: <20200616100512.2168860-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kernel.org Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Adding verifier test for attaching tracing program and calling d_path helper from within and testing that it's allowed for dentry_open function and denied for 'd_path' function with appropriate error. Signed-off-by: Jiri Olsa --- tools/testing/selftests/bpf/test_verifier.c | 13 ++++++- tools/testing/selftests/bpf/verifier/d_path.c | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/verifier/d_path.c diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c index 78a6bae56ea6..3cce3dc766a2 100644 --- a/tools/testing/selftests/bpf/test_verifier.c +++ b/tools/testing/selftests/bpf/test_verifier.c @@ -114,6 +114,7 @@ struct bpf_test { bpf_testdata_struct_t retvals[MAX_TEST_RUNS]; }; enum bpf_attach_type expected_attach_type; + const char *kfunc; }; /* Note we want this to be 64 bit aligned so that the end of our array is @@ -984,8 +985,18 @@ static void do_test_single(struct bpf_test *test, bool unpriv, attr.log_level = 4; attr.prog_flags = pflags; + if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) { + attr.attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc, + attr.expected_attach_type); + } + fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog)); - if (fd_prog < 0 && !bpf_probe_prog_type(prog_type, 0)) { + + /* BPF_PROG_TYPE_TRACING requires more setup and + * bpf_probe_prog_type won't give correct answer + */ + if (fd_prog < 0 && (prog_type != BPF_PROG_TYPE_TRACING) && + !bpf_probe_prog_type(prog_type, 0)) { printf("SKIP (unsupported program type %d)\n", prog_type); skips++; goto close_fds; diff --git a/tools/testing/selftests/bpf/verifier/d_path.c b/tools/testing/selftests/bpf/verifier/d_path.c new file mode 100644 index 000000000000..e08181abc056 --- /dev/null +++ b/tools/testing/selftests/bpf/verifier/d_path.c @@ -0,0 +1,38 @@ +{ + "d_path accept", + .insns = { + BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1, 0), + BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), + BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8), + BPF_MOV64_IMM(BPF_REG_6, 0), + BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_6, 0), + BPF_LD_IMM64(BPF_REG_3, 8), + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_d_path), + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }, + .errstr = "R0 max value is outside of the array range", + .result = ACCEPT, + .prog_type = BPF_PROG_TYPE_TRACING, + .expected_attach_type = BPF_TRACE_FENTRY, + .kfunc = "dentry_open", +}, +{ + "d_path reject", + .insns = { + BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_1, 0), + BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), + BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8), + BPF_MOV64_IMM(BPF_REG_6, 0), + BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_6, 0), + BPF_LD_IMM64(BPF_REG_3, 8), + BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_d_path), + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }, + .errstr = "helper call is not allowed in probe", + .result = REJECT, + .prog_type = BPF_PROG_TYPE_TRACING, + .expected_attach_type = BPF_TRACE_FENTRY, + .kfunc = "d_path", +}, From patchwork Tue Jun 16 10:05:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 217714 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4266C433E0 for ; Tue, 16 Jun 2020 10:06:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AC0B020707 for ; Tue, 16 Jun 2020 10:06:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592301993; bh=8tHoLQijZGCPmHqM8wBljsAxNbmESUQv/drTAn03ax4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=PUuBIT6TVP64qAn8qjwzuV0ZI979ZahvJGM+kQEAyf0VMZydL2scxRQ6ij3uyC4+k M7b0FOoeoYWmgSANQQwUbMzgEablSrJzviLMsRSrFnTt9KRNThRU+utcbeq0+bWjXo cjSUjYzBWptd1/HEoU6GW45rDW5g+KvZhCh3EyA4= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728455AbgFPKG3 convert rfc822-to-8bit (ORCPT ); Tue, 16 Jun 2020 06:06:29 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:33132 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728187AbgFPKG0 (ORCPT ); Tue, 16 Jun 2020 06:06:26 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-215-e22mfcrOPB2jR3SPGXD2QA-1; Tue, 16 Jun 2020 06:06:20 -0400 X-MC-Unique: e22mfcrOPB2jR3SPGXD2QA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 38CED803315; Tue, 16 Jun 2020 10:06:17 +0000 (UTC) Received: from krava.redhat.com (unknown [10.40.195.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 01ADC5D9D5; Tue, 16 Jun 2020 10:06:11 +0000 (UTC) From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann Cc: Wenbo Zhang , netdev@vger.kernel.org, bpf@vger.kernel.org, Song Liu , Yonghong Song , Martin KaFai Lau , David Miller , John Fastabend , KP Singh , Andrii Nakryiko , Brendan Gregg , Florent Revest , Al Viro Subject: [PATCH 11/11] selftests/bpf: Add test for d_path helper Date: Tue, 16 Jun 2020 12:05:12 +0200 Message-Id: <20200616100512.2168860-12-jolsa@kernel.org> In-Reply-To: <20200616100512.2168860-1-jolsa@kernel.org> References: <20200616100512.2168860-1-jolsa@kernel.org> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kernel.org Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Adding test for d_path helper which is pretty much copied from Wenbo Zhang's test for bpf_get_fd_path, which never made it in. I've failed so far to compile the test with kernel header, so for now adding 'struct file' with f_path member that has same offset as kernel's file object. Original-patch-by: Wenbo Zhang Signed-off-by: Jiri Olsa --- .../testing/selftests/bpf/prog_tests/d_path.c | 153 ++++++++++++++++++ .../testing/selftests/bpf/progs/test_d_path.c | 55 +++++++ 2 files changed, 208 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/d_path.c create mode 100644 tools/testing/selftests/bpf/progs/test_d_path.c diff --git a/tools/testing/selftests/bpf/prog_tests/d_path.c b/tools/testing/selftests/bpf/prog_tests/d_path.c new file mode 100644 index 000000000000..e2b7dfeb506f --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/d_path.c @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include + +#define MAX_PATH_LEN 128 +#define MAX_FILES 7 +#define MAX_EVENT_NUM 16 + +struct d_path_test_data { + pid_t pid; + __u32 cnt_stat; + __u32 cnt_close; + char paths_stat[MAX_EVENT_NUM][MAX_PATH_LEN]; + char paths_close[MAX_EVENT_NUM][MAX_PATH_LEN]; +}; + +#include "test_d_path.skel.h" + +static struct { + __u32 cnt; + char paths[MAX_EVENT_NUM][MAX_PATH_LEN]; +} src; + +static int set_pathname(int fd, pid_t pid) +{ + char buf[MAX_PATH_LEN]; + + snprintf(buf, MAX_PATH_LEN, "/proc/%d/fd/%d", pid, fd); + return readlink(buf, src.paths[src.cnt++], MAX_PATH_LEN); +} + +static int trigger_fstat_events(pid_t pid) +{ + int sockfd = -1, procfd = -1, devfd = -1; + int localfd = -1, indicatorfd = -1; + int pipefd[2] = { -1, -1 }; + struct stat fileStat; + int ret = -1; + + /* unmountable pseudo-filesystems */ + if (CHECK_FAIL(pipe(pipefd) < 0)) + return ret; + /* unmountable pseudo-filesystems */ + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (CHECK_FAIL(sockfd < 0)) + goto out_close; + /* mountable pseudo-filesystems */ + procfd = open("/proc/self/comm", O_RDONLY); + if (CHECK_FAIL(procfd < 0)) + goto out_close; + devfd = open("/dev/urandom", O_RDONLY); + if (CHECK_FAIL(devfd < 0)) + goto out_close; + localfd = open("/tmp/d_path_loadgen.txt", O_CREAT | O_RDONLY); + if (CHECK_FAIL(localfd < 0)) + goto out_close; + /* bpf_d_path will return path with (deleted) */ + remove("/tmp/d_path_loadgen.txt"); + indicatorfd = open("/tmp/", O_PATH); + if (CHECK_FAIL(indicatorfd < 0)) + goto out_close; + + ret = set_pathname(pipefd[0], pid); + if (CHECK_FAIL(ret < 0)) + goto out_close; + ret = set_pathname(pipefd[1], pid); + if (CHECK_FAIL(ret < 0)) + goto out_close; + ret = set_pathname(sockfd, pid); + if (CHECK_FAIL(ret < 0)) + goto out_close; + ret = set_pathname(procfd, pid); + if (CHECK_FAIL(ret < 0)) + goto out_close; + ret = set_pathname(devfd, pid); + if (CHECK_FAIL(ret < 0)) + goto out_close; + ret = set_pathname(localfd, pid); + if (CHECK_FAIL(ret < 0)) + goto out_close; + ret = set_pathname(indicatorfd, pid); + if (CHECK_FAIL(ret < 0)) + goto out_close; + + /* triggers vfs_getattr */ + fstat(pipefd[0], &fileStat); + fstat(pipefd[1], &fileStat); + fstat(sockfd, &fileStat); + fstat(procfd, &fileStat); + fstat(devfd, &fileStat); + fstat(localfd, &fileStat); + fstat(indicatorfd, &fileStat); + +out_close: + /* triggers filp_close */ + close(pipefd[0]); + close(pipefd[1]); + close(sockfd); + close(procfd); + close(devfd); + close(localfd); + close(indicatorfd); + return ret; +} + +void test_d_path(void) +{ + struct test_d_path *skel; + struct d_path_test_data *dst; + __u32 duration = 0; + int err; + + skel = test_d_path__open_and_load(); + if (CHECK(!skel, "test_d_path_load", "d_path skeleton failed\n")) + goto cleanup; + + err = test_d_path__attach(skel); + if (CHECK(err, "modify_return", "attach failed: %d\n", err)) + goto cleanup; + + dst = &skel->bss->data; + dst->pid = getpid(); + + err = trigger_fstat_events(skel->bss->data.pid); + if (CHECK_FAIL(err < 0)) + goto cleanup; + + for (int i = 0; i < MAX_FILES; i++) { + if (i < 3) { + CHECK((dst->paths_stat[i][0] == 0), "d_path", + "failed to filter fs [%d]: %s vs %s\n", + i, src.paths[i], dst->paths_stat[i]); + CHECK((dst->paths_close[i][0] == 0), "d_path", + "failed to filter fs [%d]: %s vs %s\n", + i, src.paths[i], dst->paths_close[i]); + } else { + CHECK(strncmp(src.paths[i], dst->paths_stat[i], MAX_PATH_LEN), + "d_path", + "failed to get stat path[%d]: %s vs %s\n", + i, src.paths[i], dst->paths_stat[i]); + CHECK(strncmp(src.paths[i], dst->paths_close[i], MAX_PATH_LEN), + "d_path", + "failed to get close path[%d]: %s vs %s\n", + i, src.paths[i], dst->paths_close[i]); + } + } + +cleanup: + test_d_path__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_d_path.c b/tools/testing/selftests/bpf/progs/test_d_path.c new file mode 100644 index 000000000000..1b478c00ee7a --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_d_path.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include + +#define MAX_PATH_LEN 128 +#define MAX_EVENT_NUM 16 + +static struct d_path_test_data { + pid_t pid; + __u32 cnt_stat; + __u32 cnt_close; + char paths_stat[MAX_EVENT_NUM][MAX_PATH_LEN]; + char paths_close[MAX_EVENT_NUM][MAX_PATH_LEN]; +} data; + +struct path; +struct kstat; + +SEC("fentry/vfs_getattr") +int BPF_PROG(prog_stat, struct path *path, struct kstat *stat, + __u32 request_mask, unsigned int query_flags) +{ + pid_t pid = bpf_get_current_pid_tgid() >> 32; + + if (pid != data.pid) + return 0; + + if (data.cnt_stat >= MAX_EVENT_NUM) + return 0; + + bpf_d_path(path, data.paths_stat[data.cnt_stat], MAX_PATH_LEN); + data.cnt_stat++; + return 0; +} + +SEC("fentry/filp_close") +int BPF_PROG(prog_close, struct file *file, void *id) +{ + pid_t pid = bpf_get_current_pid_tgid() >> 32; + + if (pid != data.pid) + return 0; + + if (data.cnt_close >= MAX_EVENT_NUM) + return 0; + + bpf_d_path((struct path *) &file->f_path, + data.paths_close[data.cnt_close], MAX_PATH_LEN); + data.cnt_close++; + return 0; +} + +char _license[] SEC("license") = "GPL";