[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 2/2] Xen/gnttab: improve error handling in gnttab_dma_{alloc,free}_pages()



From: Jan Beulich <jbeulich@xxxxxxxx>

Both decrease-reservation and populate-physmap can succeed partially. For
the former this may be pretty unlikely, but clearly the latter can hit
both out-of-memory conditions in the hypervisor or denial because of the
allocation exceeding the domain's allowance (there's no interaction with
the balloon driver here either).

In gnttab_dma_free_pages() we simply can't give back to the system what
hasn't been re-filled with backing memory. For gnttab_dma_alloc_pages()
both parts of the overall region need dealing with differently.

While no present caller of gnttab_dma_free_pages() checks its return
value, also don't use -EFAULT there: It really is an out-of-memory
condition. (In gnttab_dma_alloc_pages() -EFAULT doesn't look quite right
either, but I couldn't think of a clearly better error code there.)

Fixes: 9bdc7304f536 ("xen/grant-table: Allow allocating buffers suitable for 
DMA")
Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
Signed-off-by: Juergen Gross <jgross@xxxxxxxx>
---
 drivers/xen/grant-table.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c
index 56a497987942..2a520080a570 100644
--- a/drivers/xen/grant-table.c
+++ b/drivers/xen/grant-table.c
@@ -1096,6 +1096,24 @@ int gnttab_dma_alloc_pages(struct gnttab_dma_alloc_args 
*args)
        ret = xenmem_reservation_decrease(args->nr_pages, args->frames);
        if (ret != args->nr_pages) {
                pr_debug("Failed to decrease reservation for DMA buffer\n");
+               if (ret >= 0) {
+                       /* Free the part where decrease didn't work. */
+                       size_t done = ret << PAGE_SHIFT;
+
+                       if (args->coherent)
+                               dma_free_coherent(args->dev, size - done,
+                                                 args->vaddr + done,
+                                                 args->dev_bus_addr + done);
+                       else
+                               dma_free_wc(args->dev, size - done,
+                                           args->vaddr + done,
+                                           args->dev_bus_addr + done);
+
+                       if (!ret) /* Nothing else to do? */
+                               return -EFAULT;
+
+                       args->nr_pages = ret;
+               }
                ret = -EFAULT;
                goto fail;
        }
@@ -1132,7 +1150,12 @@ int gnttab_dma_free_pages(struct gnttab_dma_alloc_args 
*args)
        ret = xenmem_reservation_increase(args->nr_pages, args->frames);
        if (ret != args->nr_pages) {
                pr_debug("Failed to increase reservation for DMA buffer\n");
-               ret = -EFAULT;
+               /* Can only free what has been re-filled. */
+               if (!ret)
+                       return -ENOMEM;
+               if (ret > 0)
+                       args->nr_pages = ret;
+               ret = -ENOMEM;
        } else {
                ret = 0;
        }
-- 
2.56.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.