block: Fix bdrv_next() memory leak
PATCH v3 1/1 block: Fix bdrv_next() memory leak
The bdrv_next() users all leaked the BdrvNextIterator after completing the iteration. Simply changing bdrv_next() to free the iterator before returning NULL at the end of list doesn't work because some callers exit the loop before looking at all BDSes.
This patch moves the BdrvNextIterator from the heap to the stack of the caller and switches to a bdrv_first()/bdrv_next() interface for initialising the iterator.
---
v3:
- block/snapshot.c: With the change to a for loop, bdrv_next() is executed unconditionally, even if the exit condition is true. We now need to explicitly jump out of the loop if bs should stay the same.
block.c | 18 ++++++++--------- block/block-backend.c | 41 +++++++++++++++++--------------------- block/io.c | 10 ++++------ block/snapshot.c | 55 ++++++++++++++++++++++++++++++++++++++------------- blockdev.c | 4 ++-- include/block/block.h | 15 ++++++++++++-- migration/block.c | 4 ++-- monitor.c | 4 ++-- qmp.c | 5 ++--- 9 files changed, 92 insertions(+), 64 deletions(-)
block.c +8 -10
@@ -3195,9 +3195,9 @@ void bdrv_invalidate_cache_all(Error **errp) {BlockDriverState *bs;Error *local_err =NULL ; -BdrvNextIterator *it =NULL ; +BdrvNextIterator it; -while ((it =bdrv_next (it, &bs)) !=NULL ) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {AioContext *aio_context =bdrv_get_aio_context (bs);aio_context_acquire (aio_context);
@@ -3239,11 +3239,11 @@ static int bdrv_inactivate_recurse(BlockDriverState *bs,int bdrv_inactivate_all (void ) {BlockDriverState *bs =NULL ; -BdrvNextIterator *it =NULL ; +BdrvNextIterator it;int ret =0 ;int pass; -while ((it =bdrv_next (it, &bs)) !=NULL ) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {aio_context_acquire (bdrv_get_aio_context (bs)); }
@@ -3252,8 +3252,7 @@ int bdrv_inactivate_all(void)* the second pass sets the BDRV_O_INACTIVE flag so that no further write * is allowed. */ for (pass =0 ; pass <2 ; pass++) { - it =NULL ; -while ((it =bdrv_next (it, &bs)) !=NULL ) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) { ret =bdrv_inactivate_recurse (bs, pass);if (ret <0 ) { goto out;
@@ -3262,8 +3261,7 @@ int bdrv_inactivate_all(void) } out: - it =NULL ; -while ((it =bdrv_next (it, &bs)) !=NULL ) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {aio_context_release (bdrv_get_aio_context (bs)); }
@@ -3753,10 +3751,10 @@ bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs,bool bdrv_is_first_non_filter (BlockDriverState *candidate) {BlockDriverState *bs; -BdrvNextIterator *it =NULL ; +BdrvNextIterator it;/* walk down the bs forest recursively */ -while ((it =bdrv_next (it, &bs)) !=NULL ) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {bool perm;/* try to recurse in this top level bs */
block/block-backend.c +18 -23
64 lines not on this page · raw message
block/io.c +4 -6
@@ -271,10 +271,10 @@ void bdrv_drain_all(void)/* Always run first iteration so any pending completion BHs run */ bool busy = true;BlockDriverState *bs; -BdrvNextIterator *it =NULL ; +BdrvNextIterator it;GSList *aio_ctxs =NULL , *ctx; -while ((it =bdrv_next (it, &bs))) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {AioContext *aio_context =bdrv_get_aio_context (bs);aio_context_acquire (aio_context);
@@ -302,10 +302,9 @@ void bdrv_drain_all(void)for (ctx = aio_ctxs; ctx !=NULL ; ctx = ctx->next) {AioContext *aio_context = ctx->data; - it =NULL ;aio_context_acquire (aio_context); -while ((it =bdrv_next (it, &bs))) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {if (aio_context ==bdrv_get_aio_context (bs)) {if (bdrv_requests_pending (bs)) { busy = true;
@@ -318,8 +317,7 @@ void bdrv_drain_all(void) } } - it =NULL ; -while ((it =bdrv_next (it, &bs))) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {AioContext *aio_context =bdrv_get_aio_context (bs);aio_context_acquire (aio_context);
block/snapshot.c +41 -14
145 lines not on this page · raw message
blockdev.c +2 -2
@@ -4164,9 +4164,9 @@ BlockJobInfoList *qmp_query_block_jobs(Error **errp) {BlockJobInfoList *head =NULL , **p_next = &head;BlockDriverState *bs; -BdrvNextIterator *it =NULL ; +BdrvNextIterator it; -while ((it =bdrv_next (it, &bs))) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {AioContext *aio_context =bdrv_get_aio_context (bs);aio_context_acquire (aio_context);
include/block/block.h +13 -2
@@ -17,7 +17,6 @@ typedef struct BlockJob BlockJob;typedef struct BdrvChild BdrvChild ;typedef struct BdrvChildRole BdrvChildRole ;typedef struct BlockJobTxn BlockJobTxn ; -typedef struct BdrvNextIterator BdrvNextIterator ; typedef struct BlockDriverInfo { /* in bytes, 0 if irrelevant */
@@ -402,7 +401,19 @@ BlockDriverState *bdrv_lookup_bs(const char *device, Error **errp);bool bdrv_chain_contains (BlockDriverState *top,BlockDriverState *base);BlockDriverState *bdrv_next_node (BlockDriverState *bs); -BdrvNextIterator *bdrv_next (BdrvNextIterator *it,BlockDriverState **bs); + +typedef struct BdrvNextIterator { +enum { +BDRV_NEXT_BACKEND_ROOTS , +BDRV_NEXT_MONITOR_OWNED , + } phase; +BlockBackend *blk; +BlockDriverState *bs; +}BdrvNextIterator ; + +BlockDriverState *bdrv_first (BdrvNextIterator *it); +BlockDriverState *bdrv_next (BdrvNextIterator *it); +BlockDriverState *bdrv_next_monitor_owned (BlockDriverState *bs);int bdrv_is_encrypted (BlockDriverState *bs);int bdrv_key_required (BlockDriverState *bs);
migration/block.c +2 -2
@@ -383,7 +383,7 @@ static void init_blk_migration(QEMUFile *f)BlockDriverState *bs;BlkMigDevState *bmds;int64_t sectors; -BdrvNextIterator *it =NULL ; +BdrvNextIterator it; block_mig_state.submitted =0 ; block_mig_state.read_done =0 ;
@@ -394,7 +394,7 @@ static void init_blk_migration(QEMUFile *f) block_mig_state.zero_blocks =migrate_zero_blocks (); -while ((it =bdrv_next (it, &bs))) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {if (bdrv_is_read_only (bs)) {continue ; }
monitor.c +2 -2
@@ -3428,12 +3428,12 @@ static void vm_completion(ReadLineState *rs, const char *str) {size_t len;BlockDriverState *bs; -BdrvNextIterator *it =NULL ; +BdrvNextIterator it; len =strlen (str);readline_set_completion_index (rs, len); -while ((it =bdrv_next (it, &bs))) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {SnapshotInfoList *snapshots, *snapshot;AioContext *ctx =bdrv_get_aio_context (bs);bool ok = false;
qmp.c +2 -3
@@ -181,7 +181,7 @@ void qmp_cont(Error **errp)Error *local_err =NULL ;BlockBackend *blk;BlockDriverState *bs; -BdrvNextIterator *it; +BdrvNextIterator it;/* if there is a dump in background, we should wait until the dump * finished */
@@ -201,8 +201,7 @@ void qmp_cont(Error **errp)blk_iostatus_reset (blk); } - it =NULL ; -while ((it =bdrv_next (it, &bs))) { +for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {bdrv_add_key (bs,NULL , &local_err);if (local_err) {error_propagate (errp, local_err);
Signature · 1 line
1.8.3.1
Quoted 10 lines · Kevin Wolf
The
bdrv_next()users all leaked the BdrvNextIterator after completing the iteration. Simply changingbdrv_next()to free the iterator before returning NULL at the end of list doesn't work because some callers exit the loop before looking at all BDSes.This patch moves the BdrvNextIterator from the heap to the stack of the caller and switches to a bdrv_first()/bdrv_next() interface for initialising the iterator.
Signed-off-by Kevin Wolf <[email protected]>
Kevin Wolf
AppliedAm 20.05.2016 um 19:17 hat Kevin Wolf geschrieben:
Quoted 10 lines
The
bdrv_next()users all leaked the BdrvNextIterator after completing the iteration. Simply changingbdrv_next()to free the iterator before returning NULL at the end of list doesn't work because some callers exit the loop before looking at all BDSes.This patch moves the BdrvNextIterator from the heap to the stack of the caller and switches to a bdrv_first()/bdrv_next() interface for initialising the iterator.
Signed-off-by Kevin Wolf <[email protected]>
Applied to the block branch.
Kevin