From patchwork Fri Sep 2 21:10:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12964688 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 B5934C6FA82 for ; Fri, 2 Sep 2022 21:11:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5329780137; Fri, 2 Sep 2022 17:11:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4BB3380120; Fri, 2 Sep 2022 17:11:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3355980137; Fri, 2 Sep 2022 17:11:41 -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 1EFEE80120 for ; Fri, 2 Sep 2022 17:11:41 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 990074019E for ; Fri, 2 Sep 2022 21:11:40 +0000 (UTC) X-FDA: 79868391960.17.0590126 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf23.hostedemail.com (Postfix) with ESMTP id 3F770140067 for ; Fri, 2 Sep 2022 21:11:40 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id f12so2979950plb.11 for ; Fri, 02 Sep 2022 14:11:39 -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=reguNqpc0/tY/7LivNRuvOsyUpr9xAoESsfpxvrSB0I=; b=NqW7voFLIn4KDyudLyWjCqPsQS70vgg1FBUaax0Omj/X8L34xWn4m1Oqldov6DhNk1 DRikTgVelldUt/5TYCqwiwFP+E19sogjvCN2i/r6IuFzMuPEqK5E+WCcB1T67Jxlad4Q /pr8kUheAuzS4wVgo3ShyTCY6NI20wgObu3tb2q11CC36WSJrbYK0O0qYCHgwRya/QyX WMyO+f987pMSDF/ikAHg4itsfCE5DABmyZUIEnwDHy6yO/RuFp4i0kQXCldWWuo4/uAn US29dVDOUcSAL+sreWvITwKwj4GZpjgmEaEHpI9b3CRy6d5KQf7J6c2JmpFHcL+XiQHR cAZg== 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=reguNqpc0/tY/7LivNRuvOsyUpr9xAoESsfpxvrSB0I=; b=fl0ntDQF3rJSCnmeLOsZaA5gI5zBui2m4A0nbgmvbGK6zXGrat5FN7xDse1bYuF5q4 hGv+Xm1ZCo+yCZTXZpWpdr+lsSzEUFNCqNiou37WCIYuuukhEl3piST+L576gUYj3swP /I298wOWqB07h2Yfg38uewFNF6bIw2BqEy0CHS+nIeZCTwqR9RufxSeyooIe1l/U2PDs 6J/maM+v/0N8YxtB4JorjHAQ5l3bkYORfxsb4cB+lTBO5GPjbeiih4T5yIOHdBEPecJP qRuRbCHxetm0FnLCKxka5vhMuxENXsv/z51gOOpDfgKR4WOatRb+kmECxb8DdVRoyQry 4UGQ== X-Gm-Message-State: ACgBeo3UpLXEjhD9lJcbUtk7+X0ndA18l+x5NxD4gUkgQnQIKENKP6mH XCXOxXFGv+kI5ClJKpEDrns= X-Google-Smtp-Source: AA6agR58oow/Qu5gcVVaFEZHA7FIbwUtgpktlxwu81c/wARfg0WqWLFJRObHjEPbcDqYMZwzmUYc2A== X-Received: by 2002:a17:902:ccc9:b0:174:de2b:b19a with SMTP id z9-20020a170902ccc900b00174de2bb19amr24105969ple.100.1662153099200; Fri, 02 Sep 2022 14:11:39 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::c978]) by smtp.gmail.com with ESMTPSA id i9-20020a170902c94900b001637529493esm2120483pla.66.2022.09.02.14.11.37 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 02 Sep 2022 14:11:38 -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 v6 bpf-next 10/16] bpf: Add percpu allocation support to bpf_mem_alloc. Date: Fri, 2 Sep 2022 14:10:52 -0700 Message-Id: <20220902211058.60789-11-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220902211058.60789-1-alexei.starovoitov@gmail.com> References: <20220902211058.60789-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662153100; a=rsa-sha256; cv=none; b=b9rmT4wdPJTxqFzImhjNCHkd+o92d+iH97LiHZK7k1KWeuvZBsmL/tMyWx3/vV1Z+Ntk2F ML9w+k6yW8ckGHccUetyfaPthHfUBm6rOOPcu5BVljWkPHg2laMVkNacNkT8XQUMoEaQL3 OKCGHgxRmQh8SaymYPEGzf6Y2SOb0kc= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NqW7voFL; spf=pass (imf23.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.182 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=1662153100; 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=reguNqpc0/tY/7LivNRuvOsyUpr9xAoESsfpxvrSB0I=; b=HMBvqUDG7YLTXoOKCeXYNYbLZU7yulugCO10rfjilAl4ekaM89uEj9hXISZEyutpDpj0jn 2dntzml8MSIeswx1B/u+9lD5gHcQT22BRqm6soNcCVBjksDnFOkvKlwuOLe/k+VWOm+XNp xT3tyk+k3wpdDEV4aq+AamDxU+aZuIs= Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NqW7voFL; spf=pass (imf23.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Stat-Signature: qbbzg5hqkabyy9uzxxoxnksiyxtxbu4a X-Rspamd-Queue-Id: 3F770140067 X-Rspamd-Server: rspam04 X-Rspam-User: X-HE-Tag: 1662153100-554505 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 Acked-by: Andrii Nakryiko 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 0d888a90a805..70b02ff4445e 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -607,7 +607,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 5d8648a01b5c..f7b07787581b 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); @@ -193,6 +207,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 @@ -332,21 +352,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) { @@ -359,14 +388,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;