Skip to contentSkip to conversation

xen: fail gnttab_grow_table() in case of missing allocations

PATCH In review Juergen Gross 3 participants 7 messages Sep 29, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen: fail gnttab_grow_table() in case of missing allocations

In case gnttab_grow_table() is being called without grant_table_set_limits() having been called for the domain, e.g. in case of a toolstack error, fail the function instead of crashing the system.

While at it let gnttab_grow_table() return a proper error code instead of 1 for success.

---
V2:
- use gprintk() instead of gdprintk() (Roger Pau Monne)
- let gnttab_grow_table() return an error code (Andrew Cooper)

---
 xen/common/grant_table.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
xen/common/grant_table.c +12 -6
index 71706f5cba..144fa7c50f 100644
@@ -1669,7 +1669,11 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
     struct grant_table *gt = d->grant_table;
     unsigned int i, j;
 
-    ASSERT(gt->active);
+    if ( unlikely(!gt->active) )
+    {
+        gprintk(XENLOG_WARNING, "grant_table_set_limits() call missing.\n");
+        return -ENODEV;
+    }
 
     if ( req_nr_frames < INITIAL_NR_GRANT_FRAMES )
         req_nr_frames = INITIAL_NR_GRANT_FRAMES;
@@ -1710,7 +1714,7 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
         gnttab_create_shared_page(d, gt, i);
     gt->nr_grant_frames = req_nr_frames;
 
-    return 1;
+    return 0;
 
 shared_alloc_failed:
     for ( i = nr_grant_frames(gt); i < req_nr_frames; i++ )
@@ -1726,7 +1730,7 @@ active_alloc_failed:
         gt->active[i] = NULL;
     }
     gdprintk(XENLOG_INFO, "Allocation failure when expanding grant table.\n");
-    return 0;
+    return -ENOMEM;
 }
 
 static int
@@ -1769,8 +1773,10 @@ grant_table_init(struct domain *d, struct grant_table *gt)
         goto out;
 
     /* gnttab_grow_table() allocates a min number of frames, so 0 is okay. */
-    if ( gnttab_grow_table(d, 0) )
-        goto unlock;
+    ret = gnttab_grow_table(d, 0);
+    if (ret)
+        goto out;
+    goto unlock;
 
  no_mem:
     ret = -ENOMEM;
@@ -1841,7 +1847,7 @@ gnttab_setup_table(
     if ( (op.nr_frames > nr_grant_frames(gt) ||
           ((gt->gt_version > 1) &&
            (grant_to_status_frames(op.nr_frames) > nr_status_frames(gt)))) &&
-         !gnttab_grow_table(d, op.nr_frames) )
+         gnttab_grow_table(d, op.nr_frames) )
     {
         gdprintk(XENLOG_INFO,
                  "Expand grant table to %u failed. Current: %u Max: %u\n",
Signature · 7 lines
2.12.3


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Juergen Gross

In case gnttab_grow_table() is being called without grant_table_set_limits() having been called for the domain, e.g. in case of a toolstack error, fail the function instead of crashing the system.

While at it let gnttab_grow_table() return a proper error code instead of 1 for success.

Just some rants below.

xen/common/grant_table.c grant_table_init(struct domain *d, struct grant_table *gt) · line 1779

Quoted 10 lines
xen/common/grant_table.c +4 -2
@@ -1769,8 +1773,10 @@ grant_table_init(struct domain *d, struct grant_table *gt)
         goto out;
 
     /* gnttab_grow_table() allocates a min number of frames, so 0 is okay. */
-    if ( gnttab_grow_table(d, 0) )
-        goto unlock;
+    ret = gnttab_grow_table(d, 0);
+    if (ret)
+        goto out;
+    goto unlock;

I find the above chunk ugly, I usually prefer to avoid using gotos except for if error branches. In any case, this is fine given the current flow, and it's not so different from the original.

Also IMHO 'out' should be named 'error' instead, and it should have an ASSERT(rc); at the beginning.

Thanks, Roger.

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

#2 lore raw
In reply to Juergen Gross
Quoted 22 lines · Juergen Gross

In case gnttab_grow_table() is being called without grant_table_set_limits() having been called for the domain, e.g. in case of a toolstack error, fail the function instead of crashing the system.

While at it let gnttab_grow_table() return a proper error code instead of 1 for success.

---
V2:
- use gprintk() instead of gdprintk() (Roger Pau Monne)
- let gnttab_grow_table() return an error code (Andrew Cooper)

---
 xen/common/grant_table.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
xen/common/grant_table.c +0 -0
index 71706f5cba..144fa7c50f 100644

xen/common/grant_table.c gnttab_grow_table(struct domain *d, unsigned int req_nr_frames) · line 1674

Quoted 7 lines
xen/common/grant_table.c +3 -1
@@ -1669,7 +1669,11 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
     struct grant_table *gt = d->grant_table;
     unsigned int i, j;
 
-    ASSERT(gt->active);
+    if ( unlikely(!gt->active) )
+    {
+        gprintk(XENLOG_WARNING, "grant_table_set_limits() call missing.\n");

Unnecessary .

Quoted 24 lines
xen/common/grant_table.c +4 -2
+        return -ENODEV;
+    }
 
     if ( req_nr_frames < INITIAL_NR_GRANT_FRAMES )
         req_nr_frames = INITIAL_NR_GRANT_FRAMES;
@@ -1710,7 +1714,7 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
         gnttab_create_shared_page(d, gt, i);
     gt->nr_grant_frames = req_nr_frames;
 
-    return 1;
+    return 0;
 
 shared_alloc_failed:
     for ( i = nr_grant_frames(gt); i < req_nr_frames; i++ )
@@ -1726,7 +1730,7 @@ active_alloc_failed:
         gt->active[i] = NULL;
     }
     gdprintk(XENLOG_INFO, "Allocation failure when expanding grant table.\n");
-    return 0;
+    return -ENOMEM;
 }
 
 static int

