Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3737600ybb; Tue, 31 Mar 2020 11:01:33 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvRx4y3d2/5PLKNlVMEZdtODsBGTC3MOiyBlvvGf+rRZA/NunSB4cPZz08/TBnUgXPJut0O X-Received: by 2002:a9d:d36:: with SMTP id 51mr14500048oti.308.1585677692935; Tue, 31 Mar 2020 11:01:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585677692; cv=none; d=google.com; s=arc-20160816; b=r3GaVygHp6rLQJCbxEIBPv/NElHCvqgN40bpkTBVdM26LP+cear+sJ9W2oSoOIYsl+ BAj49a3qon10O1Cm9S22e0+CYnUXhkV8OEO8JGYg3g6Ziq7rx2IBJBf4JD+/F/5QUoiM /lsdHRxp0svjOhi0g0LgE09RRvjjYDzS9gAQrzLD3UcAex5D3mTcK1TKyXN+9sJXJl4u mpiNNNJwuGuHTU/i65ezgeYhzbK28RYMsEyDl4dO+hJ/84gYK3tKY4KL4ZWos1688Opz OA8hNn8kIx/e3S0yFqlevafdRg9x8HzU41D42qtZWNIkj3KFigMtcmFn2rjD32GMo+MS RVvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GmCE6DhrsMAeoiyownFN3QMkGrhvSRADlWEuxOzXXBA=; b=W3bCM+aj+c5A5lfz5p+nVK1X1mylZU5QU+NgBVALjnIxzuN1Vh9hEELl2lunVAH/LZ QnKWf+Are7bdR0zhfp37f2SYPO1fzTxYH8Di9yjlK///qXS3poHPFtHyIWtJ7jTsHDFS QBBZ3AIbgCD+viMdightcdsHNvZBLKkNb3xG97hEmB5I8fS0C79URnnK13Sbar1qx98z a2BsY7QD4rU/dlDNTAcV23qtWWbLGClhUhgQmxaFvACdDTngZpOcomVECLKEAz2IaSjz FuQuXJY22wu7HCkdR1+6RmJzGPtIzybWfJ3RHvu+qkaJ1BZEoaTfZaPCTvEfjAc+6GNj PWYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Dkm+VC3/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v17si7893992otq.250.2020.03.31.11.01.19; Tue, 31 Mar 2020 11:01:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Dkm+VC3/"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728275AbgCaSAo (ORCPT + 99 others); Tue, 31 Mar 2020 14:00:44 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:29621 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728236AbgCaSAn (ORCPT ); Tue, 31 Mar 2020 14:00:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585677641; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GmCE6DhrsMAeoiyownFN3QMkGrhvSRADlWEuxOzXXBA=; b=Dkm+VC3/27RcA/FOLvXMqNSPFJ2uagDm5MFpR0PP4IG3hCiYxj9HyYxifGy+PkQ7s53iiE jsmFKYKGEJnn4TUTCwgoiij4cotLHHOi4KCVV5f83lauMnSW6Wbzk78PkdVYfWgKZFxfYv Lhej4rwUpS12hWlmKw1Krb3+qAsRufg= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-311-9oGNtl_VPxagcr5RtCbSqw-1; Tue, 31 Mar 2020 14:00:39 -0400 X-MC-Unique: 9oGNtl_VPxagcr5RtCbSqw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AB38C107ACC7; Tue, 31 Mar 2020 18:00:37 +0000 (UTC) Received: from eperezma.remote.csb (ovpn-112-92.ams2.redhat.com [10.36.112.92]) by smtp.corp.redhat.com (Postfix) with ESMTP id 60E8E98A55; Tue, 31 Mar 2020 18:00:35 +0000 (UTC) From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: "Michael S. Tsirkin" Cc: "linux-kernel@vger.kernel.org" , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , kvm list , Halil Pasic , "virtualization@lists.linux-foundation.org" , Linux Next Mailing List , Cornelia Huck , Stephen Rothwell , Christian Borntraeger Subject: [PATCH v2 7/8] tools/virtio: Add --reset=random Date: Tue, 31 Mar 2020 20:00:05 +0200 Message-Id: <20200331180006.25829-8-eperezma@redhat.com> In-Reply-To: <20200331180006.25829-1-eperezma@redhat.com> References: <20200331180006.25829-1-eperezma@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, it only removes and add backend, but it will reset vq position in future commits. Signed-off-by: Eugenio P=C3=A9rez --- drivers/vhost/test.c | 57 ++++++++++++++++++++++++++++++++++++++ drivers/vhost/test.h | 1 + tools/virtio/virtio_test.c | 44 ++++++++++++++++++++++++++--- 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/test.c b/drivers/vhost/test.c index 58da17af93e9..329a865227ff 100644 --- a/drivers/vhost/test.c +++ b/drivers/vhost/test.c @@ -263,9 +263,62 @@ static int vhost_test_set_features(struct vhost_test= *n, u64 features) return 0; } =20 +static long vhost_test_set_backend(struct vhost_test *n, unsigned index,= int fd) +{ + static void *private_data; + + const bool enable =3D fd !=3D -1; + struct vhost_virtqueue *vq; + int r; + + mutex_lock(&n->dev.mutex); + r =3D vhost_dev_check_owner(&n->dev); + if (r) + goto err; + + if (index >=3D VHOST_TEST_VQ_MAX) { + r =3D -ENOBUFS; + goto err; + } + vq =3D &n->vqs[index]; + mutex_lock(&vq->mutex); + + /* Verify that ring has been setup correctly. */ + if (!vhost_vq_access_ok(vq)) { + r =3D -EFAULT; + goto err_vq; + } + if (!enable) { + vhost_poll_stop(&vq->poll); + private_data =3D vq->private_data; + vq->private_data =3D NULL; + } else { + r =3D vhost_vq_init_access(vq); + vq->private_data =3D private_data; + if (r =3D=3D 0) + r =3D vhost_poll_start(&vq->poll, vq->kick); + } + + mutex_unlock(&vq->mutex); + + if (enable) { + vhost_test_flush_vq(n, index); + } + + mutex_unlock(&n->dev.mutex); + return 0; + +err_vq: + mutex_unlock(&vq->mutex); +err: + mutex_unlock(&n->dev.mutex); + return r; +} + static long vhost_test_ioctl(struct file *f, unsigned int ioctl, unsigned long arg) { + struct vhost_vring_file backend; struct vhost_test *n =3D f->private_data; void __user *argp =3D (void __user *)arg; u64 __user *featurep =3D argp; @@ -277,6 +330,10 @@ static long vhost_test_ioctl(struct file *f, unsigne= d int ioctl, if (copy_from_user(&test, argp, sizeof test)) return -EFAULT; return vhost_test_run(n, test); + case VHOST_TEST_SET_BACKEND: + if (copy_from_user(&backend, argp, sizeof backend)) + return -EFAULT; + return vhost_test_set_backend(n, backend.index, backend.fd); case VHOST_GET_FEATURES: features =3D VHOST_FEATURES; if (copy_to_user(featurep, &features, sizeof features)) diff --git a/drivers/vhost/test.h b/drivers/vhost/test.h index 7dd265bfdf81..822bc4bee03a 100644 --- a/drivers/vhost/test.h +++ b/drivers/vhost/test.h @@ -4,5 +4,6 @@ =20 /* Start a given test on the virtio null device. 0 stops all tests. */ #define VHOST_TEST_RUN _IOW(VHOST_VIRTIO, 0x31, int) +#define VHOST_TEST_SET_BACKEND _IOW(VHOST_VIRTIO, 0x32, int) =20 #endif diff --git a/tools/virtio/virtio_test.c b/tools/virtio/virtio_test.c index 4a2b9d11f287..93d81cd64ba0 100644 --- a/tools/virtio/virtio_test.c +++ b/tools/virtio/virtio_test.c @@ -20,6 +20,7 @@ #include "../../drivers/vhost/test.h" =20 #define RANDOM_BATCH -1 +#define RANDOM_RESET -1 =20 /* Unused */ void *__kmalloc_fake, *__kfree_ignore_start, *__kfree_ignore_end; @@ -46,6 +47,9 @@ struct vdev_info { struct vhost_memory *mem; }; =20 +static const struct vhost_vring_file no_backend =3D { .fd =3D -1 }, + backend =3D { .fd =3D 1 }; + bool vq_notify(struct virtqueue *vq) { struct vq_info *info =3D vq->priv; @@ -155,10 +159,10 @@ static void wait_for_interrupt(struct vdev_info *de= v) } =20 static void run_test(struct vdev_info *dev, struct vq_info *vq, - bool delayed, int batch, int bufs) + bool delayed, int batch, int reset_n, int bufs) { struct scatterlist sl; - long started =3D 0, completed =3D 0; + long started =3D 0, completed =3D 0, next_reset =3D reset_n; long completed_before, started_before; int r, test =3D 1; unsigned len; @@ -171,6 +175,7 @@ static void run_test(struct vdev_info *dev, struct vq= _info *vq, completed_before =3D completed; started_before =3D started; do { + const bool reset =3D reset_n && completed > next_reset; if (random_batch) batch =3D (random() % vq->vring.num) + 1; =20 @@ -200,12 +205,26 @@ static void run_test(struct vdev_info *dev, struct = vq_info *vq, if (started >=3D bufs) r =3D -1; =20 + if (reset) { + r =3D ioctl(dev->control, VHOST_TEST_SET_BACKEND, + &no_backend); + assert(!r); + } + /* Flush out completed bufs if any */ while (virtqueue_get_buf(vq->vq, &len)) { ++completed; r =3D 0; } =20 + if (reset) { + r =3D ioctl(dev->control, VHOST_TEST_SET_BACKEND, + &backend); + assert(!r); + + while (completed > next_reset) + next_reset +=3D completed; + } } while (r =3D=3D 0); if (completed =3D=3D completed_before && started =3D=3D started_before= ) ++spurious; @@ -270,6 +289,11 @@ const struct option longopts[] =3D { .val =3D 'b', .has_arg =3D required_argument, }, + { + .name =3D "reset", + .val =3D 'r', + .has_arg =3D optional_argument, + }, { } }; @@ -282,6 +306,7 @@ static void help(void) " [--no-virtio-1]" " [--delayed-interrupt]" " [--batch=3Drandom/N]" + " [--reset=3Drandom/N]" "\n"); } =20 @@ -290,7 +315,7 @@ int main(int argc, char **argv) struct vdev_info dev; unsigned long long features =3D (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_F_VERSION_1); - long batch =3D 1; + long batch =3D 1, reset =3D 0; int o; bool delayed =3D false; =20 @@ -326,6 +351,17 @@ int main(int argc, char **argv) assert(batch < (long)INT_MAX + 1); } break; + case 'r': + if (!optarg) { + reset =3D 1; + } else if (0 =3D=3D strcmp(optarg, "random")) { + reset =3D RANDOM_RESET; + } else { + reset =3D strtol(optarg, NULL, 10); + assert(reset >=3D 0); + assert(reset < (long)INT_MAX + 1); + } + break; default: assert(0); break; @@ -335,6 +371,6 @@ int main(int argc, char **argv) done: vdev_info_init(&dev, features); vq_info_add(&dev, 256); - run_test(&dev, &dev.vqs[0], delayed, batch, 0x100000); + run_test(&dev, &dev.vqs[0], delayed, batch, reset, 0x100000); return 0; } --=20 2.18.1