Commit 8477fe1d authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'bpf-fixes-for-maybe_wait_bpf_programs'

Hou Tao says:

====================
The patch set aims to fix the problems found when inspecting the code
related with maybe_wait_bpf_programs().

Patch #1 removes unnecessary invocation of maybe_wait_bpf_programs().
Patch #2 calls maybe_wait_bpf_programs() only once for batched update.
Patch #3 adds the missed waiting when doing batched lookup_deletion on
htab of maps. Patch #4 does wait only if the update or deletion
operation succeeds. Patch #5 fixes the value of batch.count when memory
allocation fails.
====================

Link: https://lore.kernel.org/r/20231208102355.2628918-1-houtao@huaweicloud.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 32fa0583 06e5c999
...@@ -203,7 +203,6 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file, ...@@ -203,7 +203,6 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,
rcu_read_unlock(); rcu_read_unlock();
} }
bpf_enable_instrumentation(); bpf_enable_instrumentation();
maybe_wait_bpf_programs(map);
return err; return err;
} }
...@@ -264,7 +263,6 @@ static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, ...@@ -264,7 +263,6 @@ static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
} }
bpf_enable_instrumentation(); bpf_enable_instrumentation();
maybe_wait_bpf_programs(map);
return err; return err;
} }
...@@ -1578,6 +1576,8 @@ static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr) ...@@ -1578,6 +1576,8 @@ static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
} }
err = bpf_map_update_value(map, f.file, key, value, attr->flags); err = bpf_map_update_value(map, f.file, key, value, attr->flags);
if (!err)
maybe_wait_bpf_programs(map);
kvfree(value); kvfree(value);
free_key: free_key:
...@@ -1633,7 +1633,8 @@ static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr) ...@@ -1633,7 +1633,8 @@ static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr)
err = map->ops->map_delete_elem(map, key); err = map->ops->map_delete_elem(map, key);
rcu_read_unlock(); rcu_read_unlock();
bpf_enable_instrumentation(); bpf_enable_instrumentation();
maybe_wait_bpf_programs(map); if (!err)
maybe_wait_bpf_programs(map);
out: out:
kvfree(key); kvfree(key);
err_put: err_put:
...@@ -1730,6 +1731,9 @@ int generic_map_delete_batch(struct bpf_map *map, ...@@ -1730,6 +1731,9 @@ int generic_map_delete_batch(struct bpf_map *map,
if (!max_count) if (!max_count)
return 0; return 0;
if (put_user(0, &uattr->batch.count))
return -EFAULT;
key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
if (!key) if (!key)
return -ENOMEM; return -ENOMEM;
...@@ -1759,7 +1763,6 @@ int generic_map_delete_batch(struct bpf_map *map, ...@@ -1759,7 +1763,6 @@ int generic_map_delete_batch(struct bpf_map *map,
kvfree(key); kvfree(key);
maybe_wait_bpf_programs(map);
return err; return err;
} }
...@@ -1787,6 +1790,9 @@ int generic_map_update_batch(struct bpf_map *map, struct file *map_file, ...@@ -1787,6 +1790,9 @@ int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
if (!max_count) if (!max_count)
return 0; return 0;
if (put_user(0, &uattr->batch.count))
return -EFAULT;
key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN); key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
if (!key) if (!key)
return -ENOMEM; return -ENOMEM;
...@@ -1817,6 +1823,7 @@ int generic_map_update_batch(struct bpf_map *map, struct file *map_file, ...@@ -1817,6 +1823,7 @@ int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
kvfree(value); kvfree(value);
kvfree(key); kvfree(key);
return err; return err;
} }
...@@ -5030,8 +5037,10 @@ static int bpf_map_do_batch(const union bpf_attr *attr, ...@@ -5030,8 +5037,10 @@ static int bpf_map_do_batch(const union bpf_attr *attr,
else else
BPF_DO_BATCH(map->ops->map_delete_batch, map, attr, uattr); BPF_DO_BATCH(map->ops->map_delete_batch, map, attr, uattr);
err_put: err_put:
if (has_write) if (has_write) {
maybe_wait_bpf_programs(map);
bpf_map_write_active_dec(map); bpf_map_write_active_dec(map);
}
fdput(f); fdput(f);
return err; return err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment