Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp2861525rdh; Mon, 27 Nov 2023 00:06:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IE9qMYvrvYDve9xhQUgf7YIAy/96ekA3Ba7j+jWfCfRaG+k9xTuS3vjzBNzPuS9+bxpSnmh X-Received: by 2002:a17:902:a5c4:b0:1cf:b1df:47cd with SMTP id t4-20020a170902a5c400b001cfb1df47cdmr6759361plq.25.1701072390620; Mon, 27 Nov 2023 00:06:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701072390; cv=none; d=google.com; s=arc-20160816; b=PhQ4LgUDnfOyDy20MkJdRE+oTEugxG8BCi0rxVBjlSO293g4GRo0QwiXpi3AHSmNql tnegHevh+TDy0BIKRLIWk4UIVXROpzgY2lPQ3u5UvsH+XrYm1EetLOqQn4HG9Z+vjO+v 5jTrJxCVSIcQGMXod+9Tm7+VhX5Ls3oH5FXGF5EZ0wjv5c80Tu0+ElWvOL1XEwa+blAy gATvfM8DJb8F6+UEnz8BxEPynW7tfmm11dh3Fa85pbVsU7CTjp9hy7EoBy+wojaLH+0F 9YlFBroftLzOVEix+8oAixzT0or9AIleCD0Vw5X4JKyekqfjFIgnEEqHqBvGzdT7U4jG HvTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=fd12cqGjD/FIuiL12AD0xyeSHt1/olir2XnQ/BL0ALE=; fh=pE2uB+Pj5ycJqeAHmrQX21zOyGN5AMkM6doeLbKSOMs=; b=JubRHhWNdvM0jleQE4qwev5Aux/miqt0K0dkT5qFZQmORchmfb7wVDsMb4wqvavW5I 09fNOeegok3WuMXDiFCG0yCf1IB9RzVwhzUSzdu4DrHkTxeQewXVIx2U8iUx5A6DNf5m EXN21J4L/pEygQ6yGcNvIqWj4Ws9mwXqi9pQMAdGyz5aIOWboEsKq5+yysNua3OJOCL/ XQbRyd5cN8ARzELiOjar4nY9xcKHstbBt2CDqh+VJn5uYTr4i60OlBXL4K7BVqereZMB 00g25r+x1vUP2DRTuDKFyVRjhKZPxK+A2FAogCo7mW6JeMOPPRZkWon0W3Vy6Lv1GClk A8XA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id v3-20020a170902d68300b001cc2293b5e0si8944280ply.96.2023.11.27.00.06.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 00:06:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 8B3828097E4B; Mon, 27 Nov 2023 00:06:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231978AbjK0IGL (ORCPT + 99 others); Mon, 27 Nov 2023 03:06:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230062AbjK0IGK (ORCPT ); Mon, 27 Nov 2023 03:06:10 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 11D40B5; Mon, 27 Nov 2023 00:06:15 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CF59C2F4; Mon, 27 Nov 2023 00:07:01 -0800 (PST) Received: from [10.162.41.8] (a077893.blr.arm.com [10.162.41.8]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 00BAA3F6C4; Mon, 27 Nov 2023 00:06:09 -0800 (PST) Message-ID: <2bceeb7f-2c0d-4344-8a4b-a944cbd2f4f8@arm.com> Date: Mon, 27 Nov 2023 13:36:06 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [V14 3/8] drivers: perf: arm_pmuv3: Enable branch stack sampling framework Content-Language: en-US To: James Clark Cc: Mark Brown , Rob Herring , Marc Zyngier , Suzuki Poulose , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, will@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com References: <20231114051329.327572-1-anshuman.khandual@arm.com> <20231114051329.327572-4-anshuman.khandual@arm.com> <62b84faf-f413-6bfd-5fc1-ac2489e61e00@arm.com> <648dc72b-7120-498f-8963-dbdc0d1acce0@arm.com> <1d731458-d339-1ffa-0d18-33628634224f@arm.com> <6ab8a99b-00cb-4e53-9f95-51fa1f45a3b8@arm.com> <9d458499-30b2-77e8-d74c-1623e53539fc@arm.com> From: Anshuman Khandual In-Reply-To: <9d458499-30b2-77e8-d74c-1623e53539fc@arm.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 27 Nov 2023 00:06:27 -0800 (PST) On 11/23/23 18:05, James Clark wrote: > > > On 21/11/2023 09:57, Anshuman Khandual wrote: >> >> >> On 11/15/23 15:37, James Clark wrote: >>> >>> >>> On 15/11/2023 07:22, Anshuman Khandual wrote: >>>> On 11/14/23 17:44, James Clark wrote: >>>>> >>>>> >>>>> On 14/11/2023 05:13, Anshuman Khandual wrote: >>>>> [...] >>>>> >>>>>> +/* >>>>>> + * This is a read only constant and safe during multi threaded access >>>>>> + */ >>>>>> +static struct perf_branch_stack zero_branch_stack = { .nr = 0, .hw_idx = -1ULL}; >>>>>> + >>>>>> +static void read_branch_records(struct pmu_hw_events *cpuc, >>>>>> + struct perf_event *event, >>>>>> + struct perf_sample_data *data, >>>>>> + bool *branch_captured) >>>>>> +{ >>>>>> + /* >>>>>> + * CPU specific branch records buffer must have been allocated already >>>>>> + * for the hardware records to be captured and processed further. >>>>>> + */ >>>>>> + if (WARN_ON(!cpuc->branches)) >>>>>> + return; >>>>>> + >>>>>> + /* >>>>>> + * Overflowed event's branch_sample_type does not match the configured >>>>>> + * branch filters in the BRBE HW. So the captured branch records here >>>>>> + * cannot be co-related to the overflowed event. Report to the user as >>>>>> + * if no branch records have been captured, and flush branch records. >>>>>> + * The same scenario is applicable when the current task context does >>>>>> + * not match with overflown event. >>>>>> + */ >>>>>> + if ((cpuc->brbe_sample_type != event->attr.branch_sample_type) || >>>>>> + (event->ctx->task && cpuc->brbe_context != event->ctx)) { >>>>>> + perf_sample_save_brstack(data, event, &zero_branch_stack); >>>>> >>>>> Is there any benefit to outputting a zero size stack vs not outputting >>>>> anything at all? >>>> >>>> The event has got PERF_SAMPLE_BRANCH_STACK marked and hence perf_sample_data >>>> must have PERF_SAMPLE_BRANCH_STACK with it's br_stack pointing to the branch >>>> records. Hence without assigning a zeroed struct perf_branch_stack, there is >>>> a chance, that perf_sample_data will pass on some garbage branch records to >>>> the ring buffer. >>>> >>> >>> I don't think that's an issue, the perf core code handles the case where >>> no branch stack exists on a sample. It even outputs the zero length for >>> you, but there is other stuff that can be skipped if you just never call >>> perf_sample_save_brstack(): >> >> Sending out perf_sample_data without valid data->br_stack seems problematic, >> which would be the case when perf_sample_save_brstack() never gets called on >> the perf_sample_data being prepared, and depend on the below 'else' case for >> pushing out zero records. >> > > I'm not following why it would be problematic. data->br_stack is > initialised to NULL in perf_prepare_sample() and the core code > specifically has a path that was added for the case where > perf_sample_save_brstack() was never called. Without perf_sample_save_brstack() called on the perf sample data will preserve 'data->br_stack' unchanged as NULL from perf_prepare_sample(), The perf sample record, will eventually be skipped for 'data->br_stack' element in perf_output_sample(). void perf_prepare_sample(struct perf_sample_data *data, struct perf_event *event, struct pt_regs *regs) { .... if (filtered_sample_type & PERF_SAMPLE_BRANCH_STACK) { data->br_stack = NULL; data->dyn_size += sizeof(u64); data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; } .... } void perf_output_sample(struct perf_output_handle *handle, struct perf_event_header *header, struct perf_sample_data *data, struct perf_event *event) { .... if (sample_type & PERF_SAMPLE_BRANCH_STACK) { if (data->br_stack) { size_t size; size = data->br_stack->nr * sizeof(struct perf_branch_entry); perf_output_put(handle, data->br_stack->nr); if (branch_sample_hw_index(event)) perf_output_put(handle, data->br_stack->hw_idx); perf_output_copy(handle, data->br_stack->entries, size); } else { /* * we always store at least the value of nr */ u64 nr = 0; perf_output_put(handle, nr); } } .... }