Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1466247lqd; Thu, 25 Apr 2024 17:22:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVca7KbeCDLzeKY568DsC85kqWnEkGqGVISsFlz7OVFaqGCKo1pgYmpgp2FJ9NFz84rRfvWraTgP25FGA6biZaweUDwmZtDU3GPAf9uNQ== X-Google-Smtp-Source: AGHT+IGTY97lMkYxJqGz650lrYdY2ddIc/UKWd5RkvFwSHfuALKsOn2V+SMzBqfOETYjHl5xkfEz X-Received: by 2002:a05:6871:3411:b0:239:700d:61cd with SMTP id nh17-20020a056871341100b00239700d61cdmr1102569oac.8.1714090951647; Thu, 25 Apr 2024 17:22:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714090951; cv=pass; d=google.com; s=arc-20160816; b=HWutW9qQtSIBD3gAiRbVn4fuDi9+NhdLvnwJe+MUtrLrhx6KYsGQQ7LesZ2qAOVHXs 6lrEvd4Q7gy9L4RJPOsNhaBy3ePezsbFE4Tm+7VqK2WCmqFpgeXScnIje3IKRu2V7MEb Gyt+QfQuMTQUvPx5u5vlp+ZD0hFNiSYtW4ig/l++cfhjUjvlUMPUg17hb7cgnJtIaaZa hCYABmaIUEpWu5h2rI+kArsVjhlrMQumeVddPASTfLvYhXRRkxlOcQb9+cM4DiYu34A8 GXdYrFiNkrDl1f1qVKv9svSGydoX0aWBIp7g8jnXS9BhjqdlCU88SMdgF0H1puGbu2ct WVJw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=NhdyPblN85wtaKQ5OhMHaim5HzG8Sn+AY50/RgGoKFU=; fh=wF7Z/LKBnfEwcvm7HbBINjAy+OTBbmckuh38PDIBzXw=; b=VC6XFAHLGRVdbmPJ47517d4a8nC0XcQss2/6pK/ZXhxBrdZjxxAhAkI8BfAljOFKvT HqKlxHy3mmuLOmr/BZngBThsdx6SXSnH782edwa5e0fp+RDMZjW29sQCmBkIXfR5UbK5 JAolu34nmBmGS4B7acKbCvZtgZoN/tnf00hVU4p0DcFb81TMPYUjcTPKzcxkPXI8V7K9 pb4fGJYIKSAx1o66hjki0gvUdWIFMcywYyCViDFtGhsF5xn76tG+EQ7+twBbeH1wo2wl aiq+EYFWMRkWx+ua88Rd2Za4hYSre8BeHEDMifMGVY8FyC1Mkr4cq0bbFB63ErdkFDt7 KkHw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fDAKMudm; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-159155-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159155-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id r6-20020a632046000000b005cd8486d5ccsi14225985pgm.65.2024.04.25.17.22.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:22:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159155-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=@intel.com header.s=Intel header.b=fDAKMudm; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-159155-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159155-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com 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 796382820CB for ; Thu, 25 Apr 2024 20:44:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3F3F715572A; Thu, 25 Apr 2024 20:43:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="fDAKMudm" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (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 307CF153834; Thu, 25 Apr 2024 20:43:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.13 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714077826; cv=none; b=U0QtVxCfQap6iiy/HTonUDCE02cnCwhkBSrLh9oR2cusYQ/h1s2lUyY2JpCv9IvN0WakvkVN1JBHl2WOVkP3TClRuRcvu2hvF3Ebu84HmuSTwsf4zevlZkdOhBLDDLl/az5lQIzhsOeqaqyOLyf/juMUe9HGXiKjlcPxU4RL7jY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714077826; c=relaxed/simple; bh=/Bry/LrWz5s3cSAoG/NRli3XyUXM2aR+pdRh6BaisJI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=cgKIwssdIe+aI454K/EGysOjJcM8DE0QIrw0q7QgKn4rx1DwTMBxhu5Nda+UDBAbw3DSoI71b0zflqIiF2dZzqH2M7xtyGPk09G5/0v2POD6e+QtAdufYEh2i4uyXUsdnn7ZTMbLjdWTZIovCjVVgurA/QEfkCvseTvBu8FhCjQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=fDAKMudm; arc=none smtp.client-ip=192.198.163.13 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1714077824; x=1745613824; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=/Bry/LrWz5s3cSAoG/NRli3XyUXM2aR+pdRh6BaisJI=; b=fDAKMudml7s8S6cbiBZM3eQJ6rPz4KH+enecXg4i1ooepCevzeOjdkql jGFXq78l6AbvHhNWSUXdrFEPyp6VG9uXwxGrcDeafs9dEL246heZjVX7Q nVhcuvAVMCjD8jILYwxz00/yYk0CKnLRVuQD4Nid8xXrxM2Of7k74nhLI Dq8ZNV94FCInvtqHOvu+KJhQ66nKKMQE1fPdtbgwBzNt0czRUMSP3UJ92 dwIyuMOnDqP1GhTCKdpTFEk7wXRgxk2otudArTTM3YBAJ+vYYUxGC0+CW s+kUnWDIFY3jqkzm8NtXsS5fWy3Qliy0kef8UOEyg8iJkjaqUBhtpa/rs g==; X-CSE-ConnectionGUID: +9cD1Oj7S/CzpudElRlSXA== X-CSE-MsgGUID: HdguReztRk+01jG1fIOD+g== X-IronPort-AV: E=McAfee;i="6600,9927,11055"; a="12732535" X-IronPort-AV: E=Sophos;i="6.07,230,1708416000"; d="scan'208";a="12732535" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Apr 2024 13:43:43 -0700 X-CSE-ConnectionGUID: 7KgeubM6TomVXo4FwsgdrA== X-CSE-MsgGUID: HalkeOTpSIGqjCfc+OhXKQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,230,1708416000"; d="scan'208";a="25609924" Received: from linux.intel.com ([10.54.29.200]) by orviesa006.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Apr 2024 13:43:44 -0700 Received: from [10.212.96.44] (kliang2-mobl1.ccr.corp.intel.com [10.212.96.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by linux.intel.com (Postfix) with ESMTPS id 1266C206DFCE; Thu, 25 Apr 2024 13:43:40 -0700 (PDT) Message-ID: <42acf1fc-1603-4ac5-8a09-edae2d85963d@linux.intel.com> Date: Thu, 25 Apr 2024 16:43:39 -0400 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH 23/41] KVM: x86/pmu: Implement the save/restore of PMU state for Intel CPU To: Mingwei Zhang Cc: "Mi, Dapeng" , Sean Christopherson , maobibo , Xiong Zhang , pbonzini@redhat.com, peterz@infradead.org, kan.liang@intel.com, zhenyuw@linux.intel.com, jmattson@google.com, kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, zhiyuan.lv@intel.com, eranian@google.com, irogers@google.com, samantha.alt@intel.com, like.xu.linux@gmail.com, chao.gao@intel.com References: <1ec7a21c-71d0-4f3e-9fa3-3de8ca0f7315@linux.intel.com> <5279eabc-ca46-ee1b-b80d-9a511ba90a36@loongson.cn> <7834a811-4764-42aa-8198-55c4556d947b@linux.intel.com> <6af2da05-cb47-46f7-b129-08463bc9469b@linux.intel.com> Content-Language: en-US From: "Liang, Kan" In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 2024-04-25 4:16 p.m., Mingwei Zhang wrote: > On Thu, Apr 25, 2024 at 9:13 AM Liang, Kan wrote: >> >> >> >> On 2024-04-25 12:24 a.m., Mingwei Zhang wrote: >>> On Wed, Apr 24, 2024 at 8:56 PM Mi, Dapeng wrote: >>>> >>>> >>>> On 4/24/2024 11:00 PM, Sean Christopherson wrote: >>>>> On Wed, Apr 24, 2024, Dapeng Mi wrote: >>>>>> On 4/24/2024 1:02 AM, Mingwei Zhang wrote: >>>>>>>>> Maybe, (just maybe), it is possible to do PMU context switch at vcpu >>>>>>>>> boundary normally, but doing it at VM Enter/Exit boundary when host is >>>>>>>>> profiling KVM kernel module. So, dynamically adjusting PMU context >>>>>>>>> switch location could be an option. >>>>>>>> If there are two VMs with pmu enabled both, however host PMU is not >>>>>>>> enabled. PMU context switch should be done in vcpu thread sched-out path. >>>>>>>> >>>>>>>> If host pmu is used also, we can choose whether PMU switch should be >>>>>>>> done in vm exit path or vcpu thread sched-out path. >>>>>>>> >>>>>>> host PMU is always enabled, ie., Linux currently does not support KVM >>>>>>> PMU running standalone. I guess what you mean is there are no active >>>>>>> perf_events on the host side. Allowing a PMU context switch drifting >>>>>>> from vm-enter/exit boundary to vcpu loop boundary by checking host >>>>>>> side events might be a good option. We can keep the discussion, but I >>>>>>> won't propose that in v2. >>>>>> I suspect if it's really doable to do this deferring. This still makes host >>>>>> lose the most of capability to profile KVM. Per my understanding, most of >>>>>> KVM overhead happens in the vcpu loop, exactly speaking in VM-exit handling. >>>>>> We have no idea when host want to create perf event to profile KVM, it could >>>>>> be at any time. >>>>> No, the idea is that KVM will load host PMU state asap, but only when host PMU >>>>> state actually needs to be loaded, i.e. only when there are relevant host events. >>>>> >>>>> If there are no host perf events, KVM keeps guest PMU state loaded for the entire >>>>> KVM_RUN loop, i.e. provides optimal behavior for the guest. But if a host perf >>>>> events exists (or comes along), the KVM context switches PMU at VM-Enter/VM-Exit, >>>>> i.e. lets the host profile almost all of KVM, at the cost of a degraded experience >>>>> for the guest while host perf events are active. >>>> >>>> I see. So KVM needs to provide a callback which needs to be called in >>>> the IPI handler. The KVM callback needs to be called to switch PMU state >>>> before perf really enabling host event and touching PMU MSRs. And only >>>> the perf event with exclude_guest attribute is allowed to create on >>>> host. Thanks. >>> >>> Do we really need a KVM callback? I think that is one option. >>> >>> Immediately after VMEXIT, KVM will check whether there are "host perf >>> events". If so, do the PMU context switch immediately. Otherwise, keep >>> deferring the context switch to the end of vPMU loop. >>> >>> Detecting if there are "host perf events" would be interesting. The >>> "host perf events" refer to the perf_events on the host that are >>> active and assigned with HW counters and that are saved when context >>> switching to the guest PMU. I think getting those events could be done >>> by fetching the bitmaps in cpuc. >> >> The cpuc is ARCH specific structure. I don't think it can be get in the >> generic code. You probably have to implement ARCH specific functions to >> fetch the bitmaps. It probably won't worth it. >> >> You may check the pinned_groups and flexible_groups to understand if >> there are host perf events which may be scheduled when VM-exit. But it >> will not tell the idx of the counters which can only be got when the >> host event is really scheduled. >> >>> I have to look into the details. But >>> at the time of VMEXIT, kvm should already have that information, so it >>> can immediately decide whether to do the PMU context switch or not. >>> >>> oh, but when the control is executing within the run loop, a >>> host-level profiling starts, say 'perf record -a ...', it will >>> generate an IPI to all CPUs. Maybe that's when we need a callback so >>> the KVM guest PMU context gets preempted for the host-level profiling. >>> Gah.. >>> >>> hmm, not a fan of that. That means the host can poke the guest PMU >>> context at any time and cause higher overhead. But I admit it is much >>> better than the current approach. >>> >>> The only thing is that: any command like 'perf record/stat -a' shot in >>> dark corners of the host can preempt guest PMUs of _all_ running VMs. >>> So, to alleviate that, maybe a module parameter that disables this >>> "preemption" is possible? This should fit scenarios where we don't >>> want guest PMU to be preempted outside of the vCPU loop? >>> >> >> It should not happen. For the current implementation, perf rejects all >> the !exclude_guest system-wide event creation if a guest with the vPMU >> is running. >> However, it's possible to create an exclude_guest system-wide event at >> any time. KVM cannot use the information from the VM-entry to decide if >> there will be active perf events in the VM-exit. > > Hmm, why not? If there is any exclude_guest system-wide event, > perf_guest_enter() can return something to tell KVM "hey, some active > host events are swapped out. they are originally in counter #2 and > #3". If so, at the time when perf_guest_enter() returns, KVM will ack > that and keep it in its pmu data structure. I think it's possible that someone creates !exclude_guest event after the perf_guest_enter(). The stale information is saved in the KVM. Perf will schedule the event in the next perf_guest_exit(). KVM will not know it. > > Now, when doing context switching back to host at just VMEXIT, KVM > will check this data and see if host perf context has something active > (of course, they are all exclude_guest events). If not, deferring the > context switch to vcpu boundary. Otherwise, do the proper PMU context > switching by respecting the occupied counter positions on the host > side, i.e., avoid doubling the work on the KVM side. > > Kan, any suggestion on the above approach? I think we can only know the accurate event list at perf_guest_exit(). You may check the pinned_groups and flexible_groups, which tell if there are candidate events. > Totally understand that > there might be some difficulty, since perf subsystem works in several > layers and obviously fetching low-level mapping is arch specific work. > If that is difficult, we can split the work in two phases: 1) phase > #1, just ask perf to tell kvm if there are active exclude_guest events > swapped out; 2) phase #2, ask perf to tell their (low-level) counter > indices. > If you want an accurate counter mask, the changes in the arch specific code is required. Two phases sound good to me. Besides perf changes, I think the KVM should also track which counters need to be saved/restored. The information can be get from the EventSel interception. Thanks, Kan >> >> The perf_guest_exit() will reload the host state. It's impossible to >> save the guest state after that. We may need a KVM callback. So perf can >> tell KVM whether to save the guest state before perf reloads the host state. >> >> Thanks, >> Kan >>>> >>>> >>>>> >>>>> My original sketch: https://lore.kernel.org/all/ZR3eNtP5IVAHeFNC@googlecom >>> >