Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf

Alexei Starovoitov says:

====================
pull-request: bpf 2020-11-06

1) Pre-allocated per-cpu hashmap needs to zero-fill reused element, from David.

2) Tighten bpf_lsm function check, from KP.

3) Fix bpftool attaching to flow dissector, from Lorenz.

4) Use -fno-gcse for the whole kernel/bpf/core.c instead of function attribute, from Ard.

* git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf:
  bpf: Update verification logic for LSM programs
  bpf: Zero-fill re-used per-cpu map element
  bpf: BPF_PRELOAD depends on BPF_SYSCALL
  tools/bpftool: Fix attaching flow dissector
  libbpf: Fix possible use after free in xsk_socket__delete
  libbpf: Fix null dereference in xsk_socket__delete
  libbpf, hashmap: Fix undefined behavior in hash_bits
  bpf: Don't rely on GCC __attribute__((optimize)) to disable GCSE
  tools, bpftool: Remove two unused variables.
  tools, bpftool: Avoid array index warnings.
  xsk: Fix possible memory leak at socket close
  bpf: Add struct bpf_redir_neigh forward declaration to BPF helper defs
  samples/bpf: Set rlimit for memlock to infinity in all samples
  bpf: Fix -Wshadow warnings
  selftest/bpf: Fix profiler test using CO-RE relocation for enums
====================

