Repository navigation
KVM: Fix storage pool refcount leak on creation failure - #14694
dheeraj12347 wants to merge 4 commits into
Conversation
Two bugs in the storage pool teardown path. adjustStoragePoolRefCount() means to lock on the single String instance held as the map key, so that all callers share a monitor. When the map has no entry for the pool, orElse(uuid) returns the caller's own instance instead and the synchronized block guards nothing. Increments cannot race each other: they happen inside createStoragePool, which is only reached through KVMStoragePoolManager.createStoragePool, and that is synchronized. Decrements are not covered, because KVMStoragePoolManager.deleteStoragePool is not. So an increment and a decrement can run at the same time, and while the map has no entry for the pool they take different monitors and the decrement's remove() can erase the increment. The count then reaches zero while the pool is still in use. Use ConcurrentHashMap.compute(), which is atomic for the key. deleteStoragePool() decided whether the retried umount had worked from the return of runSimpleBashScript(), which is null both when the command fails, because runScript() discards the output on a non-zero exit, and when it succeeds without printing anything. A failed umount was therefore logged and returned as a success. Take the outcome from whether the path is still a mount point, which also covers the pool having been unmounted by something else in the meantime, and return false rather than throwing when it is still mounted: deleteStoragePool() is called from finally blocks, where a throw would discard the result of an operation that has already succeeded. Signed-off-by: Brad House <bhouse@nexthop.ai>
The refcount is atomic, but it cannot keep a pool alive on its own. deleteStoragePool() drops the last reference and then goes on to destroy and unmount the pool without holding anything. createStoragePool() is serialised by KVMStoragePoolManager, but deleteStoragePool() is not, so a create can find the still active pool and take a reference after the delete has seen the count reach zero, and the delete then tears the pool down underneath it. Take a monitor per pool uuid for the whole of createStoragePool() and deleteStoragePool(). Every delete path ends in deleteStoragePool(String), including LibvirtStoragePool.delete(), so locking there covers callers that bypass the manager. Pools other than the one being deleted are not held up, which matters because a delete can sit in the five second umount retry. Signed-off-by: Brad House <bhouse@nexthop.ai>
The previous commit took the per-pool lock inside LibvirtStorageAdaptor.createStoragePool(), which is only reached through KVMStoragePoolManager.createStoragePool(), and that is synchronized on the manager. A create of a pool that is being torn down therefore waited for the teardown while holding the manager wide lock. The umount retry in deleteStoragePool() runs with no timeout, so a umount hung on storage that has gone away held up the creates of every pool on the host, not just the one being deleted. Take the pool's lock in the manager first, before its own monitor, so a create waiting for a teardown holds nothing that other pools need. The locks move to KVMStoragePoolLocks so the manager and the adaptor share them; the adaptor still takes the same lock, reentrantly on the create path and on its own for deletes, including LibvirtStoragePool.delete(), which does not go through the manager. Signed-off-by: Brad House <bhouse@nexthop.ai>
|
Thanks for picking this up. The fix itself looks right to me: decrementing in a One problem: this depends on #14189 but targets 4.22 without it. On top of #14189, the main change applies cleanly but the test file conflicts with the tests #14189 adds there. Two ways to go: rebase this onto 4.22 once #14189 merges, which is the simplest; or make it standalone now by testing through the public |
Thanks for pointing that out, @bhouse-nexthop! I’ll wait for #14189 to get merged and then update my PR on top of it. Appreciate you taking a look! |
|
Hi @bhouse-nexthop and @DaanHoogland , I've completed the implementation and added a regression test for the storage pool refcount leak. I've tested the changes on top of PR #14189. The focused KVM storage tests passed (12/12). The full KVM plugin suite reported 668 passed, 2 skipped, and 1 error due to the missing native Could you please review the implementation and let me know if you recommend any further changes? I understand this PR depends on #14189, so I'll update it once the dependency is merged. Thanks! |
@dheeraj12347 if it depends on #14189, does it make sense to base it on top of that? |
b8ed8f2 to
7abbd3a
Compare
Hi @DaanHoogland , Yes, I've updated my branch on top of the latest commit of #14189 and pushed it. My fix is now a separate commit on top of those changes. Once #14189 is merged, I'll rebase my PR onto the updated 4.22 branch so that only my changes remain in the diff. Thanks for the suggestion! |
Description
This PR fixes a storage pool reference-count leak in
createStoragePoolLocked().When the reference count is incremented for a secondary storage pool, subsequent failures in
checkNetfsStoragePoolMounted()orgetStoragePool()can leave the reference count incremented because the existing exception handling only catchesLibvirtException.Solution
finallyblock to decrement the reference count if the operation fails after the increment.LibvirtExceptionhandling.Types of changes
Bug Severity
How Has This Been Tested?
Added
testCreateStoragePoolDecrementsRefCountWhenGetStoragePoolFails()to verify that the reference count is decremented whengetStoragePool()throws aCloudRuntimeException.git diff --check: Passed.Related: #14189