From e5c50640e3ec24a8133f6649c195cb1d75602912 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Wed, 8 Nov 2023 00:08:41 -0800 Subject: [PATCH] machinebase: fully initialize devices registered by other devices postinit_devices() may cause additional devices to be registered (e.g. PCI hosts will register their cards). We were not calling device_postinit on those devices, because the iterator over the device map was set up at the start of the loop. Keep looping until we've actually initialized all devices in the map. --- machines/machinebase.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/machines/machinebase.cpp b/machines/machinebase.cpp index 8f33a6f..a420f56 100644 --- a/machines/machinebase.cpp +++ b/machines/machinebase.cpp @@ -24,6 +24,7 @@ along with this program. If not, see . #include #include +#include #include std::unique_ptr gMachineObj = 0; @@ -79,9 +80,18 @@ HWComponent* MachineBase::get_comp_by_type(HWCompType type) { int MachineBase::postinit_devices() { - for (auto it = this->device_map.begin(); it != this->device_map.end(); it++) { - if (it->second->device_postinit()) { - return -1; + // Allow additional devices to be registered by device_postinit, in which + // case we'll initialize them too. + std::set initialized_devices; + while (initialized_devices.size() < this->device_map.size()) { + for (auto it = this->device_map.begin(); it != this->device_map.end(); it++) { + if (initialized_devices.find(it->first) == initialized_devices.end()) { + if (it->second->device_postinit()) { + LOG_F(ERROR, "Could not initialize device %s", it->first.c_str()); + return -1; + } + initialized_devices.insert(it->first); + } } }