tools/testing/selftests/bpf/prog_tests/global_map_resize.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/global_map_resize.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/global_map_resize.c
Extension
.c
Size
7998 bytes
Lines
236
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
#include <errno.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "test_global_map_resize.skel.h"
#include "test_progs.h"

static void run_prog_bss_array_sum(void)
{
	(void)syscall(__NR_getpid);
}

static void run_prog_data_array_sum(void)
{
	(void)syscall(__NR_getuid);
}

static void global_map_resize_bss_subtest(void)
{
	int err;
	struct test_global_map_resize *skel;
	struct bpf_map *map;
	const __u32 desired_sz = sizeof(skel->bss->sum) + sysconf(_SC_PAGE_SIZE) * 2;
	size_t array_len, actual_sz, new_sz;

	skel = test_global_map_resize__open();
	if (!ASSERT_OK_PTR(skel, "test_global_map_resize__open"))
		goto teardown;

	/* set some initial value before resizing.
	 * it is expected this non-zero value will be preserved
	 * while resizing.
	 */
	skel->bss->array[0] = 1;

	/* resize map value and verify the new size */
	map = skel->maps.bss;
	err = bpf_map__set_value_size(map, desired_sz);
	if (!ASSERT_OK(err, "bpf_map__set_value_size"))
		goto teardown;
	if (!ASSERT_EQ(bpf_map__value_size(map), desired_sz, "resize"))
		goto teardown;

	new_sz = sizeof(skel->data_percpu_arr->percpu_arr[0]) * libbpf_num_possible_cpus();
	err = bpf_map__set_value_size(skel->maps.data_percpu_arr, new_sz);
	ASSERT_OK(err, "percpu_arr_resize");

	/* set the expected number of elements based on the resized array */
	array_len = (desired_sz - sizeof(skel->bss->sum)) / sizeof(skel->bss->array[0]);
	if (!ASSERT_GT(array_len, 1, "array_len"))
		goto teardown;

	skel->bss = bpf_map__initial_value(skel->maps.bss, &actual_sz);
	if (!ASSERT_OK_PTR(skel->bss, "bpf_map__initial_value (ptr)"))
		goto teardown;
	if (!ASSERT_EQ(actual_sz, desired_sz, "bpf_map__initial_value (size)"))
		goto teardown;

	/* fill the newly resized array with ones,
	 * skipping the first element which was previously set
	 */
	for (int i = 1; i < array_len; i++)
		skel->bss->array[i] = 1;

	/* set global const values before loading */
	skel->rodata->pid = getpid();
	skel->rodata->bss_array_len = array_len;
	skel->rodata->data_array_len = 1;

	err = test_global_map_resize__load(skel);
	if (!ASSERT_OK(err, "test_global_map_resize__load"))
		goto teardown;
	err = test_global_map_resize__attach(skel);
	if (!ASSERT_OK(err, "test_global_map_resize__attach"))
		goto teardown;

	/* run the bpf program which will sum the contents of the array.
	 * since the array was filled with ones,verify the sum equals array_len
	 */
	run_prog_bss_array_sum();
	if (!ASSERT_EQ(skel->bss->sum, array_len, "sum"))
		goto teardown;

teardown:
	test_global_map_resize__destroy(skel);
}

static void global_map_resize_data_subtest(void)
{

Annotation

Implementation Notes