Skip to content

Commit

Permalink
bpf: follow idr code convention
Browse files Browse the repository at this point in the history
Generally we do a preload before doing idr allocation. This also help
improve the allocation success rate in memory pressure.

Cc: Daniel Borkmann <[email protected]>
Cc: Alexei Starovoitov <[email protected]>
Signed-off-by: Shaohua Li <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
shligit authored and borkmann committed Mar 27, 2018
1 parent 2abb5fa commit b76354c
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions kernel/bpf/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,13 @@ static int bpf_map_alloc_id(struct bpf_map *map)
{
int id;

idr_preload(GFP_KERNEL);
spin_lock_bh(&map_idr_lock);
id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
if (id > 0)
map->id = id;
spin_unlock_bh(&map_idr_lock);
idr_preload_end();

if (WARN_ON_ONCE(!id))
return -ENOSPC;
Expand Down Expand Up @@ -940,11 +942,13 @@ static int bpf_prog_alloc_id(struct bpf_prog *prog)
{
int id;

idr_preload(GFP_KERNEL);
spin_lock_bh(&prog_idr_lock);
id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
if (id > 0)
prog->aux->id = id;
spin_unlock_bh(&prog_idr_lock);
idr_preload_end();

/* id is in [1, INT_MAX) */
if (WARN_ON_ONCE(!id))
Expand Down

0 comments on commit b76354c

Please sign in to comment.