Skip to contentSkip to conversation

xen-access: Do not use ERROR out of xc handle scope

PATCH In review Aravindh Puthiyaparambil (aravindp) 3 participants 7 messages Mar 17, 2014 – Mar 21, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen-access: Do not use ERROR out of xc handle scope

In places where xc handle is not in scope, use standard printf to display errors.

---
tools/tests/xen-access/xen-access.c | 6 +++---

 1 file changed, 3 insertions(+), 3 deletions(-)
tools/tests/xen-access/xen-access.c +3 -3
index b00c05a..640250e 100644
@@ -215,7 +215,7 @@ int xenaccess_teardown(xc_interface *xch, xenaccess_t *xenaccess)
     rc = xc_interface_close(xenaccess->xc_handle);
     if ( rc != 0 )
     {
-        ERROR("Error closing connection to xen");
+        DPRINTF("Error closing connection to xen\n");
     }
     xenaccess->xc_handle = NULL;
 
@@ -523,7 +523,7 @@ int main(int argc, char *argv[])
     xenaccess = xenaccess_init(&xch, domain_id);
     if ( xenaccess == NULL )
     {
-        ERROR("Error initialising xenaccess");
+        DPRINTF("Error initialising xenaccess\n");
         return 1;
     }
 
@@ -693,7 +693,7 @@ exit:
     /* Tear down domain xenaccess */
     rc1 = xenaccess_teardown(xch, xenaccess);
     if ( rc1 != 0 )
-        ERROR("Error tearing down xenaccess");
+        DPRINTF("Error tearing down xenaccess\n");
 
     if ( rc == 0 )
         rc = rc1;
#1 lore raw

Hi,

Quoted 4 lines · Aravindh Puthiyaparambil (aravindp)

In places where xc handle is not in scope, use standard printf to display errors.

This is OK as far as it goes, but this code should not be using ERROR() &c at all -- those are libxc internals, and since we're not providing a logger argument or any flags to xc_interface_open, I don't think they do anything useful here. How about something like this (compile-tested only):

---8<----

tools/xen-access: don't use libxc internals directly.

tools/tests/xen-access/xen-access.c +9 -36
index b00c05a..4ca0782 100644
@@ -26,23 +26,24 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include <errno.h>
 #include <inttypes.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdbool.h>
+#include <string.h>
 #include <time.h>
 #include <signal.h>
 #include <unistd.h>
+#include <sys/mman.h>
 #include <sys/poll.h>
-#include <xc_private.h>
-#include <xg_save_restore.h>
 
+#include <xenctrl.h>
 #include <xen/mem_event.h>
 
-//#if 0
-#undef DPRINTF
-#define DPRINTF(a, b...) fprintf(stderr, a, ## b)
-//#endif
+#define DPRINTF(a, b...) fprintf(stderr, a "\n", ## b)
+#define ERROR(a, b...) fprintf(stderr, a "\n", ## b)
+#define PERROR(a, b...) fprintf(stderr, a ": %s\n", ## b, strerror(errno))
 
 /* Spinlock and mem event definitions */
 
@@ -104,17 +105,9 @@ typedef struct mem_event {
     spinlock_t ring_lock;
 } mem_event_t;
 
-typedef struct xc_platform_info {
-    unsigned long max_mfn;
-    unsigned long hvirt_start;
-    unsigned int  pt_levels;
-    unsigned int  guest_width;
-} xc_platform_info_t;
-
 typedef struct xenaccess {
     xc_interface *xc_handle;
 
-    xc_platform_info_t *platform_info;
     xc_domaininfo_t    *domain_info;
 
     mem_event_t mem_event;
@@ -178,7 +171,7 @@ int xenaccess_teardown(xc_interface *xch, xenaccess_t *xenaccess)
 
     /* Tear down domain xenaccess in Xen */
     if ( xenaccess->mem_event.ring_page )
-        munmap(xenaccess->mem_event.ring_page, PAGE_SIZE);
+        munmap(xenaccess->mem_event.ring_page, XC_PAGE_SIZE);
 
     if ( mem_access_enable )
     {
@@ -219,7 +212,6 @@ int xenaccess_teardown(xc_interface *xch, xenaccess_t *xenaccess)
     }
     xenaccess->xc_handle = NULL;
 
-    free(xenaccess->platform_info);
     free(xenaccess->domain_info);
     free(xenaccess);
 
@@ -328,32 +320,13 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r, domid_t domain_id)
     SHARED_RING_INIT((mem_event_sring_t *)xenaccess->mem_event.ring_page);
     BACK_RING_INIT(&xenaccess->mem_event.back_ring,
                    (mem_event_sring_t *)xenaccess->mem_event.ring_page,
-                   PAGE_SIZE);
+                   XC_PAGE_SIZE);
 
     /* Now that the ring is set, remove it from the guest's physmap */
     if ( xc_domain_decrease_reservation_exact(xch, 
                     xenaccess->mem_event.domain_id, 1, 0, &ring_pfn) )
         PERROR("Failed to remove ring from guest physmap");
 
-    /* Get platform info */
-    xenaccess->platform_info = malloc(sizeof(xc_platform_info_t));
-    if ( xenaccess->platform_info == NULL )
-    {
-        ERROR("Error allocating memory for platform info");
-        goto err;
-    }
-
-    rc = get_platform_info(xenaccess->xc_handle, domain_id,
-                           &xenaccess->platform_info->max_mfn,
-                           &xenaccess->platform_info->hvirt_start,
-                           &xenaccess->platform_info->pt_levels,
-                           &xenaccess->platform_info->guest_width);
-    if ( rc != 1 )
-    {
-        ERROR("Error getting platform info");
-        goto err;
-    }
-
     /* Get domaininfo */
     xenaccess->domain_info = malloc(sizeof(xc_domaininfo_t));
     if ( xenaccess->domain_info == NULL )
Acked-by
#2 lore raw
Quoted 14 lines · Tim Deegan

Hi,

Quoted 4 lines · Aravindh Puthiyaparambil (aravindp)

In places where xc handle is not in scope, use standard printf to display errors.

This is OK as far as it goes, but this code should not be using ERROR() &c at all -- those are libxc internals, and since we're not providing a logger argument or any flags to xc_interface_open, I don't think they do anything useful here. How about something like this (compile-tested only):

Quoted 3 lines

---8<----

tools/xen-access: don't use libxc internals directly.

Was the platform info stuff just completely unused?

Quoted 1 line
#3 lore raw
Quoted 25 lines

-----Original Message-----
From: Ian Campbell [mailto:[email protected]]
Sent: Tuesday, March 18, 2014 10:41 AM
To: Tim Deegan
Cc: Aravindh Puthiyaparambil (aravindp); [email protected]; Ian Jackson; Stefano Stabellini
Subject: Re: [PATCH] xen-access: Do not use ERROR out of xc handle scope

Quoted 3 lines · Tim Deegan

Hi,

At 20:15 +0000 on 17 Mar (1395083738), Aravindh Puthiyaparambil (aravindp)

Quoted 1 line · wrote:
Quoted 1 line

In places where xc handle is not in scope, use standard printf to display

errors.

Quoted 10 lines
Quoted 3 lines

This is OK as far as it goes, but this code should not be using ERROR() &c at all -- those are libxc internals, and since we're not providing a logger argument or any flags to xc_interface_open, I don't think they do anything useful here. How about something like this (compile-tested only):

Quoted 5 lines
Quoted 3 lines

---8<----

tools/xen-access: don't use libxc internals directly.

Was the platform info stuff just completely unused?

Yes it is completely unused. I am not sure why it was there in the first place.

Thanks,
Aravindh

Quoted 4 lines
Quoted 1 line
#4 lore raw
In reply to Tim Deegan
Quoted 15 lines

-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Tuesday, March 18, 2014 3:14 AM
To: Aravindh Puthiyaparambil (aravindp)
Cc: [email protected]; Ian Jackson; Stefano Stabellini; Ian Campbell
Subject: Re: [PATCH] xen-access: Do not use ERROR out of xc handle scope

Hi,

At 20:15 +0000 on 17 Mar (1395083738), Aravindh Puthiyaparambil (aravindp)

Quoted 1 line · wrote:

In places where xc handle is not in scope, use standard printf to display

errors.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

This is OK as far as it goes, but this code should not be using ERROR() &c at all -- those are libxc internals, and since we're not providing a logger argument or any flags to xc_interface_open, I don't think they do anything useful here. How about something like this (compile-tested only):

