diff options
author | Hans Petter Selasky <hselasky@FreeBSD.org> | 2016-10-17 10:20:38 +0000 |
---|---|---|
committer | Hans Petter Selasky <hselasky@FreeBSD.org> | 2016-10-17 10:20:38 +0000 |
commit | d3bf5efc1ff19b102b176512a66110ec84ac982d (patch) | |
tree | c35a2659be50242d73b1b0ec21d568922eca8a84 /sys/kern/subr_bus.c | |
parent | d609986008eaedae8d9e3e88f8d9c1e1c23d13a4 (diff) | |
download | src-d3bf5efc1ff19b102b176512a66110ec84ac982d.tar.gz src-d3bf5efc1ff19b102b176512a66110ec84ac982d.zip |
Fix device delete child function.
When detaching device trees parent devices must be detached prior to
detaching its children. This is because parent devices can have
pointers to the child devices in their softcs which are not
invalidated by device_delete_child(). This can cause use after free
issues and panic().
Device drivers implementing trees, must ensure its detach function
detaches or deletes all its children before returning.
While at it remove now redundant device_detach() calls before
device_delete_child() and device_delete_children(), mostly in
the USB controller drivers.
Tested by: Jan Henrik Sylvester <me@janh.de>
Reviewed by: jhb
Differential Revision: https://reviews.freebsd.org/D8070
MFC after: 2 weeks
Notes
Notes:
svn path=/head/; revision=307518
Diffstat (limited to 'sys/kern/subr_bus.c')
-rw-r--r-- | sys/kern/subr_bus.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index 1e188d3cb681..d80c3d91075e 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -1949,15 +1949,17 @@ device_delete_child(device_t dev, device_t child) PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev))); - /* remove children first */ + /* detach parent before deleting children, if any */ + if ((error = device_detach(child)) != 0) + return (error); + + /* remove children second */ while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) { error = device_delete_child(child, grandchild); if (error) return (error); } - if ((error = device_detach(child)) != 0) - return (error); if (child->devclass) devclass_delete_device(child->devclass, child); if (child->parent) |