[PATCH v2 08/15] KVM: arm64: Protect stage-2 traversal with RCU
Oliver Upton
oliver.upton at linux.dev
Thu Oct 20 01:34:24 PDT 2022
On Wed, Oct 19, 2022 at 11:29:56PM +0000, Sean Christopherson wrote:
> On Fri, Oct 07, 2022, Oliver Upton wrote:
> > The use of RCU is necessary to safely change the stage-2 page tables in
> > parallel. Acquire and release the RCU read lock when traversing the page
> > tables.
> >
> > Use the _raw() flavor of rcu_dereference when changes to the page tables
> > are otherwise protected from parallel software walkers (e.g. holding the
> > write lock).
> >
> > Signed-off-by: Oliver Upton <oliver.upton at linux.dev>
> > ---
>
> ...
>
> > @@ -32,6 +39,33 @@ static inline kvm_pte_t *kvm_dereference_pteref(kvm_pteref_t pteref, bool shared
> > return pteref;
> > }
> >
> > +static inline void kvm_pgtable_walk_begin(void) {}
> > +static inline void kvm_pgtable_walk_end(void) {}
> > +
> > +#else
> > +
> > +typedef kvm_pte_t __rcu *kvm_pteref_t;
> > +
> > +static inline kvm_pte_t *kvm_dereference_pteref(kvm_pteref_t pteref, bool shared)
> > +{
> > + if (shared)
> > + return rcu_dereference(pteref);
> > +
> > + return rcu_dereference_raw(pteref);
>
> Rather than use raw, use rcu_dereference_check(). If you can plumb down @kvm or
> @mmu_lock, the ideal check would be (apparently there's no lockdep_is_held_write()
> wrapper?)
>
> return READ_ONCE(*rcu_dereference_check(ptep, lockdep_is_held_type(mmu_lock, 0)));
>
> If getting at mmu_lock is too difficult, this can still be
>
> return READ_ONCE(*rcu_dereference_check(ptep, !shared);
>
> Doubt it matters for code generation, but IMO it's cleaner overall.
As the page table walkers can be used outside of the context of a VM
(such as hyp stage-1), I think option #2 is probably a bit easier.
--
Thanks,
Oliver
More information about the linux-arm-kernel
mailing list