Skip to contentSkip to conversation

xen/arm: Reduce redundant clear root pages when teardown p2m

PATCH Superseded Henry Wang 2 participants 4 messages Dec 12, 2022 – Dec 13, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Henry Wang

author
#0 lore raw

PATCH 1/1 xen/arm: Reduce redundant clear root pages when teardown p2m

Currently, p2m for a domain will be teardown from two paths:
(1) The normal path when a domain is destroyed.
(2) The arch_domain_destroy() in the failure path of domain creation.

When tearing down p2m from (1), the part to clear and clean the root is only needed to do once rather than for every call of p2m_teardown(). If the p2m teardown is from (2), the clear and clean of the root is unnecessary because the domain is not scheduled.

Therefore, this patch introduces a helper p2m_clear_root_pages() to do the clear and clean of the root, and move this logic outside of p2m_teardown(). With this movement, the page_list_empty(&p2m->pages) check can be dropped.

---
 xen/arch/arm/domain.c          |  5 +++++
 xen/arch/arm/include/asm/p2m.h |  1 +
 xen/arch/arm/p2m.c             | 23 ++++++++++++-----------
 3 files changed, 18 insertions(+), 11 deletions(-)
xen/arch/arm/domain.c +5 -0
index 99577adb6c..29c189aab4 100644
@@ -1022,6 +1022,11 @@ int domain_relinquish_resources(struct domain *d)
             return ret;
 
     PROGRESS(p2m):
+        /*
+         * We are about to free the intermediate page-tables, so clear the
+         * root to prevent any walk to use them.
+         */
+        p2m_clear_root_pages(&d->arch.p2m);
         ret = p2m_teardown(d, true);
         if ( ret )
             return ret;
