From mboxrd@z Thu Jan 1 00:00:00 1970 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 1A8D2ECAAD2 for ; Mon, 29 Aug 2022 21:57:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A676E940007; Mon, 29 Aug 2022 17:57:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A16EB6B0074; Mon, 29 Aug 2022 17:57:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8DE95940007; Mon, 29 Aug 2022 17:57:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 80B166B0073 for ; Mon, 29 Aug 2022 17:57:15 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 1E6FB80E4F for ; Mon, 29 Aug 2022 21:57:15 +0000 (UTC) X-FDA: 79853991630.03.204E214 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf06.hostedemail.com (Postfix) with ESMTP id D590C180019 for ; Mon, 29 Aug 2022 21:57:14 +0000 (UTC) Received: by mail-ej1-f47.google.com with SMTP id se27so10695707ejb.8 for ; Mon, 29 Aug 2022 14:57:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=VBA+kSIQPwArAD1ELvFTspW/7MJBxwV067Y2I2o663Y=; b=mdjwfXLzQ4b/3N7TJXfUe+7bylmjjR55mbwNwXga86/GmiBQ4w7xZYuTdQ5F6kxZWx lbIj0bX5bMBRPJDFxd15UZXQW8HC9KHoub1NGlepKa4htglHD4YRlHYBbQfMVegzfQGO zYSLAvIwHh1qJSkFIJcYDbyImN7EJFG83/T8UjKpwU/nkud49NQc+cHgG8cSluUeDT/d sU/7HH5FxX4kGciVILj2nGNzQ2qg9L0Vz8Sz1rDL2tRKUPq+rCXAe7Hu7KDLSVFKMPLl zXiPSqiBdpO6gVxfp7mt2udxhVvoK/18WV4NsVpkUuLJXmHR4fbN4+8TpfktmkA1qEKR MV/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=VBA+kSIQPwArAD1ELvFTspW/7MJBxwV067Y2I2o663Y=; b=j+JsY7X79zB9tG55lWC7lGmBFsUKa1dMllUN8rsdYPwf17XcsUoVA6kQHvtYZNXo3b JXv97TaiBU6EGqzGPvR5DIRjlI8hehtOw1dOg6CWP2NfHF/TzSM8SX8llcZfL8xdZ4cS f5faZ85rQI2+XT0x+kI0k1qGX+UpmGJc3+zKG4YJ4Po2Jlu5lL3lA589nDX/HXSOnHlj lb9tX4hp4o1kPb3ulEGYKXTdISqOTRvj3I7iBOnIYXkmMdQ7QnqBmLUrlLmI2pv+CB+F 0lo2MvrgSPtmVOuqG+pWJ45Wq6qlaQS3nFIxmCHVkvU+AS+nByL+bcCzmStiAYiwiypi 2m3A== X-Gm-Message-State: ACgBeo1HFDy0LvlEs0XHe+HdpsArEbVTlBSll8H1qEYOZmxewZSvdZF+ 9WFlcEfffv7FnJSNDOTwrsUPBdBd7jhSDAcsOkw= X-Google-Smtp-Source: AA6agR6zkN7FHpBFVpPrOBXm20LVg0YdiFNUgES6wyeHYh0zyWorO5bHhinZMWWHFCjSrWOPmaN8ZWrWl3V0S/JuZGQ= X-Received: by 2002:a17:907:2c74:b0:741:657a:89de with SMTP id ib20-20020a1709072c7400b00741657a89demr6549249ejc.58.1661810233628; Mon, 29 Aug 2022 14:57:13 -0700 (PDT) MIME-Version: 1.0 References: <20220826024430.84565-1-alexei.starovoitov@gmail.com> <20220826024430.84565-7-alexei.starovoitov@gmail.com> <75b5f42d-84f6-4227-0bf9-fb62c89217c7@iogearbox.net> In-Reply-To: <75b5f42d-84f6-4227-0bf9-fb62c89217c7@iogearbox.net> From: Alexei Starovoitov Date: Mon, 29 Aug 2022 14:57:02 -0700 Message-ID: Subject: Re: [PATCH v4 bpf-next 06/15] bpf: Optimize element count in non-preallocated hash map. To: Daniel Borkmann Cc: "David S. Miller" , Andrii Nakryiko , Tejun Heo , Kumar Kartikeya Dwivedi , Delyan Kratunov , linux-mm , bpf , Kernel Team Content-Type: text/plain; charset="UTF-8" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661810234; a=rsa-sha256; cv=none; b=bAPcFov+7KkJHMQ5lYvTEGgkS/jbN1EXDeWhQm9tLq0zVpeQK0uTWJq4xC+I9rqobj1ULE se8n5pXshtAc5K97dk61LTLiANdJa41y/EVTrdZypsvOEU0LW7b5FXXzbglIr73LnS9XTI GvZ/WPPXISgdjw0ECi5+Hlm+3FQpwPY= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=mdjwfXLz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.218.47 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=1661810234; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VBA+kSIQPwArAD1ELvFTspW/7MJBxwV067Y2I2o663Y=; b=I9GW4fQxCOXcR2sgFiE1aLzAtZyiImadWz0ltqyzu4mMLRoG/qo80aseZWqOc/njSGfLQ+ UnUlpr2ulMC1iGwYqNF5aKqxxnOEiMtuPlRznkiIQ/NInmaMwFQHm9jMCtz6rmyd0oKSxc kY1PdQzdq5w3+9y50LsB4GwrbtAN+pI= X-Rspam-User: X-Rspamd-Queue-Id: D590C180019 X-Rspamd-Server: rspam12 X-Stat-Signature: r5eibh1he3qy68hkxkomr7anzg8bkndg Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=mdjwfXLz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-HE-Tag: 1661810234-670579 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Aug 29, 2022 at 2:47 PM Daniel Borkmann wrote: > > On 8/26/22 4:44 AM, Alexei Starovoitov wrote: > > 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(). > > nit: Could we include this logic inside percpu_counter logic, or as an extended > version of it? Except the heuristic of attr->max_entries / 2 > num_online_cpus() * > PERCPU_COUNTER_BATCH which toggles between plain atomic vs percpu_counter, the > rest feel generic enough that it could also be applicable outside bpf. The heuristic is probably not generic enough and this optimization is a stop gap. It helps many cases, but doesn't solve all. It's ok for this specific large hash map to count max_entries, but we shouldn't claim generality to suggest this heuristic to anyone else. I was thinking to do a follow up and create a true generic combined percpu and atomic counter, similar to percpu_ref that can switch from percpu to atomic. But it's more of a wish-list task atm.