2019-11-15 02:57:04 +08: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 08:01:13 +08:00
|
|
|
#include <linux/ftrace.h>
|
2020-01-24 00:15:07 +08:00
|
|
|
#include <linux/rbtree_latch.h>
|
2020-03-13 03:56:05 +08:00
|
|
|
#include <linux/perf_event.h>
|
2020-03-29 08:43:52 +08:00
|
|
|
#include <linux/btf.h>
|
2020-08-28 06:01:11 +08:00
|
|
|
#include <linux/rcupdate_trace.h>
|
|
|
|
#include <linux/rcupdate_wait.h>
|
2021-03-26 18:59:00 +08:00
|
|
|
#include <linux/module.h>
|
2021-09-11 02:33:51 +08:00
|
|
|
#include <linux/static_call.h>
|
2022-06-29 01:43:06 +08:00
|
|
|
#include <linux/bpf_verifier.h>
|
|
|
|
#include <linux/bpf_lsm.h>
|
2022-07-20 08:21:26 +08:00
|
|
|
#include <linux/delay.h>
|
2019-11-15 02:57:04 +08:00
|
|
|
|
2020-01-21 08:53:46 +08: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-15 02:57:04 +08: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-13 03:56:07 +08:00
|
|
|
/* serializes access to trampoline_table */
|
2019-11-15 02:57:04 +08:00
|
|
|
static DEFINE_MUTEX(trampoline_mutex);
|
|
|
|
|
2022-07-20 08:21:26 +08: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
|
|
|
|
* -EAGAIN to register_ftrace_direct_multi(). Then we can
|
|
|
|
* retry register_ftrace_direct_multi() after updating the
|
|
|
|
* 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-26 06:27:33 +08:00
|
|
|
}
|
2022-07-20 08:21:26 +08:00
|
|
|
|
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-09 03:32:44 +08:00
|
|
|
bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
|
|
|
|
{
|
|
|
|
enum bpf_attach_type eatype = prog->expected_attach_type;
|
2022-05-11 04:59:21 +08:00
|
|
|
enum bpf_prog_type ptype = prog->type;
|
2021-12-09 03:32:44 +08:00
|
|
|
|
2022-05-11 04:59:21 +08: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-09 03:32:44 +08:00
|
|
|
}
|
|
|
|
|
2020-03-13 03:56:07 +08:00
|
|
|
void *bpf_jit_alloc_exec_page(void)
|
2019-12-14 01:51:07 +08:00
|
|
|
{
|
|
|
|
void *image;
|
|
|
|
|
|
|
|
image = bpf_jit_alloc_exec(PAGE_SIZE);
|
|
|
|
if (!image)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
set_vm_flush_reset_perms(image);
|
|
|
|
/* Keep image as writeable. The alternative is to keep flipping ro/rw
|
2022-02-21 02:40:55 +08:00
|
|
|
* every time new program is attached or detached.
|
2019-12-14 01:51:07 +08:00
|
|
|
*/
|
|
|
|
set_memory_x((long)image, 1);
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2020-03-13 03:56:05 +08:00
|
|
|
void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
|
|
|
|
{
|
|
|
|
ksym->start = (unsigned long) data;
|
2020-03-13 03:56:07 +08:00
|
|
|
ksym->end = ksym->start + PAGE_SIZE;
|
2020-03-13 03:56:05 +08:00
|
|
|
bpf_ksym_add(ksym);
|
|
|
|
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
|
2020-03-13 03:56:07 +08:00
|
|
|
PAGE_SIZE, false, ksym->name);
|
2020-03-13 03:56:05 +08: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-13 03:56:07 +08:00
|
|
|
PAGE_SIZE, true, ksym->name);
|
2020-03-13 03:56:05 +08:00
|
|
|
}
|
|
|
|
|
2020-09-26 05:25:02 +08:00
|
|
|
static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
|
2019-11-15 02:57:04 +08: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 08:21:26 +08: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-15 02:57:04 +08: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;
|
|
|
|
}
|
|
|
|
|
2021-03-26 18:59:00 +08:00
|
|
|
static int bpf_trampoline_module_get(struct bpf_trampoline *tr)
|
|
|
|
{
|
|
|
|
struct module *mod;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
preempt_disable();
|
|
|
|
mod = __module_text_address((unsigned long) tr->func.addr);
|
|
|
|
if (mod && !try_module_get(mod))
|
|
|
|
err = -ENOENT;
|
|
|
|
preempt_enable();
|
|
|
|
tr->mod = mod;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bpf_trampoline_module_put(struct bpf_trampoline *tr)
|
|
|
|
{
|
|
|
|
module_put(tr->mod);
|
|
|
|
tr->mod = NULL;
|
|
|
|
}
|
|
|
|
|
2019-12-09 08:01:13 +08:00
|
|
|
static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
|
|
|
|
{
|
|
|
|
void *ip = tr->func.addr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (tr->func.ftrace_managed)
|
2022-07-20 08:21:26 +08:00
|
|
|
ret = unregister_ftrace_direct_multi(tr->fops, (long)old_addr);
|
2019-12-09 08:01:13 +08:00
|
|
|
else
|
|
|
|
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
|
2021-03-26 18:59:00 +08:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
bpf_trampoline_module_put(tr);
|
2019-12-09 08:01:13 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:21:26 +08:00
|
|
|
static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr,
|
|
|
|
bool lock_direct_mutex)
|
2019-12-09 08:01:13 +08:00
|
|
|
{
|
|
|
|
void *ip = tr->func.addr;
|
|
|
|
int ret;
|
|
|
|
|
2022-07-20 08:21:26 +08:00
|
|
|
if (tr->func.ftrace_managed) {
|
|
|
|
if (lock_direct_mutex)
|
|
|
|
ret = modify_ftrace_direct_multi(tr->fops, (long)new_addr);
|
|
|
|
else
|
|
|
|
ret = modify_ftrace_direct_multi_nolock(tr->fops, (long)new_addr);
|
|
|
|
} else {
|
2019-12-09 08:01:13 +08:00
|
|
|
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr);
|
2022-07-20 08:21:26 +08:00
|
|
|
}
|
2019-12-09 08:01:13 +08: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 23:30:29 +08:00
|
|
|
unsigned long faddr;
|
2019-12-09 08:01:13 +08:00
|
|
|
int ret;
|
|
|
|
|
2022-03-08 23:30:29 +08: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 19:40:48 +08:00
|
|
|
if (faddr) {
|
|
|
|
if (!tr->fops)
|
|
|
|
return -ENOTSUPP;
|
2022-03-08 23:30:29 +08: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 19:40:48 +08:00
|
|
|
}
|
2019-12-09 08:01:13 +08:00
|
|
|
|
2021-03-26 18:59:00 +08:00
|
|
|
if (bpf_trampoline_module_get(tr))
|
|
|
|
return -ENOENT;
|
|
|
|
|
2022-07-20 08:21:26 +08:00
|
|
|
if (tr->func.ftrace_managed) {
|
2022-07-30 03:41:06 +08:00
|
|
|
ftrace_set_filter_ip(tr->fops, (unsigned long)ip, 0, 1);
|
2022-07-20 08:21:26 +08:00
|
|
|
ret = register_ftrace_direct_multi(tr->fops, (long)new_addr);
|
|
|
|
} else {
|
2019-12-09 08:01:13 +08:00
|
|
|
ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
|
2022-07-20 08:21:26 +08:00
|
|
|
}
|
2021-03-26 18:59:00 +08:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
bpf_trampoline_module_put(tr);
|
2019-12-09 08:01:13 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-05-11 04:59:19 +08:00
|
|
|
static struct bpf_tramp_links *
|
2021-07-14 17:43:54 +08:00
|
|
|
bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg)
|
2020-03-05 03:18:47 +08:00
|
|
|
{
|
2022-05-11 04:59:19 +08:00
|
|
|
struct bpf_tramp_link *link;
|
|
|
|
struct bpf_tramp_links *tlinks;
|
|
|
|
struct bpf_tramp_link **links;
|
2020-03-05 03:18:47 +08:00
|
|
|
int kind;
|
|
|
|
|
|
|
|
*total = 0;
|
2022-05-11 04:59:19 +08:00
|
|
|
tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL);
|
|
|
|
if (!tlinks)
|
2020-03-05 03:18:47 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
|
2022-05-11 04:59:19 +08:00
|
|
|
tlinks[kind].nr_links = tr->progs_cnt[kind];
|
2020-03-05 03:18:47 +08:00
|
|
|
*total += tr->progs_cnt[kind];
|
2022-05-11 04:59:19 +08:00
|
|
|
links = tlinks[kind].links;
|
2020-03-05 03:18:47 +08:00
|
|
|
|
2022-05-11 04:59:19 +08: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 17:43:54 +08:00
|
|
|
}
|
2020-03-05 03:18:47 +08:00
|
|
|
}
|
2022-05-11 04:59:19 +08:00
|
|
|
return tlinks;
|
2020-03-05 03:18:47 +08:00
|
|
|
}
|
2019-11-15 02:57:04 +08:00
|
|
|
|
2021-03-17 05:00:07 +08: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_image_ksym_del(&im->ksym);
|
|
|
|
bpf_jit_free_exec(im->image);
|
2022-02-05 02:57:35 +08:00
|
|
|
bpf_jit_uncharge_modmem(PAGE_SIZE);
|
2021-03-17 05:00:07 +08:00
|
|
|
percpu_ref_exit(&im->pcref);
|
|
|
|
kfree_rcu(im, rcu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
if (IS_ENABLED(CONFIG_PREEMPTION))
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx)
|
|
|
|
{
|
|
|
|
struct bpf_tramp_image *im;
|
|
|
|
struct bpf_ksym *ksym;
|
|
|
|
void *image;
|
|
|
|
int err = -ENOMEM;
|
|
|
|
|
|
|
|
im = kzalloc(sizeof(*im), GFP_KERNEL);
|
|
|
|
if (!im)
|
|
|
|
goto out;
|
|
|
|
|
2022-02-05 02:57:35 +08:00
|
|
|
err = bpf_jit_charge_modmem(PAGE_SIZE);
|
2021-03-17 05:00:07 +08:00
|
|
|
if (err)
|
|
|
|
goto out_free_im;
|
|
|
|
|
|
|
|
err = -ENOMEM;
|
|
|
|
im->image = image = bpf_jit_alloc_exec_page();
|
|
|
|
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);
|
|
|
|
snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu_%u", key, idx);
|
|
|
|
bpf_image_ksym_add(image, ksym);
|
|
|
|
return im;
|
|
|
|
|
|
|
|
out_free_image:
|
|
|
|
bpf_jit_free_exec(im->image);
|
|
|
|
out_uncharge:
|
2022-02-05 02:57:35 +08:00
|
|
|
bpf_jit_uncharge_modmem(PAGE_SIZE);
|
2021-03-17 05:00:07 +08:00
|
|
|
out_free_im:
|
|
|
|
kfree(im);
|
|
|
|
out:
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:21:26 +08:00
|
|
|
static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex)
|
2019-11-15 02:57:04 +08:00
|
|
|
{
|
2021-03-17 05:00:07 +08:00
|
|
|
struct bpf_tramp_image *im;
|
2022-05-11 04:59:19 +08:00
|
|
|
struct bpf_tramp_links *tlinks;
|
2022-07-20 08:21:26 +08:00
|
|
|
u32 orig_flags = tr->flags;
|
2021-07-14 17:43:54 +08:00
|
|
|
bool ip_arg = false;
|
2020-03-05 03:18:47 +08:00
|
|
|
int err, total;
|
2019-11-15 02:57:04 +08:00
|
|
|
|
2022-05-11 04:59:19 +08:00
|
|
|
tlinks = bpf_trampoline_get_progs(tr, &total, &ip_arg);
|
|
|
|
if (IS_ERR(tlinks))
|
|
|
|
return PTR_ERR(tlinks);
|
2020-03-05 03:18:47 +08:00
|
|
|
|
|
|
|
if (total == 0) {
|
2021-03-17 05:00:07 +08:00
|
|
|
err = unregister_fentry(tr, tr->cur_image->image);
|
|
|
|
bpf_tramp_image_put(tr->cur_image);
|
|
|
|
tr->cur_image = NULL;
|
2019-11-15 02:57:04 +08:00
|
|
|
tr->selector = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:00:07 +08:00
|
|
|
im = bpf_tramp_image_alloc(tr->key, tr->selector);
|
|
|
|
if (IS_ERR(im)) {
|
|
|
|
err = PTR_ERR(im);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:21:26 +08:00
|
|
|
/* clear all bits except SHARE_IPMODIFY */
|
|
|
|
tr->flags &= BPF_TRAMP_F_SHARE_IPMODIFY;
|
|
|
|
|
2022-05-11 04:59:19 +08:00
|
|
|
if (tlinks[BPF_TRAMP_FEXIT].nr_links ||
|
2022-07-20 08:21:26 +08:00
|
|
|
tlinks[BPF_TRAMP_MODIFY_RETURN].nr_links) {
|
2022-07-11 23:08:20 +08:00
|
|
|
/* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME
|
|
|
|
* should not be set together.
|
|
|
|
*/
|
2022-07-20 08:21:26 +08:00
|
|
|
tr->flags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
|
|
|
|
} else {
|
|
|
|
tr->flags |= BPF_TRAMP_F_RESTORE_REGS;
|
|
|
|
}
|
2019-11-15 02:57:04 +08:00
|
|
|
|
2021-07-14 17:43:54 +08:00
|
|
|
if (ip_arg)
|
2022-07-20 08:21:26 +08: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 17:43:54 +08:00
|
|
|
|
2021-03-17 05:00:07 +08:00
|
|
|
err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
|
2022-07-20 08:21:26 +08:00
|
|
|
&tr->func.model, tr->flags, tlinks,
|
2019-11-15 02:57:04 +08: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 08:35:05 +08:00
|
|
|
if (err < 0)
|
2019-11-15 02:57:04 +08:00
|
|
|
goto out;
|
|
|
|
|
2021-03-17 05:00:07 +08:00
|
|
|
WARN_ON(tr->cur_image && tr->selector == 0);
|
|
|
|
WARN_ON(!tr->cur_image && tr->selector);
|
|
|
|
if (tr->cur_image)
|
2019-11-15 02:57:04 +08:00
|
|
|
/* progs already running at this address */
|
2022-07-20 08:21:26 +08:00
|
|
|
err = modify_fentry(tr, tr->cur_image->image, im->image, lock_direct_mutex);
|
2019-11-15 02:57:04 +08:00
|
|
|
else
|
|
|
|
/* first time registering */
|
2021-03-17 05:00:07 +08:00
|
|
|
err = register_fentry(tr, im->image);
|
2022-07-20 08:21:26 +08: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;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
#endif
|
2019-11-15 02:57:04 +08:00
|
|
|
if (err)
|
|
|
|
goto out;
|
2022-07-20 08:21:26 +08:00
|
|
|
|
2021-03-17 05:00:07 +08:00
|
|
|
if (tr->cur_image)
|
|
|
|
bpf_tramp_image_put(tr->cur_image);
|
|
|
|
tr->cur_image = im;
|
2019-11-15 02:57:04 +08:00
|
|
|
tr->selector++;
|
|
|
|
out:
|
2022-07-20 08:21:26 +08:00
|
|
|
/* If any error happens, restore previous flags */
|
|
|
|
if (err)
|
|
|
|
tr->flags = orig_flags;
|
2022-05-11 04:59:19 +08:00
|
|
|
kfree(tlinks);
|
2019-11-15 02:57:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-03-29 08:43:52 +08:00
|
|
|
static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
|
2019-11-15 02:57:04 +08:00
|
|
|
{
|
2020-03-29 08:43:52 +08:00
|
|
|
switch (prog->expected_attach_type) {
|
2019-11-15 02:57:04 +08:00
|
|
|
case BPF_TRACE_FENTRY:
|
|
|
|
return BPF_TRAMP_FENTRY;
|
2020-03-05 03:18:49 +08:00
|
|
|
case BPF_MODIFY_RETURN:
|
|
|
|
return BPF_TRAMP_MODIFY_RETURN;
|
2020-01-21 08:53:46 +08:00
|
|
|
case BPF_TRACE_FEXIT:
|
2019-11-15 02:57:04 +08:00
|
|
|
return BPF_TRAMP_FEXIT;
|
2020-03-29 08:43:52 +08: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 08:53:46 +08:00
|
|
|
default:
|
|
|
|
return BPF_TRAMP_REPLACE;
|
2019-11-15 02:57:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 01:43:04 +08:00
|
|
|
static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
|
2019-11-15 02:57:04 +08:00
|
|
|
{
|
|
|
|
enum bpf_tramp_prog_type kind;
|
2022-05-11 04:59:19 +08:00
|
|
|
struct bpf_tramp_link *link_exiting;
|
2019-11-15 02:57:04 +08:00
|
|
|
int err = 0;
|
2022-04-30 21:08:03 +08:00
|
|
|
int cnt = 0, i;
|
2019-11-15 02:57:04 +08:00
|
|
|
|
2022-05-11 04:59:19 +08:00
|
|
|
kind = bpf_attach_type_to_tramp(link->link.prog);
|
2022-06-29 01:43:04 +08:00
|
|
|
if (tr->extension_prog)
|
2020-01-21 08:53:46 +08:00
|
|
|
/* cannot attach fentry/fexit if extension prog is attached.
|
|
|
|
* cannot overwrite extension prog either.
|
|
|
|
*/
|
2022-06-29 01:43:04 +08:00
|
|
|
return -EBUSY;
|
2022-04-30 21:08:03 +08:00
|
|
|
|
|
|
|
for (i = 0; i < BPF_TRAMP_MAX; i++)
|
|
|
|
cnt += tr->progs_cnt[i];
|
|
|
|
|
2020-01-21 08:53:46 +08:00
|
|
|
if (kind == BPF_TRAMP_REPLACE) {
|
|
|
|
/* Cannot attach extension if fentry/fexit are in use. */
|
2022-06-29 01:43:04 +08:00
|
|
|
if (cnt)
|
|
|
|
return -EBUSY;
|
2022-05-11 04:59:19 +08:00
|
|
|
tr->extension_prog = link->link.prog;
|
2022-06-29 01:43:04 +08:00
|
|
|
return bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
|
|
|
|
link->link.prog->bpf_func);
|
2019-11-15 02:57:04 +08:00
|
|
|
}
|
2022-06-29 01:43:04 +08:00
|
|
|
if (cnt >= BPF_MAX_TRAMP_LINKS)
|
|
|
|
return -E2BIG;
|
|
|
|
if (!hlist_unhashed(&link->tramp_hlist))
|
2019-11-15 02:57:04 +08:00
|
|
|
/* prog already linked */
|
2022-06-29 01:43:04 +08:00
|
|
|
return -EBUSY;
|
2022-05-11 04:59:19 +08: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-29 01:43:04 +08:00
|
|
|
return -EBUSY;
|
2022-05-11 04:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hlist_add_head(&link->tramp_hlist, &tr->progs_hlist[kind]);
|
2019-11-15 02:57:04 +08:00
|
|
|
tr->progs_cnt[kind]++;
|
2022-07-20 08:21:26 +08:00
|
|
|
err = bpf_trampoline_update(tr, true /* lock_direct_mutex */);
|
2019-11-15 02:57:04 +08:00
|
|
|
if (err) {
|
2022-05-11 04:59:19 +08:00
|
|
|
hlist_del_init(&link->tramp_hlist);
|
2019-11-15 02:57:04 +08:00
|
|
|
tr->progs_cnt[kind]--;
|
|
|
|
}
|
2022-06-29 01:43:04 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mutex_lock(&tr->mutex);
|
|
|
|
err = __bpf_trampoline_link_prog(link, tr);
|
2019-11-15 02:57:04 +08:00
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-06-29 01:43:04 +08:00
|
|
|
static int __bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
|
2019-11-15 02:57:04 +08:00
|
|
|
{
|
|
|
|
enum bpf_tramp_prog_type kind;
|
|
|
|
int err;
|
|
|
|
|
2022-05-11 04:59:19 +08:00
|
|
|
kind = bpf_attach_type_to_tramp(link->link.prog);
|
2020-01-21 08:53:46 +08: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;
|
2022-06-29 01:43:04 +08:00
|
|
|
return err;
|
2020-01-21 08:53:46 +08:00
|
|
|
}
|
2022-05-11 04:59:19 +08:00
|
|
|
hlist_del_init(&link->tramp_hlist);
|
2019-11-15 02:57:04 +08:00
|
|
|
tr->progs_cnt[kind]--;
|
2022-07-20 08:21:26 +08:00
|
|
|
return bpf_trampoline_update(tr, true /* lock_direct_mutex */);
|
2022-06-29 01:43:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* bpf_trampoline_unlink_prog() should never fail. */
|
|
|
|
int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mutex_lock(&tr->mutex);
|
|
|
|
err = __bpf_trampoline_unlink_prog(link, tr);
|
2019-11-15 02:57:04 +08:00
|
|
|
mutex_unlock(&tr->mutex);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-07-15 02:54:04 +08:00
|
|
|
#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
|
2022-06-29 01:43:06 +08: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;
|
|
|
|
|
|
|
|
WARN_ON_ONCE(bpf_trampoline_unlink_prog(&shim_link->link, shim_link->trampoline));
|
|
|
|
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-29 01:43:07 +08:00
|
|
|
bpf_cgroup_atype_get(p->aux->attach_btf_id, cgroup_atype);
|
2022-06-29 01:43:06 +08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = __bpf_trampoline_link_prog(&shim_link->link, tr);
|
|
|
|
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-26 05:25:02 +08: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-15 02:57:04 +08:00
|
|
|
void bpf_trampoline_put(struct bpf_trampoline *tr)
|
|
|
|
{
|
2022-04-30 21:08:03 +08:00
|
|
|
int i;
|
|
|
|
|
2019-11-15 02:57:04 +08: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 21:08:03 +08:00
|
|
|
|
|
|
|
for (i = 0; i < BPF_TRAMP_MAX; i++)
|
|
|
|
if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i])))
|
|
|
|
goto out;
|
|
|
|
|
2021-03-17 05:00:07 +08: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-28 06:01:11 +08:00
|
|
|
*/
|
2019-11-15 02:57:04 +08:00
|
|
|
hlist_del(&tr->hlist);
|
2022-08-02 21:56:51 +08:00
|
|
|
if (tr->fops) {
|
|
|
|
ftrace_free_filter(tr->fops);
|
|
|
|
kfree(tr->fops);
|
|
|
|
}
|
2019-11-15 02:57:04 +08:00
|
|
|
kfree(tr);
|
|
|
|
out:
|
|
|
|
mutex_unlock(&trampoline_mutex);
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:36:29 +08:00
|
|
|
#define NO_START_TIME 1
|
2021-09-11 02:33:51 +08:00
|
|
|
static __always_inline u64 notrace bpf_prog_start_time(void)
|
2021-02-10 11:36:28 +08:00
|
|
|
{
|
|
|
|
u64 start = NO_START_TIME;
|
|
|
|
|
2021-02-10 11:36:29 +08:00
|
|
|
if (static_branch_unlikely(&bpf_stats_enabled_key)) {
|
2021-02-10 11:36:28 +08:00
|
|
|
start = sched_clock();
|
2021-02-10 11:36:29 +08:00
|
|
|
if (unlikely(!start))
|
|
|
|
start = NO_START_TIME;
|
|
|
|
}
|
2021-02-10 11:36:28 +08:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:36:31 +08:00
|
|
|
static void notrace inc_misses_counter(struct bpf_prog *prog)
|
|
|
|
{
|
|
|
|
struct bpf_prog_stats *stats;
|
2022-01-22 18:29:36 +08:00
|
|
|
unsigned int flags;
|
2021-02-10 11:36:31 +08:00
|
|
|
|
|
|
|
stats = this_cpu_ptr(prog->stats);
|
2022-01-22 18:29:36 +08:00
|
|
|
flags = u64_stats_update_begin_irqsave(&stats->syncp);
|
2021-10-27 05:41:33 +08:00
|
|
|
u64_stats_inc(&stats->misses);
|
2022-01-22 18:29:36 +08:00
|
|
|
u64_stats_update_end_irqrestore(&stats->syncp, flags);
|
2021-02-10 11:36:31 +08:00
|
|
|
}
|
|
|
|
|
2021-08-15 15:05:54 +08:00
|
|
|
/* The logic is similar to bpf_prog_run(), but with an explicit
|
2020-02-24 22:01:45 +08:00
|
|
|
* rcu_read_lock() and migrate_disable() which are required
|
|
|
|
* for the trampoline. The macro is split into
|
2021-02-10 11:36:28 +08:00
|
|
|
* call __bpf_prog_enter
|
2019-11-15 02:57:04 +08:00
|
|
|
* call prog->bpf_func
|
|
|
|
* call __bpf_prog_exit
|
2021-02-10 11:36:29 +08:00
|
|
|
*
|
|
|
|
* __bpf_prog_enter returns:
|
|
|
|
* 0 - skip execution of the bpf prog
|
|
|
|
* 1 - execute bpf prog
|
2021-05-25 10:56:59 +08:00
|
|
|
* [2..MAX_U64] - execute bpf prog and record execution time.
|
2021-02-10 11:36:29 +08:00
|
|
|
* This is start time.
|
2019-11-15 02:57:04 +08:00
|
|
|
*/
|
2022-05-11 04:59:20 +08:00
|
|
|
u64 notrace __bpf_prog_enter(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx)
|
2020-03-11 09:09:01 +08:00
|
|
|
__acquires(RCU)
|
2019-11-15 02:57:04 +08:00
|
|
|
{
|
|
|
|
rcu_read_lock();
|
2020-02-24 22:01:45 +08:00
|
|
|
migrate_disable();
|
2022-05-11 04:59:20 +08:00
|
|
|
|
|
|
|
run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
|
|
|
|
|
2021-02-10 11:36:31 +08:00
|
|
|
if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
|
|
|
|
inc_misses_counter(prog);
|
2021-02-10 11:36:29 +08:00
|
|
|
return 0;
|
2021-02-10 11:36:31 +08:00
|
|
|
}
|
2021-02-10 11:36:28 +08:00
|
|
|
return bpf_prog_start_time();
|
2019-11-15 02:57:04 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 11:36:28 +08:00
|
|
|
static void notrace update_prog_stats(struct bpf_prog *prog,
|
|
|
|
u64 start)
|
2019-11-15 02:57:04 +08:00
|
|
|
{
|
|
|
|
struct bpf_prog_stats *stats;
|
|
|
|
|
|
|
|
if (static_branch_unlikely(&bpf_stats_enabled_key) &&
|
2021-02-10 11:36:28 +08:00
|
|
|
/* static_key could be enabled in __bpf_prog_enter*
|
|
|
|
* and disabled in __bpf_prog_exit*.
|
2019-11-15 02:57:04 +08:00
|
|
|
* And vice versa.
|
2021-02-10 11:36:28 +08:00
|
|
|
* Hence check that 'start' is valid.
|
2019-11-15 02:57:04 +08:00
|
|
|
*/
|
2021-02-10 11:36:28 +08:00
|
|
|
start > NO_START_TIME) {
|
2021-10-27 05:41:32 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
2021-02-10 11:36:26 +08:00
|
|
|
stats = this_cpu_ptr(prog->stats);
|
2021-10-27 05:41:32 +08:00
|
|
|
flags = u64_stats_update_begin_irqsave(&stats->syncp);
|
2021-10-27 05:41:33 +08:00
|
|
|
u64_stats_inc(&stats->cnt);
|
|
|
|
u64_stats_add(&stats->nsecs, sched_clock() - start);
|
2021-10-27 05:41:32 +08:00
|
|
|
u64_stats_update_end_irqrestore(&stats->syncp, flags);
|
2019-11-15 02:57:04 +08:00
|
|
|
}
|
2021-02-10 11:36:28 +08:00
|
|
|
}
|
|
|
|
|
2022-05-11 04:59:20 +08:00
|
|
|
void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx)
|
2021-02-10 11:36:28 +08:00
|
|
|
__releases(RCU)
|
|
|
|
{
|
2022-05-11 04:59:20 +08:00
|
|
|
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
|
|
|
|
|
2021-02-10 11:36:28 +08:00
|
|
|
update_prog_stats(prog, start);
|
2021-02-10 11:36:29 +08:00
|
|
|
__this_cpu_dec(*(prog->active));
|
2020-02-24 22:01:45 +08:00
|
|
|
migrate_enable();
|
2019-11-15 02:57:04 +08:00
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2022-06-29 01:43:06 +08:00
|
|
|
u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
|
|
|
__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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
|
|
|
__releases(RCU)
|
|
|
|
{
|
|
|
|
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
|
|
|
|
|
|
|
|
migrate_enable();
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2022-05-11 04:59:20 +08:00
|
|
|
u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx)
|
2020-08-28 06:01:11 +08:00
|
|
|
{
|
|
|
|
rcu_read_lock_trace();
|
2021-02-10 11:36:27 +08:00
|
|
|
migrate_disable();
|
2020-09-01 04:16:51 +08:00
|
|
|
might_fault();
|
2022-05-11 04:59:20 +08:00
|
|
|
|
2021-02-10 11:36:31 +08:00
|
|
|
if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
|
|
|
|
inc_misses_counter(prog);
|
2021-02-10 11:36:29 +08:00
|
|
|
return 0;
|
2021-02-10 11:36:31 +08:00
|
|
|
}
|
2022-05-11 04:59:20 +08:00
|
|
|
|
|
|
|
run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
|
|
|
|
|
2021-02-10 11:36:28 +08:00
|
|
|
return bpf_prog_start_time();
|
2020-08-28 06:01:11 +08:00
|
|
|
}
|
|
|
|
|
2022-05-11 04:59:20 +08:00
|
|
|
void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
|
|
|
|
struct bpf_tramp_run_ctx *run_ctx)
|
2020-08-28 06:01:11 +08:00
|
|
|
{
|
2022-05-11 04:59:20 +08:00
|
|
|
bpf_reset_run_ctx(run_ctx->saved_run_ctx);
|
|
|
|
|
2021-02-10 11:36:28 +08:00
|
|
|
update_prog_stats(prog, start);
|
2021-02-10 11:36:29 +08:00
|
|
|
__this_cpu_dec(*(prog->active));
|
2021-02-10 11:36:27 +08:00
|
|
|
migrate_enable();
|
2020-08-28 06:01:11 +08:00
|
|
|
rcu_read_unlock_trace();
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:00:07 +08: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);
|
|
|
|
}
|
|
|
|
|
2019-11-15 02:57:04 +08:00
|
|
|
int __weak
|
2021-03-17 05:00:07 +08:00
|
|
|
arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, 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 08:35:05 +08:00
|
|
|
const struct btf_func_model *m, u32 flags,
|
2022-05-11 04:59:19 +08:00
|
|
|
struct bpf_tramp_links *tlinks,
|
2019-11-15 02:57:04 +08:00
|
|
|
void *orig_call)
|
|
|
|
{
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|