Re: [PATCH v2 2/9] KVM: MMU: introduce for_each_rmap_spte()
On 05/12/2015 04:08 PM, Paolo Bonzini wrote: On 12/05/2015 04:32, Xiao Guangrong wrote: - while ((sptep = rmap_get_first(*rmapp, ))) { - BUG_ON(!(*sptep & PT_PRESENT_MASK)); +restart: + for_each_rmap_spte(rmapp, , sptep) { rmap_printk("kvm_rmap_unmap_hva: spte %p %llx gfn %llx (%d)\n", sptep, *sptep, gfn, level); drop_spte(kvm, sptep); need_tlb_flush = 1; + goto restart; } For this one, I would keep using rmap_get_first. Otherwise looks good. Paolo Okay, will do. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH v2 2/9] KVM: MMU: introduce for_each_rmap_spte()
On 12/05/2015 04:32, Xiao Guangrong wrote: > - while ((sptep = rmap_get_first(*rmapp, ))) { > - BUG_ON(!(*sptep & PT_PRESENT_MASK)); > +restart: > + for_each_rmap_spte(rmapp, , sptep) { > rmap_printk("kvm_rmap_unmap_hva: spte %p %llx gfn %llx (%d)\n", >sptep, *sptep, gfn, level); > > drop_spte(kvm, sptep); > need_tlb_flush = 1; > + goto restart; > } > For this one, I would keep using rmap_get_first. Otherwise looks good. Paolo -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH v2 2/9] KVM: MMU: introduce for_each_rmap_spte()
On 12/05/2015 04:32, Xiao Guangrong wrote: - while ((sptep = rmap_get_first(*rmapp, iter))) { - BUG_ON(!(*sptep PT_PRESENT_MASK)); +restart: + for_each_rmap_spte(rmapp, iter, sptep) { rmap_printk(kvm_rmap_unmap_hva: spte %p %llx gfn %llx (%d)\n, sptep, *sptep, gfn, level); drop_spte(kvm, sptep); need_tlb_flush = 1; + goto restart; } For this one, I would keep using rmap_get_first. Otherwise looks good. Paolo -- To unsubscribe from this list: send the line unsubscribe linux-kernel in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
Re: [PATCH v2 2/9] KVM: MMU: introduce for_each_rmap_spte()
On 05/12/2015 04:08 PM, Paolo Bonzini wrote: On 12/05/2015 04:32, Xiao Guangrong wrote: - while ((sptep = rmap_get_first(*rmapp, iter))) { - BUG_ON(!(*sptep PT_PRESENT_MASK)); +restart: + for_each_rmap_spte(rmapp, iter, sptep) { rmap_printk(kvm_rmap_unmap_hva: spte %p %llx gfn %llx (%d)\n, sptep, *sptep, gfn, level); drop_spte(kvm, sptep); need_tlb_flush = 1; + goto restart; } For this one, I would keep using rmap_get_first. Otherwise looks good. Paolo Okay, will do. -- To unsubscribe from this list: send the line unsubscribe linux-kernel in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
[PATCH v2 2/9] KVM: MMU: introduce for_each_rmap_spte()
It's used to walk all the sptes on the rmap to clean up the code Signed-off-by: Xiao Guangrong --- arch/x86/kvm/mmu.c | 63 +++- arch/x86/kvm/mmu_audit.c | 4 +-- 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index f5fcfc1..0da9cf0 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -1142,6 +1142,11 @@ static u64 *rmap_get_next(struct rmap_iterator *iter) return NULL; } +#define for_each_rmap_spte(_rmap_, _iter_, _spte_) \ + for (_spte_ = rmap_get_first(*_rmap_, _iter_); \ + _spte_ && ({BUG_ON(!is_shadow_present_pte(*_spte_)); 1;}); \ + _spte_ = rmap_get_next(_iter_)) + static void drop_spte(struct kvm *kvm, u64 *sptep) { if (mmu_spte_clear_track_bits(sptep)) @@ -1205,12 +1210,8 @@ static bool __rmap_write_protect(struct kvm *kvm, unsigned long *rmapp, struct rmap_iterator iter; bool flush = false; - for (sptep = rmap_get_first(*rmapp, ); sptep;) { - BUG_ON(!(*sptep & PT_PRESENT_MASK)); - + for_each_rmap_spte(rmapp, , sptep) flush |= spte_write_protect(kvm, sptep, pt_protect); - sptep = rmap_get_next(); - } return flush; } @@ -1232,12 +1233,8 @@ static bool __rmap_clear_dirty(struct kvm *kvm, unsigned long *rmapp) struct rmap_iterator iter; bool flush = false; - for (sptep = rmap_get_first(*rmapp, ); sptep;) { - BUG_ON(!(*sptep & PT_PRESENT_MASK)); - + for_each_rmap_spte(rmapp, , sptep) flush |= spte_clear_dirty(kvm, sptep); - sptep = rmap_get_next(); - } return flush; } @@ -1259,12 +1256,8 @@ static bool __rmap_set_dirty(struct kvm *kvm, unsigned long *rmapp) struct rmap_iterator iter; bool flush = false; - for (sptep = rmap_get_first(*rmapp, ); sptep;) { - BUG_ON(!(*sptep & PT_PRESENT_MASK)); - + for_each_rmap_spte(rmapp, , sptep) flush |= spte_set_dirty(kvm, sptep); - sptep = rmap_get_next(); - } return flush; } @@ -1368,13 +1361,14 @@ static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp, struct rmap_iterator iter; int need_tlb_flush = 0; - while ((sptep = rmap_get_first(*rmapp, ))) { - BUG_ON(!(*sptep & PT_PRESENT_MASK)); +restart: + for_each_rmap_spte(rmapp, , sptep) { rmap_printk("kvm_rmap_unmap_hva: spte %p %llx gfn %llx (%d)\n", sptep, *sptep, gfn, level); drop_spte(kvm, sptep); need_tlb_flush = 1; + goto restart; } return need_tlb_flush; @@ -1394,8 +1388,8 @@ static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, WARN_ON(pte_huge(*ptep)); new_pfn = pte_pfn(*ptep); - for (sptep = rmap_get_first(*rmapp, ); sptep;) { - BUG_ON(!is_shadow_present_pte(*sptep)); +restart: + for_each_rmap_spte(rmapp, , sptep) { rmap_printk("kvm_set_pte_rmapp: spte %p %llx gfn %llx (%d)\n", sptep, *sptep, gfn, level); @@ -1403,7 +1397,7 @@ static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, if (pte_write(*ptep)) { drop_spte(kvm, sptep); - sptep = rmap_get_first(*rmapp, ); + goto restart; } else { new_spte = *sptep & ~PT64_BASE_ADDR_MASK; new_spte |= (u64)new_pfn << PAGE_SHIFT; @@ -1414,7 +1408,6 @@ static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, mmu_spte_clear_track_bits(sptep); mmu_spte_set(sptep, new_spte); - sptep = rmap_get_next(); } } @@ -1518,16 +1511,13 @@ static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, BUG_ON(!shadow_accessed_mask); - for (sptep = rmap_get_first(*rmapp, ); sptep; -sptep = rmap_get_next()) { - BUG_ON(!is_shadow_present_pte(*sptep)); - + for_each_rmap_spte(rmapp, , sptep) if (*sptep & shadow_accessed_mask) { young = 1; clear_bit((ffs(shadow_accessed_mask) - 1), (unsigned long *)sptep); } - } + trace_kvm_age_page(gfn, level, slot, young); return young; } @@ -1548,15 +1538,11 @@ static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp, if (!shadow_accessed_mask) goto out; - for (sptep = rmap_get_first(*rmapp, ); sptep; -sptep = rmap_get_next()) { - BUG_ON(!is_shadow_present_pte(*sptep)); -
[PATCH v2 2/9] KVM: MMU: introduce for_each_rmap_spte()
It's used to walk all the sptes on the rmap to clean up the code Signed-off-by: Xiao Guangrong guangrong.x...@linux.intel.com --- arch/x86/kvm/mmu.c | 63 +++- arch/x86/kvm/mmu_audit.c | 4 +-- 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index f5fcfc1..0da9cf0 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -1142,6 +1142,11 @@ static u64 *rmap_get_next(struct rmap_iterator *iter) return NULL; } +#define for_each_rmap_spte(_rmap_, _iter_, _spte_) \ + for (_spte_ = rmap_get_first(*_rmap_, _iter_); \ + _spte_ ({BUG_ON(!is_shadow_present_pte(*_spte_)); 1;}); \ + _spte_ = rmap_get_next(_iter_)) + static void drop_spte(struct kvm *kvm, u64 *sptep) { if (mmu_spte_clear_track_bits(sptep)) @@ -1205,12 +1210,8 @@ static bool __rmap_write_protect(struct kvm *kvm, unsigned long *rmapp, struct rmap_iterator iter; bool flush = false; - for (sptep = rmap_get_first(*rmapp, iter); sptep;) { - BUG_ON(!(*sptep PT_PRESENT_MASK)); - + for_each_rmap_spte(rmapp, iter, sptep) flush |= spte_write_protect(kvm, sptep, pt_protect); - sptep = rmap_get_next(iter); - } return flush; } @@ -1232,12 +1233,8 @@ static bool __rmap_clear_dirty(struct kvm *kvm, unsigned long *rmapp) struct rmap_iterator iter; bool flush = false; - for (sptep = rmap_get_first(*rmapp, iter); sptep;) { - BUG_ON(!(*sptep PT_PRESENT_MASK)); - + for_each_rmap_spte(rmapp, iter, sptep) flush |= spte_clear_dirty(kvm, sptep); - sptep = rmap_get_next(iter); - } return flush; } @@ -1259,12 +1256,8 @@ static bool __rmap_set_dirty(struct kvm *kvm, unsigned long *rmapp) struct rmap_iterator iter; bool flush = false; - for (sptep = rmap_get_first(*rmapp, iter); sptep;) { - BUG_ON(!(*sptep PT_PRESENT_MASK)); - + for_each_rmap_spte(rmapp, iter, sptep) flush |= spte_set_dirty(kvm, sptep); - sptep = rmap_get_next(iter); - } return flush; } @@ -1368,13 +1361,14 @@ static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp, struct rmap_iterator iter; int need_tlb_flush = 0; - while ((sptep = rmap_get_first(*rmapp, iter))) { - BUG_ON(!(*sptep PT_PRESENT_MASK)); +restart: + for_each_rmap_spte(rmapp, iter, sptep) { rmap_printk(kvm_rmap_unmap_hva: spte %p %llx gfn %llx (%d)\n, sptep, *sptep, gfn, level); drop_spte(kvm, sptep); need_tlb_flush = 1; + goto restart; } return need_tlb_flush; @@ -1394,8 +1388,8 @@ static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, WARN_ON(pte_huge(*ptep)); new_pfn = pte_pfn(*ptep); - for (sptep = rmap_get_first(*rmapp, iter); sptep;) { - BUG_ON(!is_shadow_present_pte(*sptep)); +restart: + for_each_rmap_spte(rmapp, iter, sptep) { rmap_printk(kvm_set_pte_rmapp: spte %p %llx gfn %llx (%d)\n, sptep, *sptep, gfn, level); @@ -1403,7 +1397,7 @@ static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, if (pte_write(*ptep)) { drop_spte(kvm, sptep); - sptep = rmap_get_first(*rmapp, iter); + goto restart; } else { new_spte = *sptep ~PT64_BASE_ADDR_MASK; new_spte |= (u64)new_pfn PAGE_SHIFT; @@ -1414,7 +1408,6 @@ static int kvm_set_pte_rmapp(struct kvm *kvm, unsigned long *rmapp, mmu_spte_clear_track_bits(sptep); mmu_spte_set(sptep, new_spte); - sptep = rmap_get_next(iter); } } @@ -1518,16 +1511,13 @@ static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp, BUG_ON(!shadow_accessed_mask); - for (sptep = rmap_get_first(*rmapp, iter); sptep; -sptep = rmap_get_next(iter)) { - BUG_ON(!is_shadow_present_pte(*sptep)); - + for_each_rmap_spte(rmapp, iter, sptep) if (*sptep shadow_accessed_mask) { young = 1; clear_bit((ffs(shadow_accessed_mask) - 1), (unsigned long *)sptep); } - } + trace_kvm_age_page(gfn, level, slot, young); return young; } @@ -1548,15 +1538,11 @@ static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp, if (!shadow_accessed_mask) goto out; - for (sptep = rmap_get_first(*rmapp, iter); sptep; -