From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C103C282E1 for ; Wed, 14 May 2025 23:19:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747264781; cv=none; b=CtofxKvPt/RTnicReuYzjOFLC/W5ImLXfIBOSC5bAhdbfGTZ9QnSIT2KMoWZ+Vc7ckuvsisbCR+Adh+7+b4vsNtltiy2igN61O1R/XgoSlS68+wsKy5lWRrIfCzxo9/xD73Oir9Lg2+4xH76aYcdZr+Ntk9wXUjAWD1fxSeTkyo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747264781; c=relaxed/simple; bh=cNlVPYYnsQtqPjb2poMyKHUJYGJOPlxbA0jgyo6XDX8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SnKYu01KSfxwKZ/xQssNjxbh+KUc7F5eB3A34HCPoI9dWtHG1jUXSVH3jCI2+KfkxZOeKzOlNE3AK39WrfxiYiRhd4BXMmjanGHFPky3+7ECfD581CAE/3Hsglwnn6nvRHHT/PjIohjYDp2xB7qNp+CBKCNE7f8361F5Z/ULPCM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=T3DNf+WR; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="T3DNf+WR" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30ab5d34fdbso346785a91.0 for ; Wed, 14 May 2025 16:19:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747264778; x=1747869578; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=inzL0y+cbDFsyp/vzuyCwIe54VBn5YH91v/9/7m3MOg=; b=T3DNf+WRt5TCmAdcQ2vjHChChL+rwFC4OyuabZZXH3xG2eihOqqE0q38YK5UjrXvfK g7hSKkygwQRYlIGAsjaZhpH+GYVdZWL/ZAwRBHy6pQmVlG3igiYuBe49tVZ2KkRCyW7+ qMtGr+/PDlHcMVzt9pM1eBFCS01/nhzQ0oDKgB5JNKWrmLb+Pp1ASpGnBd7xjxZVVpos Vw/kSahbU+LOc5hM9k9OB3lJoNIfyl5uO+n1sys9BjncipwhV0SHKay2xB/zO/4froQ9 2L6rWGMwqhhip/wuTW2gUrx+wlO1CxqFLgUTMFRsG9Jktw1I21fKx1IN1q99p7WjexN1 aM3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747264778; x=1747869578; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=inzL0y+cbDFsyp/vzuyCwIe54VBn5YH91v/9/7m3MOg=; b=pJSB1lcQtndNg1VnWEi+mtaePwjj1o/EUf5xd7U5MAvTByafs4auwTdmbZ2macMc94 QuiZ1LGQxF/tvpCcyaAfZ0RIoJAQfKG9veHxS/vV3GAHLYcFnNNXrOsNasw0gGRk02qo 1VgUeZyLKi8pZqWodneRZByeDkzoHR2X6LuosqwP5/rUZZU48R09uhMMbi7LVqAZJQO0 mmXYZnoO+x3r7cmSLlibxRpOXwACB6OSe5xKNU//oqBC81L5Ab+ijLOoYxfj75iTLN43 owDZVxjjPEAUD4hsZEH91OFPAYGkSms8X2GbrX33ewdSaqPY3SsqlK/bTxj0rUn/i+HP C3Eg== X-Forwarded-Encrypted: i=1; AJvYcCVzCKT0YFv4bV1RcdAzsQeC1lG1oKTvZyTFYeZbXkbzZ84q5e3QSDXGAwgh6FD9EcZgvLk=@vger.kernel.org X-Gm-Message-State: AOJu0YxtKR/cwgW43b9EBYENcaGx/NRX3OlrTHYM01AokPtwM3KdcTIo IY1lvUgrfWLvzNTNvv9naGRp3c3YwzUD8wOncEyr0/3Eo7XOu0A+Acjs+RdylmJi/5KTLS392T5 NcA== X-Google-Smtp-Source: AGHT+IGCQ/sRX+3EYNcQDJbTeqc67xnpDL/NXZuajHJEmLyvMC58UdBnQowu6qn5H0+QlUsXIshKg9Pma6c= X-Received: from pjn12.prod.google.com ([2002:a17:90b:570c:b0:2ef:8055:93d9]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:17d2:b0:30a:214c:24c9 with SMTP id 98e67ed59e1d1-30e2e5d612dmr7495408a91.3.1747264778031; Wed, 14 May 2025 16:19:38 -0700 (PDT) Date: Wed, 14 May 2025 16:19:36 -0700 In-Reply-To: <20250425111352.GF1166@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250324173121.1275209-1-mizhang@google.com> <20250324173121.1275209-6-mizhang@google.com> <20250425111352.GF1166@noisy.programming.kicks-ass.net> Message-ID: Subject: Re: [PATCH v4 05/38] perf: Add generic exclude_guest support From: Sean Christopherson To: Peter Zijlstra Cc: Mingwei Zhang , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Paolo Bonzini , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Liang@google.com, Kan , "H. Peter Anvin" , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, Yongwei Ma , Xiong Zhang , Dapeng Mi , Jim Mattson , Sandipan Das , Zide Chen , Eranian Stephane , Shukla Manali , Nikunj Dadhania Content-Type: text/plain; charset="us-ascii" On Fri, Apr 25, 2025, Peter Zijlstra wrote: > On Mon, Mar 24, 2025 at 05:30:45PM +0000, Mingwei Zhang wrote: > > > @@ -6040,6 +6041,71 @@ void perf_put_mediated_pmu(void) > > } > > EXPORT_SYMBOL_GPL(perf_put_mediated_pmu); > > > > +static inline void perf_host_exit(struct perf_cpu_context *cpuctx) > > +{ > > + perf_ctx_disable(&cpuctx->ctx, EVENT_GUEST); > > + ctx_sched_out(&cpuctx->ctx, NULL, EVENT_GUEST); > > + perf_ctx_enable(&cpuctx->ctx, EVENT_GUEST); > > + if (cpuctx->task_ctx) { > > + perf_ctx_disable(cpuctx->task_ctx, EVENT_GUEST); > > + task_ctx_sched_out(cpuctx->task_ctx, NULL, EVENT_GUEST); > > + perf_ctx_enable(cpuctx->task_ctx, EVENT_GUEST); > > + } > > +} > > + > > +/* When entering a guest, schedule out all exclude_guest events. */ > > +void perf_guest_enter(void) > > +{ > > + struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); > > + > > + lockdep_assert_irqs_disabled(); > > + > > + perf_ctx_lock(cpuctx, cpuctx->task_ctx); > > + > > + if (WARN_ON_ONCE(__this_cpu_read(perf_in_guest))) > > + goto unlock; > > + > > + perf_host_exit(cpuctx); > > + > > + __this_cpu_write(perf_in_guest, true); > > + > > +unlock: > > + perf_ctx_unlock(cpuctx, cpuctx->task_ctx); > > +} > > +EXPORT_SYMBOL_GPL(perf_guest_enter); > > + > > +static inline void perf_host_enter(struct perf_cpu_context *cpuctx) > > +{ > > + perf_ctx_disable(&cpuctx->ctx, EVENT_GUEST); > > + if (cpuctx->task_ctx) > > + perf_ctx_disable(cpuctx->task_ctx, EVENT_GUEST); > > + > > + perf_event_sched_in(cpuctx, cpuctx->task_ctx, NULL, EVENT_GUEST); > > + > > + if (cpuctx->task_ctx) > > + perf_ctx_enable(cpuctx->task_ctx, EVENT_GUEST); > > + perf_ctx_enable(&cpuctx->ctx, EVENT_GUEST); > > +} > > + > > +void perf_guest_exit(void) > > +{ > > + struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); > > + > > + lockdep_assert_irqs_disabled(); > > + > > + perf_ctx_lock(cpuctx, cpuctx->task_ctx); > > + > > + if (WARN_ON_ONCE(!__this_cpu_read(perf_in_guest))) > > + goto unlock; > > + > > + perf_host_enter(cpuctx); > > + > > + __this_cpu_write(perf_in_guest, false); > > +unlock: > > + perf_ctx_unlock(cpuctx, cpuctx->task_ctx); > > +} > > +EXPORT_SYMBOL_GPL(perf_guest_exit); > > This naming is confusing on purpose? Pick either guest/host and stick > with it. +1. I also think the inner perf_host_{enter,exit}() helpers are superflous. These flows After a bit of hacking, and with a few spoilers, this is what I ended up with (not anywhere near fully tested). I like following KVM's kvm_xxx_{load,put}() nomenclature to tie everything together, so I went with "guest" instead of "host" even though the majority of work being down is to shedule out/in host context. /* When loading a guest's mediated PMU, schedule out all exclude_guest events. */ void perf_load_guest_context(unsigned long data) { struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); lockdep_assert_irqs_disabled(); perf_ctx_lock(cpuctx, cpuctx->task_ctx); if (WARN_ON_ONCE(__this_cpu_read(guest_ctx_loaded))) goto unlock; perf_ctx_disable(&cpuctx->ctx, EVENT_GUEST); ctx_sched_out(&cpuctx->ctx, NULL, EVENT_GUEST); perf_ctx_enable(&cpuctx->ctx, EVENT_GUEST); if (cpuctx->task_ctx) { perf_ctx_disable(cpuctx->task_ctx, EVENT_GUEST); task_ctx_sched_out(cpuctx->task_ctx, NULL, EVENT_GUEST); perf_ctx_enable(cpuctx->task_ctx, EVENT_GUEST); } arch_perf_load_guest_context(data); __this_cpu_write(guest_ctx_loaded, true); unlock: perf_ctx_unlock(cpuctx, cpuctx->task_ctx); } EXPORT_SYMBOL_GPL(perf_load_guest_context); void perf_put_guest_context(void) { struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); lockdep_assert_irqs_disabled(); perf_ctx_lock(cpuctx, cpuctx->task_ctx); if (WARN_ON_ONCE(!__this_cpu_read(guest_ctx_loaded))) goto unlock; arch_perf_put_guest_context(); perf_ctx_disable(&cpuctx->ctx, EVENT_GUEST); if (cpuctx->task_ctx) perf_ctx_disable(cpuctx->task_ctx, EVENT_GUEST); perf_event_sched_in(cpuctx, cpuctx->task_ctx, NULL, EVENT_GUEST); if (cpuctx->task_ctx) perf_ctx_enable(cpuctx->task_ctx, EVENT_GUEST); perf_ctx_enable(&cpuctx->ctx, EVENT_GUEST); __this_cpu_write(guest_ctx_loaded, false); unlock: perf_ctx_unlock(cpuctx, cpuctx->task_ctx); } EXPORT_SYMBOL_GPL(perf_put_guest_context);