Skip to content

Commit 8d5e2de

Browse files
htbeginKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
bpf: Overwrite the element in hash map atomically
Currently, the update of existing element in hash map involves two steps: 1) insert the new element at the head of the hash list 2) remove the old element It is possible that the concurrent lookup operation may fail to find either the old element or the new element if the lookup operation starts before the addition and continues after the removal. Therefore, replacing the two-step update with an atomic update. After the change, the update will be atomic in the perspective of the lookup operation: it will either find the old element or the new element. Signed-off-by: Hou Tao <[email protected]>
1 parent 06ab8b5 commit 8d5e2de

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

kernel/bpf/hashtab.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -1179,12 +1179,14 @@ static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
11791179
goto err;
11801180
}
11811181

1182-
/* add new element to the head of the list, so that
1183-
* concurrent search will find it before old elem
1184-
*/
1185-
hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1186-
if (l_old) {
1187-
hlist_nulls_del_rcu(&l_old->hash_node);
1182+
if (!l_old) {
1183+
hlist_nulls_add_head_rcu(&l_new->hash_node, head);
1184+
} else {
1185+
/* Replace the old element atomically, so that
1186+
* concurrent search will find either the new element or
1187+
* the old element.
1188+
*/
1189+
hlist_nulls_replace_rcu(&l_new->hash_node, &l_old->hash_node);
11881190

11891191
/* l_old has already been stashed in htab->extra_elems, free
11901192
* its special fields before it is available for reuse. Also

0 commit comments

Comments
 (0)