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
27 .map_delete_batch = \27 .map_delete_batch = \
28 generic_map_delete_batch28 generic_map_delete_batch
29 29
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 */
30struct bucket {50struct bucket {
31 struct hlist_nulls_head head;51 struct hlist_nulls_head head;
32 raw_spinlock_t lock;52 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
884 */904 */
885 }905 }
886 906
887 /* bpf_map_update_elem() can be called in_irq() */
888 raw_spin_lock_irqsave(&b->lock, flags);907 raw_spin_lock_irqsave(&b->lock, flags);
889 908
890 l_old = lookup_elem_raw(head, hash, key, key_size);909 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
964 return -ENOMEM;983 return -ENOMEM;
965 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);984 memcpy(l_new->key + round_up(map->key_size, 8), value, map->value_size);
966 985
967 /* bpf_map_update_elem() can be called in_irq() */
968 raw_spin_lock_irqsave(&b->lock, flags);986 raw_spin_lock_irqsave(&b->lock, flags);
969 987
970 l_old = lookup_elem_raw(head, hash, key, key_size);988 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
1019 b = __select_bucket(htab, hash);1037 b = __select_bucket(htab, hash);
1020 head = &b->head;1038 head = &b->head;
1021 1039
1022 /* bpf_map_update_elem() can be called in_irq() */
1023 raw_spin_lock_irqsave(&b->lock, flags);1040 raw_spin_lock_irqsave(&b->lock, flags);
1024 1041
1025 l_old = lookup_elem_raw(head, hash, key, key_size);1042 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
1083 return -ENOMEM;1100 return -ENOMEM;
1084 }1101 }
1085 1102
1086 /* bpf_map_update_elem() can be called in_irq() */
1087 raw_spin_lock_irqsave(&b->lock, flags);1103 raw_spin_lock_irqsave(&b->lock, flags);
1088 1104
1089 l_old = lookup_elem_raw(head, hash, key, key_size);1105 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