KVM
Classes | Macros | Typedefs | Enumerations | Functions | Variables
mmu_internal.h File Reference
#include <linux/types.h>
#include <linux/kvm_host.h>
#include <asm/kvm_host.h>
Include dependency graph for mmu_internal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  kvm_mmu_page
 
struct  kvm_page_fault
 

Macros

#define KVM_MMU_WARN_ON(x)   BUILD_BUG_ON_INVALID(x)
 
#define __PT_BASE_ADDR_MASK   GENMASK_ULL(51, 12)
 
#define __PT_LEVEL_SHIFT(level, bits_per_level)    (PAGE_SHIFT + ((level) - 1) * (bits_per_level))
 
#define __PT_INDEX(address, level, bits_per_level)    (((address) >> __PT_LEVEL_SHIFT(level, bits_per_level)) & ((1 << (bits_per_level)) - 1))
 
#define __PT_LVL_ADDR_MASK(base_addr_mask, level, bits_per_level)    ((base_addr_mask) & ~((1ULL << (PAGE_SHIFT + (((level) - 1) * (bits_per_level)))) - 1))
 
#define __PT_LVL_OFFSET_MASK(base_addr_mask, level, bits_per_level)    ((base_addr_mask) & ((1ULL << (PAGE_SHIFT + (((level) - 1) * (bits_per_level)))) - 1))
 
#define __PT_ENT_PER_PAGE(bits_per_level)   (1 << (bits_per_level))
 
#define INVALID_PAE_ROOT   0
 
#define IS_VALID_PAE_ROOT(x)   (!!(x))
 

Typedefs

typedef u64 __rcu * tdp_ptep_t
 

Enumerations

enum  {
  RET_PF_CONTINUE = 0 , RET_PF_RETRY , RET_PF_EMULATE , RET_PF_INVALID ,
  RET_PF_FIXED , RET_PF_SPURIOUS
}
 

Functions

static hpa_t kvm_mmu_get_dummy_root (void)
 
static bool kvm_mmu_is_dummy_root (hpa_t shadow_page)
 
static int kvm_mmu_role_as_id (union kvm_mmu_page_role role)
 
static int kvm_mmu_page_as_id (struct kvm_mmu_page *sp)
 
static bool kvm_mmu_page_ad_need_write_protect (struct kvm_mmu_page *sp)
 
static gfn_t gfn_round_for_level (gfn_t gfn, int level)
 
int mmu_try_to_unsync_pages (struct kvm *kvm, const struct kvm_memory_slot *slot, gfn_t gfn, bool can_unsync, bool prefetch)
 
void kvm_mmu_gfn_disallow_lpage (const struct kvm_memory_slot *slot, gfn_t gfn)
 
void kvm_mmu_gfn_allow_lpage (const struct kvm_memory_slot *slot, gfn_t gfn)
 
bool kvm_mmu_slot_gfn_write_protect (struct kvm *kvm, struct kvm_memory_slot *slot, u64 gfn, int min_level)
 
static void kvm_flush_remote_tlbs_gfn (struct kvm *kvm, gfn_t gfn, int level)
 
unsigned int pte_list_count (struct kvm_rmap_head *rmap_head)
 
static bool is_nx_huge_page_enabled (struct kvm *kvm)
 
int kvm_tdp_page_fault (struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
 
static int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 err, bool prefetch, int *emulation_type)
 
int kvm_mmu_max_mapping_level (struct kvm *kvm, const struct kvm_memory_slot *slot, gfn_t gfn, int max_level)
 
