• Roman Gushchin's avatar
    bpf: Prepare for memcg-based memory accounting for bpf maps · 48edc1f7
    Roman Gushchin authored
    Bpf maps can be updated from an interrupt context and in such
    case there is no process which can be charged. It makes the memory
    accounting of bpf maps non-trivial.
    
    Fortunately, after commit 4127c650 ("mm: kmem: enable kernel
    memcg accounting from interrupt contexts") and commit b87d8cef
    ("mm, memcg: rework remote charging API to support nesting")
    it's finally possible.
    
    To make the ownership model simple and consistent, when the map
    is created, the memory cgroup of the current process is recorded.
    All subsequent allocations related to the bpf map are charged to
    the same memory cgroup. It includes allocations made by any processes
    (even if they do belong to a different cgroup) and from interrupts.
    
    This commit introduces 3 new helpers, which will be used by following
    commits to enable the accounting of bpf maps memory:
      - bpf_map_kmalloc_node()
      - bpf_map_kzalloc()
      - bpf_map_alloc_percpu()
    
    They are wrapping popular memory allocation functions. They set
    the active memory cgroup to the map's memory cgroup and add
    __GFP_ACCOUNT to the passed gfp flags. Then they call into
    the corresponding memory allocation function and restore
    the original active memory cgroup.
    
    These helpers are supposed to use everywhere except the map creation
    path. During the map creation when the map structure is allocated by
    itself, it cannot be passed to those helpers. In those cases default
    memory allocation function will be used with the __GFP_ACCOUNT flag.
    Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
    Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20201201215900.3569844-7-guro@fb.com
    48edc1f7
syscall.c 107 KB