From patchwork Wed Aug 17 21:04:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12946478 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 5B042C25B08 for ; Wed, 17 Aug 2022 21:04:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2AE48D000B; Wed, 17 Aug 2022 17:04:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD8B78D0003; Wed, 17 Aug 2022 17:04:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C794A8D000B; Wed, 17 Aug 2022 17:04:53 -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 B734B8D0003 for ; Wed, 17 Aug 2022 17:04:53 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 94401ABF2C for ; Wed, 17 Aug 2022 21:04:53 +0000 (UTC) X-FDA: 79810314066.23.90A3C9F Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf03.hostedemail.com (Postfix) with ESMTP id 2403320165 for ; Wed, 17 Aug 2022 21:04:52 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id jm11so10776450plb.13 for ; Wed, 17 Aug 2022 14:04:52 -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=CqItYD5FIMTqL735q4RzdfGMBe2ezI24gvr9zo137dQ=; b=Ix9FtXEe9O4LvS7HxcGkD5ryZ2d6IEyvzm7T6nS9/vzVIg/95jivgmzzrBZiPu66sM soKCJQl5rIWYyIaq5MpE/lKVfQag3/Y2ba6VUNViE9RQeWAsXw2dsU5h7G4BxoJuG9aw WyIKdjqeevpmNB9AJXLlJj1w9NS9STwKDKv51GCEOPvPeYruzfEdqpxq0RN50WIywKuN kzdXCFmZO9coSK1kES9iFKyi1J4LO+P/jt3ywKeWfigT+wGdI227haKBDCFaTV4aYe43 JHs/GxZEzz/veMVyJiZTWepRCTYKoyURcXfDXSslxoxoF7uksYU+wvJC8vrqkX5S6mcf wzhg== 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=CqItYD5FIMTqL735q4RzdfGMBe2ezI24gvr9zo137dQ=; b=of5ZoJePFUW19VptiEDReWvzxgpKm5s4Rlw9oPYOuIilf1JYf4hdnlSHt3WDY+oTwg g9L+GB7nF+kCwA84psir0VSpxSVkmfPURRZJlILlrE5bVzNcG3M4rS0eNGHTJ7YkiWAe a0tN5qRwJHdtLwbu/5hdYLHBfwgRFOUl+S4/2MeZjfviM0wudBgaqwM7IMOHBwfYkBrh rNLnXAoudfzTtf69cyG7hGSH0N2ptyrvIff1pLtihvXUk5QZXE3YmKdegZHwDk17tXCx f7Mx32u4R+0aX1icfrqO42Un7EG2g8p7vrKX4LHsfDG1+DmFYS+jP6hTQDrMEwcFQOvj QPRQ== X-Gm-Message-State: ACgBeo278Qh694188Dmgy8nKAo7W9fUN1d0f+ryqxfQzJTZ23cLajwnw EkvWPM4IMpU8UNdqi2viioQ= X-Google-Smtp-Source: AA6agR4qqQ/vAvMyyxhIFskV/Nf8U14xtKT5zSR0M/4ctfttbF7UcUwMENRPo8U4t7RHZZCr57WBsw== X-Received: by 2002:a17:90a:a08:b0:1fa:b43d:68cf with SMTP id o8-20020a17090a0a0800b001fab43d68cfmr4873035pjo.41.1660770292085; Wed, 17 Aug 2022 14:04:52 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::1:ccd6]) by smtp.gmail.com with ESMTPSA id x7-20020a628607000000b0053554e0e950sm149572pfd.147.2022.08.17.14.04.50 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 17 Aug 2022 14:04:51 -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 v2 bpf-next 08/12] bpf: Adjust low/high watermarks in bpf_mem_cache Date: Wed, 17 Aug 2022 14:04:15 -0700 Message-Id: <20220817210419.95560-9-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220817210419.95560-1-alexei.starovoitov@gmail.com> References: <20220817210419.95560-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660770292; 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=CqItYD5FIMTqL735q4RzdfGMBe2ezI24gvr9zo137dQ=; b=pATZS5RMZTMhM61Z9A7N4pazMd+sHTtM7RQfUJSo0ltWcyVx9qZzDQw3YcYrwqXn5Me6QQ 45qIdsupJ52vdKGl09NurfHznlK7UqFrVFxLglBcmMYjc7uHavlQ7cS9L129pgMxeFNde1 jYtb41uc9yclBBPssK07WR17b9ekekk= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Ix9FtXEe; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660770292; a=rsa-sha256; cv=none; b=7FhJdLHVQ5Yix64KTJErdJ3vt1wUE89rDHqZ2tFJOantRk8jC4AfNCqK33FT+AtABl08Ht cdXhyMwfpT80KmwiiDu5ptApRNjCP8EuU9YvQ/b2V/zJkpvfC32NyC/9PsxG/ZSzjGPmJM 03QLj2rL0D46QfON4AVWvplWDqqXb8c= Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Ix9FtXEe; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: f6rxur5nmzqdqifudyyzdmj9j8zoisj1 X-Rspamd-Queue-Id: 2403320165 X-HE-Tag: 1660770292-369970 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 Same low/high watermarks for every bucket in bpf_mem_cache consume significant amount of memory. Preallocating 64 elements of PAGE_SIZE to the free list is not efficient. Make low/high watermarks and batching value depend on element size. This change brings significant memory savings. Signed-off-by: Alexei Starovoitov --- kernel/bpf/memalloc.c | 64 ++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index a43630371b9f..be8262f5c9ec 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -105,6 +105,7 @@ struct bpf_mem_cache { atomic_t free_cnt_nmi; /* flag to refill nmi list too */ bool refill_nmi_list; + int low_watermark, high_watermark, batch; }; struct bpf_mem_caches { @@ -123,14 +124,6 @@ static struct llist_node notrace *__llist_del_first(struct llist_head *head) return entry; } -#define BATCH 48 -#define LOW_WATERMARK 32 -#define HIGH_WATERMARK 96 -/* Assuming the average number of elements per bucket is 64, when all buckets - * are used the total memory will be: 64*16*32 + 64*32*32 + 64*64*32 + ... + - * 64*4096*32 ~ 20Mbyte - */ - /* extra macro useful for testing by randomizing in_nmi condition */ #define bpf_in_nmi() in_nmi() @@ -238,7 +231,7 @@ static void free_bulk(struct bpf_mem_cache *c) if (IS_ENABLED(CONFIG_PREEMPT_RT)) local_irq_restore(flags); free_one(c, llnode); - } while (cnt > (HIGH_WATERMARK + LOW_WATERMARK) / 2); + } while (cnt > (c->high_watermark + c->low_watermark) / 2); } static void free_bulk_nmi(struct bpf_mem_cache *c) @@ -253,7 +246,7 @@ static void free_bulk_nmi(struct bpf_mem_cache *c) else cnt = 0; free_one(c, llnode); - } while (cnt > (HIGH_WATERMARK + LOW_WATERMARK) / 2); + } while (cnt > (c->high_watermark + c->low_watermark) / 2); } static void bpf_mem_refill(struct irq_work *work) @@ -262,12 +255,12 @@ static void bpf_mem_refill(struct irq_work *work) int cnt; cnt = c->free_cnt; - if (cnt < LOW_WATERMARK) + if (cnt < c->low_watermark) /* irq_work runs on this cpu and kmalloc will allocate * from the current numa node which is what we want here. */ - alloc_bulk(c, BATCH, NUMA_NO_NODE); - else if (cnt > HIGH_WATERMARK) + alloc_bulk(c, c->batch, NUMA_NO_NODE); + else if (cnt > c->high_watermark) free_bulk(c); if (!c->refill_nmi_list) @@ -276,9 +269,9 @@ static void bpf_mem_refill(struct irq_work *work) */ return; cnt = atomic_read(&c->free_cnt_nmi); - if (cnt < LOW_WATERMARK) - alloc_bulk_nmi(c, BATCH, NUMA_NO_NODE); - else if (cnt > HIGH_WATERMARK) + if (cnt < c->low_watermark) + alloc_bulk_nmi(c, c->batch, NUMA_NO_NODE); + else if (cnt > c->high_watermark) free_bulk_nmi(c); c->refill_nmi_list = false; } @@ -294,14 +287,47 @@ static void notrace irq_work_raise(struct bpf_mem_cache *c, bool in_nmi) irq_work_queue(&c->refill_work); } +/* For typical bpf map case that uses bpf_mem_cache_alloc and single bucket + * the freelist cache will be elem_size * 64 (or less) on each cpu. + * + * For bpf programs that don't have statically known allocation sizes and + * assuming (low_mark + high_mark) / 2 as an average number of elements per + * bucket and all buckets are used the total amount of memory in freelists + * on each cpu will be: + * 64*16 + 64*32 + 64*64 + 64*96 + 64*128 + 64*196 + 64*256 + 32*512 + 16*1024 + 8*2048 + 4*4096 + * + nmi's reserves + * 1*16 + 1*32 + 1*64 + 1*96 + 1*128 + 1*196 + 1*256 + 1*512 + 1*1024 + 1*2048 + 1*4096 + * == ~ 122 Kbyte using below heuristic. + * In unlikely worst case where bpf progs used all allocations sizes from + * non-NMI and from NMI too: ~ 227 Kbyte per cpu. + * Initialized, but unused bpf allocator (not bpf map specific one) will + * consume ~ 19 Kbyte per cpu. + * Typical case will be between 19K and 122K closer to 19K. + * bpf progs can and should share bpf_mem_cache when possible. + */ + static void prefill_mem_cache(struct bpf_mem_cache *c, int cpu) { init_irq_work(&c->refill_work, bpf_mem_refill); + if (c->unit_size <= 256) { + c->low_watermark = 32; + c->high_watermark = 96; + } else { + /* When page_size == 4k, order-0 cache will have low_mark == 2 + * and high_mark == 6 with batch alloc of 3 individual pages at + * a time. + * 8k allocs and above low == 1, high == 3, batch == 1. + */ + c->low_watermark = max(32 * 256 / c->unit_size, 1); + c->high_watermark = max(96 * 256 / c->unit_size, 3); + } + c->batch = max((c->high_watermark - c->low_watermark) / 4 * 3, 1); + /* To avoid consuming memory assume that 1st run of bpf * prog won't be doing more than 4 map_update_elem from * irq disabled region */ - alloc_bulk(c, c->unit_size < 256 ? 4 : 1, cpu_to_node(cpu)); + alloc_bulk(c, c->unit_size <= 256 ? 4 : 1, cpu_to_node(cpu)); /* NMI progs are rare. Assume they have one map_update * per prog at the very beginning. @@ -442,7 +468,7 @@ static void notrace *unit_alloc(struct bpf_mem_cache *c) } WARN_ON(cnt < 0); - if (cnt < LOW_WATERMARK) + if (cnt < c->low_watermark) irq_work_raise(c, in_nmi); return llnode; } @@ -471,7 +497,7 @@ static void notrace unit_free(struct bpf_mem_cache *c, void *ptr) } WARN_ON(cnt <= 0); - if (cnt > HIGH_WATERMARK) + if (cnt > c->high_watermark) /* free few objects from current cpu into global kmalloc pool */ irq_work_raise(c, in_nmi); }