From nobody Sat May 4 21:23:35 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1529619953275826.5989845598677; Thu, 21 Jun 2018 15:25:53 -0700 (PDT) Received: from localhost ([::1]:57828 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fW81P-0006ws-4g for importer@patchew.org; Thu, 21 Jun 2018 18:25:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52866) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fW7xs-00056G-Df for qemu-devel@nongnu.org; Thu, 21 Jun 2018 18:22:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fW7xq-0008IR-45 for qemu-devel@nongnu.org; Thu, 21 Jun 2018 18:22:04 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:44473) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fW7xp-0008Hi-Rn for qemu-devel@nongnu.org; Thu, 21 Jun 2018 18:22:02 -0400 Received: by mail-pf0-x242.google.com with SMTP id h12-v6so2186850pfk.11 for ; Thu, 21 Jun 2018 15:22:01 -0700 (PDT) Received: from breakout.internal.digitalocean.com (97-120-190-204.ptld.qwest.net. [97.120.190.204]) by smtp.gmail.com with ESMTPSA id d15-v6sm8555012pgu.66.2018.06.21.15.22.00 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Jun 2018 15:22:00 -0700 (PDT) Received: by breakout.internal.digitalocean.com (Postfix, from userid 1000) id 8408B8A2A52; Thu, 21 Jun 2018 15:21:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digitalocean.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xSx53rQZDAmmsvnMwOJI/p3nAqoOaM+qaoKLVVAklwE=; b=PVNzogsvMSiPJb7hxDp443WC9EkASwszT0nTo6q9CaCIbS2E472wWGgcLilae7CWax y2Y1RQQCNKiYR1xSvJkuzdA83vdNUJ9/L33N4vRlJmExMFN18jd7G3JVoLeT/1mBPQC8 caHhpj3pw81nrpNOqhl8km1aadbkPLqwuz7tQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xSx53rQZDAmmsvnMwOJI/p3nAqoOaM+qaoKLVVAklwE=; b=nR7P7JiqqX3beTKy1ojcXOgvLQVuUs9yB3kyIY9AQGEsqMiieK++TX6OKyPctE+dZv pUKnRYtcrNDzRfR8Rxr2cBrVJWlaIe772J62cTc9yc7nP+5LFMDqvTckDZ250nCKs3g0 6TLaNFzVEAatSxJEmKOow8qZXerFVp0TdEi//m1w7Da4lJT+FSBCOLnf8s+N3941k5Gv oAyKoG1k2D6DZ6kNZAT+0KL4bhyUzo8wI7ql4s/0YZKKvwbwO5a78waZRyXtYxyBKS9D snzS5t7BhgFjWTeWwZ7LklsqUjNUggLUhEUh/F3hZ/2dMkeyOUf3K5h58aC/Ng+W7XXx Lolg== X-Gm-Message-State: APt69E3Wt2LZkPJoRLFmc8f1BtzQmZMekg4P/3oqn3XAH9qQJ0odtify e+X9kkn9QAUtObU6syJa+xxU0w== X-Google-Smtp-Source: ADUXVKKCwio1uwG8pdGxQZBYQRNguNERikG1rWXZtwcERT7f5Uj3GAQZRHguOxga6CVh5GNZT9+3zg== X-Received: by 2002:a63:9a52:: with SMTP id e18-v6mr23951963pgo.188.1529619721001; Thu, 21 Jun 2018 15:22:01 -0700 (PDT) To: naravamudan@digitalocean.com Date: Thu, 21 Jun 2018 15:21:42 -0700 Message-Id: <20180621222143.27266-2-naravamudan@digitalocean.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180621222143.27266-1-naravamudan@digitalocean.com> References: <20180621222143.27266-1-naravamudan@digitalocean.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v3 1/2] linux-aio: properly bubble up errors from initialization X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Nishanth Aravamudan via Qemu-devel Reply-To: Nishanth Aravamudan Cc: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" laio_init() can fail for a couple of reasons, which will lead to a NULL pointer dereference in laio_attach_aio_context(). To solve this, add a aio_setup_linux_aio() function which is called early in raw_open_common. If this fails, propagate the error up. The signature of aio_get_linux_aio() was not modified, because it seems preferable to return the actual errno from the possible failing initialization calls. Add an assert that aio_get_linux_aio() cannot return NULL. Signed-off-by: Nishanth Aravamudan --- Changes from v2 -> v3 (thanks to Eric Blake and Kevin Wolf for review): Use a boolean false rather than 0 in assignment to use_linux_aio. Drop ending '.' from error_report() calls. Fix typo in commit message (propogates -> propagates). Move aio_setup_linux_aio call to raw_open_common. Changes from v1 -> v2 (thanks to Kevin Wolf for review): Rather than affect virtio-scsi/blk at all, make all the changes internal to file-posix.c. Thanks to Kevin Wolf for the suggested change. block/file-posix.c | 17 ++++++++++++----- block/linux-aio.c | 15 ++++++++++----- include/block/aio.h | 3 +++ include/block/raw-aio.h | 2 +- stubs/linux-aio.c | 2 +- util/async.c | 16 +++++++++++++--- 6 files changed, 40 insertions(+), 15 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 07bb061fe4..6a1714d4a8 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -545,11 +545,18 @@ static int raw_open_common(BlockDriverState *bs, QDic= t *options, =20 #ifdef CONFIG_LINUX_AIO /* Currently Linux does AIO only for files opened with O_DIRECT */ - if (s->use_linux_aio && !(s->open_flags & O_DIRECT)) { - error_setg(errp, "aio=3Dnative was specified, but it requires " - "cache.direct=3Don, which was not specified."); - ret =3D -EINVAL; - goto fail; + if (s->use_linux_aio) { + if (!(s->open_flags & O_DIRECT)) { + error_setg(errp, "aio=3Dnative was specified, but it requires " + "cache.direct=3Don, which was not specified."= ); + ret =3D -EINVAL; + goto fail; + } + ret =3D aio_setup_linux_aio(bdrv_get_aio_context(bs)); + if (ret !=3D 0) { + error_setg(errp, "Unable to setup native AIO context."); + goto fail; + } } #else if (s->use_linux_aio) { diff --git a/block/linux-aio.c b/block/linux-aio.c index 88b8d55ec7..4d799f85fe 100644 --- a/block/linux-aio.c +++ b/block/linux-aio.c @@ -470,28 +470,33 @@ void laio_attach_aio_context(LinuxAioState *s, AioCon= text *new_context) qemu_laio_poll_cb); } =20 -LinuxAioState *laio_init(void) +int laio_init(LinuxAioState **linux_aio) { + int rc; LinuxAioState *s; =20 s =3D g_malloc0(sizeof(*s)); - if (event_notifier_init(&s->e, false) < 0) { + rc =3D event_notifier_init(&s->e, false); + if (rc < 0) { goto out_free_state; } =20 - if (io_setup(MAX_EVENTS, &s->ctx) !=3D 0) { + rc =3D io_setup(MAX_EVENTS, &s->ctx); + if (rc !=3D 0) { goto out_close_efd; } =20 ioq_init(&s->io_q); =20 - return s; + *linux_aio =3D s; + return 0; =20 out_close_efd: event_notifier_cleanup(&s->e); out_free_state: g_free(s); - return NULL; + *linux_aio =3D NULL; + return rc; } =20 void laio_cleanup(LinuxAioState *s) diff --git a/include/block/aio.h b/include/block/aio.h index ae6f354e6c..8900516ac5 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -381,6 +381,9 @@ GSource *aio_get_g_source(AioContext *ctx); /* Return the ThreadPool bound to this AioContext */ struct ThreadPool *aio_get_thread_pool(AioContext *ctx); =20 +/* Setup the LinuxAioState bound to this AioContext */ +int aio_setup_linux_aio(AioContext *ctx); + /* Return the LinuxAioState bound to this AioContext */ struct LinuxAioState *aio_get_linux_aio(AioContext *ctx); =20 diff --git a/include/block/raw-aio.h b/include/block/raw-aio.h index 0e717fd475..81b90e5fc6 100644 --- a/include/block/raw-aio.h +++ b/include/block/raw-aio.h @@ -43,7 +43,7 @@ /* linux-aio.c - Linux native implementation */ #ifdef CONFIG_LINUX_AIO typedef struct LinuxAioState LinuxAioState; -LinuxAioState *laio_init(void); +int laio_init(LinuxAioState **linux_aio); void laio_cleanup(LinuxAioState *s); int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, in= t fd, uint64_t offset, QEMUIOVector *qiov, int t= ype); diff --git a/stubs/linux-aio.c b/stubs/linux-aio.c index ed47bd443c..88ab927e35 100644 --- a/stubs/linux-aio.c +++ b/stubs/linux-aio.c @@ -21,7 +21,7 @@ void laio_attach_aio_context(LinuxAioState *s, AioContext= *new_context) abort(); } =20 -LinuxAioState *laio_init(void) +int laio_init(LinuxAioState **linux_aio) { abort(); } diff --git a/util/async.c b/util/async.c index 03f62787f2..ae88c931d0 100644 --- a/util/async.c +++ b/util/async.c @@ -323,12 +323,22 @@ ThreadPool *aio_get_thread_pool(AioContext *ctx) } =20 #ifdef CONFIG_LINUX_AIO -LinuxAioState *aio_get_linux_aio(AioContext *ctx) +int aio_setup_linux_aio(AioContext *ctx) { + int rc; + rc =3D 0; if (!ctx->linux_aio) { - ctx->linux_aio =3D laio_init(); - laio_attach_aio_context(ctx->linux_aio, ctx); + rc =3D laio_init(&ctx->linux_aio); + if (rc =3D=3D 0) { + laio_attach_aio_context(ctx->linux_aio, ctx); + } } + return rc; +} + +LinuxAioState *aio_get_linux_aio(AioContext *ctx) +{ + assert(ctx->linux_aio); return ctx->linux_aio; } #endif --=20 2.17.1 From nobody Sat May 4 21:23:35 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1529619792154716.6427608477647; Thu, 21 Jun 2018 15:23:12 -0700 (PDT) Received: from localhost ([::1]:57810 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fW7yx-0005Zs-1X for importer@patchew.org; Thu, 21 Jun 2018 18:23:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52864) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fW7xs-00056E-DB for qemu-devel@nongnu.org; Thu, 21 Jun 2018 18:22:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fW7xr-0008KG-2A for qemu-devel@nongnu.org; Thu, 21 Jun 2018 18:22:04 -0400 Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:43278) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fW7xq-0008J1-Ru for qemu-devel@nongnu.org; Thu, 21 Jun 2018 18:22:02 -0400 Received: by mail-pg0-x241.google.com with SMTP id a14-v6so2030825pgw.10 for ; Thu, 21 Jun 2018 15:22:02 -0700 (PDT) Received: from breakout.internal.digitalocean.com (97-120-190-204.ptld.qwest.net. [97.120.190.204]) by smtp.gmail.com with ESMTPSA id l8-v6sm12790096pfb.102.2018.06.21.15.22.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Jun 2018 15:22:01 -0700 (PDT) Received: by breakout.internal.digitalocean.com (Postfix, from userid 1000) id D0A008A2A84; Thu, 21 Jun 2018 15:22:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digitalocean.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Lm1SmP0GWAb8RzHozgxUy/zrAvWndyRRpbgBBsOetMk=; b=PaRpDiJV+RsAPD1zh7OFtGUGpMF0gtxmELHZ029nNOsoQ/vItYiSiSJLuCkalhwzyO 7RCRJUy62bx1J9CmrCb0LeNtCOrnxZV94aDLOpUeAEOsMyHR1fReRmoXbHespo0fDVqx A/lCiliMdHXyj5rcuzqkPl3mrYLF2kPFWNqVY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Lm1SmP0GWAb8RzHozgxUy/zrAvWndyRRpbgBBsOetMk=; b=d9M38RQz+qJsCy4mxo5+FhKoH3EkY3szyETjA/9jeH5NlKDjh8Yl/lDPH2FswixJi0 tY0JhPHu4Mep5Lyetq2sJNlSc5qyRj4dQgfykwv+vkAbDACbmjNmv6yn0BZBhfu5vfu0 pdS0+Nr/8IHdoE6Cvkge38WOBiPcSC4m/F+Fep6nsiWQFW8fA/BHhI4c6y+Bmvl2kTwV v0/cqdmEvZIgWEMbvuRSrJtn7vfUEYrfAKZ5BxYlZGgSaeitAhasB27wrFmWPeE1nF6l dasO5AqTgG+6Q4FUi/U6/Gph82k2Krlj9mjC+mhGN++gBvdlgygHKmUgnPgrXjp848+Y ST1Q== X-Gm-Message-State: APt69E2s1J9S0x14f0Zt3X+6fZdYxynlGbGWouwEfE+RSgCzPhNpiyGM xxy+/gga1MNN+aA71ti2Gwnatg== X-Google-Smtp-Source: ADUXVKIS2jHscz8/Unz1mXK1tWvWOSkxyTptxv5c8qexxnIWoZ7Fh9esHeEza4BPB/tC5n821HZw5g== X-Received: by 2002:a62:c809:: with SMTP id z9-v6mr28885484pff.5.1529619721979; Thu, 21 Jun 2018 15:22:01 -0700 (PDT) To: naravamudan@digitalocean.com Date: Thu, 21 Jun 2018 15:21:43 -0700 Message-Id: <20180621222143.27266-3-naravamudan@digitalocean.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180621222143.27266-1-naravamudan@digitalocean.com> References: <20180621222143.27266-1-naravamudan@digitalocean.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::241 Subject: [Qemu-devel] [PATCH v3 2/2] block/file-posix: reconfigure aio on iothread start X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Nishanth Aravamudan via Qemu-devel Reply-To: Nishanth Aravamudan Cc: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When the AioContext changes, we need to associate a LinuxAioState with the new AioContext. Use the bdrv_attach_aio_context callback and call the new aio_setup_linux_aio(), which will allocate a new AioContext if needed, and return errors on failures. If it fails for any reason, fallback to threaded AIO with an error message, as the device is already in-use by the guest. Signed-off-by: Nishanth Aravamudan --- Note this patch didn't exist in v2, but is a result of feedback to that posting. block/file-posix.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index 6a1714d4a8..ce24950acf 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1730,6 +1730,21 @@ static BlockAIOCB *raw_aio_flush(BlockDriverState *b= s, return paio_submit(bs, s->fd, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH); } =20 +static void raw_aio_attach_aio_context(BlockDriverState *bs, + AioContext *new_context) +{ +#ifdef CONFIG_LINUX_AIO + BDRVRawState *s =3D bs->opaque; + if (s->use_linux_aio) { + if (aio_setup_linux_aio(new_context) !=3D 0) { + error_report("Unable to use native AIO, falling back to " + "thread pool"); + s->use_linux_aio =3D false; + } + } +#endif +} + static void raw_close(BlockDriverState *bs) { BDRVRawState *s =3D bs->opaque; @@ -2608,6 +2623,7 @@ BlockDriver bdrv_file =3D { .bdrv_refresh_limits =3D raw_refresh_limits, .bdrv_io_plug =3D raw_aio_plug, .bdrv_io_unplug =3D raw_aio_unplug, + .bdrv_attach_aio_context =3D raw_aio_attach_aio_context, =20 .bdrv_truncate =3D raw_truncate, .bdrv_getlength =3D raw_getlength, --=20 2.17.1