job: Fix nested aio_poll() hanging in job_txn_apply
PATCH 1/1 job: Fix nested aio_poll() hanging in job_txn_apply
All callers have acquired ctx already. Doing that again results in aio_poll() hang. This fixes the problem that a BDRV_POLL_WHILE() in the callback cannot make progress because ctx is recursively locked, for example, when drive-backup finishes.
--- job.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)
job.c +5 -13
@@ -136,21 +136,13 @@ static void job_txn_del_job(Job *job) } } -static int job_txn_apply (JobTxn *txn,int fn (Job *),bool lock) +static int job_txn_apply (JobTxn *txn,int fn (Job *)) { -AioContext *ctx;Job *job, *next;int rc =0 ; QLIST_FOREACH_SAFE(job, &txn->jobs, txn_list, next) { -if (lock) { - ctx = job->aio_context; -aio_context_acquire (ctx); - } rc =fn (job); -if (lock) { -aio_context_release (ctx); - }if (rc) {break ; }
@@ -807,11 +799,11 @@ static void job_do_finalize(Job *job)assert (job && job->txn);/* prepare the transaction to complete */ - rc =job_txn_apply (job->txn, job_prepare, true); + rc =job_txn_apply (job->txn, job_prepare);if (rc) {job_completed_txn_abort (job); }else { -job_txn_apply (job->txn, job_finalize_single, true); +job_txn_apply (job->txn, job_finalize_single); } }
@@ -857,10 +849,10 @@ static void job_completed_txn_success(Job *job)assert (other_job->ret ==0 ); } -job_txn_apply (txn, job_transition_to_pending, false); +job_txn_apply (txn, job_transition_to_pending);/* If no jobs need manual finalization, automatically do so */ -if (job_txn_apply (txn, job_needs_finalize, false) ==0 ) { +if (job_txn_apply (txn, job_needs_finalize) ==0 ) {job_do_finalize (job); } }
Signature · 1 line
2.17.1
Quoted 17 lines · Fam Zheng
All callers have acquired ctx already. Doing that again results in
aio_poll()hang. This fixes the problem that aBDRV_POLL_WHILE()in the callback cannot make progress because ctx is recursively locked, for example, when drive-backup finishes.Reported-by Gu Nini <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- job.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)job.c +0
-0
job.c static void job_txn_del_job(Job *job) · line 139
Quoted 5 lines
job.c +1
-1@@ -136,21 +136,13 @@ static void job_txn_del_job(Job *job) } } -static int job_txn_apply (JobTxn *txn,int fn (Job *),bool lock) +static int job_txn_apply (JobTxn *txn,int fn (Job *))
Is it worth adding a comment here that callers must have already acquired the job context?
However, I was unable to quickly audit whether all callers really did have the lock (it balloons into whether all callers of job_finalize() have the lock), so I'm reluctant to give R-b.
job.c static void job_completed_txn_success(Job *job) · line 856
Quoted 11 lines
job.c +2
-2@@ -857,10 +849,10 @@ static void job_completed_txn_success(Job *job)assert (other_job->ret ==0 ); } -job_txn_apply (txn, job_transition_to_pending, false); +job_txn_apply (txn, job_transition_to_pending);/* If no jobs need manual finalization, automatically do so */ -if (job_txn_apply (txn, job_needs_finalize, false) ==0 ) { +if (job_txn_apply (txn, job_needs_finalize) ==0 ) {job_do_finalize (job);
That said, the change makes sense here: since the first direct call to job_txn_apply() did not need to lock, why should the second indirect call through job_do_finalize() need it?
Or, is the fix to have job_do_finalize() gain a bool parameter, where job_completed_txn_success() would pass false to that parameter, while job_finalize() would pass true (again, going back to auditing whether all callers of job_finalize() have already acquired the context).
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 13 lines · Eric Blake
Quoted 12 lines · Fam Zheng
All callers have acquired ctx already. Doing that again results in
aio_poll()hang. This fixes the problem that aBDRV_POLL_WHILE()in the callback cannot make progress because ctx is recursively locked, for example, when drive-backup finishes.Reported-by Gu Nini <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- job.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)
Quoted 12 lines
job.c static void job_txn_del_job(Job *job) · line 138
Quoted 9 lines
job.c +1
-1@@ -136,21 +136,13 @@ static void job_txn_del_job(Job *job) } } -static int job_txn_apply (JobTxn *txn,int fn (Job *),bool lock) +static int job_txn_apply (JobTxn *txn,int fn (Job *))Is it worth adding a comment here that callers must have already acquired the job context?
I think most job accessing functions require it. If any, the comment should go to the public functions like job_finalize().
Quoted 19 lines
However, I was unable to quickly audit whether all callers really did have the lock (it balloons into whether all callers of
job_finalize()have the lock), so I'm reluctant to give R-b.job.c static void job_completed_txn_success(Job *job) · line 854
Quoted 9 lines
job.c +2
-2@@ -857,10 +849,10 @@ static void job_completed_txn_success(Job *job)assert (other_job->ret ==0 ); } -job_txn_apply (txn, job_transition_to_pending, false); +job_txn_apply (txn, job_transition_to_pending);/* If no jobs need manual finalization, automatically do so */ -if (job_txn_apply (txn, job_needs_finalize, false) ==0 ) { +if (job_txn_apply (txn, job_needs_finalize) ==0 ) {job_do_finalize (job);That said, the change makes sense here: since the first direct call to
job_txn_apply()did not need to lock, why should the second indirect call throughjob_do_finalize()need it?
Quoted 4 lines
Or, is the fix to have
job_do_finalize()gain a bool parameter, wherejob_completed_txn_success()would pass false to that parameter, whilejob_finalize()would pass true (again, going back to auditing whether all callers ofjob_finalize()have already acquired the context).
There are two callers of job_finalize():
fam@lemon:~/work/qemu [master]$ git grep -w -A1 '^\s*job_finalize'
blockdev.c: job_finalize(&job->job, errp);
blockdev.c- aio_context_release(aio_context);
--
job-qmp.c: job_finalize(job, errp);
job-qmp.c- aio_context_release(aio_context);
--
tests/test-blockjob.c: job_finalize(&job->job, &error_abort);
tests/test-blockjob.c- assert(job->job.status == JOB_STATUS_CONCLUDED);Ignoring the test, it's very easy to see both callers have acquired the context.
Fam
Eric Blake
Reviewed-byQuoted 18 lines · Fam Zheng
Quoted 18 lines
However, I was unable to quickly audit whether all callers really did have the lock (it balloons into whether all callers of
job_finalize()have the lock), so I'm reluctant to give R-b.job.c static void job_completed_txn_success(Job *job) · line 854
Quoted 9 lines
job.c +2
-2@@ -857,10 +849,10 @@ static void job_completed_txn_success(Job *job)assert (other_job->ret ==0 ); } -job_txn_apply (txn, job_transition_to_pending, false); +job_txn_apply (txn, job_transition_to_pending);/* If no jobs need manual finalization, automatically do so */ -if (job_txn_apply (txn, job_needs_finalize, false) ==0 ) { +if (job_txn_apply (txn, job_needs_finalize) ==0 ) {job_do_finalize (job);That said, the change makes sense here: since the first direct call to
job_txn_apply()did not need to lock, why should the second indirect call throughjob_do_finalize()need it?
Quoted 6 lines
Quoted 4 lines
Or, is the fix to have
job_do_finalize()gain a bool parameter, wherejob_completed_txn_success()would pass false to that parameter, whilejob_finalize()would pass true (again, going back to auditing whether all callers ofjob_finalize()have already acquired the context).There are two callers of
job_finalize():
Okay, so the audit would have been easier if I had actually tried grepping for it. Still, it can't hurt to make the commit message give more details on what you checked, to make it easier for the reviewers.
Quoted 7 lines
fam@lemon:~/work/qemu [master]$ git grep -w -A1 '^\s*job_finalize'
blockdev.c: job_finalize(&job->job, errp);
blockdev.c- aio_context_release(aio_context);
--
job-qmp.c: job_finalize(job, errp);
job-qmp.c- aio_context_release(aio_context);Yep, that's pretty conclusive that the context is already held by all callers.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org