From patchwork Sun Feb 5 06:58:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13128979 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 BB105C636CC for ; Sun, 5 Feb 2023 06:58:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4CD1F6B0074; Sun, 5 Feb 2023 01:58:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 47CE56B0075; Sun, 5 Feb 2023 01:58:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 31DA86B0078; Sun, 5 Feb 2023 01:58:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 20DA16B0074 for ; Sun, 5 Feb 2023 01:58:33 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EBC2D1C5F54 for ; Sun, 5 Feb 2023 06:58:32 +0000 (UTC) X-FDA: 80432334864.09.35E81EC Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf26.hostedemail.com (Postfix) with ESMTP id 22BA1140007 for ; Sun, 5 Feb 2023 06:58:30 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=jW10FVIl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.181 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=1675580311; 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=cekXoP31ZR51IJygqDIUgQ8CQ5lvQGcczV3A0ytqEZw=; b=a0g/rbikAFrURSMSwg6Bus6mMQgBGU5pC1wpdf7g3CWv/oUpLyF56Dn8PjtI8sZHMdQe2o rKOLUUD9DB/nOay2qGALNZj6XhzbeR7D4f+72xljFtJnbaMYLoswr1P5NrgsbCEQAlat33 Xkn9WiHoNf9Y00+RqAsvrTfyxhAkI+Q= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=jW10FVIl; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675580311; a=rsa-sha256; cv=none; b=UBKmtIztJgZ9i7Pt8uwSiQKw7B5DSm1fm+i7qEZeP7q33MH7PFNbUSs5HqgCAS10WoZ30t N9XZicxj1F2rp8vlsQ995qUacbQ1C3/oyB/oGutrv+r34DhoWb9MDhFgwqvOfWMI/uiEJG HL+YvKeCkpCQwy9zNs9NNkXc2f0acpI= Received: by mail-pf1-f181.google.com with SMTP id t17so6412323pfj.0 for ; Sat, 04 Feb 2023 22:58:30 -0800 (PST) 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 :message-id:reply-to; bh=cekXoP31ZR51IJygqDIUgQ8CQ5lvQGcczV3A0ytqEZw=; b=jW10FVIllpk2UmObYJXnztvkxz67v4iRh5dx08YuEgB9VxdIzS1XiVDjSsmQZ/yizH TIx1w+oPsyzs6CxqC+YnzbcuutuyIhN5hMRGUyh1LBjttodYi0B2ZZ7ar3lCimJPaj9E 1BuCXvc77bDY99d/CMraUwg0ZxZvNljDPI6nrmwm6bJDgn1eO1VQ7Q5l+ErK1Zqm8Wti p0gcJ1/lWqkOpOlYWBY8uXUqPRdXOwG8a+e7bRG3KoE3rsGDtqBUhGUfTvAA305muR+V qhM185BVxKKTsWE28gnV1B3jLRF9uIiCIbTh3IgK950jdA8lCIlUdvQqDsNSoPnbMPgJ 9wKQ== 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:message-id:reply-to; bh=cekXoP31ZR51IJygqDIUgQ8CQ5lvQGcczV3A0ytqEZw=; b=6DQMTdb/tHhH/k4slYnCXmQ4Cns3/z8Gn5jJwsfStdL2WrNCXmzb/ro1oU3IbGa1kV JIgWBK3+DRMedNV2BSSQb90mVEAlcPX5GnZbLWvPwzioy3cs4Ow4lyjTctazkoPAwcbU rs9cdYgTB3q664phfRW5aDFUpFJEIA0wrqhJM1TJStfp1tD1P/u+y+rMZSBEcUOJ7svS 9yGue77bW+1jSgTfRMkPJ8xDFA6ubjqtnt8fwU2Zy5CKfEoauxTVvEPE8RCST91T5H2Y wuCJk6wAB4HHfuO9WZ3INJ7iejFoXt0lQPlJ4fq30/Ybke+tvxY3XSVOw8TMW7XF5cXE HouQ== X-Gm-Message-State: AO0yUKUYS2ds9B5vhSY2xsbThNURpQtD7P3r+oyc8l03zoKLwweH+hbe zqIcE6xuOxfF/ek1by1rwAk= X-Google-Smtp-Source: AK7set8XCAleQGwzei2uSh+f/kpAooHQ+aepCJvOk66z8RfqGTLeVFmSXEcPe1mkxEJEyMs7erfirQ== X-Received: by 2002:aa7:9af1:0:b0:57a:7140:84ae with SMTP id y17-20020aa79af1000000b0057a714084aemr14810074pfp.9.1675580310103; Sat, 04 Feb 2023 22:58:30 -0800 (PST) Received: from vultr.guest ([2401:c080:1c02:6a5:5400:4ff:fe4b:6fe6]) by smtp.gmail.com with ESMTPSA id 144-20020a621596000000b00593ce7ebbaasm4596114pfv.184.2023.02.04.22.58.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Feb 2023 22:58:29 -0800 (PST) From: Yafang Shao To: tj@kernel.org, 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, muchun.song@linux.dev, akpm@linux-foundation.org Cc: bpf@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Yafang Shao Subject: [PATCH bpf-next 2/5] bpf: use bpf_map_kvcalloc in bpf_local_storage Date: Sun, 5 Feb 2023 06:58:02 +0000 Message-Id: <20230205065805.19598-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230205065805.19598-1-laoar.shao@gmail.com> References: <20230205065805.19598-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 22BA1140007 X-Stat-Signature: xz5unqwz49ycpkxkjussfrwrukyxiaj7 X-HE-Tag: 1675580310-770857 X-HE-Meta: U2FsdGVkX189SKBd4a646zHMd7ePyiTiwDFnomEZiM1IRMcYNcmzLQCsM4O2C3xf67LbZy6mllT9J5zyWI0r8fs2oKt2mnBjGbrvkIkqLNt/EyDFEaP2Kec/+gNzEqif4Mb2rrKJ4pQLGAxkvhYtvo4334lXLNkDRLnzfZZD2XnZv9+XEODlOPcQEe0a+tqSRYqAA9apz3WmuBUpymwb16cUfLMh277xNjqlCSS2dUc7gtoa25cG/RnpK6SK4cLtoFncQ1xU5um7N8RzUNM4roQ+j5QyYsAKV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MghC8pPasVzscz+icU6B2q+sMBdDN0qKgo4rG0piw2nofsrgaYiIFqc+I0n+01if0VCbEv+Bn/ooVDs7xm2y465tt0/S2dBBF250T29EN47mjZXc3hehAlxQV/wMBJFwTQmeBTfTy64XmlFbkMNZQs+WJeWngCsL1wRYa57NXgTOy0M52MEUcHr20MluFciWxLLvkRMUSon+vc11op0bqCQyKNlJw== 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. Then bpf_local_storage will be the same with other map's creation. 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 35c18a9..fe0bf48 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1886,6 +1886,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 @@ -1902,6 +1904,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 373c3c2..35f4138 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -568,8 +568,8 @@ static struct bpf_local_storage_map *__bpf_local_storage_map_alloc(union bpf_att 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); return ERR_PTR(-ENOMEM); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index bcc9761..9d94a35 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -464,6 +464,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); + mem_cgroup_put(memcg); + + return ptr; +} + void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align, gfp_t flags) {