]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm, bpf: Use memcg in try_alloc_pages().
authorAlexei Starovoitov <ast@kernel.org>
Sat, 22 Feb 2025 02:44:26 +0000 (18:44 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 27 Feb 2025 17:39:44 +0000 (09:39 -0800)
Unconditionally use __GFP_ACCOUNT in try_alloc_pages().
The caller is responsible to setup memcg correctly.
All BPF memory accounting is memcg based.

Acked-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/r/20250222024427.30294-6-alexei.starovoitov@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
mm/page_alloc.c

index 52836eb65032547480ab0660a8c0ebde7281d4cf..a0904315d4da2b776839c07c42d85c84dc0d6c19 100644 (file)
@@ -7190,7 +7190,8 @@ struct page *try_alloc_pages_noprof(int nid, unsigned int order)
         * specify it here to highlight that try_alloc_pages()
         * doesn't want to deplete reserves.
         */
-       gfp_t alloc_gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_NOMEMALLOC;
+       gfp_t alloc_gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_NOMEMALLOC
+                       | __GFP_ACCOUNT;
        unsigned int alloc_flags = ALLOC_TRYLOCK;
        struct alloc_context ac = { };
        struct page *page;
@@ -7234,6 +7235,11 @@ struct page *try_alloc_pages_noprof(int nid, unsigned int order)
 
        /* Unlike regular alloc_pages() there is no __alloc_pages_slowpath(). */
 
+       if (memcg_kmem_online() && page &&
+           unlikely(__memcg_kmem_charge_page(page, alloc_gfp, order) != 0)) {
+               free_pages_nolock(page, order);
+               page = NULL;
+       }
        trace_mm_page_alloc(page, order, alloc_gfp, ac.migratetype);
        kmsan_alloc_page(page, order, alloc_gfp);
        return page;