History

selftests/bpf: Add tests for rbtree API interaction in sleepable progs

Confirm that the following sleepable prog states fail verification: * bpf_rcu_read_unlock before bpf_spin_unlock * RCU CS will last at least as long as spin_lock CS Also confirm that correct usage passes verification, specifically: * Explicit use of bpf_rcu_read_{lock, unlock} in sleepable test prog * Implied RCU CS due to spin_lock CS None of the selftest progs actually attach to bpf_testmod's bpf_testmod_test_read.

Dave Marchevsky committed 3 years ago

Commit 312aa5b1 parent 5861d1e2 files · +99 −0Browse files

Unified · Split

2 files
  1. tools/testing/selftests/bpf/progs/refcounted_kptr.c +71 −0
  2. tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c +28 −0
tools/testing/selftests/bpf/progs/refcounted_kptr.c+71−0
@@ -8,6 +8,9 @@ · 7 unmodified lines
88 #include "bpf_misc.h"
99 #include "bpf_experimental.h"
1010
11+extern void bpf_rcu_read_lock(void) __ksym;
12+extern void bpf_rcu_read_unlock(void) __ksym;
13+
1114 struct node_data {
1215 long key;
1316 long list_data;
@@ -497,4 +500,72 @@ long rbtree_wrong_owner_remove_fail_a2(void *ctx) · 483 unmodified lines
497500 return 0;
498501 }
499502
503+SEC("?fentry.s/bpf_testmod_test_read")
504+__success
505+int BPF_PROG(rbtree_sleepable_rcu,
506+ struct file *file, struct kobject *kobj,
507+ struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
508+{
509+ struct bpf_rb_node *rb;
510+ struct node_data *n, *m = NULL;
511+
512+ n = bpf_obj_new(typeof(*n));
513+ if (!n)
514+ return 0;
515+
516+ bpf_rcu_read_lock();
517+ bpf_spin_lock(&lock);
518+ bpf_rbtree_add(&root, &n->r, less);
519+ rb = bpf_rbtree_first(&root);
520+ if (!rb)
521+ goto err_out;
522+
523+ rb = bpf_rbtree_remove(&root, rb);
524+ if (!rb)
525+ goto err_out;
526+
527+ m = container_of(rb, struct node_data, r);
528+
529+err_out:
530+ bpf_spin_unlock(&lock);
531+ bpf_rcu_read_unlock();
532+ if (m)
533+ bpf_obj_drop(m);
534+ return 0;
535+}
536+
537+SEC("?fentry.s/bpf_testmod_test_read")
538+__success
539+int BPF_PROG(rbtree_sleepable_rcu_no_explicit_rcu_lock,
540+ struct file *file, struct kobject *kobj,
541+ struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
542+{
543+ struct bpf_rb_node *rb;
544+ struct node_data *n, *m = NULL;
545+
546+ n = bpf_obj_new(typeof(*n));
547+ if (!n)
548+ return 0;
549+
550+ /* No explicit bpf_rcu_read_lock */
551+ bpf_spin_lock(&lock);
552+ bpf_rbtree_add(&root, &n->r, less);
553+ rb = bpf_rbtree_first(&root);
554+ if (!rb)
555+ goto err_out;
556+
557+ rb = bpf_rbtree_remove(&root, rb);
558+ if (!rb)
559+ goto err_out;
560+
561+ m = container_of(rb, struct node_data, r);
562+
563+err_out:
564+ bpf_spin_unlock(&lock);
565+ /* No explicit bpf_rcu_read_unlock */
566+ if (m)
567+ bpf_obj_drop(m);
568+ return 0;
569+}
570+
500571 char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c+28−0
@@ -13,6 +13,9 @@ struct node_acquire { · 12 unmodified lines
1313 struct bpf_refcount refcount;
1414 };
1515
16+extern void bpf_rcu_read_lock(void) __ksym;
17+extern void bpf_rcu_read_unlock(void) __ksym;
18+
1619 #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))
1720 private(A) struct bpf_spin_lock glock;
1821 private(A) struct bpf_rb_root groot __contains(node_acquire, node);
@@ -71,4 +74,29 @@ long rbtree_refcounted_node_ref_escapes_owning_input(void *ctx) · 52 unmodified lines
7174 return 0;
7275 }
7376
77+SEC("?fentry.s/bpf_testmod_test_read")
78+__failure __msg("function calls are not allowed while holding a lock")
79+int BPF_PROG(rbtree_fail_sleepable_lock_across_rcu,
80+ struct file *file, struct kobject *kobj,
81+ struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
82+{
83+ struct node_acquire *n;
84+
85+ n = bpf_obj_new(typeof(*n));
86+ if (!n)
87+ return 0;
88+
89+ /* spin_{lock,unlock} are in different RCU CS */
90+ bpf_rcu_read_lock();
91+ bpf_spin_lock(&glock);
92+ bpf_rbtree_add(&groot, &n->node, less);
93+ bpf_rcu_read_unlock();
94+
95+ bpf_rcu_read_lock();
96+ bpf_spin_unlock(&glock);
97+ bpf_rcu_read_unlock();
98+
99+ return 0;
100+}
101+
74102 char _license[] SEC("license") = "GPL";

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t