Skip to contentSkip to conversation

Allow setting up shared memory areas between VMs from xl config files

RFC PATCH In review Zhongze Liu 2 participants 14 messages Aug 4, 2017 – Aug 9, 2017

lore
Share & tools
lore
#0 lore raw

cover Allow setting up shared memory areas between VMs from xl config files

This series implements the new xl config entry proposed in [1]. Users can use the new config entry to statically setup shared memory areas among VMs so that they could communicate with each other through the static shared memory areas.

[1] Proposla to allow setting up shared memory areas between VMs from xl config files:

    https://lists.xenproject.org/archives/html/xen-devel/2017-07/msg03047.html

Cheers,

Zhongze Liu (4):
libxl: introduce a new structure to represent static shared memory
regions
libxl:xl: add parsing code to parse "libxl_static_sshm" from xl config
files
x86/p2m : remove checks that forbid adding foreign pages between HVM
guests
libxl: support creation and destruction of static shared memory areas

 tools/libxl/Makefile         |   4 +-
 tools/libxl/libxl.h          |   4 +
 tools/libxl/libxl_create.c   |   7 +
 tools/libxl/libxl_dom.c      |   7 +
 tools/libxl/libxl_domain.c   |   6 +
 tools/libxl/libxl_internal.h |  14 ++
 tools/libxl/libxl_sshm.c     | 370 +++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_types.idl  |  28 ++++
 tools/libxl/libxl_xshelp.c   |   8 +
 tools/libxl/libxlu_sshm.c    | 228 ++++++++++++++++++++++++++
 tools/libxl/libxlutil.h      |   6 +
 tools/xl/xl_parse.c          |  24 ++-
 xen/arch/x86/mm/p2m.c        |  20 ++-
 13 files changed, 718 insertions(+), 8 deletions(-)
 create mode 100644 tools/libxl/libxl_sshm.c
 create mode 100644 tools/libxl/libxlu_sshm.c
Signature · 7 lines
2.13.3


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

RFC PATCH 1/4 libxl: introduce a new structure to represent static shared memory regions

Add a new structure to the IDL famliy to represent static shared memory regions, as proposed in the proposal "Allow setting up shared memory areas between VMs from xl config file" (see [1]).

[1] https://lists.xenproject.org/archives/html/xen-devel/2017-07/msg03047.html

---
Cc: Wei Liu <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: [email protected]

---
 tools/libxl/libxl.h         |  4 ++++
 tools/libxl/libxl_types.idl | 28 ++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
tools/libxl/libxl.h +4 -0
index 7cf0f31f68..7c29ebc9e0 100644
@@ -2228,6 +2228,10 @@ int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock);
 int libxl_qemu_monitor_command(libxl_ctx *ctx, uint32_t domid,
                                const char *command_line, char **output);
 
+/* Constants for libxl_static_shm */
+#define LIBXL_SSHM_RANGE_UNKNOWN UINT64_MAX
+#define LIBXL_SSHM_ID_MAXLEN    128
+
 #include <libxl_event.h>
 
 #endif /* LIBXL_H */
tools/libxl/libxl_types.idl +28 -0
index 8a9849c643..1aa59a72a2 100644
@@ -779,6 +779,33 @@ libxl_device_channel = Struct("device_channel", [
            ])),
 ])
 
