From nobody Mon Oct 6 01:23:46 2025 Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB9D325A2A2 for ; Fri, 25 Jul 2025 20:28:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475303; cv=none; b=ApIs3W5UWNII90IkZagJRyXI/WQq4c8Ihnv7Zc16TsYowCJrizZ/rjnEOMDOgQTRAOX+UxcOqdz0sNNKL5aXdTj6Xh6QQGWMe2xnuJpv9stdEzIG7Vq4x9lFJDxx9UyOon4VKPymCTi5pJArRfplizTt/vpGg0rhBtcYQUTmgYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475303; c=relaxed/simple; bh=y7ZMmXMl4AeF7iTeFou5MQEhQkYueVSw9Jduxng0eNo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PN7LFcoLOoSqOzCC4NmIJsTzT0LmK1+reStWECxNXeulzB6glgd6zqRVkiVFMEwB3KPktP6nkhzgty8uDf0UQujR0ZpGPc9F6Savfxd/MsiW4xTM8eBcbefONbZhS0VIHcw2bI8FKIgPvxC0hlMZSopBpxF4tD9r7TnqgOKXuUE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wAPUzRxI; arc=none smtp.client-ip=209.85.160.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wAPUzRxI" Received: by mail-qt1-f202.google.com with SMTP id d75a77b69052e-4ab76d734acso71795811cf.0 for ; Fri, 25 Jul 2025 13:28:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753475301; x=1754080101; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=aSnOCIynF2EPoUgwxpXJNlBKEOIAjcwZq8VcJJ4NIKo=; b=wAPUzRxIdzE2StFBz8MQFaxMr/vU3Hg6RL0xyCp4bkTyBtggfpsx9wyGJDnEiCR+Aa FnWgeyMVcU8FN3ePNub3og6Bm52yeEpgTSD2QQ9G2HoStyWcDzHc3cIRVIE3dqhPRXQt DtnMqN2gdcOwWA2GzLuzT1naquz3d9g46h3ONzXrECh88hDZVrw5+qn9oeMIQF3X3MGz V7PXNAQfVcZWHpj4a38J8GC/Cwq88eQyOpCJ1bVZEVO/0Vg1gzv+O+hCjliceWiqkPPm K6HEJWv6F/7R1oYtXyhB7hciTfk6zK4dkj3fKneVTkJ6auY2Gc/vopC26ke62MZ+D+jU d3dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753475301; x=1754080101; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aSnOCIynF2EPoUgwxpXJNlBKEOIAjcwZq8VcJJ4NIKo=; b=l3Jgll0IbyoPHp0fP5P21BrBY3YUaIU5HUWLbbzTo1x5hLwvQtoCelFk1IcEkuslk2 LkmScAwoh4GAVDYhYo4DvtPdWDxT9aSDbigb8gw/xpIiu1uiCuyIA4xuqB9q1djslLnz TRXSkw0uMLy+gwucmuXEi98c923O8gV+x7YU1C1PKfUJ4GtXxIdB2qT3xF3bUDbF0PaE wb0+9XBAdXL2WCBOppqTsX/kixRj9VNJsNf+zgLyMEmOsUeR9GywJQZyyByFe0WnQAOp s1akc4A8YZ+FXqgEXWhHlJzx5Xs8sR09hDPqyxcKwv9ETguL8joM02mk8mOsDeejPHBr Qj4A== X-Forwarded-Encrypted: i=1; AJvYcCU1wYzLvfX+RLltl3GsF9m9U6H9/lqgdWlpxAbUqM4dLjQAOSMT0km++c5eDI4P1Qlm8leyjaz2NCSM4MQ=@vger.kernel.org X-Gm-Message-State: AOJu0YymKYoVgTkcPormYj6U6CgY9V9MJGyWpEFSBaN+3d3uXI4UH63Z 3jf7iBAD9fi180UdfPw+hpbCV54vnGgQvJg+rTTxekHiDwxPKR1z7zRCAL5+9dRWwGz3gf8btEC Tk0A6p845Cg== X-Google-Smtp-Source: AGHT+IEtaubTt49Ik8s9cRgE+B+Ay3+2JHiCaKE3hc7DgJYA6W0TCehhgfWQ7j17dc3k3QPYrwA+8oaWNbSX X-Received: from qtbcp4.prod.google.com ([2002:a05:622a:4204:b0:4ab:6375:3dda]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:5448:b0:4ab:63b8:3320 with SMTP id d75a77b69052e-4ae87225337mr79667211cf.23.1753475300882; Fri, 25 Jul 2025 13:28:20 -0700 (PDT) Date: Fri, 25 Jul 2025 20:28:04 +0000 In-Reply-To: <20250725202809.1230085-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250725202809.1230085-1-zecheng@google.com> X-Mailer: git-send-email 2.50.1.470.g6ba607880d-goog Message-ID: <20250725202809.1230085-2-zecheng@google.com> Subject: [PATCH v1 1/6] perf dwarf-aux: Use signed comparison in match_var_offset From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Zecheng Li , Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" match_var_offset compares address offsets to determine if an access falls within a variable's bounds. The offsets involved for those relative to base registers from DW_OP_breg can be negative. The current implementation uses unsigned types (u64) for these offsets, which rejects almost all negative values. This commit changes the local variables within match_var_offset to signed types (s64) before performing comparisons. This ensures correct behavior when addr_offset_ or addr_type_ are negative. Signed-off-by: Zecheng Li --- tools/perf/util/dwarf-aux.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 559c953ca172..bf906dff9ef0 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1388,10 +1388,12 @@ struct find_var_data { #define DWARF_OP_DIRECT_REGS 32 =20 static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *dat= a, - u64 addr_offset, u64 addr_type, bool is_pointer) + u64 addr_offset_, u64 addr_type_, bool is_pointer) { Dwarf_Die type_die; Dwarf_Word size; + s64 addr_offset =3D (s64)addr_offset_; + s64 addr_type =3D (s64)addr_type_; =20 if (addr_offset =3D=3D addr_type) { /* Update offset relative to the start of the variable */ @@ -1414,7 +1416,7 @@ static bool match_var_offset(Dwarf_Die *die_mem, stru= ct find_var_data *data, if (dwarf_aggregate_size(&type_die, &size) < 0) return false; =20 - if (addr_offset >=3D addr_type + size) + if (addr_offset_ - addr_type_ >=3D size) return false; =20 /* Update offset relative to the start of the variable */ --=20 2.50.1.470.g6ba607880d-goog From nobody Mon Oct 6 01:23:46 2025 Received: from mail-qv1-f74.google.com (mail-qv1-f74.google.com [209.85.219.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82FD225A359 for ; Fri, 25 Jul 2025 20:28:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475305; cv=none; b=DTQ2lSjA8rLUkEhnIIWsgicgJoXmD7bUg/3YM6160t9VNXYemKIqfQG3sJX4dWLHP1mbUR4rc+/GG3m3iIsYK+b9dNKiE0J3r0yALmttjt88GW1dXl5I7EdaqonO8pOkt1qIpAQTWk4K3vfEnIYTOboInLONctH4R5Llj+GDV7w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475305; c=relaxed/simple; bh=4WUHG3gQhYHFYivqw1IpclYSWeb/z1fqX28zLV0M6hw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rRED8wLF6TLTvNi5R4WmIvr+u3GMxgDCxfTRvdFUFnPg6TZZSIeM3ngVKeebg2BjcjIa0qPIpLmDgtczV4kRR2Lnh8qgzTH5I4yRu/1nAG1IX5pek4YJVjF/HqxxFmKtI6KUK3oqb7GN/Jd8NwgXg1+AphwxhbPISiYETw6By7Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CTBB0a+R; arc=none smtp.client-ip=209.85.219.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CTBB0a+R" Received: by mail-qv1-f74.google.com with SMTP id 6a1803df08f44-707205f0405so22634996d6.1 for ; Fri, 25 Jul 2025 13:28:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753475302; x=1754080102; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=M0eYSeMuUS/cZaFjj6vw+bFZjqUrA78ZYNsNfFMz9W4=; b=CTBB0a+RvgmYLxMQKDPItn3I79dXU/N+JZMQ/ReKU9yMqCMLj9/gz2/HP8QiJyrvR9 2irvhfgFkwq8op4xL55sD5AInZqszuvb2s8MRgh79AmDQodm1UCiO1jxWMxv+c99P339 FajrR/Pe2GtZj1Iq+U/b7Tn5/1KfyGAndNg96hinNe6FKUI/Dld47FLr6kkxx74odH0v +zWC206X3Yot7zcmfE77hoCYGVkQllm1M5BjG3xjWfqeSDX7Ox2+5TNdcL4djurKz1/S 6QpSanMDJMgpzYHua3IDpxrLjhdXmJRaxkU/6nFSPyXoUT3DLCnozQFtUhnGusPX1HNa 3fCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753475302; x=1754080102; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=M0eYSeMuUS/cZaFjj6vw+bFZjqUrA78ZYNsNfFMz9W4=; b=QvOSdbHWC4avpBriYBThy5hixygAlM5Q9oiaIrlJDokqfOidpd1WsRByl0U1Q1Ueam VO+0kbq8b2r9T7UOHWD9iPpDkcODhPkmxx5rOVjPTWS+AfzGaReymIxn/8z6rnAZsEcK aBKpj3AiulY9aJx/28mE6pQ3mvRlbk29EBaoBALMipjcU/ruCNhtC3RnC9XDlOXPfv+J VoscYbGIT3mAncaB03Hw3/f4KLSf0wkKy3wzI6bJeAq2O/5zVMgqA93hvk2p1XSkP65+ Y3y2uT2zFV5mbGjkZM5Z9ZHTXdy4S+RQ/MjR4wwUtdvMqThX37sx8MptQFkcyXk3i8kC AS0g== X-Forwarded-Encrypted: i=1; AJvYcCWJIoqaehNM58za16ySW72DMdRif/s/0Kk4NM3d0NNGyybygltNmeJuAKLL5X9anfCYVlxyfT9060LZED8=@vger.kernel.org X-Gm-Message-State: AOJu0YwrO9rxS2GtrHiVX0vWa0NV9APhBy7D/N+7MJ9ga/cGNfmdHjT5 t6Gy9oisbGado4CyT1pMYTr+kBLDy+miFNk/EYGHSZqKe36vPKOIJtWjCGEra+IT6gPr9PSLTny lo+6CLJw/gw== X-Google-Smtp-Source: AGHT+IGguu6Q5s+hXxqE9U/Ws3a68elF+ciB8met5fbwhcLiqcVm4bFlvEvADCzimGciV5znCZiumxeozGxb X-Received: from qvb13.prod.google.com ([2002:a05:6214:600d:b0:6fb:5697:95f1]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6214:e46:b0:6fa:ccb6:602a with SMTP id 6a1803df08f44-7072053f3f2mr53217756d6.20.1753475302280; Fri, 25 Jul 2025 13:28:22 -0700 (PDT) Date: Fri, 25 Jul 2025 20:28:05 +0000 In-Reply-To: <20250725202809.1230085-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250725202809.1230085-1-zecheng@google.com> X-Mailer: git-send-email 2.50.1.470.g6ba607880d-goog Message-ID: <20250725202809.1230085-3-zecheng@google.com> Subject: [PATCH v1 2/6] perf dwarf-aux: More accurate variable type match for breg From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Zecheng Li , Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduces the function is_breg_access_indirect to determine whether a memory access involving a DW_OP_breg* operation refers to the variable's value directly or requires dereferencing the variable's type as a pointer based on the DWARF expression. Previously, all breg based accesses were assumed to directly access the variable's value (is_pointer =3D false). The is_breg_access_indirect function handles three main cases: 1. Base register + offset only: (e.g., DW_OP_breg7 RSP+88) The calculated address is the location of the variable. The access is direct, so no type dereference is needed. Returns false. 2. Base register + offset, followed by other operations ending in DW_OP_stack_value, including DW_OP_deref: (e.g., DW_OP_breg7 RSP+96, DW_OP_deref, DW_OP_plus_uconst 0x64, DW_OP_stack_value) The DWARF expression computes the variable's value, but that value requires a dereference. The memory access is fetching that value, so no type dereference is needed. Returns false. 3. Base register + offset, followed only by DW_OP_stack_value: (e.g., DW_OP_breg7 RSP+176, DW_OP_stack_value) This indicates the value at the base + offset is the variable's value. Since this value is being used as an address in the memory access, the variable's type is treated as a pointer and requires a type dereference. Returns true. The is_pointer argument passed to match_var_offset is now set by is_breg_access_indirect for breg accesses. Signed-off-by: Zecheng Li --- tools/perf/util/dwarf-aux.c | 42 ++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index bf906dff9ef0..814c96ea509f 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1424,6 +1424,38 @@ static bool match_var_offset(Dwarf_Die *die_mem, str= uct find_var_data *data, return true; } =20 +/** + * is_breg_access_indirect - Check if breg based access implies type deref= erence + * @ops: DWARF operations array + * @nops: Number of operations in @ops + * + * Returns true if the DWARF expression indicates the variable's value is + * a pointer that the memory access dereferences. + * Returns false if the expression evaluates to the variable's value direc= tly. + * This is called after check_allowed_ops. + */ +static bool is_breg_access_indirect(Dwarf_Op *ops, size_t nops) +{ + ops++; + nops--; + + /* only the base register */ + if (nops =3D=3D 0) + return false; + + switch (ops->atom) { + case DW_OP_stack_value: + return true; + case DW_OP_deref_size: + case DW_OP_deref: + case DW_OP_piece: + return false; + default: + /* unreachable, OP not supported */ + return false; + } +} + /* Only checks direct child DIEs in the given scope. */ static int __die_find_var_reg_cb(Dwarf_Die *die_mem, void *arg) { @@ -1452,7 +1484,7 @@ static int __die_find_var_reg_cb(Dwarf_Die *die_mem, = void *arg) if (data->is_fbreg && ops->atom =3D=3D DW_OP_fbreg && check_allowed_ops(ops, nops) && match_var_offset(die_mem, data, data->offset, ops->number, - /*is_pointer=3D*/false)) + /*is_pointer=3D*/is_breg_access_indirect(ops, nops))) return DIE_FIND_CB_END; =20 /* Only match with a simple case */ @@ -1464,11 +1496,11 @@ static int __die_find_var_reg_cb(Dwarf_Die *die_mem= , void *arg) /*is_pointer=3D*/true)) return DIE_FIND_CB_END; =20 - /* Local variables accessed by a register + offset */ + /* variables accessed by a register + offset */ if (ops->atom =3D=3D (DW_OP_breg0 + data->reg) && check_allowed_ops(ops, nops) && match_var_offset(die_mem, data, data->offset, ops->number, - /*is_pointer=3D*/false)) + /*is_pointer=3D*/is_breg_access_indirect(ops, nops))) return DIE_FIND_CB_END; } else { /* pointer variables saved in a register 32 or above */ @@ -1478,11 +1510,11 @@ static int __die_find_var_reg_cb(Dwarf_Die *die_mem= , void *arg) /*is_pointer=3D*/true)) return DIE_FIND_CB_END; =20 - /* Local variables accessed by a register + offset */ + /* variables accessed by a register + offset */ if (ops->atom =3D=3D DW_OP_bregx && data->reg =3D=3D ops->number && check_allowed_ops(ops, nops) && match_var_offset(die_mem, data, data->offset, ops->number2, - /*is_poitner=3D*/false)) + /*is_pointer=3D*/is_breg_access_indirect(ops, nops))) return DIE_FIND_CB_END; } } --=20 2.50.1.470.g6ba607880d-goog From nobody Mon Oct 6 01:23:46 2025 Received: from mail-qv1-f74.google.com (mail-qv1-f74.google.com [209.85.219.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B95C325BEFE for ; Fri, 25 Jul 2025 20:28:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475306; cv=none; b=pbwYVhXPM74lh42S9Ct7Vr4wL3G4TrIVLkvYJoduNQMAIOVLH4X7QtF5uITENwGQ0Cpjcol4aU0xc/4xtDGD1D2qSyaQie1ERCYAGlvOyFEXBYd3ktGtxnSw6jWZcKnmgZJLpVfWz7/etCutAqwR1vujLoshvuydBF59wQNJUzk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475306; c=relaxed/simple; bh=By52Shk1UPL4GrfqyjyVCfkJNBxqdlzE+Cy88LJOfbQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jr3x324FB2q1CRliU/UJWqBOy/k9bKnyvmNGr/QYxDqV+p1QLdGELl2UEtpLNMaFkfxKNpNErpENjN3AlUjflcoWyAFZZIYRImEIxX+zmaDSbLvKnOC2qAwt/Tqd7cQ7EhnPST8BU58Gr/uWLp0vR/m2yYOE35Pvr7XXqsWkWfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Sdm1D4g/; arc=none smtp.client-ip=209.85.219.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Sdm1D4g/" Received: by mail-qv1-f74.google.com with SMTP id 6a1803df08f44-702b5e87d98so44010926d6.0 for ; Fri, 25 Jul 2025 13:28:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753475303; x=1754080103; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hRd3Ypg5T3RRql/Fga2dEv1dzwKJJJhwVZRtqvAUzhI=; b=Sdm1D4g/ya1KfsrNkrrX08BoD385GzmzKCaYbl1+eK10Cm8hF496rlwcW50G6Ctvrk biOdoBS1q+gm5kDXf+iF7uibcn0u8Muxt6GKyqEif351a/dd/XSCZPZp3lj9W81Yz8bQ XRMCZNK7vs3YKluwytHkWoN6QmyHPL+OQgFb2c6ZO4QxErm814zgQT9DLixPEYAwNKl7 0LcyZXdnoWsjQU225uPSDXSXv+Sq9c52YWgujmiQ9+lJf53jm+wy/jBftMUu+v0jXVUP 6ful4FtApQ/CxS8jK7snVbzaEcKiYNrYVRQ82y6syZ31awPCP3qPw20USJBDgsJt4IBO mSZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753475303; x=1754080103; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hRd3Ypg5T3RRql/Fga2dEv1dzwKJJJhwVZRtqvAUzhI=; b=aVbcjtdpfMxoWTNN7WHVbJ8cw6WWZ7E68vz5M+DX6WzTR3NGgLBpHPiadv1kkEyx3x xFatflmO0k5WtUF5HRuIsQR2qFW09Fu2VBDKVvflUka8fPy0icvysxIkHLfCjsOWgzzA PoymChtji6UGdwCLk08SbY0HGHUP6rVxTZtOvpgkvVF9d3RxCSi/C2p+UvYf4a6KCxuf jHiWqMDEtoWV0UTQelrV9H9H9akqw7awTbNwmbfYboDO76HWIj7u/9Yx8+5nuBkwDYXq 5EXbKKAchFSFnNQGGRWxUAkkOU/epI0LBTvURSuaZ+UW3tOEsP6wsWImvTy7zay/sDTC z9wg== X-Forwarded-Encrypted: i=1; AJvYcCXtVs98y7/zXbZrdzKO28ogfap32YYx6PyIHJhqjJoZLIjVkRq1vUxAf4nhJnAf4i6StmLBLuV5qkZw54o=@vger.kernel.org X-Gm-Message-State: AOJu0Yxd6H76DETUY35lK+9yDt7y70DNZXt2eEcLkth2asW0+WYd1ktQ DZpzWUr8NobcNBDh4yu99q8t+RQD4r1UFjefzZi582WkFwuXd+QblXA/7RH1Dn136u7VJGMXmMQ Ft9E1mDzKoQ== X-Google-Smtp-Source: AGHT+IHkhcJRypn35+rkPwHsygURzzuqfpDQ+MnD/3YOoth7To9Zk50PN2vd/p+Tmd8bCubnWQ5OFkx6kB3s X-Received: from qvma10.prod.google.com ([2002:a0c:e34a:0:b0:6fb:482a:50db]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6214:485:b0:702:c4d8:ec02 with SMTP id 6a1803df08f44-707205ea833mr47604626d6.40.1753475303439; Fri, 25 Jul 2025 13:28:23 -0700 (PDT) Date: Fri, 25 Jul 2025 20:28:06 +0000 In-Reply-To: <20250725202809.1230085-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250725202809.1230085-1-zecheng@google.com> X-Mailer: git-send-email 2.50.1.470.g6ba607880d-goog Message-ID: <20250725202809.1230085-4-zecheng@google.com> Subject: [PATCH v1 3/6] perf dwarf-aux: Better type matching for stack variables From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Zecheng Li , Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Utilizes the previous is_breg_access_indirect function to determine if the stack location stores the variable itself or the struct it points to. If the DWARF expression shows DW_OP_stack_value without DW_OP_deref, it indicates the variable value is the reg + offset itself, and the stack location it points to is the dereferenced type. Signed-off-by: Zecheng Li --- tools/perf/util/dwarf-aux.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 814c96ea509f..4039dbd2b8c0 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -1635,6 +1635,14 @@ static int __die_collect_vars_cb(Dwarf_Die *die_mem,= void *arg) if (die_get_real_type(die_mem, &type_die) =3D=3D NULL) return DIE_FIND_CB_SIBLING; =20 + if ((ops->atom =3D=3D DW_OP_fbreg || ops->atom =3D=3D DW_OP_breg7) && + dwarf_tag(&type_die) =3D=3D DW_TAG_pointer_type && + is_breg_access_indirect(ops, nops)) { + /* Get the target type of the pointer */ + if (die_get_real_type(&type_die, &type_die) =3D=3D NULL) + return DIE_FIND_CB_SIBLING; + } + vt =3D malloc(sizeof(*vt)); if (vt =3D=3D NULL) return DIE_FIND_CB_END; --=20 2.50.1.470.g6ba607880d-goog From nobody Mon Oct 6 01:23:46 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDBD725D1F5 for ; Fri, 25 Jul 2025 20:28:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475307; cv=none; b=ZgL9cSni5zuhnDo3L7tTHhFO5VTVFHSp/CHs1z8SiIUcyqQX1OUN5VjlW0KP5yTgT/p6mXKAQ3rPC9B5dbRY9Wz1oU8k2TLWj3FCaGB+52nURpikQGUMFNZwT3PLkvs23uBCvu+tLXgKJgV/kIqWDHZMDDS3KSZapAZO/KroDN8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475307; c=relaxed/simple; bh=RsMdV+T8oKe7okrmOS/sDhgJDG36fgIYtZAj1bhrGk0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QjyJr+NySDoDPTEeBt2VWTfKJNzaHhrGKMbr9ea5z6tDnCkzIRJw9X7hf1ORX7qQ1oTupzsJPiD4xogvkDzpSIDsqqmt+JjoMYlVrUq+476Y8wea4MBjNW+QW7VhuUphOd2F1JkuBCJeJqxjCLigNDS+tv9AONqqN08Tf5csWkM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pxk5IvdU; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pxk5IvdU" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-71838151744so37209687b3.3 for ; Fri, 25 Jul 2025 13:28:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753475305; x=1754080105; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NH3grKs9mzJDbjLSRQxCfhlTx/fm7fL4sq33ftQ/Ko4=; b=pxk5IvdURHQ0RGBdMjYbVRhPNdKXSazeJJfaRrPCdWWy6s9gWfDBNz8DL52i7y+blq ylGxVHQ8QZsjYg3Ve6a9yJo1CGxLTkZTqCC4ov4PEDaHAw1z/KJ0yQMUb7olu2RC/s2C 1f2DyF2+/DJ67j1gYFqS3bXqACrqIYXX4Ak4BPSXhQjlPlRPZXcA3LlggJEsAsv9q1I0 d/dhLLak+TIYdwc9xK39xzqPbvUT1dfbiIPDfx83HJd7qKuiCrwijZlWFgxJt5vmiP87 2WcyKBz0aaz/09CEavL4MsyEAoK20yr3rmjhF1dHco1f+rQT6pE77+kKZ/AsVMyirxBr urOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753475305; x=1754080105; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NH3grKs9mzJDbjLSRQxCfhlTx/fm7fL4sq33ftQ/Ko4=; b=LsMYP7NZfWeXCV215QxwIwT3wjU4ltA4gBAVOLMDJ4kfhW1mj/doqyqWYxHqBGx+mM wqqC1GBwUvqmJbzfFHeaByaoDxwZZ3eA9ASHrXpI1uAt2iTSdAXGvyNou53wOB0EdTDK QnOmlLIT0VG3B4zFUZmHisu32gCzswffEs+ibo4IFI4olO6v9JKM4WFi/hVaYXPN8UCD lFuM2OYkEhwcK1nd1MIRkZyAQL5/8Fv9jqKzyY5GXEkXVGZjMkj7SAh9oTGlZdCb9wv7 bQoC/fiq1y1WtdlBS5Qx7iVLjqxR8Iv34L4bqTLcEe+Ifhngi8NR43RraFLHgu08AY3T Snzg== X-Forwarded-Encrypted: i=1; AJvYcCUPglNh8O6Ti+uqBdOSh3fNImg8schjZ75Ebg9G3xx0ix4JC/RVQ0zWEBG3unG7mhoSv4sMjnmfG39nmfw=@vger.kernel.org X-Gm-Message-State: AOJu0YyTL+KMRaEIoa5/WVVoqPuEgek9xXFKEJZ87DudSlaLev+ilBxZ Tb8T5BbX2SJc4cNpFm7k+Tcr9hJlmnas3wl89TDxrxfTRduT23SjhAT1QQAxgoRV71ZkjMiXpqq 9q4y8iNFBXg== X-Google-Smtp-Source: AGHT+IFA927CeUxwshzc5Vf180U6zjOSOk2doZe+aMZiyiIUWXypDsYJ/hDfFF5wLUe1G7GR4PTltMGsdSel X-Received: from ywbje8.prod.google.com ([2002:a05:690c:6f88:b0:70e:86e0:e0bd]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:690c:4a01:b0:70e:2c7f:2ed4 with SMTP id 00721157ae682-719e30b8bccmr46449947b3.0.1753475304908; Fri, 25 Jul 2025 13:28:24 -0700 (PDT) Date: Fri, 25 Jul 2025 20:28:07 +0000 In-Reply-To: <20250725202809.1230085-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250725202809.1230085-1-zecheng@google.com> X-Mailer: git-send-email 2.50.1.470.g6ba607880d-goog Message-ID: <20250725202809.1230085-5-zecheng@google.com> Subject: [PATCH v1 4/6] perf annotate: Skip annotating data types to lea instructions From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Zecheng Li , Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Remove type annotation on lea instructions since they are not accessing memory. It should be counted as `no_mem_ops`. Signed-off-by: Zecheng Li --- tools/perf/util/annotate.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 0dd475a744b6..0d6f85ab9170 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -2729,6 +2729,11 @@ __hist_entry__get_data_type(struct hist_entry *he, s= truct arch *arch, return &stackop_type; } =20 + if (!strncmp(dl->ins.name, "lea", 3)) { + istat->bad++; + return NULL; + } + for_each_insn_op_loc(&loc, i, op_loc) { struct data_loc_info dloc =3D { .arch =3D arch, --=20 2.50.1.470.g6ba607880d-goog From nobody Mon Oct 6 01:23:46 2025 Received: from mail-qv1-f73.google.com (mail-qv1-f73.google.com [209.85.219.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25D6A25DB1D for ; Fri, 25 Jul 2025 20:28:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475308; cv=none; b=dJyZyb8kw+Y9T6d1kxUKgob+DcnMibF4/jBXwoWkYB5VHt9IqnF4SDfoCPbcU9ZoDRA8h2I4dC3G5k7gu8sB8Yr2dtzUfepvLrd6KKyR19t/P+H8KF939vFvSEjTZMI3f8wQ5oqf2hQDCcvtbAsD41j7vp2Y+kv4C+CMQW1ITw8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475308; c=relaxed/simple; bh=eqiNdrNNyNQ3rEI/7tgLzhp5UuUTLh3U4kfXBxwdLsM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cABrOXJTEJ3KieUbv4LinsRfgBYYugRxe0Yvd1oI8FH0+/v9m9yT92hiEMIalBzFa1z4TNUY2RF+JS64aiA5gQolHDFDzHk7gNKpLAbZbaN0jDdcQFW/8rXVgMLOmJunvJfkBMZQCb8YcCRhsYS1vY4u203G+TbxAj44pH4ecPM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=BVyy4621; arc=none smtp.client-ip=209.85.219.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="BVyy4621" Received: by mail-qv1-f73.google.com with SMTP id 6a1803df08f44-7071d802a76so10482076d6.1 for ; Fri, 25 Jul 2025 13:28:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753475306; x=1754080106; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wAsAF9iZYEJs1sam0wHaYd1qu3uSk+JqG6BWLJsyAu8=; b=BVyy4621QpGYwWyB72U/65hCMdWPlauIZUGXYdR6YePrkPcHxyfKXRNPiqj1k6Qcdv I2sXaD6tIqVK1+IrPAsTi/xiiUKjvBeZe2llf/82kjq/8wfbH8fWX2q7lRwrkHgbbOWN nUuG/yDzFzkylh1CrBczmEAcMANZs9JKuDtGWzM/9IfQ+x0SNrkeTR52dCjpDIt0Gt5y 9iSfY+pN/GkoZ84xqSh5XJYLDJ480mPaWlre3IrfQ3FzKWnS7LQBFpjGO/PyCmTEmUO3 +aY62aWP6AnwKkXZCwLpF0Q+2OrSWM0EuZxEZGN7G1tbSxxLUoGP4M5W3Ok+21R3qOmg y9rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753475306; x=1754080106; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wAsAF9iZYEJs1sam0wHaYd1qu3uSk+JqG6BWLJsyAu8=; b=ffk3641xvyAQODpQnH1EHaCY2thI9EnupUQQ0ZV/qrYvE+ukGmkfLTupTtsL3/V5ez 98awB2LQrNCCC/A8QZZ84nGAai6G3LJfmlabx+Zu75Ox3RM9tMnw7QKVO7BjzvR6JcqA MJm4c044JgSxMKe2kx7+NxG9l98lwxYBiAmIef0HL5spqw1bhNEoJ0Gi4kSe51wcNkjY sfCf5S2YSeVYgvTb4HtRZ7myijHdWZt1lEAsAnjHrpUSlIHrKyfhR8+0QqZ4wYUspITu rkV7Wuk97Vs7yMy5CEQCViVkbTr3qYHJ1hTtMN7EEAh0YDwr8dT95mj21YLGJccDEp9b mNfw== X-Forwarded-Encrypted: i=1; AJvYcCWTnK+NuLfs8vRojFBWIYv0ItW38Ve5zZWsQfKOnDxmILs96RyYN5MyebV8e8hiaVRXB6rq1qk4kvyzHOk=@vger.kernel.org X-Gm-Message-State: AOJu0YxZlacKKS+Xcm2+Vs50VwJL4Oergg/cd8A89Qo5zDLY0OyeKkue hukPgWuVeqrim8Gn4DPnjJ7SoguXP2ZD3F/Tafb6WDNK/24FVbtyKPbTE6wv+RR3mKYJh9GzXkf k3c0XOhGMYA== X-Google-Smtp-Source: AGHT+IFC21i9K0c6rPNOKxlTLDf5sxgCJsd5DNkYpywRCfVT8HRuIL0FCuuTh/eeK3uPHkKd3jeNZIkFY0nx X-Received: from qvbmn12.prod.google.com ([2002:a05:6214:5ecc:b0:704:88ad:4ae9]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a0c:e304:0:b0:707:ce0:d19b with SMTP id 6a1803df08f44-707204bde1bmr32951266d6.6.1753475306090; Fri, 25 Jul 2025 13:28:26 -0700 (PDT) Date: Fri, 25 Jul 2025 20:28:08 +0000 In-Reply-To: <20250725202809.1230085-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250725202809.1230085-1-zecheng@google.com> X-Mailer: git-send-email 2.50.1.470.g6ba607880d-goog Message-ID: <20250725202809.1230085-6-zecheng@google.com> Subject: [PATCH v1 5/6] perf dwarf-aux: Find pointer type to a type From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Zecheng Li , Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement die_find_pointer_to_type that searches for the pointer type to a given type in the CU. There's no guarantee that a pointer DIE exists for every possible base type. Compilers only generate DIEs for types actually used or defined. The performance could be improved by adding a cache for the pointer types. Currently its impact on the annotation time for vmlinux is low. Signed-off-by: Zecheng Li --- tools/perf/util/dwarf-aux.c | 69 +++++++++++++++++++++++++++++++++++++ tools/perf/util/dwarf-aux.h | 4 +++ 2 files changed, 73 insertions(+) diff --git a/tools/perf/util/dwarf-aux.c b/tools/perf/util/dwarf-aux.c index 4039dbd2b8c0..49d509839a85 100644 --- a/tools/perf/util/dwarf-aux.c +++ b/tools/perf/util/dwarf-aux.c @@ -2121,3 +2121,72 @@ Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, in= t offset, =20 return die_get_member_type(&type_die, offset, die_mem); } + +struct find_pointer_type_data { + /* DIE offset of the type we want to point to */ + Dwarf_Off target_type_offset; + Dwarf_Die *found_die; +}; + +static int __die_find_pointer_to_type_cb(Dwarf_Die *die_mem, void *arg) +{ + struct find_pointer_type_data *data =3D arg; + Dwarf_Attribute type_attr; + Dwarf_Die type_die; + Dwarf_Off ref_type_offset; + + if (dwarf_tag(die_mem) !=3D DW_TAG_pointer_type) + return DIE_FIND_CB_CONTINUE; + + if (!dwarf_attr(die_mem, DW_AT_type, &type_attr)) + return DIE_FIND_CB_SIBLING; + + /* Get the DIE this pointer points to */ + if (!dwarf_formref_die(&type_attr, &type_die)) + return DIE_FIND_CB_SIBLING; + + ref_type_offset =3D dwarf_dieoffset(&type_die); + + if (ref_type_offset !=3D 0 && ref_type_offset =3D=3D data->target_type_of= fset) { + /* This die_mem is a pointer to the target type */ + if (data->found_die) + *data->found_die =3D *die_mem; + return DIE_FIND_CB_END; + } + + return DIE_FIND_CB_SIBLING; +} + +/** + * die_find_pointer_to_type - Find a DIE for a pointer to a given type + * @cu_die: The compilation unit to search within. + * @target_type: The DIE of the type you want to find a pointer to. + * @result_die: Buffer to store the found DW_TAG_pointer_type DIE. + * + * Scans the children of the @cu_die for a DW_TAG_pointer_type DIE + * whose DW_AT_type attribute references the @target_type. + * + * Return: @result_die if found, NULL otherwise. + */ +Dwarf_Die *die_find_pointer_to_type(Dwarf_Die *cu_die, Dwarf_Die *target_t= ype, + Dwarf_Die *result_die) +{ + struct find_pointer_type_data data; + Dwarf_Die search_mem; + + if (!cu_die || !target_type || !result_die) + return NULL; + + data.target_type_offset =3D dwarf_dieoffset(target_type); + if (data.target_type_offset =3D=3D 0) { + pr_debug("Target type DIE has no offset\n"); + return NULL; + } + data.found_die =3D result_die; + + if (die_find_child(cu_die, __die_find_pointer_to_type_cb, &data, &search_= mem)) + return result_die; + + return NULL; +} + diff --git a/tools/perf/util/dwarf-aux.h b/tools/perf/util/dwarf-aux.h index 892c8c5c23fc..7e1336ff276e 100644 --- a/tools/perf/util/dwarf-aux.h +++ b/tools/perf/util/dwarf-aux.h @@ -156,6 +156,10 @@ Dwarf_Die *die_get_member_type(Dwarf_Die *type_die, in= t offset, Dwarf_Die *die_m /* Return type info where the pointer and offset point to */ Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, int offset, Dwarf_Die *d= ie_mem); =20 +/* Find a DIE for a pointer to a given type */ +Dwarf_Die *die_find_pointer_to_type(Dwarf_Die *cu_die, Dwarf_Die *target_t= ype, + Dwarf_Die *result_die); + /* Get byte offset range of given variable DIE */ int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf = *buf); =20 --=20 2.50.1.470.g6ba607880d-goog From nobody Mon Oct 6 01:23:46 2025 Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23BEA25A328 for ; Fri, 25 Jul 2025 20:28:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475310; cv=none; b=AEwRn4Hap6w92MIEyPKBkwpYnGUpStPZsw9O/s05fKX3N56yiLnALOYqo7PnFPG7ecrwOiqZCo5zSYSrtzAAm8uZN4ICXEvB8d4qj+Rlvt98TyNyP9n8AUaHiiZ5iqXpGJyvz1jU7RWIkWAksQVie+YZFBlhJqf3D+hAamPU060= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753475310; c=relaxed/simple; bh=lv54xUtQ3WONfNqydnskRHUhGzP6WkcXiLDmvg3vg48=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VZDAAzsdOS01W81dCWfS9OjQirWmVRp/vmZJZxl4nExPifgUE0lqZ4Zu02eI4kUjIEYNgOLMiUrhcQe8Z7/OH7dubiCzY7sWM6CRkKbaXVeAax1gQUKuLtMy/0NY4OJ7aj3iW8/mTlXYe7OjCm9jpFqKDIdI7C1QNwGpK3+wLAo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=vOuKzQCJ; arc=none smtp.client-ip=209.85.160.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vOuKzQCJ" Received: by mail-qt1-f202.google.com with SMTP id d75a77b69052e-4ab7464e3bfso24789971cf.2 for ; Fri, 25 Jul 2025 13:28:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753475307; x=1754080107; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JgtV/JZRHlv0+PHl5j1NSg/A21YbT9f5uAr0q6A1H5c=; b=vOuKzQCJCSQPTmbimUQnQG7eIkzupEINRwMU+azquzpWHJ+3F9jXssmZNCdhyurrgs VJNkwGuQb69IZBHyzxrvX3H++z/Yhh1niFSQ6Z5t4p1BbiUDkc5MIs2GH2eKNA41ryW0 hjzkHF3wcZshawk/Yzn5YOHfWc5Zib/hTSWuxOgByheuBdzWvB0YDJQeWKfnNwGArQyv JQfkgcKecE/M2T2g1R9kRij+Ya01OUR+Kd3WWhKNfTyQm9t2dqMqNHEjQhN8h4XxE2P+ PAWnA+DVvHOdHnIU1q5G5W68Nlf0y5sn7Nhi9DEKyKVBhFEI+a2mGgApB7HlDfyphp1c aOlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753475307; x=1754080107; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JgtV/JZRHlv0+PHl5j1NSg/A21YbT9f5uAr0q6A1H5c=; b=qJZJCvVCZJDywOpvGXpo8u+6yg6EuC+5cPlvji7rjcnXFTgSBHYQMvRNhpyI3qGaGG BEepDGqUDsrK9zxT7oNVIM4A/BbJsh2QNuyNIXxgCMKzQgSZpwo/WP5NuecjYJ5DW/r7 4j+7Yy1j2fU1k7SdeN3woyVytmLOQuEMlXCdkRAWOIv8qAKNsqCBqtBgcYevVanTLcnO gYOOMsgXMZcbnIEWR7gW962fwOHU42LhDAmqfYPGMOmdxhGb4pei8p1Ux/kWek3cu4U0 dVkMQ4b7xHjtWurq/m+lcK37kPKDJbe9sJsHwPRGIFQJE7Uy+cyGYLm/XDvSCLXCMH58 kmFQ== X-Forwarded-Encrypted: i=1; AJvYcCUObK9jJJrzshx57L36L1I8lVsXr2SnbMwT1eO76DmLAAMXgBg9WsfS8Bwf/KZrv8STnlFcxLkXDcBmt1s=@vger.kernel.org X-Gm-Message-State: AOJu0Yzecm97FTN8CnHbsj6bec24L4otbEjcSDYkWWkvCaRWBBS2DqtS HOxUqQT9VDgKhMbqBEQ3bnEMQYWDSFWaBmQger9N+RqwldUiovthDl2E6NOX8mXia5fb2/SZALi DGJSI3TqbXQ== X-Google-Smtp-Source: AGHT+IEaPavaGTYObiXIJE1ukWXvmgQePpoF9ZlEjnTtODAqdevAV+DR5yKu8V2x2kO3XF3fMCQqXamylvm1 X-Received: from qtbfk17.prod.google.com ([2002:a05:622a:5591:b0:4a9:91da:e87e]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:1888:b0:4ab:b0a0:5b58 with SMTP id d75a77b69052e-4ae8f11e1efmr51406351cf.46.1753475307164; Fri, 25 Jul 2025 13:28:27 -0700 (PDT) Date: Fri, 25 Jul 2025 20:28:09 +0000 In-Reply-To: <20250725202809.1230085-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250725202809.1230085-1-zecheng@google.com> X-Mailer: git-send-email 2.50.1.470.g6ba607880d-goog Message-ID: <20250725202809.1230085-7-zecheng@google.com> Subject: [PATCH v1 6/6] perf annotate: Track arithmetic instructions on pointers From: Zecheng Li To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Masami Hiramatsu Cc: Zecheng Li , Xu Liu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Track the arithmetic operations on registers with pointer types. We handle only add and lea instructions. The original pointer information needs to be preserved for getting outermost struct types. For example, reg0 points to a struct cfs_rq, when we add 0x10 to reg0, it should preserve the information of struct cfs_rq + 0x10 in the register instead of a pointer type to the child field at 0x10. Details: 1. struct type_state_reg now includes an offset, indicating if the register points to the start or an internal part of its associated type. This offset is used in mem to reg and reg to stack mem transfers, and also applied to the final type offset. 2. lea offset(%sp/%fp), reg is now treated as taking the address of a stack variable. It worked fine in most cases, but an issue with this approach is the pointer type may not exist. 3. lea offset(%base), reg is handled by moving the type from %base and adding an offset, similar to an add operation followed by a mov reg to reg. 4. Non-stack variables from DWARF with non-zero offsets in their location expressions are now accepted with register offset tracking. Limitation: Offset tracking for register moved to the stack is not yet implemented. Currently, moving an register with offset to the stack resolves to the member type, which worked in some of the cases. Strictly it should be a pointer to the immediate child member. Multi-register addressing modes in LEA are not supported. Signed-off-by: Zecheng Li --- tools/perf/arch/x86/annotate/instructions.c | 109 +++++++++++++++++++- tools/perf/util/annotate-data.c | 14 ++- tools/perf/util/annotate-data.h | 6 ++ 3 files changed, 120 insertions(+), 9 deletions(-) diff --git a/tools/perf/arch/x86/annotate/instructions.c b/tools/perf/arch/= x86/annotate/instructions.c index c6d403eae744..82fc0106a4de 100644 --- a/tools/perf/arch/x86/annotate/instructions.c +++ b/tools/perf/arch/x86/annotate/instructions.c @@ -248,6 +248,7 @@ static void update_insn_state_x86(struct type_state *st= ate, tsr =3D &state->regs[state->ret_reg]; tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("call [%x] return -> reg%d", @@ -284,6 +285,7 @@ static void update_insn_state_x86(struct type_state *st= ate, !strcmp(var_name, "this_cpu_off") && tsr->kind =3D=3D TSR_KIND_CONST) { tsr->kind =3D TSR_KIND_PERCPU_BASE; + tsr->offset =3D 0; tsr->ok =3D true; imm_value =3D tsr->imm_value; } @@ -291,6 +293,15 @@ static void update_insn_state_x86(struct type_state *s= tate, else return; =20 + /* Ignore add to non-pointer types like int */ + if (dwarf_tag(&tsr->type) =3D=3D DW_TAG_pointer_type && + src->reg1 !=3D DWARF_REG_PC && tsr->kind =3D=3D TSR_KIND_TYPE && !ds= t->mem_ref) { + tsr->offset +=3D imm_value; + pr_debug_dtp("add [%x] offset %#"PRIx64" to reg%d", + insn_offset, imm_value, dst->reg1); + pr_debug_type_name(&tsr->type, tsr->kind); + } + if (tsr->kind !=3D TSR_KIND_PERCPU_BASE) return; =20 @@ -302,6 +313,7 @@ static void update_insn_state_x86(struct type_state *st= ate, */ tsr->type =3D type_die; tsr->kind =3D TSR_KIND_POINTER; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("add [%x] percpu %#"PRIx64" -> reg%d", @@ -311,6 +323,68 @@ static void update_insn_state_x86(struct type_state *s= tate, return; } =20 + if (!strncmp(dl->ins.name, "lea", 3)) { + struct type_state_reg *src_tsr; + int sreg =3D src->reg1; + + if (!has_reg_type(state, sreg) || + !has_reg_type(state, dst->reg1) || + !src->mem_ref) + return; + + src_tsr =3D &state->regs[sreg]; + tsr =3D &state->regs[dst->reg1]; + + tsr->copied_from =3D -1; + tsr->ok =3D false; + + /* Case 1: Based on stack pointer or frame pointer */ + if (sreg =3D=3D fbreg || sreg =3D=3D state->stack_reg) { + struct type_state_stack *stack; + int offset =3D src->offset - fboff; + + stack =3D find_stack_state(state, offset); + if (!stack) + return; + + /* TODO: the pointer type may not exist */ + /* Now the register becomes a pointer to the stack variable */ + if (!die_find_pointer_to_type(cu_die, &stack->type, &type_die)) + return; + + tsr->type =3D type_die; + tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; + tsr->ok =3D true; + + pr_debug_dtp("lea [%x] -> reg%d points to %s + %#x on stack\n", + insn_offset, dst->reg1, + (sreg =3D=3D fbreg) ? "fbreg" : "sp", offset); + pr_debug_type_name(&tsr->type, tsr->kind); + } + /* Case 2: Based on a register holding a typed pointer */ + else if (has_reg_type(state, sreg) && + state->regs[sreg].ok && + state->regs[sreg].kind =3D=3D TSR_KIND_TYPE) { + + /* Check if the target type has a member at the new offset */ + if (__die_get_real_type(&state->regs[sreg].type, &type_die) =3D=3D NULL= || + !die_get_member_type(&type_die, + src->offset + src_tsr->offset, &type_die)) + return; + + tsr->type =3D src_tsr->type; + tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D src->offset + src_tsr->offset; + tsr->ok =3D true; + + pr_debug_dtp("lea [%x] -> reg%d points to reg%d + %#x\n", + insn_offset, dst->reg1, sreg, src->offset); + pr_debug_type_name(&tsr->type, tsr->kind); + } + return; + } + if (strncmp(dl->ins.name, "mov", 3)) return; =20 @@ -345,6 +419,7 @@ static void update_insn_state_x86(struct type_state *st= ate, =20 if (var_addr =3D=3D 40) { tsr->kind =3D TSR_KIND_CANARY; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] stack canary -> reg%d\n", @@ -361,6 +436,7 @@ static void update_insn_state_x86(struct type_state *st= ate, =20 tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] this-cpu addr=3D%#"PRIx64" -> reg%d", @@ -372,6 +448,7 @@ static void update_insn_state_x86(struct type_state *st= ate, if (src->imm) { tsr->kind =3D TSR_KIND_CONST; tsr->imm_value =3D src->offset; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] imm=3D%#x -> reg%d\n", @@ -388,6 +465,7 @@ static void update_insn_state_x86(struct type_state *st= ate, tsr->type =3D state->regs[src->reg1].type; tsr->kind =3D state->regs[src->reg1].kind; tsr->imm_value =3D state->regs[src->reg1].imm_value; + tsr->offset =3D 0; tsr->ok =3D true; =20 /* To copy back the variable type later (hopefully) */ @@ -421,12 +499,14 @@ static void update_insn_state_x86(struct type_state *= state, } else if (!stack->compound) { tsr->type =3D stack->type; tsr->kind =3D stack->kind; + tsr->offset =3D 0; tsr->ok =3D true; } else if (die_get_member_type(&stack->type, offset - stack->offset, &type_die)) { tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; } else { tsr->ok =3D false; @@ -446,9 +526,10 @@ static void update_insn_state_x86(struct type_state *s= tate, else if (has_reg_type(state, sreg) && state->regs[sreg].ok && state->regs[sreg].kind =3D=3D TSR_KIND_TYPE && die_deref_ptr_type(&state->regs[sreg].type, - src->offset, &type_die)) { + src->offset + state->regs[sreg].offset, &type_die)) { tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] %#x(reg%d) -> reg%d", @@ -473,6 +554,7 @@ static void update_insn_state_x86(struct type_state *st= ate, =20 tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] global addr=3D%"PRIx64" -> reg%d", @@ -504,6 +586,7 @@ static void update_insn_state_x86(struct type_state *st= ate, die_get_member_type(&type_die, offset, &type_die)) { tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 if (src->multi_regs) { @@ -526,6 +609,7 @@ static void update_insn_state_x86(struct type_state *st= ate, src->offset, &type_die)) { tsr->type =3D type_die; tsr->kind =3D TSR_KIND_TYPE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] pointer %#x(reg%d) -> reg%d", @@ -548,6 +632,7 @@ static void update_insn_state_x86(struct type_state *st= ate, &var_name, &offset) && !strcmp(var_name, "__per_cpu_offset")) { tsr->kind =3D TSR_KIND_PERCPU_BASE; + tsr->offset =3D 0; tsr->ok =3D true; =20 pr_debug_dtp("mov [%x] percpu base reg%d\n", @@ -571,6 +656,22 @@ static void update_insn_state_x86(struct type_state *s= tate, int offset =3D dst->offset - fboff; =20 tsr =3D &state->regs[src->reg1]; + type_die =3D tsr->type; + + /* The register is derived from a pointer to the type */ + if (tsr->offset !=3D 0) { + /* + * deref gets the innermost field, but we actually want direct + * child field and take a pointer to it. + * However array type like unsigned long[] is already a pointer + * and is the most common case for this kind of stack variable. + */ + if (die_deref_ptr_type(&tsr->type, tsr->offset, &type_die)) { + pr_debug_dtp("find member: tsr->offset: %d", tsr->offset); + pr_debug_type_name(&type_die, tsr->kind); + } else + return; + } =20 stack =3D find_stack_state(state, offset); if (stack) { @@ -583,10 +684,10 @@ static void update_insn_state_x86(struct type_state *= state, */ if (!stack->compound) set_stack_state(stack, offset, tsr->kind, - &tsr->type); + &type_die); } else { findnew_stack_state(state, offset, tsr->kind, - &tsr->type); + &type_die); } =20 if (dst->reg1 =3D=3D fbreg) { @@ -596,7 +697,7 @@ static void update_insn_state_x86(struct type_state *st= ate, pr_debug_dtp("mov [%x] reg%d -> %#x(reg%d)", insn_offset, src->reg1, offset, dst->reg1); } - pr_debug_type_name(&tsr->type, tsr->kind); + pr_debug_type_name(&type_die, tsr->kind); } /* * Ignore other transfers since it'd set a value in a struct diff --git a/tools/perf/util/annotate-data.c b/tools/perf/util/annotate-dat= a.c index 1ef2edbc71d9..9cb215e499ef 100644 --- a/tools/perf/util/annotate-data.c +++ b/tools/perf/util/annotate-data.c @@ -887,7 +887,7 @@ static void update_var_state(struct type_state *state, = struct data_loc_info *dlo insn_offset, -offset); } pr_debug_type_name(&mem_die, TSR_KIND_TYPE); - } else if (has_reg_type(state, var->reg) && var->offset =3D=3D 0) { + } else if (has_reg_type(state, var->reg)) { struct type_state_reg *reg; Dwarf_Die orig_type; =20 @@ -898,13 +898,17 @@ static void update_var_state(struct type_state *state= , struct data_loc_info *dlo continue; =20 orig_type =3D reg->type; - + /* + * var->offset + reg value is the beginning of the struct + * reg->offset is the offset the reg points + */ + reg->offset =3D -var->offset; reg->type =3D mem_die; reg->kind =3D TSR_KIND_TYPE; reg->ok =3D true; =20 - pr_debug_dtp("var [%"PRIx64"] reg%d", - insn_offset, var->reg); + pr_debug_dtp("var [%"PRIx64"] reg%d at offset %d", + insn_offset, var->reg, var->offset); pr_debug_type_name(&mem_die, TSR_KIND_TYPE); =20 /* @@ -1092,7 +1096,7 @@ static enum type_match_result check_matching_type(str= uct type_state *state, if (__die_get_real_type(&state->regs[reg].type, type_die) =3D=3D NULL) return PERF_TMR_NO_POINTER; =20 - dloc->type_offset =3D dloc->op->offset; + dloc->type_offset =3D dloc->op->offset + state->regs[reg].offset; =20 if (dwarf_tag(type_die) =3D=3D DW_TAG_typedef) die_get_real_type(type_die, &sized_type); diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-dat= a.h index 541fee1a5f0a..a6df0491eebf 100644 --- a/tools/perf/util/annotate-data.h +++ b/tools/perf/util/annotate-data.h @@ -173,6 +173,12 @@ extern struct annotated_data_stat ann_data_stat; struct type_state_reg { Dwarf_Die type; u32 imm_value; + /* + * The offset within the struct that the register points to. + * A value of 0 means the register points to the beginning. + * type_offset =3D op->offset + reg->offset + */ + s32 offset; bool ok; bool caller_saved; u8 kind; --=20 2.50.1.470.g6ba607880d-goog