KVM: x86/mmu: Zap invalidated roots via asynchronous worker
[linux-2.6-microblaze.git] / arch / x86 / kvm / mmu / tdp_mmu.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "mmu.h"
4 #include "mmu_internal.h"
5 #include "mmutrace.h"
6 #include "tdp_iter.h"
7 #include "tdp_mmu.h"
8 #include "spte.h"
9
10 #include <asm/cmpxchg.h>
11 #include <trace/events/kvm.h>
12
13 static bool __read_mostly tdp_mmu_enabled = true;
14 module_param_named(tdp_mmu, tdp_mmu_enabled, bool, 0644);
15
16 /* Initializes the TDP MMU for the VM, if enabled. */
17 bool kvm_mmu_init_tdp_mmu(struct kvm *kvm)
18 {
19         if (!tdp_enabled || !READ_ONCE(tdp_mmu_enabled))
20                 return false;
21
22         /* This should not be changed for the lifetime of the VM. */
23         kvm->arch.tdp_mmu_enabled = true;
24
25         INIT_LIST_HEAD(&kvm->arch.tdp_mmu_roots);
26         spin_lock_init(&kvm->arch.tdp_mmu_pages_lock);
27         INIT_LIST_HEAD(&kvm->arch.tdp_mmu_pages);
28         kvm->arch.tdp_mmu_zap_wq =
29                 alloc_workqueue("kvm", WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 0);
30
31         return true;
32 }
33
34 /* Arbitrarily returns true so that this may be used in if statements. */
35 static __always_inline bool kvm_lockdep_assert_mmu_lock_held(struct kvm *kvm,
36                                                              bool shared)
37 {
38         if (shared)
39                 lockdep_assert_held_read(&kvm->mmu_lock);
40         else
41                 lockdep_assert_held_write(&kvm->mmu_lock);
42
43         return true;
44 }
45
46 void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)
47 {
48         if (!kvm->arch.tdp_mmu_enabled)
49                 return;
50
51         flush_workqueue(kvm->arch.tdp_mmu_zap_wq);
52         destroy_workqueue(kvm->arch.tdp_mmu_zap_wq);
53
54         WARN_ON(!list_empty(&kvm->arch.tdp_mmu_pages));
55         WARN_ON(!list_empty(&kvm->arch.tdp_mmu_roots));
56
57         /*
58          * Ensure that all the outstanding RCU callbacks to free shadow pages
59          * can run before the VM is torn down.  Work items on tdp_mmu_zap_wq
60          * can call kvm_tdp_mmu_put_root and create new callbacks.
61          */
62         rcu_barrier();
63 }
64
65 static void tdp_mmu_free_sp(struct kvm_mmu_page *sp)
66 {
67         free_page((unsigned long)sp->spt);
68         kmem_cache_free(mmu_page_header_cache, sp);
69 }
70
71 /*
72  * This is called through call_rcu in order to free TDP page table memory
73  * safely with respect to other kernel threads that may be operating on
74  * the memory.
75  * By only accessing TDP MMU page table memory in an RCU read critical
76  * section, and freeing it after a grace period, lockless access to that
77  * memory won't use it after it is freed.
78  */
79 static void tdp_mmu_free_sp_rcu_callback(struct rcu_head *head)
80 {
81         struct kvm_mmu_page *sp = container_of(head, struct kvm_mmu_page,
82                                                rcu_head);
83
84         tdp_mmu_free_sp(sp);
85 }
86
87 static void tdp_mmu_zap_root(struct kvm *kvm, struct kvm_mmu_page *root,
88                              bool shared);
89
90 static void tdp_mmu_zap_root_work(struct work_struct *work)
91 {
92         struct kvm_mmu_page *root = container_of(work, struct kvm_mmu_page,
93                                                  tdp_mmu_async_work);
94         struct kvm *kvm = root->tdp_mmu_async_data;
95
96         read_lock(&kvm->mmu_lock);
97
98         /*
99          * A TLB flush is not necessary as KVM performs a local TLB flush when
100          * allocating a new root (see kvm_mmu_load()), and when migrating vCPU
101          * to a different pCPU.  Note, the local TLB flush on reuse also
102          * invalidates any paging-structure-cache entries, i.e. TLB entries for
103          * intermediate paging structures, that may be zapped, as such entries
104          * are associated with the ASID on both VMX and SVM.
105          */
106         tdp_mmu_zap_root(kvm, root, true);
107
108         /*
109          * Drop the refcount using kvm_tdp_mmu_put_root() to test its logic for
110          * avoiding an infinite loop.  By design, the root is reachable while
111          * it's being asynchronously zapped, thus a different task can put its
112          * last reference, i.e. flowing through kvm_tdp_mmu_put_root() for an
113          * asynchronously zapped root is unavoidable.
114          */
115         kvm_tdp_mmu_put_root(kvm, root, true);
116
117         read_unlock(&kvm->mmu_lock);
118 }
119
120 static void tdp_mmu_schedule_zap_root(struct kvm *kvm, struct kvm_mmu_page *root)
121 {
122         root->tdp_mmu_async_data = kvm;
123         INIT_WORK(&root->tdp_mmu_async_work, tdp_mmu_zap_root_work);
124         queue_work(kvm->arch.tdp_mmu_zap_wq, &root->tdp_mmu_async_work);
125 }
126
127 void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root,
128                           bool shared)
129 {
130         kvm_lockdep_assert_mmu_lock_held(kvm, shared);
131
132         if (!refcount_dec_and_test(&root->tdp_mmu_root_count))
133                 return;
134
135         WARN_ON(!root->tdp_mmu_page);
136
137         spin_lock(&kvm->arch.tdp_mmu_pages_lock);
138         list_del_rcu(&root->link);
139         spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
140
141         /*
142          * A TLB flush is not necessary as KVM performs a local TLB flush when
143          * allocating a new root (see kvm_mmu_load()), and when migrating vCPU
144          * to a different pCPU.  Note, the local TLB flush on reuse also
145          * invalidates any paging-structure-cache entries, i.e. TLB entries for
146          * intermediate paging structures, that may be zapped, as such entries
147          * are associated with the ASID on both VMX and SVM.
148          */
149         tdp_mmu_zap_root(kvm, root, shared);
150
151         call_rcu(&root->rcu_head, tdp_mmu_free_sp_rcu_callback);
152 }
153
154 /*
155  * Returns the next root after @prev_root (or the first root if @prev_root is
156  * NULL).  A reference to the returned root is acquired, and the reference to
157  * @prev_root is released (the caller obviously must hold a reference to
158  * @prev_root if it's non-NULL).
159  *
160  * If @only_valid is true, invalid roots are skipped.
161  *
162  * Returns NULL if the end of tdp_mmu_roots was reached.
163  */
164 static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm,
165                                               struct kvm_mmu_page *prev_root,
166                                               bool shared, bool only_valid)
167 {
168         struct kvm_mmu_page *next_root;
169
170         rcu_read_lock();
171
172         if (prev_root)
173                 next_root = list_next_or_null_rcu(&kvm->arch.tdp_mmu_roots,
174                                                   &prev_root->link,
175                                                   typeof(*prev_root), link);
176         else
177                 next_root = list_first_or_null_rcu(&kvm->arch.tdp_mmu_roots,
178                                                    typeof(*next_root), link);
179
180         while (next_root) {
181                 if ((!only_valid || !next_root->role.invalid) &&
182                     kvm_tdp_mmu_get_root(next_root))
183                         break;
184
185                 next_root = list_next_or_null_rcu(&kvm->arch.tdp_mmu_roots,
186                                 &next_root->link, typeof(*next_root), link);
187         }
188
189         rcu_read_unlock();
190
191         if (prev_root)
192                 kvm_tdp_mmu_put_root(kvm, prev_root, shared);
193
194         return next_root;
195 }
196
197 /*
198  * Note: this iterator gets and puts references to the roots it iterates over.
199  * This makes it safe to release the MMU lock and yield within the loop, but
200  * if exiting the loop early, the caller must drop the reference to the most
201  * recent root. (Unless keeping a live reference is desirable.)
202  *
203  * If shared is set, this function is operating under the MMU lock in read
204  * mode. In the unlikely event that this thread must free a root, the lock
205  * will be temporarily dropped and reacquired in write mode.
206  */
207 #define __for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared, _only_valid)\
208         for (_root = tdp_mmu_next_root(_kvm, NULL, _shared, _only_valid);       \
209              _root;                                                             \
210              _root = tdp_mmu_next_root(_kvm, _root, _shared, _only_valid))      \
211                 if (kvm_lockdep_assert_mmu_lock_held(_kvm, _shared) &&          \
212                     kvm_mmu_page_as_id(_root) != _as_id) {                      \
213                 } else
214
215 #define for_each_valid_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared)    \
216         __for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, _shared, true)
217
218 #define for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id)                   \
219         __for_each_tdp_mmu_root_yield_safe(_kvm, _root, _as_id, false, false)
220
221 /*
222  * Iterate over all TDP MMU roots.  Requires that mmu_lock be held for write,
223  * the implication being that any flow that holds mmu_lock for read is
224  * inherently yield-friendly and should use the yield-safe variant above.
225  * Holding mmu_lock for write obviates the need for RCU protection as the list
226  * is guaranteed to be stable.
227  */
228 #define for_each_tdp_mmu_root(_kvm, _root, _as_id)                      \
229         list_for_each_entry(_root, &_kvm->arch.tdp_mmu_roots, link)     \
230                 if (kvm_lockdep_assert_mmu_lock_held(_kvm, false) &&    \
231                     kvm_mmu_page_as_id(_root) != _as_id) {              \
232                 } else
233
234 static struct kvm_mmu_page *tdp_mmu_alloc_sp(struct kvm_vcpu *vcpu)
235 {
236         struct kvm_mmu_page *sp;
237
238         sp = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
239         sp->spt = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_shadow_page_cache);
240
241         return sp;
242 }
243
244 static void tdp_mmu_init_sp(struct kvm_mmu_page *sp, tdp_ptep_t sptep,
245                             gfn_t gfn, union kvm_mmu_page_role role)
246 {
247         set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
248
249         sp->role = role;
250         sp->gfn = gfn;
251         sp->ptep = sptep;
252         sp->tdp_mmu_page = true;
253
254         trace_kvm_mmu_get_page(sp, true);
255 }
256
257 static void tdp_mmu_init_child_sp(struct kvm_mmu_page *child_sp,
258                                   struct tdp_iter *iter)
259 {
260         struct kvm_mmu_page *parent_sp;
261         union kvm_mmu_page_role role;
262
263         parent_sp = sptep_to_sp(rcu_dereference(iter->sptep));
264
265         role = parent_sp->role;
266         role.level--;
267
268         tdp_mmu_init_sp(child_sp, iter->sptep, iter->gfn, role);
269 }
270
271 hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)
272 {
273         union kvm_mmu_page_role role = vcpu->arch.mmu->mmu_role.base;
274         struct kvm *kvm = vcpu->kvm;
275         struct kvm_mmu_page *root;
276
277         lockdep_assert_held_write(&kvm->mmu_lock);
278
279         /*
280          * Check for an existing root before allocating a new one.  Note, the
281          * role check prevents consuming an invalid root.
282          */
283         for_each_tdp_mmu_root(kvm, root, kvm_mmu_role_as_id(role)) {
284                 if (root->role.word == role.word &&
285                     kvm_tdp_mmu_get_root(root))
286                         goto out;
287         }
288
289         root = tdp_mmu_alloc_sp(vcpu);
290         tdp_mmu_init_sp(root, NULL, 0, role);
291
292         refcount_set(&root->tdp_mmu_root_count, 1);
293
294         spin_lock(&kvm->arch.tdp_mmu_pages_lock);
295         list_add_rcu(&root->link, &kvm->arch.tdp_mmu_roots);
296         spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
297
298 out:
299         return __pa(root->spt);
300 }
301
302 static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
303                                 u64 old_spte, u64 new_spte, int level,
304                                 bool shared);
305
306 static void handle_changed_spte_acc_track(u64 old_spte, u64 new_spte, int level)
307 {
308         if (!is_shadow_present_pte(old_spte) || !is_last_spte(old_spte, level))
309                 return;
310
311         if (is_accessed_spte(old_spte) &&
312             (!is_shadow_present_pte(new_spte) || !is_accessed_spte(new_spte) ||
313              spte_to_pfn(old_spte) != spte_to_pfn(new_spte)))
314                 kvm_set_pfn_accessed(spte_to_pfn(old_spte));
315 }
316
317 static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn,
318                                           u64 old_spte, u64 new_spte, int level)
319 {
320         bool pfn_changed;
321         struct kvm_memory_slot *slot;
322
323         if (level > PG_LEVEL_4K)
324                 return;
325
326         pfn_changed = spte_to_pfn(old_spte) != spte_to_pfn(new_spte);
327
328         if ((!is_writable_pte(old_spte) || pfn_changed) &&
329             is_writable_pte(new_spte)) {
330                 slot = __gfn_to_memslot(__kvm_memslots(kvm, as_id), gfn);
331                 mark_page_dirty_in_slot(kvm, slot, gfn);
332         }
333 }
334
335 /**
336  * tdp_mmu_unlink_sp() - Remove a shadow page from the list of used pages
337  *
338  * @kvm: kvm instance
339  * @sp: the page to be removed
340  * @shared: This operation may not be running under the exclusive use of
341  *          the MMU lock and the operation must synchronize with other
342  *          threads that might be adding or removing pages.
343  */
344 static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp,
345                               bool shared)
346 {
347         if (shared)
348                 spin_lock(&kvm->arch.tdp_mmu_pages_lock);
349         else
350                 lockdep_assert_held_write(&kvm->mmu_lock);
351
352         list_del(&sp->link);
353         if (sp->lpage_disallowed)
354                 unaccount_huge_nx_page(kvm, sp);
355
356         if (shared)
357                 spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
358 }
359
360 /**
361  * handle_removed_pt() - handle a page table removed from the TDP structure
362  *
363  * @kvm: kvm instance
364  * @pt: the page removed from the paging structure
365  * @shared: This operation may not be running under the exclusive use
366  *          of the MMU lock and the operation must synchronize with other
367  *          threads that might be modifying SPTEs.
368  *
369  * Given a page table that has been removed from the TDP paging structure,
370  * iterates through the page table to clear SPTEs and free child page tables.
371  *
372  * Note that pt is passed in as a tdp_ptep_t, but it does not need RCU
373  * protection. Since this thread removed it from the paging structure,
374  * this thread will be responsible for ensuring the page is freed. Hence the
375  * early rcu_dereferences in the function.
376  */
377 static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)
378 {
379         struct kvm_mmu_page *sp = sptep_to_sp(rcu_dereference(pt));
380         int level = sp->role.level;
381         gfn_t base_gfn = sp->gfn;
382         int i;
383
384         trace_kvm_mmu_prepare_zap_page(sp);
385
386         tdp_mmu_unlink_sp(kvm, sp, shared);
387
388         for (i = 0; i < PT64_ENT_PER_PAGE; i++) {
389                 u64 *sptep = rcu_dereference(pt) + i;
390                 gfn_t gfn = base_gfn + i * KVM_PAGES_PER_HPAGE(level);
391                 u64 old_child_spte;
392
393                 if (shared) {
394                         /*
395                          * Set the SPTE to a nonpresent value that other
396                          * threads will not overwrite. If the SPTE was
397                          * already marked as removed then another thread
398                          * handling a page fault could overwrite it, so
399                          * set the SPTE until it is set from some other
400                          * value to the removed SPTE value.
401                          */
402                         for (;;) {
403                                 old_child_spte = xchg(sptep, REMOVED_SPTE);
404                                 if (!is_removed_spte(old_child_spte))
405                                         break;
406                                 cpu_relax();
407                         }
408                 } else {
409                         /*
410                          * If the SPTE is not MMU-present, there is no backing
411                          * page associated with the SPTE and so no side effects
412                          * that need to be recorded, and exclusive ownership of
413                          * mmu_lock ensures the SPTE can't be made present.
414                          * Note, zapping MMIO SPTEs is also unnecessary as they
415                          * are guarded by the memslots generation, not by being
416                          * unreachable.
417                          */
418                         old_child_spte = READ_ONCE(*sptep);
419                         if (!is_shadow_present_pte(old_child_spte))
420                                 continue;
421
422                         /*
423                          * Marking the SPTE as a removed SPTE is not
424                          * strictly necessary here as the MMU lock will
425                          * stop other threads from concurrently modifying
426                          * this SPTE. Using the removed SPTE value keeps
427                          * the two branches consistent and simplifies
428                          * the function.
429                          */
430                         WRITE_ONCE(*sptep, REMOVED_SPTE);
431                 }
432                 handle_changed_spte(kvm, kvm_mmu_page_as_id(sp), gfn,
433                                     old_child_spte, REMOVED_SPTE, level,
434                                     shared);
435         }
436
437         call_rcu(&sp->rcu_head, tdp_mmu_free_sp_rcu_callback);
438 }
439
440 /**
441  * __handle_changed_spte - handle bookkeeping associated with an SPTE change
442  * @kvm: kvm instance
443  * @as_id: the address space of the paging structure the SPTE was a part of
444  * @gfn: the base GFN that was mapped by the SPTE
445  * @old_spte: The value of the SPTE before the change
446  * @new_spte: The value of the SPTE after the change
447  * @level: the level of the PT the SPTE is part of in the paging structure
448  * @shared: This operation may not be running under the exclusive use of
449  *          the MMU lock and the operation must synchronize with other
450  *          threads that might be modifying SPTEs.
451  *
452  * Handle bookkeeping that might result from the modification of a SPTE.
453  * This function must be called for all TDP SPTE modifications.
454  */
455 static void __handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
456                                   u64 old_spte, u64 new_spte, int level,
457                                   bool shared)
458 {
459         bool was_present = is_shadow_present_pte(old_spte);
460         bool is_present = is_shadow_present_pte(new_spte);
461         bool was_leaf = was_present && is_last_spte(old_spte, level);
462         bool is_leaf = is_present && is_last_spte(new_spte, level);
463         bool pfn_changed = spte_to_pfn(old_spte) != spte_to_pfn(new_spte);
464
465         WARN_ON(level > PT64_ROOT_MAX_LEVEL);
466         WARN_ON(level < PG_LEVEL_4K);
467         WARN_ON(gfn & (KVM_PAGES_PER_HPAGE(level) - 1));
468
469         /*
470          * If this warning were to trigger it would indicate that there was a
471          * missing MMU notifier or a race with some notifier handler.
472          * A present, leaf SPTE should never be directly replaced with another
473          * present leaf SPTE pointing to a different PFN. A notifier handler
474          * should be zapping the SPTE before the main MM's page table is
475          * changed, or the SPTE should be zeroed, and the TLBs flushed by the
476          * thread before replacement.
477          */
478         if (was_leaf && is_leaf && pfn_changed) {
479                 pr_err("Invalid SPTE change: cannot replace a present leaf\n"
480                        "SPTE with another present leaf SPTE mapping a\n"
481                        "different PFN!\n"
482                        "as_id: %d gfn: %llx old_spte: %llx new_spte: %llx level: %d",
483                        as_id, gfn, old_spte, new_spte, level);
484
485                 /*
486                  * Crash the host to prevent error propagation and guest data
487                  * corruption.
488                  */
489                 BUG();
490         }
491
492         if (old_spte == new_spte)
493                 return;
494
495         trace_kvm_tdp_mmu_spte_changed(as_id, gfn, level, old_spte, new_spte);
496
497         if (is_leaf)
498                 check_spte_writable_invariants(new_spte);
499
500         /*
501          * The only times a SPTE should be changed from a non-present to
502          * non-present state is when an MMIO entry is installed/modified/
503          * removed. In that case, there is nothing to do here.
504          */
505         if (!was_present && !is_present) {
506                 /*
507                  * If this change does not involve a MMIO SPTE or removed SPTE,
508                  * it is unexpected. Log the change, though it should not
509                  * impact the guest since both the former and current SPTEs
510                  * are nonpresent.
511                  */
512                 if (WARN_ON(!is_mmio_spte(old_spte) &&
513                             !is_mmio_spte(new_spte) &&
514                             !is_removed_spte(new_spte)))
515                         pr_err("Unexpected SPTE change! Nonpresent SPTEs\n"
516                                "should not be replaced with another,\n"
517                                "different nonpresent SPTE, unless one or both\n"
518                                "are MMIO SPTEs, or the new SPTE is\n"
519                                "a temporary removed SPTE.\n"
520                                "as_id: %d gfn: %llx old_spte: %llx new_spte: %llx level: %d",
521                                as_id, gfn, old_spte, new_spte, level);
522                 return;
523         }
524
525         if (is_leaf != was_leaf)
526                 kvm_update_page_stats(kvm, level, is_leaf ? 1 : -1);
527
528         if (was_leaf && is_dirty_spte(old_spte) &&
529             (!is_present || !is_dirty_spte(new_spte) || pfn_changed))
530                 kvm_set_pfn_dirty(spte_to_pfn(old_spte));
531
532         /*
533          * Recursively handle child PTs if the change removed a subtree from
534          * the paging structure.  Note the WARN on the PFN changing without the
535          * SPTE being converted to a hugepage (leaf) or being zapped.  Shadow
536          * pages are kernel allocations and should never be migrated.
537          */
538         if (was_present && !was_leaf &&
539             (is_leaf || !is_present || WARN_ON_ONCE(pfn_changed)))
540                 handle_removed_pt(kvm, spte_to_child_pt(old_spte, level), shared);
541 }
542
543 static void handle_changed_spte(struct kvm *kvm, int as_id, gfn_t gfn,
544                                 u64 old_spte, u64 new_spte, int level,
545                                 bool shared)
546 {
547         __handle_changed_spte(kvm, as_id, gfn, old_spte, new_spte, level,
548                               shared);
549         handle_changed_spte_acc_track(old_spte, new_spte, level);
550         handle_changed_spte_dirty_log(kvm, as_id, gfn, old_spte,
551                                       new_spte, level);
552 }
553
554 /*
555  * tdp_mmu_set_spte_atomic - Set a TDP MMU SPTE atomically
556  * and handle the associated bookkeeping.  Do not mark the page dirty
557  * in KVM's dirty bitmaps.
558  *
559  * If setting the SPTE fails because it has changed, iter->old_spte will be
560  * refreshed to the current value of the spte.
561  *
562  * @kvm: kvm instance
563  * @iter: a tdp_iter instance currently on the SPTE that should be set
564  * @new_spte: The value the SPTE should be set to
565  * Return:
566  * * 0      - If the SPTE was set.
567  * * -EBUSY - If the SPTE cannot be set. In this case this function will have
568  *            no side-effects other than setting iter->old_spte to the last
569  *            known value of the spte.
570  */
571 static inline int tdp_mmu_set_spte_atomic(struct kvm *kvm,
572                                           struct tdp_iter *iter,
573                                           u64 new_spte)
574 {
575         u64 *sptep = rcu_dereference(iter->sptep);
576         u64 old_spte;
577
578         WARN_ON_ONCE(iter->yielded);
579
580         lockdep_assert_held_read(&kvm->mmu_lock);
581
582         /*
583          * Do not change removed SPTEs. Only the thread that froze the SPTE
584          * may modify it.
585          */
586         if (is_removed_spte(iter->old_spte))
587                 return -EBUSY;
588
589         /*
590          * Note, fast_pf_fix_direct_spte() can also modify TDP MMU SPTEs and
591          * does not hold the mmu_lock.
592          */
593         old_spte = cmpxchg64(sptep, iter->old_spte, new_spte);
594         if (old_spte != iter->old_spte) {
595                 /*
596                  * The page table entry was modified by a different logical
597                  * CPU. Refresh iter->old_spte with the current value so the
598                  * caller operates on fresh data, e.g. if it retries
599                  * tdp_mmu_set_spte_atomic().
600                  */
601                 iter->old_spte = old_spte;
602                 return -EBUSY;
603         }
604
605         __handle_changed_spte(kvm, iter->as_id, iter->gfn, iter->old_spte,
606                               new_spte, iter->level, true);
607         handle_changed_spte_acc_track(iter->old_spte, new_spte, iter->level);
608
609         return 0;
610 }
611
612 static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,
613                                           struct tdp_iter *iter)
614 {
615         int ret;
616
617         /*
618          * Freeze the SPTE by setting it to a special,
619          * non-present value. This will stop other threads from
620          * immediately installing a present entry in its place
621          * before the TLBs are flushed.
622          */
623         ret = tdp_mmu_set_spte_atomic(kvm, iter, REMOVED_SPTE);
624         if (ret)
625                 return ret;
626
627         kvm_flush_remote_tlbs_with_address(kvm, iter->gfn,
628                                            KVM_PAGES_PER_HPAGE(iter->level));
629
630         /*
631          * No other thread can overwrite the removed SPTE as they
632          * must either wait on the MMU lock or use
633          * tdp_mmu_set_spte_atomic which will not overwrite the
634          * special removed SPTE value. No bookkeeping is needed
635          * here since the SPTE is going from non-present
636          * to non-present.
637          */
638         kvm_tdp_mmu_write_spte(iter->sptep, 0);
639
640         return 0;
641 }
642
643
644 /*
645  * __tdp_mmu_set_spte - Set a TDP MMU SPTE and handle the associated bookkeeping
646  * @kvm:              KVM instance
647  * @as_id:            Address space ID, i.e. regular vs. SMM
648  * @sptep:            Pointer to the SPTE
649  * @old_spte:         The current value of the SPTE
650  * @new_spte:         The new value that will be set for the SPTE
651  * @gfn:              The base GFN that was (or will be) mapped by the SPTE
652  * @level:            The level _containing_ the SPTE (its parent PT's level)
653  * @record_acc_track: Notify the MM subsystem of changes to the accessed state
654  *                    of the page. Should be set unless handling an MMU
655  *                    notifier for access tracking. Leaving record_acc_track
656  *                    unset in that case prevents page accesses from being
657  *                    double counted.
658  * @record_dirty_log: Record the page as dirty in the dirty bitmap if
659  *                    appropriate for the change being made. Should be set
660  *                    unless performing certain dirty logging operations.
661  *                    Leaving record_dirty_log unset in that case prevents page
662  *                    writes from being double counted.
663  */
664 static void __tdp_mmu_set_spte(struct kvm *kvm, int as_id, tdp_ptep_t sptep,
665                                u64 old_spte, u64 new_spte, gfn_t gfn, int level,
666                                bool record_acc_track, bool record_dirty_log)
667 {
668         lockdep_assert_held_write(&kvm->mmu_lock);
669
670         /*
671          * No thread should be using this function to set SPTEs to or from the
672          * temporary removed SPTE value.
673          * If operating under the MMU lock in read mode, tdp_mmu_set_spte_atomic
674          * should be used. If operating under the MMU lock in write mode, the
675          * use of the removed SPTE should not be necessary.
676          */
677         WARN_ON(is_removed_spte(old_spte) || is_removed_spte(new_spte));
678
679         kvm_tdp_mmu_write_spte(sptep, new_spte);
680
681         __handle_changed_spte(kvm, as_id, gfn, old_spte, new_spte, level, false);
682
683         if (record_acc_track)
684                 handle_changed_spte_acc_track(old_spte, new_spte, level);
685         if (record_dirty_log)
686                 handle_changed_spte_dirty_log(kvm, as_id, gfn, old_spte,
687                                               new_spte, level);
688 }
689
690 static inline void _tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,
691                                      u64 new_spte, bool record_acc_track,
692                                      bool record_dirty_log)
693 {
694         WARN_ON_ONCE(iter->yielded);
695
696         __tdp_mmu_set_spte(kvm, iter->as_id, iter->sptep, iter->old_spte,
697                            new_spte, iter->gfn, iter->level,
698                            record_acc_track, record_dirty_log);
699 }
700
701 static inline void tdp_mmu_set_spte(struct kvm *kvm, struct tdp_iter *iter,
702                                     u64 new_spte)
703 {
704         _tdp_mmu_set_spte(kvm, iter, new_spte, true, true);
705 }
706
707 static inline void tdp_mmu_set_spte_no_acc_track(struct kvm *kvm,
708                                                  struct tdp_iter *iter,
709                                                  u64 new_spte)
710 {
711         _tdp_mmu_set_spte(kvm, iter, new_spte, false, true);
712 }
713
714 static inline void tdp_mmu_set_spte_no_dirty_log(struct kvm *kvm,
715                                                  struct tdp_iter *iter,
716                                                  u64 new_spte)
717 {
718         _tdp_mmu_set_spte(kvm, iter, new_spte, true, false);
719 }
720
721 #define tdp_root_for_each_pte(_iter, _root, _start, _end) \
722         for_each_tdp_pte(_iter, _root, _start, _end)
723
724 #define tdp_root_for_each_leaf_pte(_iter, _root, _start, _end)  \
725         tdp_root_for_each_pte(_iter, _root, _start, _end)               \
726                 if (!is_shadow_present_pte(_iter.old_spte) ||           \
727                     !is_last_spte(_iter.old_spte, _iter.level))         \
728                         continue;                                       \
729                 else
730
731 #define tdp_mmu_for_each_pte(_iter, _mmu, _start, _end)         \
732         for_each_tdp_pte(_iter, to_shadow_page(_mmu->root.hpa), _start, _end)
733
734 /*
735  * Yield if the MMU lock is contended or this thread needs to return control
736  * to the scheduler.
737  *
738  * If this function should yield and flush is set, it will perform a remote
739  * TLB flush before yielding.
740  *
741  * If this function yields, iter->yielded is set and the caller must skip to
742  * the next iteration, where tdp_iter_next() will reset the tdp_iter's walk
743  * over the paging structures to allow the iterator to continue its traversal
744  * from the paging structure root.
745  *
746  * Returns true if this function yielded.
747  */
748 static inline bool __must_check tdp_mmu_iter_cond_resched(struct kvm *kvm,
749                                                           struct tdp_iter *iter,
750                                                           bool flush, bool shared)
751 {
752         WARN_ON(iter->yielded);
753
754         /* Ensure forward progress has been made before yielding. */
755         if (iter->next_last_level_gfn == iter->yielded_gfn)
756                 return false;
757
758         if (need_resched() || rwlock_needbreak(&kvm->mmu_lock)) {
759                 if (flush)
760                         kvm_flush_remote_tlbs(kvm);
761
762                 rcu_read_unlock();
763
764                 if (shared)
765                         cond_resched_rwlock_read(&kvm->mmu_lock);
766                 else
767                         cond_resched_rwlock_write(&kvm->mmu_lock);
768
769                 rcu_read_lock();
770
771                 WARN_ON(iter->gfn > iter->next_last_level_gfn);
772
773                 iter->yielded = true;
774         }
775
776         return iter->yielded;
777 }
778
779 static inline gfn_t tdp_mmu_max_gfn_host(void)
780 {
781         /*
782          * Bound TDP MMU walks at host.MAXPHYADDR, guest accesses beyond that
783          * will hit a #PF(RSVD) and never hit an EPT Violation/Misconfig / #NPF,
784          * and so KVM will never install a SPTE for such addresses.
785          */
786         return 1ULL << (shadow_phys_bits - PAGE_SHIFT);
787 }
788
789 static void tdp_mmu_zap_root(struct kvm *kvm, struct kvm_mmu_page *root,
790                              bool shared)
791 {
792         bool root_is_unreachable = !refcount_read(&root->tdp_mmu_root_count);
793         struct tdp_iter iter;
794
795         gfn_t end = tdp_mmu_max_gfn_host();
796         gfn_t start = 0;
797
798         kvm_lockdep_assert_mmu_lock_held(kvm, shared);
799
800         rcu_read_lock();
801
802         /*
803          * No need to try to step down in the iterator when zapping an entire
804          * root, zapping an upper-level SPTE will recurse on its children.
805          */
806         for_each_tdp_pte_min_level(iter, root, root->role.level, start, end) {
807 retry:
808                 /*
809                  * Yielding isn't allowed when zapping an unreachable root as
810                  * the root won't be processed by mmu_notifier callbacks.  When
811                  * handling an unmap/release mmu_notifier command, KVM must
812                  * drop all references to relevant pages prior to completing
813                  * the callback.  Dropping mmu_lock can result in zapping SPTEs
814                  * for an unreachable root after a relevant callback completes,
815                  * which leads to use-after-free as zapping a SPTE triggers
816                  * "writeback" of dirty/accessed bits to the SPTE's associated
817                  * struct page.
818                  */
819                 if (!root_is_unreachable &&
820                     tdp_mmu_iter_cond_resched(kvm, &iter, false, shared))
821                         continue;
822
823                 if (!is_shadow_present_pte(iter.old_spte))
824                         continue;
825
826                 if (!shared) {
827                         tdp_mmu_set_spte(kvm, &iter, 0);
828                 } else if (tdp_mmu_set_spte_atomic(kvm, &iter, 0)) {
829                         /*
830                          * cmpxchg() shouldn't fail if the root is unreachable.
831                          * Retry so as not to leak the page and its children.
832                          */
833                         WARN_ONCE(root_is_unreachable,
834                                   "Contended TDP MMU SPTE in unreachable root.");
835                         goto retry;
836                 }
837
838                 /*
839                  * WARN if the root is invalid and is unreachable, all SPTEs
840                  * should've been zapped by kvm_tdp_mmu_zap_invalidated_roots(),
841                  * and inserting new SPTEs under an invalid root is a KVM bug.
842                  */
843                 WARN_ON_ONCE(root_is_unreachable && root->role.invalid);
844         }
845
846         rcu_read_unlock();
847 }
848
849 bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
850 {
851         u64 old_spte;
852
853         /*
854          * This helper intentionally doesn't allow zapping a root shadow page,
855          * which doesn't have a parent page table and thus no associated entry.
856          */
857         if (WARN_ON_ONCE(!sp->ptep))
858                 return false;
859
860         old_spte = kvm_tdp_mmu_read_spte(sp->ptep);
861         if (WARN_ON_ONCE(!is_shadow_present_pte(old_spte)))
862                 return false;
863
864         __tdp_mmu_set_spte(kvm, kvm_mmu_page_as_id(sp), sp->ptep, old_spte, 0,
865                            sp->gfn, sp->role.level + 1, true, true);
866
867         return true;
868 }
869
870 /*
871  * Zap leafs SPTEs for the range of gfns, [start, end). Returns true if SPTEs
872  * have been cleared and a TLB flush is needed before releasing the MMU lock.
873  *
874  * If can_yield is true, will release the MMU lock and reschedule if the
875  * scheduler needs the CPU or there is contention on the MMU lock. If this
876  * function cannot yield, it will not release the MMU lock or reschedule and
877  * the caller must ensure it does not supply too large a GFN range, or the
878  * operation can cause a soft lockup.
879  */
880 static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,
881                               gfn_t start, gfn_t end, bool can_yield, bool flush)
882 {
883         struct tdp_iter iter;
884
885         end = min(end, tdp_mmu_max_gfn_host());
886
887         lockdep_assert_held_write(&kvm->mmu_lock);
888
889         rcu_read_lock();
890
891         for_each_tdp_pte_min_level(iter, root, PG_LEVEL_4K, start, end) {
892                 if (can_yield &&
893                     tdp_mmu_iter_cond_resched(kvm, &iter, flush, false)) {
894                         flush = false;
895                         continue;
896                 }
897
898                 if (!is_shadow_present_pte(iter.old_spte) ||
899                     !is_last_spte(iter.old_spte, iter.level))
900                         continue;
901
902                 tdp_mmu_set_spte(kvm, &iter, 0);
903                 flush = true;
904         }
905
906         rcu_read_unlock();
907
908         /*
909          * Because this flow zaps _only_ leaf SPTEs, the caller doesn't need
910          * to provide RCU protection as no 'struct kvm_mmu_page' will be freed.
911          */
912         return flush;
913 }
914
915 /*
916  * Tears down the mappings for the range of gfns, [start, end), and frees the
917  * non-root pages mapping GFNs strictly within that range. Returns true if
918  * SPTEs have been cleared and a TLB flush is needed before releasing the
919  * MMU lock.
920  */
921 bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, int as_id, gfn_t start, gfn_t end,
922                            bool can_yield, bool flush)
923 {
924         struct kvm_mmu_page *root;
925
926         for_each_tdp_mmu_root_yield_safe(kvm, root, as_id)
927                 flush = tdp_mmu_zap_leafs(kvm, root, start, end, can_yield, false);
928
929         return flush;
930 }
931
932 void kvm_tdp_mmu_zap_all(struct kvm *kvm)
933 {
934         struct kvm_mmu_page *root;
935         int i;
936
937         /*
938          * Zap all roots, including invalid roots, as all SPTEs must be dropped
939          * before returning to the caller.  Zap directly even if the root is
940          * also being zapped by a worker.  Walking zapped top-level SPTEs isn't
941          * all that expensive and mmu_lock is already held, which means the
942          * worker has yielded, i.e. flushing the work instead of zapping here
943          * isn't guaranteed to be any faster.
944          *
945          * A TLB flush is unnecessary, KVM zaps everything if and only the VM
946          * is being destroyed or the userspace VMM has exited.  In both cases,
947          * KVM_RUN is unreachable, i.e. no vCPUs will ever service the request.
948          */
949         for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
950                 for_each_tdp_mmu_root_yield_safe(kvm, root, i)
951                         tdp_mmu_zap_root(kvm, root, false);
952         }
953 }
954
955 /*
956  * Zap all invalidated roots to ensure all SPTEs are dropped before the "fast
957  * zap" completes.
958  */
959 void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)
960 {
961         flush_workqueue(kvm->arch.tdp_mmu_zap_wq);
962 }
963
964 /*
965  * Mark each TDP MMU root as invalid to prevent vCPUs from reusing a root that
966  * is about to be zapped, e.g. in response to a memslots update.  The actual
967  * zapping is performed asynchronously, so a reference is taken on all roots.
968  * Using a separate workqueue makes it easy to ensure that the destruction is
969  * performed before the "fast zap" completes, without keeping a separate list
970  * of invalidated roots; the list is effectively the list of work items in
971  * the workqueue.
972  *
973  * Get a reference even if the root is already invalid, the asynchronous worker
974  * assumes it was gifted a reference to the root it processes.  Because mmu_lock
975  * is held for write, it should be impossible to observe a root with zero refcount,
976  * i.e. the list of roots cannot be stale.
977  *
978  * This has essentially the same effect for the TDP MMU
979  * as updating mmu_valid_gen does for the shadow MMU.
980  */
981 void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)
982 {
983         struct kvm_mmu_page *root;
984
985         lockdep_assert_held_write(&kvm->mmu_lock);
986         list_for_each_entry(root, &kvm->arch.tdp_mmu_roots, link) {
987                 if (!WARN_ON_ONCE(!kvm_tdp_mmu_get_root(root))) {
988                         root->role.invalid = true;
989                         tdp_mmu_schedule_zap_root(kvm, root);
990                 }
991         }
992 }
993
994 /*
995  * Installs a last-level SPTE to handle a TDP page fault.
996  * (NPT/EPT violation/misconfiguration)
997  */
998 static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,
999                                           struct kvm_page_fault *fault,
1000                                           struct tdp_iter *iter)
1001 {
1002         struct kvm_mmu_page *sp = sptep_to_sp(rcu_dereference(iter->sptep));
1003         u64 new_spte;
1004         int ret = RET_PF_FIXED;
1005         bool wrprot = false;
1006
1007         WARN_ON(sp->role.level != fault->goal_level);
1008         if (unlikely(!fault->slot))
1009                 new_spte = make_mmio_spte(vcpu, iter->gfn, ACC_ALL);
1010         else
1011                 wrprot = make_spte(vcpu, sp, fault->slot, ACC_ALL, iter->gfn,
1012                                          fault->pfn, iter->old_spte, fault->prefetch, true,
1013                                          fault->map_writable, &new_spte);
1014
1015         if (new_spte == iter->old_spte)
1016                 ret = RET_PF_SPURIOUS;
1017         else if (tdp_mmu_set_spte_atomic(vcpu->kvm, iter, new_spte))
1018                 return RET_PF_RETRY;
1019         else if (is_shadow_present_pte(iter->old_spte) &&
1020                  !is_last_spte(iter->old_spte, iter->level))
1021                 kvm_flush_remote_tlbs_with_address(vcpu->kvm, sp->gfn,
1022                                                    KVM_PAGES_PER_HPAGE(iter->level + 1));
1023
1024         /*
1025          * If the page fault was caused by a write but the page is write
1026          * protected, emulation is needed. If the emulation was skipped,
1027          * the vCPU would have the same fault again.
1028          */
1029         if (wrprot) {
1030                 if (fault->write)
1031                         ret = RET_PF_EMULATE;
1032         }
1033
1034         /* If a MMIO SPTE is installed, the MMIO will need to be emulated. */
1035         if (unlikely(is_mmio_spte(new_spte))) {
1036                 trace_mark_mmio_spte(rcu_dereference(iter->sptep), iter->gfn,
1037                                      new_spte);
1038                 ret = RET_PF_EMULATE;
1039         } else {
1040                 trace_kvm_mmu_set_spte(iter->level, iter->gfn,
1041                                        rcu_dereference(iter->sptep));
1042         }
1043
1044         /*
1045          * Increase pf_fixed in both RET_PF_EMULATE and RET_PF_FIXED to be
1046          * consistent with legacy MMU behavior.
1047          */
1048         if (ret != RET_PF_SPURIOUS)
1049                 vcpu->stat.pf_fixed++;
1050
1051         return ret;
1052 }
1053
1054 /*
1055  * tdp_mmu_link_sp - Replace the given spte with an spte pointing to the
1056  * provided page table.
1057  *
1058  * @kvm: kvm instance
1059  * @iter: a tdp_iter instance currently on the SPTE that should be set
1060  * @sp: The new TDP page table to install.
1061  * @account_nx: True if this page table is being installed to split a
1062  *              non-executable huge page.
1063  * @shared: This operation is running under the MMU lock in read mode.
1064  *
1065  * Returns: 0 if the new page table was installed. Non-0 if the page table
1066  *          could not be installed (e.g. the atomic compare-exchange failed).
1067  */
1068 static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter,
1069                            struct kvm_mmu_page *sp, bool account_nx,
1070                            bool shared)
1071 {
1072         u64 spte = make_nonleaf_spte(sp->spt, !shadow_accessed_mask);
1073         int ret = 0;
1074
1075         if (shared) {
1076                 ret = tdp_mmu_set_spte_atomic(kvm, iter, spte);
1077                 if (ret)
1078                         return ret;
1079         } else {
1080                 tdp_mmu_set_spte(kvm, iter, spte);
1081         }
1082
1083         spin_lock(&kvm->arch.tdp_mmu_pages_lock);
1084         list_add(&sp->link, &kvm->arch.tdp_mmu_pages);
1085         if (account_nx)
1086                 account_huge_nx_page(kvm, sp);
1087         spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
1088
1089         return 0;
1090 }
1091
1092 /*
1093  * Handle a TDP page fault (NPT/EPT violation/misconfiguration) by installing
1094  * page tables and SPTEs to translate the faulting guest physical address.
1095  */
1096 int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
1097 {
1098         struct kvm_mmu *mmu = vcpu->arch.mmu;
1099         struct tdp_iter iter;
1100         struct kvm_mmu_page *sp;
1101         int ret;
1102
1103         kvm_mmu_hugepage_adjust(vcpu, fault);
1104
1105         trace_kvm_mmu_spte_requested(fault);
1106
1107         rcu_read_lock();
1108
1109         tdp_mmu_for_each_pte(iter, mmu, fault->gfn, fault->gfn + 1) {
1110                 if (fault->nx_huge_page_workaround_enabled)
1111                         disallowed_hugepage_adjust(fault, iter.old_spte, iter.level);
1112
1113                 if (iter.level == fault->goal_level)
1114                         break;
1115
1116                 /*
1117                  * If there is an SPTE mapping a large page at a higher level
1118                  * than the target, that SPTE must be cleared and replaced
1119                  * with a non-leaf SPTE.
1120                  */
1121                 if (is_shadow_present_pte(iter.old_spte) &&
1122                     is_large_pte(iter.old_spte)) {
1123                         if (tdp_mmu_zap_spte_atomic(vcpu->kvm, &iter))
1124                                 break;
1125
1126                         /*
1127                          * The iter must explicitly re-read the spte here
1128                          * because the new value informs the !present
1129                          * path below.
1130                          */
1131                         iter.old_spte = kvm_tdp_mmu_read_spte(iter.sptep);
1132                 }
1133
1134                 if (!is_shadow_present_pte(iter.old_spte)) {
1135                         bool account_nx = fault->huge_page_disallowed &&
1136                                           fault->req_level >= iter.level;
1137
1138                         /*
1139                          * If SPTE has been frozen by another thread, just
1140                          * give up and retry, avoiding unnecessary page table
1141                          * allocation and free.
1142                          */
1143                         if (is_removed_spte(iter.old_spte))
1144                                 break;
1145
1146                         sp = tdp_mmu_alloc_sp(vcpu);
1147                         tdp_mmu_init_child_sp(sp, &iter);
1148
1149                         if (tdp_mmu_link_sp(vcpu->kvm, &iter, sp, account_nx, true)) {
1150                                 tdp_mmu_free_sp(sp);
1151                                 break;
1152                         }
1153                 }
1154         }
1155
1156         if (iter.level != fault->goal_level) {
1157                 rcu_read_unlock();
1158                 return RET_PF_RETRY;
1159         }
1160
1161         ret = tdp_mmu_map_handle_target_level(vcpu, fault, &iter);
1162         rcu_read_unlock();
1163
1164         return ret;
1165 }
1166
1167 bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range,
1168                                  bool flush)
1169 {
1170         return kvm_tdp_mmu_zap_leafs(kvm, range->slot->as_id, range->start,
1171                                      range->end, range->may_block, flush);
1172 }
1173
1174 typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter,
1175                               struct kvm_gfn_range *range);
1176
1177 static __always_inline bool kvm_tdp_mmu_handle_gfn(struct kvm *kvm,
1178                                                    struct kvm_gfn_range *range,
1179                                                    tdp_handler_t handler)
1180 {
1181         struct kvm_mmu_page *root;
1182         struct tdp_iter iter;
1183         bool ret = false;
1184
1185         /*
1186          * Don't support rescheduling, none of the MMU notifiers that funnel
1187          * into this helper allow blocking; it'd be dead, wasteful code.
1188          */
1189         for_each_tdp_mmu_root(kvm, root, range->slot->as_id) {
1190                 rcu_read_lock();
1191
1192                 tdp_root_for_each_leaf_pte(iter, root, range->start, range->end)
1193                         ret |= handler(kvm, &iter, range);
1194
1195                 rcu_read_unlock();
1196         }
1197
1198         return ret;
1199 }
1200
1201 /*
1202  * Mark the SPTEs range of GFNs [start, end) unaccessed and return non-zero
1203  * if any of the GFNs in the range have been accessed.
1204  */
1205 static bool age_gfn_range(struct kvm *kvm, struct tdp_iter *iter,
1206                           struct kvm_gfn_range *range)
1207 {
1208         u64 new_spte = 0;
1209
1210         /* If we have a non-accessed entry we don't need to change the pte. */
1211         if (!is_accessed_spte(iter->old_spte))
1212                 return false;
1213
1214         new_spte = iter->old_spte;
1215
1216         if (spte_ad_enabled(new_spte)) {
1217                 new_spte &= ~shadow_accessed_mask;
1218         } else {
1219                 /*
1220                  * Capture the dirty status of the page, so that it doesn't get
1221                  * lost when the SPTE is marked for access tracking.
1222                  */
1223                 if (is_writable_pte(new_spte))
1224                         kvm_set_pfn_dirty(spte_to_pfn(new_spte));
1225
1226                 new_spte = mark_spte_for_access_track(new_spte);
1227         }
1228
1229         tdp_mmu_set_spte_no_acc_track(kvm, iter, new_spte);
1230
1231         return true;
1232 }
1233
1234 bool kvm_tdp_mmu_age_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
1235 {
1236         return kvm_tdp_mmu_handle_gfn(kvm, range, age_gfn_range);
1237 }
1238
1239 static bool test_age_gfn(struct kvm *kvm, struct tdp_iter *iter,
1240                          struct kvm_gfn_range *range)
1241 {
1242         return is_accessed_spte(iter->old_spte);
1243 }
1244
1245 bool kvm_tdp_mmu_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
1246 {
1247         return kvm_tdp_mmu_handle_gfn(kvm, range, test_age_gfn);
1248 }
1249
1250 static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,
1251                          struct kvm_gfn_range *range)
1252 {
1253         u64 new_spte;
1254
1255         /* Huge pages aren't expected to be modified without first being zapped. */
1256         WARN_ON(pte_huge(range->pte) || range->start + 1 != range->end);
1257
1258         if (iter->level != PG_LEVEL_4K ||
1259             !is_shadow_present_pte(iter->old_spte))
1260                 return false;
1261
1262         /*
1263          * Note, when changing a read-only SPTE, it's not strictly necessary to
1264          * zero the SPTE before setting the new PFN, but doing so preserves the
1265          * invariant that the PFN of a present * leaf SPTE can never change.
1266          * See __handle_changed_spte().
1267          */
1268         tdp_mmu_set_spte(kvm, iter, 0);
1269
1270         if (!pte_write(range->pte)) {
1271                 new_spte = kvm_mmu_changed_pte_notifier_make_spte(iter->old_spte,
1272                                                                   pte_pfn(range->pte));
1273
1274                 tdp_mmu_set_spte(kvm, iter, new_spte);
1275         }
1276
1277         return true;
1278 }
1279
1280 /*
1281  * Handle the changed_pte MMU notifier for the TDP MMU.
1282  * data is a pointer to the new pte_t mapping the HVA specified by the MMU
1283  * notifier.
1284  * Returns non-zero if a flush is needed before releasing the MMU lock.
1285  */
1286 bool kvm_tdp_mmu_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
1287 {
1288         /*
1289          * No need to handle the remote TLB flush under RCU protection, the
1290          * target SPTE _must_ be a leaf SPTE, i.e. cannot result in freeing a
1291          * shadow page.  See the WARN on pfn_changed in __handle_changed_spte().
1292          */
1293         return kvm_tdp_mmu_handle_gfn(kvm, range, set_spte_gfn);
1294 }
1295
1296 /*
1297  * Remove write access from all SPTEs at or above min_level that map GFNs
1298  * [start, end). Returns true if an SPTE has been changed and the TLBs need to
1299  * be flushed.
1300  */
1301 static bool wrprot_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
1302                              gfn_t start, gfn_t end, int min_level)
1303 {
1304         struct tdp_iter iter;
1305         u64 new_spte;
1306         bool spte_set = false;
1307
1308         rcu_read_lock();
1309
1310         BUG_ON(min_level > KVM_MAX_HUGEPAGE_LEVEL);
1311
1312         for_each_tdp_pte_min_level(iter, root, min_level, start, end) {
1313 retry:
1314                 if (tdp_mmu_iter_cond_resched(kvm, &iter, false, true))
1315                         continue;
1316
1317                 if (!is_shadow_present_pte(iter.old_spte) ||
1318                     !is_last_spte(iter.old_spte, iter.level) ||
1319                     !(iter.old_spte & PT_WRITABLE_MASK))
1320                         continue;
1321
1322                 new_spte = iter.old_spte & ~PT_WRITABLE_MASK;
1323
1324                 if (tdp_mmu_set_spte_atomic(kvm, &iter, new_spte))
1325                         goto retry;
1326
1327                 spte_set = true;
1328         }
1329
1330         rcu_read_unlock();
1331         return spte_set;
1332 }
1333
1334 /*
1335  * Remove write access from all the SPTEs mapping GFNs in the memslot. Will
1336  * only affect leaf SPTEs down to min_level.
1337  * Returns true if an SPTE has been changed and the TLBs need to be flushed.
1338  */
1339 bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,
1340                              const struct kvm_memory_slot *slot, int min_level)
1341 {
1342         struct kvm_mmu_page *root;
1343         bool spte_set = false;
1344
1345         lockdep_assert_held_read(&kvm->mmu_lock);
1346
1347         for_each_valid_tdp_mmu_root_yield_safe(kvm, root, slot->as_id, true)
1348                 spte_set |= wrprot_gfn_range(kvm, root, slot->base_gfn,
1349                              slot->base_gfn + slot->npages, min_level);
1350
1351         return spte_set;
1352 }
1353
1354 static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp)
1355 {
1356         struct kvm_mmu_page *sp;
1357
1358         gfp |= __GFP_ZERO;
1359
1360         sp = kmem_cache_alloc(mmu_page_header_cache, gfp);
1361         if (!sp)
1362                 return NULL;
1363
1364         sp->spt = (void *)__get_free_page(gfp);
1365         if (!sp->spt) {
1366                 kmem_cache_free(mmu_page_header_cache, sp);
1367                 return NULL;
1368         }
1369
1370         return sp;
1371 }
1372
1373 static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,
1374                                                        struct tdp_iter *iter,
1375                                                        bool shared)
1376 {
1377         struct kvm_mmu_page *sp;
1378
1379         /*
1380          * Since we are allocating while under the MMU lock we have to be
1381          * careful about GFP flags. Use GFP_NOWAIT to avoid blocking on direct
1382          * reclaim and to avoid making any filesystem callbacks (which can end
1383          * up invoking KVM MMU notifiers, resulting in a deadlock).
1384          *
1385          * If this allocation fails we drop the lock and retry with reclaim
1386          * allowed.
1387          */
1388         sp = __tdp_mmu_alloc_sp_for_split(GFP_NOWAIT | __GFP_ACCOUNT);
1389         if (sp)
1390                 return sp;
1391
1392         rcu_read_unlock();
1393
1394         if (shared)
1395                 read_unlock(&kvm->mmu_lock);
1396         else
1397                 write_unlock(&kvm->mmu_lock);
1398
1399         iter->yielded = true;
1400         sp = __tdp_mmu_alloc_sp_for_split(GFP_KERNEL_ACCOUNT);
1401
1402         if (shared)
1403                 read_lock(&kvm->mmu_lock);
1404         else
1405                 write_lock(&kvm->mmu_lock);
1406
1407         rcu_read_lock();
1408
1409         return sp;
1410 }
1411
1412 static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter,
1413                                    struct kvm_mmu_page *sp, bool shared)
1414 {
1415         const u64 huge_spte = iter->old_spte;
1416         const int level = iter->level;
1417         int ret, i;
1418
1419         tdp_mmu_init_child_sp(sp, iter);
1420
1421         /*
1422          * No need for atomics when writing to sp->spt since the page table has
1423          * not been linked in yet and thus is not reachable from any other CPU.
1424          */
1425         for (i = 0; i < PT64_ENT_PER_PAGE; i++)
1426                 sp->spt[i] = make_huge_page_split_spte(huge_spte, level, i);
1427
1428         /*
1429          * Replace the huge spte with a pointer to the populated lower level
1430          * page table. Since we are making this change without a TLB flush vCPUs
1431          * will see a mix of the split mappings and the original huge mapping,
1432          * depending on what's currently in their TLB. This is fine from a
1433          * correctness standpoint since the translation will be the same either
1434          * way.
1435          */
1436         ret = tdp_mmu_link_sp(kvm, iter, sp, false, shared);
1437         if (ret)
1438                 goto out;
1439
1440         /*
1441          * tdp_mmu_link_sp_atomic() will handle subtracting the huge page we
1442          * are overwriting from the page stats. But we have to manually update
1443          * the page stats with the new present child pages.
1444          */
1445         kvm_update_page_stats(kvm, level - 1, PT64_ENT_PER_PAGE);
1446
1447 out:
1448         trace_kvm_mmu_split_huge_page(iter->gfn, huge_spte, level, ret);
1449         return ret;
1450 }
1451
1452 static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,
1453                                          struct kvm_mmu_page *root,
1454                                          gfn_t start, gfn_t end,
1455                                          int target_level, bool shared)
1456 {
1457         struct kvm_mmu_page *sp = NULL;
1458         struct tdp_iter iter;
1459         int ret = 0;
1460
1461         rcu_read_lock();
1462
1463         /*
1464          * Traverse the page table splitting all huge pages above the target
1465          * level into one lower level. For example, if we encounter a 1GB page
1466          * we split it into 512 2MB pages.
1467          *
1468          * Since the TDP iterator uses a pre-order traversal, we are guaranteed
1469          * to visit an SPTE before ever visiting its children, which means we
1470          * will correctly recursively split huge pages that are more than one
1471          * level above the target level (e.g. splitting a 1GB to 512 2MB pages,
1472          * and then splitting each of those to 512 4KB pages).
1473          */
1474         for_each_tdp_pte_min_level(iter, root, target_level + 1, start, end) {
1475 retry:
1476                 if (tdp_mmu_iter_cond_resched(kvm, &iter, false, shared))
1477                         continue;
1478
1479                 if (!is_shadow_present_pte(iter.old_spte) || !is_large_pte(iter.old_spte))
1480                         continue;
1481
1482                 if (!sp) {
1483                         sp = tdp_mmu_alloc_sp_for_split(kvm, &iter, shared);
1484                         if (!sp) {
1485                                 ret = -ENOMEM;
1486                                 trace_kvm_mmu_split_huge_page(iter.gfn,
1487                                                               iter.old_spte,
1488                                                               iter.level, ret);
1489                                 break;
1490                         }
1491
1492                         if (iter.yielded)
1493                                 continue;
1494                 }
1495
1496                 if (tdp_mmu_split_huge_page(kvm, &iter, sp, shared))
1497                         goto retry;
1498
1499                 sp = NULL;
1500         }
1501
1502         rcu_read_unlock();
1503
1504         /*
1505          * It's possible to exit the loop having never used the last sp if, for
1506          * example, a vCPU doing HugePage NX splitting wins the race and
1507          * installs its own sp in place of the last sp we tried to split.
1508          */
1509         if (sp)
1510                 tdp_mmu_free_sp(sp);
1511
1512         return ret;
1513 }
1514
1515
1516 /*
1517  * Try to split all huge pages mapped by the TDP MMU down to the target level.
1518  */
1519 void kvm_tdp_mmu_try_split_huge_pages(struct kvm *kvm,
1520                                       const struct kvm_memory_slot *slot,
1521                                       gfn_t start, gfn_t end,
1522                                       int target_level, bool shared)
1523 {
1524         struct kvm_mmu_page *root;
1525         int r = 0;
1526
1527         kvm_lockdep_assert_mmu_lock_held(kvm, shared);
1528
1529         for_each_valid_tdp_mmu_root_yield_safe(kvm, root, slot->as_id, shared) {
1530                 r = tdp_mmu_split_huge_pages_root(kvm, root, start, end, target_level, shared);
1531                 if (r) {
1532                         kvm_tdp_mmu_put_root(kvm, root, shared);
1533                         break;
1534                 }
1535         }
1536 }
1537
1538 /*
1539  * Clear the dirty status of all the SPTEs mapping GFNs in the memslot. If
1540  * AD bits are enabled, this will involve clearing the dirty bit on each SPTE.
1541  * If AD bits are not enabled, this will require clearing the writable bit on
1542  * each SPTE. Returns true if an SPTE has been changed and the TLBs need to
1543  * be flushed.
1544  */
1545 static bool clear_dirty_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
1546                            gfn_t start, gfn_t end)
1547 {
1548         struct tdp_iter iter;
1549         u64 new_spte;
1550         bool spte_set = false;
1551
1552         rcu_read_lock();
1553
1554         tdp_root_for_each_leaf_pte(iter, root, start, end) {
1555 retry:
1556                 if (tdp_mmu_iter_cond_resched(kvm, &iter, false, true))
1557                         continue;
1558
1559                 if (!is_shadow_present_pte(iter.old_spte))
1560                         continue;
1561
1562                 if (spte_ad_need_write_protect(iter.old_spte)) {
1563                         if (is_writable_pte(iter.old_spte))
1564                                 new_spte = iter.old_spte & ~PT_WRITABLE_MASK;
1565                         else
1566                                 continue;
1567                 } else {
1568                         if (iter.old_spte & shadow_dirty_mask)
1569                                 new_spte = iter.old_spte & ~shadow_dirty_mask;
1570                         else
1571                                 continue;
1572                 }
1573
1574                 if (tdp_mmu_set_spte_atomic(kvm, &iter, new_spte))
1575                         goto retry;
1576
1577                 spte_set = true;
1578         }
1579
1580         rcu_read_unlock();
1581         return spte_set;
1582 }
1583
1584 /*
1585  * Clear the dirty status of all the SPTEs mapping GFNs in the memslot. If
1586  * AD bits are enabled, this will involve clearing the dirty bit on each SPTE.
1587  * If AD bits are not enabled, this will require clearing the writable bit on
1588  * each SPTE. Returns true if an SPTE has been changed and the TLBs need to
1589  * be flushed.
1590  */
1591 bool kvm_tdp_mmu_clear_dirty_slot(struct kvm *kvm,
1592                                   const struct kvm_memory_slot *slot)
1593 {
1594         struct kvm_mmu_page *root;
1595         bool spte_set = false;
1596
1597         lockdep_assert_held_read(&kvm->mmu_lock);
1598
1599         for_each_valid_tdp_mmu_root_yield_safe(kvm, root, slot->as_id, true)
1600                 spte_set |= clear_dirty_gfn_range(kvm, root, slot->base_gfn,
1601                                 slot->base_gfn + slot->npages);
1602
1603         return spte_set;
1604 }
1605
1606 /*
1607  * Clears the dirty status of all the 4k SPTEs mapping GFNs for which a bit is
1608  * set in mask, starting at gfn. The given memslot is expected to contain all
1609  * the GFNs represented by set bits in the mask. If AD bits are enabled,
1610  * clearing the dirty status will involve clearing the dirty bit on each SPTE
1611  * or, if AD bits are not enabled, clearing the writable bit on each SPTE.
1612  */
1613 static void clear_dirty_pt_masked(struct kvm *kvm, struct kvm_mmu_page *root,
1614                                   gfn_t gfn, unsigned long mask, bool wrprot)
1615 {
1616         struct tdp_iter iter;
1617         u64 new_spte;
1618
1619         rcu_read_lock();
1620
1621         tdp_root_for_each_leaf_pte(iter, root, gfn + __ffs(mask),
1622                                     gfn + BITS_PER_LONG) {
1623                 if (!mask)
1624                         break;
1625
1626                 if (iter.level > PG_LEVEL_4K ||
1627                     !(mask & (1UL << (iter.gfn - gfn))))
1628                         continue;
1629
1630                 mask &= ~(1UL << (iter.gfn - gfn));
1631
1632                 if (wrprot || spte_ad_need_write_protect(iter.old_spte)) {
1633                         if (is_writable_pte(iter.old_spte))
1634                                 new_spte = iter.old_spte & ~PT_WRITABLE_MASK;
1635                         else
1636                                 continue;
1637                 } else {
1638                         if (iter.old_spte & shadow_dirty_mask)
1639                                 new_spte = iter.old_spte & ~shadow_dirty_mask;
1640                         else
1641                                 continue;
1642                 }
1643
1644                 tdp_mmu_set_spte_no_dirty_log(kvm, &iter, new_spte);
1645         }
1646
1647         rcu_read_unlock();
1648 }
1649
1650 /*
1651  * Clears the dirty status of all the 4k SPTEs mapping GFNs for which a bit is
1652  * set in mask, starting at gfn. The given memslot is expected to contain all
1653  * the GFNs represented by set bits in the mask. If AD bits are enabled,
1654  * clearing the dirty status will involve clearing the dirty bit on each SPTE
1655  * or, if AD bits are not enabled, clearing the writable bit on each SPTE.
1656  */
1657 void kvm_tdp_mmu_clear_dirty_pt_masked(struct kvm *kvm,
1658                                        struct kvm_memory_slot *slot,
1659                                        gfn_t gfn, unsigned long mask,
1660                                        bool wrprot)
1661 {
1662         struct kvm_mmu_page *root;
1663
1664         lockdep_assert_held_write(&kvm->mmu_lock);
1665         for_each_tdp_mmu_root(kvm, root, slot->as_id)
1666                 clear_dirty_pt_masked(kvm, root, gfn, mask, wrprot);
1667 }
1668
1669 /*
1670  * Clear leaf entries which could be replaced by large mappings, for
1671  * GFNs within the slot.
1672  */
1673 static void zap_collapsible_spte_range(struct kvm *kvm,
1674                                        struct kvm_mmu_page *root,
1675                                        const struct kvm_memory_slot *slot)
1676 {
1677         gfn_t start = slot->base_gfn;
1678         gfn_t end = start + slot->npages;
1679         struct tdp_iter iter;
1680         kvm_pfn_t pfn;
1681
1682         rcu_read_lock();
1683
1684         tdp_root_for_each_pte(iter, root, start, end) {
1685 retry:
1686                 if (tdp_mmu_iter_cond_resched(kvm, &iter, false, true))
1687                         continue;
1688
1689                 if (!is_shadow_present_pte(iter.old_spte) ||
1690                     !is_last_spte(iter.old_spte, iter.level))
1691                         continue;
1692
1693                 pfn = spte_to_pfn(iter.old_spte);
1694                 if (kvm_is_reserved_pfn(pfn) ||
1695                     iter.level >= kvm_mmu_max_mapping_level(kvm, slot, iter.gfn,
1696                                                             pfn, PG_LEVEL_NUM))
1697                         continue;
1698
1699                 /* Note, a successful atomic zap also does a remote TLB flush. */
1700                 if (tdp_mmu_zap_spte_atomic(kvm, &iter))
1701                         goto retry;
1702         }
1703
1704         rcu_read_unlock();
1705 }
1706
1707 /*
1708  * Clear non-leaf entries (and free associated page tables) which could
1709  * be replaced by large mappings, for GFNs within the slot.
1710  */
1711 void kvm_tdp_mmu_zap_collapsible_sptes(struct kvm *kvm,
1712                                        const struct kvm_memory_slot *slot)
1713 {
1714         struct kvm_mmu_page *root;
1715
1716         lockdep_assert_held_read(&kvm->mmu_lock);
1717
1718         for_each_valid_tdp_mmu_root_yield_safe(kvm, root, slot->as_id, true)
1719                 zap_collapsible_spte_range(kvm, root, slot);
1720 }
1721
1722 /*
1723  * Removes write access on the last level SPTE mapping this GFN and unsets the
1724  * MMU-writable bit to ensure future writes continue to be intercepted.
1725  * Returns true if an SPTE was set and a TLB flush is needed.
1726  */
1727 static bool write_protect_gfn(struct kvm *kvm, struct kvm_mmu_page *root,
1728                               gfn_t gfn, int min_level)
1729 {
1730         struct tdp_iter iter;
1731         u64 new_spte;
1732         bool spte_set = false;
1733
1734         BUG_ON(min_level > KVM_MAX_HUGEPAGE_LEVEL);
1735
1736         rcu_read_lock();
1737
1738         for_each_tdp_pte_min_level(iter, root, min_level, gfn, gfn + 1) {
1739                 if (!is_shadow_present_pte(iter.old_spte) ||
1740                     !is_last_spte(iter.old_spte, iter.level))
1741                         continue;
1742
1743                 new_spte = iter.old_spte &
1744                         ~(PT_WRITABLE_MASK | shadow_mmu_writable_mask);
1745
1746                 if (new_spte == iter.old_spte)
1747                         break;
1748
1749                 tdp_mmu_set_spte(kvm, &iter, new_spte);
1750                 spte_set = true;
1751         }
1752
1753         rcu_read_unlock();
1754
1755         return spte_set;
1756 }
1757
1758 /*
1759  * Removes write access on the last level SPTE mapping this GFN and unsets the
1760  * MMU-writable bit to ensure future writes continue to be intercepted.
1761  * Returns true if an SPTE was set and a TLB flush is needed.
1762  */
1763 bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm,
1764                                    struct kvm_memory_slot *slot, gfn_t gfn,
1765                                    int min_level)
1766 {
1767         struct kvm_mmu_page *root;
1768         bool spte_set = false;
1769
1770         lockdep_assert_held_write(&kvm->mmu_lock);
1771         for_each_tdp_mmu_root(kvm, root, slot->as_id)
1772                 spte_set |= write_protect_gfn(kvm, root, gfn, min_level);
1773
1774         return spte_set;
1775 }
1776
1777 /*
1778  * Return the level of the lowest level SPTE added to sptes.
1779  * That SPTE may be non-present.
1780  *
1781  * Must be called between kvm_tdp_mmu_walk_lockless_{begin,end}.
1782  */
1783 int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes,
1784                          int *root_level)
1785 {
1786         struct tdp_iter iter;
1787         struct kvm_mmu *mmu = vcpu->arch.mmu;
1788         gfn_t gfn = addr >> PAGE_SHIFT;
1789         int leaf = -1;
1790
1791         *root_level = vcpu->arch.mmu->shadow_root_level;
1792
1793         tdp_mmu_for_each_pte(iter, mmu, gfn, gfn + 1) {
1794                 leaf = iter.level;
1795                 sptes[leaf] = iter.old_spte;
1796         }
1797
1798         return leaf;
1799 }
1800
1801 /*
1802  * Returns the last level spte pointer of the shadow page walk for the given
1803  * gpa, and sets *spte to the spte value. This spte may be non-preset. If no
1804  * walk could be performed, returns NULL and *spte does not contain valid data.
1805  *
1806  * Contract:
1807  *  - Must be called between kvm_tdp_mmu_walk_lockless_{begin,end}.
1808  *  - The returned sptep must not be used after kvm_tdp_mmu_walk_lockless_end.
1809  *
1810  * WARNING: This function is only intended to be called during fast_page_fault.
1811  */
1812 u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr,
1813                                         u64 *spte)
1814 {
1815         struct tdp_iter iter;
1816         struct kvm_mmu *mmu = vcpu->arch.mmu;
1817         gfn_t gfn = addr >> PAGE_SHIFT;
1818         tdp_ptep_t sptep = NULL;
1819
1820         tdp_mmu_for_each_pte(iter, mmu, gfn, gfn + 1) {
1821                 *spte = iter.old_spte;
1822                 sptep = iter.sptep;
1823         }
1824
1825         /*
1826          * Perform the rcu_dereference to get the raw spte pointer value since
1827          * we are passing it up to fast_page_fault, which is shared with the
1828          * legacy MMU and thus does not retain the TDP MMU-specific __rcu
1829          * annotation.
1830          *
1831          * This is safe since fast_page_fault obeys the contracts of this
1832          * function as well as all TDP MMU contracts around modifying SPTEs
1833          * outside of mmu_lock.
1834          */
1835         return rcu_dereference(sptep);
1836 }