libxl/devd: fix a race and a leak
cover libxl/devd: fix a race and a leak
Hello,
The following two patches fix a race with concurrent device addition/removal and a possible dereference in the list of active domains in the devd subcommand.
Although they are bugfixes I'm not sure whether they should be in 4.9, IMHO the current devd code is broken, so having these two fixes is unlikely to make it any worse. I will leave to the maintainers whether to Cc Julien on their reviews if they feel like this should be in 4.9.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH 2/2 libxl/devd: prevent adding spurious domains to the list
Current code in backend_watch_callback forgets to remove a libxl__ddomain_guest from the list of tracked domains when the related data is freed, causing dereferences later on when the list is traversed. Make sure that a domain is always removed from the list when freed.
---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
--- tools/libxl/libxl_device.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-)
tools/libxl/libxl_device.c +14 -16
@@ -1602,7 +1602,7 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch,STATE_AO_GC (nested_ao);char *p, *path;const char *sstate, *sonline; -int state, online, rc, num_devs; +int state, online, rc;libxl__device *dev;libxl__ddomain_device *ddev =NULL ;libxl__ddomain_guest *dguest =NULL ;
@@ -1684,21 +1684,9 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch, path); rc =remove_device (egc, nested_ao, dguest, ddev);if (rc >0 ) - free_ao = true; +libxl__nested_ao_free (nested_ao); -free (ddev->dev); -free (ddev); -/* If this was the last device in the domain, remove it from the list */ - num_devs = dguest->num_vifs + dguest->num_vbds + dguest->num_qdisks; -if (num_devs ==0 ) { -LIBXL_SLIST_REMOVE (&ddomain->guests, dguest, libxl__ddomain_guest, - next); -LOGD (DEBUG , dguest->domid,"Removed domain from the list of active guests" ); -/* Clear any leftovers in libxl/<domid> */ -libxl__xs_rm_checked (gc,XBT_NULL , -GCSPRINTF ("libxl/%u" , dguest->domid)); -free (dguest); - } + goto clean; }if (free_ao)
@@ -1708,10 +1696,20 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch, skip:libxl__nested_ao_free (nested_ao); +clean:if (ddev)free (ddev->dev);free (ddev); -free (dguest); +if (dguest !=NULL && + dguest->num_vifs + dguest->num_vbds + dguest->num_qdisks ==0 ) { +LIBXL_SLIST_REMOVE (&ddomain->guests, dguest, libxl__ddomain_guest, + next); +LOGD (DEBUG , dguest->domid,"Removed domain from the list of active guests" ); +/* Clear any leftovers in libxl/<domid> */ +libxl__xs_rm_checked (gc,XBT_NULL , +GCSPRINTF ("libxl/%u" , dguest->domid)); +free (dguest); + }return ; }
Signature · 7 lines
2.11.0 (Apple Git-81) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 1/2 libxl/devd: fix a race with concurrent device addition/removal
In reply to Roger Pau MonneCurrent code can free the libxl__device inside of the libxl__ddomain_device before the addition has finished if a removal happens while an addition is still in process:
backend_watch_callback
|
v
add_device
| backend_watch_callback
(async operation) |
| v
| remove_device
| |
| V
| device_complete
| (free libxl__device)
v
device_complete
(deref libxl__device)
Fix this by creating a temporary copy of the libxl__device, that's tracked by the GC of the nested async operation. This ensures that the libxl__device used by the async operations cannot be freed while being used.
---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
--- tools/libxl/libxl_device.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-)
tools/libxl/libxl_device.c +19 -13
@@ -1415,9 +1415,6 @@ static void device_complete(libxl__egc *egc, libxl__ao_device *aodev)libxl__device_action_to_string (aodev->action), aodev->rc ? "failed" : "succeed"); -if (aodev->action ==LIBXL__DEVICE_ACTION_REMOVE ) -free (aodev->dev); -libxl__nested_ao_free (aodev->ao); }
@@ -1521,7 +1518,12 @@ static int add_device(libxl__egc *egc, libxl__ao *ao,GCNEW (aodev);libxl__prepare_ao_device (ao, aodev); - aodev->dev = dev; +/* +* Clone the libxl__device to avoid races if remove_device is called +* before the device addition has finished. +*/ +GCNEW (aodev->dev); + *aodev->dev = *dev; aodev->action =LIBXL__DEVICE_ACTION_ADD ; aodev->callback = device_complete;libxl__wait_device_connection (egc, aodev);
@@ -1564,7 +1566,12 @@ static int remove_device(libxl__egc *egc, libxl__ao *ao,GCNEW (aodev);libxl__prepare_ao_device (ao, aodev); - aodev->dev = dev; +/* +* Clone the libxl__device to avoid races if there's a add_device +* running in parallel. +*/ +GCNEW (aodev->dev); + *aodev->dev = *dev; aodev->action =LIBXL__DEVICE_ACTION_REMOVE ; aodev->callback = device_complete;libxl__initiate_device_generic_remove (egc, aodev);
@@ -1576,7 +1583,6 @@ static int remove_device(libxl__egc *egc, libxl__ao *ao, goto out; }libxl__device_destroy (gc, dev); -free (dev);/* Fall through to return > 0, no ao has been dispatched */ default : rc =1 ;
@@ -1597,7 +1603,7 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch,char *p, *path;const char *sstate, *sonline;int state, online, rc, num_devs; -libxl__device *dev =NULL ; +libxl__device *dev;libxl__ddomain_device *ddev =NULL ;libxl__ddomain_guest *dguest =NULL ;bool free_ao = false;
@@ -1625,7 +1631,7 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch, goto skip; online =atoi (sonline); - dev =libxl__zalloc (NOGC ,sizeof (*dev)); +GCNEW (dev); rc =libxl__parse_backend_path (gc, path, dev);if (rc) goto skip;
@@ -1659,7 +1665,8 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch,* to the list of active devices for a given guest. */ ddev =libxl__zalloc (NOGC ,sizeof (*ddev)); - ddev->dev = dev; + ddev->dev =libxl__zalloc (NOGC ,sizeof (*ddev->dev)); + *ddev->dev = *dev;LIBXL_SLIST_INSERT_HEAD (&dguest->devices, ddev, next);LOGD (DEBUG , dev->domid,"Added device %s to the list of active devices" , path);
@@ -1670,9 +1677,6 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch,/* * Removal of an active device, remove it from the list and * free it's data structures if they are no longer needed. -* -* The free of the associated libxl__device is left to the -* helper remove_device function. */ LIBXL_SLIST_REMOVE (&dguest->devices, ddev, libxl__ddomain_device, next);
@@ -1682,6 +1686,7 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch,if (rc >0 ) free_ao = true; +free (ddev->dev);free (ddev);/* If this was the last device in the domain, remove it from the list */ num_devs = dguest->num_vifs + dguest->num_vbds + dguest->num_qdisks;
@@ -1703,7 +1708,8 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch, skip:libxl__nested_ao_free (nested_ao); -free (dev); +if (ddev) +free (ddev->dev);free (ddev);free (dguest);return ;
Signature · 7 lines
2.11.0 (Apple Git-81) _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal
Roger Pau Monne writes ("[PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 20 lines
Current code can free the
libxl__deviceinside of thelibxl__ddomain_devicebefore the addition has finished if a removal happens while an addition is still in process:
backend_watch_callback
|
v
add_device
|backend_watch_callback
(async operation) |
| v
|remove_device
| |
| V
|device_complete
| (freelibxl__device)
v
device_complete
(dereflibxl__device)
Quoted 4 lines
Fix this by creating a temporary copy of the
libxl__device, that's tracked by the GC of the nested async operation. This ensures that thelibxl__deviceused by the async operations cannot be freed while being used.
Doesn't this arrange that the remove hotplug script will be invoked while the add hotplug script is still running ? Is that really desirable (or allowed!) ?
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal
Quoted 21 lines · Ian Jackson
Roger Pau Monne writes ("[PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 20 lines
Current code can free the
libxl__deviceinside of thelibxl__ddomain_devicebefore the addition has finished if a removal happens while an addition is still in process:
backend_watch_callback
|
v
add_device
|backend_watch_callback
(async operation) |
| v
|remove_device
| |
| V
|device_complete
| (freelibxl__device)
v
device_complete
(dereflibxl__device)
Quoted 7 lines
Quoted 4 lines
Fix this by creating a temporary copy of the
libxl__device, that's tracked by the GC of the nested async operation. This ensures that thelibxl__deviceused by the async operations cannot be freed while being used.Doesn't this arrange that the remove hotplug script will be invoked while the add hotplug script is still running ?
That's indeed possible (either with the current code or with this patch), although unlikely. The async code called by remove_device will wait for the backend to switch to state 6, while the add_device code will wait for state 2 IIRC (one can change those states to make them clash probably).
Quoted 1 line
Is that really desirable (or allowed!) ?
Hm, no, I don't think it's desirable at all. I still think this is better that the previous code (at lest it doesn't dereference libxl__device anymore), but clearly needs further improvements.
Also, it seems to me the same can happen even without driver domains, if a user executes concurrent block-{attach/detach} operations, but maybe I'm missing something?
Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal
Quoted 37 lines · Roger Pau Monne
Quoted 21 lines · Ian Jackson
Roger Pau Monne writes ("[PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 20 lines
Current code can free the
libxl__deviceinside of thelibxl__ddomain_devicebefore the addition has finished if a removal happens while an addition is still in process:
backend_watch_callback
|
v
add_device
|backend_watch_callback
(async operation) |
| v
|remove_device
| |
| V
|device_complete
| (freelibxl__device)
v
device_complete
(dereflibxl__device)Quoted 7 lines
Quoted 4 lines
Fix this by creating a temporary copy of the
libxl__device, that's tracked by the GC of the nested async operation. This ensures that thelibxl__deviceused by the async operations cannot be freed while being used.Doesn't this arrange that the remove hotplug script will be invoked while the add hotplug script is still running ?
That's indeed possible (either with the current code or with this patch), although unlikely. The async code called by
remove_devicewill wait for the backend to switch to state 6, while theadd_devicecode will wait for state 2 IIRC (one can change those states to make them clash probably).Quoted 1 line
Is that really desirable (or allowed!) ?
Quoted 8 lines
Hm, no, I don't think it's desirable at all. I still think this is better that the previous code (at lest it doesn't dereference
libxl__deviceanymore), but clearly needs further improvements.Also, it seems to me the same can happen even without driver domains, if a user executes concurrent block-{attach/detach} operations, but maybe I'm missing something?
There is a lot of locking for all the device add / remove code. See libxl_internal.h L2588.
Quoted 1 line
Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal
Quoted 42 lines · Wei Liu
Quoted 41 lines · Roger Pau Monne
Quoted 21 lines · Ian Jackson
Roger Pau Monne writes ("[PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 20 lines
Current code can free the
libxl__deviceinside of thelibxl__ddomain_devicebefore the addition has finished if a removal happens while an addition is still in process:
backend_watch_callback
|
v
add_device
|backend_watch_callback
(async operation) |
| v
|remove_device
| |
| V
|device_complete
| (freelibxl__device)
v
device_complete
(dereflibxl__device)Quoted 7 lines
Quoted 4 lines
Fix this by creating a temporary copy of the
libxl__device, that's tracked by the GC of the nested async operation. This ensures that thelibxl__deviceused by the async operations cannot be freed while being used.Doesn't this arrange that the remove hotplug script will be invoked while the add hotplug script is still running ?
That's indeed possible (either with the current code or with this patch), although unlikely. The async code called by
remove_devicewill wait for the backend to switch to state 6, while theadd_devicecode will wait for state 2 IIRC (one can change those states to make them clash probably).Quoted 1 line
Is that really desirable (or allowed!) ?
Hm, no, I don't think it's desirable at all. I still think this is better that the previous code (at lest it doesn't dereference
libxl__deviceanymore), but clearly needs further improvements.
Quoted 7 lines
Quoted 4 lines
Also, it seems to me the same can happen even without driver domains, if a user executes concurrent block-{attach/detach} operations, but maybe I'm missing something?
There is a lot of locking for all the device add / remove code. See
libxl_internal.hL2588.
It's been a long time since I've played with libxl, last time none of this existed, sorry. Sadly devd completely bypasses all this, a simple way to fix this would be to call libxl__lock_domain_userdata from {add/remove}_device maybe? (and drop the lock at device_complete).
Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal
Quoted 48 lines · Roger Pau Monne
Quoted 47 lines · Wei Liu
Quoted 45 lines · Roger Pau Monne
Quoted 21 lines · Ian Jackson
Roger Pau Monne writes ("[PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 20 lines
Current code can free the
libxl__deviceinside of thelibxl__ddomain_devicebefore the addition has finished if a removal happens while an addition is still in process:
backend_watch_callback
|
v
add_device
|backend_watch_callback
(async operation) |
| v
|remove_device
| |
| V
|device_complete
| (freelibxl__device)
v
device_complete
(dereflibxl__device)Quoted 7 lines
Quoted 4 lines
Fix this by creating a temporary copy of the
libxl__device, that's tracked by the GC of the nested async operation. This ensures that thelibxl__deviceused by the async operations cannot be freed while being used.Doesn't this arrange that the remove hotplug script will be invoked while the add hotplug script is still running ?
That's indeed possible (either with the current code or with this patch), although unlikely. The async code called by
remove_devicewill wait for the backend to switch to state 6, while theadd_devicecode will wait for state 2 IIRC (one can change those states to make them clash probably).Quoted 1 line
Is that really desirable (or allowed!) ?
Hm, no, I don't think it's desirable at all. I still think this is better that the previous code (at lest it doesn't dereference
libxl__deviceanymore), but clearly needs further improvements.Also, it seems to me the same can happen even without driver domains, if a user executes concurrent block-{attach/detach} operations, but maybe I'm missing something?
Quoted 7 lines
Quoted 2 lines
There is a lot of locking for all the device add / remove code. See
libxl_internal.hL2588.It's been a long time since I've played with libxl, last time none of this existed, sorry. Sadly devd completely bypasses all this, a simple way to fix this would be to call
libxl__lock_domain_userdatafrom {add/remove}_device maybe? (and drop the lock atdevice_complete).
As discussed on IRC, this is not possible because in order to lock the user data lock the CTX lock must be held, and leaving libxl with the CTX lock held is not allowed (as would happen when waiting for an async operation to finish).
I think we have concluded that this, although nice to have, is not worse than the situation when running hotplug scripts from Dom0, hence the patch still stands.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 1/2] libxl/devd: fix a race with concurrent device addition/removal
In reply to Roger Pau MonneQuoted 20 lines · Roger Pau Monne
Current code can free the
libxl__deviceinside of thelibxl__ddomain_devicebefore the addition has finished if a removal happens while an addition is still in process:
backend_watch_callback
|
v
add_device
|backend_watch_callback
(async operation) |
| v
|remove_device
| |
| V
|device_complete
| (freelibxl__device)
v
device_complete
(dereflibxl__device)
Quoted 6 lines
Fix this by creating a temporary copy of the
libxl__device, that's tracked by the GC of the nested async operation. This ensures that thelibxl__deviceused by the async operations cannot be freed while being used.Signed-off-by Roger Pau Monné <[email protected]>Reported-by Ian Jackson <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/2] libxl/devd: prevent adding spurious domains to the list
In reply to Roger Pau MonneQuoted 4 lines · Roger Pau Monne
Current code in
backend_watch_callbackforgets to remove alibxl__ddomain_guestfrom the list of tracked domains when the related data is freed, causing dereferences later on when the list is traversed. Make sure that a domain is always removed from the list when freed.
There is another bug: dguest was freed by a spurious event. That is also fixed by this patch.
I would suggest changing the tite to "correctly manipulate the dguest list" and also extend the commit message to include that other bug.
Code-wise:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/2] libxl/devd: prevent adding spurious domains to the list
Quoted 11 lines · Wei Liu
Quoted 4 lines · Roger Pau Monne
Current code in
backend_watch_callbackforgets to remove alibxl__ddomain_guestfrom the list of tracked domains when the related data is freed, causing dereferences later on when the list is traversed. Make sure that a domain is always removed from the list when freed.There is another bug: dguest was freed by a spurious event. That is also fixed by this patch.
I would suggest changing the tite to "correctly manipulate the dguest list" and also extend the commit message to include that other bug.
What about the following commit message:
libxl/devd: correctly manipulate the dguest list
Current code in backend_watch_callback has two issues when manipulating the dgest list:
1. backend_watch_callback forgets to remove a libxl__ddomain_guest from the list of tracked domains when the related data is freed, causing dereferences later on when the list is traversed. Make sure that a domain is always removed from the list when freed.
2. A spurious device state change can cause a dguest to be freed, with active devices and without being removed from the list. Fix this by always checking if a dguest has active devices before freeing and removing it.
Let me know if you want me to resend the patch or if you will fix the message while committing.
Quoted 3 lines
Code-wise:
Reviewed-by Wei Liu <[email protected]>
Thanks.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 2/2] libxl/devd: prevent adding spurious domains to the list
Quoted 25 lines · Roger Pau Monne
Quoted 11 lines · Wei Liu
Quoted 4 lines · Roger Pau Monne
Current code in
backend_watch_callbackforgets to remove alibxl__ddomain_guestfrom the list of tracked domains when the related data is freed, causing dereferences later on when the list is traversed. Make sure that a domain is always removed from the list when freed.There is another bug: dguest was freed by a spurious event. That is also fixed by this patch.
I would suggest changing the tite to "correctly manipulate the dguest list" and also extend the commit message to include that other bug.
What about the following commit message:
libxl/devd: correctly manipulate the dguest list
Current code in
backend_watch_callbackhas two issues when manipulating the dgest list:1.
backend_watch_callbackforgets to remove alibxl__ddomain_guestfrom the list of tracked domains when the related data is freed, causing dereferences later on when the list is traversed. Make sure that a domain is always removed from the list when freed.
Quoted 7 lines
2. A spurious device state change can cause a dguest to be freed, with active devices and without being removed from the list. Fix this by always checking if a dguest has active devices before freeing and removing it.
Let me know if you want me to resend the patch or if you will fix the message while committing.
Resending and CC Julien would be best.
Quoted 5 lines
Quoted 3 lines
Code-wise:
Reviewed-by Wei Liu <[email protected]>Thanks.
Wei.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel