From nobody Tue Dec 2 02:30:05 2025 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 070863A79B4 for ; Wed, 19 Nov 2025 14:34:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763562859; cv=none; b=BNyfycjLoyLssA9PjXnD7r7LY7hhL2LfsQNk37xm22S5WqxkV3IEDJSCoKVshsyPR6L2Ac5KbYabo9BcCDGmThWkZjABE8TquZbb+bbw0xN9DKcTVCycFtCsQj+98Ditrg8tNdw80hpsL0oCSLKIX23sNJp0WjCRHEV9pli5SrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763562859; c=relaxed/simple; bh=5r9KPynWPNRi7pXiMJT2iAdw5FVZk86lKwvnqQr5dtA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bo4hDUTQlSgRUc08sZR3I+pX2BHACqfwrNqw9Xh90Nmd+KXIcXMiNrsyDVFMZd4qHWDeuZwBh98osg3AWpXoeWYdvesmY36I83JJu5koBisZbCdhKd4zJxTiGT4sPMJ1a+0R7bU+cGFvuOPJb9MQp9JkNMPg1nkQGY10nZlSiI4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=lYCCf6nu; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="lYCCf6nu" Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 5AJEQx4Z014116; Wed, 19 Nov 2025 14:33:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2025-04-25; bh=0wILD QiVJau/1bpQTUwpwXYP2TrcZ1FsrRSvHvTMf10=; b=lYCCf6nuVZS5qYKOs6IP5 qHiFL8FqzfhmkKqhO9LQefkcBSBjR8qUY9Pxpj9wwxHGy8xW8xMGxh1YGmER7fP5 NXA9KZ8lYxSaW+/mlcof1EucZn5JH4oAehZUarkljEkrWP8YuPsg9mu8sbdUVNcr fGmtdOt3EHnyrH1lQWClavex9lc6QglGGujmaePRGmQwosYjPbPWsC39VEW+dP51 JcknQoxRkXerMUAaR8xOmWoun/nFvLSbI34QWeD00NzS9YCzckY6MQ1yXjnlasTn HIwHEkhFfFBG7SJPVdAeVXupylIAGkJ4fdORHKBIx8894Xs9OlW+TKP1scI2KAti g== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 4aej9077by-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 19 Nov 2025 14:33:19 +0000 (GMT) Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 5AJDW9t4002488; Wed, 19 Nov 2025 14:33:18 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 4aefyanrq1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 19 Nov 2025 14:33:18 +0000 Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 5AJEWnnY022495; Wed, 19 Nov 2025 14:33:18 GMT Received: from laptop-dell-latitude7430.nl.oracle.com (dhcp-10-154-160-110.vpn.oracle.com [10.154.160.110]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 4aefyanr5p-16; Wed, 19 Nov 2025 14:33:18 +0000 From: Alexandre Chartre To: linux-kernel@vger.kernel.org, mingo@kernel.org, jpoimboe@kernel.org, peterz@infradead.org, david.laight.linux@gmail.com Cc: alexandre.chartre@oracle.com Subject: [PATCH v5 15/30] objtool: Improve tracing of alternative instructions Date: Wed, 19 Nov 2025 15:32:32 +0100 Message-ID: <20251119143247.3944213-16-alexandre.chartre@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20251119143247.3944213-1-alexandre.chartre@oracle.com> References: <20251119143247.3944213-1-alexandre.chartre@oracle.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-11-19_04,2025-11-18_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 phishscore=0 malwarescore=0 suspectscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2510240000 definitions=main-2511190116 X-Authority-Analysis: v=2.4 cv=OMAqHCaB c=1 sm=1 tr=0 ts=691dd52f b=1 cx=c_pps a=WeWmnZmh0fydH62SvGsd2A==:117 a=WeWmnZmh0fydH62SvGsd2A==:17 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=yPCof4ZbAAAA:8 a=il4VveK9W3mU_z1A6oMA:9 X-Proofpoint-ORIG-GUID: tsoLv-pFNmOjxlrH9393VtXFfdDv-XxN X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTE1MDAzMSBTYWx0ZWRfX709U5i4VnZ7H jq4ytv1tn1vrjealQtShMBjMLbvuDJntVg8q9R5nZ0OHWnXaKHdUmtEaJ5hurJOvQlvDnwkA8b7 u/oz1kSEdpK2W79uMcCM38BJYE8VIbmBxWkCDtP7tGra0e9cEjy0MXascWk3Lq4Lk2K2OJ5hlwC f/GIZfLjZ3xR2kfmqWJRGBV85pbOAdaJuHKbo5+yn4b61UBmogsTXyl5ZHviV0kZ8eXDVsl5dg8 1PAf0Mg5J4xXucL4X+8A2IUOzpN3cxTUXv6UcaSoF1KzDKA8uF3TlBs1cnGXeAup5eXuiGc104d QaekGCn+2N8zNnmLY9YAhfL4wRTt09RjgeXEyczH+ri/eKY0jFkpxn7xLJtsMUzpQeLyhFfDb+0 Ot/yn1/8owzTr8GVMefrhKNZTvLt0g== X-Proofpoint-GUID: tsoLv-pFNmOjxlrH9393VtXFfdDv-XxN Content-Type: text/plain; charset="utf-8" When tracing function validation, improve the reporting of alternative instruction by more clearly showing the different alternatives beginning and end. Signed-off-by: Alexandre Chartre --- tools/objtool/check.c | 18 +++----- tools/objtool/include/objtool/trace.h | 65 ++++++++++++++++++++++++++- tools/objtool/trace.c | 55 +++++++++++++++++++++++ 3 files changed, 125 insertions(+), 13 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 20630df83e85c..fb000923718dc 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -3616,7 +3616,7 @@ static bool skip_alt_group(struct instruction *insn) =20 /* ANNOTATE_IGNORE_ALTERNATIVE */ if (insn->alt_group->ignore) { - TRACE_INSN(insn, "alt group ignored"); + TRACE_ALT(insn, "alt group ignored"); return true; } =20 @@ -3732,8 +3732,9 @@ static int validate_insn(struct objtool_file *file, s= truct symbol *func, struct instruction *prev_insn, struct instruction *next_insn, bool *dead_end) { - /* prev_state is not used if there is no disassembly support */ + /* prev_state and alt_name are not used if there is no disassembly suppor= t */ struct insn_state prev_state __maybe_unused; + char *alt_name __maybe_unused =3D NULL; struct alternative *alt; u8 visited; int ret; @@ -3820,23 +3821,16 @@ static int validate_insn(struct objtool_file *file,= struct symbol *func, return 1; =20 if (insn->alts) { - int i, num_alts; - - num_alts =3D 0; - for (alt =3D insn->alts; alt; alt =3D alt->next) - num_alts++; - - i =3D 1; for (alt =3D insn->alts; alt; alt =3D alt->next) { - TRACE_INSN(insn, "alternative %d/%d", i, num_alts); + TRACE_ALT_BEGIN(insn, alt, alt_name); ret =3D validate_branch(file, func, alt->insn, *statep); + TRACE_ALT_END(insn, alt, alt_name); if (ret) { BT_INSN(insn, "(alt)"); return ret; } - i++; } - TRACE_INSN(insn, "alternative DEFAULT"); + TRACE_ALT_INFO_NOADDR(insn, "/ ", "DEFAULT"); } =20 if (skip_alt_group(insn)) diff --git a/tools/objtool/include/objtool/trace.h b/tools/objtool/include/= objtool/trace.h index 33fe9c6acb4fd..70b574366797b 100644 --- a/tools/objtool/include/objtool/trace.h +++ b/tools/objtool/include/objtool/trace.h @@ -19,11 +19,26 @@ extern int trace_depth; fprintf(stderr, fmt, ##__VA_ARGS__); \ }) =20 +/* + * Print the instruction address and a message. The instruction + * itself is not printed. + */ +#define TRACE_ADDR(insn, fmt, ...) \ +({ \ + if (trace) { \ + disas_print_info(stderr, insn, trace_depth - 1, \ + fmt "\n", ##__VA_ARGS__); \ + } \ +}) + +/* + * Print the instruction address, the instruction and a message. + */ #define TRACE_INSN(insn, fmt, ...) \ ({ \ if (trace) { \ disas_print_insn(stderr, objtool_disas_ctx, \ - insn, trace_depth - 1, \ + insn, trace_depth - 1, \ fmt, ##__VA_ARGS__); \ fprintf(stderr, "\n"); \ insn->trace =3D 1; \ @@ -36,6 +51,37 @@ extern int trace_depth; trace_insn_state(insn, sprev, snext); \ }) =20 +#define TRACE_ALT_FMT(pfx, fmt) pfx "<%s.%lx> " fmt +#define TRACE_ALT_ARG(insn) disas_alt_type_name(insn), (insn)->offset + +#define TRACE_ALT(insn, fmt, ...) \ + TRACE_INSN(insn, TRACE_ALT_FMT("", fmt), \ + TRACE_ALT_ARG(insn), ##__VA_ARGS__) + +#define TRACE_ALT_INFO(insn, pfx, fmt, ...) \ + TRACE_ADDR(insn, TRACE_ALT_FMT(pfx, fmt), \ + TRACE_ALT_ARG(insn), ##__VA_ARGS__) + +#define TRACE_ALT_INFO_NOADDR(insn, pfx, fmt, ...) \ + TRACE_ADDR(NULL, TRACE_ALT_FMT(pfx, fmt), \ + TRACE_ALT_ARG(insn), ##__VA_ARGS__) + +#define TRACE_ALT_BEGIN(insn, alt, alt_name) \ +({ \ + if (trace) { \ + alt_name =3D disas_alt_name(alt); \ + trace_alt_begin(insn, alt, alt_name); \ + } \ +}) + +#define TRACE_ALT_END(insn, alt, alt_name) \ +({ \ + if (trace) { \ + trace_alt_end(insn, alt, alt_name); \ + free(alt_name); \ + } \ +}) + static inline void trace_enable(void) { trace =3D true; @@ -61,17 +107,34 @@ static inline void trace_depth_dec(void) =20 void trace_insn_state(struct instruction *insn, struct insn_state *sprev, struct insn_state *snext); +void trace_alt_begin(struct instruction *orig_insn, struct alternative *al= t, + char *alt_name); +void trace_alt_end(struct instruction *orig_insn, struct alternative *alt, + char *alt_name); =20 #else /* DISAS */ =20 #define TRACE(fmt, ...) ({}) +#define TRACE_ADDR(insn, fmt, ...) ({}) #define TRACE_INSN(insn, fmt, ...) ({}) #define TRACE_INSN_STATE(insn, sprev, snext) ({}) +#define TRACE_ALT(insn, fmt, ...) ({}) +#define TRACE_ALT_INFO(insn, fmt, ...) ({}) +#define TRACE_ALT_INFO_NOADDR(insn, fmt, ...) ({}) +#define TRACE_ALT_BEGIN(insn, alt, alt_name) ({}) +#define TRACE_ALT_END(insn, alt, alt_name) ({}) + =20 static inline void trace_enable(void) {} static inline void trace_disable(void) {} static inline void trace_depth_inc(void) {} static inline void trace_depth_dec(void) {} +static inline void trace_alt_begin(struct instruction *orig_insn, + struct alternative *alt, + char *alt_name) {}; +static inline void trace_alt_end(struct instruction *orig_insn, + struct alternative *alt, + char *alt_name) {}; =20 #endif =20 diff --git a/tools/objtool/trace.c b/tools/objtool/trace.c index d70d47081e82d..5dec44dab781c 100644 --- a/tools/objtool/trace.c +++ b/tools/objtool/trace.c @@ -146,3 +146,58 @@ void trace_insn_state(struct instruction *insn, struct= insn_state *sprev, =20 insn->trace =3D 1; } + +void trace_alt_begin(struct instruction *orig_insn, struct alternative *al= t, + char *alt_name) +{ + struct instruction *alt_insn; + char suffix[2]; + + alt_insn =3D alt->insn; + + if (alt->type =3D=3D ALT_TYPE_EX_TABLE) { + /* + * When there is an exception table then the instruction + * at the original location is executed but it can cause + * an exception. In that case, the execution will be + * redirected to the alternative instruction. + * + * The instruction at the original location can have + * instruction alternatives, so we just print the location + * of the instruction that can cause the exception and + * not the instruction itself. + */ + TRACE_ALT_INFO_NOADDR(orig_insn, "/ ", "%s for instruction at 0x%lx <%s+= 0x%lx>", + alt_name, + orig_insn->offset, orig_insn->sym->name, + orig_insn->offset - orig_insn->sym->offset); + } else { + TRACE_ALT_INFO_NOADDR(orig_insn, "/ ", "%s", alt_name); + } + + if (alt->type =3D=3D ALT_TYPE_JUMP_TABLE) { + /* + * For a jump alternative, if the default instruction is + * a NOP then it is replaced with the jmp instruction, + * otherwise it is replaced with a NOP instruction. + */ + trace_depth++; + if (orig_insn->type =3D=3D INSN_NOP) { + suffix[0] =3D (orig_insn->len =3D=3D 5) ? 'q' : '\0'; + TRACE_ADDR(orig_insn, "jmp%-3s %lx <%s+0x%lx>", suffix, + alt_insn->offset, alt_insn->sym->name, + alt_insn->offset - alt_insn->sym->offset); + } else { + TRACE_ADDR(orig_insn, "nop%d", orig_insn->len); + trace_depth--; + } + } +} + +void trace_alt_end(struct instruction *orig_insn, struct alternative *alt, + char *alt_name) +{ + if (alt->type =3D=3D ALT_TYPE_JUMP_TABLE && orig_insn->type =3D=3D INSN_N= OP) + trace_depth--; + TRACE_ALT_INFO_NOADDR(orig_insn, "\\ ", "%s", alt_name); +} --=20 2.43.5