qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@redhat.com>
To: Stefan Hajnoczi <stefanha@redhat.com>, qemu-devel@nongnu.org
Cc: Xiao Guangrong <xiaoguangrong.eric@gmail.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Julio Montes <julio.montes@intel.com>,
	Igor Mammedov <imammedo@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Richard Henderson <rth@twiddle.net>,
	Eduardo Habkost <ehabkost@redhat.com>
Subject: Re: [PATCH 1/3] memory: add readonly support to memory_region_init_ram_from_file()
Date: Tue, 4 Aug 2020 14:26:22 +0200	[thread overview]
Message-ID: <1240d81b-7ac5-d06d-671a-70614004a721@redhat.com> (raw)
In-Reply-To: <febc6361-1c62-bc3e-7924-bbd19711b5df@redhat.com>

On 8/4/20 2:25 PM, Philippe Mathieu-Daudé wrote:
> Hi Stefan,
> 
> On 8/4/20 12:12 PM, Stefan Hajnoczi wrote:
>> There is currently no way to open(O_RDONLY) and mmap(PROT_READ) when
>> creating a memory region from a file. This functionality is needed since
>> the underlying host file may not allow writing.
>>
>> Add a bool readonly argument to memory_region_init_ram_from_file() and
>> the APIs it calls.
>>
>> Extend memory_region_init_ram_from_file() rather than introducing a
>> memory_region_init_rom_from_file() API so that callers can easily make a
>> choice between read/write and read-only at runtime without calling
>> different APIs.
> 
> What happens if we call:
> 
>  memory_region_init_ram_from_file(mr, ..., readonly=false, ...);
>  memory_region_set_readonly(mr, false);

In case my error is not obvious, I meant:

   memory_region_init_ram_from_file(mr, ..., readonly=true, ...);
   memory_region_set_readonly(mr, false);

