[PATCH v4 4/6] xen/p2m: move xenmem_access_to_p2m_access() to common p2m.c

Penny Zheng posted 6 patches 1 week, 3 days ago
Only 5 patches received!
[PATCH v4 4/6] xen/p2m: move xenmem_access_to_p2m_access() to common p2m.c
Posted by Penny Zheng 1 week, 3 days ago
Memory access and ALTP2M are two seperate features, while both depending on
helper xenmem_access_to_p2m_access(). So it betters lives in common p2m.c,
other than mem_access.c which will be compiled out when VM_EVENT=n && ALTP2M=y.
Guard xenmem_access_to_p2m_access() with VM_EVENT || ALTP2M, otherwise it
will become unreachable when both VM_EVENT=n and ALTP2M=n, and hence
violating Misra rule 2.1
We also need to move declaration from mem_access.h to p2m-common.h
An extra blank line is inserted after each case-block to correct coding
style at the same time.

Signed-off-by: Penny Zheng <Penny.Zheng@amd.com>
Acked-by: Jan Beulich <jbeulich@suse.com>
---
v1 -> v3:
- Guard xenmem_access_to_p2m_access() with VM_EVENT || ALTP2M
- Move declaration from mem_access.h to p2m-common.h
- refine commit message
---
 xen/arch/x86/mm/mem_access.c | 36 --------------------------------
 xen/arch/x86/mm/p2m.c        | 40 ++++++++++++++++++++++++++++++++++++
 xen/include/xen/mem_access.h |  5 -----
 xen/include/xen/p2m-common.h |  3 +++
 4 files changed, 43 insertions(+), 41 deletions(-)

diff --git a/xen/arch/x86/mm/mem_access.c b/xen/arch/x86/mm/mem_access.c
index e6b609064c..e55e53f44c 100644
--- a/xen/arch/x86/mm/mem_access.c
+++ b/xen/arch/x86/mm/mem_access.c
@@ -298,42 +298,6 @@ static int set_mem_access(struct domain *d, struct p2m_domain *p2m,
     return rc;
 }
 
-bool xenmem_access_to_p2m_access(const struct p2m_domain *p2m,
-                                 xenmem_access_t xaccess,
-                                 p2m_access_t *paccess)
-{
-    static const p2m_access_t memaccess[] = {
-#define ACCESS(ac) [XENMEM_access_##ac] = p2m_access_##ac
-        ACCESS(n),
-        ACCESS(r),
-        ACCESS(w),
-        ACCESS(rw),
-        ACCESS(x),
-        ACCESS(rx),
-        ACCESS(wx),
-        ACCESS(rwx),
-        ACCESS(rx2rw),
-        ACCESS(n2rwx),
-        ACCESS(r_pw),
-#undef ACCESS
-    };
-
-    switch ( xaccess )
-    {
-    case 0 ... ARRAY_SIZE(memaccess) - 1:
-        xaccess = array_index_nospec(xaccess, ARRAY_SIZE(memaccess));
-        *paccess = memaccess[xaccess];
-        break;
-    case XENMEM_access_default:
-        *paccess = p2m->default_access;
-        break;
-    default:
-        return false;
-    }
-
-    return true;
-}
-
 /*
  * Set access type for a region of gfns.
  * If gfn == INVALID_GFN, sets the default access type.
diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c
index 759f3273d3..8d34357bcb 100644
--- a/xen/arch/x86/mm/p2m.c
+++ b/xen/arch/x86/mm/p2m.c
@@ -2203,6 +2203,46 @@ void p2m_log_dirty_range(struct domain *d, unsigned long begin_pfn,
     guest_flush_tlb_mask(d, d->dirty_cpumask);
 }
 
+#if defined(CONFIG_VM_EVENT) || defined(CONFIG_ALTP2M)
+bool xenmem_access_to_p2m_access(const struct p2m_domain *p2m,
+                                 xenmem_access_t xaccess,
+                                 p2m_access_t *paccess)
+{
+    static const p2m_access_t memaccess[] = {
+#define ACCESS(ac) [XENMEM_access_##ac] = p2m_access_##ac
+        ACCESS(n),
+        ACCESS(r),
+        ACCESS(w),
+        ACCESS(rw),
+        ACCESS(x),
+        ACCESS(rx),
+        ACCESS(wx),
+        ACCESS(rwx),
+        ACCESS(rx2rw),
+        ACCESS(n2rwx),
+        ACCESS(r_pw),
+#undef ACCESS
+    };
+
+    switch ( xaccess )
+    {
+    case 0 ... ARRAY_SIZE(memaccess) - 1:
+        xaccess = array_index_nospec(xaccess, ARRAY_SIZE(memaccess));
+        *paccess = memaccess[xaccess];
+        break;
+
+    case XENMEM_access_default:
+        *paccess = p2m->default_access;
+        break;
+
+    default:
+        return false;
+    }
+
+    return true;
+}
+#endif /* VM_EVENT || ALTP2M */
+
 /*
  * Local variables:
  * mode: C
diff --git a/xen/include/xen/mem_access.h b/xen/include/xen/mem_access.h
index 4de651038d..8e7d9ea2e3 100644
--- a/xen/include/xen/mem_access.h
+++ b/xen/include/xen/mem_access.h
@@ -73,11 +73,6 @@ typedef enum {
     /* NOTE: Assumed to be only 4 bits right now on x86. */
 } p2m_access_t;
 
