Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3571055iob; Tue, 17 May 2022 02:59:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzw3qw6s9EJhzh2jtuXbcLUNvEm3CLMGUoR3eVhDm4cEE4kWl+ZPWUtcUCDt902DtoSiqQw X-Received: by 2002:a17:902:bf4c:b0:15c:3d1b:8a47 with SMTP id u12-20020a170902bf4c00b0015c3d1b8a47mr22073256pls.118.1652781578363; Tue, 17 May 2022 02:59:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652781578; cv=none; d=google.com; s=arc-20160816; b=TKzSA5kI0A93QVKir0cZDCVowWdHGbBbd1Rp58nWHSrk5NfcLVXLXdbtDpM7hukvtE +OEcuzla+Sfn49o1moVWedX0xQgF4i1PTyZy8qfXotqMQ+ZIaQ65MWidojZCTeC1rl5Z ny17F4iled3PWBdsejxTRejli6bHOoEDGyOWs6tz5BiyWBZ3tJlrVX546uLBcQ99xI+K tbrHkpbYuMWyC16h96cgsKtT/mNZHsgjztWDCUfiJ5OAFL573Ea2PbBky/t+WYURpKLI DEpvZlSB8eaCh0uukQVT2p996Y6WBAOwx4NjjLBg2qvvemk2DYd23qvKQuSRiWOA2jwY +htQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=PWhv8SUJ7Zi/KQsr7OOR1d8BfvT5pK75grSbAtZ3mLs=; b=R+mF53/+S+OpBQvM04VBNr+j00V9c7EPfoxqoPcxU72N7gJXoz770m0qaNinS9EzMw 30x8TQxMSV4lrpNyM6sjIhRRsMwor5wFUO+la9Oubkr/DPaTjn9kEwhPM2XunXk1fawW rnuJcfLFOpyGaagA5FT8lLJekZKFuhoKVmzyBWaNSPLWgiBqf+UuiWmKQg0yxn8EWzvN uKtQBYVqAM4zyD5pFM83uQ1icF9pST9UUy7ZKV3XUTmY7XkFNnwNv0weFXGD7x+ybxZk Ax8rWn5nyfo/mDA1rQ9iAdhHL5RRt+6Jx5rNvq98yw6hioZEvxLNWoGmrHlBN5ZnvfX0 TefQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nu10-20020a17090b1b0a00b001dce4a1a83esi2844366pjb.177.2022.05.17.02.59.25; Tue, 17 May 2022 02:59:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239675AbiEQDN3 (ORCPT + 99 others); Mon, 16 May 2022 23:13:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239648AbiEQDNV (ORCPT ); Mon, 16 May 2022 23:13:21 -0400 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B5E440A13; Mon, 16 May 2022 20:13:20 -0700 (PDT) Received: by mail-oi1-f176.google.com with SMTP id e189so20910737oia.8; Mon, 16 May 2022 20:13:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PWhv8SUJ7Zi/KQsr7OOR1d8BfvT5pK75grSbAtZ3mLs=; b=l0nfXAAVQbga+lEfnM7gmAl2xbS58DEieZl9riWRs02jOjfjdZ9/o3t+JCx//KnStW DmG73SRzvvxnmvh4oBZ48nxHeCHQEFUUVsU7mqWQgial8ELwN+41LsqbXQxLl8o5ekyt jI9yF4999oThvsWikeEEY+3H3C81hMDtXjOSbsTnR3h7feSB11ERI6pKS7P2UmnbdBcn VgqBxoLPcgRW15cFtDnvY9Yt3j8MwPdxdwi2Bqw9ynyNBLqyTDCMLwtMunZ5SxhilMsu ziThsoytp7G7LQdaLKtIfXxrrgVPcFKDQ5ljFi8cnj2dyPNVwTaIEya+sDJUgsyCXV7u sSPQ== X-Gm-Message-State: AOAM5304WRnHaz+yLl/CTaGMarLq/LTWwx18S1BvIs/D22dTgEK56fEC hkvxPA9lcFjZ2FDwYYxuCviQJQW9ACERv83Pt8U= X-Received: by 2002:a05:6808:2218:b0:326:bd8c:d044 with SMTP id bd24-20020a056808221800b00326bd8cd044mr9426742oib.92.1652757199897; Mon, 16 May 2022 20:13:19 -0700 (PDT) MIME-Version: 1.0 References: <20220513090237.10444-1-adrian.hunter@intel.com> <20220513090237.10444-4-adrian.hunter@intel.com> In-Reply-To: <20220513090237.10444-4-adrian.hunter@intel.com> From: Namhyung Kim Date: Mon, 16 May 2022 20:13:08 -0700 Message-ID: Subject: Re: [PATCH 3/6] perf tools: Add guest_code support To: Adrian Hunter Cc: Arnaldo Carvalho de Melo , Jiri Olsa , Ian Rogers , Leo Yan , linux-kernel , linux-perf-users , KVM Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Adrian, On Fri, May 13, 2022 at 2:03 AM Adrian Hunter wrote: > > A common case for KVM test programs is that the guest object code can be > found in the hypervisor process (i.e. the test program running on the > host). To support that, copy the host thread's maps to the guest thread's > maps. Note, we do not discover the guest until we encounter a guest event, > which works well because it is not until then that we know that the host > thread's maps have been set up. > > Typically the main function for the guest object code is called > "guest_code", hence the name chosen for this feature. Ok, so that's just a convention and there's no hard-coded support for the "guest_code" function in this code, right? > > This is primarily aimed at supporting Intel PT, or similar, where trace > data can be recorded for a guest. Refer to the final patch in this series > "perf intel-pt: Add guest_code support" for an example. > > Signed-off-by: Adrian Hunter > --- > tools/perf/util/event.c | 7 +++- > tools/perf/util/machine.c | 70 +++++++++++++++++++++++++++++++++++ > tools/perf/util/machine.h | 2 + > tools/perf/util/session.c | 7 ++++ > tools/perf/util/symbol_conf.h | 3 +- > 5 files changed, 86 insertions(+), 3 deletions(-) > > diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c > index 6439c888ae38..0476bb3a4188 100644 > --- a/tools/perf/util/event.c > +++ b/tools/perf/util/event.c > @@ -683,9 +683,12 @@ static bool check_address_range(struct intlist *addr_list, int addr_range, > int machine__resolve(struct machine *machine, struct addr_location *al, > struct perf_sample *sample) > { > - struct thread *thread = machine__findnew_thread(machine, sample->pid, > - sample->tid); > + struct thread *thread; > > + if (symbol_conf.guest_code && !machine__is_host(machine)) > + thread = machine__findnew_guest_code(machine, sample->pid); > + else > + thread = machine__findnew_thread(machine, sample->pid, sample->tid); > if (thread == NULL) > return -1; > > diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c > index e67b5a7670f3..ae2e1fb422e2 100644 > --- a/tools/perf/util/machine.c > +++ b/tools/perf/util/machine.c > @@ -392,6 +392,76 @@ struct machine *machines__find_guest(struct machines *machines, pid_t pid) > return machine; > } > > +/* > + * A common case for KVM test programs is that the guest object code can be > + * found in the hypervisor process (i.e. the test program running on the host). > + * To support that, copy the host thread's maps to the guest thread's maps. > + * Note, we do not discover the guest until we encounter a guest event, > + * which works well because it is not until then that we know that the host > + * thread's maps have been set up. > + */ > +static struct thread *findnew_guest_code(struct machine *machine, But this function returns a thread and IIUC that's the task which does the host to guest transition. Then why not calling it just findnew__hypervisor() ? Thanks, Namhyung > + struct machine *host_machine, > + pid_t pid) > +{ > + struct thread *host_thread; > + struct thread *thread; > + int err; > + > + if (!machine) > + return NULL; > + > + thread = machine__findnew_thread(machine, -1, pid); > + if (!thread) > + return NULL; > + > + /* Assume maps are set up if there are any */ > + if (thread->maps->nr_maps) > + return thread; > + > + host_thread = machine__find_thread(host_machine, -1, pid); > + if (!host_thread) > + goto out_err; > + > + thread__set_guest_comm(thread, pid); > + > + /* > + * Guest code can be found in hypervisor process at the same address > + * so copy host maps. > + */ > + err = maps__clone(thread, host_thread->maps); > + thread__put(host_thread); > + if (err) > + goto out_err; > + > + return thread; > + > +out_err: > + thread__zput(thread); > + return NULL; > +} > +