Skip to contentSkip to conversation

xen/grants: fix hypercall continuation for GNTTABOP_cache_flush

PATCH In review Juergen Gross 4 participants 7 messages Apr 22, 2020 – Apr 23, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/grants: fix hypercall continuation for GNTTABOP_cache_flush

The GNTTABOP_cache_flush hypercall has a wrong test for hypercall continuation, the test today is:

    if ( rc > 0 || opaque_out != 0 )

Unfortunately this will be true even in case of an error (rc < 0), possibly leading to very long lasting hypercalls (times of more than an hour have been observed in a test case).

Correct the test condition to result in false with rc < 0 and set opaque_out only if no error occurred, to be on the safe side.

---
 xen/common/grant_table.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/common/grant_table.c +3 -3
index 96080b3dec..5ef7ff940d 100644
@@ -3626,12 +3626,12 @@ do_grant_table_op(
         if ( unlikely(!guest_handle_okay(cflush, count)) )
             goto out;
         rc = gnttab_cache_flush(cflush, &opaque_in, count);
-        if ( rc > 0 )
+        if ( rc >= 0 )
         {
             guest_handle_add_offset(cflush, rc);
             uop = guest_handle_cast(cflush, void);
+            opaque_out = opaque_in;
         }
-        opaque_out = opaque_in;
         break;
     }
 
@@ -3641,7 +3641,7 @@ do_grant_table_op(
     }
 
   out:
