Xen/gnttab: improve error handling in gnttab_dma_{alloc,free}_pages()

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: 9bdc7304f5 ("xen/grant-table: Allow allocating buffers suitable for DMA")
Signed-off-by: Jan Beulich <[email protected]>
Reviewed-by: Jan Beulich <[email protected]>
Signed-off-by: Juergen Gross <[email protected]>
Message-ID: <[email protected]>
This commit is contained in:
Jan Beulich
2026-11-13 12:10:17 +01:00
committed by Juergen Gross
parent 4ebb3b8d12
commit 2b66f2cc75
+24 -1
View File
@@ -1097,6 +1097,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;
}
@@ -1133,7 +1151,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;
}