Received: by 2002:a05:6512:e85:0:0:0:0 with SMTP id bi5csp3101815lfb; Tue, 28 Jun 2022 06:33:27 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uZPCMNKN6jUYsb4uRKrpS9qKWUzSybMcPhE8hkmt4WjKPoB26I2vI3T4aTjqYLmN80fCBT X-Received: by 2002:a05:6402:3448:b0:435:71ca:b46b with SMTP id l8-20020a056402344800b0043571cab46bmr24180084edc.348.1656423207290; Tue, 28 Jun 2022 06:33:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656423207; cv=none; d=google.com; s=arc-20160816; b=yphgesXfxC1dQQAikVAy7LrnSpMfPmN24t61D2G0T1TgPq/UcCUYvq5Jbbo7Ypb/4n wb6CZNu0KgDLNyVRim9OdqwQIyKBBLsAJEtTD4ypiJFRacYHqqiu7Xqe69Ekz5Og5d09 b7tyh3vjgtbk3USxqByLxlGYNEgaizODhD3UwWhjwaJ6KlxoDZ21BqMABfsyNsw0H97q 5/xK/KFrHn5LwcLh1DJ1TSpK/GmPx7PChrUATtwqdy/lkRwQE9I9pIQnl1iWZLuMmTQ5 z8JTkOwmWQu3jOpVBUAPDiA6xCf8S0ewWiZomAhCbwX93RRGEQ+9p9yEufYWsp11pV1R JqKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=3i9OmUcCYtxYmLpPV5EK9xnLGgU5ydCPjivPQxAxv/Y=; b=uQOHynM+eAiCmKP8dAs9pHKgjcqd2x+y7yHawmPBmqQ13YziN5+iDcd8KsoNz5MleC kfGJHkRSs/5zjNBRNS0M043FnbCtLPemGfmd1n1E752ELJ7DCXm4zNYhh9mcZB2DiDzr 7Nrd8TQubBRtaqLZAE8tfz4dkt/283/76661McR25E/gq6G+oG8aDAnLE6Nk03xudIbN ZnHFD0+URlA1oyg7cB2BV8PDuGb0WNMRYej+FcCI9Si1MaUznD+GL8PbDptS2D3AYN4/ ELiYnOBEAF7bYOUnj3zc6OVIi7umAjkMXgC0AFXTeMbsraAB/uOwxFiSTPhI6g7XhzwE wjAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="agU/nLOg"; 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 l25-20020a1709062a9900b0071216b08163si14197411eje.264.2022.06.28.06.32.59; Tue, 28 Jun 2022 06:33:27 -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="agU/nLOg"; 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 S1346367AbiF1NZV (ORCPT + 99 others); Tue, 28 Jun 2022 09:25:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346349AbiF1NZG (ORCPT ); Tue, 28 Jun 2022 09:25:06 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE33236159 for ; Tue, 28 Jun 2022 06:21:59 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id t25so22240166lfg.7 for ; Tue, 28 Jun 2022 06:21:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3i9OmUcCYtxYmLpPV5EK9xnLGgU5ydCPjivPQxAxv/Y=; b=agU/nLOghT98fW7OTeKe1t8hM9IAuOqXbJvVYBkIY0pwT7jzkuo1hC7JWefLk3ZPuZ XCpzgUUYiwhU7daiVyX3LObunGknvxDjuw7oofsHkcBOK6FP72r4etfNToivTsbTZ7XN hF2Zq7SWwkX/QXrF8bs40XMZM6u7tYNztWRJUMBlqjVAUq/l4nQSXnlUItSoqAmzi1NN dISZhTEyFUrKYN0788YlMK6YOiEgB5xQ56h9JLInHH8arNXOw4mu1TelK8FhepHfoqHT WWP02IsHmnkBNCTdyKIM3cCEzk7lAh5tWAqiwRvdZfidF2UMFJkwjjptYbI8fXlg9skk bgGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3i9OmUcCYtxYmLpPV5EK9xnLGgU5ydCPjivPQxAxv/Y=; b=1pk4i95tKB0Z2nNOcycSLBooTpswhahYPEfB3KL4krDeOdoWjG+W/B1LMEIA0NIRqm XdYh822yTx8FwJ4jNZNuqHp+cibDfuIAOJOAJrf+I/TQtsUex6u+1yAV35UnCK0gcfU3 +1q8za1S2oZFpiSwSwdqIQeAkLVPmgtrbHwYmrwU1G/PeFbPY8jAku3n9GVbgLhOHlbe I/nfemmj28cEB/KrgqLhhQUcdUX9egm2LIUoCN0izdirGQsrJDkm5r6nIfRWLyylF1nF Q9V/qG5JYs6gV1fWOjG6sa1abLCZxzyq+b6ANfJgZtrW+NJzSvOgTgEvqPyXFLw37FXZ vdxg== X-Gm-Message-State: AJIora8PynJnZxqLPIWP5b3yXvLZLNWg38RyN+aEpIjsyKMPgcoDgrcj Qn+R863kCceat9asqNsa2S1waVvAruFWngyDfTzJXQ== X-Received: by 2002:ac2:4906:0:b0:47f:6c71:6de5 with SMTP id n6-20020ac24906000000b0047f6c716de5mr12443219lfi.137.1656422518034; Tue, 28 Jun 2022 06:21:58 -0700 (PDT) MIME-Version: 1.0 References: <20220628095833.2579903-1-elver@google.com> <20220628095833.2579903-9-elver@google.com> In-Reply-To: <20220628095833.2579903-9-elver@google.com> From: Dmitry Vyukov Date: Tue, 28 Jun 2022 15:21:46 +0200 Message-ID: Subject: Re: [PATCH v2 08/13] powerpc/hw_breakpoint: Avoid relying on caller synchronization To: Marco Elver Cc: Peter Zijlstra , Frederic Weisbecker , Ingo Molnar , Thomas Gleixner , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , 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=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_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 On Tue, 28 Jun 2022 at 11:59, Marco Elver wrote: > > 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 Acked-by: Dmitry Vyukov > --- > v2: > * New patch. > --- > 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 2669f80b3a49..8db1a15d7acb 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; > -- > 2.37.0.rc0.161.g10f37bed90-goog >