From patchwork Fri Aug 26 02:44:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12955502 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 4C6AFECAAA2 for ; Fri, 26 Aug 2022 02:45:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DFFB68000D; Thu, 25 Aug 2022 22:45:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DAEAA80007; Thu, 25 Aug 2022 22:45:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C286D8000D; Thu, 25 Aug 2022 22:45:14 -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 ADEEA80007 for ; Thu, 25 Aug 2022 22:45:14 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 84B451A06B5 for ; Fri, 26 Aug 2022 02:45:14 +0000 (UTC) X-FDA: 79840202148.07.346C5C6 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf07.hostedemail.com (Postfix) with ESMTP id 38C8640009 for ; Fri, 26 Aug 2022 02:45:14 +0000 (UTC) Received: by mail-pf1-f174.google.com with SMTP id 76so326535pfy.3 for ; Thu, 25 Aug 2022 19:45:14 -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=YZHKbeH8DnXbWPWBMsdiPtaW1ZKeFO8TVBseUASZqVM=; b=GpjL7fUxJMG0/zA4mo4xdTv/r1CC2MmWh8AC7pNBP0L30+xpxMGnp4gJg5hUbUSh8M PcxqY9cJV/NHV5jRouSevPYwJYvQ4zOPwihjyQfO2HzyBPXNThjHHoQR8c9xD0MtGvLU wKmGX9d5HDh2hDH83P3Msz5LMSj+aCBlspPN/m3oW/sW8bA7KNFeqX5KlaH1WgBXvuG0 xnPYL3d65t6kcG+D32p2p9/17oXtt+70XGAc1o07dDv96/CXktZRxDH3EzrllrlLk22P LeXXSPWSITd5r0wv9l9CvxvEwPFKZAtdqnO6Q6+Eqn4DFXAd9l3+sMQ/ZVDzZKXuNTO8 UDfA== 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=YZHKbeH8DnXbWPWBMsdiPtaW1ZKeFO8TVBseUASZqVM=; b=u0E3FikeBgrjRp0kK0nOCnJEv4J3RLS1ezjgr02sozCu4IeBBAPF7/S5v0SvbQYhoG ISUwHGlOdGG4jcdnFK8IfpBnA34sQ8POuF+hJYS85Og0FNp6ocr8fsFY0XAll68Jzyvj V6ARXYA6GiryKZ5dooT2vqT8pFx7D+YBiVdn/pY2tq6FTgGe2Messhxbtc1TqAqC+893 Z7syd8Ovhdmiv3WE+/AnaJM2ojCzxcwGQGQyanW2IIPrHptmpTtHukEo8eY0GZjLMB5q 3fXQgs71OlpOU9h8eUSUMjt7lON+kwrcKvMslxVQg4rgTrv5VmF2/UE0105Y0KW63GPx kg2w== X-Gm-Message-State: ACgBeo0TUjabK2hY1MxuLsGBdfEsMGlPCVCviu2/MTlWLPqO782TdUxO v+4jqWTbCITOIIQVfchR3Sg= X-Google-Smtp-Source: AA6agR5TGr1wZaD3/psfDBLsZ0Te84G7Qc7MK5kymK0o3fuFIppuFSKhzWWIbyXSWXW5VL+Banlqog== X-Received: by 2002:a63:e412:0:b0:41d:9c6a:7e with SMTP id a18-20020a63e412000000b0041d9c6a007emr1577624pgi.575.1661481913392; Thu, 25 Aug 2022 19:45:13 -0700 (PDT) Received: from macbook-pro-3.dhcp.thefacebook.com ([2620:10d:c090:400::5:15dc]) by smtp.gmail.com with ESMTPSA id y1-20020a17090a154100b001fac90ead43sm430265pja.29.2022.08.25.19.45.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 25 Aug 2022 19:45:12 -0700 (PDT) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, andrii@kernel.org, tj@kernel.org, memxor@gmail.com, delyank@fb.com, linux-mm@kvack.org, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v4 bpf-next 10/15] bpf: Add percpu allocation support to bpf_mem_alloc. Date: Thu, 25 Aug 2022 19:44:25 -0700 Message-Id: <20220826024430.84565-11-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220826024430.84565-1-alexei.starovoitov@gmail.com> References: <20220826024430.84565-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661481914; a=rsa-sha256; cv=none; b=u5S665oyn9mYAbUeS/X+syUCtLEk+kfN83eSzYMCrdZwNj80JAE1HvPymBFV6eDT09gGtn zpiw9JrV4VVS0CG91+yIFm0Rya/vF4HLd8TjIJICKZnIwJmsLIwNk7ejR5XWtxK47WGjHO zKC+XJzdLf6kRWe4JSzP8mqSOsBCCv4= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GpjL7fUx; spf=pass (imf07.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661481914; 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=YZHKbeH8DnXbWPWBMsdiPtaW1ZKeFO8TVBseUASZqVM=; b=c0l2TtSdgisQDrkIDQ4KLQjLMG0yWlqqK9C2coTVMfwFOycARel/CZRsuHHeyhcFab5hdF 4fzXuRbZ28aO/hTAVsnd+KFktpiyvReOQ0jnDtwVJCzrxvK6/9Da1HgX+B4faQAb67wEB7 mSM4b7lS6F7bWPUQ/ULW4EnONQzws/Y= X-Stat-Signature: 6w1bqx1np6bdtp83n7a353hye6ch7mpx X-Rspamd-Queue-Id: 38C8640009 X-Rspam-User: X-Rspamd-Server: rspam06 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GpjL7fUx; spf=pass (imf07.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1661481914-533779 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: From: Alexei Starovoitov Extend bpf_mem_alloc to cache free list of fixed size per-cpu allocations. Once such cache is created bpf_mem_cache_alloc() will return per-cpu objects. bpf_mem_cache_free() will free them back into global per-cpu pool after observing RCU grace period. per-cpu flavor of bpf_mem_alloc is going to be used by per-cpu hash maps. The free list cache consists of tuples { llist_node, per-cpu pointer } Unlike alloc_percpu() that returns per-cpu pointer the bpf_mem_cache_alloc() returns a pointer to per-cpu pointer and bpf_mem_cache_free() expects to receive it back. Acked-by: Kumar Kartikeya Dwivedi Signed-off-by: Alexei Starovoitov --- include/linux/bpf_mem_alloc.h | 2 +- kernel/bpf/hashtab.c | 2 +- kernel/bpf/memalloc.c | 44 +++++++++++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf_mem_alloc.h b/include/linux/bpf_mem_alloc.h index 804733070f8d..653ed1584a03 100644 --- a/include/linux/bpf_mem_alloc.h +++ b/include/linux/bpf_mem_alloc.h @@ -12,7 +12,7 @@ struct bpf_mem_alloc { struct bpf_mem_cache __percpu *cache; }; -int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size); +int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size, bool percpu); void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma); /* kmalloc/kfree equivalent: */ diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 299ab98f9811..8daa1132d43c 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -594,7 +594,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) goto free_prealloc; } } else { - err = bpf_mem_alloc_init(&htab->ma, htab->elem_size); + err = bpf_mem_alloc_init(&htab->ma, htab->elem_size, false); if (err) goto free_map_locked; } diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index 6a252d495f6c..54455a64699b 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -101,6 +101,7 @@ struct bpf_mem_cache { /* count of objects in free_llist */ int free_cnt; int low_watermark, high_watermark, batch; + bool percpu; struct rcu_head rcu; struct llist_head free_by_rcu; @@ -133,6 +134,19 @@ static void *__alloc(struct bpf_mem_cache *c, int node) */ gfp_t flags = GFP_NOWAIT | __GFP_NOWARN | __GFP_ACCOUNT; + if (c->percpu) { + void **obj = kmem_cache_alloc_node(c->kmem_cache, flags, node); + void *pptr = __alloc_percpu_gfp(c->unit_size, 8, flags); + + if (!obj || !pptr) { + free_percpu(pptr); + kfree(obj); + return NULL; + } + obj[1] = pptr; + return obj; + } + if (c->kmem_cache) return kmem_cache_alloc_node(c->kmem_cache, flags, node); @@ -188,6 +202,12 @@ static void alloc_bulk(struct bpf_mem_cache *c, int cnt, int node) static void free_one(struct bpf_mem_cache *c, void *obj) { + if (c->percpu) { + free_percpu(((void **)obj)[1]); + kmem_cache_free(c->kmem_cache, obj); + return; + } + if (c->kmem_cache) kmem_cache_free(c->kmem_cache, obj); else @@ -328,21 +348,30 @@ static void prefill_mem_cache(struct bpf_mem_cache *c, int cpu) * kmalloc/kfree. Max allocation size is 4096 in this case. * This is bpf_dynptr and bpf_kptr use case. */ -int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size) +int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size, bool percpu) { static u16 sizes[NUM_CACHES] = {96, 192, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096}; struct bpf_mem_caches *cc, __percpu *pcc; struct bpf_mem_cache *c, __percpu *pc; - struct kmem_cache *kmem_cache; + struct kmem_cache *kmem_cache = NULL; struct obj_cgroup *objcg = NULL; char buf[32]; - int cpu, i; + int cpu, i, unit_size; if (size) { pc = __alloc_percpu_gfp(sizeof(*pc), 8, GFP_KERNEL); if (!pc) return -ENOMEM; - size += LLIST_NODE_SZ; /* room for llist_node */ + + if (percpu) { + unit_size = size; + /* room for llist_node and per-cpu pointer */ + size = LLIST_NODE_SZ + sizeof(void *); + } else { + size += LLIST_NODE_SZ; /* room for llist_node */ + unit_size = size; + } + snprintf(buf, sizeof(buf), "bpf-%u", size); kmem_cache = kmem_cache_create(buf, size, 8, 0, NULL); if (!kmem_cache) { @@ -355,14 +384,19 @@ int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size) for_each_possible_cpu(cpu) { c = per_cpu_ptr(pc, cpu); c->kmem_cache = kmem_cache; - c->unit_size = size; + c->unit_size = unit_size; c->objcg = objcg; + c->percpu = percpu; prefill_mem_cache(c, cpu); } ma->cache = pc; return 0; } + /* size == 0 && percpu is an invalid combination */ + if (WARN_ON_ONCE(percpu)) + return -EINVAL; + pcc = __alloc_percpu_gfp(sizeof(*cc), 8, GFP_KERNEL); if (!pcc) return -ENOMEM;