From patchwork Wed Aug 17 21:04:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12946476 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 CE9ACC25B08 for ; Wed, 17 Aug 2022 21:04:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 68AAF8D0009; Wed, 17 Aug 2022 17:04:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 637578D0003; Wed, 17 Aug 2022 17:04:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D8288D0009; Wed, 17 Aug 2022 17:04:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 3F57D8D0003 for ; Wed, 17 Aug 2022 17:04:46 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 22761A0B0E for ; Wed, 17 Aug 2022 21:04:46 +0000 (UTC) X-FDA: 79810313772.04.6106765 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf16.hostedemail.com (Postfix) with ESMTP id BC2351801B6 for ; Wed, 17 Aug 2022 21:04:45 +0000 (UTC) Received: by mail-pf1-f175.google.com with SMTP id p125so13062653pfp.2 for ; Wed, 17 Aug 2022 14:04:45 -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=1DZ3i2bbkc3eTrJ/YvjarNAdlh/YgPZTxrhVDFxf0vQ=; b=O2oXhew5JiBi3bP9J6m/k7tE2dafEvAM/6B5hr+5iROIDquBiUTfxxeajve9s5fWqG wj8zhx1RmWLM+I96Lh+vrK8vurZa4TTqxZMt9EYbNqIHkmT74gLXfhDb6R8FGZz37fNL ImkQGrK9EQ+ETzgk7gEQRsOF5ya8U3/DZtlRf7R50awNPoGsSSh1qpympx3DNlR7fQzT NX0bA+e3YJqiBWvqCICAMDJsIUr7MQ5SNDu4Fk4PPYstEql3nAU2OOVfLmJN5ZKoN1L1 mOvePRVcU58g4mO9CRIjP/9H3JYTEc13VKgwwnVSEvnzPnD09B3wBWmLm823PavK0skX 21DA== 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=1DZ3i2bbkc3eTrJ/YvjarNAdlh/YgPZTxrhVDFxf0vQ=; b=UrPS/2YKQWtv0/JfUuZqVFom/0QAaU6oxqW4D16FggfFr8jLnKIfXgscDmb9Wl9q/a q1XIASHzGITzv2OZQAJKeR/66V0vYXRiN3sJwhxpQbhAhLQtAG4TwepspNTQ+XP3qqKp cJ+IMOeoRE2Jk1LCjrm9wQ6KDND/hlPaZu1x+qeFv++JcnWvEcZCdBlNXOB+gIPe6fpT 9nwULmS6dJ+V6+zhmG33klCS5DvswNiiie2os6HCfuNDgmO2ylNJiCPaF9axt2VPm/ZJ AOgyDeU608O0d9gkRZZjUVOLCTo2lcRQYbsPLh4Knnysrk3ttZxYqWJIh3XmgmkG/4lp M/Dw== X-Gm-Message-State: ACgBeo1cL6mw+YmwjuDSCbgiMrEn+e2QnryPUWZRBgHauThsFCcwZJAQ j1mQz/V+DeCsUGdk1hhlVY8= X-Google-Smtp-Source: AA6agR5ZXTfkc32b+ie7Gu+KRGH8xSXKaoIu4ajEMJHyyrfMuIggVVP0qg+DGC2trYE7cpazwq6ihw== X-Received: by 2002:a63:e448:0:b0:429:f52a:3ad2 with SMTP id i8-20020a63e448000000b00429f52a3ad2mr74547pgk.138.1660770284799; Wed, 17 Aug 2022 14:04:44 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::1:ccd6]) by smtp.gmail.com with ESMTPSA id t20-20020a170902dcd400b0016cd74e5f87sm335557pll.240.2022.08.17.14.04.43 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 17 Aug 2022 14:04:44 -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 06/12] bpf: Optimize element count in non-preallocated hash map. Date: Wed, 17 Aug 2022 14:04:13 -0700 Message-Id: <20220817210419.95560-7-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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660770284; a=rsa-sha256; cv=none; b=vuVbtdmedlPIejLIwUwEhDon0zlhQ1z9lbnxiqGAR4vGy0DSD1eQGJdRs6uG/tYOsWvFwQ FUi2VvExiD3yeyqKjt6GSyukE83XNblHeWw/FZtmmpexdB9LMp5yWUmSH73FwqiAxY+7pQ GTtIUZuOWhIVU69XgIQzPdu4VVIpQOI= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=O2oXhew5; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660770284; 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=1DZ3i2bbkc3eTrJ/YvjarNAdlh/YgPZTxrhVDFxf0vQ=; b=zYfyQEwAoqPcFja/6W0GKeMMR9+UUtf1l6RK2k+7XNRvzPtZxG4iq72nAWuHdIPvc7smP3 LMjQdzf8x3PJEVRZXvT5WkZqai9PAxeByKb0h+kGsxZ89J5oL/T25v5qyTQRxD5v0Wzrai EHv65t60ovkhpKZXCKyaBfKMnLQGKMY= X-Stat-Signature: 6dtxgmaxpf8qg415q9g18tzjy6wu84qb X-Rspamd-Queue-Id: BC2351801B6 Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=O2oXhew5; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1660770285-937138 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 atomic_inc/dec might cause extreme cache line bouncing when multiple cpus access the same bpf map. Based on specified max_entries for the hash map calculate when percpu_counter becomes faster than atomic_t and use it for such maps. For example samples/bpf/map_perf_test is using hash map with max_entries 1000. On a system with 16 cpus the 'map_perf_test 4' shows 14k events per second using atomic_t. On a system with 15 cpus it shows 100k events per second using percpu. map_perf_test is an extreme case where all cpus colliding on atomic_t which causes extreme cache bouncing. Note that the slow path of percpu_counter is 5k events per secound vs 14k for atomic, so the heuristic is necessary. See comment in the code why the heuristic is based on num_online_cpus(). Signed-off-by: Alexei Starovoitov --- kernel/bpf/hashtab.c | 70 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 6c0db430507a..65ebe5a719f5 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -101,7 +101,12 @@ struct bpf_htab { struct bpf_lru lru; }; struct htab_elem *__percpu *extra_elems; - atomic_t count; /* number of elements in this hashtable */ + /* number of elements in non-preallocated hashtable are kept + * in either pcount or count + */ + struct percpu_counter pcount; + atomic_t count; + bool use_percpu_counter; u32 n_buckets; /* number of hash buckets */ u32 elem_size; /* size of each element in bytes */ u32 hashrnd; @@ -556,6 +561,29 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) htab_init_buckets(htab); +/* compute_batch_value() computes batch value as num_online_cpus() * 2 + * and __percpu_counter_compare() needs + * htab->max_entries - cur_number_of_elems to be more than batch * num_online_cpus() + * for percpu_counter to be faster than atomic_t. In practice the average bpf + * hash map size is 10k, which means that a system with 64 cpus will fill + * hashmap to 20% of 10k before percpu_counter becomes ineffective. Therefore + * define our own batch count as 32 then 10k hash map can be filled up to 80%: + * 10k - 8k > 32 _batch_ * 64 _cpus_ + * and __percpu_counter_compare() will still be fast. At that point hash map + * collisions will dominate its performance anyway. Assume that hash map filled + * to 50+% isn't going to be O(1) and use the following formula to choose + * between percpu_counter and atomic_t. + */ +#define PERCPU_COUNTER_BATCH 32 + if (attr->max_entries / 2 > num_online_cpus() * PERCPU_COUNTER_BATCH) + htab->use_percpu_counter = true; + + if (htab->use_percpu_counter) { + err = percpu_counter_init(&htab->pcount, 0, GFP_KERNEL); + if (err) + goto free_map_locked; + } + if (prealloc) { err = prealloc_init(htab); if (err) @@ -882,6 +910,31 @@ static void htab_put_fd_value(struct bpf_htab *htab, struct htab_elem *l) } } +static bool is_map_full(struct bpf_htab *htab) +{ + if (htab->use_percpu_counter) + return __percpu_counter_compare(&htab->pcount, htab->map.max_entries, + PERCPU_COUNTER_BATCH) >= 0; + return atomic_read(&htab->count) >= htab->map.max_entries; +} + +static void inc_elem_count(struct bpf_htab *htab) +{ + if (htab->use_percpu_counter) + percpu_counter_add_batch(&htab->pcount, 1, PERCPU_COUNTER_BATCH); + else + atomic_inc(&htab->count); +} + +static void dec_elem_count(struct bpf_htab *htab) +{ + if (htab->use_percpu_counter) + percpu_counter_add_batch(&htab->pcount, -1, PERCPU_COUNTER_BATCH); + else + atomic_dec(&htab->count); +} + + static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l) { htab_put_fd_value(htab, l); @@ -890,7 +943,7 @@ static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l) check_and_free_fields(htab, l); __pcpu_freelist_push(&htab->freelist, &l->fnode); } else { - atomic_dec(&htab->count); + dec_elem_count(htab); l->htab = htab; call_rcu(&l->rcu, htab_elem_free_rcu); } @@ -974,16 +1027,15 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, l_new = container_of(l, struct htab_elem, fnode); } } else { - if (atomic_inc_return(&htab->count) > htab->map.max_entries) - if (!old_elem) { + if (is_map_full(htab)) + if (!old_elem) /* when map is full and update() is replacing * old element, it's ok to allocate, since * old element will be freed immediately. * Otherwise return an error */ - l_new = ERR_PTR(-E2BIG); - goto dec_count; - } + return ERR_PTR(-E2BIG); + inc_elem_count(htab); l_new = bpf_mem_cache_alloc(&htab->ma); if (!l_new) { l_new = ERR_PTR(-ENOMEM); @@ -1025,7 +1077,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, l_new->hash = hash; return l_new; dec_count: - atomic_dec(&htab->count); + dec_elem_count(htab); return l_new; } @@ -1499,6 +1551,8 @@ static void htab_map_free(struct bpf_map *map) free_percpu(htab->extra_elems); bpf_map_area_free(htab->buckets); bpf_mem_alloc_destroy(&htab->ma); + if (htab->use_percpu_counter) + percpu_counter_destroy(&htab->pcount); for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++) free_percpu(htab->map_locked[i]); lockdep_unregister_key(&htab->lockdep_key);