Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-07 Thread George Dunlap
On Fri, Sep 4, 2026 at 11:34 AM Roger Pau Monné  wrote:
>
> On Fri, Sep 04, 2026 at 09:50:02AM +0100, George Dunlap wrote:
> > On Fri, Sep 4, 2026 at 9:29 AM Jan Beulich  wrote:
> > > > your position here is really inconsistent:  You wave
> > > > away a partial pagetable walk with three map/unmap operations on the
> > > > context switch path as something we'll have to do in the interim, and
> > > > can optimize later, but are now threatening to make me add in
> > > > special-case codepaths and run tests to save a few memory reads and
> > > > shifts.
> > >
> > > I think you misunderstood. There was a concern raised already on v1,
> > > and that concern wasn't covered by the patch description. In my initial
> > > reply I said "Functionally the change looks okay to me" for a reason,
> > > after all.
> >
> > To quote Andy's mail:
> >
> > <<<
> >
> > So what this patch is doing is still keeping the double copy (the
> > fragility) but reintroducing the expensive part of the operation into
> > the context switch path.  If you can't keep it being L1e, there's
> > probably no point keeping the optimisation at all.
> >
> > >>>
> >
> > Basically what I took from this is;
> >
> > - Andy thinks stashing any intermediate form (whether L1E or MFN) has
> > a technical cost (two copies that could potentially go out of sync,
> > thus "fragility")
> >
> > - Andy thinks that the expensive part of the conversion is the MFN ->
> > L1E conversion, not the vaddr -> MFN conversion
>
> We later discussed this, and the assumption was that the expensive
> part was the vaddr -> MFN translation, as that's where PDX is
> involved.  I expect crafting a PTE shouldn't be expensive at all, but
> maybe there's something I'm missing here.
>
> The original patch cached the MFN in an attempt to not remove the
> optimization, because my understanding was that the possible expensive
> part was the PDX translation.  Then again I don't have real
> measurements to back up any of the claims above, and hence it might
> all be plain wrong.

I ran some tests on my NUC.  Measuring cycles for *just*
update_xen_slot_in_full_gdt(), using the previous version (stashed
xenheap pointer + stashed l1e), and then mapcache walk with {stashed
mfn, full conversion} x {default, PDX forced on}.

xenheap+l1e: 38 cycles
mapcache walk, stashed mfn (v1), no PDX: 98 cycles
mapcache walk, full convervion, no PDX: 97 cycles
mapcache walk, stashed mfn (v1), PDX forced on: 128 cycles
mapcache walk, full conversion: 122 cycles

The variance is pretty high, so basically the mfn stashing didn't have
any statistically significant effect.

I'll leave it doing the full conversion for now.

 -George



Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-04 Thread Jürgen Groß

On 04.09.26 08:00, Jan Beulich wrote:

On 04.09.2026 00:35, George Dunlap wrote:

On Thu, Sep 3, 2026 at 5:11 PM Jan Beulich  wrote:


On 02.09.2026 11:43, George Dunlap wrote:

From: Roger Pau Monné 

Currently, update_xen_slot_in_full_gdt() uses the stashed direct-map
pointer in d->arch.pv.gdt_ldt_l1tab to update the incoming vcpu's
page tables with Xen's GDT, by writing a stashed per-cpu copy of a
pre-baked L1 entry (either 64-bit or compat version).

Switch this to using populate_perdomain_mapping(), which doesn't rely
on the stashed address of the l1 page in the direct map.  Rather than
also stashing a pre-baked value for the payload, compute the mfn from
the per-cpu GDT pointer at use: the conversion is a handful of cycles
on a path costing thousands, and computing at use removes the
parallel {,compat_}gdt_l1e bookkeeping along with its boot-ordering
constraint (the cached value could only be generated after Xen's
physical relocation, and had to be in place before the first context
switch; a use-time lookup is correct by construction).  The flags on
the final mapping are identical.

Signed-off-by: Roger Pau Monné 
Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8
Signed-off-by: George Dunlap 
---
Changes in v2:
- Drop the {,compat_}gdt_mfn caching entirely (suggested by Andrew
Cooper): compute virt_to_mfn() from the per-cpu GDT pointer at use.
The PDX lookup behind it measures ~5-10 cycles warm against a
~1,500-cycle context switch, and this removes the double
bookkeeping and the after-relocation caching constraint.  The
cached-MFN assertion goes with the cache: a use-time computation
from a live pointer needs no staleness check.