-struct p2m_domain;
-bool xenmem_access_to_p2m_access(const struct p2m_domain *p2m,
-                                 xenmem_access_t xaccess,
-                                 p2m_access_t *paccess);
-
 /*
  * Set access type for a region of gfns.
  * If gfn == INVALID_GFN, sets the default access type.
diff --git a/xen/include/xen/p2m-common.h b/xen/include/xen/p2m-common.h
index f0bd9a6b98..bd4169caee 100644
--- a/xen/include/xen/p2m-common.h
+++ b/xen/include/xen/p2m-common.h
@@ -43,5 +43,8 @@ int __must_check check_get_page_from_gfn(struct domain *d, gfn_t gfn,
                                          bool readonly, p2m_type_t *p2mt_p,
                                          struct page_info **page_p);
 
+bool xenmem_access_to_p2m_access(const struct p2m_domain *p2m,
+                                 xenmem_access_t xaccess,
+                                 p2m_access_t *paccess);
 
 #endif /* _XEN_P2M_COMMON_H */
-- 
2.34.1
Re: [PATCH v4 4/6] xen/p2m: move xenmem_access_to_p2m_access() to common p2m.c
Posted by Jan Beulich 6 days, 22 hours ago
On 15.01.2026 10:28, Penny Zheng wrote:
> --- a/xen/arch/x86/mm/p2m.c
> +++ b/xen/arch/x86/mm/p2m.c
> @@ -2203,6 +2203,46 @@ void p2m_log_dirty_range(struct domain *d, unsigned long begin_pfn,
>      guest_flush_tlb_mask(d, d->dirty_cpumask);
>  }
>  
> +#if defined(CONFIG_VM_EVENT) || defined(CONFIG_ALTP2M)
> +bool xenmem_access_to_p2m_access(const struct p2m_domain *p2m,
> +                                 xenmem_access_t xaccess,
> +                                 p2m_access_t *paccess)
> +{
> +    static const p2m_access_t memaccess[] = {
> +#define ACCESS(ac) [XENMEM_access_##ac] = p2m_access_##ac
> +        ACCESS(n),
> +        ACCESS(r),
> +        ACCESS(w),
> +        ACCESS(rw),
> +        ACCESS(x),
> +        ACCESS(rx),
> +        ACCESS(wx),
> +        ACCESS(rwx),
> +        ACCESS(rx2rw),
> +        ACCESS(n2rwx),
> +        ACCESS(r_pw),
> +#undef ACCESS
> +    };
> +
> +    switch ( xaccess )
> +    {
> +    case 0 ... ARRAY_SIZE(memaccess) - 1:
> +        xaccess = array_index_nospec(xaccess, ARRAY_SIZE(memaccess));
> +        *paccess = memaccess[xaccess];
> +        break;
> +
> +    case XENMEM_access_default:
> +        *paccess = p2m->default_access;
> +        break;
> +
> +    default:
> +        return false;
> +    }
> +
> +    return true;
> +}
> +#endif /* VM_EVENT || ALTP2M */
> +
>  /*
>   * Local variables:
>   * mode: C
> diff --git a/xen/include/xen/mem_access.h b/xen/include/xen/mem_access.h
> index 4de651038d..8e7d9ea2e3 100644
> --- a/xen/include/xen/mem_access.h
> +++ b/xen/include/xen/mem_access.h
> @@ -73,11 +73,6 @@ typedef enum {
>      /* NOTE: Assumed to be only 4 bits right now on x86. */
>  } p2m_access_t;
>  
> -struct p2m_domain;
> -bool xenmem_access_to_p2m_access(const struct p2m_domain *p2m,
> -                                 xenmem_access_t xaccess,
> -                                 p2m_access_t *paccess);
> -
>  /*
>   * Set access type for a region of gfns.
>   * If gfn == INVALID_GFN, sets the default access type.
> diff --git a/xen/include/xen/p2m-common.h b/xen/include/xen/p2m-common.h
> index f0bd9a6b98..bd4169caee 100644
> --- a/xen/include/xen/p2m-common.h
> +++ b/xen/include/xen/p2m-common.h
> @@ -43,5 +43,8 @@ int __must_check check_get_page_from_gfn(struct domain *d, gfn_t gfn,
>                                           bool readonly, p2m_type_t *p2mt_p,
>                                           struct page_info **page_p);
>  
> +bool xenmem_access_to_p2m_access(const struct p2m_domain *p2m,
> +                                 xenmem_access_t xaccess,
> +                                 p2m_access_t *paccess);

CI says "no" on both PPC and RISC-V. I wouldn't be surprised of build issues
on Arm or x86 either, seeing that p2m-common.h doesn't (and shouldn't) include
xen/mem_access.h. It's arch/<arch>/include/asm/p2m.h which is responsible for
the inclusion ahead of including p2m-common.h. Question though is: If this is
an x86-only function, why was its decl put in xen/mem_access.h rather than
x86'es asm/mem_access.h. I'll try that before giving up and handing this back
to you, but may I stress again that you please properly test your changes? It
is not the responsibility of the committer to deal with such fallout.

Jan
Re: [PATCH v4 4/6] xen/p2m: move xenmem_access_to_p2m_access() to common p2m.c
Posted by Jason Andryuk 1 week, 3 days ago
On 2026-01-15 04:28, Penny Zheng wrote:
> Memory access and ALTP2M are two seperate features, while both depending on
> helper xenmem_access_to_p2m_access(). So it betters lives in common p2m.c,
> other than mem_access.c which will be compiled out when VM_EVENT=n && ALTP2M=y.
> Guard xenmem_access_to_p2m_access() with VM_EVENT || ALTP2M, otherwise it
> will become unreachable when both VM_EVENT=n and ALTP2M=n, and hence
> violating Misra rule 2.1
> We also need to move declaration from mem_access.h to p2m-common.h
> An extra blank line is inserted after each case-block to correct coding
> style at the same time.
> 
> Signed-off-by: Penny Zheng <Penny.Zheng@amd.com>
> Acked-by: Jan Beulich <jbeulich@suse.com>

Reviewed-by: Jason Andryuk <jason.andryuk@amd.com>
Re: [PATCH v4 4/6] xen/p2m: move xenmem_access_to_p2m_access() to common p2m.c
Posted by Tamas K Lengyel 1 week, 3 days ago
On Thu, Jan 15, 2026 at 10:21 AM Jason Andryuk <jason.andryuk@amd.com>
wrote:

> On 2026-01-15 04:28, Penny Zheng wrote:
> > Memory access and ALTP2M are two seperate features, while both depending
> on
> > helper xenmem_access_to_p2m_access(). So it betters lives in common
> p2m.c,
> > other than mem_access.c which will be compiled out when VM_EVENT=n &&
> ALTP2M=y.
> > Guard xenmem_access_to_p2m_access() with VM_EVENT || ALTP2M, otherwise it
> > will become unreachable when both VM_EVENT=n and ALTP2M=n, and hence
> > violating Misra rule 2.1
> > We also need to move declaration from mem_access.h to p2m-common.h
> > An extra blank line is inserted after each case-block to correct coding
> > style at the same time.
> >
> > Signed-off-by: Penny Zheng <Penny.Zheng@amd.com>
> > Acked-by: Jan Beulich <jbeulich@suse.com>
>
> Reviewed-by: Jason Andryuk <jason.andryuk@amd.com>
>

Acked-by: Tamas K Lengyel <tamas@tklengyel.com>