[PATCH v3 6/8] crypto: x86/crc32c - Use idiomatic relative jump table

Ard Biesheuvel posted 8 patches 1 month, 2 weeks ago
[PATCH v3 6/8] crypto: x86/crc32c - Use idiomatic relative jump table
Posted by Ard Biesheuvel 1 month, 2 weeks ago
From: Ard Biesheuvel <ardb@kernel.org>

The original crc32c code used a place-relative jump table but with a
slightly awkward use of two separate symbols. To help objtool, this was
replaced with a bog-standard position dependent jump table call, which
was subsequently tweaked to use a RIP-relative reference to the table,
but still populate it with absolute 64-bit references.

Given that objtool will need to be taught about the jump table idiom
that compilers use when running with -fpie enabled, let's update the
jump table in the crc32c code once again to use this standard idiom,
where the jump table carries 32-bit references relative to the start of
the table, and the destination address can be obtained by adding the
two.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/x86/crypto/crc32c-pcl-intel-asm_64.S | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S
index bbcff1fb78cb..45b005935194 100644
--- a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S
+++ b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S
@@ -53,7 +53,7 @@
 .endm
 
 .macro JMPTBL_ENTRY i
-.quad .Lcrc_\i
+.long .Lcrc_\i - jump_table
 .endm
 
 .macro JNC_LESS_THAN j
@@ -169,7 +169,8 @@ SYM_FUNC_START(crc_pcl)
 
 	## branch into array
 	leaq	jump_table(%rip), %bufp
-	mov	(%bufp,%rax,8), %bufp
+	movslq	(%bufp,%rax,4), len
+	addq	len, %bufp
 	JMP_NOSPEC bufp
 
 	################################################################
-- 
2.47.0.rc1.288.g06298d1525-goog
Re: [PATCH v3 6/8] crypto: x86/crc32c - Use idiomatic relative jump table
Posted by Eric Biggers 1 month, 2 weeks ago
On Fri, Oct 11, 2024 at 07:08:54PM +0200, Ard Biesheuvel wrote:
> diff --git a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S
> index bbcff1fb78cb..45b005935194 100644
> --- a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S
> +++ b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S
> @@ -53,7 +53,7 @@
>  .endm
>  
>  .macro JMPTBL_ENTRY i
> -.quad .Lcrc_\i
> +.long .Lcrc_\i - jump_table
>  .endm
>  
>  .macro JNC_LESS_THAN j
> @@ -169,7 +169,8 @@ SYM_FUNC_START(crc_pcl)
>  
>  	## branch into array
>  	leaq	jump_table(%rip), %bufp
> -	mov	(%bufp,%rax,8), %bufp
> +	movslq	(%bufp,%rax,4), len
> +	addq	len, %bufp
>  	JMP_NOSPEC bufp

I think it would be much better to just get rid of the jump table here.

I have done this at
https://lore.kernel.org/linux-crypto/20241014042447.50197-4-ebiggers@kernel.org/

- Eric
Re: [PATCH v3 6/8] crypto: x86/crc32c - Use idiomatic relative jump table
Posted by Ard Biesheuvel 1 month, 1 week ago
On Mon, 14 Oct 2024 at 06:28, Eric Biggers <ebiggers@kernel.org> wrote:
>
> On Fri, Oct 11, 2024 at 07:08:54PM +0200, Ard Biesheuvel wrote:
> > diff --git a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S
> > index bbcff1fb78cb..45b005935194 100644
> > --- a/arch/x86/crypto/crc32c-pcl-intel-asm_64.S
> > +++ b/arch/x86/crypto/crc32c-pcl-intel-asm_64.S
> > @@ -53,7 +53,7 @@
> >  .endm
> >
> >  .macro JMPTBL_ENTRY i
> > -.quad .Lcrc_\i
> > +.long .Lcrc_\i - jump_table
> >  .endm
> >
> >  .macro JNC_LESS_THAN j
> > @@ -169,7 +169,8 @@ SYM_FUNC_START(crc_pcl)
> >
> >       ## branch into array
> >       leaq    jump_table(%rip), %bufp
> > -     mov     (%bufp,%rax,8), %bufp
> > +     movslq  (%bufp,%rax,4), len
> > +     addq    len, %bufp
> >       JMP_NOSPEC bufp
>
> I think it would be much better to just get rid of the jump table here.
>

Good point.

> I have done this at
> https://lore.kernel.org/linux-crypto/20241014042447.50197-4-ebiggers@kernel.org/
>

I'll go and take a look - thanks.