This looks to contradict what 564d261687c0 ("x86/ctxt-switch: Document
and improve GDT handling") used as justification to put in place the
caching. Also Cc-ing Jürgen, who also was involved there, for possible
further insight.

Functionally the change looks okay to me, but the above will need
sorting, at the very least by specifically discussing why effectively
undoing that earlier change is okay.


So looking back at the thread, Jürgen measured a 14% improvement for
something that might be described as a microbenchmark before and after
the patch (a benchmark purposely trying to set up an unusual scenario
to maximize the effect of context switch overhead, not one to
represent a typical workflow).  But are the numbers really plausible?
Even at an implausible 100k switches/s across the box, saving 100
cycles per switch is about 0.04% of eight 3 GHz cores.

At any rate, we're already adding several map/unmap operations, and
about to add several more.  Keeping the PTE caching would require
adding a separate path that can write just PTEs, which then will
potentially further complication future paths where we need to make
sure we handle both domain-wide perdomain areas and per-vcpu areas.
If it were easy I would already have been keeping it.

I'd be inclined to say: Since we're going to be adding more
populate_perdomain_mapping() calls anyway, let's do it the simple
correct way first; and then explore the idea of stashing mfns of
frequently-mapped L1s (rather than having to walk L3 -> L2 -> L1); and
at that time look into stashing baked l1es to avoid conversions.


Perhaps; I'd like to have Jürgen's and/or Andrew's input here, though.


At that time I implemented core scheduling in Xen. I noticed that very
subtle changes in the context switch path could result in unexpected large
performance differences. As I had the performance test for my purpose
already set up, I used it for Andrew's patch (which was a result of my
context switch path performance findings) and really did measure the
impressive effect of it.

Note that you can't only count instructions, often cache effects and
branch predictions are dominating the performance.


Juergen


OpenPGP_0xB0DE9DD628BF132F.asc
Description: OpenPGP public key


OpenPGP_signature.asc
Description: OpenPGP digital signature


Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-04 Thread Roger Pau Monné
On Fri, Sep 04, 2026 at 09:50:02AM +0100, George Dunlap wrote:
> On Fri, Sep 4, 2026 at 9:29 AM Jan Beulich  wrote:
> > > your position here is really inconsistent:  You wave
> > > away a partial pagetable walk with three map/unmap operations on the
> > > context switch path as something we'll have to do in the interim, and
> > > can optimize later, but are now threatening to make me add in
> > > special-case codepaths and run tests to save a few memory reads and
> > > shifts.
> >
> > I think you misunderstood. There was a concern raised already on v1,
> > and that concern wasn't covered by the patch description. In my initial
> > reply I said "Functionally the change looks okay to me" for a reason,
> > after all.
> 
> To quote Andy's mail:
> 
> <<<
> 
> So what this patch is doing is still keeping the double copy (the
> fragility) but reintroducing the expensive part of the operation into
> the context switch path.  If you can't keep it being L1e, there's
> probably no point keeping the optimisation at all.
> 
> >>>
> 
> Basically what I took from this is;
> 
> - Andy thinks stashing any intermediate form (whether L1E or MFN) has
> a technical cost (two copies that could potentially go out of sync,
> thus "fragility")
> 
> - Andy thinks that the expensive part of the conversion is the MFN ->
> L1E conversion, not the vaddr -> MFN conversion

We later discussed this, and the assumption was that the expensive
part was the vaddr -> MFN translation, as that's where PDX is
involved.  I expect crafting a PTE shouldn't be expensive at all, but
maybe there's something I'm missing here.

The original patch cached the MFN in an attempt to not remove the
optimization, because my understanding was that the possible expensive
part was the PDX translation.  Then again I don't have real
measurements to back up any of the claims above, and hence it might
all be plain wrong.

Regards, Roger.



Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-04 Thread Jan Beulich
On 04.09.2026 10:50, George Dunlap wrote:
> On Fri, Sep 4, 2026 at 9:29 AM Jan Beulich  wrote:
>>> your position here is really inconsistent:  You wave
>>> away a partial pagetable walk with three map/unmap operations on the
>>> context switch path as something we'll have to do in the interim, and
>>> can optimize later, but are now threatening to make me add in
>>> special-case codepaths and run tests to save a few memory reads and
>>> shifts.
>>
>> I think you misunderstood. There was a concern raised already on v1,
>> and that concern wasn't covered by the patch description. In my initial
>> reply I said "Functionally the change looks okay to me" for a reason,
>> after all.
> 
> To quote Andy's mail:
> 
> <<<
> 
> So what this patch is doing is still keeping the double copy (the
> fragility) but reintroducing the expensive part of the operation into
> the context switch path.  If you can't keep it being L1e, there's
> probably no point keeping the optimisation at all.
> 

