History

bpf: Update locking comment in hashtab code

The comment where the bucket lock is acquired says: /* bpf_map_update_elem() can be called in_irq() */ which is not really helpful and aside of that it does not explain the subtle details of the hash bucket locks expecially in the context of BPF and perf, kprobes and tracing. Add a comment at the top of the file which explains the protection scopes and the details how potential deadlocks are prevented.

Thomas Gleixner committed 6 years ago

Commit dbca1511 parent 2ed905c1 file · +20 −4Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/bpf/hashtab.c+20−4
@@ -27,6 +27,26 @@ · 26 unmodified lines
2727 .map_delete_batch = \
2828 generic_map_delete_batch
2929
30+/*
31+ * The bucket lock has two protection scopes:
32+ *
33+ * 1) Serializing concurrent operations from BPF programs on differrent
34+ * CPUs
35+ *
36+ * 2) Serializing concurrent operations from BPF programs and sys_bpf()
37+ *
38+ * BPF programs can execute in any context including perf, kprobes and
39+ * tracing. As there are almost no limits where perf, kprobes and tracing
40+ * can be invoked from the lock operations need to be protected against
41+ * deadlocks. Deadlocks can be caused by recursion and by an invocation in
42+ * the lock held section when functions which acquire this lock are invoked
43+ * from sys_bpf(). BPF recursion is prevented by incrementing the per CPU
44+ * variable bpf_prog_active, which prevents BPF programs attached to perf
45+ * events, kprobes and tracing to be invoked before the prior invocation
46+ * from one of these contexts completed. sys_bpf() uses the same mechanism
47+ * by pinning the task to the current CPU and incrementing the recursion
48+ * protection accross the map operation.
49+ */
3050 struct bucket {
3151 struct hlist_nulls_head head;
3252 raw_spinlock_t lock;
@@ -884,7 +904,6 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value, · 851 unmodified lines
884904 */
885905 }
886906
887− /* bpf_map_update_elem() can be called in_irq() */
888907 raw_spin_lock_irqsave(&b->lock, flags);
889908
890909 l_old = lookup_elem_raw(head, hash, key, key_size);
@@ -964,7 +983,6 @@ static int htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value, · 73 unmodified lines
964983 return -ENOMEM;
965984 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
966985
967− /* bpf_map_update_elem() can be called in_irq() */
968986 raw_spin_lock_irqsave(&b->lock, flags);
969987
970988 l_old = lookup_elem_raw(head, hash, key, key_size);
@@ -1019,7 +1037,6 @@ static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key, · 48 unmodified lines
10191037 b = __select_bucket(htab, hash);
10201038 head = &b->head;
10211039
1022− /* bpf_map_update_elem() can be called in_irq() */
10231040 raw_spin_lock_irqsave(&b->lock, flags);
10241041
10251042 l_old = lookup_elem_raw(head, hash, key, key_size);
@@ -1083,7 +1100,6 @@ static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key, · 57 unmodified lines
10831100 return -ENOMEM;
10841101 }
10851102
1086− /* bpf_map_update_elem() can be called in_irq() */
10871103 raw_spin_lock_irqsave(&b->lock, flags);
10881104
10891105 l_old = lookup_elem_raw(head, hash, key, key_size);

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