xen/common/grant_table.c grant_table_init(struct domain *d, struct grant_table *gt) · line 1779

Quoted 9 lines
xen/common/grant_table.c +4 -2
@@ -1769,8 +1773,10 @@ grant_table_init(struct domain *d, struct grant_table *gt)
         goto out;
 
     /* gnttab_grow_table() allocates a min number of frames, so 0 is okay. */
-    if ( gnttab_grow_table(d, 0) )
-        goto unlock;
+    ret = gnttab_grow_table(d, 0);
+    if (ret)
+        goto out;
+    goto unlock;

Eww.  This isn't your fault, but the goto lables here are horrible.

Here is a fix.  I'm unsure whether to merge it with yours, or do it as a standalone patch and rebase yours over the top.  Either way, nothing I can't fix while committing.

~Andrew

xen/common/grant_table.c +0 -0
index 144fa7c..c58aa5d 100644
@@ -1736,37 +1736,37 @@ gnttab_grow_table(struct domain *d, unsigned int

req_nr_frames)
 static int
 grant_table_init(struct domain *d, struct grant_table *gt)
 {
-    int ret;
+    int ret = -ENOMEM;

     grant_write_lock(gt);

     if ( gt->active )
     {
         ret = -EBUSY;
-        goto unlock;
+        goto out;
     }

     /* Active grant table. */
     gt->active = xzalloc_array(struct active_grant_entry *, max_nr_active_grant_frames); if ( gt->active == NULL )
-        goto no_mem;
+        goto out;

     /* Tracking of mapped foreign frames table */ gt->maptrack = vzalloc(max_maptrack_frames * sizeof(*gt->maptrack)); if ( gt->maptrack == NULL )
-        goto no_mem;
+        goto out;

     /* Shared grant table. */
     gt->shared_raw = xzalloc_array(void *, max_grant_frames); if ( gt->shared_raw == NULL )
-        goto no_mem;
+        goto out;

     /* Status pages for grant table - for version 2 */ gt->status = xzalloc_array(grant_status_t *, grant_to_status_frames(max_grant_frames)); if ( gt->status == NULL )
-        goto no_mem;
+        goto out;

     ret = gnttab_init_arch(gt);
     if ( ret )
@@ -1774,24 +1774,21 @@ grant_table_init(struct domain *d, struct grant_table *gt)

     /* gnttab_grow_table() allocates a min number of frames, so 0 is okay. */
     ret = gnttab_grow_table(d, 0);
-    if (ret)
-        goto out;
-    goto unlock;

- no_mem:
-    ret = -ENOMEM;
  out:
-    gnttab_destroy_arch(gt);
-    xfree(gt->status);
-    gt->status = NULL;
-    xfree(gt->shared_raw);
-    gt->shared_raw = NULL;
-    vfree(gt->maptrack);
-    gt->maptrack = NULL;
-    xfree(gt->active);
-    gt->active = NULL;
+    if ( ret )
+    {
+        gnttab_destroy_arch(gt);
+        xfree(gt->status);
+        gt->status = NULL;
+        xfree(gt->shared_raw);
+        gt->shared_raw = NULL;
+        vfree(gt->maptrack);
+        gt->maptrack = NULL;
+        xfree(gt->active);
+        gt->active = NULL;
+    }

- unlock:
     grant_write_unlock(gt);

     return ret;

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

#3 lore raw

PATCH v3 1/2 xen/gnttab: Clean up goto tangle in grant_table_init()

In reply to Juergen Gross

No functional change.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>

---
 xen/common/grant_table.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
xen/common/grant_table.c +19 -20
index 71706f5..2e4b5e7 100644
@@ -1732,60 +1732,59 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
 static int
 grant_table_init(struct domain *d, struct grant_table *gt)
 {
-    int ret;
+    int ret = -ENOMEM;
 
     grant_write_lock(gt);
 
     if ( gt->active )
     {
         ret = -EBUSY;
-        goto unlock;
+        goto out;
     }
 
     /* Active grant table. */
     gt->active = xzalloc_array(struct active_grant_entry *,
                                max_nr_active_grant_frames);
     if ( gt->active == NULL )
-        goto no_mem;
+        goto out;
 
     /* Tracking of mapped foreign frames table */
     gt->maptrack = vzalloc(max_maptrack_frames * sizeof(*gt->maptrack));
     if ( gt->maptrack == NULL )
-        goto no_mem;
+        goto out;
 
     /* Shared grant table. */
     gt->shared_raw = xzalloc_array(void *, max_grant_frames);
     if ( gt->shared_raw == NULL )
-        goto no_mem;
+        goto out;
 
     /* Status pages for grant table - for version 2 */
     gt->status = xzalloc_array(grant_status_t *,
                                grant_to_status_frames(max_grant_frames));
     if ( gt->status == NULL )
-        goto no_mem;
+        goto out;
 
     ret = gnttab_init_arch(gt);
     if ( ret )
         goto out;
 
     /* gnttab_grow_table() allocates a min number of frames, so 0 is okay. */
-    if ( gnttab_grow_table(d, 0) )
-        goto unlock;
+    ret = gnttab_grow_table(d, 0) ? 0 : -ENOMEM;
 
- no_mem:
-    ret = -ENOMEM;
  out:
-    gnttab_destroy_arch(gt);
-    xfree(gt->status);
-    gt->status = NULL;
-    xfree(gt->shared_raw);
-    gt->shared_raw = NULL;
-    vfree(gt->maptrack);
-    gt->maptrack = NULL;
-    xfree(gt->active);
-    gt->active = NULL;
+    if ( ret )
+    {
+        gnttab_destroy_arch(gt);
+        xfree(gt->status);
+        gt->status = NULL;
+        xfree(gt->shared_raw);
+        gt->shared_raw = NULL;
+        vfree(gt->maptrack);
+        gt->maptrack = NULL;
+        xfree(gt->active);
+        gt->active = NULL;
+    }
 
- unlock:
     grant_write_unlock(gt);
 
     return ret;
Signature · 7 lines
2.1.4


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

PATCH v3 2/2 xen: fail gnttab_grow_table() in case of missing allocations

From: Juergen Gross <[email protected]>

In case gnttab_grow_table() is being called without grant_table_set_limits() having been called for the domain, e.g. in case of a toolstack error, fail the function instead of crashing the system.

While at it let gnttab_grow_table() return a proper error code instead of 1 for success.

---
V2:
- use gprintk() instead of gdprintk() (Roger Pau Monne)
- let gnttab_grow_table() return an error code (Andrew Cooper)

v3:
* Rebase over new precursor patch.

---
 xen/common/grant_table.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
xen/common/grant_table.c +10 -5
index 2e4b5e7..bbcffe2 100644
@@ -1669,7 +1669,11 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
     struct grant_table *gt = d->grant_table;
     unsigned int i, j;
 
-    ASSERT(gt->active);
+    if ( unlikely(!gt->active) )
+    {
+        gprintk(XENLOG_WARNING, "grant_table_set_limits() call missing\n");
+        return -ENODEV;
+    }
 
     if ( req_nr_frames < INITIAL_NR_GRANT_FRAMES )
         req_nr_frames = INITIAL_NR_GRANT_FRAMES;
@@ -1710,7 +1714,7 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
         gnttab_create_shared_page(d, gt, i);
     gt->nr_grant_frames = req_nr_frames;
 
-    return 1;
+    return 0;
 
 shared_alloc_failed:
     for ( i = nr_grant_frames(gt); i < req_nr_frames; i++ )
@@ -1726,7 +1730,8 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
         gt->active[i] = NULL;
     }
     gdprintk(XENLOG_INFO, "Allocation failure when expanding grant table.\n");
-    return 0;
+
+    return -ENOMEM;
 }
 
 static int
