Skip to content

Commit

Permalink
Merge pull request #5 from last-genius/private/asultanov/double-unmap
Browse files Browse the repository at this point in the history
gnt: Avoid double unmapping on domain cleanup
  • Loading branch information
last-genius authored Jan 23, 2025
2 parents 41e311b + 24b70bc commit d854ac1
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions gnt/gnttab_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,26 @@ stub_gnttab_unmap (value xgh, value array)
{
CAMLparam2 (xgh, array);
int result;

caml_enter_blocking_section ();
result = xengnttab_unmap (_G (xgh), _M (array)->addr,
_M (array)->len >> XEN_PAGE_SHIFT);
caml_leave_blocking_section ();

if (result != 0)
{
caml_failwith ("Failed to unmap grant");
}
xengnttab_handle* xgt = _G(xgh);
void* start_address = _M(array)->addr;
uint32_t count = _M(array)->len >> XEN_PAGE_SHIFT;
char s[64];

/* Check if this grant hasn't already been unmapped before */
if (start_address) {
caml_enter_blocking_section ();
result = xengnttab_unmap (xgt, start_address, count);
caml_leave_blocking_section ();
/* Avoid double unmapping by NULL-ing the pointer */
_M(array)->addr = NULL;
_M(array)->len = 0;

if (result != 0)
{
int r = snprintf(s, 64, "Failed to unmap grant (errno %d, rc %d)", errno, result);
caml_failwith(s);
}
}

CAMLreturn (Val_unit);
}
Expand All @@ -94,12 +104,13 @@ stub_gnttab_map_fresh (value xgh,
CAMLparam4 (xgh, reference, domid, writable);
CAMLlocal1 (contents);
void *map;
xengnttab_handle* xgt = _G(xgh);
uint32_t domid_c = Int_val(domid);
uint32_t ref = Int_val(reference);
int prot = Bool_val (writable) ? PROT_READ | PROT_WRITE : PROT_READ;

caml_enter_blocking_section ();
map = xengnttab_map_grant_ref (_G (xgh), Int_val (domid),
Int_val (reference),
Bool_val (writable) ? PROT_READ |
PROT_WRITE : PROT_READ);
map = xengnttab_map_grant_ref (xgt, domid_c, ref, prot);
caml_leave_blocking_section ();

if (map == NULL)
Expand Down

0 comments on commit d854ac1

Please sign in to comment.