Commit b76354cd authored by Shaohua Li's avatar Shaohua Li Committed by Daniel Borkmann

bpf: follow idr code convention

Generally we do a preload before doing idr allocation. This also help
improve the allocation success rate in memory pressure.

Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarShaohua Li <shli@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 2abb5fad
...@@ -203,11 +203,13 @@ static int bpf_map_alloc_id(struct bpf_map *map) ...@@ -203,11 +203,13 @@ static int bpf_map_alloc_id(struct bpf_map *map)
{ {
int id; int id;
idr_preload(GFP_KERNEL);
spin_lock_bh(&map_idr_lock); spin_lock_bh(&map_idr_lock);
id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC); id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
if (id > 0) if (id > 0)
map->id = id; map->id = id;
spin_unlock_bh(&map_idr_lock); spin_unlock_bh(&map_idr_lock);
idr_preload_end();
if (WARN_ON_ONCE(!id)) if (WARN_ON_ONCE(!id))
return -ENOSPC; return -ENOSPC;
...@@ -940,11 +942,13 @@ static int bpf_prog_alloc_id(struct bpf_prog *prog) ...@@ -940,11 +942,13 @@ static int bpf_prog_alloc_id(struct bpf_prog *prog)
{ {
int id; int id;
idr_preload(GFP_KERNEL);
spin_lock_bh(&prog_idr_lock); spin_lock_bh(&prog_idr_lock);
id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC); id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
if (id > 0) if (id > 0)
prog->aux->id = id; prog->aux->id = id;
spin_unlock_bh(&prog_idr_lock); spin_unlock_bh(&prog_idr_lock);
idr_preload_end();
/* id is in [1, INT_MAX) */ /* id is in [1, INT_MAX) */
if (WARN_ON_ONCE(!id)) if (WARN_ON_ONCE(!id))
......
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