I tested this out and it works well. Thanks for fixing it right way. I only have one comment which is inline below.

Thanks,
Aravindh

Quoted 27 lines

---8<----

tools/xen-access: don't use libxc internals directly.

tools/tests/xen- renamed from tools/tests/xen-access/xen-access.c +0 -0

access/xen-access.c
index b00c05a..4ca0782 100644

tools/tests/xen-access/xen-access.c +3 -1
@@ -26,23 +26,24 @@
 * DEALINGS IN THE SOFTWARE.
 */
 
+#include <errno.h>
 #include <inttypes.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdbool.h>
+#include <string.h>
 #include <time.h>
 #include <signal.h>
 #include <unistd.h>
+#include <sys/mman.h>
 #include <sys/poll.h>
-#include <xc_private.h>

tools/tests/xen-access/xen-access.c line 44

Quoted 10 lines
tools/tests/xen-access/xen-access.c +2 -5
-#include <xg_save_restore.h>
 
+#include <xenctrl.h>
 #include <xen/mem_event.h>
 
-//#if 0
-#undef DPRINTF
-#define DPRINTF(a, b...) fprintf(stderr, a, ## b)
-//#endif
+#define DPRINTF(a, b...) fprintf(stderr, a "\n", ## b)

Can you please not add the new line here as it is causing an extra new line to be printed for the existing DPRINTFs? Or this patch can go in and I can submit another one which fixes the call sites.

Quoted 78 lines
tools/tests/xen-access/xen-access.c +3 -9
+#define ERROR(a, b...) fprintf(stderr, a "\n", ## b)
+#define PERROR(a, b...) fprintf(stderr, a ": %s\n", ## b, strerror(errno))
 
 /* Spinlock and mem event definitions */
 
@@ -104,17 +105,9 @@ typedef struct mem_event {
    spinlock_t ring_lock;
 } mem_event_t;
 
-typedef struct xc_platform_info {
-    unsigned long max_mfn;
-    unsigned long hvirt_start;
-    unsigned int  pt_levels;
-    unsigned int  guest_width;
-} xc_platform_info_t;
-
 typedef struct xenaccess {
    xc_interface *xc_handle;
 
-    xc_platform_info_t *platform_info;
    xc_domaininfo_t    *domain_info;
 
    mem_event_t mem_event;
@@ -178,7 +171,7 @@ int xenaccess_teardown(xc_interface *xch,
 xenaccess_t *xenaccess)
 
    /* Tear down domain xenaccess in Xen */
    if ( xenaccess->mem_event.ring_page )
-        munmap(xenaccess->mem_event.ring_page, PAGE_SIZE);
+        munmap(xenaccess->mem_event.ring_page, XC_PAGE_SIZE);
 
    if ( mem_access_enable )
    {
tools/tests/xen-access/xen-access.c +1 -21
@@ -219,7 +212,6 @@ int xenaccess_teardown(xc_interface *xch,
 xenaccess_t *xenaccess)
    }
    xenaccess->xc_handle = NULL;
 
-    free(xenaccess->platform_info);
    free(xenaccess->domain_info);
    free(xenaccess);
@@ -328,32 +320,13 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r,
 domid_t domain_id)
    SHARED_RING_INIT((mem_event_sring_t *)xenaccess-
 >mem_event.ring_page);
    BACK_RING_INIT(&xenaccess->mem_event.back_ring,
                   (mem_event_sring_t *)xenaccess->mem_event.ring_page,
-                   PAGE_SIZE);
+                   XC_PAGE_SIZE);
 
    /* Now that the ring is set, remove it from the guest's physmap */
    if ( xc_domain_decrease_reservation_exact(xch,
                    xenaccess->mem_event.domain_id, 1, 0, &ring_pfn) )
        PERROR("Failed to remove ring from guest physmap");
 
-    /* Get platform info */
-    xenaccess->platform_info = malloc(sizeof(xc_platform_info_t));
-    if ( xenaccess->platform_info == NULL )
-    {
-        ERROR("Error allocating memory for platform info");
-        goto err;
-    }
-
-    rc = get_platform_info(xenaccess->xc_handle, domain_id,
-                           &xenaccess->platform_info->max_mfn,
-                           &xenaccess->platform_info->hvirt_start,
-                           &xenaccess->platform_info->pt_levels,
-                           &xenaccess->platform_info->guest_width);
-    if ( rc != 1 )
-    {
-        ERROR("Error getting platform info");
-        goto err;
-    }
-
    /* Get domaininfo */
    xenaccess->domain_info = malloc(sizeof(xc_domaininfo_t));
    if ( xenaccess->domain_info == NULL )
