This patch adds test cases for single-cpu updates of per-cpu maps. It updates values for the first and last CPUs and checks that only their values were updated. Signed-off-by: Paul Chaignon <paul.chaignon@xxxxxxxxxx> --- tools/testing/selftests/bpf/test_maps.c | 34 ++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/test_maps.c b/tools/testing/selftests/bpf/test_maps.c index 02eae1e864c2..e3d3b15e1b91 100644 --- a/tools/testing/selftests/bpf/test_maps.c +++ b/tools/testing/selftests/bpf/test_maps.c @@ -150,6 +150,7 @@ static void test_hashmap_percpu(unsigned int task, void *data) BPF_DECLARE_PERCPU(long, value); long long key, next_key, first_key; int expected_key_mask = 0; + long new_value = 42; int fd, i; fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key), @@ -232,6 +233,23 @@ static void test_hashmap_percpu(unsigned int task, void *data) key = 1; assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); + /* Update value for a single CPU. */ + assert(bpf_map_update_elem(fd, &key, &new_value, + BPF_NOEXIST | BPF_CPU) == -1 && + errno == EEXIST); + assert(bpf_map_update_elem(fd, &key, &new_value, + ((__u64)nr_cpus) << 32 | BPF_CPU) == -1 && + errno == EINVAL); + assert(bpf_map_update_elem(fd, &key, &new_value, + BPF_EXIST | BPF_CPU) == 0); + assert(bpf_map_update_elem(fd, &key, &new_value, + (nr_cpus - 1ULL) << 32 | BPF_CPU) == 0); + assert(bpf_map_lookup_elem(fd, &key, value) == 0); + assert(bpf_percpu(value, 0) == new_value); + for (i = 1; i < nr_cpus - 1; i++) + assert(bpf_percpu(value, i) == i + 100); + assert(bpf_percpu(value, nr_cpus - 1) == new_value); + /* Delete both elements. */ key = 1; assert(bpf_map_delete_elem(fd, &key) == 0); @@ -402,6 +420,7 @@ static void test_arraymap_percpu(unsigned int task, void *data) unsigned int nr_cpus = bpf_num_possible_cpus(); BPF_DECLARE_PERCPU(long, values); int key, next_key, fd, i; + long new_value = 42; fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), sizeof(bpf_percpu(values, 0)), 2, 0); @@ -449,8 +468,21 @@ static void test_arraymap_percpu(unsigned int task, void *data) assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && errno == ENOENT); - /* Delete shouldn't succeed. */ + /* Update value for a single CPU */ key = 1; + assert(bpf_map_update_elem(fd, &key, &new_value, + ((__u64)nr_cpus) << 32 | BPF_CPU) == -1 && + errno == EINVAL); + assert(bpf_map_update_elem(fd, &key, &new_value, BPF_CPU) == 0); + assert(bpf_map_update_elem(fd, &key, &new_value, + (nr_cpus - 1ULL) << 32 | BPF_CPU) == 0); + assert(bpf_map_lookup_elem(fd, &key, values) == 0); + assert(bpf_percpu(values, 0) == new_value); + for (i = 1; i < nr_cpus - 1; i++) + assert(bpf_percpu(values, i) == i + 100); + assert(bpf_percpu(values, nr_cpus - 1) == new_value); + + /* Delete shouldn't succeed. */ assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL); close(fd); -- 2.24.0