libxl/devd: bufixes
v2 cover libxl/devd: bufixes
Hello,
The following two patches fix a race with concurrent device addition/removal and two bugs related to manipulation of the list of active domains in the devd subcommand.
IMHO they should be part of 4.9 because they are confined to devd code, and without them devd is unusable (it's trivial to segfault it), so the risk is low. Worse thing that could happen is that devd crashes, which is already the case without them.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v2 1/2 libxl/devd: fix a race with concurrent device addition/removal
Current 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]>
Cc: Julien Grall <[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
PATCH v2 2/2 libxl/devd: correctly manipulate the dguest list
In reply to Roger Pau MonneCurrent code in backend_watch_callback has two issues when manipulating the dguest 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.
---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Julien Grall <[email protected]>
Changes since v1:
- Fix commit message
--- 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
Re: [PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal
In reply to Roger Pau MonneRoger Pau Monne writes ("[PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 3 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:
...
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.
...
tools/libxl/libxl_device.c static int add_device(libxl__egc *egc, libxl__ao *ao, · line 1526
Quoted 9 lines
GCNEW(aodev); libxl__prepare_ao_device(ao, aodev);tools/libxl/libxl_device.c +6
-1- 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;
This does conveniently disentangle the memory management, so I think it's a good approach.
But it reads kind of oddly to me. I think it is not buggy, but can you add a comment to the definition of libxl__device, saying that it is a transparent structure containing no external memory references ?
Otherwise this copy is not really justifiable, because in C, in general, structs might contain private fields, or memory references or linked list entries or something.
Thanks,
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 2/2] libxl/devd: correctly manipulate the dguest list
In reply to Roger Pau MonneRoger Pau Monne writes ("[PATCH v2 2/2] libxl/devd: correctly manipulate the dguest list"):
Quoted 2 lines
Current code in
backend_watch_callbackhas two issues when manipulating the dguest list:
...
Quoted 5 lines
skip:
libxl__nested_ao_free(nested_ao); +clean: if (ddev) free(ddev->dev);
This is starting to be quite goto-rich, and the memory ownership rules become less clear. Rather than try to analyse this in detail, I wonder if it would be better to try to rework this so that it fits CODING_STYLE better.
Wei, what do you think ?
tools/libxl/libxl_device.c static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch, · line 1704
Quoted 4 lines
free(ddev);tools/libxl/libxl_device.c +2
-1-free (dguest); +if (dguest !=NULL && + dguest->num_vifs + dguest->num_vbds + dguest->num_qdisks ==0 ) {
Perhaps this cleanup functionality could become a function if its own. check_maybe_free_dguest or something ?
I haven't gone through the code in detail trying to convince myself it's OK. Even if there are to be no significant code changes, I would like to see the memory ownership and lifetime rules here written down (in comments in the code). That way readers wouldn't have to reverse-engineer them, and bugs will be clearer.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 2/2] libxl/devd: correctly manipulate the dguest list
Quoted 10 lines · Ian Jackson
Roger Pau Monne writes ("[PATCH v2 2/2] libxl/devd: correctly manipulate the dguest list"):
Quoted 2 lines
Current code in
backend_watch_callbackhas two issues when manipulating the dguest list:...
Quoted 5 lines
skip:
libxl__nested_ao_free(nested_ao); +clean: if (ddev) free(ddev->dev);
Quoted 7 lines
This is starting to be quite goto-rich, and the memory ownership rules become less clear. Rather than try to analyse this in detail, I wonder if it would be better to try to rework this so that it fits
CODING_STYLEbetter.Wei, what do you think ?
No objection from me, of course.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal
In reply to Ian JacksonQuoted 20 lines · Ian Jackson
Roger Pau Monne writes ("[PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 3 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:...
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....
tools/libxl/libxl_device.c static int add_device(libxl__egc *egc, libxl__ao *ao, · line 1526
Quoted 9 lines
GCNEW(aodev); libxl__prepare_ao_device(ao, aodev);tools/libxl/libxl_device.c +6
-1- 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;
Quoted 6 lines
This does conveniently disentangle the memory management, so I think it's a good approach.
But it reads kind of oddly to me. I think it is not buggy, but can you add a comment to the definition of
libxl__device, saying that it is a transparent structure containing no external memory references ?
Sure, before implementing this I already took a look at the contents of the libxl__device struct, but I agree that a comment is in place in case someone expands the fields of the struct later on.
Quoted 3 lines
Otherwise this copy is not really justifiable, because in C, in general, structs might contain private fields, or memory references or linked list entries or something.
Thanks, Roger.
NB: FWIW, I'm planning to keep Wei's RB since this is a cosmetic change.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal
Hi Roger,
Quoted 32 lines · Roger Pau Monne
Quoted 20 lines · Ian Jackson
Roger Pau Monne writes ("[PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 3 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:...
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....
tools/libxl/libxl_device.c static int add_device(libxl__egc *egc, libxl__ao *ao, · line 1526
Quoted 9 lines
GCNEW(aodev); libxl__prepare_ao_device(ao, aodev);tools/libxl/libxl_device.c +6
-1- 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;Quoted 6 lines
This does conveniently disentangle the memory management, so I think it's a good approach.
But it reads kind of oddly to me. I think it is not buggy, but can you add a comment to the definition of
libxl__device, saying that it is a transparent structure containing no external memory references ?Sure, before implementing this I already took a look at the contents of the
libxl__devicestruct, but I agree that a comment is in place in case someone expands the fields of the struct later on.
Quoted 7 lines
Quoted 3 lines
Otherwise this copy is not really justifiable, because in C, in general, structs might contain private fields, or memory references or linked list entries or something.
Thanks, Roger.
NB: FWIW, I'm planning to keep Wei's RB since this is a cosmetic change.
Is this patch series targeting Xen 4.9?
Cheers,
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal
Quoted 39 lines · Julien Grall
Hi Roger,
Quoted 36 lines · Roger Pau Monne
Quoted 20 lines · Ian Jackson
Roger Pau Monne writes ("[PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 3 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:...
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....
tools/libxl/libxl_device.c static int add_device(libxl__egc *egc, libxl__ao *ao, · line 1526
Quoted 9 lines
GCNEW(aodev); libxl__prepare_ao_device(ao, aodev);tools/libxl/libxl_device.c +6
-1- 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;Quoted 6 lines
This does conveniently disentangle the memory management, so I think it's a good approach.
But it reads kind of oddly to me. I think it is not buggy, but can you add a comment to the definition of
libxl__device, saying that it is a transparent structure containing no external memory references ?Sure, before implementing this I already took a look at the contents of the
libxl__devicestruct, but I agree that a comment is in place in case someone expands the fields of the struct later on.Quoted 3 lines
Otherwise this copy is not really justifiable, because in C, in general, structs might contain private fields, or memory references or linked list entries or something.
Quoted 5 lines
Quoted 3 lines
Thanks, Roger.
NB: FWIW, I'm planning to keep Wei's RB since this is a cosmetic change.
Is this patch series targeting Xen 4.9?
Yes, I think so. I will post a new version soon and Cc you.
Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal
In reply to Julien GrallJulien Grall writes ("Re: [PATCH v2 1/2] libxl/devd: fix a race with concurrent device addition/removal"):
Quoted 1 line
Is this patch series targeting Xen 4.9?
IMO it should be. The changes are confined to the xl devd code, which is currently broken. So the reward/risk ratio is good.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel