All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 for-4.5] libxl: account for romfile memory
@ 2014-11-25 12:43 Stefano Stabellini
  2014-11-25 14:24 ` Ian Campbell
  2014-11-26 20:17 ` Don Slutz
  0 siblings, 2 replies; 13+ messages in thread
From: Stefano Stabellini @ 2014-11-25 12:43 UTC (permalink / raw)
  To: xen-devel
  Cc: Wei Liu, Ian Campbell, stefano.stabellini, Don Slutz, hanyandong

Account for the extra memory needed for the rom files of any emulated nics:
QEMU uses xc_domain_populate_physmap_exact to allocate the memory for
each them. Assume 256K each.

This patch fixes a QEMU abort() when more than 4 emulated nics are
assigned to a VM.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
CC: Don Slutz <dslutz@verizon.com>
CC: hanyandong <hanyandong@iie.ac.cn>
CC: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
CC: Ian Campbell <Ian.Campbell@citrix.com>
CC: Wei Liu <wei.liu2@citrix.com>

---

Changes in v2:
- remove double return statement;
- check for return errors;
- check for overflows.
---
 tools/libxl/libxl.c          |   53 +++++++++++++++++++++++++++++++++++++-----
 tools/libxl/libxl_dom.c      |    8 +++++--
 tools/libxl/libxl_internal.h |    7 ++++++
 3 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index de23fec..2cdb768 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -4527,13 +4527,40 @@ out:
 
 /******************************************************************************/
 
+int libxl__get_rom_memory_kb(libxl__gc *gc, uint32_t domid, libxl_domain_config *d_config)
+{
+    int i, romsize, rc;
+    libxl_domain_config local_d_config;
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+
+    if (d_config == NULL) {
+        libxl_domain_config_init(&local_d_config);
+        rc = libxl_retrieve_domain_configuration(ctx, domid, &local_d_config);
+        if (rc < 0)
+            return rc;
+        d_config = &local_d_config;
+    }
+
+    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV)
+        return 0;
+
+    for (i = 0, romsize = 0;
+         i < d_config->num_nics && romsize < INT_MAX;
+         i++) {
+        if (d_config->nics[i].nictype == LIBXL_NIC_TYPE_VIF_IOEMU)
+            romsize += LIBXL_ROMSIZE_KB;
+    }
+
+    return romsize;
+}
+
 int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
 {
     GC_INIT(ctx);
     char *mem, *endptr;
     uint32_t memorykb;
     char *dompath = libxl__xs_get_dompath(gc, domid);
-    int rc = 1;
+    int rc = 1, romsize;
 
     mem = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/memory/target", dompath));
     if (!mem) {
@@ -4550,11 +4577,18 @@ int libxl_domain_setmaxmem(libxl_ctx *ctx, uint32_t domid, uint32_t max_memkb)
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "memory_static_max must be greater than or or equal to memory_dynamic_max\n");
         goto out;
     }
-    rc = xc_domain_setmaxmem(ctx->xch, domid, max_memkb + LIBXL_MAXMEM_CONSTANT);
+    rc = libxl__get_rom_memory_kb(gc, domid, NULL);
+    if (rc < 0)
+        goto out;
+    romsize = rc;
+    rc = xc_domain_setmaxmem(ctx->xch, domid,
+                             max_memkb + LIBXL_MAXMEM_CONSTANT
+                             + romsize);
     if (rc != 0) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
                 "xc_domain_setmaxmem domid=%d memkb=%d failed "
-                "rc=%d\n", domid, max_memkb + LIBXL_MAXMEM_CONSTANT, rc);
+                "rc=%d\n", domid, max_memkb + LIBXL_MAXMEM_CONSTANT +
+                romsize, rc);
         goto out;
     }
 
