Skip to contentSkip to conversation

Arm/P2M: reduce locking in p2m_{alloc,free}_page()

PATCH In review Jan Beulich 2 participants 3 messages Nov 29, 2022 – Dec 3, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Arm/P2M: reduce locking in p2m_{alloc,free}_page()

It is generally preferable to not hold locks around allocation functions. And indeed in the hwdom case there's no point at all to hold the paging lock. Reduce the locked regions to the non-hwdom case, while at the same time arranging for p2m_alloc_page() to have just a single return point.

xen/arch/arm/p2m.c +6 -13
@@ -48,7 +48,6 @@ static struct page_info *p2m_alloc_page(
 {
     struct page_info *pg;
 
-    spin_lock(&d->arch.paging.lock);
     /*
      * For hardware domain, there should be no limit in the number of pages that
      * can be allocated, so that the kernel may take advantage of the extended
@@ -58,34 +57,28 @@ static struct page_info *p2m_alloc_page(
     {
         pg = alloc_domheap_page(NULL, 0);
         if ( pg == NULL )
-        {
             printk(XENLOG_G_ERR "Failed to allocate P2M pages for hwdom.\n");
-            spin_unlock(&d->arch.paging.lock);
-            return NULL;
-        }
     }
     else
     {
+        spin_lock(&d->arch.paging.lock);
         pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
-        if ( unlikely(!pg) )
-        {
-            spin_unlock(&d->arch.paging.lock);
-            return NULL;
-        }
+        spin_unlock(&d->arch.paging.lock);
     }
-    spin_unlock(&d->arch.paging.lock);
 
     return pg;
 }
 
 static void p2m_free_page(struct domain *d, struct page_info *pg)
 {
-    spin_lock(&d->arch.paging.lock);
     if ( is_hardware_domain(d) )
         free_domheap_page(pg);
     else
+    {
+        spin_lock(&d->arch.paging.lock);
         page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
-    spin_unlock(&d->arch.paging.lock);
+        spin_unlock(&d->arch.paging.lock);
+    }
 }
 
 /* Return the size of the pool, in bytes. */
Reviewed-by on v1 1/1
#1 lore raw

Hi Jan,

Quoted 7 lines · Jan Beulich

It is generally preferable to not hold locks around allocation functions. And indeed in the hwdom case there's no point at all to hold the paging lock. Reduce the locked regions to the non-hwdom case, while at the same time arranging for p2m_alloc_page() to have just a single return point.

Cheers,

Quoted 52 lines
xen/arch/arm/p2m.c +6 -13
@@ -48,7 +48,6 @@ static struct page_info *p2m_alloc_page(
  {
      struct page_info *pg;
 
-    spin_lock(&d->arch.paging.lock);
      /*
       * For hardware domain, there should be no limit in the number of pages that
       * can be allocated, so that the kernel may take advantage of the extended
@@ -58,34 +57,28 @@ static struct page_info *p2m_alloc_page(
      {
          pg = alloc_domheap_page(NULL, 0);
          if ( pg == NULL )
-        {
              printk(XENLOG_G_ERR "Failed to allocate P2M pages for hwdom.\n");
-            spin_unlock(&d->arch.paging.lock);
-            return NULL;
-        }
      }
      else
      {
+        spin_lock(&d->arch.paging.lock);
          pg = page_list_remove_head(&d->arch.paging.p2m_freelist);
-        if ( unlikely(!pg) )
-        {
-            spin_unlock(&d->arch.paging.lock);
-            return NULL;
-        }
+        spin_unlock(&d->arch.paging.lock);
      }
-    spin_unlock(&d->arch.paging.lock);
 
      return pg;
  }
 
  static void p2m_free_page(struct domain *d, struct page_info *pg)
  {
-    spin_lock(&d->arch.paging.lock);
      if ( is_hardware_domain(d) )
          free_domheap_page(pg);
      else
+    {
+        spin_lock(&d->arch.paging.lock);
          page_list_add_tail(pg, &d->arch.paging.p2m_freelist);
-    spin_unlock(&d->arch.paging.lock);
+        spin_unlock(&d->arch.paging.lock);
+    }
  }
 
  /* Return the size of the pool, in bytes. */
Signature · 1 line
Julien Grall
#2 lore raw
Quoted 9 lines · Julien Grall

Hi Jan,

Quoted 6 lines · Jan Beulich

It is generally preferable to not hold locks around allocation functions. And indeed in the hwdom case there's no point at all to hold the paging lock. Reduce the locked regions to the non-hwdom case, while at the same time arranging for p2m_alloc_page() to have just a single return point.

Quoted 3 lines
Quoted 1 line

It is now committed.

Cheers,

Signature · 1 line
Julien Grall