All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tools/libxl: Correct use of phyinfo_{init, dispose}()
@ 2015-08-04 18:58 Andrew Cooper
  2015-08-04 21:25 ` Wei Liu
  2015-08-05  8:40 ` Ian Campbell
  0 siblings, 2 replies; 5+ messages in thread
From: Andrew Cooper @ 2015-08-04 18:58 UTC (permalink / raw)
  To: Xen-devel; +Cc: Andrew Cooper, Ian Jackson, Ian Campbell, Wei Liu

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Ian Campbell <Ian.Campbell@citrix.com>
CC: Ian Jackson <Ian.Jackson@eu.citrix.com>
CC: Wei Liu <wei.liu2@citrix.com>

For my cpuid work which I have just started, I added an array to physinfo.
Coverity then frowned at me when it spotted all the memory leaks.

Technically speaking, 4.6 isn't broken due to not having an allocation to free
in _dispose(), but this patch might still be worth taking in 4.6.
---
 tools/libxl/libxl.c       |   12 ++++++++++++
 tools/libxl/libxl_utils.c |    9 ++++-----
 tools/libxl/xl_cmdimpl.c  |    6 +++++-
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 083f099..56cf6ed 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -4615,6 +4615,7 @@ static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb,
     libxl_ctx *ctx = libxl__gc_owner(gc);
 
     libxl_dominfo_init(&info);
+    libxl_physinfo_init(&physinfo);
 
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
@@ -4653,6 +4654,8 @@ static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb,
     if (rc < 0)
         goto out;
 
+    libxl_physinfo_dispose(&physinfo);
+    libxl_physinfo_init(&physinfo);
     rc = libxl_get_physinfo(ctx, &physinfo);
     if (rc < 0)
         goto out;
@@ -4679,6 +4682,7 @@ static int libxl__fill_dom0_memory_info(libxl__gc *gc, uint32_t *target_memkb,
     }
 
     libxl_dominfo_dispose(&info);
+    libxl_physinfo_dispose(&physinfo);
     return rc;
 }
 
@@ -4944,6 +4948,8 @@ int libxl_get_free_memory(libxl_ctx *ctx, uint32_t *memkb)
     libxl_physinfo info;
     GC_INIT(ctx);
 
+    libxl_physinfo_init(&info);
+
     rc = libxl_get_physinfo(ctx, &info);
     if (rc < 0)
         goto out;
@@ -4952,6 +4958,7 @@ int libxl_get_free_memory(libxl_ctx *ctx, uint32_t *memkb)
 
 out:
     GC_FREE;
+    libxl_physinfo_dispose(&info);
     return rc;
 }
 
@@ -4962,7 +4969,11 @@ int libxl_wait_for_free_memory(libxl_ctx *ctx, uint32_t domid, uint32_t
     libxl_physinfo info;
     GC_INIT(ctx);
 
+    libxl_physinfo_init(&info);
+
     while (wait_secs > 0) {
+        libxl_physinfo_dispose(&info);
+        libxl_physinfo_init(&info);
         rc = libxl_get_physinfo(ctx, &info);
         if (rc < 0)
             goto out;
@@ -4977,6 +4988,7 @@ int libxl_wait_for_free_memory(libxl_ctx *ctx, uint32_t domid, uint32_t
 
 out:
     GC_FREE;
+    libxl_physinfo_dispose(&info);
     return rc;
 }
 
diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c
index bfc9699..13dc2de 100644
--- a/tools/libxl/libxl_utils.c
+++ b/tools/libxl/libxl_utils.c
@@ -848,11 +848,10 @@ int libxl__count_physical_sockets(libxl__gc *gc, int *sockets)
     libxl_physinfo_init(&info);
 
     rc = libxl_get_physinfo(CTX, &info);
-    if (rc)
-        return rc;
-
-    *sockets = info.nr_cpus / info.threads_per_core
-                            / info.cores_per_socket;
+    if (!rc) {
+        *sockets = info.nr_cpus / info.threads_per_core
+                                / info.cores_per_socket;
+    }
 
     libxl_physinfo_dispose(&info);
     return 0;
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 499a05c..9b77b10 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -5492,9 +5492,11 @@ static void output_physinfo(void)
     libxl_bitmap cpumap;
     int n = 0;
 
+    libxl_physinfo_init(&info);
+
     if (libxl_get_physinfo(ctx, &info) != 0) {
         fprintf(stderr, "libxl_physinfo failed.\n");
-        return;
+        goto out;
     }
     printf("nr_cpus                : %d\n", info.nr_cpus);
     printf("max_cpu_id             : %d\n", info.max_cpu_id);
@@ -5527,6 +5529,8 @@ static void output_physinfo(void)
         printf("free_cpus              : %d\n", n);
         free(cpumap.map);
     }
+
+ out:
     libxl_physinfo_dispose(&info);
     return;
 }
-- 
1.7.10.4

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

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

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-04 18:58 [PATCH] tools/libxl: Correct use of phyinfo_{init, dispose}() Andrew Cooper
2015-08-04 21:25 ` Wei Liu
2015-08-04 21:27   ` Andrew Cooper
2015-08-05  8:40 ` Ian Campbell
2015-08-05  8:52   ` Andrew Cooper

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.