void kvm_mmu_hugepage_adjust (struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
 
void disallowed_hugepage_adjust (struct kvm_page_fault *fault, u64 spte, int cur_level)
 
void * mmu_memory_cache_alloc (struct kvm_mmu_memory_cache *mc)
 
void track_possible_nx_huge_page (struct kvm *kvm, struct kvm_mmu_page *sp)
 
void untrack_possible_nx_huge_page (struct kvm *kvm, struct kvm_mmu_page *sp)
 

Variables

struct kmem_cache * mmu_page_header_cache
 
int nx_huge_pages
 

Macro Definition Documentation

◆ __PT_BASE_ADDR_MASK

#define __PT_BASE_ADDR_MASK   GENMASK_ULL(51, 12)

Definition at line 16 of file mmu_internal.h.

◆ __PT_ENT_PER_PAGE

#define __PT_ENT_PER_PAGE (   bits_per_level)    (1 << (bits_per_level))

Definition at line 28 of file mmu_internal.h.

◆ __PT_INDEX

#define __PT_INDEX (   address,
  level,
  bits_per_level 
)     (((address) >> __PT_LEVEL_SHIFT(level, bits_per_level)) & ((1 << (bits_per_level)) - 1))

Definition at line 19 of file mmu_internal.h.

◆ __PT_LEVEL_SHIFT

#define __PT_LEVEL_SHIFT (   level,
  bits_per_level 
)     (PAGE_SHIFT + ((level) - 1) * (bits_per_level))

Definition at line 17 of file mmu_internal.h.

◆ __PT_LVL_ADDR_MASK

#define __PT_LVL_ADDR_MASK (   base_addr_mask,
  level,
  bits_per_level 
)     ((base_addr_mask) & ~((1ULL << (PAGE_SHIFT + (((level) - 1) * (bits_per_level)))) - 1))

Definition at line 22 of file mmu_internal.h.

◆ __PT_LVL_OFFSET_MASK

#define __PT_LVL_OFFSET_MASK (   base_addr_mask,
  level,
  bits_per_level 
)     ((base_addr_mask) & ((1ULL << (PAGE_SHIFT + (((level) - 1) * (bits_per_level)))) - 1))

Definition at line 25 of file mmu_internal.h.

◆ INVALID_PAE_ROOT

#define INVALID_PAE_ROOT   0

Definition at line 37 of file mmu_internal.h.

◆ IS_VALID_PAE_ROOT

#define IS_VALID_PAE_ROOT (   x)    (!!(x))

Definition at line 38 of file mmu_internal.h.

◆ KVM_MMU_WARN_ON

#define KVM_MMU_WARN_ON (   x)    BUILD_BUG_ON_INVALID(x)

Definition at line 12 of file mmu_internal.h.

Typedef Documentation

◆ tdp_ptep_t

typedef u64 __rcu* tdp_ptep_t

Definition at line 50 of file mmu_internal.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
RET_PF_CONTINUE 
RET_PF_RETRY 
RET_PF_EMULATE 
RET_PF_INVALID 
RET_PF_FIXED 
RET_PF_SPURIOUS 

Definition at line 273 of file mmu_internal.h.

273  {
274  RET_PF_CONTINUE = 0,
275  RET_PF_RETRY,
278  RET_PF_FIXED,
280 };
@ RET_PF_RETRY
Definition: mmu_internal.h:275
@ RET_PF_INVALID
Definition: mmu_internal.h:277
@ RET_PF_FIXED
Definition: mmu_internal.h:278
@ RET_PF_EMULATE
Definition: mmu_internal.h:276
@ RET_PF_SPURIOUS
Definition: mmu_internal.h:279
@ RET_PF_CONTINUE
Definition: mmu_internal.h:274

Function Documentation

◆ disallowed_hugepage_adjust()

void disallowed_hugepage_adjust ( struct kvm_page_fault fault,
u64  spte,
int  cur_level 
)

Definition at line 3216 of file mmu.c.

3217 {
3218  if (cur_level > PG_LEVEL_4K &&
3219  cur_level == fault->goal_level &&
3220  is_shadow_present_pte(spte) &&
3221  !is_large_pte(spte) &&
3222  spte_to_child_sp(spte)->nx_huge_page_disallowed) {
3223  /*
3224  * A small SPTE exists for this pfn, but FNAME(fetch),
3225  * direct_map(), or kvm_tdp_mmu_map() would like to create a
3226  * large PTE instead: just force them to go down another level,
3227  * patching back for them into pfn the next 9 bits of the
3228  * address.
3229  */
3230  u64 page_mask = KVM_PAGES_PER_HPAGE(cur_level) -
3231  KVM_PAGES_PER_HPAGE(cur_level - 1);
3232  fault->pfn |= fault->gfn & page_mask;
3233  fault->goal_level--;
3234  }
3235 }
static bool is_shadow_present_pte(u64 pte)
Definition: spte.h:258
static bool is_large_pte(u64 pte)
Definition: spte.h:313
static struct kvm_mmu_page * spte_to_child_sp(u64 spte)
Definition: spte.h:230
kvm_pfn_t pfn
Definition: mmu_internal.h:240
Here is the call graph for this function:
Here is the caller graph for this function:

