Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp229751imw; Mon, 4 Jul 2022 08:11:46 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s2r4NhFAKW3diwx+rnkCbjbxFuLza9S/qSAbb6e9p9Ax1RnfvWNNbYwHv7dR5ryeU61Mp1 X-Received: by 2002:a05:6402:1cc8:b0:437:a61a:5713 with SMTP id ds8-20020a0564021cc800b00437a61a5713mr39864528edb.340.1656947506409; Mon, 04 Jul 2022 08:11:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656947506; cv=none; d=google.com; s=arc-20160816; b=0ndlZl2HqGKyolYGLuW6GlHa8qmpW0+7sBW//OQv+329IygDRXTAp5TNOjA+hdoJew YxjwslMLMdOdTuB9tQgiqH13KKz6e0W3rv9t6ughWP071wXbB6EO7UKNlqUdSmYzRsQ+ 9TKC/WY9hI91xV2FsBzK0NibrRVprHknu7pcUNs0MSyu6bRbSZ4QyP3rN7rNmkcwfPBb vPbFji+dHl6oi3VGInaiLXkOkbC5oXN+9bS2sauMUIZx5JUBYQ3mUP52w3H7HM6QhiC3 ubhkE0WdWhKUPpiehySglkm07px2Rq9xu4jmUP59T+tcusfSKJNU3dZrG10KEtv2owE9 bfGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=EpoFcAL2fWdwh8cZOIq67BCzlwBRSTcqZCW5NPscTUg=; b=VwUEa+W46UmmYXZ1bWtVjo/mGUrtGEe2Nke3c0d8HG2a2wS1P7nEgivy8FmDl9s4nI cf9i8jSD7jV4Bd5N4KZU7VyXZrdPvPHgedM9qgUtYQVTD1ne41H0Mh2GDRwngznwgDTn qPclnDLVN85Z5z7zwvTRE0z2W7RSKoTWmPKDbDWiW++ZQNV4OdgFqbpPq3oPBG4C51nZ BVKWQEJQDitabBCsEOYUZzAwD22fCVpo5btPpfLK47dh2aOHVGpNQxqCHH6BotqfQiPv hBqL6zbbjGbzvVn505hHmgfPGhO+HQSba69/lZU0Ss0sK8fEvVtTotL5YHWelAVEs+rX X00Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=pKioeff8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dm14-20020a170907948e00b007262b2de87bsi41377990ejc.839.2022.07.04.08.11.21; Mon, 04 Jul 2022 08:11:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=pKioeff8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234972AbiGDPHc (ORCPT + 99 others); Mon, 4 Jul 2022 11:07:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234889AbiGDPGy (ORCPT ); Mon, 4 Jul 2022 11:06:54 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BA7E1209F for ; Mon, 4 Jul 2022 08:06:32 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id l6-20020a25bf86000000b00668c915a3f2so7808948ybk.4 for ; Mon, 04 Jul 2022 08:06:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=EpoFcAL2fWdwh8cZOIq67BCzlwBRSTcqZCW5NPscTUg=; b=pKioeff8Zt98apefL+CuPVSgFzEs6CUUotIOCnzqVqh4uiPglGtrmsSAXQvEQlr8jv 2vLVqk8DunF3s5CtCa/MHcGnylOwhqJ/4bPz0sloQhUN0DeVb+e9mfJzJ1cnsLX8IO8h XD4SzSVbQC3UvJaqjpaP5qfiqLMujcfWn04/zRVJ0kZFBK9Hzeyc192SMw4sfStOYRYV RLLDIffmYT8UaCxUd7v5DyP+x+CZAOT/PeSpZfiAb86m8Z5ql82QrfEZLyo7Ycb91TEm ABJOpBhMcgNXg9Fvj8bzJTdGyXsUy1yD+uQc+o3wYSV9cvBX/bd2cSpPJ4tEc8QCSEXA oo1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=EpoFcAL2fWdwh8cZOIq67BCzlwBRSTcqZCW5NPscTUg=; b=NicS3mk2pk7PaECPSEJFDdcYJzh+oPxqA3FuQAKuH8IGv93Q0ZEjvCQLMifGJkau5a 0AMfHzLjOQTyGFbFAqWeoPyN6iTAG/o0WwqdGM7k1oPALjCUTIs7motJpCTEhX45zlCm JrFoWje9gTVRoR3/jySTNCdAVEm9NiyeS1MHtThuYz6tG1qlTw4wa3E2vzB6aKgJhn5B I0IGNUpb/XeKT2Qcey3hgYtLMolNG9nFeGP+Bbv+bHPCIlQQqjkF2JHwzaKsvqBq+C6R MrfMX/J6iLOjPp6m7Q7Xhzarm6xd93zjRR8pkvkUGUuuC3J3tolqdXI0y7JgYyZNwKWM TS6w== X-Gm-Message-State: AJIora91CdxWmq17Q8iA3TLPrAaPcM0DRsI+aGpxNyNkjMcIsFX1tDaI aVGPWS22BgS2i+G8DhEr3V0Zn7p/kQ== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:6edf:e1bc:9a92:4ad0]) (user=elver job=sendgmr) by 2002:a81:a08d:0:b0:31c:b3d9:f93f with SMTP id x135-20020a81a08d000000b0031cb3d9f93fmr1684676ywg.492.1656947191436; Mon, 04 Jul 2022 08:06:31 -0700 (PDT) Date: Mon, 4 Jul 2022 17:05:13 +0200 In-Reply-To: <20220704150514.48816-1-elver@google.com> Message-Id: <20220704150514.48816-14-elver@google.com> Mime-Version: 1.0 References: <20220704150514.48816-1-elver@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v3 13/14] perf/hw_breakpoint: Optimize max_bp_pinned_slots() for CPU-independent task targets From: Marco Elver To: elver@google.com, Peter Zijlstra , Frederic Weisbecker , Ingo Molnar Cc: Thomas Gleixner , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Dmitry Vyukov , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, linux-perf-users@vger.kernel.org, x86@kernel.org, linux-sh@vger.kernel.org, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham 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 Running the perf benchmark with (note: more aggressive parameters vs. preceding changes, but same 256 CPUs host): | $> perf bench -r 100 breakpoint thread -b 4 -p 128 -t 512 | # Running 'breakpoint/thread' benchmark: | # Created/joined 100 threads with 4 breakpoints and 128 parallelism | Total time: 1.989 [sec] | | 38.854160 usecs/op | 4973.332500 usecs/op/cpu 20.43% [kernel] [k] queued_spin_lock_slowpath 18.75% [kernel] [k] osq_lock 16.98% [kernel] [k] rhashtable_jhash2 8.34% [kernel] [k] task_bp_pinned 4.23% [kernel] [k] smp_cfm_core_cond 3.65% [kernel] [k] bcmp 2.83% [kernel] [k] toggle_bp_slot 1.87% [kernel] [k] find_next_bit 1.49% [kernel] [k] __reserve_bp_slot We can see that a majority of the time is now spent hashing task pointers to index into task_bps_ht in task_bp_pinned(). Obtaining the max_bp_pinned_slots() for CPU-independent task targets currently is O(#cpus), and calls task_bp_pinned() for each CPU, even if the result of task_bp_pinned() is CPU-independent. The loop in max_bp_pinned_slots() wants to compute the maximum slots across all CPUs. If task_bp_pinned() is CPU-independent, we can do so by obtaining the max slots across all CPUs and adding task_bp_pinned(). To do so in O(1), use a bp_slots_histogram for CPU-pinned slots. After this optimization: | $> perf bench -r 100 breakpoint thread -b 4 -p 128 -t 512 | # Running 'breakpoint/thread' benchmark: | # Created/joined 100 threads with 4 breakpoints and 128 parallelism | Total time: 1.930 [sec] | | 37.697832 usecs/op | 4825.322500 usecs/op/cpu 19.13% [kernel] [k] queued_spin_lock_slowpath 18.21% [kernel] [k] rhashtable_jhash2 15.46% [kernel] [k] osq_lock 6.27% [kernel] [k] toggle_bp_slot 5.91% [kernel] [k] task_bp_pinned 5.05% [kernel] [k] smp_cfm_core_cond 1.78% [kernel] [k] update_sg_lb_stats 1.36% [kernel] [k] llist_reverse_order 1.34% [kernel] [k] find_next_bit 1.19% [kernel] [k] bcmp Suggesting that time spent in task_bp_pinned() has been reduced. However, we're still hashing too much, which will be addressed in the subsequent change. Signed-off-by: Marco Elver Reviewed-by: Dmitry Vyukov --- v3: * Update hw_breakpoint_is_used() to include global cpu_pinned. v2: * New patch. --- kernel/events/hw_breakpoint.c | 57 ++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/kernel/events/hw_breakpoint.c b/kernel/events/hw_breakpoint.c index 03ebecf048c0..a489f31fe147 100644 --- a/kernel/events/hw_breakpoint.c +++ b/kernel/events/hw_breakpoint.c @@ -64,6 +64,9 @@ static struct bp_cpuinfo *get_bp_info(int cpu, enum bp_type_idx type) return per_cpu_ptr(bp_cpuinfo + type, cpu); } +/* Number of pinned CPU breakpoints globally. */ +static struct bp_slots_histogram cpu_pinned[TYPE_MAX]; + /* Keep track of the breakpoints attached to tasks */ static struct rhltable task_bps_ht; static const struct rhashtable_params task_bps_ht_params = { @@ -194,6 +197,10 @@ static __init int init_breakpoint_slots(void) goto err; } } + for (i = 0; i < TYPE_MAX; i++) { + if (!bp_slots_histogram_alloc(&cpu_pinned[i], i)) + goto err; + } return 0; err: @@ -203,6 +210,8 @@ static __init int init_breakpoint_slots(void) if (err_cpu == cpu) break; } + for (i = 0; i < TYPE_MAX; i++) + bp_slots_histogram_free(&cpu_pinned[i]); return -ENOMEM; } @@ -270,6 +279,9 @@ static unsigned int max_task_bp_pinned(int cpu, enum bp_type_idx type) /* * Count the number of breakpoints of the same type and same task. * The given event must be not on the list. + * + * If @cpu is -1, but the result of task_bp_pinned() is not CPU-independent, + * returns a negative value. */ static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type) { @@ -288,9 +300,18 @@ static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type) goto out; rhl_for_each_entry_rcu(iter, pos, head, hw.bp_list) { - if (find_slot_idx(iter->attr.bp_type) == type && - (iter->cpu < 0 || cpu == iter->cpu)) - count += hw_breakpoint_weight(iter); + if (find_slot_idx(iter->attr.bp_type) != type) + continue; + + if (iter->cpu >= 0) { + if (cpu == -1) { + count = -1; + goto out; + } else if (cpu != iter->cpu) + continue; + } + + count += hw_breakpoint_weight(iter); } out: @@ -316,6 +337,19 @@ max_bp_pinned_slots(struct perf_event *bp, enum bp_type_idx type) int pinned_slots = 0; int cpu; + if (bp->hw.target && bp->cpu < 0) { + int max_pinned = task_bp_pinned(-1, bp, type); + + if (max_pinned >= 0) { + /* + * Fast path: task_bp_pinned() is CPU-independent and + * returns the same value for any CPU. + */ + max_pinned += bp_slots_histogram_max(&cpu_pinned[type], type); + return max_pinned; + } + } + for_each_cpu(cpu, cpumask) { struct bp_cpuinfo *info = get_bp_info(cpu, type); int nr; @@ -366,8 +400,11 @@ toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type, /* Pinned counter cpu profiling */ if (!bp->hw.target) { + struct bp_cpuinfo *info = get_bp_info(bp->cpu, type); + lockdep_assert_held_write(&bp_cpuinfo_sem); - get_bp_info(bp->cpu, type)->cpu_pinned += weight; + bp_slots_histogram_add(&cpu_pinned[type], info->cpu_pinned, weight); + info->cpu_pinned += weight; return 0; } @@ -804,6 +841,18 @@ bool hw_breakpoint_is_used(void) } } + for (int type = 0; type < TYPE_MAX; ++type) { + for (int slot = 0; slot < hw_breakpoint_slots_cached(type); ++slot) { + /* + * Warn, because if there are CPU pinned counters, + * should never get here; bp_cpuinfo::cpu_pinned should + * be consistent with the global cpu_pinned histogram. + */ + if (WARN_ON(atomic_read(&cpu_pinned[type].count[slot]))) + return true; + } + } + return false; } -- 2.37.0.rc0.161.g10f37bed90-goog