Skip to contentSkip to conversation

libs/evtchn: Remove active handler on clean-up or failure

PATCH In review Julien Grall 4 participants 15 messages Nov 10, 2017 – Nov 16, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libs/evtchn: Remove active handler on clean-up or failure

Commit 89d55473ed16543044a31d1e0d4660cf5a3f49df "xentoolcore_restrict_all:
Implement for libxenevtchn" added a call to register allowing to restrict the event channel.

However, the call to deregister the handler was not performed if open failed or when closing the event channel. This will result to corrupt the list of handlers and potentially crash the application later one.

Fix it by calling xentoolcore_deregister_active_handle on failure and closure.

---

This patch is fixing a bug introduced after the code freeze by "xentoolcore_restrict_all: Implement for libxenevtchn".

The call to xentoolcore_deregister_active_handle is done at the same place as for the grants. But I am not convinced this is thread safe as there are potential race between close the event channel and restict handler. Do we care about that?

---
 tools/libs/evtchn/core.c | 2 ++
 1 file changed, 2 insertions(+)
tools/libs/evtchn/core.c +2 -0
index 14b7549a6b..2dba58bf00 100644
@@ -56,6 +56,7 @@ xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags)
 
 err:
     osdep_evtchn_close(xce);
+    xentoolcore__deregister_active_handle(&xce->tc_ah);
     xtl_logger_destroy(xce->logger_tofree);
     free(xce);
     return NULL;
@@ -69,6 +70,7 @@ int xenevtchn_close(xenevtchn_handle *xce)
         return 0;
 
     rc = osdep_evtchn_close(xce);
+    xentoolcore__deregister_active_handle(&xce->tc_ah);
     xtl_logger_destroy(xce->logger_tofree);
     free(xce);
     return rc;
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 10 lines · Julien Grall

Commit 89d55473ed16543044a31d1e0d4660cf5a3f49df "xentoolcore_restrict_all:
Implement for libxenevtchn" added a call to register allowing to restrict the event channel.

However, the call to deregister the handler was not performed if open failed or when closing the event channel. This will result to corrupt the list of handlers and potentially crash the application later one.

Fix it by calling xentoolcore_deregister_active_handle on failure and closure.

Thanks for fixing this.

Quoted 5 lines

---

Quoted 7 lines

This patch is fixing a bug introduced after the code freeze by "xentoolcore_restrict_all: Implement for libxenevtchn".

The call to xentoolcore_deregister_active_handle is done at the same place as for the grants. But I am not convinced this is thread safe as there are potential race between close the event channel and restict handler. Do we care about that?

Both xentoolcore__deregister_active_handle() and xentoolcore_restrict_all() hold the same lock when mutating the list so there shouldn't be a problem with the list itself.

However, I think it should call xentoolcore__deregister_active_handle() _before_ calling osdep_evtchn_close() to avoid trying to restrict a closed fd or some other fd that happens to have the same number.

I think all the other libs need to be fixed as well, unless there was a reason it was done this way.

Signature · 6 lines
Ross Lagerwall

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

Ross Lagerwall writes ("Re: [PATCH for-4.10] libs/evtchn: Remove active handler on clean-up or failure"):

Quoted 8 lines · Julien Grall
Quoted 7 lines

Commit 89d55473ed16543044a31d1e0d4660cf5a3f49df "xentoolcore_restrict_all:
Implement for libxenevtchn" added a call to register allowing to restrict the event channel.

However, the call to deregister the handler was not performed if open failed or when closing the event channel. This will result to corrupt the list of handlers and potentially crash the application later one.

Sorry for not spotting this during review.
The fix is correct as far as it goes, so:

Quoted 4 lines
Quoted 4 lines

The call to xentoolcore_deregister_active_handle is done at the same place as for the grants. But I am not convinced this is thread safe as there are potential race between close the event channel and restict handler. Do we care about that?

...

Quoted 3 lines

