Fix nested permission update
cover Fix nested permission update
Hi! With help of some assertions (patch 2) I've found that bdrv_drop_intermediate() do nested permission update which in my opinion may lead to unpredictable behavior.
Vladimir Sementsov-Ogievskiy (2):
block: make bdrv_drop_intermediate() less wrong
block: assert that permission commit sets same permissions
block.c | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-)
Signature · 1 line
2.21.3
PATCH 1/2 block: make bdrv_drop_intermediate() less wrong
First, permission update loop tries to do iterations transactionally, but the whole update is not transactional: nobody roll-back successful loop iterations when some iteration fails.
Second, in the iteration we have nested permission update:c->klass->update_filename may point to bdrv_child_cb_update_filename() which calls bdrv_backing_update_filename(), which may do node reopen to RW.
Permission update system is not prepared to nested updates, at least it has intermediate permission-update state stored in BdrvChild
structures: has_backup_perm, backup_perm and backup_shared_perm.
So, let's first do bdrv_replace_node() (which is more transactional than open-coded update in bdrv_drop_intermediate()) and then call update_filename() in separate. We still do not rollback changes in case of update_filename() failure but it's not much worse than pre-patch behavior.
--- block.c | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-)
block.c +15 -21
@@ -4958,36 +4958,30 @@ int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, backing_file_str = base->filename; } - QLIST_FOREACH_SAFE(c, &top->parents, next_parent, next) { -/* Check whether we are allowed to switch c from top to base */ -GSList *ignore_children =g_slist_prepend (NULL , c); - ret =bdrv_check_update_perm (base,NULL , c->perm, c->shared_perm, - ignore_children,NULL , &local_err); -g_slist_free (ignore_children); -if (ret <0 ) { -error_report_err (local_err); - goto exit; - } +bdrv_replace_node (top, base, &local_err); +if (local_err) { +error_report_err (local_err); + goto exit; + } -/* If so, update the backing file path in the image file */ + QLIST_FOREACH_SAFE(c, &base->parents, next_parent, next) {if (c->klass->update_filename) { ret = c->klass->update_filename (c, base, backing_file_str, &local_err);if (ret <0 ) { -bdrv_abort_perm_update (base); +/* +* TODO: Actually, we want to rollback all previous iterations +* of this loop, and (which is almost impossible) previous +* bdrv_replace_node()... +* +* Note, that c->klass->update_filename may lead to permission +* update, so it's a bad idea to call it inside permission +* update transaction of bdrv_replace_node. +*/ error_report_err (local_err); goto exit; } } - -/* -* Do the actual switch in the in-memory graph. -* Completes bdrv_check_update_perm() transaction internally. -* c->frozen is false, we have checked that above. -*/ -bdrv_ref (base); -bdrv_replace_child (c, base); -bdrv_unref (top); }if (update_inherits_from) {
Signature · 1 line
2.21.3
PATCH 2/2 block: assert that permission commit sets same permissions
In reply to Vladimir Sementsov-OgievskiyOn permission update commit we must set same permissions as on _check_. Let's add assertions. Next step may be to drop permission parameters from _set_.
Note that prior to previous commit, fixing bdrv_drop_intermediate(), new assertion in bdrv_child_set_perm() crashes on iotests 30 and 40.
--- block.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
block.c +9 -2
@@ -2105,9 +2105,10 @@ static void bdrv_abort_perm_update(BlockDriverState *bs) } } -static void bdrv_set_perm (BlockDriverState *bs,uint64_t cumulative_perms, -uint64_t cumulative_shared_perms) +static void bdrv_set_perm (BlockDriverState *bs,uint64_t _cumulative_perms, +uint64_t _cumulative_shared_perms) { +uint64_t cumulative_perms, cumulative_shared_perms;BlockDriver *drv = bs->drv;BdrvChild *c;
@@ -2115,6 +2116,10 @@ static void bdrv_set_perm(BlockDriverState *bs, uint64_t cumulative_perms,return ; } +bdrv_get_cumulative_perm (bs, &cumulative_perms, &cumulative_shared_perms); +assert (_cumulative_perms == cumulative_perms); +assert (_cumulative_shared_perms == cumulative_shared_perms); +/* Update this node */ if (drv->bdrv_set_perm) { drv->bdrv_set_perm (bs, cumulative_perms, cumulative_shared_perms);
@@ -2301,6 +2306,8 @@ static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared) c->has_backup_perm = false; +assert (c->perm == perm); +assert (c->shared_perm == shared); c->perm = perm; c->shared_perm = shared;
Signature · 1 line
2.21.3
Re: [PATCH 2/2] block: assert that permission commit sets same permissions
Quoted 42 lines · Vladimir Sementsov-Ogievskiy
On permission update commit we must set same permissions as on _check_. Let's add assertions. Next step may be to drop permission parameters from _set_.
Note that prior to previous commit, fixing
bdrv_drop_intermediate(), new assertion inbdrv_child_set_perm()crashes on iotests 30 and 40.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)block.c +7
-2@@ -2105,9 +2105,10 @@ static void bdrv_abort_perm_update(BlockDriverState *bs) } } -static void bdrv_set_perm (BlockDriverState *bs,uint64_t cumulative_perms, -uint64_t cumulative_shared_perms) +static void bdrv_set_perm (BlockDriverState *bs,uint64_t _cumulative_perms, +uint64_t _cumulative_shared_perms) { +uint64_t cumulative_perms, cumulative_shared_perms;BlockDriver *drv = bs->drv;BdrvChild *c;@@ -2115,6 +2116,10 @@ static void bdrv_set_perm(BlockDriverState *bs, uint64_t cumulative_perms,return ; } +bdrv_get_cumulative_perm (bs, &cumulative_perms, &cumulative_shared_perms); +assert (_cumulative_perms == cumulative_perms); +assert (_cumulative_shared_perms == cumulative_shared_perms); +/* Update this node */ if (drv->bdrv_set_perm) { drv->bdrv_set_perm (bs, cumulative_perms, cumulative_shared_perms);
block.c static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared) · line 2313
Quoted 8 lines
block.c +2
-0@@ -2301,6 +2306,8 @@ static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared) c->has_backup_perm = false; +assert (c->perm == perm); +assert (c->shared_perm == shared); c->perm = perm; c->shared_perm = shared;
squash:
@@ -2308,8 +2308,6 @@ static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared)
assert(c->perm == perm);
assert(c->shared_perm == shared);- c->perm = perm;
- c->shared_perm = shared;
bdrv_get_cumulative_perm(c->bs, &cumulative_perms,
&cumulative_shared_perms);Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 1/2] block: make bdrv_drop_intermediate() less wrong
In reply to Vladimir Sementsov-OgievskiyQuoted 3 lines · Vladimir Sementsov-Ogievskiy
First, permission update loop tries to do iterations transactionally, but the whole update is not transactional: nobody roll-back successful loop iterations when some iteration fails.
Indeed. Another thing that should be noted:
bdrv_check_update_perm() doc:
Quoted 2 lines
Needs to be followed by a call to either
bdrv_set_perm()orbdrv_abort_perm_update().
And besides rolling back on error, we don’t commit on success either.
Quoted 15 lines
Second, in the iteration we have nested permission update:
c->klass->update_filenamemay point tobdrv_child_cb_update_filename()which callsbdrv_backing_update_filename(), which may do node reopen to RW.Permission update system is not prepared to nested updates, at least it has intermediate permission-update state stored in BdrvChild
structures:has_backup_perm,backup_permandbackup_shared_perm.So, let's first do
bdrv_replace_node()(which is more transactional than open-coded update inbdrv_drop_intermediate()) and then callupdate_filename()in separate. We still do not rollback changes in case ofupdate_filename()failure but it's not much worse than pre-patch behavior.
Quoted 4 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-)
And it’s also much simpler and clearer now.
Re: [PATCH 1/2] block: make bdrv_drop_intermediate() less wrong
Quoted 4 lines · Max Reitz
Quoted 3 lines · Vladimir Sementsov-Ogievskiy
First, permission update loop tries to do iterations transactionally, but the whole update is not transactional: nobody roll-back successful loop iterations when some iteration fails.
[...]
Quoted 1 line
And besides rolling back on error, we don’t commit on success either.
(Oh, we do, through bdrv_replace_child(). Well.)
Max
Re: [PATCH 2/2] block: assert that permission commit sets same permissions
In reply to Vladimir Sementsov-OgievskiyQuoted 16 lines · Vladimir Sementsov-Ogievskiy
On permission update commit we must set same permissions as on _check_. Let's add assertions. Next step may be to drop permission parameters from _set_.
Note that prior to previous commit, fixing
bdrv_drop_intermediate(), new assertion inbdrv_child_set_perm()crashes on iotests 30 and 40.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>--- block.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)block.c +0
-0
[...]
block.c static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared) · line 2312
Quoted 8 lines
block.c +2
-0@@ -2301,6 +2306,8 @@ static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared) c->has_backup_perm = false; +assert (c->perm == perm); +assert (c->shared_perm == shared); c->perm = perm; c->shared_perm = shared;
Then we can drop the assignments, no?
(And, as you write, in the future potentially drop the parameters.)
Anyway:
Re: [PATCH 1/2] block: make bdrv_drop_intermediate() less wrong
In reply to Vladimir Sementsov-OgievskiyQuoted 9 lines · Vladimir Sementsov-Ogievskiy
block.c +0
-5@@ -4958,36 +4958,30 @@ int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, backing_file_str = base->filename; } - QLIST_FOREACH_SAFE(c, &top->parents, next_parent, next) { -/* Check whether we are allowed to switch c from top to base */ -GSList *ignore_children =g_slist_prepend (NULL , c); - ret =bdrv_check_update_perm (base,NULL , c->perm, c->shared_perm, - ignore_children,NULL , &local_err);
block.c int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, · line 4965
Quoted 10 lines
block.c +5
-5-g_slist_free (ignore_children); -if (ret <0 ) { -error_report_err (local_err); - goto exit; - } +bdrv_replace_node (top, base, &local_err); +if (local_err) { +error_report_err (local_err); + goto exit; + }
At the beginning of this function there's a check for c->frozen. I think you can remove it safely because you also have it in bdrv_replace_node()
Berto
Re: [PATCH 1/2] block: make bdrv_drop_intermediate() less wrong
In reply to Vladimir Sementsov-OgievskiyQuoted 1 line · Vladimir Sementsov-Ogievskiy
-
QLIST_FOREACH_SAFE(c, &top->parents, next_parent, next){
/* ... */
Quoted 1 line
+
QLIST_FOREACH_SAFE(c, &base->parents, next_parent, next){
I also wonder, is top->parents and base->parents guaranteed to be the same list in this case?
If not you could store the list of top->parents before calling bdrv_replace_node() and use it afterwards.
QLIST_FOREACH(c, &top->parents, next_parent) {
parents = g_slist_prepend(parents, c);
}Berto
Re: [PATCH 1/2] block: make bdrv_drop_intermediate() less wrong
Quoted 8 lines · Alberto Garcia
Quoted 1 line · Vladimir Sementsov-Ogievskiy
-
QLIST_FOREACH_SAFE(c, &top->parents, next_parent, next){/* ... */
Quoted 1 line
+
QLIST_FOREACH_SAFE(c, &base->parents, next_parent, next){I also wonder, is
top->parentsandbase->parentsguaranteed to be the same list in this case?
Quoted 9 lines
If not you could store the list of
top->parentsbefore callingbdrv_replace_node()and use it afterwards.QLIST_FOREACH(c, &top->parents, next_parent) { parents = g_slist_prepend(parents, c); }Berto
Hmm... We should not touch other parents of base, which it had prior to bdrv_replace_node().
On the other hand, it should be safe to call update_filename for not-updated parents..
And we should keep in mind that bdrv_replace_node may replace not all children. Still, in this case we must replace all of them. Seems, we need additional argument for bdrv_replace_node() to fail, if it want's to skip some children replacement.
So, I'm going to resend to add this parameter to bdrv_replace_node() and will use your suggestion to be stricter on what we update, thanks!
Signature · 2 lines
Best regards, Vladimir