From patchwork Wed Aug 10 15:18:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12940676 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 C2587C00140 for ; Wed, 10 Aug 2022 15:19:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 439936B0072; Wed, 10 Aug 2022 11:19:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C17F6B0075; Wed, 10 Aug 2022 11:19:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 213DD6B0078; Wed, 10 Aug 2022 11:19:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0E34F6B0072 for ; Wed, 10 Aug 2022 11:19:23 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CBD211C6D2D for ; Wed, 10 Aug 2022 15:19:22 +0000 (UTC) X-FDA: 79784041764.08.436322C Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf28.hostedemail.com (Postfix) with ESMTP id B1001C0161 for ; Wed, 10 Aug 2022 15:19:08 +0000 (UTC) Received: by mail-pf1-f169.google.com with SMTP id p125so10453535pfp.2 for ; Wed, 10 Aug 2022 08:19:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=L21h4+LhKzVbOgymwJqIY9/nxSMZ9tbw+WqBvebpAHo=; b=KeXYWGzf22r6tp6AGlXKgfkYSLL92qFn/v0xzLOawzsAZ7dKaX1F920h1kIiDeKqy0 tIHMdiZFS147YlQvLzXvGD77IukqdqITM58J2OHO510EF6687XIPaU1ye2Teqep5Rcmj 2wR4anFZfqV3I/hi2S8GmdWxvXYkIQKh4Assq6E3oFz8i40BkflACjOeQKb6uw/dppMd KicCGJhhWGMlTNJUfj25v8cpn/3/8GdksrHDBln/s9HeC2TLRj7cvg11mtdNrvVrLQtm FYJtK1KD+fEhK0mwIqsAjP9QcBv4TY3vPmhmiMxdZ2nooUKbEuseAnZ20wQYspkh1wNG YYqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=L21h4+LhKzVbOgymwJqIY9/nxSMZ9tbw+WqBvebpAHo=; b=UcLMnIIlQd1SrlhQol81h2NNBKlmy53U2udrGYI2fgv5DOSaSAYLzTPUw+Xg7KK2YF C6WEw8GeU8WrjJdkCXLJCObXUwcFdLU865Ng9q8ZGBp9jOOW+Z3yYCV6e6HM7hGpjrCx rtR8ywxqzJZOVmL9tB3S3QqPfmQmeR4bOq60gHdzHwR3luuEposcVE7Wm8hQOFuP1sjE qZGDtROF21hMpi1QyDYTWfk0XRh1/hG8RgnZbpQROiASKsvfbKRVMU0+B2zWBvc7+9Wm /4UKK2glfxkU3fPx9EPoICWqjim1tmt12pi+R44793+h/McYBb8sC4hxUo28t8RMsAk2 SwcQ== X-Gm-Message-State: ACgBeo3zy1wHmBVaxzAfy2Wss7O+MWQEvRdbkKsJDCG9z0An04Bb8PX/ bo1cpJVra+uElpiYJC1s8So= X-Google-Smtp-Source: AA6agR4Qm1piYZUT+NZjX322Lv/JWEoiqSZroQJTNfAvHnmXAGUoEZ2XBS2sQ0tRlxEBO/vP2ie4Ag== X-Received: by 2002:a05:6a00:1706:b0:52f:6f75:991b with SMTP id h6-20020a056a00170600b0052f6f75991bmr12147620pfc.34.1660144747830; Wed, 10 Aug 2022 08:19:07 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:5c3e:5400:4ff:fe19:c3bc]) by smtp.gmail.com with ESMTPSA id 85-20020a621558000000b0052b6ed5ca40sm2071935pfv.192.2022.08.10.08.19.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Aug 2022 08:19:07 -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: [PATCH bpf-next 12/15] bpf: Use bpf_map_kvcalloc in bpf_local_storage Date: Wed, 10 Aug 2022 15:18:37 +0000 Message-Id: <20220810151840.16394-13-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220810151840.16394-1-laoar.shao@gmail.com> References: <20220810151840.16394-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660144760; a=rsa-sha256; cv=none; b=7Gjny7RZCKXIDff7Rs/2HFGWaiSVxnnL7OSkAqbrbuFn/UYoAEgvC5aONpqYJodZPV7eRr Ux3mgnkQTYODIg/LWDe1JoWlbc28P3Ev/dGMLt1Q7IrZnEL8xF33AD/S5vXR392crQH57G dxDly1Gj83RjoZQflcrYDWTIw31GoZ4= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=KeXYWGzf; dmarc=temperror reason="query timed out" header.from=gmail.com (policy=temperror); spf=pass (imf28.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660144760; 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=L21h4+LhKzVbOgymwJqIY9/nxSMZ9tbw+WqBvebpAHo=; b=nzomqzGdnCdFBx/v+eWAjvXs3/FXNLSVrijObyKwZUj5QqGyyj3E/EjZvJ2Ka8a7Kw78pe 92max22ahjuA54OgSz9K04mgi8bsdVqMEWAUOHLdXLe6Wv4RNhlJVRphGRXXkq7N7r4MiK ZVyCvDjf+khGmiGUGiX2/oRtX8yoLHU= X-Rspamd-Server: rspam12 X-Rspam-User: X-Rspamd-Queue-Id: B1001C0161 Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=KeXYWGzf; dmarc=temperror reason="query timed out" header.from=gmail.com (policy=temperror); spf=pass (imf28.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Stat-Signature: 7rhtaast7mq15rf4b5hkp65mdydpzngk X-HE-Tag: 1660144748-649979 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 | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index d7485b7..ebb6ed4 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1656,6 +1656,8 @@ int generic_map_delete_batch(struct bpf_map *map, 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 @@ -1672,6 +1674,12 @@ void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, 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 67ab249..71d5bf1 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_area_free(smap, &smap->map); return ERR_PTR(-ENOMEM); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 02ce7e9..2bd3bcf 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -489,6 +489,21 @@ 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); + bpf_map_put_memcg(memcg); + + return ptr; +} + void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align, gfp_t flags) {