From patchwork Wed Jul 3 08:18:04 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takuya Yoshikawa X-Patchwork-Id: 2815481 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 3A25A9F3EB for ; Wed, 3 Jul 2013 08:17:04 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D8FD82013E for ; Wed, 3 Jul 2013 08:17:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4188D20137 for ; Wed, 3 Jul 2013 08:17:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754542Ab3GCIQ5 (ORCPT ); Wed, 3 Jul 2013 04:16:57 -0400 Received: from tama500.ecl.ntt.co.jp ([129.60.39.148]:43840 "EHLO tama500.ecl.ntt.co.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753521Ab3GCIQz (ORCPT ); Wed, 3 Jul 2013 04:16:55 -0400 Received: from mfs5.rdh.ecl.ntt.co.jp (mfs5.rdh.ecl.ntt.co.jp [129.60.39.144]) by tama500.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id r638Gngx009430; Wed, 3 Jul 2013 17:16:49 +0900 Received: from mfs5.rdh.ecl.ntt.co.jp (localhost.localdomain [127.0.0.1]) by mfs5.rdh.ecl.ntt.co.jp (Postfix) with ESMTP id 0E553E0057; Wed, 3 Jul 2013 17:16:49 +0900 (JST) Received: from imail2.m.ecl.ntt.co.jp (imail2.m.ecl.ntt.co.jp [129.60.5.247]) by mfs5.rdh.ecl.ntt.co.jp (Postfix) with ESMTP id ED089E0054; Wed, 3 Jul 2013 17:16:48 +0900 (JST) Received: from yshpad ([129.60.241.163]) by imail2.m.ecl.ntt.co.jp (8.13.8/8.13.8) with SMTP id r638Ga07020335; Wed, 3 Jul 2013 17:16:37 +0900 Date: Wed, 3 Jul 2013 17:18:04 +0900 From: Takuya Yoshikawa To: gleb@redhat.com, pbonzini@redhat.com Cc: kvm@vger.kernel.org Subject: [PATCH] KVM: x86: Avoid zapping mmio sptes twice for generation wraparound Message-Id: <20130703171804.89d6cc2c.yoshikawa_takuya_b1@lab.ntt.co.jp> X-Mailer: Sylpheed 3.1.0 (GTK+ 2.24.4; x86_64-pc-linux-gnu) Mime-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Since kvm_arch_prepare_memory_region() is called right after installing the slot marked invalid, wraparound checking should be there to avoid zapping mmio sptes when mmio generation is still MMIO_MAX_GEN - 1. Signed-off-by: Takuya Yoshikawa --- This seems to be the simplest solution for fixing the off-by-one issue we discussed before. arch/x86/kvm/mmu.c | 5 +---- arch/x86/kvm/x86.c | 7 +++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index 0d094da..bf7af1e 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -4383,11 +4383,8 @@ void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm) /* * The very rare case: if the generation-number is round, * zap all shadow pages. - * - * The max value is MMIO_MAX_GEN - 1 since it is not called - * when mark memslot invalid. */ - if (unlikely(kvm_current_mmio_generation(kvm) >= (MMIO_MAX_GEN - 1))) { + if (unlikely(kvm_current_mmio_generation(kvm) >= MMIO_MAX_GEN)) { printk_ratelimited(KERN_INFO "kvm: zapping shadow pages for mmio generation wraparound\n"); kvm_mmu_invalidate_zap_all_pages(kvm); } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7d71c0f..9ddd4ff 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7046,6 +7046,13 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, memslot->userspace_addr = userspace_addr; } + /* + * In these cases, slots->generation has been increased for marking the + * slot invalid, so we need wraparound checking here. + */ + if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) + kvm_mmu_invalidate_mmio_sptes(kvm); + return 0; }