virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: mst@redhat.com, jasowang@redhat.com
Cc: axboe@kernel.dk, pbonzini@redhat.com,
	linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
	virtualization@lists.linux-foundation.org
Subject: [PATCH 4/9] virtio_pci: simplify MSI-X setup
Date: Fri, 27 Jan 2017 09:16:32 +0100	[thread overview]
Message-ID: <1485504997-17584-5-git-send-email-hch@lst.de> (raw)
In-Reply-To: <1485504997-17584-1-git-send-email-hch@lst.de>

Try to grab the MSI-X vectors early and fall back to the shared one
before doing lots of allocations.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/virtio/virtio_pci_common.c | 58 +++++++++++++++++++-------------------
 1 file changed, 29 insertions(+), 29 deletions(-)

diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
index 9c4ad7d3f..74ff74c0 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -142,32 +142,39 @@ void vp_del_vqs(struct virtio_device *vdev)
 }
 
 static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
-			      struct virtqueue *vqs[],
-			      vq_callback_t *callbacks[],
-			      const char * const names[],
-			      bool per_vq_vectors)
+		struct virtqueue *vqs[], vq_callback_t *callbacks[],
+		const char * const names[])
 {
 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
 	const char *name = dev_name(&vp_dev->vdev.dev);
 	int i, err = -ENOMEM, allocated_vectors, nvectors;
+	bool shared = false;
 	u16 msix_vec;
 
-	if (per_vq_vectors) {
-		/* Best option: one for change interrupt, one per vq. */
-		nvectors = 1;
-		for (i = 0; i < nvqs; ++i)
-			if (callbacks[i])
-				++nvectors;
-	} else {
-		/* Second best: one for change, shared for all vqs. */
+	nvectors = 1;
+	for (i = 0; i < nvqs; i++) {
+		if (callbacks[i])
+			nvectors++;
+	}
+
+	/* Try one vector per queue first. */
+	err = pci_alloc_irq_vectors(vp_dev->pci_dev, nvectors, nvectors,
+			PCI_IRQ_MSIX);
+	if (err < 0) {
+		/* Fallback to one vector for config, one shared for queues. */
+		shared = true;
 		nvectors = 2;
+		err = pci_alloc_irq_vectors(vp_dev->pci_dev, nvectors, nvectors,
+				PCI_IRQ_MSIX);
+		if (err < 0)
+			return err;
 	}
 
 	vp_dev->msix_vectors = nvectors;
 	vp_dev->msix_names = kmalloc_array(nvectors,
 			sizeof(*vp_dev->msix_names), GFP_KERNEL);
 	if (!vp_dev->msix_names)
-		return err;
+		goto out_free_irq_vectors;
 
 	vp_dev->msix_affinity_masks = kcalloc(nvectors,
 			sizeof(*vp_dev->msix_affinity_masks), GFP_KERNEL);
@@ -180,18 +187,13 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
 			goto out_free_msix_affinity_masks;
 	}
 
-	err = pci_alloc_irq_vectors(vp_dev->pci_dev, nvectors, nvectors,
-			PCI_IRQ_MSIX);
-	if (err < 0)
-		goto out_free_msix_affinity_masks;
-
 	/* Set the vector used for configuration */
 	snprintf(vp_dev->msix_names[0], sizeof(*vp_dev->msix_names),
 		 "%s-config", name);
 	err = request_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_config_changed,
 			0, vp_dev->msix_names[0], vp_dev);
 	if (err)
-		goto out_free_irq_vectors;
+		goto out_free_msix_affinity_masks;
 
 	/* Verify we had enough resources to assign the vector */
 	if (vp_dev->config_vector(vp_dev, 0) == VIRTIO_MSI_NO_VECTOR) {
@@ -241,7 +243,11 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
 		}
 		vp_dev->msix_vector_map[i] = msix_vec;
 
-		if (per_vq_vectors)
+		/*
+		 * Use a different vector for each queue if they are available,
+		 * else share the same vector for all VQs.
+		 */
+		if (!shared)
 			allocated_vectors++;
 	}
 
@@ -254,8 +260,6 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
 	vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
 out_free_config_irq:
 	free_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_dev);
-out_free_irq_vectors:
-	pci_free_irq_vectors(vp_dev->pci_dev);
 out_free_msix_affinity_masks:
 	for (i = 0; i < nvectors; i++) {
 		if (vp_dev->msix_affinity_masks[i])
@@ -264,6 +268,8 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
 	kfree(vp_dev->msix_affinity_masks);
 out_free_msix_names:
 	kfree(vp_dev->msix_names);
+out_free_irq_vectors:
+	pci_free_irq_vectors(vp_dev->pci_dev);
 	return err;
 }
 
@@ -308,15 +314,9 @@ int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
 {
 	int err;
 
-	/* Try MSI-X with one vector per queue. */
-	err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true);
-	if (!err)
-		return 0;
-	/* Fallback: MSI-X with one vector for config, one shared for queues. */
-	err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false);
+	err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names);
 	if (!err)
 		return 0;
-	/* Finally fall back to regular interrupts. */
 	return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names);
 }
 
-- 
2.1.4

  parent reply	other threads:[~2017-01-27  8:16 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-27  8:16 automatic IRQ affinity for virtio V2 Christoph Hellwig
2017-01-27  8:16 ` [PATCH 1/9] virtio_pci: remove struct virtio_pci_vq_info Christoph Hellwig
2017-02-03  7:54   ` Jason Wang
     [not found]   ` <a5568074-d92e-8322-6955-19c640006fda@redhat.com>
2017-02-03  8:22     ` Christoph Hellwig
2017-01-27  8:16 ` [PATCH 2/9] virtio_pci: use shared interrupts for virtqueues Christoph Hellwig
2017-02-03  7:54   ` Jason Wang
     [not found]   ` <da34883b-1069-2cfc-aaa3-6aaf3ee0ebf2@redhat.com>
2017-02-03  8:26     ` Christoph Hellwig
2017-02-03  9:47       ` Jason Wang
2017-02-03  9:52         ` Christoph Hellwig
2017-02-03  9:56           ` Jason Wang
2017-01-27  8:16 ` [PATCH 3/9] virtio_pci: don't duplicate the msix_enable flag in struct pci_dev Christoph Hellwig
2017-02-03  7:56   ` Jason Wang
2017-01-27  8:16 ` Christoph Hellwig [this message]
2017-02-03  7:57   ` [PATCH 4/9] virtio_pci: simplify MSI-X setup Jason Wang
2017-01-27  8:16 ` [PATCH 5/9] virtio: allow drivers to request IRQ affinity when creating VQs Christoph Hellwig
2017-02-03  8:01   ` Jason Wang
2017-01-27  8:16 ` [PATCH 6/9] virtio: provide a method to get the IRQ affinity mask for a virtqueue Christoph Hellwig
2017-02-03  8:02   ` Jason Wang
2017-01-27  8:16 ` [PATCH 7/9] blk-mq: provide a default queue mapping for virtio device Christoph Hellwig
2017-01-27  8:16 ` [PATCH 8/9] virtio_blk: use virtio IRQ affinity Christoph Hellwig
2017-01-27  8:16 ` [PATCH 9/9] virtio_scsi: " Christoph Hellwig
     [not found] <20170205171526.6224-1-hch@lst.de>
2017-02-05 17:15 ` [PATCH 4/9] virtio_pci: simplify MSI-X setup Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1485504997-17584-5-git-send-email-hch@lst.de \
    --to=hch@lst.de \
    --cc=axboe@kernel.dk \
    --cc=jasowang@redhat.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).