From patchwork Fri Jul 29 15:23:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12932541 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 69A64C00144 for ; Fri, 29 Jul 2022 15:23:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3C9B6B0082; Fri, 29 Jul 2022 11:23:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E9F786B0083; Fri, 29 Jul 2022 11:23:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1C378E0001; Fri, 29 Jul 2022 11:23:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C3A896B0082 for ; Fri, 29 Jul 2022 11:23:45 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9B0DCAB332 for ; Fri, 29 Jul 2022 15:23:45 +0000 (UTC) X-FDA: 79740507210.17.E82FB33 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf11.hostedemail.com (Postfix) with ESMTP id 3C4EC40041 for ; Fri, 29 Jul 2022 15:23:45 +0000 (UTC) Received: by mail-pj1-f46.google.com with SMTP id w63-20020a17090a6bc500b001f3160a6011so6814536pjj.5 for ; Fri, 29 Jul 2022 08:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Z/tZbl83Zz5RN5ZtLnfBVgtLVcF0QM8whQKykqg7AFs=; b=Odn0BIB7DUWvvQYR+O232jYQnTPeV1M0T4uHezkKllPsrtVCWuN6AzQ9mmkzucvR59 NX5o5yZSdNiIqSyZzPWhgHumWOjIJL+SKOtj8RlgwBtnTHMgLQhYipVDoMc5ijk8yzqj 8VrpnerKwh4TL5/YY4y9Q3NIOzSrvZoT2ZrhHkofgszttpTtHSgak+49Gyl7kIyMrLo5 g72j99GhmqgfeRYM72OCw+fhCkgTDLLIEUg36nSIcn58t1eyOlb4mYDULoC8UEz12ewD eC5PlwZh3NoqqPlJW8eZedRUR/d9FhBuaStavi861xJUBbKYcZykH8Kgu2gga/dsQNdg d2QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Z/tZbl83Zz5RN5ZtLnfBVgtLVcF0QM8whQKykqg7AFs=; b=XH6BSJrVV2LWliq/5dOPOipwRfJi5la3d/PzCsVz0fuuHEtGZ0XRzpwOMU3IAwdXke zNI6hfi1Xqer119kVS7frWuccHZFApXVZFQMIk4bCPL4uY8GKTCndTT0dstlmDtdv4tP 18/gi1tfFtwU2tLk0CCM5tp4S6tqJPeO479GFMJQBS+KSMOIHDm4wnU/5hH+Vl9j5BiR 7z7UcqTbTEQgxd7wZlspsOoCG+hLPE3nFl3UuKtfNeqw/leqSMqQ3t6ZVzd42tMYQKqM bayNm534nXI6xhCb51kf4+ln1+TpM7BbEkKzUVgNyCN7NZirKMP9sFjgFQsCtnKcXtsq PmEw== X-Gm-Message-State: ACgBeo0CeCIEYAeCz45ZW4YO5f9Xezq0NCekjoOecfMQhVtU6hLQtarV HGK2zPMZ2JsPiq8DTg8W2EA= X-Google-Smtp-Source: AA6agR7BcQrR/Mgac/I67FnC6AY4rn3lQ3Gnc2LbnKyRmp4ej47lpNm/jWkrdbAsyc1iR3dwwKRDNQ== X-Received: by 2002:a17:902:8502:b0:16c:c5c5:a198 with SMTP id bj2-20020a170902850200b0016cc5c5a198mr4366289plb.88.1659108224367; Fri, 29 Jul 2022 08:23:44 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:2912:5400:4ff:fe16:4344]) by smtp.gmail.com with ESMTPSA id b12-20020a1709027e0c00b0016d3a354cffsm3714219plm.89.2022.07.29.08.23.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Jul 2022 08:23:43 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, songmuchun@bytedance.com, akpm@linux-foundation.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [RFC PATCH bpf-next 11/15] bpf: Use bpf_map_kvcalloc in bpf_local_storage Date: Fri, 29 Jul 2022 15:23:12 +0000 Message-Id: <20220729152316.58205-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220729152316.58205-1-laoar.shao@gmail.com> References: <20220729152316.58205-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Odn0BIB7; spf=pass (imf11.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1659108225; a=rsa-sha256; cv=none; b=JSJFEXIWfjxGe3/hYt47ms/TFTc3tHAxS98miImNlJJLUj0h0LOQ9fab+cAMPLsY2KTW3y snOgv5sxrP837GrdF3ktPo8tiXj28mDuSTL15God132QrqXo4SCsW7wESuLIhn667ebGbu UWxgVkKPelfE/qup638sil43IljtQcQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1659108225; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Z/tZbl83Zz5RN5ZtLnfBVgtLVcF0QM8whQKykqg7AFs=; b=UCdK1y24QG7mr0O0D33A4dwuve9Q50DpTQyoPFDGuIJh6/yzsw5fYPdvDrAo7mvOzIKqKO /q96AcMogDMYoVSu/b1DHIrHUouKdWl6RdOygDm00Ybnf5j97xJ/rhEyfEz5eiSU9Krfeo LEW1PBTe17eq5R/SQPTGHCK69iA7v/8= Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Odn0BIB7; spf=pass (imf11.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3C4EC40041 X-Stat-Signature: defqgaqtdsd41p1gmrooofy8ukeq89ri X-HE-Tag: 1659108225-800088 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Introduce new helper bpf_map_kvcalloc() for this memory allocation. Signed-off-by: Yafang Shao --- include/linux/bpf.h | 8 ++++++++ kernel/bpf/bpf_local_storage.c | 4 ++-- kernel/bpf/syscall.c | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4af72d2b6d73..605214fedd6d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1662,6 +1662,8 @@ struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id); void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags, int node); void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags); +void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size, + gfp_t flags); void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align, gfp_t flags); #else @@ -1678,6 +1680,12 @@ bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags) return kzalloc(size, flags); } +static inline void * +bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size, gfp_t flags) +{ + return kvcalloc(n, size, flags); +} + static inline void __percpu * bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align, gfp_t flags) diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index 0b50cb2e1d5b..3440135c3612 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -620,8 +620,8 @@ struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr) nbuckets = max_t(u32, 2, nbuckets); smap->bucket_log = ilog2(nbuckets); - smap->buckets = kvcalloc(sizeof(*smap->buckets), nbuckets, - GFP_USER | __GFP_NOWARN | __GFP_ACCOUNT); + smap->buckets = bpf_map_kvcalloc(&smap->map, sizeof(*smap->buckets), + nbuckets, GFP_USER | __GFP_NOWARN); if (!smap->buckets) { bpf_map_container_free(smap); return ERR_PTR(-ENOMEM); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 5c13782839f3..f7210fa8c3be 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -455,6 +455,20 @@ void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags) return ptr; } +void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size, + gfp_t flags) +{ + struct mem_cgroup *memcg, *old_memcg; + void *ptr; + + memcg = bpf_map_get_memcg(map); + old_memcg = set_active_memcg(memcg); + ptr = kvcalloc(n, size, flags | __GFP_ACCOUNT); + set_active_memcg(old_memcg); + + return ptr; +} + void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align, gfp_t flags) {