Re: [PATCH v3 3/3] x86/vmscape: Remove LFENCE from BHB clearing long loop
From: Dave Hansen
Date: Mon Nov 03 2025 - 15:45:49 EST
On 10/27/25 16:43, Pawan Gupta wrote:
> Long loop is used to clear the branch history when switching from a guest
> to host userspace. The LFENCE barrier is not required in this case as ring
> transition itself acts as a barrier.
>
> Move the prologue, LFENCE and epilogue out of __CLEAR_BHB_LOOP macro to
> allow skipping the LFENCE in the long loop variant. Rename the long loop
> function to clear_bhb_long_loop_no_barrier() to reflect the change.
Too. Much. Assembly.
Is there a reason we can't do more of this in C? Can we have _one_
assembly function, please? One that takes the loop counts? No macros, no
duplication functions. Just one:
void __clear_bhb_loop(int inner, int outer);
Then we have sensible code that looks like this:
void clear_bhb_loop()
{
__clear_bhb_loop(inner, outer);
lfence();
}
void clear_bhb_loop_nofence()
{
__clear_bhb_loop(inner, outer);
}
We don't need a short and a long *version*. We just have one function
(or pair of functions) that gets called that works everywhere.
Actually, if you just used global variables and called the assembly one:
extern void clear_bhb_loop_nofence();
then the other implementation would just be:
void clear_bhb_loop()
{
__clear_bhb_loop(inner, outer);
lfence();
}
Then we have *ONE* assembly function instead of four.
Right? What am I missing?
Does the LFENCE *need* to be before that last pop and RET?