Skip to contentSkip to conversation

xen/x86: hap: Small clean-up/hardening in hap_enable()

PATCH In review Julien Grall 4 participants 12 messages Feb 4, 2020 – Mar 4, 2020

lore
Share & tools
lore
#0 lore raw

v2 cover xen/x86: hap: Small clean-up/hardening in hap_enable()

Hi all,

This series contains a couple of clean-up/hardening for the function hap_enable().

Cheers,

Julien Grall (2):
xen/x86: hap: Fix coding style in hap_enable()
xen/x86: hap: Clean-up and harden hap_enable()

 xen/arch/x86/mm/hap/hap.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
Signature · 7 lines
2.17.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v2 1/2 xen/x86: hap: Fix coding style in hap_enable()

---
Changes in v2:
- Add Roger's reviewed-by

---
 xen/arch/x86/mm/hap/hap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/mm/hap/hap.c +2 -1
index 3d93f3451c..31362a31b6 100644
@@ -473,7 +473,8 @@ int hap_enable(struct domain *d, u32 mode)
             goto out;
     }
 
-    for (i = 0; i < MAX_NESTEDP2M; i++) {
+    for ( i = 0; i < MAX_NESTEDP2M; i++ )
+    {
         rv = p2m_alloc_table(d->arch.nested_p2m[i]);
         if ( rv != 0 )
            goto out;
Signature · 7 lines
2.17.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

PATCH v2 2/2 xen/x86: hap: Clean-up and harden hap_enable()

In reply to Julien Grall

From: Julien Grall <[email protected]>

Unlike shadow_enable(), hap_enable() can only be called once during domain creation and with the mode equal to mode equal to PG_external | PG_translate | PG_refcounts.

If it were called twice, then we might have something interesting problem as the p2m tables would be re-allocated (and therefore all the mappings would be lost).

Add code to sanity check the mode and that the function is only called once. Take the opportunity to an if checking that PG_translate is set.

---

It is not entirely clear when PG_translate was enforced.

I keep the check != 0 because this is consistent with the rest of the file. If we want to omit comparison against 0, then this should be in a separate patches converting the file.

Changes in v2:
- Fix typoes in the commit message
- Use -EEXIST instead of -EINVAL

---
 xen/arch/x86/mm/hap/hap.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
xen/arch/x86/mm/hap/hap.c +11 -7
index 31362a31b6..4974bd13d4 100644
@@ -445,6 +445,13 @@ int hap_enable(struct domain *d, u32 mode)
     unsigned int i;
     int rv = 0;
 
+    if ( mode != (PG_external | PG_translate | PG_refcounts) )
+        return -EINVAL;
+
+    /* The function can only be called once */
+    if ( d->arch.paging.mode != 0 )
+        return -EEXIST;
+
     domain_pause(d);
 
     old_pages = d->arch.paging.hap.total_pages;
@@ -465,13 +472,10 @@ int hap_enable(struct domain *d, u32 mode)
     d->arch.paging.alloc_page = hap_alloc_p2m_page;
     d->arch.paging.free_page = hap_free_p2m_page;
 
-    /* allocate P2m table */
-    if ( mode & PG_translate )
-    {
-        rv = p2m_alloc_table(p2m_get_hostp2m(d));
-        if ( rv != 0 )
-            goto out;
-    }
+    /* allocate P2M table */
+    rv = p2m_alloc_table(p2m_get_hostp2m(d));
+    if ( rv != 0 )
+        goto out;
 
     for ( i = 0; i < MAX_NESTEDP2M; i++ )
     {
Signature · 7 lines
2.17.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

Re: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

Quoted 14 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Julien Grall
Sent: 04 February 2020 13:06
To: [email protected]
Cc: [email protected]; Wei Liu <[email protected]>; George Dunlap <[email protected]>; Andrew Cooper <[email protected]>; Grall, Julien <[email protected]>; Jan Beulich <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

From: Julien Grall <[email protected]>

Quoted 7 lines

Unlike shadow_enable(), hap_enable() can only be called once during domain creation and with the mode equal to mode equal to PG_external | PG_translate | PG_refcounts.

If it were called twice, then we might have something interesting problem as the p2m tables would be re-allocated (and therefore all the mappings would be lost).

There are two tests in p2m_alloc_tabl2: whether the domain has memory allocated, and whether the domain already has a p2m. Can these now be dropped?

Paul

Quoted 61 lines

Add code to sanity check the mode and that the function is only called once. Take the opportunity to an if checking that PG_translate is set.

---

It is not entirely clear when PG_translate was enforced.

I keep the check != 0 because this is consistent with the rest of the file. If we want to omit comparison against 0, then this should be in a separate patches converting the file.

Changes in v2:
- Fix typoes in the commit message
- Use -EEXIST instead of -EINVAL

---
 xen/arch/x86/mm/hap/hap.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
xen/arch/x86/mm/hap/hap.c +11 -8
index 31362a31b6..4974bd13d4 100644
@@ -445,6 +445,13 @@ int hap_enable(struct domain *d, u32 mode)
     unsigned int i;
     int rv = 0;
 
+    if ( mode != (PG_external | PG_translate | PG_refcounts) )
+        return -EINVAL;
+
+    /* The function can only be called once */
+    if ( d->arch.paging.mode != 0 )
+        return -EEXIST;
+
     domain_pause(d);
 
     old_pages = d->arch.paging.hap.total_pages;
@@ -465,13 +472,10 @@ int hap_enable(struct domain *d, u32 mode)
     d->arch.paging.alloc_page = hap_alloc_p2m_page;
     d->arch.paging.free_page = hap_free_p2m_page;
 
-    /* allocate P2m table */
-    if ( mode & PG_translate )
-    {
-        rv = p2m_alloc_table(p2m_get_hostp2m(d));
-        if ( rv != 0 )
-            goto out;
-    }
+    /* allocate P2M table */
+    rv = p2m_alloc_table(p2m_get_hostp2m(d));
+    if ( rv != 0 )
+        goto out;
 
     for ( i = 0; i < MAX_NESTEDP2M; i++ )
     {
--

2.17.1

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

Re: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

Quoted 18 lines · Paul
Quoted 18 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Julien Grall
Sent: 04 February 2020 13:06
To: [email protected]
Cc: [email protected]; Wei Liu <[email protected]>; George Dunlap <[email protected]>; Andrew Cooper <[email protected]>; Grall, Julien <[email protected]>; Jan Beulich <[email protected]>; Roger Pau Monné <[email protected]>
Subject: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

From: Julien Grall <[email protected]>

Unlike shadow_enable(), hap_enable() can only be called once during domain creation and with the mode equal to mode equal to PG_external | PG_translate | PG_refcounts.

Quoted 5 lines
Quoted 3 lines

If it were called twice, then we might have something interesting problem as the p2m tables would be re-allocated (and therefore all the mappings would be lost).

There are two tests in p2m_alloc_tabl2: whether the domain has memory allocated, and whether the domain already has a p2m. Can these now be dropped?

I don't think so. They are still necessary for the shadow page-tables. AFAICT, they are enabled via a DOMCTL.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

Re: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

In reply to Julien Grall

Hi,

Gentle ping.

Cheers,

Quoted 67 lines · Julien Grall

From: Julien Grall <[email protected]>

Unlike shadow_enable(), hap_enable() can only be called once during domain creation and with the mode equal to mode equal to PG_external | PG_translate | PG_refcounts.

If it were called twice, then we might have something interesting problem as the p2m tables would be re-allocated (and therefore all the mappings would be lost).

Add code to sanity check the mode and that the function is only called once. Take the opportunity to an if checking that PG_translate is set.

---

It is not entirely clear when PG_translate was enforced.

I keep the check != 0 because this is consistent with the rest of the file. If we want to omit comparison against 0, then this should be in a separate patches converting the file.

Changes in v2:
- Fix typoes in the commit message
- Use -EEXIST instead of -EINVAL

---
  xen/arch/x86/mm/hap/hap.c | 18 +++++++++++-------
  1 file changed, 11 insertions(+), 7 deletions(-)
xen/arch/x86/mm/hap/hap.c +11 -7
index 31362a31b6..4974bd13d4 100644
@@ -445,6 +445,13 @@ int hap_enable(struct domain *d, u32 mode)
      unsigned int i;
      int rv = 0;
 
+    if ( mode != (PG_external | PG_translate | PG_refcounts) )
+        return -EINVAL;
+
+    /* The function can only be called once */
+    if ( d->arch.paging.mode != 0 )
+        return -EEXIST;
+
      domain_pause(d);
 
      old_pages = d->arch.paging.hap.total_pages;
@@ -465,13 +472,10 @@ int hap_enable(struct domain *d, u32 mode)
      d->arch.paging.alloc_page = hap_alloc_p2m_page;
      d->arch.paging.free_page = hap_free_p2m_page;
 
-    /* allocate P2m table */
-    if ( mode & PG_translate )
-    {
-        rv = p2m_alloc_table(p2m_get_hostp2m(d));
-        if ( rv != 0 )
-            goto out;
-    }
+    /* allocate P2M table */
+    rv = p2m_alloc_table(p2m_get_hostp2m(d));
+    if ( rv != 0 )
+        goto out;
 
      for ( i = 0; i < MAX_NESTEDP2M; i++ )
      {
Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

Re: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

Ping again.

Quoted 71 lines · Julien Grall

Hi,

Gentle ping.

Cheers,

Quoted 63 lines · Julien Grall

From: Julien Grall <[email protected]>

Unlike shadow_enable(), hap_enable() can only be called once during domain creation and with the mode equal to mode equal to PG_external | PG_translate | PG_refcounts.

If it were called twice, then we might have something interesting problem as the p2m tables would be re-allocated (and therefore all the mappings would be lost).

Add code to sanity check the mode and that the function is only called once. Take the opportunity to an if checking that PG_translate is set.

---

It is not entirely clear when PG_translate was enforced.

I keep the check != 0 because this is consistent with the rest of the file. If we want to omit comparison against 0, then this should be in a separate patches converting the file.

     Changes in v2:
         - Fix typoes in the commit message
         - Use -EEXIST instead of -EINVAL
---
  xen/arch/x86/mm/hap/hap.c | 18 +++++++++++-------

  1 file changed, 11 insertions(+), 7 deletions(-)
xen/arch/x86/mm/hap/hap.c +11 -7
index 31362a31b6..4974bd13d4 100644
@@ -445,6 +445,13 @@ int hap_enable(struct domain *d, u32 mode)
       unsigned int i;
       int rv = 0;
+    if ( mode != (PG_external | PG_translate | PG_refcounts) )
+        return -EINVAL;
+
+    /* The function can only be called once */
+    if ( d->arch.paging.mode != 0 )
+        return -EEXIST;
+
       domain_pause(d);
       old_pages = d->arch.paging.hap.total_pages;
@@ -465,13 +472,10 @@ int hap_enable(struct domain *d, u32 mode)
       d->arch.paging.alloc_page = hap_alloc_p2m_page;
       d->arch.paging.free_page = hap_free_p2m_page;
-    /* allocate P2m table */
-    if ( mode & PG_translate )
-    {
-        rv = p2m_alloc_table(p2m_get_hostp2m(d));
-        if ( rv != 0 )
-            goto out;
-    }
+    /* allocate P2M table */
+    rv = p2m_alloc_table(p2m_get_hostp2m(d));
+    if ( rv != 0 )
+        goto out;
       for ( i = 0; i < MAX_NESTEDP2M; i++ )
       {
 
Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#8 lore raw

Re: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

Quoted 1 line · Julien Grall

Ping again.

To be honest, with the recent maintainer change it would probably have been helpful if you had simply re-sent the patch. I did see it back then, but had no comments to make and didn't have the authority to ack it, so simply dropped it from my mailbox seeing that you would be able to commit it yourself eventually.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

Re: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

Quoted 5 lines · Jan Beulich
Quoted 1 line · Julien Grall

Ping again.

To be honest, with the recent maintainer change it would probably have been helpful if you had simply re-sent the patch.

Well, I don't think you can expect a contributor to resend a patch because the maintainers has changed. It would have been nicer if there was an handover with the list of pending patches.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v2 2/2
#10 lore raw

Re: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

In reply to Julien Grall
Quoted 6 lines · Julien Grall

From: Julien Grall <[email protected]>

Unlike shadow_enable(), hap_enable() can only be called once during domain creation and with the mode equal to mode equal to PG_external | PG_translate | PG_refcounts.

Quoted 8 lines

If it were called twice, then we might have something interesting problem as the p2m tables would be re-allocated (and therefore all the mappings would be lost).

Add code to sanity check the mode and that the function is only called once. Take the opportunity to an if checking that PG_translate is set.

preferably with the duplicate words on the second line of the description dropped.

Quoted 3 lines

I keep the check != 0 because this is consistent with the rest of the file. If we want to omit comparison against 0, then this should be in a separate patches converting the file.

I disagree, but not enough to make the ack dependent upon the adjustment.

xen/arch/x86/mm/hap/hap.c int hap_enable(struct domain *d, u32 mode) · line 453

Quoted 12 lines
xen/arch/x86/mm/hap/hap.c +6 -0
@@ -445,6 +445,13 @@ int hap_enable(struct domain *d, u32 mode)
     unsigned int i;
     int rv = 0;
 
+    if ( mode != (PG_external | PG_translate | PG_refcounts) )
+        return -EINVAL;
+
+    /* The function can only be called once */
+    if ( d->arch.paging.mode != 0 )
+        return -EEXIST;

I think if such a comment gets added, it should be unambiguous. The function can be called once per domain, not just once in total.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw

Re: [Xen-devel] [PATCH v2 2/2] xen/x86: hap: Clean-up and harden hap_enable()

Quoted 11 lines · Jan Beulich
Quoted 10 lines · Julien Grall

From: Julien Grall <[email protected]>

Unlike shadow_enable(), hap_enable() can only be called once during domain creation and with the mode equal to mode equal to PG_external | PG_translate | PG_refcounts.

If it were called twice, then we might have something interesting problem as the p2m tables would be re-allocated (and therefore all the mappings would be lost).

Quoted 8 lines
Quoted 4 lines

Add code to sanity check the mode and that the function is only called once. Take the opportunity to an if checking that PG_translate is set.

preferably with the duplicate words on the second line of the description dropped.

I will remove it on commit.

Quoted 13 lines
Quoted 4 lines

I keep the check != 0 because this is consistent with the rest of the file. If we want to omit comparison against 0, then this should be in a separate patches converting the file.

I disagree, but not enough to make the ack dependent upon the adjustment.

Quoted 6 lines
xen/arch/x86/mm/hap/hap.c +0 -0
@@ -445,6 +445,13 @@ int hap_enable(struct domain *d, u32 mode)
      unsigned int i;
      int rv = 0;
 
Quoted 9 lines

xen/arch/x86/mm/hap/hap.c int hap_enable(struct domain *d, u32 mode) · line 453

Quoted 6 lines
xen/arch/x86/mm/hap/hap.c +6 -0
+    if ( mode != (PG_external | PG_translate | PG_refcounts) )
+        return -EINVAL;
+
+    /* The function can only be called once */
+    if ( d->arch.paging.mode != 0 )
+        return -EEXIST;

I think if such a comment gets added, it should be unambiguous. The function can be called once per domain, not just once in total.

I will replace with "The function can only be called one per domain".

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel