xref: /linux/tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
16147f151SDave Marchevsky // SPDX-License-Identifier: GPL-2.0
26147f151SDave Marchevsky #include <vmlinux.h>
36147f151SDave Marchevsky #include <bpf/bpf_tracing.h>
46147f151SDave Marchevsky #include <bpf/bpf_helpers.h>
56147f151SDave Marchevsky #include <bpf/bpf_core_read.h>
66147f151SDave Marchevsky #include "bpf_experimental.h"
76147f151SDave Marchevsky #include "bpf_misc.h"
86147f151SDave Marchevsky 
96147f151SDave Marchevsky struct node_acquire {
106147f151SDave Marchevsky 	long key;
116147f151SDave Marchevsky 	long data;
126147f151SDave Marchevsky 	struct bpf_rb_node node;
136147f151SDave Marchevsky 	struct bpf_refcount refcount;
146147f151SDave Marchevsky };
156147f151SDave Marchevsky 
16312aa5bdSDave Marchevsky extern void bpf_rcu_read_lock(void) __ksym;
17312aa5bdSDave Marchevsky extern void bpf_rcu_read_unlock(void) __ksym;
18312aa5bdSDave Marchevsky 
196147f151SDave Marchevsky #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))
206147f151SDave Marchevsky private(A) struct bpf_spin_lock glock;
216147f151SDave Marchevsky private(A) struct bpf_rb_root groot __contains(node_acquire, node);
226147f151SDave Marchevsky 
less(struct bpf_rb_node * a,const struct bpf_rb_node * b)236147f151SDave Marchevsky static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b)
246147f151SDave Marchevsky {
256147f151SDave Marchevsky 	struct node_acquire *node_a;
266147f151SDave Marchevsky 	struct node_acquire *node_b;
276147f151SDave Marchevsky 
286147f151SDave Marchevsky 	node_a = container_of(a, struct node_acquire, node);
296147f151SDave Marchevsky 	node_b = container_of(b, struct node_acquire, node);
306147f151SDave Marchevsky 
316147f151SDave Marchevsky 	return node_a->key < node_b->key;
326147f151SDave Marchevsky }
336147f151SDave Marchevsky 
346147f151SDave Marchevsky SEC("?tc")
35*f8d16175SEduard Zingerman __failure __msg("Unreleased reference id=4 alloc_insn={{[0-9]+}}")
rbtree_refcounted_node_ref_escapes(void * ctx)366147f151SDave Marchevsky long rbtree_refcounted_node_ref_escapes(void *ctx)
376147f151SDave Marchevsky {
386147f151SDave Marchevsky 	struct node_acquire *n, *m;
396147f151SDave Marchevsky 
406147f151SDave Marchevsky 	n = bpf_obj_new(typeof(*n));
416147f151SDave Marchevsky 	if (!n)
426147f151SDave Marchevsky 		return 1;
436147f151SDave Marchevsky 
446147f151SDave Marchevsky 	bpf_spin_lock(&glock);
456147f151SDave Marchevsky 	bpf_rbtree_add(&groot, &n->node, less);
466147f151SDave Marchevsky 	/* m becomes an owning ref but is never drop'd or added to a tree */
476147f151SDave Marchevsky 	m = bpf_refcount_acquire(n);
486147f151SDave Marchevsky 	bpf_spin_unlock(&glock);
497793fc3bSDave Marchevsky 	if (!m)
507793fc3bSDave Marchevsky 		return 2;
516147f151SDave Marchevsky 
526147f151SDave Marchevsky 	m->key = 2;
536147f151SDave Marchevsky 	return 0;
546147f151SDave Marchevsky }
556147f151SDave Marchevsky 
566147f151SDave Marchevsky SEC("?tc")
57f460e7bdSDave Marchevsky __failure __msg("Possibly NULL pointer passed to trusted arg0")
refcount_acquire_maybe_null(void * ctx)58f460e7bdSDave Marchevsky long refcount_acquire_maybe_null(void *ctx)
59f460e7bdSDave Marchevsky {
60f460e7bdSDave Marchevsky 	struct node_acquire *n, *m;
61f460e7bdSDave Marchevsky 
62f460e7bdSDave Marchevsky 	n = bpf_obj_new(typeof(*n));
63f460e7bdSDave Marchevsky 	/* Intentionally not testing !n
64f460e7bdSDave Marchevsky 	 * it's MAYBE_NULL for refcount_acquire
65f460e7bdSDave Marchevsky 	 */
66f460e7bdSDave Marchevsky 	m = bpf_refcount_acquire(n);
67f460e7bdSDave Marchevsky 	if (m)
68f460e7bdSDave Marchevsky 		bpf_obj_drop(m);
69f460e7bdSDave Marchevsky 	if (n)
70f460e7bdSDave Marchevsky 		bpf_obj_drop(n);
71f460e7bdSDave Marchevsky 
72f460e7bdSDave Marchevsky 	return 0;
73f460e7bdSDave Marchevsky }
74f460e7bdSDave Marchevsky 
75f460e7bdSDave Marchevsky SEC("?tc")
76*f8d16175SEduard Zingerman __failure __msg("Unreleased reference id=3 alloc_insn={{[0-9]+}}")
rbtree_refcounted_node_ref_escapes_owning_input(void * ctx)776147f151SDave Marchevsky long rbtree_refcounted_node_ref_escapes_owning_input(void *ctx)
786147f151SDave Marchevsky {
796147f151SDave Marchevsky 	struct node_acquire *n, *m;
806147f151SDave Marchevsky 
816147f151SDave Marchevsky 	n = bpf_obj_new(typeof(*n));
826147f151SDave Marchevsky 	if (!n)
836147f151SDave Marchevsky 		return 1;
846147f151SDave Marchevsky 
856147f151SDave Marchevsky 	/* m becomes an owning ref but is never drop'd or added to a tree */
866147f151SDave Marchevsky 	m = bpf_refcount_acquire(n);
876147f151SDave Marchevsky 	m->key = 2;
886147f151SDave Marchevsky 
896147f151SDave Marchevsky 	bpf_spin_lock(&glock);
906147f151SDave Marchevsky 	bpf_rbtree_add(&groot, &n->node, less);
916147f151SDave Marchevsky 	bpf_spin_unlock(&glock);
926147f151SDave Marchevsky 
936147f151SDave Marchevsky 	return 0;
946147f151SDave Marchevsky }
956147f151SDave Marchevsky 
96312aa5bdSDave Marchevsky SEC("?fentry.s/bpf_testmod_test_read")
97312aa5bdSDave Marchevsky __failure __msg("function calls are not allowed while holding a lock")
BPF_PROG(rbtree_fail_sleepable_lock_across_rcu,struct file * file,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t len)98312aa5bdSDave Marchevsky int BPF_PROG(rbtree_fail_sleepable_lock_across_rcu,
99312aa5bdSDave Marchevsky 	     struct file *file, struct kobject *kobj,
100312aa5bdSDave Marchevsky 	     struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
101312aa5bdSDave Marchevsky {
102312aa5bdSDave Marchevsky 	struct node_acquire *n;
103312aa5bdSDave Marchevsky 
104312aa5bdSDave Marchevsky 	n = bpf_obj_new(typeof(*n));
105312aa5bdSDave Marchevsky 	if (!n)
106312aa5bdSDave Marchevsky 		return 0;
107312aa5bdSDave Marchevsky 
108312aa5bdSDave Marchevsky 	/* spin_{lock,unlock} are in different RCU CS */
109312aa5bdSDave Marchevsky 	bpf_rcu_read_lock();
110312aa5bdSDave Marchevsky 	bpf_spin_lock(&glock);
111312aa5bdSDave Marchevsky 	bpf_rbtree_add(&groot, &n->node, less);
112312aa5bdSDave Marchevsky 	bpf_rcu_read_unlock();
113312aa5bdSDave Marchevsky 
114312aa5bdSDave Marchevsky 	bpf_rcu_read_lock();
115312aa5bdSDave Marchevsky 	bpf_spin_unlock(&glock);
116312aa5bdSDave Marchevsky 	bpf_rcu_read_unlock();
117312aa5bdSDave Marchevsky 
118312aa5bdSDave Marchevsky 	return 0;
119312aa5bdSDave Marchevsky }
120312aa5bdSDave Marchevsky 
1216147f151SDave Marchevsky char _license[] SEC("license") = "GPL";
122