Skip to contentSkip to conversation

tools: Miscellanous fixes for 4.4

PATCH Superseded Ian Jackson 3 participants 9 messages Jan 17, 2014 – Jan 20, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover tools: Miscellanous fixes for 4.4

Here are three bugfixes. 1/3 is quite important. I'm CCing it to Jim Fehlig in case he happens to see fallout from it with libvirt.

1/3 libxl: events: Pass correct nfds to poll
2/3 xl: Free optdata_begin when saving domain config 3/3 xenstore: xs_suspend_evtchn_port: always free portstr

These are based on staging, but they will also apply without trouble to my recent libxl fork series.

#1 lore raw

PATCH 1/3 libxl: events: Pass correct nfds to poll

libxl_event.c:eventloop_iteration would pass the allocated pollfds array size, rather than the used size, to poll (and to afterpoll_internal).

The effect is that if the number of fds to poll on reduces, libxl will poll on stale entries. Because of the way the return value from poll is processed these stale entries are often harmless because any events coming back from poll ignored by libxl. However, it could cause malfunctions:

It could result in unwanted SIGTTIN/SIGTTOU/SIGPIPE, for example, if the fd has been reused to refer to an object which can generate those signals. Alternatively, it could result in libxl spinning if the stale entry refers to an fd which happens now to be ready for the previously-requested operation.

I have tested this with a localhost migration and inspected the strace output.

Cc Jim Fehlig <[email protected]>
Cc Ian Campbell <[email protected]>
---
 tools/libxl/libxl_event.c |    9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tools/libxl/libxl_event.c +4 -5
