mirror of
https://github.com/mii443/qemu.git
synced 2025-08-22 15:15:46 +00:00
jobs: add job lock in find_* functions
Both blockdev.c and job-qmp.c have TOC/TOU conditions, because they first search for the job and then perform an action on it. Therefore, we need to do the search + action under the same job mutex critical section. Note: at this stage, job_{lock/unlock} and job lock guard macros are *nop*. Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Kevin Wolf <kwolf@redhat.com> Message-Id: <20220926093214.506243-9-eesposit@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
committed by
Kevin Wolf
parent
f41ab73fa2
commit
9624112441
57
job-qmp.c
57
job-qmp.c
@ -29,14 +29,19 @@
|
||||
#include "qapi/error.h"
|
||||
#include "trace/trace-root.h"
|
||||
|
||||
/* Get a job using its ID and acquire its AioContext */
|
||||
static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
|
||||
/*
|
||||
* Get a job using its ID and acquire its AioContext.
|
||||
* Called with job_mutex held.
|
||||
*/
|
||||
static Job *find_job_locked(const char *id,
|
||||
AioContext **aio_context,
|
||||
Error **errp)
|
||||
{
|
||||
Job *job;
|
||||
|
||||
*aio_context = NULL;
|
||||
|
||||
job = job_get(id);
|
||||
job = job_get_locked(id);
|
||||
if (!job) {
|
||||
error_setg(errp, "Job not found");
|
||||
return NULL;
|
||||
@ -51,71 +56,86 @@ static Job *find_job(const char *id, AioContext **aio_context, Error **errp)
|
||||
void qmp_job_cancel(const char *id, Error **errp)
|
||||
{
|
||||
AioContext *aio_context;
|
||||
Job *job = find_job(id, &aio_context, errp);
|
||||
Job *job;
|
||||
|
||||
JOB_LOCK_GUARD();
|
||||
job = find_job_locked(id, &aio_context, errp);
|
||||
|
||||
if (!job) {
|
||||
return;
|
||||
}
|
||||
|
||||
trace_qmp_job_cancel(job);
|
||||
job_user_cancel(job, true, errp);
|
||||
job_user_cancel_locked(job, true, errp);
|
||||
aio_context_release(aio_context);
|
||||
}
|
||||
|
||||
void qmp_job_pause(const char *id, Error **errp)
|
||||
{
|
||||
AioContext *aio_context;
|
||||
Job *job = find_job(id, &aio_context, errp);
|
||||
Job *job;
|
||||
|
||||
JOB_LOCK_GUARD();
|
||||
job = find_job_locked(id, &aio_context, errp);
|
||||
|
||||
if (!job) {
|
||||
return;
|
||||
}
|
||||
|
||||
trace_qmp_job_pause(job);
|
||||
job_user_pause(job, errp);
|
||||
job_user_pause_locked(job, errp);
|
||||
aio_context_release(aio_context);
|
||||
}
|
||||
|
||||
void qmp_job_resume(const char *id, Error **errp)
|
||||
{
|
||||
AioContext *aio_context;
|
||||
Job *job = find_job(id, &aio_context, errp);
|
||||
Job *job;
|
||||
|
||||
JOB_LOCK_GUARD();
|
||||
job = find_job_locked(id, &aio_context, errp);
|
||||
|
||||
if (!job) {
|
||||
return;
|
||||
}
|
||||
|
||||
trace_qmp_job_resume(job);
|
||||
job_user_resume(job, errp);
|
||||
job_user_resume_locked(job, errp);
|
||||
aio_context_release(aio_context);
|
||||
}
|
||||
|
||||
void qmp_job_complete(const char *id, Error **errp)
|
||||
{
|
||||
AioContext *aio_context;
|
||||
Job *job = find_job(id, &aio_context, errp);
|
||||
Job *job;
|
||||
|
||||
JOB_LOCK_GUARD();
|
||||
job = find_job_locked(id, &aio_context, errp);
|
||||
|
||||
if (!job) {
|
||||
return;
|
||||
}
|
||||
|
||||
trace_qmp_job_complete(job);
|
||||
job_complete(job, errp);
|
||||
job_complete_locked(job, errp);
|
||||
aio_context_release(aio_context);
|
||||
}
|
||||
|
||||
void qmp_job_finalize(const char *id, Error **errp)
|
||||
{
|
||||
AioContext *aio_context;
|
||||
Job *job = find_job(id, &aio_context, errp);
|
||||
Job *job;
|
||||
|
||||
JOB_LOCK_GUARD();
|
||||
job = find_job_locked(id, &aio_context, errp);
|
||||
|
||||
if (!job) {
|
||||
return;
|
||||
}
|
||||
|
||||
trace_qmp_job_finalize(job);
|
||||
job_ref(job);
|
||||
job_finalize(job, errp);
|
||||
job_ref_locked(job);
|
||||
job_finalize_locked(job, errp);
|
||||
|
||||
/*
|
||||
* Job's context might have changed via job_finalize (and job_txn_apply
|
||||
@ -123,21 +143,24 @@ void qmp_job_finalize(const char *id, Error **errp)
|
||||
* one.
|
||||
*/
|
||||
aio_context = job->aio_context;
|
||||
job_unref(job);
|
||||
job_unref_locked(job);
|
||||
aio_context_release(aio_context);
|
||||
}
|
||||
|
||||
void qmp_job_dismiss(const char *id, Error **errp)
|
||||
{
|
||||
AioContext *aio_context;
|
||||
Job *job = find_job(id, &aio_context, errp);
|
||||
Job *job;
|
||||
|
||||
JOB_LOCK_GUARD();
|
||||
job = find_job_locked(id, &aio_context, errp);
|
||||
|
||||
if (!job) {
|
||||
return;
|
||||
}
|
||||
|
||||
trace_qmp_job_dismiss(job);
|
||||
job_dismiss(&job, errp);
|
||||
job_dismiss_locked(&job, errp);
|
||||
aio_context_release(aio_context);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user