qemu_thread: supplement error handling for iothread_complete
PATCH 7/11 qemu_thread: supplement error handling for iothread_complete
From: Fei Li <[email protected]>
For iothread_complete: utilize the existed errp to propagate the error and do the corresponding cleanup to replace the temporary &error_abort.
--- iothread.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
iothread.c +13 -6
@@ -148,6 +148,7 @@ static void iothread_complete(UserCreatable *obj, Error **errp)Error *local_error =NULL ;IOThread *iothread =IOTHREAD (obj);char *name, *thread_name; +int thread_ok; iothread->stopping = false; iothread->running = true;
@@ -164,9 +165,7 @@ static void iothread_complete(UserCreatable *obj, Error **errp) &local_error);if (local_error) {error_propagate (errp, local_error); -aio_context_unref (iothread->ctx); - iothread->ctx =NULL ; -return ; + goto fail; }qemu_mutex_init (&iothread->init_done_lock);
@@ -178,11 +177,15 @@ static void iothread_complete(UserCreatable *obj, Error **errp)*/ name =object_get_canonical_path_component (OBJECT (obj)); thread_name =g_strdup_printf ("IO %s" , name); -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&iothread->thread, thread_name, iothread_run, - iothread,QEMU_THREAD_JOINABLE , &error_abort); + thread_ok =qemu_thread_create (&iothread->thread, thread_name, iothread_run, + iothread,QEMU_THREAD_JOINABLE , errp);g_free (thread_name);g_free (name); +if (thread_ok <0 ) { +qemu_cond_destroy (&iothread->init_done_cond); +qemu_mutex_destroy (&iothread->init_done_lock); + goto fail; + }/* Wait for initialization to complete */ qemu_mutex_lock (&iothread->init_done_lock);
@@ -191,6 +194,10 @@ static void iothread_complete(UserCreatable *obj, Error **errp) &iothread->init_done_lock); }qemu_mutex_unlock (&iothread->init_done_lock); +return ; +fail: +aio_context_unref (iothread->ctx); + iothread->ctx =NULL ; } typedef struct {
Signature · 1 line
2.17.2 (Apple Git-113)
PATCH 7/11 qemu_thread: supplement error handling for iothread_complete
From: Fei Li <[email protected]>
For iothread_complete: utilize the existed errp to propagate the error and do the corresponding cleanup to replace the temporary &error_abort.
--- iothread.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
iothread.c +13 -6
@@ -148,6 +148,7 @@ static void iothread_complete(UserCreatable *obj, Error **errp)Error *local_error =NULL ;IOThread *iothread =IOTHREAD (obj);char *name, *thread_name; +int thread_ok; iothread->stopping = false; iothread->running = true;
@@ -164,9 +165,7 @@ static void iothread_complete(UserCreatable *obj, Error **errp) &local_error);if (local_error) {error_propagate (errp, local_error); -aio_context_unref (iothread->ctx); - iothread->ctx =NULL ; -return ; + goto fail; }qemu_mutex_init (&iothread->init_done_lock);
@@ -178,11 +177,15 @@ static void iothread_complete(UserCreatable *obj, Error **errp)*/ name =object_get_canonical_path_component (OBJECT (obj)); thread_name =g_strdup_printf ("IO %s" , name); -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&iothread->thread, thread_name, iothread_run, - iothread,QEMU_THREAD_JOINABLE , &error_abort); + thread_ok =qemu_thread_create (&iothread->thread, thread_name, iothread_run, + iothread,QEMU_THREAD_JOINABLE , errp);g_free (thread_name);g_free (name); +if (thread_ok <0 ) { +qemu_cond_destroy (&iothread->init_done_cond); +qemu_mutex_destroy (&iothread->init_done_lock); + goto fail; + }/* Wait for initialization to complete */ qemu_mutex_lock (&iothread->init_done_lock);
@@ -191,6 +194,10 @@ static void iothread_complete(UserCreatable *obj, Error **errp) &iothread->init_done_lock); }qemu_mutex_unlock (&iothread->init_done_lock); +return ; +fail: +aio_context_unref (iothread->ctx); + iothread->ctx =NULL ; } typedef struct {
Signature · 1 line
2.17.2 (Apple Git-113)
PATCH 8/11 qemu_thread: supplement error handling for qemu_signalfd_compat
In reply to Fei LiFrom: Fei Li <[email protected]>
For qemu_signalfd_compat: set errno, do some cleanup, and return -1 to replace the temporary &error_abort when failing to create sigwait_compat.
--- util/compatfd.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
util/compatfd.c +10 -3
@@ -71,6 +71,7 @@ static int qemu_signalfd_compat(const sigset_t *mask)struct sigfd_compat_info *info;QemuThread thread;int fds[2 ]; +int ret; info =malloc (sizeof (*info));if (info ==NULL ) {
@@ -89,9 +90,15 @@ static int qemu_signalfd_compat(const sigset_t *mask)memcpy (&info->mask, mask,sizeof (*mask)); info->fd = fds[1 ]; -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&thread,"signalfd_compat" , sigwait_compat, - info,QEMU_THREAD_DETACHED , &error_abort); + ret =qemu_thread_create (&thread,"signalfd_compat" , sigwait_compat, + info,QEMU_THREAD_DETACHED ,NULL ); +if (ret <0 ) { +close (fds[0 ]); +close (fds[1 ]); +free (info); + errno = -ret; +return -1 ; + }return fds[0 ]; }
Signature · 1 line
2.17.2 (Apple Git-113)
PATCH 8/11 qemu_thread: supplement error handling for qemu_signalfd_compat
In reply to Fei LiFrom: Fei Li <[email protected]>
For qemu_signalfd_compat: set errno, do some cleanup, and return -1 to replace the temporary &error_abort when failing to create sigwait_compat.
--- util/compatfd.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
util/compatfd.c +10 -3
@@ -71,6 +71,7 @@ static int qemu_signalfd_compat(const sigset_t *mask)struct sigfd_compat_info *info;QemuThread thread;int fds[2 ]; +int ret; info =malloc (sizeof (*info));if (info ==NULL ) {
@@ -89,9 +90,15 @@ static int qemu_signalfd_compat(const sigset_t *mask)memcpy (&info->mask, mask,sizeof (*mask)); info->fd = fds[1 ]; -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&thread,"signalfd_compat" , sigwait_compat, - info,QEMU_THREAD_DETACHED , &error_abort); + ret =qemu_thread_create (&thread,"signalfd_compat" , sigwait_compat, + info,QEMU_THREAD_DETACHED ,NULL ); +if (ret <0 ) { +close (fds[0 ]); +close (fds[1 ]); +free (info); + errno = -ret; +return -1 ; + }return fds[0 ]; }
Signature · 1 line
2.17.2 (Apple Git-113)
PATCH 9/11 qemu_thread: supplement error handling for migration
In reply to Fei LiFrom: Fei Li <[email protected]>
Update qemu_thread_create()'s callers by
- setting an error on qemu_thread_create() failure for callers that set an error on failure;
- reporting the error and returning failure for callers that return an error code on failure;
- reporting the error and setting some state for callers that just report errors and choose not to continue on.
Besides, make compress_threads_save_cleanup() cope with partially initialized comp_param[i] to adapt to the new qemu_thread_create() failure case.
--- migration/migration.c | 35 +++++++++++++------- migration/postcopy-ram.c | 16 ++++++--- migration/ram.c | 70 ++++++++++++++++++++++++++-------------- migration/savevm.c | 12 ++++--- 4 files changed, 89 insertions(+), 44 deletions(-)
migration/migration.c +24 -11
@@ -447,10 +447,13 @@ static void process_incoming_migration_co(void *opaque) goto fail; } -/* TODO: let the further caller handle the error instead of abort() */ -qemu_thread_create (&mis->colo_incoming_thread,"COLO incoming" , - colo_process_incoming_thread, mis, -QEMU_THREAD_JOINABLE , &error_abort); +if (qemu_thread_create (&mis->colo_incoming_thread,"COLO incoming" , + colo_process_incoming_thread, mis, +QEMU_THREAD_JOINABLE , &local_err) <0 ) { +error_reportf_err (local_err,"failed to create " +"colo_process_incoming_thread: " ); + goto fail; + } mis->have_colo_incoming_thread = true;qemu_coroutine_yield ();
@@ -2349,6 +2352,7 @@ out:static int open_return_path_on_source (MigrationState *ms,bool create_thread) { +Error *local_err =NULL ; ms->rp_state.from_dst_file =qemu_file_get_return_path (ms->to_dst_file);if (!ms->rp_state.from_dst_file) {
@@ -2362,10 +2366,15 @@ static int open_return_path_on_source(MigrationState *ms,return 0 ; } -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&ms->rp_state.rp_thread,"return path" , - source_return_path_thread, ms, -QEMU_THREAD_JOINABLE , &error_abort); +if (qemu_thread_create (&ms->rp_state.rp_thread,"return path" , + source_return_path_thread, ms, +QEMU_THREAD_JOINABLE , &local_err) <0 ) { +error_reportf_err (local_err, +"failed to create source_return_path_thread: " ); +qemu_fclose (ms->rp_state.from_dst_file); + ms->rp_state.from_dst_file =NULL ; +return -1 ; + }trace_open_return_path_on_source_continue ();
@@ -3201,9 +3210,13 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)migrate_fd_cleanup (s);return ; } -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE , &error_abort); +if (qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, +QEMU_THREAD_JOINABLE , &error_in) <0 ) { +error_reportf_err (error_in,"failed to create migration_thread: " ); +migrate_set_state (&s->state, s->state,MIGRATION_STATUS_FAILED ); +migrate_fd_cleanup (s); +return ; + } s->migration_thread_running = true; }
migration/postcopy-ram.c +12 -4
@@ -1083,6 +1083,8 @@ retry:int postcopy_ram_enable_notify (MigrationIncomingState *mis) { +Error *local_err =NULL ; +/* Open the fd for the kernel to give us userfaults */ mis->userfault_fd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK );if (mis->userfault_fd ==-1 ) {
@@ -1109,10 +1111,16 @@ int postcopy_ram_enable_notify(MigrationIncomingState *mis) }qemu_sem_init (&mis->fault_thread_sem,0 ); -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&mis->fault_thread,"postcopy/fault" , - postcopy_ram_fault_thread, mis, -QEMU_THREAD_JOINABLE , &error_abort); +if (qemu_thread_create (&mis->fault_thread,"postcopy/fault" , + postcopy_ram_fault_thread, mis, +QEMU_THREAD_JOINABLE , &local_err) <0 ) { +error_reportf_err (local_err, +"failed to create postcopy_ram_fault_thread: " ); +close (mis->userfault_event_fd); +close (mis->userfault_fd); +qemu_sem_destroy (&mis->fault_thread_sem); +return -1 ; + }qemu_sem_wait (&mis->fault_thread_sem);qemu_sem_destroy (&mis->fault_thread_sem); mis->have_fault_thread = true;
migration/ram.c +45 -25
125 lines not on this page · raw message
migration/savevm.c +8 -4
@@ -1747,10 +1747,14 @@ static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis) mis->have_listen_thread = true;/* Start up the listening thread and wait for it to signal ready */ qemu_sem_init (&mis->listen_thread_sem,0 ); -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&mis->listen_thread,"postcopy/listen" , - postcopy_ram_listen_thread,NULL , -QEMU_THREAD_DETACHED , &error_abort); +if (qemu_thread_create (&mis->listen_thread,"postcopy/listen" , + postcopy_ram_listen_thread,NULL , +QEMU_THREAD_DETACHED , &local_err) <0 ) { +error_reportf_err (local_err, +"failed to create postcopy_ram_listen_thread: " ); +qemu_sem_destroy (&mis->listen_thread_sem); +return -1 ; + }qemu_sem_wait (&mis->listen_thread_sem);qemu_sem_destroy (&mis->listen_thread_sem);
Signature · 1 line
2.17.2 (Apple Git-113)
PATCH 9/11 qemu_thread: supplement error handling for migration
In reply to Fei LiFrom: Fei Li <[email protected]>
Update qemu_thread_create()'s callers by
- setting an error on qemu_thread_create() failure for callers that set an error on failure;
- reporting the error and returning failure for callers that return an error code on failure;
- reporting the error and setting some state for callers that just report errors and choose not to continue on.
Besides, make compress_threads_save_cleanup() cope with partially initialized comp_param[i] to adapt to the new qemu_thread_create() failure case.
--- migration/migration.c | 35 +++++++++++++------- migration/postcopy-ram.c | 16 ++++++--- migration/ram.c | 70 ++++++++++++++++++++++++++-------------- migration/savevm.c | 12 ++++--- 4 files changed, 89 insertions(+), 44 deletions(-)
migration/migration.c +24 -11
@@ -447,10 +447,13 @@ static void process_incoming_migration_co(void *opaque) goto fail; } -/* TODO: let the further caller handle the error instead of abort() */ -qemu_thread_create (&mis->colo_incoming_thread,"COLO incoming" , - colo_process_incoming_thread, mis, -QEMU_THREAD_JOINABLE , &error_abort); +if (qemu_thread_create (&mis->colo_incoming_thread,"COLO incoming" , + colo_process_incoming_thread, mis, +QEMU_THREAD_JOINABLE , &local_err) <0 ) { +error_reportf_err (local_err,"failed to create " +"colo_process_incoming_thread: " ); + goto fail; + } mis->have_colo_incoming_thread = true;qemu_coroutine_yield ();
@@ -2349,6 +2352,7 @@ out:static int open_return_path_on_source (MigrationState *ms,bool create_thread) { +Error *local_err =NULL ; ms->rp_state.from_dst_file =qemu_file_get_return_path (ms->to_dst_file);if (!ms->rp_state.from_dst_file) {
@@ -2362,10 +2366,15 @@ static int open_return_path_on_source(MigrationState *ms,return 0 ; } -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&ms->rp_state.rp_thread,"return path" , - source_return_path_thread, ms, -QEMU_THREAD_JOINABLE , &error_abort); +if (qemu_thread_create (&ms->rp_state.rp_thread,"return path" , + source_return_path_thread, ms, +QEMU_THREAD_JOINABLE , &local_err) <0 ) { +error_reportf_err (local_err, +"failed to create source_return_path_thread: " ); +qemu_fclose (ms->rp_state.from_dst_file); + ms->rp_state.from_dst_file =NULL ; +return -1 ; + }trace_open_return_path_on_source_continue ();
@@ -3201,9 +3210,13 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)migrate_fd_cleanup (s);return ; } -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, -QEMU_THREAD_JOINABLE , &error_abort); +if (qemu_thread_create (&s->thread,"live_migration" , migration_thread, s, +QEMU_THREAD_JOINABLE , &error_in) <0 ) { +error_reportf_err (error_in,"failed to create migration_thread: " ); +migrate_set_state (&s->state, s->state,MIGRATION_STATUS_FAILED ); +migrate_fd_cleanup (s); +return ; + } s->migration_thread_running = true; }
migration/postcopy-ram.c +12 -4
@@ -1083,6 +1083,8 @@ retry:int postcopy_ram_enable_notify (MigrationIncomingState *mis) { +Error *local_err =NULL ; +/* Open the fd for the kernel to give us userfaults */ mis->userfault_fd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK );if (mis->userfault_fd ==-1 ) {
@@ -1109,10 +1111,16 @@ int postcopy_ram_enable_notify(MigrationIncomingState *mis) }qemu_sem_init (&mis->fault_thread_sem,0 ); -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&mis->fault_thread,"postcopy/fault" , - postcopy_ram_fault_thread, mis, -QEMU_THREAD_JOINABLE , &error_abort); +if (qemu_thread_create (&mis->fault_thread,"postcopy/fault" , + postcopy_ram_fault_thread, mis, +QEMU_THREAD_JOINABLE , &local_err) <0 ) { +error_reportf_err (local_err, +"failed to create postcopy_ram_fault_thread: " ); +close (mis->userfault_event_fd); +close (mis->userfault_fd); +qemu_sem_destroy (&mis->fault_thread_sem); +return -1 ; + }qemu_sem_wait (&mis->fault_thread_sem);qemu_sem_destroy (&mis->fault_thread_sem); mis->have_fault_thread = true;
migration/ram.c +45 -25
125 lines not on this page · raw message
migration/savevm.c +8 -4
@@ -1747,10 +1747,14 @@ static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis) mis->have_listen_thread = true;/* Start up the listening thread and wait for it to signal ready */ qemu_sem_init (&mis->listen_thread_sem,0 ); -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&mis->listen_thread,"postcopy/listen" , - postcopy_ram_listen_thread,NULL , -QEMU_THREAD_DETACHED , &error_abort); +if (qemu_thread_create (&mis->listen_thread,"postcopy/listen" , + postcopy_ram_listen_thread,NULL , +QEMU_THREAD_DETACHED , &local_err) <0 ) { +error_reportf_err (local_err, +"failed to create postcopy_ram_listen_thread: " ); +qemu_sem_destroy (&mis->listen_thread_sem); +return -1 ; + }qemu_sem_wait (&mis->listen_thread_sem);qemu_sem_destroy (&mis->listen_thread_sem);
Signature · 1 line
2.17.2 (Apple Git-113)
PATCH 10/11 qemu_thread: supplement error handling for vnc_start_worker_thread
In reply to Fei LiFrom: Fei Li <[email protected]>
Supplement the error handling for vnc_thread_worker_thread: add an Error parameter for it to propagate the error to its caller to handle in case it fails, and make it return a Boolean to indicate whether it succeeds.
--- ui/vnc-jobs.c | 17 +++++++++++------ ui/vnc-jobs.h | 2 +- ui/vnc.c | 4 +++- 3 files changed, 15 insertions(+), 8 deletions(-)
ui/vnc-jobs.c +11 -6
@@ -332,16 +332,21 @@ static bool vnc_worker_thread_running(void)return queue;/* Check global queue */ } -void vnc_start_worker_thread (void ) +bool vnc_start_worker_thread (Error **errp) {VncJobQueue *q; -if (vnc_worker_thread_running ()) -return ; +if (vnc_worker_thread_running ()) { +return true; + } q =vnc_queue_init (); -/* TODO: let the further caller handle the error instead of abort() here */ -qemu_thread_create (&q->thread,"vnc_worker" , vnc_worker_thread, - q,QEMU_THREAD_DETACHED , &error_abort); +if (qemu_thread_create (&q->thread,"vnc_worker" , vnc_worker_thread, + q,QEMU_THREAD_DETACHED , errp) <0 ) { +vnc_queue_clear (q); +return false; + } queue = q;/* Set global queue */ + +return true; }
ui/vnc-jobs.h +1 -1
@@ -37,7 +37,7 @@ void vnc_job_push(VncJob *job);void vnc_jobs_join (VncState *vs);void vnc_jobs_consume_buffer (VncState *vs); -void vnc_start_worker_thread (void ); +bool vnc_start_worker_thread (Error **errp);/* Locks */ static inline int vnc_trylock_display (VncDisplay *vd)
ui/vnc.c +3 -1
@@ -3255,7 +3255,9 @@ void vnc_display_init(const char *id, Error **errp) vd->connections_limit =32 ;qemu_mutex_init (&vd->mutex); -vnc_start_worker_thread (); +if (!vnc_start_worker_thread (errp)) { +return ; + } vd->dcl.ops = &dcl_ops;register_displaychangelistener (&vd->dcl);
Signature · 1 line
2.17.2 (Apple Git-113)
PATCH 11/11 qemu_thread: supplement error handling for touch_all_pages
In reply to Fei LiFrom: Fei Li <[email protected]>
Supplement the error handling for touch_all_pages: add an Error parameter for it to propagate the error to its caller to do the handling in case it fails.
--- util/oslib-posix.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
util/oslib-posix.c +16 -10
@@ -435,7 +435,7 @@ static inline int get_memset_num_threads(int smp_cpus) }static bool touch_all_pages (char *area,size_t hpagesize,size_t numpages, -int smp_cpus) +int smp_cpus,Error **errp) {size_t numpages_per_thread;size_t size_per_thread;
@@ -452,20 +452,29 @@ static bool touch_all_pages(char *area, size_t hpagesize, size_t numpages, memset_thread[i].numpages = (i == (memset_num_threads -1 )) ? numpages : numpages_per_thread; memset_thread[i].hpagesize = hpagesize; -/* TODO: let the callers handle the error instead of abort() here */ -qemu_thread_create (&memset_thread[i].pgthread,"touch_pages" , - do_touch_pages, &memset_thread[i], -QEMU_THREAD_JOINABLE , &error_abort); +if (qemu_thread_create (&memset_thread[i].pgthread,"touch_pages" , + do_touch_pages, &memset_thread[i], +QEMU_THREAD_JOINABLE , errp) <0 ) { + memset_thread_failed = true; +break ; + } addr += size_per_thread; numpages -= numpages_per_thread; } + + memset_num_threads = i;for (i =0 ; i < memset_num_threads; i++) {qemu_thread_join (&memset_thread[i].pgthread); }g_free (memset_thread); memset_thread =NULL ; -return memset_thread_failed; +if (memset_thread_failed) { +error_append_hint (errp,"os_mem_prealloc: Insufficient free host " +"memory pages available to allocate guest RAM" ); +return false; + } +return true; } void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
@@ -488,10 +497,7 @@ void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, }/* touch pages simultaneously */ -if (touch_all_pages (area, hpagesize, numpages, smp_cpus)) { -error_setg (errp,"os_mem_prealloc: Insufficient free host memory " -"pages available to allocate guest RAM" ); - } +touch_all_pages (area, hpagesize, numpages, smp_cpus, errp); ret =sigaction (SIGBUS , &oldact,NULL );if (ret) {
Signature · 1 line
2.17.2 (Apple Git-113)