Loading libmodprobe/libmodprobe.cpp +36 −45 Original line number Original line Diff line number Diff line Loading @@ -439,58 +439,54 @@ bool Modprobe::IsBlocklisted(const std::string& module_name) { return module_blocklist_.count(canonical_name) > 0; return module_blocklist_.count(canonical_name) > 0; } } // Another option to load kernel modules. load independent modules dependencies // Another option to load kernel modules. load in independent modules in parallel // in parallel and then update dependency list of other remaining modules, // and then update dependency list of other remaining modules, repeat these steps // repeat these steps until all modules are loaded. // until all modules are loaded. // Discard all blocklist. // Softdeps are taken care in InsmodWithDeps(). bool Modprobe::LoadModulesParallel(int num_threads) { bool Modprobe::LoadModulesParallel(int num_threads) { bool ret = true; bool ret = true; std::unordered_map<std::string, std::vector<std::string>> mod_with_deps; int count = -1; std::map<std::string, std::set<std::string>> mod_with_deps; // Get dependencies // Get dependencies for (const auto& module : module_load_) { for (const auto& module : module_load_) { // Skip blocklist modules if (IsBlocklisted(module)) { LOG(VERBOSE) << "LMP: Blocklist: Module " << module << " skipping..."; continue; } auto dependencies = GetDependencies(MakeCanonical(module)); auto dependencies = GetDependencies(MakeCanonical(module)); if (dependencies.empty()) { LOG(ERROR) << "LMP: Hard-dep: Module " << module for (auto dep = dependencies.rbegin(); dep != dependencies.rend(); dep++) { << " not in .dep file"; mod_with_deps[module].emplace(*dep); return false; } } // Get soft dependencies for (const auto& [it_mod, it_softdep] : module_pre_softdep_) { if (mod_with_deps.find(MakeCanonical(it_softdep)) != mod_with_deps.end()) { mod_with_deps[MakeCanonical(it_mod)].emplace( GetDependencies(MakeCanonical(it_softdep))[0]); } } // Get soft post dependencies for (const auto& [it_mod, it_softdep] : module_post_softdep_) { if (mod_with_deps.find(MakeCanonical(it_softdep)) != mod_with_deps.end()) { mod_with_deps[MakeCanonical(it_softdep)].emplace( GetDependencies(MakeCanonical(it_mod))[0]); } } mod_with_deps[MakeCanonical(module)] = dependencies; } } while (!mod_with_deps.empty()) { while (!mod_with_deps.empty() && count != module_loaded_.size()) { std::vector<std::thread> threads; std::vector<std::thread> threads; std::vector<std::string> mods_path_to_load; std::vector<std::string> mods_path_to_load; std::mutex vector_lock; std::mutex vector_lock; count = module_loaded_.size(); // Find independent modules // Find independent modules for (const auto& [it_mod, it_dep] : mod_with_deps) { for (const auto& [it_mod, it_dep] : mod_with_deps) { auto itd_last = it_dep.rbegin(); if (it_dep.size() == 1) { if (itd_last == it_dep.rend()) if (module_options_[it_mod].find("load_sequential=1") != std::string::npos) { continue; if (!LoadWithAliases(it_mod, true) && !IsBlocklisted(it_mod)) { auto cnd_last = MakeCanonical(*itd_last); // Hard-dependencies cannot be blocklisted if (IsBlocklisted(cnd_last)) { LOG(ERROR) << "LMP: Blocklist: Module-dep " << cnd_last << " : failed to load module " << it_mod; return false; } if (module_options_[cnd_last].find("load_sequential=1") != std::string::npos) { if (!LoadWithAliases(cnd_last, true)) { return false; return false; } } } else { } else { if (std::find(mods_path_to_load.begin(), mods_path_to_load.end(), mods_path_to_load.emplace_back(it_mod); cnd_last) == mods_path_to_load.end()) { mods_path_to_load.emplace_back(cnd_last); } } } } } } Loading @@ -506,7 +502,7 @@ bool Modprobe::LoadModulesParallel(int num_threads) { lk.unlock(); lk.unlock(); ret_load &= LoadWithAliases(mod_to_load, true); ret_load &= LoadWithAliases(mod_to_load, true); lk.lock(); lk.lock(); if (!ret_load) { if (!ret_load && !IsBlocklisted(mod_to_load)) { ret &= ret_load; ret &= ret_load; } } } } Loading @@ -525,18 +521,13 @@ bool Modprobe::LoadModulesParallel(int num_threads) { std::lock_guard guard(module_loaded_lock_); std::lock_guard guard(module_loaded_lock_); // Remove loaded module form mod_with_deps and soft dependencies of other modules // Remove loaded module form mod_with_deps and soft dependencies of other modules for (const auto& module_loaded : module_loaded_) { for (const auto& module_loaded : module_loaded_) { if (mod_with_deps.find(module_loaded) != mod_with_deps.end()) { mod_with_deps.erase(module_loaded); mod_with_deps.erase(module_loaded); } } } // Remove loaded module form dependencies of other modules which are not loaded yet // Remove loaded module form dependencies of other modules which are not loaded yet for (const auto& module_loaded_path : module_loaded_paths_) { for (const auto& module_loaded_path : module_loaded_paths_) { for (auto& [mod, deps] : mod_with_deps) { for (auto& [mod, deps] : mod_with_deps) { auto it = std::find(deps.begin(), deps.end(), module_loaded_path); deps.erase(module_loaded_path); if (it != deps.end()) { deps.erase(it); } } } } } } } Loading Loading
libmodprobe/libmodprobe.cpp +36 −45 Original line number Original line Diff line number Diff line Loading @@ -439,58 +439,54 @@ bool Modprobe::IsBlocklisted(const std::string& module_name) { return module_blocklist_.count(canonical_name) > 0; return module_blocklist_.count(canonical_name) > 0; } } // Another option to load kernel modules. load independent modules dependencies // Another option to load kernel modules. load in independent modules in parallel // in parallel and then update dependency list of other remaining modules, // and then update dependency list of other remaining modules, repeat these steps // repeat these steps until all modules are loaded. // until all modules are loaded. // Discard all blocklist. // Softdeps are taken care in InsmodWithDeps(). bool Modprobe::LoadModulesParallel(int num_threads) { bool Modprobe::LoadModulesParallel(int num_threads) { bool ret = true; bool ret = true; std::unordered_map<std::string, std::vector<std::string>> mod_with_deps; int count = -1; std::map<std::string, std::set<std::string>> mod_with_deps; // Get dependencies // Get dependencies for (const auto& module : module_load_) { for (const auto& module : module_load_) { // Skip blocklist modules if (IsBlocklisted(module)) { LOG(VERBOSE) << "LMP: Blocklist: Module " << module << " skipping..."; continue; } auto dependencies = GetDependencies(MakeCanonical(module)); auto dependencies = GetDependencies(MakeCanonical(module)); if (dependencies.empty()) { LOG(ERROR) << "LMP: Hard-dep: Module " << module for (auto dep = dependencies.rbegin(); dep != dependencies.rend(); dep++) { << " not in .dep file"; mod_with_deps[module].emplace(*dep); return false; } } // Get soft dependencies for (const auto& [it_mod, it_softdep] : module_pre_softdep_) { if (mod_with_deps.find(MakeCanonical(it_softdep)) != mod_with_deps.end()) { mod_with_deps[MakeCanonical(it_mod)].emplace( GetDependencies(MakeCanonical(it_softdep))[0]); } } // Get soft post dependencies for (const auto& [it_mod, it_softdep] : module_post_softdep_) { if (mod_with_deps.find(MakeCanonical(it_softdep)) != mod_with_deps.end()) { mod_with_deps[MakeCanonical(it_softdep)].emplace( GetDependencies(MakeCanonical(it_mod))[0]); } } mod_with_deps[MakeCanonical(module)] = dependencies; } } while (!mod_with_deps.empty()) { while (!mod_with_deps.empty() && count != module_loaded_.size()) { std::vector<std::thread> threads; std::vector<std::thread> threads; std::vector<std::string> mods_path_to_load; std::vector<std::string> mods_path_to_load; std::mutex vector_lock; std::mutex vector_lock; count = module_loaded_.size(); // Find independent modules // Find independent modules for (const auto& [it_mod, it_dep] : mod_with_deps) { for (const auto& [it_mod, it_dep] : mod_with_deps) { auto itd_last = it_dep.rbegin(); if (it_dep.size() == 1) { if (itd_last == it_dep.rend()) if (module_options_[it_mod].find("load_sequential=1") != std::string::npos) { continue; if (!LoadWithAliases(it_mod, true) && !IsBlocklisted(it_mod)) { auto cnd_last = MakeCanonical(*itd_last); // Hard-dependencies cannot be blocklisted if (IsBlocklisted(cnd_last)) { LOG(ERROR) << "LMP: Blocklist: Module-dep " << cnd_last << " : failed to load module " << it_mod; return false; } if (module_options_[cnd_last].find("load_sequential=1") != std::string::npos) { if (!LoadWithAliases(cnd_last, true)) { return false; return false; } } } else { } else { if (std::find(mods_path_to_load.begin(), mods_path_to_load.end(), mods_path_to_load.emplace_back(it_mod); cnd_last) == mods_path_to_load.end()) { mods_path_to_load.emplace_back(cnd_last); } } } } } } Loading @@ -506,7 +502,7 @@ bool Modprobe::LoadModulesParallel(int num_threads) { lk.unlock(); lk.unlock(); ret_load &= LoadWithAliases(mod_to_load, true); ret_load &= LoadWithAliases(mod_to_load, true); lk.lock(); lk.lock(); if (!ret_load) { if (!ret_load && !IsBlocklisted(mod_to_load)) { ret &= ret_load; ret &= ret_load; } } } } Loading @@ -525,18 +521,13 @@ bool Modprobe::LoadModulesParallel(int num_threads) { std::lock_guard guard(module_loaded_lock_); std::lock_guard guard(module_loaded_lock_); // Remove loaded module form mod_with_deps and soft dependencies of other modules // Remove loaded module form mod_with_deps and soft dependencies of other modules for (const auto& module_loaded : module_loaded_) { for (const auto& module_loaded : module_loaded_) { if (mod_with_deps.find(module_loaded) != mod_with_deps.end()) { mod_with_deps.erase(module_loaded); mod_with_deps.erase(module_loaded); } } } // Remove loaded module form dependencies of other modules which are not loaded yet // Remove loaded module form dependencies of other modules which are not loaded yet for (const auto& module_loaded_path : module_loaded_paths_) { for (const auto& module_loaded_path : module_loaded_paths_) { for (auto& [mod, deps] : mod_with_deps) { for (auto& [mod, deps] : mod_with_deps) { auto it = std::find(deps.begin(), deps.end(), module_loaded_path); deps.erase(module_loaded_path); if (it != deps.end()) { deps.erase(it); } } } } } } } Loading