From patchwork Fri Aug 26 02:44:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12955499 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 8832EECAAD1 for ; Fri, 26 Aug 2022 02:45:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F0FD8000A; Thu, 25 Aug 2022 22:45:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 17E0B80007; Thu, 25 Aug 2022 22:45:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 041DD8000A; Thu, 25 Aug 2022 22:45:02 -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 E393F80007 for ; Thu, 25 Aug 2022 22:45:02 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C9B1C1C6442 for ; Fri, 26 Aug 2022 02:45:02 +0000 (UTC) X-FDA: 79840201644.27.7D3545F Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf16.hostedemail.com (Postfix) with ESMTP id 8592A18000E for ; Fri, 26 Aug 2022 02:45:02 +0000 (UTC) Received: by mail-pj1-f43.google.com with SMTP id e19so381441pju.1 for ; Thu, 25 Aug 2022 19:45:02 -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=PaPuYu1jW6l30jyUgEzf6fCeC16QOg08yHB4Ew2HdVM=; b=GRWpSaM70EZmvKMlFNvW71O+vyfhIii8KVD3IfMCQDb8TiWvtRl//AZ0NMAoAnkp52 lCPxgH8uRmBE7iuNdblxsUTzr7XnprwwQjuPsDEZL2N9jm1C53QVEzWXezafTMsByITm 0MHH6PmJRDm1vOWc76ZZ/IprpTh2GBWLqeDp3HNMbXLUh7shyvKYYYWnqKuKU95tGXWY gufIxRKVaOfKTpoqxebuo+OYy+AZ8LDWxoZ6QPFjF73r5Ljl9f8+nn/N1HNXc31xx52/ dECetY7+9Fmelb70kDDTVQh9MGPbJ8LK4ubKuMnUws8UqnwmbIyhAxbTElaqiIMWFrN3 MMwQ== 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=PaPuYu1jW6l30jyUgEzf6fCeC16QOg08yHB4Ew2HdVM=; b=QxejVjkRjonl8cYieuwbObe6edHT4PGjI6uGXdCkfUE8ybMEy8MVlNJteE3I0KYqBW AZcr/+Gr0rvvbNSp074LL8a2uEwVXVUWHMyhkWNkybrK4vRMqjPLZ7Qu3B9XXqx/PW4N ncJLfkHta0rAiNFEly4Wbx785R87sn+/R9Aapz20gQE2IT1pcS1eHakueYTI+RVq6EsH tL9Vtp2ru7fv6FNlZDbBo1S+ou2LO+5Hd5hTwCgNuhHlz+worqAQMW3/eNlYRwnMzsXj FNgJFvW3zg0EocQ2JolcsFLfDkE1p72YhdRJYfWshxcd0RT1ZqpslIaPQeuzZaa9anW7 36Iw== X-Gm-Message-State: ACgBeo2TsECiyw18E8b63m4DtmXY9KTJLmEN4HJFGT0M/cIakY9LhUxX 4vkV1RxInP6rMmjLW0S/IZCN3zAEnic= X-Google-Smtp-Source: AA6agR5R7JS+PwvjO3raqPgX79bYewUm8RfaJcq/kT1isr5WF/WqZYncmh5vmqWXfYf/7p1jNenqbA== X-Received: by 2002:a17:90a:de96:b0:1fa:e427:e18e with SMTP id n22-20020a17090ade9600b001fae427e18emr2152653pjv.116.1661481901540; Thu, 25 Aug 2022 19:45:01 -0700 (PDT) Received: from macbook-pro-3.dhcp.thefacebook.com ([2620:10d:c090:400::5:15dc]) by smtp.gmail.com with ESMTPSA id k6-20020a170902ce0600b0015e8d4eb1dbsm292164plg.37.2022.08.25.19.45.00 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 25 Aug 2022 19:45:01 -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 07/15] bpf: Optimize call_rcu in non-preallocated hash map. Date: Thu, 25 Aug 2022 19:44:22 -0700 Message-Id: <20220826024430.84565-8-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-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GRWpSaM7; spf=pass (imf16.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661481902; a=rsa-sha256; cv=none; b=8eTYXimXvT+95T2aE2MiLmuLvfTuAgcsoHzsn13wfs82p2Kf+E4X9YaR9EOF9On/XozDF1 Z/PhwHxM4OR2afmh0fgFxzoMj5c3Dy6IRxnF5IlJVqOj8GdOGBxmk52A/6LyrIktL+Yvmx nS289Gcy3IC+zS5GQLZgK21CeyiECr8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661481902; 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=PaPuYu1jW6l30jyUgEzf6fCeC16QOg08yHB4Ew2HdVM=; b=TcFXSt8HeG6WwpkWX/YjxFtKWypSj6I4/Azi/xyMJtysOm52wAy/S4rx4812/T9m4tBaPv 5souVQcWmikrzD+UioHCR4gQlHWu0y3KNaQXxAQ7KlDLI0qwcU4zR1MGYyh4GJAQ5MwVER RU2PMmRpgOuDZqVcgX8is/rUgMMO/4I= X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 8592A18000E X-Rspam-User: X-Stat-Signature: sr7ptgzs97ma6dann73iiiyuinrg89nw Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GRWpSaM7; spf=pass (imf16.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1661481902-585717 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 Doing call_rcu() million times a second becomes a bottle neck. Convert non-preallocated hash map from call_rcu to SLAB_TYPESAFE_BY_RCU. The rcu critical section is no longer observed for one htab element which makes non-preallocated hash map behave just like preallocated hash map. The map elements are released back to kernel memory after observing rcu critical section. This improves 'map_perf_test 4' performance from 100k events per second to 250k events per second. bpf_mem_alloc + percpu_counter + typesafe_by_rcu provide 10x performance boost to non-preallocated hash map and make it within few % of preallocated map while consuming fraction of memory. Acked-by: Kumar Kartikeya Dwivedi Signed-off-by: Alexei Starovoitov --- kernel/bpf/hashtab.c | 8 ++++++-- kernel/bpf/memalloc.c | 2 +- tools/testing/selftests/bpf/progs/timer.c | 11 ----------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 8f68c6e13339..299ab98f9811 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -940,8 +940,12 @@ static void free_htab_elem(struct bpf_htab *htab, struct htab_elem *l) __pcpu_freelist_push(&htab->freelist, &l->fnode); } else { dec_elem_count(htab); - l->htab = htab; - call_rcu(&l->rcu, htab_elem_free_rcu); + if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH) { + l->htab = htab; + call_rcu(&l->rcu, htab_elem_free_rcu); + } else { + htab_elem_free(htab, l); + } } } diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index 29f340016e9e..c1817f14c25a 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -277,7 +277,7 @@ int bpf_mem_alloc_init(struct bpf_mem_alloc *ma, int size) return -ENOMEM; size += LLIST_NODE_SZ; /* room for llist_node */ snprintf(buf, sizeof(buf), "bpf-%u", size); - kmem_cache = kmem_cache_create(buf, size, 8, 0, NULL); + kmem_cache = kmem_cache_create(buf, size, 8, SLAB_TYPESAFE_BY_RCU, NULL); if (!kmem_cache) { free_percpu(pc); return -ENOMEM; diff --git a/tools/testing/selftests/bpf/progs/timer.c b/tools/testing/selftests/bpf/progs/timer.c index 5f5309791649..0053c5402173 100644 --- a/tools/testing/selftests/bpf/progs/timer.c +++ b/tools/testing/selftests/bpf/progs/timer.c @@ -208,17 +208,6 @@ static int timer_cb2(void *map, int *key, struct hmap_elem *val) */ bpf_map_delete_elem(map, key); - /* in non-preallocated hashmap both 'key' and 'val' are RCU - * protected and still valid though this element was deleted - * from the map. Arm this timer for ~35 seconds. When callback - * finishes the call_rcu will invoke: - * htab_elem_free_rcu - * check_and_free_timer - * bpf_timer_cancel_and_free - * to cancel this 35 second sleep and delete the timer for real. - */ - if (bpf_timer_start(&val->timer, 1ull << 35, 0) != 0) - err |= 256; ok |= 4; } return 0;