migration: fix ram decompression race deadlock
PATCH 1/1 migration: fix ram decompression race deadlock
From: Maxim Nestratov <[email protected]>
There is a race in between do_data_decompress and start_decompression.
do_data_decompress()
while (!quit_decomp_thread) {
qemu_mutex_lock(¶m->mutex);
while (!param->start && !quit_decomp_thread) {
qemu_cond_wait(¶m->cond, ¶m->mutex);
...
param->start = false;
}
qemu_mutex_unlock(¶m->mutex);
[ preempted here, start_decompression() is executed ]
}start_decompression()
{
qemu_mutex_lock(¶m->mutex);
param->start = true;
qemu_cond_signal(¶m->cond);
qemu_mutex_unlock(¶m->mutex);
}In this case do_data_decompress will never enter inner loop again and will eat 100% CPU. The patch fixes this problem by correcting while loop where we wait for condition only and other actions are moved out of it.
--- migration/ram.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
migration/ram.c +11 -11
@@ -2193,18 +2193,18 @@ static void *do_data_decompress(void *opaque)qemu_mutex_lock (¶m->mutex);while (!param->start && !quit_decomp_thread) {qemu_cond_wait (¶m->cond, ¶m->mutex); - pagesize =TARGET_PAGE_SIZE ; -if (!quit_decomp_thread) { -/* uncompress() will return failed in some case, especially -* when the page is dirted when doing the compression, it's -* not a problem because the dirty page will be retransferred -* and uncompress() won't break the data in other pages. -*/ -uncompress ((Bytef *)param->des, &pagesize, - (const Bytef *)param->compbuf, param->len); - } - param->start = false; } + pagesize =TARGET_PAGE_SIZE ; +if (!quit_decomp_thread) { +/* uncompress() will return failed in some case, especially +* when the page is dirted when doing the compression, it's +* not a problem because the dirty page will be retransferred +* and uncompress() won't break the data in other pages. +*/ +uncompress ((Bytef *)param->des, &pagesize, + (const Bytef *)param->compbuf, param->len); + } + param->start = false;qemu_mutex_unlock (¶m->mutex); }
Signature · 1 line
2.1.4
Quoted 60 lines · Denis V. Lunev
From: Maxim Nestratov <[email protected]>
There is a race in between
do_data_decompressandstart_decompression.
do_data_decompress()while (!quit_decomp_thread) { qemu_mutex_lock(¶m->mutex); while (!param->start && !quit_decomp_thread) { qemu_cond_wait(¶m->cond, ¶m->mutex); ... param->start = false; } qemu_mutex_unlock(¶m->mutex); [ preempted here, start_decompression() is executed ] }
start_decompression(){ qemu_mutex_lock(¶m->mutex); param->start = true; qemu_cond_signal(¶m->cond); qemu_mutex_unlock(¶m->mutex); }In this case
do_data_decompresswill never enter inner loop again and will eat 100% CPU. The patch fixes this problem by correcting while loop where we wait for condition only and other actions are moved out of it.Signed-off-by Maxim Nestratov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Juan Quintela <[email protected]>CC Amit Shah <[email protected]>--- migration/ram.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)migration/ram.c +4
-11@@ -2193,18 +2193,18 @@ static void *do_data_decompress(void *opaque)qemu_mutex_lock (¶m->mutex);while (!param->start && !quit_decomp_thread) {qemu_cond_wait (¶m->cond, ¶m->mutex); - pagesize =TARGET_PAGE_SIZE ; -if (!quit_decomp_thread) { -/* uncompress() will return failed in some case, especially -* when the page is dirted when doing the compression, it's -* not a problem because the dirty page will be retransferred -* and uncompress() won't break the data in other pages. -*/ -uncompress ((Bytef *)param->des, &pagesize, - (const Bytef *)param->compbuf, param->len); - } - param->start = false; } + pagesize =TARGET_PAGE_SIZE ; +if (!quit_decomp_thread) { +/* uncompress() will return failed in some case, especially +* when the page is dirted when doing the compression, it's
migration/ram.c static void *do_data_decompress(void *opaque) · line 2210
Quoted 10 lines
migration/ram.c +7
-0+* not a problem because the dirty page will be retransferred +* and uncompress() won't break the data in other pages. +*/ +uncompress ((Bytef *)param->des, &pagesize, + (const Bytef *)param->compbuf, param->len); + } + param->start = false;qemu_mutex_unlock (¶m->mutex); }
ping
Adding Liang Li to CC for his comments as the author of the feature.
Quoted 73 lines · Denis V. Lunev
From: Maxim Nestratov <[email protected]>
There is a race in between
do_data_decompressandstart_decompression.
do_data_decompress()while (!quit_decomp_thread) { qemu_mutex_lock(¶m->mutex); while (!param->start && !quit_decomp_thread) { qemu_cond_wait(¶m->cond, ¶m->mutex); ... param->start = false; } qemu_mutex_unlock(¶m->mutex); [ preempted here, start_decompression() is executed ] }
start_decompression(){ qemu_mutex_lock(¶m->mutex); param->start = true; qemu_cond_signal(¶m->cond); qemu_mutex_unlock(¶m->mutex); }In this case
do_data_decompresswill never enter inner loop again and will eat 100% CPU. The patch fixes this problem by correcting while loop where we wait for condition only and other actions are moved out of it.Signed-off-by Maxim Nestratov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Juan Quintela <[email protected]>CC Amit Shah <[email protected]>--- migration/ram.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)migration/ram.c +11
-11@@ -2193,18 +2193,18 @@ static void *do_data_decompress(void *opaque)qemu_mutex_lock (¶m->mutex);while (!param->start && !quit_decomp_thread) {qemu_cond_wait (¶m->cond, ¶m->mutex); - pagesize =TARGET_PAGE_SIZE ; -if (!quit_decomp_thread) { -/* uncompress() will return failed in some case, especially -* when the page is dirted when doing the compression, it's -* not a problem because the dirty page will be retransferred -* and uncompress() won't break the data in other pages. -*/ -uncompress ((Bytef *)param->des, &pagesize, - (const Bytef *)param->compbuf, param->len); - } - param->start = false; } + pagesize =TARGET_PAGE_SIZE ; +if (!quit_decomp_thread) { +/* uncompress() will return failed in some case, especially +* when the page is dirted when doing the compression, it's +* not a problem because the dirty page will be retransferred +* and uncompress() won't break the data in other pages. +*/ +uncompress ((Bytef *)param->des, &pagesize, + (const Bytef *)param->compbuf, param->len); + } + param->start = false;qemu_mutex_unlock (¶m->mutex); }Signature · 1 line
2.1.4
Amit
Quoted 40 lines
Adding Liang Li to CC for his comments as the author of the feature.
Quoted 24 lines · Denis V. Lunev
From: Maxim Nestratov <[email protected]>
There is a race in between
do_data_decompressandstart_decompression.
do_data_decompress()while (!quit_decomp_thread) { qemu_mutex_lock(¶m->mutex); while (!param->start && !quit_decomp_thread) { qemu_cond_wait(¶m->cond, ¶m->mutex); ... param->start = false; } qemu_mutex_unlock(¶m->mutex); [ preempted here, start_decompression() is executed ] }
start_decompression(){ qemu_mutex_lock(¶m->mutex); param->start = true; qemu_cond_signal(¶m->cond); qemu_mutex_unlock(¶m->mutex); }Quoted 3 lines
In this case
do_data_decompresswill never enter inner loop again and will eat 100% CPU. The patch fixes this problem by correcting while loop where we wait for condition only and other actions are moved out ofit.
Quoted 9 lines
Signed-off-by Maxim Nestratov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Juan Quintela <[email protected]>CC Amit Shah <[email protected]>--- migration/ram.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
Quoted 34 lines
migration/ram.c static void *do_data_decompress(void
Quoted 5 lines
migration/ram.c index 3f05738..579bfc0 renamed from migration/ram.c +0
-0100644
migration/ram.c +0
-0@@ -2193,18 +2193,18 @@ static void *do_data_decompress(void*opaque)
Quoted 28 lines
migration/ram.c +11
-11qemu_mutex_lock (¶m->mutex);while (!param->start && !quit_decomp_thread) {qemu_cond_wait (¶m->cond, ¶m->mutex); - pagesize =TARGET_PAGE_SIZE ; -if (!quit_decomp_thread) { -/* uncompress() will return failed in some case, especially -* when the page is dirted when doing the compression, it's -* not a problem because the dirty page will be retransferred -* and uncompress() won't break the data in other pages. -*/ -uncompress ((Bytef *)param->des, &pagesize, - (const Bytef *)param->compbuf, param->len); - } - param->start = false; } + pagesize =TARGET_PAGE_SIZE ; +if (!quit_decomp_thread) { +/* uncompress() will return failed in some case, especially +* when the page is dirted when doing the compression, it's +* not a problem because the dirty page will be retransferred +* and uncompress() won't break the data in other pages. +*/ +uncompress ((Bytef *)param->des, &pagesize, + (const Bytef *)param->compbuf, param->len); + } + param->start = false;qemu_mutex_unlock (¶m->mutex); }
Thanks for your patch! And it can help to fix this issue.
Actually, the is not the only issue in the current multi-thread (de)compression code. So I submitted a path set to fix all of them before your patch. And sorry for my mistake.
Refer to the link:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg00631.html
for more information.
Thanks
Liang
Quoted 77 lines · Liang Z
Quoted 39 lines
Adding Liang Li to CC for his comments as the author of the feature.
Quoted 24 lines · Denis V. Lunev
From: Maxim Nestratov <[email protected]>
There is a race in between
do_data_decompressandstart_decompression.
do_data_decompress()while (!quit_decomp_thread) { qemu_mutex_lock(¶m->mutex); while (!param->start && !quit_decomp_thread) { qemu_cond_wait(¶m->cond, ¶m->mutex); ... param->start = false; } qemu_mutex_unlock(¶m->mutex); [ preempted here, start_decompression() is executed ] }
start_decompression(){ qemu_mutex_lock(¶m->mutex); param->start = true; qemu_cond_signal(¶m->cond); qemu_mutex_unlock(¶m->mutex); }Quoted 3 lines
In this case
do_data_decompresswill never enter inner loop again and will eat 100% CPU. The patch fixes this problem by correcting while loop where we wait for condition only and other actions are moved out ofit.
Quoted 8 lines
Signed-off-by Maxim Nestratov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Juan Quintela <[email protected]>CC Amit Shah <[email protected]>--- migration/ram.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)Quoted 34 lines
migration/ram.c static void *do_data_decompress(void
Quoted 5 lines
migration/ram.c index 3f05738..579bfc0 renamed from migration/ram.c +0
-0100644
migration/ram.c +0
-0@@ -2193,18 +2193,18 @@ static void *do_data_decompress(void*opaque)
Quoted 28 lines
migration/ram.c +11
-11qemu_mutex_lock (¶m->mutex);while (!param->start && !quit_decomp_thread) {qemu_cond_wait (¶m->cond, ¶m->mutex); - pagesize =TARGET_PAGE_SIZE ; -if (!quit_decomp_thread) { -/* uncompress() will return failed in some case, especially -* when the page is dirted when doing the compression, it's -* not a problem because the dirty page will be retransferred -* and uncompress() won't break the data in other pages. -*/ -uncompress ((Bytef *)param->des, &pagesize, - (const Bytef *)param->compbuf, param->len); - } - param->start = false; } + pagesize =TARGET_PAGE_SIZE ; +if (!quit_decomp_thread) { +/* uncompress() will return failed in some case, especially +* when the page is dirted when doing the compression, it's +* not a problem because the dirty page will be retransferred +* and uncompress() won't break the data in other pages. +*/ +uncompress ((Bytef *)param->des, &pagesize, + (const Bytef *)param->compbuf, param->len); + } + param->start = false;qemu_mutex_unlock (¶m->mutex); }Thanks for your patch! And it can help to fix this issue.
Actually, the is not the only issue in the current multi-thread (de)compression code. So I submitted a path set to fix all of them before your patch. And sorry for my mistake.
Quoted 6 lines
Refer to the link:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg00631.html
for more information.Thanks
Liang
yep. thanks a lot. Your patch would be better ;)
Quoted 6 lines · Denis V. Lunev
Quoted 4 lines · Liang Z
Thanks for your patch! And it can help to fix this issue.
Actually, the is not the only issue in the current multi-thread (de)compression code. So I submitted a path set to fix all of them before your patch. And sorry for my mistake.
Quoted 7 lines
Quoted 6 lines
Refer to the link:
https://lists.gnu.org/archive/html/qemu-devel/2016-05/msg00631.html
for more information.Thanks
Liangyep. thanks a lot. Your patch would be better ;)
OK, I'll drop this patch.
Amit