From patchwork Wed Sep 21 17:00:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12984030 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 AECFEC6FA82 for ; Wed, 21 Sep 2022 17:00:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 453356B0080; Wed, 21 Sep 2022 13:00:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DD206B0081; Wed, 21 Sep 2022 13:00:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E1446B0082; Wed, 21 Sep 2022 13:00:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 0FD5A6B0080 for ; Wed, 21 Sep 2022 13:00:27 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9A62D1A10F5 for ; Wed, 21 Sep 2022 17:00:26 +0000 (UTC) X-FDA: 79936706052.19.54D78D0 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf10.hostedemail.com (Postfix) with ESMTP id 4181CC009C for ; Wed, 21 Sep 2022 17:00:26 +0000 (UTC) Received: by mail-pl1-f181.google.com with SMTP id iw17so6330167plb.0 for ; Wed, 21 Sep 2022 10:00:26 -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:subject:date; bh=86Fvvo7OHCl7WcRNmebHsC+UjHYkXcsdLASZGPmT+6Y=; b=Oq03LOMMFgsLNPoDix5xEX0x1FdtZZSvbxGuAYQ4ywUrDpKo4H2L8SHRgi01pOHZI8 vLbrDrut8/Szy52XWOK7vDIIy3EtGQdHA9FBQF6IoiRRpX8JeE1QjlLCYNVvi6lw8Ykn HjT9HSRJkSulSHbK4VhpWAJX8JBZHYvYWRo6w656Tnag7jwqL4C95a5GOJtRqq2+wM/u DmXP37nSBNVxJ9+qKBqGAYksqrkHOPYy/iYLPse5wd3DfoXkxKfraUH5agPIJ3iXwvPX SO065VFBaLWKhXyu20BZbWN6OcjgtdsyQVLmCwBy8e3IlypTydv7n8tKF+YY2CWAU8iM FsAg== 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 :subject:date; bh=86Fvvo7OHCl7WcRNmebHsC+UjHYkXcsdLASZGPmT+6Y=; b=lM1ZrddhWSF3xqB1RTFpEweLaKLEscMwhNoJZC5wKoXLHK0EaPpfN5BJzNnuvGuL06 r96uIZtLW9+Ups5GqYX0IDJZbQDwB5gVHhj2PM5dLsDcEBkxUDWT8sLw8nOME0n5jVBR N1HJk6bahamZyS0S5mklWuABKVC7wDb4BIWR+3EljbDPyDj9R7ZcL4ID0mfm0jdAFrh+ 2A5sZdj2xqGBKrSvNquFFRRhw4dNHqTXEmwdHs8qrwhYkF2Vm9ZMksAmp7QaALJbPzZB H5a/TWHB1GnMnae4R5l8odhYgKlZy9vqZDzi2dd17ceqAeI076Tx2/TWuHKQwuPXeVFe ftqA== X-Gm-Message-State: ACrzQf3qCnk+AvUaVlm2OH/97MhE1K8aVhT9yfeUYAHlbeMniLpGzXRB 7sco/APTWixm5LT7RlTHqbw= X-Google-Smtp-Source: AMsMyM71OEz6FCG+w0oFSQ37gXstmmjQLulm8CE9fLNx0xAKUpv8TdHR3SpgktqUbz5zKADwFjdWDw== X-Received: by 2002:a17:90b:1e49:b0:200:6d41:9662 with SMTP id pi9-20020a17090b1e4900b002006d419662mr10364539pjb.221.1663779625342; Wed, 21 Sep 2022 10:00:25 -0700 (PDT) Received: from vultr.guest ([2001:19f0:6001:488e:5400:4ff:fe25:7db8]) by smtp.gmail.com with ESMTPSA id mp4-20020a17090b190400b002006f8e7688sm2102495pjb.32.2022.09.21.10.00.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 10:00:24 -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, tj@kernel.org, lizefan.x@bytedance.com Cc: cgroups@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [RFC PATCH bpf-next 08/10] bpf: Use bpf_map_kvcalloc in bpf_local_storage Date: Wed, 21 Sep 2022 17:00:00 +0000 Message-Id: <20220921170002.29557-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220921170002.29557-1-laoar.shao@gmail.com> References: <20220921170002.29557-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Oq03LOMM; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663779626; a=rsa-sha256; cv=none; b=JX8FuPpEgdM3iJAOj7MstiOxMlg/eW14QPEuUtNX2STWz4gMsPAm/wTUrPDUi6B2yjcIFO z7TE5QSZwQfe7EzPtd4tGzkzcIjFzQGegFV8qd/K214zAhTOA0GsqByJdX1PB9J+SHPQcO XxcPhzcy82Su6AgKIDk5MgyHUEbs1rc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663779626; 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=86Fvvo7OHCl7WcRNmebHsC+UjHYkXcsdLASZGPmT+6Y=; b=nat/Vnf8HCujHMgU4fiJmuqOJjK115ZnU+kTkWhVr/WFayToNK1IP+G+QiV4mr/ua2T++g uB0x0OyuEw3mPJXn5PUMip1ex1h1zXPmuwsqyrxLBWsHMjUfRtoNZPYSrxEVU5K+VvYr0Q KYN0+0pNq0/6gkXAp+FkX/e0zvV/qWA= Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Oq03LOMM; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspamd-Server: rspam06 X-Stat-Signature: 5a51sgyahusqkpu9ndeczeqd8rmxdq83 X-Rspam-User: X-Rspamd-Queue-Id: 4181CC009C X-HE-Tag: 1663779626-527052 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 eca1502..e1e5ada 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1717,6 +1717,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 @@ -1733,6 +1735,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 44498d7d..8a24828 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 727c04c..6123c71 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) {