> 
> ?
> 
>>
>> No new RAMBlock flag is introduced for read-only because it's unclear
>> whether RAMBlocks need to know that they are read-only. Pass a bool
>> readonly argument instead.
>>
>> Both of these design decisions can be changed in the future. It just
>> seemed like the simplest approach to me.
>>
>> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
>> ---
>>  include/exec/memory.h     |  2 ++
>>  include/exec/ram_addr.h   |  5 +++--
>>  include/qemu/mmap-alloc.h |  2 ++
>>  backends/hostmem-file.c   |  2 +-
>>  exec.c                    | 18 +++++++++++-------
>>  softmmu/memory.c          |  7 +++++--
>>  util/mmap-alloc.c         | 10 ++++++----
>>  util/oslib-posix.c        |  2 +-
>>  8 files changed, 31 insertions(+), 17 deletions(-)
>>
>> diff --git a/include/exec/memory.h b/include/exec/memory.h
>> index 307e527835..1ae7b31e3a 100644
>> --- a/include/exec/memory.h
>> +++ b/include/exec/memory.h
>> @@ -884,6 +884,7 @@ void memory_region_init_resizeable_ram(MemoryRegion *mr,
>>   *             - RAM_PMEM: the memory is persistent memory
>>   *             Other bits are ignored now.
>>   * @path: the path in which to allocate the RAM.
>> + * @readonly: true to open @path for reading, false for read/write.
>>   * @errp: pointer to Error*, to store an error if it happens.
>>   *
>>   * Note that this function does not do anything to cause the data in the
>> @@ -896,6 +897,7 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
>>                                        uint64_t align,
>>                                        uint32_t ram_flags,
>>                                        const char *path,
>> +                                      bool readonly,
>>                                        Error **errp);
>>  
> [...]
>> diff --git a/softmmu/memory.c b/softmmu/memory.c
>> index af25987518..d228635bb3 100644
>> --- a/softmmu/memory.c
>> +++ b/softmmu/memory.c
>> @@ -1553,15 +1553,18 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
>>                                        uint64_t align,
>>                                        uint32_t ram_flags,
>>                                        const char *path,
>> +                                      bool readonly,
>>                                        Error **errp)
>>  {
>>      Error *err = NULL;
>>      memory_region_init(mr, owner, name, size);
>>      mr->ram = true;
>> +    mr->readonly = readonly;
>>      mr->terminates = true;
>>      mr->destructor = memory_region_destructor_ram;
>>      mr->align = align;
>> -    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, &err);
>> +    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path,
>> +                                             readonly, &err);
>>      mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
>>      if (err) {
>>          mr->size = int128_zero();
>> @@ -1585,7 +1588,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
>>      mr->destructor = memory_region_destructor_ram;
>>      mr->ram_block = qemu_ram_alloc_from_fd(size, mr,
>>                                             share ? RAM_SHARED : 0,
>> -                                           fd, &err);
>> +                                           fd, false, &err);
>>      mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
>>      if (err) {
>>          mr->size = int128_zero();
>> diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c
>> index 27dcccd8ec..890fda6a35 100644
>> --- a/util/mmap-alloc.c
>> +++ b/util/mmap-alloc.c
>> @@ -85,9 +85,11 @@ size_t qemu_mempath_getpagesize(const char *mem_path)
>>  void *qemu_ram_mmap(int fd,
>>                      size_t size,
>>                      size_t align,
>> +                    bool readonly,
>>                      bool shared,
>>                      bool is_pmem)
>>  {
>> +    int prot;
>>      int flags;
>>      int map_sync_flags = 0;
>>      int guardfd;
>> @@ -146,8 +148,9 @@ void *qemu_ram_mmap(int fd,
>>  
>>      offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
>>  
>> -    ptr = mmap(guardptr + offset, size, PROT_READ | PROT_WRITE,
>> -               flags | map_sync_flags, fd, 0);
>> +    prot = PROT_READ | (readonly ? 0 : PROT_WRITE);
>> +
>> +    ptr = mmap(guardptr + offset, size, prot, flags | map_sync_flags, fd, 0);
>>  
>>      if (ptr == MAP_FAILED && map_sync_flags) {
>>          if (errno == ENOTSUP) {
>> @@ -171,8 +174,7 @@ void *qemu_ram_mmap(int fd,
>>           * if map failed with MAP_SHARED_VALIDATE | MAP_SYNC,
>>           * we will remove these flags to handle compatibility.
>>           */
>> -        ptr = mmap(guardptr + offset, size, PROT_READ | PROT_WRITE,
>> -                   flags, fd, 0);
>> +        ptr = mmap(guardptr + offset, size, prot, flags, fd, 0);
>>      }
>>  
>>      if (ptr == MAP_FAILED) {
> [...]
> 



  reply	other threads:[~2020-08-04 12:27 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-04 10:12 [PATCH 0/3] nvdimm: read-only file support Stefan Hajnoczi
2020-08-04 10:12 ` [PATCH 1/3] memory: add readonly support to memory_region_init_ram_from_file() Stefan Hajnoczi
2020-08-04 12:25   ` Philippe Mathieu-Daudé
2020-08-04 12:26     ` Philippe Mathieu-Daudé [this message]
2020-08-04 13:47       ` Stefan Hajnoczi
2020-08-04 13:57         ` Philippe Mathieu-Daudé
2020-08-04 10:12 ` [PATCH 2/3] hostmem-file: add readonly=on|off option Stefan Hajnoczi
2020-08-21 12:50   ` Philippe Mathieu-Daudé
2020-09-16  9:31     ` Stefan Hajnoczi
2020-09-16 10:17       ` Philippe Mathieu-Daudé
2020-08-04 10:12 ` [PATCH 3/3] nvdimm: honor -object memory-backend-file, readonly=on option Stefan Hajnoczi
2020-08-21 13:03   ` Philippe Mathieu-Daudé
2020-09-16  9:39     ` Stefan Hajnoczi
2020-08-04 12:28 ` [PATCH 0/3] nvdimm: read-only file support Michael S. Tsirkin
2020-08-21 12:18 ` Stefan Hajnoczi

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=1240d81b-7ac5-d06d-671a-70614004a721@redhat.com \
    --to=philmd@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=julio.montes@intel.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    --cc=stefanha@redhat.com \
    --cc=xiaoguangrong.eric@gmail.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).