From patchwork Fri Aug 19 21:42:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12949281 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 2B05FC32771 for ; Fri, 19 Aug 2022 21:45:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B92D26B0073; Fri, 19 Aug 2022 17:45:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B41708D0005; Fri, 19 Aug 2022 17:45:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E3278D0003; Fri, 19 Aug 2022 17:45:07 -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 88E626B0073 for ; Fri, 19 Aug 2022 17:45:07 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 6DBCC1A1B45 for ; Fri, 19 Aug 2022 21:45:07 +0000 (UTC) X-FDA: 79817673054.22.2A2DBE2 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf05.hostedemail.com (Postfix) with ESMTP id F134C100019 for ; Fri, 19 Aug 2022 21:43:01 +0000 (UTC) Received: by mail-pg1-f172.google.com with SMTP id 24so4646704pgr.7 for ; Fri, 19 Aug 2022 14:43:01 -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=R7CCPPKWPgzyGKsplgrBfSdH/nOmKmQ7KDErBAIDa18=; b=W109X9RhtblogGbY/aVlPs1Y4LZp8SnbcvVkgcoF8tC4PawP3w35jFiqmf99IA4VfR lNl6t9GtQBMfrrLFXxkGSfCj5BGXJg/Afiw91kdLkjh44GEkUw3CyszIGlkf2dAxLOV8 IsTsIhRf6AIjxIS6Or2OPut3RHRKf/6bv0PxJYrg5tNq8EQvIRtTqGZDMwEegLFAjJlu R7EXthDU0ZmhTptdSHzFdiW57EwqU0bMHyHzcgiV96oBiqNly4eCk+bDi753HCZ6s8cF XSzINLFY2+KEGJonLOROpd4rFrRv8rvHafNS0iH3L/JZrlU8AtjdKMZ35S/7t/AdBMuV dhvA== 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=R7CCPPKWPgzyGKsplgrBfSdH/nOmKmQ7KDErBAIDa18=; b=g38F/m/algpRX8oLJW0fgYqDC6d099Fdtp0EseyV2qvpiaY/qcLaAo2uWW1zIHdwYx SY6n4HLlw8TRxQf/1Wag2lMYesB7nJld/SX1ibMr5w1xs7CYmRPBA+KaOlpCUgjtpZKr 9injzMyaUqZdLCWyhT5Y15/rux4oWSSFhdLSeGjC3PU7+z6184TdKUTI3NrisCn3TjcR JHoD2+Q1mp4C6b2jk4CtD5B1z0Z9SxuW2LEWCKNQxGXLXq8afbv4UcaCmFQDdlMg4iF5 xAhGLSZKQ9s5ZfgUxj7zC47oaYHtT++tiNF52XAl54wQNW/shfcp4w6FYPT6i0BBo1tR Potg== X-Gm-Message-State: ACgBeo2W0xZnDJLD4DshbKy9rvlq6FeZ5V2fHHWM/4hZCR0gFu7M8xVC 9PhXYn32i6D4deaLNybHWMQ= X-Google-Smtp-Source: AA6agR601MFqy59cpbHMqgqpRSfKpiJoe5v62VbSTaZ9Yb5MeIfvR/bq2hylOcfwGkv2xgu4gTHZ/Q== X-Received: by 2002:a62:1c81:0:b0:52f:ccb5:9de1 with SMTP id c123-20020a621c81000000b0052fccb59de1mr9648921pfc.45.1660945380983; Fri, 19 Aug 2022 14:43:00 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::1:c4b1]) by smtp.gmail.com with ESMTPSA id e13-20020a17090301cd00b0016ef6c2375fsm3620437plh.217.2022.08.19.14.42.59 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 19 Aug 2022 14:43:00 -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 07/15] bpf: Optimize call_rcu in non-preallocated hash map. Date: Fri, 19 Aug 2022 14:42:24 -0700 Message-Id: <20220819214232.18784-8-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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660945382; a=rsa-sha256; cv=none; b=pJMAtlaX/XnsZco/4LDiUnp425pV/MRM5oa3CObGf6wKO+4dWvrsnQFNRTsKSLnhbmxu2c 4825dEORpBFHWQzRxdlTnZe276/elpQHkVV44PckBsPh7itGtHZhKuhwZ7mn+j3QsZyZQn ZCdaeajpmZtrGbEP9LNgpDRqxaxkJRw= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=W109X9Rh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.215.172 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=1660945381; 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=R7CCPPKWPgzyGKsplgrBfSdH/nOmKmQ7KDErBAIDa18=; b=1oX6RwzeD+X8VZMQTrmAK/SEcSVurD5RMzi87IUHaDm+NVisgjBeqz12xj1yw5wZyOMFy5 ujeltl4uyu72zNHhICB3pIp/vhvhCuvG7RaSX2fRCtof5iLKXShi6eeCJAMlhzTvvb3MwJ q8u2OYm9UPT7ffTn+oyU+qv3egATu9s= X-Stat-Signature: hwd1ppoen8acmagsfjq6rt7rmpj8yrj4 X-Rspamd-Queue-Id: F134C100019 X-Rspam-User: Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=W109X9Rh; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-Rspamd-Server: rspam11 X-HE-Tag: 1660945381-258557 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. 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 293380eaea41..cfa07f539eda 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -276,7 +276,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;