From patchwork Wed Apr 19 13:21:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216803 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F1D66C6FD18 for ; Wed, 19 Apr 2023 13:27:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232849AbjDSN1s (ORCPT ); Wed, 19 Apr 2023 09:27:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232784AbjDSN1r (ORCPT ); Wed, 19 Apr 2023 09:27:47 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57915469A for ; Wed, 19 Apr 2023 06:27:45 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id o6-20020a05600c4fc600b003ef6e6754c5so1442395wmq.5 for ; Wed, 19 Apr 2023 06:27:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910864; x=1684502864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=h3zoHmoW9qrNtTnR3VsgQ7FWQYUQLqEmc0mtF1/E1WY=; b=F/DprwDvywW/AQ7aZhge8nnu6bAuRdw85BZU0yLLzIdPgvREjnLHWqyMzvE+CqEbQD trYNJOfQNax6u14UnYxesDsPdm0nU/e/ZuK1yRdV56gqqCmqOjcKCFrWz+Iy8lZ/Tdke jb4ZC/hvvfHFD+u06peYv4IpivJIwZDFfc1p1ur2fZtNwgGDGySkqVbTZoIYid6kkdOY CN0gOg1zAZxBqkIkZogXL4PDooBxjtaUJKVvtN8gUDnIRSjN3egiw0hRHm/k9h0rrmwI EGVYPE41EPBymUQ4Wt92i1rhRbAF8ZaSbMdCz6Hqi9+izTzlMRwe0JD6CwvXnVBbuRW5 lvTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910864; x=1684502864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=h3zoHmoW9qrNtTnR3VsgQ7FWQYUQLqEmc0mtF1/E1WY=; b=OYzQRt5/bOTsfeGjvSQtOlROlg5PB00aV7kFP29tmoQsn6QxI4QdPLNNuO/Q+qis4u 9xyB9bY13El1t1SOFB8q8VLYbJqdbb6iSVu/8Wp7TnVYNJ6J0tEzWzbGK3MIMrplGqIZ us2RpiDJGrtg0YRckTMwPcrA47P9sPrAAJrSZZ74APQ64e7hpkzT2leb5B8IrP+XfSD2 svrfoErEtTVnkopRk5STZLABV1ox9aInIdCX0GiRoH3upLRTh3Ycr8mymSn6ppjSkul6 iBaW6WSwqQoDvz+H1t+tD+YpPxstHdwvB/NXkxd+2peKM61Cke2gdp/3MsvaG2X1FpWF JwkQ== X-Gm-Message-State: AAQBX9caVzcp5BmcFkTuCpmSa0kVm/QPUs2XB4/QKm/f8CsJREU1yKkL 5JKeBJbs4y6p8ot5SYug1gAZJ+T6FNdbjk/J9I4= X-Google-Smtp-Source: AKy350YQbd+vOREgQGmpY2sbirpsOvUDvFGWsvX7nxv6nSF3QpKa3NadOW4jwbRDAMAj1AsJEWb/kQ== X-Received: by 2002:a7b:c850:0:b0:3ee:96f0:ea31 with SMTP id c16-20020a7bc850000000b003ee96f0ea31mr17293584wml.18.1681910863805; Wed, 19 Apr 2023 06:27:43 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:43 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 01/16] virtio: Factor vhost initialization Date: Wed, 19 Apr 2023 14:21:05 +0100 Message-Id: <20230419132119.124457-2-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move vhost owner and memory table setup to virtio/vhost.c. This also fixes vsock and SCSI which did not support multiple memory regions until now (vsock didn't allocate the right region size and would trigger a buffer overflow). Signed-off-by: Jean-Philippe Brucker Reviewed-by: Andre Przywara --- Makefile | 1 + include/kvm/virtio.h | 1 + virtio/net.c | 29 +---------------------------- virtio/scsi.c | 21 +-------------------- virtio/vhost.c | 36 ++++++++++++++++++++++++++++++++++++ virtio/vsock.c | 29 ++--------------------------- 6 files changed, 42 insertions(+), 75 deletions(-) create mode 100644 virtio/vhost.c diff --git a/Makefile b/Makefile index ed2414bd..86e19339 100644 --- a/Makefile +++ b/Makefile @@ -76,6 +76,7 @@ OBJS += virtio/pci.o OBJS += virtio/vsock.o OBJS += virtio/pci-legacy.o OBJS += virtio/pci-modern.o +OBJS += virtio/vhost.o OBJS += disk/blk.o OBJS += disk/qcow.o OBJS += disk/raw.o diff --git a/include/kvm/virtio.h b/include/kvm/virtio.h index 0e8c7a67..cd72bf11 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -247,6 +247,7 @@ void virtio_set_guest_features(struct kvm *kvm, struct virtio_device *vdev, void *dev, u64 features); void virtio_notify_status(struct kvm *kvm, struct virtio_device *vdev, void *dev, u8 status); +void virtio_vhost_init(struct kvm *kvm, int vhost_fd); int virtio_transport_parser(const struct option *opt, const char *arg, int unset); diff --git a/virtio/net.c b/virtio/net.c index 8749ebfe..6b44754f 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -791,40 +791,13 @@ static struct virtio_ops net_dev_virtio_ops = { static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev) { - struct kvm_mem_bank *bank; - struct vhost_memory *mem; - int r, i; - ndev->vhost_fd = open("/dev/vhost-net", O_RDWR); if (ndev->vhost_fd < 0) die_perror("Failed openning vhost-net device"); - mem = calloc(1, sizeof(*mem) + kvm->mem_slots * sizeof(struct vhost_memory_region)); - if (mem == NULL) - die("Failed allocating memory for vhost memory map"); - - i = 0; - list_for_each_entry(bank, &kvm->mem_banks, list) { - mem->regions[i] = (struct vhost_memory_region) { - .guest_phys_addr = bank->guest_phys_addr, - .memory_size = bank->size, - .userspace_addr = (unsigned long)bank->host_addr, - }; - i++; - } - mem->nregions = i; - - r = ioctl(ndev->vhost_fd, VHOST_SET_OWNER); - if (r != 0) - die_perror("VHOST_SET_OWNER failed"); - - r = ioctl(ndev->vhost_fd, VHOST_SET_MEM_TABLE, mem); - if (r != 0) - die_perror("VHOST_SET_MEM_TABLE failed"); + virtio_vhost_init(kvm, ndev->vhost_fd); ndev->vdev.use_vhost = true; - - free(mem); } static inline void str_to_mac(const char *str, char *mac) diff --git a/virtio/scsi.c b/virtio/scsi.c index 893dfe60..4dee24a0 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -203,7 +203,6 @@ static struct virtio_ops scsi_dev_virtio_ops = { static void virtio_scsi_vhost_init(struct kvm *kvm, struct scsi_dev *sdev) { - struct vhost_memory *mem; u64 features; int r; @@ -211,20 +210,7 @@ static void virtio_scsi_vhost_init(struct kvm *kvm, struct scsi_dev *sdev) if (sdev->vhost_fd < 0) die_perror("Failed openning vhost-scsi device"); - mem = calloc(1, sizeof(*mem) + sizeof(struct vhost_memory_region)); - if (mem == NULL) - die("Failed allocating memory for vhost memory map"); - - mem->nregions = 1; - mem->regions[0] = (struct vhost_memory_region) { - .guest_phys_addr = 0, - .memory_size = kvm->ram_size, - .userspace_addr = (unsigned long)kvm->ram_start, - }; - - r = ioctl(sdev->vhost_fd, VHOST_SET_OWNER); - if (r != 0) - die_perror("VHOST_SET_OWNER failed"); + virtio_vhost_init(kvm, sdev->vhost_fd); r = ioctl(sdev->vhost_fd, VHOST_GET_FEATURES, &features); if (r != 0) @@ -233,13 +219,8 @@ static void virtio_scsi_vhost_init(struct kvm *kvm, struct scsi_dev *sdev) r = ioctl(sdev->vhost_fd, VHOST_SET_FEATURES, &features); if (r != 0) die_perror("VHOST_SET_FEATURES failed"); - r = ioctl(sdev->vhost_fd, VHOST_SET_MEM_TABLE, mem); - if (r != 0) - die_perror("VHOST_SET_MEM_TABLE failed"); sdev->vdev.use_vhost = true; - - free(mem); } diff --git a/virtio/vhost.c b/virtio/vhost.c new file mode 100644 index 00000000..f9f72f51 --- /dev/null +++ b/virtio/vhost.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include "kvm/virtio.h" + +void virtio_vhost_init(struct kvm *kvm, int vhost_fd) +{ + struct kvm_mem_bank *bank; + struct vhost_memory *mem; + int i = 0, r; + + mem = calloc(1, sizeof(*mem) + + kvm->mem_slots * sizeof(struct vhost_memory_region)); + if (mem == NULL) + die("Failed allocating memory for vhost memory map"); + + list_for_each_entry(bank, &kvm->mem_banks, list) { + mem->regions[i] = (struct vhost_memory_region) { + .guest_phys_addr = bank->guest_phys_addr, + .memory_size = bank->size, + .userspace_addr = (unsigned long)bank->host_addr, + }; + i++; + } + mem->nregions = i; + + r = ioctl(vhost_fd, VHOST_SET_OWNER); + if (r != 0) + die_perror("VHOST_SET_OWNER failed"); + + r = ioctl(vhost_fd, VHOST_SET_MEM_TABLE, mem); + if (r != 0) + die_perror("VHOST_SET_MEM_TABLE failed"); + + free(mem); +} diff --git a/virtio/vsock.c b/virtio/vsock.c index a108e637..4b8be8d7 100644 --- a/virtio/vsock.c +++ b/virtio/vsock.c @@ -218,37 +218,14 @@ static struct virtio_ops vsock_dev_virtio_ops = { static void virtio_vhost_vsock_init(struct kvm *kvm, struct vsock_dev *vdev) { - struct kvm_mem_bank *bank; - struct vhost_memory *mem; u64 features; - int r, i; + int r; vdev->vhost_fd = open("/dev/vhost-vsock", O_RDWR); if (vdev->vhost_fd < 0) die_perror("Failed opening vhost-vsock device"); - mem = calloc(1, sizeof(*mem) + sizeof(struct vhost_memory_region)); - if (mem == NULL) - die("Failed allocating memory for vhost memory map"); - - i = 0; - list_for_each_entry(bank, &kvm->mem_banks, list) { - mem->regions[i] = (struct vhost_memory_region) { - .guest_phys_addr = bank->guest_phys_addr, - .memory_size = bank->size, - .userspace_addr = (unsigned long)bank->host_addr, - }; - i++; - } - mem->nregions = i; - - r = ioctl(vdev->vhost_fd, VHOST_SET_OWNER); - if (r != 0) - die_perror("VHOST_SET_OWNER failed"); - - r = ioctl(vdev->vhost_fd, VHOST_SET_MEM_TABLE, mem); - if (r != 0) - die_perror("VHOST_SET_MEM_TABLE failed"); + virtio_vhost_init(kvm, vdev->vhost_fd); r = ioctl(vdev->vhost_fd, VHOST_GET_FEATURES, &features); if (r != 0) @@ -263,8 +240,6 @@ static void virtio_vhost_vsock_init(struct kvm *kvm, struct vsock_dev *vdev) die_perror("VHOST_VSOCK_SET_GUEST_CID failed"); vdev->vdev.use_vhost = true; - - free(mem); } static int virtio_vsock_init_one(struct kvm *kvm, u64 guest_cid) From patchwork Wed Apr 19 13:21:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216804 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E22FC77B73 for ; Wed, 19 Apr 2023 13:27:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233117AbjDSN1v (ORCPT ); Wed, 19 Apr 2023 09:27:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232784AbjDSN1u (ORCPT ); Wed, 19 Apr 2023 09:27:50 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C641946B8 for ; Wed, 19 Apr 2023 06:27:45 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id o9-20020a05600c510900b003f17012276fso1448207wms.4 for ; Wed, 19 Apr 2023 06:27:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910864; x=1684502864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dG6Xi5SOJYX9tPMmvGwZ7Yq19JpXfFx6Wpu13jh0dK4=; b=oFJ4QFuwFgqBqWtibkqRDTRIkNYgbXPA3V1/WyazATLUi0tY0SbP62P69xfI/lffFw mqBt4/w/hqRPdQi+c5enMUyEaTJDZ27DXPz6Gwi1okOdz9JEh5r1EpwIlY3KN6xjmi9I vnMRWX8H6rnnFK4NTI2uS5ZLRE/KfnZ6HMBOmODaCA9nvmzR1IuI+dPoJlayGaz7SYW5 p2ZEJS/wVH9XwvR0B6m6OXGq2h0Qrkg5s7RaSBi8RNXz19/dKqEoz3rIy3CtPi5+Ao8M fSUBgV6N2ftM8AWzsDTYjOX3/oE5xJGEV1hI3oVC52uwcgcK8llC3+Cvf3BziFbfDOK/ iFbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910864; x=1684502864; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dG6Xi5SOJYX9tPMmvGwZ7Yq19JpXfFx6Wpu13jh0dK4=; b=Amg4zW0q3Ruz4s/EJlePkyEWii4hGIP97lSfVSBg5AudC3necrhnaSEeuWtGuqPAtW A/sjkkR7Kcu70UiU0n8rlkhS3dT3HIR/K8r9nWn+El5fLST4UfcjR6sSQwcWa1i6Byit VjtrXE2oUgFRX7gz/d/siCicUu8OahFifvJxR09u7eaAPbFLwW/8XBfEZtE2CEITIWuD O1499CULWIpIga7L8pafP2gxu3R0U4TrtWx2lH34cN83xAb71K+Byv4/HLL9b913XBDR cdPKjQtBWai3WZHfvNxJCF0XYmIpBXwx+sXhPGygTkltHriSdpv9VUhyKsQvpfbtMzzX KqZQ== X-Gm-Message-State: AAQBX9ecsmpaxMGRjqFbYX0Fr7oCRqE6jdpYzv8ft6ATZzfqJBSaRnJb ilONmAAtGy7K4anHFxnRUrGYp0lrdLKKBR2EUKw= X-Google-Smtp-Source: AKy350YzkwRMfalEPSFShkUHuKhHcYo43UfGZEbaXgMqzSFZiwoCQ7qHivH+MzE9VfQz7OeHqwztaw== X-Received: by 2002:a05:600c:b49:b0:3f1:7123:fd12 with SMTP id k9-20020a05600c0b4900b003f17123fd12mr9723612wmr.34.1681910864296; Wed, 19 Apr 2023 06:27:44 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:44 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 02/16] virtio/vhost: Factor vring operation Date: Wed, 19 Apr 2023 14:21:06 +0100 Message-Id: <20230419132119.124457-3-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The VHOST_VRING* ioctls are common to all device types, move them to virtio/vhost.c Signed-off-by: Jean-Philippe Brucker Reviewed-by: Andre Przywara --- include/kvm/virtio.h | 2 ++ virtio/net.c | 25 +------------------------ virtio/scsi.c | 24 +----------------------- virtio/vhost.c | 33 ++++++++++++++++++++++++++++++++- virtio/vsock.c | 30 ++---------------------------- 5 files changed, 38 insertions(+), 76 deletions(-) diff --git a/include/kvm/virtio.h b/include/kvm/virtio.h index cd72bf11..c8fd69e0 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -248,6 +248,8 @@ void virtio_set_guest_features(struct kvm *kvm, struct virtio_device *vdev, void virtio_notify_status(struct kvm *kvm, struct virtio_device *vdev, void *dev, u8 status); void virtio_vhost_init(struct kvm *kvm, int vhost_fd); +void virtio_vhost_set_vring(struct kvm *kvm, int vhost_fd, u32 index, + struct virt_queue *queue); int virtio_transport_parser(const struct option *opt, const char *arg, int unset); diff --git a/virtio/net.c b/virtio/net.c index 6b44754f..021c81d3 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -600,10 +600,8 @@ static bool is_ctrl_vq(struct net_dev *ndev, u32 vq) static int init_vq(struct kvm *kvm, void *dev, u32 vq) { - struct vhost_vring_state state = { .index = vq }; struct vhost_vring_file file = { .index = vq }; struct net_dev_queue *net_queue; - struct vhost_vring_addr addr; struct net_dev *ndev = dev; struct virt_queue *queue; int r; @@ -634,28 +632,7 @@ static int init_vq(struct kvm *kvm, void *dev, u32 vq) return 0; } - if (queue->endian != VIRTIO_ENDIAN_HOST) - die_perror("VHOST requires the same endianness in guest and host"); - - state.num = queue->vring.num; - r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_NUM, &state); - if (r < 0) - die_perror("VHOST_SET_VRING_NUM failed"); - state.num = 0; - r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_BASE, &state); - if (r < 0) - die_perror("VHOST_SET_VRING_BASE failed"); - - addr = (struct vhost_vring_addr) { - .index = vq, - .desc_user_addr = (u64)(unsigned long)queue->vring.desc, - .avail_user_addr = (u64)(unsigned long)queue->vring.avail, - .used_user_addr = (u64)(unsigned long)queue->vring.used, - }; - - r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_ADDR, &addr); - if (r < 0) - die_perror("VHOST_SET_VRING_ADDR failed"); + virtio_vhost_set_vring(kvm, ndev->vhost_fd, vq, queue); file.fd = ndev->tap_fd; r = ioctl(ndev->vhost_fd, VHOST_NET_SET_BACKEND, &file); diff --git a/virtio/scsi.c b/virtio/scsi.c index 4dee24a0..674aad34 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -74,11 +74,8 @@ static void notify_status(struct kvm *kvm, void *dev, u32 status) static int init_vq(struct kvm *kvm, void *dev, u32 vq) { - struct vhost_vring_state state = { .index = vq }; - struct vhost_vring_addr addr; struct scsi_dev *sdev = dev; struct virt_queue *queue; - int r; compat__remove_message(compat_id); @@ -89,26 +86,7 @@ static int init_vq(struct kvm *kvm, void *dev, u32 vq) if (sdev->vhost_fd == 0) return 0; - state.num = queue->vring.num; - r = ioctl(sdev->vhost_fd, VHOST_SET_VRING_NUM, &state); - if (r < 0) - die_perror("VHOST_SET_VRING_NUM failed"); - state.num = 0; - r = ioctl(sdev->vhost_fd, VHOST_SET_VRING_BASE, &state); - if (r < 0) - die_perror("VHOST_SET_VRING_BASE failed"); - - addr = (struct vhost_vring_addr) { - .index = vq, - .desc_user_addr = (u64)(unsigned long)queue->vring.desc, - .avail_user_addr = (u64)(unsigned long)queue->vring.avail, - .used_user_addr = (u64)(unsigned long)queue->vring.used, - }; - - r = ioctl(sdev->vhost_fd, VHOST_SET_VRING_ADDR, &addr); - if (r < 0) - die_perror("VHOST_SET_VRING_ADDR failed"); - + virtio_vhost_set_vring(kvm, sdev->vhost_fd, vq, queue); return 0; } diff --git a/virtio/vhost.c b/virtio/vhost.c index f9f72f51..afe37465 100644 --- a/virtio/vhost.c +++ b/virtio/vhost.c @@ -1,7 +1,8 @@ +#include "kvm/virtio.h" + #include #include #include -#include "kvm/virtio.h" void virtio_vhost_init(struct kvm *kvm, int vhost_fd) { @@ -34,3 +35,33 @@ void virtio_vhost_init(struct kvm *kvm, int vhost_fd) free(mem); } + +void virtio_vhost_set_vring(struct kvm *kvm, int vhost_fd, u32 index, + struct virt_queue *queue) +{ + int r; + struct vhost_vring_addr addr = { + .index = index, + .desc_user_addr = (u64)(unsigned long)queue->vring.desc, + .avail_user_addr = (u64)(unsigned long)queue->vring.avail, + .used_user_addr = (u64)(unsigned long)queue->vring.used, + }; + struct vhost_vring_state state = { .index = index }; + + if (queue->endian != VIRTIO_ENDIAN_HOST) + die("VHOST requires the same endianness in guest and host"); + + state.num = queue->vring.num; + r = ioctl(vhost_fd, VHOST_SET_VRING_NUM, &state); + if (r < 0) + die_perror("VHOST_SET_VRING_NUM failed"); + + state.num = 0; + r = ioctl(vhost_fd, VHOST_SET_VRING_BASE, &state); + if (r < 0) + die_perror("VHOST_SET_VRING_BASE failed"); + + r = ioctl(vhost_fd, VHOST_SET_VRING_ADDR, &addr); + if (r < 0) + die_perror("VHOST_SET_VRING_ADDR failed"); +} diff --git a/virtio/vsock.c b/virtio/vsock.c index 4b8be8d7..2f7906f2 100644 --- a/virtio/vsock.c +++ b/virtio/vsock.c @@ -62,44 +62,18 @@ static bool is_event_vq(u32 vq) static int init_vq(struct kvm *kvm, void *dev, u32 vq) { - struct vhost_vring_state state = { .index = vq }; - struct vhost_vring_addr addr; struct vsock_dev *vdev = dev; struct virt_queue *queue; - int r; compat__remove_message(compat_id); queue = &vdev->vqs[vq]; virtio_init_device_vq(kvm, &vdev->vdev, queue, VIRTIO_VSOCK_QUEUE_SIZE); - if (vdev->vhost_fd == -1) + if (vdev->vhost_fd == -1 || is_event_vq(vq)) return 0; - if (is_event_vq(vq)) - return 0; - - state.num = queue->vring.num; - r = ioctl(vdev->vhost_fd, VHOST_SET_VRING_NUM, &state); - if (r < 0) - die_perror("VHOST_SET_VRING_NUM failed"); - - state.num = 0; - r = ioctl(vdev->vhost_fd, VHOST_SET_VRING_BASE, &state); - if (r < 0) - die_perror("VHOST_SET_VRING_BASE failed"); - - addr = (struct vhost_vring_addr) { - .index = vq, - .desc_user_addr = (u64)(unsigned long)queue->vring.desc, - .avail_user_addr = (u64)(unsigned long)queue->vring.avail, - .used_user_addr = (u64)(unsigned long)queue->vring.used, - }; - - r = ioctl(vdev->vhost_fd, VHOST_SET_VRING_ADDR, &addr); - if (r < 0) - die_perror("VHOST_SET_VRING_ADDR failed"); - + virtio_vhost_set_vring(kvm, vdev->vhost_fd, vq, queue); return 0; } From patchwork Wed Apr 19 13:21:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216805 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96879C77B75 for ; Wed, 19 Apr 2023 13:27:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233191AbjDSN1x (ORCPT ); Wed, 19 Apr 2023 09:27:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233064AbjDSN1v (ORCPT ); Wed, 19 Apr 2023 09:27:51 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 456BA46BB for ; Wed, 19 Apr 2023 06:27:46 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id a8-20020a05600c348800b003f17ddb04e3so1370748wmq.2 for ; Wed, 19 Apr 2023 06:27:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910865; x=1684502865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zXXPAPd7Lhm10tnhbD93859rtM6+xJNLXlNQi2l/X0c=; b=zZt2owNkIF/em6SsvBR4V2ThQ//6hSvYcdmLofZlZpRJP1kkWk0SvBziPb3CcDKqZi Sda5O9qv04fHV3XCa3iZYMmBfR7YJViUaPzy59Dn6m12n6dVZWpjqwP01y5a4LhCuF0j HcYlkO5McEtRNN0+mtrskx6f6cO8p83lSSrqlmEnemgtY430c47ZfHBaIXsqHm0t81v0 BRC5Zr1wb3Ihr4xiC8bGtGM74NMdj05BHcmdco7h1GSn02JZ8NBhitOaP3+oE1lVoNNA iPJnlMGoYDomOCGcWoHfahdUuvRHc0hXbaok8W367KPE2BpuHKPHSv4NAFvZEe3UobtB krFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910865; x=1684502865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zXXPAPd7Lhm10tnhbD93859rtM6+xJNLXlNQi2l/X0c=; b=Qa6x0y2ILfDpHivlbWGWlH9DBeyOzWuvpBA9SicNv2yxPAOzqwc3LpB2+s3VmvzF+5 e+Um6ssRtR427wbZ7AGzpYPKkyLfrhRjJf0b0xIyRVcW8w48hVHbWC/7TvdZH1MehsWP ygHGeu2rM/NoeFmCv8BAUcYt5AX/TslXU05zw/5rti5rFkmcITjIdBjS00kBL4BmwOmi xAfUwFhdMC5Gkf/kY0pMCEnesCPRZJtPtNlIcBJE2+7IXSceLKI+LjcHONyyIUxsIM7s C8ihKqmeTFYAXFaKYIXCJvgU26DkSvKps0ctHjZ84wosrbNdz1u5pQFmW5T+uyfzbL0m 1yKw== X-Gm-Message-State: AAQBX9fEMuzMicrFLOImn0DwpecMzyxGDoOOAraIHuJkPccQe+MrNpqd joaIKbWPJhnEiQTXvW4HnugkXSs3QDlTiVRPitk= X-Google-Smtp-Source: AKy350ZsTNOTfkGJNDgRAiMdWGQVr/Q+y7HhNQcCUcQkZvcAomHjBfYje6Zto9k4habQ3aunPzhTIg== X-Received: by 2002:a7b:cbd3:0:b0:3f0:a785:f0f5 with SMTP id n19-20020a7bcbd3000000b003f0a785f0f5mr15850062wmi.16.1681910864795; Wed, 19 Apr 2023 06:27:44 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:44 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 03/16] virtio/vhost: Factor notify_vq_eventfd() Date: Wed, 19 Apr 2023 14:21:07 +0100 Message-Id: <20230419132119.124457-4-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org All vhost devices perform the same operation when setting up the ioeventfd. Move it to virtio/vhost.c Signed-off-by: Jean-Philippe Brucker Reviewed-by: Andre Przywara --- include/kvm/virtio.h | 2 ++ virtio/net.c | 9 +-------- virtio/scsi.c | 9 +-------- virtio/vhost.c | 14 ++++++++++++++ virtio/vsock.c | 14 ++------------ 5 files changed, 20 insertions(+), 28 deletions(-) diff --git a/include/kvm/virtio.h b/include/kvm/virtio.h index c8fd69e0..4a364a02 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -250,6 +250,8 @@ void virtio_notify_status(struct kvm *kvm, struct virtio_device *vdev, void virtio_vhost_init(struct kvm *kvm, int vhost_fd); void virtio_vhost_set_vring(struct kvm *kvm, int vhost_fd, u32 index, struct virt_queue *queue); +void virtio_vhost_set_vring_kick(struct kvm *kvm, int vhost_fd, + u32 index, int event_fd); int virtio_transport_parser(const struct option *opt, const char *arg, int unset); diff --git a/virtio/net.c b/virtio/net.c index 021c81d3..b935d24f 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -701,18 +701,11 @@ static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) { struct net_dev *ndev = dev; - struct vhost_vring_file file = { - .index = vq, - .fd = efd, - }; - int r; if (ndev->vhost_fd == 0 || is_ctrl_vq(ndev, vq)) return; - r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_KICK, &file); - if (r < 0) - die_perror("VHOST_SET_VRING_KICK failed"); + virtio_vhost_set_vring_kick(kvm, ndev->vhost_fd, vq, efd); } static int notify_vq(struct kvm *kvm, void *dev, u32 vq) diff --git a/virtio/scsi.c b/virtio/scsi.c index 674aad34..1f757404 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -123,18 +123,11 @@ static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) { struct scsi_dev *sdev = dev; - struct vhost_vring_file file = { - .index = vq, - .fd = efd, - }; - int r; if (sdev->vhost_fd == 0) return; - r = ioctl(sdev->vhost_fd, VHOST_SET_VRING_KICK, &file); - if (r < 0) - die_perror("VHOST_SET_VRING_KICK failed"); + virtio_vhost_set_vring_kick(kvm, sdev->vhost_fd, vq, efd); } static int notify_vq(struct kvm *kvm, void *dev, u32 vq) diff --git a/virtio/vhost.c b/virtio/vhost.c index afe37465..3acfd30a 100644 --- a/virtio/vhost.c +++ b/virtio/vhost.c @@ -65,3 +65,17 @@ void virtio_vhost_set_vring(struct kvm *kvm, int vhost_fd, u32 index, if (r < 0) die_perror("VHOST_SET_VRING_ADDR failed"); } + +void virtio_vhost_set_vring_kick(struct kvm *kvm, int vhost_fd, + u32 index, int event_fd) +{ + int r; + struct vhost_vring_file file = { + .index = index, + .fd = event_fd, + }; + + r = ioctl(vhost_fd, VHOST_SET_VRING_KICK, &file); + if (r < 0) + die_perror("VHOST_SET_VRING_KICK failed"); +} diff --git a/virtio/vsock.c b/virtio/vsock.c index 2f7906f2..0ada9e09 100644 --- a/virtio/vsock.c +++ b/virtio/vsock.c @@ -80,21 +80,11 @@ static int init_vq(struct kvm *kvm, void *dev, u32 vq) static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) { struct vsock_dev *vdev = dev; - struct vhost_vring_file file = { - .index = vq, - .fd = efd, - }; - int r; - if (is_event_vq(vq)) - return; - - if (vdev->vhost_fd == -1) + if (vdev->vhost_fd == -1 || is_event_vq(vq)) return; - r = ioctl(vdev->vhost_fd, VHOST_SET_VRING_KICK, &file); - if (r < 0) - die_perror("VHOST_SET_VRING_KICK failed"); + virtio_vhost_set_vring_kick(kvm, vdev->vhost_fd, vq, efd); } static void notify_status(struct kvm *kvm, void *dev, u32 status) From patchwork Wed Apr 19 13:21:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216806 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD7C9C6FD18 for ; Wed, 19 Apr 2023 13:27:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233236AbjDSN1y (ORCPT ); Wed, 19 Apr 2023 09:27:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233054AbjDSN1v (ORCPT ); Wed, 19 Apr 2023 09:27:51 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B746A30D4 for ; Wed, 19 Apr 2023 06:27:46 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id iw7-20020a05600c54c700b003f16fce55b5so1460170wmb.0 for ; Wed, 19 Apr 2023 06:27:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910865; x=1684502865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sf5jQ7FsskeiIkP7ak2gohZd1HhOl8Mi8w0DmpQUx7Y=; b=twXb0VmsbnVwF0zIwgKqy9r2oFER1FayZMFEuxP3JdhNVOr/Ku6Z0C2+Q8u8nvOJuE OSGxynG974S7fbpBk9SV1RS4eSWlN6MxdnOsaovh5iMBblSRpx/42w+KmTbmsb62FR6Z sf9/O3DdVouA6a3E01oKWHW3oLpzAeZWD0JIYgfiZ7ATHrvldB1hUHNPx72lKL1WpVGF szHpDFl8YM1m8yiuslt9M79TfH9uiXUx2is3SH0GpOqD0vGIqzAZxEXrsNCKE1jtHR4/ /2HJx6Ys/0B+VRcsOGjmyDP/Pno/id8VHOnuZjGHszuzDvBOrbphkg5q4pXTjuhwu6EC wUmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910865; x=1684502865; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sf5jQ7FsskeiIkP7ak2gohZd1HhOl8Mi8w0DmpQUx7Y=; b=JjpoAS5rYoAUDF42327pquG2dWUXXb/JtIhU4FUuBNHLhVKMWeJitmvnYwfWBYImbK eAtWySZ8++6UAyWV9RUnRdv0HWHwFsDLNKm0EnhKEX1NfVvBZXuzgNa1WeAY/DQ/njkl NwKHMRPRzAAGtZ+EpdmMm+S5ZyfsF7yXJkQSm/uUwQmVx/cQukiF0XzkFsg+puq5a/GL fRdXAL/K1eRr2t/zzF6N0clSZ0YIDojlEa85jrR1qG73Jjscr+bIjYeZVR8m0vl5vp12 YTramq2r4KTUTOxuDzWVIqi3LxLPur8WlqdWLHi4sumcd032MCOhlO95FO+OZfGRsVIy c+3A== X-Gm-Message-State: AAQBX9fsAcm/tCPOGWSlgIgK0/Vv2fXZcZYSiZW9EDApADzqCwkXBXnZ wWZ2yfR8EXcHsBFb0YObE1q4Vb5Co5QTI6E5aHM= X-Google-Smtp-Source: AKy350aLPsCQDPiEFrCFZTq4ivfnQTP/RVndwRTujvJwYN5/otW55YKff6iZ9kyiomZ9LxbX/FzvdA== X-Received: by 2002:a7b:c309:0:b0:3f0:967e:2cfb with SMTP id k9-20020a7bc309000000b003f0967e2cfbmr17899956wmj.36.1681910865312; Wed, 19 Apr 2023 06:27:45 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:45 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 04/16] virtio/vhost: Factor notify_vq_gsi() Date: Wed, 19 Apr 2023 14:21:08 +0100 Message-Id: <20230419132119.124457-5-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org All vhost devices should perform the same operations when initializing the IRQFD. Move it to virtio/vhost.c This fixes vsock, which didn't go through the irq__add_irqfd() helper and couldn't be used on systems that require GSI translation (GICv2m). Signed-off-by: Jean-Philippe Brucker --- include/kvm/virtio.h | 8 ++++++++ virtio/net.c | 30 ++++-------------------------- virtio/scsi.c | 15 ++------------- virtio/vhost.c | 35 +++++++++++++++++++++++++++++++++++ virtio/vsock.c | 26 +++----------------------- 5 files changed, 52 insertions(+), 62 deletions(-) diff --git a/include/kvm/virtio.h b/include/kvm/virtio.h index 4a364a02..96c7b3ea 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -77,6 +77,10 @@ struct virt_queue { u16 endian; bool use_event_idx; bool enabled; + + /* vhost IRQ handling */ + int gsi; + int irqfd; }; /* @@ -252,6 +256,10 @@ void virtio_vhost_set_vring(struct kvm *kvm, int vhost_fd, u32 index, struct virt_queue *queue); void virtio_vhost_set_vring_kick(struct kvm *kvm, int vhost_fd, u32 index, int event_fd); +void virtio_vhost_set_vring_call(struct kvm *kvm, int vhost_fd, u32 index, + u32 gsi, struct virt_queue *queue); +void virtio_vhost_reset_vring(struct kvm *kvm, int vhost_fd, u32 index, + struct virt_queue *queue); int virtio_transport_parser(const struct option *opt, const char *arg, int unset); diff --git a/virtio/net.c b/virtio/net.c index b935d24f..519dcbb7 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -4,12 +4,12 @@ #include "kvm/mutex.h" #include "kvm/util.h" #include "kvm/kvm.h" -#include "kvm/irq.h" #include "kvm/uip.h" #include "kvm/guest_compat.h" #include "kvm/iovec.h" #include "kvm/strbuf.h" +#include #include #include #include @@ -25,7 +25,6 @@ #include #include #include -#include #define VIRTIO_NET_QUEUE_SIZE 256 #define VIRTIO_NET_NUM_QUEUES 8 @@ -44,8 +43,6 @@ struct net_dev_queue { pthread_t thread; struct mutex lock; pthread_cond_t cond; - int gsi; - int irqfd; }; struct net_dev { @@ -647,11 +644,7 @@ static void exit_vq(struct kvm *kvm, void *dev, u32 vq) struct net_dev *ndev = dev; struct net_dev_queue *queue = &ndev->queues[vq]; - if (!is_ctrl_vq(ndev, vq) && queue->gsi) { - irq__del_irqfd(kvm, queue->gsi, queue->irqfd); - close(queue->irqfd); - queue->gsi = queue->irqfd = 0; - } + virtio_vhost_reset_vring(kvm, ndev->vhost_fd, vq, &queue->vq); /* * TODO: vhost reset owner. It's the only way to cleanly stop vhost, but @@ -675,27 +668,12 @@ static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) { struct net_dev *ndev = dev; struct net_dev_queue *queue = &ndev->queues[vq]; - struct vhost_vring_file file; - int r; if (ndev->vhost_fd == 0) return; - file = (struct vhost_vring_file) { - .index = vq, - .fd = eventfd(0, 0), - }; - - r = irq__add_irqfd(kvm, gsi, file.fd, -1); - if (r < 0) - die_perror("KVM_IRQFD failed"); - - queue->irqfd = file.fd; - queue->gsi = gsi; - - r = ioctl(ndev->vhost_fd, VHOST_SET_VRING_CALL, &file); - if (r < 0) - die_perror("VHOST_SET_VRING_CALL failed"); + virtio_vhost_set_vring_call(kvm, ndev->vhost_fd, vq, gsi, + &queue->vq); } static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) diff --git a/virtio/scsi.c b/virtio/scsi.c index 1f757404..29acf57c 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -92,25 +92,14 @@ static int init_vq(struct kvm *kvm, void *dev, u32 vq) static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) { - struct vhost_vring_file file; struct scsi_dev *sdev = dev; int r; if (sdev->vhost_fd == 0) return; - file = (struct vhost_vring_file) { - .index = vq, - .fd = eventfd(0, 0), - }; - - r = irq__add_irqfd(kvm, gsi, file.fd, -1); - if (r < 0) - die_perror("KVM_IRQFD failed"); - - r = ioctl(sdev->vhost_fd, VHOST_SET_VRING_CALL, &file); - if (r < 0) - die_perror("VHOST_SET_VRING_CALL failed"); + virtio_vhost_set_vring_call(kvm, sdev->vhost_fd, vq, gsi, + &sdev->vqs[vq]); if (vq > 0) return; diff --git a/virtio/vhost.c b/virtio/vhost.c index 3acfd30a..cd83645c 100644 --- a/virtio/vhost.c +++ b/virtio/vhost.c @@ -1,9 +1,12 @@ +#include "kvm/irq.h" #include "kvm/virtio.h" #include #include #include +#include + void virtio_vhost_init(struct kvm *kvm, int vhost_fd) { struct kvm_mem_bank *bank; @@ -79,3 +82,35 @@ void virtio_vhost_set_vring_kick(struct kvm *kvm, int vhost_fd, if (r < 0) die_perror("VHOST_SET_VRING_KICK failed"); } + +void virtio_vhost_set_vring_call(struct kvm *kvm, int vhost_fd, u32 index, + u32 gsi, struct virt_queue *queue) +{ + int r; + struct vhost_vring_file file = { + .index = index, + .fd = eventfd(0, 0), + }; + + r = irq__add_irqfd(kvm, gsi, file.fd, -1); + if (r < 0) + die_perror("KVM_IRQFD failed"); + + r = ioctl(vhost_fd, VHOST_SET_VRING_CALL, &file); + if (r < 0) + die_perror("VHOST_SET_VRING_CALL failed"); + + queue->irqfd = file.fd; + queue->gsi = gsi; +} + +void virtio_vhost_reset_vring(struct kvm *kvm, int vhost_fd, u32 index, + struct virt_queue *queue) + +{ + if (queue->gsi) { + irq__del_irqfd(kvm, queue->gsi, queue->irqfd); + close(queue->irqfd); + queue->gsi = queue->irqfd = 0; + } +} diff --git a/virtio/vsock.c b/virtio/vsock.c index 0ada9e09..559fbaba 100644 --- a/virtio/vsock.c +++ b/virtio/vsock.c @@ -131,33 +131,13 @@ static int set_size_vq(struct kvm *kvm, void *dev, u32 vq, int size) static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) { - struct vhost_vring_file file; struct vsock_dev *vdev = dev; - struct kvm_irqfd irq; - int r; - - if (vdev->vhost_fd == -1) - return; - if (is_event_vq(vq)) + if (vdev->vhost_fd == -1 || is_event_vq(vq)) return; - irq = (struct kvm_irqfd) { - .gsi = gsi, - .fd = eventfd(0, 0), - }; - file = (struct vhost_vring_file) { - .index = vq, - .fd = irq.fd, - }; - - r = ioctl(kvm->vm_fd, KVM_IRQFD, &irq); - if (r < 0) - die_perror("KVM_IRQFD failed"); - - r = ioctl(vdev->vhost_fd, VHOST_SET_VRING_CALL, &file); - if (r < 0) - die_perror("VHOST_SET_VRING_CALL failed"); + virtio_vhost_set_vring_call(kvm, vdev->vhost_fd, vq, gsi, + &vdev->vqs[vq]); } static unsigned int get_vq_count(struct kvm *kvm, void *dev) From patchwork Wed Apr 19 13:21:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216807 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFFA6C77B73 for ; Wed, 19 Apr 2023 13:27:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233239AbjDSN14 (ORCPT ); Wed, 19 Apr 2023 09:27:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233159AbjDSN1w (ORCPT ); Wed, 19 Apr 2023 09:27:52 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 820D349C1 for ; Wed, 19 Apr 2023 06:27:47 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id o9-20020a05600c510900b003f17012276fso1448241wms.4 for ; Wed, 19 Apr 2023 06:27:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910866; x=1684502866; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=I+MhtDXpNm5xTXmxBDZjwHd5dF/CwFonCJebOs/Sjv0=; b=ag8TNikmslT8ZwFI28bXZQp0jQ2NEOpkgY1fledFnA3HspOJqQuC4vKaUQPFtaNgyV 4qReeD36oVcndfZECBFEYE5iQ0T62fncXo5R9OscHdNX1BKP/zRUx+Ni9W2vaJHtqOHl JHDq2Evn9NM2B1i48cuyKZ0aeRcDlRB/O5drzibvpmQ3GNAgcVuOi7sJqxtvAxuIbHf8 DRDbs/MPy9+4/C9xM9MwGd71FCZ67MdCfTehpUusYkgze7IF9voBDe984dgGYekxiVNg lzzuvNJJUFjT5MrLY85cB+gU/1LEpneLTrQMt+mzMz10VsAJN2Z1LQgET+mAkwqGM+7g nh5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910866; x=1684502866; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=I+MhtDXpNm5xTXmxBDZjwHd5dF/CwFonCJebOs/Sjv0=; b=ReHc3QFlk0Yj8XrMwUkpj7PfrHbxRTLkvNYPsKEqjGkCt1JFR/GzTfFwxLJ4YSbKTB MbF9S8ldjU8D30bgv1Yuyir7m2dkn6Hpz4341I5WjrWMp96lttLqzaK40dVjQ0QN3foJ udfkhX5YgXbBnRP5URJQ32z9zE31tJZaCpzNWELk/Qnl6R7+dl/NX5EzEqWw2jYRo3Sw jcLkyLTAlvb0x3OtjBJ74M0EIorhU9mUMtuG1rRS2OaAaDybtPZ3pif2ki9j/H4EGyCm 3jH5IqQ6DtQ4GJyjBojsqsX74Cws7Q7cvGlxrNKIFLAME1w92b+d3pP7w/u3wJCQaHiF z8Mw== X-Gm-Message-State: AAQBX9eDIH9op5rPmSf+q7cCpIHOAtBKssOKwkixokOZKfscL8NMPte7 GYK6iMRNfLRRja/bMS2v8pw6f97PWht0umOJ6kE= X-Google-Smtp-Source: AKy350YOmAzAPp044dVvjFGW2cY2ENgXf3qvzYHzQDkOSeSSjV8wtfZTiZqj2aN7T6bI3I+fiN1Bow== X-Received: by 2002:a05:600c:2195:b0:3f1:7277:eaa with SMTP id e21-20020a05600c219500b003f172770eaamr8244940wme.31.1681910865802; Wed, 19 Apr 2023 06:27:45 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:45 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 05/16] virtio/scsi: Move VHOST_SCSI_SET_ENDPOINT to device start Date: Wed, 19 Apr 2023 14:21:09 +0100 Message-Id: <20230419132119.124457-6-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The vhost driver expects virtqueues to be operational by the time we call SET_ENDPOINT. We currently do it too early. Device start, which happens when the driver writes the DRIVER_OK status, is a good time to do this. Signed-off-by: Jean-Philippe Brucker --- virtio/scsi.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/virtio/scsi.c b/virtio/scsi.c index 29acf57c..2bc1ec20 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -56,6 +56,13 @@ static void notify_status(struct kvm *kvm, void *dev, u32 status) struct virtio_device *vdev = &sdev->vdev; struct virtio_scsi_config *conf = &sdev->config; + if (status & VIRTIO__STATUS_START) { + int r = ioctl(sdev->vhost_fd, VHOST_SCSI_SET_ENDPOINT, + &sdev->target); + if (r != 0) + die("VHOST_SCSI_SET_ENDPOINT failed %d", errno); + } + if (!(status & VIRTIO__STATUS_CONFIG)) return; @@ -93,20 +100,12 @@ static int init_vq(struct kvm *kvm, void *dev, u32 vq) static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) { struct scsi_dev *sdev = dev; - int r; if (sdev->vhost_fd == 0) return; virtio_vhost_set_vring_call(kvm, sdev->vhost_fd, vq, gsi, &sdev->vqs[vq]); - - if (vq > 0) - return; - - r = ioctl(sdev->vhost_fd, VHOST_SCSI_SET_ENDPOINT, &sdev->target); - if (r != 0) - die("VHOST_SCSI_SET_ENDPOINT failed %d", errno); } static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) From patchwork Wed Apr 19 13:21:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216809 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E9ACC77B73 for ; Wed, 19 Apr 2023 13:28:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233214AbjDSN16 (ORCPT ); Wed, 19 Apr 2023 09:27:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233213AbjDSN1y (ORCPT ); Wed, 19 Apr 2023 09:27:54 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF0B349C9 for ; Wed, 19 Apr 2023 06:27:47 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id v10so10482144wmn.5 for ; Wed, 19 Apr 2023 06:27:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910866; x=1684502866; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hwApBVQ2g0KAbIfkD7yCskt8c3DezfvPvJZ20OTJHoI=; b=T74+kEScG2EHyAnIyGBsEff27uucPLexIizTr4P+vhkAM+hKg7edh1Fghsi3/uux8Y LzCpwmxfnE7WPIJQL5MdKr+ZoCEPMllw67gr1kh7L3s3LgRN2hZ1Qrq2NICYJcUHgT4y QH82k5v46pKtVl3N+DWBY5L2jXoLUWOkyavdO6ee7dBhsZcEosheBryHt+YB2J1Whbib ozFmtlQ+OaAU/vXhvCMyseyweF4U+mXYHCN9YaFsLElqRWeueH9CnLzVEN3o8yrVkGMa Gz2ctp1EGTM4URDcUevrgQL/0EPrCrgg6UxYqMEXxhfNZNjyNxe/v5pL3Ks9RjFpP5Hv +ung== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910866; x=1684502866; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hwApBVQ2g0KAbIfkD7yCskt8c3DezfvPvJZ20OTJHoI=; b=QtisxvBHpzn9/wN9bTHoO+o4jOih/sQSgwU+ShO2G2wcgKaQGXA8n6gO2/jyEZ+b2l +p7jaWVBIVwFNbLss/NwmWSD6IgmBatMsNC2vCRIkphpMU3bf8jWhGNzyOB48N+aphLA QwBcomDnn7HJR1eXvu9j39QdDGmGF+LcnewaqKOqo6xb8T/Enztmn9grHEMmeI334vSb SRAdMTeUPxL1T5qWI+iHxtasSZAzxg8GMY8zWgV+vzp7MKXhzbXBSIe2kgmYaJSDX3u1 ixW/skLMKNZ7U5mnC0dz/lErAeczIpZf8Gm3/9Lfzt7ARFMmticlwBbbscfkLVz8kOc2 EGQw== X-Gm-Message-State: AAQBX9eQTre9ZaWSOU3CZKaMriS7qz1GzRZPm/KNvOT2aED6Yz636Y/P V6CbbLdnYZYcDKPCxVqZVYEaVCsQ7y2rvJ6wqaw= X-Google-Smtp-Source: AKy350bSP1G8uPOdUlOE3W/QNq4H11j7PLeFb1wD4n7gIzJClD1GuwdpPqLHKBjE5gaqBnIR7aGWtA== X-Received: by 2002:a7b:c40f:0:b0:3f1:6757:6238 with SMTP id k15-20020a7bc40f000000b003f167576238mr11741592wmi.21.1681910866369; Wed, 19 Apr 2023 06:27:46 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:46 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 06/16] virtio/scsi: Fix and simplify command-line Date: Wed, 19 Apr 2023 14:21:10 +0100 Message-Id: <20230419132119.124457-7-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Fix and simplify the command-line parameter for virtio-scsi. Currently passing a "scsi:xxxx" parameter without the second "tpgt" argument causes kvmtool to segfault. But only the "wwpn" parameter is necessary. The tpgt parameter is ignored and was never used upstream. See linux/vhost_types.h: * ABI Rev 0: July 2012 version starting point for v3.6-rc merge candidate + * RFC-v2 vhost-scsi userspace. Add GET_ABI_VERSION ioctl usage * ABI Rev 1: January 2013. Ignore vhost_tpgt field in struct vhost_scsi_target. * All the targets under vhost_wwpn can be seen and used by guset. Signed-off-by: Jean-Philippe Brucker --- include/kvm/disk-image.h | 7 +------ disk/core.c | 11 ++++------- virtio/scsi.c | 2 +- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/include/kvm/disk-image.h b/include/kvm/disk-image.h index b2123838..bf602b58 100644 --- a/include/kvm/disk-image.h +++ b/include/kvm/disk-image.h @@ -48,12 +48,8 @@ struct disk_image_operations { struct disk_image_params { const char *filename; - /* - * wwpn == World Wide Port Number - * tpgt == Target Portal Group Tag - */ + /* wwpn == World Wide Port Number */ const char *wwpn; - const char *tpgt; bool readonly; bool direct; }; @@ -74,7 +70,6 @@ struct disk_image { u64 aio_inflight; #endif /* CONFIG_HAS_AIO */ const char *wwpn; - const char *tpgt; int debug_iodelay; }; diff --git a/disk/core.c b/disk/core.c index f69095d9..45681024 100644 --- a/disk/core.c +++ b/disk/core.c @@ -25,14 +25,14 @@ int disk_img_name_parser(const struct option *opt, const char *arg, int unset) if (strncmp(arg, "scsi:", 5) == 0) { sep = strstr(arg, ":"); - if (sep) - kvm->cfg.disk_image[kvm->nr_disks].wwpn = sep + 1; + kvm->cfg.disk_image[kvm->nr_disks].wwpn = sep + 1; + + /* Old invocation had two parameter. Ignore second one. */ sep = strstr(sep + 1, ":"); if (sep) { *sep = 0; - kvm->cfg.disk_image[kvm->nr_disks].tpgt = sep + 1; + cur = sep + 1; } - cur = sep + 1; } do { @@ -147,7 +147,6 @@ static struct disk_image **disk_image__open_all(struct kvm *kvm) struct disk_image **disks; const char *filename; const char *wwpn; - const char *tpgt; bool readonly; bool direct; void *err; @@ -169,14 +168,12 @@ static struct disk_image **disk_image__open_all(struct kvm *kvm) readonly = params[i].readonly; direct = params[i].direct; wwpn = params[i].wwpn; - tpgt = params[i].tpgt; if (wwpn) { disks[i] = malloc(sizeof(struct disk_image)); if (!disks[i]) return ERR_PTR(-ENOMEM); disks[i]->wwpn = wwpn; - disks[i]->tpgt = tpgt; continue; } diff --git a/virtio/scsi.c b/virtio/scsi.c index 2bc1ec20..f059fc37 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -199,7 +199,7 @@ static int virtio_scsi_init_one(struct kvm *kvm, struct disk_image *disk) .kvm = kvm, }; strlcpy((char *)&sdev->target.vhost_wwpn, disk->wwpn, sizeof(sdev->target.vhost_wwpn)); - sdev->target.vhost_tpgt = strtol(disk->tpgt, NULL, 0); + sdev->target.abi_version = VHOST_SCSI_ABI_VERSION; list_add_tail(&sdev->list, &sdevs); From patchwork Wed Apr 19 13:21:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216808 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0DFD6C77B7C for ; Wed, 19 Apr 2023 13:27:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233337AbjDSN15 (ORCPT ); Wed, 19 Apr 2023 09:27:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233214AbjDSN1y (ORCPT ); Wed, 19 Apr 2023 09:27:54 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F3D54C19 for ; Wed, 19 Apr 2023 06:27:48 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-2fa36231b1cso1688683f8f.2 for ; Wed, 19 Apr 2023 06:27:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910867; x=1684502867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Lc2RgCQ3JuNR8hddRAxLIwG/89ZluMdXFWj41Fj8oSw=; b=n6mRyT2MIv9lHGN1wXoCG/p65YnzXn1RG9mdHYzy34f6EeJY/i4+hqAIjmLPuNbce8 AnlbMGcnPuVf/MOUdobUUSvNnk20EaAkRC9gVZqo4Xmkh1GgGQsk6u/UO45beHVnQVKe DpKJ057QKXFw5SyAWzF0EPb99j8a4tCbE+mWRr5DqR32hL0JsI0Yl1/x1HynuJoH/i+4 oG0sX9ofbwbvrLBN56MwGK5eMQzO7fcH+YmG81gfd5GWAqIKX9LhrQhWyREmzfUo0mo1 a5Ryu1RO+OETJOGh1kYKK9UveTZi5sEXjvMDhxjmITpS76MOB6bbZE8lvBz09aG+IdF3 1AJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910867; x=1684502867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Lc2RgCQ3JuNR8hddRAxLIwG/89ZluMdXFWj41Fj8oSw=; b=Pu5vMn6h/eMuoK8Stxf4cUlirMlVEHnpyG0Fb+HJO2DlkvvKM8DZ2vW24AQrphjqJi xK/YbMty49TZl215UhFIHFCMUAGBOKsCIcOAsoohKxsDIJeX4RBUVws4sQ7ZXBATssbF hT2at955KOqYpCzJ4A3j2z3tiwlCJCJ4sUL4WNU7UfRytpEF42GbOZEvZAa8x2Ct2Yb7 p0xamoIH9TIbpQIU/Aut6oiZFDuknxwFO00lMT3Q25KkiTf/jxkqxVuOwPYp+P85K2cm 6dq8TQQTDKISpkzA0BhTx2O+pxGwKJuwAMLIzSP3lxdGIHmtkpthr4ZoMsV3Kzr+36Ok ajNQ== X-Gm-Message-State: AAQBX9c+TCJZdy1GB7WikwzSws9X+HvHK0eY2e/zSfDj5QlSWUIW+asY pCWl3PtrzwCeoE9z8wlUONV8WEuA2Wm0/8BOYiI= X-Google-Smtp-Source: AKy350Yg2CANpIRLxHFGFcgtXtzLWSpsQpLpAVW91z6ktR855Cdf0VQv7lN2ITR60Bq5lZI88wYSKA== X-Received: by 2002:adf:e787:0:b0:2f2:8de9:f744 with SMTP id n7-20020adfe787000000b002f28de9f744mr4419076wrm.31.1681910866890; Wed, 19 Apr 2023 06:27:46 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:46 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 07/16] disk/core: Fix segfault on exit with SCSI Date: Wed, 19 Apr 2023 14:21:11 +0100 Message-Id: <20230419132119.124457-8-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The SCSI backend doesn't call disk_image__new() so the disk ops are NULL. Check for this case on exit. Signed-off-by: Jean-Philippe Brucker --- disk/core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/disk/core.c b/disk/core.c index 45681024..35689b8e 100644 --- a/disk/core.c +++ b/disk/core.c @@ -223,10 +223,10 @@ static int disk_image__close(struct disk_image *disk) disk_aio_destroy(disk); - if (disk->ops->close) + if (disk->ops && disk->ops->close) return disk->ops->close(disk); - if (close(disk->fd) < 0) + if (disk->fd && close(disk->fd) < 0) pr_warning("close() failed"); free(disk); From patchwork Wed Apr 19 13:21:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216811 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D3FDC77B75 for ; Wed, 19 Apr 2023 13:28:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233445AbjDSN2B (ORCPT ); Wed, 19 Apr 2023 09:28:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233054AbjDSN1z (ORCPT ); Wed, 19 Apr 2023 09:27:55 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C333859DF for ; Wed, 19 Apr 2023 06:27:48 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id he11-20020a05600c540b00b003ef6d684102so1450175wmb.3 for ; Wed, 19 Apr 2023 06:27:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910867; x=1684502867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=whKVeNhm2/WfA3zu2frln50fm/eIKX+KatcyC647UUQ=; b=DV/fNHDX0ZMlhe4V4S5gfOnqyYYsYVFwSPyIBZVKNMFXb78AOvqDmPD4SsHQNHwDV+ GFPB1R5WhbUm/37KiE+MwWjj2AcoVbJRCMzx6mgcE3UOhK4F4LRq+iuOkhGQXaq8kMI5 E0gnYC6mXM2dGfFkkgXLbP1+M13hlY1imYsCHXubjVzt+Zl1SUfJF6CIo8I3+tYSHa3V aKYS8nM8yOQNrldXV2PAF3pvOwLF/KSkIo0wciPBnlF0PKIsdvYbGc3sKPzFkkKY3Gej uiNZShtpbyT9oerFX2LW19kOsAKD+xtZMlNMNwzV3JZvCmOcn2dmHD7X2GeuV5hXLpwn qvjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910867; x=1684502867; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=whKVeNhm2/WfA3zu2frln50fm/eIKX+KatcyC647UUQ=; b=aQkqqvGAfZDgmgAzGuCCqUI+ris8R321ZFd9e3vkH2PshbCcPpoEAu5FL39APy5qOw e9cQeQbNrI7TDCQVEXRTl9uc93HQKqmITWtV3WyS80X8LhmJLhU/1AlsgYEOvg7HFshX vmE3J2Y0DG+LeEN22kPFH7brWCruGyDl+oaD7j6gF/iM7iq4yhN2IK7YyHwmCDpqxS0/ GZ6+Zq5z0nGKvQleiipDHxZIF2ZCOj8+oA+ISRvp3/SRjPhqzc+z3ZliZt9jaJkrOws2 d6fkV0PNtVNozHtkHpEqN7ZeCecT7eD7fPi2sGRpNCGuHf6Kxm+2/k70mvULWmxKb+d7 I0Jg== X-Gm-Message-State: AAQBX9eOJ6mQwdDA4osjVYXXBOakUcX3MwAYYVHVQHpbFOE2LyQe04TU 2xtC2WPYnGt4rb5PoR6uW6tfGKAp2uFuytdxb+8= X-Google-Smtp-Source: AKy350bWGN+fAZqwUsNHozqjs4Ra8wtNhThRPf1OYPNFXFd8wBF5WS867krnsy01o+qnODSXTnj7+g== X-Received: by 2002:a05:600c:2103:b0:3f1:6474:c144 with SMTP id u3-20020a05600c210300b003f16474c144mr12061764wml.24.1681910867394; Wed, 19 Apr 2023 06:27:47 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:47 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 08/16] virtio/scsi: Initialize max_target Date: Wed, 19 Apr 2023 14:21:12 +0100 Message-Id: <20230419132119.124457-9-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The Linux guest does not find any target when 'max_target' is 0. Initialize it to the maximum defined by virtio, "5.6.4 Device configuration layout": max_target SHOULD be less than or equal to 255. Signed-off-by: Jean-Philippe Brucker --- virtio/scsi.c | 1 + 1 file changed, 1 insertion(+) diff --git a/virtio/scsi.c b/virtio/scsi.c index f059fc37..fc1c2ad9 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -76,6 +76,7 @@ static void notify_status(struct kvm *kvm, void *dev, u32 status) conf->sense_size = virtio_host_to_guest_u32(vdev, VIRTIO_SCSI_SENSE_SIZE); conf->cdb_size = virtio_host_to_guest_u32(vdev, VIRTIO_SCSI_CDB_SIZE); conf->max_lun = virtio_host_to_guest_u32(vdev, 16383); + conf->max_target = virtio_host_to_guest_u32(vdev, 255); conf->event_info_size = virtio_host_to_guest_u32(vdev, sizeof(struct virtio_scsi_event)); } From patchwork Wed Apr 19 13:21:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216812 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9929C6FD18 for ; Wed, 19 Apr 2023 13:28:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233475AbjDSN2D (ORCPT ); Wed, 19 Apr 2023 09:28:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233288AbjDSN1z (ORCPT ); Wed, 19 Apr 2023 09:27:55 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71CBC59F0 for ; Wed, 19 Apr 2023 06:27:49 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id r15so7558937wmo.1 for ; Wed, 19 Apr 2023 06:27:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910868; x=1684502868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SkoSoKNv/YXHE8EKeXeR9Q6UNSRCk5/IST0YxoWr2N8=; b=npOrSGVDRUHtOj31sPo069NHOAG1xZsv+DPlGjIbzXnTcQf6Ij9BUoS9lz2M7Vd/7N Tx4eiu/f66QO5nko83CWd5KYN9IMzj54DkDOSbmHFzO3tWqmpXz2eROF9z52Rh0T17XJ wKuVzQFmCBlo+Kvm7+WDTdJEj9yAfgzblGEZoHVwjirUH+ffhOzLkq6ZsEJc4j+GEgaV g9c+ZUJOq0MnQ7N+m+9bq4q9ruMhWXBNymUNtaDzj3C8eWrqyqR/K0sq/KmcJB+hgTOS U/B4I6HC3tMtPDwsg7oFUfzobuYwJod9/PY7C+Yi2MhSBuDcGJfNPzHOHxgoEm3dcYF2 CXwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910868; x=1684502868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SkoSoKNv/YXHE8EKeXeR9Q6UNSRCk5/IST0YxoWr2N8=; b=PhAoIKoIkhFxi4MqcR/BQCEIOjE6nm8Eo0DEZ0LiDOw3NU9nfbeEBGwPgha8vSuhfG yK+1xVLARWgisb4bSY5qLYMZtLeim31R3tBSerq6grOlqWcWwpK+v6pNaiREyyITgpNN 4BtxXjvkzEh6a87O9fG1+62MYbab4PhyOlW/gkrFT+ohhh0JiEaAZjwqafej2C0M++h+ +cZ3WZPbdEKw8GNTCKMnhiFxOly991rL3M/nzgW5ZXbrbRf5BsJ5ueAmfuwIMHZKpmuU WxisuaJWOk9P15PfDKw/aOiYO4EkwgaaiHxsgo68dxC0zOF8WPL1tM2VkzWUrHqpuXbn bqbQ== X-Gm-Message-State: AAQBX9ethLMPvYZbFlDmcFGp34pzWKF04Cqmy2UJHdWbT41sSiwH5Adf nZtuIhME7otwDJq4UZl6ZfDLXGBSZ1/ZFVLo+40= X-Google-Smtp-Source: AKy350bKrOHr39CsBEQClTVd7Dgwd3ZhsMnjPwIG/93FjsB0ZrwgjSOd+rSxZJnpL77vMQWG07wgyg== X-Received: by 2002:a7b:c3cc:0:b0:3f1:6fca:d5a5 with SMTP id t12-20020a7bc3cc000000b003f16fcad5a5mr9258589wmj.17.1681910867904; Wed, 19 Apr 2023 06:27:47 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:47 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 09/16] virtio/scsi: Fix feature selection Date: Wed, 19 Apr 2023 14:21:13 +0100 Message-Id: <20230419132119.124457-10-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org We should advertise to the guest only the features supported by vhost and kvmtool. Then we should set in vhost only the features acked by the guest. Move vhost feature query to get_host_features(), and vhost feature setting to device start (after the guest has acked features). This fixes scsi because we used to enable all vhost features including VIRTIO_SCSI_F_T10_PI which changes the request layout and caused inconsistency between guest and vhost. Signed-off-by: Jean-Philippe Brucker --- virtio/scsi.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/virtio/scsi.c b/virtio/scsi.c index fc1c2ad9..5f392814 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -46,18 +46,34 @@ static size_t get_config_size(struct kvm *kvm, void *dev) static u64 get_host_features(struct kvm *kvm, void *dev) { - return 1UL << VIRTIO_RING_F_EVENT_IDX | - 1UL << VIRTIO_RING_F_INDIRECT_DESC; + int r; + u64 features; + struct scsi_dev *sdev = dev; + + r = ioctl(sdev->vhost_fd, VHOST_GET_FEATURES, &features); + if (r != 0) + die_perror("VHOST_GET_FEATURES failed"); + + return features & + (1ULL << VIRTIO_RING_F_EVENT_IDX | \ + 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \ + 1ULL << VIRTIO_F_ANY_LAYOUT); } static void notify_status(struct kvm *kvm, void *dev, u32 status) { + int r; struct scsi_dev *sdev = dev; struct virtio_device *vdev = &sdev->vdev; struct virtio_scsi_config *conf = &sdev->config; if (status & VIRTIO__STATUS_START) { - int r = ioctl(sdev->vhost_fd, VHOST_SCSI_SET_ENDPOINT, + r = ioctl(sdev->vhost_fd, VHOST_SET_FEATURES, + &sdev->vdev.features); + if (r != 0) + die_perror("VHOST_SET_FEATURES failed"); + + r = ioctl(sdev->vhost_fd, VHOST_SCSI_SET_ENDPOINT, &sdev->target); if (r != 0) die("VHOST_SCSI_SET_ENDPOINT failed %d", errno); @@ -163,23 +179,12 @@ static struct virtio_ops scsi_dev_virtio_ops = { static void virtio_scsi_vhost_init(struct kvm *kvm, struct scsi_dev *sdev) { - u64 features; - int r; - sdev->vhost_fd = open("/dev/vhost-scsi", O_RDWR); if (sdev->vhost_fd < 0) die_perror("Failed openning vhost-scsi device"); virtio_vhost_init(kvm, sdev->vhost_fd); - r = ioctl(sdev->vhost_fd, VHOST_GET_FEATURES, &features); - if (r != 0) - die_perror("VHOST_GET_FEATURES failed"); - - r = ioctl(sdev->vhost_fd, VHOST_SET_FEATURES, &features); - if (r != 0) - die_perror("VHOST_SET_FEATURES failed"); - sdev->vdev.use_vhost = true; } From patchwork Wed Apr 19 13:21:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216813 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CCCA1C77B73 for ; Wed, 19 Apr 2023 13:28:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233357AbjDSN2E (ORCPT ); Wed, 19 Apr 2023 09:28:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233159AbjDSN14 (ORCPT ); Wed, 19 Apr 2023 09:27:56 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DCE315B85 for ; Wed, 19 Apr 2023 06:27:49 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id v3so4079213wml.0 for ; Wed, 19 Apr 2023 06:27:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910868; x=1684502868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dixA9kKFl5JSfKMwpuKUuIPy386aRkC3mwFk4r9wlY8=; b=Y3KwykM3BE51NzROM2NHye3ECanMguJpIUmsLTQn+5ija77ydBuqvzFyau3aOfT9a3 occxsEWlpknfvqwLvMSPyRvKGWazl7cRxE1fJOJ+W1tTLwH7JFH78a2EJdryxUNZVHnk soRKNwTJJGUMvga7rG2Yv4srg8X3tDFUd7JAwDasU1HCdADnCeXO1t/yv/GXv0j58paq uxMbEI8ES8GQR7NrKftErhK8cQYwltpbWg6wGnlke5YBWOSLLE59dwUG+l+d3LNk5gtu 9irel1dUak0g5OVrCPsJzLxuv03m05HDUndqfsIuRary7HjDzzJc/BYq5z4QynvYh3Yg x4bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910868; x=1684502868; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dixA9kKFl5JSfKMwpuKUuIPy386aRkC3mwFk4r9wlY8=; b=H5uc7ZNpH5f0c+KlmXJPVxsLzgwNJUWlR1LpaCVXKtvu8NlZj5jo6MlNGm8IizpO7G RMWvYrcKgX/xX26+bbHgJ9Lk9zM/f3QGV1Oz4wzKzqXlZvdyKlHDFIrZoVk7JByMAtDO TUDRsCJdnreXw1j4nOvtAYuDxiG9AOm9Nq8gEUE7JHCHWo8e1kLnBA6/KvrTnvnnYaJA EhUu+Ul437XQnx16jr/wuVyCke6PuDRbhfJVk6cwiDpyMRIVxLqdvIX9U4jXI+JVCFum i5sqonwaqaKczmYT76LobwCOJmo0HvLllviHSTZE9VEJXpxiWqFiFtc68UssQNHigkQf WPyg== X-Gm-Message-State: AAQBX9e2APdjyhqiZqLno8XpYGKJNWJELWu9yHTnES00Gq9VqZZ6Ky3f j9mdOu1jhXpajbEVe6+8+W7eHcqyDWWSmRJnBZ8= X-Google-Smtp-Source: AKy350ZxldFMhe1QuQBvEujxYp1jIY6xzrKkIHksCqeF2ngc5eLqwL03sgg+U4Zg0WfUbDMhUTBaYw== X-Received: by 2002:a05:600c:2241:b0:3f1:7a44:317c with SMTP id a1-20020a05600c224100b003f17a44317cmr4207622wmm.24.1681910868493; Wed, 19 Apr 2023 06:27:48 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:48 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 10/16] virtio/vsock: Fix feature selection Date: Wed, 19 Apr 2023 14:21:14 +0100 Message-Id: <20230419132119.124457-11-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org We should advertise to the guest only the features supported by vhost and kvmtool. Then we should set in vhost only the features acked by the guest. Move vhost feature query to get_host_features(), and vhost feature setting to device start (after the guest has acked features). This fixes vsock because we used to enable all vhost features including VIRTIO_F_ACCESS_PLATFORM, which forces vhost to use vhost-iotlb and isn't supported by kvmtool. Signed-off-by: Jean-Philippe Brucker --- virtio/vsock.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/virtio/vsock.c b/virtio/vsock.c index 559fbaba..64512713 100644 --- a/virtio/vsock.c +++ b/virtio/vsock.c @@ -51,8 +51,17 @@ static size_t get_config_size(struct kvm *kvm, void *dev) static u64 get_host_features(struct kvm *kvm, void *dev) { - return 1UL << VIRTIO_RING_F_EVENT_IDX - | 1UL << VIRTIO_RING_F_INDIRECT_DESC; + int r; + u64 features; + struct vsock_dev *vdev = dev; + + r = ioctl(vdev->vhost_fd, VHOST_GET_FEATURES, &features); + if (r != 0) + die_perror("VHOST_GET_FEATURES failed"); + + return features & + (1ULL << VIRTIO_RING_F_EVENT_IDX | + 1ULL << VIRTIO_RING_F_INDIRECT_DESC); } static bool is_event_vq(u32 vq) @@ -95,12 +104,18 @@ static void notify_status(struct kvm *kvm, void *dev, u32 status) if (status & VIRTIO__STATUS_CONFIG) vdev->config.guest_cid = cpu_to_le64(vdev->guest_cid); - if (status & VIRTIO__STATUS_START) + if (status & VIRTIO__STATUS_START) { start = 1; - else if (status & VIRTIO__STATUS_STOP) + + r = ioctl(vdev->vhost_fd, VHOST_SET_FEATURES, + &vdev->vdev.features); + if (r != 0) + die_perror("VHOST_SET_FEATURES failed"); + } else if (status & VIRTIO__STATUS_STOP) { start = 0; - else + } else { return; + } r = ioctl(vdev->vhost_fd, VHOST_VSOCK_SET_RUNNING, &start); if (r != 0) @@ -162,7 +177,6 @@ static struct virtio_ops vsock_dev_virtio_ops = { static void virtio_vhost_vsock_init(struct kvm *kvm, struct vsock_dev *vdev) { - u64 features; int r; vdev->vhost_fd = open("/dev/vhost-vsock", O_RDWR); @@ -171,14 +185,6 @@ static void virtio_vhost_vsock_init(struct kvm *kvm, struct vsock_dev *vdev) virtio_vhost_init(kvm, vdev->vhost_fd); - r = ioctl(vdev->vhost_fd, VHOST_GET_FEATURES, &features); - if (r != 0) - die_perror("VHOST_GET_FEATURES failed"); - - r = ioctl(vdev->vhost_fd, VHOST_SET_FEATURES, &features); - if (r != 0) - die_perror("VHOST_SET_FEATURES failed"); - r = ioctl(vdev->vhost_fd, VHOST_VSOCK_SET_GUEST_CID, &vdev->guest_cid); if (r != 0) die_perror("VHOST_VSOCK_SET_GUEST_CID failed"); From patchwork Wed Apr 19 13:21:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216810 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D4A9C6FD18 for ; Wed, 19 Apr 2023 13:28:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233213AbjDSN2A (ORCPT ); Wed, 19 Apr 2023 09:28:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233203AbjDSN1y (ORCPT ); Wed, 19 Apr 2023 09:27:54 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE6B7AF18 for ; Wed, 19 Apr 2023 06:27:50 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id a8-20020a05600c348800b003f17ddb04e3so1370864wmq.2 for ; Wed, 19 Apr 2023 06:27:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910869; x=1684502869; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Jl9r/SrtoqzL08B+Mo9CU9ryDJ9DromwEAR1LvYPmow=; b=nGOp9ESTpMcF48UcXqxvhnG4mwz/jSO8A1V4V6+hSFKP8aoAl8AM0EhAJ7b/Ex5Ohc jyD6oYNmXnl3IYxYPX1cc83MCnLSTjhxSyI56cyve/vgJM+BuPzoq1cYn0Ii1A/iVSB3 MjLqA5wMxRG/y+oTReCviguincuz6KKIPCzP8jmvusXcaXq+n7qk3cN8NZV2SUKWgF9p xdNB1HRr+BDMYOF5xtGheCDNTFGvURQbMiDbgJScaUOO+LUeRO14ybv+ng8qdnRjaF66 fZ0osc/fjuIg+Lc0zRgqqdtjKc5AKW9VKjOMIHIuNraPlYFyHIq69FwHv+pjMDQ9vHuv YP+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910869; x=1684502869; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Jl9r/SrtoqzL08B+Mo9CU9ryDJ9DromwEAR1LvYPmow=; b=NLgiELvEgEqJ4nO7OZrWYrGT4Pg8Rp/YJ92Lj41NbRR3gO8wyTHxu7Ie1TmI2i9J6g t3/0dI0O4rzcNK5LDgbTMjuHc00N6cbY8F/j6so77iE0CTq9kLA/hlynBVrICgBhrihk KcNGiC88S2cjXIE6zkwSRxid1Qq8iF2T8zRv/K4l3Iefg90FqbL9Fpww3r3Jmr6reWou 5rnpf8s+RWfbOvmN+GZSDD6LKgSw1mIEKGKSaDhA6CopgNggQkSepxx6ZXzaAvNI/fbX 4pBmDfUIYDIdV7/a+fG8IUWzY/6bauoV+7KQBFEKYqo2AqdKV0vtSWd9yKco0aU1YDDn 3Slg== X-Gm-Message-State: AAQBX9d0GIVkIAoQzH4qwedYH+5RTiMoKZq/XBmizQGsg6jzN4JZzGfx pzmEdU8jbDKaD37uX1Q33ZY9T+B3iqce4lwdL9s= X-Google-Smtp-Source: AKy350atw9xXHyTP1rNg5en1YnZQh7WRU4SFZ3oUSVwB0iANrEUPcLhhjwg1/THfR+Ac0dqXFH9N8w== X-Received: by 2002:a7b:cbcb:0:b0:3f1:7675:fb82 with SMTP id n11-20020a7bcbcb000000b003f17675fb82mr6753735wmi.10.1681910869043; Wed, 19 Apr 2023 06:27:49 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:48 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 11/16] virtio/net: Fix feature selection Date: Wed, 19 Apr 2023 14:21:15 +0100 Message-Id: <20230419132119.124457-12-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move VHOST_GET_FEATURES to get_host_features() so the guest is aware of what will actually be supported. This removes the invalid guess about VIRTIO_NET_F_MRG_RXBUF (if vhost didn't support it, we shouldn't let the guest negotiate it). Note the masking of VHOST_NET_F_VIRTIO_NET_HDR when handing features to vhost. Unfortunately the vhost-net driver interprets VIRTIO_F_ANY_LAYOUT as VHOST_NET_F_VIRTIO_NET_HDR, which is specific to vhost and forces vhost-net to supply the vnet header. Since this is done by tap, we don't want to set the bit. Signed-off-by: Jean-Philippe Brucker --- virtio/net.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/virtio/net.c b/virtio/net.c index 519dcbb7..56dcfdb0 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -505,21 +505,23 @@ static u64 get_host_features(struct kvm *kvm, void *dev) features |= (1UL << VIRTIO_NET_F_HOST_UFO | 1UL << VIRTIO_NET_F_GUEST_UFO); + if (ndev->vhost_fd) { + u64 vhost_features; + + if (ioctl(ndev->vhost_fd, VHOST_GET_FEATURES, &vhost_features) != 0) + die_perror("VHOST_GET_FEATURES failed"); + + features &= vhost_features; + } + return features; } static int virtio_net__vhost_set_features(struct net_dev *ndev) { - u64 features = 1UL << VIRTIO_RING_F_EVENT_IDX; - u64 vhost_features; - - if (ioctl(ndev->vhost_fd, VHOST_GET_FEATURES, &vhost_features) != 0) - die_perror("VHOST_GET_FEATURES failed"); - - /* make sure both side support mergable rx buffers */ - if (vhost_features & 1UL << VIRTIO_NET_F_MRG_RXBUF && - has_virtio_feature(ndev, VIRTIO_NET_F_MRG_RXBUF)) - features |= 1UL << VIRTIO_NET_F_MRG_RXBUF; + /* VHOST_NET_F_VIRTIO_NET_HDR clashes with VIRTIO_F_ANY_LAYOUT! */ + u64 features = ndev->vdev.features & + ~(1UL << VHOST_NET_F_VIRTIO_NET_HDR); return ioctl(ndev->vhost_fd, VHOST_SET_FEATURES, &features); } From patchwork Wed Apr 19 13:21:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216814 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBFB3C6FD18 for ; Wed, 19 Apr 2023 13:28:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233465AbjDSN2G (ORCPT ); Wed, 19 Apr 2023 09:28:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233400AbjDSN16 (ORCPT ); Wed, 19 Apr 2023 09:27:58 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2446549D6 for ; Wed, 19 Apr 2023 06:27:51 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id d8-20020a05600c3ac800b003ee6e324b19so1438776wms.1 for ; Wed, 19 Apr 2023 06:27:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910869; x=1684502869; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=81uxyuscrQ/gZcha8TPra4ym0kBDH5BwVhM3rEYE4OE=; b=BmEK08wxnMyw5wJ7AXb8roz4nE0XWgAHlCBiI3EmhYmL4tC+lIN46PnjSqJuH4psX8 3K+NYmS79qtjR3rtTUOb6TR7XferYjrCjzeX5QUyqPIXwRKPvTXtuTfnLe9QXRKbUC9O OSmDgV0U2e29FKOQVUe7Da31pvNcBbOmiT5zxm5Yc+FfN6H1iGLKaKS1Gf1yA/y2ImxL 7cjcLx0f4xn/UysjRaEmHUaSE2bsW4PUUMguXMUb+OSIexfw+VVbKr+FewDWipRvImJd FZhnLSD5zPy9cC1fkY2DmogspuXDNvGayic8Xq+r5AC14rihNdSxyiMZt4vQPa+jrODZ w5kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910869; x=1684502869; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=81uxyuscrQ/gZcha8TPra4ym0kBDH5BwVhM3rEYE4OE=; b=TsparuUwfnSow5TYo8QXfXacV6atOZAzDxrUt/PG5AGrGbTX5WseSv/0mCZg2OzJfb uX3V03W8XJVY4fLBA1hoXD3jAbtZysTlYmZ96qplYvr7Iu9hwy1cZJJBHTG4SawykzXj TKCQiCc0DVMEmfwHcP1sBGnHyH0ZT4+iWTIU0bw7GsxfFfUivL3DpgTa8e3EkjrFhtf1 STX/uz8SFihv6g+HzOHTavXXOPocZVzEocgn3HNkdHqsDYjYCOA9a+u4encXmN/nRxJk CJsPD7R75sFEkmAinekZaadTY4dLKp+e/t4alRVZoHiXprlAo0+kDK3rjyYfnuRMu3iE klBA== X-Gm-Message-State: AAQBX9engND7Gd7viYjiXGDVZw57M0/zTTV4IL7hRiQRGNqMNMy337rc MWM++HRDHaMLqMy7Tm/E3fz5o4SwV8EVEl1Z4Fw= X-Google-Smtp-Source: AKy350YBYC3Ha1Ay0Ic7WVctSKxvpf84VjLWo5nH1f41gd0zO6gDQynARD+QU/bPCysdbo8xRtvMyg== X-Received: by 2002:a05:600c:b49:b0:3f1:7123:5f82 with SMTP id k9-20020a05600c0b4900b003f171235f82mr9813529wmr.12.1681910869602; Wed, 19 Apr 2023 06:27:49 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:49 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 12/16] virtio: Document how to test the devices Date: Wed, 19 Apr 2023 14:21:16 +0100 Message-Id: <20230419132119.124457-13-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a few instructions for testing the devices. Testing devices like vhost-scsi or vsock may seem daunting but is relatively easy. Signed-off-by: Jean-Philippe Brucker --- Documentation/io-testing.txt | 141 +++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 Documentation/io-testing.txt diff --git a/Documentation/io-testing.txt b/Documentation/io-testing.txt new file mode 100644 index 00000000..c2e41902 --- /dev/null +++ b/Documentation/io-testing.txt @@ -0,0 +1,141 @@ +This document describes how to test each device, which is required when +modifying the common I/O infrastructure. + + +9P +-- + + CONFIG_NET_9P_VIRTIO + +Without a --disk parameter, kvmtool shares part of the host filesystem +with the guest using 9p. Otherwise, use the `--9p ,` +parameter to share a directory with the guest, and mount it in the guest +with: + + $ mount -t 9p + + +BALLOON +------- + + CONFIG_VIRTIO_BALLOON + + $ lkvm run ... --balloon + +Display memory statistics: + + $ lkvm stat -a -m + *** Guest memory statistics *** + ... + +Remove 20MB of memory from the guest: + + $ lkvm balloon -n guest-$(pidof lkvm) -i 20 + + +BLOCK +----- + + CONFIG_VIRTIO_BLK + + $ lkvm run ... --disk + + +CONSOLE +------- + + $ lkvm run ... --console virtio + +See also virtio-console.txt + + +NET +--- + + CONFIG_VIRTIO_NET (guest) + CONFIG_VHOST_NET (host) + +By default kvmtool instantiates a user network device. In order to test +both tap and vhost, setup a tap interface on a local network. + +In the host: + + # ip tuntap add tap0 mode tap user $USER + # ip link set tap0 up + # ip link add br0 type bridge + # ip link set tap0 master br0 + # ip link set br0 up + # ip addr add 192.168.3.1/24 dev br0 + + $ lkvm run ... -n mode=tap,tapif=tap0,vhost=1 + +In the guest: + + # ip link set eth0 up + # ip addr add 192.168.3.12/24 dev eth0 + $ ping -c 1 192.168.3.1 + 64 bytes from 192.168.3.1: seq=0 ttl=64 time=0.303 ms + + +RNG +--- + + CONFIG_HW_RANDOM_VIRTIO + + $ lkvm run ... --rng + +In the guest: + + $ cat /sys/devices/virtual/misc/hw_random/rng_available + virtio_rng.0 + + +SCSI +---- + + CONFIG_SCSI_VIRTIO (guest) + CONFIG_TCM_FILEIO (host) + CONFIG_VHOST_SCSI (host) + +In the host, create a fileio backstore and a target: + + # targetcli (https://github.com/open-iscsi/targetcli-fb) + /> cd backstores/fileio + /backstores/fileio> create kvmtool_1 /srv/kvmtool_1 2M + Created fileio kvmtool_1 with size 2097152 + /backstores/fileio> cd /vhost + /vhost> create + Created target naa.500140571c9308aa. + Created TPG 1. + /vhost> cd naa.500140571c9308aa/tpg1/luns + /vhost/naa.50...8aa/tpg1/luns> create /backstores/fileio/kvmtool_1 + Created LUN 0. + + $ lkvm run ... --disk scsi:naa.500140571c9308aa + [ 0.479644] scsi host0: Virtio SCSI HBA + [ 0.483009] scsi 0:0:1:0: Direct-Access LIO-ORG kvmtool_1 4.0 PQ: 0 ANSI: 6 + + [ 1.242833] sd 0:0:1:0: [sda] 4096 512-byte logical blocks: (2.10 MB/2.00 MiB) + + +VSOCK +----- + + CONFIG_VSOCKETS + CONFIG_VIRTIO_VSOCKETS (guest) + CONFIG_VHOST_VSOCK (host) + +In the host, start a vsock server: + + $ socat - VSOCK-LISTEN:1234 + +We pick 12 as the guest ID. 0 and 1 are reserved, and the host has default +ID 2. + + $ lkvm run ... --vsock 12 + +In the guest, send a message to the host: + + $ echo Hello | socat - VSOCK-CONNECT:2:1234 + +The host server should display "Hello". From patchwork Wed Apr 19 13:21:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216815 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E536CC77B73 for ; Wed, 19 Apr 2023 13:28:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233159AbjDSN2I (ORCPT ); Wed, 19 Apr 2023 09:28:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233417AbjDSN16 (ORCPT ); Wed, 19 Apr 2023 09:27:58 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A7ED259C7 for ; Wed, 19 Apr 2023 06:27:51 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id v10so10482254wmn.5 for ; Wed, 19 Apr 2023 06:27:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910870; x=1684502870; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZW2jlw7dDgPAO2ayYcgdDHoNJv0ScBNbTXJD3AC7Ezg=; b=Rrw/uZoWEs6dsfgaHNPXeNWMrZS1w3jt2oONJvTJvnAjuVO4bBIkcw9D21CXjBkZbB Fue/8mHdQYVtSURsBJByLAe5b2+4G0eGS+zec35Oxxqr6mmKFZtViXbtpuNDfgnvqPz6 2Vy7jv+2Lu80yrP8CTb/+jXy10/BSKmCHoedQ7ols+ZTA8ergk9BDV2B9b099giCX3kw xa5OSsomtXwb0sAVFEUI8beM6mcuka9EvJeLqNPcCNvc13iQvzkqhuvd2DuVlECkDS3e 1GeRnbkka7K3xEjNEsWOH2NqpEAzeihGsQLfOTZ56s6elRiKhWo4IitISx69W/6Z31ep LLKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910870; x=1684502870; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZW2jlw7dDgPAO2ayYcgdDHoNJv0ScBNbTXJD3AC7Ezg=; b=RVxQ0ph9MwthfoL6RK3NCHlAIScjZCs4MFksfh23hxpgBhwb+MK4sA5YJPxcXMj51O PlG7hl42xFQiiV8cK9QGmO+xLeLN3pYppgzHC0QUyNbxE2tv0KG4reoQSVRnvhD79zXm cSVi1x/peHgdxJiJySTscVYUIdaJyy/xG/CKVQLj+Fzpk09iGu4uhp5TGg9EYoRrgXNf x/eGvtET2qD1vKm5pQ39Z0bgUpysxHxpon41OFSo0EMIT2OovM4eV14SCwMblwAD++ef uMefIa8v6p8Lg0/3q+Lo9k4hOKZv3mx+JPvIvdy2bPprvrWh7ug7n0FldjYdxWXFthg8 ZR/g== X-Gm-Message-State: AAQBX9e/mnmwwJs5omtie1nFlJbvQCqjzah9yErAhIxJC66DXrAltN4R 2e4GT+MsaqYIk6AbfmggSsmfSvKP1N4j1RSAKXQ= X-Google-Smtp-Source: AKy350Y640UUTTQeGJv20pwrR1EbSEShfXEil7vJJEZ9iev3KDkk84joqUfs2oGzxUVXRrZOj2b0Lw== X-Received: by 2002:a7b:c8d4:0:b0:3ef:6aa1:9284 with SMTP id f20-20020a7bc8d4000000b003ef6aa19284mr16847034wml.29.1681910870183; Wed, 19 Apr 2023 06:27:50 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:49 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 13/16] virtio: Fix messages about missing Linux config Date: Wed, 19 Apr 2023 14:21:17 +0100 Message-Id: <20230419132119.124457-14-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The suggested CONFIG options do not exist. Signed-off-by: Jean-Philippe Brucker --- virtio/scsi.c | 2 +- virtio/vsock.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/virtio/scsi.c b/virtio/scsi.c index 5f392814..02afee2a 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -218,7 +218,7 @@ static int virtio_scsi_init_one(struct kvm *kvm, struct disk_image *disk) virtio_scsi_vhost_init(kvm, sdev); if (compat_id == -1) - compat_id = virtio_compat_add_message("virtio-scsi", "CONFIG_VIRTIO_SCSI"); + compat_id = virtio_compat_add_message("virtio-scsi", "CONFIG_SCSI_VIRTIO"); return 0; } diff --git a/virtio/vsock.c b/virtio/vsock.c index 64512713..070cfbb6 100644 --- a/virtio/vsock.c +++ b/virtio/vsock.c @@ -218,7 +218,7 @@ static int virtio_vsock_init_one(struct kvm *kvm, u64 guest_cid) virtio_vhost_vsock_init(kvm, vdev); if (compat_id == -1) - compat_id = virtio_compat_add_message("virtio-vsock", "CONFIG_VIRTIO_VSOCK"); + compat_id = virtio_compat_add_message("virtio-vsock", "CONFIG_VIRTIO_VSOCKETS"); return 0; } From patchwork Wed Apr 19 13:21:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216818 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B924C6FD18 for ; Wed, 19 Apr 2023 13:28:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233427AbjDSN2M (ORCPT ); Wed, 19 Apr 2023 09:28:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233423AbjDSN17 (ORCPT ); Wed, 19 Apr 2023 09:27:59 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 626205242 for ; Wed, 19 Apr 2023 06:27:52 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id q5so17102570wmo.4 for ; Wed, 19 Apr 2023 06:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910871; x=1684502871; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vmoGW5X1O839Cwu8Q7/lXYJaS1OiSBNH/27QndO9Yqc=; b=iu8BNsFASwRmYCmLAkI8gpitRqLAGARQs33KvZi6k+ro3vRggzWzNh+be7jzJCHT0E 3L3WgfWK5988Pi2IToPtKG+CgeAayyj21Q8FwtGcdvRp1qH5d+5orYRS4Vo/0enMClbz JWAfLOyRyOvLNE+G4T+7HYBiThPYpZTuC+0YTdObiTkLgyswnGNGlrQCxUUIuJenVExX leXJcWI94wt1uYSKhSl152nrFU26yiSQ2VR0jU4m4eUrbIInS1wWtZF3iIo/qTboFNVu pa9VXmuVluwKXtJ8SPc6dlrOrOdQNJj5Ud+PDiM1BrHvSMwBTXnFsgGLXEY8iaS3eIiN PTKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910871; x=1684502871; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vmoGW5X1O839Cwu8Q7/lXYJaS1OiSBNH/27QndO9Yqc=; b=OGaYcZ135Rmoim5eyxkWf78KY2iC14e4b5+nzq7+QYFgMBDULjXqY6pdnHFDnWENoN ZeBx5YSDEEDr0PuI9De6bjh5tyyPtPV7qLHzRTkwzOt2252bUWCd63qQUkMd/we+NfwN 434vCxGaFiaKVgX8SD4DMWH7fY2EBheD+L7Ix417GI+eMXBqz7P44I5r+4HydRLy6xJU MRHyxhtM927wrZf+OEWf0AjYD7DfTzzA5djcQvHu03bMFCSfwTiSuDDmBB/34dcInei2 qAEtLg3aUCKxENlrPM8Oc9IWQEffCYKqI3IwZBn5Xp3mOshlYXvqIF1QqGy658GaQnag GyOQ== X-Gm-Message-State: AAQBX9cpv+nK9TRDIdwQwqMVfQpEceYYumD+zr0rCLiUZEnRvVRJdhjQ Y4H8ja/33a1Ube5u9wPTzQE/RxE+aFbCtYnjmFw= X-Google-Smtp-Source: AKy350Z3l/qgOAMab7FXe1T0+BciW8XQQ8M02hdD8sFDM0n8nEfflTYkDNNK5fYnNUAKSQJQz56lRQ== X-Received: by 2002:a05:600c:20b:b0:3f1:72dc:8bae with SMTP id 11-20020a05600c020b00b003f172dc8baemr9487957wmi.21.1681910870792; Wed, 19 Apr 2023 06:27:50 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:50 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 14/16] Factor epoll thread Date: Wed, 19 Apr 2023 14:21:18 +0100 Message-Id: <20230419132119.124457-15-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Both ioeventfd and ipc use an epoll thread roughly the same way. In order to add a new epoll user, factor the common bits into epoll.c Slight implementation changes which shouldn't affect behavior: * At the moment ioeventfd mixes file descriptor (for the stop event) and pointers in the epoll_event.data union, which could in theory cause aliasing. Use a pointer for the stop event instead. kvm-ipc uses only file descriptors. It could be changed but since epoll.c compares the stop event pointer first, the risk of aliasing with an fd is much lower there. * kvm-ipc uses EPOLLET, edge-triggered events, but having the stop event level-triggered shouldn't make a difference. Signed-off-by: Jean-Philippe Brucker --- Makefile | 1 + include/kvm/epoll.h | 17 ++++++++ epoll.c | 89 ++++++++++++++++++++++++++++++++++++++ ioeventfd.c | 94 ++++++---------------------------------- kvm-ipc.c | 103 +++++++++++++------------------------------- 5 files changed, 149 insertions(+), 155 deletions(-) create mode 100644 include/kvm/epoll.h create mode 100644 epoll.c diff --git a/Makefile b/Makefile index 86e19339..6b742369 100644 --- a/Makefile +++ b/Makefile @@ -80,6 +80,7 @@ OBJS += virtio/vhost.o OBJS += disk/blk.o OBJS += disk/qcow.o OBJS += disk/raw.o +OBJS += epoll.o OBJS += ioeventfd.o OBJS += net/uip/core.o OBJS += net/uip/arp.o diff --git a/include/kvm/epoll.h b/include/kvm/epoll.h new file mode 100644 index 00000000..dbb5a8d9 --- /dev/null +++ b/include/kvm/epoll.h @@ -0,0 +1,17 @@ +#include +#include "kvm/kvm.h" + +typedef void (*epoll__event_handler_t)(struct kvm *kvm, struct epoll_event *ev); + +struct kvm__epoll { + int fd; + int stop_fd; + struct kvm *kvm; + const char *name; + pthread_t thread; + epoll__event_handler_t handle_event; +}; + +int epoll__init(struct kvm *kvm, struct kvm__epoll *epoll, + const char *name, epoll__event_handler_t handle_event); +int epoll__exit(struct kvm__epoll *epoll); diff --git a/epoll.c b/epoll.c new file mode 100644 index 00000000..e0725a57 --- /dev/null +++ b/epoll.c @@ -0,0 +1,89 @@ +#include + +#include "kvm/epoll.h" + +#define EPOLLFD_MAX_EVENTS 20 + +static void *epoll__thread(void *param) +{ + u64 stop; + int nfds, i; + struct kvm__epoll *epoll = param; + struct kvm *kvm = epoll->kvm; + struct epoll_event events[EPOLLFD_MAX_EVENTS]; + + kvm__set_thread_name(epoll->name); + + for (;;) { + nfds = epoll_wait(epoll->fd, events, EPOLLFD_MAX_EVENTS, -1); + for (i = 0; i < nfds; i++) { + if (events[i].data.ptr == &epoll->stop_fd) + goto done; + + epoll->handle_event(kvm, &events[i]); + } + } +done: + read(epoll->stop_fd, &stop, sizeof(stop)); + write(epoll->stop_fd, &stop, sizeof(stop)); + return NULL; +} + +int epoll__init(struct kvm *kvm, struct kvm__epoll *epoll, + const char *name, epoll__event_handler_t handle_event) +{ + int r; + struct epoll_event stop_event = { + .events = EPOLLIN, + .data.ptr = &epoll->stop_fd, + }; + + epoll->kvm = kvm; + epoll->name = name; + epoll->handle_event = handle_event; + + epoll->fd = epoll_create(EPOLLFD_MAX_EVENTS); + if (epoll->fd < 0) + return -errno; + + epoll->stop_fd = eventfd(0, 0); + if (epoll->stop_fd < 0) { + r = -errno; + goto err_close_fd; + } + + r = epoll_ctl(epoll->fd, EPOLL_CTL_ADD, epoll->stop_fd, &stop_event); + if (r < 0) + goto err_close_all; + + r = pthread_create(&epoll->thread, NULL, epoll__thread, epoll); + if (r < 0) + goto err_close_all; + + return 0; + +err_close_all: + close(epoll->stop_fd); +err_close_fd: + close(epoll->fd); + + return r; +} + +int epoll__exit(struct kvm__epoll *epoll) +{ + int r; + u64 stop = 1; + + r = write(epoll->stop_fd, &stop, sizeof(stop)); + if (r < 0) + return r; + + r = read(epoll->stop_fd, &stop, sizeof(stop)); + if (r < 0) + return r; + + close(epoll->stop_fd); + close(epoll->fd); + return 0; +} diff --git a/ioeventfd.c b/ioeventfd.c index 3ae82672..a0fc2578 100644 --- a/ioeventfd.c +++ b/ioeventfd.c @@ -9,113 +9,45 @@ #include #include +#include "kvm/epoll.h" #include "kvm/ioeventfd.h" #include "kvm/kvm.h" #include "kvm/util.h" #define IOEVENTFD_MAX_EVENTS 20 -static struct epoll_event events[IOEVENTFD_MAX_EVENTS]; -static int epoll_fd, epoll_stop_fd; static LIST_HEAD(used_ioevents); static bool ioeventfd_avail; +static struct kvm__epoll epoll; -static void *ioeventfd__thread(void *param) +static void ioeventfd__handle_event(struct kvm *kvm, struct epoll_event *ev) { - u64 tmp = 1; + u64 tmp; + struct ioevent *ioevent = ev->data.ptr; - kvm__set_thread_name("ioeventfd-worker"); + if (read(ioevent->fd, &tmp, sizeof(tmp)) < 0) + die("Failed reading event"); - for (;;) { - int nfds, i; - - nfds = epoll_wait(epoll_fd, events, IOEVENTFD_MAX_EVENTS, -1); - for (i = 0; i < nfds; i++) { - struct ioevent *ioevent; - - if (events[i].data.fd == epoll_stop_fd) - goto done; - - ioevent = events[i].data.ptr; - - if (read(ioevent->fd, &tmp, sizeof(tmp)) < 0) - die("Failed reading event"); - - ioevent->fn(ioevent->fn_kvm, ioevent->fn_ptr); - } - } - -done: - tmp = write(epoll_stop_fd, &tmp, sizeof(tmp)); - - return NULL; -} - -static int ioeventfd__start(void) -{ - pthread_t thread; - - if (!ioeventfd_avail) - return -ENOSYS; - - return pthread_create(&thread, NULL, ioeventfd__thread, NULL); + ioevent->fn(ioevent->fn_kvm, ioevent->fn_ptr); } int ioeventfd__init(struct kvm *kvm) { - struct epoll_event epoll_event = {.events = EPOLLIN}; - int r; - ioeventfd_avail = kvm__supports_extension(kvm, KVM_CAP_IOEVENTFD); if (!ioeventfd_avail) return 1; /* Not fatal, but let caller determine no-go. */ - epoll_fd = epoll_create(IOEVENTFD_MAX_EVENTS); - if (epoll_fd < 0) - return -errno; - - epoll_stop_fd = eventfd(0, 0); - epoll_event.data.fd = epoll_stop_fd; - - r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, epoll_stop_fd, &epoll_event); - if (r < 0) - goto cleanup; - - r = ioeventfd__start(); - if (r < 0) - goto cleanup; - - r = 0; - - return r; - -cleanup: - close(epoll_stop_fd); - close(epoll_fd); - - return r; + return epoll__init(kvm, &epoll, "ioeventfd-worker", + ioeventfd__handle_event); } base_init(ioeventfd__init); int ioeventfd__exit(struct kvm *kvm) { - u64 tmp = 1; - int r; - if (!ioeventfd_avail) return 0; - r = write(epoll_stop_fd, &tmp, sizeof(tmp)); - if (r < 0) - return r; - - r = read(epoll_stop_fd, &tmp, sizeof(tmp)); - if (r < 0) - return r; - - close(epoll_fd); - close(epoll_stop_fd); - + epoll__exit(&epoll); return 0; } base_exit(ioeventfd__exit); @@ -165,7 +97,7 @@ int ioeventfd__add_event(struct ioevent *ioevent, int flags) .data.ptr = new_ioevent, }; - r = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, event, &epoll_event); + r = epoll_ctl(epoll.fd, EPOLL_CTL_ADD, event, &epoll_event); if (r) { r = -errno; goto cleanup; @@ -213,7 +145,7 @@ int ioeventfd__del_event(u64 addr, u64 datamatch) ioctl(ioevent->fn_kvm->vm_fd, KVM_IOEVENTFD, &kvm_ioevent); - epoll_ctl(epoll_fd, EPOLL_CTL_DEL, ioevent->fd, NULL); + epoll_ctl(epoll.fd, EPOLL_CTL_DEL, ioevent->fd, NULL); list_del(&ioevent->list); diff --git a/kvm-ipc.c b/kvm-ipc.c index 23f7b12e..265d80c5 100644 --- a/kvm-ipc.c +++ b/kvm-ipc.c @@ -2,9 +2,9 @@ #include #include #include -#include #include +#include "kvm/epoll.h" #include "kvm/kvm-ipc.h" #include "kvm/rwsem.h" #include "kvm/read-write.h" @@ -28,8 +28,8 @@ struct kvm_ipc_head { extern __thread struct kvm_cpu *current_kvm_cpu; static void (*msgs[KVM_IPC_MAX_MSGS])(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg); static DECLARE_RWSEM(msgs_rwlock); -static int epoll_fd, server_fd, stop_fd; -static pthread_t thread; +static int server_fd; +static struct kvm__epoll epoll; static int kvm__create_socket(struct kvm *kvm) { @@ -268,7 +268,7 @@ static int kvm_ipc__new_conn(int fd) ev.events = EPOLLIN | EPOLLRDHUP; ev.data.fd = client; - if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client, &ev) < 0) { + if (epoll_ctl(epoll.fd, EPOLL_CTL_ADD, client, &ev) < 0) { close(client); return -1; } @@ -278,7 +278,7 @@ static int kvm_ipc__new_conn(int fd) static void kvm_ipc__close_conn(int fd) { - epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, NULL); + epoll_ctl(epoll.fd, EPOLL_CTL_DEL, fd, NULL); close(fd); } @@ -309,42 +309,26 @@ done: return -1; } -static void *kvm_ipc__thread(void *param) +static void kvm_ipc__handle_event(struct kvm *kvm, struct epoll_event *ev) { - struct epoll_event event; - struct kvm *kvm = param; + int fd = ev->data.fd; - kvm__set_thread_name("kvm-ipc"); + if (fd == server_fd) { + int client, r; - for (;;) { - int nfds; - - nfds = epoll_wait(epoll_fd, &event, 1, -1); - if (nfds > 0) { - int fd = event.data.fd; + client = kvm_ipc__new_conn(fd); + /* + * Handle multiple IPC cmd at a time + */ + do { + r = kvm_ipc__receive(kvm, client); + } while (r == 0); - if (fd == stop_fd && event.events & EPOLLIN) { - break; - } else if (fd == server_fd) { - int client, r; - - client = kvm_ipc__new_conn(fd); - /* - * Handle multiple IPC cmd at a time - */ - do { - r = kvm_ipc__receive(kvm, client); - } while (r == 0); - - } else if (event.events & (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) { - kvm_ipc__close_conn(fd); - } else { - kvm_ipc__receive(kvm, fd); - } - } + } else if (ev->events & (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) { + kvm_ipc__close_conn(fd); + } else { + kvm_ipc__receive(kvm, fd); } - - return NULL; } static void kvm__pid(struct kvm *kvm, int fd, u32 type, u32 len, u8 *msg) @@ -482,42 +466,21 @@ int kvm_ipc__init(struct kvm *kvm) server_fd = sock; - epoll_fd = epoll_create(KVM_IPC_MAX_MSGS); - if (epoll_fd < 0) { - perror("epoll_create"); - ret = epoll_fd; + ret = epoll__init(kvm, &epoll, "kvm-ipc", + kvm_ipc__handle_event); + if (ret) { + pr_err("Failed starting IPC thread"); goto err; } ev.events = EPOLLIN | EPOLLET; ev.data.fd = sock; - if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) < 0) { + if (epoll_ctl(epoll.fd, EPOLL_CTL_ADD, sock, &ev) < 0) { pr_err("Failed adding socket to epoll"); ret = -EFAULT; goto err_epoll; } - stop_fd = eventfd(0, 0); - if (stop_fd < 0) { - perror("eventfd"); - ret = stop_fd; - goto err_epoll; - } - - ev.events = EPOLLIN | EPOLLET; - ev.data.fd = stop_fd; - if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, stop_fd, &ev) < 0) { - pr_err("Failed adding stop event to epoll"); - ret = -EFAULT; - goto err_stop; - } - - if (pthread_create(&thread, NULL, kvm_ipc__thread, kvm) != 0) { - pr_err("Failed starting IPC thread"); - ret = -EFAULT; - goto err_stop; - } - kvm_ipc__register_handler(KVM_IPC_PID, kvm__pid); kvm_ipc__register_handler(KVM_IPC_DEBUG, handle_debug); kvm_ipc__register_handler(KVM_IPC_PAUSE, handle_pause); @@ -528,10 +491,9 @@ int kvm_ipc__init(struct kvm *kvm) return 0; -err_stop: - close(stop_fd); err_epoll: - close(epoll_fd); + epoll__exit(&epoll); + close(server_fd); err: return ret; } @@ -539,18 +501,11 @@ base_init(kvm_ipc__init); int kvm_ipc__exit(struct kvm *kvm) { - u64 val = 1; - int ret; - - ret = write(stop_fd, &val, sizeof(val)); - if (ret < 0) - return ret; - + epoll__exit(&epoll); close(server_fd); - close(epoll_fd); kvm__remove_socket(kvm->cfg.guest_name); - return ret; + return 0; } base_exit(kvm_ipc__exit); From patchwork Wed Apr 19 13:21:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216817 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87674C77B73 for ; Wed, 19 Apr 2023 13:28:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233470AbjDSN2L (ORCPT ); Wed, 19 Apr 2023 09:28:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233427AbjDSN17 (ORCPT ); Wed, 19 Apr 2023 09:27:59 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC2D3118FA for ; Wed, 19 Apr 2023 06:27:52 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id r15so7559027wmo.1 for ; Wed, 19 Apr 2023 06:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910871; x=1684502871; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+Ul6nlKXm3ANtzcLKZeMo4cAjbCzXANhKz5rZh2m+ZQ=; b=N62vmmjaWYEgd6+Ca7GeNKRCDccrrRr3UTUqFWSvB/sgClUg4HZQw8xL9+tOocTO51 drEU/WceHlbHuQCcj4+5+YftlIq65qr1zsqyXJcxeSirDUa/WyZXrvNMgUp8+NwwW6lO RNUacN8SZg20nw4oU8VN5PDWs6dJRh8682nCiW9mu4dK5fOWZH+jW/S0dd2nqfg0rAsm IZfLOec1tt6kG/+XaJl2ceN52JRe7Df2hEKUXrmHy81mzkdR+R5BYc/yXN2O9OpHURsu XE5zlTToEsg0pXIfhGyEeUoHt5ijNWfyCTPNZqMH15rWWoMb4dH3bAEyoKYc70UbA+8x 8jvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910871; x=1684502871; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+Ul6nlKXm3ANtzcLKZeMo4cAjbCzXANhKz5rZh2m+ZQ=; b=eFiwbiWo1hTkerQ01vGiYAq2vb4Yeot7lWMXBtfpkjJsZApxOYrJ3R/OBknVXBwdzC lDoZnsJIFF5QAJX54uL4Z5KqxAS2e7ozxOx9XFlymKdU8tEcikkANHkHNzQXlclr0Nbu GAKvgygYHwoNewBcoNgB/sTEyA9c7WuKITzjafToYvNoS/xEIs1ykGx7DYWAdScofG8r kkqXsu7FoUE/1AHJtcY4eApXkr4lqR6EhiqZpagxj5LwmjB/A/lgnJOf4HIey9Dg822T hDv+naBKk3sf1d/5rPUoeLsaHMJ7mbN7w0zMh3WcoZblzt2/GwgzUQ6XwuKPFbGCfduO XCmg== X-Gm-Message-State: AAQBX9dwuph5on/m5bwFph2eI28HJ/FCdgur3XQTXjzAH7Dt28bWTe6z H7xN7GjTAKqxjem2kjRdnBn3Bhl5iuJol+yuP+g= X-Google-Smtp-Source: AKy350alkcrnhkUhfGnjbqJPl6ndrTvfvrxepSMuq7YGBS8Z0COGdbqa9VJ725gLBwg21V/WFgywYQ== X-Received: by 2002:a1c:4c15:0:b0:3f1:7bac:d408 with SMTP id z21-20020a1c4c15000000b003f17bacd408mr4009790wmf.26.1681910871363; Wed, 19 Apr 2023 06:27:51 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:51 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 15/16] virtio/vhost: Support line interrupt signaling Date: Wed, 19 Apr 2023 14:21:19 +0100 Message-Id: <20230419132119.124457-16-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org To signal a virtqueue, a kernel vhost worker writes an eventfd registered by kvmtool with VHOST_SET_VRING_CALL. When MSIs are supported, this eventfd is connected directly to KVM IRQFD to inject the interrupt into the guest. However direct injection does not work when MSIs are not supported. The virtio-mmio transport does not support MSIs at all, and even with virtio-pci, the guest may use INTx if the irqchip does not support MSIs (e.g. irqchip=gicv3 on arm64). In this case, injecting the interrupt requires writing an ISR register in virtio to signal that it is a virtqueue notification rather than a config change. Add a thread that polls the vhost eventfd for interrupts, and notifies the guest. When the guest configures MSIs, disable polling on the eventfd and enable direct injection. Signed-off-by: Jean-Philippe Brucker --- include/kvm/virtio.h | 6 ++- virtio/core.c | 1 + virtio/net.c | 3 +- virtio/scsi.c | 3 +- virtio/vhost.c | 110 +++++++++++++++++++++++++++++++++++++------ virtio/vsock.c | 3 +- 6 files changed, 104 insertions(+), 22 deletions(-) diff --git a/include/kvm/virtio.h b/include/kvm/virtio.h index 96c7b3ea..738efcd1 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -77,10 +77,12 @@ struct virt_queue { u16 endian; bool use_event_idx; bool enabled; + struct virtio_device *vdev; /* vhost IRQ handling */ int gsi; int irqfd; + int index; }; /* @@ -256,8 +258,8 @@ void virtio_vhost_set_vring(struct kvm *kvm, int vhost_fd, u32 index, struct virt_queue *queue); void virtio_vhost_set_vring_kick(struct kvm *kvm, int vhost_fd, u32 index, int event_fd); -void virtio_vhost_set_vring_call(struct kvm *kvm, int vhost_fd, u32 index, - u32 gsi, struct virt_queue *queue); +void virtio_vhost_set_vring_irqfd(struct kvm *kvm, u32 gsi, + struct virt_queue *queue); void virtio_vhost_reset_vring(struct kvm *kvm, int vhost_fd, u32 index, struct virt_queue *queue); diff --git a/virtio/core.c b/virtio/core.c index 568243ab..b31ea46b 100644 --- a/virtio/core.c +++ b/virtio/core.c @@ -197,6 +197,7 @@ void virtio_init_device_vq(struct kvm *kvm, struct virtio_device *vdev, vq->endian = vdev->endian; vq->use_event_idx = (vdev->features & (1UL << VIRTIO_RING_F_EVENT_IDX)); vq->enabled = true; + vq->vdev = vdev; if (addr->legacy) { unsigned long base = (u64)addr->pfn * addr->pgsize; diff --git a/virtio/net.c b/virtio/net.c index 56dcfdb0..9568b055 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -674,8 +674,7 @@ static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) if (ndev->vhost_fd == 0) return; - virtio_vhost_set_vring_call(kvm, ndev->vhost_fd, vq, gsi, - &queue->vq); + virtio_vhost_set_vring_irqfd(kvm, gsi, &queue->vq); } static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) diff --git a/virtio/scsi.c b/virtio/scsi.c index 02afee2a..60b5bc36 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -121,8 +121,7 @@ static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) if (sdev->vhost_fd == 0) return; - virtio_vhost_set_vring_call(kvm, sdev->vhost_fd, vq, gsi, - &sdev->vqs[vq]); + virtio_vhost_set_vring_irqfd(kvm, gsi, &sdev->vqs[vq]); } static void notify_vq_eventfd(struct kvm *kvm, void *dev, u32 vq, u32 efd) diff --git a/virtio/vhost.c b/virtio/vhost.c index cd83645c..0049003b 100644 --- a/virtio/vhost.c +++ b/virtio/vhost.c @@ -1,5 +1,6 @@ #include "kvm/irq.h" #include "kvm/virtio.h" +#include "kvm/epoll.h" #include #include @@ -7,12 +8,52 @@ #include +static struct kvm__epoll epoll; + +static void virtio_vhost_signal_vq(struct kvm *kvm, struct epoll_event *ev) +{ + int r; + u64 tmp; + struct virt_queue *queue = ev->data.ptr; + + if (read(queue->irqfd, &tmp, sizeof(tmp)) < 0) + pr_warning("%s: failed to read eventfd", __func__); + + r = queue->vdev->ops->signal_vq(kvm, queue->vdev, queue->index); + if (r) + pr_warning("%s failed to signal virtqueue", __func__); +} + +static int virtio_vhost_start_poll(struct kvm *kvm) +{ + if (epoll.fd) + return 0; + + if (epoll__init(kvm, &epoll, "vhost-irq-worker", + virtio_vhost_signal_vq)) + return -1; + + return 0; +} + +static int virtio_vhost_stop_poll(struct kvm *kvm) +{ + if (epoll.fd) + epoll__exit(&epoll); + return 0; +} +base_exit(virtio_vhost_stop_poll); + void virtio_vhost_init(struct kvm *kvm, int vhost_fd) { struct kvm_mem_bank *bank; struct vhost_memory *mem; int i = 0, r; + r = virtio_vhost_start_poll(kvm); + if (r) + die("Unable to start vhost polling thread\n"); + mem = calloc(1, sizeof(*mem) + kvm->mem_slots * sizeof(struct vhost_memory_region)); if (mem == NULL) @@ -39,6 +80,16 @@ void virtio_vhost_init(struct kvm *kvm, int vhost_fd) free(mem); } +static int virtio_vhost_get_irqfd(struct virt_queue *queue) +{ + if (!queue->irqfd) { + queue->irqfd = eventfd(0, 0); + if (queue->irqfd < 0) + die_perror("eventfd()"); + } + return queue->irqfd; +} + void virtio_vhost_set_vring(struct kvm *kvm, int vhost_fd, u32 index, struct virt_queue *queue) { @@ -50,6 +101,16 @@ void virtio_vhost_set_vring(struct kvm *kvm, int vhost_fd, u32 index, .used_user_addr = (u64)(unsigned long)queue->vring.used, }; struct vhost_vring_state state = { .index = index }; + struct vhost_vring_file file = { + .index = index, + .fd = virtio_vhost_get_irqfd(queue), + }; + struct epoll_event event = { + .events = EPOLLIN, + .data.ptr = queue, + }; + + queue->index = index; if (queue->endian != VIRTIO_ENDIAN_HOST) die("VHOST requires the same endianness in guest and host"); @@ -67,6 +128,14 @@ void virtio_vhost_set_vring(struct kvm *kvm, int vhost_fd, u32 index, r = ioctl(vhost_fd, VHOST_SET_VRING_ADDR, &addr); if (r < 0) die_perror("VHOST_SET_VRING_ADDR failed"); + + r = ioctl(vhost_fd, VHOST_SET_VRING_CALL, &file); + if (r < 0) + die_perror("VHOST_SET_VRING_CALL failed"); + + r = epoll_ctl(epoll.fd, EPOLL_CTL_ADD, file.fd, &event); + if (r < 0) + die_perror("EPOLL_CTL_ADD vhost call fd"); } void virtio_vhost_set_vring_kick(struct kvm *kvm, int vhost_fd, @@ -83,24 +152,23 @@ void virtio_vhost_set_vring_kick(struct kvm *kvm, int vhost_fd, die_perror("VHOST_SET_VRING_KICK failed"); } -void virtio_vhost_set_vring_call(struct kvm *kvm, int vhost_fd, u32 index, - u32 gsi, struct virt_queue *queue) +void virtio_vhost_set_vring_irqfd(struct kvm *kvm, u32 gsi, + struct virt_queue *queue) { int r; - struct vhost_vring_file file = { - .index = index, - .fd = eventfd(0, 0), - }; + int fd = virtio_vhost_get_irqfd(queue); - r = irq__add_irqfd(kvm, gsi, file.fd, -1); - if (r < 0) - die_perror("KVM_IRQFD failed"); + if (queue->gsi) + irq__del_irqfd(kvm, queue->gsi, fd); + else + /* Disconnect user polling thread */ + epoll_ctl(epoll.fd, EPOLL_CTL_DEL, fd, NULL); - r = ioctl(vhost_fd, VHOST_SET_VRING_CALL, &file); + /* Connect the direct IRQFD route */ + r = irq__add_irqfd(kvm, gsi, fd, -1); if (r < 0) - die_perror("VHOST_SET_VRING_CALL failed"); + die_perror("KVM_IRQFD failed"); - queue->irqfd = file.fd; queue->gsi = gsi; } @@ -108,9 +176,23 @@ void virtio_vhost_reset_vring(struct kvm *kvm, int vhost_fd, u32 index, struct virt_queue *queue) { + struct vhost_vring_file file = { + .index = index, + .fd = -1, + }; + + if (!queue->irqfd) + return; + if (queue->gsi) { irq__del_irqfd(kvm, queue->gsi, queue->irqfd); - close(queue->irqfd); - queue->gsi = queue->irqfd = 0; + queue->gsi = 0; } + + epoll_ctl(epoll.fd, EPOLL_CTL_DEL, queue->irqfd, NULL); + + if (ioctl(vhost_fd, VHOST_SET_VRING_CALL, &file)) + perror("SET_VRING_CALL"); + close(queue->irqfd); + queue->irqfd = 0; } diff --git a/virtio/vsock.c b/virtio/vsock.c index 070cfbb6..5d22bd24 100644 --- a/virtio/vsock.c +++ b/virtio/vsock.c @@ -151,8 +151,7 @@ static void notify_vq_gsi(struct kvm *kvm, void *dev, u32 vq, u32 gsi) if (vdev->vhost_fd == -1 || is_event_vq(vq)) return; - virtio_vhost_set_vring_call(kvm, vdev->vhost_fd, vq, gsi, - &vdev->vqs[vq]); + virtio_vhost_set_vring_irqfd(kvm, gsi, &vdev->vqs[vq]); } static unsigned int get_vq_count(struct kvm *kvm, void *dev) From patchwork Wed Apr 19 13:21:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 13216816 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39BA2C77B75 for ; Wed, 19 Apr 2023 13:28:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233414AbjDSN2J (ORCPT ); Wed, 19 Apr 2023 09:28:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233395AbjDSN17 (ORCPT ); Wed, 19 Apr 2023 09:27:59 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8917F4EEC for ; Wed, 19 Apr 2023 06:27:53 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id r15so7559036wmo.1 for ; Wed, 19 Apr 2023 06:27:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681910872; x=1684502872; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GAzpvDGwBUf7AnIRiZR96oNsqPjKS9udD96ExLeWH18=; b=pHeQ4HSMM4WAl00aXKa1tDhTXIqToR4vfF89/rBXeGkA31U025Gm/zhHyaQM4mdCKX WanZqy5C3NVhfHcSzG2Xs+6mbI7uSpzxppxb/aLig8oGCGMmDVH3G5WUhtrOoeKHDfM4 bkuozmYgzGGomu1bgeVVu5CflN8GNIeX057U7kMT3hn55RhtiT5vihIvzkSDeMBMkC6D yIK3xC+g6fGXWK0oqktMxUZlhdSmOeOMePpzjwgIYeghYn9PvEM8OcdbadrQNe0Lr4G6 NOCK1+yCu/R6vWRf3xB03FvgugTO/UVRMDsUPZ3vaIivO5pwbzWsehNXWiKYEwyFodor Qebw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681910872; x=1684502872; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GAzpvDGwBUf7AnIRiZR96oNsqPjKS9udD96ExLeWH18=; b=cf+/QM9MAyr4PXe55kvJlEWThRCYSsKd553yWu7dYG44VH9+myPq/k37OdNrwqX7Db MtLFCb5dLd9z9lrHv1g3K4lCknBFyJHI+FwzsXZ3B5M3x2KM8wtkzehLyza0NZaJmDz9 tU5d8lAvZz5k8sT7I+TcqwRSHYCFrcam3AKpUIuIWbtzMbycdphC4kL0juRFO5ec+INk xrUcudSFiRUVfmDRFyTeqSHPg0CRyfoNfZOku0eHIM8FCaX4xuhc3gnsPAhErjAgJLVo ZEZUsrqVcxPkSvarGHBSsOkvYnHsPBiC18VGKdI6S6ig8SKKNvGkFApStwuCWFewZl9s TVZg== X-Gm-Message-State: AAQBX9cabXd9HDiiXJqOSj0zl5qe60WP29Nc6xFyBI1wZXe6IdVqoEWt ot6++F6XdUGD0fK0ANXvWBZtxkYS//zFuA73b/U= X-Google-Smtp-Source: AKy350YCBqMAxrQXhEuFUr3vZSirNOurg42CE5n97uFoG+TSFke4IcXbMtOmgmgLk/PsiKVC3ciZdw== X-Received: by 2002:a1c:740b:0:b0:3ed:276d:81a4 with SMTP id p11-20020a1c740b000000b003ed276d81a4mr17103168wmc.32.1681910871870; Wed, 19 Apr 2023 06:27:51 -0700 (PDT) Received: from localhost.localdomain (054592b0.skybroadband.com. [5.69.146.176]) by smtp.gmail.com with ESMTPSA id p8-20020a05600c358800b003f1738d0d13sm3497017wmq.1.2023.04.19.06.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 06:27:51 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: suzuki.poulose@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool 16/16] virtio/vhost: Clear VIRTIO_F_ACCESS_PLATFORM Date: Wed, 19 Apr 2023 14:21:20 +0100 Message-Id: <20230419132119.124457-17-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230419132119.124457-1-jean-philippe@linaro.org> References: <20230419132119.124457-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Vhost interprets the VIRTIO_F_ACCESS_PLATFORM flag as if accesses need to use vhost-iotlb, and since kvmtool does not implement vhost-iotlb, vhost will fail to access the virtqueue. This fix is preventive. Kvmtool does not set VIRTIO_F_ACCESS_PLATFORM at the moment but the Arm CCA and pKVM changes will likely hit the issue (as experienced with the CCA development tree), so we might as well fix it now. Signed-off-by: Jean-Philippe Brucker --- include/kvm/virtio.h | 1 + virtio/net.c | 14 ++++---------- virtio/scsi.c | 3 +-- virtio/vhost.c | 11 +++++++++++ virtio/vsock.c | 4 ++-- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/kvm/virtio.h b/include/kvm/virtio.h index 738efcd1..c6938e75 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -262,6 +262,7 @@ void virtio_vhost_set_vring_irqfd(struct kvm *kvm, u32 gsi, struct virt_queue *queue); void virtio_vhost_reset_vring(struct kvm *kvm, int vhost_fd, u32 index, struct virt_queue *queue); +int virtio_vhost_set_features(int vhost_fd, u64 features); int virtio_transport_parser(const struct option *opt, const char *arg, int unset); diff --git a/virtio/net.c b/virtio/net.c index 9568b055..8be90159 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -517,23 +517,17 @@ static u64 get_host_features(struct kvm *kvm, void *dev) return features; } -static int virtio_net__vhost_set_features(struct net_dev *ndev) +static void virtio_net_start(struct net_dev *ndev) { /* VHOST_NET_F_VIRTIO_NET_HDR clashes with VIRTIO_F_ANY_LAYOUT! */ - u64 features = ndev->vdev.features & - ~(1UL << VHOST_NET_F_VIRTIO_NET_HDR); - - return ioctl(ndev->vhost_fd, VHOST_SET_FEATURES, &features); -} + u64 features = ndev->vdev.features & ~(1UL << VHOST_NET_F_VIRTIO_NET_HDR); -static void virtio_net_start(struct net_dev *ndev) -{ if (ndev->mode == NET_MODE_TAP) { if (!virtio_net__tap_init(ndev)) die_perror("TAP device initialized failed because"); - if (ndev->vhost_fd && - virtio_net__vhost_set_features(ndev) != 0) + if (ndev->vhost_fd && virtio_vhost_set_features(ndev->vhost_fd, + features)) die_perror("VHOST_SET_FEATURES failed"); } else { ndev->info.vnet_hdr_len = virtio_net_hdr_len(ndev); diff --git a/virtio/scsi.c b/virtio/scsi.c index 60b5bc36..88e6fe5d 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -68,8 +68,7 @@ static void notify_status(struct kvm *kvm, void *dev, u32 status) struct virtio_scsi_config *conf = &sdev->config; if (status & VIRTIO__STATUS_START) { - r = ioctl(sdev->vhost_fd, VHOST_SET_FEATURES, - &sdev->vdev.features); + r = virtio_vhost_set_features(sdev->vhost_fd, sdev->vdev.features); if (r != 0) die_perror("VHOST_SET_FEATURES failed"); diff --git a/virtio/vhost.c b/virtio/vhost.c index 0049003b..287e48dc 100644 --- a/virtio/vhost.c +++ b/virtio/vhost.c @@ -196,3 +196,14 @@ void virtio_vhost_reset_vring(struct kvm *kvm, int vhost_fd, u32 index, close(queue->irqfd); queue->irqfd = 0; } + +int virtio_vhost_set_features(int vhost_fd, u64 features) +{ + /* + * vhost takes VIRTIO_F_ACCESS_PLATFORM as meaning there is an iotlb. + * This is not the case for kvmtool, so mask it always. + */ + u64 masked_feat = features & ~(1ULL << VIRTIO_F_ACCESS_PLATFORM); + + return ioctl(vhost_fd, VHOST_SET_FEATURES, &masked_feat); +} diff --git a/virtio/vsock.c b/virtio/vsock.c index 5d22bd24..7d4053a1 100644 --- a/virtio/vsock.c +++ b/virtio/vsock.c @@ -107,8 +107,8 @@ static void notify_status(struct kvm *kvm, void *dev, u32 status) if (status & VIRTIO__STATUS_START) { start = 1; - r = ioctl(vdev->vhost_fd, VHOST_SET_FEATURES, - &vdev->vdev.features); + r = virtio_vhost_set_features(vdev->vhost_fd, + vdev->vdev.features); if (r != 0) die_perror("VHOST_SET_FEATURES failed"); } else if (status & VIRTIO__STATUS_STOP) {