diff mbox series

[v4,5/5] tools/libs/ctrl: Simplify xc helpers related to populate_physmap()

Message ID 20240409045357.236802-6-xin.wang2@amd.com (mailing list archive)
State New, archived
Headers show
Series DOMCTL-based guest magic region allocation for 11 domUs | expand

Commit Message

Henry Wang April 9, 2024, 4:53 a.m. UTC
There are currently a lot of xc helpers to call populate_physmap()
in the hypervisor with different parameters, such as:
- xc_domain_{increase, decrease}_{reservation, reservation_exact}
- xc_domain_populate_physmap
- xc_domain_populate_{physmap_exact, physmap_heap_exact}

Most of them share the same and duplicated logic.

Extract the duplicated code of these xc helpers to local helper
functions to simplify the code.

No functional change intended.

Suggested-by: Jan Beulich <jbeulich@suse.com>
Signed-off-by: Henry Wang <xin.wang2@amd.com>
---
v4:
- New patch.
---
 tools/libs/ctrl/xc_domain.c | 178 +++++++++++++-----------------------
 1 file changed, 62 insertions(+), 116 deletions(-)
diff mbox series

Patch

diff --git a/tools/libs/ctrl/xc_domain.c b/tools/libs/ctrl/xc_domain.c
index 82c1554613..d023596bed 100644
--- a/tools/libs/ctrl/xc_domain.c
+++ b/tools/libs/ctrl/xc_domain.c
@@ -935,7 +935,7 @@  static int xc_populate_physmap_cmd(xc_interface *xch,
 
     if ( xc_hypercall_bounce_pre(xch, extent_start) )
     {
-        PERROR("Could not bounce memory for XENMEM_populate_physmap hypercall");
+        PERROR("Could not bounce memory for hypercall %u", cmd);
         return -1;
     }
     set_xen_guest_handle(reservation.extent_start, extent_start);
@@ -946,39 +946,8 @@  static int xc_populate_physmap_cmd(xc_interface *xch,
     return err;
 }
 
-int xc_domain_increase_reservation(xc_interface *xch,
-                                   uint32_t domid,
-                                   unsigned long nr_extents,
-                                   unsigned int extent_order,
-                                   unsigned int mem_flags,
-                                   xen_pfn_t *extent_start)
-{
-    int err;
-    DECLARE_HYPERCALL_BOUNCE(extent_start, nr_extents * sizeof(*extent_start), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
-    struct xen_memory_reservation reservation = {
-        .nr_extents   = nr_extents,
-        .extent_order = extent_order,
-        .mem_flags    = mem_flags,
-        .domid        = domid
-    };
-
-    /* may be NULL */
-    if ( xc_hypercall_bounce_pre(xch, extent_start) )
-    {
-        PERROR("Could not bounce memory for XENMEM_increase_reservation hypercall");
-        return -1;
-    }
-
-    set_xen_guest_handle(reservation.extent_start, extent_start);
-
-    err = xc_memory_op(xch, XENMEM_increase_reservation, &reservation, sizeof(reservation));
-
-    xc_hypercall_bounce_post(xch, extent_start);
-
-    return err;
-}
-
-int xc_domain_increase_reservation_exact(xc_interface *xch,
+static int xc_populate_physmap_cmd_exact(xc_interface *xch,
+                                         unsigned int cmd,
                                          uint32_t domid,
                                          unsigned long nr_extents,
                                          unsigned int extent_order,
@@ -987,58 +956,75 @@  int xc_domain_increase_reservation_exact(xc_interface *xch,
 {
     int err;
 
-    err = xc_domain_increase_reservation(xch, domid, nr_extents,
-                                         extent_order, mem_flags, extent_start);
-
+    err = xc_populate_physmap_cmd(xch, cmd, domid, nr_extents,
+                                  extent_order, mem_flags, extent_start);
     if ( err == nr_extents )
         return 0;
 
     if ( err >= 0 )
     {
-        DPRINTF("Failed allocation for dom %d: "
+        switch ( cmd )
+        {
+        case XENMEM_increase_reservation:
+            DPRINTF("Failed allocation for dom %d: "
                 "%ld extents of order %d, mem_flags %x\n",
                 domid, nr_extents, extent_order, mem_flags);
-        errno = ENOMEM;
+            errno = ENOMEM;
+            break;
+        case XENMEM_decrease_reservation:
+            DPRINTF("Failed deallocation for dom %d: %ld extents of order %d\n",
+                    domid, nr_extents, extent_order);
+            errno = EINVAL;
+            break;
+        case XENMEM_populate_physmap_heap_alloc:
+        case XENMEM_populate_physmap:
+            DPRINTF("Failed allocation for dom %d: %ld extents of order %d\n",
+                    domid, nr_extents, extent_order);
+            errno = EBUSY;
+            break;
+        default:
+            DPRINTF("Invalid cmd %u\n", cmd);
+            errno = EINVAL;
+            break;
+        }
         err = -1;
     }
 
     return err;
 }
 
-int xc_domain_decrease_reservation(xc_interface *xch,
+int xc_domain_increase_reservation(xc_interface *xch,
                                    uint32_t domid,
                                    unsigned long nr_extents,
                                    unsigned int extent_order,
+                                   unsigned int mem_flags,
                                    xen_pfn_t *extent_start)
 {
-    int err;
-    DECLARE_HYPERCALL_BOUNCE(extent_start, nr_extents * sizeof(*extent_start), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
-    struct xen_memory_reservation reservation = {
-        .nr_extents   = nr_extents,
-        .extent_order = extent_order,
-        .mem_flags    = 0,
-        .domid        = domid
-    };
-
-    if ( extent_start == NULL )
-    {
-        DPRINTF("decrease_reservation extent_start is NULL!\n");
-        errno = EINVAL;
-        return -1;
-    }
-
-    if ( xc_hypercall_bounce_pre(xch, extent_start) )
-    {
-        PERROR("Could not bounce memory for XENMEM_decrease_reservation hypercall");
-        return -1;
-    }
-    set_xen_guest_handle(reservation.extent_start, extent_start);
-
-    err = xc_memory_op(xch, XENMEM_decrease_reservation, &reservation, sizeof(reservation));
+    return xc_populate_physmap_cmd(xch, XENMEM_increase_reservation, domid,
+                                   nr_extents, extent_order, mem_flags,
+                                   extent_start);
+}
 
-    xc_hypercall_bounce_post(xch, extent_start);
+int xc_domain_increase_reservation_exact(xc_interface *xch,
+                                         uint32_t domid,
+                                         unsigned long nr_extents,
+                                         unsigned int extent_order,
+                                         unsigned int mem_flags,
+                                         xen_pfn_t *extent_start)
+{
+    return xc_populate_physmap_cmd_exact(xch, XENMEM_increase_reservation,
+                                         domid, nr_extents, extent_order,
+                                         mem_flags, extent_start);
+}
 
-    return err;
+int xc_domain_decrease_reservation(xc_interface *xch,
+                                   uint32_t domid,
+                                   unsigned long nr_extents,
+                                   unsigned int extent_order,
+                                   xen_pfn_t *extent_start)
+{
+    return xc_populate_physmap_cmd(xch, XENMEM_decrease_reservation, domid,
+                                   nr_extents, extent_order, 0, extent_start);
 }
 
 int xc_domain_decrease_reservation_exact(xc_interface *xch,
@@ -1047,23 +1033,9 @@  int xc_domain_decrease_reservation_exact(xc_interface *xch,
                                          unsigned int extent_order,
                                          xen_pfn_t *extent_start)
 {
-    int err;
-
-    err = xc_domain_decrease_reservation(xch, domid, nr_extents,
-                                         extent_order, extent_start);
-
-    if ( err == nr_extents )
-        return 0;
-
-    if ( err >= 0 )
-    {
-        DPRINTF("Failed deallocation for dom %d: %ld extents of order %d\n",
-                domid, nr_extents, extent_order);
-        errno = EINVAL;
-        err = -1;
-    }
-
-    return err;
+    return xc_populate_physmap_cmd_exact(xch, XENMEM_decrease_reservation,
+                                         domid, nr_extents, extent_order,
+                                         0, extent_start);
 }
 
 int xc_domain_add_to_physmap(xc_interface *xch,
@@ -1177,22 +1149,9 @@  int xc_domain_populate_physmap_exact(xc_interface *xch,
                                      unsigned int mem_flags,
                                      xen_pfn_t *extent_start)
 {
-    int err;
-
-    err = xc_domain_populate_physmap(xch, domid, nr_extents,
-                                     extent_order, mem_flags, extent_start);
-    if ( err == nr_extents )
-        return 0;
-
-    if ( err >= 0 )
-    {
-        DPRINTF("Failed allocation for dom %d: %ld extents of order %d\n",
-                domid, nr_extents, extent_order);
-        errno = EBUSY;
-        err = -1;
-    }
-
-    return err;
+    return xc_populate_physmap_cmd_exact(xch, XENMEM_populate_physmap,
+                                         domid, nr_extents, extent_order,
+                                         mem_flags, extent_start);
 }
 
 int xc_domain_populate_physmap_heap_exact(xc_interface *xch,
@@ -1202,23 +1161,10 @@  int xc_domain_populate_physmap_heap_exact(xc_interface *xch,
                                           unsigned int mem_flags,
                                           xen_pfn_t *extent_start)
 {
-    int err;
-
-    err = xc_populate_physmap_cmd(xch, XENMEM_populate_physmap_heap_alloc,
-                                  domid, nr_extents, extent_order, mem_flags,
-                                  extent_start);
-    if ( err == nr_extents )
-        return 0;
-
-    if ( err >= 0 )
-    {
-        DPRINTF("Failed allocation for dom %d: %ld extents of order %d\n",
-                domid, nr_extents, extent_order);
-        errno = EBUSY;
-        err = -1;
-    }
-
-    return err;
+    return xc_populate_physmap_cmd_exact(xch,
+                                         XENMEM_populate_physmap_heap_alloc,
+                                         domid, nr_extents, extent_order,
+                                         mem_flags, extent_start);
 }
 
 int xc_domain_memory_exchange_pages(xc_interface *xch,