Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp427400rwe; Thu, 1 Sep 2022 01:43:52 -0700 (PDT) X-Google-Smtp-Source: AA6agR7DIiRfJwrSTLwoabyAUCiA8SIzCnApiZ8jXQwQo7MJQ5qCwQpmxB7mP4oD9HrJbEuU7CU0 X-Received: by 2002:a17:902:cf11:b0:172:6437:412d with SMTP id i17-20020a170902cf1100b001726437412dmr29803362plg.10.1662021832124; Thu, 01 Sep 2022 01:43:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662021832; cv=none; d=google.com; s=arc-20160816; b=qUOoRaVQC543KA/EicoOqAAL4vOD/OeSm7N5cjMqYbJHxzA1LHsFsZC4bFonQ6e7AN Qw39cwg6GU4oFYcTCGuMUu7VejHRNeVooQUGkj5ZolmEKYVt3GVlQVDMrRiaWj+mq/Cp xlX0iS57xjwIxzqv1bqVKcPmSCLVqqs0HlIwH845VZil1ys5CUqzH/z2Ja/xfSY/zC17 6lymbMbt0BY1y16xQ3DAwHlIvphv0IzFlWieMaIPLb+aKmywsk1Eb3IX1eDIlKza4kOU cFQHHzmpxo0nzQytZFpGTbhrkOzb0rgU/CcOC0e9/8ncdRSyXZTHBVXy57M608yORclX 9Sqg== 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=nfuQ0Vwg/5vIFBggno4nVA/RHScWxPWw8paaD15Yw48=; b=CYYJ/HFFOs5ReH84gXgKsGVJbldsaUj+6U9XwXmqfyIAQVfmz3kpQciFN3EsEb0Krz In3FYbl9OKSmUBiWn/0uqYyoQZHi26rrE2hVsVAxW7lx+37guLrNjcDlsfJ6YFZ9oapj jGuRJf/gBZQK0d6L308RUn3DTvFAdC374dVTzpr9Fmv/fdR0CvxaKlLaWp60Va8Zgcdu g9s38FVHN9Vv+avyigSakyC8OqimIX/kAOp+tR26myESD/n7OnktrAQ2sMNWXSkn8k9j gzlCos2+tGN9RUb0fRmUFo5PTaGQ5c943dAJ0lPU+RAnrhIlmTxgXdHYbbzcvhANp2ed 9m0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=CuAYgNsV; dkim=neutral (no key) header.i=@linutronix.de header.b=c6YDvWlE; 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 c15-20020a170903234f00b0016efa52d3ffsi18498064plh.241.2022.09.01.01.43.33; Thu, 01 Sep 2022 01:43:52 -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=CuAYgNsV; dkim=neutral (no key) header.i=@linutronix.de header.b=c6YDvWlE; 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 S234077AbiIAINP (ORCPT + 99 others); Thu, 1 Sep 2022 04:13:15 -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: 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: f95e5a3d59011eec1257d0e76de1e1f8969d426f Gitweb: https://git.kernel.org/tip/f95e5a3d59011eec1257d0e76de1e1f8969d426f 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_breakpoint.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 @@ -129,7 +130,14 @@ struct breakpoint { bool ptrace_bp; }; +/* + * While kernel/events/hw_breakpoint.c does its own synchronization, we cannot + * 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); 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); + spin_lock(&task_bps_lock); list_add(&tmp->list, &task_bps); + spin_unlock(&task_bps_lock); return 0; } @@ -182,6 +192,7 @@ static void task_bps_remove(struct perf_event *bp) { struct list_head *pos, *q; + spin_lock(&task_bps_lock); list_for_each_safe(pos, q, &task_bps) { struct breakpoint *tmp = list_entry(pos, struct breakpoint, list); @@ -191,6 +202,7 @@ static void task_bps_remove(struct perf_event *bp) break; } } + spin_unlock(&task_bps_lock); } /* @@ -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 = false; + 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 = true; + break; + } } - return false; + spin_unlock(&task_bps_lock); + return ret; } /* @@ -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 = false; + spin_lock(&task_bps_lock); list_for_each_entry(tmp, &task_bps, list) { if (tmp->bp->hw.target == bp->hw.target && - !can_co_exist(tmp, bp)) - return true; + !can_co_exist(tmp, bp)) { + ret = true; + break; + } } - return false; + spin_unlock(&task_bps_lock); + return ret; } 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); + spin_lock(&cpu_bps_lock); cpu_bp = per_cpu_ptr(cpu_bps, bp->cpu); for (i = 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; } @@ -249,6 +273,7 @@ static void cpu_bps_remove(struct perf_event *bp) struct breakpoint **cpu_bp; int i = 0; + spin_lock(&cpu_bps_lock); cpu_bp = per_cpu_ptr(cpu_bps, bp->cpu); for (i = 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); } static bool cpu_bps_check(int cpu, struct perf_event *bp) { struct breakpoint **cpu_bp; + bool ret = false; int i; + spin_lock(&cpu_bps_lock); cpu_bp = per_cpu_ptr(cpu_bps, cpu); for (i = 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 = true; + break; + } } - return false; + spin_unlock(&cpu_bps_lock); + return ret; } 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; } -/* - * 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;