Skip to contentSkip to conversation

x86: mapcache_current_vcpu() adjustments

PATCH In review Jan Beulich 3 participants 8 messages Oct 13 – Oct 29

lore
Share & tools
lore
#0 lore raw

cover x86: mapcache_current_vcpu() adjustments

1: constify local variables in mapcache_current_vcpu() 2: replace this_cpu() uses in mapcache_current_vcpu()

The latter patch is somewhat RFC; see commentary there.

Jan

#1 lore raw

PATCH 1/2 x86: constify local variables in mapcache_current_vcpu()

As of the referenced commit v won't be returned from the function anymore. Yet that was the sole reason for it to not be pointer-to-const. curr, which was introduced by that change, had no reason to be pointer-to-non- const from the start.

Amends: 622c9a5ba95d ("x86/mm: accurately track which vCPU page-tables are loaded")

---
I was heavily tempted to also replace the smp_processor_id() at this occasion (by curr->processor). With gcc16, that leads to the function actually being inlined (in debug builds), i.e. a significant change in generated code, and a measurable increase in code size. From the function being declared "inline", that looks to have been the intention, yet then that use of "inline" doesn't quite fit our principles. The main reasoning for this as a secondary change would be:
- "curr" could be dropped (instead of having const added) if code generated from ASSERT() wouldn't matter quality-wise,
- if generated code quality matters there, then the unnecessary use of smp_processor_id() should matter, too.
However, an adjustment along these lines may better fit in a subsequent change dealing with the repeated this_cpu() here.

