Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5780366ybp; Tue, 15 Oct 2019 05:04:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqwt3FM4hZFNmyIpz/TTcVQ1+/Lqn+ZoM3M6rNsulHJ9c3FgcK5P0OZ0KTnxaYd0tXSn1nJy X-Received: by 2002:aa7:ca59:: with SMTP id j25mr33047310edt.78.1571141060086; Tue, 15 Oct 2019 05:04:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571141060; cv=none; d=google.com; s=arc-20160816; b=Pm/LINYLChpxDdE08xwNtzZsSFT5BRQTektMm9w84yxT1wWdMa2YBveteAZ/7EYgEz LP27VMh8bfMQKV17agsDCa/iJyGwUEl/17aqhYjh97VAT5qJetMotYdsuy3fKp920i+Y mCnDOPQk0IRYm1ENI4oaVqdn7sI33aKRqqSwsZVDQH6XgrqiTDIGpRyJZYzCpABSfuiZ Xgp6f44ruiykObaf19UUjxkvYWeMAZZHMq9nhKoyg3QU09/X/5A7aG1eOKAs0SJxG9H4 jZhGPjhCpiY9TubI4DjZXtT1F1Ma8JnKLKrwtvVUrEhgRS91gZfnDp8nf0j7Tjo/Z8ID jwOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:from:references:cc:to:subject; bh=oqvS0iZYaFcTI1rGicUkRb3jOTZvLrLh4CTcd9Eoy9k=; b=R8CP0+yLuMGcPdlHnSQJOyYYM6s9AF80X/L0KIkV+58Cpu/kY22z/Dxc5S2G3tKO/4 F1P7kU29J8euDuPxgJuWVeTKQ4enKhlz+K1KFfI9kTmMURFyhhRMLcQAfG+p7pILTdTS yGZEZr//1jYy8ZW2Ly/ZjPF6youev3Szs6VwsYvCN/NGWgcTh9tM312tl0SHgKD5ArZX Xb+y/bgutvpetTquqECXPaf55Au4n4+rYAk+K8yhKzVhnLYr96sWhQo4qN1gu5kOWNIj v1jTa0Ha1pdHpxsCmHLNDrFwdZf80ae2Ay1a0LPJ8owrVchvT+iWNjgZArNMddiimh3U ZHEg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c24si13171391edt.364.2019.10.15.05.03.55; Tue, 15 Oct 2019 05:04:20 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731572AbfJOKzr (ORCPT + 99 others); Tue, 15 Oct 2019 06:55:47 -0400 Received: from mga14.intel.com ([192.55.52.115]:53449 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726054AbfJOKzq (ORCPT ); Tue, 15 Oct 2019 06:55:46 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Oct 2019 03:55:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,299,1566889200"; d="scan'208";a="370421259" Received: from linux.intel.com ([10.54.29.200]) by orsmga005.jf.intel.com with ESMTP; 15 Oct 2019 03:55:45 -0700 Received: from [10.125.253.10] (abudanko-mobl.ccr.corp.intel.com [10.125.253.10]) by linux.intel.com (Postfix) with ESMTP id 512B0580379; Tue, 15 Oct 2019 03:55:42 -0700 (PDT) Subject: Re: [PATCH v1] perf/core: fix restoring of Intel LBR call stack on a context switch To: Peter Zijlstra Cc: Arnaldo Carvalho de Melo , Ingo Molnar , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Andi Kleen , Kan Liang , Stephane Eranian , Ian Rogers , Song Liu , linux-kernel References: <20191015091617.GF2311@hirez.programming.kicks-ass.net> From: Alexey Budankov Organization: Intel Corp. Message-ID: Date: Tue, 15 Oct 2019 13:55:41 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20191015091617.GF2311@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 15.10.2019 12:16, Peter Zijlstra wrote: > On Mon, Oct 14, 2019 at 09:08:34AM +0300, Alexey Budankov wrote: >> >> Restore Intel LBR call stack from cloned inactive task perf context on >> a context switch. This change inherently addresses inconsistency in LBR >> call stack data provided on a sample in record profiling mode for >> example like this: >> >> $ perf record -N -B -T -R --call-graph lbr \ >> -e cpu/period=0xcdfe60,event=0x3c,name=\'CPU_CLK_UNHALTED.THREAD\'/Duk \ >> --clockid=monotonic_raw -- ./miniFE.x nx 25 ny 25 nz 25 >> >> Let's assume threads A, B, C belonging to the same process. >> B and C are siblings of A and their perf contexts are treated as equivalent. >> At some point B blocks on a futex (non preempt context switch). >> B's LBRs are preserved at B's perf context task_ctx_data and B's events >> are removed from PMU and disabled. B's perf context becomes inactive. >> >> Later C gets on a cpu, runs, gets profiled and eventually switches to >> the awaken but not yet running B. The optimized context switch path is >> executed coping B's task_ctx_data to C's one and updating B's perf context >> pointer to refer to C's task_ctx_data that contains preserved B's LBRs >> after coping. >> >> However, as far B's perf context is inactive there is no enabled events >> in there and B's task_ctx_data->lbr_callstack_users is equal to 0. >> When B gets on the cpu B's events reviving is skipped following >> the optimized context switch path and B's task_ctx_data->lbr_callstack_users >> remains 0. Thus B's LBR's are not restored by pmu sched_task() code called >> in the end of perf context switch sched_in callback for B. >> >> In the report that manifests as having short fragments of B's >> call stack, still tracked by LBR's HW between adjacent samples, >> but the whole thread call tree doesn't aggregate. >> > >> Signed-off-by: Alexey Budankov >> --- >> kernel/events/core.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/kernel/events/core.c b/kernel/events/core.c >> index 2aad959e6def..74c2ff38e079 100644 >> --- a/kernel/events/core.c >> +++ b/kernel/events/core.c >> @@ -3181,7 +3181,7 @@ static void perf_event_context_sched_out(struct task_struct *task, int ctxn, >> >> rcu_read_lock(); >> next_ctx = next->perf_event_ctxp[ctxn]; >> - if (!next_ctx) >> + if (!next_ctx || !next_ctx->is_active) >> goto unlock; > > AFAICT this completely kills off the optimization. next_ctx->is_active > cannot be set at this point. Hmm, the intention was to skip optimization path only once when switching to just resumed thread. Thanks for observation. ~Alexey