+libxl_sshm_cachepolicy = Enumeration("sshm_cachepolicy", [
+    (-1, "UNKNOWN"),
+    (0,  "ARM_NORMAL"),  # ARM policies should be between 0 ~ 16
+    (32,  "X86_NORMAL"), # X86 policies should be >= 32
+    ], init_val = "LIBXL_SSHM_CHCHE_POLICY_UNKNOWN")
+
+libxl_sshm_prot = Enumeration("sshm_prot", [
+    (-1, "UNKNOWN"),
+    (3,  "RW"),
+    ], init_val = "LIBXL_SSHM_PROT_UNKNOWN")
+
+libxl_sshm_role = Enumeration("sshm_role", [
+    (-1, "UNKNOWN"),
+    (0,  "MASTER"),
+    (1,  "SLAVE"),
+    ], init_val = "LIBXL_SSHM_ROLE_UNKNOWN")
+
+libxl_static_shm = Struct("static_shm", [
+    ("id", string),
+    ("offset", uint64, {'init_val': 'LIBXL_SSHM_RANGE_UNKNOWN'}),
+    ("begin", uint64, {'init_val': 'LIBXL_SSHM_RANGE_UNKNOWN'}),
+    ("end", uint64, {'init_val': 'LIBXL_SSHM_RANGE_UNKNOWN'}),
+    ("prot", libxl_sshm_prot, {'init_val': 'LIBXL_SSHM_PROT_UNKNOWN'}),
+    ("cache_policy", libxl_sshm_cachepolicy, {'init_val': 'LIBXL_SSHM_CACHEPOLICY_UNKNOWN'}),
+    ("role", libxl_sshm_role, {'init_val': 'LIBXL_SSHM_ROLE_UNKNOWN'}),
+])
+
 libxl_domain_config = Struct("domain_config", [
     ("c_info", libxl_domain_create_info),
     ("b_info", libxl_domain_build_info),
@@ -797,6 +824,7 @@ libxl_domain_config = Struct("domain_config", [
     ("channels", Array(libxl_device_channel, "num_channels")),
     ("usbctrls", Array(libxl_device_usbctrl, "num_usbctrls")),
     ("usbdevs", Array(libxl_device_usbdev, "num_usbdevs")),
+    ("sshms", Array(libxl_static_shm, "num_sshms")),
 
     ("on_poweroff", libxl_action_on_shutdown),
     ("on_reboot", libxl_action_on_shutdown),
Signature · 7 lines
2.13.3


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#2 lore raw

RFC PATCH 2/4 libxl:xl: add parsing code to parse "libxl_static_sshm" from xl config files

In reply to Zhongze Liu

Add the parsing utils for the newly introduced libxl_static_sshm struct to the libxl/libxlu_* family. And add realated parsing code in xl to parse the struct from xl config files. This is for the proposal "Allow setting up shared memory areas between VMs from xl config file" (see [1]).

[1] https://lists.xenproject.org/archives/html/xen-devel/2017-07/msg03047.html

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: [email protected]

---
 tools/libxl/Makefile      |   2 +-
 tools/libxl/libxlu_sshm.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxlutil.h   |   6 ++
 tools/xl/xl_parse.c       |  24 ++++-
 4 files changed, 258 insertions(+), 2 deletions(-)
 create mode 100644 tools/libxl/libxlu_sshm.c
tools/libxl/Makefile +1 -1
index 082af8f716..3b63fb2cad 100644
@@ -175,7 +175,7 @@ AUTOINCS= libxlu_cfg_y.h libxlu_cfg_l.h _libxl_list.h _paths.h \
 AUTOSRCS= libxlu_cfg_y.c libxlu_cfg_l.c
 AUTOSRCS += _libxl_save_msgs_callout.c _libxl_save_msgs_helper.c
 LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_l.o libxlu_cfg.o \
-	libxlu_disk_l.o libxlu_disk.o libxlu_vif.o libxlu_pci.o
+	libxlu_disk_l.o libxlu_disk.o libxlu_vif.o libxlu_pci.o libxlu_sshm.o
 $(LIBXLU_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 
 $(TEST_PROG_OBJS) _libxl.api-for-check: CFLAGS += $(CFLAGS_libxentoollog)
tools/libxl/libxlu_sshm.c new file +228 -0

228 lines not on this page · raw message

tools/libxl/libxlutil.h +6 -0
index e81b644c01..ee39cb5bdc 100644
@@ -118,6 +118,12 @@ int xlu_rdm_parse(XLU_Config *cfg, libxl_rdm_reserve *rdm, const char *str);
 int xlu_vif_parse_rate(XLU_Config *cfg, const char *rate,
                        libxl_device_nic *nic);
 
+/*
+ * static shared memory specification parsing
+ */
+int xlu_sshm_parse(XLU_Config *cfg, const char *spec,
+                   libxl_static_shm *sshm);
+
 #endif /* LIBXLUTIL_H */
 
 /*
tools/xl/xl_parse.c +23 -1
index 5c2bf17222..82d955b8b9 100644
@@ -813,7 +813,7 @@ void parse_config_data(const char *config_source,
     long l, vcpus = 0;
     XLU_Config *config;
     XLU_ConfigList *cpus, *vbds, *nics, *pcis, *cvfbs, *cpuids, *vtpms,
-                   *usbctrls, *usbdevs, *p9devs;
+                   *usbctrls, *usbdevs, *p9devs, *sshms;
     XLU_ConfigList *channels, *ioports, *irqs, *iomem, *viridian, *dtdevs,
                    *mca_caps;
     int num_ioports, num_irqs, num_iomem, num_cpus, num_viridian, num_mca_caps;
@@ -1392,6 +1392,28 @@ void parse_config_data(const char *config_source,
         }
     }
 
+    if (!xlu_cfg_get_list (config, "static_shm", &sshms, 0, 0)) {
+        d_config->num_sshms = 0;
+        d_config->sshms = NULL;
+        while ((buf = xlu_cfg_get_listitem (sshms, d_config->num_sshms)) != NULL) {
+            libxl_static_shm *sshm;
+            char *buf2 = strdup(buf);
+            int ret;
+
+            sshm = ARRAY_EXTEND_INIT_NODEVID(d_config->sshms,
+                                             d_config->num_sshms,
+                                             libxl_static_shm_init);
+            ret = xlu_sshm_parse(config, buf2, sshm);
+            if (ret) {
+                fprintf(stderr,
+                        "xl: Invalid argument for static_shm: %s", buf2);
+                exit(EXIT_FAILURE);
+            }
+
+            free(buf2);
+        }
+    }
+
     if (!xlu_cfg_get_list(config, "p9", &p9devs, 0, 0)) {
         libxl_device_p9 *p9;
         char *security_model = NULL;
Signature · 7 lines
2.13.3


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#3 lore raw

RFC PATCH 3/4 x86/p2m : remove checks that forbid adding foreign pages between HVM guests

In reply to Zhongze Liu

Two checks in the p2m code forbids sharing physical pages among DomU's by using xc_add_to_physmap_batch with XENMAPSPACE_gmfn_foreign. Just simply remove them in this RFC patch to ask for suggestions on how to properly handle this.

This is for the proposal "Allow setting up shared memory areas between VMs from xl config file" (see [1]).

[1] https://lists.xenproject.org/archives/html/xen-devel/2017-07/msg03047.html

---
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: [email protected]

---
 xen/arch/x86/mm/p2m.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
xen/arch/x86/mm/p2m.c +15 -5
index e8a57d118c..3ee4c14ed4 100644
@@ -2531,8 +2531,13 @@ int p2m_add_foreign(struct domain *tdom, unsigned long fgfn,
      * hvm fixme: until support is added to p2m teardown code to cleanup any
      * foreign entries, limit this to hardware domain only.
      */
-    if ( !is_hardware_domain(tdom) )
-        return -EPERM;
+    /* The following check prevents us from doing a XENMEM_add_to_physmap
+     * between two domU's. Asking for suggestions on how to remove or
+     * work around it.
+     *
+     *     if ( !is_hardware_domain(tdom) )
+     *         return -EPERM;
+     */
 
     if ( foreigndom == DOMID_XEN )
         fdom = rcu_lock_domain(dom_xen);
@@ -2545,9 +2550,14 @@ int p2m_add_foreign(struct domain *tdom, unsigned long fgfn,
     if ( tdom == fdom )
         goto out;
 
-    rc = xsm_map_gmfn_foreign(XSM_TARGET, tdom, fdom);
-    if ( rc )
-        goto out;
+    /* The following check prevents us from doing a XENMEM_add_to_physmap
+     * between two domU's. Asking for suggestions on how to remove or
+     * work around it.
+     *
+     *  rc = xsm_map_gmfn_foreign(XSM_TARGET, tdom, fdom);
+     *  if ( rc )
+     *      goto out;
+     */
 
     /*
      * Take a refcnt on the mfn. NB: following supported for foreign mapping:
Signature · 7 lines
2.13.3


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#6 lore raw

RFC PATCH 4/4 libxl: support creation and destruction of static shared memory areas

In reply to Zhongze Liu

creation:
* Check for further errors in the static_shm configs: overlapping
areas, invalid ranges, duplicated master domain, no master domain etc.
* Add code for writing infomations of static shared memory areas
into the appropriate xenstore paths.
* use xc_domain_add_to_physmap_batch to do the page sharing.

destruction:
* Check for errors that try to remove master while there'are still
living slaves.
* Cleanup related xenstore paths.

This is for the proposal "Allow setting up shared memory areas between VMs from xl config file" (see [1]).

[1] https://lists.xenproject.org/archives/html/xen-devel/2017-07/msg03047.html

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: [email protected]

---
 tools/libxl/Makefile         |   2 +-
 tools/libxl/libxl_create.c   |   7 +
 tools/libxl/libxl_dom.c      |   7 +
 tools/libxl/libxl_domain.c   |   6 +
 tools/libxl/libxl_internal.h |  14 ++
 tools/libxl/libxl_sshm.c     | 370 +++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_xshelp.c   |   8 +
 7 files changed, 413 insertions(+), 1 deletion(-)
 create mode 100644 tools/libxl/libxl_sshm.c
tools/libxl/Makefile +1 -1
index 3b63fb2cad..fd624b28f3 100644
@@ -138,7 +138,7 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_dom_suspend.o libxl_dom_save.o libxl_usb.o \
 			libxl_vtpm.o libxl_nic.o libxl_disk.o libxl_console.o \
 			libxl_cpupool.o libxl_mem.o libxl_sched.o libxl_tmem.o \
-			libxl_9pfs.o libxl_domain.o \
+			libxl_9pfs.o libxl_domain.o libxl_sshm.o \
                         $(LIBXL_OBJS-y)
 LIBXL_OBJS += libxl_genid.o
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
tools/libxl/libxl_create.c +7 -0
index 1158303e1a..06dba2178d 100644
@@ -918,6 +918,13 @@ static void initiate_domain_create(libxl__egc *egc,
         goto error_out;
     }
 
+    if (d_config->c_info.type != LIBXL_DOMAIN_TYPE_HVM &&
+        d_config->num_sshms != 0) {
+        LOGD(ERROR, domid, "static_shm is only applicable to HVM domains");
+        ret = ERROR_INVAL;
+        goto error_out;
+    }
+
     ret = libxl__domain_make(gc, d_config, &domid, &state->config);
     if (ret) {
         LOGD(ERROR, domid, "cannot make domain: %d", ret);
tools/libxl/libxl_dom.c +7 -0
index f54fd49a73..1958667344 100644
@@ -1194,6 +1194,13 @@ int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
         goto out;
     }
 
+    /* the p2m has been setup, we could map the static shared memory now. */
+    rc = libxl__sshm_add(gc, domid, d_config->sshms, d_config->num_sshms);
+    if (rc != 0) {
+        LOG(ERROR, "failed to map static shared memory");
+        goto out;
+    }
+
     rc = hvm_build_set_params(ctx->xch, domid, info, state->store_port,
                                &state->store_mfn, state->console_port,
                                &state->console_mfn, state->store_domid,
tools/libxl/libxl_domain.c +6 -0
index 08eccd082b..0702a575f2 100644
@@ -1028,6 +1028,12 @@ void libxl__destroy_domid(libxl__egc *egc, libxl__destroy_domid_state *dis)
         goto out;
     }
 
+    rc = libxl__sshm_del(gc, domid);
+    if (rc) {
+        LOGD(ERROR, domid, "Deleting static shm failed.");
+        goto out;
+    }
+
     if (libxl__device_pci_destroy_all(gc, domid) < 0)
         LOGD(ERROR, domid, "Pci shutdown failed");
     rc = xc_domain_pause(ctx->xch, domid);
tools/libxl/libxl_internal.h +14 -0
index 724750967c..9c9f69c50f 100644
@@ -721,6 +721,7 @@ _hidden char **libxl__xs_directory(libxl__gc *gc, xs_transaction_t t,
                                    const char *path, unsigned int *nb);
    /* On error: returns NULL, sets errno (no logging) */
 _hidden char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid);
+_hidden char *libxl__xs_get_sshmpath(libxl__gc *gc, const char *id);
 
 _hidden int libxl__backendpath_parse_domid(libxl__gc *gc, const char *be_path,
                                            libxl_domid *domid_out);
@@ -4353,6 +4354,19 @@ static inline bool libxl__acpi_defbool_val(const libxl_domain_build_info *b_info
 #endif
 
 /*
+ * Set up static shared ram pages for HVM domains to communicate
+ *
+ * This function should only be called after the memory map is constructed
+ * and before any further memory access. */
+int libxl__sshm_add(libxl__gc *gc, uint32_t domid,
+                    libxl_static_shm *sshm, int len);
+
+int libxl__sshm_del(libxl__gc *gc, uint32_t domid);
+
+int libxl__sshm_check_slaves_overlap(libxl__gc *gc, uint32_t domid,
+                                     libxl_static_shm *sshms, int len);
+
+/*
  * Local variables:
  * mode: C
  * c-basic-offset: 4
tools/libxl/libxl_sshm.c new file +370 -0

370 lines not on this page · raw message

tools/libxl/libxl_xshelp.c +8 -0
index c4a18df353..d91fbf5fda 100644
@@ -193,6 +193,14 @@ char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid)
     return s;
 }
 
+char *libxl__xs_get_sshmpath(libxl__gc *gc, const char *id)
+{
+    char *s = GCSPRINTF("/local/static_shm/%s", id);
+    if (!s)
+        LOGE(ERROR, "cannot allocate static shm path");
+    return s;
+}
+
 int libxl__xs_read_mandatory(libxl__gc *gc, xs_transaction_t t,
                              const char *path, const char **result_out)
 {
Signature · 7 lines
2.13.3


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#13 lore raw
In reply to Zhongze Liu

Hi,

I should have mentioned that this RFC only adds support to the x86 side. Sorry for that.

Cheers,

Zhongze Liu

2017-08-04 10:20 GMT+08:00 Zhongze Liu <[email protected]>:

Quoted 35 lines

This series implements the new xl config entry proposed in [1]. Users can use the new config entry to statically setup shared memory areas among VMs so that they could communicate with each other through the static shared memory areas.

[1] Proposla to allow setting up shared memory areas between VMs from xl config files:

    https://lists.xenproject.org/archives/html/xen-devel/2017-07/msg03047.html

Cheers,

Zhongze Liu (4):
libxl: introduce a new structure to represent static shared memory
regions
libxl:xl: add parsing code to parse "libxl_static_sshm" from xl config
files
x86/p2m : remove checks that forbid adding foreign pages between HVM
guests
libxl: support creation and destruction of static shared memory areas

 tools/libxl/Makefile         |   4 +-
 tools/libxl/libxl.h          |   4 +
 tools/libxl/libxl_create.c   |   7 +
 tools/libxl/libxl_dom.c      |   7 +
 tools/libxl/libxl_domain.c   |   6 +
 tools/libxl/libxl_internal.h |  14 ++
 tools/libxl/libxl_sshm.c     | 370 +++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_types.idl  |  28 ++++
 tools/libxl/libxl_xshelp.c   |   8 +
 tools/libxl/libxlu_sshm.c    | 228 ++++++++++++++++++++++++++
 tools/libxl/libxlutil.h      |   6 +
 tools/xl/xl_parse.c          |  24 ++-
 xen/arch/x86/mm/p2m.c        |  20 ++-
 13 files changed, 718 insertions(+), 8 deletions(-)
 create mode 100644 tools/libxl/libxl_sshm.c
 create mode 100644 tools/libxl/libxlu_sshm.c
Quoted 3 lines

--
2.13.3

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw

Re: [RFC PATCH 3/4] x86/p2m : remove checks that forbid adding foreign pages between HVM guests

In reply to Zhongze Liu
Quoted 26 lines · Zhongze Liu

Two checks in the p2m code forbids sharing physical pages among DomU's by using xc_add_to_physmap_batch with XENMAPSPACE_gmfn_foreign. Just simply remove them in this RFC patch to ask for suggestions on how to properly handle this.

This is for the proposal "Allow setting up shared memory areas between VMs from xl config file" (see [1]).

[1] https://lists.xenproject.org/archives/html/xen-devel/2017-07/msg03047.html

---
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: [email protected]

---
 xen/arch/x86/mm/p2m.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
xen/arch/x86/mm/p2m.c +0 -0
index e8a57d118c..3ee4c14ed4 100644

xen/arch/x86/mm/p2m.c int p2m_add_foreign(struct domain *tdom, unsigned long fgfn, · line 2532

Quoted 2 lines
xen/arch/x86/mm/p2m.c +0 -0
@@ -2531,8 +2531,13 @@ int p2m_add_foreign(struct domain *tdom, unsigned long fgfn,
      * hvm fixme: until support is added to p2m teardown code to cleanup any
      * foreign entries, limit this to hardware domain only.

This HVM fixme needs to be fixed before the check can go away. This is going to be a non-trivial project, but it is going to be useful in its own right.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw

Re: [RFC PATCH 3/4] x86/p2m : remove checks that forbid adding foreign pages between HVM guests

Hi Wei,

Yes, indeed. Before the teardown is implemented, even if I simply remove the first
check, when I try to destroy the master domain, the domain will become a zombie, because the refcount of the shared pages won't be correctly decreased on slave destruction.

I don't know why this hasn't been done, so I'm separating this patch out to serve
as a place to discuss this problem.

For this GSoC project, I would also ask if there is any temporary workaround. While waiting for any further suggestions and answers, I'll continue to work on the ARM side.

Cheers,

Zhongze Liu

2017-08-04 21:27 GMT+08:00 Wei Liu <[email protected]>:

Quoted 22 lines · Zhongze Liu
Quoted 21 lines

Two checks in the p2m code forbids sharing physical pages among DomU's by using xc_add_to_physmap_batch with XENMAPSPACE_gmfn_foreign. Just simply remove them in this RFC patch to ask for suggestions on how to properly handle this.

This is for the proposal "Allow setting up shared memory areas between VMs from xl config file" (see [1]).

[1] https://lists.xenproject.org/archives/html/xen-devel/2017-07/msg03047.html

---
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: [email protected]

---
 xen/arch/x86/mm/p2m.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
Quoted 11 lines

xen/arch/x86/mm/p2m.c int p2m_add_foreign(struct domain *tdom, unsigned long fgfn, · line 2532

Quoted 7 lines
xen/arch/x86/mm/p2m.c +0 -0
index e8a57d118c..3ee4c14ed4 100644
@@ -2531,8 +2531,13 @@ int p2m_add_foreign(struct domain *tdom, unsigned long fgfn,
      * hvm fixme: until support is added to p2m teardown code to cleanup any
      * foreign entries, limit this to hardware domain only.

This HVM fixme needs to be fixed before the check can go away. This is going to be a non-trivial project, but it is going to be useful in its own right.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#7 lore raw

Re: [RFC PATCH 4/4] libxl: support creation and destruction of static shared memory areas

In reply to Zhongze Liu

I skim through this patch and have some questions.

Quoted 12 lines · Zhongze Liu
tools/libxl/libxl_sshm.c new file +12 -0
+
+static int libxl__sshm_add_master(libxl__gc *gc, uint32_t domid,
+                                  libxl_static_shm *sshm)
+{
+    int rc, aborting;
+    char *sshm_path, *dom_path, *dom_role_path;
+    char *ents[11];
+    struct xs_permissions noperm;
+    xs_transaction_t xt = XBT_NULL;
+
+    sshm_path = libxl__xs_get_sshmpath(gc, sshm->id);
+    dom_path = libxl__xs_get_dompath(gc, domid);

tools/libxl/libxl_sshm.c line 92

Quoted 10 lines
tools/libxl/libxl_sshm.c new file +10 -0
+    /* the domain should be in xenstore by now */
+    assert(dom_path);
+    dom_role_path = GCSPRINTF("%s/static_shm/%s/role", dom_path, sshm->id);
+
+
+ retry_transaction:
+    /* Within the transaction, goto out by default means aborting */
+    aborting = 1;
+    rc = libxl__xs_transaction_start(gc, &xt);
+    if (rc) { goto out; }

if (rc) goto out;

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+    if (NULL == libxl__xs_read(gc, xt, sshm_path)) {

!libxl__xs_read

We don't use "Yoda conditions".

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+        rc = libxl__xs_write_checked(gc, xt, dom_role_path, "master");
+        if (rc) { goto out; };
+
+        ents[0] = "master";
+        ents[1] = GCSPRINTF("%"PRIu32, domid);
+        ents[2] = "begin";

tools/libxl/libxl_sshm.c line 110

Quoted 10 lines
tools/libxl/libxl_sshm.c new file +10 -0
+        ents[3] = GCSPRINTF("0x%"PRIx64, sshm->begin);
+        ents[4] = "end";
+        ents[5] = GCSPRINTF("0x%"PRIx64, sshm->end);
+        ents[6] = "prot";
+        ents[7] = libxl__strdup(gc, libxl_sshm_prot_to_string(sshm->prot));
+        ents[8] = "cache_policy";
+        ents[9] = libxl__strdup(gc,
+                      libxl_sshm_cachepolicy_to_string(sshm->cache_policy));
+        ents[10] = NULL;
+

These aren't going to change from iteration to iteration, so you can prepare them before entering the loop.

BTW it would be cleaner to use a for (;;) {} or while (1) {} loop to implement this instead of using goto label. You can then eliminate the TRY_TRANSACTION_OR_FAIL macro.

Quoted 18 lines
tools/libxl/libxl_sshm.c new file +18 -0
+        /* could only be accessed by Dom0 */
+        noperm.id = 0;
+        noperm.perms = XS_PERM_NONE;
+        libxl__xs_mknod(gc, xt, sshm_path, &noperm, 1);
+        libxl__xs_writev(gc, xt, sshm_path, ents);
+    } else {
+        SSHM_ERROR(domid, sshm->id, "can only have one master.");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    aborting = rc = 0;
+
+ out:
+    TRY_TRANSACTION_OR_FAIL(aborting);
+    return rc;
+}
+

[...]

tools/libxl/libxl_sshm.c line 275

Quoted 10 lines
tools/libxl/libxl_sshm.c new file +10 -0
+static int libxl__sshm_del_single(libxl__gc *gc, xs_transaction_t xt,
+                                  uint32_t domid, const char *id, bool master)
+{
+    char *sshm_path, *slaves_path;
+
+    sshm_path = libxl__xs_get_sshmpath(gc, id);
+    slaves_path = GCSPRINTF("%s/slaves", sshm_path);
+
+    if (master) {
+        /* we know that domid can't be both a master and a slave for one id,

Is this enforced in code?

tools/libxl/libxl_sshm.c line 281

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+         * so the number of slaves won't change during iteration. Simply check
+         * sshm_path/slavea to tell if there are still living slaves. */
+        if (NULL != libxl__xs_read(gc, xt, slaves_path)) {
+            SSHM_ERROR(domid, id,
+                       "can't remove master when there are living slaves.");
+            return ERROR_FAIL;

Isn't this going to leave a half-destructed domain in userspace components? Maybe we should proceed anyway?

tools/libxl/libxl_sshm.c line 286

Quoted 5 lines
tools/libxl/libxl_sshm.c new file +5 -0
+        }
+        libxl__xs_path_cleanup(gc, xt, sshm_path);
+    } else {
+        libxl__xs_path_cleanup(gc, xt,
+            GCSPRINTF("%s/%"PRIu32, slaves_path, domid));

Is this really enough? What will happen to the mapping? You merely delete the xenstore node for it but the mapping is still there.

I suppose you're relying on the hypervisor to remove the mapping from p2m?

Quoted 5 lines
tools/libxl/libxl_sshm.c new file +5 -0
+    }
+
+    return 0;
+}
+

[...]

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 200

Quoted 13 lines
tools/libxl/libxl_xshelp.c +5 -0
index c4a18df353..d91fbf5fda 100644
@@ -193,6 +193,14 @@ char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid)
     return s;
 }
 
+char *libxl__xs_get_sshmpath(libxl__gc *gc, const char *id)
+{
+    char *s = GCSPRINTF("/local/static_shm/%s", id);
+    if (!s)
+        LOGE(ERROR, "cannot allocate static shm path");

GCSPRINTF can't fail. You can eliminate the check.

Quoted 9 lines
tools/libxl/libxl_xshelp.c +3 -0
+    return s;
+}
+
 int libxl__xs_read_mandatory(libxl__gc *gc, xs_transaction_t t,
                              const char *path, const char **result_out)
 {
Signature · 1 line
2.13.3

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#8 lore raw

Re: [RFC PATCH 4/4] libxl: support creation and destruction of static shared memory areas

Hi Wei,

Thank you for reviewing my patch.

2017-08-04 23:20 GMT+08:00 Wei Liu <[email protected]>:

Quoted 19 lines

I skim through this patch and have some questions.

Quoted 16 lines · Zhongze Liu
tools/libxl/libxl_sshm.c new file +16 -0
+
+static int libxl__sshm_add_master(libxl__gc *gc, uint32_t domid,
+                                  libxl_static_shm *sshm)
+{
+    int rc, aborting;
+    char *sshm_path, *dom_path, *dom_role_path;
+    char *ents[11];
+    struct xs_permissions noperm;
+    xs_transaction_t xt = XBT_NULL;
+
+    sshm_path = libxl__xs_get_sshmpath(gc, sshm->id);
+    dom_path = libxl__xs_get_dompath(gc, domid);
+    /* the domain should be in xenstore by now */
+    assert(dom_path);
+    dom_role_path = GCSPRINTF("%s/static_shm/%s/role", dom_path, sshm->id);
+
Quoted 8 lines

tools/libxl/libxl_sshm.c line 92

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+
+ retry_transaction:
+    /* Within the transaction, goto out by default means aborting */
+    aborting = 1;
+    rc = libxl__xs_transaction_start(gc, &xt);
+    if (rc) { goto out; }

if (rc) goto out;

OK. Will remove all the {}. BTW, do I have to place "goto out;" in a newline?

Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+    if (NULL == libxl__xs_read(gc, xt, sshm_path)) {

!libxl__xs_read

We don't use "Yoda conditions".

I'll turn all the Yoda conditions into "natural" ones.

Quoted 21 lines
Quoted 7 lines
tools/libxl/libxl_sshm.c new file +6 -0
+        rc = libxl__xs_write_checked(gc, xt, dom_role_path, "master");
+        if (rc) { goto out; };
+
+        ents[0] = "master";
+        ents[1] = GCSPRINTF("%"PRIu32, domid);
+        ents[2] = "begin";

tools/libxl/libxl_sshm.c line 110

Quoted 10 lines
tools/libxl/libxl_sshm.c new file +10 -0
+        ents[3] = GCSPRINTF("0x%"PRIx64, sshm->begin);
+        ents[4] = "end";
+        ents[5] = GCSPRINTF("0x%"PRIx64, sshm->end);
+        ents[6] = "prot";
+        ents[7] = libxl__strdup(gc, libxl_sshm_prot_to_string(sshm->prot));
+        ents[8] = "cache_policy";
+        ents[9] = libxl__strdup(gc,
+                      libxl_sshm_cachepolicy_to_string(sshm->cache_policy));
+        ents[10] = NULL;
+

These aren't going to change from iteration to iteration, so you can prepare them before entering the loop.

Quoted 3 lines

BTW it would be cleaner to use a for (;;) {} or while (1) {} loop to implement this instead of using goto label. You can then eliminate the TRY_TRANSACTION_OR_FAIL macro.

OK. I'll move the entries out of the iteration. And yes, using an infinite loop and
break on appropriate conditions will make it more concise.

Quoted 20 lines
Quoted 19 lines
tools/libxl/libxl_sshm.c new file +18 -0
+        /* could only be accessed by Dom0 */
+        noperm.id = 0;
+        noperm.perms = XS_PERM_NONE;
+        libxl__xs_mknod(gc, xt, sshm_path, &noperm, 1);
+        libxl__xs_writev(gc, xt, sshm_path, ents);
+    } else {
+        SSHM_ERROR(domid, sshm->id, "can only have one master.");
+        rc = ERROR_FAIL;
+        goto out;
+    }
+
+    aborting = rc = 0;
+
+ out:
+    TRY_TRANSACTION_OR_FAIL(aborting);
+    return rc;
+}
+

[...]

Quoted 12 lines

tools/libxl/libxl_sshm.c line 275

Quoted 10 lines
tools/libxl/libxl_sshm.c new file +10 -0
+static int libxl__sshm_del_single(libxl__gc *gc, xs_transaction_t xt,
+                                  uint32_t domid, const char *id, bool master)
+{
+    char *sshm_path, *slaves_path;
+
+    sshm_path = libxl__xs_get_sshmpath(gc, id);
+    slaves_path = GCSPRINTF("%s/slaves", sshm_path);
+
+    if (master) {
+        /* we know that domid can't be both a master and a slave for one id,

Is this enforced in code?

Yes...and...no. I've done this in libxl__sshm_add_slave() by doing:

+ if (NULL != libxl__xs_read(gc, xt, dom_sshm_path)) {
+ SSHM_ERROR(domid, sshm->id,
+ "domain tried to share the same region twice.");
+ rc = ERROR_FAIL;
+ goto out;
+ }

Maybe the comment is a little bit confusing. What I was planning to do is to place such a check in both *_add_slave() and *_add_master(), so that one ID can't appear twice within one domain, so that one domain will not be able to be both a master and a slave.

Quoted 10 lines

tools/libxl/libxl_sshm.c line 281

Quoted 7 lines
tools/libxl/libxl_sshm.c new file +6 -0
+         * so the number of slaves won't change during iteration. Simply check
+         * sshm_path/slavea to tell if there are still living slaves. */
+        if (NULL != libxl__xs_read(gc, xt, slaves_path)) {
+            SSHM_ERROR(domid, id,
+                       "can't remove master when there are living slaves.");
+            return ERROR_FAIL;

Isn't this going to leave a half-destructed domain in userspace components? Maybe we should proceed anyway?

This is also among the points that I'm not very sure. What is the best way to handle this type of error during domain destruction?

Quoted 7 lines

tools/libxl/libxl_sshm.c line 286

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +5 -0
+        }
+        libxl__xs_path_cleanup(gc, xt, sshm_path);
+    } else {
+        libxl__xs_path_cleanup(gc, xt,
+            GCSPRINTF("%s/%"PRIu32, slaves_path, domid));
Quoted 5 lines

Is this really enough? What will happen to the mapping? You merely delete the xenstore node for it but the mapping is still there.

I suppose you're relying on the hypervisor to remove the mapping from p2m?

Yes, the "sharing" here simply means adding one physical page to the slave domains stage-2 mapping. The only thing that's changed is the refcount, and I rely one the teardown code to decrease the refcount, which is implemented on ARM but unimplemented on x86.

Quoted 15 lines
Quoted 6 lines
tools/libxl/libxl_sshm.c new file +5 -0
+    }
+
+    return 0;
+}
+

[...]

Quoted 8 lines
tools/libxl/libxl_xshelp.c +0 -0
index c4a18df353..d91fbf5fda 100644
@@ -193,6 +193,14 @@ char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid)
     return s;
 }
 
Quoted 7 lines

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 200

Quoted 5 lines
tools/libxl/libxl_xshelp.c +5 -0
+char *libxl__xs_get_sshmpath(libxl__gc *gc, const char *id)
+{
+    char *s = GCSPRINTF("/local/static_shm/%s", id);
+    if (!s)
+        LOGE(ERROR, "cannot allocate static shm path");

GCSPRINTF can't fail. You can eliminate the check.

I was also confused about the other similar checks around the file since GCSPRINTF will die on failure. Em...It seems that I've copied the previous errors.

Then I'll remove this function and replace it with a macro in libxl_sshm.c instead.

Quoted 10 lines
Quoted 10 lines
tools/libxl/libxl_xshelp.c +3 -1
+    return s;
+}
+
 int libxl__xs_read_mandatory(libxl__gc *gc, xs_transaction_t t,
                              const char *path, const char **result_out)
 {
--

2.13.3

Cheers,

Zhongze Liu.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#9 lore raw

Re: [RFC PATCH 4/4] libxl: support creation and destruction of static shared memory areas

Quoted 33 lines · Zhongze Liu

Hi Wei,

Thank you for reviewing my patch.

2017-08-04 23:20 GMT+08:00 Wei Liu <[email protected]>:

Quoted 19 lines

I skim through this patch and have some questions.

Quoted 16 lines · Zhongze Liu
tools/libxl/libxl_sshm.c new file +16 -0
+
+static int libxl__sshm_add_master(libxl__gc *gc, uint32_t domid,
+                                  libxl_static_shm *sshm)
+{
+    int rc, aborting;
+    char *sshm_path, *dom_path, *dom_role_path;
+    char *ents[11];
+    struct xs_permissions noperm;
+    xs_transaction_t xt = XBT_NULL;
+
+    sshm_path = libxl__xs_get_sshmpath(gc, sshm->id);
+    dom_path = libxl__xs_get_dompath(gc, domid);
+    /* the domain should be in xenstore by now */
+    assert(dom_path);
+    dom_role_path = GCSPRINTF("%s/static_shm/%s/role", dom_path, sshm->id);
+
Quoted 8 lines

tools/libxl/libxl_sshm.c line 92

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+
+ retry_transaction:
+    /* Within the transaction, goto out by default means aborting */
+    aborting = 1;
+    rc = libxl__xs_transaction_start(gc, &xt);
+    if (rc) { goto out; }

if (rc) goto out;

Quoted 2 lines

OK. Will remove all the {}. BTW, do I have to place "goto out;" in a newline?

Youc can look for examples in existing code and follow those.

[...]

Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
tools/libxl/libxl_sshm.c new file +4 -0
+static int libxl__sshm_del_single(libxl__gc *gc, xs_transaction_t xt,
+                                  uint32_t domid, const char *id, bool master)
+{
+    char *sshm_path, *slaves_path;
Quoted 8 lines

tools/libxl/libxl_sshm.c line 275

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+
+    sshm_path = libxl__xs_get_sshmpath(gc, id);
+    slaves_path = GCSPRINTF("%s/slaves", sshm_path);
+
+    if (master) {
+        /* we know that domid can't be both a master and a slave for one id,

Is this enforced in code?

Yes...and...no. I've done this in libxl__sshm_add_slave() by doing:

tools/libxl/libxl_sshm.c new file +6 -0
+        if (NULL != libxl__xs_read(gc, xt, dom_sshm_path)) {
+                    SSHM_ERROR(domid, sshm->id,
+                               "domain tried to share the same region twice.");
+                    rc = ERROR_FAIL;
+                    goto out;
+        }
Quoted 5 lines

Maybe the comment is a little bit confusing. What I was planning to do is to place such a check in both *_add_slave() and *_add_master(), so that one ID can't appear twice within one domain, so that one domain will not be able to be both a master and a slave.

OK this sounds plausible.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
tools/libxl/libxl_sshm.c new file +6 -0
+         * so the number of slaves won't change during iteration. Simply check
+         * sshm_path/slavea to tell if there are still living slaves. */
+        if (NULL != libxl__xs_read(gc, xt, slaves_path)) {
+            SSHM_ERROR(domid, id,
+                       "can't remove master when there are living slaves.");
+            return ERROR_FAIL;
Quoted 6 lines
Quoted 2 lines

Isn't this going to leave a half-destructed domain in userspace components? Maybe we should proceed anyway?

This is also among the points that I'm not very sure. What is the best way to handle this type of error during domain destruction?

I think we should destroy everything in relation to the guest in Dom0 (or other service domains). Some pages for the master guests might be referenced by slaves, but they will eventually be freed (hence the domain struct will be freed) within Xen. Do experiment with this to see if my prediction is right.

It also occurs to me you need to guard against circular references. That is, DomA and DomB have a mutual master-slave relationship.

Quoted 8 lines
Quoted 7 lines

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 200

Quoted 5 lines
tools/libxl/libxl_xshelp.c +5 -0
+char *libxl__xs_get_sshmpath(libxl__gc *gc, const char *id)
+{
+    char *s = GCSPRINTF("/local/static_shm/%s", id);
+    if (!s)
+        LOGE(ERROR, "cannot allocate static shm path");

GCSPRINTF can't fail. You can eliminate the check.

Quoted 7 lines

I was also confused about the other similar checks around the file since GCSPRINTF will die on failure. Em...It seems that I've copied the previous errors.

Then I'll remove this function and replace it with a macro in libxl_sshm.c instead.

Using a static inline function is better.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#10 lore raw

Re: [RFC PATCH 4/4] libxl: support creation and destruction of static shared memory areas

Quoted 85 lines · Wei Liu
Quoted 33 lines · Zhongze Liu

Hi Wei,

Thank you for reviewing my patch.

2017-08-04 23:20 GMT+08:00 Wei Liu <[email protected]>:

Quoted 19 lines

I skim through this patch and have some questions.

Quoted 16 lines · Zhongze Liu
tools/libxl/libxl_sshm.c new file +16 -0
+
+static int libxl__sshm_add_master(libxl__gc *gc, uint32_t domid,
+                                  libxl_static_shm *sshm)
+{
+    int rc, aborting;
+    char *sshm_path, *dom_path, *dom_role_path;
+    char *ents[11];
+    struct xs_permissions noperm;
+    xs_transaction_t xt = XBT_NULL;
+
+    sshm_path = libxl__xs_get_sshmpath(gc, sshm->id);
+    dom_path = libxl__xs_get_dompath(gc, domid);
+    /* the domain should be in xenstore by now */
+    assert(dom_path);
+    dom_role_path = GCSPRINTF("%s/static_shm/%s/role", dom_path, sshm->id);
+
Quoted 8 lines

tools/libxl/libxl_sshm.c line 92

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+
+ retry_transaction:
+    /* Within the transaction, goto out by default means aborting */
+    aborting = 1;
+    rc = libxl__xs_transaction_start(gc, &xt);
+    if (rc) { goto out; }

if (rc) goto out;

Quoted 2 lines

OK. Will remove all the {}. BTW, do I have to place "goto out;" in a newline?

Youc can look for examples in existing code and follow those.

[...]

Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
tools/libxl/libxl_sshm.c new file +4 -0
+static int libxl__sshm_del_single(libxl__gc *gc, xs_transaction_t xt,
+                                  uint32_t domid, const char *id, bool master)
+{
+    char *sshm_path, *slaves_path;
Quoted 8 lines

tools/libxl/libxl_sshm.c line 275

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+
+    sshm_path = libxl__xs_get_sshmpath(gc, id);
+    slaves_path = GCSPRINTF("%s/slaves", sshm_path);
+
+    if (master) {
+        /* we know that domid can't be both a master and a slave for one id,

Is this enforced in code?

Yes...and...no. I've done this in libxl__sshm_add_slave() by doing:

tools/libxl/libxl_sshm.c new file +6 -0
+        if (NULL != libxl__xs_read(gc, xt, dom_sshm_path)) {
+                    SSHM_ERROR(domid, sshm->id,
+                               "domain tried to share the same region twice.");
+                    rc = ERROR_FAIL;
+                    goto out;
+        }
Quoted 5 lines

Maybe the comment is a little bit confusing. What I was planning to do is to place such a check in both *_add_slave() and *_add_master(), so that one ID can't appear twice within one domain, so that one domain will not be able to be both a master and a slave.

OK this sounds plausible.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
tools/libxl/libxl_sshm.c new file +6 -0
+         * so the number of slaves won't change during iteration. Simply check
+         * sshm_path/slavea to tell if there are still living slaves. */
+        if (NULL != libxl__xs_read(gc, xt, slaves_path)) {
+            SSHM_ERROR(domid, id,
+                       "can't remove master when there are living slaves.");
+            return ERROR_FAIL;
Quoted 6 lines
Quoted 2 lines

Isn't this going to leave a half-destructed domain in userspace components? Maybe we should proceed anyway?

This is also among the points that I'm not very sure. What is the best way to handle this type of error during domain destruction?

Quoted 9 lines

I think we should destroy everything in relation to the guest in Dom0 (or other service domains). Some pages for the master guests might be referenced by slaves, but they will eventually be freed (hence the domain struct will be freed) within Xen. Do experiment with this to see if my prediction is right.

It also occurs to me you need to guard against circular references. That is, DomA and DomB have a mutual master-slave relationship.

This probably can't happen because you can't construct such pair of guests in the first place.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#11 lore raw

Re: [RFC PATCH 4/4] libxl: support creation and destruction of static shared memory areas

2017-08-08 18:56 GMT+08:00 Wei Liu <[email protected]>:

Quoted 92 lines · Wei Liu
Quoted 91 lines · Zhongze Liu
Quoted 33 lines

Hi Wei,

Thank you for reviewing my patch.

2017-08-04 23:20 GMT+08:00 Wei Liu <[email protected]>:

Quoted 19 lines

I skim through this patch and have some questions.

Quoted 16 lines · Zhongze Liu
tools/libxl/libxl_sshm.c new file +16 -0
+
+static int libxl__sshm_add_master(libxl__gc *gc, uint32_t domid,
+                                  libxl_static_shm *sshm)
+{
+    int rc, aborting;
+    char *sshm_path, *dom_path, *dom_role_path;
+    char *ents[11];
+    struct xs_permissions noperm;
+    xs_transaction_t xt = XBT_NULL;
+
+    sshm_path = libxl__xs_get_sshmpath(gc, sshm->id);
+    dom_path = libxl__xs_get_dompath(gc, domid);
+    /* the domain should be in xenstore by now */
+    assert(dom_path);
+    dom_role_path = GCSPRINTF("%s/static_shm/%s/role", dom_path, sshm->id);
+
Quoted 8 lines

tools/libxl/libxl_sshm.c line 92

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+
+ retry_transaction:
+    /* Within the transaction, goto out by default means aborting */
+    aborting = 1;
+    rc = libxl__xs_transaction_start(gc, &xt);
+    if (rc) { goto out; }

if (rc) goto out;

Quoted 2 lines

OK. Will remove all the {}. BTW, do I have to place "goto out;" in a newline?

Youc can look for examples in existing code and follow those.

[...]

Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
tools/libxl/libxl_sshm.c new file +4 -0
+static int libxl__sshm_del_single(libxl__gc *gc, xs_transaction_t xt,
+                                  uint32_t domid, const char *id, bool master)
+{
+    char *sshm_path, *slaves_path;
Quoted 8 lines

tools/libxl/libxl_sshm.c line 275

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+
+    sshm_path = libxl__xs_get_sshmpath(gc, id);
+    slaves_path = GCSPRINTF("%s/slaves", sshm_path);
+
+    if (master) {
+        /* we know that domid can't be both a master and a slave for one id,

Is this enforced in code?

Yes...and...no. I've done this in libxl__sshm_add_slave() by doing:

tools/libxl/libxl_sshm.c new file +6 -0
+        if (NULL != libxl__xs_read(gc, xt, dom_sshm_path)) {
+                    SSHM_ERROR(domid, sshm->id,
+                               "domain tried to share the same region twice.");
+                    rc = ERROR_FAIL;
+                    goto out;
+        }
Quoted 5 lines

Maybe the comment is a little bit confusing. What I was planning to do is to place such a check in both *_add_slave() and *_add_master(), so that one ID can't appear twice within one domain, so that one domain will not be able to be both a master and a slave.

OK this sounds plausible.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
tools/libxl/libxl_sshm.c new file +6 -0
+         * so the number of slaves won't change during iteration. Simply check
+         * sshm_path/slavea to tell if there are still living slaves. */
+        if (NULL != libxl__xs_read(gc, xt, slaves_path)) {
+            SSHM_ERROR(domid, id,
+                       "can't remove master when there are living slaves.");
+            return ERROR_FAIL;
Quoted 6 lines
Quoted 2 lines

Isn't this going to leave a half-destructed domain in userspace components? Maybe we should proceed anyway?

This is also among the points that I'm not very sure. What is the best way to handle this type of error during domain destruction?

I think we should destroy everything in relation to the guest in Dom0 (or other service domains). Some pages for the master guests might be referenced by slaves, but they will eventually be freed (hence the domain struct will be freed) within Xen. Do experiment with this to see if my prediction is right.

Quoted 6 lines
Quoted 3 lines

It also occurs to me you need to guard against circular references. That is, DomA and DomB have a mutual master-slave relationship.

This probably can't happen because you can't construct such pair of guests in the first place.

Yes, indeed. Because masters can only be created prior the slaves. So it must be a forest-like structure.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#12 lore raw

Re: [RFC PATCH 4/4] libxl: support creation and destruction of static shared memory areas

In reply to Wei Liu

2017-08-08 18:49 GMT+08:00 Wei Liu <[email protected]>:

Quoted 85 lines · Zhongze Liu
Quoted 33 lines

Hi Wei,

Thank you for reviewing my patch.

2017-08-04 23:20 GMT+08:00 Wei Liu <[email protected]>:

Quoted 19 lines

I skim through this patch and have some questions.

Quoted 16 lines · Zhongze Liu
tools/libxl/libxl_sshm.c new file +16 -0
+
+static int libxl__sshm_add_master(libxl__gc *gc, uint32_t domid,
+                                  libxl_static_shm *sshm)
+{
+    int rc, aborting;
+    char *sshm_path, *dom_path, *dom_role_path;
+    char *ents[11];
+    struct xs_permissions noperm;
+    xs_transaction_t xt = XBT_NULL;
+
+    sshm_path = libxl__xs_get_sshmpath(gc, sshm->id);
+    dom_path = libxl__xs_get_dompath(gc, domid);
+    /* the domain should be in xenstore by now */
+    assert(dom_path);
+    dom_role_path = GCSPRINTF("%s/static_shm/%s/role", dom_path, sshm->id);
+
Quoted 8 lines

tools/libxl/libxl_sshm.c line 92

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+
+ retry_transaction:
+    /* Within the transaction, goto out by default means aborting */
+    aborting = 1;
+    rc = libxl__xs_transaction_start(gc, &xt);
+    if (rc) { goto out; }

if (rc) goto out;

Quoted 2 lines

OK. Will remove all the {}. BTW, do I have to place "goto out;" in a newline?

Youc can look for examples in existing code and follow those.

[...]

Quoted 22 lines
Quoted 4 lines
Quoted 4 lines
tools/libxl/libxl_sshm.c new file +4 -0
+static int libxl__sshm_del_single(libxl__gc *gc, xs_transaction_t xt,
+                                  uint32_t domid, const char *id, bool master)
+{
+    char *sshm_path, *slaves_path;
Quoted 8 lines

tools/libxl/libxl_sshm.c line 275

Quoted 6 lines
tools/libxl/libxl_sshm.c new file +6 -0
+
+    sshm_path = libxl__xs_get_sshmpath(gc, id);
+    slaves_path = GCSPRINTF("%s/slaves", sshm_path);
+
+    if (master) {
+        /* we know that domid can't be both a master and a slave for one id,

Is this enforced in code?

Yes...and...no. I've done this in libxl__sshm_add_slave() by doing:

tools/libxl/libxl_sshm.c new file +6 -0
+        if (NULL != libxl__xs_read(gc, xt, dom_sshm_path)) {
+                    SSHM_ERROR(domid, sshm->id,
+                               "domain tried to share the same region twice.");
+                    rc = ERROR_FAIL;
+                    goto out;
+        }
Quoted 5 lines

Maybe the comment is a little bit confusing. What I was planning to do is to place such a check in both *_add_slave() and *_add_master(), so that one ID can't appear twice within one domain, so that one domain will not be able to be both a master and a slave.

OK this sounds plausible.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
tools/libxl/libxl_sshm.c new file +6 -0
+         * so the number of slaves won't change during iteration. Simply check
+         * sshm_path/slavea to tell if there are still living slaves. */
+        if (NULL != libxl__xs_read(gc, xt, slaves_path)) {
+            SSHM_ERROR(domid, id,
+                       "can't remove master when there are living slaves.");
+            return ERROR_FAIL;
Quoted 6 lines
Quoted 2 lines

Isn't this going to leave a half-destructed domain in userspace components? Maybe we should proceed anyway?

This is also among the points that I'm not very sure. What is the best way to handle this type of error during domain destruction?

Quoted 5 lines

I think we should destroy everything in relation to the guest in Dom0 (or other service domains). Some pages for the master guests might be referenced by slaves, but they will eventually be freed (hence the domain struct will be freed) within Xen. Do experiment with this to see if my prediction is right.

Yes, that's right. I'll turn the erros into warnings and proceed anyway.

Quoted 16 lines

It also occurs to me you need to guard against circular references. That is, DomA and DomB have a mutual master-slave relationship.

Quoted 12 lines
Quoted 7 lines

tools/libxl/libxl_xshelp.c char *libxl__xs_libxl_path(libxl__gc *gc, uint32_t domid) · line 200

Quoted 5 lines
tools/libxl/libxl_xshelp.c +5 -0
+char *libxl__xs_get_sshmpath(libxl__gc *gc, const char *id)
+{
+    char *s = GCSPRINTF("/local/static_shm/%s", id);
+    if (!s)
+        LOGE(ERROR, "cannot allocate static shm path");

GCSPRINTF can't fail. You can eliminate the check.

I was also confused about the other similar checks around the file since GCSPRINTF will die on failure. Em...It seems that I've copied the previous errors.

Quoted 5 lines
Quoted 3 lines

Then I'll remove this function and replace it with a macro in libxl_sshm.c instead.

Using a static inline function is better.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel