diff mbox series

[kvmtool,7/9] disk/aio: Cancel AIO thread on cleanup

Message ID 20190218130702.32575-8-jean-philippe.brucker@arm.com (mailing list archive)
State New, archived
Headers show
Series Disk fixes and AIO reset | expand

Commit Message

Jean-Philippe Brucker Feb. 18, 2019, 1:07 p.m. UTC
If the AIO thread is still calling io_getevents() while the exit path
calls io_destroy(), it will segfault. Wait for the thread to finish before
destroying the context.

Signed-off-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
---
 disk/aio.c               | 5 +++--
 include/kvm/disk-image.h | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)

Comments

Andre Przywara Feb. 22, 2019, 4:38 p.m. UTC | #1
On Mon, 18 Feb 2019 13:07:00 +0000
Jean-Philippe Brucker <jean-philippe.brucker@arm.com> wrote:

> If the AIO thread is still calling io_getevents() while the exit path
> calls io_destroy(), it will segfault. Wait for the thread to finish before
> destroying the context.
> 
> Signed-off-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>

Reviewed-by: Andre Przywara <andre.przywara@arm.com>

Cheers,
Andre.

> ---
>  disk/aio.c               | 5 +++--
>  include/kvm/disk-image.h | 1 +
>  2 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/disk/aio.c b/disk/aio.c
> index 1fcf36857..277ddf7c9 100644
> --- a/disk/aio.c
> +++ b/disk/aio.c
> @@ -99,7 +99,6 @@ static void *disk_aio_thread(void *param)
>  int disk_aio_setup(struct disk_image *disk)
>  {
>  	int r;
> -	pthread_t thread;
>  
>  	/* No need to setup AIO if the disk ops won't make use of it */
>  	if (!disk->ops->async)
> @@ -110,7 +109,7 @@ int disk_aio_setup(struct disk_image *disk)
>  		return -errno;
>  
>  	io_setup(AIO_MAX, &disk->ctx);
> -	r = pthread_create(&thread, NULL, disk_aio_thread, disk);
> +	r = pthread_create(&disk->thread, NULL, disk_aio_thread, disk);
>  	if (r) {
>  		r = -errno;
>  		close(disk->evt);
> @@ -126,6 +125,8 @@ void disk_aio_destroy(struct disk_image *disk)
>  	if (!disk->async)
>  		return;
>  
> +	pthread_cancel(disk->thread);
> +	pthread_join(disk->thread, NULL);
>  	close(disk->evt);
>  	io_destroy(disk->ctx);
>  }
> diff --git a/include/kvm/disk-image.h b/include/kvm/disk-image.h
> index adc9fe465..2275e2343 100644
> --- a/include/kvm/disk-image.h
> +++ b/include/kvm/disk-image.h
> @@ -69,6 +69,7 @@ struct disk_image {
>  #ifdef CONFIG_HAS_AIO
>  	io_context_t			ctx;
>  	int				evt;
> +	pthread_t			thread;
>  #endif /* CONFIG_HAS_AIO */
>  	const char			*wwpn;
>  	const char			*tpgt;
diff mbox series

Patch

diff --git a/disk/aio.c b/disk/aio.c
index 1fcf36857..277ddf7c9 100644
--- a/disk/aio.c
+++ b/disk/aio.c
@@ -99,7 +99,6 @@  static void *disk_aio_thread(void *param)
 int disk_aio_setup(struct disk_image *disk)
 {
 	int r;
-	pthread_t thread;
 
 	/* No need to setup AIO if the disk ops won't make use of it */
 	if (!disk->ops->async)
@@ -110,7 +109,7 @@  int disk_aio_setup(struct disk_image *disk)
 		return -errno;
 
 	io_setup(AIO_MAX, &disk->ctx);
-	r = pthread_create(&thread, NULL, disk_aio_thread, disk);
+	r = pthread_create(&disk->thread, NULL, disk_aio_thread, disk);
 	if (r) {
 		r = -errno;
 		close(disk->evt);
@@ -126,6 +125,8 @@  void disk_aio_destroy(struct disk_image *disk)
 	if (!disk->async)
 		return;
 
+	pthread_cancel(disk->thread);
+	pthread_join(disk->thread, NULL);
 	close(disk->evt);
 	io_destroy(disk->ctx);
 }
diff --git a/include/kvm/disk-image.h b/include/kvm/disk-image.h
index adc9fe465..2275e2343 100644
--- a/include/kvm/disk-image.h
+++ b/include/kvm/disk-image.h
@@ -69,6 +69,7 @@  struct disk_image {
 #ifdef CONFIG_HAS_AIO
 	io_context_t			ctx;
 	int				evt;
+	pthread_t			thread;
 #endif /* CONFIG_HAS_AIO */
 	const char			*wwpn;
 	const char			*tpgt;