Skip to contentSkip to conversation

xl: event machinery fixes

PATCH In review Wei Liu 3 participants 13 messages Feb 2, 2017 – Feb 3, 2017

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

cover xl: event machinery fixes

Wei Liu (2):
xl: free event in DOMAIN_RESTART_RENAME error path
xl: disable events earlier for shutdown event

 tools/libxl/xl_cmdimpl.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
Signature · 7 lines
2.11.0


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

PATCH 1/2 xl: free event in DOMAIN_RESTART_RENAME error path

Otherwise it is leaked. Found by code inspection.

---
Cc: Ian Jackson <[email protected]>
Cc: Fatih Acar <[email protected]>

Backport candidate.

---
 tools/libxl/xl_cmdimpl.c | 1 +
 1 file changed, 1 insertion(+)
tools/libxl/xl_cmdimpl.c +1 -0
index 7e8a8ae5c4..9bf10dfcb2 100644
@@ -3145,6 +3145,7 @@ start:
             case DOMAIN_RESTART_RENAME:
                 if (domid_soft_reset == INVALID_DOMID &&
                     !preserve_domain(&domid, event, &d_config)) {
+                    libxl_event_free(event);
                     /* If we fail then exit leaving the old domain in place. */
                     ret = -1;
                     goto out;
Signature · 7 lines
2.11.0


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

Re: [PATCH 1/2] xl: free event in DOMAIN_RESTART_RENAME error path

Quoted 17 lines · Wei Liu

Otherwise it is leaked. Found by code inspection.

---
Cc: Ian Jackson <[email protected]>
Cc: Fatih Acar <[email protected]>

Backport candidate.

---
 tools/libxl/xl_cmdimpl.c | 1 +
 1 file changed, 1 insertion(+)
tools/libxl/xl_cmdimpl.c +0 -0
index 7e8a8ae5c4..9bf10dfcb2 100644

tools/libxl/xl_cmdimpl.c start: · line 3148

Quoted 4 lines
tools/libxl/xl_cmdimpl.c +1 -0
@@ -3145,6 +3145,7 @@ start:
             case DOMAIN_RESTART_RENAME:
                 if (domid_soft_reset == INVALID_DOMID &&
                     !preserve_domain(&domid, event, &d_config)) {
+                    libxl_event_free(event);

Stupid me.

This should have been

libxl_event_free(ctx, event);

Quoted 6 lines
tools/libxl/xl_cmdimpl.c +0 -0
                     /* If we fail then exit leaving the old domain in place. */
                     ret = -1;
                     goto out;
Signature · 1 line
2.11.0

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

author
#4 lore raw

PATCH 2/2 xl: disable events earlier for shutdown event

In reply to Wei Liu

We need to disable event machinery when the guest shuts down. It doesn't really matter where we disable it as long as it is within the branch for shutdown event.

Move the code snippet before handle_domain_death, so that d_config is not yet updated and we have the correct ->num_disks. And the free diskws and set it to NULL, so that diskws gets automatically set up again when xl goes over the domain creation code path.

---
Cc: Ian Jackson <[email protected]>
Cc: Fatih Acar <[email protected]>

Fatih, please test this series to see if it fixes your issue.

Ian, backport candidate?

---
 tools/libxl/xl_cmdimpl.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/libxl/xl_cmdimpl.c +5 -3
index 9bf10dfcb2..7df6db1ec9 100644
@@ -3137,6 +3137,11 @@ start:
             LOG("Domain %u has shut down, reason code %d 0x%x", domid,
                 event->u.domain_shutdown.shutdown_reason,
                 event->u.domain_shutdown.shutdown_reason);
+            libxl_evdisable_domain_death(ctx, deathw);
+            deathw = NULL;
+            evdisable_disk_ejects(diskws, d_config.num_disks);
+            free(diskws);
+            diskws = NULL;
             switch (handle_domain_death(&domid, event, &d_config)) {
             case DOMAIN_RESTART_SOFT_RESET:
                 domid_soft_reset = domid;
@@ -3154,9 +3159,6 @@ start:
                 /* Otherwise fall through and restart. */
             case DOMAIN_RESTART_NORMAL:
                 libxl_event_free(ctx, event);
-                libxl_evdisable_domain_death(ctx, deathw);
-                deathw = NULL;
-                evdisable_disk_ejects(diskws, d_config.num_disks);
                 /* discard any other events which may have been generated */
                 while (!(ret = libxl_event_check(ctx, &event,
                                                  LIBXL_EVENTMASK_ALL, 0,0))) {
Signature · 7 lines
2.11.0


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

Re: [PATCH 2/2] xl: disable events earlier for shutdown event

Wei Liu writes ("[PATCH 2/2] xl: disable events earlier for shutdown event"):

Quoted 3 lines

We need to disable event machinery when the guest shuts down. It doesn't really matter where we disable it as long as it is within the branch for shutdown event.

I don't think this is necessary. My intent was that libxl_ctx_free would automatically disable all event generation, and that it is therefore not necessary to call _disable.

This isn't documented particularly clearly, but libxl_event.h says:

* An evgen is associated with the libxl_ctx used for its creation.
* After libxl_ctx_free, all corresponding evgen handles become
* invalid and must no longer be passed to evdisable.

This implies it's legal to call libxl_ctx_free with some events enabled.

But I think I don't really understand what the original bug is.

Ian.

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

author
#6 lore raw

Re: [PATCH 2/2] xl: disable events earlier for shutdown event

Quoted 9 lines · Ian Jackson

Wei Liu writes ("[PATCH 2/2] xl: disable events earlier for shutdown event"):

Quoted 3 lines

We need to disable event machinery when the guest shuts down. It doesn't really matter where we disable it as long as it is within the branch for shutdown event.

I don't think this is necessary. My intent was that libxl_ctx_free would automatically disable all event generation, and that it is therefore not necessary to call _disable.

Quoted 9 lines

This isn't documented particularly clearly, but libxl_event.h says:

* An evgen is associated with the libxl_ctx used for its creation.
* After libxl_ctx_free, all corresponding evgen handles become
* invalid and must no longer be passed to evdisable.

This implies it's legal to call libxl_ctx_free with some events enabled.

That's right. But this is not saying there is bug in the event machinery.

Quoted 2 lines

But I think I don't really understand what the original bug is.

The original bug is that:

1. boot a guest with no disks, so diskws is NULL, num_disks == 0
2. some removable disks are added, and domain config updated
3. guest reboots, xl gets shutdown event
4. handle_domain_death gets the latest d_config, num_disks != 0 now
5. try to disable disk eject events with evdisable_disk_ejects -> BOOM

So basically 5 needs to happen before 4. Moving that snippet seems to do the trick, and then freeing diskws + setting it to NULL makes the reboot path automatically re-register diskws.

Wei.

Quoted 1 line

Ian.

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

#7 lore raw

Re: [PATCH 2/2] xl: disable events earlier for shutdown event

Wei Liu writes ("Re: [PATCH 2/2] xl: disable events earlier for shutdown event"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

But I think I don't really understand what the original bug is.

The original bug is that:

Ah.

Quoted 9 lines

1. boot a guest with no disks, so diskws is NULL, num_disks == 0
2. some removable disks are added, and domain config updated
3. guest reboots, xl gets shutdown event
4. handle_domain_death gets the latest d_config, num_disks != 0 now
5. try to disable disk eject events with evdisable_disk_ejects -> BOOM

So basically 5 needs to happen before 4. Moving that snippet seems to do the trick, and then freeing diskws + setting it to NULL makes the reboot path automatically re-register diskws.

Surely the right answer is to make evdisable_disk_ejects tolerate whatever it finds in diskws (including diskws==NULL). It could be idempotent too.

That way nothing depends on the exact ordering.

Ian.

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

author
#8 lore raw

Re: [PATCH 2/2] xl: disable events earlier for shutdown event

Quoted 14 lines · Ian Jackson

Wei Liu writes ("Re: [PATCH 2/2] xl: disable events earlier for shutdown event"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

But I think I don't really understand what the original bug is.

The original bug is that:

Ah.

Quoted 6 lines

1. boot a guest with no disks, so diskws is NULL, num_disks == 0
2. some removable disks are added, and domain config updated
3. guest reboots, xl gets shutdown event
4. handle_domain_death gets the latest d_config, num_disks != 0 now
5. try to disable disk eject events with evdisable_disk_ejects -> BOOM

Quoted 8 lines
Quoted 3 lines

So basically 5 needs to happen before 4. Moving that snippet seems to do the trick, and then freeing diskws + setting it to NULL makes the reboot path automatically re-register diskws.

Surely the right answer is to make evdisable_disk_ejects tolerate whatever it finds in diskws (including diskws==NULL). It could be idempotent too.

No, handling NULL is not enough. It could well be possible that diskws is not NULL but then num_disks grows, thus making evdisable_disk_ejects access out of bound.

The other solution is to have a dedicated counter for diskws instead of relying on num_disks.

---8<---
From 43dc119327923ad6237e2d4d7bde5ec1147d9007 Mon Sep 17 00:00:00 2001
From: Wei Liu <[email protected]>
Date: Thu, 2 Feb 2017 16:16:53 +0000
Subject: [PATCH] xl: track size of diskws with a dedicated counter

The num_disks field can change during guest lifetime. Don't use that as the size of diskws, use a dedicated counter instead.

Also free diskws and reset diskws to NULL after disabling events so that it will be automatically re-created when the guest reboots.

---
 tools/libxl/xl_cmdimpl.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +7 -2
index b25ac6efa0..358757fd09 100644
@@ -2811,6 +2811,7 @@ static int create_domain(struct domain_create *dom_info)
     int ret, rc;
     libxl_evgen_domain_death *deathw = NULL;
     libxl_evgen_disk_eject **diskws = NULL; /* one per disk */
+    unsigned int num_diskws = 0;
     void *config_data = 0;
     int config_len = 0;
     int restore_fd = -1;
@@ -3119,8 +3120,9 @@ start:
         diskws = xmalloc(sizeof(*diskws) * d_config.num_disks);
         for (i = 0; i < d_config.num_disks; i++)
             diskws[i] = NULL;
+        num_diskws = d_config.num_disks;
     }
-    for (i = 0; i < d_config.num_disks; i++) {
+    for (i = 0; i < num_diskws; i++) {
         if (d_config.disks[i].removable) {
             ret = libxl_evenable_disk_eject(ctx, domid, d_config.disks[i].vdev,
                                             0, &diskws[i]);
@@ -3157,7 +3159,10 @@ start:
                 libxl_event_free(ctx, event);
                 libxl_evdisable_domain_death(ctx, deathw);
                 deathw = NULL;
-                evdisable_disk_ejects(diskws, d_config.num_disks);
+                evdisable_disk_ejects(diskws, num_diskws);
+                free(diskws);
+                diskws = NULL;
+                num_diskws = 0;
                 /* discard any other events which may have been generated */
                 while (!(ret = libxl_event_check(ctx, &event,
                                                  LIBXL_EVENTMASK_ALL, 0,0))) {
Signature · 7 lines
2.11.0


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

Re: [PATCH 2/2] xl: disable events earlier for shutdown event

Wei Liu writes ("Re: [PATCH 2/2] xl: disable events earlier for shutdown event"):

Quoted 3 lines

No, handling NULL is not enough. It could well be possible that diskws is not NULL but then num_disks grows, thus making evdisable_disk_ejects access out of bound.

The additional diskws entries could be zeroed then. But, ...

Quoted 2 lines

The other solution is to have a dedicated counter for diskws instead of relying on num_disks.

... this is a good approach too.

Quoted 7 lines

Subject: [PATCH] xl: track size of diskws with a dedicated counter

The num_disks field can change during guest lifetime. Don't use that as the size of diskws, use a dedicated counter instead.

Also free diskws and reset diskws to NULL after disabling events so that it will be automatically re-created when the guest reboots.

Ian.

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

#10 lore raw

Re: [PATCH 2/2] xl: disable events earlier for shutdown event

In reply to Wei Liu
Quoted 4 lines · Wei Liu

The other solution is to have a dedicated counter for diskws instead of relying on num_disks.

It's all good, both patches fix the issue, thank you.

Signature · 8 lines
Fatih ACAR
Gandi
[email protected]

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

Re: [PATCH 2/2] xl: disable events earlier for shutdown event

Quoted 8 lines · Fatih Acar
Quoted 4 lines · Wei Liu

The other solution is to have a dedicated counter for diskws instead of relying on num_disks.

It's all good, both patches fix the issue, thank you.

Cool, I will add a Tested-by tag.

Quoted 4 lines
Signature · 3 lines
Fatih ACAR
Gandi
[email protected]

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

author
#12 lore raw
In reply to Wei Liu
Quoted 6 lines · Wei Liu

Wei Liu (2):
xl: free event in DOMAIN_RESTART_RENAME error path
xl: disable events earlier for shutdown event

 tools/libxl/xl_cmdimpl.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Pushed the most up to date patches to staging.

Quoted 4 lines
Signature · 2 lines
-- 
2.11.0

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