> 
> Basically what I took from this is;
> 
> - Andy thinks stashing any intermediate form (whether L1E or MFN) has
> a technical cost (two copies that could potentially go out of sync,
> thus "fragility")
> 
> - Andy thinks that the expensive part of the conversion is the MFN ->
> L1E conversion, not the vaddr -> MFN conversion

Iirc later, when discussing with me and Roger, this was somewhat adjusted.
Unfortunately the outcome of that discussion wasn't put in a reply there.

> - So, stashing the L1E might be a win, but stashing the MFN is unlikely to be.
> 
> - If we're not going to special-case this path, we have to pass an
> MFN; and if we're going to pass an MFN, it's probably better to just
> to get rid of the stashing; the extra fragility introduced doesn't pay
> for itself in terms of potential performance improvement.
> 
> Note also that by the end of the series, we add two more
> populate_perdomain_mapping() calls to the context switch path, at
> least for ASI domains, which means another two of the "expensive" MFN
> -> L1E conversions.
> 
> So v2 is doing what I understood Andy to have suggested.  I agree the
> meaning isn't 100% clear, though, so I may have misunderstood him.
> 
> As I've said, I'm not opposed to optimizing this path once we have the
> final form functional and have measured it.  Mapping the three tables
> we need to modify in vmap, and stashing both the addresses and
> pre-baked l1es, sounds like a perfectly reasonable thing to do,
> *after* we get things functional and have had a chance to measure the
> new context switch in its entirety.

And I (largely) agree. What I'm asking for (beyond feedback from those
who were involved in putting in the optimization) is that the removal
of that optimization be justified against the original commit's
reasoning.

Jan



Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-04 Thread George Dunlap
On Fri, Sep 4, 2026 at 7:54 AM Jürgen Groß  wrote:
>
> On 04.09.26 08:00, Jan Beulich wrote:
> > On 04.09.2026 00:35, George Dunlap wrote:
> >> On Thu, Sep 3, 2026 at 5:11 PM Jan Beulich  wrote:
> >>>
> >>> On 02.09.2026 11:43, George Dunlap wrote:
>  From: Roger Pau Monné 
> 
>  Currently, update_xen_slot_in_full_gdt() uses the stashed direct-map
>  pointer in d->arch.pv.gdt_ldt_l1tab to update the incoming vcpu's
>  page tables with Xen's GDT, by writing a stashed per-cpu copy of a
>  pre-baked L1 entry (either 64-bit or compat version).
> 
>  Switch this to using populate_perdomain_mapping(), which doesn't rely
>  on the stashed address of the l1 page in the direct map.  Rather than
>  also stashing a pre-baked value for the payload, compute the mfn from
>  the per-cpu GDT pointer at use: the conversion is a handful of cycles
>  on a path costing thousands, and computing at use removes the
>  parallel {,compat_}gdt_l1e bookkeeping along with its boot-ordering
>  constraint (the cached value could only be generated after Xen's
>  physical relocation, and had to be in place before the first context
>  switch; a use-time lookup is correct by construction).  The flags on
>  the final mapping are identical.
> 
>  Signed-off-by: Roger Pau Monné 
>  Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8
>  Signed-off-by: George Dunlap 
>  ---
>  Changes in v2:
>  - Drop the {,compat_}gdt_mfn caching entirely (suggested by Andrew
>  Cooper): compute virt_to_mfn() from the per-cpu GDT pointer at use.
>  The PDX lookup behind it measures ~5-10 cycles warm against a
>  ~1,500-cycle context switch, and this removes the double
>  bookkeeping and the after-relocation caching constraint.  The
>  cached-MFN assertion goes with the cache: a use-time computation
>  from a live pointer needs no staleness check.
> >>>
> >>> This looks to contradict what 564d261687c0 ("x86/ctxt-switch: Document
> >>> and improve GDT handling") used as justification to put in place the
> >>> caching. Also Cc-ing Jürgen, who also was involved there, for possible
> >>> further insight.
> >>>
> >>> Functionally the change looks okay to me, but the above will need
> >>> sorting, at the very least by specifically discussing why effectively
> >>> undoing that earlier change is okay.
> >>
> >> So looking back at the thread, Jürgen measured a 14% improvement for
> >> something that might be described as a microbenchmark before and after
> >> the patch (a benchmark purposely trying to set up an unusual scenario
> >> to maximize the effect of context switch overhead, not one to
> >> represent a typical workflow).  But are the numbers really plausible?
> >> Even at an implausible 100k switches/s across the box, saving 100
> >> cycles per switch is about 0.04% of eight 3 GHz cores.
> >>
> >> At any rate, we're already adding several map/unmap operations, and
> >> about to add several more.  Keeping the PTE caching would require
> >> adding a separate path that can write just PTEs, which then will
> >> potentially further complication future paths where we need to make
> >> sure we handle both domain-wide perdomain areas and per-vcpu areas.
> >> If it were easy I would already have been keeping it.
> >>
> >> I'd be inclined to say: Since we're going to be adding more
> >> populate_perdomain_mapping() calls anyway, let's do it the simple
> >> correct way first; and then explore the idea of stashing mfns of
> >> frequently-mapped L1s (rather than having to walk L3 -> L2 -> L1); and
> >> at that time look into stashing baked l1es to avoid conversions.
> >
> > Perhaps; I'd like to have Jürgen's and/or Andrew's input here, though.
>
> At that time I implemented core scheduling in Xen. I noticed that very
> subtle changes in the context switch path could result in unexpected large
> performance differences. As I had the performance test for my purpose
> already set up, I used it for Andrew's patch (which was a result of my
> context switch path performance findings) and really did measure the
> impressive effect of it.
>
> Note that you can't only count instructions, often cache effects and
> branch predictions are dominating the performance.

Right, but:

1. That's going to be very much hardware- and workload- dependent.
Even on the same hardware, if you'd made a slight change in the
workload, you might have seen a very different result; and on
different hardware you're going to see something different again

2. As I said, we're now adding two extra map / unmaps, which is going
to perturb everything again.

If anything, your argument says we should wait until we've stopped
modifying the context switch path (which won't happen until patch 49
at least, guessing from the patch titles), and then measure things
again to see what's actually slow.

I'm sorry Jan, your position here is really inconsistent:  You 

Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-04 Thread George Dunlap
On Fri, Sep 4, 2026 at 9:29 AM Jan Beulich  wrote:
> > your position here is really inconsistent:  You wave
> > away a partial pagetable walk with three map/unmap operations on the
> > context switch path as something we'll have to do in the interim, and
> > can optimize later, but are now threatening to make me add in
> > special-case codepaths and run tests to save a few memory reads and
> > shifts.
>
> I think you misunderstood. There was a concern raised already on v1,
> and that concern wasn't covered by the patch description. In my initial
> reply I said "Functionally the change looks okay to me" for a reason,
> after all.

To quote Andy's mail:

<<<

So what this patch is doing is still keeping the double copy (the
fragility) but reintroducing the expensive part of the operation into
the context switch path.  If you can't keep it being L1e, there's
probably no point keeping the optimisation at all.

>>>

Basically what I took from this is;

- Andy thinks stashing any intermediate form (whether L1E or MFN) has
a technical cost (two copies that could potentially go out of sync,
thus "fragility")

- Andy thinks that the expensive part of the conversion is the MFN ->
L1E conversion, not the vaddr -> MFN conversion

- So, stashing the L1E might be a win, but stashing the MFN is unlikely to be.

- If we're not going to special-case this path, we have to pass an
MFN; and if we're going to pass an MFN, it's probably better to just
to get rid of the stashing; the extra fragility introduced doesn't pay
for itself in terms of potential performance improvement.

Note also that by the end of the series, we add two more
populate_perdomain_mapping() calls to the context switch path, at
least for ASI domains, which means another two of the "expensive" MFN
-> L1E conversions.

So v2 is doing what I understood Andy to have suggested.  I agree the
meaning isn't 100% clear, though, so I may have misunderstood him.

As I've said, I'm not opposed to optimizing this path once we have the
final form functional and have measured it.  Mapping the three tables
we need to modify in vmap, and stashing both the addresses and
pre-baked l1es, sounds like a perfectly reasonable thing to do,
*after* we get things functional and have had a chance to measure the
new context switch in its entirety.

 -George



Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-04 Thread Jan Beulich
On 04.09.2026 10:06, George Dunlap wrote:
> On Fri, Sep 4, 2026 at 7:54 AM Jürgen Groß  wrote:
>>
>> On 04.09.26 08:00, Jan Beulich wrote:
>>> On 04.09.2026 00:35, George Dunlap wrote:
 On Thu, Sep 3, 2026 at 5:11 PM Jan Beulich  wrote:
>
> On 02.09.2026 11:43, George Dunlap wrote:
>> From: Roger Pau Monné 
>>
>> Currently, update_xen_slot_in_full_gdt() uses the stashed direct-map
>> pointer in d->arch.pv.gdt_ldt_l1tab to update the incoming vcpu's
>> page tables with Xen's GDT, by writing a stashed per-cpu copy of a
>> pre-baked L1 entry (either 64-bit or compat version).
>>
>> Switch this to using populate_perdomain_mapping(), which doesn't rely
>> on the stashed address of the l1 page in the direct map.  Rather than
>> also stashing a pre-baked value for the payload, compute the mfn from
>> the per-cpu GDT pointer at use: the conversion is a handful of cycles
>> on a path costing thousands, and computing at use removes the
>> parallel {,compat_}gdt_l1e bookkeeping along with its boot-ordering
>> constraint (the cached value could only be generated after Xen's
>> physical relocation, and had to be in place before the first context
>> switch; a use-time lookup is correct by construction).  The flags on
>> the final mapping are identical.
>>
>> Signed-off-by: Roger Pau Monné 
>> Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8
>> Signed-off-by: George Dunlap 
>> ---
>> Changes in v2:
>> - Drop the {,compat_}gdt_mfn caching entirely (suggested by Andrew
>> Cooper): compute virt_to_mfn() from the per-cpu GDT pointer at use.
>> The PDX lookup behind it measures ~5-10 cycles warm against a
>> ~1,500-cycle context switch, and this removes the double
>> bookkeeping and the after-relocation caching constraint.  The
>> cached-MFN assertion goes with the cache: a use-time computation
>> from a live pointer needs no staleness check.
>
> This looks to contradict what 564d261687c0 ("x86/ctxt-switch: Document
> and improve GDT handling") used as justification to put in place the
> caching. Also Cc-ing Jürgen, who also was involved there, for possible
> further insight.
>
> Functionally the change looks okay to me, but the above will need
> sorting, at the very least by specifically discussing why effectively
> undoing that earlier change is okay.

 So looking back at the thread, Jürgen measured a 14% improvement for
 something that might be described as a microbenchmark before and after
 the patch (a benchmark purposely trying to set up an unusual scenario
 to maximize the effect of context switch overhead, not one to
 represent a typical workflow).  But are the numbers really plausible?
 Even at an implausible 100k switches/s across the box, saving 100
 cycles per switch is about 0.04% of eight 3 GHz cores.

 At any rate, we're already adding several map/unmap operations, and
 about to add several more.  Keeping the PTE caching would require
 adding a separate path that can write just PTEs, which then will
 potentially further complication future paths where we need to make
 sure we handle both domain-wide perdomain areas and per-vcpu areas.
 If it were easy I would already have been keeping it.

 I'd be inclined to say: Since we're going to be adding more
 populate_perdomain_mapping() calls anyway, let's do it the simple
 correct way first; and then explore the idea of stashing mfns of
 frequently-mapped L1s (rather than having to walk L3 -> L2 -> L1); and
 at that time look into stashing baked l1es to avoid conversions.
>>>
>>> Perhaps; I'd like to have Jürgen's and/or Andrew's input here, though.
>>
>> At that time I implemented core scheduling in Xen. I noticed that very
>> subtle changes in the context switch path could result in unexpected large
>> performance differences. As I had the performance test for my purpose
>> already set up, I used it for Andrew's patch (which was a result of my
>> context switch path performance findings) and really did measure the
>> impressive effect of it.
>>
>> Note that you can't only count instructions, often cache effects and
>> branch predictions are dominating the performance.
> 
> Right, but:
> 
> 1. That's going to be very much hardware- and workload- dependent.
> Even on the same hardware, if you'd made a slight change in the
> workload, you might have seen a very different result; and on
> different hardware you're going to see something different again
> 
> 2. As I said, we're now adding two extra map / unmaps, which is going
> to perturb everything again.
> 
> If anything, your argument says we should wait until we've stopped
> modifying the context switch path (which won't happen until patch 49
> at least, guessing from the patch titles), and then measure things
> again to see 

Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-03 Thread Jan Beulich
On 04.09.2026 00:35, George Dunlap wrote:
> On Thu, Sep 3, 2026 at 5:11 PM Jan Beulich  wrote:
>>
>> On 02.09.2026 11:43, George Dunlap wrote:
>>> From: Roger Pau Monné 
>>>
>>> Currently, update_xen_slot_in_full_gdt() uses the stashed direct-map
>>> pointer in d->arch.pv.gdt_ldt_l1tab to update the incoming vcpu's
>>> page tables with Xen's GDT, by writing a stashed per-cpu copy of a
>>> pre-baked L1 entry (either 64-bit or compat version).
>>>
>>> Switch this to using populate_perdomain_mapping(), which doesn't rely
>>> on the stashed address of the l1 page in the direct map.  Rather than
>>> also stashing a pre-baked value for the payload, compute the mfn from
>>> the per-cpu GDT pointer at use: the conversion is a handful of cycles
>>> on a path costing thousands, and computing at use removes the
>>> parallel {,compat_}gdt_l1e bookkeeping along with its boot-ordering
>>> constraint (the cached value could only be generated after Xen's
>>> physical relocation, and had to be in place before the first context
>>> switch; a use-time lookup is correct by construction).  The flags on
>>> the final mapping are identical.
>>>
>>> Signed-off-by: Roger Pau Monné 
>>> Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8
>>> Signed-off-by: George Dunlap 
>>> ---
>>> Changes in v2:
>>> - Drop the {,compat_}gdt_mfn caching entirely (suggested by Andrew
>>>Cooper): compute virt_to_mfn() from the per-cpu GDT pointer at use.
>>>The PDX lookup behind it measures ~5-10 cycles warm against a
>>>~1,500-cycle context switch, and this removes the double
>>>bookkeeping and the after-relocation caching constraint.  The
>>>cached-MFN assertion goes with the cache: a use-time computation
>>>from a live pointer needs no staleness check.
>>
>> This looks to contradict what 564d261687c0 ("x86/ctxt-switch: Document
>> and improve GDT handling") used as justification to put in place the
>> caching. Also Cc-ing Jürgen, who also was involved there, for possible
>> further insight.
>>
>> Functionally the change looks okay to me, but the above will need
>> sorting, at the very least by specifically discussing why effectively
>> undoing that earlier change is okay.
> 
> So looking back at the thread, Jürgen measured a 14% improvement for
> something that might be described as a microbenchmark before and after
> the patch (a benchmark purposely trying to set up an unusual scenario
> to maximize the effect of context switch overhead, not one to
> represent a typical workflow).  But are the numbers really plausible?
> Even at an implausible 100k switches/s across the box, saving 100
> cycles per switch is about 0.04% of eight 3 GHz cores.
> 
> At any rate, we're already adding several map/unmap operations, and
> about to add several more.  Keeping the PTE caching would require
> adding a separate path that can write just PTEs, which then will
> potentially further complication future paths where we need to make
> sure we handle both domain-wide perdomain areas and per-vcpu areas.
> If it were easy I would already have been keeping it.
> 
> I'd be inclined to say: Since we're going to be adding more
> populate_perdomain_mapping() calls anyway, let's do it the simple
> correct way first; and then explore the idea of stashing mfns of
> frequently-mapped L1s (rather than having to walk L3 -> L2 -> L1); and
> at that time look into stashing baked l1es to avoid conversions.

Perhaps; I'd like to have Jürgen's and/or Andrew's input here, though.

Jan



Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-03 Thread George Dunlap
On Thu, Sep 3, 2026 at 5:11 PM Jan Beulich  wrote:
>
> On 02.09.2026 11:43, George Dunlap wrote:
> > From: Roger Pau Monné 
> >
> > Currently, update_xen_slot_in_full_gdt() uses the stashed direct-map
> > pointer in d->arch.pv.gdt_ldt_l1tab to update the incoming vcpu's
> > page tables with Xen's GDT, by writing a stashed per-cpu copy of a
> > pre-baked L1 entry (either 64-bit or compat version).
> >
> > Switch this to using populate_perdomain_mapping(), which doesn't rely
> > on the stashed address of the l1 page in the direct map.  Rather than
> > also stashing a pre-baked value for the payload, compute the mfn from
> > the per-cpu GDT pointer at use: the conversion is a handful of cycles
> > on a path costing thousands, and computing at use removes the
> > parallel {,compat_}gdt_l1e bookkeeping along with its boot-ordering
> > constraint (the cached value could only be generated after Xen's
> > physical relocation, and had to be in place before the first context
> > switch; a use-time lookup is correct by construction).  The flags on
> > the final mapping are identical.
> >
> > Signed-off-by: Roger Pau Monné 
> > Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8
> > Signed-off-by: George Dunlap 
> > ---
> > Changes in v2:
> > - Drop the {,compat_}gdt_mfn caching entirely (suggested by Andrew
> >Cooper): compute virt_to_mfn() from the per-cpu GDT pointer at use.
> >The PDX lookup behind it measures ~5-10 cycles warm against a
> >~1,500-cycle context switch, and this removes the double
> >bookkeeping and the after-relocation caching constraint.  The
> >cached-MFN assertion goes with the cache: a use-time computation
> >from a live pointer needs no staleness check.
>
> This looks to contradict what 564d261687c0 ("x86/ctxt-switch: Document
> and improve GDT handling") used as justification to put in place the
> caching. Also Cc-ing Jürgen, who also was involved there, for possible
> further insight.
>
> Functionally the change looks okay to me, but the above will need
> sorting, at the very least by specifically discussing why effectively
> undoing that earlier change is okay.

So looking back at the thread, Jürgen measured a 14% improvement for
something that might be described as a microbenchmark before and after
the patch (a benchmark purposely trying to set up an unusual scenario
to maximize the effect of context switch overhead, not one to
represent a typical workflow).  But are the numbers really plausible?
Even at an implausible 100k switches/s across the box, saving 100
cycles per switch is about 0.04% of eight 3 GHz cores.

At any rate, we're already adding several map/unmap operations, and
about to add several more.  Keeping the PTE caching would require
adding a separate path that can write just PTEs, which then will
potentially further complication future paths where we need to make
sure we handle both domain-wide perdomain areas and per-vcpu areas.
If it were easy I would already have been keeping it.

I'd be inclined to say: Since we're going to be adding more
populate_perdomain_mapping() calls anyway, let's do it the simple
correct way first; and then explore the idea of stashing mfns of
frequently-mapped L1s (rather than having to walk L3 -> L2 -> L1); and
at that time look into stashing baked l1es to avoid conversions.

 -George



Re: [PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-03 Thread Jan Beulich
On 02.09.2026 11:43, George Dunlap wrote:
> From: Roger Pau Monné 
> 
> Currently, update_xen_slot_in_full_gdt() uses the stashed direct-map
> pointer in d->arch.pv.gdt_ldt_l1tab to update the incoming vcpu's
> page tables with Xen's GDT, by writing a stashed per-cpu copy of a
> pre-baked L1 entry (either 64-bit or compat version).
> 
> Switch this to using populate_perdomain_mapping(), which doesn't rely
> on the stashed address of the l1 page in the direct map.  Rather than
> also stashing a pre-baked value for the payload, compute the mfn from
> the per-cpu GDT pointer at use: the conversion is a handful of cycles
> on a path costing thousands, and computing at use removes the
> parallel {,compat_}gdt_l1e bookkeeping along with its boot-ordering
> constraint (the cached value could only be generated after Xen's
> physical relocation, and had to be in place before the first context
> switch; a use-time lookup is correct by construction).  The flags on
> the final mapping are identical.
> 
> Signed-off-by: Roger Pau Monné 
> Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8
> Signed-off-by: George Dunlap 
> ---
> Changes in v2:
> - Drop the {,compat_}gdt_mfn caching entirely (suggested by Andrew
>Cooper): compute virt_to_mfn() from the per-cpu GDT pointer at use.
>The PDX lookup behind it measures ~5-10 cycles warm against a
>~1,500-cycle context switch, and this removes the double
>bookkeeping and the after-relocation caching constraint.  The
>cached-MFN assertion goes with the cache: a use-time computation
>from a live pointer needs no staleness check.

This looks to contradict what 564d261687c0 ("x86/ctxt-switch: Document
and improve GDT handling") used as justification to put in place the
caching. Also Cc-ing Jürgen, who also was involved there, for possible
further insight.

Functionally the change looks okay to me, but the above will need
sorting, at the very least by specifically discussing why effectively
undoing that earlier change is okay.

Jan



[PATCH v2 03/14] x86/pv: use populate_perdomain_mapping() to map the Xen GDT

2026-09-02 Thread George Dunlap
From: Roger Pau Monné 

Currently, update_xen_slot_in_full_gdt() uses the stashed direct-map
pointer in d->arch.pv.gdt_ldt_l1tab to update the incoming vcpu's
page tables with Xen's GDT, by writing a stashed per-cpu copy of a
pre-baked L1 entry (either 64-bit or compat version).

Switch this to using populate_perdomain_mapping(), which doesn't rely
on the stashed address of the l1 page in the direct map.  Rather than
also stashing a pre-baked value for the payload, compute the mfn from
the per-cpu GDT pointer at use: the conversion is a handful of cycles
on a path costing thousands, and computing at use removes the
parallel {,compat_}gdt_l1e bookkeeping along with its boot-ordering
constraint (the cached value could only be generated after Xen's
physical relocation, and had to be in place before the first context
switch; a use-time lookup is correct by construction).  The flags on
the final mapping are identical.

Signed-off-by: Roger Pau Monné 
Assisted-by: Claude Code:claude-fable-5, Claude Code:claude-opus-4-8
Signed-off-by: George Dunlap 
---
Changes in v2:
- Drop the {,compat_}gdt_mfn caching entirely (suggested by Andrew
   Cooper): compute virt_to_mfn() from the per-cpu GDT pointer at use.
   The PDX lookup behind it measures ~5-10 cycles warm against a
   ~1,500-cycle context switch, and this removes the double
   bookkeeping and the after-relocation caching constraint.  The
   cached-MFN assertion goes with the cache: a use-time computation
   from a live pointer needs no staleness check.

Changes since the previously posted version:
- populate_perdomain_mapping() introduction split into the previous
   patch; this patch is now just the Xen GDT conversion.
- Retain the "GDT MFN cached" check as ASSERT(mfn_x(mfn)).
---
 xen/arch/x86/domain.c   | 13 -
 xen/arch/x86/include/asm/desc.h |  2 --
 xen/arch/x86/smpboot.c  | 15 ---
 xen/arch/x86/traps.c|  2 --
 4 files changed, 8 insertions(+), 24 deletions(-)

diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
index 996b50af7a..d8af06e533 100644
--- a/xen/arch/x86/domain.c
+++ b/xen/arch/x86/domain.c
@@ -2062,11 +2062,14 @@ static always_inline bool need_full_gdt(const struct 
domain *d)
 
 static void update_xen_slot_in_full_gdt(const struct vcpu *v, unsigned int cpu)
 {
-ASSERT(per_cpu(gdt_l1e, cpu).l1); /* Confirm these have been cached. */
-
-l1e_write(pv_gdt_ptes(v) + FIRST_RESERVED_GDT_PAGE,
-  !is_pv_32bit_vcpu(v) ? per_cpu(gdt_l1e, cpu)
-   : per_cpu(compat_gdt_l1e, cpu));
+mfn_t mfn = _mfn(virt_to_mfn(!is_pv_32bit_vcpu(v)
+ ? per_cpu(gdt, cpu)
+ : per_cpu(compat_gdt, cpu)));
+
+populate_perdomain_mapping(v,
+   GDT_VIRT_START(v) +
+   (FIRST_RESERVED_GDT_PAGE << PAGE_SHIFT),
+   &mfn, 1, __PAGE_HYPERVISOR_RW);
 }
 
 static void load_full_gdt(const struct vcpu *v, unsigned int cpu)
diff --git a/xen/arch/x86/include/asm/desc.h b/xen/arch/x86/include/asm/desc.h
index dcbdac3ff7..a860134211 100644
--- a/xen/arch/x86/include/asm/desc.h
+++ b/xen/arch/x86/include/asm/desc.h
@@ -136,10 +136,8 @@ struct __packed desc_ptr {
 
 extern seg_desc_t boot_gdt[];
 DECLARE_PER_CPU(seg_desc_t *, gdt);
-DECLARE_PER_CPU(l1_pgentry_t, gdt_l1e);
 extern seg_desc_t boot_compat_gdt[];
 DECLARE_PER_CPU(seg_desc_t *, compat_gdt);
-DECLARE_PER_CPU(l1_pgentry_t, compat_gdt_l1e);
 DECLARE_PER_CPU(bool, full_gdt_loaded);
 
 static inline void lgdt(const struct desc_ptr *gdtr)
diff --git a/xen/arch/x86/smpboot.c b/xen/arch/x86/smpboot.c
index 84e9e4beed..9b837a1769 100644
--- a/xen/arch/x86/smpboot.c
+++ b/xen/arch/x86/smpboot.c
@@ -1085,8 +1085,6 @@ static int cpu_smpboot_alloc(unsigned int cpu)
 if ( gdt == NULL )
 goto out;
 per_cpu(gdt, cpu) = gdt;
-per_cpu(gdt_l1e, cpu) =
-l1e_from_pfn(virt_to_mfn(gdt), __PAGE_HYPERVISOR_RW);
 memcpy(gdt, boot_gdt, NR_RESERVED_GDT_PAGES * PAGE_SIZE);
 BUILD_BUG_ON(NR_CPUS > 0x1);
 gdt[PER_CPU_GDT_ENTRY - FIRST_RESERVED_GDT_ENTRY].a = cpu;
@@ -1095,8 +1093,6 @@ static int cpu_smpboot_alloc(unsigned int cpu)
 per_cpu(compat_gdt, cpu) = gdt = alloc_xenheap_pages(0, memflags);
 if ( gdt == NULL )
 goto out;
-per_cpu(compat_gdt_l1e, cpu) =
-l1e_from_pfn(virt_to_mfn(gdt), __PAGE_HYPERVISOR_RW);
 memcpy(gdt, boot_compat_gdt, NR_RESERVED_GDT_PAGES * PAGE_SIZE);
 gdt[PER_CPU_GDT_ENTRY - FIRST_RESERVED_GDT_ENTRY].a = cpu;
 #endif
@@ -1173,17 +1169,6 @@ void __init smp_prepare_cpus(void)
 initialize_cpu_data(0); /* Final full version of the data */
 print_cpu_info(0);
 
-/*
- * Cache {,compat_}gdt_l1e for the BSP now that physically relocation is
- * done.  It must be after physical relocation of Xen, and before the
- * first context_switch().
- */
-this_cpu(gdt_l1e) =
-