Link: https://lore.kernel.org/r/20201106221759.24143-1-alexei.starovoitov@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski
2020-11-06 17:49:34 -08:00
25 changed files with 346 additions and 49 deletions
@@ -0,0 +1,214 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2020 Tessares SA <http://www.tessares.net> */
#include <test_progs.h>
#include "test_map_init.skel.h"
#define TEST_VALUE 0x1234
#define FILL_VALUE 0xdeadbeef
static int nr_cpus;
static int duration;
typedef unsigned long long map_key_t;
typedef unsigned long long map_value_t;
typedef struct {
map_value_t v; /* padding */
} __bpf_percpu_val_align pcpu_map_value_t;
static int map_populate(int map_fd, int num)
{
pcpu_map_value_t value[nr_cpus];
int i, err;
map_key_t key;
for (i = 0; i < nr_cpus; i++)
bpf_percpu(value, i) = FILL_VALUE;
for (key = 1; key <= num; key++) {
err = bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST);
if (!ASSERT_OK(err, "bpf_map_update_elem"))
return -1;
}
return 0;
}
static struct test_map_init *setup(enum bpf_map_type map_type, int map_sz,
int *map_fd, int populate)
{
struct test_map_init *skel;
int err;
skel = test_map_init__open();
if (!ASSERT_OK_PTR(skel, "skel_open"))
return NULL;
err = bpf_map__set_type(skel->maps.hashmap1, map_type);
if (!ASSERT_OK(err, "bpf_map__set_type"))
goto error;
err = bpf_map__set_max_entries(skel->maps.hashmap1, map_sz);
if (!ASSERT_OK(err, "bpf_map__set_max_entries"))
goto error;
err = test_map_init__load(skel);
if (!ASSERT_OK(err, "skel_load"))
goto error;
*map_fd = bpf_map__fd(skel->maps.hashmap1);
if (CHECK(*map_fd < 0, "bpf_map__fd", "failed\n"))
goto error;
err = map_populate(*map_fd, populate);
if (!ASSERT_OK(err, "map_populate"))
goto error_map;
return skel;
error_map:
close(*map_fd);
error:
test_map_init__destroy(skel);
return NULL;
}
/* executes bpf program that updates map with key, value */
static int prog_run_insert_elem(struct test_map_init *skel, map_key_t key,
map_value_t value)
{
struct test_map_init__bss *bss;
bss = skel->bss;
bss->inKey = key;
bss->inValue = value;
bss->inPid = getpid();
if (!ASSERT_OK(test_map_init__attach(skel), "skel_attach"))
return -1;
/* Let tracepoint trigger */
syscall(__NR_getpgid);
test_map_init__detach(skel);
return 0;
}
static int check_values_one_cpu(pcpu_map_value_t *value, map_value_t expected)
{
int i, nzCnt = 0;
map_value_t val;
for (i = 0; i < nr_cpus; i++) {
val = bpf_percpu(value, i);
if (val) {
if (CHECK(val != expected, "map value",
"unexpected for cpu %d: 0x%llx\n", i, val))
return -1;
nzCnt++;
}
}
if (CHECK(nzCnt != 1, "map value", "set for %d CPUs instead of 1!\n",
nzCnt))
return -1;
return 0;
}
/* Add key=1 elem with values set for all CPUs
* Delete elem key=1
* Run bpf prog that inserts new key=1 elem with value=0x1234
* (bpf prog can only set value for current CPU)
* Lookup Key=1 and check value is as expected for all CPUs:
* value set by bpf prog for one CPU, 0 for all others
*/
static void test_pcpu_map_init(void)
{
pcpu_map_value_t value[nr_cpus];
struct test_map_init *skel;
int map_fd, err;
map_key_t key;
/* max 1 elem in map so insertion is forced to reuse freed entry */
skel = setup(BPF_MAP_TYPE_PERCPU_HASH, 1, &map_fd, 1);
if (!ASSERT_OK_PTR(skel, "prog_setup"))
return;
/* delete element so the entry can be re-used*/
key = 1;
err = bpf_map_delete_elem(map_fd, &key);
if (!ASSERT_OK(err, "bpf_map_delete_elem"))
goto cleanup;
/* run bpf prog that inserts new elem, re-using the slot just freed */
err = prog_run_insert_elem(skel, key, TEST_VALUE);
if (!ASSERT_OK(err, "prog_run_insert_elem"))
goto cleanup;
/* check that key=1 was re-created by bpf prog */
err = bpf_map_lookup_elem(map_fd, &key, value);
if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
goto cleanup;
/* and has expected values */
check_values_one_cpu(value, TEST_VALUE);
cleanup:
test_map_init__destroy(skel);
}
/* Add key=1 and key=2 elems with values set for all CPUs
* Run bpf prog that inserts new key=3 elem
* (only for current cpu; other cpus should have initial value = 0)
* Lookup Key=1 and check value is as expected for all CPUs
*/
static void test_pcpu_lru_map_init(void)
{
pcpu_map_value_t value[nr_cpus];
struct test_map_init *skel;
int map_fd, err;
map_key_t key;
/* Set up LRU map with 2 elements, values filled for all CPUs.
* With these 2 elements, the LRU map is full
*/
skel = setup(BPF_MAP_TYPE_LRU_PERCPU_HASH, 2, &map_fd, 2);
if (!ASSERT_OK_PTR(skel, "prog_setup"))
return;
/* run bpf prog that inserts new key=3 element, re-using LRU slot */
key = 3;
err = prog_run_insert_elem(skel, key, TEST_VALUE);
if (!ASSERT_OK(err, "prog_run_insert_elem"))
goto cleanup;
/* check that key=3 replaced one of earlier elements */
err = bpf_map_lookup_elem(map_fd, &key, value);
if (!ASSERT_OK(err, "bpf_map_lookup_elem"))
goto cleanup;
/* and has expected values */
check_values_one_cpu(value, TEST_VALUE);
cleanup:
test_map_init__destroy(skel);
}
void test_map_init(void)
{
nr_cpus = bpf_num_possible_cpus();
if (nr_cpus <= 1) {
printf("%s:SKIP: >1 cpu needed for this test\n", __func__);
test__skip();
return;
}
if (test__start_subtest("pcpu_map_init"))
test_pcpu_map_init();
if (test__start_subtest("pcpu_lru_map_init"))
test_pcpu_lru_map_init();
}
@@ -243,7 +243,10 @@ static ino_t get_inode_from_kernfs(struct kernfs_node* node)
}
}
int pids_cgrp_id = 1;
extern bool CONFIG_CGROUP_PIDS __kconfig __weak;
enum cgroup_subsys_id___local {
pids_cgrp_id___local = 123, /* value doesn't matter */
};
static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data,
struct task_struct* task,
@@ -253,7 +256,9 @@ static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data,
BPF_CORE_READ(task, nsproxy, cgroup_ns, root_cset, dfl_cgrp, kn);
struct kernfs_node* proc_kernfs = BPF_CORE_READ(task, cgroups, dfl_cgrp, kn);
if (ENABLE_CGROUP_V1_RESOLVER) {
if (ENABLE_CGROUP_V1_RESOLVER && CONFIG_CGROUP_PIDS) {
int cgrp_id = bpf_core_enum_value(enum cgroup_subsys_id___local,
pids_cgrp_id___local);
#ifdef UNROLL
#pragma unroll
#endif
@@ -262,7 +267,7 @@ static INLINE void* populate_cgroup_info(struct cgroup_data_t* cgroup_data,
BPF_CORE_READ(task, cgroups, subsys[i]);
if (subsys != NULL) {
int subsys_id = BPF_CORE_READ(subsys, ss, id);
if (subsys_id == pids_cgrp_id) {
if (subsys_id == cgrp_id) {
proc_kernfs = BPF_CORE_READ(subsys, cgroup, kn);
root_kernfs = BPF_CORE_READ(subsys, ss, root, kf_root, kn);
break;
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Tessares SA <http://www.tessares.net> */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
__u64 inKey = 0;
__u64 inValue = 0;
__u32 inPid = 0;
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
__uint(max_entries, 2);
__type(key, __u64);
__type(value, __u64);
} hashmap1 SEC(".maps");
SEC("tp/syscalls/sys_enter_getpgid")
int sysenter_getpgid(const void *ctx)
{
/* Just do it for once, when called from our own test prog. This
* ensures the map value is only updated for a single CPU.
*/
int cur_pid = bpf_get_current_pid_tgid() >> 32;
if (cur_pid == inPid)
bpf_map_update_elem(&hashmap1, &inKey, &inValue, BPF_NOEXIST);
return 0;
}
char _license[] SEC("license") = "GPL";