From mboxrd@z Thu Jan 1 00:00:00 1970 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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9F2F0C43334 for ; Tue, 28 Jun 2022 13:22:40 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4LXQHz0YYDz3cf2 for ; Tue, 28 Jun 2022 23:22:39 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20210112 header.b=agU/nLOg; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=google.com (client-ip=2a00:1450:4864:20::136; helo=mail-lf1-x136.google.com; envelope-from=dvyukov@google.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20210112 header.b=agU/nLOg; dkim-atps=neutral Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4LXQHG60N5z304r for ; Tue, 28 Jun 2022 23:22:02 +1000 (AEST) Received: by mail-lf1-x136.google.com with SMTP id a4so8685626lfm.0 for ; Tue, 28 Jun 2022 06:22:02 -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=be+Lf34zIYxC1aHuBjjwqUN6IGD5kruVpfKIJ1bYLysbGRyfnDtNRpfk2BCPo4ZrCc PVIDeoz99KD8AM0C00hMdRiErVAegoUKOa0uTqpkbjZaXBGNxOoCxjOT1G5dpzux2ZNu zYNp0rffLR/cwL3HpiEERedy20fl7QKQfk4ccdxO0oQFEc3vD/LqdV1vKmSsGeUaRFlB 2KcRAiYmHTAbTcgwscNwSC8gv7viT90viM4jjyr+2hxS5Ank+7VyNsgEvtyudspnYFbe UtjqdVAA9dKYI5cIBmbGka6GYywa5mxbO/E2hSVPZWjhMF5rKmxb2u0a8kmlwGv4YIlp A9Rw== X-Gm-Message-State: AJIora959QhZOvjdc5olIqRkSmrDTwUEOjScAn9gbVLdnO7TFstQZ3BY XIdmgBr2C9PGYe36ys06VdAT8zu5d/4679Y3tPsU1Q== X-Google-Smtp-Source: AGRyM1v0fgdpjHzHv8JkI02NBZZld3DmYsZhkHLm7JBkFeSf1j0FSQ2QhutxzIkbCur8DQvQQ7UcECyAPVNgYChUaNk= 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 Content-Type: text/plain; charset="UTF-8" X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , linux-sh@vger.kernel.org, Peter Zijlstra , Frederic Weisbecker , x86@kernel.org, linuxppc-dev@lists.ozlabs.org, Arnaldo Carvalho de Melo , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Alexander Shishkin , kasan-dev@googlegroups.com, Namhyung Kim , Thomas Gleixner , Jiri Olsa , Ingo Molnar Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" 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 > From mboxrd@z Thu Jan 1 00:00:00 1970 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 6936CC43334 for ; Tue, 28 Jun 2022 13:25:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346322AbiF1NZU (ORCPT ); Tue, 28 Jun 2022 09:25:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346352AbiF1NZG (ORCPT ); Tue, 28 Jun 2022 09:25:06 -0400 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E07EE3615B for ; Tue, 28 Jun 2022 06:21:59 -0700 (PDT) Received: by mail-lf1-x136.google.com with SMTP id j21so22306164lfe.1 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=TZCGVlR7kYdI01yjZXFoOPM+c0F+lnCKx239prkvWf7WSH1uFmo/1y9LUnnGbMUiR0 EJVyvXIS//UYf/Xu5S/A49R0zFuewZtPlJLS76IqgaRTD4VkhHVtQIAeRWHBn3JYIXeF L54dBGtSxJ8QRAqypCwrzYRBne9TjmaERRARkAFGA7AVMWydbhpX+iJl1I3EyryW0T4G PFn10stf3gyWUozKD/aD34vT53Ia8PJ2x3kZJrW5Vx+uVX9EsiKYgVgjHcefiEB7LI9l EWGknYZD5FKn/d6f5x/suzU+lj7cUa4fTTZeUHnAErSJgpG+qjbwgtje5hCr4tt37tbu 8XXA== X-Gm-Message-State: AJIora+xEoQwZ7bp1ioiJ0LLAt3gW4Y8scsRjKpzw08A4Hn8cRPl4Na6 cTHpulbzeD3AVLqwmNin60Qz3u3jO8dzH8u6Yv3rGQ== X-Google-Smtp-Source: AGRyM1v0fgdpjHzHv8JkI02NBZZld3DmYsZhkHLm7JBkFeSf1j0FSQ2QhutxzIkbCur8DQvQQ7UcECyAPVNgYChUaNk= 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" Precedence: bulk List-ID: X-Mailing-List: linux-sh@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 >