Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp423042rwe; Thu, 1 Sep 2022 01:37:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR7UeuihMjDH/32gLFW+/nyC3khzIHo9cID5T+Npmhj0Lr8t+FmKaWvTy/PvIWzDv853CTy9 X-Received: by 2002:a63:69c7:0:b0:41c:590a:62dc with SMTP id e190-20020a6369c7000000b0041c590a62dcmr24462374pgc.388.1662021431059; Thu, 01 Sep 2022 01:37:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662021431; cv=none; d=google.com; s=arc-20160816; b=oR9S8zzLAENBmRQDBiyBoRQw+TTLl3iu4F2pC0J4/NVhrTB/O7AYxYe3/yPrkBAmLq wRw3Yo8n4/Qk3tVnuo9v0U6JNk8fwXkoM5NZ3/FI5In+ZChF0cF2h+PFREVvDT7gpKEX 2bYFX/JJ/cS5/f3AsnlzOoTG/Uzt276ymXZ+3oh3NB6hysbI9D3Dm++NqtN8hp0zSMv3 jaAprgGyAxvkve7UOgAoXHjRI0yI9qxGuUOk+uHDjVCx7nOqbE2qfcDcWHwQnuXKE8zD J0c2REfIrkmcWNjBnEs0Lcx2DA+OKRMDheMfijWT+oIhFnfJmhM3u1xi5tEoi0O27abC nGTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=S44+nghuDguwn5b7/VaiSHT77Bb5G9PAQN3aR6NjenA=; b=BM6LP7Q3W52EFeduPsgo6/qRfmfk8Xv4YTw2immN7dsWHqiGUE9/I9zraTZXU4tJK8 iZg5/x8rN31ASfCi39Zf5YVJZdxAjs3FUmMcp7NJUtXAsgtbA1J26qijgc20oLOATrwR h74OajlAWwjfvFF4Cwp/dew5tRCLBviZju9/2D3gorS8whxml9B0rlc5qU0KvJy8OieO /VgaKaJDnLhWu/SMUKomSgfUZRteargv4G/J/01K1bN3sQMiOvGYwgrCcWdQ9xraUg/w v7bgMAgBbibCKfqyz4jNvd3LL/WnNhDjBrsNgBYOTOLiLxEVpQ+ZfY9A0nIPPzom3pYy nE3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=p1bW+6vq; dkim=neutral (no key) header.i=@linutronix.de header.b=Wtuh1mbJ; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bu4-20020a17090aee4400b001f74f8ee43asi3789759pjb.25.2022.09.01.01.36.37; Thu, 01 Sep 2022 01:37:11 -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=@linutronix.de header.s=2020 header.b=p1bW+6vq; dkim=neutral (no key) header.i=@linutronix.de header.b=Wtuh1mbJ; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233862AbiIAIMt (ORCPT + 99 others); Thu, 1 Sep 2022 04:12:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233404AbiIAIMp (ORCPT ); Thu, 1 Sep 2022 04:12:45 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3D2812BF4A; Thu, 1 Sep 2022 01:12:43 -0700 (PDT) Date: Thu, 01 Sep 2022 08:12:40 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1662019961; h=from:from:sender:sender:reply-to: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S44+nghuDguwn5b7/VaiSHT77Bb5G9PAQN3aR6NjenA=; b=p1bW+6vqdiQTiEVhqdCHu6K/9SjriSo78jMuNkdSUs3bxRFUvP/SCmRQb0yW9C0mMbr12J RbR8MQMjqocRij2KwrQjw1c37bllFw4A0mpNl7ChSsr8I5IIC9DhJzWWI1ebt3GsStejpn luvE9iI6HHvYcubia094bQvhlkN6GL8QXJLU4t1H57NlimRTdcB5DC4mi2QENVijfGeIYn XmORMcmzn9ejsMLdZ6HhS+L7hF/8W6qxW/h48h9APxctu429tDfbdViYcsgdg/fvD+YmIn RFGQVL7k77VUjEt/U5zxf8BxQ/8xIHHSyMY0phZTB0Szro3hs160hoo98xN5HQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1662019961; h=from:from:sender:sender:reply-to: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S44+nghuDguwn5b7/VaiSHT77Bb5G9PAQN3aR6NjenA=; b=Wtuh1mbJRjtkkkbC2RdQVsLQFXZLj4p6467cVp9Fz32JfB1Zo2DW+1rmJ6o+MojZRbiU0j DzYVUcYb+k/1YTDA== From: "tip-bot2 for Marco Elver" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: perf/core] perf/hw_breakpoint: Optimize max_bp_pinned_slots() for CPU-independent task targets Cc: Marco Elver , "Peter Zijlstra (Intel)" , Dmitry Vyukov , Ian Rogers , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220829124719.675715-14-elver@google.com> References: <20220829124719.675715-14-elver@google.com> MIME-Version: 1.0 Message-ID: <166201996014.401.12224348433665300054.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 The following commit has been merged into the perf/core branch of tip: Commit-ID: 9b1933b864a10e7f66b06d10c39217142baed28b Gitweb: https://git.kernel.org/tip/9b1933b864a10e7f66b06d10c39217142baed28b Author: Marco Elver AuthorDate: Mon, 29 Aug 2022 14:47:18 +02:00 Committer: Peter Zijlstra CommitterDate: Tue, 30 Aug 2022 10:56:24 +02:00 perf/hw_breakpoint: Optimize max_bp_pinned_slots() for CPU-independent task targets 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 Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Dmitry Vyukov Acked-by: Ian Rogers Link: https://lore.kernel.org/r/20220829124719.675715-14-elver@google.com --- 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 03ebecf..a489f31 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 @@ err: 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; }