From patchwork Tue Jun 6 13:04: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: 13269158 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 4C886C77B73 for ; Tue, 6 Jun 2023 13:05:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237953AbjFFNFe (ORCPT ); Tue, 6 Jun 2023 09:05:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238042AbjFFNFQ (ORCPT ); Tue, 6 Jun 2023 09:05:16 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60BDA118 for ; Tue, 6 Jun 2023 06:05:14 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-30af56f5f52so5243091f8f.1 for ; Tue, 06 Jun 2023 06:05:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056713; x=1688648713; 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=h/ixVhafCrLnwDcDpSyg5o/+ZmCnuYZQQzXZfM2c9ig=; b=Q3ykR9M1bjftPS/DYKcMsqfRnbYmL4jNnUTQHHVcMxg6Sz+EljZRiifVkoNTf+TzUy pOQ1YjlKOqOuGKHjYwsODK+kaISOSGulmKWRwENUTMUvG9hGSWngNn++9Q5E9wCNhP7J GNu4YS0g2ugDW27TBzWOa3FEMlOH87lakB6SuJcP0rLycymjDC/2W9CbAQC9jgXm/qrH SGjWXM9J+XZKMYV/MyOhvFIrIweLj9kJsRWmRzzhLs3smWePKMdneoFOvYMFV0fRs8RB yPnPMFZkp0fopKgWZi33XgcRoncWI3nHaBFokKma1CBPldjKduIVWWWnfVS2qzRoqLeL z+Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056713; x=1688648713; 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=h/ixVhafCrLnwDcDpSyg5o/+ZmCnuYZQQzXZfM2c9ig=; b=i/55WD7jTAuk/OKMtxkxw27VgfY8iQI26z44DGl5UtJ1fOWjEAAZRVUV++GF0j8GE7 FWi06yI4kRHajIH9LCO3gPupDRTBXdscE3YehMOnWnbNL4oo7s2dYjJTtUru4hlXBN/e MZjvJuvx+AoeF7/48vFkb2W9HYKIzxhwRWBlHZxXi78L0zohphSZpiJYr77OTGy0XK0B zclEWGVSwxWrLWf0ASCi11+CS6LVqTwwa+2veZBa4orcnFj5yLQWDHLeXUSP4YRM0PsW /ad6/41UzSj7/GzEpuwBfFsj0HcX5DKrGsQMNSVaT8uu/9vqQLGM6mAsrwgwW/f18yul V+fg== X-Gm-Message-State: AC+VfDzuOgVVMZCtydU+G4/hv/w86oKUa+vzEBbPnLN0jhNS5MPEnttS 9irTqOeeF9nja92bjD8bKTsCieR7FllpBBmbCZcMyQ== X-Google-Smtp-Source: ACHHUZ6bw6WWzl/ocKpYiCMiuwJHztUfvRmJiLzDzffEL3c6MxYSBfVkaR7cbKUycyBj6sEClKor4g== X-Received: by 2002:a05:6000:1209:b0:2dd:11b8:8aa9 with SMTP id e9-20020a056000120900b002dd11b88aa9mr1700889wrx.15.1686056712874; Tue, 06 Jun 2023 06:05:12 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:12 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 01/17] virtio: Factor vhost initialization Date: Tue, 6 Jun 2023 14:04:10 +0100 Message-Id: <20230606130426.978945-2-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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). Reviewed-by: Andre Przywara Signed-off-by: Jean-Philippe Brucker --- 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 a8bbaf21..4cc2e3d2 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -258,6 +258,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 bc20ce09..65fdbd17 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 9af8a65c..621a8334 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -201,7 +201,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; @@ -209,20 +208,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) @@ -231,13 +217,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 Tue Jun 6 13:04: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: 13269157 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 2B8EBC77B73 for ; Tue, 6 Jun 2023 13:05:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237976AbjFFNFc (ORCPT ); Tue, 6 Jun 2023 09:05:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238045AbjFFNFQ (ORCPT ); Tue, 6 Jun 2023 09:05:16 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF89012F for ; Tue, 6 Jun 2023 06:05:14 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-30aeee7c8a0so4400954f8f.1 for ; Tue, 06 Jun 2023 06:05:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056713; x=1688648713; 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=I5RAi9Tjtg+0tkfH2bckb9u+xg+1YndWuvZYGgLAmRU=; b=pDvZfjHqEAKLC8nx+Mv5Ui4fiK6UuQtOnUzRlghm8ggbA1Qt2S61QtBp61+sQg+gZO 4l5+XHu7pwrK+G7JiLpGiITAEzVlbbL7VqHKqB1JkgsEp1t6MrSExaFZh4I24qKP1398 YETVEvBVRfZXHVStqYxozbaoi0kXwgmGI1WKryQXgbSnXN3Nb8HnWdq3XIAFuVABpjhe FKaSNPs2qd3zK/djEAvu9LYoTgxuD8TZpScI0Cn3Gn5S0LVF7s6oYo5nqzAMSTOfaumL LXWb83tk7QV0v+kZP7XVjIAQwxrDiS10Q9Jnl4Cx+/Ra7BsbxJ6/Dik4e95vYcjC6862 XPlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056713; x=1688648713; 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=I5RAi9Tjtg+0tkfH2bckb9u+xg+1YndWuvZYGgLAmRU=; b=l82DnrL1hkEUxuCdWiRQlyd2iLfUdIUtFrSBG0gcmoERf8EcyCLiVypf3DvSP3Se2N KUcGBSRMhYHK6Qis4np5uid6Depjk31+HVbc8dlT3VUxZ11KHUVFb1f/VoWYfWZS1r9Y S9up0WO47U3RNuQdLQsY6eHi3Akrjmh6DJWRCU/cEnFTjtFiHJZboTZ0Ozc3Gli57q9V M3TzilwJ0/m8JCUwz63GoXht/H4iWG6WplZrSDCsdInuvqCqLrsQvBLIqE+uKaXB7lhH yL6a3P0FyLrpxVmQNLEYN6L32spGrkfX5ellTu50qkORI//cgGVkhw7sE+sH8USnx0mq 8GHA== X-Gm-Message-State: AC+VfDyFj5Om+3UOAe7D1d1nPj1Ivsv/6rSUIr8ZG0C04XgNDBUzedCG kt9nVFoDBM5S9lJ4U94TLECpGtKYykuit6+MY9JhIA== X-Google-Smtp-Source: ACHHUZ7mn64NhP17ugCoFl9Kv0DRiLKXJdNVeVj4qhRwNaTn3fQg+aQIXqGU1vRE44XunOFYeWKUAQ== X-Received: by 2002:a05:6000:1052:b0:2f0:583:44be with SMTP id c18-20020a056000105200b002f0058344bemr2064652wrx.0.1686056713359; Tue, 06 Jun 2023 06:05:13 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:13 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 02/17] virtio/vhost: Factor vring operation Date: Tue, 6 Jun 2023 14:04:11 +0100 Message-Id: <20230606130426.978945-3-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 Reviewed-by: Andre Przywara Signed-off-by: Jean-Philippe Brucker --- 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 4cc2e3d2..daea8554 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -259,6 +259,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 65fdbd17..b7c64a08 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 621a8334..6aa0909f 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -72,11 +72,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); @@ -87,26 +84,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 Tue Jun 6 13:04: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: 13269154 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 3AB5AC77B7A for ; Tue, 6 Jun 2023 13:05:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237958AbjFFNF0 (ORCPT ); Tue, 6 Jun 2023 09:05:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238046AbjFFNFQ (ORCPT ); Tue, 6 Jun 2023 09:05:16 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 489C8E78 for ; Tue, 6 Jun 2023 06:05:15 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-30c2bd52f82so6200285f8f.3 for ; Tue, 06 Jun 2023 06:05:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056714; x=1688648714; 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=JloPPQ1qbkFN5goPusSpNn9bb4SJrlcsJMCIiGexgDs=; b=gX3hw9OR9nrLa44yB9razmn8dCcgDmYHZMMdYYlL/XQ81CgdmesUhyYYGyZHfMN1KP UX/5OQJUcoCa5VukebfZr7KguCNCkQKNxOYaH4jFkhQBTqO6t0Q9n/AHAm+M823QO2LT /JFacp6jKeK9aqEwBggLfY/fShGtfGC88H8WeQgtUUp+VXdJMzEuPVPlrT7KfdVae5iS 9RVtKXAyISNXjfapdrhFMkCgpbgfXi6TmH/Mhd98FcUhRoaaSBFpFSMihppTJvEDkAXL Orn1b9KzcZh7iAi/Y79EpmQy37UFjJcbWEzcEP62+rsk9+wwjyBjJSRXknJr064b3HdO rz3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056714; x=1688648714; 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=JloPPQ1qbkFN5goPusSpNn9bb4SJrlcsJMCIiGexgDs=; b=QzFIwaymP/s17V6t+oG4kWk+sFR9k3kkTgJhzckwYy2d7gWCuP/ewy66AYtzqx43on KHafe4cE72gM19Pmu2ZoEIiVUIxAOWawD9aVaWoCsvOiV2y6kivMvbwVvzrKFJIKefXm s9XW6DK0UVsrMDQHU8GMiBqAhB5PMBqiI/FapdPN/7OJaLbQVi/un0257cunjYKejt3w 9BeKurd7+KhNcvJcvHlww/wHQCUanLuupiLyE6hSwZBl4wmsU0dzclfCsNhLoaDxoewa v527LIhEmUJzpGJMWeZi5ayjDjKO8SoB1lLQDSjlHLuPbsOLzSVpBB/ufSDjkf72AuzS w+/A== X-Gm-Message-State: AC+VfDyL84Y90XuZNZPSGwpTCjcR/u9Yfzmw/Kvh/9KDMbXYQxW4tBWH XUyOb9zrlB6Umt7rBJYL0lfXtFVgPLG32zb4AXh9+g== X-Google-Smtp-Source: ACHHUZ6Y0Pmr4FBKzBkJ4oAYPHb/yMHTYZxtIk/9VokRTHSwN6rU/G/SdZmuAHB1XuvExObYEr/H7A== X-Received: by 2002:a05:6000:9:b0:30a:d517:2359 with SMTP id h9-20020a056000000900b0030ad5172359mr2064159wrx.64.1686056713910; Tue, 06 Jun 2023 06:05:13 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:13 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 03/17] virtio/vhost: Factor notify_vq_eventfd() Date: Tue, 6 Jun 2023 14:04:12 +0100 Message-Id: <20230606130426.978945-4-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 Reviewed-by: Andre Przywara Signed-off-by: Jean-Philippe Brucker --- 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 daea8554..6912875e 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -261,6 +261,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 b7c64a08..c0871163 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 6aa0909f..ebddec36 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -121,18 +121,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)) + if (vdev->vhost_fd == -1 || is_event_vq(vq)) return; - if (vdev->vhost_fd == -1) - 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 Tue Jun 6 13:04: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: 13269152 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 0F69FC7EE2F for ; Tue, 6 Jun 2023 13:05:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237902AbjFFNFV (ORCPT ); Tue, 6 Jun 2023 09:05:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238050AbjFFNFR (ORCPT ); Tue, 6 Jun 2023 09:05:17 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE62C10C0 for ; Tue, 6 Jun 2023 06:05:15 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-30af86a96b4so4963220f8f.3 for ; Tue, 06 Jun 2023 06:05:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056714; x=1688648714; 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=c0xP9PWAS49h9In3aRW4DLV5cYu/h2mprcrQV5sIQmY=; b=wQXMA41ppkAmBOVA/A1V6+guQdMNibhN0DKfwZOF5Jqu5qUFXZK1fOuCV4Av0nEFqg rklos2Kz+Q840HAtL8F+qRFKDZIPnJi0dWa5lWy8KnLtuFIym0kkc6AKmEF/B+LPLGMK wJsrEfNNUjZHpJp3AaiybwNF3E5PBavwqE+/t+pSXgAL26pws7V5xBTTbxFzoYeTX/2O /drlLAFIhDRuV1fSoRHh9GeSSY+l3mIvDk8YOYHCCa2Coz8ldV4xMFgUkS7VdSL7rS92 zBOpeNVgW/gnI+fhHhXBK9uhdBjoSCSRyAAnhTe5d5YgphtGwE2fuj+z6r22VZFx4Agk CFTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056714; x=1688648714; 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=c0xP9PWAS49h9In3aRW4DLV5cYu/h2mprcrQV5sIQmY=; b=CarhFCM4HsI+kABwKnTd4qfCxM5FvWqftaEeVGoa7IeCfHNUYXDcS9pzOxuQlklkox xsajsf0v29+yVM71YaosHdxDUVEbx+ulxed9glL5UhRtiywQegwaAhSTVrTfcpvujwKU DV5Rxot9C71r8D0CjywL0e26huAAyJYHtqWTapCnKUsyI9Bp9u7rPd8aiRx5z2G3KUMG J6aykmO6RD0DPpkx47iJi5jpfMwIw0Ptnwlhpb/ZjO3IorCFj9kb65LqooGBHVk7KIgC O4LkzhT2Xih8GIatb6hACffSEe7uG0BWDxvJnNoyc704SNRCxy4lWm9HStChn6LMEQsq a02A== X-Gm-Message-State: AC+VfDzawyNEAzht0TsNBTTWe2GuEvbxvJRCMd47gYGwNHqnwJusiqdO sphOakoVRXLul2VTbpQrSeT+0RAGHhibGhcdnWY+Tw== X-Google-Smtp-Source: ACHHUZ4MDaxxFktj8qI7LMYGdD8Dpnh/5txMVVroWMJ1EzhHy26rvHfF9r/kMDqFmttZaL5I80GUqQ== X-Received: by 2002:a5d:6449:0:b0:306:28f4:963c with SMTP id d9-20020a5d6449000000b0030628f4963cmr1658532wrw.23.1686056714404; Tue, 06 Jun 2023 06:05:14 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:14 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 04/17] virtio/vhost: Factor notify_vq_gsi() Date: Tue, 6 Jun 2023 14:04:13 +0100 Message-Id: <20230606130426.978945-5-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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). Also correct notify_vq_gsi() in net.c, to check which virtqueue is being configured. Since vhost only manages the data queues, we shouldn't try to setup GSI routing for the control queue. This hasn't been a problem so far because the Linux guest doesn't use IRQs for the control queue. Signed-off-by: Jean-Philippe Brucker --- include/kvm/virtio.h | 8 ++++++++ virtio/net.c | 32 +++++--------------------------- virtio/scsi.c | 15 ++------------- virtio/vhost.c | 35 +++++++++++++++++++++++++++++++++++ virtio/vsock.c | 26 +++----------------------- 5 files changed, 53 insertions(+), 63 deletions(-) diff --git a/include/kvm/virtio.h b/include/kvm/virtio.h index 6912875e..a2f8355a 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; }; /* @@ -263,6 +267,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 c0871163..3e1aedf7 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) + if (ndev->vhost_fd == 0 || is_ctrl_vq(ndev, vq)) 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 ebddec36..708fb23a 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -90,25 +90,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) + if (vdev->vhost_fd == -1 || is_event_vq(vq)) return; - if (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 Tue Jun 6 13:04: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: 13269159 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 E88B0C7EE29 for ; Tue, 6 Jun 2023 13:05:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237961AbjFFNFf (ORCPT ); Tue, 6 Jun 2023 09:05:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238049AbjFFNFR (ORCPT ); Tue, 6 Jun 2023 09:05:17 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92066F4 for ; Tue, 6 Jun 2023 06:05:16 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3f6da07feb2so60669415e9.0 for ; Tue, 06 Jun 2023 06:05:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056715; x=1688648715; 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=J1z+0zhNC212zBv9qAk/OMWR4cU4by9XEvjFcTPlw7g=; b=oU4JU2zAU7G1Ka0jsaqEKUl0gVS5ra9i4Tar3gwglmOEUGEaUWQBGTImV07tdLKWUk aPg0kBRnMvbF8wB5wdEj0udhZHIGRNd1YyChODuydet6nh+wwtDoSBRaBZ27YaQ8FQnU HJqA8A210dK4uEBiicYQzCxxl7CWTpPt4XN2k4yYLDoJxhONkdt0QegwJ2reyuHmAa8B uHVpU+45fh2BnVn7Q63FhulSBD00ePY9fdiYf0FwK84fbsH/oepk10168Mn6Bjpk1aAW ZGvhnK0k6kvDrfTH4ipsCar07uoQaO9rEg//kLGSr35hD4VTbKNFBqynZgR1odc/RjDn M3qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056715; x=1688648715; 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=J1z+0zhNC212zBv9qAk/OMWR4cU4by9XEvjFcTPlw7g=; b=kFUkZXW5wQERdbCVGeOi+HNGmPitUvDJhR2KnG1R5lp3T5yR/w2wtae02jQ3xDE/ov +5FPpgsx2/168Owh8dMPmIc5b4ujZUEVLaX6SrrrT3cXN3KsN5Qpf49mgqh7ZPbdJ5vH PhVrzqr7oOJR5gWzqf5rt0PwDuvv3VmQpvMKCwi9eHRD5MxeEAj+qfyBW969/d+Gzb/9 WUwg4A34aTLR5mcaoZJE/+KrFYaGPFt64QKQRahHfcy4fH8gEjMLAwc+xKV198dKqEjK P+y6kokDd33ZgeGrmNhQM9BH8AQE9r7Y751TVt15kWxaMQD7GBbMcijbKIRIHbI4aTSr IURQ== X-Gm-Message-State: AC+VfDwLZt6S3gJWKTuHpMSshVyicPNAOgOXEMcxslSh/M17yi35Rmpj 2sDy2hk+Nkq2hDifJ3rKraN1ca8YEj7odXYCoBvm9Q== X-Google-Smtp-Source: ACHHUZ4NJLr6SxPw7r5fyTpbA3bQoGABJyNAApfStsTrsNES7sRVKmt/GUKflSF4XiJTkDC8a07mZQ== X-Received: by 2002:a7b:ce87:0:b0:3f1:72fb:461a with SMTP id q7-20020a7bce87000000b003f172fb461amr1999928wmj.2.1686056714968; Tue, 06 Jun 2023 06:05:14 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:14 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 05/17] virtio/scsi: Move VHOST_SCSI_SET_ENDPOINT to device start Date: Tue, 6 Jun 2023 14:04:14 +0100 Message-Id: <20230606130426.978945-6-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 708fb23a..8249a9cd 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -57,6 +57,13 @@ static void notify_status(struct kvm *kvm, void *dev, u32 status) struct virtio_scsi_config *conf = &sdev->config; u16 endian = vdev->endian; + 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; @@ -91,20 +98,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 Tue Jun 6 13:04: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: 13269156 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 0E134C7EE2F for ; Tue, 6 Jun 2023 13:05:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237943AbjFFNF3 (ORCPT ); Tue, 6 Jun 2023 09:05:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238055AbjFFNFT (ORCPT ); Tue, 6 Jun 2023 09:05:19 -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 11E4C10C3 for ; Tue, 6 Jun 2023 06:05:17 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3f732d37d7cso33542505e9.2 for ; Tue, 06 Jun 2023 06:05:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056715; x=1688648715; 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=nmfrQoo49WqxWZYTviu+kFsW7GdGM61jUCMoNJCbn40=; b=oCbOFuRRd+TvTK7lHtpP/uX3SxAVho8Iqc+P24JmLwu9e+uWYMIgSuM5V3kGvLo7Zr UbP8tepIURF++6uLiszCSBM0XnR5VMSFfPATAISm6Y/hbqzxZsENvsI2kgL729FeuuAN R5x8mrQVmuVW5NF+4mfp27lb0qdAVA7tmMhd0SUn0eC4VPw3spnPK5n70rgxs2w8o0Gm 3oYxE/iMYvdagLMwxLLUdY1w0mRgkTE4Qs9g444Z6DXwrToreoNjsc6AhS//yJ6y7nqZ 79P9/q/y//O/OlY6Gqn2r2xwQ3SS5BxIMDxcbD2TTmRFTltO6swX1myO/gt1uoOHotet iedg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056715; x=1688648715; 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=nmfrQoo49WqxWZYTviu+kFsW7GdGM61jUCMoNJCbn40=; b=dx4/PKDNu2gSox4awGlQx5cedsvC8vIna/Dfu4EN1I+gSUA+M/lutW5qPnVpWqVPrE R95c/DfylGjjTZpTYzFBYmr5IphxyKeA6JqVVZGoziCnSkeOo9R+V7Nv3d/DxfgGKv0O D6Ov5PfqX7fOOjdjRIt/6AqdFMHTAH0m9wCZQ6bnYzZHPfy7rbdKbool0RQXZxZa8Vbg SZgcgar7vYuft4NewEb/4j8xnjCYFyr+srLiTGW7aQHiO7TNbzCWK3yMnZ8QuXy/IP+P HiYikno2pypjFhxahYMv0uhfSiKN0lGWwd3p+lhB7RdiJFbI2Q6jAaDo9d4faBN7PtYu 2Tmg== X-Gm-Message-State: AC+VfDydJz2J2fonZf8hDMnamQoyhSlwjzdaY1MIeDeXosbR5HuDq840 1J4ohglX3HMFKanN77VoDCCYvVIFJG22a0UiWDOV3w== X-Google-Smtp-Source: ACHHUZ6+DpdCaBz0VImmrywLXCrIo8ffDuHq0DUNfJrSQ8aINGGpALIV8waMU6No+VsMOWyWIrMn9A== X-Received: by 2002:a5d:4686:0:b0:309:491b:7a81 with SMTP id u6-20020a5d4686000000b00309491b7a81mr1866965wrq.14.1686056715613; Tue, 06 Jun 2023 06:05:15 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:15 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 06/17] virtio/scsi: Fix and simplify command-line Date: Tue, 6 Jun 2023 14:04:15 +0100 Message-Id: <20230606130426.978945-7-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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..652fcd1a 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 parameters. Ignore the 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 8249a9cd..db4adc75 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -197,7 +197,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 Tue Jun 6 13:04: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: 13269153 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 853F4C7EE31 for ; Tue, 6 Jun 2023 13:05:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237850AbjFFNFX (ORCPT ); Tue, 6 Jun 2023 09:05:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238058AbjFFNFT (ORCPT ); Tue, 6 Jun 2023 09:05:19 -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 E8123EA for ; Tue, 6 Jun 2023 06:05:17 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3f73617a292so30516135e9.2 for ; Tue, 06 Jun 2023 06:05:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056716; x=1688648716; 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=kj3OUo5lr0NnhQqfqhjJ+2l8a+i+fStbRuEnyaGYNtc=; b=lL2PnVAB+7LJtudUyHLUgMA3MAgaTAXSq0cShuK/Am3j0CpNQBWfXHMq/Y50cJuZcE TNHZ/Mx1t5v3SF/fDNg4Nerj0akVS2EHiZGve2lCQRn6wFYzl/IMNJjw/KmY9ME9G2Fa ziCteRMKwp3oCuxSGNlRDbzLGKSCUpMstKAHICOzwFSq8o9eoc+b36ws4g1rQg3zg6rR HLU0aDZilXOeTSP6u6g3S9GuPsvU2+29xjkqFd2rinqnnhheWLOKfcYhN8VQ9uM0ymZG YtSMhNaVilcIoUKLvD5dj0b2UZ1pSNX12KLqsoiwfIftSK96SaEl2NRumW0V2UYIB73I up+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056716; x=1688648716; 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=kj3OUo5lr0NnhQqfqhjJ+2l8a+i+fStbRuEnyaGYNtc=; b=J2lby8Y0T6wJbNGb1EUK+RnaeT93jWv1PW5lYQjAzvJ3ehRI5cft9dZAagEl3i4x5v YVZplkQso4h0FE60SStxg1GD3ryOUBMglHQqpr/iMtD4AzkeXVFNyMpfTr+X4xjnUoxQ LIa5u07yOWrQ8AXrEYpl0JufXNCfw04bJ6JRp5PRfB+vAlsyscvRQ2vMBuqNfJnGzGgR dkQyfync9xXKamYBg/9yJbv0bPUYuVMvwu6Ids2OZJDWz/jRrZT+xISdCIY2Wg63Rs8o yqMCayYHxJ3MFhQKasxq96h6s8JZji+lDo5vz3lt5jf3AXQNW1EJCRzW5cFWSokpljA5 996Q== X-Gm-Message-State: AC+VfDw/+B7jf6c8sLfVNUZCupFmHsi0y07yTAa/ucxUSMQhdc4VaRMB P38ZlrzEzGnwuQLuoIhyfVR6A7dqI4+h2SSz00FY7A== X-Google-Smtp-Source: ACHHUZ7SA/eYETDWMfxewPrFWC36woImFCmq3kzLCkw18cUN33WDaJu/myJLRgaSOzizrVgd4ll5CQ== X-Received: by 2002:a7b:c3d9:0:b0:3f6:787:5e53 with SMTP id t25-20020a7bc3d9000000b003f607875e53mr1904489wmj.20.1686056716102; Tue, 06 Jun 2023 06:05:16 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:15 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 07/17] disk/core: Fix segfault on exit with SCSI Date: Tue, 6 Jun 2023 14:04:16 +0100 Message-Id: <20230606130426.978945-8-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 652fcd1a..b00b0c0a 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 Tue Jun 6 13:04: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: 13269164 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 EE39DC7EE29 for ; Tue, 6 Jun 2023 13:05:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237974AbjFFNFn (ORCPT ); Tue, 6 Jun 2023 09:05:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237916AbjFFNFW (ORCPT ); Tue, 6 Jun 2023 09:05:22 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DCBBE78 for ; Tue, 6 Jun 2023 06:05:18 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-3f6e13940daso61926955e9.0 for ; Tue, 06 Jun 2023 06:05:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056717; x=1688648717; 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=NJve2gG2xkfjku6DUmV88NrnWL7jRqBntctBajIvbRo=; b=XImr9kOS5lS9h86FQQwWgbanzCLPXe3BCROPUSTK2zsFxxSOGQ1O/HEv10QLbd45DX gz6JJnwzpjC2P9/bLhUMErPjkae62KGKMnnLimdu/43rtY0oSMteCsodX/gMLFC7fT0f OQWjPs4FC1bp/l+fHKo00kWql8ei1kSmMFJdywuOIniBEEc/Q/OH33/LtS56xixZOX9Z hT4UMYQ4Ht79DToaCB28LPVJ4ZXmxyR14BLKilG+FnQQVg0Uog7apr3ufVPuU6gEuvzO /K+AuClATC0tbn6ZAjm6JYyxxSke1wakOZcjM95b1SFD2YNEUDVZXqTebplmGMeuUxkR DEzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056717; x=1688648717; 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=NJve2gG2xkfjku6DUmV88NrnWL7jRqBntctBajIvbRo=; b=GDaQ3K0YuEikmdyu1nFY7LABCkNsmORbpfUsAjqjlFXns7aF/3LEsgn/ahfi0DiKyG n/uzl/nQeqozo+MbGtE+aAGxgj48vlBzl3edjCCfSQAQULmfegIZfl1xxmE4saYT47rT cExNyA9/yI1PxXXKtJ1eiuQv3VCMzSwmXmEz/M1rdwNj5Ez2k4gVlvR4YEbVZfZG+KN8 aGq6NiXaYqfQPGRCBia+6mtPqb2mVgsmwljmIkYaipxsX82AUTk0/Xr2xuhRHE3nN+bK crt37jm6+v5EImT88Pgdc8pZLRseGtIfQVB0+TvQWjopkTi2zl91jb+HWSmuLD1N5aPN oVVQ== X-Gm-Message-State: AC+VfDwHiY7aHsy3IORylRI78WcarwdWvV0fp3R9WoVWCCYOC/0N8+Jd qlqS1mPCEvpIwoHEX6MkEMbz21Z7cG5H64H7axCBiA== X-Google-Smtp-Source: ACHHUZ5GVkG5KeRrr91RmSwQMN1YpkPYxj0aQlG2S7XskI8eG0W5YGPZ+B8ucBlrr5PJNxvstx1z4g== X-Received: by 2002:a05:600c:291:b0:3f7:e6be:bba5 with SMTP id 17-20020a05600c029100b003f7e6bebba5mr2127802wmk.28.1686056717143; Tue, 06 Jun 2023 06:05:17 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:16 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 08/17] virtio/scsi: Initialize max_target Date: Tue, 6 Jun 2023 14:04:17 +0100 Message-Id: <20230606130426.978945-9-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 db4adc75..4d1ed9b8 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -73,6 +73,7 @@ static void notify_status(struct kvm *kvm, void *dev, u32 status) conf->cmd_per_lun = virtio_host_to_guest_u32(endian, 128); conf->sense_size = virtio_host_to_guest_u32(endian, VIRTIO_SCSI_SENSE_SIZE); conf->cdb_size = virtio_host_to_guest_u32(endian, VIRTIO_SCSI_CDB_SIZE); + conf->max_target = virtio_host_to_guest_u16(endian, 255); conf->max_lun = virtio_host_to_guest_u32(endian, 16383); conf->event_info_size = virtio_host_to_guest_u32(endian, sizeof(struct virtio_scsi_event)); } From patchwork Tue Jun 6 13:04: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: 13269160 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 3E8B9C7EE32 for ; Tue, 6 Jun 2023 13:05:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237962AbjFFNFh (ORCPT ); Tue, 6 Jun 2023 09:05:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237895AbjFFNFU (ORCPT ); Tue, 6 Jun 2023 09:05:20 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 374E1E5D for ; Tue, 6 Jun 2023 06:05:19 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-30c5e5226bdso4123031f8f.2 for ; Tue, 06 Jun 2023 06:05:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056717; x=1688648717; 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=TRqOAAr0go5z5Hhx7QytTfw699MDxCAnmUjosYF7e0M=; b=Nbw4sqNGJ9UISJbM7xRTYoCv+x1Ajy/AOlHsecUsda5WWz/Z5CzLT/bngCanJUhyML Hy7lBIdQq9YSuTTs2/ROaQPhhXxEHTK0FLZ7imQZMD1pFv9u5FBvpgYK4uX/jHFJJCik ZN1Nx7VLuBMbw+epduRsPfHkGFVhnPHSCdKKmwd6yypLersx8V7RE/MLOWKCbjNu0ITU v/7Nla2YNTnT9yelBtJGFEgIlXlGNvy1LEKANa7UrEvsEzZiQ//JwvkqedaG7MrK9MKB sTLMidxiYjfBYM2+tfGin7O2DdX2tV3OlC8rdG5q1TEX+riNYpCYD+U9gPlTGQyei0zb wLSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056717; x=1688648717; 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=TRqOAAr0go5z5Hhx7QytTfw699MDxCAnmUjosYF7e0M=; b=cQtQXGAsCDEpVCcD5sxZsveQ1QXBgsbtuSKswBHWv34PYPbhzwyZwoWqxO53gwwev1 OOShq0TzLf8HgTJ64Tqh4qlMEhsSpVc37+uKbNMHRfVEN+b73SB1S5bYsFiILZIpciMW arso3POSaATLgxYh5E2Alpobi9qVjlEsk62sJqzzEKlW6XXgYrD7MOl63lHGsKMw09Nz 6zkd6lTDKxfYsr5EjvoKBYye0G1tXiiFV5jnP9cThxVRAmCDNfmOdj37ONWUq6hfw8Ya UR/zRUCY3fZsMIAJa6YZAdbkaASQF0GBoAQfsXzxCZJ6BCWT85BkldSwQd/LWyappbyV lbNw== X-Gm-Message-State: AC+VfDze5FhgwQn8Icuem4ZCm7TlQbdFjMuWF3t7Ls0aIOL0hn+LW4KN /vjJ9q6KmozoLHd/H1v9Z8SF3MJrDjYjhQFkTOiesw== X-Google-Smtp-Source: ACHHUZ6GWYE8INrWNnx7RSKrqhXGsloyGvknuoUQ3r3PIf7b04Nmdmmy2NvvZGbkpSXTsSvVaz59sQ== X-Received: by 2002:adf:dec3:0:b0:309:49e3:efb4 with SMTP id i3-20020adfdec3000000b0030949e3efb4mr1773175wrn.63.1686056717751; Tue, 06 Jun 2023 06:05:17 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:17 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 09/17] virtio/scsi: Fix feature selection Date: Tue, 6 Jun 2023 14:04:18 +0100 Message-Id: <20230606130426.978945-10-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 4d1ed9b8..50f184c7 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -46,19 +46,35 @@ 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; u16 endian = vdev->endian; 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); @@ -161,23 +177,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 Tue Jun 6 13:04: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: 13269161 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 D2334C77B7A for ; Tue, 6 Jun 2023 13:05:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237954AbjFFNFi (ORCPT ); Tue, 6 Jun 2023 09:05:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237905AbjFFNFV (ORCPT ); Tue, 6 Jun 2023 09:05:21 -0400 Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18806EA for ; Tue, 6 Jun 2023 06:05:20 -0700 (PDT) Received: by mail-lf1-x130.google.com with SMTP id 2adb3069b0e04-4f3ba703b67so7497554e87.1 for ; Tue, 06 Jun 2023 06:05:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056718; x=1688648718; 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=SMCSj9ykrTkYqs21+BkUjr47dMUoWMQ/hEySo2FJU04=; b=CX58Ts/sgWwSznUPLCLmtUXiJqOoIvlMuYj0jb8QOzcuKyQuv+x0LpihgNlp8TDfEf hOdw5B5ad8W9iSQ9LoSSXDl8frVwOwDONyqYctwMagBu/E0FbHozV2siT9uqdL3Zft/S XceqLenu7XIdmSBAWebFCHdzSA0gFWVkVAPkeDbrCyqmsZJo3ox9xOnOralAdm3J+cSx aYgPdLLP3bFE1ZmeuURR3NSU4FlUxEeKtlYn5y6DxQ0n18j255aq2tpW+UpgMrj+ZZXd N6MoBPPAcs4GrbIojFr4xoK2z6zFTw83L/KVp2ve5h1BeT0MmsAWEgCtbuXZFVfq3M9h ppHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056718; x=1688648718; 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=SMCSj9ykrTkYqs21+BkUjr47dMUoWMQ/hEySo2FJU04=; b=Q4l7Oo8MV44fHXd5YMs4hFZvfVEetPS/n1qY0gxTzML3Ixn73tAwu/H7G+D+hhMUDP lN4gMG6+p7FVXfIKucnmt0klRYVQ6HHk1n0yfsRo1hM1nNBPlYcNTD7PjwbFDTQK6FDw lvd7IgxwCJj4+bvbkgJSHDbwvvUTS06tdA9PZJWxKzSYfh9FiI3BIMf6RuoPz5obe0jn 9ZftjNxH3VDmY9e2/3UiWK800qit9C9zgJPLp1Khv7AHJ/O1EiPteDMaZUPT/10s3pj2 /xoJyOJgGTLHCvLzgmvyIsPJrcgb3IQtfOSzaVayiQXZvP90f86g0B0YvE09qY4Gw4T7 1Fzg== X-Gm-Message-State: AC+VfDxT7FcOPim1vTL6J7T+jIKMUBAQw+Nev/ODA7bW2Est1HgYPxHy vKgPmElM1SdoI88pB0vB0Dwf6fNALV318ggd2maQ9Q== X-Google-Smtp-Source: ACHHUZ5NkIFQuJZG1ezT1j+aT1JZHIyO3ZJNXh+xuayBkIMwWj/5NIIAll3hrQPElMaDDR2KTZMGWg== X-Received: by 2002:ac2:554f:0:b0:4ea:f8f0:545f with SMTP id l15-20020ac2554f000000b004eaf8f0545fmr879200lfk.52.1686056718345; Tue, 06 Jun 2023 06:05:18 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:18 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 10/17] virtio/vsock: Fix feature selection Date: Tue, 6 Jun 2023 14:04:19 +0100 Message-Id: <20230606130426.978945-11-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 Tue Jun 6 13:04: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: 13269162 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 A0EDDC7EE29 for ; Tue, 6 Jun 2023 13:05:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237960AbjFFNFk (ORCPT ); Tue, 6 Jun 2023 09:05:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237906AbjFFNFV (ORCPT ); Tue, 6 Jun 2023 09:05:21 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F83E12F for ; Tue, 6 Jun 2023 06:05:20 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3f735bfcbbbso24389335e9.2 for ; Tue, 06 Jun 2023 06:05:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056719; x=1688648719; 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=TZXTqbmcrb3RPdtDiUPuxGbbBbDZFyzE4xKU+0q08Rc=; b=UemHrYou4X8b5dcNO5RoPfJvHltDScdRjge5X6YP9HDCaoUrpzC/YGcW5e4Zp3sl+M ihaZ0n3QkObuT70zQvbLn5s8f4/fijR3M0Rqf7hoMaZB5XFFolmysOxuUiEnr62qnOhw Hx9/6ns39fT3rrGdkFX9GPWwk6+TzN46jlRIaGhscfLuex5aMlYtQykHOK8oWjNbi6xS +ffPR2/CTfvH4OXwoqqE943d0HyIjnHKimLSYS5HxFA/JC8WkHThjZWup+oW+F1LmoNo YO10rAoQTy/6BBobjQyTv/w/6cKYQEdCpLYRJgv8bd+U/QrCub2P4TBrBqdiQ1t/XoA4 CDxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056719; x=1688648719; 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=TZXTqbmcrb3RPdtDiUPuxGbbBbDZFyzE4xKU+0q08Rc=; b=iZEHKxY0oWNjiCNb1qnZR3F/+OYW/XEd0aLiiYhUsZ2nuZen08cY5asRuR+AujrCd/ MxHN9A1h+lO0GEhN4X/MRkIG6CNSNujV5+OYiUlYIyw4RswbUMZBpINySX7zSRR5Lfye PYLx8Yu8vrUQQ8twV8V3UoDlntB1J4h0dbmXMSNWN6Q5a3wXT54ew+i0iDfNgiNy5QeH 9wQ1DfcQMGVL+tkfo2kUVKnYJbeF5O5/AGdVodCII3aglQlvzq6N33zQRrRH7r2sKXY+ 5vHpUj1CalH4R6WX3wQCPdf3Az9HZb7PznxIFm985qlyD+qoRK12nlnGo14CmN5T2+28 ME8g== X-Gm-Message-State: AC+VfDybMTxZyrz/7yS5sIMGi1GWj5s5jYvlEe6BJjYuLPTN6CL1ptD6 LYfOoD4eXTP8w1ybWq+1ywZKoIGb4RdcRALdq1saJQ== X-Google-Smtp-Source: ACHHUZ4QelMp3kWl9CI8l2y8c77Tmi0A4PApbzGf4lrW3T46cLLI0k5zZSOYa0MW9NLeWas89oTkxA== X-Received: by 2002:a05:600c:cf:b0:3f7:33cd:59b3 with SMTP id u15-20020a05600c00cf00b003f733cd59b3mr2158014wmm.22.1686056718899; Tue, 06 Jun 2023 06:05:18 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:18 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 11/17] virtio/net: Fix feature selection Date: Tue, 6 Jun 2023 14:04:20 +0100 Message-Id: <20230606130426.978945-12-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 3e1aedf7..c4d20f22 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 Tue Jun 6 13:04:21 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: 13269166 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 CD30FC77B73 for ; Tue, 6 Jun 2023 13:05:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237734AbjFFNFr (ORCPT ); Tue, 6 Jun 2023 09:05:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237944AbjFFNFX (ORCPT ); Tue, 6 Jun 2023 09:05:23 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6C52E7A for ; Tue, 6 Jun 2023 06:05:20 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-3094910b150so6210501f8f.0 for ; Tue, 06 Jun 2023 06:05:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056719; x=1688648719; 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=1xpIC4oCXJDkpNBSRGuPaqdbC+Y8smCgQBRBUHbLH5A=; b=xWcr8ck4TNtllrbLTeWyVNsVA1b4dC+0tDFN4rYR0epv0GgHPoL9Dkkkiu92UiLwGn YFjPUNVrLdoXylstYOmYYtL23vYHZQ/ZdXefo86faKGxjlQPfalnJYbnQJQD8VJAWli/ cHxZC1hrBlxcWncULYJFrRmSxwmAQKf0HyhP4gyfH5Yk3ArDrFRtvO4xJ/v+cNqPEmdI HE2ztPFS/GocQ3BeQG2Di17wyWlgbwKIu8vmke1ZQU2pF0DS+X74tHt0jxi7HpjEnQB4 psoSQts74GPMkNYQKeNWYMpt/64uq+BSnMa0NEOgpmNk991mqMK/FSEg7XpC43HP9z2c lzew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056719; x=1688648719; 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=1xpIC4oCXJDkpNBSRGuPaqdbC+Y8smCgQBRBUHbLH5A=; b=AAfoE7bfoVJBVYlRg2iy0Np1FVdNxSqwD2wYEUKezMUIT96usaWpnqMHr5FAmQfZI6 yDhTomKotonhl4jN6xprUO26mVUrqwZ0Rcs9L2HUuIsQFqOrJIvwdJq1iDKGkxs/adfT cxuhzGbSmFxxivNiTSuPJUN8gHnjhjQRSkoXjJbcwUsE+ep6Ile4A6mpYvVLfdSawd07 LlGfWw4/pRttL5niNUdPTuGsz/ZrzNLMUx2XhOIZ090UaqHjtUXEru21YpY/+6ltFd73 6j0nVvrr37h4bKM1bmqUgxzynjlw8Z+xryZCm8yjXMYxoJM4QSRoVtTx/gee45xMODUb Z1Zw== X-Gm-Message-State: AC+VfDwiY2IPJjNuKloe4NO3gCZBNdUWoEQV6AAlSpP4VLL5fpz4LARk 8VyKt1dsToBMCWpOyGdUROW+dBteAWqXh+LxKvABgA== X-Google-Smtp-Source: ACHHUZ71AqW0WvB300VnsIpwY+J+p1jlc3EJ9yWD0lIfADoeMywG5DIw6EvE9/nYyz/lCA9OATNrHg== X-Received: by 2002:adf:d083:0:b0:30e:46d4:64ee with SMTP id y3-20020adfd083000000b0030e46d464eemr2042105wrh.29.1686056719384; Tue, 06 Jun 2023 06:05:19 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:19 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 12/17] virtio: Document how to test the devices Date: Tue, 6 Jun 2023 14:04:21 +0100 Message-Id: <20230606130426.978945-13-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 Tue Jun 6 13:04:22 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: 13269165 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 18728C77B7A for ; Tue, 6 Jun 2023 13:05:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237921AbjFFNFp (ORCPT ); Tue, 6 Jun 2023 09:05:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237934AbjFFNFW (ORCPT ); Tue, 6 Jun 2023 09:05:22 -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 80AC7E77 for ; Tue, 6 Jun 2023 06:05:20 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3f732d37d7cso33543395e9.2 for ; Tue, 06 Jun 2023 06:05:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056720; x=1688648720; 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=Pcmz6upDEb4xnpErOWweTRHm49Jr2vi2d8azt0RGjI0=; b=fu/62poHG1E0x1ygRWkxbauISQwrEIXKMLD3913dshfY4yMEHLwp0wsOn3D4hEfB6q yhOHTy+cvdICWk3q1geEDzuotpeqv0i8pZl2MWxA0p/Lrh3/17Tk0aaycKTO1xZ2ZCjn ZmbQX64vSGqEA3p/3qCikvFm+2M7AHchgDeDQyUuN/fBhp91yjewQQXWo3ymUHF1k85Y 4afZQGY0hZwTANe4zQW35fRL/mTD8do9yuEb+D0TuYv23hRVDneUBipANGSZ1d4jVjQf KuU8FI7Xs9nXsdppa3lKYW6cMTWusPX8e2EEVf2enc3IdnbjoVbNIS0UaPl4J1tTjXxZ PnvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056720; x=1688648720; 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=Pcmz6upDEb4xnpErOWweTRHm49Jr2vi2d8azt0RGjI0=; b=GxwlJ0IyfxfuMsNzNE9LrnDOy+Y7Nc75lODVEEJBlk93ZdLhT9mucNyJA1S+6ejxTo eQNEgSSgxwsun7R4IT8hSkAjVpmWNxtWztBEJaD7y4ti5s+WxN2nDfS74/NE9DJK37Y7 5itiTcTzPdaFKNz/96iss3xXB0vmV1tGCD9eWnr6wnlXMuvP2uUp8wpCuqhCGutS532z BL4l+6aPrBqlmHdy/5lf1g/fsTvh+biOnnKQAFSc8mywWhdImPnGglTs0wfC7PUtVNCI tKR24k6nJVm/wtpXwcs/kd+0alTG1ulIGsPzhSqfOyGoYMTnb47+CfY1mq66iZ0yjpcj dl4w== X-Gm-Message-State: AC+VfDzX+NRWqPYzllH4QOIjmVXpxJqE+7svHJUkreVEqB/EPcdrej7m HuZ/U3nwmeV+Rpt/c+mUrUSuiRtX6Odhn/EixZ3lyw== X-Google-Smtp-Source: ACHHUZ6itCUkaPCuCDuYN2PbN+pHK7Jer4WtFhnfrmG8/DdNswnthVKmPa7EeIFayM/18b8vPdZ7QA== X-Received: by 2002:a05:600c:210e:b0:3f7:30a8:3888 with SMTP id u14-20020a05600c210e00b003f730a83888mr2347965wml.37.1686056720100; Tue, 06 Jun 2023 06:05:20 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:19 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 13/17] virtio: Fix messages about missing Linux config Date: Tue, 6 Jun 2023 14:04:22 +0100 Message-Id: <20230606130426.978945-14-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 50f184c7..52bc4936 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -216,7 +216,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 Tue Jun 6 13:04:23 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: 13269163 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 25353C77B7A for ; Tue, 6 Jun 2023 13:05:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237951AbjFFNFl (ORCPT ); Tue, 6 Jun 2023 09:05:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237919AbjFFNFW (ORCPT ); Tue, 6 Jun 2023 09:05:22 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF622118 for ; Tue, 6 Jun 2023 06:05:20 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3f6da07feb2so60670225e9.0 for ; Tue, 06 Jun 2023 06:05:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056720; x=1688648720; 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=H8CiZLAonXPhMpCBmNdJAlRlloBFl60CEtSHxKl/b6Y=; b=qtKeEMKVJ/8CiyNzrp5tN5cjwJCoe3eyabwWLYBFWtWVidAdKTigL7EQePI9UjWu9/ 0XCS4OSiglO6Z57C8M9SzHGa2js2aZKNQs8xHk2vrWhZi+EDJh2bttYraQhEPxNHRyy7 +gDYV7XNWSF8AoN1zfuenViUZAzfyvCoF10BlKoMyNRFf60Y5aF4Q0/GWYTPC76/dirQ UNNtIiAAY+0evixIefbWScbNISMjeXmcPDiXkM7EOKrigjw2vWnVlDSKwFkPXPsRZejJ 1dF76whoe+bBaZClN9tdVZxracVr4BgPiybK/YQdy7wAuNvrQJNFtGVWw51bW1Qh1OjL TJNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056720; x=1688648720; 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=H8CiZLAonXPhMpCBmNdJAlRlloBFl60CEtSHxKl/b6Y=; b=IP+wIRFz++a2bvWgMMfpBenSJqID83fzbQHHKwXydI6r6HTB7gR/YwGWuCBBFzCWVd 4uXbiWH0Qs/nLvObw8Iy1P0oH/UBms/7ZeNHoLrFd1N7EjpsKghh66B1t6dk4OET2psN cdJyryXGLMescwJhtMFj/FCt6niOfC9MRlUsXIhBhAIM3LC8uHmYsI1vwKAsYFPLFdcf B7vc2dcVLN1D2s8wby3P4ZwOGNdUbY+9dy+8yASd7qEDxIS0Q7AB7l4doJzzW8fvKzwk g+MBWSOMQW4cqtNTAWKGRyh6MYM5gZTNeLKGc0CyCaxXcWcCproMRNqhPh6oO5RUrOlN Yj5A== X-Gm-Message-State: AC+VfDx34uJ9LB6PUzLmZb2mFW06aLWyYN7nXn83dr64mGjGCq67x0iN GM+EnNVmsOQnwPWftZO4nNOVnTLlhuRQxzrkQep7hw== X-Google-Smtp-Source: ACHHUZ5+9j7szOf4UJJe8yB402qVI5zVrAn2knHGQqVCi44H8chcpcDpANdomK6S75HtZezhcymNdw== X-Received: by 2002:a7b:cb94:0:b0:3f7:381a:f5b5 with SMTP id m20-20020a7bcb94000000b003f7381af5b5mr2042256wmi.9.1686056720607; Tue, 06 Jun 2023 06:05:20 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:20 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 14/17] virtio/net: Warn about enabling multiqueue with vhost Date: Tue, 6 Jun 2023 14:04:23 +0100 Message-Id: <20230606130426.978945-15-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-1-jean-philippe@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org vhost-net requires to open one file descriptor for each TX/RX queue pair. At the moment kvmtool does not support multi-queue vhost: it issues all vhost ioctls on the first pair, and the other pairs are broken. Refuse the enable vhost when the user asks for multi-queue. Using multi-queue vhost-net also requires creating the tap interface with the 'multi_queue' parameter. Signed-off-by: Jean-Philippe Brucker --- virtio/net.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/virtio/net.c b/virtio/net.c index c4d20f22..02667176 100644 --- a/virtio/net.c +++ b/virtio/net.c @@ -741,6 +741,11 @@ static struct virtio_ops net_dev_virtio_ops = { static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev) { + if (ndev->queue_pairs > 1) { + pr_warning("multiqueue is not supported with vhost yet"); + return; + } + ndev->vhost_fd = open("/dev/vhost-net", O_RDWR); if (ndev->vhost_fd < 0) die_perror("Failed openning vhost-net device"); From patchwork Tue Jun 6 13:04:24 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: 13269170 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 AB0ECC77B7A for ; Tue, 6 Jun 2023 13:05:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237957AbjFFNFu (ORCPT ); Tue, 6 Jun 2023 09:05:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237898AbjFFNFa (ORCPT ); Tue, 6 Jun 2023 09:05:30 -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 10C0D12F for ; Tue, 6 Jun 2023 06:05:23 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3f736e0c9a8so23002455e9.2 for ; Tue, 06 Jun 2023 06:05:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056721; x=1688648721; 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=bafWw00t7cTGxHpQCzq9vzrCX5SH2wkuuxmiXYU5Gf0=; b=VUMYVxJrlHCAUsJHS6E+6B1Mb6+x06uQcXAB7wTIn0FT8JFTkwSIppMHJfr2BSBMHa AI8BortySeqwn2OPxSYGTBurV7aC3Fwjd8AEZ5GtV2ZTpMpYwWKhLnnb/alMxBKPj3M8 9eMXUe+Nt896KEHxnwe6GqkDDov37FFVyi4TBaAaANSmDdjpUXiF+c+Xr5fnac9xw3hF +pLRzY/frteFmPoISbnAv9jt+qKvscDNzY1VZeUSQfb3zxfZjxx1DXYG2n2Ne7pIeVZH vfK5aP0D3MpBkQFpFHwaiigAhNdy5Lt70jPctajliApDBKfkTlWkxtyiVYbwry3jY1kW z/BA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056721; x=1688648721; 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=bafWw00t7cTGxHpQCzq9vzrCX5SH2wkuuxmiXYU5Gf0=; b=JZk7C1k0kSwX4PU4embElQU3LPba2v1JhTuUCZ99oLO3/zG/eW/5rKHHN3YyGg5B4y AIldT7lmVP7rMLsMR5r4BYCZ846K9XZ1qfECiVPSwR+tB382m56uH6zgojEqK2qrLHb6 qmWN03UVvA9mFw+XUAAmVO0VLNlJS+O/BVWxHEKSMYbX/x5lCAYD/V02yMkJlt1eGxpR 5YUC1zgvgTau0a/fBgw+vmug9gEN8Ec/Dcu994FR8xyRs8pFuI//TIXafSYIJiCH8VZQ 5S6/7mOiSm+UAOCsN5pzKP6dFDACYNJ0V03Vh4tQO7MR7OfsJIsg1uU0jaHQfnPECKGk g9Cw== X-Gm-Message-State: AC+VfDxn8zUGBs92TXA6y++Y4njHuha+yV3JJyo6KhJCfjjfCNZpxbH9 RHjotwSd88tpqU/Pg/6FI/5e98K++G4Xf022s7nFxg== X-Google-Smtp-Source: ACHHUZ6pwM6tPmEgwU2GnQOQ7Q4W0IeLZwxj1YFD0ocis7jqVk7/zYXfbS1dA4BW3v6hPbk1ihNZ5A== X-Received: by 2002:a1c:7408:0:b0:3f7:3a19:b951 with SMTP id p8-20020a1c7408000000b003f73a19b951mr2094097wmc.21.1686056721242; Tue, 06 Jun 2023 06:05:21 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:20 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 15/17] Factor epoll thread Date: Tue, 6 Jun 2023 14:04:24 +0100 Message-Id: <20230606130426.978945-16-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 | 91 ++++++++++++++++++++++++++++++++++++++ ioeventfd.c | 94 ++++++---------------------------------- kvm-ipc.c | 103 +++++++++++++------------------------------- 5 files changed, 151 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..8cb0cee5 --- /dev/null +++ b/epoll.c @@ -0,0 +1,91 @@ +#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: + if (read(epoll->stop_fd, &stop, sizeof(stop)) < 0) + pr_warning("%s: read(stop) failed with %d", __func__, errno); + if (write(epoll->stop_fd, &stop, sizeof(stop)) < 0) + pr_warning("%s: write(stop) failed with %d", __func__, errno); + 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; + client = kvm_ipc__new_conn(fd); + /* + * Handle multiple IPC cmd at a time + */ + do { + r = kvm_ipc__receive(kvm, client); + } while (r == 0); - nfds = epoll_wait(epoll_fd, &event, 1, -1); - if (nfds > 0) { - int fd = event.data.fd; - - 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 Tue Jun 6 13:04:25 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: 13269167 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 300B0C77B7A for ; Tue, 6 Jun 2023 13:05:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237934AbjFFNFs (ORCPT ); Tue, 6 Jun 2023 09:05:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237952AbjFFNFZ (ORCPT ); Tue, 6 Jun 2023 09:05:25 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79A57118 for ; Tue, 6 Jun 2023 06:05:23 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-30ad8f33f1aso5346793f8f.0 for ; Tue, 06 Jun 2023 06:05:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056722; x=1688648722; 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=Zut6N+o9DqsqZ3QqueaCrmqxL7T3XNKiHLWPtWI/Av8=; b=ISRALrDCAoS9krTJIYl8JyD+rV5oBa2m9aFPptO95FfYWEe2NdMGe/R6a6uAk649Hs QxM8LjoECCcdhAyuBr8vpGvH6RDj37USyzjRvoHSHeS7Fr9diGxuLYzKrPU8l/tK8ArR p6X7C+e5ki4p1gNWGbSfOPwAhqyP+H4ad87Q7D63SMacqHbRI7o8+dVSgKYRKUYJncrr l6ztgP9xGwyAhc+LJrMb6GrTCX4aK3fNsZbsdwy3CgWOsU0C18sSrrcwaILcuFUpJLVj MIIjfPpggeHGKvgLbSOusRe0fCkJjTDDl/yipLQCuS7MrTAqQDKH2+abrzz6CqrPTKjK BhQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056722; x=1688648722; 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=Zut6N+o9DqsqZ3QqueaCrmqxL7T3XNKiHLWPtWI/Av8=; b=Q4lrw7cFtZL0/+VkWbbs/Nitq+AEFf8mMzKj0jRIB3fpYbB+oNi+a2kMkO3+ej3qla Wd6rsKSIMKQ3pUjhPGZ20wMclwlyn7w3YK2sQyRB3KihmbrZGaNg6blKNj6zfu2LR2xx 6SWmg0X6IzfelXADth04G+WJ04tl9vjtJRr8wcEvAnNTq+bu86BHKdbi79WTAxSb7y5V oMUO+gA0SGdpvmrqhgc7269Ll770ZfD26oDzyG6OpAbde2d6k2S6SCpenYhgSfEMiNSp GLCkzbgA9/o/U+VFspZVuQiJWKfahfoTadfltC883Pol94xIvPs5nBlVac2+mQgd3kKu Xsxg== X-Gm-Message-State: AC+VfDzA6ueQ7aW/JsOlkNCT9i4J97X0ENurhAwHI8RIwvxjDYae8QsS 4jrMq3G2s2nFSLLpiAq1Nv0b8IkHkL57ba8ecpF2vQ== X-Google-Smtp-Source: ACHHUZ774OHoW2or+KWWTTDVMXbviLDTvKqQJ7cF5VRST8yYqvnIXQydzR9iwSDPBRrP4K1ryuvhBg== X-Received: by 2002:a05:6000:1142:b0:30a:eeff:3e77 with SMTP id d2-20020a056000114200b0030aeeff3e77mr1679893wrx.14.1686056721931; Tue, 06 Jun 2023 06:05:21 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:21 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 16/17] virtio/vhost: Support line interrupt signaling Date: Tue, 6 Jun 2023 14:04:25 +0100 Message-Id: <20230606130426.978945-17-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 a2f8355a..1fa33e5b 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; }; /* @@ -267,8 +269,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 63735fae..a77e23bc 100644 --- a/virtio/core.c +++ b/virtio/core.c @@ -198,6 +198,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 02667176..2b4b3661 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 || is_ctrl_vq(ndev, vq)) 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 52bc4936..27cb3798 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -119,8 +119,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 (queue->gsi) + irq__del_irqfd(kvm, queue->gsi, fd); + else + /* Disconnect user polling thread */ + epoll_ctl(epoll.fd, EPOLL_CTL_DEL, fd, NULL); + + /* Connect the direct IRQFD route */ + r = irq__add_irqfd(kvm, gsi, 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; } @@ -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 Tue Jun 6 13:04:26 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: 13269168 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 427F8C77B73 for ; Tue, 6 Jun 2023 13:05:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237971AbjFFNFt (ORCPT ); Tue, 6 Jun 2023 09:05:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237957AbjFFNFZ (ORCPT ); Tue, 6 Jun 2023 09:05:25 -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 08DDFE78 for ; Tue, 6 Jun 2023 06:05:23 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3f739ec88b2so21792495e9.1 for ; Tue, 06 Jun 2023 06:05:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1686056722; x=1688648722; 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=pdfytQloM9GeRF7CxB2XjlnNueBYEnZCkjNuVpc2Ttw=; b=lSQpQlaZ8OdCRKVxOclE35LDsH9mDXhUQNEpYW1nGHq+pQ+7fSuQQ+0AmcEoRi4ZaZ NgPQhlAMV5TGnFqV7HIwNH+xdpsEu1gTcANWSf3DYZMyHB9Zc0LBGZTLZhFoA+Swdm0H LtJRg6jyV29b9s+8L485hM+ThB3RyqJHPCG0kb3p0pfa9/VOw/2YRlo+Y78ZuErVnGnr dMUyxwNuOZY/vc4Q/04l+2B9c3D6u1oGs0HJuQs4vnQtBMQ03ZLUrnyPbBF5ZEQdWYI3 bUYJV3KflDPVsk8Rk/aqLIY2gDi+zoD8ww4U/e40Gdq+4q0sTjY+fMJEENbav0YZd0WN zz0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686056722; x=1688648722; 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=pdfytQloM9GeRF7CxB2XjlnNueBYEnZCkjNuVpc2Ttw=; b=j6PiV0LOU0XmW2lB6YA3MXEzjky3g8eqTYkyH+3pc2/SfJHb8JQhziNqCtGCZMc7FT swW+RtfranuG3CX0EZWpOzEw9ZN/ilTeFmVs9KtYG2JS9Sm6Z2BYwvI3nKTqFkP525ab LzTOWzvq+XgdTgnCaJ3mU6laq0gw5KXXt5ty5vDFrpk3uKPtjQm2B2rWj3wEz0K9rNiL YnCF7ldgr2xvKU467qsR6Nm+kwj1KvCzGiHJTM8cmMddcbXq5lm53S1uV/91kL9utcxx 3nM3yqpjpWyt0xKJdAl3ntqSl3TWDtvZ8xBVO+SffTaTzkNH4M6xgt6+GyLreM0jnP9l q9ag== X-Gm-Message-State: AC+VfDxe2G+P+JJh2y85De+dXIm0gdS0SxTtnA+9qNabNvdDWb5gpt5u eBdiLSPfO6LpAX5HTbhqhGJad1E0oSZBg8QGPBg2IQ== X-Google-Smtp-Source: ACHHUZ73Fj0AX18apnyC3Af20nKXQt8bwuhd/9TkJB8q7tAZX1RkHMyF8uueo3TnlEJqUHz0pwp/5A== X-Received: by 2002:adf:ec43:0:b0:30e:4265:c903 with SMTP id w3-20020adfec43000000b0030e4265c903mr1626264wrn.66.1686056722490; Tue, 06 Jun 2023 06:05:22 -0700 (PDT) Received: from localhost.localdomain (5750a5b3.skybroadband.com. [87.80.165.179]) by smtp.gmail.com with ESMTPSA id h14-20020a5d504e000000b00300aee6c9cesm12636125wrt.20.2023.06.06.06.05.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:05:22 -0700 (PDT) From: Jean-Philippe Brucker To: kvm@vger.kernel.org, will@kernel.org Cc: andre.przywara@arm.com, Jean-Philippe Brucker Subject: [PATCH kvmtool v2 17/17] virtio/vhost: Clear VIRTIO_F_ACCESS_PLATFORM Date: Tue, 6 Jun 2023 14:04:26 +0100 Message-Id: <20230606130426.978945-18-jean-philippe@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230606130426.978945-1-jean-philippe@linaro.org> References: <20230606130426.978945-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 | 16 +++++----------- virtio/scsi.c | 3 +-- virtio/vhost.c | 11 +++++++++++ virtio/vsock.c | 4 ++-- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/include/kvm/virtio.h b/include/kvm/virtio.h index 1fa33e5b..95b5142b 100644 --- a/include/kvm/virtio.h +++ b/include/kvm/virtio.h @@ -273,6 +273,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 2b4b3661..f09dd0a4 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) -{ - /* 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); -} - 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); + 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 27cb3798..a842290b 100644 --- a/virtio/scsi.c +++ b/virtio/scsi.c @@ -69,8 +69,7 @@ static void notify_status(struct kvm *kvm, void *dev, u32 status) u16 endian = vdev->endian; 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..ea640fa6 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 interprets VIRTIO_F_ACCESS_PLATFORM as meaning there is an + * iotlb. Since this is not the case for kvmtool, mask it. + */ + 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) {