However, I think it should call xentoolcore__deregister_active_handle() _before_ calling osdep_evtchn_close() to avoid trying to restrict a closed fd or some other fd that happens to have the same number.

You are right. But this slightly weakens the guarantee provided by xentoolcore_restrict_all.

Quoted 2 lines

I think all the other libs need to be fixed as well, unless there was a reason it was done this way.

I will send a further patch. In the meantime I suggest we apply Julien's fix.

Ian.

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

#3 lore raw
Quoted 19 lines · Ian Jackson

Ross Lagerwall writes ("Re: [PATCH for-4.10] libs/evtchn: Remove active handler on clean-up or failure"):

Quoted 8 lines · Julien Grall
Quoted 7 lines

Commit 89d55473ed16543044a31d1e0d4660cf5a3f49df "xentoolcore_restrict_all:
Implement for libxenevtchn" added a call to register allowing to restrict the event channel.

However, the call to deregister the handler was not performed if open failed or when closing the event channel. This will result to corrupt the list of handlers and potentially crash the application later one.

Sorry for not spotting this during review.
The fix is correct as far as it goes, so:

Quoted 4 lines
Quoted 4 lines

The call to xentoolcore_deregister_active_handle is done at the same place as for the grants. But I am not convinced this is thread safe as there are potential race between close the event channel and restict handler. Do we care about that?

Quoted 8 lines

...

Quoted 3 lines

However, I think it should call xentoolcore__deregister_active_handle() _before_ calling osdep_evtchn_close() to avoid trying to restrict a closed fd or some other fd that happens to have the same number.

You are right. But this slightly weakens the guarantee provided by xentoolcore_restrict_all.

Now that I look at it, a similar scenario can happen during open. Since the handle is registered before it is actually opened, a concurrent xentoolcore_restrict_all() will try to restrict a handle that it not properly set up.

I think it is OK if xentoolcore_restrict_all() works with any open handle where a handle is defined as open if it has _completed_ the call to e.g. xenevtchn_open() and has not yet called xenevtchn_close().

Signature · 6 lines
Ross Lagerwall

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

Ross Lagerwall writes ("Re: [PATCH for-4.10] libs/evtchn: Remove active handler on clean-up or failure"):

Quoted 4 lines

Now that I look at it, a similar scenario can happen during open. Since the handle is registered before it is actually opened, a concurrent xentoolcore_restrict_all() will try to restrict a handle that it not properly set up.

I think this is not a problem because the handle has thing->fd = -1. So the restrict call will be a no-op (or give EBADF).

Ian.

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

#5 lore raw
In reply to Ian Jackson

Hi,

Quoted 24 lines · Ian Jackson

Ross Lagerwall writes ("Re: [PATCH for-4.10] libs/evtchn: Remove active handler on clean-up or failure"):

Quoted 8 lines · Julien Grall
Quoted 7 lines

Commit 89d55473ed16543044a31d1e0d4660cf5a3f49df "xentoolcore_restrict_all:
Implement for libxenevtchn" added a call to register allowing to restrict the event channel.

However, the call to deregister the handler was not performed if open failed or when closing the event channel. This will result to corrupt the list of handlers and potentially crash the application later one.

Sorry for not spotting this during review.
The fix is correct as far as it goes, so:

Quoted 4 lines
Quoted 4 lines

The call to xentoolcore_deregister_active_handle is done at the same place as for the grants. But I am not convinced this is thread safe as there are potential race between close the event channel and restict handler. Do we care about that?

...

Quoted 3 lines

However, I think it should call xentoolcore__deregister_active_handle() _before_ calling osdep_evtchn_close() to avoid trying to restrict a closed fd or some other fd that happens to have the same number.

Quoted 8 lines

You are right. But this slightly weakens the guarantee provided by xentoolcore_restrict_all.

Quoted 2 lines

I think all the other libs need to be fixed as well, unless there was a reason it was done this way.

I will send a further patch. In the meantime I suggest we apply Julien's fix.

