Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KvxjX-0001BB-Ck for qemu-devel@nongnu.org; Fri, 31 Oct 2008 13:24:27 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KvxjV-0001Ak-Dj for qemu-devel@nongnu.org; Fri, 31 Oct 2008 13:24:26 -0400 Received: from [199.232.76.173] (port=57521 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KvxjV-0001Ah-8Q for qemu-devel@nongnu.org; Fri, 31 Oct 2008 13:24:25 -0400 Received: from savannah.gnu.org ([199.232.41.3]:46471 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KvxjU-0002Sn-TP for qemu-devel@nongnu.org; Fri, 31 Oct 2008 13:24:25 -0400 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.63) (envelope-from ) id 1KvxjT-0003lk-1a for qemu-devel@nongnu.org; Fri, 31 Oct 2008 17:24:23 +0000 Received: from aliguori by cvs.savannah.gnu.org with local (Exim 4.63) (envelope-from ) id 1KvxjS-0003lg-Av for qemu-devel@nongnu.org; Fri, 31 Oct 2008 17:24:22 +0000 MIME-Version: 1.0 Errors-To: aliguori Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Anthony Liguori Message-Id: Date: Fri, 31 Oct 2008 17:24:22 +0000 Subject: [Qemu-devel] [5572] Make bottom halves more robust Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Revision: 5572 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5572 Author: aliguori Date: 2008-10-31 17:24:21 +0000 (Fri, 31 Oct 2008) Log Message: ----------- Make bottom halves more robust Bottom halves are supposed to not complete until the next iteration of the main loop. This is very important to ensure that guests can not cause stack overflows in the block driver code. Right now, if you attempt to schedule a bottom half within a bottom half callback, you will enter an infinite loop. This patch uses the same logic that we use for the IOHandler loop to make the bottom half processing robust in list manipulation while in a callback. This patch also introduces idle scheduling for bottom halves. qemu_bh_poll() returns an indication of whether any bottom halves were successfully executed. qemu_aio_wait() uses this to immediately return if a bottom half was executed instead of waiting for a completion notification. qemu_bh_schedule_idle() works around this by not reporting the callback has run in the qemu_bh_poll loop. qemu_aio_wait() probably needs some refactoring but that would require a larger code audit. idle scheduling seems like a good compromise. Signed-off-by: Anthony Liguori Modified Paths: -------------- trunk/qemu-common.h trunk/vl.c Modified: trunk/qemu-common.h =================================================================== --- trunk/qemu-common.h 2008-10-29 14:37:18 UTC (rev 5571) +++ trunk/qemu-common.h 2008-10-31 17:24:21 UTC (rev 5572) @@ -70,6 +70,7 @@ QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque); void qemu_bh_schedule(QEMUBH *bh); +void qemu_bh_schedule_idle(QEMUBH *bh); void qemu_bh_cancel(QEMUBH *bh); void qemu_bh_delete(QEMUBH *bh); int qemu_bh_poll(void); Modified: trunk/vl.c =================================================================== --- trunk/vl.c 2008-10-29 14:37:18 UTC (rev 5571) +++ trunk/vl.c 2008-10-31 17:24:21 UTC (rev 5572) @@ -7578,6 +7578,8 @@ QEMUBHFunc *cb; void *opaque; int scheduled; + int idle; + int deleted; QEMUBH *next; }; @@ -7591,37 +7593,56 @@ return NULL; bh->cb = cb; bh->opaque = opaque; + bh->next = first_bh; + first_bh = bh; return bh; } int qemu_bh_poll(void) { - QEMUBH *bh, **pbh; + QEMUBH *bh, **bhp; int ret; ret = 0; - for(;;) { - pbh = &first_bh; - bh = *pbh; - if (!bh) - break; - ret = 1; - *pbh = bh->next; - bh->scheduled = 0; - bh->cb(bh->opaque); + for (bh = first_bh; bh; bh = bh->next) { + if (!bh->deleted && bh->scheduled) { + bh->scheduled = 0; + if (!bh->idle) + ret = 1; + bh->idle = 0; + bh->cb(bh->opaque); + } } + + /* remove deleted bhs */ + bhp = &first_bh; + while (*bhp) { + bh = *bhp; + if (bh->deleted) { + *bhp = bh->next; + qemu_free(bh); + } else + bhp = &bh->next; + } + return ret; } +void qemu_bh_schedule_idle(QEMUBH *bh) +{ + if (bh->scheduled) + return; + bh->scheduled = 1; + bh->idle = 1; +} + void qemu_bh_schedule(QEMUBH *bh) { CPUState *env = cpu_single_env; if (bh->scheduled) return; bh->scheduled = 1; - bh->next = first_bh; - first_bh = bh; - + bh->idle = 0; /* stop the currently executing CPU to execute the BH ASAP */ if (env) { cpu_interrupt(env, CPU_INTERRUPT_EXIT); @@ -7630,20 +7651,13 @@ void qemu_bh_cancel(QEMUBH *bh) { - QEMUBH **pbh; - if (bh->scheduled) { - pbh = &first_bh; - while (*pbh != bh) - pbh = &(*pbh)->next; - *pbh = bh->next; - bh->scheduled = 0; - } + bh->scheduled = 0; } void qemu_bh_delete(QEMUBH *bh) { - qemu_bh_cancel(bh); - qemu_free(bh); + bh->scheduled = 0; + bh->deleted = 1; } /***********************************************************/