xen/arch/x86/domain_page.c +2 -2
@@ -20,8 +20,8 @@
 
 static inline struct vcpu *mapcache_current_vcpu(void)
 {
-    struct vcpu *v = this_cpu(pgtable_vcpu);
-    struct vcpu *curr = current;
+    const struct vcpu *v = this_cpu(pgtable_vcpu);
+    const struct vcpu *curr = current;
 
     /*
      * During early boot pgtable_vcpu is not set, callers must handle NULL.
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] x86: constify local variables in mapcache_current_vcpu()

Quoted 6 lines · Jan Beulich

As of the referenced commit v won't be returned from the function anymore. Yet that was the sole reason for it to not be pointer-to-const. curr, which was introduced by that change, had no reason to be pointer-to-non- const from the start.

Amends: 622c9a5ba95d ("x86/mm: accurately track which vCPU page-tables are loaded")

I think I recall how this ended the way it is: initially I was returning v, which was wrong because this_cpu(pgtable_vcpu) will have changed after the sync_local_execstate(). Later I adjusted the return value of the function to be this_cpu(pgtable_vcpu), but didn't made v const.

Quoted 1 line
Quoted 6 lines

---
I was heavily tempted to also replace the smp_processor_id() at this occasion (by curr->processor). With gcc16, that leads to the function actually being inlined (in debug builds), i.e. a significant change in generated code, and a measurable increase in code size. From the function being declared "inline", that looks to have been the intention, yet then

Quoted 8 lines

that use of "inline" doesn't quite fit our principles. The main reasoning for this as a secondary change would be:
- "curr" could be dropped (instead of having const added) if code generated from ASSERT() wouldn't matter quality-wise,
- if generated code quality matters there, then the unnecessary use of smp_processor_id() should matter, too.
However, an adjustment along these lines may better fit in a subsequent change dealing with the repeated this_cpu() here.

We might want to introduce a local cpu variable to hold curr->processor and then replace the this_cpu() with per_cpu(), together with removing the usage of smp_processor_id(). Shouldn't make the code more complicated IMO.

Thanks, Roger.

#3 lore raw

PATCH 2/2 x86: replace this_cpu() uses in mapcache_current_vcpu()

In reply to Jan Beulich

We have accumulated three of them, plus a hidden similar 4th construct from smp_processor_id(). Latch the result of the latter into a local variable and use the slightly more efficient per_cpu() instead.

---
A 5th such construct is "current", yet to replace that we'd need to open- code both it (i.e. get_current()) and then also smp_processor_id(). I wasn't quite sure whether that would be deemed acceptable.

Like indicated in the earlier patch, this similarly leads to the function actually being inlined (and hence generated code size actually growing) in gcc16 debug builds. In release builds (with older gcc) the function is being inlined no matter what. Overall, having looked at generated code differences, I'm not quite sure anymore whether the earlier observed inefficiency with repeated this_cpu() really is much of an issue (here or maybe even more generally).

xen/arch/x86/domain_page.c +5 -4
@@ -20,7 +20,8 @@
 
 static inline struct vcpu *mapcache_current_vcpu(void)
 {
-    const struct vcpu *v = this_cpu(pgtable_vcpu);
+    unsigned int cpu = smp_processor_id();
+    const struct vcpu *v = per_cpu(pgtable_vcpu, cpu);
     const struct vcpu *curr = current;
 
     /*
@@ -37,9 +38,9 @@ static inline struct vcpu *mapcache_curr
      * change the page tables under our feet an invalidate any in-use mapcache
      * entries.
      */
-    if ( unlikely(this_cpu(curr_vcpu) != curr) )
+    if ( unlikely(per_cpu(curr_vcpu, cpu) != curr) )
     {
-        ASSERT(curr == idle_vcpu[smp_processor_id()]);
+        ASSERT(curr == idle_vcpu[cpu]);
         sync_local_execstate();
         /* We must now be running on the idle page table. */
         ASSERT(cr3_pa(read_cr3()) == __pa(idle_pg_table));
@@ -51,7 +52,7 @@ static inline struct vcpu *mapcache_curr
      * FLUSH_VCPU_STATE IPI has done so behind our back.  Use ACCESS_ONCE to
      * ensure the compiler never returns the locally cached pgtable_vcpu value.
      */
-    return ACCESS_ONCE(this_cpu(pgtable_vcpu));
+    return ACCESS_ONCE(per_cpu(pgtable_vcpu, cpu));
 }
 
 #define mapcache_l2_entry(e) ((e) >> PAGETABLE_ORDER)
#4 lore raw

Re: [PATCH 2/2] x86: replace this_cpu() uses in mapcache_current_vcpu()

Quoted 5 lines · Jan Beulich

We have accumulated three of them, plus a hidden similar 4th construct from smp_processor_id(). Latch the result of the latter into a local variable and use the slightly more efficient per_cpu() instead.

Oh, so that's kind of what I've suggested in the previous patch.

Quoted 4 lines

---
A 5th such construct is "current", yet to replace that we'd need to open- code both it (i.e. get_current()) and then also smp_processor_id(). I wasn't quite sure whether that would be deemed acceptable.

See below, we can reorder and use the processor ID value stashed in current?

Quoted 7 lines

Like indicated in the earlier patch, this similarly leads to the function actually being inlined (and hence generated code size actually growing) in gcc16 debug builds. In release builds (with older gcc) the function is being inlined no matter what. Overall, having looked at generated code differences, I'm not quite sure anymore whether the earlier observed inefficiency with repeated this_cpu() really is much of an issue (here or maybe even more generally).

I think we sometimes focus too much on this kind of micro-optimizations when the cost of the operation itself is orders of magnitude bigger than any performance difference the optimization might cause.

In this case I don't think the resulting code is bad or harder to read, so I don't mind adjusting the logic. But that's a question of taste, if someone else has a preference for the original form I wouldn't change it.

xen/arch/x86/domain_page.c line 25

Quoted 10 lines
xen/arch/x86/domain_page.c +2 -1
@@ -20,7 +20,8 @@
 
 static inline struct vcpu *mapcache_current_vcpu(void)
 {
-    const struct vcpu *v = this_cpu(pgtable_vcpu);
+    unsigned int cpu = smp_processor_id();
+    const struct vcpu *v = per_cpu(pgtable_vcpu, cpu);
     const struct vcpu *curr = current;

Maybe you can reorder the variables here:

const struct vcpu *curr = current; unsigned int cpu = curr->processor;
const struct vcpu *v = per_cpu(pgtable_vcpu, cpu);

So that we avoid two smp_processor_id() calls (ie: the hidden one in current).

Thanks, Roger.

#5 lore raw

Re: [PATCH 2/2] x86: replace this_cpu() uses in mapcache_current_vcpu()

Quoted 25 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich

We have accumulated three of them, plus a hidden similar 4th construct from smp_processor_id(). Latch the result of the latter into a local variable and use the slightly more efficient per_cpu() instead.

Oh, so that's kind of what I've suggested in the previous patch.

Quoted 4 lines

---
A 5th such construct is "current", yet to replace that we'd need to open- code both it (i.e. get_current()) and then also smp_processor_id(). I wasn't quite sure whether that would be deemed acceptable.

See below, we can reorder and use the processor ID value stashed in current?

Quoted 7 lines

Like indicated in the earlier patch, this similarly leads to the function actually being inlined (and hence generated code size actually growing) in gcc16 debug builds. In release builds (with older gcc) the function is being inlined no matter what. Overall, having looked at generated code differences, I'm not quite sure anymore whether the earlier observed inefficiency with repeated this_cpu() really is much of an issue (here or maybe even more generally).

Quoted 4 lines

I think we sometimes focus too much on this kind of micro-optimizations when the cost of the operation itself is orders of magnitude bigger than any performance difference the optimization might cause.

Well, any improvement we can reasonably (and safely) make counts. Even if not measurable performance-wise, it may still make a difference (also in power consumption) when accumulated over long enough a period.

Quoted 4 lines

In this case I don't think the resulting code is bad or harder to read, so I don't mind adjusting the logic. But that's a question of taste, if someone else has a preference for the original form I wouldn't change it.

I'll give it some time for comments to that (or any other) effect.

Quoted 13 lines

xen/arch/x86/domain_page.c line 25

Quoted 10 lines
xen/arch/x86/domain_page.c +2 -1
@@ -20,7 +20,8 @@
 
 static inline struct vcpu *mapcache_current_vcpu(void)
 {
-    const struct vcpu *v = this_cpu(pgtable_vcpu);
+    unsigned int cpu = smp_processor_id();
+    const struct vcpu *v = per_cpu(pgtable_vcpu, cpu);
     const struct vcpu *curr = current;

Maybe you can reorder the variables here:

Quoted 3 lines

const struct vcpu *curr = current; unsigned int cpu = curr->processor;
const struct vcpu *v = per_cpu(pgtable_vcpu, cpu);

The use of curr->processor in the remark in patch 1 was safe afaict. I'm not fully sure the one here would be safe: It would require thorough auditing of scheduler code to be sure, I think. (I don't _think_ ->processor would be altered from a remote CPU, but I'm not entirely certain.)

Quoted 2 lines

So that we avoid two smp_processor_id() calls (ie: the hidden one in current).

Strictly speaking there's none hidden in current. That accesses a different cpu_info field.

Jan

Reviewed-by
#6 lore raw

Re: [PATCH 2/2] x86: replace this_cpu() uses in mapcache_current_vcpu()

Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 7 lines

Like indicated in the earlier patch, this similarly leads to the function actually being inlined (and hence generated code size actually growing) in gcc16 debug builds. In release builds (with older gcc) the function is being inlined no matter what. Overall, having looked at generated code differences, I'm not quite sure anymore whether the earlier observed inefficiency with repeated this_cpu() really is much of an issue (here or maybe even more generally).

Quoted 8 lines
Quoted 4 lines

I think we sometimes focus too much on this kind of micro-optimizations when the cost of the operation itself is orders of magnitude bigger than any performance difference the optimization might cause.

Well, any improvement we can reasonably (and safely) make counts. Even if not measurable performance-wise, it may still make a difference (also in power consumption) when accumulated over long enough a period.

If we had an infinite amount of developer and reviewer attention, this would be true; if for instance we could have LLMs just review and check in patches they were confident of. But reviewer bandwidth is in fact a major bottleneck; the fact that Roger and I are looking at this patch now means we're not looking at some other patch.

Quoted 6 lines
Quoted 4 lines

In this case I don't think the resulting code is bad or harder to read, so I don't mind adjusting the logic. But that's a question of taste, if someone else has a preference for the original form I wouldn't change it.

I'll give it some time for comments to that (or any other) effect.

FWIW, having already looked at the patch, I don't mind either way. "this_cpu()" is slightly more readable; one could argue that per_cpu(foo, cpu) sort of implies that it could be a *different* cpu -- at very least someone reading the code has to go check at least once. And again, human cognitive effort is the main bottleneck for a correct, feature-ful, and performant hypervisor. But both the potential efficiency win, and the potential cognitive load, are so small that I don't feel like it's a clear win either way.

So I would leave it off, but if you'd rather:

Quoted 17 lines
Quoted 13 lines

xen/arch/x86/domain_page.c line 25

Quoted 10 lines
xen/arch/x86/domain_page.c +2 -1
@@ -20,7 +20,8 @@
 
 static inline struct vcpu *mapcache_current_vcpu(void)
 {
-    const struct vcpu *v = this_cpu(pgtable_vcpu);
+    unsigned int cpu = smp_processor_id();
+    const struct vcpu *v = per_cpu(pgtable_vcpu, cpu);
     const struct vcpu *curr = current;

Maybe you can reorder the variables here:

Quoted 3 lines

const struct vcpu *curr = current; unsigned int cpu = curr->processor;
const struct vcpu *v = per_cpu(pgtable_vcpu, cpu);

Quoted 5 lines

The use of curr->processor in the remark in patch 1 was safe afaict. I'm not fully sure the one here would be safe: It would require thorough auditing of scheduler code to be sure, I think. (I don't _think_ ->processor would be altered from a remote CPU, but I'm not entirely certain.)

Claude points out that curr is never dereferenced, only compared; so the code as it is works before current is set. The first map_domain_page() comes long before current is set in init_idle_domain(), so Roger's version crashes during boot (Claude verified this).

-George

#7 lore raw

Re: [PATCH 2/2] x86: replace this_cpu() uses in mapcache_current_vcpu()

Quoted 41 lines · George Dunlap
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 7 lines

Like indicated in the earlier patch, this similarly leads to the function actually being inlined (and hence generated code size actually growing) in gcc16 debug builds. In release builds (with older gcc) the function is being inlined no matter what. Overall, having looked at generated code differences, I'm not quite sure anymore whether the earlier observed inefficiency with repeated this_cpu() really is much of an issue (here or maybe even more generally).

Quoted 8 lines
Quoted 4 lines

I think we sometimes focus too much on this kind of micro-optimizations when the cost of the operation itself is orders of magnitude bigger than any performance difference the optimization might cause.

Well, any improvement we can reasonably (and safely) make counts. Even if not measurable performance-wise, it may still make a difference (also in power consumption) when accumulated over long enough a period.

If we had an infinite amount of developer and reviewer attention, this would be true; if for instance we could have LLMs just review and check in patches they were confident of. But reviewer bandwidth is in fact a major bottleneck; the fact that Roger and I are looking at this patch now means we're not looking at some other patch.

Quoted 6 lines
Quoted 4 lines

In this case I don't think the resulting code is bad or harder to read, so I don't mind adjusting the logic. But that's a question of taste, if someone else has a preference for the original form I wouldn't change it.

I'll give it some time for comments to that (or any other) effect.

FWIW, having already looked at the patch, I don't mind either way. "this_cpu()" is slightly more readable; one could argue that per_cpu(foo, cpu) sort of implies that it could be a *different* cpu -- at very least someone reading the code has to go check at least once. And again, human cognitive effort is the main bottleneck for a correct, feature-ful, and performant hypervisor. But both the potential efficiency win, and the potential cognitive load, are so small that I don't feel like it's a clear win either way.

Quoted 3 lines

So I would leave it off, but if you'd rather:

That said, Claude's analysis of the generated code doesn't give any benefits. Its take below.

-George

It saves no work in the generated code (gcc 16.2.0, staging 41ddcfe808):

- Why: is_pv_vcpu() → evaluate_nospec() is an asm volatile with a "memory" clobber. So the per-CPU offset is reloaded after it either way, whether it comes from cpu_info or from __per_cpu_offset[cpu]. Latching cpu only saves the %rsp arithmetic, and costs the processor_id load.

- Release builds: the same number of loads on the common path, one instruction fewer, and map_domain_page/unmap_domain_page grow by 16 and 21 bytes. In release builds this code runs only for frames above 5 TiB anyway, because of the NDEBUG shortcut.

- Debug builds: gcc inlines the function into both callers (.text +384 bytes, as Jan said), and the common path gains one load.

So, as Roger said, it’s a matter of taste; the generated code gives no reason to take it.