◆ gfn_round_for_level()

static gfn_t gfn_round_for_level ( gfn_t  gfn,
int  level 
)
inlinestatic

Definition at line 161 of file mmu_internal.h.

162 {
163  return gfn & -KVM_PAGES_PER_HPAGE(level);
164 }
Here is the caller graph for this function:

◆ is_nx_huge_page_enabled()

static bool is_nx_huge_page_enabled ( struct kvm *  kvm)
inlinestatic

Definition at line 185 of file mmu_internal.h.

186 {
187  return READ_ONCE(nx_huge_pages) && !kvm->arch.disable_nx_huge_pages;
188 }
int nx_huge_pages
Definition: mmu.c:64
Here is the caller graph for this function:

◆ kvm_flush_remote_tlbs_gfn()

static void kvm_flush_remote_tlbs_gfn ( struct kvm *  kvm,
gfn_t  gfn,
int  level 
)
inlinestatic

Definition at line 176 of file mmu_internal.h.

177 {
179  KVM_PAGES_PER_HPAGE(level));
180 }
void kvm_flush_remote_tlbs_range(struct kvm *kvm, gfn_t gfn, u64 nr_pages)
Definition: kvm_main.c:367
static gfn_t gfn_round_for_level(gfn_t gfn, int level)
Definition: mmu_internal.h:161
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_mmu_do_page_fault()

static int kvm_mmu_do_page_fault ( struct kvm_vcpu *  vcpu,
gpa_t  cr2_or_gpa,
u32  err,
bool  prefetch,
int *  emulation_type 
)
inlinestatic

Definition at line 282 of file mmu_internal.h.

284 {
285  struct kvm_page_fault fault = {
286  .addr = cr2_or_gpa,
287  .error_code = err,
288  .exec = err & PFERR_FETCH_MASK,
289  .write = err & PFERR_WRITE_MASK,
290  .present = err & PFERR_PRESENT_MASK,
291  .rsvd = err & PFERR_RSVD_MASK,
292  .user = err & PFERR_USER_MASK,
293  .prefetch = prefetch,
294  .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault),
295  .nx_huge_page_workaround_enabled =
296  is_nx_huge_page_enabled(vcpu->kvm),
297 
298  .max_level = KVM_MAX_HUGEPAGE_LEVEL,
299  .req_level = PG_LEVEL_4K,
300  .goal_level = PG_LEVEL_4K,
301  .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT),
302  };
303  int r;
304 
305  if (vcpu->arch.mmu->root_role.direct) {
306  fault.gfn = fault.addr >> PAGE_SHIFT;
307  fault.slot = kvm_vcpu_gfn_to_memslot(vcpu, fault.gfn);
308  }
309 
310  /*
311  * Async #PF "faults", a.k.a. prefetch faults, are not faults from the
312  * guest perspective and have already been counted at the time of the
313  * original fault.
314  */
315  if (!prefetch)
316  vcpu->stat.pf_taken++;
317 
318  if (IS_ENABLED(CONFIG_RETPOLINE) && fault.is_tdp)
319  r = kvm_tdp_page_fault(vcpu, &fault);
320  else
321  r = vcpu->arch.mmu->page_fault(vcpu, &fault);
322 
323  if (fault.write_fault_to_shadow_pgtable && emulation_type)
324  *emulation_type |= EMULTYPE_WRITE_PF_TO_SP;
325 
326  /*
327  * Similar to above, prefetch faults aren't truly spurious, and the
328  * async #PF path doesn't do emulation. Do count faults that are fixed
329  * by the async #PF handler though, otherwise they'll never be counted.
330  */
331  if (r == RET_PF_FIXED)
332  vcpu->stat.pf_fixed++;
333  else if (prefetch)
334  ;
335  else if (r == RET_PF_EMULATE)
336  vcpu->stat.pf_emulate++;
337  else if (r == RET_PF_SPURIOUS)
338  vcpu->stat.pf_spurious++;
339  return r;
340 }
struct kvm_memory_slot * kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn)
Definition: kvm_main.c:2636
int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
Definition: mmu.c:4623
static bool is_nx_huge_page_enabled(struct kvm *kvm)
Definition: mmu_internal.h:185
bool write_fault_to_shadow_pgtable
Definition: mmu_internal.h:249
const bool prefetch
Definition: mmu_internal.h:194
const bool is_tdp
Definition: mmu_internal.h:204
struct kvm_memory_slot * slot
Definition: mmu_internal.h:236
const gpa_t addr
Definition: mmu_internal.h:192
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_mmu_get_dummy_root()