I am going to leave the decision to you and Wei. It feels a bit odd to release-ack my patch :).

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#6 lore raw
Quoted 30 lines · Julien Grall

Hi,

Quoted 27 lines · Ian Jackson

Ross Lagerwall writes ("Re: [PATCH for-4.10] libs/evtchn: Remove active handler on clean-up or failure"):

Quoted 8 lines · Julien Grall
Quoted 7 lines

Commit 89d55473ed16543044a31d1e0d4660cf5a3f49df "xentoolcore_restrict_all:
Implement for libxenevtchn" added a call to register allowing to restrict the event channel.

However, the call to deregister the handler was not performed if open failed or when closing the event channel. This will result to corrupt the list of handlers and potentially crash the application later one.

Sorry for not spotting this during review.
The fix is correct as far as it goes, so:

Quoted 4 lines
Quoted 4 lines

The call to xentoolcore_deregister_active_handle is done at the same place as for the grants. But I am not convinced this is thread safe as there are potential race between close the event channel and restict handler. Do we care about that?

...

Quoted 3 lines

However, I think it should call xentoolcore__deregister_active_handle() _before_ calling osdep_evtchn_close() to avoid trying to restrict a closed fd or some other fd that happens to have the same number.

You are right. But this slightly weakens the guarantee provided by xentoolcore_restrict_all.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I think all the other libs need to be fixed as well, unless there was a reason it was done this way.

I will send a further patch. In the meantime I suggest we apply Julien's fix.

I am going to leave the decision to you and Wei. It feels a bit odd to release-ack my patch :).

We can only commit patches that are both acked and release-acked. The latter gives RM control over when the patch should be applied. Sometimes it is better to wait until something else happens (like getting the tree to a stable state).

That's how I used release-ack anyway.

For this particular patch, my interpretation of what you just said is you've given us release-ack and we can apply this patch anytime. I will commit it soon.

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

#7 lore raw

Hi Wei,

Quoted 40 lines · Wei Liu
Quoted 30 lines · Julien Grall

Hi,

Quoted 27 lines · Ian Jackson

Ross Lagerwall writes ("Re: [PATCH for-4.10] libs/evtchn: Remove active handler on clean-up or failure"):

Quoted 8 lines · Julien Grall
Quoted 7 lines

Commit 89d55473ed16543044a31d1e0d4660cf5a3f49df "xentoolcore_restrict_all:
Implement for libxenevtchn" added a call to register allowing to restrict the event channel.

However, the call to deregister the handler was not performed if open failed or when closing the event channel. This will result to corrupt the list of handlers and potentially crash the application later one.

Sorry for not spotting this during review.
The fix is correct as far as it goes, so:

Quoted 4 lines
Quoted 4 lines

The call to xentoolcore_deregister_active_handle is done at the same place as for the grants. But I am not convinced this is thread safe as there are potential race between close the event channel and restict handler. Do we care about that?

...

Quoted 3 lines

However, I think it should call xentoolcore__deregister_active_handle() _before_ calling osdep_evtchn_close() to avoid trying to restrict a closed fd or some other fd that happens to have the same number.

You are right. But this slightly weakens the guarantee provided by xentoolcore_restrict_all.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I think all the other libs need to be fixed as well, unless there was a reason it was done this way.

I will send a further patch. In the meantime I suggest we apply Julien's fix.

I am going to leave the decision to you and Wei. It feels a bit odd to release-ack my patch :).

Quoted 6 lines

We can only commit patches that are both acked and release-acked. The latter gives RM control over when the patch should be applied. Sometimes it is better to wait until something else happens (like getting the tree to a stable state).

That's how I used release-ack anyway.

I feel a bit odd to release-ack my patch and usually for Arm patches deferred to Stefano the decision whether the patch is suitable for the release.

Quoted 4 lines

For this particular patch, my interpretation of what you just said is you've given us release-ack and we can apply this patch anytime. I will commit it soon.

