From nobody Fri Oct 24 22:12:11 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 1518787136396323.2627017913492; Fri, 16 Feb 2018 05:18:56 -0800 (PST) Received: from localhost ([::1]:48149 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emfuh-0002SE-EA for importer@patchew.org; Fri, 16 Feb 2018 08:18:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51641) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emfsN-00018k-Bn for qemu-devel@nongnu.org; Fri, 16 Feb 2018 08:16:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1emfsL-0005Xp-M2 for qemu-devel@nongnu.org; Fri, 16 Feb 2018 08:16:31 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:35996 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 1emfsL-0005XL-G0 for qemu-devel@nongnu.org; Fri, 16 Feb 2018 08:16:29 -0500 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 2332C4027CDF for ; Fri, 16 Feb 2018 13:16:29 +0000 (UTC) Received: from dgilbert-t530.redhat.com (unknown [10.36.118.23]) by smtp.corp.redhat.com (Postfix) with ESMTP id CEACC213AEE2; Fri, 16 Feb 2018 13:16:27 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, maxime.coquelin@redhat.com, marcandre.lureau@redhat.com, peterx@redhat.com, imammedo@redhat.com, mst@redhat.com Date: Fri, 16 Feb 2018 13:15:57 +0000 Message-Id: <20180216131625.9639-2-dgilbert@redhat.com> In-Reply-To: <20180216131625.9639-1-dgilbert@redhat.com> References: <20180216131625.9639-1-dgilbert@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.6]); Fri, 16 Feb 2018 13:16:29 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Fri, 16 Feb 2018 13:16:29 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'dgilbert@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] [PATCH v3 01/29] migrate: Update ram_block_discard_range for shared 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: aarcange@redhat.com, quintela@redhat.com 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" From: "Dr. David Alan Gilbert" The choice of call to discard a block is getting more complicated for other cases. We use fallocate PUNCH_HOLE in any file cases; it works for both hugepage and for tmpfs. We use the DONTNEED for non-hugepage cases either where they're anonymous or where they're private. Care should be taken when trying other backing files. Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Peter Xu --- exec.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++----------= ---- trace-events | 3 ++- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/exec.c b/exec.c index e8d7b335b6..b1bb477776 100644 --- a/exec.c +++ b/exec.c @@ -3702,6 +3702,7 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t st= art, size_t length) } =20 if ((start + length) <=3D rb->used_length) { + bool need_madvise, need_fallocate; uint8_t *host_endaddr =3D host_startaddr + length; if ((uintptr_t)host_endaddr & (rb->page_size - 1)) { error_report("ram_block_discard_range: Unaligned end address: = %p", @@ -3711,29 +3712,60 @@ int ram_block_discard_range(RAMBlock *rb, uint64_t = start, size_t length) =20 errno =3D ENOTSUP; /* If we are missing MADVISE etc */ =20 - if (rb->page_size =3D=3D qemu_host_page_size) { -#if defined(CONFIG_MADVISE) - /* Note: We need the madvise MADV_DONTNEED behaviour of defini= tely - * freeing the page. - */ - ret =3D madvise(host_startaddr, length, MADV_DONTNEED); -#endif - } else { - /* Huge page case - unfortunately it can't do DONTNEED, but - * it can do the equivalent by FALLOC_FL_PUNCH_HOLE in the - * huge page file. + /* The logic here is messy; + * madvise DONTNEED fails for hugepages + * fallocate works on hugepages and shmem + */ + need_madvise =3D (rb->page_size =3D=3D qemu_host_page_size); + need_fallocate =3D rb->fd !=3D -1; + if (need_fallocate) { + /* For a file, this causes the area of the file to be zero'd + * if read, and for hugetlbfs also causes it to be unmapped + * so a userfault will trigger. */ #ifdef CONFIG_FALLOCATE_PUNCH_HOLE ret =3D fallocate(rb->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEE= P_SIZE, start, length); + if (ret) { + ret =3D -errno; + error_report("ram_block_discard_range: Failed to fallocate= " + "%s:%" PRIx64 " +%zx (%d)", + rb->idstr, start, length, ret); + goto err; + } +#else + ret =3D -ENOSYS; + error_report("ram_block_discard_range: fallocate not available= /file" + "%s:%" PRIx64 " +%zx (%d)", + rb->idstr, start, length, ret); + goto err; #endif } - if (ret) { - ret =3D -errno; - error_report("ram_block_discard_range: Failed to discard range= " + if (need_madvise) { + /* For normal RAM this causes it to be unmapped, + * for shared memory it causes the local mapping to disappear + * and to fall back on the file contents (which we just + * fallocate'd away). + */ +#if defined(CONFIG_MADVISE) + ret =3D madvise(host_startaddr, length, MADV_DONTNEED); + if (ret) { + ret =3D -errno; + error_report("ram_block_discard_range: Failed to discard r= ange " + "%s:%" PRIx64 " +%zx (%d)", + rb->idstr, start, length, ret); + goto err; + } +#else + ret =3D -ENOSYS; + error_report("ram_block_discard_range: MADVISE not available" "%s:%" PRIx64 " +%zx (%d)", rb->idstr, start, length, ret); + goto err; +#endif } + trace_ram_block_discard_range(rb->idstr, host_startaddr, + need_madvise, need_fallocate, ret); } else { error_report("ram_block_discard_range: Overrun block '%s' (%" PRIu= 64 "/%zx/" RAM_ADDR_FMT")", diff --git a/trace-events b/trace-events index ec95e67089..bf9741d930 100644 --- a/trace-events +++ b/trace-events @@ -55,9 +55,10 @@ dma_complete(void *dbs, int ret, void *cb) "dbs=3D%p ret= =3D%d cb=3D%p" dma_blk_cb(void *dbs, int ret) "dbs=3D%p ret=3D%d" dma_map_wait(void *dbs) "dbs=3D%p" =20 -# # exec.c +# exec.c find_ram_offset(uint64_t size, uint64_t offset) "size: 0x%" PRIx64 " @ 0x%= " PRIx64 find_ram_offset_loop(uint64_t size, uint64_t candidate, uint64_t offset, u= int64_t next, uint64_t mingap) "trying size: 0x%" PRIx64 " @ 0x%" PRIx64 ",= offset: 0x%" PRIx64" next: 0x%" PRIx64 " mingap: 0x%" PRIx64 +ram_block_discard_range(const char *rbname, void *hva, bool need_madvise, = bool need_fallocate, int ret) "%s@%p: madvise: %d fallocate: %d ret: %d" =20 # memory.c memory_region_ops_read(int cpu_index, void *mr, uint64_t addr, uint64_t va= lue, unsigned size) "cpu %d mr %p addr 0x%"PRIx64" value 0x%"PRIx64" size %= u" --=20 2.14.3