Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp605314iol; Thu, 9 Jun 2022 09:57:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxPOdxEv5iwDPDMy4CJ9dRmOkjN19SNg5PjbAfu6pzVsmAyv7g4aMSC6GhfXDx3s17m1hl X-Received: by 2002:a05:6402:35d0:b0:42e:1a76:67ae with SMTP id z16-20020a05640235d000b0042e1a7667aemr42634543edc.311.1654793853885; Thu, 09 Jun 2022 09:57:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654793853; cv=none; d=google.com; s=arc-20160816; b=Kiy/16BQvTs+nlJytOn5cDSxMZLQAnd/5VAxfQS9heRNSFIizw2szx/cm0Omavm3J/ spyyFjRbXjDkEZpPoag9vtu5oe6yCncmdZ2cjdKfYnxkgDeqqRgyhpP+5B2wx467WDxb 94PX848MYQHmli3sAf0OZLbIfx3kiCICODobgg8iULMgOWDC7NdpZ/zZnvvXshl6m0Ar 4dS61wxmTt/dFPJEgyUK7kYZmkxkF8XzqiBRi6CgDUYNRth0a9hNB5G5W+E1fUr++P47 t6aVxUGULPna+HiUROIV8C7Vu4h1qHEEl5iUBZr3EWOh2b0XRL+v0HMZqD5AUlajaC4Z WtTQ== 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=RnnI75MEx4lNy+rx7v/bh01lvj2FiAMBVy1u9JepGfo=; b=YWkt8p6Vcd53qcWxmAGKfXLcxIefp5A2RC3yUuXUaZNYKTNQ6XgHnPvDt7FQ9/bdaT 1l2Rr8IJ/BZgbMzDb8LJ3q+4aeeRVnZzOPRhWJivIv1cAKxQNdX+p5KioCeXx8E9b05y iIJafwIav12spXgFyD6SojHWVHPy0Po7fs+691qhhRyIV4mJ5UT+j50WUsc21vKfmCw8 mRUv6X2lW/F00goozjGt8y7+ulyzKlaS/gu/F1A6lqBhQI49JN6IIoPACw5r/BAqNW6I CTJukeyTkzqkjkWJb95KXnT4gze/s+82UUMk8PlajT51BpqGB329Sbzi31nW19ZLIreR PRgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rMqG2WJP; 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 l21-20020a056402125500b0042deea0e95bsi6116325edw.167.2022.06.09.09.57.05; Thu, 09 Jun 2022 09:57:33 -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=rMqG2WJP; 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 S1344944AbiFIQxg (ORCPT + 99 others); Thu, 9 Jun 2022 12:53:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232459AbiFIQxb (ORCPT ); Thu, 9 Jun 2022 12:53:31 -0400 Received: from mail-lj1-x235.google.com (mail-lj1-x235.google.com [IPv6:2a00:1450:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DA2E12D17 for ; Thu, 9 Jun 2022 09:53:26 -0700 (PDT) Received: by mail-lj1-x235.google.com with SMTP id l18so19513527lje.13 for ; Thu, 09 Jun 2022 09:53:26 -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=RnnI75MEx4lNy+rx7v/bh01lvj2FiAMBVy1u9JepGfo=; b=rMqG2WJPOlD4LQ8ZwCDsf+vQZC1NKtoCU+xmI1t5RCDuq2ySS5PmrgARG1EiNn2RX+ Eb7iTRgP8sGNIcNEEbi/NqOM+lt0qQYV3x20KYp6V3al+e/KQpw781+9fEqSMJLW8QQL 20IkhuMCuZJYN9Mkw+boqnomMUpkNCuxDQJRX4oHUyoHLAu9PhTI7GnGIVNI83TXU+2V s2RSR2OCyAZYiNcCreJt9MQV8dckZhtArxQ5dwqQ8HrdYsQLCNqSkoHlGsdfmBv8KFon edHyjyy7pby2JLfCK4aGFphbGZtKtnNEcQy2CvuqiDcVI9dQCu5kQ0FzK2THigolw0or gtcA== 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=RnnI75MEx4lNy+rx7v/bh01lvj2FiAMBVy1u9JepGfo=; b=4tunZ3/vHdqMdunEyLiyTFFVc56AnodlKeJ3wpcz0q31YcTyuRr+gOFFsrK6MSYquo 8qJkdCv6G8CKkovzcKqZTAXkedbzwbqcOlE0pYL/js6gWpKvktHzPOc27TGejslYCn4N I/Zjexp8MPp/K4c4+OKdjBXkzCZxdeGVLzhjW17bga0aw6xSEmSMKNi+d4xfKlk4Xjc1 sJUE35jlSb0yDRO7L1Q0wybuvU101V1ltMzXs3YTLE5DvuJLTH085eSiC5UhLxoH61L1 CMK6pX/PA3sawjvueHJQGpmFU+hMCQ1M33l/rcG2pLzmmsuYtlcgsLdfxFdzZnN6oGIE C1cQ== X-Gm-Message-State: AOAM533TaO4At6Lud5yMlkoTJP8UiMctV/t4ZBYX5y+IcNajKOhVCuzt wUcbLp9+lsRaqieP2odxCKdAAbLvz6gUX7V+SpGoRw== X-Received: by 2002:a2e:b0fc:0:b0:255:6f92:f9d4 with SMTP id h28-20020a2eb0fc000000b002556f92f9d4mr22608060ljl.92.1654793604135; Thu, 09 Jun 2022 09:53:24 -0700 (PDT) MIME-Version: 1.0 References: <20220609113046.780504-1-elver@google.com> <20220609113046.780504-2-elver@google.com> In-Reply-To: From: Dmitry Vyukov Date: Thu, 9 Jun 2022 18:53:12 +0200 Message-ID: Subject: Re: [PATCH 1/8] perf/hw_breakpoint: Optimize list of per-task breakpoints To: Marco Elver Cc: Peter Zijlstra , Frederic Weisbecker , Ingo Molnar , Thomas Gleixner , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , 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 Thu, 9 Jun 2022 at 16:56, Marco Elver wrote: > > > On a machine with 256 CPUs, running the recently added perf breakpoint > > > benchmark results in: > > > > > > | $> perf bench -r 30 breakpoint thread -b 4 -p 64 -t 64 > > > | # Running 'breakpoint/thread' benchmark: > > > | # Created/joined 30 threads with 4 breakpoints and 64 parallelism > > > | Total time: 236.418 [sec] > > > | > > > | 123134.794271 usecs/op > > > | 7880626.833333 usecs/op/cpu > > > > > > The benchmark tests inherited breakpoint perf events across many > > > threads. > > > > > > Looking at a perf profile, we can see that the majority of the time is > > > spent in various hw_breakpoint.c functions, which execute within the > > > 'nr_bp_mutex' critical sections which then results in contention on that > > > mutex as well: > > > > > > 37.27% [kernel] [k] osq_lock > > > 34.92% [kernel] [k] mutex_spin_on_owner > > > 12.15% [kernel] [k] toggle_bp_slot > > > 11.90% [kernel] [k] __reserve_bp_slot > > > > > > The culprit here is task_bp_pinned(), which has a runtime complexity of > > > O(#tasks) due to storing all task breakpoints in the same list and > > > iterating through that list looking for a matching task. Clearly, this > > > does not scale to thousands of tasks. > > > > > > While one option would be to make task_struct a breakpoint list node, > > > this would only further bloat task_struct for infrequently used data. > > > > task_struct already has: > > > > #ifdef CONFIG_PERF_EVENTS > > struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts]; > > struct mutex perf_event_mutex; > > struct list_head perf_event_list; > > #endif > > > > Wonder if it's possible to use perf_event_mutex instead of the task_sharded_mtx? > > And possibly perf_event_list instead of task_bps_ht? It will contain > > other perf_event types, so we will need to test type as well, but on > > the positive side, we don't need any management of the separate > > container. > > Hmm, yes, I looked at that but then decided against messing the > perf/core internals. The main issue I have with using perf_event_mutex > is that we might interfere with perf/core's locking rules as well as > interfere with other concurrent perf event additions. Using > perf_event_list is very likely a no-go because it requires reworking > perf/core as well. > > I can already hear Peter shouting, but maybe I'm wrong. :-) Let's wait for Peter to shout then :) A significant part of this change is having per-task data w/o having per-task data. The current perf-related data in task_struct is already multiple words and it's also not used in lots of production cases. Maybe we could have something like: struct perf_task_data* lazily_allocated_perf_data; that's lazily allocated on first use instead of the current perf_event_ctxp/perf_event_mutex/perf_event_list. This way we could both reduce task_size when perf is not used and have more perf-related data (incl breakpoints) when it's used.