Skip to contentSkip to conversation

libxl: event fixes for Roger

PATCH In review Ian Jackson 3 participants 12 messages Jul 20, 2012 – Jul 26, 2012

lore
Share & tools
lore
#0 lore raw

cover libxl: event fixes for Roger

These two patches have been posted before. The fd reentrancy hazard fix (1/1) has been improved and simplified.

NB I have NOT executed these against recent xen-unstable tip and I haven't tested 1/1 since I changed it. These should not be applied without testing. But I thought it better to post them than leave them lurking in my tree.

1/2 libxl: fix reentrancy hazard in fd event processing 2/2 libxl: do not blunder on if bootloader fails

#1 lore raw

PATCH 1/2 libxl: fix reentrancy hazard in fd event processing

In afterpoll_internal, the callback functions may register and deregister events arbitrarily. This means that we need to consider the reentrancy-safety of the event machinery state variables.

Most of the code is safe but the fd handling is not. Fix this by arranging to restart the fd scan loop every time we call one of these callback functions.

For this loop to terminate, we modify afterpoll_check_fd so that it returns only once for each of afterpoll's efds.

Another possible solution would be simply to return from afterpoll_internal after calling efd->func. That would be a small and more obviously correct change but would prevent the process from handling more than one fd event with a single call to poll.

This is apropos of a report from Roger Pau Monne to me (pers.comm.) of this crash on NetBSD:

Program terminated with signal 11, Segmentation fault.
#0 0x00007f7ff743131b in afterpoll_check_fd (poller=<optimized out>, fds=0x7f7ff7b241c0, nfds=7, fd=-1, events=1)

      at libxl_event.c:856

856 if (fds[slot].fd != fd)

-
Changes since initial posting:
* Abolish the rindices[] return parameter from afterpoll_check_fd. Instead have afterpoll_check_fd invalidate the slot itself, so that it always only returns nonzero once for each slot.
* Better commit message.

---
 tools/libxl/libxl_event.c    |   90 +++++++++++++++++++++++++++++++++++-------
 tools/libxl/libxl_internal.h |    2 +-
 2 files changed, 76 insertions(+), 16 deletions(-)
tools/libxl/libxl_event.c +75 -15
index 1957505..8902314 100644
@@ -839,18 +839,26 @@ int libxl_osevent_beforepoll(libxl_ctx *ctx, int *nfds_io,
 static int afterpoll_check_fd(libxl__poller *poller,
                               const struct pollfd *fds, int nfds,
                               int fd, int events)
-    /* returns mask of events which were requested and occurred */
+    /* Returns mask of events which were requested and occurred.  Will
+     * return nonzero only once for each (poller,fd,events)
+     * combination, until the next beforepoll.  If events from
+     * different combinations overlap, between one such combination
+     * and all distinct combinations will produce nonzero returns. */
 {
     if (fd >= poller->fd_rindices_allocd)
         /* added after we went into poll, have to try again */
         return 0;
 
+    events |= POLLERR | POLLHUP;
+
     int i, revents = 0;
     for (i=0; i<3; i++) {
-        int slot = poller->fd_rindices[fd][i];
+        int *slotp = &poller->fd_rindices[fd][i];
+        int slot = *slotp;
 
         if (slot >= nfds)
-            /* stale slot entry; again, added afterwards */
+            /* stale slot entry (again, added afterwards), */
+            /* or slot for which we have already returned nonzero */
             continue;
 
         if (fds[slot].fd != fd)
@@ -858,11 +866,16 @@ static int afterpoll_check_fd(libxl__poller *poller,
             continue;
 
         assert(!(fds[slot].revents & POLLNVAL));
-        revents |= fds[slot].revents;
-    }
 
-    /* we mask in case requested events have changed */
-    revents &= (events | POLLERR | POLLHUP);
+        /* we mask in case requested events have changed */
+        int slot_revents = fds[slot].revents & events;
+        if (!slot_revents)
+            /* this slot is for a different set of events */
+            continue;
+
+        revents |= slot_revents;
+        *slotp = INT_MAX; /* so that next time we'll see slot >= nfds */
+    }
 
     return revents;
 }
@@ -876,16 +889,63 @@ static void afterpoll_internal(libxl__egc *egc, libxl__poller *poller,
     EGC_GC;
     libxl__ev_fd *efd;
 
-    LIBXL_LIST_FOREACH(efd, &CTX->efds, entry) {
-        if (!efd->events)
-            continue;
+    /*
+     * Warning! Reentrancy hazards!
+     *
+     * Many parts of this function eventually call arbitrary callback
+     * functions which may modify the event handling data structures.
+     *
+     * Of the data structures used here:
+     *
+     *   egc, poller, now
+     *                are allocated by our caller and relate to the
+     *                current thread and its call stack down into the
+     *                event machinery; it is not freed until we return.
+     *                So it is safe.
+     *
+     *   fds          is either what application passed into
+     *                libxl_osevent_afterpoll (which, although this
+     *                isn't explicitly stated, clearly must remain
+     *                valid until libxl_osevent_afterpoll returns) or
+     *                it's poller->fd_polls which is modified only by
+     *                our (non-recursive) caller eventloop_iteration.
+     *   
+     *   CTX          comes from our caller, and applications are
+     *                forbidden from destroying it while we are running.
+     *                So the ctx pointer itself is safe to use; now
+     *                for its contents:
+     *
+     *   CTX->etimes  is used in a simple reentrancy-safe manner.
+     *
+     *   CTX->efds    is more complicated; see below.
+     */
 
-        int revents = afterpoll_check_fd(poller,fds,nfds, efd->fd,efd->events);
-        if (revents) {
-            DBG("ev_fd=%p occurs fd=%d events=%x revents=%x",
-                efd, efd->fd, efd->events, revents);
-            efd->func(egc, efd, efd->fd, efd->events, revents);
+    for (;;) {
+        /* We restart our scan of fd events whenever we call a
+         * callback function.  This is necessary because such
+         * a callback might make arbitrary changes to CTX->efds.
+         * We invalidate the fd_rindices[] entries which were used
+         * so that we don't call the same function again. */
+        int revents;
+
+        LIBXL_LIST_FOREACH(efd, &CTX->efds, entry) {
+
+            if (!efd->events)
+                continue;
+
+            revents = afterpoll_check_fd(poller,fds,nfds,
+                                         efd->fd,efd->events);
+            if (revents)
+                goto found_fd_event;
         }
+        /* no ordinary fd events, then */
+        break;
+
+    found_fd_event:
+        DBG("ev_fd=%p occurs fd=%d events=%x revents=%x",
+            efd, efd->fd, efd->events, revents);
+
+        efd->func(egc, efd, efd->fd, efd->events, revents);
     }
 
     if (afterpoll_check_fd(poller,fds,nfds, poller->wakeup_pipe[0],POLLIN)) {
tools/libxl/libxl_internal.h +1 -1
index 2781398..e938660 100644
@@ -272,7 +272,7 @@ struct libxl__poller {
     int fd_polls_allocd;
 
     int fd_rindices_allocd;
-    int (*fd_rindices)[3]; /* see libxl_osevent_beforepoll */
+    int (*fd_rindices)[3]; /* see libxl_event.c:beforepoll_internal */
 
     int wakeup_pipe[2]; /* 0 means no fd allocated */
 };
Signature · 1 line
1.7.2.5
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing

Quoted 16 lines · Ian Jackson

In afterpoll_internal, the callback functions may register and deregister events arbitrarily. This means that we need to consider the reentrancy-safety of the event machinery state variables.

Most of the code is safe but the fd handling is not. Fix this by arranging to restart the fd scan loop every time we call one of these callback functions.

For this loop to terminate, we modify afterpoll_check_fd so that it returns only once for each of afterpoll's efds.

Another possible solution would be simply to return from afterpoll_internal after calling efd->func. That would be a small and more obviously correct change but would prevent the process from handling more than one fd event with a single call to poll.

Quoted 7 lines

This is apropos of a report from Roger Pau Monne to me (pers.comm.) of this crash on NetBSD:

Program terminated with signal 11, Segmentation fault.
#0 0x00007f7ff743131b in afterpoll_check_fd (poller=<optimized out>, fds=0x7f7ff7b241c0, nfds=7, fd=-1, events=1)

      at libxl_event.c:856

856 if (fds[slot].fd != fd)

Has Roger or you tested this now?

It looks plausible to me.

tools/libxl/libxl_internal.h struct libxl__poller { · line 275

Quoted 10 lines
tools/libxl/libxl_internal.h +1 -1
index 2781398..e938660 100644
@@ -272,7 +272,7 @@ struct libxl__poller {
     int fd_polls_allocd;
 
     int fd_rindices_allocd;
-    int (*fd_rindices)[3]; /* see libxl_osevent_beforepoll */
+    int (*fd_rindices)[3]; /* see libxl_event.c:beforepoll_internal */

do you mean afterpoll here?

#3 lore raw

Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing

Ian Campbell writes ("Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing"):

Quoted 1 line · Ian Jackson

...

Quoted 6 lines
Quoted 4 lines

Program terminated with signal 11, Segmentation fault.
#0 0x00007f7ff743131b in afterpoll_check_fd (poller=<optimized out>, fds=0x7f7ff7b241c0, nfds=7, fd=-1, events=1)

      at libxl_event.c:856

856 if (fds[slot].fd != fd)

Has Roger or you tested this now?

I still haven't tested the new version I'm afraid.

Quoted 1 line

It looks plausible to me.

:-).

Quoted 7 lines
Quoted 7 lines
tools/libxl/libxl_internal.h +0 -0
index 2781398..e938660 100644
@@ -272,7 +272,7 @@ struct libxl__poller {
     int fd_polls_allocd;
 
Quoted 5 lines

tools/libxl/libxl_internal.h struct libxl__poller { · line 275

Quoted 3 lines
tools/libxl/libxl_internal.h +1 -1
     int fd_rindices_allocd;
-    int (*fd_rindices)[3]; /* see libxl_osevent_beforepoll */
+    int (*fd_rindices)[3]; /* see libxl_event.c:beforepoll_internal */

do you mean afterpoll here?

No. I mean this comment:

/*
* In order to be able to efficiently find the libxl__ev_fd for a
* struct poll during _afterpoll, we maintain a shadow data
* structure in CTX->fd_rindices: each fd corresponds to a slot in
* fd_rindices, and each element in the rindices is three indices
* into the fd array (for POLLIN, POLLPRI and POLLOUT). */

which is halfway down beforepoll_internal.

Ian.

#4 lore raw

Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing

Quoted 11 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing"):

Quoted 1 line · Ian Jackson

...

Quoted 6 lines
Quoted 4 lines

Program terminated with signal 11, Segmentation fault.
#0 0x00007f7ff743131b in afterpoll_check_fd (poller=<optimized out>, fds=0x7f7ff7b241c0, nfds=7, fd=-1, events=1)

      at libxl_event.c:856

856 if (fds[slot].fd != fd)

Has Roger or you tested this now?

I still haven't tested the new version I'm afraid.

OK, I'll hold off on committing then.

Quoted 28 lines
Quoted 2 lines

It looks plausible to me.

:-).

Quoted 7 lines
Quoted 7 lines
tools/libxl/libxl_internal.h +0 -0
index 2781398..e938660 100644
@@ -272,7 +272,7 @@ struct libxl__poller {
     int fd_polls_allocd;
 
Quoted 5 lines

tools/libxl/libxl_internal.h struct libxl__poller { · line 275

Quoted 3 lines
tools/libxl/libxl_internal.h +1 -1
     int fd_rindices_allocd;
-    int (*fd_rindices)[3]; /* see libxl_osevent_beforepoll */
+    int (*fd_rindices)[3]; /* see libxl_event.c:beforepoll_internal */

do you mean afterpoll here?

No. I mean this comment:

/*
* In order to be able to efficiently find the libxl__ev_fd for a
* struct poll during _afterpoll, we maintain a shadow data
* structure in CTX->fd_rindices: each fd corresponds to a slot in
* fd_rindices, and each element in the rindices is three indices
* into the fd array (for POLLIN, POLLPRI and POLLOUT). */

Quoted 1 line

which is halfway down beforepoll_internal.

Ah, OK. I thought it was a reference to one of the new comments. Nevermnd.

Quoted 2 lines

Ian.

#5 lore raw

Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing

Ian Campbell writes ("Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing"):

Quoted 9 lines · Ian Jackson
Quoted 8 lines

Ian Campbell writes ("Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing"):

Quoted 1 line · Ian Jackson

...

Quoted 5 lines
Quoted 4 lines

Program terminated with signal 11, Segmentation fault.
#0 0x00007f7ff743131b in afterpoll_check_fd (poller=<optimized out>, fds=0x7f7ff7b241c0, nfds=7, fd=-1, events=1)

      at libxl_event.c:856

856 if (fds[slot].fd != fd)

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Has Roger or you tested this now?

I still haven't tested the new version I'm afraid.

OK, I'll hold off on committing then.

I have now tested these as part of my testing of Roger's hotplug series.

Ian.

#6 lore raw

Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing

Quoted 10 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing"):

Quoted 9 lines · Ian Jackson
Quoted 8 lines

Ian Campbell writes ("Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing"):

Quoted 1 line · Ian Jackson

...

Quoted 5 lines
Quoted 4 lines

Program terminated with signal 11, Segmentation fault.
#0 0x00007f7ff743131b in afterpoll_check_fd (poller=<optimized out>, fds=0x7f7ff7b241c0, nfds=7, fd=-1, events=1)

      at libxl_event.c:856

856 if (fds[slot].fd != fd)

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Has Roger or you tested this now?

I still haven't tested the new version I'm afraid.

OK, I'll hold off on committing then.

I have now tested these as part of my testing of Roger's hotplug series.

Thx. Added Roger's Tested-by and my Acked-by and committed.

#7 lore raw

Re: [PATCH 1/2] libxl: fix reentrancy hazard in fd event processing

In reply to Ian Campbell
Quoted 20 lines · Ian Campbell
Quoted 19 lines · Ian Jackson

In afterpoll_internal, the callback functions may register and deregister events arbitrarily. This means that we need to consider the reentrancy-safety of the event machinery state variables.

Most of the code is safe but the fd handling is not. Fix this by arranging to restart the fd scan loop every time we call one of these callback functions.

For this loop to terminate, we modify afterpoll_check_fd so that it returns only once for each of afterpoll's efds.

Another possible solution would be simply to return from afterpoll_internal after calling efd->func. That would be a small and more obviously correct change but would prevent the process from handling more than one fd event with a single call to poll.

This is apropos of a report from Roger Pau Monne to me (pers.comm.) of this crash on NetBSD:

Quoted 6 lines
Quoted 4 lines

Program terminated with signal 11, Segmentation fault.
#0 0x00007f7ff743131b in afterpoll_check_fd (poller=<optimized out>, fds=0x7f7ff7b241c0, nfds=7, fd=-1, events=1)

      at libxl_event.c:856

856 if (fds[slot].fd != fd)

Has Roger or you tested this now?

This works ok.

Quoted 18 lines

It looks plausible to me.

tools/libxl/libxl_internal.h struct libxl__poller { · line 275

Quoted 10 lines
tools/libxl/libxl_internal.h +1 -1
index 2781398..e938660 100644
@@ -272,7 +272,7 @@ struct libxl__poller {
     int fd_polls_allocd;
 
     int fd_rindices_allocd;
-    int (*fd_rindices)[3]; /* see libxl_osevent_beforepoll */
+    int (*fd_rindices)[3]; /* see libxl_event.c:beforepoll_internal */

do you mean afterpoll here?

#8 lore raw

PATCH 2/2 libxl: do not blunder on if bootloader fails

In reply to Ian Jackson

If the bootloader failed, we would call the creation failure callback but _also_ blunder on trying to recreate the domain, due to a missing "return".

---
 tools/libxl/libxl_create.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
tools/libxl/libxl_create.c +4 -1
index 294a73f..0bfa8ba 100644
@@ -672,7 +672,10 @@ static void domcreate_bootloader_done(libxl__egc *egc,
     libxl__srm_restore_autogen_callbacks *const callbacks =
         &dcs->shs.callbacks.restore.a;
 
-    if (rc) domcreate_rebuild_done(egc, dcs, rc);
+    if (rc) {
+        domcreate_rebuild_done(egc, dcs, rc);
+        return;
+    }
 
     /* consume bootloader outputs. state->pv_{kernel,ramdisk} have
      * been initialised by the bootloader already.
Signature · 1 line
1.7.2.5
Acked-by on v1 2/2
#9 lore raw

Re: [PATCH 2/2] libxl: do not blunder on if bootloader fails

Quoted 6 lines · Ian Jackson

If the bootloader failed, we would call the creation failure callback but _also_ blunder on trying to recreate the domain, due to a missing "return".

I have been running with this one applied locally since you first posted it so I think it is good to go in.

Quoted 20 lines
---
 tools/libxl/libxl_create.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
tools/libxl/libxl_create.c +4 -1
index 294a73f..0bfa8ba 100644
@@ -672,7 +672,10 @@ static void domcreate_bootloader_done(libxl__egc *egc,
     libxl__srm_restore_autogen_callbacks *const callbacks =
         &dcs->shs.callbacks.restore.a;
 
-    if (rc) domcreate_rebuild_done(egc, dcs, rc);
+    if (rc) {
+        domcreate_rebuild_done(egc, dcs, rc);
+        return;
+    }
 
     /* consume bootloader outputs. state->pv_{kernel,ramdisk} have
      * been initialised by the bootloader already.
#10 lore raw

Re: [PATCH 2/2] libxl: do not blunder on if bootloader fails

Quoted 5 lines · Ian Campbell
Quoted 4 lines · Ian Jackson

If the bootloader failed, we would call the creation failure callback but _also_ blunder on trying to recreate the domain, due to a missing "return".

Quoted 7 lines
Quoted 2 lines

I have been running with this one applied locally since you first posted it so I think it is good to go in.

I have applied this.

Quoted 28 lines
Quoted 11 lines
---
 tools/libxl/libxl_create.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
tools/libxl/libxl_create.c +0 -0
index 294a73f..0bfa8ba 100644
@@ -672,7 +672,10 @@ static void domcreate_bootloader_done(libxl__egc *egc,
     libxl__srm_restore_autogen_callbacks *const callbacks =

tools/libxl/libxl_create.c static void domcreate_bootloader_done(libxl__egc *egc, · line 681

Quoted 10 lines
tools/libxl/libxl_create.c +4 -1
         &dcs->shs.callbacks.restore.a;
 
-    if (rc) domcreate_rebuild_done(egc, dcs, rc);
+    if (rc) {
+        domcreate_rebuild_done(egc, dcs, rc);
+        return;
+    }
 
     /* consume bootloader outputs. state->pv_{kernel,ramdisk} have
      * been initialised by the bootloader already.

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

#11 lore raw
In reply to Ian Jackson

Ian Jackson writes ("[PATCH 0/2] libxl: event fixes for Roger"):

Quoted 10 lines

These two patches have been posted before. The fd reentrancy hazard fix (1/1) has been improved and simplified.

NB I have NOT executed these against recent xen-unstable tip and I haven't tested 1/1 since I changed it. These should not be applied without testing. But I thought it better to post them than leave them lurking in my tree.

1/2 libxl: fix reentrancy hazard in fd event processing 2/2 libxl: do not blunder on if bootloader fails

Roger, do you want me to prepare a patch to fix the bootloader pty HUP bug which shows up on NetBSD ?

Ian.