Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp53916rdg; Wed, 11 Oct 2023 20:57:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGlp7vfUsTm3Z6rlH+lXZ4q+85siCze93RoGmwnD1Al/lcSi6gDcL/BmYQvnvUTQvnrNEYP X-Received: by 2002:a05:6808:f01:b0:3a7:c2e8:5e7c with SMTP id m1-20020a0568080f0100b003a7c2e85e7cmr28465313oiw.43.1697083075083; Wed, 11 Oct 2023 20:57:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697083075; cv=none; d=google.com; s=arc-20160816; b=rbQmDvMCvoKcSuihO3GOM8dk4lijn4ogQ3vGriAG12s7J2WdFt4DHDG8XSGfk0OjWS hJAKsyDUbwciE9xV8NrALXaRSHtdTKo7rwEkuWjonNg5GTGVKqtXECJRgn8yShZAUeH2 tY2UoyyCJVKw+9azZy2kPRNRrOxuDqMTds/shpggd3tsPwDj2dGzc6yIvBhQjdDlgCtG THOVa7q8K5BkFpUKuCCtHzfGe7UmcAssrg8jW4gbxqFuhR9VeOQaxE+CmQSMaWhhOUlQ UULh/WSsyNMjD0dP0w8dc7S7bojGASZ7su3l4H2/ma7tv6nRxcvk50rtOXGuiYVuUSel Mp0w== 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=n1kE8iuDTwlI9GdS4S0Z9K7JNGWfoZ1amqFutl3GXsA=; fh=7Id7wL2M+oCJXD4imOzx4OKioxBXSOZqypDprm7aO4E=; b=VDtxCGYbeX/I3ejca1oshGzAvxCto+gjtAp+J/oGmJLc3Ea0LYeoyqupV+h1PULbxy cpvtW05xOdhsWNf/JJA+Ovq0xQ39k+hFl6oi722RyutoFGW8eoCO8V/7IR2romjZZ+nk SSMymS+hj8l6/aWTQbx8NB7XA2ug2PiZTqqFmBNotZ1Wq0Cgmuz6Go8XQ4CW8pLQ2QiN Ubg91uIBPgY7GN3DvqtPGIAFYGMNHGO2SIGl3dsBWxTFfOsJfzWPwd7fNKiaVNFilRgf ZtjNRJSBLN8LXMbmQ89d8N0Ym/ygXXIbeHbJ2OynVzuXOgVs0T06614JARVUrHFEAWDj RTYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="h5a/6m3p"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id bw10-20020a056a02048a00b00573f703a239si1483376pgb.414.2023.10.11.20.57.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 20:57:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="h5a/6m3p"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id 2CADF80A0DEC; Wed, 11 Oct 2023 20:57:49 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377317AbjJLD4C (ORCPT + 99 others); Wed, 11 Oct 2023 23:56:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377167AbjJLDyV (ORCPT ); Wed, 11 Oct 2023 23:54:21 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0AF7199A; Wed, 11 Oct 2023 20:52:14 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-5809d5fe7f7so398725a12.3; Wed, 11 Oct 2023 20:52:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697082733; x=1697687533; 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=n1kE8iuDTwlI9GdS4S0Z9K7JNGWfoZ1amqFutl3GXsA=; b=h5a/6m3ptIDnWrQnWunqK6YgJdALR4Rx83P6CTcH7uiJUnYE+HmptQswrc6ortrgaK dc2X1uPxUO0/flFZDWltm/RV6GdDqVb/1BTJAwO3qyj//GKhXhgc6A+o6+91w6XgCRP7 DMUuZauEi+l696AV0ifNOrwJI/0SCmjj7Sv9hSD6GKzsS6LfBrp5Qs3/bGss4PMEZHil 9fBfAiwNlwdFzeqk7TVTEJF6i/i66aSrYCTxx3nR/rwlKA9fxwEBy7WMp8obaMQS+/xb Ps0TviaIbNpU/JfhxrcdJEi7Jm4MCmu6dqb3y3ywq5ElkNCnMVoYgCO29MfCUUwYsyUq t2kQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697082733; x=1697687533; 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=n1kE8iuDTwlI9GdS4S0Z9K7JNGWfoZ1amqFutl3GXsA=; b=w7+0Xq36mJrZtessq/l6HzwRVFRKb3B5H9BmSLBEDTntz532N9ANZ8s0Ovq8+L6LF7 uAVMQi0fyNMNU6UAqdDAT1PhZYwgZKLMAixi7E+X5jXqohxuiwPiFHlY/bj0gAzDVXJW qU0qMXzKjGc/IYbxtSlm3dbcyV1kxkgQNNG17EcBm2UKShYcK88DoQaNrjXJC8aN4q5w pFkmDCZORL1ovc1Ap3r37cu+DgN6euidu4AAFIWW8inmkEIqRnSx5NCpGvQWjnfIU6at yQqVU+xoFuqk9wbwZInZkdUWnhwkeE1eWem+WntFXjk5U8RoQOZtk7FS7T+rW5UitEtr Xh2w== X-Gm-Message-State: AOJu0Ywa781CHAF/nItbnqJ/F2PK1fbuOEVg7BzYNLfRrZPwi654NtH/ ar2tVkj5ItCbNlDvihwj87c= X-Received: by 2002:a05:6a20:12c1:b0:174:63a9:2ab with SMTP id v1-20020a056a2012c100b0017463a902abmr2119183pzg.45.1697082733543; Wed, 11 Oct 2023 20:52:13 -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.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 20:52:13 -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 45/48] perf annotate-data: Handle this-cpu variables in kernel Date: Wed, 11 Oct 2023 20:51:08 -0700 Message-ID: <20231012035111.676789-46-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 Content-Transfer-Encoding: 8bit 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 fry.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 (fry.vger.email [0.0.0.0]); Wed, 11 Oct 2023 20:57:49 -0700 (PDT) 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 7a097f64a28a..414ae45b7c06 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -3766,6 +3766,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 @@ -3857,6 +3878,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 == -1 && 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 076b5338ade1..c090cea1abdc 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -511,6 +511,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 -- 2.42.0.655.g421f12c284-goog