From patchwork Thu Oct 12 03:51:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 151713 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp965520vqb; Wed, 11 Oct 2023 20:55:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFzfh2B1vX0ZlANZYb271pOzb3w9qcJqBbZHZZcHQAa5Q4Xyn+203tsmVRto/0ZMWB1WcAp X-Received: by 2002:a05:6808:6408:b0:3af:6453:2d83 with SMTP id fg8-20020a056808640800b003af64532d83mr21570153oib.2.1697082947836; Wed, 11 Oct 2023 20:55:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697082947; cv=none; d=google.com; s=arc-20160816; b=wLfxLIL0tW62fMaT1ZRe+/BCPtTs7Pa0raiBeCqOfuelSVzc2HgICxYCM3ZGBc29Nk OTmOShmYvRkc6dcdsBZyyIcv4ZVZt5oPSo7kL7txkh8x5EMJAuSsOBb0EUMqaeNZraLB Bnt0lXLmVidbSjDl/4zv51ypJxuApW+jfKrtmFhkMRRZl5C1oz0wIpfdnTsETQqPo3RN vAiKteEps2KUKEy8Vzno7G5hGrvzNYarFXGHylOvM5B9pCRt62zDU1c/UG9eL7lMWLz0 JAmySpqet6fi8mnmon9snkYQz6kze0+IrKuH/byaRreivn7SyIewUsxcpABfBWv3GfhT bKiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=5VcGuty2WfdJlpv/evrY/Aeps8iBoToI2rEN24hSWRo=; fh=7Id7wL2M+oCJXD4imOzx4OKioxBXSOZqypDprm7aO4E=; b=rCeqRLIzIKw8VifeWQ5MPtENr9pfT9CLdidewLDc5urx09Dt9bw2eiWuu4DpYjvLSM V61uFVy3Y3lmxqUct35A/ODbwMgH/nY8SQG1gp7whanSR55cVpkJ0naDSOp/M362+0V+ g0Y40IZrFmA1yCoEaDPudEt0asn1rj2F7iJy0tX4EOnvOILFEYLKK5aQxciHQ2pRRDJN WH6mpKgfYNGWPVK8gV+Dtg6+CqrcbGonn3IYep2b9FnaBFLRvAdQg6S1oiaKxlx3zDrl pBpIcprknpQ/RYtq1w6MPo4+Bt3WQbOWW3vQObHZdg/Nrm9Tbw728ikWwaGQWBWmwlCY R4CA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=IUU+5ywP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id x190-20020a6363c7000000b00578ca217736si1228196pgb.719.2023.10.11.20.55.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 20:55:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=IUU+5ywP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id A02B78042C21; Wed, 11 Oct 2023 20:55:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377552AbjJLDzf (ORCPT + 18 others); Wed, 11 Oct 2023 23:55:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377371AbjJLDyA (ORCPT ); Wed, 11 Oct 2023 23:54:00 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0AC2F3; Wed, 11 Oct 2023 20:52:10 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1c77449a6daso4675125ad.0; Wed, 11 Oct 2023 20:52:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697082730; x=1697687530; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=5VcGuty2WfdJlpv/evrY/Aeps8iBoToI2rEN24hSWRo=; b=IUU+5ywP6Pz6bPSwet+E9QBRdVO63wof5vkhnFIlGS28oD4G+ojCgBFPw18YqR2/Hy 4n1g2MLsyiepo4zChhzxXFwxMUFj++s1VCiiszVKoxf/p7rQ2PXe6uYZjZfGirTotglH 0mQnQtKxJRyxmddk12Y0YNK4jgHDsnfKWCj6K+VAyKtv7Tb75fK/+ZS+tQrZ5TblfWg3 tdPKkquZCE2/2YLuqtopYUbX6JOLIcqfDmIQNFhxjtLMgfVCIA5ut+/cWH8HC8PyX4GT aIIgVpSBnGNMEpKM9hTgXbMtUtomOaJ3KfVhYisH83nS0ODhb9GJsOx9dCQgidgk0zQ6 EikQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697082730; x=1697687530; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5VcGuty2WfdJlpv/evrY/Aeps8iBoToI2rEN24hSWRo=; b=A2ujDGqJ/SaW42eIRB0PmNLMNmMsoDt7q8tzbMn1sY3kAr9a/nktEnDnOrTGz3l+H8 zDUJqfzzh8my2SeIA62MEQN2nzon7ssqneo2twwMNO+oA0RTMK10dNaDJwtdGS7h8Qny 7XIcn91pS3eSiNWOhV0tnlbqbrKpj1HF0CftHMuQlgkOnwBJeJGFf77l8q9AzX9pqicH SFXM60JSJ9XIy2ls7Expn1/I+8/L0I/R16aWbbMvt5VKE7nH3xB/Ndhh6DeIh+69RAf7 Wt+kNGjuP17hgrfmYEVoepPqyvSOqv1ozjpdlQapWjW7DJPHy1sPqCWum88BlG98aD7J KbYw== X-Gm-Message-State: AOJu0YwfXvdT6qePP1oVp9l9BWzWEglIJ6uAvLHPNOuXUItoalLdJ4En HEE58mi8jXReFSvM5qJ+RIo= X-Received: by 2002:a17:902:d30d:b0:1c5:ecff:1bc7 with SMTP id b13-20020a170902d30d00b001c5ecff1bc7mr23183988plc.4.1697082729721; Wed, 11 Oct 2023 20:52:09 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:42e0:b1b9:d490:2f5e:be06]) by smtp.gmail.com with ESMTPSA id w8-20020a170902d70800b001bc18e579aesm711374ply.101.2023.10.11.20.52.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 20:52:09 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa , Peter Zijlstra Cc: Ian Rogers , Adrian Hunter , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Linus Torvalds , Stephane Eranian , Masami Hiramatsu , linux-toolchains@vger.kernel.org, linux-trace-devel@vger.kernel.org Subject: [PATCH 42/48] perf annotate-data: Handle call instructions Date: Wed, 11 Oct 2023 20:51:05 -0700 Message-ID: <20231012035111.676789-43-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog In-Reply-To: <20231012035111.676789-1-namhyung@kernel.org> References: <20231012035111.676789-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 11 Oct 2023 20:55:45 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779520449332704052 X-GMAIL-MSGID: 1779520449332704052 When updating instruction states, the call instruction should play a role since it can change the register states. For simplicity, mark some registers as scratch registers (should be arch-dependent), and invalidate them all after a function call. If the function returns something, the designated register (ret_reg) will have the type info. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate-data.c | 45 +++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-data.c index 37135698a5c8..f3f85cb9ac00 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -40,10 +40,14 @@ struct annotated_data_type stackop_type = { /* Data type collection debug statistics */ struct annotated_data_stat ann_data_stat; -/* Type information in a register, valid when ok is true */ +/* + * Type information in a register, valid when ok is true. + * The scratch registers are invalidated after a function call. + */ struct type_state_reg { Dwarf_Die type; bool ok; + bool scratch; }; /* Type information in a stack location, dynamically allocated */ @@ -67,6 +71,7 @@ struct type_state_stack { struct type_state { struct type_state_reg regs[TYPE_STATE_MAX_REGS]; struct list_head stack_vars; + int ret_reg; }; static bool has_reg_type(struct type_state *state, int reg) @@ -74,10 +79,23 @@ static bool has_reg_type(struct type_state *state, int reg) return (unsigned)reg < ARRAY_SIZE(state->regs); } -void init_type_state(struct type_state *state, struct arch *arch __maybe_unused) +void init_type_state(struct type_state *state, struct arch *arch) { memset(state, 0, sizeof(*state)); INIT_LIST_HEAD(&state->stack_vars); + + if (arch__is(arch, "x86")) { + state->regs[0].scratch = true; + state->regs[1].scratch = true; + state->regs[2].scratch = true; + state->regs[4].scratch = true; + state->regs[5].scratch = true; + state->regs[8].scratch = true; + state->regs[9].scratch = true; + state->regs[10].scratch = true; + state->regs[11].scratch = true; + state->ret_reg = 0; + } } void exit_type_state(struct type_state *state) @@ -434,6 +452,29 @@ void update_insn_state(struct type_state *state, struct data_loc_info *dloc, int fbreg = dloc->fbreg; int fboff = 0; + if (ins__is_call(&dl->ins)) { + Dwarf_Die func_die; + + /* __fentry__ will preserve all registers */ + if (dl->ops.target.sym && + !strcmp(dl->ops.target.sym->name, "__fentry__")) + return; + + /* Otherwise invalidate scratch registers after call */ + for (unsigned i = 0; i < ARRAY_SIZE(state->regs); i++) { + if (state->regs[i].scratch) + state->regs[i].ok = false; + } + + /* Update register with the return type (if any) */ + if (die_find_realfunc(cu_die, dl->ops.target.addr, &func_die) && + die_get_real_type(&func_die, &type_die)) { + state->regs[state->ret_reg].type = type_die; + state->regs[state->ret_reg].ok = true; + } + return; + } + /* FIXME: remove x86 specific code and handle more instructions like LEA */ if (!strstr(dl->ins.name, "mov")) return;