From nobody Mon Apr 6 23:08:49 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87BBAECAAD8 for ; Thu, 1 Sep 2022 08:13:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233996AbiIAINQ (ORCPT ); Thu, 1 Sep 2022 04:13:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233832AbiIAIMt (ORCPT ); Thu, 1 Sep 2022 04:12:49 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 656D51299E6; Thu, 1 Sep 2022 01:12:47 -0700 (PDT) Date: Thu, 01 Sep 2022 08:12:44 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1662019965; 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=nfuQ0Vwg/5vIFBggno4nVA/RHScWxPWw8paaD15Yw48=; b=CuAYgNsVsdGuN8NWMi/Ki+8QbtH6/trpvSK9DPwHY1RU4EyTEzgK6q6k5rnlZiohrFI7bD hbSHZqUP1oS+bJB8taYMberDGpS4jiWjAAld79kDu73xJRBe9R/lhFktGmqvu+jrszIKWh jQhByxM7u1QWS9oSiXm8DJa1ZkFTHjdj6hcWN5PTtjyMNC5Zz3JfC3W4TLuHVPPovrKWvS 7Lniv/fqK4T4Tw4hP5X7Rry0WAzV970MLK5WlEUtvEmlU6hnjByc4qcLid6gPBmzueVwZX M4+lyOxNfshc92fR14QFzCDk8UwzZBhpBNvCpx7KQZ2cnZzpnsioScsn1PjzvQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1662019965; 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=nfuQ0Vwg/5vIFBggno4nVA/RHScWxPWw8paaD15Yw48=; b=c6YDvWlE+JLmMiw7hDGzEQFF9S+bVZMhYPzHc9JRC/n6a5qTN0lTvEuWzWR1kgbmxsil8W x0S0vxGEJh+mlrAw== 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] powerpc/hw_breakpoint: Avoid relying on caller synchronization Cc: Dmitry Vyukov , Marco Elver , "Peter Zijlstra (Intel)" , Ian Rogers , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220829124719.675715-10-elver@google.com> References: <20220829124719.675715-10-elver@google.com> MIME-Version: 1.0 Message-ID: <166201996465.401.14227071145123098066.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: quoted-printable 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: f95e5a3d59011eec1257d0e76de1e1f8969d426f Gitweb: https://git.kernel.org/tip/f95e5a3d59011eec1257d0e76de1e1f89= 69d426f Author: Marco Elver AuthorDate: Mon, 29 Aug 2022 14:47:14 +02:00 Committer: Peter Zijlstra CommitterDate: Tue, 30 Aug 2022 10:56:23 +02:00 powerpc/hw_breakpoint: Avoid relying on caller synchronization Internal data structures (cpu_bps, task_bps) of powerpc's hw_breakpoint implementation have relied on nr_bp_mutex serializing access to them. Before overhauling synchronization of kernel/events/hw_breakpoint.c, introduce 2 spinlocks to synchronize cpu_bps and task_bps respectively, thus avoiding reliance on callers synchronizing powerpc's hw_breakpoint. Reported-by: Dmitry Vyukov Signed-off-by: Marco Elver Signed-off-by: Peter Zijlstra (Intel) Acked-by: Dmitry Vyukov Acked-by: Ian Rogers Link: https://lore.kernel.org/r/20220829124719.675715-10-elver@google.com --- arch/powerpc/kernel/hw_breakpoint.c | 53 +++++++++++++++++++++------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/arch/powerpc/kernel/hw_breakpoint.c b/arch/powerpc/kernel/hw_b= reakpoint.c index 2669f80..8db1a15 100644 --- a/arch/powerpc/kernel/hw_breakpoint.c +++ b/arch/powerpc/kernel/hw_breakpoint.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include =20 @@ -129,7 +130,14 @@ struct breakpoint { bool ptrace_bp; }; =20 +/* + * While kernel/events/hw_breakpoint.c does its own synchronization, we ca= nnot + * rely on it safely synchronizing internals here; however, we can rely on= it + * not requesting more breakpoints than available. + */ +static DEFINE_SPINLOCK(cpu_bps_lock); static DEFINE_PER_CPU(struct breakpoint *, cpu_bps[HBP_NUM_MAX]); +static DEFINE_SPINLOCK(task_bps_lock); static LIST_HEAD(task_bps); =20 static struct breakpoint *alloc_breakpoint(struct perf_event *bp) @@ -174,7 +182,9 @@ static int task_bps_add(struct perf_event *bp) if (IS_ERR(tmp)) return PTR_ERR(tmp); =20 + spin_lock(&task_bps_lock); list_add(&tmp->list, &task_bps); + spin_unlock(&task_bps_lock); return 0; } =20 @@ -182,6 +192,7 @@ static void task_bps_remove(struct perf_event *bp) { struct list_head *pos, *q; =20 + spin_lock(&task_bps_lock); list_for_each_safe(pos, q, &task_bps) { struct breakpoint *tmp =3D list_entry(pos, struct breakpoint, list); =20 @@ -191,6 +202,7 @@ static void task_bps_remove(struct perf_event *bp) break; } } + spin_unlock(&task_bps_lock); } =20 /* @@ -200,12 +212,17 @@ static void task_bps_remove(struct perf_event *bp) static bool all_task_bps_check(struct perf_event *bp) { struct breakpoint *tmp; + bool ret =3D false; =20 + spin_lock(&task_bps_lock); list_for_each_entry(tmp, &task_bps, list) { - if (!can_co_exist(tmp, bp)) - return true; + if (!can_co_exist(tmp, bp)) { + ret =3D true; + break; + } } - return false; + spin_unlock(&task_bps_lock); + return ret; } =20 /* @@ -215,13 +232,18 @@ static bool all_task_bps_check(struct perf_event *bp) static bool same_task_bps_check(struct perf_event *bp) { struct breakpoint *tmp; + bool ret =3D false; =20 + spin_lock(&task_bps_lock); list_for_each_entry(tmp, &task_bps, list) { if (tmp->bp->hw.target =3D=3D bp->hw.target && - !can_co_exist(tmp, bp)) - return true; + !can_co_exist(tmp, bp)) { + ret =3D true; + break; + } } - return false; + spin_unlock(&task_bps_lock); + return ret; } =20 static int cpu_bps_add(struct perf_event *bp) @@ -234,6 +256,7 @@ static int cpu_bps_add(struct perf_event *bp) if (IS_ERR(tmp)) return PTR_ERR(tmp); =20 + spin_lock(&cpu_bps_lock); cpu_bp =3D per_cpu_ptr(cpu_bps, bp->cpu); for (i =3D 0; i < nr_wp_slots(); i++) { if (!cpu_bp[i]) { @@ -241,6 +264,7 @@ static int cpu_bps_add(struct perf_event *bp) break; } } + spin_unlock(&cpu_bps_lock); return 0; } =20 @@ -249,6 +273,7 @@ static void cpu_bps_remove(struct perf_event *bp) struct breakpoint **cpu_bp; int i =3D 0; =20 + spin_lock(&cpu_bps_lock); cpu_bp =3D per_cpu_ptr(cpu_bps, bp->cpu); for (i =3D 0; i < nr_wp_slots(); i++) { if (!cpu_bp[i]) @@ -260,19 +285,25 @@ static void cpu_bps_remove(struct perf_event *bp) break; } } + spin_unlock(&cpu_bps_lock); } =20 static bool cpu_bps_check(int cpu, struct perf_event *bp) { struct breakpoint **cpu_bp; + bool ret =3D false; int i; =20 + spin_lock(&cpu_bps_lock); cpu_bp =3D per_cpu_ptr(cpu_bps, cpu); for (i =3D 0; i < nr_wp_slots(); i++) { - if (cpu_bp[i] && !can_co_exist(cpu_bp[i], bp)) - return true; + if (cpu_bp[i] && !can_co_exist(cpu_bp[i], bp)) { + ret =3D true; + break; + } } - return false; + spin_unlock(&cpu_bps_lock); + return ret; } =20 static bool all_cpu_bps_check(struct perf_event *bp) @@ -286,10 +317,6 @@ static bool all_cpu_bps_check(struct perf_event *bp) return false; } =20 -/* - * We don't use any locks to serialize accesses to cpu_bps or task_bps - * because are already inside nr_bp_mutex. - */ int arch_reserve_bp_slot(struct perf_event *bp) { int ret;