From patchwork Thu Aug 18 14:31:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12947863 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 613B9C00140 for ; Thu, 18 Aug 2022 20:38:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB3ED8D0003; Thu, 18 Aug 2022 16:37:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E630B8D0002; Thu, 18 Aug 2022 16:37:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D05108D0003; Thu, 18 Aug 2022 16:37:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C3CF38D0002 for ; Thu, 18 Aug 2022 16:37:59 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8EC151421F3 for ; Thu, 18 Aug 2022 20:37:59 +0000 (UTC) X-FDA: 79813875078.06.01BC84F Received: from mail-io1-f46.google.com (mail-io1-f46.google.com [209.85.166.46]) by imf05.hostedemail.com (Postfix) with ESMTP id C4C8A10049F for ; Thu, 18 Aug 2022 20:16:17 +0000 (UTC) Received: by mail-io1-f46.google.com with SMTP id y187so1948323iof.0 for ; Thu, 18 Aug 2022 13:16:17 -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=JWj3ghaLi18D37Utgpu0sbreSBl9gVi+HG8XRXJ1k4E=; b=J/vjjRSdspqfz5QQqVSBxlG3cZRAEs5vE1YwK2jl25Xu07tjrYiNRrxlucPklmoR9x fBJUXAQ/LT3QR42GeO3UqrnqRp1RSdz+Swira1oRSwzSExCLMH+CuRCFEcRhvHrFW78v tng9ceDAB9ew2ydHzWZnZxvNtW2T8poiW5TUemW/mvFECm1CyXNstuUVpnJyVVd0JQ42 YAHYbedJSYa9NAsf76auiydFa3uO2+AC0S3Uw0DnpnJXy7daYP7XQjFs//HOX0+9aAIU MXoS+DnB6GhRcPCFe5Xzh+tzSuYHsA0ibH1Y2orQGs1+05k1JUR/i+F72OtwdTOj61Wn 9JGQ== 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=JWj3ghaLi18D37Utgpu0sbreSBl9gVi+HG8XRXJ1k4E=; b=CU0XdFFeNBGv8e6hXwNAUOtFwD7pe9KnPIZkHUcQmvKgG5YdHAonY/Tzs3YQy0J6Vo JUpViIJmcoPpoIZn6MwNT+ACRBfPdUTo1BFlpZducLLL53oDCycW+Mnu7lZadpmC78PV 2lXL49UufLtggWiddiOERNtiTIybx7bZFVHjLkbBE5w33IdSgfDtBEKO15E+hoyxM15S Q9XiQUDBy9KP3sMHa3t0Qspe84gRZCYmOmstFDZyUv8DjzXQyBlUMUn9nAlAhqskkqPW uEChT5M85Fgfw29qm3g3u0X2qBg3YB9QyP/yvrM/T7FSFTx3/tLF5mFAXfksnAxxGiqs RhuQ== X-Gm-Message-State: ACgBeo02VOtnbar/HDPcpE2/p9AbMyjAWSE72rP2gctzyTlUu84HXNx4 iCB1JwFk6fPLuz3ZRJAkdBCGubeOaQ357V21 X-Google-Smtp-Source: AA6agR6tlBl2i+jwjIYUJY+noBYDNAFEC5gq9HwJ1ZBb4KG+9KIyTOYDLgt8RG6tQbIPRV4tNA0M8g== X-Received: by 2002:a63:e90c:0:b0:422:5ab9:99d6 with SMTP id i12-20020a63e90c000000b004225ab999d6mr2697130pgh.394.1660833125123; Thu, 18 Aug 2022 07:32:05 -0700 (PDT) Received: from vultr.guest ([45.32.72.237]) by smtp.gmail.com with ESMTPSA id h5-20020a63f905000000b003fdc16f5de2sm1379124pgi.15.2022.08.18.07.32.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 07:32:04 -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: [PATCH bpf-next v2 09/12] bpf: Use bpf_map_kvcalloc in bpf_local_storage Date: Thu, 18 Aug 2022 14:31:15 +0000 Message-Id: <20220818143118.17733-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20220818143118.17733-1-laoar.shao@gmail.com> References: <20220818143118.17733-1-laoar.shao@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660853778; a=rsa-sha256; cv=none; b=SXve7AmGDWBq9JmSO+x7l/apmzpiQbDvAKYcmFjqfv0xpIYLqX0c/G30nYqZJJpd+K/a0E 6OFr0/XZRCCARrZhl6dEkugqxlsGqjy6PvNphb+aFYNGpmy0JRqvFLL2azKGeyaiwlxiUU NhfiP9vwZZRBBr1vZ+xTLFejYh7eEGQ= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="J/vjjRSd"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.166.46 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=1660853778; 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=JWj3ghaLi18D37Utgpu0sbreSBl9gVi+HG8XRXJ1k4E=; b=b64bSxk4f8Majidm/6QY2qNE8FX3rBta1K44Vz67aBp5XdQFUuU6++FL58cCRpqVeQzRzE JepJwUT5cUiUexBh9Xap0+eBvBr4UP3E3txIQlu2fl1wJ0KWZOeFzG+sbCLrcDo+GPYf8X cIcd6AG/2wS2WPeXzBtIJoCzuWfgmkw= X-Stat-Signature: biwf45epchtuemmk1kyqp9xpqqu6mg8m X-Rspamd-Queue-Id: C4C8A10049F Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="J/vjjRSd"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.166.46 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1660853777-731612 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 066f351d..f95a2df 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) {