Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp420271rwe; Thu, 1 Sep 2022 01:33:07 -0700 (PDT) X-Google-Smtp-Source: AA6agR5gBcXAgAEGz3aZ3hPz0RxM7dmMBKbBAzVP8B1Byr5LMn/6DSu5QS09fp2QVg7LqpJ/fHs0 X-Received: by 2002:a17:907:7f0b:b0:731:b81a:1912 with SMTP id qf11-20020a1709077f0b00b00731b81a1912mr23013447ejc.8.1662021186852; Thu, 01 Sep 2022 01:33:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662021186; cv=none; d=google.com; s=arc-20160816; b=SKYjiBYH4QmXzfjbBFQNfbfKVM/zIr/kx5jjhwKw1/8+rpUEOLZo3ZJZA/XctHjZWW 2N8dG3+FijfC/fzInZrL74z9kBP6ZLembi6tF5nlWEJiI6LxMh8kkkdybJY3Nk7tTp86 sIZ4csyw/U1fG6MPbYoL3RI61JDFiEUMjjXltrfyGjAzxrIds7hkLBKpV7nGDsfU1g4e 1cCqbXmT3kBCnaibfimBxPS9Pqvpp3op1zydSTVhPSxF2lQZlVW1bsUhx60ioACAHR3/ plQFXKxlmUfEt01486/vrGtUobn2novQChaivJBUYiCkaMKxJ5+q0c3Jr2mIMPvVCFRM 444g== 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=mgGRg/STS361PDYmiOe4lr55h2/AVhNCvLkPlfFOwWc=; b=XcJX9qU58EgNbQ0u27f8lIosCdDq3/IikJpFHsO4o+o38YZQ1RaZuROZJYN1zFDv7u 5YNW4O8c/STb/vVExaJA+5YJWTsMKrehrb3pZCYCUC2Qj5Qgic9JT+dykdKHJzgFtB9q f21ISoLR5TXAJdE1yQNx4Y+8X29TFfTjTFrEzxyQx2WHUFRAATA7dNTPVMDsDxPItXOm ve+ji6q4BycBvvStUvrZ0woPWcWKTm9Wg0TmcgCvm3cC26uM2+IWwDj+Myu/h8w9soas ylLaCjgPsarmKdwTSMIZflvYrszsT9/a17DS0MhwriA8dF/fnQGIjBiakyXtd9PhmLQV ksaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=u1aYPcXc; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 hq30-20020a1709073f1e00b00738346168dcsi14602092ejc.811.2022.09.01.01.32.37; Thu, 01 Sep 2022 01:33:06 -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=u1aYPcXc; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 S234271AbiIAINu (ORCPT + 99 others); Thu, 1 Sep 2022 04:13:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234008AbiIAIM6 (ORCPT ); Thu, 1 Sep 2022 04:12:58 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A60F12BF5D; Thu, 1 Sep 2022 01:12:55 -0700 (PDT) Date: Thu, 01 Sep 2022 08:12:52 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1662019973; 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=mgGRg/STS361PDYmiOe4lr55h2/AVhNCvLkPlfFOwWc=; b=u1aYPcXcisIRi2xAmiIB55A7B4sBoT3/azfxKceBMfiygPubK//HhWIHREYdY9eDpssHDn Ckbs5mvduyq4bZ9nJ2IXxZyPeJx2S0UEkIHmKB1jvy5gJFMUd7zn2iGDX4GBQKOYY7VgDK JIlJbRVE0D7J3ewinZp1QVU4KQ6ZTiD3PdsK0Sd5jf6cW01Zj6dFr7VI4ioFQiNOzOIpOw 9AvpNaRMzuu3HsfeMSZVr37QuEk2u61C62So46qP/XUQuZ4v5F1aCNsVup/hQwbFVdjsDP w9bxs43W0YsSBmcJoNNjJFXYwGSgmKbcP7cbVP5oJo/zWGpQP5R+aVhYsgsaaA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1662019973; 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=mgGRg/STS361PDYmiOe4lr55h2/AVhNCvLkPlfFOwWc=; b=eyLlVtVwtGAThe2L1rSj/KrXhJH9a2tYA6yb7w4a+eu+U+iT6X9kLHizLz7g8Z11f3Gn5V pro4V4YSw9mXLbDw== 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: Provide hw_breakpoint_is_used() and use in test Cc: Marco Elver , "Peter Zijlstra (Intel)" , Dmitry Vyukov , Ian Rogers , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220829124719.675715-3-elver@google.com> References: <20220829124719.675715-3-elver@google.com> MIME-Version: 1.0 Message-ID: <166201997242.401.15301297317070829692.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: c5b81449f915a28bb9c7725e53aebab3ba39b4a2 Gitweb: https://git.kernel.org/tip/c5b81449f915a28bb9c7725e53aebab3ba39b4a2 Author: Marco Elver AuthorDate: Mon, 29 Aug 2022 14:47:07 +02:00 Committer: Peter Zijlstra CommitterDate: Tue, 30 Aug 2022 10:56:20 +02:00 perf/hw_breakpoint: Provide hw_breakpoint_is_used() and use in test Provide hw_breakpoint_is_used() to check if breakpoints are in use on the system. Use it in the KUnit test to verify the global state before and after a test case. 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-3-elver@google.com --- include/linux/hw_breakpoint.h | 3 +++- kernel/events/hw_breakpoint.c | 29 +++++++++++++++++++++++++++++- kernel/events/hw_breakpoint_test.c | 12 +++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/include/linux/hw_breakpoint.h b/include/linux/hw_breakpoint.h index 78dd703..a3fb846 100644 --- a/include/linux/hw_breakpoint.h +++ b/include/linux/hw_breakpoint.h @@ -74,6 +74,7 @@ register_wide_hw_breakpoint(struct perf_event_attr *attr, extern int register_perf_hw_breakpoint(struct perf_event *bp); extern void unregister_hw_breakpoint(struct perf_event *bp); extern void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events); +extern bool hw_breakpoint_is_used(void); extern int dbg_reserve_bp_slot(struct perf_event *bp); extern int dbg_release_bp_slot(struct perf_event *bp); @@ -121,6 +122,8 @@ register_perf_hw_breakpoint(struct perf_event *bp) { return -ENOSYS; } static inline void unregister_hw_breakpoint(struct perf_event *bp) { } static inline void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events) { } +static inline bool hw_breakpoint_is_used(void) { return false; } + static inline int reserve_bp_slot(struct perf_event *bp) {return -ENOSYS; } static inline void release_bp_slot(struct perf_event *bp) { } diff --git a/kernel/events/hw_breakpoint.c b/kernel/events/hw_breakpoint.c index f32320a..fd5cd1f 100644 --- a/kernel/events/hw_breakpoint.c +++ b/kernel/events/hw_breakpoint.c @@ -604,6 +604,35 @@ void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events) } EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint); +/** + * hw_breakpoint_is_used - check if breakpoints are currently used + * + * Returns: true if breakpoints are used, false otherwise. + */ +bool hw_breakpoint_is_used(void) +{ + int cpu; + + if (!constraints_initialized) + return false; + + for_each_possible_cpu(cpu) { + for (int type = 0; type < TYPE_MAX; ++type) { + struct bp_cpuinfo *info = get_bp_info(cpu, type); + + if (info->cpu_pinned) + return true; + + for (int slot = 0; slot < nr_slots[type]; ++slot) { + if (info->tsk_pinned[slot]) + return true; + } + } + } + + return false; +} + static struct notifier_block hw_breakpoint_exceptions_nb = { .notifier_call = hw_breakpoint_exceptions_notify, /* we need to be notified first */ diff --git a/kernel/events/hw_breakpoint_test.c b/kernel/events/hw_breakpoint_test.c index 433c5c4..5ced822 100644 --- a/kernel/events/hw_breakpoint_test.c +++ b/kernel/events/hw_breakpoint_test.c @@ -294,7 +294,14 @@ static struct kunit_case hw_breakpoint_test_cases[] = { static int test_init(struct kunit *test) { /* Most test cases want 2 distinct CPUs. */ - return num_online_cpus() < 2 ? -EINVAL : 0; + if (num_online_cpus() < 2) + return -EINVAL; + + /* Want the system to not use breakpoints elsewhere. */ + if (hw_breakpoint_is_used()) + return -EBUSY; + + return 0; } static void test_exit(struct kunit *test) @@ -308,6 +315,9 @@ static void test_exit(struct kunit *test) kthread_stop(__other_task); __other_task = NULL; } + + /* Verify that internal state agrees that no breakpoints are in use. */ + KUNIT_EXPECT_FALSE(test, hw_breakpoint_is_used()); } static struct kunit_suite hw_breakpoint_test_suite = {