static hpa_t kvm_mmu_get_dummy_root ( void  )
inlinestatic

Definition at line 40 of file mmu_internal.h.

41 {
42  return my_zero_pfn(0) << PAGE_SHIFT;
43 }
Here is the caller graph for this function:

◆ kvm_mmu_gfn_allow_lpage()

void kvm_mmu_gfn_allow_lpage ( const struct kvm_memory_slot *  slot,
gfn_t  gfn 
)

Definition at line 822 of file mmu.c.

823 {
824  update_gfn_disallow_lpage_count(slot, gfn, -1);
825 }
static void update_gfn_disallow_lpage_count(const struct kvm_memory_slot *slot, gfn_t gfn, int count)
Definition: mmu.c:802
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_mmu_gfn_disallow_lpage()

void kvm_mmu_gfn_disallow_lpage ( const struct kvm_memory_slot *  slot,
gfn_t  gfn 
)

Definition at line 817 of file mmu.c.

818 {
819  update_gfn_disallow_lpage_count(slot, gfn, 1);
820 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_mmu_hugepage_adjust()

void kvm_mmu_hugepage_adjust ( struct kvm_vcpu *  vcpu,
struct kvm_page_fault fault 
)

Definition at line 3180 of file mmu.c.

3181 {
3182  struct kvm_memory_slot *slot = fault->slot;
3183  kvm_pfn_t mask;
3184 
3185  fault->huge_page_disallowed = fault->exec && fault->nx_huge_page_workaround_enabled;
3186 
3187  if (unlikely(fault->max_level == PG_LEVEL_4K))
3188  return;
3189 
3190  if (is_error_noslot_pfn(fault->pfn))
3191  return;
3192 
3193  if (kvm_slot_dirty_track_enabled(slot))
3194  return;
3195 
3196  /*
3197  * Enforce the iTLB multihit workaround after capturing the requested
3198  * level, which will be used to do precise, accurate accounting.
3199  */
3200  fault->req_level = __kvm_mmu_max_mapping_level(vcpu->kvm, slot,
3201  fault->gfn, fault->max_level,
3202  fault->is_private);
3203  if (fault->req_level == PG_LEVEL_4K || fault->huge_page_disallowed)
3204  return;
3205 
3206  /*
3207  * mmu_invalidate_retry() was successful and mmu_lock is held, so
3208  * the pmd can't be split from under us.
3209  */
3210  fault->goal_level = fault->req_level;
3211  mask = KVM_PAGES_PER_HPAGE(fault->goal_level) - 1;
3212  VM_BUG_ON((fault->gfn & mask) != (fault->pfn & mask));
3213  fault->pfn &= ~mask;
3214 }
static int __kvm_mmu_max_mapping_level(struct kvm *kvm, const struct kvm_memory_slot *slot, gfn_t gfn, int max_level, bool is_private)
Definition: mmu.c:3146
bool huge_page_disallowed
Definition: mmu_internal.h:212
const bool nx_huge_page_workaround_enabled
Definition: mmu_internal.h:206
const bool is_private
Definition: mmu_internal.h:205
const bool exec
Definition: mmu_internal.h:197
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_mmu_is_dummy_root()

static bool kvm_mmu_is_dummy_root ( hpa_t  shadow_page)
inlinestatic

Definition at line 45 of file mmu_internal.h.

46 {
47  return is_zero_pfn(shadow_page >> PAGE_SHIFT);
48 }
Here is the caller graph for this function:

◆ kvm_mmu_max_mapping_level()

int kvm_mmu_max_mapping_level ( struct kvm *  kvm,
const struct kvm_memory_slot *  slot,
gfn_t  gfn,
int  max_level 
)

Definition at line 3170 of file mmu.c.

3173 {
3174  bool is_private = kvm_slot_can_be_private(slot) &&
3175  kvm_mem_is_private(kvm, gfn);
3176 
3177  return __kvm_mmu_max_mapping_level(kvm, slot, gfn, max_level, is_private);
3178 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_mmu_page_ad_need_write_protect()

static bool kvm_mmu_page_ad_need_write_protect ( struct kvm_mmu_page sp)
inlinestatic

Definition at line 148 of file mmu_internal.h.

149 {
150  /*
151  * When using the EPT page-modification log, the GPAs in the CPU dirty
152  * log would come from L2 rather than L1. Therefore, we need to rely
153  * on write protection to record dirty pages, which bypasses PML, since
154  * writes now result in a vmexit. Note, the check on CPU dirty logging
155  * being enabled is mandatory as the bits used to denote WP-only SPTEs
156  * are reserved for PAE paging (32-bit KVM).
157  */
158  return kvm_x86_ops.cpu_dirty_log_size && sp->role.guest_mode;
159 }
union kvm_mmu_page_role role
Definition: mmu_internal.h:80
Here is the caller graph for this function:

◆ kvm_mmu_page_as_id()

static int kvm_mmu_page_as_id ( struct kvm_mmu_page sp)
inlinestatic

Definition at line 143 of file mmu_internal.h.

144 {
145  return kvm_mmu_role_as_id(sp->role);
146 }
static int kvm_mmu_role_as_id(union kvm_mmu_page_role role)
Definition: mmu_internal.h:138
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_mmu_role_as_id()

static int kvm_mmu_role_as_id ( union kvm_mmu_page_role  role)
inlinestatic

Definition at line 138 of file mmu_internal.h.

139 {
140  return role.smm ? 1 : 0;
141 }
Here is the caller graph for this function:

◆ kvm_mmu_slot_gfn_write_protect()

bool kvm_mmu_slot_gfn_write_protect ( struct kvm *  kvm,
struct kvm_memory_slot *  slot,
u64  gfn,
int  min_level 
)

Definition at line 1414 of file mmu.c.

1417 {
1418  struct kvm_rmap_head *rmap_head;
1419  int i;
1420  bool write_protected = false;
1421 
1422  if (kvm_memslots_have_rmaps(kvm)) {
1423  for (i = min_level; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) {
1424  rmap_head = gfn_to_rmap(gfn, i, slot);
1425  write_protected |= rmap_write_protect(rmap_head, true);
1426  }
1427  }
1428 
1429  if (tdp_mmu_enabled)
1430  write_protected |=
1431  kvm_tdp_mmu_write_protect_gfn(kvm, slot, gfn, min_level);
1432 
1433  return write_protected;
1434 }
static bool rmap_write_protect(struct kvm_rmap_head *rmap_head, bool pt_protect)
Definition: mmu.c:1244
static struct kvm_rmap_head * gfn_to_rmap(gfn_t gfn, int level, const struct kvm_memory_slot *slot)
Definition: mmu.c:1086
#define tdp_mmu_enabled
Definition: mmu.h:276
static bool kvm_memslots_have_rmaps(struct kvm *kvm)
Definition: mmu.h:279
bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, int min_level)
Definition: tdp_mmu.c:1746
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_tdp_page_fault()

int kvm_tdp_page_fault ( struct kvm_vcpu *  vcpu,
struct kvm_page_fault fault 
)

Definition at line 4623 of file mmu.c.

4624 {
4625  /*
4626  * If the guest's MTRRs may be used to compute the "real" memtype,
4627  * restrict the mapping level to ensure KVM uses a consistent memtype
4628  * across the entire mapping.
4629  */
4630  if (kvm_mmu_honors_guest_mtrrs(vcpu->kvm)) {
4631  for ( ; fault->max_level > PG_LEVEL_4K; --fault->max_level) {
4632  int page_num = KVM_PAGES_PER_HPAGE(fault->max_level);
4633  gfn_t base = gfn_round_for_level(fault->gfn,
4634  fault->max_level);
4635 
4636  if (kvm_mtrr_check_gfn_range_consistency(vcpu, base, page_num))
4637  break;
4638  }
4639  }
4640 
4641 #ifdef CONFIG_X86_64
4642  if (tdp_mmu_enabled)
4643  return kvm_tdp_mmu_page_fault(vcpu, fault);
4644 #endif
4645 
4646  return direct_page_fault(vcpu, fault);
4647 }
static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
Definition: mmu.c:4491
static bool kvm_mmu_honors_guest_mtrrs(struct kvm *kvm)
Definition: mmu.h:250
bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, int page_num)
Definition: mtrr.c:690
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mmu_memory_cache_alloc()

