x86/mm: Remove unnecessary mfn_valid() call from get_page_from_l1e()
PATCH 1/1 x86/mm: Remove unnecessary mfn_valid() call from get_page_from_l1e()
mfn_valid() is not a trivially simple, and contains an evaluate_nospec() for speculative defence. Avoid calling it redundantly, and just store the result of the first call.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
--- xen/arch/x86/mm.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
xen/arch/x86/mm.c +5 -3
@@ -887,7 +887,7 @@ get_page_from_l1e(uint32_t l1f =l1e_get_flags (l1e);struct vcpu *curr = current;struct domain *real_pg_owner; -bool write; +bool write, valid;if (unlikely (!(l1f & _PAGE_PRESENT)) ) {
@@ -902,13 +902,15 @@ get_page_from_l1e(return -EINVAL ; } -if ( !mfn_valid (_mfn (mfn)) || + valid =mfn_valid (_mfn (mfn)); + +if ( !valid || (real_pg_owner =page_get_owner_and_reference (page)) == dom_io ) {int flip =0 ;/* Only needed the reference to confirm dom_io ownership. */ -if (mfn_valid (_mfn (mfn)) ) +if ( valid )put_page (page);/* DOMID_IO reverts to caller for privilege checks. */
Signature · 1 line
2.11.0
Quoted 5 lines · Andrew Cooper
mfn_valid()is not a trivially simple, and contains anevaluate_nospec()for speculative defence. Avoid calling it redundantly, and just store the result of the first call.Signed-off-by Andrew Cooper <[email protected]>
Quoted 3 lines · Andrew Cooper
mfn_valid()is not a trivially simple, and contains anevaluate_nospec()for speculative defence. Avoid calling it redundantly, and just store the result of the first call.
Since it took quite some time for this to actually be committed, I did notice it among more recent commits, and I've grown a question: Isn't the latching of the result in a local variable undermining the supposed speculative defense? It's not as if I could point out a particular gadget here, but it feels like the adjustment should have specifically justified the speculative safety ... But I guess my understanding of all of this might still be somewhat flaky?
Jan
Quoted 18 lines
xen/arch/x86/mm.c +4
-2@@ -902,13 +902,15 @@ get_page_from_l1e(return -EINVAL ; } -if ( !mfn_valid (_mfn (mfn)) || + valid =mfn_valid (_mfn (mfn)); + +if ( !valid || (real_pg_owner =page_get_owner_and_reference (page)) == dom_io ) {int flip =0 ;/* Only needed the reference to confirm dom_io ownership. */ -if (mfn_valid (_mfn (mfn)) ) +if ( valid )put_page (page);/* DOMID_IO reverts to caller for privilege checks. */
Quoted 11 lines · Jan Beulich
Quoted 3 lines · Andrew Cooper
mfn_valid()is not a trivially simple, and contains anevaluate_nospec()for speculative defence. Avoid calling it redundantly, and just store the result of the first call.Since it took quite some time for this to actually be committed, I did notice it among more recent commits, and I've grown a question: Isn't the latching of the result in a local variable undermining the supposed speculative defense? It's not as if I could point out a particular gadget here, but it feels like the adjustment should have specifically justified the speculative safety ... But I guess my understanding of all of this might still be somewhat flaky?
The eval_nospec() in mfn_valid() is to avoid a speculative over-read of pdx_group_valid[].
It does not provide any protection for any other logic.
In particular, it does not provide any safety whatsoever in get_page_from_l1e() because the lfence is the wrong side of the conditional jump. i.e. you've got:
...
call mfn_valid // lfence in here
test %al, %al
je 1f
... // lfence missing from here 1:
... // and here
The change I've made is simply CSE that a compiler could do automatically. if it could be persuaded that __mfn_valid() was pure (which it logically is.)
If logic in get_page_from_l1e() needs Spectre protections for any reason, it needs its own eval_nospec()/array_access_nospec()/etc because it is specifically not safe to rely on incidental lfence's from unrelated protections.
~Andrew
P.S. I need to add this to the list of reasons of why we need a "coding & speculation safety" doc in the tree.