Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Liu Yi L <yi.l.liu@intel.com>
To: alex.williamson@redhat.com, kwankhede@nvidia.com
Cc: kevin.tian@intel.com, baolu.lu@linux.intel.com,
	yi.l.liu@intel.com, yi.y.sun@intel.com, joro@8bytes.org,
	jean-philippe.brucker@arm.com, peterx@redhat.com,
	linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Subject: [PATCH v3 08/10] vfio/pci: protect cap/ecap_perm bits alloc/free
Date: Thu, 21 Nov 2019 19:23:45 +0800	[thread overview]
Message-ID: <1574335427-3763-9-git-send-email-yi.l.liu@intel.com> (raw)
In-Reply-To: <1574335427-3763-1-git-send-email-yi.l.liu@intel.com>

This patch add a user numer track for the shared cap/ecap_perms bits,
and the alloc/free will hold a semaphore to protect the operations.
With the changes, first caller of vfio_pci_init_perm_bits() will
initialize the bits. While the last caller of vfio_pci_uninit_perm_bits()
will free the bits. This is a preparation to have multiple cap/ecap_perms
bits users.

Cc: Kevin Tian <kevin.tian@intel.com>
Cc: Lu Baolu <baolu.lu@linux.intel.com>
Suggested-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Liu Yi L <yi.l.liu@intel.com>
---
 drivers/vfio/pci/vfio_pci_config.c | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
index f0891bd..274c993 100644
--- a/drivers/vfio/pci/vfio_pci_config.c
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -36,6 +36,13 @@
 	 (offset >= PCI_ROM_ADDRESS && offset < PCI_ROM_ADDRESS + 4))
 
 /*
+ * vfio_perm_bits_sem: prorects the shared perm_bits alloc/free
+ * vfio_pci_perm_bits_users: tracks the user of the shared perm_bits
+ */
+static DEFINE_SEMAPHORE(vfio_perm_bits_sem);
+static int vfio_pci_perm_bits_users;
+
+/*
  * Lengths of PCI Config Capabilities
  *   0: Removed from the user visible capability list
  *   FF: Variable length
@@ -995,7 +1002,7 @@ static int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
 /*
  * Initialize the shared permission tables
  */
-void vfio_pci_uninit_perm_bits(void)
+static void vfio_pci_uninit_perm_bits_internal(void)
 {
 	free_perm_bits(&cap_perms[PCI_CAP_ID_BASIC]);
 
@@ -1009,10 +1016,30 @@ void vfio_pci_uninit_perm_bits(void)
 	free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
 }
 
+void vfio_pci_uninit_perm_bits(void)
+{
+	down(&vfio_perm_bits_sem);
+
+	if (--vfio_pci_perm_bits_users > 0)
+		goto out;
+
+	vfio_pci_uninit_perm_bits_internal();
+
+out:
+	up(&vfio_perm_bits_sem);
+}
+
 int __init vfio_pci_init_perm_bits(void)
 {
 	int ret;
 
+	down(&vfio_perm_bits_sem);
+
+	if (++vfio_pci_perm_bits_users > 1) {
+		ret = 0;
+		goto out;
+	}
+
 	/* Basic config space */
 	ret = init_pci_cap_basic_perm(&cap_perms[PCI_CAP_ID_BASIC]);
 
@@ -1030,8 +1057,10 @@ int __init vfio_pci_init_perm_bits(void)
 	ecap_perms[PCI_EXT_CAP_ID_VNDR].writefn = vfio_raw_config_write;
 
 	if (ret)
-		vfio_pci_uninit_perm_bits();
+		vfio_pci_uninit_perm_bits_internal();
 
+out:
+	up(&vfio_perm_bits_sem);
 	return ret;
 }
 
-- 
2.7.4


  parent reply	other threads:[~2019-11-22 11:42 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-21 11:23 [PATCH v3 00/10] vfio_pci: wrap pci device as a mediated device Liu Yi L
2019-11-21 11:23 ` [PATCH v3 01/10] vfio_pci: move vfio_pci_is_vga/vfio_vga_disabled to header Liu Yi L
2019-11-21 11:23 ` [PATCH v3 02/10] vfio_pci: refine user config reference in vfio-pci module Liu Yi L
2019-11-21 11:23 ` [PATCH v3 03/10] vfio_pci: refine vfio_pci_driver reference in vfio_pci.c Liu Yi L
2019-12-15 22:46   ` Alex Williamson
2019-12-16 11:59     ` Liu, Yi L
2019-11-21 11:23 ` [PATCH v3 04/10] vfio_pci: make common functions be extern Liu Yi L
2019-11-21 11:23 ` [PATCH v3 05/10] vfio_pci: duplicate vfio_pci.c Liu Yi L
2019-11-21 11:23 ` [PATCH v3 06/10] vfio_pci: shrink vfio_pci_common.c Liu Yi L
2019-11-21 11:23 ` [PATCH v3 07/10] vfio_pci: shrink vfio_pci.c Liu Yi L
2019-11-21 11:23 ` Liu Yi L [this message]
2019-12-15 22:46   ` [PATCH v3 08/10] vfio/pci: protect cap/ecap_perm bits alloc/free Alex Williamson
2019-12-16 11:57     ` Liu, Yi L
2019-12-16 17:42       ` Alex Williamson
2020-01-06  5:29         ` Liu, Yi L
2019-11-21 11:23 ` [PATCH v3 09/10] samples: add vfio-mdev-pci driver Liu Yi L
2019-11-21 11:23 ` [PATCH v3 10/10] samples: refine " Liu Yi L
2019-12-09 11:49 ` [PATCH v3 00/10] vfio_pci: wrap pci device as a mediated device Liu, Yi L

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=1574335427-3763-9-git-send-email-yi.l.liu@intel.com \
    --to=yi.l.liu@intel.com \
    --cc=alex.williamson@redhat.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=jean-philippe.brucker@arm.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwankhede@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterx@redhat.com \
    --cc=yi.y.sun@intel.com \
    /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