-    if ( rc > 0 || opaque_out != 0 )
+    if ( rc > 0 || (opaque_out != 0 && rc == 0) )
     {
         /* Adjust rc, see gnttab_copy() for why this is needed. */
         if ( cmd == GNTTABOP_copy )
Signature · 1 line
2.16.4
#1 lore raw

Hi Juergen,

Quoted 38 lines · Juergen Gross

The GNTTABOP_cache_flush hypercall has a wrong test for hypercall continuation, the test today is:

     if ( rc > 0 || opaque_out != 0 )

Unfortunately this will be true even in case of an error (rc < 0), possibly leading to very long lasting hypercalls (times of more than an hour have been observed in a test case).

Correct the test condition to result in false with rc < 0 and set opaque_out only if no error occurred, to be on the safe side.

---
  xen/common/grant_table.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
xen/common/grant_table.c +2 -2
index 96080b3dec..5ef7ff940d 100644
@@ -3626,12 +3626,12 @@ do_grant_table_op(
          if ( unlikely(!guest_handle_okay(cflush, count)) )
              goto out;
          rc = gnttab_cache_flush(cflush, &opaque_in, count);
-        if ( rc > 0 )
+        if ( rc >= 0 )
          {
              guest_handle_add_offset(cflush, rc);
              uop = guest_handle_cast(cflush, void);
+            opaque_out = opaque_in;
          }
-        opaque_out = opaque_in;
          break;
      }
 

xen/common/grant_table.c do_grant_table_op( · line 3644

Quoted 5 lines
xen/common/grant_table.c +1 -1
@@ -3641,7 +3641,7 @@ do_grant_table_op(
      }
 
    out:
-    if ( rc > 0 || opaque_out != 0 )
+    if ( rc > 0 || (opaque_out != 0 && rc == 0) )

I don't understand this change. If you look at the implementation of gnttab_flush() it is not possible to have opaque_out non-zero with rc = 0.

So what's the point of the second condition?

xen/common/grant_table.c do_grant_table_op( · line 3647

Quoted 4 lines
xen/common/grant_table.c +0 -0
      {
          /* Adjust rc, see gnttab_copy() for why this is needed. */
          if ( cmd == GNTTABOP_copy )

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 38 lines · Julien Grall

Hi Juergen,

Quoted 35 lines · Juergen Gross

The GNTTABOP_cache_flush hypercall has a wrong test for hypercall continuation, the test today is:

     if ( rc > 0 || opaque_out != 0 )

Unfortunately this will be true even in case of an error (rc < 0), possibly leading to very long lasting hypercalls (times of more than an hour have been observed in a test case).

Correct the test condition to result in false with rc < 0 and set opaque_out only if no error occurred, to be on the safe side.

---
  xen/common/grant_table.c | 6 +++---

  1 file changed, 3 insertions(+), 3 deletions(-)
xen/common/grant_table.c +2 -2
index 96080b3dec..5ef7ff940d 100644
@@ -3626,12 +3626,12 @@ do_grant_table_op(
           if ( unlikely(!guest_handle_okay(cflush, count)) )
               goto out;
           rc = gnttab_cache_flush(cflush, &opaque_in, count);
-        if ( rc > 0 )
+        if ( rc >= 0 )
           {
               guest_handle_add_offset(cflush, rc);
               uop = guest_handle_cast(cflush, void);
+            opaque_out = opaque_in;
           }
-        opaque_out = opaque_in;
           break;
Quoted 8 lines
Quoted 6 lines
xen/common/grant_table.c +0 -0
       }
@@ -3641,7 +3641,7 @@ do_grant_table_op(
       }

    out:

-    if ( rc > 0 || opaque_out != 0 )
+    if ( rc > 0 || (opaque_out != 0 && rc == 0) )

I don't understand this change. If you look at the implementation of gnttab_flush() it is not possible to have opaque_out non-zero with rc = 0.

Why not?

In gnttab_cache_flush() we have:

if ( hypercall_preempt_check() )

      return i;

i will be 0 in the first loop iteration.

Juergen

Reviewed-by
#3 lore raw
In reply to Julien Grall
Quoted 38 lines · Julien Grall

Hi Juergen,

Quoted 35 lines · Juergen Gross

The GNTTABOP_cache_flush hypercall has a wrong test for hypercall continuation, the test today is:

     if ( rc > 0 || opaque_out != 0 )

Unfortunately this will be true even in case of an error (rc < 0), possibly leading to very long lasting hypercalls (times of more than an hour have been observed in a test case).

Correct the test condition to result in false with rc < 0 and set opaque_out only if no error occurred, to be on the safe side.

---
  xen/common/grant_table.c | 6 +++---

  1 file changed, 3 insertions(+), 3 deletions(-)
xen/common/grant_table.c +2 -2
index 96080b3dec..5ef7ff940d 100644
@@ -3626,12 +3626,12 @@ do_grant_table_op(
           if ( unlikely(!guest_handle_okay(cflush, count)) )
               goto out;
           rc = gnttab_cache_flush(cflush, &opaque_in, count);
-        if ( rc > 0 )
+        if ( rc >= 0 )
           {
               guest_handle_add_offset(cflush, rc);
               uop = guest_handle_cast(cflush, void);
+            opaque_out = opaque_in;
           }
-        opaque_out = opaque_in;
           break;
Quoted 8 lines
Quoted 6 lines
xen/common/grant_table.c +0 -0
       }
@@ -3641,7 +3641,7 @@ do_grant_table_op(
       }

    out:

-    if ( rc > 0 || opaque_out != 0 )
+    if ( rc > 0 || (opaque_out != 0 && rc == 0) )

I don't understand this change. If you look at the implementation of gnttab_flush() it is not possible to have opaque_out non-zero with rc = 0.

Hmmm... I misread the code and missed the:

if ( hypercall_preempt_check() )
return i;

Sorry for the noise.

I am also assuming this want a Fixes tag on 18e8d22fe750c8c7b2830fa37961352693425cf1 "introduce GNTTABOP_cache_flush".

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw
In reply to Juergen Gross
Quoted 18 lines · Juergen Gross
xen/common/grant_table.c +2 -2
@@ -3626,12 +3626,12 @@ do_grant_table_op(
         if ( unlikely(!guest_handle_okay(cflush, count)) )
             goto out;
         rc = gnttab_cache_flush(cflush, &opaque_in, count);
-        if ( rc > 0 )
+        if ( rc >= 0 )
         {
             guest_handle_add_offset(cflush, rc);
             uop = guest_handle_cast(cflush, void);
+            opaque_out = opaque_in;
         }
-        opaque_out = opaque_in;
         break;
     }
 

xen/common/grant_table.c do_grant_table_op( · line 3644

Quoted 5 lines
xen/common/grant_table.c +1 -1
@@ -3641,7 +3641,7 @@ do_grant_table_op(
     }
 
   out:
-    if ( rc > 0 || opaque_out != 0 )
+    if ( rc > 0 || (opaque_out != 0 && rc == 0) )

I disagree with this part - opaque_out shouldn't end up non-zero when rc < 0, and it won't anymore with the change in the earlier hunk.

Jan

Reviewed-by
#5 lore raw
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
xen/common/grant_table.c +2 -2
@@ -3626,12 +3626,12 @@ do_grant_table_op(
         if ( unlikely(!guest_handle_okay(cflush, count)) )
             goto out;
         rc = gnttab_cache_flush(cflush, &opaque_in, count);
-        if ( rc > 0 )
+        if ( rc >= 0 )
         {
             guest_handle_add_offset(cflush, rc);
             uop = guest_handle_cast(cflush, void);
+            opaque_out = opaque_in;
         }
-        opaque_out = opaque_in;
         break;
     }
 
@@ -3641,7 +3641,7 @@ do_grant_table_op(
     }
 
Quoted 7 lines

xen/common/grant_table.c do_grant_table_op( · line 3644

Quoted 3 lines
xen/common/grant_table.c +1 -1
   out:
-    if ( rc > 0 || opaque_out != 0 )
+    if ( rc > 0 || (opaque_out != 0 && rc == 0) )

I disagree with this part - opaque_out shouldn't end up non-zero when rc < 0, and it won't anymore with the change in the earlier hunk.

But opaque_out could end up being non-zero when rc == 0. I think it is a good improvement that we explicitly prevent rc < 0 from entering this if condition. This is why this new version of the patch is my favorite:
it is the one that leads to the code most robust.

That said, as I mentioned before, I would be OK even without the last part because it would still be enough to fix the bug.

#6 lore raw
Quoted 30 lines · Stefano Stabellini
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
xen/common/grant_table.c +2 -2
@@ -3626,12 +3626,12 @@ do_grant_table_op(
         if ( unlikely(!guest_handle_okay(cflush, count)) )
             goto out;
         rc = gnttab_cache_flush(cflush, &opaque_in, count);
-        if ( rc > 0 )
+        if ( rc >= 0 )
         {
             guest_handle_add_offset(cflush, rc);
             uop = guest_handle_cast(cflush, void);
+            opaque_out = opaque_in;
         }
-        opaque_out = opaque_in;
         break;
     }
 
@@ -3641,7 +3641,7 @@ do_grant_table_op(
     }
 
Quoted 7 lines

xen/common/grant_table.c do_grant_table_op( · line 3644

Quoted 3 lines
xen/common/grant_table.c +1 -1
   out:
-    if ( rc > 0 || opaque_out != 0 )
+    if ( rc > 0 || (opaque_out != 0 && rc == 0) )

I disagree with this part - opaque_out shouldn't end up non-zero when rc < 0, and it won't anymore with the change in the earlier hunk.

Quoted 1 line

But opaque_out could end up being non-zero when rc == 0.

Which is the case the original code meant to deal with. (I still think it is unfortunate behavior of the cache-flush implementation to assign meaning other than "success, done" to rc == 0.)

Quoted 6 lines

I think it is a
good improvement that we explicitly prevent rc < 0 from entering this if condition. This is why this new version of the patch is my favorite:
it is the one that leads to the code most robust.

Well, looks like I'm outvoted then. Nevertheless thanks ...

Quoted 2 lines

That said, as I mentioned before, I would be OK even without the last part because it would still be enough to fix the bug.

.. for also clarifying this.

Jan