Alexei Starovoitov 03b77e17ae bpf: Rename __kptr_ref -> __kptr and __kptr -> __kptr_untrusted.
__kptr meant to store PTR_UNTRUSTED kernel pointers inside bpf maps.
The concept felt useful, but didn't get much traction,
since bpf_rdonly_cast() was added soon after and bpf programs received
a simpler way to access PTR_UNTRUSTED kernel pointers
without going through restrictive __kptr usage.

Rename __kptr_ref -> __kptr and __kptr -> __kptr_untrusted to indicate
its intended usage.
The main goal of __kptr_untrusted was to read/write such pointers
directly while bpf_kptr_xchg was a mechanism to access refcnted
kernel pointers. The next patch will allow RCU protected __kptr access
with direct read. At that point __kptr_untrusted will be deprecated.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: David Vernet <void@manifault.com>
Link: https://lore.kernel.org/bpf/20230303041446.3630-2-alexei.starovoitov@gmail.com
2023-03-03 17:42:20 +01:00

50 lines
1007 B
C

// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
struct map_value {
struct task_struct __kptr_untrusted *ptr;
};
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 1);
__type(key, int);
__type(value, struct map_value);
} lru_map SEC(".maps");
int pid = 0;
int result = 1;
SEC("fentry/bpf_ktime_get_ns")
int printk(void *ctx)
{
struct map_value v = {};
if (pid == bpf_get_current_task_btf()->pid)
bpf_map_update_elem(&lru_map, &(int){0}, &v, 0);
return 0;
}
SEC("fentry/do_nanosleep")
int nanosleep(void *ctx)
{
struct map_value val = {}, *v;
struct task_struct *current;
bpf_map_update_elem(&lru_map, &(int){0}, &val, 0);
v = bpf_map_lookup_elem(&lru_map, &(int){0});
if (!v)
return 0;
bpf_map_delete_elem(&lru_map, &(int){0});
current = bpf_get_current_task_btf();
v->ptr = current;
pid = current->pid;
bpf_ktime_get_ns();
result = !v->ptr;
return 0;
}
char _license[] SEC("license") = "GPL";