index bdef7ac..1c48fee 100644
@@ -1386,7 +1386,7 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {
      * can unlock it when it polls.
      */
     EGC_GC;
-    int rc;
+    int rc, nfds;
     struct timeval now;
     
     rc = libxl__gettimeofday(gc, &now);
@@ -1395,7 +1395,7 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {
     int timeout;
 
     for (;;) {
-        int nfds = poller->fd_polls_allocd;
+        nfds = poller->fd_polls_allocd;
         timeout = -1;
         rc = beforepoll_internal(gc, poller, &nfds, poller->fd_polls,
                                  &timeout, now);
@@ -1413,7 +1413,7 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {
     }
 
     CTX_UNLOCK;
-    rc = poll(poller->fd_polls, poller->fd_polls_allocd, timeout);
+    rc = poll(poller->fd_polls, nfds, timeout);
     CTX_LOCK;
 
     if (rc < 0) {
@@ -1428,8 +1428,7 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {
     rc = libxl__gettimeofday(gc, &now);
     if (rc) goto out;
 
-    afterpoll_internal(egc, poller,
-                       poller->fd_polls_allocd, poller->fd_polls, now);
+    afterpoll_internal(egc, poller, nfds, poller->fd_polls, now);
 
     rc = 0;
  out:
Signature · 1 line
1.7.10.4
Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] libxl: events: Pass correct nfds to poll

Quoted 16 lines · Ian Jackson

libxl_event.c:eventloop_iteration would pass the allocated pollfds array size, rather than the used size, to poll (and to afterpoll_internal).

The effect is that if the number of fds to poll on reduces, libxl will poll on stale entries. Because of the way the return value from poll is processed these stale entries are often harmless because any events coming back from poll ignored by libxl. However, it could cause malfunctions:

It could result in unwanted SIGTTIN/SIGTTOU/SIGPIPE, for example, if the fd has been reused to refer to an object which can generate those signals. Alternatively, it could result in libxl spinning if the stale entry refers to an fd which happens now to be ready for the previously-requested operation.

Quoted 5 lines

I have tested this with a localhost migration and inspected the strace output.

Quoted 45 lines
---
 tools/libxl/libxl_event.c |    9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
tools/libxl/libxl_event.c +4 -5
index bdef7ac..1c48fee 100644
@@ -1386,7 +1386,7 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {
      * can unlock it when it polls.
      */
     EGC_GC;
-    int rc;
+    int rc, nfds;
     struct timeval now;
 
     rc = libxl__gettimeofday(gc, &now);
@@ -1395,7 +1395,7 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {
     int timeout;
 
     for (;;) {
-        int nfds = poller->fd_polls_allocd;
+        nfds = poller->fd_polls_allocd;
         timeout = -1;
         rc = beforepoll_internal(gc, poller, &nfds, poller->fd_polls,
                                  &timeout, now);
@@ -1413,7 +1413,7 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {
     }
 
     CTX_UNLOCK;
-    rc = poll(poller->fd_polls, poller->fd_polls_allocd, timeout);
+    rc = poll(poller->fd_polls, nfds, timeout);
     CTX_LOCK;
 
     if (rc < 0) {
@@ -1428,8 +1428,7 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {
     rc = libxl__gettimeofday(gc, &now);
     if (rc) goto out;
 
-    afterpoll_internal(egc, poller,
-                       poller->fd_polls_allocd, poller->fd_polls, now);
+    afterpoll_internal(egc, poller, nfds, poller->fd_polls, now);
 
     rc = 0;
  out:
#3 lore raw

PATCH 2/3 xl: Free optdata_begin when saving domain config

In reply to Ian Jackson

This makes valgrind a bit happier.

---
 tools/libxl/xl_cmdimpl.c |    2 ++
 1 file changed, 2 insertions(+)
tools/libxl/xl_cmdimpl.c +2 -0
index d93e01b..aff6f90 100644
@@ -3442,6 +3442,8 @@ static void save_domain_core_writeconfig(int fd, const char *source,
                      ctx, fd, optdata_begin, hdr.optional_data_len,
                      source, "header"));
 
+    free(optdata_begin);
+
     fprintf(stderr, "Saving to %s new xl format (info"
             " 0x%"PRIx32"/0x%"PRIx32"/%"PRIu32")\n",
             source, hdr.mandatory_flags, hdr.optional_flags,
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [PATCH 2/3] xl: Free optdata_begin when saving domain config

Quoted 4 lines · Ian Jackson

This makes valgrind a bit happier.

This is CID: 1055903

It is probably worth noting that there are 11 other resource leaks Coverity has found in this file.

~Andrew

Quoted 17 lines
---
 tools/libxl/xl_cmdimpl.c |    2 ++
 1 file changed, 2 insertions(+)
tools/libxl/xl_cmdimpl.c +2 -0
index d93e01b..aff6f90 100644
@@ -3442,6 +3442,8 @@ static void save_domain_core_writeconfig(int fd, const char *source,
                      ctx, fd, optdata_begin, hdr.optional_data_len,
                      source, "header"));
 
+    free(optdata_begin);
+
     fprintf(stderr, "Saving to %s new xl format (info"
             " 0x%"PRIx32"/0x%"PRIx32"/%"PRIu32")\n",
             source, hdr.mandatory_flags, hdr.optional_flags,
Acked-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] xl: Free optdata_begin when saving domain config

In reply to Ian Jackson
Quoted 3 lines · Ian Jackson

This makes valgrind a bit happier.

Quoted 17 lines
---
 tools/libxl/xl_cmdimpl.c |    2 ++
 1 file changed, 2 insertions(+)
tools/libxl/xl_cmdimpl.c +2 -0
index d93e01b..aff6f90 100644
@@ -3442,6 +3442,8 @@ static void save_domain_core_writeconfig(int fd, const char *source,
                      ctx, fd, optdata_begin, hdr.optional_data_len,
                      source, "header"));
 
+    free(optdata_begin);
+
     fprintf(stderr, "Saving to %s new xl format (info"
             " 0x%"PRIx32"/0x%"PRIx32"/%"PRIu32")\n",
             source, hdr.mandatory_flags, hdr.optional_flags,
#6 lore raw

PATCH 3/3 xenstore: xs_suspend_evtchn_port: always free portstr

In reply to Ian Jackson

If portstr!=NULL but plen==0 this function would leak portstr.

---
 tools/xenstore/xs.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
tools/xenstore/xs.c +6 -3
index a636498..9cd99eb 100644
@@ -1095,12 +1095,15 @@ int xs_suspend_evtchn_port(int domid)
     portstr = xs_read(xs, XBT_NULL, path, &plen);
     xs_daemon_close(xs);
 
-    if (!portstr || !plen)
-        return -1;
+    if (!portstr || !plen) {
+        port = -1;
+	goto out;
+    }
 
     port = atoi(portstr);
-    free(portstr);
 
+out:
+    free(portstr);
     return port;
 }
 
Signature · 1 line
1.7.10.4
#7 lore raw

Re: [PATCH 3/3] xenstore: xs_suspend_evtchn_port: always free portstr

Quoted 13 lines · Ian Jackson

If portstr!=NULL but plen==0 this function would leak portstr.

---
 tools/xenstore/xs.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
tools/xenstore/xs.c +0 -0
index a636498..9cd99eb 100644

tools/xenstore/xs.c int xs_suspend_evtchn_port(int domid) · line 1100

Quoted 8 lines
tools/xenstore/xs.c +3 -2
@@ -1095,12 +1095,15 @@ int xs_suspend_evtchn_port(int domid)
     portstr = xs_read(xs, XBT_NULL, path, &plen);
     xs_daemon_close(xs);
 
-    if (!portstr || !plen)
-        return -1;
+    if (!portstr || !plen) {
+        port = -1;
+	goto out;

You have mixed tabs/spaces here.

~Andrew

Quoted 10 lines
tools/xenstore/xs.c +3 -1
+    }
 
     port = atoi(portstr);
-    free(portstr);
 
+out:
+    free(portstr);
     return port;
 }
 
Acked-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] xenstore: xs_suspend_evtchn_port: always free portstr

In reply to Ian Jackson
Quoted 3 lines · Ian Jackson

If portstr!=NULL but plen==0 this function would leak portstr.

With the whitespace fixed:

Quoted 27 lines
---
 tools/xenstore/xs.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
tools/xenstore/xs.c +6 -3
index a636498..9cd99eb 100644
@@ -1095,12 +1095,15 @@ int xs_suspend_evtchn_port(int domid)
     portstr = xs_read(xs, XBT_NULL, path, &plen);
     xs_daemon_close(xs);
 
-    if (!portstr || !plen)
-        return -1;
+    if (!portstr || !plen) {
+        port = -1;
+	goto out;
+    }
 
     port = atoi(portstr);
-    free(portstr);
 
+out:
+    free(portstr);
     return port;
 }