• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/rbtree_latch.h>
8 #include <linux/perf_event.h>
9 #include <linux/btf.h>
10 #include <linux/rcupdate_trace.h>
11 #include <linux/rcupdate_wait.h>
12 #include <linux/module.h>
13 
14 /* dummy _ops. The verifier will operate on target program's ops. */
15 const struct bpf_verifier_ops bpf_extension_verifier_ops = {
16 };
17 const struct bpf_prog_ops bpf_extension_prog_ops = {
18 };
19 
20 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */
21 #define TRAMPOLINE_HASH_BITS 10
22 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
23 
24 static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
25 
26 /* serializes access to trampoline_table */
27 static DEFINE_MUTEX(trampoline_mutex);
28 
bpf_jit_alloc_exec_page(void)29 void *bpf_jit_alloc_exec_page(void)
30 {
31 	void *image;
32 
33 	image = bpf_jit_alloc_exec(PAGE_SIZE);
34 	if (!image)
35 		return NULL;
36 
37 	set_vm_flush_reset_perms(image);
38 	/* Keep image as writeable. The alternative is to keep flipping ro/rw
39 	 * everytime new program is attached or detached.
40 	 */
41 	set_memory_x((long)image, 1);
42 	return image;
43 }
44 
bpf_image_ksym_add(void * data,struct bpf_ksym * ksym)45 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
46 {
47 	ksym->start = (unsigned long) data;
48 	ksym->end = ksym->start + PAGE_SIZE;
49 	bpf_ksym_add(ksym);
50 	perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
51 			   PAGE_SIZE, false, ksym->name);
52 }
53 
bpf_image_ksym_del(struct bpf_ksym * ksym)54 void bpf_image_ksym_del(struct bpf_ksym *ksym)
55 {
56 	bpf_ksym_del(ksym);
57 	perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
58 			   PAGE_SIZE, true, ksym->name);
59 }
60 
bpf_trampoline_lookup(u64 key)61 static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
62 {
63 	struct bpf_trampoline *tr;
64 	struct hlist_head *head;
65 	int i;
66 
67 	mutex_lock(&trampoline_mutex);
68 	head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
69 	hlist_for_each_entry(tr, head, hlist) {
70 		if (tr->key == key) {
71 			refcount_inc(&tr->refcnt);
72 			goto out;
73 		}
74 	}
75 	tr = kzalloc(sizeof(*tr), GFP_KERNEL);
76 	if (!tr)
77 		goto out;
78 
79 	tr->key = key;
80 	INIT_HLIST_NODE(&tr->hlist);
81 	hlist_add_head(&tr->hlist, head);
82 	refcount_set(&tr->refcnt, 1);
83 	mutex_init(&tr->mutex);
84 	for (i = 0; i < BPF_TRAMP_MAX; i++)
85 		INIT_HLIST_HEAD(&tr->progs_hlist[i]);
86 out:
87 	mutex_unlock(&trampoline_mutex);
88 	return tr;
89 }
90 
bpf_trampoline_module_get(struct bpf_trampoline * tr)91 static int bpf_trampoline_module_get(struct bpf_trampoline *tr)
92 {
93 	struct module *mod;
94 	int err = 0;
95 
96 	preempt_disable();
97 	mod = __module_text_address((unsigned long) tr->func.addr);
98 	if (mod && !try_module_get(mod))
99 		err = -ENOENT;
100 	preempt_enable();
101 	tr->mod = mod;
102 	return err;
103 }
104 
bpf_trampoline_module_put(struct bpf_trampoline * tr)105 static void bpf_trampoline_module_put(struct bpf_trampoline *tr)
106 {
107 	module_put(tr->mod);
108 	tr->mod = NULL;
109 }
110 
is_ftrace_location(void * ip)111 static int is_ftrace_location(void *ip)
112 {
113 	long addr;
114 
115 	addr = ftrace_location((long)ip);
116 	if (!addr)
117 		return 0;
118 	if (WARN_ON_ONCE(addr != (long)ip))
119 		return -EFAULT;
120 	return 1;
121 }
122 
unregister_fentry(struct bpf_trampoline * tr,void * old_addr)123 static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
124 {
125 	void *ip = tr->func.addr;
126 	int ret;
127 
128 	if (tr->func.ftrace_managed)
129 		ret = unregister_ftrace_direct((long)ip, (long)old_addr);
130 	else
131 		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
132 
133 	if (!ret)
134 		bpf_trampoline_module_put(tr);
135 	return ret;
136 }
137 
modify_fentry(struct bpf_trampoline * tr,void * old_addr,void * new_addr)138 static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr)
139 {
140 	void *ip = tr->func.addr;
141 	int ret;
142 
143 	if (tr->func.ftrace_managed)
144 		ret = modify_ftrace_direct((long)ip, (long)old_addr, (long)new_addr);
145 	else
146 		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr);
147 	return ret;
148 }
149 
150 /* first time registering */
register_fentry(struct bpf_trampoline * tr,void * new_addr)151 static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
152 {
153 	void *ip = tr->func.addr;
154 	int ret;
155 
156 	ret = is_ftrace_location(ip);
157 	if (ret < 0)
158 		return ret;
159 	tr->func.ftrace_managed = ret;
160 
161 	if (bpf_trampoline_module_get(tr))
162 		return -ENOENT;
163 
164 	if (tr->func.ftrace_managed)
165 		ret = register_ftrace_direct((long)ip, (long)new_addr);
166 	else
167 		ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
168 
169 	if (ret)
170 		bpf_trampoline_module_put(tr);
171 	return ret;
172 }
173 
174 static struct bpf_tramp_progs *
bpf_trampoline_get_progs(const struct bpf_trampoline * tr,int * total,bool * ip_arg)175 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg)
176 {
177 	const struct bpf_prog_aux *aux;
178 	struct bpf_tramp_progs *tprogs;
179 	struct bpf_prog **progs;
180 	int kind;
181 
182 	*total = 0;
183 	tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
184 	if (!tprogs)
185 		return ERR_PTR(-ENOMEM);
186 
187 	for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
188 		tprogs[kind].nr_progs = tr->progs_cnt[kind];
189 		*total += tr->progs_cnt[kind];
190 		progs = tprogs[kind].progs;
191 
192 		hlist_for_each_entry(aux, &tr->progs_hlist[kind], tramp_hlist) {
193 			*ip_arg |= aux->prog->call_get_func_ip;
194 			*progs++ = aux->prog;
195 		}
196 	}
197 	return tprogs;
198 }
199 
__bpf_tramp_image_put_deferred(struct work_struct * work)200 static void __bpf_tramp_image_put_deferred(struct work_struct *work)
201 {
202 	struct bpf_tramp_image *im;
203 
204 	im = container_of(work, struct bpf_tramp_image, work);
205 	bpf_image_ksym_del(&im->ksym);
206 	bpf_jit_free_exec(im->image);
207 	bpf_jit_uncharge_modmem(1);
208 	percpu_ref_exit(&im->pcref);
209 	kfree_rcu(im, rcu);
210 }
211 
212 /* callback, fexit step 3 or fentry step 2 */
__bpf_tramp_image_put_rcu(struct rcu_head * rcu)213 static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu)
214 {
215 	struct bpf_tramp_image *im;
216 
217 	im = container_of(rcu, struct bpf_tramp_image, rcu);
218 	INIT_WORK(&im->work, __bpf_tramp_image_put_deferred);
219 	schedule_work(&im->work);
220 }
221 
222 /* callback, fexit step 2. Called after percpu_ref_kill confirms. */
__bpf_tramp_image_release(struct percpu_ref * pcref)223 static void __bpf_tramp_image_release(struct percpu_ref *pcref)
224 {
225 	struct bpf_tramp_image *im;
226 
227 	im = container_of(pcref, struct bpf_tramp_image, pcref);
228 	call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
229 }
230 
231 /* callback, fexit or fentry step 1 */
__bpf_tramp_image_put_rcu_tasks(struct rcu_head * rcu)232 static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)
233 {
234 	struct bpf_tramp_image *im;
235 
236 	im = container_of(rcu, struct bpf_tramp_image, rcu);
237 	if (im->ip_after_call)
238 		/* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */
239 		percpu_ref_kill(&im->pcref);
240 	else
241 		/* the case of fentry trampoline */
242 		call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
243 }
244 
bpf_tramp_image_put(struct bpf_tramp_image * im)245 static void bpf_tramp_image_put(struct bpf_tramp_image *im)
246 {
247 	/* The trampoline image that calls original function is using:
248 	 * rcu_read_lock_trace to protect sleepable bpf progs
249 	 * rcu_read_lock to protect normal bpf progs
250 	 * percpu_ref to protect trampoline itself
251 	 * rcu tasks to protect trampoline asm not covered by percpu_ref
252 	 * (which are few asm insns before __bpf_tramp_enter and
253 	 *  after __bpf_tramp_exit)
254 	 *
255 	 * The trampoline is unreachable before bpf_tramp_image_put().
256 	 *
257 	 * First, patch the trampoline to avoid calling into fexit progs.
258 	 * The progs will be freed even if the original function is still
259 	 * executing or sleeping.
260 	 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on
261 	 * first few asm instructions to execute and call into
262 	 * __bpf_tramp_enter->percpu_ref_get.
263 	 * Then use percpu_ref_kill to wait for the trampoline and the original
264 	 * function to finish.
265 	 * Then use call_rcu_tasks() to make sure few asm insns in
266 	 * the trampoline epilogue are done as well.
267 	 *
268 	 * In !PREEMPT case the task that got interrupted in the first asm
269 	 * insns won't go through an RCU quiescent state which the
270 	 * percpu_ref_kill will be waiting for. Hence the first
271 	 * call_rcu_tasks() is not necessary.
272 	 */
273 	if (im->ip_after_call) {
274 		int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_JUMP,
275 					     NULL, im->ip_epilogue);
276 		WARN_ON(err);
277 		if (IS_ENABLED(CONFIG_PREEMPTION))
278 			call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
279 		else
280 			percpu_ref_kill(&im->pcref);
281 		return;
282 	}
283 
284 	/* The trampoline without fexit and fmod_ret progs doesn't call original
285 	 * function and doesn't use percpu_ref.
286 	 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish.
287 	 * Then use call_rcu_tasks() to wait for the rest of trampoline asm
288 	 * and normal progs.
289 	 */
290 	call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
291 }
292 
bpf_tramp_image_alloc(u64 key,u32 idx)293 static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx)
294 {
295 	struct bpf_tramp_image *im;
296 	struct bpf_ksym *ksym;
297 	void *image;
298 	int err = -ENOMEM;
299 
300 	im = kzalloc(sizeof(*im), GFP_KERNEL);
301 	if (!im)
302 		goto out;
303 
304 	err = bpf_jit_charge_modmem(1);
305 	if (err)
306 		goto out_free_im;
307 
308 	err = -ENOMEM;
309 	im->image = image = bpf_jit_alloc_exec_page();
310 	if (!image)
311 		goto out_uncharge;
312 
313 	err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL);
314 	if (err)
315 		goto out_free_image;
316 
317 	ksym = &im->ksym;
318 	INIT_LIST_HEAD_RCU(&ksym->lnode);
319 	snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu_%u", key, idx);
320 	bpf_image_ksym_add(image, ksym);
321 	return im;
322 
323 out_free_image:
324 	bpf_jit_free_exec(im->image);
325 out_uncharge:
326 	bpf_jit_uncharge_modmem(1);
327 out_free_im:
328 	kfree(im);
329 out:
330 	return ERR_PTR(err);
331 }
332 
bpf_trampoline_update(struct bpf_trampoline * tr)333 static int bpf_trampoline_update(struct bpf_trampoline *tr)
334 {
335 	struct bpf_tramp_image *im;
336 	struct bpf_tramp_progs *tprogs;
337 	u32 flags = BPF_TRAMP_F_RESTORE_REGS;
338 	bool ip_arg = false;
339 	int err, total;
340 
341 	tprogs = bpf_trampoline_get_progs(tr, &total, &ip_arg);
342 	if (IS_ERR(tprogs))
343 		return PTR_ERR(tprogs);
344 
345 	if (total == 0) {
346 		err = unregister_fentry(tr, tr->cur_image->image);
347 		bpf_tramp_image_put(tr->cur_image);
348 		tr->cur_image = NULL;
349 		tr->selector = 0;
350 		goto out;
351 	}
352 
353 	im = bpf_tramp_image_alloc(tr->key, tr->selector);
354 	if (IS_ERR(im)) {
355 		err = PTR_ERR(im);
356 		goto out;
357 	}
358 
359 	if (tprogs[BPF_TRAMP_FEXIT].nr_progs ||
360 	    tprogs[BPF_TRAMP_MODIFY_RETURN].nr_progs)
361 		flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
362 
363 	if (ip_arg)
364 		flags |= BPF_TRAMP_F_IP_ARG;
365 
366 	err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
367 					  &tr->func.model, flags, tprogs,
368 					  tr->func.addr);
369 	if (err < 0)
370 		goto out;
371 
372 	WARN_ON(tr->cur_image && tr->selector == 0);
373 	WARN_ON(!tr->cur_image && tr->selector);
374 	if (tr->cur_image)
375 		/* progs already running at this address */
376 		err = modify_fentry(tr, tr->cur_image->image, im->image);
377 	else
378 		/* first time registering */
379 		err = register_fentry(tr, im->image);
380 	if (err)
381 		goto out;
382 	if (tr->cur_image)
383 		bpf_tramp_image_put(tr->cur_image);
384 	tr->cur_image = im;
385 	tr->selector++;
386 out:
387 	kfree(tprogs);
388 	return err;
389 }
390 
bpf_attach_type_to_tramp(struct bpf_prog * prog)391 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
392 {
393 	switch (prog->expected_attach_type) {
394 	case BPF_TRACE_FENTRY:
395 		return BPF_TRAMP_FENTRY;
396 	case BPF_MODIFY_RETURN:
397 		return BPF_TRAMP_MODIFY_RETURN;
398 	case BPF_TRACE_FEXIT:
399 		return BPF_TRAMP_FEXIT;
400 	case BPF_LSM_MAC:
401 		if (!prog->aux->attach_func_proto->type)
402 			/* The function returns void, we cannot modify its
403 			 * return value.
404 			 */
405 			return BPF_TRAMP_FEXIT;
406 		else
407 			return BPF_TRAMP_MODIFY_RETURN;
408 	default:
409 		return BPF_TRAMP_REPLACE;
410 	}
411 }
412 
bpf_trampoline_link_prog(struct bpf_prog * prog,struct bpf_trampoline * tr)413 int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
414 {
415 	enum bpf_tramp_prog_type kind;
416 	int err = 0;
417 	int cnt = 0, i;
418 
419 	kind = bpf_attach_type_to_tramp(prog);
420 	mutex_lock(&tr->mutex);
421 	if (tr->extension_prog) {
422 		/* cannot attach fentry/fexit if extension prog is attached.
423 		 * cannot overwrite extension prog either.
424 		 */
425 		err = -EBUSY;
426 		goto out;
427 	}
428 
429 	for (i = 0; i < BPF_TRAMP_MAX; i++)
430 		cnt += tr->progs_cnt[i];
431 
432 	if (kind == BPF_TRAMP_REPLACE) {
433 		/* Cannot attach extension if fentry/fexit are in use. */
434 		if (cnt) {
435 			err = -EBUSY;
436 			goto out;
437 		}
438 		tr->extension_prog = prog;
439 		err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
440 					 prog->bpf_func);
441 		goto out;
442 	}
443 	if (cnt >= BPF_MAX_TRAMP_PROGS) {
444 		err = -E2BIG;
445 		goto out;
446 	}
447 	if (!hlist_unhashed(&prog->aux->tramp_hlist)) {
448 		/* prog already linked */
449 		err = -EBUSY;
450 		goto out;
451 	}
452 	hlist_add_head(&prog->aux->tramp_hlist, &tr->progs_hlist[kind]);
453 	tr->progs_cnt[kind]++;
454 	err = bpf_trampoline_update(tr);
455 	if (err) {
456 		hlist_del_init(&prog->aux->tramp_hlist);
457 		tr->progs_cnt[kind]--;
458 	}
459 out:
460 	mutex_unlock(&tr->mutex);
461 	return err;
462 }
463 
464 /* bpf_trampoline_unlink_prog() should never fail. */
bpf_trampoline_unlink_prog(struct bpf_prog * prog,struct bpf_trampoline * tr)465 int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
466 {
467 	enum bpf_tramp_prog_type kind;
468 	int err;
469 
470 	kind = bpf_attach_type_to_tramp(prog);
471 	mutex_lock(&tr->mutex);
472 	if (kind == BPF_TRAMP_REPLACE) {
473 		WARN_ON_ONCE(!tr->extension_prog);
474 		err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP,
475 					 tr->extension_prog->bpf_func, NULL);
476 		tr->extension_prog = NULL;
477 		goto out;
478 	}
479 	hlist_del_init(&prog->aux->tramp_hlist);
480 	tr->progs_cnt[kind]--;
481 	err = bpf_trampoline_update(tr);
482 out:
483 	mutex_unlock(&tr->mutex);
484 	return err;
485 }
486 
bpf_trampoline_get(u64 key,struct bpf_attach_target_info * tgt_info)487 struct bpf_trampoline *bpf_trampoline_get(u64 key,
488 					  struct bpf_attach_target_info *tgt_info)
489 {
490 	struct bpf_trampoline *tr;
491 
492 	tr = bpf_trampoline_lookup(key);
493 	if (!tr)
494 		return NULL;
495 
496 	mutex_lock(&tr->mutex);
497 	if (tr->func.addr)
498 		goto out;
499 
500 	memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel));
501 	tr->func.addr = (void *)tgt_info->tgt_addr;
502 out:
503 	mutex_unlock(&tr->mutex);
504 	return tr;
505 }
506 
bpf_trampoline_put(struct bpf_trampoline * tr)507 void bpf_trampoline_put(struct bpf_trampoline *tr)
508 {
509 	int i;
510 
511 	if (!tr)
512 		return;
513 	mutex_lock(&trampoline_mutex);
514 	if (!refcount_dec_and_test(&tr->refcnt))
515 		goto out;
516 	WARN_ON_ONCE(mutex_is_locked(&tr->mutex));
517 
518 	for (i = 0; i < BPF_TRAMP_MAX; i++)
519 		if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i])))
520 			goto out;
521 
522 	/* This code will be executed even when the last bpf_tramp_image
523 	 * is alive. All progs are detached from the trampoline and the
524 	 * trampoline image is patched with jmp into epilogue to skip
525 	 * fexit progs. The fentry-only trampoline will be freed via
526 	 * multiple rcu callbacks.
527 	 */
528 	hlist_del(&tr->hlist);
529 	kfree(tr);
530 out:
531 	mutex_unlock(&trampoline_mutex);
532 }
533 
534 #define NO_START_TIME 1
bpf_prog_start_time(void)535 static u64 notrace bpf_prog_start_time(void)
536 {
537 	u64 start = NO_START_TIME;
538 
539 	if (static_branch_unlikely(&bpf_stats_enabled_key)) {
540 		start = sched_clock();
541 		if (unlikely(!start))
542 			start = NO_START_TIME;
543 	}
544 	return start;
545 }
546 
inc_misses_counter(struct bpf_prog * prog)547 static void notrace inc_misses_counter(struct bpf_prog *prog)
548 {
549 	struct bpf_prog_stats *stats;
550 	unsigned int flags;
551 
552 	stats = this_cpu_ptr(prog->stats);
553 	flags = u64_stats_update_begin_irqsave(&stats->syncp);
554 	u64_stats_inc(&stats->misses);
555 	u64_stats_update_end_irqrestore(&stats->syncp, flags);
556 }
557 
558 /* The logic is similar to bpf_prog_run(), but with an explicit
559  * rcu_read_lock() and migrate_disable() which are required
560  * for the trampoline. The macro is split into
561  * call __bpf_prog_enter
562  * call prog->bpf_func
563  * call __bpf_prog_exit
564  *
565  * __bpf_prog_enter returns:
566  * 0 - skip execution of the bpf prog
567  * 1 - execute bpf prog
568  * [2..MAX_U64] - execute bpf prog and record execution time.
569  *     This is start time.
570  */
__bpf_prog_enter(struct bpf_prog * prog)571 u64 notrace __bpf_prog_enter(struct bpf_prog *prog)
572 	__acquires(RCU)
573 {
574 	rcu_read_lock();
575 	migrate_disable();
576 	if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
577 		inc_misses_counter(prog);
578 		return 0;
579 	}
580 	return bpf_prog_start_time();
581 }
582 
update_prog_stats(struct bpf_prog * prog,u64 start)583 static void notrace update_prog_stats(struct bpf_prog *prog,
584 				      u64 start)
585 {
586 	struct bpf_prog_stats *stats;
587 
588 	if (static_branch_unlikely(&bpf_stats_enabled_key) &&
589 	    /* static_key could be enabled in __bpf_prog_enter*
590 	     * and disabled in __bpf_prog_exit*.
591 	     * And vice versa.
592 	     * Hence check that 'start' is valid.
593 	     */
594 	    start > NO_START_TIME) {
595 		unsigned long flags;
596 
597 		stats = this_cpu_ptr(prog->stats);
598 		flags = u64_stats_update_begin_irqsave(&stats->syncp);
599 		u64_stats_inc(&stats->cnt);
600 		u64_stats_add(&stats->nsecs, sched_clock() - start);
601 		u64_stats_update_end_irqrestore(&stats->syncp, flags);
602 	}
603 }
604 
__bpf_prog_exit(struct bpf_prog * prog,u64 start)605 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start)
606 	__releases(RCU)
607 {
608 	update_prog_stats(prog, start);
609 	__this_cpu_dec(*(prog->active));
610 	migrate_enable();
611 	rcu_read_unlock();
612 }
613 
__bpf_prog_enter_sleepable(struct bpf_prog * prog)614 u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog)
615 {
616 	rcu_read_lock_trace();
617 	migrate_disable();
618 	might_fault();
619 	if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
620 		inc_misses_counter(prog);
621 		return 0;
622 	}
623 	return bpf_prog_start_time();
624 }
625 
__bpf_prog_exit_sleepable(struct bpf_prog * prog,u64 start)626 void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start)
627 {
628 	update_prog_stats(prog, start);
629 	__this_cpu_dec(*(prog->active));
630 	migrate_enable();
631 	rcu_read_unlock_trace();
632 }
633 
__bpf_tramp_enter(struct bpf_tramp_image * tr)634 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr)
635 {
636 	percpu_ref_get(&tr->pcref);
637 }
638 
__bpf_tramp_exit(struct bpf_tramp_image * tr)639 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr)
640 {
641 	percpu_ref_put(&tr->pcref);
642 }
643 
644 int __weak
arch_prepare_bpf_trampoline(struct bpf_tramp_image * tr,void * image,void * image_end,const struct btf_func_model * m,u32 flags,struct bpf_tramp_progs * tprogs,void * orig_call)645 arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
646 			    const struct btf_func_model *m, u32 flags,
647 			    struct bpf_tramp_progs *tprogs,
648 			    void *orig_call)
649 {
650 	return -ENOTSUPP;
651 }
652 
init_trampolines(void)653 static int __init init_trampolines(void)
654 {
655 	int i;
656 
657 	for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
658 		INIT_HLIST_HEAD(&trampoline_table[i]);
659 	return 0;
660 }
661 late_initcall(init_trampolines);
662