From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 8CE912F1FC9 for ; Mon, 29 Jun 2026 18:08:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=170.10.133.124 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782756490; cv=pass; b=E9rvQrQoD5D7uK7H2iYW91sth8Ch6IF76Fkkbqo4jIlPwDAHVJBg9qsxH3R1h2qRfkkk8T6YiBP5pRkvD1EQxxDSfl55BbM/8cAOWO/274iOlF/G5Q42fuwWenZ2pNnmVLaKwkSLpqP/zVqwrjWM8tESRbHqxffHhIqbIYxLA1U= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782756490; c=relaxed/simple; bh=avOnL3A8aTp+SytE+H4A/lkLT0DTp3jf42S0BlB4Sjs=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=NuHqCQdQiG8Eo7leBOTzEJVCAwKrfRvrKdRWFA0kH9GmAn8K5qaXwzPfBt/rQOU4xwjko8L9xfohkKwOv2UrsFkSgoSWtPlHgrEmDuK5TA+fpiyVVTsQzzBdrnOGYWuONhIkeWM7A8NGdUzmPWRt+xDwZw1qv2Kdpw74bMhr7Uk= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=G0WwiVgv; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=NBLTUe1Y; arc=pass smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="G0WwiVgv"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="NBLTUe1Y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1782756486; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=THLii+AuvMtyVQW0et/j6izh4HdLW1xzxGPITOaWX44=; b=G0WwiVgvRU+amRbvdG63RJcTMmT4dcbcA0ajyaESeXaYoBD2ZwIr/Tc4tCtOOdFcAcFzRo wYsMPzP1qxVliqJF52V2o038SDPM4ZiuH25Fwpw0+cBuintDFXMy2MrI5cvJZzHLM5fUw1 IPV9eOYeSOuztIcz3P9/p88dmDu29vY= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-688-p53C9jqNNz-Dyv_rNIiJ6g-1; Mon, 29 Jun 2026 14:08:04 -0400 X-MC-Unique: p53C9jqNNz-Dyv_rNIiJ6g-1 X-Mimecast-MFC-AGG-ID: p53C9jqNNz-Dyv_rNIiJ6g_1782756484 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-49271013dcdso22357795e9.0 for ; Mon, 29 Jun 2026 11:08:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1782756483; cv=none; d=google.com; s=arc-20260327; b=r36R7ltMYrllesR4ybg6x4wPG5/Yct5GFPl3XOrN2mah0iCqb3wBbE6aCK15UuzdyZ yRbZoGNaEokezMzqrmsrEnMYzInT0+kpzL5nzFdZ5yP6X3zDlZ7+PUFQO0bF9AOc/VjV 5Cn6950KljWaqS0hvHFwtsgMDlhoJGvecRXST2Vo+nyKG+e5ShiPDO/EInBIBvCZSVyT 1kCBnDXzFlB6x68NfLXpHf79DoULIzZXpm78Qy7ZNVIn3TJIS90aworAWwtORZFQ4x0h qMGlW+Exwl4ISXf6qLmoyEsRXQLkkLYpL3/O4lsh9q9xQjIzfROWzbQYm7fb22K2Higr dupw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=THLii+AuvMtyVQW0et/j6izh4HdLW1xzxGPITOaWX44=; fh=decA8SOn5Uq6E46Thsu9MwKsQo4OpVv0KGOsVgr3pBs=; b=Z2iTahuXJcHVjzHHWyTWaNjMYY4k4NlFDq53F9ZEhkxtorzy61okNCq/+gNE9qfq5u UO6bBdRE11ZqvxD0L4VRtCvXoYEo2vFcTISRu6HYXrPfTmDMgVHuIV1yWKavtfLVAirb LUPhphc80OD8zmukeWSP8UroG+eEP0M1BJV0KqdIsIfsg4ATU7zX+LeNFa6AEIKjczYA hLXlSP6DWoEmXsF5SZQpdZOV4ambBA8wSiV60JXLzAgYnLBv3Wpogz3N3CEnmCs51+O7 jEu1TRIwMHEDCXepXEMW3F/f5Eju35n3PUetC4/e8ZQ4VSwBiqHToN0+8xbRxN0i39kR 9wPA==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1782756483; x=1783361283; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=THLii+AuvMtyVQW0et/j6izh4HdLW1xzxGPITOaWX44=; b=NBLTUe1Yhowxzu/44WWPCSVA5sJ+PxVNP3CB4mImvhKrSjHnNhU3A6eg+ej+3MO3JU ikBMsvEH4agOn3wyihq0i0QYO5vRdjfabjYAIYrlJ2P5ZXgZLMpdXD1ykPTAOqzMsJUB ELr8nXLiNXqyZnrtTAHKrtkpBbRcAHvwflQoWxtjdN7b9+BorITgbKcp+8a9dOr0ogBC c1IcYyQgxAPvvb4zQCasw0lnYPy26PgEkXn0p7dp0F/6W+O/JDIHPy7VQhNIuRUo1r1D nAPeyH7/OMtN6wJ0w3+u+Avt3Ea3ASzvOupp5+MLv2batp4GQuhK8+2VZmJ5kZvH+//x 8iVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782756483; x=1783361283; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=THLii+AuvMtyVQW0et/j6izh4HdLW1xzxGPITOaWX44=; b=V6HmI+zSlNry1fEBsYlgIO6/THiTw07KoPIpleLQcf2nknOclfJGfPn7ia0kxw+hd9 wyM7/sbdwL5auWrewIILsOnYWh3jH7hn/V65xmrLRQAzsbSY96egCFEFJgg3FtKcT4pD +DRSldtUs+nPINwu+3wlkDOPMvsKRnCzyBsj90kF2e8DYpbHFTPIakJT3gXAlT59VK6U puy+WZXj2b6bmdIPEOaE1Q+28+YxYVcdoBKeMzJSzH3awz1SgMDHhyFcZXDoVIrEGVVy pc+YnjU1sedq+IeHkOD2NTfBhbM4iyNGtvvtF1nGxCrgAHk1Ebm5rARn43XXd4DwyeHe 4qIw== X-Forwarded-Encrypted: i=1; AFNElJ+r3re3oz8kUFUp9MLMPzZ5XHIopxJq+qjV6mlv0qP4DR4wtNct6i18d6vyM7QDiM0kAkGlDTGDypurVaw=@vger.kernel.org X-Gm-Message-State: AOJu0YyivCTre0hxOGFC8UMBwE3pCBdII8Rn8nUCGmZ/YLd2bxmA8F4Z xbdwj8PosFp3pp/Vy3adaTiWW3hMP9rWEmtcOdHW9hfiY90f6AIzYFxq69TNiC7tK6LQeep8PsV erffECXWAy3nJm+ljUFdae94Li/WpiBRzNB0Hk9odYwM2qZdbNF9seYLaxaJtE6oqUtDrsYOgxf 318hUzcbfpmlt20RLSZeIF2iNpxRBmZO3yQDZS2hWc X-Gm-Gg: AfdE7cnvkEqcGqUtBoBUhdBosSWwBwYq0pXcBaaoq7CEpTawT+Q/Q/U9D+2IVgIRxCm /pruWKHYnKtQIDpqD7v1XDr1F351/HXqp4PPwLH6LCpzns/KdTQAsCbyy7CK9zVe60ERe3n6kWB yrKVQUObzEGabBtsNXmEPzgTfBJzg4Sz2+yKHXVK0Qfr/GqhPuHGR6l+r/kGXG3FKggYaaTnGr/ fYyKWW4niVzklo9NO2aeiI5JQvt X-Received: by 2002:a05:600c:4712:b0:492:523f:a3df with SMTP id 5b1f17b1804b1-493b827fa6dmr11407795e9.5.1782756483486; Mon, 29 Jun 2026 11:08:03 -0700 (PDT) X-Received: by 2002:a05:600c:4712:b0:492:523f:a3df with SMTP id 5b1f17b1804b1-493b827fa6dmr11407205e9.5.1782756483021; Mon, 29 Jun 2026 11:08:03 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20260621-riscv_insn_table-v2-0-7f0810ee2274@gmail.com> <20260621-riscv_insn_table-v2-2-7f0810ee2274@gmail.com> In-Reply-To: <20260621-riscv_insn_table-v2-2-7f0810ee2274@gmail.com> From: Jesse Taube Date: Mon, 29 Jun 2026 14:07:51 -0400 X-Gm-Features: AVVi8CdEQG7Qxlxta4-nm3blkK4c7RnQNE8PBBgxHxyXyoKpWdAVunc0FesI1v8 Message-ID: Subject: Re: [PATCH v2 02/16] riscv: alternatives: Use generated instruction headers for patching code To: thecharlesjenkins@gmail.com Cc: Paul Walmsley , Palmer Dabbelt , Nam Cao , Alexandre Ghiti , Anup Patel , Atish Patra , Conor Dooley , Paolo Bonzini , Andrew Morton , Shuah Khan , =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jun 22, 2026 at 12:02=E2=80=AFAM Charlie Jenkins via B4 Relay wrote: > > From: Charlie Jenkins > > Migrate the alternatives patching code to use the generated instruction > headers instead of the hand-written instruction composition functions. > > Signed-off-by: Charlie Jenkins Reviewed-by: Jesse Taube > --- > > These function expansions are very simple and almost expand to the same > thing in the new and old version. The main difference is > riscv_insn_auipc_extract_imm() in the new version expands to: > > ((_insn >> 12 & GENMASK(19, 0)) << 12) > > while it expands to the following in the old version: > > ((_insn >> 0) & GENMASK(31, 12)) > > These are the same thing, but GCC is unable to properly optimize the > second one so the first one ends up using almost half as many > instructions. With this finding and other similar examples, I made > the generated headers construct in the first way to help GCC optimize > all of these functions. > > Brute force can also be checked with this code: > > void check_auipc_jalr() { > for (unsigned int auipc =3D 0; auipc < ((1ULL << 12) - 1); auipc+= +) { > for (unsigned int jalr =3D 0; jalr < ((1ULL << 20) - 1); = jalr++) { > unsigned int auipc_t=3Driscv_insn_auipc(auipc, 0)= , jalr_t=3Driscv_insn_jalr(jalr, 0, 0), auipc_t2=3Driscv_insn_auipc(auipc, = 0), jalr_t2=3Driscv_insn_jalr(jalr, 0, 0); > riscv_alternative_fix_auipc_jalr(&auipc_t, &jalr_= t, 0); > riscv_alternative_fix_auipc_jalr2(&auipc_t2, &jal= r_t2, 0); > > if (auipc_t !=3D auipc_t2) { > printf("auipcs don't match %u, %u: %u != =3D %u\n", auipc, jalr, auipc_t, auipc_t2); > return; > } > > if (jalr_t !=3D jalr_t2) { > printf("jalrs don't match %u: %u !=3D %u\= n", i, jalr_t, jalr_t2); > } > } > } > } > --- > arch/riscv/include/asm/insn.h | 74 -----------------------------------= ------ > arch/riscv/kernel/alternative.c | 23 +++++++++---- > 2 files changed, 17 insertions(+), 80 deletions(-) > > diff --git a/arch/riscv/include/asm/insn.h b/arch/riscv/include/asm/insn.= h > index d562b2b40ba1..d0e137f9bcd7 100644 > --- a/arch/riscv/include/asm/insn.h > +++ b/arch/riscv/include/asm/insn.h > @@ -514,78 +514,4 @@ static __always_inline bool riscv_insn_is_branch(u32= code) > > #define RVV_EXTRACT_VL_VS_WIDTH(x) RVFDQ_EXTRACT_FL_FS_WIDTH(x) > > -/* > - * Get the immediate from a J-type instruction. > - * > - * @insn: instruction to process > - * Return: immediate > - */ > -static inline s32 riscv_insn_extract_jtype_imm(u32 insn) > -{ > - return RV_EXTRACT_JTYPE_IMM(insn); > -} > - > -/* > - * Update a J-type instruction with an immediate value. > - * > - * @insn: pointer to the jtype instruction > - * @imm: the immediate to insert into the instruction > - */ > -static inline void riscv_insn_insert_jtype_imm(u32 *insn, s32 imm) > -{ > - /* drop the old IMMs, all jal IMM bits sit at 31:12 */ > - *insn &=3D ~GENMASK(31, 12); > - *insn |=3D (RV_X_MASK(imm, RV_J_IMM_10_1_OFF, RV_J_IMM_10_1_MASK)= << RV_J_IMM_10_1_OPOFF) | > - (RV_X_MASK(imm, RV_J_IMM_11_OFF, RV_J_IMM_11_MASK) << RV= _J_IMM_11_OPOFF) | > - (RV_X_MASK(imm, RV_J_IMM_19_12_OFF, RV_J_IMM_19_12_MASK)= << RV_J_IMM_19_12_OPOFF) | > - (RV_X_MASK(imm, RV_J_IMM_SIGN_OFF, 1) << RV_J_IMM_SIGN_O= POFF); > -} > - > -/* > - * Put together one immediate from a U-type and I-type instruction pair. > - * > - * The U-type contains an upper immediate, meaning bits[31:12] with [11:= 0] > - * being zero, while the I-type contains a 12bit immediate. > - * Combined these can encode larger 32bit values and are used for exampl= e > - * in auipc + jalr pairs to allow larger jumps. > - * > - * @utype_insn: instruction containing the upper immediate > - * @itype_insn: instruction > - * Return: combined immediate > - */ > -static inline s32 riscv_insn_extract_utype_itype_imm(u32 utype_insn, u32= itype_insn) > -{ > - s32 imm; > - > - imm =3D RV_EXTRACT_UTYPE_IMM(utype_insn); > - imm +=3D RV_EXTRACT_ITYPE_IMM(itype_insn); > - > - return imm; > -} > - > -/* > - * Update a set of two instructions (U-type + I-type) with an immediate = value. > - * > - * Used for example in auipc+jalrs pairs the U-type instructions contain= s > - * a 20bit upper immediate representing bits[31:12], while the I-type > - * instruction contains a 12bit immediate representing bits[11:0]. > - * > - * This also takes into account that both separate immediates are > - * considered as signed values, so if the I-type immediate becomes > - * negative (BIT(11) set) the U-type part gets adjusted. > - * > - * @utype_insn: pointer to the utype instruction of the pair > - * @itype_insn: pointer to the itype instruction of the pair > - * @imm: the immediate to insert into the two instructions > - */ > -static inline void riscv_insn_insert_utype_itype_imm(u32 *utype_insn, u3= 2 *itype_insn, s32 imm) > -{ > - /* drop possible old IMM values */ > - *utype_insn &=3D ~(RV_U_IMM_31_12_MASK); > - *itype_insn &=3D ~(RV_I_IMM_11_0_MASK << RV_I_IMM_11_0_OPOFF); > - > - /* add the adapted IMMs */ > - *utype_insn |=3D (imm & RV_U_IMM_31_12_MASK) + ((imm & BIT(11)) <= < 1); > - *itype_insn |=3D ((imm & RV_I_IMM_11_0_MASK) << RV_I_IMM_11_0_OPO= FF); > -} > #endif /* _ASM_RISCV_INSN_H */ > diff --git a/arch/riscv/kernel/alternative.c b/arch/riscv/kernel/alternat= ive.c > index 7642704c7f18..b26a90eb65cc 100644 > --- a/arch/riscv/kernel/alternative.c > +++ b/arch/riscv/kernel/alternative.c > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -78,14 +79,24 @@ static void riscv_alternative_fix_auipc_jalr(void *pt= r, u32 auipc_insn, > u32 jalr_insn, int patch_off= set) > { > u32 call[2] =3D { auipc_insn, jalr_insn }; > + u32 auipc_imm; > s32 imm; > > /* get and adjust new target address */ > - imm =3D riscv_insn_extract_utype_itype_imm(auipc_insn, jalr_insn)= ; > + imm =3D riscv_insn_auipc_extract_imm(auipc_insn) + riscv_insn_jal= r_extract_imm(jalr_insn); > imm -=3D patch_offset; > > + /* > + * When the 32-bit immediate is split across auipc and jalr, the > + * constructed immediates need to be treated as individually sign > + * extended numbers. Add the sign bit of the lower 12 bits to the= upper > + * 20 bits to undo the bleeding of the sign. > + */ > + auipc_imm =3D imm + (BIT(11) << 1); > + > /* update instructions */ > - riscv_insn_insert_utype_itype_imm(&call[0], &call[1], imm); > + riscv_insn_auipc_insert_imm(&call[0], auipc_imm); > + riscv_insn_jalr_insert_imm(&call[1], imm); > > /* patch the call place again */ > patch_text_nosync(ptr, call, sizeof(u32) * 2); > @@ -96,11 +107,11 @@ static void riscv_alternative_fix_jal(void *ptr, u32= jal_insn, int patch_offset) > s32 imm; > > /* get and adjust new target address */ > - imm =3D riscv_insn_extract_jtype_imm(jal_insn); > + imm =3D riscv_insn_jal_extract_imm(jal_insn); > imm -=3D patch_offset; > > /* update instruction */ > - riscv_insn_insert_jtype_imm(&jal_insn, imm); > + riscv_insn_jal_insert_imm(&jal_insn, imm); > > /* patch the call place again */ > patch_text_nosync(ptr, &jal_insn, sizeof(u32)); > @@ -127,7 +138,7 @@ void riscv_alternative_fix_offsets(void *alt_ptr, uns= igned int len, > continue; > > /* if instruction pair is a call, it will use the= ra register */ > - if (RV_EXTRACT_RD_REG(insn) !=3D 1) > + if (riscv_insn_jalr_extract_xd(insn) !=3D 1) > continue; > > riscv_alternative_fix_auipc_jalr(alt_ptr + i * si= zeof(u32), > @@ -136,7 +147,7 @@ void riscv_alternative_fix_offsets(void *alt_ptr, uns= igned int len, > } > > if (riscv_insn_is_jal(insn)) { > - s32 imm =3D riscv_insn_extract_jtype_imm(insn); > + s32 imm =3D riscv_insn_jal_extract_imm(insn); > > /* Don't modify jumps inside the alternative bloc= k */ > if ((alt_ptr + i * sizeof(u32) + imm) >=3D alt_pt= r && > > -- > 2.54.0 > > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv >