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 C10A11FBE87 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=1747264780; cv=none; b=NsHRJ+92KqDs/o4OEB9aVVRabEnPhbWk5mjs8hao6Ez7NbZYbln2TIY8rh9+lLsgK8ZnLlcs7BJIzhiuJuoCo+gecXi3cTwSIMD5U6+JlsYf5sz9ExVlP1rVvhwbqwpzFzLvFYwZD37m3rBKDDX3XCAIFV9BbK1uxRvbwYqJZrs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747264780; c=relaxed/simple; bh=cNlVPYYnsQtqPjb2poMyKHUJYGJOPlxbA0jgyo6XDX8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SwcKCGuPwcmrQqXYZZPdF+cNnlTUy8xEye/00RsO6LxisPNqEoovXx8i46FVM+ItdCdMQTHnKnHvVKZS1VH3qET1uyfKj47CNwKAXpNmEAgLO9HcUeiUkP2y1weyNDoW1KibBu1LkuovUqt1dqpmu52j9Iy3n5TaWMDtbA6eVpA= 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-30df7c1287fso284864a91.1 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=Q4khEdv6gacFjc2QnkHFkwBFsqt1XbSr5wsupJ0sFKDXcGK8W6V6Y7Dy/f2GP9gFdp f7WfXcp+Q8vBytxsTp8oaUNUw2KdC8rSNkRt+w6/EqnITeH2M7IrYl+tPU+92yXlaCn9 X691BLsPRGLAWjZ1m40g94bkueLt1NPgR10KWsdSZL4dJ8miGMeuet6Ab20G7tZvFqqv e4zNicx50xe5TQSYcRP1ebzyGoz5Y4zoxF9T5P1oF81OUE/VxzrJJl2c+Ay94wxmUmDk Ke2cQl+6w82nDX933eVS3Fh7MK9LpJEMFIdC//KMa9EflUqkyOEcUI0ablOH9uhEAeJB Ki9g== X-Forwarded-Encrypted: i=1; AJvYcCUQ7UdFYz9Q3KWiD1+Vk1+5z3jd0yWnnVrEXlvxkHPATrOCLQ4AW8ncv3wCE9Dh04gN0Hgw2Icz4eMZFFc=@vger.kernel.org X-Gm-Message-State: AOJu0Yxv90NG0XDuh5RjVPy3rqjsd2HV5R3weXS2/FjdWr0NxQ5XLI8B TtE6jJ5qcMLTtkfHQH4Wn4R/BDqRxZlUBRY8OREMqJdyihQ96TguX9cHs8K/tRbTEEnENnH9S/i nwg== 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: linux-kernel@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);