qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: Peter Xu <peterx@redhat.com>
Cc: qemu-devel@nongnu.org, "Paolo Bonzini" <pbonzini@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Thiner Logoer" <logoerthiner1@163.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: Re: [PATCH v1 1/3] softmmu/physmem: fallback to opening guest RAM file as readonly in a MAP_PRIVATE mapping
Date: Wed, 9 Aug 2023 11:20:11 +0200	[thread overview]
Message-ID: <1d1a7d8f-6260-5905-57ea-514b762ce869@redhat.com> (raw)
In-Reply-To: <ZNKtHVotkfgI1tb4@x1n>

Hi Peter!

>> -    fd = file_ram_open(mem_path, memory_region_name(mr), readonly, &created,
>> -                       errp);
>> +    fd = file_ram_open(mem_path, memory_region_name(mr), readonly, &created);
>> +    if (fd == -EACCES && !(ram_flags & RAM_SHARED) && !readonly) {
>> +        /*
>> +         * We can have a writable MAP_PRIVATE mapping of a readonly file.
>> +         * However, some operations like ftruncate() or fallocate() might fail
>> +         * later, let's warn the user.
>> +         */
>> +        fd = file_ram_open(mem_path, memory_region_name(mr), true, &created);
>> +        if (fd >= 0) {
>> +            warn_report("backing store %s for guest RAM (MAP_PRIVATE) opened"
>> +                        " readonly because the file is not writable", mem_path);
> 
> I can understand the use case, but this will be slightly unwanted,
> especially the user doesn't yet have a way to predict when will it happen.

Users can set the file permissions accordingly I guess. If they don't 
want the file to never ever be modified via QEMU, set it R/O.

> 
> Meanwhile this changes the behavior, is it a concern that someone may want
> to rely on current behavior of failing?

The scenario would be that someone passes a readonly file to "-mem-path" 
or "-object memory-backend-file,share=off,readonly=off", with the 
expectation that it would currently fail.

If it now doesn't fail (and we warn instead), what would happen is:
* In file_ram_alloc() we won't even try ftruncate(), because the file
   already had a size > 0. So ftruncate() is not a concern as I now
   realize.
* fallocate might fail later. AFAIKS, that only applies to
   ram_block_discard_range().
  -> virtio-mem performs an initial ram_block_discard_range() check and
     fails gracefully early.
  -> virtio-ballooon ignores any errors
  -> ram_discard_range() in migration code fails early for postcopy in
     init_range() and loadvm_postcopy_ram_handle_discard(), handling it
     gracefully.

So mostly nothing "bad" would happen, it might just be undesirable, and 
we properly warn.

Most importantly, we won't be corrupting/touching the original file in 
any case, because it is R/O.

If we really want to be careful, we could clue that behavior to compat 
machines. I'm not really sure yet if we really have to go down that path.

Any other alternatives? I'd like to avoid new flags where not really 
required.

> 
> To think from a higher level of current use case, the ideal solution seems
> to me that if the ram file can be put on a file system that supports CoW
> itself (like btrfs), we can snapshot that ram file and make it RW for the
> qemu instance. Then here it'll be able to open the file.  We'll be able to
> keep the interface working as before, meanwhile it'll work with fallocate
> or truncations too I assume.
> 
> Would that be better instead of changing QEMU?

As I recently learned, using file-backed VMs (on real ssd/disks, not 
shmem/hugetlb) is usually undesired, because the dirtied pages will 
constantly get rewritten to disk by background writeback threads, 
eventually resulting in bad performance and SSD wear.

So while using a COW filesystem sounds cleaner in theory, it's not 
applicable in practice -- unless one disables any background writeback, 
which has different side effects because it cannot be configured on a 
per-file basis.

So for VM templating, it makes sense to capture the guest RAM and store 
it in a file, to then use a COW (MAP_PRIVATE) mapping. Using a read-only 
file makes perfect sense in that scenario IMHO.

[I'm curious at what point a filesystem will actually break COW. if it's 
wired up to the writenotify infrastructure, it would happen when 
actually writing to a page, not at mmap time. I know that filesystems 
use writenotify for lazy allocation of disk blocks on file holes, maybe 
they also do that for lazy allocation of disk blocks on COW]

Thanks!

-- 
Cheers,

David / dhildenb



  parent reply	other threads:[~2023-08-09  9:20 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-07 19:07 [PATCH v1 0/3] softmmu/physmem: file_ram_open() readonly improvements David Hildenbrand
2023-08-07 19:07 ` [PATCH v1 1/3] softmmu/physmem: fallback to opening guest RAM file as readonly in a MAP_PRIVATE mapping David Hildenbrand
2023-08-08 21:01   ` Peter Xu
2023-08-09  5:39     ` ThinerLogoer
2023-08-09  9:20     ` David Hildenbrand [this message]
2023-08-09 15:15       ` Peter Xu
2023-08-10 14:19         ` David Hildenbrand
2023-08-10 17:06           ` ThinerLogoer
2023-08-10 21:24             ` Peter Xu
2023-08-11  5:49               ` ThinerLogoer
2023-08-11 14:31                 ` Peter Xu
2023-08-12  6:21                   ` ThinerLogoer
2023-08-22 13:35                     ` David Hildenbrand
2023-08-11 19:00                 ` David Hildenbrand
2023-08-12  5:18                   ` ThinerLogoer
2023-08-17  9:07                     ` David Hildenbrand
2023-08-17 14:30                       ` David Hildenbrand
2023-08-17 14:37                         ` Daniel P. Berrangé
2023-08-17 14:37                           ` David Hildenbrand
2023-08-17 14:45                             ` Daniel P. Berrangé
2023-08-17 14:47                               ` David Hildenbrand
2023-08-17 14:41                       ` Peter Xu
2023-08-17 15:02                         ` David Hildenbrand
2023-08-17 15:13                       ` Stefan Hajnoczi
2023-08-17 15:15                         ` David Hildenbrand
2023-08-17 15:25                           ` David Hildenbrand
2023-08-17 15:31                           ` Peter Xu
2023-08-17 15:43                             ` David Hildenbrand
2023-08-17 13:46                   ` Daniel P. Berrangé
2023-08-17 13:48                     ` David Hildenbrand
2023-08-11 14:59               ` David Hildenbrand
2023-08-11 15:26                 ` David Hildenbrand
2023-08-11 16:16                   ` Peter Xu
2023-08-11 16:17                     ` David Hildenbrand
2023-08-11 16:22                       ` Peter Xu
2023-08-11 16:25                         ` David Hildenbrand
2023-08-11 16:54                           ` Peter Xu
2023-08-11 17:39                             ` David Hildenbrand
2023-08-11 21:07                               ` Peter Xu
2023-08-21 12:20                   ` Igor Mammedov
2023-08-11 15:47                 ` Peter Xu
2023-08-17 13:42           ` Daniel P. Berrangé
2023-08-17 13:45             ` David Hildenbrand
2023-08-17 13:37   ` Daniel P. Berrangé
2023-08-17 13:44     ` David Hildenbrand
2023-08-07 19:07 ` [PATCH v1 2/3] softmmu/physmem: fail creation of new files in file_ram_open() with readonly=true David Hildenbrand
2023-08-07 19:07 ` [PATCH v1 3/3] softmmu/physmem: never return directories from file_ram_open() David Hildenbrand
2023-08-08 17:26 ` Re:[PATCH v1 0/3] softmmu/physmem: file_ram_open() readonly improvements ThinerLogoer
2023-08-10 11:11   ` [PATCH " Philippe Mathieu-Daudé
2023-08-10 16:35     ` ThinerLogoer

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=1d1a7d8f-6260-5905-57ea-514b762ce869@redhat.com \
    --to=david@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=logoerthiner1@163.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /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).