mbox series

[bpf-next,v2,00/16] bpf: Reduce the use of migrate_{disable|enable}()

Message ID 20250108010728.207536-1-houtao@huaweicloud.com (mailing list archive)
Headers show
Series bpf: Reduce the use of migrate_{disable|enable}() | expand

Message

Hou Tao Jan. 8, 2025, 1:07 a.m. UTC
From: Hou Tao <houtao1@huawei.com>

Hi,

The use of migrate_{disable|enable} pair in BPF is mainly due to the
introduction of bpf memory allocator and the use of per-CPU data struct
in its internal implementation. The caller needs to disable migration
before invoking the alloc or free APIs of bpf memory allocator, and
enable migration after the invocation.

The main users of bpf memory allocator are various kind of bpf maps in
which the map values or the special fields in the map values are
allocated by using bpf memory allocator.

At present, the running context for bpf program has already disabled
migration explictly or implictly, therefore, when these maps are
manipulated in bpf program, it is OK to not invoke migrate_disable()
and migrate_enable() pair. Howevers, it is not always the case when
these maps are manipulated through bpf syscall, therefore many
migrate_{disable|enable} pairs are added when the map can either be
manipulated by BPF program or BPF syscall.

The initial idea of reducing the use of migrate_{disable|enable} comes
from Alexei [1]. I turned it into a patch set that archives the goals
through the following three methods:

1. remove unnecessary migrate_{disable|enable} pair
when the BPF syscall path also disables migration, it is OK to remove
the pair. Patch #1~#3 fall into this category, while patch #4~#5 are
partially included.

2. move the migrate_{disable|enable} pair from inner callee to outer
   caller
Instead of invoking migrate_disable() in the inner callee, invoking
migrate_disable() in the outer caller to simplify reasoning about when
migrate_disable() is needed. Patch #4~#5 and patch #6~#19 belongs to
this category.

3. add cant_migrate() check in the inner callee
Add cant_migrate() check in the inner callee to ensure the guarantee
that migration is disabled is not broken. Patch #1~#5, #13, #16~#19 also
belong to this category.

Please check the individual patches for more details. Comments are
always welcome.

Change Log:
v2:
  * sqaush the ->map_free related patches (#10~#12, #15) into one patch
  * remove unnecessary cant_migrate() checks.

v1: https://lore.kernel.org/bpf/20250106081900.1665573-1-houtao@huaweicloud.com

Hou Tao (16):
  bpf: Remove migrate_{disable|enable} from LPM trie
  bpf: Remove migrate_{disable|enable} in ->map_for_each_callback
  bpf: Remove migrate_{disable|enable} in htab_elem_free
  bpf: Remove migrate_{disable|enable} from bpf_cgrp_storage_lock
    helpers
  bpf: Remove migrate_{disable|enable} from bpf_task_storage_lock
    helpers
  bpf: Disable migration when destroying inode storage
  bpf: Disable migration when destroying sock storage
  bpf: Disable migration when cloning sock storage
  bpf: Disable migration in bpf_selem_free_rcu
  bpf: Disable migration before calling ops->map_free()
  bpf: Remove migrate_{disable|enable} in bpf_obj_free_fields()
  bpf: Remove migrate_{disable,enable} in bpf_cpumask_release()
  bpf: Remove migrate_{disable|enable} from bpf_selem_alloc()
  bpf: Remove migrate_{disable|enable} from bpf_local_storage_alloc()
  bpf: Remove migrate_{disable|enable} from bpf_local_storage_free()
  bpf: Remove migrate_{disable|enable} from bpf_selem_free()

 kernel/bpf/arraymap.c          |  6 ++----
 kernel/bpf/bpf_cgrp_storage.c  | 15 +++++++--------
 kernel/bpf/bpf_inode_storage.c |  9 +++++----
 kernel/bpf/bpf_local_storage.c | 30 +++++++++---------------------
 kernel/bpf/bpf_task_storage.c  | 15 +++++++--------
 kernel/bpf/cpumask.c           |  2 --
 kernel/bpf/hashtab.c           | 19 +++++++------------
 kernel/bpf/helpers.c           |  4 ----
 kernel/bpf/lpm_trie.c          | 20 ++++----------------
 kernel/bpf/range_tree.c        |  2 --
 kernel/bpf/syscall.c           | 10 +++++++---
 net/core/bpf_sk_storage.c      | 11 +++++++----
 12 files changed, 55 insertions(+), 88 deletions(-)

Comments

patchwork-bot+netdevbpf@kernel.org Jan. 9, 2025, 2:20 a.m. UTC | #1
Hello:

This series was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Wed,  8 Jan 2025 09:07:12 +0800 you wrote:
> From: Hou Tao <houtao1@huawei.com>
> 
> Hi,
> 
> The use of migrate_{disable|enable} pair in BPF is mainly due to the
> introduction of bpf memory allocator and the use of per-CPU data struct
> in its internal implementation. The caller needs to disable migration
> before invoking the alloc or free APIs of bpf memory allocator, and
> enable migration after the invocation.
> 
> [...]

Here is the summary with links:
  - [bpf-next,v2,01/16] bpf: Remove migrate_{disable|enable} from LPM trie
    https://git.kernel.org/bpf/bpf-next/c/1b1a01db17af
  - [bpf-next,v2,02/16] bpf: Remove migrate_{disable|enable} in ->map_for_each_callback
    https://git.kernel.org/bpf/bpf-next/c/ea5b229630a6
  - [bpf-next,v2,03/16] bpf: Remove migrate_{disable|enable} in htab_elem_free
    https://git.kernel.org/bpf/bpf-next/c/53f2ba0b1cc0
  - [bpf-next,v2,04/16] bpf: Remove migrate_{disable|enable} from bpf_cgrp_storage_lock helpers
    https://git.kernel.org/bpf/bpf-next/c/25dc65f75b08
  - [bpf-next,v2,05/16] bpf: Remove migrate_{disable|enable} from bpf_task_storage_lock helpers
    https://git.kernel.org/bpf/bpf-next/c/9e6c958b5466
  - [bpf-next,v2,06/16] bpf: Disable migration when destroying inode storage
    https://git.kernel.org/bpf/bpf-next/c/e319cdc89566
  - [bpf-next,v2,07/16] bpf: Disable migration when destroying sock storage
    https://git.kernel.org/bpf/bpf-next/c/7d1032d1e303
  - [bpf-next,v2,08/16] bpf: Disable migration when cloning sock storage
    https://git.kernel.org/bpf/bpf-next/c/dfccfc47bde5
  - [bpf-next,v2,09/16] bpf: Disable migration in bpf_selem_free_rcu
    https://git.kernel.org/bpf/bpf-next/c/090d7f2e640b
  - [bpf-next,v2,10/16] bpf: Disable migration before calling ops->map_free()
    https://git.kernel.org/bpf/bpf-next/c/4b7e7cd1c105
  - [bpf-next,v2,11/16] bpf: Remove migrate_{disable|enable} in bpf_obj_free_fields()
    https://git.kernel.org/bpf/bpf-next/c/1d2dbe7120e8
  - [bpf-next,v2,12/16] bpf: Remove migrate_{disable,enable} in bpf_cpumask_release()
    https://git.kernel.org/bpf/bpf-next/c/6a52b965ab6f
  - [bpf-next,v2,13/16] bpf: Remove migrate_{disable|enable} from bpf_selem_alloc()
    https://git.kernel.org/bpf/bpf-next/c/2269b32ab00e
  - [bpf-next,v2,14/16] bpf: Remove migrate_{disable|enable} from bpf_local_storage_alloc()
    https://git.kernel.org/bpf/bpf-next/c/4855a75ebf48
  - [bpf-next,v2,15/16] bpf: Remove migrate_{disable|enable} from bpf_local_storage_free()
    https://git.kernel.org/bpf/bpf-next/c/7b984359e097
  - [bpf-next,v2,16/16] bpf: Remove migrate_{disable|enable} from bpf_selem_free()
    https://git.kernel.org/bpf/bpf-next/c/d86088e2c35d

You are awesome, thank you!