Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp5729755ybp; Tue, 15 Oct 2019 04:17:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqyUO1Egf/Y02ySyu+BzuwIWFlzWHUx4Dxb5G/tDDb/I0eSFzmO1+H2YglIvRjOQtHdV+Mxj X-Received: by 2002:a50:cc43:: with SMTP id n3mr33362796edi.287.1571138228249; Tue, 15 Oct 2019 04:17:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571138228; cv=none; d=google.com; s=arc-20160816; b=NxVuYFxLDvUdB3+2znEScY5Z6Hz793nIkpN0jGy6zfZaAFapaRErG7zArJaxqSJZ0g yTh6TpV93YzabfLm7FRFBeBa4t/ww8dQQOpZA8MkKdcOdUCVRQfy/IZGvVcglRRWH+lB ScIRDovuLurTHPq4Pe+pJkVdWWWnCDyzTm+5HQOgGqzamVHPFY9IKjkG7rNrumIXBLPo 5jc0gyXNUxzVBxG7mJJZrH4MPaBO4IGGzKh+xcjLXS9ZY5hy2mFSmUPXA/296uxoW8Db tFQeHSLtFCKlOVL43ypTdrZafODs4KMcgPvZ9rP6G1f0VLVMTlsDAMWUYwEpL3tbl2qu kfTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=oIjZ3ZGDT1Dl1PI+PmIwiAz6EXPC9LLSMZjb9xG3QJY=; b=jT5Ynpx74motMKd3gQB+g4LkyaeaXDudtc3Ee9LV2PJ+d+M7DwluaMykqPRj/VK83t 8XsdcNmRPRIhOB6gUy9ZxOTKRp7KJXj/3ec4ZMHtODogcCcQ9ESymNOQN9G7Ovjwk6zT YM+QemFf5PzNmHLfF+v79bQG45RkhyUJPe9nMGNeB0j7mgg9FkYZRZp8fdtOLJfHRFhn zvd+cfR0HQ2/ey8er0j8X5t4z6S3HoQLzStbqsG6x0LyEuL6e+Rl5blwu8PmQtST0rRn 9BmllBxfG4OV1QVr8g4Fg7mm9p9GD/UGTKf1HXS9HGaFeRG+lNhyObZechq//PgiiT/a HVWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=0tvZRA8H; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b34si16415656edb.439.2019.10.15.04.16.43; Tue, 15 Oct 2019 04:17:08 -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; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=0tvZRA8H; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727384AbfJOJQ2 (ORCPT + 99 others); Tue, 15 Oct 2019 05:16:28 -0400 Received: from merlin.infradead.org ([205.233.59.134]:34042 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725815AbfJOJQ2 (ORCPT ); Tue, 15 Oct 2019 05:16:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=oIjZ3ZGDT1Dl1PI+PmIwiAz6EXPC9LLSMZjb9xG3QJY=; b=0tvZRA8H2b8vxdLFiyget6nIR qFYTIRGIwD2c1gGkp8OPm0/zir5ZcOVOrsfxSDs+g55d4taRwX0tcxVMUBb8QTuL28O+tchAHm+zv byZWfBvvTSwYvJXlYHTnbVpUVmUcWZJF4Dpq1Lrt+2wfaX+YVvDI/Ntj0RUpbR3pcYWhLOl1rQzUq 31GwVUUIB6Mpaw1MXQOwohHZy2GYHctc2Z3lzMDCjgFTjgjkuv/munV468tDUGmDMAWGKJWwFX5Ua HmcR/A3WQ3Y+ozViXTkJSzpz/ECS9BXJ6wTPc0zuQmq1Jx0SnZGrJTto4oY4jGyO8QNeUPq6rKNS/ lpT8lIgQQ==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1iKIwF-00010O-SZ; Tue, 15 Oct 2019 09:16:21 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 6A5CD3032F8; Tue, 15 Oct 2019 11:15:23 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id CC90728B550B6; Tue, 15 Oct 2019 11:16:17 +0200 (CEST) Date: Tue, 15 Oct 2019 11:16:17 +0200 From: Peter Zijlstra To: Alexey Budankov 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 Subject: Re: [PATCH v1] perf/core: fix restoring of Intel LBR call stack on a context switch Message-ID: <20191015091617.GF2311@hirez.programming.kicks-ass.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.