Skip to content

Commit 8f82583

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Reduce the scope of rcu_read_lock when updating fd map
There is no rcu-read-lock requirement for ops->map_fd_get_ptr() or ops->map_fd_put_ptr(), so doesn't use rcu-read-lock for these two callbacks. For bpf_fd_array_map_update_elem(), accessing array->ptrs doesn't need rcu-read-lock because array->ptrs must still be allocated. For bpf_fd_htab_map_update_elem(), htab_map_update_elem() only requires rcu-read-lock to be held to avoid the WARN_ON_ONCE(), so only use rcu_read_lock() during the invocation of htab_map_update_elem(). Acked-by: Yonghong Song <[email protected]> Signed-off-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2a0c6b4 commit 8f82583

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

kernel/bpf/hashtab.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,13 @@ int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
25232523
if (IS_ERR(ptr))
25242524
return PTR_ERR(ptr);
25252525

2526+
/* The htab bucket lock is always held during update operations in fd
2527+
* htab map, and the following rcu_read_lock() is only used to avoid
2528+
* the WARN_ON_ONCE in htab_map_update_elem().
2529+
*/
2530+
rcu_read_lock();
25262531
ret = htab_map_update_elem(map, key, &ptr, map_flags);
2532+
rcu_read_unlock();
25272533
if (ret)
25282534
map->ops->map_fd_put_ptr(map, ptr, false);
25292535

kernel/bpf/syscall.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,11 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,
184184
err = bpf_percpu_cgroup_storage_update(map, key, value,
185185
flags);
186186
} else if (IS_FD_ARRAY(map)) {
187-
rcu_read_lock();
188187
err = bpf_fd_array_map_update_elem(map, map_file, key, value,
189188
flags);
190-
rcu_read_unlock();
191189
} else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
192-
rcu_read_lock();
193190
err = bpf_fd_htab_map_update_elem(map, map_file, key, value,
194191
flags);
195-
rcu_read_unlock();
196192
} else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
197193
/* rcu_read_lock() is not needed */
198194
err = bpf_fd_reuseport_array_update_elem(map, key, value,

0 commit comments

Comments
 (0)