#5 lore raw

PATCH v2 1/1 tools/xen-access: don't use libxc internals directly.

---
v2: DPRINTF() doesn't need to add a newline.

tools/tests/xen-access/xen-access.c +8 -35
index b00c05a..f6522a1 100644
@@ -26,23 +26,24 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include <errno.h>
 #include <inttypes.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdbool.h>
+#include <string.h>
 #include <time.h>
 #include <signal.h>
 #include <unistd.h>
+#include <sys/mman.h>
 #include <sys/poll.h>
-#include <xc_private.h>
-#include <xg_save_restore.h>
 
+#include <xenctrl.h>
 #include <xen/mem_event.h>
 
-//#if 0
-#undef DPRINTF
 #define DPRINTF(a, b...) fprintf(stderr, a, ## b)
-//#endif
+#define ERROR(a, b...) fprintf(stderr, a "\n", ## b)
+#define PERROR(a, b...) fprintf(stderr, a ": %s\n", ## b, strerror(errno))
 
 /* Spinlock and mem event definitions */
 
@@ -104,17 +105,9 @@ typedef struct mem_event {
     spinlock_t ring_lock;
 } mem_event_t;
 
-typedef struct xc_platform_info {
-    unsigned long max_mfn;
-    unsigned long hvirt_start;
-    unsigned int  pt_levels;
-    unsigned int  guest_width;
-} xc_platform_info_t;
-
 typedef struct xenaccess {
     xc_interface *xc_handle;
 
-    xc_platform_info_t *platform_info;
     xc_domaininfo_t    *domain_info;
 
     mem_event_t mem_event;
@@ -178,7 +171,7 @@ int xenaccess_teardown(xc_interface *xch, xenaccess_t *xenaccess)
 
     /* Tear down domain xenaccess in Xen */
     if ( xenaccess->mem_event.ring_page )
-        munmap(xenaccess->mem_event.ring_page, PAGE_SIZE);
+        munmap(xenaccess->mem_event.ring_page, XC_PAGE_SIZE);
 
     if ( mem_access_enable )
     {
@@ -219,7 +212,6 @@ int xenaccess_teardown(xc_interface *xch, xenaccess_t *xenaccess)
     }
     xenaccess->xc_handle = NULL;
 
-    free(xenaccess->platform_info);
     free(xenaccess->domain_info);
     free(xenaccess);
 
@@ -328,32 +320,13 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r, domid_t domain_id)
     SHARED_RING_INIT((mem_event_sring_t *)xenaccess->mem_event.ring_page);
     BACK_RING_INIT(&xenaccess->mem_event.back_ring,
                    (mem_event_sring_t *)xenaccess->mem_event.ring_page,
-                   PAGE_SIZE);
+                   XC_PAGE_SIZE);
 
     /* Now that the ring is set, remove it from the guest's physmap */
     if ( xc_domain_decrease_reservation_exact(xch, 
                     xenaccess->mem_event.domain_id, 1, 0, &ring_pfn) )
         PERROR("Failed to remove ring from guest physmap");
 
-    /* Get platform info */
-    xenaccess->platform_info = malloc(sizeof(xc_platform_info_t));
-    if ( xenaccess->platform_info == NULL )
-    {
-        ERROR("Error allocating memory for platform info");
-        goto err;
-    }
-
-    rc = get_platform_info(xenaccess->xc_handle, domain_id,
-                           &xenaccess->platform_info->max_mfn,
-                           &xenaccess->platform_info->hvirt_start,
-                           &xenaccess->platform_info->pt_levels,
-                           &xenaccess->platform_info->guest_width);
-    if ( rc != 1 )
-    {
-        ERROR("Error getting platform info");
-        goto err;
-    }
-
     /* Get domaininfo */
     xenaccess->domain_info = malloc(sizeof(xc_domaininfo_t));
     if ( xenaccess->domain_info == NULL )