From nobody Sat Apr 27 01:22:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1581700861497622.8224272267275; Fri, 14 Feb 2020 09:21:01 -0800 (PST) Received: from localhost ([::1]:42098 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2eeC-0006Ed-8U for importer@patchew.org; Fri, 14 Feb 2020 12:21:00 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49107) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2eao-0000bn-Tx for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j2ean-0007FY-Rr for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:30 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:43050 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j2ean-0007Eq-Ns for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:29 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-329-ch3_E6IMPeOCiv8erYUMFg-1; Fri, 14 Feb 2020 12:17:27 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8DB0F107B273; Fri, 14 Feb 2020 17:17:26 +0000 (UTC) Received: from localhost (ovpn-116-135.ams2.redhat.com [10.36.116.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7527B5DA81; Fri, 14 Feb 2020 17:17:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581700649; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q9Ymt1qQx6vp3IXr8OqQ4TVPGWbrPBjiOimPtVWKsWE=; b=eG+cIERjQkA6qx+u1d20LV4U54rIwNW6sM+F0lUtQ3QouVYJCw47gnRy+X5AKrKkrYZ8p1 25gFeotpKZ32mpltbCjpSN7S/jvEM+fYd6Lo4/N3nmFUCN7aW3R7e4bBd9OwS+0QgvXa0c Wg29h2kyjya8REcgCDNUOzQnMkmpltg= From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 1/5] aio-posix: fix use after leaving scope in aio_poll() Date: Fri, 14 Feb 2020 17:17:08 +0000 Message-Id: <20200214171712.541358-2-stefanha@redhat.com> In-Reply-To: <20200214171712.541358-1-stefanha@redhat.com> References: <20200214171712.541358-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: ch3_E6IMPeOCiv8erYUMFg-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" epoll_handler is a stack variable and must not be accessed after it goes out of scope: if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) { AioHandler epoll_handler; ... add_pollfd(&epoll_handler); ret =3D aio_epoll(ctx, pollfds, npfd, timeout); } ... ... /* if we have any readable fds, dispatch event */ if (ret > 0) { for (i =3D 0; i < npfd; i++) { nodes[i]->pfd.revents =3D pollfds[i].revents; } } nodes[0] is &epoll_handler, which has already gone out of scope. There is no need to use pollfds[] for epoll. We don't need an AioHandler for the epoll fd. Signed-off-by: Stefan Hajnoczi Reviewed-by: Sergio Lopez --- util/aio-posix.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/util/aio-posix.c b/util/aio-posix.c index a4977f538e..31a8e03ca7 100644 --- a/util/aio-posix.c +++ b/util/aio-posix.c @@ -104,17 +104,18 @@ static void aio_epoll_update(AioContext *ctx, AioHand= ler *node, bool is_new) } } =20 -static int aio_epoll(AioContext *ctx, GPollFD *pfds, - unsigned npfd, int64_t timeout) +static int aio_epoll(AioContext *ctx, int64_t timeout) { + GPollFD pfd =3D { + .fd =3D ctx->epollfd, + .events =3D G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR, + }; AioHandler *node; int i, ret =3D 0; struct epoll_event events[128]; =20 - assert(npfd =3D=3D 1); - assert(pfds[0].fd =3D=3D ctx->epollfd); if (timeout > 0) { - ret =3D qemu_poll_ns(pfds, npfd, timeout); + ret =3D qemu_poll_ns(&pfd, 1, timeout); } if (timeout <=3D 0 || ret > 0) { ret =3D epoll_wait(ctx->epollfd, events, @@ -658,13 +659,8 @@ bool aio_poll(AioContext *ctx, bool blocking) =20 /* wait until next event */ if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) { - AioHandler epoll_handler; - - epoll_handler.pfd.fd =3D ctx->epollfd; - epoll_handler.pfd.events =3D G_IO_IN | G_IO_OUT | G_IO_HUP | G= _IO_ERR; - npfd =3D 0; - add_pollfd(&epoll_handler); - ret =3D aio_epoll(ctx, pollfds, npfd, timeout); + npfd =3D 0; /* pollfds[] is not being used */ + ret =3D aio_epoll(ctx, timeout); } else { ret =3D qemu_poll_ns(pollfds, npfd, timeout); } --=20 2.24.1 From nobody Sat Apr 27 01:22:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1581700737755331.5502021004554; Fri, 14 Feb 2020 09:18:57 -0800 (PST) Received: from localhost ([::1]:42046 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2ecC-0002b6-5m for importer@patchew.org; Fri, 14 Feb 2020 12:18:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49141) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2eax-0000rr-MT for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j2eav-0007Ob-HG for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:39 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:55807 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j2eav-0007O9-Dl for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:37 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-46-orqvC7QpOZ2PA2kZeH3HVw-1; Fri, 14 Feb 2020 12:17:35 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 83772DBA6; Fri, 14 Feb 2020 17:17:33 +0000 (UTC) Received: from localhost (ovpn-116-135.ams2.redhat.com [10.36.116.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id F049B5C126; Fri, 14 Feb 2020 17:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581700656; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TE3YZlGhEob/gLlLYAmhUGskynNyvdDHL/TV6S3PPic=; b=GFtgXRaWCFuKPn8CYz91JjgP8U+cXS48AsaHV0p7JYbUla0aUS5ezhwL71FpbbmT8Q+ol7 9Y7DFZuFTgmo1xw9NZRRvmdriDGOWNVOxjWmoOu7B2Sa/XaW1AKZ5PPMg25RuAbcdZ5VOo b98pkALm9JhZrVPNIInZM+W40ZW7xD8= From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 2/5] aio-posix: don't pass ns timeout to epoll_wait() Date: Fri, 14 Feb 2020 17:17:09 +0000 Message-Id: <20200214171712.541358-3-stefanha@redhat.com> In-Reply-To: <20200214171712.541358-1-stefanha@redhat.com> References: <20200214171712.541358-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-MC-Unique: orqvC7QpOZ2PA2kZeH3HVw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Don't pass the nanosecond timeout into epoll_wait(), which expects milliseconds. The epoll_wait() timeout value does not matter if qemu_poll_ns() determined that the poll fd is ready, but passing a value in the wrong units is still ugly. Pass a 0 timeout to epoll_wait() instead. Signed-off-by: Stefan Hajnoczi Reviewed-by: Sergio Lopez --- util/aio-posix.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/util/aio-posix.c b/util/aio-posix.c index 31a8e03ca7..b21bcd8e97 100644 --- a/util/aio-posix.c +++ b/util/aio-posix.c @@ -116,6 +116,9 @@ static int aio_epoll(AioContext *ctx, int64_t timeout) =20 if (timeout > 0) { ret =3D qemu_poll_ns(&pfd, 1, timeout); + if (ret > 0) { + timeout =3D 0; + } } if (timeout <=3D 0 || ret > 0) { ret =3D epoll_wait(ctx->epollfd, events, --=20 2.24.1 From nobody Sat Apr 27 01:22:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1581700925045447.6315995805048; Fri, 14 Feb 2020 09:22:05 -0800 (PST) Received: from localhost ([::1]:42108 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2efC-0007ZB-R0 for importer@patchew.org; Fri, 14 Feb 2020 12:22:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49181) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2eb8-0001At-E1 for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j2eb7-0007bq-C0 for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:50 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:52406 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j2eb7-0007bB-89 for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:49 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-430-vCCuurZsOGyua8N-nfDS6g-1; Fri, 14 Feb 2020 12:17:41 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7A613DBA9; Fri, 14 Feb 2020 17:17:40 +0000 (UTC) Received: from localhost (ovpn-116-135.ams2.redhat.com [10.36.116.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id B65AC1001DD8; Fri, 14 Feb 2020 17:17:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581700668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eASxJapIrJ7Wu9AyB2H0naFmKyv7JMS1os8B2nbIQDA=; b=P/sJXipqv9WO9lrznhw+SvtnYZwkPU8P87Xs0o2jf6kcpqFEu5VsXoUTqd3Gm1ob4OlDnc A5YqSf+YDGpIhWs0ldx27N+3dsHeN6Kk1xn01OO0NOHjV4bCmE/ZNaNScXwijjIDQVqD6x SBweZ4KqWldOYO691PU8CPsYq1m8UYA= From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 3/5] qemu/queue.h: add QLIST_SAFE_REMOVE() Date: Fri, 14 Feb 2020 17:17:10 +0000 Message-Id: <20200214171712.541358-4-stefanha@redhat.com> In-Reply-To: <20200214171712.541358-1-stefanha@redhat.com> References: <20200214171712.541358-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-MC-Unique: vCCuurZsOGyua8N-nfDS6g-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" QLIST_REMOVE() assumes the element is in a list. It also leaves the element's linked list pointers dangling. Introduce a safe version of QLIST_REMOVE() and convert open-coded instances of this pattern. Signed-off-by: Stefan Hajnoczi Reviewed-by: Sergio Lopez --- block.c | 5 +---- chardev/spice.c | 4 +--- include/qemu/queue.h | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/block.c b/block.c index 9c810534d6..484e01d042 100644 --- a/block.c +++ b/block.c @@ -2499,10 +2499,7 @@ BdrvChild *bdrv_attach_child(BlockDriverState *paren= t_bs, =20 static void bdrv_detach_child(BdrvChild *child) { - if (child->next.le_prev) { - QLIST_REMOVE(child, next); - child->next.le_prev =3D NULL; - } + QLIST_SAFE_REMOVE(child, next); =20 bdrv_replace_child(child, NULL); =20 diff --git a/chardev/spice.c b/chardev/spice.c index 241e2b7770..bf7ea1e294 100644 --- a/chardev/spice.c +++ b/chardev/spice.c @@ -216,9 +216,7 @@ static void char_spice_finalize(Object *obj) =20 vmc_unregister_interface(s); =20 - if (s->next.le_prev) { - QLIST_REMOVE(s, next); - } + QLIST_SAFE_REMOVE(s, next); =20 g_free((char *)s->sin.subtype); g_free((char *)s->sin.portname); diff --git a/include/qemu/queue.h b/include/qemu/queue.h index 19425f973f..a276363372 100644 --- a/include/qemu/queue.h +++ b/include/qemu/queue.h @@ -144,6 +144,20 @@ struct { = \ *(elm)->field.le_prev =3D (elm)->field.le_next; \ } while (/*CONSTCOND*/0) =20 +/* + * Like QLIST_REMOVE() but safe to call when elm is not in a list + */ +#define QLIST_SAFE_REMOVE(elm, field) do { \ + if ((elm)->field.le_prev !=3D NULL) { \ + if ((elm)->field.le_next !=3D NULL) \ + (elm)->field.le_next->field.le_prev =3D \ + (elm)->field.le_prev; \ + *(elm)->field.le_prev =3D (elm)->field.le_next; \ + (elm)->field.le_next =3D NULL; \ + (elm)->field.le_prev =3D NULL; \ + } \ +} while (/*CONSTCOND*/0) + #define QLIST_FOREACH(var, head, field) \ for ((var) =3D ((head)->lh_first); \ (var); \ --=20 2.24.1 From nobody Sat Apr 27 01:22:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1581700751035836.5495198877828; Fri, 14 Feb 2020 09:19:11 -0800 (PST) Received: from localhost ([::1]:42050 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2ecP-00036V-Mu for importer@patchew.org; Fri, 14 Feb 2020 12:19:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49269) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2ebM-0001Sc-OQ for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:18:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j2ebL-00084A-CO for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:18:04 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:23874 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j2ebL-00083Q-8w for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:18:03 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-58-e2kK7k8KNWqLJnbpetpojw-1; Fri, 14 Feb 2020 12:17:48 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 73B92189F760; Fri, 14 Feb 2020 17:17:47 +0000 (UTC) Received: from localhost (ovpn-116-135.ams2.redhat.com [10.36.116.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE3915C1C3; Fri, 14 Feb 2020 17:17:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581700682; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pEfGyIySH/diqfJDkGzBmpZrSWEyb9Lev6WK6Q4sL5A=; b=EhD9vZXx/GvM5rD6ulrUHpARYyd/K1fzJ8JPndrzIVh3RfucCnqTGgxnj3e3NBaYEcCe7+ 8fQEHVCux8cvruye65GgP/hxaNKqvuB2EyE4Ofe4blUn6e5p4VdxXDc5+nhNfLRoowBp1s IZYl01kIpWr3Wvw0WpQ1kyb9hAdY2rg= From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 4/5] aio-posix: make AioHandler deletion O(1) Date: Fri, 14 Feb 2020 17:17:11 +0000 Message-Id: <20200214171712.541358-5-stefanha@redhat.com> In-Reply-To: <20200214171712.541358-1-stefanha@redhat.com> References: <20200214171712.541358-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: e2kK7k8KNWqLJnbpetpojw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" It is not necessary to scan all AioHandlers for deletion. Keep a list of deleted handlers instead of scanning the full list of all handlers. The AioHandler->deleted field can be dropped. Let's check if the handler has been inserted into the deleted list instead. Add a new QLIST_IS_INSERTED() API for this check. Signed-off-by: Stefan Hajnoczi Reviewed-by: Sergio Lopez --- include/block/aio.h | 6 ++++- include/qemu/queue.h | 3 +++ util/aio-posix.c | 53 +++++++++++++++++++++++++++++--------------- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/include/block/aio.h b/include/block/aio.h index 7ba9bd7874..1a0de1508c 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -42,6 +42,7 @@ void qemu_aio_unref(void *p); void qemu_aio_ref(void *p); =20 typedef struct AioHandler AioHandler; +typedef QLIST_HEAD(, AioHandler) AioHandlerList; typedef void QEMUBHFunc(void *opaque); typedef bool AioPollFn(void *opaque); typedef void IOHandler(void *opaque); @@ -58,7 +59,10 @@ struct AioContext { QemuRecMutex lock; =20 /* The list of registered AIO handlers. Protected by ctx->list_lock. = */ - QLIST_HEAD(, AioHandler) aio_handlers; + AioHandlerList aio_handlers; + + /* The list of AIO handlers to be deleted. Protected by ctx->list_loc= k. */ + AioHandlerList deleted_aio_handlers; =20 /* Used to avoid unnecessary event_notifier_set calls in aio_notify; * accessed with atomic primitives. If this field is 0, everything diff --git a/include/qemu/queue.h b/include/qemu/queue.h index a276363372..699a8a0568 100644 --- a/include/qemu/queue.h +++ b/include/qemu/queue.h @@ -158,6 +158,9 @@ struct { = \ } \ } while (/*CONSTCOND*/0) =20 +/* Is elm in a list? */ +#define QLIST_IS_INSERTED(elm, field) ((elm)->field.le_prev !=3D NULL) + #define QLIST_FOREACH(var, head, field) \ for ((var) =3D ((head)->lh_first); \ (var); \ diff --git a/util/aio-posix.c b/util/aio-posix.c index b21bcd8e97..3a98a2acb9 100644 --- a/util/aio-posix.c +++ b/util/aio-posix.c @@ -31,10 +31,10 @@ struct AioHandler AioPollFn *io_poll; IOHandler *io_poll_begin; IOHandler *io_poll_end; - int deleted; void *opaque; bool is_external; QLIST_ENTRY(AioHandler) node; + QLIST_ENTRY(AioHandler) node_deleted; }; =20 #ifdef CONFIG_EPOLL_CREATE1 @@ -67,7 +67,7 @@ static bool aio_epoll_try_enable(AioContext *ctx) =20 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { int r; - if (node->deleted || !node->pfd.events) { + if (QLIST_IS_INSERTED(node, node_deleted) || !node->pfd.events) { continue; } event.events =3D epoll_events_from_pfd(node->pfd.events); @@ -195,9 +195,11 @@ static AioHandler *find_aio_handler(AioContext *ctx, i= nt fd) AioHandler *node; =20 QLIST_FOREACH(node, &ctx->aio_handlers, node) { - if (node->pfd.fd =3D=3D fd) - if (!node->deleted) + if (node->pfd.fd =3D=3D fd) { + if (!QLIST_IS_INSERTED(node, node_deleted)) { return node; + } + } } =20 return NULL; @@ -216,7 +218,7 @@ static bool aio_remove_fd_handler(AioContext *ctx, AioH= andler *node) =20 /* If a read is in progress, just mark the node as deleted */ if (qemu_lockcnt_count(&ctx->list_lock)) { - node->deleted =3D 1; + QLIST_INSERT_HEAD_RCU(&ctx->deleted_aio_handlers, node, node_delet= ed); node->pfd.revents =3D 0; return false; } @@ -358,7 +360,7 @@ static void poll_set_started(AioContext *ctx, bool star= ted) QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { IOHandler *fn; =20 - if (node->deleted) { + if (QLIST_IS_INSERTED(node, node_deleted)) { continue; } =20 @@ -415,6 +417,26 @@ bool aio_pending(AioContext *ctx) return result; } =20 +static void aio_free_deleted_handlers(AioContext *ctx) +{ + AioHandler *node; + + if (QLIST_EMPTY_RCU(&ctx->deleted_aio_handlers)) { + return; + } + if (!qemu_lockcnt_dec_if_lock(&ctx->list_lock)) { + return; /* we are nested, let the parent do the freeing */ + } + + while ((node =3D QLIST_FIRST_RCU(&ctx->deleted_aio_handlers))) { + QLIST_REMOVE(node, node); + QLIST_REMOVE(node, node_deleted); + g_free(node); + } + + qemu_lockcnt_inc_and_unlock(&ctx->list_lock); +} + static bool aio_dispatch_handlers(AioContext *ctx) { AioHandler *node, *tmp; @@ -426,7 +448,7 @@ static bool aio_dispatch_handlers(AioContext *ctx) revents =3D node->pfd.revents & node->pfd.events; node->pfd.revents =3D 0; =20 - if (!node->deleted && + if (!QLIST_IS_INSERTED(node, node_deleted) && (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && aio_node_check(ctx, node->is_external) && node->io_read) { @@ -437,21 +459,13 @@ static bool aio_dispatch_handlers(AioContext *ctx) progress =3D true; } } - if (!node->deleted && + if (!QLIST_IS_INSERTED(node, node_deleted) && (revents & (G_IO_OUT | G_IO_ERR)) && aio_node_check(ctx, node->is_external) && node->io_write) { node->io_write(node->opaque); progress =3D true; } - - if (node->deleted) { - if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) { - QLIST_REMOVE(node, node); - g_free(node); - qemu_lockcnt_inc_and_unlock(&ctx->list_lock); - } - } } =20 return progress; @@ -462,6 +476,7 @@ void aio_dispatch(AioContext *ctx) qemu_lockcnt_inc(&ctx->list_lock); aio_bh_poll(ctx); aio_dispatch_handlers(ctx); + aio_free_deleted_handlers(ctx); qemu_lockcnt_dec(&ctx->list_lock); =20 timerlistgroup_run_timers(&ctx->tlg); @@ -519,7 +534,7 @@ static bool run_poll_handlers_once(AioContext *ctx, int= 64_t *timeout) AioHandler *node; =20 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { - if (!node->deleted && node->io_poll && + if (!QLIST_IS_INSERTED(node, node_deleted) && node->io_poll && aio_node_check(ctx, node->is_external) && node->io_poll(node->opaque)) { /* @@ -653,7 +668,7 @@ bool aio_poll(AioContext *ctx, bool blocking) =20 if (!aio_epoll_enabled(ctx)) { QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { - if (!node->deleted && node->pfd.events + if (!QLIST_IS_INSERTED(node, node_deleted) && node->pfd.ev= ents && aio_node_check(ctx, node->is_external)) { add_pollfd(node); } @@ -730,6 +745,8 @@ bool aio_poll(AioContext *ctx, bool blocking) progress |=3D aio_dispatch_handlers(ctx); } =20 + aio_free_deleted_handlers(ctx); + qemu_lockcnt_dec(&ctx->list_lock); =20 progress |=3D timerlistgroup_run_timers(&ctx->tlg); --=20 2.24.1 From nobody Sat Apr 27 01:22:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 158170098571536.03585624861; Fri, 14 Feb 2020 09:23:05 -0800 (PST) Received: from localhost ([::1]:42120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2egB-0000sS-E9 for importer@patchew.org; Fri, 14 Feb 2020 12:23:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49225) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2ebH-0001HK-Me for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:18:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j2ebG-0007sf-44 for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:59 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:47155 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j2ebF-0007rG-Sy for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:17:58 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-394-s3250BkROFKRDPf6AzzoPQ-1; Fri, 14 Feb 2020 12:17:55 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6CB2E800D55; Fri, 14 Feb 2020 17:17:54 +0000 (UTC) Received: from localhost (ovpn-116-135.ams2.redhat.com [10.36.116.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id D57D05DA81; Fri, 14 Feb 2020 17:17:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581700677; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OnSEfRGmRliVSiKT2mcNbQ5S8FcvsiuOgfaoBZf3qzc=; b=IzPhanVW9+SL0OWDqJuEyYkL4JL6tBfXStu4noUD2otYip0rCkpTCakVvPZpuVj0kuKlhx VV9e2+OpexgVh94k0JLTn5DikbDa74rg6zCBLDWeC0KucVZlLy5ZLVvAJNTq2r8G6YbZHb 69hbYqFqUJ6yXf9Ymy7lBFEER1f/0KA= From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 5/5] aio-posix: make AioHandler dispatch O(1) with epoll Date: Fri, 14 Feb 2020 17:17:12 +0000 Message-Id: <20200214171712.541358-6-stefanha@redhat.com> In-Reply-To: <20200214171712.541358-1-stefanha@redhat.com> References: <20200214171712.541358-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: s3250BkROFKRDPf6AzzoPQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" File descriptor monitoring is O(1) with epoll(7), but aio_dispatch_handlers() still scans all AioHandlers instead of dispatching just those that are ready. This makes aio_poll() O(n) with respect to the total number of registered handlers. Add a local ready_list to aio_poll() so that each nested aio_poll() builds a list of handlers ready to be dispatched. Since file descriptor polling is level-triggered, nested aio_poll() calls also see fds that were ready in the parent but not yet dispatched. This guarantees that nested aio_poll() invocations will dispatch all fds, even those that became ready before the nested invocation. Since only handlers ready to be dispatched are placed onto the ready_list, the new aio_dispatch_ready_handlers() function provides O(1) dispatch. Note that AioContext polling is still O(n) and currently cannot be fully disabled. This still needs to be fixed before aio_poll() is fully O(1). Signed-off-by: Stefan Hajnoczi Reviewed-by: Sergio Lopez --- util/aio-posix.c | 106 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 30 deletions(-) diff --git a/util/aio-posix.c b/util/aio-posix.c index 3a98a2acb9..dc33ca08a6 100644 --- a/util/aio-posix.c +++ b/util/aio-posix.c @@ -34,6 +34,7 @@ struct AioHandler void *opaque; bool is_external; QLIST_ENTRY(AioHandler) node; + QLIST_ENTRY(AioHandler) node_ready; /* only used during aio_poll() */ QLIST_ENTRY(AioHandler) node_deleted; }; =20 @@ -104,7 +105,18 @@ static void aio_epoll_update(AioContext *ctx, AioHandl= er *node, bool is_new) } } =20 -static int aio_epoll(AioContext *ctx, int64_t timeout) +/* Add a handler to a ready list */ +static void add_ready_handler(AioHandlerList *ready_list, + AioHandler *node, + int revents) +{ + QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's li= st */ + node->pfd.revents =3D revents; + QLIST_INSERT_HEAD(ready_list, node, node_ready); +} + +static int aio_epoll(AioContext *ctx, AioHandlerList *ready_list, + int64_t timeout) { GPollFD pfd =3D { .fd =3D ctx->epollfd, @@ -129,11 +141,13 @@ static int aio_epoll(AioContext *ctx, int64_t timeout) } for (i =3D 0; i < ret; i++) { int ev =3D events[i].events; + int revents =3D (ev & EPOLLIN ? G_IO_IN : 0) | + (ev & EPOLLOUT ? G_IO_OUT : 0) | + (ev & EPOLLHUP ? G_IO_HUP : 0) | + (ev & EPOLLERR ? G_IO_ERR : 0); + node =3D events[i].data.ptr; - node->pfd.revents =3D (ev & EPOLLIN ? G_IO_IN : 0) | - (ev & EPOLLOUT ? G_IO_OUT : 0) | - (ev & EPOLLHUP ? G_IO_HUP : 0) | - (ev & EPOLLERR ? G_IO_ERR : 0); + add_ready_handler(ready_list, node, revents); } } out: @@ -437,36 +451,63 @@ static void aio_free_deleted_handlers(AioContext *ctx) qemu_lockcnt_inc_and_unlock(&ctx->list_lock); } =20 -static bool aio_dispatch_handlers(AioContext *ctx) +static bool aio_dispatch_handler(AioContext *ctx, AioHandler *node) { - AioHandler *node, *tmp; bool progress =3D false; + int revents; =20 - QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) { - int revents; + revents =3D node->pfd.revents & node->pfd.events; + node->pfd.revents =3D 0; =20 - revents =3D node->pfd.revents & node->pfd.events; - node->pfd.revents =3D 0; + if (!QLIST_IS_INSERTED(node, node_deleted) && + (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && + aio_node_check(ctx, node->is_external) && + node->io_read) { + node->io_read(node->opaque); =20 - if (!QLIST_IS_INSERTED(node, node_deleted) && - (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) && - aio_node_check(ctx, node->is_external) && - node->io_read) { - node->io_read(node->opaque); - - /* aio_notify() does not count as progress */ - if (node->opaque !=3D &ctx->notifier) { - progress =3D true; - } - } - if (!QLIST_IS_INSERTED(node, node_deleted) && - (revents & (G_IO_OUT | G_IO_ERR)) && - aio_node_check(ctx, node->is_external) && - node->io_write) { - node->io_write(node->opaque); + /* aio_notify() does not count as progress */ + if (node->opaque !=3D &ctx->notifier) { progress =3D true; } } + if (!QLIST_IS_INSERTED(node, node_deleted) && + (revents & (G_IO_OUT | G_IO_ERR)) && + aio_node_check(ctx, node->is_external) && + node->io_write) { + node->io_write(node->opaque); + progress =3D true; + } + + return progress; +} + +/* + * If we have a list of ready handlers then this is more efficient than + * scanning all handlers with aio_dispatch_handlers(). + */ +static bool aio_dispatch_ready_handlers(AioContext *ctx, + AioHandlerList *ready_list) +{ + bool progress =3D false; + AioHandler *node; + + while ((node =3D QLIST_FIRST(ready_list))) { + QLIST_SAFE_REMOVE(node, node_ready); + progress =3D aio_dispatch_handler(ctx, node) || progress; + } + + return progress; +} + +/* Slower than aio_dispatch_ready_handlers() but only used via glib */ +static bool aio_dispatch_handlers(AioContext *ctx) +{ + AioHandler *node, *tmp; + bool progress =3D false; + + QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) { + progress =3D aio_dispatch_handler(ctx, node) || progress; + } =20 return progress; } @@ -628,6 +669,7 @@ static bool try_poll_mode(AioContext *ctx, int64_t *tim= eout) =20 bool aio_poll(AioContext *ctx, bool blocking) { + AioHandlerList ready_list =3D QLIST_HEAD_INITIALIZER(ready_list); AioHandler *node; int i; int ret =3D 0; @@ -678,7 +720,7 @@ bool aio_poll(AioContext *ctx, bool blocking) /* wait until next event */ if (aio_epoll_check_poll(ctx, pollfds, npfd, timeout)) { npfd =3D 0; /* pollfds[] is not being used */ - ret =3D aio_epoll(ctx, timeout); + ret =3D aio_epoll(ctx, &ready_list, timeout); } else { ret =3D qemu_poll_ns(pollfds, npfd, timeout); } @@ -733,7 +775,11 @@ bool aio_poll(AioContext *ctx, bool blocking) /* if we have any readable fds, dispatch event */ if (ret > 0) { for (i =3D 0; i < npfd; i++) { - nodes[i]->pfd.revents =3D pollfds[i].revents; + int revents =3D pollfds[i].revents; + + if (revents) { + add_ready_handler(&ready_list, nodes[i], revents); + } } } =20 @@ -742,7 +788,7 @@ bool aio_poll(AioContext *ctx, bool blocking) progress |=3D aio_bh_poll(ctx); =20 if (ret > 0) { - progress |=3D aio_dispatch_handlers(ctx); + progress |=3D aio_dispatch_ready_handlers(ctx, &ready_list); } =20 aio_free_deleted_handlers(ctx); --=20 2.24.1