From patchwork Fri Aug 27 00:57:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12461081 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36685C432BE for ; Fri, 27 Aug 2021 01:14:53 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EBC7E60ED3 for ; Fri, 27 Aug 2021 01:14:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org EBC7E60ED3 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Reply-To:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References :Mime-Version:Message-Id:In-Reply-To:Date:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=NkHF4kfIT69yiOVufsn1owS6bL3kWG/RcrAP5MOnONg=; b=wUYP70KQspj3gI 63rR3c7GuRgopaAv2xjgka/+NPXJVBUvcR13xbaXs+O8W15aNOdCghDldQqEs2WnLzcvF8Q3R4EcQ iObdoySK+2ci4U0BfmRoOio+R0af9/7GC7oycE2hZll4BywFK7J4VhF/1oL/IGnmcSf+UDVGieTYK ZSr/yD0+OK8nh00aT5hUiDzZ2SDdX8UT2HOatbIORsCOq5eEu9N7HqFBPSbq/oJODJ92PvmSUpbhC o8l+i6pM0rOtoe5Fa/epKej8GXQRkApKtJYE2FIKqEH1TmbUKeXzMDt14xY4pCFYTDPBVvhWveqCg goOltPznUHzQbWD41BnQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mJQP2-00BGrN-47; Fri, 27 Aug 2021 01:11:29 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mJQC4-00BB9k-Nr for linux-arm-kernel@lists.infradead.org; Fri, 27 Aug 2021 00:58:06 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 83-20020a251956000000b0059948f541cbso4914841ybz.7 for ; Thu, 26 Aug 2021 17:58:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=5wQJhGi+ZyGOAOMPxE71g3TXDJHEcKM3NH5k2IjIokU=; b=OW6tj0qrfqLnmyAG7bwsLD1ivWUlSQB5/rmjy7WLw347oAMVCG3/Qk7isJlZCVSFFV W7vIlgUdwAlJx2kpTZ9A55Gu92o/DT6okHoyWj++aIqZDyTZVtHibehQi7O2j3MjMThX Eos5GY/kAJpzkfPs9z5NTcHHEpUm/XVAQpx4rHw8kLZUkO7HNuZJLNnlhhsXFJlEJkCy p6hhGZMhQP2aZtmOVXw5yZNFRx/aQuH9Vj4YrQddHKnPUiY2qaww7D6c/XyLTUmGdHj3 smqjImdUUuenQzBvxAy/5tpxtN+1AQqndlIihp99we7A59b3jtdrbJnHyTatubIFGc7+ +UOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=5wQJhGi+ZyGOAOMPxE71g3TXDJHEcKM3NH5k2IjIokU=; b=ss43ST3BCYqjVoiq/Kbhvm2Uk0gaIlqWtVbmUIwmnLt03JYaD2ztWfxgPFd+HDSNu9 LNBIU+KyLBSQQe+GZkuxwLJAiXP1nZ3RAewPsv+JUyujojPW2jzycBmSKXbqWJa0xAxR 5q9PuvAnCj1HoqEifwCSuCHism0HFllJV93eGXdm5C56XD8Sq0WJwWskjNEUnNh/fmCu IG7pL+HvnnImb0gyFx0c6w52Zac/NJHIQMPdc3eC8joPOgmKz3sYisYmEY2rF69fPydo MLNMyUYhyhd7lM0auYcHSpffK7Cq4pFLL6fcpVTJRAuhiD6R2nM3xKaPC6onivU0aXU8 x9JQ== X-Gm-Message-State: AOAM5333yoGjCcPaowUz9br4rCqvo9f9DImf7AigWlImhgDJfs286QTK UJB+YKxE2Cat0c4XbwF7hZ49GtfsYTA= X-Google-Smtp-Source: ABdhPJxNS9FKdHggEYyf3FZBaeUKCfIfMsPYcyI+FugrK+/XdFcM7Qj6lsB8B0S5DygYRKG5HNpbYwSWz/0= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:c16c:db05:96b2:1475]) (user=seanjc job=sendgmr) by 2002:a25:ea51:: with SMTP id o17mr1969948ybe.253.1630025883284; Thu, 26 Aug 2021 17:58:03 -0700 (PDT) Date: Thu, 26 Aug 2021 17:57:18 -0700 In-Reply-To: <20210827005718.585190-1-seanjc@google.com> Message-Id: <20210827005718.585190-16-seanjc@google.com> Mime-Version: 1.0 References: <20210827005718.585190-1-seanjc@google.com> X-Mailer: git-send-email 2.33.0.259.gc128427fd7-goog Subject: [PATCH 15/15] perf: KVM: Indicate "in guest" via NULL ->is_in_guest callback From: Sean Christopherson To: Will Deacon , Mark Rutland , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Catalin Marinas , Marc Zyngier , Guo Ren , Nick Hu , Greentime Hu , Vincent Chen , Paul Walmsley , Palmer Dabbelt , Albert Ou , Thomas Gleixner , Borislav Petkov , x86@kernel.org, Paolo Bonzini , Boris Ostrovsky , Juergen Gross Cc: Alexander Shishkin , Jiri Olsa , Namhyung Kim , James Morse , Alexandru Elisei , Suzuki K Poulose , "H. Peter Anvin" , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Stefano Stabellini , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-csky@vger.kernel.org, linux-riscv@lists.infradead.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org, Artem Kashkanov , Like Xu , Zhu Lingshan X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210826_175804_873021_3CD5005A X-CRM114-Status: GOOD ( 21.23 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Interpret a null ->is_in_guest callback as meaning "in guest" and use the new semantics in KVM, which currently returns 'true' unconditionally in its implementation of ->is_in_guest(). This avoids a retpoline on the indirect call for PMIs that arrive in a KVM guest, and also provides a handy excuse for a wrapper around retrieval of perf_get_guest_cbs, e.g. to reduce the probability of an errant direct read of perf_guest_cbs. Signed-off-by: Sean Christopherson --- arch/x86/events/core.c | 16 ++++++++-------- arch/x86/events/intel/core.c | 5 ++--- include/linux/perf_event.h | 17 +++++++++++++++++ virt/kvm/kvm_main.c | 9 ++------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 34155a52e498..b60c339ae06b 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -2761,11 +2761,11 @@ static bool perf_hw_regs(struct pt_regs *regs) void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { - struct perf_guest_info_callbacks *guest_cbs = this_cpu_read(perf_guest_cbs); + struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs(); struct unwind_state state; unsigned long addr; - if (guest_cbs && guest_cbs->is_in_guest()) { + if (guest_cbs) { /* TODO: We don't support guest os callchain now */ return; } @@ -2865,11 +2865,11 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *ent void perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { - struct perf_guest_info_callbacks *guest_cbs = this_cpu_read(perf_guest_cbs); + struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs(); struct stack_frame frame; const struct stack_frame __user *fp; - if (guest_cbs && guest_cbs->is_in_guest()) { + if (guest_cbs) { /* TODO: We don't support guest os callchain now */ return; } @@ -2946,9 +2946,9 @@ static unsigned long code_segment_base(struct pt_regs *regs) unsigned long perf_instruction_pointer(struct pt_regs *regs) { - struct perf_guest_info_callbacks *guest_cbs = this_cpu_read(perf_guest_cbs); + struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs(); - if (guest_cbs && guest_cbs->is_in_guest()) + if (guest_cbs) return guest_cbs->get_guest_ip(); return regs->ip + code_segment_base(regs); @@ -2956,10 +2956,10 @@ unsigned long perf_instruction_pointer(struct pt_regs *regs) unsigned long perf_misc_flags(struct pt_regs *regs) { - struct perf_guest_info_callbacks *guest_cbs = this_cpu_read(perf_guest_cbs); + struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs(); int misc = 0; - if (guest_cbs && guest_cbs->is_in_guest()) { + if (guest_cbs) { if (guest_cbs->is_user_mode()) misc |= PERF_RECORD_MISC_GUEST_USER; else diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 96001962c24d..9a8c18b51a96 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -2853,9 +2853,8 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status) */ if (__test_and_clear_bit(GLOBAL_STATUS_TRACE_TOPAPMI_BIT, (unsigned long *)&status)) { handled++; - guest_cbs = this_cpu_read(perf_guest_cbs); - if (unlikely(guest_cbs && guest_cbs->is_in_guest() && - guest_cbs->handle_intel_pt_intr)) + guest_cbs = perf_get_guest_cbs(); + if (unlikely(guest_cbs && guest_cbs->handle_intel_pt_intr)) guest_cbs->handle_intel_pt_intr(); else intel_pt_interrupt(); diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index db701409a62f..6e3a10784d24 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1241,6 +1241,23 @@ DECLARE_PER_CPU(struct perf_guest_info_callbacks *, perf_guest_cbs); extern void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs); extern void perf_unregister_guest_info_callbacks(void); extern void perf_register_guest_info_callbacks_all_cpus(struct perf_guest_info_callbacks *cbs); +/* + * Returns guest callbacks for the current CPU if callbacks are registered and + * the PMI fired while a guest was running, otherwise returns NULL. + */ +static inline struct perf_guest_info_callbacks *perf_get_guest_cbs(void) +{ + struct perf_guest_info_callbacks *guest_cbs = this_cpu_read(perf_guest_cbs); + + /* + * Implementing is_in_guest is optional if the callbacks are registered + * only when "in guest". + */ + if (guest_cbs && (!guest_cbs->is_in_guest || guest_cbs->is_in_guest())) + return guest_cbs; + + return NULL; +} #endif /* CONFIG_HAVE_GUEST_PERF_EVENTS */ extern void perf_event_exec(void); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 1bcc3eab510b..fa83d3846785 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5461,12 +5461,6 @@ struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void) } #ifdef __KVM_WANT_PERF_CALLBACKS -static int kvm_is_in_guest(void) -{ - /* Registration of KVM's callback signifies "in guest". */ - return true; -} - static int kvm_is_user_mode(void) { struct kvm_vcpu *vcpu = kvm_get_running_vcpu(); @@ -5488,7 +5482,8 @@ static unsigned long kvm_get_guest_ip(void) } static struct perf_guest_info_callbacks kvm_guest_cbs = { - .is_in_guest = kvm_is_in_guest, + /* Registration of KVM's callback signifies "in guest". */ + .is_in_guest = NULL, .is_user_mode = kvm_is_user_mode, .get_guest_ip = kvm_get_guest_ip, .handle_intel_pt_intr = NULL,