From patchwork Fri Aug 30 03:52:26 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takuya Yoshikawa X-Patchwork-Id: 2851755 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 621DC9F313 for ; Fri, 30 Aug 2013 03:50:27 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7C5862028F for ; Fri, 30 Aug 2013 03:50:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 705CA20257 for ; Fri, 30 Aug 2013 03:50:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752963Ab3H3DuV (ORCPT ); Thu, 29 Aug 2013 23:50:21 -0400 Received: from tama500.ecl.ntt.co.jp ([129.60.39.148]:35449 "EHLO tama500.ecl.ntt.co.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752621Ab3H3DuT (ORCPT ); Thu, 29 Aug 2013 23:50:19 -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 r7U3oExu026274; Fri, 30 Aug 2013 12:50:14 +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 A68F9E0144; Fri, 30 Aug 2013 12:50:14 +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 9ACC2E0142; Fri, 30 Aug 2013 12:50:14 +0900 (JST) Received: from yshpad ([129.60.241.195]) by imail2.m.ecl.ntt.co.jp (8.13.8/8.13.8) with SMTP id r7U3oDMd027981; Fri, 30 Aug 2013 12:50:13 +0900 Date: Fri, 30 Aug 2013 12:52:26 +0900 From: Takuya Yoshikawa To: gleb@redhat.com, pbonzini@redhat.com Cc: kvm@vger.kernel.org, xiaoguangrong@linux.vnet.ibm.com Subject: [PATCH 2/2] KVM: Stop using extra buffer for copying dirty_bitmap to user-space Message-Id: <20130830125226.aaa778a3.yoshikawa_takuya_b1@lab.ntt.co.jp> In-Reply-To: <20130830125011.ba85e94d.yoshikawa_takuya_b1@lab.ntt.co.jp> References: <20130830125011.ba85e94d.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 X-TM-AS-MML: No Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-9.4 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 Now that mmu_lock is held only inside kvm_mmu_write_protect_pt_masked(), we can do __put_user() for copying each 64/32 dirty bits to user-space. This eliminates the need to copy the whole bitmap to an extra buffer and the resulting code is much more cache friendly than before. Signed-off-by: Takuya Yoshikawa Cc: Xiao Guangrong --- arch/x86/kvm/x86.c | 18 ++++++++---------- virt/kvm/kvm_main.c | 6 +----- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1d1f6df..79e8ad0 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3522,7 +3522,7 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) struct kvm_memory_slot *memslot; unsigned long n, i; unsigned long *dirty_bitmap; - unsigned long *dirty_bitmap_buffer; + unsigned long __user *p_user; bool is_dirty = false; mutex_lock(&kvm->slots_lock); @@ -3539,11 +3539,12 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) goto out; n = kvm_dirty_bitmap_bytes(memslot); + r = -EFAULT; + if (clear_user(log->dirty_bitmap, n)) + goto out; - dirty_bitmap_buffer = dirty_bitmap + n / sizeof(long); - memset(dirty_bitmap_buffer, 0, n); - - for (i = 0; i < n / sizeof(long); i++) { + p_user = (unsigned long __user *)log->dirty_bitmap; + for (i = 0; i < n / sizeof(long); i++, p_user++) { unsigned long mask; gfn_t offset; @@ -3553,7 +3554,8 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) is_dirty = true; mask = xchg(&dirty_bitmap[i], 0); - dirty_bitmap_buffer[i] = mask; + if (__put_user(mask, p_user)) + goto out; offset = i * BITS_PER_LONG; kvm_mmu_write_protect_pt_masked(kvm, memslot, offset, mask); @@ -3561,10 +3563,6 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) if (is_dirty) kvm_flush_remote_tlbs(kvm); - r = -EFAULT; - if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n)) - goto out; - r = 0; out: mutex_unlock(&kvm->slots_lock); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index bf040c4..c919f58 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -626,14 +626,10 @@ static int kvm_vm_release(struct inode *inode, struct file *filp) return 0; } -/* - * Allocation size is twice as large as the actual dirty bitmap size. - * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed. - */ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot) { #ifndef CONFIG_S390 - unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot); + unsigned long dirty_bytes = kvm_dirty_bitmap_bytes(memslot); memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes); if (!memslot->dirty_bitmap)