Re: [PATCH 20/24] kvm: x86/mmu: Add atomic option for setting SPTEs

From: Paolo Bonzini
Date: Tue Jan 26 2021 - 09:23:45 EST


On 12/01/21 19:10, Ben Gardon wrote:
static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
- u64 old_spte, u64 new_spte, int level);
+ u64 old_spte, u64 new_spte, int level,
+ bool atomic);

If you don't mind, I prefer "shared" as the name for the new argument (i.e. "this is what you need to know", rathar than "this is what I want you to do").


+/*
+ * tdp_mmu_set_spte_atomic - Set a TDP MMU SPTE atomically and handle the
+ * associated bookkeeping
+ *
+ * @kvm: kvm instance
+ * @iter: a tdp_iter instance currently on the SPTE that should be set
+ * @new_spte: The value the SPTE should be set to
+ * Returns: true if the SPTE was set, false if it was not. If false is returned,
+ * this function will have no side-effects.
+ */
+static inline bool tdp_mmu_set_spte_atomic(struct kvm *kvm,
+ struct tdp_iter *iter,
+ u64 new_spte)
+{
+ u64 *root_pt = tdp_iter_root_pt(iter);
+ struct kvm_mmu_page *root = sptep_to_sp(root_pt);
+ int as_id = kvm_mmu_page_as_id(root);
+
+ kvm_mmu_lock_assert_held_shared(kvm);
+
+ if (cmpxchg64(iter->sptep, iter->old_spte, new_spte) != iter->old_spte)
+ return false;
+
+ handle_changed_spte(kvm, as_id, iter->gfn, iter->old_spte, new_spte,
+ iter->level, true);
+
+ return true;
+}
+
+

Still unused as of this patch, so please move it where it's used.

Note that in this case, "atomic" in the name is appropriate, think of hypothetical code like this:

if (!shared)
tdp_mmu_set_spte(...)
else if (!tdp_mmu_set_spte_atomic(...)


which says "if there could be concurrent changes, be careful and do everything with atomic operations".

Paolo