@@ -4683,7 +4717,7 @@ int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid,
         int32_t target_memkb, int relative, int enforce)
 {
     GC_INIT(ctx);
-    int rc = 1, abort_transaction = 0;
+    int rc = 1, abort_transaction = 0, romsize;
     uint32_t memorykb = 0, videoram = 0;
     uint32_t current_target_memkb = 0, new_target_memkb = 0;
     uint32_t current_max_memkb = 0;
@@ -4769,12 +4803,19 @@ retry_transaction:
 
     if (enforce) {
         memorykb = new_target_memkb;
+        rc = libxl__get_rom_memory_kb(gc, domid, NULL);
+        if (rc < 0) {
+            abort_transaction = 1;
+            goto out;
+        }
+        romsize = rc;
         rc = xc_domain_setmaxmem(ctx->xch, domid, memorykb +
-                LIBXL_MAXMEM_CONSTANT);
+                LIBXL_MAXMEM_CONSTANT + romsize);
         if (rc != 0) {
             LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR,
                     "xc_domain_setmaxmem domid=%d memkb=%d failed "
-                    "rc=%d\n", domid, memorykb + LIBXL_MAXMEM_CONSTANT, rc);
+                    "rc=%d\n", domid, memorykb + LIBXL_MAXMEM_CONSTANT +
+                    romsize, rc);
             abort_transaction = 1;
             goto out;
         }
diff --git a/tools/libxl/libxl_dom.c b/tools/libxl/libxl_dom.c
index 74ea84b..733f4c7 100644
--- a/tools/libxl/libxl_dom.c
+++ b/tools/libxl/libxl_dom.c
@@ -305,7 +305,7 @@ int libxl__build_pre(libxl__gc *gc, uint32_t domid,
     libxl_domain_build_info *const info = &d_config->b_info;
     libxl_ctx *ctx = libxl__gc_owner(gc);
     char *xs_domid, *con_domid;
-    int rc;
+    int rc, romsize;
 
     if (xc_domain_max_vcpus(ctx->xch, domid, info->max_vcpus) != 0) {
         LOG(ERROR, "Couldn't set max vcpu count");
@@ -405,8 +405,12 @@ int libxl__build_pre(libxl__gc *gc, uint32_t domid,
         }
     }
 
+	rc = libxl__get_rom_memory_kb(gc, domid, d_config);
+	if (rc < 0)
+		return rc;
+	romsize = rc;
     if (xc_domain_setmaxmem(ctx->xch, domid, info->target_memkb +
-        LIBXL_MAXMEM_CONSTANT) < 0) {
+        LIBXL_MAXMEM_CONSTANT + romsize) < 0) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Couldn't set max memory");
         return ERROR_FAIL;
     }
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 4361421..33826ea 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -90,6 +90,7 @@
 #define LIBXL_XENCONSOLE_LIMIT 1048576
 #define LIBXL_XENCONSOLE_PROTOCOL "vt100"
 #define LIBXL_MAXMEM_CONSTANT 1024
+#define LIBXL_ROMSIZE_KB 256
 #define LIBXL_PV_EXTRA_MEMORY 1024
 #define LIBXL_HVM_EXTRA_MEMORY 2048
 #define LIBXL_MIN_DOM0_MEM (128*1024)
@@ -1023,6 +1024,12 @@ _hidden char * libxl__domain_pvcontrol_read(libxl__gc *gc,
 _hidden int libxl__domain_pvcontrol_write(libxl__gc *gc, xs_transaction_t t,
                                           uint32_t domid, const char *cmd);
 
+/* Returns the amount of extra mem required to allocate roms or an libxl
+ * error code on error.
+ * The *d_config parameter is optional.
+ */
+_hidden int libxl__get_rom_memory_kb(libxl__gc *gc, uint32_t domid, libxl_domain_config *d_config);
+
 /* from xl_device */
 _hidden char *libxl__device_disk_string_of_backend(libxl_disk_backend backend);
 _hidden char *libxl__device_disk_string_of_format(libxl_disk_format format);
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2015-01-08 13:52 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-25 12:43 [PATCH v2 for-4.5] libxl: account for romfile memory Stefano Stabellini
2014-11-25 14:24 ` Ian Campbell
2014-11-25 16:49   ` Stefano Stabellini
2014-11-25 16:56     ` Ian Campbell
2014-11-25 17:04       ` Wei Liu
2014-11-25 17:05       ` Stefano Stabellini
2014-11-26  9:32         ` Ian Campbell
2014-11-26 12:39           ` Stefano Stabellini
2014-11-26 13:04             ` Ian Campbell
2014-11-26 14:40               ` Konrad Rzeszutek Wilk
2014-11-26 20:05                 ` Don Slutz
2015-01-08 13:52     ` Ian Campbell
2014-11-26 20:17 ` Don Slutz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.