[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2 1/4] pci: add rwlock to pcidevs_lock machinery


  • To: Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>, "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Wei Chen <Wei.Chen@xxxxxxx>
  • Date: Tue, 19 Jul 2022 14:20:26 +0800
  • Arc-authentication-results: i=2; mx.microsoft.com 1; spf=pass (sender ip is 63.35.35.123) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; arc=pass (0 oda=1 ltdi=1 spf=[1,1,smtp.mailfrom=arm.com] dkim=[1,1,header.d=arm.com] dmarc=[1,1,header.from=arm.com])
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none
  • Arc-message-signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=OtRjFiW1AfgmSvZaCYkGvgrGtvipb+RsPJ4GAoae4AU=; b=PawqjOfnMYVarVm47DTFpUn+W0jGmY31fLJpPJ4MBCCCPXuCTBMxGIzKTA/KyhsqnnMLyeRNNFQfg1R1ORmHBY2ZofxdCKSebGoUOU0TCbLEPoDtTyHuFnqBsZvueep9jATdrmuPhsmDyUA2nPSHz5rxD1agb8g1Biz7BbFzrCQRhG5uQBuVkFDqWGVd0AiYuXVyij9h5druZMF1z/BFakdVmRvz1ftH0/ltllMTrFR+hGsDQxUCdGH/L5Rm2i+4lw3Q9hdVUO5AMSObeUojLejoX0DxdCxWtznUW+DxYtmaO6Ld4vXbe5XziaJfovl1l5sQKNW8gUesUFEtj+9SIA==
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=OtRjFiW1AfgmSvZaCYkGvgrGtvipb+RsPJ4GAoae4AU=; b=RN3zFiPAXo6Ees+nVpklzg8R3BsdwckSgYKXe35Qc8jGGubL9Qp92Adpp1UyCMN8hxtlhn3GSdeIMCCQAGnsO2pMsnjDevSH7iqz/5HARMx4UuhrSzbji7cb1KNwYL2/6yyUljs/ZKx/hzBaROwbL8m8C6PvO26YMrIrlxFrSgtE3ywcK1lH6TdcswDPoeOxkg94Niegs2JoS26hMb+HX8ipVGDmXEkS0kXFSBeH2PiyxQ/wUqmQMg6NXs6Ih6YKqg2CfmgteatlgLrs0BwakgYQ0Yji8Emw3eyo7jgSLz8TiCXLR/+pUsdZKcaX2iCAbCkTfC/YETkJkcj5MxpCzw==
  • Arc-seal: i=2; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=pass; b=hyucavG9sL5B23ZBgVWFiWrc7IK1TYO+pEoM+HbBXNo4xLeq6cmXkOPLi3Go4PAlyRJIrMO/XK3uNsGhyijzHfl81TzBYh/KQ/QxxuPHEjasNKC9MzZ3Vw/fxCVTm0N9UD4X15K8XVPSM1mA/HUIlqxQ8t515L3Giu54P8f4XJS3qASPERcAwxboGFNTSU5wJiSakAF+ZvAqoOT+KmDOKK1DgL62lmPfPi+9HXDRZSxHiBdrbPYvThCSltiASecqSBqjLsF0m8tIF2RqBfA+G0OhINDXauWdeyf2/mBBO3bycu7KmsVbPQMVdKyrkYvXWLOvLjd2R6EEz4TzEqF10A==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h67VteHR6Sp+f+BHoAXvruTJLMNYDlgN44IPF9jOw4+Zr7LI/k38stZvP7tLwKQN1MNXaW5aw+PPLTwkDS9OpcVmPeGhJT675jfiFdZlQ4fERH71QCCJ5VEB4Y+GrglZRTX6AKsh6EJwfwO2VQnZkI3qZlKWmCb40PQbFz4gfruULaYjLNkYV1sLq8ABt2fNBHdj8n6mw6rOHaKNFLfrhv1VnBA4lnVHEzcjeZEfh27cyI2hU6KekAsvFk7Ee1oYYDQPbtvQy6EVCwrCIw9IZZEKRum4yfFhsBjD6s6/rfQNi5SjnArzE37ZCx8ahMGiUxyH65YS9c+ASl7d9RFb3Q==
  • Authentication-results-original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com;
  • Cc: Oleksandr Andrushchenko <Oleksandr_Andrushchenko@xxxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, George Dunlap <george.dunlap@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Paul Durrant <paul@xxxxxxx>
  • Delivery-date: Tue, 19 Jul 2022 06:20:58 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Nodisclaimer: true
  • Original-authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com;

Hi Volodymyr,

On 2022/7/19 5:15, Volodymyr Babchuk wrote:
From: Oleksandr Andrushchenko <oleksandr_andrushchenko@xxxxxxxx>

if ( !use_msi )
          return -EOPNOTSUPP;
diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c
index 938821e593..f93922acc8 100644
--- a/xen/drivers/passthrough/pci.c
+++ b/xen/drivers/passthrough/pci.c
@@ -50,21 +50,74 @@ struct pci_seg {
      } bus2bridge[MAX_BUSES];
  };
-static spinlock_t _pcidevs_lock = SPIN_LOCK_UNLOCKED;
+static DEFINE_RWLOCK(_pcidevs_rwlock);
+static DEFINE_PER_CPU(unsigned int, pcidevs_read_cnt);
+static DEFINE_PER_CPU(unsigned int, pcidevs_write_cnt);
void pcidevs_lock(void)
  {
-    spin_lock_recursive(&_pcidevs_lock);
+    pcidevs_write_lock();
  }
void pcidevs_unlock(void)
  {
-    spin_unlock_recursive(&_pcidevs_lock);
+    pcidevs_write_unlock();
  }
-bool_t pcidevs_locked(void)
+bool pcidevs_locked(void)
  {
-    return !!spin_is_locked(&_pcidevs_lock);
+    return pcidevs_write_locked();
+}
+
+void pcidevs_read_lock(void)
+{
+    if ( this_cpu(pcidevs_read_cnt)++ == 0 )
+        read_lock(&_pcidevs_rwlock);
+}
+

For my understanding, if pcidevs_read_cnt > 0, pcidevs_read_lock
will be unblocked.I am not sure if this behavior is consistent with
the original lock? According to my understanding, the original spinlock should be blocked all the time, if the lock is not acquired. Maybe
I have misunderstanding something, I am not very familiar with PCI
subsystem.

Cheers,
Wei Chen

+int pcidevs_read_trylock(void)
+{
+    int ret = 1;
+
+    if ( this_cpu(pcidevs_read_cnt) == 0 )
+        ret = read_trylock(&_pcidevs_rwlock);
+    if ( ret )
+        this_cpu(pcidevs_read_cnt)++;
+
+    return ret;
+}
+
+void pcidevs_read_unlock(void)
+{
+    ASSERT(this_cpu(pcidevs_read_cnt));
+
+    if ( --this_cpu(pcidevs_read_cnt) == 0 )
+        read_unlock(&_pcidevs_rwlock);
+}
+
+bool pcidevs_read_locked(void)
+{
+    /* Write lock implies read lock */
+    return this_cpu(pcidevs_read_cnt) || this_cpu(pcidevs_write_cnt);
+}
+
+void pcidevs_write_lock(void)
+{
+    if ( this_cpu(pcidevs_write_cnt)++ == 0 )
+        write_lock(&_pcidevs_rwlock);
+}
+
+void pcidevs_write_unlock(void)
+{
+    ASSERT(this_cpu(pcidevs_write_cnt));
+
+    if ( --this_cpu(pcidevs_write_cnt) == 0 )
+        write_unlock(&_pcidevs_rwlock);
+}
+
+bool pcidevs_write_locked(void)
+{
+    return rw_is_write_locked(&_pcidevs_rwlock);
  }
static struct radix_tree_root pci_segments;
@@ -581,7 +634,7 @@ struct pci_dev *pci_get_pdev(int seg, int bus, int devfn)
      struct pci_seg *pseg = get_pseg(seg);
      struct pci_dev *pdev = NULL;
- ASSERT(pcidevs_locked());
+    ASSERT(pcidevs_read_locked());
      ASSERT(seg != -1 || bus == -1);
      ASSERT(bus != -1 || devfn == -1);
diff --git a/xen/include/xen/pci.h b/xen/include/xen/pci.h
index f34368643c..052b2ddf9f 100644
--- a/xen/include/xen/pci.h
+++ b/xen/include/xen/pci.h
@@ -158,7 +158,15 @@ struct pci_dev {
void pcidevs_lock(void);
  void pcidevs_unlock(void);
-bool_t __must_check pcidevs_locked(void);
+bool __must_check pcidevs_locked(void);
+
+void pcidevs_read_lock(void);
+void pcidevs_read_unlock(void);
+bool __must_check pcidevs_read_locked(void);
+
+void pcidevs_write_lock(void);
+void pcidevs_write_unlock(void);
+bool __must_check pcidevs_write_locked(void);
bool_t pci_known_segment(u16 seg);
  bool_t pci_device_detect(u16 seg, u8 bus, u8 dev, u8 func);



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.