From: David Hildenbrand <david@redhat.com>
To: Stefan Hajnoczi <stefanha@gmail.com>,
Stefan Hajnoczi <stefanha@redhat.com>
Cc: Albert Esteve <aesteve@redhat.com>,
qemu-devel@nongnu.org, mst@redhat.com, slp@redhat.com,
jasowang@redhat.com
Subject: Re: [RFC PATCH 1/1] vhost-user: add shmem mmap request
Date: Wed, 5 Jun 2024 18:02:06 +0200 [thread overview]
Message-ID: <c89da51b-fef4-4c95-bf71-29598e6553df@redhat.com> (raw)
In-Reply-To: <CAJSP0QUL8VNsYB-D1gaBoE0nrXAQozELU4fwNqoFy5FC9OiEng@mail.gmail.com>
On 05.06.24 17:19, Stefan Hajnoczi wrote:
> On Wed, 5 Jun 2024 at 10:29, Stefan Hajnoczi <stefanha@redhat.com> wrote:
>>
>> On Wed, Jun 05, 2024 at 10:13:32AM +0200, Albert Esteve wrote:
>>> On Tue, Jun 4, 2024 at 8:54 PM Stefan Hajnoczi <stefanha@redhat.com> wrote:
>>>
>>>> On Thu, May 30, 2024 at 05:22:23PM +0200, Albert Esteve wrote:
>>>>> Add SHMEM_MAP/UNMAP requests to vhost-user.
>>>>>
>>>>> This request allows backends to dynamically map
>>>>> fds into a shared memory region indentified by
>>>>
>>>> Please call this "VIRTIO Shared Memory Region" everywhere (code,
>>>> vhost-user spec, commit description, etc) so it's clear that this is not
>>>> about vhost-user shared memory tables/regions.
>>>>
>>>>> its `shmid`. Then, the fd memory is advertised
>>>>> to the frontend through a BAR+offset, so it can
>>>>> be read by the driver while its valid.
>>>>
>>>> Why is a PCI BAR mentioned here? vhost-user does not know about the
>>>> VIRTIO Transport (e.g. PCI) being used. It's the frontend's job to
>>>> report VIRTIO Shared Memory Regions to the driver.
>>>>
>>>>
>>> I will remove PCI BAR, as it is true that it depends on the
>>> transport. I was trying to explain that the driver
>>> will use the shm_base + shm_offset to access
>>> the mapped memory.
>>>
>>>
>>>>>
>>>>> Then, the backend can munmap the memory range
>>>>> in a given shared memory region (again, identified
>>>>> by its `shmid`), to free it. After this, the
>>>>> region becomes private and shall not be accessed
>>>>> by the frontend anymore.
>>>>
>>>> What does "private" mean?
>>>>
>>>> The frontend must mmap PROT_NONE to reserve the virtual memory space
>>>> when no fd is mapped in the VIRTIO Shared Memory Region. Otherwise an
>>>> unrelated mmap(NULL, ...) might use that address range and the guest
>>>> would have access to the host memory! This is a security issue and needs
>>>> to be mentioned explicitly in the spec.
>>>>
>>>
>>> I mentioned private because it changes the mapping from MAP_SHARED
>>> to MAP_PRIVATE. I will highlight PROT_NONE instead.
>>
>> I see. Then "MAP_PRIVATE" would be clearer. I wasn't sure whether you
>> mean mmap flags or something like the memory range is no longer
>> accessible to the driver.
>
> One more thing: please check whether kvm.ko memory regions need to be
> modified or split to match the SHMEM_MAP mapping's read/write
> permissions.
>
> The VIRTIO Shared Memory Area pages can have PROT_READ, PROT_WRITE,
> PROT_READ|PROT_WRITE, or PROT_NONE.
>
> kvm.ko memory regions are read/write or read-only. I'm not sure what
> happens when the guest accesses a kvm.ko memory region containing
> mappings with permissions more restrictive than its kvm.ko memory
> region.
IIRC, the KVM R/O memory region requests could allow to further reduce
permissions (assuming your mmap is R/W you could map it R/O into the KVM
MMU), but I might remember things incorrectly.
> In other words, the kvm.ko memory region would allow the
> access but the Linux virtual memory configuration would cause a page
> fault.
>
> For example, imagine a QEMU MemoryRegion containing a SHMEM_MAP
> mapping with PROT_READ. The kvm.ko memory region would be read/write
> (unless extra steps were taken to tell kvm.ko about the permissions).
> When the guest stores to the PROT_READ page, kvm.ko will process a
> fault...and I'm not sure what happens next.
>
> A similar scenario occurs when a PROT_NONE mapping exists within a
> kvm.ko memory region. I don't remember how kvm.ko behaves when the
> guest tries to access the pages.
>
> It's worth figuring this out before going further because it could
> become tricky if issues are discovered later. I have CCed David
> Hildenbrand in case he knows.
One relevant piece is likely:
"When the KVM_CAP_SYNC_MMU capability is available, changes in the
backing of the memory region are automatically reflected into the guest.
For example, an mmap() that affects the region will be made visible
immediately. "
We can already effectively get R/O or PROT_NONE PTEs in
PROT_READ|PROT_WRITE mappings, and the KVM must be able to handle that
properly -- trigegring a page fault to let core-MM resolve that.
If we have a PROT_NONE VMA and the guest writes to it, we'd likely end
up (to resolve the KVM MMU page fault) in
hva_to_pfn_slow()->get_user_pages_unlocked(), which would return -EFAULT.
Not sure if we really inject a page fault into the guest or if the KVM
run would effectively fail with -EFAULT and make user space unhappy.
Certainly something to play with!
--
Cheers,
David / dhildenb
next prev parent reply other threads:[~2024-06-05 16:02 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-30 15:22 [RFC PATCH 0/1] vhost-user: Add SHMEM_MAP/UNMAP requests Albert Esteve
2024-05-30 15:22 ` [RFC PATCH 1/1] vhost-user: add shmem mmap request Albert Esteve
2024-06-04 18:54 ` Stefan Hajnoczi
2024-06-05 8:13 ` Albert Esteve
2024-06-05 14:28 ` Stefan Hajnoczi
2024-06-05 15:19 ` Stefan Hajnoczi
2024-06-05 16:02 ` David Hildenbrand [this message]
2024-06-05 16:57 ` Stefan Hajnoczi
2024-06-26 7:53 ` Albert Esteve
2024-06-26 13:31 ` Stefan Hajnoczi
2024-06-04 18:16 ` [RFC PATCH 0/1] vhost-user: Add SHMEM_MAP/UNMAP requests Stefan Hajnoczi
2024-06-05 7:24 ` Albert Esteve
2024-06-05 11:16 ` Stefan Hajnoczi
2024-06-05 11:50 ` Albert Esteve
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=c89da51b-fef4-4c95-bf71-29598e6553df@redhat.com \
--to=david@redhat.com \
--cc=aesteve@redhat.com \
--cc=jasowang@redhat.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=slp@redhat.com \
--cc=stefanha@gmail.com \
--cc=stefanha@redhat.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).