xen/arch/arm/include/asm/p2m.h +1 -0
index 91df922e1c..bf5183e53a 100644
@@ -281,6 +281,7 @@ int p2m_set_entry(struct p2m_domain *p2m,
 
 bool p2m_resolve_translation_fault(struct domain *d, gfn_t gfn);
 
+void p2m_clear_root_pages(struct p2m_domain *p2m);
 void p2m_invalidate_root(struct p2m_domain *p2m);
 
 /*
xen/arch/arm/p2m.c +12 -11
index 948f199d84..0c942c5923 100644
@@ -1314,6 +1314,18 @@ static void p2m_invalidate_table(struct p2m_domain *p2m, mfn_t mfn)
     p2m->need_flush = true;
 }
 
+void p2m_clear_root_pages(struct p2m_domain *p2m)
+{
+    unsigned int i;
+
+    p2m_write_lock(p2m);
+
+    for ( i = 0; i < P2M_ROOT_PAGES; i++ )
+        clear_and_clean_page(p2m->root + i);
+
+    p2m_write_unlock(p2m);
+}
+
 /*
  * Invalidate all entries in the root page-tables. This is
  * useful to get fault on entry and do an action.
@@ -1698,21 +1710,10 @@ int p2m_teardown(struct domain *d, bool allow_preemption)
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
     unsigned long count = 0;
     struct page_info *pg;
-    unsigned int i;
     int rc = 0;
 
-    if ( page_list_empty(&p2m->pages) )
-        return 0;
-
     p2m_write_lock(p2m);
 
-    /*
-     * We are about to free the intermediate page-tables, so clear the
-     * root to prevent any walk to use them.
-     */
-    for ( i = 0; i < P2M_ROOT_PAGES; i++ )
-        clear_and_clean_page(p2m->root + i);
-
     /*
      * The domain will not be scheduled anymore, so in theory we should
      * not need to flush the TLBs. Do it for safety purpose.
Signature · 1 line
2.25.1
#1 lore raw

Hi Henry,

Quoted 26 lines · Henry Wang

Currently, p2m for a domain will be teardown from two paths:
(1) The normal path when a domain is destroyed.
(2) The arch_domain_destroy() in the failure path of domain creation.

When tearing down p2m from (1), the part to clear and clean the root is only needed to do once rather than for every call of p2m_teardown(). If the p2m teardown is from (2), the clear and clean of the root is unnecessary because the domain is not scheduled.

Therefore, this patch introduces a helper p2m_clear_root_pages() to do the clear and clean of the root, and move this logic outside of p2m_teardown(). With this movement, the page_list_empty(&p2m->pages) check can be dropped.

---
  xen/arch/arm/domain.c          |  5 +++++
  xen/arch/arm/include/asm/p2m.h |  1 +
  xen/arch/arm/p2m.c             | 23 ++++++++++++-----------
  3 files changed, 18 insertions(+), 11 deletions(-)
xen/arch/arm/domain.c +0 -0
index 99577adb6c..29c189aab4 100644

xen/arch/arm/domain.c int domain_relinquish_resources(struct domain *d) · line 1029

Quoted 8 lines
xen/arch/arm/domain.c +5 -0
@@ -1022,6 +1022,11 @@ int domain_relinquish_resources(struct domain *d)
              return ret;
 
      PROGRESS(p2m):
+        /*
+         * We are about to free the intermediate page-tables, so clear the
+         * root to prevent any walk to use them.
+         */
+        p2m_clear_root_pages(&d->arch.p2m);

The commit message suggests this should only be called once. However, you will still end up to call p2m_clear_root_pages() multiple time if p2m_teardown() is preempted.

Quoted 50 lines
xen/arch/arm/domain.c +0 -0
          ret = p2m_teardown(d, true);
          if ( ret )
              return ret;
xen/arch/arm/include/asm/p2m.h +1 -0
index 91df922e1c..bf5183e53a 100644
@@ -281,6 +281,7 @@ int p2m_set_entry(struct p2m_domain *p2m,
 
  bool p2m_resolve_translation_fault(struct domain *d, gfn_t gfn);
 
+void p2m_clear_root_pages(struct p2m_domain *p2m);
  void p2m_invalidate_root(struct p2m_domain *p2m);
 
  /*
xen/arch/arm/p2m.c +12 -4
index 948f199d84..0c942c5923 100644
@@ -1314,6 +1314,18 @@ static void p2m_invalidate_table(struct p2m_domain *p2m, mfn_t mfn)
      p2m->need_flush = true;
  }
 
+void p2m_clear_root_pages(struct p2m_domain *p2m)
+{
+    unsigned int i;
+
+    p2m_write_lock(p2m);
+
+    for ( i = 0; i < P2M_ROOT_PAGES; i++ )
+        clear_and_clean_page(p2m->root + i);
+
+    p2m_write_unlock(p2m);
+}
+
  /*
   * Invalidate all entries in the root page-tables. This is
   * useful to get fault on entry and do an action.
@@ -1698,21 +1710,10 @@ int p2m_teardown(struct domain *d, bool allow_preemption)
      struct p2m_domain *p2m = p2m_get_hostp2m(d);
      unsigned long count = 0;
      struct page_info *pg;
-    unsigned int i;
      int rc = 0;
 
-    if ( page_list_empty(&p2m->pages) )
-        return 0;
-
      p2m_write_lock(p2m);
 

xen/arch/arm/p2m.c int p2m_teardown(struct domain *d, bool allow_preemption) · line 1719

Quoted 10 lines
xen/arch/arm/p2m.c +0 -7
-    /*
-     * We are about to free the intermediate page-tables, so clear the
-     * root to prevent any walk to use them.
-     */
-    for ( i = 0; i < P2M_ROOT_PAGES; i++ )
-        clear_and_clean_page(p2m->root + i);
-
      /*
       * The domain will not be scheduled anymore, so in theory we should
       * not need to flush the TLBs. Do it for safety purpose.

This flush needs to be moved in p2m_clear_root_pages().

Cheers,

Signature · 1 line
Julien Grall
author
#2 lore raw

Hi Julien,

Quoted 14 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Subject: Re: [PATCH] xen/arm: Reduce redundant clear root pages when teardown p2m

Hi Henry,

xen/arch/arm/domain.c int domain_relinquish_resources(struct domain *d) · line 1029

Quoted 6 lines
      PROGRESS(p2m):
xen/arch/arm/domain.c +5 -0
+        /*
+         * We are about to free the intermediate page-tables, so clear the
+         * root to prevent any walk to use them.
+         */
+        p2m_clear_root_pages(&d->arch.p2m);
Quoted 3 lines

The commit message suggests this should only be called once. However, you will still end up to call p2m_clear_root_pages() multiple time if p2m_teardown() is preempted.

Thanks for confirming this, would adding another PROGRESS stage such as PROGRESS(p2m_clean) before PROGRESS(p2m) seem ok to you?

Quoted 23 lines

Review

Quoted 5 lines
          ret = p2m_teardown(d, true);
          if ( ret )
              return ret;
(quoted diff) +0 -0

b/xen/arch/arm/include/asm/p2m.h

xen/arch/arm/p2m.c static void p2m_invalidate_table(struct

Quoted 16 lines

index 91df922e1c..bf5183e53a 100644

xen/arch/arm/include/asm/p2m.h +1 -0
@@ -281,6 +281,7 @@ int p2m_set_entry(struct p2m_domain *p2m,
 
  bool p2m_resolve_translation_fault(struct domain *d, gfn_t gfn);
 
+void p2m_clear_root_pages(struct p2m_domain *p2m);
  void p2m_invalidate_root(struct p2m_domain *p2m);
 
  /*
xen/arch/arm/p2m.c +0 -0
index 948f199d84..0c942c5923 100644
@@ -1314,6 +1314,18 @@ static void p2m_invalidate_table(struct

p2m_domain *p2m, mfn_t mfn)

Quoted 8 lines

xen/arch/arm/p2m.c static void p2m_invalidate_table(struct p2m_domain *p2m, mfn_t mfn)

Quoted 6 lines
xen/arch/arm/p2m.c +0 -0
      p2m->need_flush = true;
  }
 
      /*
       * The domain will not be scheduled anymore, so in theory we should
       * not need to flush the TLBs. Do it for safety purpose.

This flush needs to be moved in p2m_clear_root_pages().

Sure, will move this in v2.

Kind regards,
Henry

Quoted 5 lines

Cheers,

--
Julien Grall

#3 lore raw
Quoted 1 line · Henry Wang

Hi Julien,

Hi,

Quoted 14 lines
Quoted 14 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Subject: Re: [PATCH] xen/arm: Reduce redundant clear root pages when teardown p2m

Hi Henry,

Quoted 6 lines
       PROGRESS(p2m):
xen/arch/arm/domain.c +5 -0
+        /*
+         * We are about to free the intermediate page-tables, so clear the
+         * root to prevent any walk to use them.
+         */
+        p2m_clear_root_pages(&d->arch.p2m);
Quoted 6 lines
Quoted 3 lines

The commit message suggests this should only be called once. However, you will still end up to call p2m_clear_root_pages() multiple time if p2m_teardown() is preempted.

Thanks for confirming this, would adding another PROGRESS stage such as PROGRESS(p2m_clean) before PROGRESS(p2m) seem ok to you?

The would be fine with me.

Cheers,

Signature · 1 line
Julien Grall