Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2366749lqt; Mon, 22 Apr 2024 08:49:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWQdSSOaykSvnYC1wYMCHoRIQkGkfj6C+JjEeo//AGpP5vd6/9pFNkv3NgB7JNo+LohlW2qtSH/UiIW1ryEPCo2U+eJYezkyATaitewNg== X-Google-Smtp-Source: AGHT+IHE6loh4V0drXUMCYlHxA7kZDx84mvAwb4CxWxnixK4DJLQpoMjOvd3IwV75LBvXedHaZCy X-Received: by 2002:a17:902:d481:b0:1e0:b62c:460d with SMTP id c1-20020a170902d48100b001e0b62c460dmr14664829plg.38.1713800959649; Mon, 22 Apr 2024 08:49:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713800959; cv=pass; d=google.com; s=arc-20160816; b=1CsqEUWzT5JOj7bu3fy2lvvuxCgmLt+AcAlbOxrf51xRx2tDBQ8Zc8Rz60NKH/4gBg SjsVhXKYyGAgsGBhm3pVnWRIH3mSRr/TjnXEdjGRvbDjuWZa6YaH4d8QuSdzY8HxHJ3u P+pF6LYjUFd0RsqpUzZX+nFnwz78HxAalPUoBls/mbetLoJRQh77p5jp2Rew2u62hYsT 8J4/zD2nztA/P0eFtemFLACt/YpcgRZHRvw/ewZfyEF1KClpYgLIm4VY7UqEbISYpYrw tsi2jXtcjPCt4yY/2BvqwFby6WlQylgbe5YC77/EyCePoVgVyO3c1qtBlXcecqNM/VVp OiDg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=fMAUzzhHTsmZdmb9pm8IobQWgPvkWjpBR9N9dvrl2T8=; fh=EkSS16PuOk+PFNoP6PSd9hc5qvSvQwec6vHX4D+0GV8=; b=CyBYlHCWGBVPYLRqCdHx/JxrBkR97s9/xmxIaF0E8jEOyD7cYdkt/WjDcrIDCoby5r xn+bRom1zfhrIy+p96nUmmELDY0jeoBVO3b71hVAhZqjNEVGnlyhxxVNV1b41og9HebC beT/c0aF+dqUr1pMa0An0AOAlRZFknkDl91oKV31zv+LUJ3jEnPeYVj4tt6P/OXuyTWy +EkFJV7/VbJ3wqfWpW0owXiaCdLjxCI4PFt7Gdz/rdxsDkmpcRBNW6XDeleRbgMXWgDt ELiE+kzqlm2zQS8CrhGgYqRhFzamucnfESmfXS/v/Ww71/YloGML04PbDj31+AsSHxGC 0zPg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eqzijObR; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-153655-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153655-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e13-20020a170902ef4d00b001dd693cc79csi8101475plx.620.2024.04.22.08.49.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 08:49:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-153655-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eqzijObR; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-153655-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-153655-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 2964B283880 for ; Mon, 22 Apr 2024 15:48:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 321F215356C; Mon, 22 Apr 2024 15:48:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="eqzijObR" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C4911534F3; Mon, 22 Apr 2024 15:48:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713800889; cv=none; b=EABxh2EwJk1S/Wgjb/kRypky4dcoLhnVaYyy5FJzsn4r68Qf0+RV8p8f0W5otd61STKoVJHb3Z2O8OHRZsqxvV5h8EUDkLOqqvJvYVVOTzICCzv3u2rpCKR0OJcl9oYMCrMvIRKiQKLJkZNvqfnEwo0ULFezmjD2351zboN41vA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713800889; c=relaxed/simple; bh=TGkCC1wb/ipHHm0xz1HjzQqToV5Sfv84Bet9nt2x64E=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=YZoq94HMSZPRiteVYokdohvTcljQnl+435eRB4dXTIQoxZLS8PjXNxB9PlOoL8LtFjE4MU6JgkBKEiXkXJAwMNOAgXPhp7je7jTkBeylGfmli4N3Bi56LVgsfjQ7Hxe+vQy/VZwE7OcwvwtvY+qJ93yZHxvVMjnpWNxMJTjV6lY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eqzijObR; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14060C113CC; Mon, 22 Apr 2024 15:48:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713800888; bh=TGkCC1wb/ipHHm0xz1HjzQqToV5Sfv84Bet9nt2x64E=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eqzijObRUsJ59JAHcqlBP0SQl5L5rx9qT/7vnci9gVpigjDM4nbbwfpOoOTHizdQt eNvZISlygM5uYM0hosK0Vmom9czSy3J7P3Pj72vY1r5dZDVzppKwqbB0t9rvG78wPo Ocoo829L6rUzqT/8+eIU//ta/cAI+1pPrlUlaAL6EWoweY32E7S8ewcipGnVSRinCv R6zXwDoAC1cYV36MYmO0IOQq2YdqHhvWhNpyprxnUWCeCcIyjvZ/ngRRGD2rFMfqeW AkvrVN+vANtrmgGHLXyChEiTTbxac62koeGlwjWSDiCP/K34jP6KFMzL/GsMF3z+Z6 3uSrzXaBvZQrA== Date: Mon, 22 Apr 2024 21:15:02 +0530 From: Naveen N Rao To: Gautam Menghani Cc: mpe@ellerman.id.au, npiggin@gmail.com, christophe.leroy@csgroup.eu, aneesh.kumar@kernel.org, Vaibhav Jain , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: Re: [PATCH v5 RESEND] arch/powerpc/kvm: Add support for reading VPA counters for pseries guests Message-ID: References: <20240402070656.28441-1-gautam@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240402070656.28441-1-gautam@linux.ibm.com> On Tue, Apr 02, 2024 at 12:36:54PM +0530, Gautam Menghani wrote: > PAPR hypervisor has introduced three new counters in the VPA area of > LPAR CPUs for KVM L2 guest (see [1] for terminology) observability - 2 > for context switches from host to guest and vice versa, and 1 counter > for getting the total time spent inside the KVM guest. Add a tracepoint > that enables reading the counters for use by ftrace/perf. Note that this > tracepoint is only available for nestedv2 API (i.e, KVM on PowerVM). > > Also maintain an aggregation of the context switch times in vcpu->arch. > This will be useful in getting the aggregate times with a pmu driver > which will be upstreamed in the near future. It would be better to add code to maintain aggregate times as part of that pmu driver. > > [1] Terminology: > a. L1 refers to the VM (LPAR) booted on top of PAPR hypervisor > b. L2 refers to the KVM guest booted on top of L1. > > Signed-off-by: Vaibhav Jain > Signed-off-by: Gautam Menghani > --- > v5 RESEND: > 1. Add the changelog > > v4 -> v5: > 1. Define helper functions for getting/setting the accumulation counter > in L2's VPA > > v3 -> v4: > 1. After vcpu_run, check the VPA flag instead of checking for tracepoint > being enabled for disabling the cs time accumulation. > > v2 -> v3: > 1. Move the counter disabling and zeroing code to a different function. > 2. Move the get_lppaca() inside the tracepoint_enabled() branch. > 3. Add the aggregation logic to maintain total context switch time. > > v1 -> v2: > 1. Fix the build error due to invalid struct member reference. > > arch/powerpc/include/asm/kvm_host.h | 5 ++++ > arch/powerpc/include/asm/lppaca.h | 11 +++++--- > arch/powerpc/kvm/book3s_hv.c | 40 +++++++++++++++++++++++++++++ > arch/powerpc/kvm/trace_hv.h | 25 ++++++++++++++++++ > 4 files changed, 78 insertions(+), 3 deletions(-) > > diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h > index 8abac532146e..d953b32dd68a 100644 > --- a/arch/powerpc/include/asm/kvm_host.h > +++ b/arch/powerpc/include/asm/kvm_host.h > @@ -847,6 +847,11 @@ struct kvm_vcpu_arch { > gpa_t nested_io_gpr; > /* For nested APIv2 guests*/ > struct kvmhv_nestedv2_io nestedv2_io; > + > + /* Aggregate context switch and guest run time info (in ns) */ > + u64 l1_to_l2_cs_agg; > + u64 l2_to_l1_cs_agg; > + u64 l2_runtime_agg; Can be dropped from this patch. > #endif > > #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING > diff --git a/arch/powerpc/include/asm/lppaca.h b/arch/powerpc/include/asm/lppaca.h > index 61ec2447dabf..bda6b86b9f13 100644 > --- a/arch/powerpc/include/asm/lppaca.h > +++ b/arch/powerpc/include/asm/lppaca.h > @@ -62,7 +62,8 @@ struct lppaca { > u8 donate_dedicated_cpu; /* Donate dedicated CPU cycles */ > u8 fpregs_in_use; > u8 pmcregs_in_use; > - u8 reserved8[28]; > + u8 l2_accumul_cntrs_enable; /* Enable usage of counters for KVM guest */ A simpler name - l2_counters_enable or such? > + u8 reserved8[27]; > __be64 wait_state_cycles; /* Wait cycles for this proc */ > u8 reserved9[28]; > __be16 slb_count; /* # of SLBs to maintain */ > @@ -92,9 +93,13 @@ struct lppaca { > /* cacheline 4-5 */ > > __be32 page_ins; /* CMO Hint - # page ins by OS */ > - u8 reserved12[148]; > + u8 reserved12[28]; > + volatile __be64 l1_to_l2_cs_tb; > + volatile __be64 l2_to_l1_cs_tb; > + volatile __be64 l2_runtime_tb; > + u8 reserved13[96]; > volatile __be64 dtl_idx; /* Dispatch Trace Log head index */ > - u8 reserved13[96]; > + u8 reserved14[96]; > } ____cacheline_aligned; > > #define lppaca_of(cpu) (*paca_ptrs[cpu]->lppaca_ptr) > diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c > index 8e86eb577eb8..fea1c1429975 100644 > --- a/arch/powerpc/kvm/book3s_hv.c > +++ b/arch/powerpc/kvm/book3s_hv.c > @@ -4108,6 +4108,37 @@ static void vcpu_vpa_increment_dispatch(struct kvm_vcpu *vcpu) > } > } > > +static inline int kvmhv_get_l2_accumul(void) > +{ > + return get_lppaca()->l2_accumul_cntrs_enable; > +} > + > +static inline void kvmhv_set_l2_accumul(int val) ^^^ bool? > +{ > + get_lppaca()->l2_accumul_cntrs_enable = val; > +} > + > +static void do_trace_nested_cs_time(struct kvm_vcpu *vcpu) > +{ > + struct lppaca *lp = get_lppaca(); > + u64 l1_to_l2_ns, l2_to_l1_ns, l2_runtime_ns; > + > + l1_to_l2_ns = tb_to_ns(be64_to_cpu(lp->l1_to_l2_cs_tb)); > + l2_to_l1_ns = tb_to_ns(be64_to_cpu(lp->l2_to_l1_cs_tb)); > + l2_runtime_ns = tb_to_ns(be64_to_cpu(lp->l2_runtime_tb)); > + trace_kvmppc_vcpu_exit_cs_time(vcpu, l1_to_l2_ns, l2_to_l1_ns, > + l2_runtime_ns); > + lp->l1_to_l2_cs_tb = 0; > + lp->l2_to_l1_cs_tb = 0; > + lp->l2_runtime_tb = 0; > + kvmhv_set_l2_accumul(0); > + > + // Maintain an aggregate of context switch times > + vcpu->arch.l1_to_l2_cs_agg += l1_to_l2_ns; > + vcpu->arch.l2_to_l1_cs_agg += l2_to_l1_ns; > + vcpu->arch.l2_runtime_agg += l2_runtime_ns; > +} > + > static int kvmhv_vcpu_entry_nestedv2(struct kvm_vcpu *vcpu, u64 time_limit, > unsigned long lpcr, u64 *tb) > { > @@ -4130,6 +4161,11 @@ static int kvmhv_vcpu_entry_nestedv2(struct kvm_vcpu *vcpu, u64 time_limit, > kvmppc_gse_put_u64(io->vcpu_run_input, KVMPPC_GSID_LPCR, lpcr); > > accumulate_time(vcpu, &vcpu->arch.in_guest); > + > + /* Enable the guest host context switch time tracking */ > + if (unlikely(trace_kvmppc_vcpu_exit_cs_time_enabled())) > + kvmhv_set_l2_accumul(1); > + > rc = plpar_guest_run_vcpu(0, vcpu->kvm->arch.lpid, vcpu->vcpu_id, > &trap, &i); > > @@ -4156,6 +4192,10 @@ static int kvmhv_vcpu_entry_nestedv2(struct kvm_vcpu *vcpu, u64 time_limit, > > timer_rearm_host_dec(*tb); > > + /* Record context switch and guest_run_time data */ > + if (kvmhv_get_l2_accumul()) > + do_trace_nested_cs_time(vcpu); > + > return trap; > } I'm assuming the counters in VPA are cumulative, since you are zero'ing them out on exit. If so, I think a better way to implement this is to use TRACE_EVENT_FN() and provide tracepoint registration and unregistration functions. You can then enable the counters once during registration and avoid repeated writes to the VPA area. With that, you also won't need to do anything before vcpu entry. If you maintain previous values, you can calculate the delta and emit the trace on vcpu exit. The values in VPA area can then serve as the cumulative values. > > diff --git a/arch/powerpc/kvm/trace_hv.h b/arch/powerpc/kvm/trace_hv.h > index 8d57c8428531..ab19977c91b4 100644 > --- a/arch/powerpc/kvm/trace_hv.h > +++ b/arch/powerpc/kvm/trace_hv.h > @@ -491,6 +491,31 @@ TRACE_EVENT(kvmppc_run_vcpu_enter, > TP_printk("VCPU %d: tgid=%d", __entry->vcpu_id, __entry->tgid) > ); > > +TRACE_EVENT(kvmppc_vcpu_exit_cs_time, Not sure what "exit" signifies in the tracepoint name. Can this be simplified to kvmppc_vcpu_cs_time? Perhaps kvmppc_vcpu_stats, which will allow more vcpu stats to be exposed in future as necessary? > + TP_PROTO(struct kvm_vcpu *vcpu, u64 l1_to_l2_cs, u64 l2_to_l1_cs, > + u64 l2_runtime), Can be on a single line, we no longer restrict lines to 80 columns. 100 or so is fine. > + > + TP_ARGS(vcpu, l1_to_l2_cs, l2_to_l1_cs, l2_runtime), > + > + TP_STRUCT__entry( > + __field(int, vcpu_id) > + __field(__u64, l1_to_l2_cs_ns) > + __field(__u64, l2_to_l1_cs_ns) > + __field(__u64, l2_runtime_ns) Not sure there is a reason to use __u64 - just u64 should work. > + ), > + > + TP_fast_assign( > + __entry->vcpu_id = vcpu->vcpu_id; > + __entry->l1_to_l2_cs_ns = l1_to_l2_cs; > + __entry->l2_to_l1_cs_ns = l2_to_l1_cs; > + __entry->l2_runtime_ns = l2_runtime; > + ), > + > + TP_printk("VCPU %d: l1_to_l2_cs_time=%llu-ns l2_to_l1_cs_time=%llu-ns l2_runtime=%llu-ns", ^^^ You can drop the hyphen before "ns". Just put a space there. > + __entry->vcpu_id, __entry->l1_to_l2_cs_ns, > + __entry->l2_to_l1_cs_ns, __entry->l2_runtime_ns) There is l1_to_l2_cs, l1_to_l2_cs_ns and l1_to_l2_cs_time - can you use a single name for that? > +); > + As a minor nit, it will be good to put the new tracepoint after the below vcpu exit tracepoint just so the entry/exit tracepoints are together in the file. > TRACE_EVENT(kvmppc_run_vcpu_exit, > TP_PROTO(struct kvm_vcpu *vcpu), > > -- > 2.43.2 > - Naveen