[PATCH v15 03/10] x86/shadow: 1-bit-disable doesn't need to detach old tables

Jan Beulich <[email protected]> Tue, 28 Jul 2026 16:21:22 +0200
Newsgroups gmane.comp.emulators.xen.devel
Message-ID <[email protected]>
Just ahead of the loop being modified sh_new_mode() is called, which in
turn calls sh_update_paging_modes() and thus sh_detach_old_tables(). To
retain the intended effect of not leaving any shadows in place, avoid
re-establishing a new top-level shadow from sh_update_paging_modes().

Signed-off-by: Jan Beulich <[email protected]>
Acked-by: Roger Pau MonnĂ© <[email protected]>
---
This doesn't need to be part of the XSA, but it eliminates one point of
concern wrt preemption checking. However, in the context of "x86/mm:
make more of log-dirty mode enable/disable preemptable" I started
wondering whether things don't want doing differently. 1-bit-enables
technically don't need to purge all shadows:
- log-dirty enable only needs to get rid of all L1 entries; this could
  be done via hash lookup (of all L1/FL1 shadows) instead of
  shadow_blow_tables(),
- test enable doesn't need any purging; it may be the intention though
  that it does a certain amount of purging,
- force enable doesn't need to do any purging either.
We could therefore pass a new boolean through sh_new_mode() to
sh_update_paging_modes() to suppress the call to sh_detach_old_tables()
for some (all?) of the 1-bit enables.

1-bit disables (log-dirty as well as test) match the above, but of
course they will need to (at least) detach all tables when shadow mode
is being turned off altogether (i.e. the very invocation that's being
deleted here). As done in the other patch, the amount of work to do by
the (then possibly retained) sh_detach_old_tables() here could be
bounded by "preparatory" purging (in a properly preemptable way) in
shadow_one_bit_disable() or its callers. Using shadow_blow_tables() for
that purpose is probably preferable anyway, as sh_detach_old_tables()
alone won't get rid of pinned shadows.
---
v15: Re-base.
v13: New.

--- a/xen/arch/x86/mm/shadow/common.c
+++ b/xen/arch/x86/mm/shadow/common.c
@@ -1982,7 +1982,8 @@ static void sh_update_paging_modes(struc
     }
 #endif /* OOS */
 
-    v->arch.paging.mode->update_cr3(v, false);
+    if ( paging_mode_enabled(d) )
+        v->arch.paging.mode->update_cr3(v, false);
 }
 
 /*
@@ -2459,8 +2460,6 @@ static int shadow_one_bit_disable(struct
                        d->arch.paging.free_pages, d->arch.paging.p2m_pages);
         for_each_vcpu(d, v)
         {
-            if ( v->arch.paging.mode )
-                sh_detach_old_tables(v);
             if ( !(v->arch.flags & TF_kernel_mode) )
                 make_cr3(v, pagetable_get_mfn(v->arch.guest_table_user));
             else