mbox series

[v4,bpf-next,00/14] bpf: Introduce bpf_mem_cache_free_rcu().

Message ID 20230706033447.54696-1-alexei.starovoitov@gmail.com (mailing list archive)
Headers show
Series bpf: Introduce bpf_mem_cache_free_rcu(). | expand

Message

Alexei Starovoitov July 6, 2023, 3:34 a.m. UTC
From: Alexei Starovoitov <ast@kernel.org>

v3->v4:
- extra patch 14 from Hou to check for object leaks.
- fixed the race/leak in free_by_rcu_ttrace. Extra hunk in patch 8.
- added Acks and fixed typos.

v2->v3:
- dropped _tail optimization for free_by_rcu_ttrace
- new patch 5 to refactor inc/dec of c->active
- change 'draining' logic in patch 7
- add rcu_barrier in patch 12
- __llist_add-> llist_add(waiting_for_gp_ttrace) in patch 9 to fix race
- David's Ack in patch 13 and explanation that migrate_disable cannot be removed just yet.

v1->v2:
- Fixed race condition spotted by Hou. Patch 7.

v1:

Introduce bpf_mem_cache_free_rcu() that is similar to kfree_rcu except
the objects will go through an additional RCU tasks trace grace period
before being freed into slab.

Patches 1-9 - a bunch of prep work
Patch 10 - a patch from Paul that exports rcu_request_urgent_qs_task().
Patch 12 - the main bpf_mem_cache_free_rcu patch.
Patch 13 - use it in bpf_cpumask.

bpf_local_storage, bpf_obj_drop, qp-trie will be other users eventually.

With additional hack patch to htab that replaces bpf_mem_cache_free with bpf_mem_cache_free_rcu
the following are benchmark results:
- map_perf_test 4 8 16348 1000000
drops from 800k to 600k. Waiting for RCU GP makes objects cache cold.

- bench htab-mem -a -p 8
20% drop in performance and big increase in memory. From 3 Mbyte to 50 Mbyte. As expected.

- bench htab-mem -a -p 16 --use-case add_del_on_diff_cpu
Same performance and better memory consumption.
Before these patches this bench would OOM (with or without 'reuse after GP')
Patch 8 addresses the issue.

At the end the performance drop and additional memory consumption due to _rcu()
were expected and came out to be within reasonable margin.
Without Paul's patch 10 the memory consumption in 'bench htab-mem' is in Gbytes
which wouldn't be acceptable.

Patch 8 is a heuristic to address 'alloc on one cpu, free on another' issue.
It works well in practice. One can probably construct an artificial benchmark
to make heuristic ineffective, but we have to trade off performance, code complexity,
and memory consumption.

The life cycle of objects:
alloc: dequeue free_llist
free: enqeueu free_llist
free_llist above high watermark -> free_by_rcu_ttrace
free_rcu: enqueue free_by_rcu -> waiting_for_gp
after RCU GP waiting_for_gp -> free_by_rcu_ttrace
free_by_rcu_ttrace -> waiting_for_gp_ttrace -> slab

Alexei Starovoitov (12):
  bpf: Rename few bpf_mem_alloc fields.
  bpf: Simplify code of destroy_mem_alloc() with kmemdup().
  bpf: Let free_all() return the number of freed elements.
  bpf: Refactor alloc_bulk().
  bpf: Factor out inc/dec of active flag into helpers.
  bpf: Further refactor alloc_bulk().
  bpf: Change bpf_mem_cache draining process.
  bpf: Add a hint to allocated objects.
  bpf: Allow reuse from waiting_for_gp_ttrace list.
  selftests/bpf: Improve test coverage of bpf_mem_alloc.
  bpf: Introduce bpf_mem_free_rcu() similar to kfree_rcu().
  bpf: Convert bpf_cpumask to bpf_mem_cache_free_rcu.

Hou Tao (1):
  bpf: Add object leak check.

Paul E. McKenney (1):
  rcu: Export rcu_request_urgent_qs_task()

 include/linux/bpf_mem_alloc.h                 |   2 +
 include/linux/rcutiny.h                       |   2 +
 include/linux/rcutree.h                       |   1 +
 kernel/bpf/cpumask.c                          |  20 +-
 kernel/bpf/memalloc.c                         | 378 +++++++++++++-----
 kernel/rcu/rcu.h                              |   2 -
 .../testing/selftests/bpf/progs/linked_list.c |   2 +-
 7 files changed, 298 insertions(+), 109 deletions(-)

Comments

patchwork-bot+netdevbpf@kernel.org July 12, 2023, 9:50 p.m. UTC | #1
Hello:

This series was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Wed,  5 Jul 2023 20:34:33 -0700 you wrote:
> From: Alexei Starovoitov <ast@kernel.org>
> 
> v3->v4:
> - extra patch 14 from Hou to check for object leaks.
> - fixed the race/leak in free_by_rcu_ttrace. Extra hunk in patch 8.
> - added Acks and fixed typos.
> 
> [...]

Here is the summary with links:
  - [v4,bpf-next,01/14] bpf: Rename few bpf_mem_alloc fields.
    https://git.kernel.org/bpf/bpf-next/c/12c8d0f4c870
  - [v4,bpf-next,02/14] bpf: Simplify code of destroy_mem_alloc() with kmemdup().
    https://git.kernel.org/bpf/bpf-next/c/a80672d7e10e
  - [v4,bpf-next,03/14] bpf: Let free_all() return the number of freed elements.
    https://git.kernel.org/bpf/bpf-next/c/9de3e81521b4
  - [v4,bpf-next,04/14] bpf: Refactor alloc_bulk().
    https://git.kernel.org/bpf/bpf-next/c/05ae68656a8e
  - [v4,bpf-next,05/14] bpf: Factor out inc/dec of active flag into helpers.
    https://git.kernel.org/bpf/bpf-next/c/18e027b1c7c6
  - [v4,bpf-next,06/14] bpf: Further refactor alloc_bulk().
    https://git.kernel.org/bpf/bpf-next/c/7468048237b8
  - [v4,bpf-next,07/14] bpf: Change bpf_mem_cache draining process.
    https://git.kernel.org/bpf/bpf-next/c/d114dde245f9
  - [v4,bpf-next,08/14] bpf: Add a hint to allocated objects.
    https://git.kernel.org/bpf/bpf-next/c/822fb26bdb55
  - [v4,bpf-next,09/14] bpf: Allow reuse from waiting_for_gp_ttrace list.
    https://git.kernel.org/bpf/bpf-next/c/04fabf00b4d3
  - [v4,bpf-next,10/14] rcu: Export rcu_request_urgent_qs_task()
    https://git.kernel.org/bpf/bpf-next/c/43a89baecfe2
  - [v4,bpf-next,11/14] selftests/bpf: Improve test coverage of bpf_mem_alloc.
    https://git.kernel.org/bpf/bpf-next/c/f76faa65c971
  - [v4,bpf-next,12/14] bpf: Introduce bpf_mem_free_rcu() similar to kfree_rcu().
    https://git.kernel.org/bpf/bpf-next/c/5af6807bdb10
  - [v4,bpf-next,13/14] bpf: Convert bpf_cpumask to bpf_mem_cache_free_rcu.
    https://git.kernel.org/bpf/bpf-next/c/8e07bb9ebcd9
  - [v4,bpf-next,14/14] bpf: Add object leak check.
    https://git.kernel.org/bpf/bpf-next/c/4ed8b5bcfada

You are awesome, thank you!