void* mmu_memory_cache_alloc ( struct kvm_mmu_memory_cache *  mc)

◆ mmu_try_to_unsync_pages()

int mmu_try_to_unsync_pages ( struct kvm *  kvm,
const struct kvm_memory_slot *  slot,
gfn_t  gfn,
bool  can_unsync,
bool  prefetch 
)

Definition at line 2805 of file mmu.c.

2807 {
2808  struct kvm_mmu_page *sp;
2809  bool locked = false;
2810 
2811  /*
2812  * Force write-protection if the page is being tracked. Note, the page
2813  * track machinery is used to write-protect upper-level shadow pages,
2814  * i.e. this guards the role.level == 4K assertion below!
2815  */
2816  if (kvm_gfn_is_write_tracked(kvm, slot, gfn))
2817  return -EPERM;
2818 
2819  /*
2820  * The page is not write-tracked, mark existing shadow pages unsync
2821  * unless KVM is synchronizing an unsync SP (can_unsync = false). In
2822  * that case, KVM must complete emulation of the guest TLB flush before
2823  * allowing shadow pages to become unsync (writable by the guest).
2824  */
2826  if (!can_unsync)
2827  return -EPERM;
2828 
2829  if (sp->unsync)
2830  continue;
2831 
2832  if (prefetch)
2833  return -EEXIST;
2834 
2835  /*
2836  * TDP MMU page faults require an additional spinlock as they
2837  * run with mmu_lock held for read, not write, and the unsync
2838  * logic is not thread safe. Take the spinklock regardless of
2839  * the MMU type to avoid extra conditionals/parameters, there's
2840  * no meaningful penalty if mmu_lock is held for write.
2841  */
2842  if (!locked) {
2843  locked = true;
2844  spin_lock(&kvm->arch.mmu_unsync_pages_lock);
2845 
2846  /*
2847  * Recheck after taking the spinlock, a different vCPU
2848  * may have since marked the page unsync. A false
2849  * negative on the unprotected check above is not
2850  * possible as clearing sp->unsync _must_ hold mmu_lock
2851  * for write, i.e. unsync cannot transition from 1->0
2852  * while this CPU holds mmu_lock for read (or write).
2853  */
2854  if (READ_ONCE(sp->unsync))
2855  continue;
2856  }
2857 
2858  WARN_ON_ONCE(sp->role.level != PG_LEVEL_4K);
2859  kvm_unsync_page(kvm, sp);
2860  }
2861  if (locked)
2862  spin_unlock(&kvm->arch.mmu_unsync_pages_lock);
2863 
2864  /*
2865  * We need to ensure that the marking of unsync pages is visible
2866  * before the SPTE is updated to allow writes because
2867  * kvm_mmu_sync_roots() checks the unsync flags without holding
2868  * the MMU lock and so can race with this. If the SPTE was updated
2869  * before the page had been marked as unsync-ed, something like the
2870  * following could happen:
2871  *
2872  * CPU 1 CPU 2
2873  * ---------------------------------------------------------------------
2874  * 1.2 Host updates SPTE
2875  * to be writable
2876  * 2.1 Guest writes a GPTE for GVA X.
2877  * (GPTE being in the guest page table shadowed
2878  * by the SP from CPU 1.)
2879  * This reads SPTE during the page table walk.
2880  * Since SPTE.W is read as 1, there is no
2881  * fault.
2882  *
2883  * 2.2 Guest issues TLB flush.
2884  * That causes a VM Exit.
2885  *
2886  * 2.3 Walking of unsync pages sees sp->unsync is
2887  * false and skips the page.
2888  *
2889  * 2.4 Guest accesses GVA X.
2890  * Since the mapping in the SP was not updated,
2891  * so the old mapping for GVA X incorrectly
2892  * gets used.
2893  * 1.1 Host marks SP
2894  * as unsync
2895  * (sp->unsync = true)
2896  *
2897  * The write barrier below ensures that 1.1 happens before 1.2 and thus
2898  * the situation in 2.4 does not arise. It pairs with the read barrier
2899  * in is_unsync_root(), placed between 2.1's load of SPTE.W and 2.3.
2900  */
2901  smp_wmb();
2902 
2903  return 0;
2904 }
static void kvm_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
Definition: mmu.c:2790
#define for_each_gfn_valid_sp_with_gptes(_kvm, _sp, _gfn)
Definition: mmu.c:1913
bool kvm_gfn_is_write_tracked(struct kvm *kvm, const struct kvm_memory_slot *slot, gfn_t gfn)
Definition: page_track.c:123
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pte_list_count()

