From: Junaid Shahid Subject: Re: [PATCH v2 4/5] kvm: x86: mmu: Lockless access tracking for Intel CPUs without EPT A bits. Date: Wed, 23 Nov 2016 19:50:43 -0800 Message-ID: <41116680.VFaKihePAT@js-desktop.mtv.corp.google.com> References: <1478646030-101103-5-git-send-email-junaids@google.com> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Cc: kvm@vger.kernel.org, andreslc@google.com, pfeiner@google.com, guangrong.xiao@linux.intel.com To: Paolo Bonzini Return-path: Received: from mail-pg0-f45.google.com ([74.125.83.45]:35699 "EHLO mail-pg0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754500AbcKXDuq (ORCPT ); Wed, 23 Nov 2016 22:50:46 -0500 Received: by mail-pg0-f45.google.com with SMTP id p66so13374201pga.2 for ; Wed, 23 Nov 2016 19:50:45 -0800 (PST) In-Reply-To: Sender: kvm-owner@vger.kernel.org List-ID: Hi Paolo, Thank you for the detailed feedback. I will send an updated version of = the patch soon. A few comments below: On Monday, November 21, 2016 03:42:23 PM Paolo Bonzini wrote: > > @@ -576,20 +617,34 @@ static bool mmu_spte_update(u64 *sptep, u64 n= ew_spte) > > =09 */ > > =09if (spte_is_bit_changed(old_spte, new_spte, > > shadow_accessed_mask | shadow_dirt= y_mask)) > > -=09=09ret =3D true; > > +=09=09flush =3D true; > > =20 > > -=09if (spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mas= k)) > > +=09writable_cleared =3D is_writable_pte(old_spte) && > > +=09=09=09 !is_writable_pte(new_spte); > > +=09acc_track_enabled =3D !is_access_track_spte(old_spte) && > > +=09=09=09 is_access_track_spte(new_spte); > > + > > +=09if (writable_cleared || acc_track_enabled) > > +=09=09flush =3D true; > > + > > +=09if (shadow_accessed_mask ? > > +=09 spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mas= k) : > > +=09 acc_track_enabled) >=20 > Please introduce a new function spte_is_access_tracking_enabled(u64=20= > old_spte, u64 new_spte) and use it here: >=20 > =09if (shadow_accessed_mask ? > =09 spte_is_bit_cleared(old_spte, new_spte, shadow_accessed_mask) = : > =09 spte_is_access_tracking_enabled(old_spte, new_spte)) { > =09=09flush |=3D !shadow_accessed_mask; > =09=09kvm_set_pfn_accessed(spte_to_pfn(old_spte)); > =09} >=20 I think we can just set flush =3D true in the then block instead of=20 flush |=3D !shadow_accessed_mask. And while we are at it, is there any reason to flush the TLB when setti= ng the A or D bit in the PTE? If not, we can remove this earlier block since t= he clearing case is now handled in the separate if blocks for accessed and= dirty: =09if (spte_is_bit_changed(old_spte, new_spte, shadow_accessed_mask | shadow_dirty_mask)) =09=09flush =3D true; Also, instead of spte_is_access_tracking_enabled(), I=E2=80=99ve added = is_accessed_spte as you suggested later and used that here as well. > > -=09if (spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask))= > > + > > +=09if (shadow_dirty_mask ? > > +=09 spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask) = : > > +=09 writable_cleared) >=20 > writable_cleared can be inline here and written as >=20 > =09spte_is_bit_cleared(old_spte, new_spte, PT_WRITABLE_MASK) >=20 > so >=20 > =09if (shadow_dirty_mask ? > =09 spte_is_bit_cleared(old_spte, new_spte, shadow_dirty_mask) : > =09 spte_is_bit_cleared(old_spte, new_spte, PT_WRITABLE_MASK)) { > =09=09flush |=3D !shadow_dirty_mask; > =09=09kvm_set_pfn_dirty(spte_to_pfn(old_spte)); > =09} > > =09=09kvm_set_pfn_dirty(spte_to_pfn(old_spte)); > > =20 > > -=09return ret; > > +=09return flush; > > } >=20 > But please anticipate the changes to this function, except for=20 > introducing is_access_track_spte of course, to a separate function. Sorry, I didn=E2=80=99t exactly understand what you meant by the last l= ine. But I have made it like this: =09if (is_accessed_spte(old_spte) && !is_accessed_spte(new_spte)) { =09=09flush =3D true; =09=09kvm_set_pfn_accessed(spte_to_pfn(old_spte)); =09} =09if (is_dirty_spte(old_spte) && !is_dirty_spte(new_spte)) { =09=09flush =3D true; =09=09kvm_set_pfn_dirty(spte_to_pfn(old_spte)); =09} > > static int mmu_spte_clear_track_bits(u64 *sptep) > > { > > @@ -604,6 +659,13 @@ static int mmu_spte_clear_track_bits(u64 *spte= p) > > =09if (!is_shadow_present_pte(old_spte)) > > =09=09return 0; > > =20 > > +=09/* > > +=09 * For access tracking SPTEs, the pfn was already marked access= ed/dirty > > +=09 * when the SPTE was marked for access tracking, so nothing to = do here. > > +=09 */ > > +=09if (is_access_track_spte(old_spte)) > > +=09=09return 1; >=20 > This should go after the "WARN_ON", since that's a valuable check. I= n=20 > addition, I think it's a good idea to keep similar idioms between=20 > mmu_spte_update and mmu_spte_clear_track_bits, like this: >=20 > =09if (shadow_accessed_mask > =09 ? old_spte & shadow_accessed_mask > =09 : !is_access_track_spte(old_spte)) > =09=09kvm_set_pfn_accessed(pfn); > if (shadow_dirty_mask > =09 ? old_spte & shadow_dirty_mask > =09 : old_spte & PT_WRITABLE_MASK) > kvm_set_pfn_dirty(pfn); >=20 > =09return 1; >=20 > or (you pick) >=20 > =09if (shadow_accessed_mask > =09 ? !(old_spte & shadow_accessed_mask) > =09 : is_access_track_spte(old_spte)) > =09=09return 1; >=20 > =09kvm_set_pfn_accessed(pfn); > if (shadow_dirty_mask > =09 ? old_spte & shadow_dirty_mask > =09 : old_spte & PT_WRITABLE_MASK) > kvm_set_pfn_dirty(pfn); >=20 > =09return 1; I=E2=80=99ve replaced the checks with if(is_accessed_spte...) / if (is_= dirty_spte...) > > +=09/* > > +=09 * Any PTE marked for access tracking should also be marked for= dirty > > +=09 * tracking (by being non-writable) > > +=09 */ > > +=09spte &=3D ~PT_WRITABLE_MASK; >=20 > This should be implicit in the definition of=20 > shadow_acc_track_mask/value, so it's not necessary (or it can be a=20= > WARN_ONCE). It can't be handled by shadow_acc_track_mask/value, but I have changed shadow_acc_track_saved_bits_mask to save only the R/X bits, which achie= ves the same result. >=20 > > +=09spte &=3D ~(shadow_acc_track_saved_bits_mask << > > +=09=09 shadow_acc_track_saved_bits_shift); >=20 > Should there be a WARN_ON if these bits are not zero? No, these bits can be non-zero from a previous instance of=20 mark_spte_for_access_track. They are not cleared when the PTE is restor= ed to normal state. (Though we could do that and then have a WARN_ONCE here.)= > > @@ -2919,10 +3063,17 @@ fast_pf_fix_direct_spte(struct kvm_vcpu *vc= pu, struct kvm_mmu_page *sp, > > =09 * > > =09 * Compare with set_spte where instead shadow_dirty_mask is set= . > > =09 */ > > -=09if (cmpxchg64(sptep, spte, spte | PT_WRITABLE_MASK) !=3D spte) > > +=09if (cmpxchg64(sptep, old_spte, new_spte) !=3D old_spte) > > =09=09return false; > > =20 > > -=09kvm_vcpu_mark_page_dirty(vcpu, gfn); > > +=09if (remove_write_prot) { >=20 > Stupid question ahead, why not call kvm_vcpu_mark_page_accessed in th= is=20 > function? We could, but I kept that call in the same paths as before for consiste= ncy with the other cases. Plus, even though it is a cheap call, why add it to th= e fast=20 PF path if it is not necessary. > > @@ -2953,36 +3104,43 @@ static bool fast_page_fault(struct kvm_vcpu= *vcpu, gva_t gva, int level, > > =20 > > =09=09/* > > -=09=09 * Check if it is a spurious fault caused by TLB lazily flus= hed. > > +=09=09 * Check whether the memory access that caused the fault wou= ld > > +=09=09 * still cause it if it were to be performed right now. If n= ot, > > +=09=09 * then this is a spurious fault caused by TLB lazily flushe= d, > > +=09=09 * or some other CPU has already fixed the PTE after the > > +=09=09 * current CPU took the fault. > > +=09=09 * > > +=09=09 * If Write-Only mappings ever become supported, then the > > +=09=09 * condition below would need to be changed appropriately. > > =09=09 * > > =09=09 * Need not check the access of upper level table entries si= nce > > =09=09 * they are always ACC_ALL. > > =09=09 */ > > -=09=09if (is_writable_pte(spte)) { > > -=09=09=09ret =3D true; > > +=09=09if (((spte & PT_PRESENT_MASK) && !remove_write_prot) || > > +=09=09 valid_exec_access) { > > +=09=09=09fault_handled =3D true; > > =09=09=09break; > > =09=09} >=20 > Let's separate the three conditions (R/W/X): >=20 > =09=09if ((error_code & PFERR_FETCH_MASK) { > =09=09=09if ((spte & (shadow_x_mask|shadow_nx_mask)) > =09=09=09 =3D=3D shadow_x_mask) { > =09=09=09=09fault_handled =3D true; > =09=09=09=09break; > =09=09=09} > =09=09} > =09=09if (error_code & PFERR_WRITE_MASK) { > =09=09=09if (is_writable_pte(spte)) { > =09=09=09=09fault_handled =3D true; > =09=09=09=09break; > =09=09=09} > =09=09=09remove_write_prot =3D > =09=09=09=09spte_can_locklessly_be_made_writable(spte); > =09=09} > =09=09if (!(error_code & PFERR_PRESENT_MASK)) { > =09=09=09if (!is_access_track_spte(spte)) { > =09=09=09=09fault_handled =3D true; > =09=09=09=09break; > =09=09=09} > =09=09=09remove_acc_track =3D true; > =09=09} I think the third block is incorrect e.g. it will set fault_handled =3D= true even for a completely zero PTE. I have replaced it with the following: =09=09if ((error_code & PFERR_USER_MASK) && =09=09 (spte & PT_PRESENT_MASK)) { =09=09=09fault_handled =3D true; =09=09=09break; =09=09} =09=09remove_acc_track =3D is_access_track_spte(spte); > > =20 > > +/* This is only supposed to be used for non-EPT mappings */ >=20 > It's only used for non-EPT mappings, why is it only *supposed* to be=20= > used for non-EPT mappings? It seems to me that it would work. >=20 > > static bool need_remote_flush(u64 old, u64 new) > > { It would work but it will return true in at least one (probably only) c= ase=20 where it doesn=E2=80=99t need to e.g. going from an acc-track PTE to a = zeroed PTE. Though maybe that is not a big deal. Of course, we could also just upda= te it=20 to handle acc-track.=20 Thanks, Junaid