From patchwork Fri Aug 19 21:42: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: 12949273 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 C0E92C32793 for ; Fri, 19 Aug 2022 21:43:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B47E6B007E; Fri, 19 Aug 2022 17:43:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43CF46B0080; Fri, 19 Aug 2022 17:43:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B6B38D0001; Fri, 19 Aug 2022 17:43:06 -0400 (EDT) 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 1A2E06B007E for ; Fri, 19 Aug 2022 17:43:06 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C6FFEC1B6E for ; Fri, 19 Aug 2022 21:43:05 +0000 (UTC) X-FDA: 79817667930.16.FF4FC14 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf29.hostedemail.com (Postfix) with ESMTP id 85BCF120126 for ; Fri, 19 Aug 2022 21:43:05 +0000 (UTC) Received: by mail-pj1-f47.google.com with SMTP id t2-20020a17090a4e4200b001f21572f3a4so6053869pjl.0 for ; Fri, 19 Aug 2022 14:43:05 -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=9NJuZZGi61ivxe7o1wakU220SGtbaFMb+lW1/WXuBSo=; b=GXP7gjWcLzkdZc//4cZi2WcfReF8h2ed3jYt6HNXUljAIxDeVaMXDydRzNLfxKuXCk 9YsdNRLZrWtA+/Cu5ZoK7ww2AV3xfK3ONHZESILERBYJxRSrlskhbp4JdgFh/XlXfXYl gUaEiq4hkIOwSPhQdQgqjLxQ7M2rDwxY2zO1XjhFRrmraSgDaG9aw3tRVw3R5VlpiAkm EQ1q3/UYsfoqupXJ5v3ZogbfXjWpnvKfVVdb9jt6pluoDwa39Jh3cZ8jbDoY3nIPX/85 1ZpfJvL4xHrHUb/rr6e/BjDduz1bvoBZ6hX14siv03bSTsvO+mxOWwoBTvSPm0PXlbfv x+xg== 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=9NJuZZGi61ivxe7o1wakU220SGtbaFMb+lW1/WXuBSo=; b=5683nkJgLUN0pl8Eodul4k5SppVLKz9der6XlfhWSfINu0r1Fi+EfD9d4elU9gXY9R xBG9EWz+15C0eKlKuiOjQGHGeXP9TNq06aCZ66D6vWM7N6Ias2esxR4p8Y47HeqWGIwh cgWZ8Gjpcfcm2fr/rryhnZZst26ONjcbLY843V/kmswEdJrbB5pfcoU5bmIW8WrLfPix 6kLISHLZmSJ29AZev5ehJPK5Ge7TjmQv0GC7z96jd6YbWVZ/T46Br/ukGVPbzj1BSvec fxkNjw93bZp1cIIdIDsUSx/n8mDwkybURdfu/U5xQ+yq7JmJkpXC9dyJlNGSsVRhXJEz Q2Jg== X-Gm-Message-State: ACgBeo1iiEHekfok+oowZZtMP3rwCAuyi5GBkpsyh6FdHCtHYwLDioU5 QNBbD3xcaYzjuM+Kyhta15U= X-Google-Smtp-Source: AA6agR6Y/FL1isCq/nfuK7x5R2qTj7SNzOmwS6LqoSR2m4C9z+rfVY4THDenlt8EtdaWG9doWa2+fg== X-Received: by 2002:a17:902:e5cc:b0:16f:1153:c519 with SMTP id u12-20020a170902e5cc00b0016f1153c519mr9393770plf.151.1660945384628; Fri, 19 Aug 2022 14:43:04 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::1:c4b1]) by smtp.gmail.com with ESMTPSA id x24-20020a17090a789800b001f312e7665asm3582549pjk.47.2022.08.19.14.43.03 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 19 Aug 2022 14:43:04 -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 v3 bpf-next 08/15] bpf: Adjust low/high watermarks in bpf_mem_cache Date: Fri, 19 Aug 2022 14:42:25 -0700 Message-Id: <20220819214232.18784-9-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220819214232.18784-1-alexei.starovoitov@gmail.com> References: <20220819214232.18784-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=1660945385; 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=9NJuZZGi61ivxe7o1wakU220SGtbaFMb+lW1/WXuBSo=; b=u+iLEXmqhZbM6A0Hg1b3Eq+03xA6SeoFT1r19kVXnTfTYSlA4FR3bU6no8Py3Ua1/TYtYS xReU1fw9hTAICSZNRX3VQDHbAjGeflUi271fSnx574ZJkY6KEywGmVgyseHonwWZ1y5qDs oFp/LkejcwSGbBYGGi0pMeetXoA5jPo= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GXP7gjWc; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660945385; a=rsa-sha256; cv=none; b=QM3FXYJz4O76ABFO31ZHxlhQS0sJAAmfb4SHIee2OPsuWJ9iEEVBXIIJUYnxZGfIBdomCU 6DiFejdzqMkoSNHeSdKcQa9sEDNrpmdH3hHDEZl9l/o+4+pbKiurJjj/2AYc2A8/xVKdLF pNH2EvwrWL6kQqy+jXqZ0OCZZdjpOEs= Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GXP7gjWc; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf29.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 85BCF120126 X-Stat-Signature: 9shc4j43ts9zjj9ypr46xmg4t6y3wtiu X-Rspam-User: X-HE-Tag: 1660945385-277550 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 The same low/high watermarks for every bucket in bpf_mem_cache consume significant amount of memory. Preallocating 64 elements of 4096 bytes each in the free list is not efficient. Make low/high watermarks and batching value dependent on element size. This change brings significant memory savings. Signed-off-by: Alexei Starovoitov --- kernel/bpf/memalloc.c | 50 +++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index cfa07f539eda..22b729914afe 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -99,6 +99,7 @@ struct bpf_mem_cache { int unit_size; /* count of objects in free_llist */ int free_cnt; + int low_watermark, high_watermark, batch; }; struct bpf_mem_caches { @@ -117,14 +118,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 - */ - static void *__alloc(struct bpf_mem_cache *c, int node) { /* Allocate, but don't deplete atomic reserves that typical @@ -215,7 +208,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); /* and drain free_llist_extra */ llist_for_each_safe(llnode, t, llist_del_all(&c->free_llist_extra)) @@ -229,12 +222,12 @@ static void bpf_mem_refill(struct irq_work *work) /* Racy access to free_cnt. It doesn't need to be 100% accurate */ 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); } @@ -243,9 +236,38 @@ static void notrace irq_work_raise(struct bpf_mem_cache *c) 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 + * == ~ 116 Kbyte using below heuristic. + * Initialized, but unused bpf allocator (not bpf map specific one) will + * consume ~ 11 Kbyte per cpu. + * Typical case will be between 11K and 116K closer to 11K. + * 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 @@ -387,7 +409,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); return llnode; } @@ -420,7 +442,7 @@ static void notrace unit_free(struct bpf_mem_cache *c, void *ptr) local_dec(&c->active); local_irq_restore(flags); - if (cnt > HIGH_WATERMARK) + if (cnt > c->high_watermark) /* free few objects from current cpu into global kmalloc pool */ irq_work_raise(c); }