From: Peter Xu <peterx@redhat.com>
To: "“William Roche" <william.roche@oracle.com>
Cc: david@redhat.com, kvm@vger.kernel.org, qemu-devel@nongnu.org,
qemu-arm@nongnu.org, pbonzini@redhat.com,
richard.henderson@linaro.org, philmd@linaro.org,
peter.maydell@linaro.org, mtosatti@redhat.com,
imammedo@redhat.com, eduardo@habkost.net,
marcel.apfelbaum@gmail.com, wangyanan55@huawei.com,
zhao1.liu@intel.com, joao.m.martins@oracle.com
Subject: Re: [PATCH v7 2/6] system/physmem: poisoned memory discard on reboot
Date: Tue, 4 Feb 2025 12:09:37 -0500 [thread overview]
Message-ID: <Z6JJ0fDjkttUcW7n@x1.local> (raw)
In-Reply-To: <20250201095726.3768796-3-william.roche@oracle.com>
On Sat, Feb 01, 2025 at 09:57:22AM +0000, “William Roche wrote:
> From: William Roche <william.roche@oracle.com>
>
> Repair poisoned memory location(s), calling ram_block_discard_range():
> punching a hole in the backend file when necessary and regenerating
> a usable memory.
> If the kernel doesn't support the madvise calls used by this function
> and we are dealing with anonymous memory, fall back to remapping the
> location(s).
>
> Signed-off-by: William Roche <william.roche@oracle.com>
> Acked-by: David Hildenbrand <david@redhat.com>
> ---
> system/physmem.c | 58 ++++++++++++++++++++++++++++++------------------
> 1 file changed, 36 insertions(+), 22 deletions(-)
>
> diff --git a/system/physmem.c b/system/physmem.c
> index 3dd2adde73..e8ff930bc9 100644
> --- a/system/physmem.c
> +++ b/system/physmem.c
> @@ -2167,6 +2167,23 @@ void qemu_ram_free(RAMBlock *block)
> }
>
> #ifndef _WIN32
> +/* Simply remap the given VM memory location from start to start+length */
> +static int qemu_ram_remap_mmap(RAMBlock *block, uint64_t start, size_t length)
> +{
> + int flags, prot;
> + void *area;
> + void *host_startaddr = block->host + start;
> +
> + assert(block->fd < 0);
> + flags = MAP_FIXED | MAP_ANONYMOUS;
> + flags |= block->flags & RAM_SHARED ? MAP_SHARED : MAP_PRIVATE;
> + flags |= block->flags & RAM_NORESERVE ? MAP_NORESERVE : 0;
> + prot = PROT_READ;
> + prot |= block->flags & RAM_READONLY ? 0 : PROT_WRITE;
> + area = mmap(host_startaddr, length, prot, flags, -1, 0);
> + return area != host_startaddr ? -errno : 0;
> +}
> +
> /*
> * qemu_ram_remap - remap a single RAM page
> *
> @@ -2184,9 +2201,7 @@ void qemu_ram_remap(ram_addr_t addr)
> {
> RAMBlock *block;
> uint64_t offset;
> - int flags;
> - void *area, *vaddr;
> - int prot;
> + void *vaddr;
> size_t page_size;
>
> RAMBLOCK_FOREACH(block) {
> @@ -2201,25 +2216,24 @@ void qemu_ram_remap(ram_addr_t addr)
> ;
> } else if (xen_enabled()) {
> abort();
> - } else {
Do we need to keep this line? Otherwise it looks to me the new code won't
be executed at all in !xen..
> - flags = MAP_FIXED;
> - flags |= block->flags & RAM_SHARED ?
> - MAP_SHARED : MAP_PRIVATE;
> - flags |= block->flags & RAM_NORESERVE ? MAP_NORESERVE : 0;
> - prot = PROT_READ;
> - prot |= block->flags & RAM_READONLY ? 0 : PROT_WRITE;
> - if (block->fd >= 0) {
> - area = mmap(vaddr, page_size, prot, flags, block->fd,
> - offset + block->fd_offset);
> - } else {
> - flags |= MAP_ANONYMOUS;
> - area = mmap(vaddr, page_size, prot, flags, -1, 0);
> - }
> - if (area != vaddr) {
> - error_report("Could not remap RAM %s:%" PRIx64 "+%" PRIx64
> - " +%zx", block->idstr, offset,
> - block->fd_offset, page_size);
> - exit(1);
> + if (ram_block_discard_range(block, offset, page_size) != 0) {
> + /*
> + * Fall back to using mmap() only for anonymous mapping,
> + * as if a backing file is associated we may not be able
> + * to recover the memory in all cases.
> + * So don't take the risk of using only mmap and fail now.
> + */
> + if (block->fd >= 0) {
> + error_report("Could not remap RAM %s:%" PRIx64 "+%"
> + PRIx64 " +%zx", block->idstr, offset,
> + block->fd_offset, page_size);
> + exit(1);
> + }
> + if (qemu_ram_remap_mmap(block, offset, page_size) != 0) {
> + error_report("Could not remap RAM %s:%" PRIx64 " +%zx",
> + block->idstr, offset, page_size);
> + exit(1);
> + }
> }
> memory_try_enable_merging(vaddr, page_size);
> qemu_ram_setup_dump(vaddr, page_size);
> --
> 2.43.5
>
--
Peter Xu
next prev parent reply other threads:[~2025-02-04 17:09 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-01 9:57 [PATCH v7 0/6] Poisoned memory recovery on reboot “William Roche
2025-02-01 9:57 ` [PATCH v7 1/6] system/physmem: handle hugetlb correctly in qemu_ram_remap() “William Roche
2025-02-04 17:09 ` Peter Xu
2025-02-01 9:57 ` [PATCH v7 2/6] system/physmem: poisoned memory discard on reboot “William Roche
2025-02-04 17:09 ` Peter Xu [this message]
2025-02-05 16:27 ` William Roche
2025-02-01 9:57 ` [PATCH v7 3/6] accel/kvm: Report the loss of a large memory page “William Roche
2025-02-04 17:01 ` Peter Xu
2025-02-05 16:27 ` William Roche
2025-02-05 17:07 ` Peter Xu
2025-02-07 18:02 ` William Roche
2025-02-10 16:48 ` Peter Xu
2025-02-11 21:22 ` William Roche
2025-02-11 21:45 ` Peter Xu
2025-02-01 9:57 ` [PATCH v7 4/6] numa: Introduce and use ram_block_notify_remap() “William Roche
2025-02-04 17:17 ` Peter Xu
2025-02-04 17:42 ` David Hildenbrand
2025-02-01 9:57 ` [PATCH v7 5/6] hostmem: Factor out applying settings “William Roche
2025-02-01 9:57 ` [PATCH v7 6/6] hostmem: Handle remapping of RAM “William Roche
2025-02-04 17:50 ` David Hildenbrand
2025-02-04 17:58 ` Peter Xu
2025-02-04 18:55 ` David Hildenbrand
2025-02-04 20:16 ` Peter Xu
2025-02-05 16:27 ` William Roche
2025-02-05 17:58 ` Peter Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z6JJ0fDjkttUcW7n@x1.local \
--to=peterx@redhat.com \
--cc=david@redhat.com \
--cc=eduardo@habkost.net \
--cc=imammedo@redhat.com \
--cc=joao.m.martins@oracle.com \
--cc=kvm@vger.kernel.org \
--cc=marcel.apfelbaum@gmail.com \
--cc=mtosatti@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=wangyanan55@huawei.com \
--cc=william.roche@oracle.com \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).