2019-11-14 18:57:04 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/* Copyright (c) 2019 Facebook */
|
|
|
|
#include <linux/hash.h>
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/filter.h>
|
2019-12-09 00:01:13 +00:00
|
|
|
#include <linux/ftrace.h>
|
2020-01-23 16:15:07 +00:00
|
|
|
#include <linux/rbtree_latch.h>
|
2020-03-12 19:56:05 +00:00
|
|
|
#include <linux/perf_event.h>
|
2020-03-29 00:43:52 +00:00
|
|
|
#include <linux/btf.h>
|
2020-08-27 22:01:11 +00:00
|
|
|
#include <linux/rcupdate_trace.h>
|
|
|
|
#include <linux/rcupdate_wait.h>
|
2021-09-10 18:33:51 +00:00
|
|
|
#include <linux/static_call.h>
|
2022-06-28 17:43:06 +00:00
|
|
|
#include <linux/bpf_verifier.h>
|
|
|
|
#include <linux/bpf_lsm.h>
|
2022-07-20 00:21:26 +00:00
|
|
|
#include <linux/delay.h>
|
2019-11-14 18:57:04 +00:00
|
|
|
|
2020-01-21 00:53:46 +00:00
|
|
|
/* dummy _ops. The verifier will operate on target program's ops. */
|
|
|
|
const struct bpf_verifier_ops bpf_extension_verifier_ops = {
|
|
|
|
};
|
|
|
|
const struct bpf_prog_ops bpf_extension_prog_ops = {
|
|
|
|
};
|
|
|
|
|
2019-11-14 18:57:04 +00:00
|
|
|
/* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */
|
|
|
|
#define TRAMPOLINE_HASH_BITS 10
|
|
|
|
#define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
|
|
|
|
|
|
|
|
static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
|
|
|
|
|
2020-03-12 19:56:07 +00:00
|
|
|
/* serializes access to trampoline_table */
|
2019-11-14 18:57:04 +00:00
|
|
|
static DEFINE_MUTEX(trampoline_mutex);
|
|
|
|
|
2022-07-20 00:21:26 +00:00
|
|
|
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
|
|
|
|
static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex);
|
|
|
|
|
|
|
|
static int bpf_tramp_ftrace_ops_func(struct ftrace_ops *ops, enum ftrace_ops_cmd cmd)
|
|
|
|
{
|
|
|
|
struct bpf_trampoline *tr = ops->private;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (cmd == FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF) {
|
|
|
|
/* This is called inside register_ftrace_direct_multi(), so
|
|
|
|
* tr->mutex is already locked.
|
|
|
|
*/
|
|
|
|
lockdep_assert_held_once(&tr->mutex);
|
|
|
|
|
|
|
|
/* Instead of updating the trampoline here, we propagate
|
2023-03-21 14:04:21 +00:00
|
|
|
* -EAGAIN to register_ftrace_direct(). Then we can
|
|
|
|
* retry register_ftrace_direct() after updating the
|
2022-07-20 00:21:26 +00:00
|
|
|
* trampoline.
|
|
|
|
*/
|
|
|
|
if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) &&
|
|
|
|
!(tr->flags & BPF_TRAMP_F_ORIG_STACK)) {
|
|
|
|
if (WARN_ON_ONCE(tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY;
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The normal locking order is
|
|
|
|
* tr->mutex => direct_mutex (ftrace.c) => ftrace_lock (ftrace.c)
|
|
|
|
*
|
|
|
|
* The following two commands are called from
|
|
|
|
*
|
|
|
|
* prepare_direct_functions_for_ipmodify
|
|
|
|
* cleanup_direct_functions_after_ipmodify
|
|
|
|
*
|
|
|
|
* In both cases, direct_mutex is already locked. Use
|
|
|
|
* mutex_trylock(&tr->mutex) to avoid deadlock in race condition
|
|
|
|
* (something else is making changes to this same trampoline).
|
|
|
|
*/
|
|
|
|
if (!mutex_trylock(&tr->mutex)) {
|
|
|
|
/* sleep 1 ms to make sure whatever holding tr->mutex makes
|
|
|
|
* some progress.
|
|
|
|
*/
|
|
|
|
msleep(1);
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER:
|
|
|
|
tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY;
|
|
|
|
|
|
|
|
if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) &&
|
|
|
|
!(tr->flags & BPF_TRAMP_F_ORIG_STACK))
|
|
|
|
ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */);
|
|
|
|
break;
|
|
|
|
case FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER:
|
|
|
|
tr->flags &= ~BPF_TRAMP_F_SHARE_IPMODIFY;
|
|
|
|
|
|
|
|
if (tr->flags & BPF_TRAMP_F_ORIG_STACK)
|
|
|
|
ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
2022-07-25 22:27:33 +00:00
|
|
|
}
|
2022-07-20 00:21:26 +00:00
|
|
|
|
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-08 19:32:44 +00:00
|
|
|
bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
|
|
|
|
{
|
|
|
|
enum bpf_attach_type eatype = prog->expected_attach_type;
|
2022-05-10 20:59:21 +00:00
|
|
|
enum bpf_prog_type ptype = prog->type;
|
2021-12-08 19:32:44 +00:00
|
|
|
|
2022-05-10 20:59:21 +00:00
|
|
|
return (ptype == BPF_PROG_TYPE_TRACING &&
|
|
|
|
(eatype == BPF_TRACE_FENTRY || eatype == BPF_TRACE_FEXIT ||
|
|
|
|
eatype == BPF_MODIFY_RETURN)) ||
|
|
|
|
(ptype == BPF_PROG_TYPE_LSM && eatype == BPF_LSM_MAC);
|
2021-12-08 19:32:44 +00:00
|
|
|
}
|
|
|
|
|
bpf: Add kernel symbol for struct_ops trampoline
Without kernel symbols for struct_ops trampoline, the unwinder may
produce unexpected stacktraces.
For example, the x86 ORC and FP unwinders check if an IP is in kernel
text by verifying the presence of the IP's kernel symbol. When a
struct_ops trampoline address is encountered, the unwinder stops due
to the absence of symbol, resulting in an incomplete stacktrace that
consists only of direct and indirect child functions called from the
trampoline.
The arm64 unwinder is another example. While the arm64 unwinder can
proceed across a struct_ops trampoline address, the corresponding
symbol name is displayed as "unknown", which is confusing.
Thus, add kernel symbol for struct_ops trampoline. The name is
bpf__<struct_ops_name>_<member_name>, where <struct_ops_name> is the
type name of the struct_ops, and <member_name> is the name of
the member that the trampoline is linked to.
Below is a comparison of stacktraces captured on x86 by perf record,
before and after this patch.
Before:
ffffffff8116545d __lock_acquire+0xad ([kernel.kallsyms])
ffffffff81167fcc lock_acquire+0xcc ([kernel.kallsyms])
ffffffff813088f4 __bpf_prog_enter+0x34 ([kernel.kallsyms])
After:
ffffffff811656bd __lock_acquire+0x30d ([kernel.kallsyms])
ffffffff81167fcc lock_acquire+0xcc ([kernel.kallsyms])
ffffffff81309024 __bpf_prog_enter+0x34 ([kernel.kallsyms])
ffffffffc000d7e9 bpf__tcp_congestion_ops_cong_avoid+0x3e ([kernel.kallsyms])
ffffffff81f250a5 tcp_ack+0x10d5 ([kernel.kallsyms])
ffffffff81f27c66 tcp_rcv_established+0x3b6 ([kernel.kallsyms])
ffffffff81f3ad03 tcp_v4_do_rcv+0x193 ([kernel.kallsyms])
ffffffff81d65a18 __release_sock+0xd8 ([kernel.kallsyms])
ffffffff81d65af4 release_sock+0x34 ([kernel.kallsyms])
ffffffff81f15c4b tcp_sendmsg+0x3b ([kernel.kallsyms])
ffffffff81f663d7 inet_sendmsg+0x47 ([kernel.kallsyms])
ffffffff81d5ab40 sock_write_iter+0x160 ([kernel.kallsyms])
ffffffff8149c67b vfs_write+0x3fb ([kernel.kallsyms])
ffffffff8149caf6 ksys_write+0xc6 ([kernel.kallsyms])
ffffffff8149cb5d __x64_sys_write+0x1d ([kernel.kallsyms])
ffffffff81009200 x64_sys_call+0x1d30 ([kernel.kallsyms])
ffffffff82232d28 do_syscall_64+0x68 ([kernel.kallsyms])
ffffffff8240012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms])
Fixes: 85d33df357b6 ("bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS")
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20241112145849.3436772-4-xukuohai@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-11-12 14:58:49 +00:00
|
|
|
void bpf_image_ksym_init(void *data, unsigned int size, struct bpf_ksym *ksym)
|
2020-03-12 19:56:05 +00:00
|
|
|
{
|
|
|
|
ksym->start = (unsigned long) data;
|
2023-12-06 22:40:53 +00:00
|
|
|
ksym->end = ksym->start + size;
|
bpf: Add kernel symbol for struct_ops trampoline
Without kernel symbols for struct_ops trampoline, the unwinder may
produce unexpected stacktraces.
For example, the x86 ORC and FP unwinders check if an IP is in kernel
text by verifying the presence of the IP's kernel symbol. When a
struct_ops trampoline address is encountered, the unwinder stops due
to the absence of symbol, resulting in an incomplete stacktrace that
consists only of direct and indirect child functions called from the
trampoline.
The arm64 unwinder is another example. While the arm64 unwinder can
proceed across a struct_ops trampoline address, the corresponding
symbol name is displayed as "unknown", which is confusing.
Thus, add kernel symbol for struct_ops trampoline. The name is
bpf__<struct_ops_name>_<member_name>, where <struct_ops_name> is the
type name of the struct_ops, and <member_name> is the name of
the member that the trampoline is linked to.
Below is a comparison of stacktraces captured on x86 by perf record,
before and after this patch.
Before:
ffffffff8116545d __lock_acquire+0xad ([kernel.kallsyms])
ffffffff81167fcc lock_acquire+0xcc ([kernel.kallsyms])
ffffffff813088f4 __bpf_prog_enter+0x34 ([kernel.kallsyms])
After:
ffffffff811656bd __lock_acquire+0x30d ([kernel.kallsyms])
ffffffff81167fcc lock_acquire+0xcc ([kernel.kallsyms])
ffffffff81309024 __bpf_prog_enter+0x34 ([kernel.kallsyms])
ffffffffc000d7e9 bpf__tcp_congestion_ops_cong_avoid+0x3e ([kernel.kallsyms])
ffffffff81f250a5 tcp_ack+0x10d5 ([kernel.kallsyms])
ffffffff81f27c66 tcp_rcv_established+0x3b6 ([kernel.kallsyms])
ffffffff81f3ad03 tcp_v4_do_rcv+0x193 ([kernel.kallsyms])
ffffffff81d65a18 __release_sock+0xd8 ([kernel.kallsyms])
ffffffff81d65af4 release_sock+0x34 ([kernel.kallsyms])
ffffffff81f15c4b tcp_sendmsg+0x3b ([kernel.kallsyms])
ffffffff81f663d7 inet_sendmsg+0x47 ([kernel.kallsyms])
ffffffff81d5ab40 sock_write_iter+0x160 ([kernel.kallsyms])
ffffffff8149c67b vfs_write+0x3fb ([kernel.kallsyms])
ffffffff8149caf6 ksys_write+0xc6 ([kernel.kallsyms])
ffffffff8149cb5d __x64_sys_write+0x1d ([kernel.kallsyms])
ffffffff81009200 x64_sys_call+0x1d30 ([kernel.kallsyms])
ffffffff82232d28 do_syscall_64+0x68 ([kernel.kallsyms])
ffffffff8240012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms])
Fixes: 85d33df357b6 ("bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS")
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20241112145849.3436772-4-xukuohai@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-11-12 14:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bpf_image_ksym_add(struct bpf_ksym *ksym)
|
|
|
|
{
|
2020-03-12 19:56:05 +00:00
|
|
|
bpf_ksym_add(ksym);
|
|
|
|
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
|
2020-03-12 19:56:07 +00:00
|
|
|
PAGE_SIZE, false, ksym->name);
|
2020-03-12 19:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bpf_image_ksym_del(struct bpf_ksym *ksym)
|
|
|
|
{
|
|
|
|
bpf_ksym_del(ksym);
|
|
|
|
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
|
2020-03-12 19:56:07 +00:00
|
|
|
PAGE_SIZE, true, ksym->name);
|
2020-03-12 19:56:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 21:25:02 +00:00
|
|
|
static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
|
2019-11-14 18:57:04 +00:00
|
|
|
{
|
|
|
|
struct bpf_trampoline *tr;
|
|
|
|
struct hlist_head *head;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mutex_lock(&trampoline_mutex);
|
|
|
|
head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
|
|
|
|
hlist_for_each_entry(tr, head, hlist) {
|
|
|
|
if (tr->key == key) {
|
|
|
|
refcount_inc(&tr->refcnt);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tr = kzalloc(sizeof(*tr), GFP_KERNEL);
|
|
|
|
if (!tr)
|
|
|
|
goto out;
|
2022-07-20 00:21:26 +00:00
|
|
|
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
|
|
|
|
tr->fops = kzalloc(sizeof(struct ftrace_ops), GFP_KERNEL);
|
|
|
|
if (!tr->fops) {
|
|
|
|
kfree(tr);
|
|
|
|
tr = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
tr->fops->private = tr;
|
|
|
|
tr->fops->ops_func = bpf_tramp_ftrace_ops_func;
|
|
|
|
#endif
|
2019-11-14 18:57:04 +00:00
|
|
|
|
|
|
|
tr->key = key;
|
|
|
|
INIT_HLIST_NODE(&tr->hlist);
|
|
|
|
hlist_add_head(&tr->hlist, head);
|
|
|
|
refcount_set(&tr->refcnt, 1);
|
|
|
|
mutex_init(&tr->mutex);
|
|
|
|
for (i = 0; i < BPF_TRAMP_MAX; i++)
|
|
|
|
INIT_HLIST_HEAD(&tr->progs_hlist[i]);
|
|
|
|
out:
|
|
|
|
mutex_unlock(&trampoline_mutex);
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
2019-12-09 00:01:13 +00:00
|
|
|
static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
|
|
|
|
{
|
|
|
|
void *ip = tr->func.addr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (tr->func.ftrace_managed)
|
2023-03-21 14:04:21 +00:00
|
|
|
ret = unregister_ftrace_direct(tr->fops, (long)old_addr, false);
|
2019-12-09 00:01:13 +00:00
|
|
|
else
|
|
|
|
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
|
2021-03-26 10:59:00 +00:00
|
|
|
|
2019-12-09 00:01:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-20 00:21:26 +00:00
|
|
|
static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr,
|
|
|
|
bool lock_direct_mutex)
|
2019-12-09 00:01:13 +00:00
|
|
|
{
|
|
|
|
void *ip = tr->func.addr;
|
|
|
|
int ret;
|
|
|
|
|
2022-07-20 00:21:26 +00:00
|
|
|
if (tr->func.ftrace_managed) {
|
|
|
|
if (lock_direct_mutex)
|
2023-03-21 14:04:21 +00:00
|
|
|
ret = modify_ftrace_direct(tr->fops, (long)new_addr);
|
2022-07-20 00:21:26 +00:00
|
|
|
else
|
2023-03-21 14:04:21 +00:00
|
|
|
ret = modify_ftrace_direct_nolock(tr->fops, (long)new_addr);
|
2022-07-20 00:21:26 +00:00
|
|
|
} else {
|
2019-12-09 00:01:13 +00:00
|
|
|
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr);
|
2022-07-20 00:21:26 +00:00
|
|
|
}
|
2019-12-09 00:01:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* first time registering */
|
|
|
|
static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
|
|
|
|
{
|
|
|
|
void *ip = tr->func.addr;
|
2022-03-08 15:30:29 +00:00
|
|
|
unsigned long faddr;
|
2019-12-09 00:01:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2022-03-08 15:30:29 +00:00
|
|
|
faddr = ftrace_location((unsigned long)ip);
|
bpf: Fix NULL pointer dereference when registering bpf trampoline
A panic was reported on arm64:
[ 44.517109] audit: type=1334 audit(1658859870.268:59): prog-id=19 op=LOAD
[ 44.622031] Unable to handle kernel NULL pointer dereference at
virtual address 0000000000000010
[ 44.624321] Mem abort info:
[ 44.625049] ESR = 0x0000000096000004
[ 44.625935] EC = 0x25: DABT (current EL), IL = 32 bits
[ 44.627182] SET = 0, FnV = 0
[ 44.627930] EA = 0, S1PTW = 0
[ 44.628684] FSC = 0x04: level 0 translation fault
[ 44.629788] Data abort info:
[ 44.630474] ISV = 0, ISS = 0x00000004
[ 44.631362] CM = 0, WnR = 0
[ 44.632041] user pgtable: 4k pages, 48-bit VAs, pgdp=0000000100ab5000
[ 44.633494] [0000000000000010] pgd=0000000000000000, p4d=0000000000000000
[ 44.635202] Internal error: Oops: 96000004 [#1] SMP
[ 44.636452] Modules linked in: xfs crct10dif_ce ghash_ce virtio_blk
virtio_console virtio_mmio qemu_fw_cfg
[ 44.638713] CPU: 2 PID: 1 Comm: systemd Not tainted 5.19.0-rc7 #1
[ 44.640164] Hardware name: QEMU KVM Virtual Machine, BIOS 0.0.0 02/06/2015
[ 44.641799] pstate: 00400005 (nzcv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[ 44.643404] pc : ftrace_set_filter_ip+0x24/0xa0
[ 44.644659] lr : bpf_trampoline_update.constprop.0+0x428/0x4a0
[ 44.646118] sp : ffff80000803b9f0
[ 44.646950] x29: ffff80000803b9f0 x28: ffff0b5d80364400 x27: ffff80000803bb48
[ 44.648721] x26: ffff8000085ad000 x25: ffff0b5d809d2400 x24: 0000000000000000
[ 44.650493] x23: 00000000ffffffed x22: ffff0b5dd7ea0900 x21: 0000000000000000
[ 44.652279] x20: 0000000000000000 x19: 0000000000000000 x18: ffffffffffffffff
[ 44.654067] x17: 0000000000000000 x16: 0000000000000000 x15: ffffffffffffffff
[ 44.655787] x14: ffff0b5d809d2498 x13: ffff0b5d809d2432 x12: 0000000005f5e100
[ 44.657535] x11: abcc77118461cefd x10: 000000000000005f x9 : ffffa7219cb5b190
[ 44.659254] x8 : ffffa7219c8e0000 x7 : 0000000000000000 x6 : ffffa7219db075e0
[ 44.661066] x5 : ffffa7219d3130e0 x4 : ffffa7219cab9da0 x3 : 0000000000000000
[ 44.662837] x2 : 0000000000000000 x1 : ffffa7219cb7a5c0 x0 : 0000000000000000
[ 44.664675] Call trace:
[ 44.665274] ftrace_set_filter_ip+0x24/0xa0
[ 44.666327] bpf_trampoline_update.constprop.0+0x428/0x4a0
[ 44.667696] __bpf_trampoline_link_prog+0xcc/0x1c0
[ 44.668834] bpf_trampoline_link_prog+0x40/0x64
[ 44.669919] bpf_tracing_prog_attach+0x120/0x490
[ 44.671011] link_create+0xe0/0x2b0
[ 44.671869] __sys_bpf+0x484/0xd30
[ 44.672706] __arm64_sys_bpf+0x30/0x40
[ 44.673678] invoke_syscall+0x78/0x100
[ 44.674623] el0_svc_common.constprop.0+0x4c/0xf4
[ 44.675783] do_el0_svc+0x38/0x4c
[ 44.676624] el0_svc+0x34/0x100
[ 44.677429] el0t_64_sync_handler+0x11c/0x150
[ 44.678532] el0t_64_sync+0x190/0x194
[ 44.679439] Code: 2a0203f4 f90013f5 2a0303f5 f9001fe1 (f9400800)
[ 44.680959] ---[ end trace 0000000000000000 ]---
[ 44.682111] Kernel panic - not syncing: Oops: Fatal exception
[ 44.683488] SMP: stopping secondary CPUs
[ 44.684551] Kernel Offset: 0x2721948e0000 from 0xffff800008000000
[ 44.686095] PHYS_OFFSET: 0xfffff4a380000000
[ 44.687144] CPU features: 0x010,00022811,19001080
[ 44.688308] Memory Limit: none
[ 44.689082] ---[ end Kernel panic - not syncing: Oops: Fatal exception ]---
It's caused by a NULL tr->fops passed to ftrace_set_filter_ip(). tr->fops
is initialized to NULL and is assigned to an allocated memory address if
CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS is enabled. Since there is no
direct call on arm64 yet, the config can't be enabled.
To fix it, call ftrace_set_filter_ip() only if tr->fops is not NULL.
Fixes: 00963a2e75a8 ("bpf: Support bpf_trampoline on functions with IPMODIFY (e.g. livepatch)")
Reported-by: Bruno Goncalves <bgoncalv@redhat.com>
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Tested-by: Bruno Goncalves <bgoncalv@redhat.com>
Acked-by: Song Liu <songliubraving@fb.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20220728114048.3540461-1-xukuohai@huaweicloud.com
2022-07-28 11:40:48 +00:00
|
|
|
if (faddr) {
|
|
|
|
if (!tr->fops)
|
|
|
|
return -ENOTSUPP;
|
2022-03-08 15:30:29 +00:00
|
|
|
tr->func.ftrace_managed = true;
|
bpf: Fix NULL pointer dereference when registering bpf trampoline
A panic was reported on arm64:
[ 44.517109] audit: type=1334 audit(1658859870.268:59): prog-id=19 op=LOAD
[ 44.622031] Unable to handle kernel NULL pointer dereference at
virtual address 0000000000000010
[ 44.624321] Mem abort info:
[ 44.625049] ESR = 0x0000000096000004
[ 44.625935] EC = 0x25: DABT (current EL), IL = 32 bits
[ 44.627182] SET = 0, FnV = 0
[ 44.627930] EA = 0, S1PTW = 0
[ 44.628684] FSC = 0x04: level 0 translation fault
[ 44.629788] Data abort info:
[ 44.630474] ISV = 0, ISS = 0x00000004
[ 44.631362] CM = 0, WnR = 0
[ 44.632041] user pgtable: 4k pages, 48-bit VAs, pgdp=0000000100ab5000
[ 44.633494] [0000000000000010] pgd=0000000000000000, p4d=0000000000000000
[ 44.635202] Internal error: Oops: 96000004 [#1] SMP
[ 44.636452] Modules linked in: xfs crct10dif_ce ghash_ce virtio_blk
virtio_console virtio_mmio qemu_fw_cfg
[ 44.638713] CPU: 2 PID: 1 Comm: systemd Not tainted 5.19.0-rc7 #1
[ 44.640164] Hardware name: QEMU KVM Virtual Machine, BIOS 0.0.0 02/06/2015
[ 44.641799] pstate: 00400005 (nzcv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[ 44.643404] pc : ftrace_set_filter_ip+0x24/0xa0
[ 44.644659] lr : bpf_trampoline_update.constprop.0+0x428/0x4a0
[ 44.646118] sp : ffff80000803b9f0
[ 44.646950] x29: ffff80000803b9f0 x28: ffff0b5d80364400 x27: ffff80000803bb48
[ 44.648721] x26: ffff8000085ad000 x25: ffff0b5d809d2400 x24: 0000000000000000
[ 44.650493] x23: 00000000ffffffed x22: ffff0b5dd7ea0900 x21: 0000000000000000
[ 44.652279] x20: 0000000000000000 x19: 0000000000000000 x18: ffffffffffffffff
[ 44.654067] x17: 0000000000000000 x16: 0000000000000000 x15: ffffffffffffffff
[ 44.655787] x14: ffff0b5d809d2498 x13: ffff0b5d809d2432 x12: 0000000005f5e100
[ 44.657535] x11: abcc77118461cefd x10: 000000000000005f x9 : ffffa7219cb5b190
[ 44.659254] x8 : ffffa7219c8e0000 x7 : 0000000000000000 x6 : ffffa7219db075e0
[ 44.661066] x5 : ffffa7219d3130e0 x4 : ffffa7219cab9da0 x3 : 0000000000000000
[ 44.662837] x2 : 0000000000000000 x1 : ffffa7219cb7a5c0 x0 : 0000000000000000
[ 44.664675] Call trace:
[ 44.665274] ftrace_set_filter_ip+0x24/0xa0
[ 44.666327] bpf_trampoline_update.constprop.0+0x428/0x4a0
[ 44.667696] __bpf_trampoline_link_prog+0xcc/0x1c0
[ 44.668834] bpf_trampoline_link_prog+0x40/0x64
[ 44.669919] bpf_tracing_prog_attach+0x120/0x490
[ 44.671011] link_create+0xe0/0x2b0
[ 44.671869] __sys_bpf+0x484/0xd30
[ 44.672706] __arm64_sys_bpf+0x30/0x40
[ 44.673678] invoke_syscall+0x78/0x100
[ 44.674623] el0_svc_common.constprop.0+0x4c/0xf4
[ 44.675783] do_el0_svc+0x38/0x4c
[ 44.676624] el0_svc+0x34/0x100
[ 44.677429] el0t_64_sync_handler+0x11c/0x150
[ 44.678532] el0t_64_sync+0x190/0x194
[ 44.679439] Code: 2a0203f4 f90013f5 2a0303f5 f9001fe1 (f9400800)
[ 44.680959] ---[ end trace 0000000000000000 ]---
[ 44.682111] Kernel panic - not syncing: Oops: Fatal exception
[ 44.683488] SMP: stopping secondary CPUs
[ 44.684551] Kernel Offset: 0x2721948e0000 from 0xffff800008000000
[ 44.686095] PHYS_OFFSET: 0xfffff4a380000000
[ 44.687144] CPU features: 0x010,00022811,19001080
[ 44.688308] Memory Limit: none
[ 44.689082] ---[ end Kernel panic - not syncing: Oops: Fatal exception ]---
It's caused by a NULL tr->fops passed to ftrace_set_filter_ip(). tr->fops
is initialized to NULL and is assigned to an allocated memory address if
CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS is enabled. Since there is no
direct call on arm64 yet, the config can't be enabled.
To fix it, call ftrace_set_filter_ip() only if tr->fops is not NULL.
Fixes: 00963a2e75a8 ("bpf: Support bpf_trampoline on functions with IPMODIFY (e.g. livepatch)")
Reported-by: Bruno Goncalves <bgoncalv@redhat.com>
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Tested-by: Bruno Goncalves <bgoncalv@redhat.com>
Acked-by: Song Liu <songliubraving@fb.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20220728114048.3540461-1-xukuohai@huaweicloud.com
2022-07-28 11:40:48 +00:00
|
|
|
}
|
2019-12-09 00:01:13 +00:00
|
|
|
|
2022-07-20 00:21:26 +00:00
|
|
|
if (tr->func.ftrace_managed) {
|
2022-07-29 19:41:06 +00:00
|
|
|
ftrace_set_filter_ip(tr->fops, (unsigned long)ip, 0, 1);
|
2023-03-21 14:04:21 +00:00
|
|
|
ret = register_ftrace_direct(tr->fops, (long)new_addr);
|
2022-07-20 00:21:26 +00:00
|
|
|
} else {
|
2019-12-09 00:01:13 +00:00
|
|
|
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
|
2022-07-20 00:21:26 +00:00
|
|
|
}
|
2021-03-26 10:59:00 +00:00
|
|
|
|
2019-12-09 00:01:13 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-05-10 20:59:19 +00:00
|
|
|
static struct bpf_tramp_links *
|
2021-07-14 09:43:54 +00:00
|
|
|
bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg)
|
2020-03-04 19:18:47 +00:00
|
|
|
{
|
2022-05-10 20:59:19 +00:00
|
|
|
struct bpf_tramp_link *link;
|
|
|
|
struct bpf_tramp_links *tlinks;
|
|
|
|
struct bpf_tramp_link **links;
|
2020-03-04 19:18:47 +00:00
|
|
|
int kind;
|
|
|
|
|
|
|
|
*total = 0;
|
2022-05-10 20:59:19 +00:00
|
|
|
tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL);
|
|
|
|
if (!tlinks)
|
2020-03-04 19:18:47 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
|
2022-05-10 20:59:19 +00:00
|
|
|
tlinks[kind].nr_links = tr->progs_cnt[kind];
|
2020-03-04 19:18:47 +00:00
|
|
|
*total += tr->progs_cnt[kind];
|
2022-05-10 20:59:19 +00:00
|
|
|
links = tlinks[kind].links;
|
2020-03-04 19:18:47 +00:00
|
|
|
|
2022-05-10 20:59:19 +00:00
|
|
|
hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) {
|
|
|
|
*ip_arg |= link->link.prog->call_get_func_ip;
|
|
|
|
*links++ = link;
|
2021-07-14 09:43:54 +00:00
|
|
|
}
|
2020-03-04 19:18:47 +00:00
|
|
|
}
|
2022-05-10 20:59:19 +00:00
|
|
|
return tlinks;
|
2020-03-04 19:18:47 +00:00
|
|
|
}
|
2019-11-14 18:57:04 +00:00
|
|
|
|
2023-05-15 13:08:47 +00:00
|
|
|
static void bpf_tramp_image_free(struct bpf_tramp_image *im)
|
2021-03-16 21:00:07 +00:00
|
|
|
{
|
|
|
|
bpf_image_ksym_del(&im->ksym);
|
2023-12-06 22:40:53 +00:00
|
|
|
arch_free_bpf_trampoline(im->image, im->size);
|
|
|
|
bpf_jit_uncharge_modmem(im->size);
|
2021-03-16 21:00:07 +00:00
|
|
|
percpu_ref_exit(&im->pcref);
|
|
|
|
kfree_rcu(im, rcu);
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:08:47 +00:00
|
|
|
static void __bpf_tramp_image_put_deferred(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct bpf_tramp_image *im;
|
|
|
|
|
|
|
|
im = container_of(work, struct bpf_tramp_image, work);
|
|
|
|
bpf_tramp_image_free(im);
|
|
|
|
}
|
|
|
|
|
2021-03-16 21:00:07 +00:00
|
|
|
/* callback, fexit step 3 or fentry step 2 */
|
|
|
|
static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu)
|
|
|
|
{
|
|
|
|
struct bpf_tramp_image *im;
|
|
|
|
|
|
|
|
im = container_of(rcu, struct bpf_tramp_image, rcu);
|
|
|
|
INIT_WORK(&im->work, __bpf_tramp_image_put_deferred);
|
|
|
|
schedule_work(&im->work);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* callback, fexit step 2. Called after percpu_ref_kill confirms. */
|
|
|
|
static void __bpf_tramp_image_release(struct percpu_ref *pcref)
|
|
|
|
{
|
|
|
|
struct bpf_tramp_image *im;
|
|
|
|
|
|
|
|
im = container_of(pcref, struct bpf_tramp_image, pcref);
|
|
|
|
call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* callback, fexit or fentry step 1 */
|
|
|
|
static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)
|
|
|
|
{
|
|
|
|
struct bpf_tramp_image *im;
|
|
|
|
|
|
|
|
im = container_of(rcu, struct bpf_tramp_image, rcu);
|
|
|
|
if (im->ip_after_call)
|
|
|
|
/* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */
|
|
|
|
percpu_ref_kill(&im->pcref);
|
|
|
|
else
|
|
|
|
/* the case of fentry trampoline */
|
|
|
|
call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bpf_tramp_image_put(struct bpf_tramp_image *im)
|
|
|
|
{
|
|
|
|
/* The trampoline image that calls original function is using:
|
|
|
|
* rcu_read_lock_trace to protect sleepable bpf progs
|
|
|
|
* rcu_read_lock to protect normal bpf progs
|
|
|
|
* percpu_ref to protect trampoline itself
|
|
|
|
* rcu tasks to protect trampoline asm not covered by percpu_ref
|
|
|
|
* (which are few asm insns before __bpf_tramp_enter and
|
|
|
|
* after __bpf_tramp_exit)
|
|
|
|
*
|
|
|
|
* The trampoline is unreachable before bpf_tramp_image_put().
|
|
|
|
*
|
|
|
|
* First, patch the trampoline to avoid calling into fexit progs.
|
|
|
|
* The progs will be freed even if the original function is still
|
|
|
|
* executing or sleeping.
|
|
|
|
* In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on
|
|
|
|
* first few asm instructions to execute and call into
|
|
|
|
* __bpf_tramp_enter->percpu_ref_get.
|
|
|
|
* Then use percpu_ref_kill to wait for the trampoline and the original
|
|
|
|
* function to finish.
|
|
|
|
* Then use call_rcu_tasks() to make sure few asm insns in
|
|
|
|
* the trampoline epilogue are done as well.
|
|
|
|
*
|
|
|
|
* In !PREEMPT case the task that got interrupted in the first asm
|
|
|
|
* insns won't go through an RCU quiescent state which the
|
|
|
|
* percpu_ref_kill will be waiting for. Hence the first
|
|
|
|
* call_rcu_tasks() is not necessary.
|
|
|
|
*/
|
|
|
|
if (im->ip_after_call) {
|
|
|
|
int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_JUMP,
|
|
|
|
NULL, im->ip_epilogue);
|
|
|
|
WARN_ON(err);
|
2024-02-28 19:17:28 +00:00
|
|
|
if (IS_ENABLED(CONFIG_TASKS_RCU))
|
2021-03-16 21:00:07 +00:00
|
|
|
call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
|
|
|
|
else
|
|
|
|
percpu_ref_kill(&im->pcref);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The trampoline without fexit and fmod_ret progs doesn't call original
|
|
|
|
* function and doesn't use percpu_ref.
|
|
|
|
* Use call_rcu_tasks_trace() to wait for sleepable progs to finish.
|
|
|
|
* Then use call_rcu_tasks() to wait for the rest of trampoline asm
|
|
|
|
* and normal progs.
|
|
|
|
*/
|
|
|
|
call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:40:53 +00:00
|
|
|
static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)
|
2021-03-16 21:00:07 +00:00
|
|
|
{
|
|
|
|
struct bpf_tramp_image *im;
|
|
|
|
struct bpf_ksym *ksym;
|
|
|
|
void *image;
|
|
|
|
int err = -ENOMEM;
|
|
|
|
|
|
|
|
im = kzalloc(sizeof(*im), GFP_KERNEL);
|
|
|
|
if (!im)
|
|
|
|
goto out;
|
|
|
|
|
2023-12-06 22:40:53 +00:00
|
|
|
err = bpf_jit_charge_modmem(size);
|
2021-03-16 21:00:07 +00:00
|
|
|
if (err)
|
|
|
|
goto out_free_im;
|
2023-12-06 22:40:53 +00:00
|
|
|
im->size = size;
|
2021-03-16 21:00:07 +00:00
|
|
|
|
|
|
|
err = -ENOMEM;
|
2023-12-06 22:40:53 +00:00
|
|
|
im->image = image = arch_alloc_bpf_trampoline(size);
|
2021-03-16 21:00:07 +00:00
|
|
|
if (!image)
|
|
|
|
goto out_uncharge;
|
|
|
|
|
|
|
|
err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL);
|
|
|
|
if (err)
|
|
|
|
goto out_free_image;
|
|
|
|
|
|
|
|
ksym = &im->ksym;
|
|
|
|
INIT_LIST_HEAD_RCU(&ksym->lnode);
|
2023-05-15 13:08:48 +00:00
|
|
|
snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", key);
|
bpf: Add kernel symbol for struct_ops trampoline
Without kernel symbols for struct_ops trampoline, the unwinder may
produce unexpected stacktraces.
For example, the x86 ORC and FP unwinders check if an IP is in kernel
text by verifying the presence of the IP's kernel symbol. When a
struct_ops trampoline address is encountered, the unwinder stops due
to the absence of symbol, resulting in an incomplete stacktrace that
consists only of direct and indirect child functions called from the
trampoline.
The arm64 unwinder is another example. While the arm64 unwinder can
proceed across a struct_ops trampoline address, the corresponding
symbol name is displayed as "unknown", which is confusing.
Thus, add kernel symbol for struct_ops trampoline. The name is
bpf__<struct_ops_name>_<member_name>, where <struct_ops_name> is the
type name of the struct_ops, and <member_name> is the name of
the member that the trampoline is linked to.
Below is a comparison of stacktraces captured on x86 by perf record,
before and after this patch.
Before:
ffffffff8116545d __lock_acquire+0xad ([kernel.kallsyms])
ffffffff81167fcc lock_acquire+0xcc ([kernel.kallsyms])
ffffffff813088f4 __bpf_prog_enter+0x34 ([kernel.kallsyms])
After:
ffffffff811656bd __lock_acquire+0x30d ([kernel.kallsyms])
ffffffff81167fcc lock_acquire+0xcc ([kernel.kallsyms])
ffffffff81309024 __bpf_prog_enter+0x34 ([kernel.kallsyms])
ffffffffc000d7e9 bpf__tcp_congestion_ops_cong_avoid+0x3e ([kernel.kallsyms])
ffffffff81f250a5 tcp_ack+0x10d5 ([kernel.kallsyms])
ffffffff81f27c66 tcp_rcv_established+0x3b6 ([kernel.kallsyms])
ffffffff81f3ad03 tcp_v4_do_rcv+0x193 ([kernel.kallsyms])
ffffffff81d65a18 __release_sock+0xd8 ([kernel.kallsyms])
ffffffff81d65af4 release_sock+0x34 ([kernel.kallsyms])
ffffffff81f15c4b tcp_sendmsg+0x3b ([kernel.kallsyms])
ffffffff81f663d7 inet_sendmsg+0x47 ([kernel.kallsyms])
ffffffff81d5ab40 sock_write_iter+0x160 ([kernel.kallsyms])
ffffffff8149c67b vfs_write+0x3fb ([kernel.kallsyms])
ffffffff8149caf6 ksys_write+0xc6 ([kernel.kallsyms])
ffffffff8149cb5d __x64_sys_write+0x1d ([kernel.kallsyms])
ffffffff81009200 x64_sys_call+0x1d30 ([kernel.kallsyms])
ffffffff82232d28 do_syscall_64+0x68 ([kernel.kallsyms])
ffffffff8240012f entry_SYSCALL_64_after_hwframe+0x76 ([kernel.kallsyms])
Fixes: 85d33df357b6 ("bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS")
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20241112145849.3436772-4-xukuohai@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-11-12 14:58:49 +00:00
|
|
|
bpf_image_ksym_init(image, size, ksym);
|
|
|
|
bpf_image_ksym_add(ksym);
|
2021-03-16 21:00:07 +00:00
|
|
|
return im;
|
|
|
|
|
|
|
|
out_free_image:
|
2023-12-06 22:40:53 +00:00
|
|
|
arch_free_bpf_trampoline(im->image, im->size);
|
2021-03-16 21:00:07 +00:00
|
|
|
out_uncharge:
|
2023-12-06 22:40:53 +00:00
|
|
|
bpf_jit_uncharge_modmem(size);
|
2021-03-16 21:00:07 +00:00
|
|
|
out_free_im:
|
|
|
|
kfree(im);
|
|
|
|
out:
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
2022-07-20 00:21:26 +00:00
|
|
|
static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex)
|
2019-11-14 18:57:04 +00:00
|
|
|
{
|
2021-03-16 21:00:07 +00:00
|
|
|
struct bpf_tramp_image *im;
|
2022-05-10 20:59:19 +00:00
|
|
|
struct bpf_tramp_links *tlinks;
|
2022-07-20 00:21:26 +00:00
|
|
|
u32 orig_flags = tr->flags;
|
2021-07-14 09:43:54 +00:00
|
|
|
bool ip_arg = false;
|
2023-12-06 22:40:53 +00:00
|
|
|
int err, total, size;
|
2019-11-14 18:57:04 +00:00
|
|
|
|
2022-05-10 20:59:19 +00:00
|
|
|
tlinks = bpf_trampoline_get_progs(tr, &total, &ip_arg);
|
|
|
|
if (IS_ERR(tlinks))
|
|
|
|
return PTR_ERR(tlinks);
|
2020-03-04 19:18:47 +00:00
|
|
|
|
|
|
|
if (total == 0) {
|
2021-03-16 21:00:07 +00:00
|
|
|
err = unregister_fentry(tr, tr->cur_image->image);
|
|
|
|
bpf_tramp_image_put(tr->cur_image);
|
|
|
|
tr->cur_image = NULL;
|
2019-11-14 18:57:04 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-09-12 15:04:41 +00:00
|
|
|
/* clear all bits except SHARE_IPMODIFY and TAIL_CALL_CTX */
|
|
|
|
tr->flags &= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX);
|
2022-07-20 00:21:26 +00:00
|
|
|
|
2022-05-10 20:59:19 +00:00
|
|
|
if (tlinks[BPF_TRAMP_FEXIT].nr_links ||
|
2022-07-20 00:21:26 +00:00
|
|
|
tlinks[BPF_TRAMP_MODIFY_RETURN].nr_links) {
|
2022-07-11 15:08:20 +00:00
|
|
|
/* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME
|
|
|
|
* should not be set together.
|
|
|
|
*/
|
2022-07-20 00:21:26 +00:00
|
|
|
tr->flags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
|
|
|
|
} else {
|
|
|
|
tr->flags |= BPF_TRAMP_F_RESTORE_REGS;
|
|
|
|
}
|
2019-11-14 18:57:04 +00:00
|
|
|
|
2021-07-14 09:43:54 +00:00
|
|
|
if (ip_arg)
|
2022-07-20 00:21:26 +00:00
|
|
|
tr->flags |= BPF_TRAMP_F_IP_ARG;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
|
|
|
|
again:
|
|
|
|
if ((tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY) &&
|
|
|
|
(tr->flags & BPF_TRAMP_F_CALL_ORIG))
|
|
|
|
tr->flags |= BPF_TRAMP_F_ORIG_STACK;
|
|
|
|
#endif
|
2021-07-14 09:43:54 +00:00
|
|
|
|
2023-12-06 22:40:53 +00:00
|
|
|
size = arch_bpf_trampoline_size(&tr->func.model, tr->flags,
|
|
|
|
tlinks, tr->func.addr);
|
|
|
|
if (size < 0) {
|
|
|
|
err = size;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > PAGE_SIZE) {
|
|
|
|
err = -E2BIG;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
im = bpf_tramp_image_alloc(tr->key, size);
|
|
|
|
if (IS_ERR(im)) {
|
|
|
|
err = PTR_ERR(im);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = arch_prepare_bpf_trampoline(im, im->image, im->image + size,
|
2022-07-20 00:21:26 +00:00
|
|
|
&tr->func.model, tr->flags, tlinks,
|
2019-11-14 18:57:04 +00:00
|
|
|
tr->func.addr);
|
bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
The patch introduces BPF_MAP_TYPE_STRUCT_OPS. The map value
is a kernel struct with its func ptr implemented in bpf prog.
This new map is the interface to register/unregister/introspect
a bpf implemented kernel struct.
The kernel struct is actually embedded inside another new struct
(or called the "value" struct in the code). For example,
"struct tcp_congestion_ops" is embbeded in:
struct bpf_struct_ops_tcp_congestion_ops {
refcount_t refcnt;
enum bpf_struct_ops_state state;
struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */
}
The map value is "struct bpf_struct_ops_tcp_congestion_ops".
The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g.
number of tcp_sock in the tcp_congestion_ops case). This "value" struct
is created automatically by a macro. Having a separate "value" struct
will also make extending "struct bpf_struct_ops_XYZ" easier (e.g. adding
"void (*init)(void)" to "struct bpf_struct_ops_XYZ" to do some
initialization works before registering the struct_ops to the kernel
subsystem). The libbpf will take care of finding and populating the
"struct bpf_struct_ops_XYZ" from "struct XYZ".
Register a struct_ops to a kernel subsystem:
1. Load all needed BPF_PROG_TYPE_STRUCT_OPS prog(s)
2. Create a BPF_MAP_TYPE_STRUCT_OPS with attr->btf_vmlinux_value_type_id
set to the btf id "struct bpf_struct_ops_tcp_congestion_ops" of the
running kernel.
Instead of reusing the attr->btf_value_type_id,
btf_vmlinux_value_type_id s added such that attr->btf_fd can still be
used as the "user" btf which could store other useful sysadmin/debug
info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "struct bpf_struct_ops_tcp_congestion_ops" object as described
in the running kernel btf. Populate the value of this object.
The function ptr should be populated with the prog fds.
4. Call BPF_MAP_UPDATE with the object created in (3) as
the map value. The key is always "0".
During BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's
args as an array of u64 is generated. BPF_MAP_UPDATE also allows
the specific struct_ops to do some final checks in "st_ops->init_member()"
(e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct
to the kernel subsystem. The map will not allow further update
from this point.
Unregister a struct_ops from the kernel subsystem:
BPF_MAP_DELETE with key "0".
Introspect a struct_ops:
BPF_MAP_LOOKUP_ELEM with key "0". The map value returned will
have the prog _id_ populated as the func ptr.
The map value state (enum bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)
The kernel subsystem needs to call bpf_struct_ops_get() and
bpf_struct_ops_put() to manage the "refcnt" in the
"struct bpf_struct_ops_XYZ". This patch uses a separate refcnt
for the purose of tracking the subsystem usage. Another approach
is to reuse the map->refcnt and then "show" (i.e. during map_lookup)
the subsystem's usage by doing map->refcnt - map->usercnt to filter out
the map-fd/pinned-map usage. However, that will also tie down the
future semantics of map->refcnt and map->usercnt.
The very first subsystem's refcnt (during reg()) holds one
count to map->refcnt. When the very last subsystem's refcnt
is gone, it will also release the map->refcnt. All bpf_prog will be
freed when the map->refcnt reaches 0 (i.e. during map_free()).
Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6: struct_ops name dctcp flags 0x0
key 4B value 256B max_entries 1 memlock 4096B
btf_id 6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6
[{
"value": {
"refcnt": {
"refs": {
"counter": 1
}
},
"state": 1,
"data": {
"list": {
"next": 0,
"prev": 0
},
"key": 0,
"flags": 2,
"init": 24,
"release": 0,
"ssthresh": 25,
"cong_avoid": 30,
"set_state": 27,
"cwnd_event": 28,
"in_ack_event": 26,
"undo_cwnd": 29,
"pkts_acked": 0,
"min_tso_segs": 0,
"sndbuf_expand": 0,
"cong_control": 0,
"get_info": 0,
"name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0
],
"owner": 0
}
}
}
]
Misc Notes:
* bpf_struct_ops_map_sys_lookup_elem() is added for syscall lookup.
It does an inplace update on "*value" instead returning a pointer
to syscall.c. Otherwise, it needs a separate copy of "zero" value
for the BPF_STRUCT_OPS_STATE_INIT to avoid races.
* The bpf_struct_ops_map_delete_elem() is also called without
preempt_disable() from map_delete_elem(). It is because
the "->unreg()" may requires sleepable context, e.g.
the "tcp_unregister_congestion_control()".
* "const" is added to some of the existing "struct btf_func_model *"
function arg to avoid a compiler warning caused by this patch.
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200109003505.3855919-1-kafai@fb.com
2020-01-09 00:35:05 +00:00
|
|
|
if (err < 0)
|
2023-05-15 13:08:47 +00:00
|
|
|
goto out_free;
|
2019-11-14 18:57:04 +00:00
|
|
|
|
2024-03-16 07:35:41 +00:00
|
|
|
err = arch_protect_bpf_trampoline(im->image, im->size);
|
|
|
|
if (err)
|
|
|
|
goto out_free;
|
2022-09-26 18:47:39 +00:00
|
|
|
|
2023-05-15 13:08:48 +00:00
|
|
|
WARN_ON(tr->cur_image && total == 0);
|
2021-03-16 21:00:07 +00:00
|
|
|
if (tr->cur_image)
|
2019-11-14 18:57:04 +00:00
|
|
|
/* progs already running at this address */
|
2022-07-20 00:21:26 +00:00
|
|
|
err = modify_fentry(tr, tr->cur_image->image, im->image, lock_direct_mutex);
|
2019-11-14 18:57:04 +00:00
|
|
|
else
|
|
|
|
/* first time registering */
|
2021-03-16 21:00:07 +00:00
|
|
|
err = register_fentry(tr, im->image);
|
2022-07-20 00:21:26 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
|
|
|
|
if (err == -EAGAIN) {
|
|
|
|
/* -EAGAIN from bpf_tramp_ftrace_ops_func. Now
|
|
|
|
* BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the
|
|
|
|
* trampoline again, and retry register.
|
|
|
|
*/
|
|
|
|
/* reset fops->func and fops->trampoline for re-register */
|
|
|
|
tr->fops->func = NULL;
|
|
|
|
tr->fops->trampoline = 0;
|
2022-12-24 13:31:46 +00:00
|
|
|
|
2023-12-06 22:40:53 +00:00
|
|
|
/* free im memory and reallocate later */
|
|
|
|
bpf_tramp_image_free(im);
|
2022-07-20 00:21:26 +00:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
#endif
|
2019-11-14 18:57:04 +00:00
|
|
|
if (err)
|
2023-05-15 13:08:47 +00:00
|
|
|
goto out_free;
|
2022-07-20 00:21:26 +00:00
|
|
|
|
2021-03-16 21:00:07 +00:00
|
|
|
if (tr->cur_image)
|
|
|
|
bpf_tramp_image_put(tr->cur_image);
|
|
|
|
tr->cur_image = im;
|
2019-11-14 18:57:04 +00:00
|
|
|
out:
|
2022-07-20 00:21:26 +00:00
|
|
|
/* If any error happens, restore previous flags */
|
|
|
|
if (err)
|
|
|
|
tr->flags = orig_flags;
|
2022-05-10 20:59:19 +00:00
|
|
|
kfree(tlinks);
|
2019-11-14 18:57:04 +00:00
|
|
|
return err;
|
2023-05-15 13:08:47 +00:00
|
|
|
|
|
|
|
out_free:
|
|
|
|
bpf_tramp_image_free(im);
|
|
|
|
goto out;
|
2019-11-14 18:57:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 00:43:52 +00:00
|
|
|
static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
|
2019-11-14 18:57:04 +00:00
|
|
|
{
|
2020-03-29 00:43:52 +00:00
|
|
|
switch (prog->expected_attach_type) {
|
2019-11-14 18:57:04 +00:00
|
|
|
case BPF_TRACE_FENTRY:
|
|
|
|
return BPF_TRAMP_FENTRY;
|
2020-03-04 19:18:49 +00:00
|
|
|
case BPF_MODIFY_RETURN:
|
|
|
|
return BPF_TRAMP_MODIFY_RETURN;
|
2020-01-21 00:53:46 +00:00
|
|
|
case BPF_TRACE_FEXIT:
|
2019-11-14 18:57:04 +00:00
|
|
|
return BPF_TRAMP_FEXIT;
|
2020-03-29 00:43:52 +00:00
|
|
|
case BPF_LSM_MAC:
|
|
|
|
if (!prog->aux->attach_func_proto->type)
|
|
|
|
/* The function returns void, we cannot modify its
|
|
|
|
* return value.
|
|
|
|
*/
|
|
|
|
return BPF_TRAMP_FEXIT;
|
|
|
|
else
|
|
|
|
return BPF_TRAMP_MODIFY_RETURN;
|
2020-01-21 00:53:46 +00:00
|
|
|
default:
|
|
|
|
return BPF_TRAMP_REPLACE;
|
2019-11-14 18:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
static int bpf_freplace_check_tgt_prog(struct bpf_prog *tgt_prog)
|
|
|
|
{
|
|
|
|
struct bpf_prog_aux *aux = tgt_prog->aux;
|
|
|
|
|
|
|
|
guard(mutex)(&aux->ext_mutex);
|
|
|
|
if (aux->prog_array_member_cnt)
|
|
|
|
/* Program extensions can not extend target prog when the target
|
|
|
|
* prog has been updated to any prog_array map as tail callee.
|
|
|
|
* It's to prevent a potential infinite loop like:
|
|
|
|
* tgt prog entry -> tgt prog subprog -> freplace prog entry
|
|
|
|
* --tailcall-> tgt prog entry.
|
|
|
|
*/
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
aux->is_extended = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link,
|
|
|
|
struct bpf_trampoline *tr,
|
|
|
|
struct bpf_prog *tgt_prog)
|
2019-11-14 18:57:04 +00:00
|
|
|
{
|
|
|
|
enum bpf_tramp_prog_type kind;
|
2022-05-10 20:59:19 +00:00
|
|
|
struct bpf_tramp_link *link_exiting;
|
2019-11-14 18:57:04 +00:00
|
|
|
int err = 0;
|
2022-04-30 13:08:03 +00:00
|
|
|
int cnt = 0, i;
|
2019-11-14 18:57:04 +00:00
|
|
|
|
2022-05-10 20:59:19 +00:00
|
|
|
kind = bpf_attach_type_to_tramp(link->link.prog);
|
2022-06-28 17:43:04 +00:00
|
|
|
if (tr->extension_prog)
|
2020-01-21 00:53:46 +00:00
|
|
|
/* cannot attach fentry/fexit if extension prog is attached.
|
|
|
|
* cannot overwrite extension prog either.
|
|
|
|
*/
|
2022-06-28 17:43:04 +00:00
|
|
|
return -EBUSY;
|
2022-04-30 13:08:03 +00:00
|
|
|
|
|
|
|
for (i = 0; i < BPF_TRAMP_MAX; i++)
|
|
|
|
cnt += tr->progs_cnt[i];
|
|
|
|
|
2020-01-21 00:53:46 +00:00
|
|
|
if (kind == BPF_TRAMP_REPLACE) {
|
|
|
|
/* Cannot attach extension if fentry/fexit are in use. */
|
2022-06-28 17:43:04 +00:00
|
|
|
if (cnt)
|
|
|
|
return -EBUSY;
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
err = bpf_freplace_check_tgt_prog(tgt_prog);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2022-05-10 20:59:19 +00:00
|
|
|
tr->extension_prog = link->link.prog;
|
2022-06-28 17:43:04 +00:00
|
|
|
return bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
|
|
|
|
link->link.prog->bpf_func);
|
2019-11-14 18:57:04 +00:00
|
|
|
}
|
2022-06-28 17:43:04 +00:00
|
|
|
if (cnt >= BPF_MAX_TRAMP_LINKS)
|
|
|
|
return -E2BIG;
|
|
|
|
if (!hlist_unhashed(&link->tramp_hlist))
|
2019-11-14 18:57:04 +00:00
|
|
|
/* prog already linked */
|
2022-06-28 17:43:04 +00:00
|
|
|
return -EBUSY;
|
2022-05-10 20:59:19 +00:00
|
|
|
hlist_for_each_entry(link_exiting, &tr->progs_hlist[kind], tramp_hlist) {
|
|
|
|
if (link_exiting->link.prog != link->link.prog)
|
|
|
|
continue;
|
|
|
|
/* prog already linked */
|
2022-06-28 17:43:04 +00:00
|
|
|
return -EBUSY;
|
2022-05-10 20:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hlist_add_head(&link->tramp_hlist, &tr->progs_hlist[kind]);
|
2019-11-14 18:57:04 +00:00
|
|
|
tr->progs_cnt[kind]++;
|
2022-07-20 00:21:26 +00:00
|
|
|
err = bpf_trampoline_update(tr, true /* lock_direct_mutex */);
|
2019-11-14 18:57:04 +00:00
|
|
|
if (err) {
|
2022-05-10 20:59:19 +00:00
|
|
|
hlist_del_init(&link->tramp_hlist);
|
2019-11-14 18:57:04 +00:00
|
|
|
tr->progs_cnt[kind]--;
|
|
|
|
}
|
2022-06-28 17:43:04 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
|
|
|
|
struct bpf_trampoline *tr,
|
|
|
|
struct bpf_prog *tgt_prog)
|
2022-06-28 17:43:04 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mutex_lock(&tr->mutex);
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
err = __bpf_trampoline_link_prog(link, tr, tgt_prog);
|
2019-11-14 18:57:04 +00:00
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
static int __bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
|
|
|
|
struct bpf_trampoline *tr,
|
|
|
|
struct bpf_prog *tgt_prog)
|
2019-11-14 18:57:04 +00:00
|
|
|
{
|
|
|
|
enum bpf_tramp_prog_type kind;
|
|
|
|
int err;
|
|
|
|
|
2022-05-10 20:59:19 +00:00
|
|
|
kind = bpf_attach_type_to_tramp(link->link.prog);
|
2020-01-21 00:53:46 +00:00
|
|
|
if (kind == BPF_TRAMP_REPLACE) {
|
|
|
|
WARN_ON_ONCE(!tr->extension_prog);
|
|
|
|
err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP,
|
|
|
|
tr->extension_prog->bpf_func, NULL);
|
|
|
|
tr->extension_prog = NULL;
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
guard(mutex)(&tgt_prog->aux->ext_mutex);
|
|
|
|
tgt_prog->aux->is_extended = false;
|
2022-06-28 17:43:04 +00:00
|
|
|
return err;
|
2020-01-21 00:53:46 +00:00
|
|
|
}
|
2022-05-10 20:59:19 +00:00
|
|
|
hlist_del_init(&link->tramp_hlist);
|
2019-11-14 18:57:04 +00:00
|
|
|
tr->progs_cnt[kind]--;
|
2022-07-20 00:21:26 +00:00
|
|
|
return bpf_trampoline_update(tr, true /* lock_direct_mutex */);
|
2022-06-28 17:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* bpf_trampoline_unlink_prog() should never fail. */
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
|
|
|
|
struct bpf_trampoline *tr,
|
|
|
|
struct bpf_prog *tgt_prog)
|
2022-06-28 17:43:04 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mutex_lock(&tr->mutex);
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
err = __bpf_trampoline_unlink_prog(link, tr, tgt_prog);
|
2019-11-14 18:57:04 +00:00
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-07-14 18:54:04 +00:00
|
|
|
#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
|
2022-06-28 17:43:06 +00:00
|
|
|
static void bpf_shim_tramp_link_release(struct bpf_link *link)
|
|
|
|
{
|
|
|
|
struct bpf_shim_tramp_link *shim_link =
|
|
|
|
container_of(link, struct bpf_shim_tramp_link, link.link);
|
|
|
|
|
|
|
|
/* paired with 'shim_link->trampoline = tr' in bpf_trampoline_link_cgroup_shim */
|
|
|
|
if (!shim_link->trampoline)
|
|
|
|
return;
|
|
|
|
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
WARN_ON_ONCE(bpf_trampoline_unlink_prog(&shim_link->link, shim_link->trampoline, NULL));
|
2022-06-28 17:43:06 +00:00
|
|
|
bpf_trampoline_put(shim_link->trampoline);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bpf_shim_tramp_link_dealloc(struct bpf_link *link)
|
|
|
|
{
|
|
|
|
struct bpf_shim_tramp_link *shim_link =
|
|
|
|
container_of(link, struct bpf_shim_tramp_link, link.link);
|
|
|
|
|
|
|
|
kfree(shim_link);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct bpf_link_ops bpf_shim_tramp_link_lops = {
|
|
|
|
.release = bpf_shim_tramp_link_release,
|
|
|
|
.dealloc = bpf_shim_tramp_link_dealloc,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct bpf_shim_tramp_link *cgroup_shim_alloc(const struct bpf_prog *prog,
|
|
|
|
bpf_func_t bpf_func,
|
|
|
|
int cgroup_atype)
|
|
|
|
{
|
|
|
|
struct bpf_shim_tramp_link *shim_link = NULL;
|
|
|
|
struct bpf_prog *p;
|
|
|
|
|
|
|
|
shim_link = kzalloc(sizeof(*shim_link), GFP_USER);
|
|
|
|
if (!shim_link)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
p = bpf_prog_alloc(1, 0);
|
|
|
|
if (!p) {
|
|
|
|
kfree(shim_link);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->jited = false;
|
|
|
|
p->bpf_func = bpf_func;
|
|
|
|
|
|
|
|
p->aux->cgroup_atype = cgroup_atype;
|
|
|
|
p->aux->attach_func_proto = prog->aux->attach_func_proto;
|
|
|
|
p->aux->attach_btf_id = prog->aux->attach_btf_id;
|
|
|
|
p->aux->attach_btf = prog->aux->attach_btf;
|
|
|
|
btf_get(p->aux->attach_btf);
|
|
|
|
p->type = BPF_PROG_TYPE_LSM;
|
|
|
|
p->expected_attach_type = BPF_LSM_MAC;
|
|
|
|
bpf_prog_inc(p);
|
|
|
|
bpf_link_init(&shim_link->link.link, BPF_LINK_TYPE_UNSPEC,
|
|
|
|
&bpf_shim_tramp_link_lops, p);
|
2022-06-28 17:43:07 +00:00
|
|
|
bpf_cgroup_atype_get(p->aux->attach_btf_id, cgroup_atype);
|
2022-06-28 17:43:06 +00:00
|
|
|
|
|
|
|
return shim_link;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bpf_shim_tramp_link *cgroup_shim_find(struct bpf_trampoline *tr,
|
|
|
|
bpf_func_t bpf_func)
|
|
|
|
{
|
|
|
|
struct bpf_tramp_link *link;
|
|
|
|
int kind;
|
|
|
|
|
|
|
|
for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
|
|
|
|
hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) {
|
|
|
|
struct bpf_prog *p = link->link.prog;
|
|
|
|
|
|
|
|
if (p->bpf_func == bpf_func)
|
|
|
|
return container_of(link, struct bpf_shim_tramp_link, link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
|
|
|
|
int cgroup_atype)
|
|
|
|
{
|
|
|
|
struct bpf_shim_tramp_link *shim_link = NULL;
|
|
|
|
struct bpf_attach_target_info tgt_info = {};
|
|
|
|
struct bpf_trampoline *tr;
|
|
|
|
bpf_func_t bpf_func;
|
|
|
|
u64 key;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = bpf_check_attach_target(NULL, prog, NULL,
|
|
|
|
prog->aux->attach_btf_id,
|
|
|
|
&tgt_info);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf,
|
|
|
|
prog->aux->attach_btf_id);
|
|
|
|
|
|
|
|
bpf_lsm_find_cgroup_shim(prog, &bpf_func);
|
|
|
|
tr = bpf_trampoline_get(key, &tgt_info);
|
|
|
|
if (!tr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mutex_lock(&tr->mutex);
|
|
|
|
|
|
|
|
shim_link = cgroup_shim_find(tr, bpf_func);
|
|
|
|
if (shim_link) {
|
|
|
|
/* Reusing existing shim attached by the other program. */
|
|
|
|
bpf_link_inc(&shim_link->link.link);
|
|
|
|
|
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
bpf_trampoline_put(tr); /* bpf_trampoline_get above */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and install new shim. */
|
|
|
|
|
|
|
|
shim_link = cgroup_shim_alloc(prog, bpf_func, cgroup_atype);
|
|
|
|
if (!shim_link) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
bpf: Prevent tailcall infinite loop caused by freplace
There is a potential infinite loop issue that can occur when using a
combination of tail calls and freplace.
In an upcoming selftest, the attach target for entry_freplace of
tailcall_freplace.c is subprog_tc of tc_bpf2bpf.c, while the tail call in
entry_freplace leads to entry_tc. This results in an infinite loop:
entry_tc -> subprog_tc -> entry_freplace --tailcall-> entry_tc.
The problem arises because the tail_call_cnt in entry_freplace resets to
zero each time entry_freplace is executed, causing the tail call mechanism
to never terminate, eventually leading to a kernel panic.
To fix this issue, the solution is twofold:
1. Prevent updating a program extended by an freplace program to a
prog_array map.
2. Prevent extending a program that is already part of a prog_array map
with an freplace program.
This ensures that:
* If a program or its subprogram has been extended by an freplace program,
it can no longer be updated to a prog_array map.
* If a program has been added to a prog_array map, neither it nor its
subprograms can be extended by an freplace program.
Moreover, an extension program should not be tailcalled. As such, return
-EINVAL if the program has a type of BPF_PROG_TYPE_EXT when adding it to a
prog_array map.
Additionally, fix a minor code style issue by replacing eight spaces with a
tab for proper formatting.
Reviewed-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/r/20241015150207.70264-2-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2024-10-15 15:02:06 +00:00
|
|
|
err = __bpf_trampoline_link_prog(&shim_link->link, tr, NULL);
|
2022-06-28 17:43:06 +00:00
|
|
|
if (err)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
shim_link->trampoline = tr;
|
|
|
|
/* note, we're still holding tr refcnt from above */
|
|
|
|
|
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
|
|
|
|
if (shim_link)
|
|
|
|
bpf_link_put(&shim_link->link.link);
|
|
|
|
|
|
|
|
/* have to release tr while _not_ holding its mutex */
|
|
|
|
bpf_trampoline_put(tr); /* bpf_trampoline_get above */
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
|
|
|
|
{
|
|
|
|
struct bpf_shim_tramp_link *shim_link = NULL;
|
|
|
|
struct bpf_trampoline *tr;
|
|
|
|
bpf_func_t bpf_func;
|
|
|
|
u64 key;
|
|
|
|
|
|
|
|
key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf,
|
|
|
|
prog->aux->attach_btf_id);
|
|
|
|
|
|
|
|
bpf_lsm_find_cgroup_shim(prog, &bpf_func);
|
|
|
|
tr = bpf_trampoline_lookup(key);
|
|
|
|
if (WARN_ON_ONCE(!tr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
mutex_lock(&tr->mutex);
|
|
|
|
shim_link = cgroup_shim_find(tr, bpf_func);
|
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
|
|
|
|
if (shim_link)
|
|
|
|
bpf_link_put(&shim_link->link.link);
|
|
|
|
|
|
|
|
bpf_trampoline_put(tr); /* bpf_trampoline_lookup above */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-25 21:25:02 +00:00
|
|
|
struct bpf_trampoline *bpf_trampoline_get(u64 key,
|
|
|
|
struct bpf_attach_target_info *tgt_info)
|
|
|
|
{
|
|
|
|
struct bpf_trampoline *tr;
|
|
|
|
|
|
|
|
tr = bpf_trampoline_lookup(key);
|
|
|
|
if (!tr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mutex_lock(&tr->mutex);
|
|
|
|
if (tr->func.addr)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel));
|
|
|
|
tr->func.addr = (void *)tgt_info->tgt_addr;
|
|
|
|
out:
|
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:57:04 +00:00
|
|
|
void bpf_trampoline_put(struct bpf_trampoline *tr)
|
|
|
|
{
|
2022-04-30 13:08:03 +00:00
|
|
|
int i;
|
|
|
|
|
2019-11-14 18:57:04 +00:00
|
|
|
if (!tr)
|
|
|
|
return;
|
|
|
|
mutex_lock(&trampoline_mutex);
|
|
|
|
if (!refcount_dec_and_test(&tr->refcnt))
|
|
|
|
goto out;
|
|
|
|
WARN_ON_ONCE(mutex_is_locked(&tr->mutex));
|
2022-04-30 13:08:03 +00:00
|
|
|
|
|
|
|
for (i = 0; i < BPF_TRAMP_MAX; i++)
|
|
|
|
if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i])))
|
|
|
|
goto out;
|
|
|
|
|
2021-03-16 21:00:07 +00:00
|
|
|
/* This code will be executed even when the last bpf_tramp_image
|
|
|
|
* is alive. All progs are detached from the trampoline and the
|
|
|
|
* trampoline image is patched with jmp into epilogue to skip
|
|
|
|
* fexit progs. The fentry-only trampoline will be freed via
|
|
|
|
* multiple rcu callbacks.
|
2020-08-27 22:01:11 +00:00
|
|
|
*/
|
2019-11-14 18:57:04 +00:00
|
|
|
hlist_del(&tr->hlist);
|
2022-08-02 13:56:51 +00:00
|
|
|
if (tr->fops) {
|
|
|
|
ftrace_free_filter(tr->fops);
|
|
|
|
kfree(tr->fops);
|
|
|
|
}
|
2019-11-14 18:57:04 +00:00
|
|
|
kfree(tr);
|
|
|
|
out:
|
|
|
|
mutex_unlock(&trampoline_mutex);
|
|
|
|
}
|
|
|
|
|
2021-02-10 03:36:29 +00:00
|
|
|
#define NO_START_TIME 1
|
2021-09-10 18:33:51 +00:00
|
|
|
static __always_inline u64 notrace bpf_prog_start_time(void)
|
2021-02-10 03:36:28 +00:00
|
|
|
{
|
|
|
|
u64 start = NO_START_TIME;
|
|
|
|
|
2021-02-10 03:36:29 +00:00
|
|
|
if (static_branch_unlikely(&bpf_stats_enabled_key)) {
|
2021-02-10 03:36:28 +00:00
|
|
|
start = sched_clock();
|
2021-02-10 03:36:29 +00:00
|
|
|
if (unlikely(!start))
|
|
|
|
start = NO_START_TIME;
|
|
|
|
}
|
2021-02-10 03:36:28 +00:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2021-08-15 07:05:54 +00:00
|
|
|
/* The logic is similar to bpf_prog_run(), but with an explicit
|
2020-02-24 14:01:45 +00:00
|
|
|
* rcu_read_lock() and migrate_disable() which are required
|
|
|
|
* for the trampoline. The macro is split into
|
2021-02-10 03:36:28 +00:00
|
|
|
* call __bpf_prog_enter
|
2019-11-14 18:57:04 +00:00
|
|
|
* call prog->bpf_func
|
|
|
|
* call __bpf_prog_exit
|
2021-02-10 03:36:29 +00:00
|
|
|
*
|
|
|
|
* __bpf_prog_enter returns:
|
|
|
|
* 0 - skip execution of the bpf prog
|
|
|
|
* 1 - execute bpf prog
|
2021-05-25 02:56:59 +00:00
|
|
|
* [2..MAX_U64] - execute bpf prog and record execution time.
|
2021-02-10 03:36:29 +00:00
|
|
|
* This is start time.
|
2019-11-14 18:57:04 +00:00
|
|
|
*/
|
2022-10-25 18:45:16 +00:00
|
|
|
static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx)
|
2020-03-11 01:09:01 +00:00
|
|
|
__acquires(RCU)
|
2019-11-14 18:57:04 +00:00
|
|
|
{
|
|
|
|
rcu_read_lock();
|
2020-02-24 14:01:45 +00:00
|
|
|
migrate_disable();
|
2022-05-10 20:59:20 +00:00
|
|
|
|
|
|
|
run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
|
|
|
|
|
2022-09-01 06:19:36 +00:00
|
|
|
if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
|
2022-09-16 07:19:14 +00:00
|
|
|
bpf_prog_inc_misses_counter(prog);
|
2024-11-12 16:39:33 +00:00
|
|
|
if (prog->aux->recursion_detected)
|
|
|
|
prog->aux->recursion_detected(prog);
|
2021-02-10 03:36:29 +00:00
|
|
|
return 0;
|
2021-02-10 03:36:31 +00:00
|
|
|
}
|
2021-02-10 03:36:28 +00:00
|
|
|
return bpf_prog_start_time();
|
2019-11-14 18:57:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 03:36:28 +00:00
|
|
|
static void notrace update_prog_stats(struct bpf_prog *prog,
|
|
|
|
u64 start)
|
2019-11-14 18:57:04 +00:00
|
|
|
{
|
|
|
|
struct bpf_prog_stats *stats;
|
|
|
|
|
|
|
|
if (static_branch_unlikely(&bpf_stats_enabled_key) &&
|
2021-02-10 03:36:28 +00:00
|
|
|
/* static_key could be enabled in __bpf_prog_enter*
|
|
|
|
* and disabled in __bpf_prog_exit*.
|
2019-11-14 18:57:04 +00:00
|
|
|
* And vice versa.
|
2021-02-10 03:36:28 +00:00
|
|
|
* Hence check that 'start' is valid.
|
2019-11-14 18:57:04 +00:00
|
|
|
*/
|
2021-02-10 03:36:28 +00:00
|
|
|
start > NO_START_TIME) {
|
2024-04-02 03:40:10 +00:00
|
|
|
u64 duration = sched_clock() - start;
|
2021-10-26 21:41:32 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
2021-02-10 03:36:26 +00:00
|
|
|
stats = this_cpu_ptr(prog->stats);
|
2021-10-26 21:41:32 +00:00
|
|
|
flags = u64_stats_update_begin_irqsave(&stats->syncp);
|
2021-10-26 21:41:33 +00:00
|
|
|
u64_stats_inc(&stats->cnt);
|
2024-04-02 03:40:10 +00:00
|
|
|
u64_stats_add(&stats->nsecs, duration);
|
2021-10-26 21:41:32 +00:00
|
|
|
u64_stats_update_end_irqrestore(&stats->syncp, flags);
|
2019-11-14 18:57:04 +00:00
|
|
|
}
|
2021-02-10 03:36:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:16 +00:00
|
|
|
static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
2021-02-10 03:36:28 +00:00
|
|
|
__releases(RCU)
|
|
|
|
{
|
2022-05-10 20:59:20 +00:00
|
|
|
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
|
|
|
|
|
2021-02-10 03:36:28 +00:00
|
|
|
update_prog_stats(prog, start);
|
2022-09-01 06:19:36 +00:00
|
|
|
this_cpu_dec(*(prog->active));
|
2020-02-24 14:01:45 +00:00
|
|
|
migrate_enable();
|
2019-11-14 18:57:04 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:16 +00:00
|
|
|
static u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
2022-06-28 17:43:06 +00:00
|
|
|
__acquires(RCU)
|
|
|
|
{
|
|
|
|
/* Runtime stats are exported via actual BPF_LSM_CGROUP
|
|
|
|
* programs, not the shims.
|
|
|
|
*/
|
|
|
|
rcu_read_lock();
|
|
|
|
migrate_disable();
|
|
|
|
|
|
|
|
run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
|
|
|
|
|
|
|
|
return NO_START_TIME;
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:16 +00:00
|
|
|
static void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
2022-06-28 17:43:06 +00:00
|
|
|
__releases(RCU)
|
|
|
|
{
|
|
|
|
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
|
|
|
|
|
|
|
|
migrate_enable();
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:16 +00:00
|
|
|
u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
2020-08-27 22:01:11 +00:00
|
|
|
{
|
|
|
|
rcu_read_lock_trace();
|
2021-02-10 03:36:27 +00:00
|
|
|
migrate_disable();
|
2020-08-31 20:16:51 +00:00
|
|
|
might_fault();
|
2022-05-10 20:59:20 +00:00
|
|
|
|
2023-08-30 08:04:05 +00:00
|
|
|
run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
|
|
|
|
|
2022-09-01 06:19:36 +00:00
|
|
|
if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
|
2022-09-16 07:19:14 +00:00
|
|
|
bpf_prog_inc_misses_counter(prog);
|
2024-11-12 16:39:33 +00:00
|
|
|
if (prog->aux->recursion_detected)
|
|
|
|
prog->aux->recursion_detected(prog);
|
2021-02-10 03:36:29 +00:00
|
|
|
return 0;
|
2021-02-10 03:36:31 +00:00
|
|
|
}
|
2021-02-10 03:36:28 +00:00
|
|
|
return bpf_prog_start_time();
|
2020-08-27 22:01:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:16 +00:00
|
|
|
void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
2020-08-27 22:01:11 +00:00
|
|
|
{
|
2022-05-10 20:59:20 +00:00
|
|
|
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
|
|
|
|
|
2021-02-10 03:36:28 +00:00
|
|
|
update_prog_stats(prog, start);
|
2022-09-01 06:19:36 +00:00
|
|
|
this_cpu_dec(*(prog->active));
|
2021-02-10 03:36:27 +00:00
|
|
|
migrate_enable();
|
2020-08-27 22:01:11 +00:00
|
|
|
rcu_read_unlock_trace();
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:16 +00:00
|
|
|
static u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
|
|
|
{
|
|
|
|
rcu_read_lock_trace();
|
|
|
|
migrate_disable();
|
|
|
|
might_fault();
|
|
|
|
|
|
|
|
run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
|
|
|
|
|
|
|
|
return bpf_prog_start_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
|
|
|
{
|
|
|
|
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
|
|
|
|
|
|
|
|
update_prog_stats(prog, start);
|
|
|
|
migrate_enable();
|
|
|
|
rcu_read_unlock_trace();
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 notrace __bpf_prog_enter(struct bpf_prog *prog,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
2022-09-29 07:04:03 +00:00
|
|
|
__acquires(RCU)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
|
|
|
migrate_disable();
|
|
|
|
|
|
|
|
run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
|
|
|
|
|
|
|
|
return bpf_prog_start_time();
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:16 +00:00
|
|
|
static void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
2022-09-29 07:04:03 +00:00
|
|
|
__releases(RCU)
|
|
|
|
{
|
|
|
|
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
|
|
|
|
|
|
|
|
update_prog_stats(prog, start);
|
|
|
|
migrate_enable();
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2021-03-16 21:00:07 +00:00
|
|
|
void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr)
|
|
|
|
{
|
|
|
|
percpu_ref_get(&tr->pcref);
|
|
|
|
}
|
|
|
|
|
|
|
|
void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr)
|
|
|
|
{
|
|
|
|
percpu_ref_put(&tr->pcref);
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:45:16 +00:00
|
|
|
bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog)
|
|
|
|
{
|
2024-03-09 00:47:39 +00:00
|
|
|
bool sleepable = prog->sleepable;
|
2022-10-25 18:45:16 +00:00
|
|
|
|
|
|
|
if (bpf_prog_check_recur(prog))
|
|
|
|
return sleepable ? __bpf_prog_enter_sleepable_recur :
|
|
|
|
__bpf_prog_enter_recur;
|
|
|
|
|
|
|
|
if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM &&
|
|
|
|
prog->expected_attach_type == BPF_LSM_CGROUP)
|
|
|
|
return __bpf_prog_enter_lsm_cgroup;
|
|
|
|
|
|
|
|
return sleepable ? __bpf_prog_enter_sleepable : __bpf_prog_enter;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog)
|
|
|
|
{
|
2024-03-09 00:47:39 +00:00
|
|
|
bool sleepable = prog->sleepable;
|
2022-10-25 18:45:16 +00:00
|
|
|
|
|
|
|
if (bpf_prog_check_recur(prog))
|
|
|
|
return sleepable ? __bpf_prog_exit_sleepable_recur :
|
|
|
|
__bpf_prog_exit_recur;
|
|
|
|
|
|
|
|
if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM &&
|
|
|
|
prog->expected_attach_type == BPF_LSM_CGROUP)
|
|
|
|
return __bpf_prog_exit_lsm_cgroup;
|
|
|
|
|
|
|
|
return sleepable ? __bpf_prog_exit_sleepable : __bpf_prog_exit;
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:57:04 +00:00
|
|
|
int __weak
|
2023-12-06 22:40:49 +00:00
|
|
|
arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,
|
bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS
The patch introduces BPF_MAP_TYPE_STRUCT_OPS. The map value
is a kernel struct with its func ptr implemented in bpf prog.
This new map is the interface to register/unregister/introspect
a bpf implemented kernel struct.
The kernel struct is actually embedded inside another new struct
(or called the "value" struct in the code). For example,
"struct tcp_congestion_ops" is embbeded in:
struct bpf_struct_ops_tcp_congestion_ops {
refcount_t refcnt;
enum bpf_struct_ops_state state;
struct tcp_congestion_ops data; /* <-- kernel subsystem struct here */
}
The map value is "struct bpf_struct_ops_tcp_congestion_ops".
The "bpftool map dump" will then be able to show the
state ("inuse"/"tobefree") and the number of subsystem's refcnt (e.g.
number of tcp_sock in the tcp_congestion_ops case). This "value" struct
is created automatically by a macro. Having a separate "value" struct
will also make extending "struct bpf_struct_ops_XYZ" easier (e.g. adding
"void (*init)(void)" to "struct bpf_struct_ops_XYZ" to do some
initialization works before registering the struct_ops to the kernel
subsystem). The libbpf will take care of finding and populating the
"struct bpf_struct_ops_XYZ" from "struct XYZ".
Register a struct_ops to a kernel subsystem:
1. Load all needed BPF_PROG_TYPE_STRUCT_OPS prog(s)
2. Create a BPF_MAP_TYPE_STRUCT_OPS with attr->btf_vmlinux_value_type_id
set to the btf id "struct bpf_struct_ops_tcp_congestion_ops" of the
running kernel.
Instead of reusing the attr->btf_value_type_id,
btf_vmlinux_value_type_id s added such that attr->btf_fd can still be
used as the "user" btf which could store other useful sysadmin/debug
info that may be introduced in the furture,
e.g. creation-date/compiler-details/map-creator...etc.
3. Create a "struct bpf_struct_ops_tcp_congestion_ops" object as described
in the running kernel btf. Populate the value of this object.
The function ptr should be populated with the prog fds.
4. Call BPF_MAP_UPDATE with the object created in (3) as
the map value. The key is always "0".
During BPF_MAP_UPDATE, the code that saves the kernel-func-ptr's
args as an array of u64 is generated. BPF_MAP_UPDATE also allows
the specific struct_ops to do some final checks in "st_ops->init_member()"
(e.g. ensure all mandatory func ptrs are implemented).
If everything looks good, it will register this kernel struct
to the kernel subsystem. The map will not allow further update
from this point.
Unregister a struct_ops from the kernel subsystem:
BPF_MAP_DELETE with key "0".
Introspect a struct_ops:
BPF_MAP_LOOKUP_ELEM with key "0". The map value returned will
have the prog _id_ populated as the func ptr.
The map value state (enum bpf_struct_ops_state) will transit from:
INIT (map created) =>
INUSE (map updated, i.e. reg) =>
TOBEFREE (map value deleted, i.e. unreg)
The kernel subsystem needs to call bpf_struct_ops_get() and
bpf_struct_ops_put() to manage the "refcnt" in the
"struct bpf_struct_ops_XYZ". This patch uses a separate refcnt
for the purose of tracking the subsystem usage. Another approach
is to reuse the map->refcnt and then "show" (i.e. during map_lookup)
the subsystem's usage by doing map->refcnt - map->usercnt to filter out
the map-fd/pinned-map usage. However, that will also tie down the
future semantics of map->refcnt and map->usercnt.
The very first subsystem's refcnt (during reg()) holds one
count to map->refcnt. When the very last subsystem's refcnt
is gone, it will also release the map->refcnt. All bpf_prog will be
freed when the map->refcnt reaches 0 (i.e. during map_free()).
Here is how the bpftool map command will look like:
[root@arch-fb-vm1 bpf]# bpftool map show
6: struct_ops name dctcp flags 0x0
key 4B value 256B max_entries 1 memlock 4096B
btf_id 6
[root@arch-fb-vm1 bpf]# bpftool map dump id 6
[{
"value": {
"refcnt": {
"refs": {
"counter": 1
}
},
"state": 1,
"data": {
"list": {
"next": 0,
"prev": 0
},
"key": 0,
"flags": 2,
"init": 24,
"release": 0,
"ssthresh": 25,
"cong_avoid": 30,
"set_state": 27,
"cwnd_event": 28,
"in_ack_event": 26,
"undo_cwnd": 29,
"pkts_acked": 0,
"min_tso_segs": 0,
"sndbuf_expand": 0,
"cong_control": 0,
"get_info": 0,
"name": [98,112,102,95,100,99,116,99,112,0,0,0,0,0,0,0
],
"owner": 0
}
}
}
]
Misc Notes:
* bpf_struct_ops_map_sys_lookup_elem() is added for syscall lookup.
It does an inplace update on "*value" instead returning a pointer
to syscall.c. Otherwise, it needs a separate copy of "zero" value
for the BPF_STRUCT_OPS_STATE_INIT to avoid races.
* The bpf_struct_ops_map_delete_elem() is also called without
preempt_disable() from map_delete_elem(). It is because
the "->unreg()" may requires sleepable context, e.g.
the "tcp_unregister_congestion_control()".
* "const" is added to some of the existing "struct btf_func_model *"
function arg to avoid a compiler warning caused by this patch.
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200109003505.3855919-1-kafai@fb.com
2020-01-09 00:35:05 +00:00
|
|
|
const struct btf_func_model *m, u32 flags,
|
2022-05-10 20:59:19 +00:00
|
|
|
struct bpf_tramp_links *tlinks,
|
2023-12-06 22:40:49 +00:00
|
|
|
void *func_addr)
|
2019-11-14 18:57:04 +00:00
|
|
|
{
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:40:50 +00:00
|
|
|
void * __weak arch_alloc_bpf_trampoline(unsigned int size)
|
|
|
|
{
|
|
|
|
void *image;
|
|
|
|
|
|
|
|
if (WARN_ON_ONCE(size > PAGE_SIZE))
|
|
|
|
return NULL;
|
|
|
|
image = bpf_jit_alloc_exec(PAGE_SIZE);
|
|
|
|
if (image)
|
|
|
|
set_vm_flush_reset_perms(image);
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __weak arch_free_bpf_trampoline(void *image, unsigned int size)
|
|
|
|
{
|
|
|
|
WARN_ON_ONCE(size > PAGE_SIZE);
|
|
|
|
/* bpf_jit_free_exec doesn't need "size", but
|
|
|
|
* bpf_prog_pack_free() needs it.
|
|
|
|
*/
|
|
|
|
bpf_jit_free_exec(image);
|
|
|
|
}
|
|
|
|
|
2024-03-16 07:35:41 +00:00
|
|
|
int __weak arch_protect_bpf_trampoline(void *image, unsigned int size)
|
2023-12-06 22:40:50 +00:00
|
|
|
{
|
|
|
|
WARN_ON_ONCE(size > PAGE_SIZE);
|
2024-03-16 07:35:41 +00:00
|
|
|
return set_memory_rox((long)image, 1);
|
2023-12-06 22:40:50 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 22:40:52 +00:00
|
|
|
int __weak arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
|
|
|
|
struct bpf_tramp_links *tlinks, void *func_addr)
|
|
|
|
{
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:57:04 +00:00
|
|
|
static int __init init_trampolines(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
|
|
|
|
INIT_HLIST_HEAD(&trampoline_table[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
late_initcall(init_trampolines);
|