Thanks! I hope it will fixed some osstest failure.

Cheers,

Signature · 6 lines
Julien Grall

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

PATCH 1/1 tools: xentoolcore_restrict_all: Do deregistration before close

In reply to Ian Jackson

Closing the fd before unhooking it from the list runs the risk that a concurrent thread calls xentoolcore_restrict_all will operate on the old fd value, which might refer to a new fd by then. So we need to do it in the other order.

Sadly this weakens the guarantee provided by xentoolcore_restrict_all slight, but not (I think) in a problematic way. It would be possible to implement the previous guarantee, but it would involve replacing all of the close() calls in all of the individual osdep parts of all of the individual libraries with calls to a new function which does
dup2("/dev/null", thing->fd); pthread_mutex_lock(&handles_lock);
thing->fd = -1;
pthread_mutex_unlock(&handles_lock);
close(fd);
which would be terribly tedious.

---
 tools/libs/call/core.c                             | 4 ++--
 tools/libs/devicemodel/core.c                      | 4 ++--
 tools/libs/evtchn/core.c                           | 4 ++--
 tools/libs/foreignmemory/core.c                    | 4 ++--
 tools/libs/gnttab/gnttab_core.c                    | 4 ++--
 tools/libs/toolcore/include/xentoolcore.h          | 9 +++++++++
 tools/libs/toolcore/include/xentoolcore_internal.h | 6 ++++--
 tools/xenstore/xs.c                                | 4 ++--
 8 files changed, 25 insertions(+), 14 deletions(-)
tools/libs/call/core.c +2 -2
index b256fce..f3a3400 100644
@@ -59,8 +59,8 @@ xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags)
     return xcall;
 
 err:
-    osdep_xencall_close(xcall);
     xentoolcore__deregister_active_handle(&xcall->tc_ah);
+    osdep_xencall_close(xcall);
     xtl_logger_destroy(xcall->logger_tofree);
     free(xcall);
     return NULL;
@@ -73,8 +73,8 @@ int xencall_close(xencall_handle *xcall)
     if ( !xcall )
         return 0;
 
-    rc = osdep_xencall_close(xcall);
     xentoolcore__deregister_active_handle(&xcall->tc_ah);
+    rc = osdep_xencall_close(xcall);
     buffer_release_cache(xcall);
     xtl_logger_destroy(xcall->logger_tofree);
     free(xcall);
tools/libs/devicemodel/core.c +2 -2
index b66d4f9..355b7de 100644
@@ -68,8 +68,8 @@ xendevicemodel_handle *xendevicemodel_open(xentoollog_logger *logger,
 
 err:
     xtl_logger_destroy(dmod->logger_tofree);
-    xencall_close(dmod->xcall);
     xentoolcore__deregister_active_handle(&dmod->tc_ah);
+    xencall_close(dmod->xcall);
     free(dmod);
     return NULL;
 }
@@ -83,8 +83,8 @@ int xendevicemodel_close(xendevicemodel_handle *dmod)
 
     rc = osdep_xendevicemodel_close(dmod);
 
-    xencall_close(dmod->xcall);
     xentoolcore__deregister_active_handle(&dmod->tc_ah);
+    xencall_close(dmod->xcall);
     xtl_logger_destroy(dmod->logger_tofree);
     free(dmod);
     return rc;
tools/libs/evtchn/core.c +2 -2
index 2dba58b..aff6ecf 100644
@@ -55,8 +55,8 @@ xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags)
     return xce;
 
 err:
-    osdep_evtchn_close(xce);
     xentoolcore__deregister_active_handle(&xce->tc_ah);
+    osdep_evtchn_close(xce);
     xtl_logger_destroy(xce->logger_tofree);
     free(xce);
     return NULL;
@@ -69,8 +69,8 @@ int xenevtchn_close(xenevtchn_handle *xce)
     if ( !xce )
         return 0;
 
-    rc = osdep_evtchn_close(xce);
     xentoolcore__deregister_active_handle(&xce->tc_ah);
+    rc = osdep_evtchn_close(xce);
     xtl_logger_destroy(xce->logger_tofree);
     free(xce);
     return rc;
tools/libs/foreignmemory/core.c +2 -2
index 79b24d2..7c8562a 100644
@@ -57,8 +57,8 @@ xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger *logger,
     return fmem;
 
 err:
-    osdep_xenforeignmemory_close(fmem);
     xentoolcore__deregister_active_handle(&fmem->tc_ah);
+    osdep_xenforeignmemory_close(fmem);
     xtl_logger_destroy(fmem->logger_tofree);
     free(fmem);
     return NULL;
@@ -71,8 +71,8 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem)
     if ( !fmem )
         return 0;
 
-    rc = osdep_xenforeignmemory_close(fmem);
     xentoolcore__deregister_active_handle(&fmem->tc_ah);
+    rc = osdep_xenforeignmemory_close(fmem);
     xtl_logger_destroy(fmem->logger_tofree);
     free(fmem);
     return rc;
tools/libs/gnttab/gnttab_core.c +2 -2
index 5f761e5..98f1591 100644
@@ -54,8 +54,8 @@ xengnttab_handle *xengnttab_open(xentoollog_logger *logger, unsigned open_flags)
     return xgt;
 
 err:
-    osdep_gnttab_close(xgt);
     xentoolcore__deregister_active_handle(&xgt->tc_ah);
+    osdep_gnttab_close(xgt);
     xtl_logger_destroy(xgt->logger_tofree);
     free(xgt);
     return NULL;
@@ -68,8 +68,8 @@ int xengnttab_close(xengnttab_handle *xgt)
     if ( !xgt )
         return 0;
 
-    rc = osdep_gnttab_close(xgt);
     xentoolcore__deregister_active_handle(&xgt->tc_ah);
+    rc = osdep_gnttab_close(xgt);
     xtl_logger_destroy(xgt->logger_tofree);
     free(xgt);
     return rc;
tools/libs/toolcore/include/xentoolcore.h +9 -0
index 8d28c2d..b3a3c93 100644
@@ -39,6 +39,15 @@
  * fail (even though such a call is potentially meaningful).
  * (If called again with a different domid, it will necessarily fail.)
  *
+ * Note for multi-threaded programs: If xentoolcore_restrict_all is
+ * called concurrently with a function which /or closes Xen library
+ * handles (e.g.  libxl_ctx_free, xs_close), the restriction is only
+ * guaranteed to be effective after all of the closing functions have
+ * returned, even if that is later than the return from
+ * xentoolcore_restrict_all.  (Of course if xentoolcore_restrict_all
+ * it is called concurrently with opening functions, the new handles
+ * might or might not be restricted.)
+ *
  *  ====================================================================
  *  IMPORTANT - IMPLEMENTATION STATUS
  *
tools/libs/toolcore/include/xentoolcore_internal.h +4 -2
index dbdb1dd..04f5848 100644
@@ -48,8 +48,10 @@
  *     4. ONLY THEN actually open the relevant fd or whatever
  *
  *   III. during the "close handle" function
- *     1. FIRST close the relevant fd or whatever
- *     2. call xentoolcore__deregister_active_handle
+ *     1. FIRST call xentoolcore__deregister_active_handle
+ *     2. close the relevant fd or whatever
+ *
+ * [ III(b). Do the same as III for error exit from the open function. ]
  *
  *   IV. in the restrict_callback function
  *     * Arrange that the fd (or other handle) can no longer by used
tools/xenstore/xs.c +2 -2
index 23f3f09..abffd9c 100644
@@ -279,9 +279,9 @@ err:
 	saved_errno = errno;
 
 	if (h) {
+		xentoolcore__deregister_active_handle(&h->tc_ah);
 		if (h->fd >= 0)
 			close(h->fd);
-		xentoolcore__deregister_active_handle(&h->tc_ah);
 	}
 	free(h);
 
@@ -342,8 +342,8 @@ static void close_fds_free(struct xs_handle *h) {
 		close(h->watch_pipe[1]);
 	}
 
-        close(h->fd);
 	xentoolcore__deregister_active_handle(&h->tc_ah);
+        close(h->fd);
         
 	free(h);
 }
Signature · 7 lines
2.1.4


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

Re: [PATCH] tools: xentoolcore_restrict_all: Do deregistration before close

Quoted 7 lines · Ian Jackson

Closing the fd before unhooking it from the list runs the risk that a concurrent thread calls xentoolcore_restrict_all will operate on the old fd value, which might refer to a new fd by then. So we need to do it in the other order.

Sadly this weakens the guarantee provided by xentoolcore_restrict_all slight, but not (I think) in a problematic way. It would be possible

slightly

Quoted 11 lines

to implement the previous guarantee, but it would involve replacing all of the close() calls in all of the individual osdep parts of all of the individual libraries with calls to a new function which does

   dup2("/dev/null", thing->fd);
   pthread_mutex_lock(&handles_lock);
   thing->fd = -1;
   pthread_mutex_unlock(&handles_lock);
   close(fd);

which would be terribly tedious.

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

#10 lore raw

Re: [PATCH] tools: xentoolcore_restrict_all: Do deregistration before close

Hi,

Quoted 21 lines · Wei Liu
Quoted 7 lines · Ian Jackson

Closing the fd before unhooking it from the list runs the risk that a concurrent thread calls xentoolcore_restrict_all will operate on the old fd value, which might refer to a new fd by then. So we need to do it in the other order.

Sadly this weakens the guarantee provided by xentoolcore_restrict_all slight, but not (I think) in a problematic way. It would be possible

slightly

Quoted 10 lines

to implement the previous guarantee, but it would involve replacing all of the close() calls in all of the individual osdep parts of all of the individual libraries with calls to a new function which does

    dup2("/dev/null", thing->fd);
    pthread_mutex_lock(&handles_lock);
    thing->fd = -1;
    pthread_mutex_unlock(&handles_lock);
    close(fd);

which would be terribly tedious.

Quoted 3 lines
Quoted 1 line

I think this is 4.10 material, xentoolcore was introduced in this release and it would be good to have it right from now. I want to confirm that you are both happy with that?

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH for-4.10] tools: xentoolcore_restrict_all: Do deregistration before close

Julien Grall writes ("Re: [PATCH] tools: xentoolcore_restrict_all: Do deregistration before close"):

Quoted 3 lines

I think this is 4.10 material, xentoolcore was introduced in this release and it would be good to have it right from now. I want to confirm that you are both happy with that?

Yes, absolutely. Sorry, I forgot the for-4.10 tag in the Subject.

Ian.

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

#12 lore raw

Re: [PATCH for-4.10] tools: xentoolcore_restrict_all: Do deregistration before close

Hi Ian,

Quoted 6 lines · Ian Jackson

Julien Grall writes ("Re: [PATCH] tools: xentoolcore_restrict_all: Do deregistration before close"):

Quoted 3 lines

I think this is 4.10 material, xentoolcore was introduced in this release and it would be good to have it right from now. I want to confirm that you are both happy with that?

Yes, absolutely. Sorry, I forgot the for-4.10 tag in the Subject.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH] tools: xentoolcore_restrict_all: Do deregistration before close

In reply to Ian Jackson
Quoted 17 lines · Ian Jackson

Closing the fd before unhooking it from the list runs the risk that a concurrent thread calls xentoolcore_restrict_all will operate on the old fd value, which might refer to a new fd by then. So we need to do it in the other order.

Sadly this weakens the guarantee provided by xentoolcore_restrict_all slight, but not (I think) in a problematic way. It would be possible to implement the previous guarantee, but it would involve replacing all of the close() calls in all of the individual osdep parts of all of the individual libraries with calls to a new function which does

    dup2("/dev/null", thing->fd);
    pthread_mutex_lock(&handles_lock);
    thing->fd = -1;
    pthread_mutex_unlock(&handles_lock);
    close(fd);

which would be terribly tedious.

...

tools/libs/toolcore/include/xentoolcore.h line 43

Quoted 10 lines
tools/libs/toolcore/include/xentoolcore.h +2 -0
index 8d28c2d..b3a3c93 100644
@@ -39,6 +39,15 @@
   * fail (even though such a call is potentially meaningful).
   * (If called again with a different domid, it will necessarily fail.)
   *
+ * Note for multi-threaded programs: If xentoolcore_restrict_all is
+ * called concurrently with a function which /or closes Xen library

"which /or closes..." - Is this a typo?

Quoted 43 lines
tools/libs/toolcore/include/xentoolcore.h +7 -0
+ * handles (e.g.  libxl_ctx_free, xs_close), the restriction is only
+ * guaranteed to be effective after all of the closing functions have
+ * returned, even if that is later than the return from
+ * xentoolcore_restrict_all.  (Of course if xentoolcore_restrict_all
+ * it is called concurrently with opening functions, the new handles
+ * might or might not be restricted.)
+ *
   *  ====================================================================
   *  IMPORTANT - IMPLEMENTATION STATUS
   *
tools/libs/toolcore/include/xentoolcore_internal.h +4 -2
index dbdb1dd..04f5848 100644
@@ -48,8 +48,10 @@
   *     4. ONLY THEN actually open the relevant fd or whatever
   *
   *   III. during the "close handle" function
- *     1. FIRST close the relevant fd or whatever
- *     2. call xentoolcore__deregister_active_handle
+ *     1. FIRST call xentoolcore__deregister_active_handle
+ *     2. close the relevant fd or whatever
+ *
+ * [ III(b). Do the same as III for error exit from the open function. ]
   *
   *   IV. in the restrict_callback function
   *     * Arrange that the fd (or other handle) can no longer by used
tools/xenstore/xs.c +1 -1
index 23f3f09..abffd9c 100644
@@ -279,9 +279,9 @@ err:
  	saved_errno = errno;
 
  	if (h) {
+		xentoolcore__deregister_active_handle(&h->tc_ah);
  		if (h->fd >= 0)
  			close(h->fd);
-		xentoolcore__deregister_active_handle(&h->tc_ah);
  	}
  	free(h);
 

tools/xenstore/xs.c static void close_fds_free(struct xs_handle *h) { · line 347

Quoted 7 lines
tools/xenstore/xs.c +1 -1
@@ -342,8 +342,8 @@ static void close_fds_free(struct xs_handle *h) {
  		close(h->watch_pipe[1]);
  	}
 
-        close(h->fd);
  	xentoolcore__deregister_active_handle(&h->tc_ah);
+        close(h->fd);
 

Since the rest of this file uses tabs, you may as well use tabs for this line as well.

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

#14 lore raw

Re: [PATCH] tools: xentoolcore_restrict_all: Do deregistration before close

Ross Lagerwall writes ("Re: [PATCH] tools: xentoolcore_restrict_all: Do deregistration before close"):

Quoted 5 lines · Ian Jackson
Quoted 2 lines

+ * Note for multi-threaded programs: If xentoolcore_restrict_all is
+ * called concurrently with a function which /or closes Xen library

"which /or closes..." - Is this a typo?

Yes, fixed, thanks.

Quoted 7 lines

tools/xenstore/xs.c static void close_fds_free(struct xs_handle *h) { · line 346

Quoted 4 lines
tools/xenstore/xs.c +1 -1
-        close(h->fd);
  	xentoolcore__deregister_active_handle(&h->tc_ah);
+        close(h->fd);

Since the rest of this file uses tabs, you may as well use tabs for this line as well.

I didn't change the use of tabs vs. the use of spaces.

Quoted 1 line

Thanks,
Ian.

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