unsigned int pte_list_count ( struct kvm_rmap_head *  rmap_head)

Definition at line 1073 of file mmu.c.

1074 {
1075  struct pte_list_desc *desc;
1076 
1077  if (!rmap_head->val)
1078  return 0;
1079  else if (!(rmap_head->val & 1))
1080  return 1;
1081 
1082  desc = (struct pte_list_desc *)(rmap_head->val & ~1ul);
1083  return desc->tail_count + desc->spte_count;
1084 }
u32 tail_count
Definition: mmu.c:151
u32 spte_count
Definition: mmu.c:149
Here is the caller graph for this function:

◆ track_possible_nx_huge_page()

void track_possible_nx_huge_page ( struct kvm *  kvm,
struct kvm_mmu_page sp 
)

Definition at line 848 of file mmu.c.

849 {
850  /*
851  * If it's possible to replace the shadow page with an NX huge page,
852  * i.e. if the shadow page is the only thing currently preventing KVM
853  * from using a huge page, add the shadow page to the list of "to be
854  * zapped for NX recovery" pages. Note, the shadow page can already be
855  * on the list if KVM is reusing an existing shadow page, i.e. if KVM
856  * links a shadow page at multiple points.
857  */
858  if (!list_empty(&sp->possible_nx_huge_page_link))
859  return;
860 
861  ++kvm->stat.nx_lpage_splits;
862  list_add_tail(&sp->possible_nx_huge_page_link,
863  &kvm->arch.possible_nx_huge_pages);
864 }
struct list_head possible_nx_huge_page_link
Definition: mmu_internal.h:118
Here is the caller graph for this function:

◆ untrack_possible_nx_huge_page()

void untrack_possible_nx_huge_page ( struct kvm *  kvm,
struct kvm_mmu_page sp 
)

Definition at line 891 of file mmu.c.

892 {
893  if (list_empty(&sp->possible_nx_huge_page_link))
894  return;
895 
896  --kvm->stat.nx_lpage_splits;
897  list_del_init(&sp->possible_nx_huge_page_link);
898 }
Here is the caller graph for this function:

Variable Documentation

◆ mmu_page_header_cache

struct kmem_cache* mmu_page_header_cache
extern

Definition at line 181 of file mmu.c.

◆ nx_huge_pages

int nx_huge_pages
extern

Definition at line 64 of file mmu.c.