Received: by 2002:a19:771d:0:0:0:0:0 with SMTP id s29csp1246490lfc; Wed, 1 Jun 2022 12:59:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGe9pa3FqlO9rE6Nukvy66r6mPGFpPdsSgDhH0vhTipZAali7C6QpcBvGXr+rW+67LTbI6 X-Received: by 2002:a17:902:cec4:b0:163:fa31:9431 with SMTP id d4-20020a170902cec400b00163fa319431mr1096815plg.73.1654113556509; Wed, 01 Jun 2022 12:59:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654113556; cv=none; d=google.com; s=arc-20160816; b=jmVgHnwAOx/e3hV2qdlKeAhcq8Zgmoc9wBno2Y3J9NXDTclM7sWgYPWI+QikCxdxBb TnnYHx2c8Gs70TehybGc1GhBoUny4kkSmAnTKDdV95g4D8eEQuobJCv+XFNz/BJjJeMt aXQH1Fv6FV4AbjxvM4qCRI3j2C6D59220FdOknxr00BCw1F1wZCqdTKta8I2wJHOmZQ4 3hQuXdd+pJAAw/T8+/gmBl9gQHsnVoqGwnemvN9wbPJSypt2uZPUHo3VTnq2D2FjbN7T EAtalFSLqvTKL7zoa8Dg+6GRclQZgFFun9Bal95FKQiCkxYyYevwOz652dLNBk3D3oNQ jUzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=17jbuB5ApQ2iSURu9wqbSRYsL+H5kiM4U+n5E3wCBBM=; b=aL2lr9VGOLsq5EYJJ32imWKVQmAIPuHUf75iS2FUBQPnpbY+olRCerA4kMG7pU9ZD4 6IyH/7raXX+v6MhEHGH5yitjwrO929ZVfkA7A2Nd9zsb2CYbDqr/zAL5C1cfl2hE1MP9 qz8NeYLO1SoHiX7ZJDV4C4dESZbq/2niCjsMWtWH66CH+6Vz4irpyZzlPSwr62r413DX PwzEnVv1W02YLjkkyoMxvU8CSDF9jKmPJbytVFmR/QVkQ4R8Q4wIqwN/KHFTlnsxVP/a SyPYaya4DvP9bEDDb+JERC7bo3EQh2wU3Vn/m+lnV0RAUziRenrTR1jAPUtpFuQZnByP c5mQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=YYZpXVz7; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id y22-20020a1709029b9600b001617784ea88si3287459plp.356.2022.06.01.12.59.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 12:59:16 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=YYZpXVz7; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 976C521AC45; Wed, 1 Jun 2022 12:18:08 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237049AbiFAImI (ORCPT + 99 others); Wed, 1 Jun 2022 04:42:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235456AbiFAImE (ORCPT ); Wed, 1 Jun 2022 04:42:04 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D111A5D1B3 for ; Wed, 1 Jun 2022 01:42:02 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id h1so1126715plf.11 for ; Wed, 01 Jun 2022 01:42:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=17jbuB5ApQ2iSURu9wqbSRYsL+H5kiM4U+n5E3wCBBM=; b=YYZpXVz73I9bIFut7IGyzJAak1gGAmGFP83a1uzg+MbXHP4zJnmYRW9bqGEDjGyR2I HJXNd8uEjusQFPFgN6CF3/Or0xXd9WObAbppzV4d8Kjakf1n5Bqq541sZNKKbrak0Rf1 hrDXWpV3hBS7wdFZwABFJY2YTT0OEV3UliHm8En96ewOikvWUC24a3lWC5aEIyjl1Rq3 Pa6mrpOFzFDUQXjNa0LWgt2GDywWPnSzh6tHJvyuW2v+Jyn5xhWaNAl/abKtz2/oxhuw DEZnaVlwaujCN2IiKgLcW9S1QrinBOu344RdmTHD3dq3DqOWnxjSb1oExOmkyerAVMQ+ kWNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=17jbuB5ApQ2iSURu9wqbSRYsL+H5kiM4U+n5E3wCBBM=; b=oUFQEkJOW2Tc98350Kfo2dkWyYbTCrJieC6+tl3xfpQXmCWuOzpK9kqX5+giOXTYbq XijhTlABRNAOStROpW1TUrXrXboIoouGRDQk+T4nrTsSq0icgtFFCUjV0RR5U2wZNjIH 7TTNyqe6/EeXYVXg71RKWTEZg3kUBboPI1iFzAsAIyL0hxYvuT44C/BFGymb2Y6JOH79 F6QskvrLOArO5o4X4odRu7oTTstWjDjPpS5zFLT268qcfpwO7HAIzL90e7o48fl77Zr+ e3hOYwi0Qh23E7O0t8kCG6+6Z/OYhuBxhj0lv0c2j5nqAxzDgPZUr0hEKon8TuKmPBw/ 7UfQ== X-Gm-Message-State: AOAM530KAm40aY/p7m33gnWlsqyYGQ6npQLaCu6Gdl6HCV9V7ilfqrdo 4/sQ0yKe+IqXcxDz/NB9T9E0ew== X-Received: by 2002:a17:902:8c90:b0:163:b989:3cd6 with SMTP id t16-20020a1709028c9000b00163b9893cd6mr17865260plo.170.1654072922062; Wed, 01 Jun 2022 01:42:02 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.241]) by smtp.gmail.com with ESMTPSA id u11-20020a63d34b000000b003c14af505f6sm827952pgi.14.2022.06.01.01.41.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Jun 2022 01:42:01 -0700 (PDT) From: Feng zhou To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, wangdongdong.6@bytedance.com, cong.wang@bytedance.com, zhouchengming@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH v4 0/2] Optimize performance of update hash-map when free is zero Date: Wed, 1 Jun 2022 16:41:47 +0800 Message-Id: <20220601084149.13097-1-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Feng Zhou We encountered bad case on big system with 96 CPUs that alloc_htab_elem() would last for 1ms. The reason is that after the prealloc hashtab has no free elems, when trying to update, it will still grab spin_locks of all cpus. If there are multiple update users, the competition is very serious. 0001: Add is_empty to check whether the free list is empty or not before taking the lock. 0002: Add benchmark to reproduce this worst case. Changelog: v3->v4: Addressed comments from Daniel Borkmann. - Use READ_ONCE/WRITE_ONCE. some details in here: https://lore.kernel.org/all/20220530091340.53443-1-zhoufeng.zf@bytedance.com/ v2->v3: Addressed comments from Alexei Starovoitov, Andrii Nakryiko. - Adjust the way the benchmark is tested. - Adjust the code format. some details in here: https://lore.kernel.org/all/20220524075306.32306-1-zhoufeng.zf@bytedance.com/T/ v1->v2: Addressed comments from Alexei Starovoitov. - add a benchmark to reproduce the issue. - Adjust the code format that avoid adding indent. some details in here: https://lore.kernel.org/all/877ac441-045b-1844-6938-fcaee5eee7f2@bytedance.com/T/ Feng Zhou (2): bpf: avoid grabbing spin_locks of all cpus when no free elems selftest/bpf/benchs: Add bpf_map benchmark kernel/bpf/percpu_freelist.c | 28 +++++- kernel/bpf/percpu_freelist.h | 1 + tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bench.c | 2 + .../benchs/bench_bpf_hashmap_full_update.c | 96 +++++++++++++++++++ .../run_bench_bpf_hashmap_full_update.sh | 11 +++ .../bpf/progs/bpf_hashmap_full_update_bench.c | 40 ++++++++ 7 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_bpf_hashmap_full_update.sh create mode 100644 tools/testing/selftests/bpf/progs/bpf_hashmap_full_update_bench.c -- 2.20.1