Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp86491lqb; Tue, 16 Apr 2024 09:26:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVEvKlEjnuaEvTKga/2Fd/EUhwzzChgLcPedaQS/SBLy/KOxveuxu2l8AAs8vQpPl2WXqDt4QukVwWOlMWwtZaxnuEbd3KgxJgcpSH1gg== X-Google-Smtp-Source: AGHT+IHEaSr38oacwcXWgXmGi+lqG/hCs/swK1yxXwTtDUYrO1Yx+FvYUy0bPoBu8I/sKsiRiMCR X-Received: by 2002:a17:906:33d9:b0:a52:3975:6e47 with SMTP id w25-20020a17090633d900b00a5239756e47mr6329136eja.34.1713284816437; Tue, 16 Apr 2024 09:26:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713284816; cv=pass; d=google.com; s=arc-20160816; b=heT6p168x9jAQpamtJDyyRXIVSv85LPv69C8l1zgEaxnDot2I9tQeLKrn+bw6lfAXA mLTXVSB8+BWEjoKVXOL3m/5xaeYQ91HL4Z6YPOP5FGqOJ8yXOciR4+dZmpWuZEhZY1oa iuyMeISDf+Sg7o0DMWzPPC8M+41ZP2fvZ8ysn1IGfbX3Oiq2mho0MQRBbJeGrcDIXjhj j7Upnh6D3+EulCB9ZkVVVpEQhCmDbK0sQAmT9IAKA/0afOzFMFAgHdhodFUI0//gw11o OTAA5tZuWs/0/yKdeqm84utHuU273wshe+bEkqnEtjQtRfNywvsEaULEx3HiuaJJIRMJ ugkA== 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=gGBWCxCT7h001yOzrzbMsZT5KWWidJTLv3TkiZlNl94=; fh=2BHVZAUwEQhqGN4nAeElIyUYkcBs1heLEGZF5hFVFrc=; b=INs6dPIvRrt3dyFdq2rINgwnXXIhnW5c0DMFv0aOr2047cRSGKUeovsgEliNufcu/J eDylD9g0hro3nrs7D9g9A2t72Nful8JWEdcxv0FVYo1CuXrAfuwKDbEcDEFqcRk7IXTn aQ2E/gV3dQlVq85oDTK9pLcC6oPtnIAPgrJlrFDodhjmzzoGjf6+mjwazStRqf1c2ESP aQ3aQLqwRU/SNcvLfAiayk4ti715Ka2kq0Po/LR3IRr4UEjNru49BgXPJcqvQwfuXR6S G5mD1K6tjn1672FMhfMUQ+bkozl6X0FJ5fY/q2RFEdFnz3zwc9vz82puMgoO1ORNXywX tw+A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mEevrMEh; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-146818-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-146818-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id he15-20020a1709073d8f00b00a4739d7c665si6199512ejc.269.2024.04.16.09.26.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 09:26:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-146818-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mEevrMEh; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-146818-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-146818-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 am.mirrors.kernel.org (Postfix) with ESMTPS id C34331F224A4 for ; Tue, 16 Apr 2024 12:48:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D72A12BF25; Tue, 16 Apr 2024 12:48:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mEevrMEh" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (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 E35DA6BFDD; Tue, 16 Apr 2024 12:48:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.11 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713271704; cv=none; b=jExF1Vwfoj+TyMxJy05O2TzHFzyWYx4ndKVyjmO9fHYsthfyhQ9YAjG0OB+ZvhIMajAwhxJnmv5EZBO9UIKp3kNFLLW66+WtlI3vXH2vvY0AhG+cCmgDjkS+4nu8RL71wllRPzZ8rhxo6a6MaGfy4OxTX8IldISDOlLWaEpflbM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713271704; c=relaxed/simple; bh=icMcUvf31+/80gYgujfIto6pSPFOvMmLnaKaozMWjL0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=g/dwNlF2K40aHdrfNVpV6B5CREBVPl0G4fB/kXl6bbWXqPC6tmpXGB6oIVa1SJAhtLjcNgTIQKb8PJ9zgWpNgzYBOT0BhIf4Y+J0xuL1FmYlogqBQMUHFnjd52Gt+3pCGN+7HFFngbOYHaxwuQ8pucxxjTWAb6qJt2+LlV5rYiE= 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=mEevrMEh; arc=none smtp.client-ip=192.198.163.11 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=1713271703; x=1744807703; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=icMcUvf31+/80gYgujfIto6pSPFOvMmLnaKaozMWjL0=; b=mEevrMEhp9sL464IgUXAl0mbgwwYRONwkysp5hfOB5YkF4ox6qpQkP+h VrjFWUAgNEQhmcWnrZIxTiNzZGxLKQQa/DUGFdVVLuEeiYuKiCcpxoiKC hDYb5SafMLQaWq3oZmcKxpQKf9YgXVICrPXtnsLxS8yf/cDUHCQKC2rVJ vOuxW5kemi+2bR58tb8JpvaTMTaAArhpOEAQEJTileZFOobfuUQ4TOEkO Gg5WxdC0CDoh1QSfQ32Gwuv7+fste62kjmT1IY4hSsRKBcpz843kQ2L7c jQiSTO+gKIjm0sgINIfqmx9+j/e17baa5P2SKgTUoyP85yW8C/4eK47Lj g==; X-CSE-ConnectionGUID: MmuzrKozSzCqYV5UULhT8g== X-CSE-MsgGUID: 7+/QCdGKS+KLiF/JHOPETQ== X-IronPort-AV: E=McAfee;i="6600,9927,11046"; a="19309077" X-IronPort-AV: E=Sophos;i="6.07,206,1708416000"; d="scan'208";a="19309077" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Apr 2024 05:48:22 -0700 X-CSE-ConnectionGUID: mNaG9A7FTq+gt5+eM4DuKQ== X-CSE-MsgGUID: fF1WUH3iTo29H0/k4xAE1A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,206,1708416000"; d="scan'208";a="59699656" Received: from linux.intel.com ([10.54.29.200]) by orviesa001.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Apr 2024 05:48:22 -0700 Received: from [10.213.186.145] (kliang2-mobl1.ccr.corp.intel.com [10.213.186.145]) (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 B488C206DFDC; Tue, 16 Apr 2024 05:48:19 -0700 (PDT) Message-ID: Date: Tue, 16 Apr 2024 08:48:18 -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 02/41] perf: Support guest enter/exit interfaces To: "Zhang, Xiong Y" , Sean Christopherson Cc: pbonzini@redhat.com, peterz@infradead.org, mizhang@google.com, kan.liang@intel.com, zhenyuw@linux.intel.com, dapeng1.mi@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: <20240126085444.324918-1-xiong.y.zhang@linux.intel.com> <20240126085444.324918-3-xiong.y.zhang@linux.intel.com> <23af8648-ca9f-41d2-8782-f2ffc3c11e9e@linux.intel.com> <2342a4e2-2834-48e2-8403-f0050481e59e@linux.intel.com> <998fd76f-2bd9-4492-bf2e-e8cd981df67f@linux.intel.com> Content-Language: en-US From: "Liang, Kan" In-Reply-To: <998fd76f-2bd9-4492-bf2e-e8cd981df67f@linux.intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 2024-04-16 1:34 a.m., Zhang, Xiong Y wrote: > > > On 4/16/2024 12:03 AM, Liang, Kan wrote: >> >> >> On 2024-04-12 4:56 p.m., Liang, Kan wrote: >>>> What if perf had a global knob to enable/disable mediate PMU support? Then when >>>> KVM is loaded with enable_mediated_true, call into perf to (a) check that there >>>> are no existing !exclude_guest events (this part could be optional), and (b) set >>>> the global knob to reject all new !exclude_guest events (for the core PMU?). >>>> >>>> Hmm, or probably better, do it at VM creation. That has the advantage of playing >>>> nice with CONFIG_KVM=y (perf could reject the enabling without completely breaking >>>> KVM), and not causing problems if KVM is auto-probed but the user doesn't actually >>>> want to run VMs. >>> I think it should be doable, and may simplify the perf implementation. >>> (The check in the schedule stage should not be necessary anymore.) >>> >>> With this, something like NMI watchdog should fail the VM creation. The >>> user should either disable the NMI watchdog or use a replacement. >>> >>> Thanks, >>> Kan >>>> E.g. (very roughly) >>>> >>>> int x86_perf_get_mediated_pmu(void) >>>> { >>>> if (refcount_inc_not_zero(...)) >>>> return 0; >>>> >>>> if () >>>> return -EBUSY; >>>> >>>> >>>> } >>>> >>>> void x86_perf_put_mediated_pmu(void) >>>> { >>>> if (!refcount_dec_and_test(...)) >>>> return; >>>> >>>> >>>> } >> >> >> I think the locking should include the refcount check and system wide >> event check as well. >> It should be possible that two VMs are created very close. >> The second creation may mistakenly return 0 if there is no lock. >> >> I plan to do something as below (not test yet). >> >> +/* >> + * Currently invoked at VM creation to >> + * - Check whether there are existing !exclude_guest system wide events >> + * of PMU with PERF_PMU_CAP_MEDIATED_VPMU >> + * - Set nr_mediated_pmu to prevent !exclude_guest event creation on >> + * PMUs with PERF_PMU_CAP_MEDIATED_VPMU >> + * >> + * No impact for the PMU without PERF_PMU_CAP_MEDIATED_VPMU. The perf >> + * still owns all the PMU resources. >> + */ >> +int x86_perf_get_mediated_pmu(void) >> +{ >> + int ret = 0; >> + mutex_lock(&perf_mediated_pmu_mutex); >> + if (refcount_inc_not_zero(&nr_mediated_pmu_vms)) >> + goto end; >> + >> + if (atomic_read(&nr_include_guest_events)) { >> + ret = -EBUSY; >> + goto end; >> + } >> + refcount_inc(&nr_mediated_pmu_vms); >> +end: >> + mutex_unlock(&perf_mediated_pmu_mutex); >> + return ret; >> +} >> +EXPORT_SYMBOL_GPL(x86_perf_get_mediated_pmu); >> + >> +void x86_perf_put_mediated_pmu(void) >> +{ >> + mutex_lock(&perf_mediated_pmu_mutex); >> + refcount_dec(&nr_mediated_pmu_vms); >> + mutex_unlock(&perf_mediated_pmu_mutex); >> +} >> +EXPORT_SYMBOL_GPL(x86_perf_put_mediated_pmu); >> >> >> Thanks, >> Kan > x86_perf_get_mediated_pmu() is called at vm_create(), x86_perf_put_mediated_pmu() is called at vm_destroy(), then system wide perf events without exclude_guest=1 can not be created during the whole vm life cycle (where nr_mediated_pmu_vms > 0 always), do I understand and use the interface correctly ? Right, but it only impacts the events of PMU with the PERF_PMU_CAP_MEDIATED_VPMU. For other PMUs, the event with exclude_guest=1 can still be created. KVM should not touch the counters of the PMU without PERF_PMU_CAP_MEDIATED_VPMU. BTW: I will also remove the prefix x86, since the functions are in the generic code. Thanks, Kan