bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Anton Protopopov <aspsk@isovalent.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 4/5] selftests/bpf: Add tests for fd_array_cnt
Date: Fri, 15 Nov 2024 19:06:41 -0800	[thread overview]
Message-ID: <d4a2099893f2cf3c2a97fd1960b269a0850dcf50.camel@gmail.com> (raw)
In-Reply-To: <20241115004607.3144806-5-aspsk@isovalent.com>

On Fri, 2024-11-15 at 00:46 +0000, Anton Protopopov wrote:

[...]

> @@ -0,0 +1,374 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <test_progs.h>
> +
> +#include <linux/btf.h>
> +#include <sys/syscall.h>
> +#include <bpf/bpf.h>
> +
> +static inline int _bpf_map_create(void)
> +{
> +	static union bpf_attr attr = {
> +		.map_type = BPF_MAP_TYPE_ARRAY,
> +		.key_size = 4,
> +		.value_size = 8,
> +		.max_entries = 1,
> +	};
> +
> +	return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
> +}
> +
> +#define BTF_INFO_ENC(kind, kind_flag, vlen) \
> +	((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
> +#define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)
> +#define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
> +	((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
> +#define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
> +	BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
> +	BTF_INT_ENC(encoding, bits_offset, bits)

Nit: these macro are already defined in tools/testing/selftests/bpf/test_btf.h .

> +
> +static int _btf_create(void)
> +{
> +	struct btf_blob {
> +		struct btf_header btf_hdr;
> +		__u32 types[8];
> +		__u32 str;
> +	} raw_btf = {
> +		.btf_hdr = {
> +			.magic = BTF_MAGIC,
> +			.version = BTF_VERSION,
> +			.hdr_len = sizeof(struct btf_header),
> +			.type_len = sizeof(__u32) * 8,
> +			.str_off = sizeof(__u32) * 8,
> +			.str_len = sizeof(__u32),

Nit: offsetof(struct btf_blob, str), sizeof(raw_btf.str), sizeof(raw_btf.types)
     are legal in this position.

> +		},
> +		.types = {
> +			/* long */
> +			BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),  /* [1] */
> +			/* unsigned long */
> +			BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),  /* [2] */
> +		},
> +	};
> +	static union bpf_attr attr = {
> +		.btf_size = sizeof(raw_btf),
> +	};
> +
> +	attr.btf = (long)&raw_btf;
> +
> +	return syscall(__NR_bpf, BPF_BTF_LOAD, &attr, sizeof(attr));
> +}

[...]

> +static void check_fd_array_cnt__fd_array_ok(void)
> +{
> +	int extra_fds[128];
> +	__u32 map_ids[16];
> +	__u32 nr_map_ids;
> +	int prog_fd;
> +
> +	extra_fds[0] = _bpf_map_create();
> +	if (!ASSERT_GE(extra_fds[0], 0, "_bpf_map_create"))
> +		return;
> +	extra_fds[1] = _bpf_map_create();
> +	if (!ASSERT_GE(extra_fds[1], 0, "_bpf_map_create"))
> +		return;
> +	prog_fd = load_test_prog(extra_fds, 2);
> +	if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD"))
> +		return;
> +	nr_map_ids = ARRAY_SIZE(map_ids);
> +	if (!check_expected_map_ids(prog_fd, 3, map_ids, &nr_map_ids))

Nit: should probably close prog_fd and extra_fds (and in tests below).

> +		return;
> +
> +	/* maps should still exist when original file descriptors are closed */
> +	close(extra_fds[0]);
> +	close(extra_fds[1]);
> +	if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map_ids[0] should exist"))
> +		return;
> +	if (!ASSERT_EQ(map_exists(map_ids[1]), true, "map_ids[1] should exist"))
> +		return;
> +
> +	close(prog_fd);
> +}

[...]


  reply	other threads:[~2024-11-16  3:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-15  0:46 [PATCH bpf-next 0/5] Add fd_array_cnt attribute for BPF_PROG_LOAD Anton Protopopov
2024-11-15  0:46 ` [PATCH bpf-next 1/5] bpf: add a __btf_get_by_fd helper Anton Protopopov
2024-11-15  0:46 ` [PATCH bpf-next 2/5] bpf: move map/prog compatibility checks Anton Protopopov
2024-11-15  0:46 ` [PATCH bpf-next 3/5] bpf: add fd_array_cnt attribute for prog_load Anton Protopopov
2024-11-16  3:06   ` Eduard Zingerman
2024-11-17 21:24     ` Anton Protopopov
2024-11-15  0:46 ` [PATCH bpf-next 4/5] selftests/bpf: Add tests for fd_array_cnt Anton Protopopov
2024-11-16  3:06   ` Eduard Zingerman [this message]
2024-11-17 21:30     ` Anton Protopopov
2024-11-15  0:46 ` [PATCH bpf-next 5/5] bpf: fix potential error return Anton Protopopov
2024-11-16  3:06 ` [PATCH bpf-next 0/5] Add fd_array_cnt attribute for BPF_PROG_LOAD Eduard Zingerman
2024-11-17 21:22   ` Anton Protopopov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d4a2099893f2cf3c2a97fd1960b269a0850dcf50.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=aspsk@isovalent.com \
    --cc=bpf@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).