@@ -1769,7 +1774,7 @@ grant_table_init(struct domain *d, struct grant_table *gt)
         goto out;
 
     /* gnttab_grow_table() allocates a min number of frames, so 0 is okay. */
-    ret = gnttab_grow_table(d, 0) ? 0 : -ENOMEM;
+    ret = gnttab_grow_table(d, 0);
 
  out:
     if ( ret )
@@ -1840,7 +1845,7 @@ gnttab_setup_table(
     if ( (op.nr_frames > nr_grant_frames(gt) ||
           ((gt->gt_version > 1) &&
            (grant_to_status_frames(op.nr_frames) > nr_status_frames(gt)))) &&
-         !gnttab_grow_table(d, op.nr_frames) )
+         gnttab_grow_table(d, op.nr_frames) )
     {
         gdprintk(XENLOG_INFO,
                  "Expand grant table to %u failed. Current: %u Max: %u\n",
Signature · 7 lines
2.1.4


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

Re: [PATCH v3 1/2] xen/gnttab: Clean up goto tangle in grant_table_init()

In reply to Andrew Cooper
Quoted 23 lines · Andrew Cooper

No functional change.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>

---
 xen/common/grant_table.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
xen/common/grant_table.c +0 -0
index 71706f5..2e4b5e7 100644
@@ -1732,60 +1732,59 @@ gnttab_grow_table(struct domain *d, unsigned int req_nr_frames)
 static int
 grant_table_init(struct domain *d, struct grant_table *gt)
 {

xen/common/grant_table.c gnttab_grow_table(struct domain *d, unsigned int req_nr_frames) · line 1742

Quoted 10 lines
xen/common/grant_table.c +2 -2
-    int ret;
+    int ret = -ENOMEM;
 
     grant_write_lock(gt);
 
     if ( gt->active )
     {
         ret = -EBUSY;
-        goto unlock;
+        goto out;

That's wrong. In case someone is calling grant_table_init() again you are freeing all resources.

Juergen

Quoted 57 lines
xen/common/grant_table.c +12 -17
     }
 
     /* Active grant table. */
     gt->active = xzalloc_array(struct active_grant_entry *,
                                max_nr_active_grant_frames);
     if ( gt->active == NULL )
-        goto no_mem;
+        goto out;
 
     /* Tracking of mapped foreign frames table */
     gt->maptrack = vzalloc(max_maptrack_frames * sizeof(*gt->maptrack));
     if ( gt->maptrack == NULL )
-        goto no_mem;
+        goto out;
 
     /* Shared grant table. */
     gt->shared_raw = xzalloc_array(void *, max_grant_frames);
     if ( gt->shared_raw == NULL )
-        goto no_mem;
+        goto out;
 
     /* Status pages for grant table - for version 2 */
     gt->status = xzalloc_array(grant_status_t *,
                                grant_to_status_frames(max_grant_frames));
     if ( gt->status == NULL )
-        goto no_mem;
+        goto out;
 
     ret = gnttab_init_arch(gt);
     if ( ret )
         goto out;
 
     /* gnttab_grow_table() allocates a min number of frames, so 0 is okay. */
-    if ( gnttab_grow_table(d, 0) )
-        goto unlock;
+    ret = gnttab_grow_table(d, 0) ? 0 : -ENOMEM;
 
- no_mem:
-    ret = -ENOMEM;
  out:
-    gnttab_destroy_arch(gt);
-    xfree(gt->status);
-    gt->status = NULL;
-    xfree(gt->shared_raw);
-    gt->shared_raw = NULL;
-    vfree(gt->maptrack);
-    gt->maptrack = NULL;
-    xfree(gt->active);
-    gt->active = NULL;
+    if ( ret )
+    {
+        gnttab_destroy_arch(gt);
+        xfree(gt->status);
+        gt->status = NULL;
+        xfree(gt->shared_raw);
+        gt->shared_raw = NULL;

xen/common/grant_table.c gnttab_grow_table(struct domain *d, unsigned int req_nr_frames) · line 1790

Quoted 10 lines
xen/common/grant_table.c +5 -1
+        vfree(gt->maptrack);
+        gt->maptrack = NULL;
+        xfree(gt->active);
+        gt->active = NULL;
+    }
 
- unlock:
     grant_write_unlock(gt);
 
     return ret;

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