Received: by 2002:a05:6512:e85:0:0:0:0 with SMTP id bi5csp3100908lfb; Tue, 28 Jun 2022 06:32:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1skmyEjdqvXnUGXjTP976qKdSOSN/yy3zwRVIB9ezLrxAqJjddg42OezXiQg2jZZeEYEqvA X-Received: by 2002:a05:6402:3587:b0:435:b9a1:d5c5 with SMTP id y7-20020a056402358700b00435b9a1d5c5mr23468684edc.219.1656423132800; Tue, 28 Jun 2022 06:32:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656423132; cv=none; d=google.com; s=arc-20160816; b=ud9GEkwOdWwakHRXOQBhbjpWW9VyW2Nhp3cPNCemxv/wZuemYyvDVGQonVM+X0huZG H3xD8RbIpoh3v4Jt5H6uGyHf3e8e03hszUgu0/SDblwZH0df8djeWNeG14cp09qD3oCd 6nq7Id72SfesY0Uie5GFcrh3lk32D66KJNpC5KmtuiP6SKcU55c9SJDJuIuRLJtqWz6s OspfNhk/Z72VxqTUuLc5mwtI7AALY6usQbIoAPaCI/H0Z9Muv1XI8eVwMqP0WccWfuF+ 6RJyt+ItMcRVWGJ/UYtmMtK50EGNTfvHTy2RchzgNCedqe9tqGX+uQ05d1cfuywkIqsv 17jg== 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=aeUiRngewZvVOC/Ws+kiYpWX0PNqIg9bWynr1VvD7h0=; b=Y6XU2OgVty5Vl6S4XU5VaND8//cS4AhVCjXm77EhFhGFy1AshURvAzFwpzVi7zJ5fW EqfRBbND7I9yOnRdqKegZU8/uRocfqDXxtlec25Anx4EdkFmA1D3bES/3zLq9I0linYZ H1Y46E8+VZf1liDdgbr1rVZZ7lRf7nFt7Dk7x21TIPJRMio+sjMPPhR2QLIeOOl7UZ4w usF5XPS+QVBmOwGaYZlv4M4fVWS1jICAbQEu1Eg4iv6v0390JshD4x6XBzkBZa787oND g63tVunueQUkJNWoK/a2Tvq4D1MGer3h8g84xAmkcbBATnoNHo7K8EonYZUz/snBJo1E 5xFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Dyb0kRO5; 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 ho14-20020a1709070e8e00b0070434ac836esi17065156ejc.468.2022.06.28.06.31.45; Tue, 28 Jun 2022 06:32:12 -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=Dyb0kRO5; 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 S1346035AbiF1NIl (ORCPT + 99 others); Tue, 28 Jun 2022 09:08:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346069AbiF1NI3 (ORCPT ); Tue, 28 Jun 2022 09:08:29 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A1A2A477 for ; Tue, 28 Jun 2022 06:08:22 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id bx13so6234798ljb.1 for ; Tue, 28 Jun 2022 06:08:22 -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=aeUiRngewZvVOC/Ws+kiYpWX0PNqIg9bWynr1VvD7h0=; b=Dyb0kRO5lv6/jlkR7UH+AIG4MayAFkpnXQual6TJkVwNXD96DRjWuzuV9N+HHZOSEL 1SkcnMjb3o4ZYF7t/CZ5cbeZtJunU860Suf2JYWemXvsB+QKvZPplCsnosLlVg5rMy76 SPJ9rcG4iTMIkrpi6gj+DI/6RnV4hTtZ831gM+wK8o/OWIbH6DaC+w0q0XUap1HYr8Ug 7hB6nyY81sR+ByS7Wtg1if49gU/3T4dQ9ntUs1w5hDGp+AK7tPp6H3LZ183LrrC/2IRr 5aFP2iCQJqaIGiK8/mALMglRqSdI44wI1vHUCbleKi49mAF/Ca5jDyXF43Phe1ystGGv WK7g== 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=aeUiRngewZvVOC/Ws+kiYpWX0PNqIg9bWynr1VvD7h0=; b=BuxKzbyMs8HlV5HaLue2x5a4EXN3kye34zH6ENcXPIx0dRQ66adx1UIsl0Pntl9HOX 3jQmH4OtRc2NAGoAueNIDCOz7cSJ7pFUFd3vQsHSoavtgutAYBAB77NgLIZC2VD9CkLy YrK1f8xpx01NI815RPfCX55sLk3zHOJm7B2l/RtVPeUxkhHZnGq0020hApkkT9oYvHUd 6PoY7Y7n26+mD+GtPK0VhuN4jbOVXxyLilSR64x11GCG0fVhv5cj5b7UqbZpCVApf7LN /riZywj7ez7jvun2vZ/Auio5vQwVL/0m/num7THW4fPHU2hNIb5HRJmZIP67uw+qYuNo 0TpA== X-Gm-Message-State: AJIora9TA19hLxdGt5FjTqZjTLwQ9rVv87mrQ3yjSZWtf5t9sMcC6F8W YA+EuCyRr4Vj3NgIj81Of2ElfN1qj/TyERA+EMGh4Q== X-Received: by 2002:a2e:9f42:0:b0:25b:5649:1331 with SMTP id v2-20020a2e9f42000000b0025b56491331mr9293742ljk.268.1656421700299; Tue, 28 Jun 2022 06:08:20 -0700 (PDT) MIME-Version: 1.0 References: <20220628095833.2579903-1-elver@google.com> <20220628095833.2579903-4-elver@google.com> In-Reply-To: <20220628095833.2579903-4-elver@google.com> From: Dmitry Vyukov Date: Tue, 28 Jun 2022 15:08:08 +0200 Message-ID: Subject: Re: [PATCH v2 03/13] 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 , 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=unavailable 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: > > 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. > > Instead, make use of the "rhashtable" variant "rhltable" which stores > multiple items with the same key in a list. This results in average > runtime complexity of O(1) for task_bp_pinned(). > > With the optimization, the benchmark shows: > > | $> 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: 0.208 [sec] > | > | 108.422396 usecs/op > | 6939.033333 usecs/op/cpu > > On this particular setup that's a speedup of ~1135x. > > While one option would be to make task_struct a breakpoint list node, > this would only further bloat task_struct for infrequently used data. > Furthermore, after all optimizations in this series, there's no evidence > it would result in better performance: later optimizations make the time > spent looking up entries in the hash table negligible (we'll reach the > theoretical ideal performance i.e. no constraints). > > Signed-off-by: Marco Elver > --- > v2: > * Commit message tweaks. > --- > include/linux/perf_event.h | 3 +- > kernel/events/hw_breakpoint.c | 56 ++++++++++++++++++++++------------- > 2 files changed, 37 insertions(+), 22 deletions(-) > > diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h > index 01231f1d976c..e27360436dc6 100644 > --- a/include/linux/perf_event.h > +++ b/include/linux/perf_event.h > @@ -36,6 +36,7 @@ struct perf_guest_info_callbacks { > }; > > #ifdef CONFIG_HAVE_HW_BREAKPOINT > +#include > #include > #endif > > @@ -178,7 +179,7 @@ struct hw_perf_event { > * creation and event initalization. > */ > struct arch_hw_breakpoint info; > - struct list_head bp_list; > + struct rhlist_head bp_list; > }; > #endif > struct { /* amd_iommu */ > diff --git a/kernel/events/hw_breakpoint.c b/kernel/events/hw_breakpoint.c > index 1b013968b395..add1b9c59631 100644 > --- a/kernel/events/hw_breakpoint.c > +++ b/kernel/events/hw_breakpoint.c > @@ -26,10 +26,10 @@ > #include > #include > #include > -#include > #include > #include > #include > +#include > #include > #include > > @@ -54,7 +54,13 @@ static struct bp_cpuinfo *get_bp_info(int cpu, enum bp_type_idx type) > } > > /* Keep track of the breakpoints attached to tasks */ > -static LIST_HEAD(bp_task_head); > +static struct rhltable task_bps_ht; > +static const struct rhashtable_params task_bps_ht_params = { > + .head_offset = offsetof(struct hw_perf_event, bp_list), > + .key_offset = offsetof(struct hw_perf_event, target), > + .key_len = sizeof_field(struct hw_perf_event, target), > + .automatic_shrinking = true, > +}; > > static int constraints_initialized; > > @@ -103,17 +109,23 @@ static unsigned int max_task_bp_pinned(int cpu, enum bp_type_idx type) > */ > static int task_bp_pinned(int cpu, struct perf_event *bp, enum bp_type_idx type) > { > - struct task_struct *tsk = bp->hw.target; > + struct rhlist_head *head, *pos; > struct perf_event *iter; > int count = 0; > > - list_for_each_entry(iter, &bp_task_head, hw.bp_list) { > - if (iter->hw.target == tsk && > - find_slot_idx(iter->attr.bp_type) == type && > + rcu_read_lock(); > + head = rhltable_lookup(&task_bps_ht, &bp->hw.target, task_bps_ht_params); > + if (!head) > + goto out; > + > + rhl_for_each_entry_rcu(iter, pos, head, hw.bp_list) { > + if (find_slot_idx(iter->attr.bp_type) == type && > (iter->cpu < 0 || cpu == iter->cpu)) > count += hw_breakpoint_weight(iter); > } > > +out: > + rcu_read_unlock(); > return count; > } > > @@ -186,7 +198,7 @@ static void toggle_bp_task_slot(struct perf_event *bp, int cpu, > /* > * Add/remove the given breakpoint in our constraint table > */ > -static void > +static int > toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type, > int weight) > { > @@ -199,7 +211,7 @@ toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type, > /* Pinned counter cpu profiling */ > if (!bp->hw.target) { > get_bp_info(bp->cpu, type)->cpu_pinned += weight; > - return; > + return 0; > } > > /* Pinned counter task profiling */ > @@ -207,9 +219,9 @@ toggle_bp_slot(struct perf_event *bp, bool enable, enum bp_type_idx type, > toggle_bp_task_slot(bp, cpu, type, weight); > > if (enable) > - list_add_tail(&bp->hw.bp_list, &bp_task_head); > + return rhltable_insert(&task_bps_ht, &bp->hw.bp_list, task_bps_ht_params); > else > - list_del(&bp->hw.bp_list); > + return rhltable_remove(&task_bps_ht, &bp->hw.bp_list, task_bps_ht_params); > } > > __weak int arch_reserve_bp_slot(struct perf_event *bp) > @@ -307,9 +319,7 @@ static int __reserve_bp_slot(struct perf_event *bp, u64 bp_type) > if (ret) > return ret; > > - toggle_bp_slot(bp, true, type, weight); > - > - return 0; > + return toggle_bp_slot(bp, true, type, weight); > } > > int reserve_bp_slot(struct perf_event *bp) > @@ -334,7 +344,7 @@ static void __release_bp_slot(struct perf_event *bp, u64 bp_type) > > type = find_slot_idx(bp_type); > weight = hw_breakpoint_weight(bp); > - toggle_bp_slot(bp, false, type, weight); > + WARN_ON(toggle_bp_slot(bp, false, type, weight)); > } > > void release_bp_slot(struct perf_event *bp) > @@ -678,7 +688,7 @@ static struct pmu perf_breakpoint = { > int __init init_hw_breakpoint(void) > { > int cpu, err_cpu; > - int i; > + int i, ret; > > for (i = 0; i < TYPE_MAX; i++) > nr_slots[i] = hw_breakpoint_slots(i); > @@ -689,18 +699,24 @@ int __init init_hw_breakpoint(void) > > info->tsk_pinned = kcalloc(nr_slots[i], sizeof(int), > GFP_KERNEL); > - if (!info->tsk_pinned) > - goto err_alloc; > + if (!info->tsk_pinned) { > + ret = -ENOMEM; > + goto err; > + } > } > } > > + ret = rhltable_init(&task_bps_ht, &task_bps_ht_params); > + if (ret) > + goto err; > + > constraints_initialized = 1; > > perf_pmu_register(&perf_breakpoint, "breakpoint", PERF_TYPE_BREAKPOINT); > > return register_die_notifier(&hw_breakpoint_exceptions_nb); It seems there is a latent bug here: if register_die_notifier() fails we also need to execute the err: label code. Otherwise the patch looks good. Reviewed-by: Dmitry Vyukov > - err_alloc: > +err: > for_each_possible_cpu(err_cpu) { > for (i = 0; i < TYPE_MAX; i++) > kfree(get_bp_info(err_cpu, i)->tsk_pinned); > @@ -708,7 +724,5 @@ int __init init_hw_breakpoint(void) > break; > } > > - return -ENOMEM; > + return ret; > } > - > - > -- > 2.37.0.rc0.161.g10f37bed90-goog >