From patchwork Fri Nov 10 00:00:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 163685 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp789224vqs; Thu, 9 Nov 2023 16:07:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IHZemMx+XGvnAwCXR6ltuow9vzebb/ANrqS813uKBx/2MyiS8KJAnaeC+yA0C+IyryaBaOC X-Received: by 2002:a05:6830:16ca:b0:6d3:165d:f19c with SMTP id l10-20020a05683016ca00b006d3165df19cmr6768041otr.11.1699574860551; Thu, 09 Nov 2023 16:07:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699574860; cv=none; d=google.com; s=arc-20160816; b=QF2ZFFmy2IXj3pUEQqCOcveLzo9DOtoMN5IFiXsuXj0opIDPpAnY5H7nRfsBNclZ/9 jSlZP6WHhdWBnl+sEQTVCLHWPLHaOymgYQI5SMm4BtkQFC4YWkZOWMTGOqVOeXNrWTJf Ks/eQP41T57LAe/2p4TqEgKZkd054IFjYndBCTUfcU5L/QSk5IcvW6HLr9PYFmZ0g9+a hBDy/HBTh/8RfxioiZ3VddpN1C1uqW2m2DafShfGeTpvRagsMr1wot79L7IbgnV6TrrH rOudXyZzH1rsBikmQcAaXtWm88nUCtuQ1pHBsVV7ExiL63VyzepYY7ZEOe2fL1IjgUuO HDeQ== 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=PPYCIl0SPFtUtXhBJ2STPHUgQ84W10L+ZFD5fhYt5l4=; fh=K//si1Hi/RYC7s39rVvmvh0YbZGmBrdWWHQg3cPF7Xc=; b=IxazLmSxoBab/uKPAkjyTLZx7DoBycqR/b8CYaTMRFoLN79Hb9TL673jQv4s9WhUgc 1R9rut+ESEQ3hrHRZPmsw9GGkwX/ji9N9eUm4egxnelvrwltf03ER8WTwcCISRrjeCX4 cGglYJOfGEGj3Tg81o2XO5Zx0ybxe0SIuWr9fRkZCBvMFsBjrZoN+sXLfb4nXETr+Ugn nrdlaREmYKUp9aVewHorAFM02EO2LepuzX6aDjYm6VoiyqGLuNwGF3USHKGnv03Y+c0C 4wPIAM60WmVIqmzPVpTSvP7Fepv4Uy/0DsYJUJSuMWFoGycbXj3ah+iasW65/rlmsgzA AP2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=k48OsoUf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id q186-20020a4a4bc3000000b0057b9d1a44c6si6149983ooa.44.2023.11.09.16.07.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:07:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=k48OsoUf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 3EEA9809EC9B; Thu, 9 Nov 2023 16:06:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345807AbjKJAEn (ORCPT + 30 others); Thu, 9 Nov 2023 19:04:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345497AbjKJADz (ORCPT ); Thu, 9 Nov 2023 19:03:55 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5F421FFD; Thu, 9 Nov 2023 16:01:32 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-2809b4d648bso1369689a91.2; Thu, 09 Nov 2023 16:01:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699574492; x=1700179292; 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=PPYCIl0SPFtUtXhBJ2STPHUgQ84W10L+ZFD5fhYt5l4=; b=k48OsoUfHytUkMVPN/zc+YpSz3fz4LzjI0/hcjSRAZdO+g1yF9MljGrgvj51CcUC7c DMtmMkC8FAtwn/e0pwGUOv5fqDZND24rUBLe24OD6CymI424G8rJ1+Z1J2SYaD9JfErR Z4O/wGZCmJO0dLA+iL3Cw4wl//wY9vh/DaM+e8uiZ8HeNn6dUMH+qINH7XOOH5ZCVpJp MMeVk8mCjtBEwufm4wsGD9HFUMwUp0DsqLTVMz6kVmxuo5h/2h2KWlkTJE4XMNPX1L4V AjmXFpgoTL1S7lpLv4HiuTqQPjIcHLeHTKbwzVoaLOVsEhmROggWuZcpq3SQpi1fGESU yKUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699574492; x=1700179292; 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=PPYCIl0SPFtUtXhBJ2STPHUgQ84W10L+ZFD5fhYt5l4=; b=pufvxUGNlVh9Vy30u7mczicFAQIrxY0bskCzoenYwQIi1MhDoVsZKnDLhI4YN15l/9 JkrP8IdgVtuloPnJQmQ9x5lxVGXiF1is0M/wx/VuFNjdmgU7FhutFSnMQ0UFV+X9hWBj RkZpYS2hzMiAluLwCvIniznEkoZKMVJ9rw4GO4NevzTcP+bt6d+ZYjVe0arHng62SvPg sKnkojOQ48C17ykVYDv2gtsCuY6+zCNvSRvm+wpTUbYQYFHXGWjHeUp1yrEIcMTx3sDP bmN+jgsvxU+xrBYpghurtHcW9i9/lOA9VlgyG+4OppzyTancuNsGxNLNt0EdukGoQXQ0 vl8g== X-Gm-Message-State: AOJu0Yy7O9ANpqz6P92527ec0aRMorje2xKadxSK5fnqmb+FMBXlOSu4 FmYht/w4kXfVDdIGrXaYukI= X-Received: by 2002:a17:90b:1e04:b0:280:62a7:3743 with SMTP id pg4-20020a17090b1e0400b0028062a73743mr3412008pjb.30.1699574491830; Thu, 09 Nov 2023 16:01:31 -0800 (PST) Received: from bangji.corp.google.com ([2620:15c:2c0:5:d45c:ae16:d959:a5f1]) by smtp.gmail.com with ESMTPSA id k32-20020a17090a4ca300b002635db431a0sm371312pjh.45.2023.11.09.16.01.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:01:31 -0800 (PST) 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 , Andi Kleen , linux-trace-devel@vger.kernel.org, linux-toolchains@vger.kernel.org Subject: [PATCH 49/52] perf annotate-data: Handle this-cpu variables in kernel Date: Thu, 9 Nov 2023 16:00:08 -0800 Message-ID: <20231110000012.3538610-50-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog In-Reply-To: <20231110000012.3538610-1-namhyung@kernel.org> References: <20231110000012.3538610-1-namhyung@kernel.org> MIME-Version: 1.0 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 (agentk.vger.email [0.0.0.0]); Thu, 09 Nov 2023 16:06:09 -0800 (PST) X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782133409079429200 X-GMAIL-MSGID: 1782133409079429200 On x86, the kernel gets the current task using the current macro like below: #define current get_current() static __always_inline struct task_struct *get_current(void) { return this_cpu_read_stable(pcpu_hot.current_task); } So it returns the current_task field of struct pcpu_hot which is the first member. On my build, it's located at 0x32940. $ nm vmlinux | grep pcpu_hot 0000000000032940 D pcpu_hot And the current macro generates the instructions like below: mov %gs:0x32940, %rcx So the %gs segment register points to the beginning of the per-cpu region of this cpu and it points the variable with a constant. Let's update the instruction location info to have a segment register and handle %gs in kernel to look up a global variable. The new get_percpu_var_info() helper is to get information about the variable. Pretend it as a global variable by changing the register number to DWARF_REG_PC. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate.c | 31 +++++++++++++++++++++++++++++++ tools/perf/util/annotate.h | 4 ++++ 2 files changed, 35 insertions(+) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index a9075af10d24..9b72eae2400c 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -3814,6 +3814,27 @@ void get_global_var_info(struct thread *thread, struct map_symbol *ms, u64 ip, addr_location__exit(&al); } +void get_percpu_var_info(struct thread *thread, struct map_symbol *ms, + u8 cpumode, u64 var_addr, const char **var_name, + int *poffset) +{ + struct addr_location al; + struct symbol *var; + u64 map_addr; + + /* Kernel symbols might be relocated */ + map_addr = var_addr + map__reloc(ms->map); + + addr_location__init(&al); + var = thread__find_symbol_fb(thread, cpumode, map_addr, &al); + if (var) { + *var_name = var->name; + /* Calculate type offset from the start of variable */ + *poffset = map_addr - map__unmap_ip(al.map, var->start); + } + addr_location__exit(&al); +} + /** * hist_entry__get_data_type - find data type for given hist entry * @he: hist entry @@ -3905,6 +3926,16 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) &dloc.type_offset); } + /* This CPU access in kernel - pretend PC-relative addressing */ + if (op_loc->reg1 < 0 && ms->map->dso->kernel && + arch__is(arch, "x86") && op_loc->segment == INSN_SEG_X86_GS) { + dloc.var_addr = op_loc->offset; + get_percpu_var_info(he->thread, ms, he->cpumode, + dloc.var_addr, &dloc.var_name, + &dloc.type_offset); + op_loc->reg1 = DWARF_REG_PC; + } + mem_type = find_data_type(&dloc); if (mem_type) istat->good++; diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 21a0947ed5e9..c3cc0cba10b7 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -519,6 +519,10 @@ void get_global_var_info(struct thread *thread, struct map_symbol *ms, u64 ip, struct disasm_line *dl, u8 cpumode, u64 *var_addr, const char **var_name, int *poffset); +void get_percpu_var_info(struct thread *thread, struct map_symbol *ms, + u8 cpumode, u64 var_addr, const char **var_name, + int *poffset); + /** * struct annotated_basic_block - Basic block of instructions * @list: List node