Skip to content

Commit b514c30

Browse files
committed
KVM: MMU: Avoid access/dirty update loop if all is well
Keep track of accessed/dirty bits; if they are all set, do not enter the accessed/dirty update loop. Reviewed-by: Xiao Guangrong <[email protected]> Signed-off-by: Avi Kivity <[email protected]>
1 parent 71331a1 commit b514c30

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

arch/x86/kvm/paging_tmpl.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
151151
pt_element_t pte;
152152
pt_element_t __user *uninitialized_var(ptep_user);
153153
gfn_t table_gfn;
154-
unsigned index, pt_access, pte_access;
154+
unsigned index, pt_access, pte_access, accessed_dirty, shift;
155155
gpa_t pte_gpa;
156156
int offset;
157157
const int write_fault = access & PFERR_WRITE_MASK;
@@ -180,6 +180,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
180180
ASSERT((!is_long_mode(vcpu) && is_pae(vcpu)) ||
181181
(mmu->get_cr3(vcpu) & CR3_NONPAE_RESERVED_BITS) == 0);
182182

183+
accessed_dirty = PT_ACCESSED_MASK;
183184
pt_access = pte_access = ACC_ALL;
184185
++walker->level;
185186

@@ -224,6 +225,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
224225
goto error;
225226
}
226227

228+
accessed_dirty &= pte;
227229
pte_access = pt_access & gpte_access(vcpu, pte);
228230

229231
walker->ptes[walker->level - 1] = pte;
@@ -251,11 +253,23 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,
251253
if (!write_fault)
252254
protect_clean_gpte(&pte_access, pte);
253255

254-
ret = FNAME(update_accessed_dirty_bits)(vcpu, mmu, walker, write_fault);
255-
if (unlikely(ret < 0))
256-
goto error;
257-
else if (ret)
258-
goto retry_walk;
256+
/*
257+
* On a write fault, fold the dirty bit into accessed_dirty by shifting it one
258+
* place right.
259+
*
260+
* On a read fault, do nothing.
261+
*/
262+
shift = write_fault >> ilog2(PFERR_WRITE_MASK);
263+
shift *= PT_DIRTY_SHIFT - PT_ACCESSED_SHIFT;
264+
accessed_dirty &= pte >> shift;
265+
266+
if (unlikely(!accessed_dirty)) {
267+
ret = FNAME(update_accessed_dirty_bits)(vcpu, mmu, walker, write_fault);
268+
if (unlikely(ret < 0))
269+
goto error;
270+
else if (ret)
271+
goto retry_walk;
272+
}
259273

260274
walker->pt_access = pt_access;
261275
walker->pte_access = pte_access;

0 commit comments

Comments
 (0)