From nobody Wed Dec 17 21:44:44 2025 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1526118000488776.4101869236086; Sat, 12 May 2018 02:40:00 -0700 (PDT) Received: from localhost ([::1]:60582 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fHR0R-0002X2-NJ for importer@patchew.org; Sat, 12 May 2018 05:39:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34946) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fHQqs-0006Vl-77 for qemu-devel@nongnu.org; Sat, 12 May 2018 05:30:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fHQqq-0000sg-Re for qemu-devel@nongnu.org; Sat, 12 May 2018 05:30:06 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:50326 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fHQqo-0000pu-1c; Sat, 12 May 2018 05:30:02 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AF7DB7D85E; Sat, 12 May 2018 09:30:01 +0000 (UTC) Received: from localhost (ovpn-116-42.ams2.redhat.com [10.36.116.42]) by smtp.corp.redhat.com (Postfix) with ESMTP id 65ECC215CDA7; Sat, 12 May 2018 09:30:01 +0000 (UTC) From: Stefan Hajnoczi To: Date: Sat, 12 May 2018 10:28:24 +0100 Message-Id: <20180512092824.13848-9-stefanha@redhat.com> In-Reply-To: <20180512092824.13848-1-stefanha@redhat.com> References: <20180512092824.13848-1-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Sat, 12 May 2018 09:30:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Sat, 12 May 2018 09:30:01 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'stefanha@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 8/8] block/file-posix: add x-check-page-cache=on|off option 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: , Cc: Peter Maydell , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" mincore(2) checks whether pages are resident. Use it to verify that page cache has been dropped. You can trigger a verification failure by mmapping the image file from another process that loads a byte from a page, forcing it to become resident. bdrv_co_invalidate_cache() will fail while that process is alive. Signed-off-by: Stefan Hajnoczi Reviewed-by: Fam Zheng Message-id: 20180427162312.18583-3-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- qapi/block-core.json | 7 ++- block/file-posix.c | 100 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index c50517bff3..21c3470234 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -2530,6 +2530,10 @@ # @locking: whether to enable file locking. If set to 'auto', only ena= ble # when Open File Descriptor (OFD) locking API is available # (default: auto, since 2.10) +# @x-check-cache-dropped: whether to check that page cache was dropped on = live +# migration. May cause noticeable delays if the i= mage +# file is large, do not use in production. +# (default: off) (since: 2.13) # # Since: 2.9 ## @@ -2537,7 +2541,8 @@ 'data': { 'filename': 'str', '*pr-manager': 'str', '*locking': 'OnOffAuto', - '*aio': 'BlockdevAioOptions' } } + '*aio': 'BlockdevAioOptions', + '*x-check-cache-dropped': 'bool' } } =20 ## # @BlockdevOptionsNull: diff --git a/block/file-posix.c b/block/file-posix.c index 3707ea2d1c..5a602cfe37 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -161,6 +161,7 @@ typedef struct BDRVRawState { bool page_cache_inconsistent:1; bool has_fallocate; bool needs_alignment; + bool check_cache_dropped; =20 PRManager *pr_mgr; } BDRVRawState; @@ -168,6 +169,7 @@ typedef struct BDRVRawState { typedef struct BDRVRawReopenState { int fd; int open_flags; + bool check_cache_dropped; } BDRVRawReopenState; =20 static int fd_open(BlockDriverState *bs); @@ -415,6 +417,11 @@ static QemuOptsList raw_runtime_opts =3D { .type =3D QEMU_OPT_STRING, .help =3D "id of persistent reservation manager object (defaul= t: none)", }, + { + .name =3D "x-check-cache-dropped", + .type =3D QEMU_OPT_BOOL, + .help =3D "check that page cache was dropped on live migration= (default: off)" + }, { /* end of list */ } }, }; @@ -500,6 +507,9 @@ static int raw_open_common(BlockDriverState *bs, QDict = *options, } } =20 + s->check_cache_dropped =3D qemu_opt_get_bool(opts, "x-check-cache-drop= ped", + false); + s->open_flags =3D open_flags; raw_parse_flags(bdrv_flags, &s->open_flags); =20 @@ -777,6 +787,7 @@ static int raw_reopen_prepare(BDRVReopenState *state, { BDRVRawState *s; BDRVRawReopenState *rs; + QemuOpts *opts; int ret =3D 0; Error *local_err =3D NULL; =20 @@ -787,6 +798,19 @@ static int raw_reopen_prepare(BDRVReopenState *state, =20 state->opaque =3D g_new0(BDRVRawReopenState, 1); rs =3D state->opaque; + rs->fd =3D -1; + + /* Handle options changes */ + opts =3D qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); + qemu_opts_absorb_qdict(opts, state->options, &local_err); + if (local_err) { + error_propagate(errp, local_err); + ret =3D -EINVAL; + goto out; + } + + rs->check_cache_dropped =3D qemu_opt_get_bool(opts, "x-check-cache-dro= pped", + s->check_cache_dropped); =20 if (s->type =3D=3D FTYPE_CD) { rs->open_flags |=3D O_NONBLOCK; @@ -794,8 +818,6 @@ static int raw_reopen_prepare(BDRVReopenState *state, =20 raw_parse_flags(state->flags, &rs->open_flags); =20 - rs->fd =3D -1; - int fcntl_flags =3D O_APPEND | O_NONBLOCK; #ifdef O_NOATIME fcntl_flags |=3D O_NOATIME; @@ -850,6 +872,8 @@ static int raw_reopen_prepare(BDRVReopenState *state, } } =20 +out: + qemu_opts_del(opts); return ret; } =20 @@ -858,6 +882,7 @@ static void raw_reopen_commit(BDRVReopenState *state) BDRVRawReopenState *rs =3D state->opaque; BDRVRawState *s =3D state->bs->opaque; =20 + s->check_cache_dropped =3D rs->check_cache_dropped; s->open_flags =3D rs->open_flags; =20 qemu_close(s->fd); @@ -2236,6 +2261,73 @@ static int coroutine_fn raw_co_block_status(BlockDri= verState *bs, return ret | BDRV_BLOCK_OFFSET_VALID; } =20 +#if defined(__linux__) +/* Verify that the file is not in the page cache */ +static void check_cache_dropped(BlockDriverState *bs, Error **errp) +{ + const size_t window_size =3D 128 * 1024 * 1024; + BDRVRawState *s =3D bs->opaque; + void *window =3D NULL; + size_t length =3D 0; + unsigned char *vec; + size_t page_size; + off_t offset; + off_t end; + + /* mincore(2) page status information requires 1 byte per page */ + page_size =3D sysconf(_SC_PAGESIZE); + vec =3D g_malloc(DIV_ROUND_UP(window_size, page_size)); + + end =3D raw_getlength(bs); + + for (offset =3D 0; offset < end; offset +=3D window_size) { + void *new_window; + size_t new_length; + size_t vec_end; + size_t i; + int ret; + + /* Unmap previous window if size has changed */ + new_length =3D MIN(end - offset, window_size); + if (new_length !=3D length) { + munmap(window, length); + window =3D NULL; + length =3D 0; + } + + new_window =3D mmap(window, new_length, PROT_NONE, MAP_PRIVATE, + s->fd, offset); + if (new_window =3D=3D MAP_FAILED) { + error_setg_errno(errp, errno, "mmap failed"); + break; + } + + window =3D new_window; + length =3D new_length; + + ret =3D mincore(window, length, vec); + if (ret < 0) { + error_setg_errno(errp, errno, "mincore failed"); + break; + } + + vec_end =3D DIV_ROUND_UP(length, page_size); + for (i =3D 0; i < vec_end; i++) { + if (vec[i] & 0x1) { + error_setg(errp, "page cache still in use!"); + break; + } + } + } + + if (window) { + munmap(window, length); + } + + g_free(vec); +} +#endif /* __linux__ */ + static void coroutine_fn raw_co_invalidate_cache(BlockDriverState *bs, Error **errp) { @@ -2269,6 +2361,10 @@ static void coroutine_fn raw_co_invalidate_cache(Blo= ckDriverState *bs, error_setg_errno(errp, ret, "fadvise failed"); return; } + + if (s->check_cache_dropped) { + check_cache_dropped(bs, errp); + } #else /* __linux__ */ /* Do nothing. Live migration to a remote host with cache.direct=3Dof= f is * unsupported on other host operating systems. Cache consistency iss= ues --=20 2.17.0