All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Borislav Petkov <bp@alien8.de>
Cc: x86@kernel.org, linux-kernel@vger.kernel.org,
	David.Kaplan@amd.com, Andrew.Cooper3@citrix.com,
	jpoimboe@kernel.org, gregkh@linuxfoundation.org,
	nik.borisov@suse.com
Subject: Re: [PATCH v2 10/11] x86/alternatives: Simplify ALTERNATIVE_n()
Date: Thu, 7 Sep 2023 13:09:17 +0200	[thread overview]
Message-ID: <20230907110917.GA10955@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20230907083158.GBZPmKfjarnaQk1ofB@fat_crate.local>

On Thu, Sep 07, 2023 at 10:31:58AM +0200, Borislav Petkov wrote:
> On Mon, Aug 14, 2023 at 01:44:36PM +0200, Peter Zijlstra wrote:
> > Instead of making increasingly complicated ALTERNATIVE_n()
> > implementations, use a nested alternative expression.
> > 
> > The only difference between:
> > 
> >   ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2)
> > 
> > and
> > 
> >   ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1),
> >               newinst2, flag2)
> 
> Hmm, one more problem I see with this. You're handling it, it seems, but
> the whole thing doesn't feel clean to me.
> 
> Here's an exemplary eval:
> 
> > #APP
> > # 53 "./arch/x86/include/asm/page_64.h" 1
> > 	# ALT: oldnstr
> > 661:
> > 	# ALT: oldnstr
> > 661:
> 
> <--- X
> 
> > 	call clear_page_orig	#
> > 662:
> > # ALT: padding
> > .skip -(((665f-664f)-(662b-661b)) > 0) * ((665f-664f)-(662b-661b)),0x90

665f-664f = 5 (rep)
662b-661b = 5 (orig)

5-5 > 0 = 0

so no padding

> > 663:
> > .pushsection .altinstructions,"a"
> >  .long 661b - .
> >  .long 664f - .
> >  .4byte ( 3*32+16)
> >  .byte 663b-661b
> >  .byte 665f-664f
> > .popsection
> > .pushsection .altinstr_replacement, "ax"
> > # ALT: replacement 
> > 664:
> > 	call clear_page_rep	#
> >  665:
> > .popsection
> > 
> > 662:
> > # ALT: padding
> > .skip -(((665f-664f)-(662b-661b)) > 0) * ((665f-664f)-(662b-661b)),0x90
> > 663:
> 
> <--- Z
> 
> So here it would add the padding again, unnecessarily.

665f-664f = 5 (erms)
662b-661b = 5 (orig + padding)

5-5 > 0 = 0

no padding, also since, as you note 661b is the first, we include all
previous padding, and the skip will only add additional padding if the
new sequence is longer still.

So, no I'm not seeing it. Doubly not with this example where all 3
variants are 5 bytes.

Notably, the following nonsense alternative with 1 2 and 3 bytes
instructions:

	asm volatile (
		ALTERNATIVE_2("push %rbp",
			"push %r12", X86_FEATURE_ALWAYS,
			"mov %rsp,%rbp", X86_FEATURE_ALWAYS));

ends up as:

0004  204:      55                      push   %rbp
0005  205:      90                      nop
0006  206:      90                      nop

If you flip the 3 and 2 byte instructions the result is the same. No
extra padding.

And no, I had not actually tested this before, because clearly this is
all obvious ;-)

Anyway, the 1,3,2 variant spelled out reads like:

#APP
# 1563 "../arch/x86/kernel/alternative.c" 1
# ALT: oldnstr
661:
# ALT: oldnstr
661:
push %rbp
662:
# ALT: padding
.skip -(((665f-664f)-(662b-661b)) > 0) * ((665f-664f)-(662b-661b)),0x90

 #   Which evaluates like:
 #     665f-664f = 3
 #     662b-661b = 1
 #     3-1 > 0 = -1
 #     --1 * (3-1) = 2
 #
 #   so two single byte nops get emitted here.

663:
.pushsection .altinstructions,"a"
.long 661b - .
.long 664f - .
.4byte ( 3*32+21)
.byte 663b-661b
.byte 665f-664f
.popsection
.pushsection .altinstr_replacement, "ax"
# ALT: replacement
664:
mov %rsp,%rbp
665:
.popsection

662:
# ALT: padding
.skip -(((665f-664f)-(662b-661b)) > 0) * ((665f-664f)-(662b-661b)),0x90

 #   And this evaluates to:
 #     665f-664f = 2
 #     662b-661b = 3 (because it includes the original 1 byte instruction and 2 bytes padding)
 #     3-1 > 0 = 0
 #     0 * (3-1) = 0
 #
 #   so no extra padding

663:
.pushsection .altinstructions,"a"
.long 661b - .
.long 664f - .
.4byte ( 3*32+21)
.byte 663b-661b
.byte 665f-664f
.popsection
.pushsection .altinstr_replacement, "ax"
# ALT: replacement
664:
push %r12
665:
.popsection

# 0 "" 2
# ../arch/x86/kernel/alternative.c:1569:        int3_selftest();
#NO_APP

  reply	other threads:[~2023-09-07 15:40 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-14 11:44 [PATCH v2 00/11] Fix up SRSO stuff Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 01/11] x86/cpu: Fixup __x86_return_thunk Peter Zijlstra
2023-08-16  7:55   ` [tip: x86/urgent] x86/cpu: Fix __x86_return_thunk symbol type tip-bot2 for Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 02/11] x86/cpu: Fix up srso_safe_ret() and __x86_return_thunk() Peter Zijlstra
2023-08-16  7:55   ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 03/11] objtool/x86: Fix SRSO mess Peter Zijlstra
2023-08-14 12:54   ` Andrew.Cooper3
2023-08-16  7:55   ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2023-08-16 11:59     ` Peter Zijlstra
2023-08-16 20:31       ` Josh Poimboeuf
2023-08-16 22:08         ` [PATCH] objtool/x86: Fixup frame-pointer vs rethunk Peter Zijlstra
2023-08-16 22:22           ` Josh Poimboeuf
2023-08-17  8:39       ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 04/11] x86/alternative: Make custom return thunk unconditional Peter Zijlstra
2023-08-16  7:55   ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 05/11] x86/cpu: Clean up SRSO return thunk mess Peter Zijlstra
2023-08-14 13:02   ` Borislav Petkov
2023-08-14 17:48   ` Borislav Petkov
2023-08-15 21:29   ` Nathan Chancellor
2023-08-15 22:43     ` Peter Zijlstra
2023-08-16  7:38       ` Borislav Petkov
2023-08-16 14:52         ` Nathan Chancellor
2023-08-16 15:08           ` Borislav Petkov
2023-08-16  7:55   ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2023-08-16 18:58     ` Nathan Chancellor
2023-08-16 19:24       ` Borislav Petkov
2023-08-16 19:30         ` Nathan Chancellor
2023-08-16 19:42           ` Borislav Petkov
2023-08-16 19:57             ` Borislav Petkov
2023-08-16 21:20   ` tip-bot2 for Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 06/11] x86/cpu: Rename original retbleed methods Peter Zijlstra
2023-08-14 19:41   ` Josh Poimboeuf
2023-08-16  7:55   ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2023-08-16 21:20   ` tip-bot2 for Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 07/11] x86/cpu: Rename srso_(.*)_alias to srso_alias_\1 Peter Zijlstra
2023-08-16  7:55   ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2023-08-16 21:20   ` tip-bot2 for Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 08/11] x86/cpu: Cleanup the untrain mess Peter Zijlstra
2023-08-16  7:55   ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2023-08-16 21:20   ` tip-bot2 for Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 09/11] x86/cpu/kvm: Provide UNTRAIN_RET_VM Peter Zijlstra
2023-08-16  7:55   ` [tip: x86/urgent] " tip-bot2 for Peter Zijlstra
2023-08-16 21:20   ` tip-bot2 for Peter Zijlstra
2023-08-14 11:44 ` [PATCH v2 10/11] x86/alternatives: Simplify ALTERNATIVE_n() Peter Zijlstra
2023-08-15 20:49   ` Nikolay Borisov
2023-08-15 22:44     ` Peter Zijlstra
2023-09-07  8:31   ` Borislav Petkov
2023-09-07 11:09     ` Peter Zijlstra [this message]
2023-09-07 11:11       ` Peter Zijlstra
2023-09-07 11:16         ` Peter Zijlstra
2023-09-07 15:06       ` Borislav Petkov
2023-09-07 15:30         ` Borislav Petkov
2023-09-09  7:50           ` Borislav Petkov
2023-09-09  9:25             ` Peter Zijlstra
2023-09-09  9:42               ` Peter Zijlstra
2023-09-10 14:42               ` Borislav Petkov
2023-09-12  9:27                 ` Peter Zijlstra
2023-09-12  9:44                   ` Peter Zijlstra
2023-09-13  4:37                     ` Borislav Petkov
2023-09-13  8:46                       ` Peter Zijlstra
2023-09-13 14:38                         ` Borislav Petkov
2023-09-13 16:14                           ` Peter Zijlstra
2023-09-15  7:46                           ` Peter Zijlstra
2023-09-15  7:51                             ` Peter Zijlstra
2023-09-15 12:05                               ` Borislav Petkov
2023-09-13  4:24                   ` Borislav Petkov
2023-08-14 11:44 ` [PATCH v2 11/11] x86/cpu: Use fancy alternatives to get rid of entry_untrain_ret() Peter Zijlstra
2023-08-14 16:44 ` [PATCH v2 00/11] Fix up SRSO stuff Borislav Petkov
2023-08-14 19:51   ` Josh Poimboeuf
2023-08-14 19:57     ` Borislav Petkov
2023-08-14 20:01     ` Josh Poimboeuf
2023-08-14 20:09       ` Borislav Petkov
2023-08-15 14:26         ` [PATCH] x86/srso: Explain the untraining sequences a bit more Borislav Petkov
2023-08-15 15:41           ` Nikolay Borisov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230907110917.GA10955@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=Andrew.Cooper3@citrix.com \
    --cc=David.Kaplan@amd.com \
    --cc=bp@alien8.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nik.borisov@suse.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.