From patchwork Wed Jul 11 16:08:14 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 1183891 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id AC5F2DF25A for ; Wed, 11 Jul 2012 16:06:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757515Ab2GKQGu (ORCPT ); Wed, 11 Jul 2012 12:06:50 -0400 Received: from mail-pb0-f46.google.com ([209.85.160.46]:64753 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756126Ab2GKQGt (ORCPT ); Wed, 11 Jul 2012 12:06:49 -0400 Received: by mail-pb0-f46.google.com with SMTP id rp8so2200678pbb.19 for ; Wed, 11 Jul 2012 09:06:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=tZyR8oz6KiNyVTD2EMJE8Xz9zBctkFBxgpx3YI4IHzw=; b=U0uIpKWSptBI7OnG9gmekBNy51EVcuSle7jzXqXeNMHnkUKBKM5AQGDYR66vmHVjHM HfPSy7BjXps6WZbXRb8mm/0b07NCb4UnN5NtYwGDkXccc1KoslBho0w/eW6q6DMsl+o5 nUyqYhf4FOV/WkoL8WMRPw37Y48zVNk4MZQgJGikeUUshyGC3awYlVlYQ0+o+17DEoNn 35Jo5C3ACH00vfpauORbppli56a19dOQpB9BmsDYqIqy77kbNWf6H/o1zr9a5WpAQ1YD 4559JIXNuaQEaf+k1UK+vS/7h+mtfTkcQkOOTg5moo/1ZsrhssZMRVCw/V26q4webLU8 tlXQ== Received: by 10.68.116.203 with SMTP id jy11mr78173838pbb.129.1342022808727; Wed, 11 Jul 2012 09:06:48 -0700 (PDT) Received: from localhost.localdomain ([58.194.229.69]) by mx.google.com with ESMTPS id pq5sm1957086pbb.30.2012.07.11.09.06.44 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 11 Jul 2012 09:06:47 -0700 (PDT) From: Asias He To: Pekka Enberg Cc: Sasha Levin , Ingo Molnar , Cyrill Gorcunov , kvm@vger.kernel.org, Asias He Subject: [PATCH v2 2/2] kvm tools: Do not poll ioeventfd if vhost is enabled Date: Thu, 12 Jul 2012 00:08:14 +0800 Message-Id: <1342022894-1034-2-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1342022894-1034-1-git-send-email-asias.hejun@gmail.com> References: <1342022894-1034-1-git-send-email-asias.hejun@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org If vhost is enabled for a virtio device, vhost will poll the ioeventfd in kernel side and there is no need to poll it in userspace. Otherwise, both vhost kernel and userspace will race to poll. Signed-off-by: Asias He --- tools/kvm/include/kvm/ioeventfd.h | 2 +- tools/kvm/include/kvm/virtio.h | 1 + tools/kvm/ioeventfd.c | 5 ++++- tools/kvm/virtio/mmio.c | 10 +++++++++- tools/kvm/virtio/net.c | 3 +++ tools/kvm/virtio/pci.c | 10 +++++++++- 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/tools/kvm/include/kvm/ioeventfd.h b/tools/kvm/include/kvm/ioeventfd.h index 70cce9a..d71fa40 100644 --- a/tools/kvm/include/kvm/ioeventfd.h +++ b/tools/kvm/include/kvm/ioeventfd.h @@ -22,7 +22,7 @@ struct ioevent { int ioeventfd__init(struct kvm *kvm); int ioeventfd__exit(struct kvm *kvm); -int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio); +int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio, bool poll_in_userspace); int ioeventfd__del_event(u64 addr, u64 datamatch); #endif diff --git a/tools/kvm/include/kvm/virtio.h b/tools/kvm/include/kvm/virtio.h index a957a5b..71b6bad 100644 --- a/tools/kvm/include/kvm/virtio.h +++ b/tools/kvm/include/kvm/virtio.h @@ -72,6 +72,7 @@ enum virtio_trans { }; struct virtio_device { + bool use_vhost; void *virtio; struct virtio_ops *ops; }; diff --git a/tools/kvm/ioeventfd.c b/tools/kvm/ioeventfd.c index 97deb06..226876f 100644 --- a/tools/kvm/ioeventfd.c +++ b/tools/kvm/ioeventfd.c @@ -117,7 +117,7 @@ int ioeventfd__exit(struct kvm *kvm) return 0; } -int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio) +int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio, bool poll_in_userspace) { struct kvm_ioeventfd kvm_ioevent; struct epoll_event epoll_event; @@ -151,6 +151,9 @@ int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio) goto cleanup; } + if (!poll_in_userspace) + return 0; + epoll_event = (struct epoll_event) { .events = EPOLLIN, .data.ptr = new_ioevent, diff --git a/tools/kvm/virtio/mmio.c b/tools/kvm/virtio/mmio.c index 8319954..1cf0815 100644 --- a/tools/kvm/virtio/mmio.c +++ b/tools/kvm/virtio/mmio.c @@ -48,7 +48,15 @@ static int virtio_mmio_init_ioeventfd(struct kvm *kvm, .fd = eventfd(0, 0), }; - err = ioeventfd__add_event(&ioevent, false); + if (vdev->use_vhost) + /* + * Vhost will poll the eventfd in host kernel side, + * no need to poll in userspace. + */ + err = ioeventfd__add_event(&ioevent, true, false); + else + /* Need to poll in userspace. */ + err = ioeventfd__add_event(&ioevent, true, true); if (err) return err; diff --git a/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c index aa769d9..10420ae 100644 --- a/tools/kvm/virtio/net.c +++ b/tools/kvm/virtio/net.c @@ -492,6 +492,9 @@ static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev) r = ioctl(ndev->vhost_fd, VHOST_SET_MEM_TABLE, mem); if (r != 0) die_perror("VHOST_SET_MEM_TABLE failed"); + + ndev->vdev.use_vhost = true; + free(mem); } diff --git a/tools/kvm/virtio/pci.c b/tools/kvm/virtio/pci.c index 8558341e..f17cd8a 100644 --- a/tools/kvm/virtio/pci.c +++ b/tools/kvm/virtio/pci.c @@ -40,7 +40,15 @@ static int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_device *vde .fd = eventfd(0, 0), }; - r = ioeventfd__add_event(&ioevent, true); + if (vdev->use_vhost) + /* + * Vhost will poll the eventfd in host kernel side, + * no need to poll in userspace. + */ + r = ioeventfd__add_event(&ioevent, true, false); + else + /* Need to poll in userspace. */ + r = ioeventfd__add_event(&ioevent, true, true); if (r) return r;