1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2019 Facebook */ 3 #include <linux/hash.h> 4 #include <linux/bpf.h> 5 #include <linux/filter.h> 6 #include <linux/ftrace.h> 7 8 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */ 9 #define TRAMPOLINE_HASH_BITS 10 10 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS) 11 12 static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE]; 13 14 /* serializes access to trampoline_table */ 15 static DEFINE_MUTEX(trampoline_mutex); 16 17 struct bpf_trampoline *bpf_trampoline_lookup(u64 key) 18 { 19 struct bpf_trampoline *tr; 20 struct hlist_head *head; 21 void *image; 22 int i; 23 24 mutex_lock(&trampoline_mutex); 25 head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)]; 26 hlist_for_each_entry(tr, head, hlist) { 27 if (tr->key == key) { 28 refcount_inc(&tr->refcnt); 29 goto out; 30 } 31 } 32 tr = kzalloc(sizeof(*tr), GFP_KERNEL); 33 if (!tr) 34 goto out; 35 36 /* is_root was checked earlier. No need for bpf_jit_charge_modmem() */ 37 image = bpf_jit_alloc_exec(PAGE_SIZE); 38 if (!image) { 39 kfree(tr); 40 tr = NULL; 41 goto out; 42 } 43 44 tr->key = key; 45 INIT_HLIST_NODE(&tr->hlist); 46 hlist_add_head(&tr->hlist, head); 47 refcount_set(&tr->refcnt, 1); 48 mutex_init(&tr->mutex); 49 for (i = 0; i < BPF_TRAMP_MAX; i++) 50 INIT_HLIST_HEAD(&tr->progs_hlist[i]); 51 52 set_vm_flush_reset_perms(image); 53 /* Keep image as writeable. The alternative is to keep flipping ro/rw 54 * everytime new program is attached or detached. 55 */ 56 set_memory_x((long)image, 1); 57 tr->image = image; 58 out: 59 mutex_unlock(&trampoline_mutex); 60 return tr; 61 } 62 63 static int is_ftrace_location(void *ip) 64 { 65 long addr; 66 67 addr = ftrace_location((long)ip); 68 if (!addr) 69 return 0; 70 if (WARN_ON_ONCE(addr != (long)ip)) 71 return -EFAULT; 72 return 1; 73 } 74 75 static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr) 76 { 77 void *ip = tr->func.addr; 78 int ret; 79 80 if (tr->func.ftrace_managed) 81 ret = unregister_ftrace_direct((long)ip, (long)old_addr); 82 else 83 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL); 84 return ret; 85 } 86 87 static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr) 88 { 89 void *ip = tr->func.addr; 90 int ret; 91 92 if (tr->func.ftrace_managed) 93 ret = modify_ftrace_direct((long)ip, (long)old_addr, (long)new_addr); 94 else 95 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr); 96 return ret; 97 } 98 99 /* first time registering */ 100 static int register_fentry(struct bpf_trampoline *tr, void *new_addr) 101 { 102 void *ip = tr->func.addr; 103 int ret; 104 105 ret = is_ftrace_location(ip); 106 if (ret < 0) 107 return ret; 108 tr->func.ftrace_managed = ret; 109 110 if (tr->func.ftrace_managed) 111 ret = register_ftrace_direct((long)ip, (long)new_addr); 112 else 113 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr); 114 return ret; 115 } 116 117 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 118 * bytes on x86. Pick a number to fit into PAGE_SIZE / 2 119 */ 120 #define BPF_MAX_TRAMP_PROGS 40 121 122 static int bpf_trampoline_update(struct bpf_trampoline *tr) 123 { 124 void *old_image = tr->image + ((tr->selector + 1) & 1) * PAGE_SIZE/2; 125 void *new_image = tr->image + (tr->selector & 1) * PAGE_SIZE/2; 126 struct bpf_prog *progs_to_run[BPF_MAX_TRAMP_PROGS]; 127 int fentry_cnt = tr->progs_cnt[BPF_TRAMP_FENTRY]; 128 int fexit_cnt = tr->progs_cnt[BPF_TRAMP_FEXIT]; 129 struct bpf_prog **progs, **fentry, **fexit; 130 u32 flags = BPF_TRAMP_F_RESTORE_REGS; 131 struct bpf_prog_aux *aux; 132 int err; 133 134 if (fentry_cnt + fexit_cnt == 0) { 135 err = unregister_fentry(tr, old_image); 136 tr->selector = 0; 137 goto out; 138 } 139 140 /* populate fentry progs */ 141 fentry = progs = progs_to_run; 142 hlist_for_each_entry(aux, &tr->progs_hlist[BPF_TRAMP_FENTRY], tramp_hlist) 143 *progs++ = aux->prog; 144 145 /* populate fexit progs */ 146 fexit = progs; 147 hlist_for_each_entry(aux, &tr->progs_hlist[BPF_TRAMP_FEXIT], tramp_hlist) 148 *progs++ = aux->prog; 149 150 if (fexit_cnt) 151 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; 152 153 err = arch_prepare_bpf_trampoline(new_image, &tr->func.model, flags, 154 fentry, fentry_cnt, 155 fexit, fexit_cnt, 156 tr->func.addr); 157 if (err) 158 goto out; 159 160 if (tr->selector) 161 /* progs already running at this address */ 162 err = modify_fentry(tr, old_image, new_image); 163 else 164 /* first time registering */ 165 err = register_fentry(tr, new_image); 166 if (err) 167 goto out; 168 tr->selector++; 169 out: 170 return err; 171 } 172 173 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(enum bpf_attach_type t) 174 { 175 switch (t) { 176 case BPF_TRACE_FENTRY: 177 return BPF_TRAMP_FENTRY; 178 default: 179 return BPF_TRAMP_FEXIT; 180 } 181 } 182 183 int bpf_trampoline_link_prog(struct bpf_prog *prog) 184 { 185 enum bpf_tramp_prog_type kind; 186 struct bpf_trampoline *tr; 187 int err = 0; 188 189 tr = prog->aux->trampoline; 190 kind = bpf_attach_type_to_tramp(prog->expected_attach_type); 191 mutex_lock(&tr->mutex); 192 if (tr->progs_cnt[BPF_TRAMP_FENTRY] + tr->progs_cnt[BPF_TRAMP_FEXIT] 193 >= BPF_MAX_TRAMP_PROGS) { 194 err = -E2BIG; 195 goto out; 196 } 197 if (!hlist_unhashed(&prog->aux->tramp_hlist)) { 198 /* prog already linked */ 199 err = -EBUSY; 200 goto out; 201 } 202 hlist_add_head(&prog->aux->tramp_hlist, &tr->progs_hlist[kind]); 203 tr->progs_cnt[kind]++; 204 err = bpf_trampoline_update(prog->aux->trampoline); 205 if (err) { 206 hlist_del(&prog->aux->tramp_hlist); 207 tr->progs_cnt[kind]--; 208 } 209 out: 210 mutex_unlock(&tr->mutex); 211 return err; 212 } 213 214 /* bpf_trampoline_unlink_prog() should never fail. */ 215 int bpf_trampoline_unlink_prog(struct bpf_prog *prog) 216 { 217 enum bpf_tramp_prog_type kind; 218 struct bpf_trampoline *tr; 219 int err; 220 221 tr = prog->aux->trampoline; 222 kind = bpf_attach_type_to_tramp(prog->expected_attach_type); 223 mutex_lock(&tr->mutex); 224 hlist_del(&prog->aux->tramp_hlist); 225 tr->progs_cnt[kind]--; 226 err = bpf_trampoline_update(prog->aux->trampoline); 227 mutex_unlock(&tr->mutex); 228 return err; 229 } 230 231 void bpf_trampoline_put(struct bpf_trampoline *tr) 232 { 233 if (!tr) 234 return; 235 mutex_lock(&trampoline_mutex); 236 if (!refcount_dec_and_test(&tr->refcnt)) 237 goto out; 238 WARN_ON_ONCE(mutex_is_locked(&tr->mutex)); 239 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FENTRY]))) 240 goto out; 241 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT]))) 242 goto out; 243 bpf_jit_free_exec(tr->image); 244 hlist_del(&tr->hlist); 245 kfree(tr); 246 out: 247 mutex_unlock(&trampoline_mutex); 248 } 249 250 /* The logic is similar to BPF_PROG_RUN, but with explicit rcu and preempt that 251 * are needed for trampoline. The macro is split into 252 * call _bpf_prog_enter 253 * call prog->bpf_func 254 * call __bpf_prog_exit 255 */ 256 u64 notrace __bpf_prog_enter(void) 257 { 258 u64 start = 0; 259 260 rcu_read_lock(); 261 preempt_disable(); 262 if (static_branch_unlikely(&bpf_stats_enabled_key)) 263 start = sched_clock(); 264 return start; 265 } 266 267 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) 268 { 269 struct bpf_prog_stats *stats; 270 271 if (static_branch_unlikely(&bpf_stats_enabled_key) && 272 /* static_key could be enabled in __bpf_prog_enter 273 * and disabled in __bpf_prog_exit. 274 * And vice versa. 275 * Hence check that 'start' is not zero. 276 */ 277 start) { 278 stats = this_cpu_ptr(prog->aux->stats); 279 u64_stats_update_begin(&stats->syncp); 280 stats->cnt++; 281 stats->nsecs += sched_clock() - start; 282 u64_stats_update_end(&stats->syncp); 283 } 284 preempt_enable(); 285 rcu_read_unlock(); 286 } 287 288 int __weak 289 arch_prepare_bpf_trampoline(void *image, struct btf_func_model *m, u32 flags, 290 struct bpf_prog **fentry_progs, int fentry_cnt, 291 struct bpf_prog **fexit_progs, int fexit_cnt, 292 void *orig_call) 293 { 294 return -ENOTSUPP; 295 } 296 297 static int __init init_trampolines(void) 298 { 299 int i; 300 301 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 302 INIT_HLIST_HEAD(&trampoline_table[i]); 303 return 0; 304 } 305 late_initcall(init_trampolines); 306