lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:	Fri, 11 Jul 2014 13:52:41 +0800
From:	Ethan Zhao <ethan.zhao@...cle.com>
To:	bhelgaas@...gle.com, konrad.wilk@...cle.com,
	boris.ostrovsky@...cle.com, david.vrabel@...rix.com,
	gleb@...nel.org, pbonzini@...hat.com, jeffrey.t.kirsher@...el.com,
	jesse.brandeburg@...el.com, bruce.w.allan@...el.com,
	carolyn.wyborny@...el.com, donald.c.skidmore@...el.com,
	gregory.v.rose@...el.com, alexander.h.duyck@...el.com,
	john.ronciak@...el.com, mitch.a.williams@...el.com,
	alex.williamson@...hat.com
Cc:	linux-pci@...r.kernel.org, kvm@...r.kernel.org,
	linux.nics@...el.com, e1000-devel@...ts.sourceforge.net,
	netdev@...r.kernel.org, linux-kernel@...r.kernel.org,
	ethan.kernel@...il.com, vaughan.cao@...cle.com,
	Ethan Zhao <ethan.zhao@...cle.com>
Subject: [PATCH 2/2 V2] PCI: implement VFs assignment reference counter

Current implementation of helper function pci_vfs_assigned() is a
little complex, to get sum of VFs that assigned to VM, access low
level configuration space register and then loop in traversing
device tree.

This patch introduces an atomic reference counter for VFs those
were assigned to VM in struct pci_sriov. and simplify the code in
pci_vfs_assigned().

v2: reorder the patchset according to the suggestion from
alex.williamson@...hat.com

Signed-off-by: Ethan Zhao <ethan.zhao@...cle.com>
---
 drivers/pci/iov.c |   45 +++++++++++++++++----------------------------
 drivers/pci/pci.h |    1 +
 2 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index c082523..5478a0c 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -382,6 +382,7 @@ found:
 	iov->nres = nres;
 	iov->ctrl = ctrl;
 	iov->total_VFs = total;
+	atomic_set(&iov->VFs_assigned_cnt, 0);
 	iov->offset = offset;
 	iov->stride = stride;
 	iov->pgsz = pgsz;
@@ -603,43 +604,21 @@ int pci_num_vf(struct pci_dev *dev)
 EXPORT_SYMBOL_GPL(pci_num_vf);
 
 /**
- * pci_vfs_assigned - returns number of VFs are assigned to a guest
- * @dev: the PCI device
+ * pci_vfs_assigned - returns number of VFs are assigned to VM
+ * @dev: the physical PCI device that contains the VFs.
  *
- * Returns number of VFs belonging to this device that are assigned to a guest.
+ * Returns number of VFs belonging to this device that are assigned to VM.
  * If device is not a physical function returns 0.
  */
 int pci_vfs_assigned(struct pci_dev *dev)
 {
-	struct pci_dev *vfdev;
-	unsigned int vfs_assigned = 0;
-	unsigned short dev_id;
-
 	/* only search if we are a PF */
 	if (!dev->is_physfn)
 		return 0;
 
-	/*
-	 * determine the device ID for the VFs, the vendor ID will be the
-	 * same as the PF so there is no need to check for that one
-	 */
-	pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
-
-	/* loop through all the VFs to see if we own any that are assigned */
-	vfdev = pci_get_device(dev->vendor, dev_id, NULL);
-	while (vfdev) {
-		/*
-		 * It is considered assigned if it is a virtual function with
-		 * our dev as the physical function and the assigned bit is set
-		 */
-		if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
-		    (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED))
-			vfs_assigned++;
-
-		vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
-	}
-
-	return vfs_assigned;
+	if (dev->sriov)
+		return atomic_read(&dev->sriov->VFs_assigned_cnt);
+	return 0;
 }
 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
 
@@ -650,6 +629,11 @@ EXPORT_SYMBOL_GPL(pci_vfs_assigned);
 void pci_sriov_assign_device(struct pci_dev *pdev)
 {
 	pdev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
+	if (pdev->is_virtfn && !pdev->is_physfn)
+		if (pdev->physfn)
+			if (pdev->physfn->sriov)
+				atomic_inc(&pdev->physfn->sriov->
+					VFs_assigned_cnt);
 }
 EXPORT_SYMBOL_GPL(pci_sriov_assign_device);
 
@@ -660,6 +644,11 @@ EXPORT_SYMBOL_GPL(pci_sriov_assign_device);
 void pci_sriov_deassign_device(struct pci_dev *pdev)
 {
 	pdev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
+	if (pdev->is_virtfn && !pdev->is_physfn)
+		if (pdev->physfn)
+			if (pdev->physfn->sriov)
+				atomic_dec(&pdev->physfn->sriov->
+					VFs_assigned_cnt);
 }
 EXPORT_SYMBOL_GPL(pci_sriov_deassign_device);      
 
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 6bd0822..d17bda2 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -235,6 +235,7 @@ struct pci_sriov {
 	u32 pgsz;		/* page size for BAR alignment */
 	u8 link;		/* Function Dependency Link */
 	u16 driver_max_VFs;	/* max num VFs driver supports */
+	atomic_t VFs_assigned_cnt; /* counter of VFs assigned to VM */
 	struct pci_dev *dev;	/* lowest numbered PF */
 	struct pci_dev *self;	/* this PF */
 	struct mutex lock;	/* lock for VF bus */
-- 
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Powered by blists - more mailing lists