KVM: guest_memfd: use write for population
Subsystems (3)
v6 cover KVM: guest_memfd: use write for population
[ based on kvm/next ]
Implement guest_memfd population via the write syscall.
This is useful in non-CoCo use cases where the host can access guest memory. Even though the same can also be achieved via userspace mapping and memcpying from userspace, write provides a more performant option because it does not need to set page tables and it does not cause a page fault for every page like memcpy would. Note that memcpy cannot be accelerated via MADV_POPULATE_WRITE as it is not supported by guest_memfd and relies on GUP.
Populating 512MiB of guest_memfd on a x86 machine:
- via memcpy: 436 ms
- via write: 202 ms (-54%)
The write syscall support is conditional on kvm_gmem_supports_mmap. When in-place shared/private conversion is supported, write should only be allowed on shared pages.
v6:
- Make write support conditional on mmap support instead of relying on the up-to-date flag to decide whether writing to a page is allowed
- James: Remove depenendencies on folio_test_large
- James: Remove page alignment restriction
- James: Formatting fixes
v5:
- https://lore.kernel.org/kvm/[email protected]/
- Replace the call to the unexported filemap_remove_folio with zeroing the bytes that could not be copied
- Fix checkpatch findings
v4:
- https://lore.kernel.org/kvm/[email protected]
- Switch from implementing the write callback to write_iter
- Remove conditional compilation
v3:
- https://lore.kernel.org/kvm/[email protected]
- David/Mike D: Only compile support for the write syscall if CONFIG_KVM_GMEM_SHARED_MEM (now gone) is enabled.
v2:
- https://lore.kernel.org/kvm/[email protected]
- Switch from an ioctl to the write syscall to implement population
v1:
- https://lore.kernel.org/kvm/[email protected]
Nikita Kalyazin (2):
KVM: guest_memfd: add generic population via write
KVM: selftests: update guest_memfd write tests
.../testing/selftests/kvm/guest_memfd_test.c | 51 ++++++++++++++++--- virt/kvm/guest_memfd.c | 49 ++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-)
base-commit: 6b36119b94d0b2bb8cea9d512017efafd461d6ac
Signature · 1 line
2.50.1
PATCH v6 1/2 KVM: guest_memfd: add generic population via write
From: Nikita Kalyazin <[email protected]>
write syscall populates guest_memfd with user-supplied data in a generic way, ie no vendor-specific preparation is performed. If the request is not page-aligned, the remaining bytes are initialised to 0.
write is only supported for non-CoCo setups where guest memory is not hardware-encrypted.
--- virt/kvm/guest_memfd.c | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
virt/kvm/guest_memfd.c +48 -0
@@ -380,6 +380,8 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)static struct file_operations kvm_gmem_fops = { .mmap = kvm_gmem_mmap, + .llseek = default_llseek, + .write_iter = generic_perform_write, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate,
@@ -390,6 +392,49 @@ void kvm_gmem_init(struct module *module) kvm_gmem_fops.owner = module; } +static int kvm_kmem_gmem_write_begin (const struct kiocb *kiocb, +struct address_space *mapping, +loff_t pos,unsigned int len, +struct folio **foliop, +void **fsdata) +{ +struct file *file = kiocb->ki_filp; +struct inode *inode =file_inode (file); +pgoff_t index = pos >>PAGE_SHIFT ; +struct folio *folio; + +if (!kvm_gmem_supports_mmap (inode)) +return -ENODEV ; + +if (pos + len >i_size_read (inode)) +return -EINVAL ; + + folio =kvm_gmem_get_folio (inode, index); +if (IS_ERR (folio)) +return -EFAULT ; + + *foliop = folio; +return 0 ; +} + +static int kvm_kmem_gmem_write_end (const struct kiocb *kiocb, +struct address_space *mapping, +loff_t pos,unsigned int len, +unsigned int copied, +struct folio *folio,void *fsdata) +{ +if (copied && copied < len) { +unsigned int from = pos & ((1UL <<folio_order (folio)) -1 ); + +folio_zero_range (folio, from + copied, len - copied); + } + +folio_unlock (folio); +folio_put (folio); + +return copied; +} +static int kvm_gmem_migrate_folio (struct address_space *mapping,struct folio *dst,struct folio *src,enum migrate_mode mode)
@@ -442,6 +487,8 @@ static void kvm_gmem_free_folio(struct folio *folio)static const struct address_space_operations kvm_gmem_aops = { .dirty_folio = noop_dirty_folio, + .write_begin = kvm_kmem_gmem_write_begin, + .write_end = kvm_kmem_gmem_write_end, .migrate_folio = kvm_gmem_migrate_folio, .error_remove_folio = kvm_gmem_error_folio, #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE
@@ -489,6 +536,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) } file->f_flags |=O_LARGEFILE ; + file->f_mode |=FMODE_LSEEK |FMODE_PWRITE ; inode = file->f_inode;WARN_ON (file->f_mapping != inode->i_mapping);
Signature · 1 line
2.50.1
Sean Christopherson
maintainerRe: [PATCH v6 1/2] KVM: guest_memfd: add generic population via write
Quoted 8 lines · Nikita Kalyazin
From: Nikita Kalyazin <[email protected]>
write syscall populates
guest_memfdwith user-supplied data in a generic way, ie no vendor-specific preparation is performed. If the request is not page-aligned, the remaining bytes are initialised to 0.write is only supported for non-CoCo setups where guest memory is not hardware-encrypted.
Please include all of the "why". The code mostly communicates the "what", but it doesn't capture why write() support is at all interesting, nor does it explain why read() isn't supported.
Quoted 3 lines
Signed-off-by Nikita Kalyazin <[email protected]>--- virt/kvm/guest_memfd.c | 48 ++++++++++++++++++++++++++++++++++++++++++
There's a notable lack of uAPI and Documentation chanegs. I.e. this needs a GUEST_MEMFD_FLAG_xxx along with proper documentation.
And while it's definitely it's a-ok to land .write() in advance of the direct map changes, we do need to at least map out how we want the two to interact, e.g. so that we don't end up with constraints that are impossible to satisfy.
Quoted 16 lines
1 file changed, 48 insertions(+)virt/kvm/guest_memfd.c +2
-0@@ -380,6 +380,8 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)static struct file_operations kvm_gmem_fops = { .mmap = kvm_gmem_mmap, + .llseek = default_llseek, + .write_iter = generic_perform_write, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate,
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 399
Quoted 8 lines
virt/kvm/guest_memfd.c +5
-0@@ -390,6 +392,49 @@ void kvm_gmem_init(struct module *module) kvm_gmem_fops.owner = module; } +static int kvm_kmem_gmem_write_begin (const struct kiocb *kiocb, +struct address_space *mapping, +loff_t pos,unsigned int len, +struct folio **foliop, +void **fsdata)
Over-aggressive wrapping, this can be
static int kvm_kmem_gmem_write_begin(const struct kiocb *kiocb,
struct address_space *mapping, loff_t pos, unsigned int len, struct folio **folio, void **fsdata)
or
static int kvm_kmem_gmem_write_begin(const struct kiocb *kiocb,
struct address_space *mapping, loff_t pos, unsigned int len, struct folio **folio, void **fsdata)
if we want to bundle pos+len.
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 401
Quoted 2 lines
virt/kvm/guest_memfd.c +2
-0+{ +struct file *file = kiocb->ki_filp;
ki_filp is already a file, and even if it were a "void *", there's no need for a local variable.
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 406
Quoted 5 lines
virt/kvm/guest_memfd.c +5
-0+struct inode *inode =file_inode (file); +pgoff_t index = pos >>PAGE_SHIFT ; +struct folio *folio; + +if (!kvm_gmem_supports_mmap (inode))
Checking for MMAP is neither sufficient nor strictly necessary. MMAP doesn't imply SHARED, and it's not clear to me that mmap() support should be in any way tied to WRITE support.
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 412
Quoted 6 lines
virt/kvm/guest_memfd.c +6
-0+return -ENODEV ; + +if (pos + len >i_size_read (inode)) +return -EINVAL ; + + folio =kvm_gmem_get_folio (inode, index);
Eh, since "index" is only used once, my vote is to use "pos" and do the shift here, so that it's obvious that the input to kvm_gmem_get_folio() is being checked.
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 414
Quoted 2 lines
virt/kvm/guest_memfd.c +2
-0+if (IS_ERR (folio)) +return -EFAULT ;
Why EFAULT?
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 416
Quoted 2 lines
virt/kvm/guest_memfd.c +2
-0+ + *foliop = folio;
There shouldn't be any need for a local "folio". What about having the "out" param be just "folio"?
E.g.
static int kvm_kmem_gmem_write_begin(const struct kiocb *kiocb,
struct address_space *mapping, loff_t pos, unsigned int len, struct folio **folio, void **fsdata)
{
struct inode *inode = file_inode(kiocb->ki_filp);
if (!kvm_gmem_supports_write(inode))
return -ENODEV;
if (pos + len > i_size_read(inode))
return -EINVAL;
*folio = kvm_gmem_get_folio(inode, pos >> PAGE_SHIFT);
if (IS_ERR(*folio))
return PTR_ERR(*folio);
return 0;
}virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 426
Quoted 10 lines
virt/kvm/guest_memfd.c +10
-0+return 0 ; +} + +static int kvm_kmem_gmem_write_end (const struct kiocb *kiocb, +struct address_space *mapping, +loff_t pos,unsigned int len, +unsigned int copied, +struct folio *folio,void *fsdata) +{ +if (copied && copied < len) {
Why check if "copied" is non-zero? I don't see why KVM should behave differently with respect to unwritten bytes if copy_folio_from_iter_atomic() fails on the first byte or the Nth byte.
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 427
Quoted 1 line
virt/kvm/guest_memfd.c +1
-0+unsigned int from = pos & ((1UL <<folio_order (folio)) -1 );
Uh, isn't this just offset_in_folio()?
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 429
Quoted 2 lines
virt/kvm/guest_memfd.c +2
-0+ +folio_zero_range (folio, from + copied, len - copied);
I'd probably be in favor of omitting "from" entirely, e.g.
if (copied < len)
folio_zero_range(folio, offset_in_folio(pos) + copied,
len - copied);
Quoted 7 lines
virt/kvm/guest_memfd.c +7
-0+ } + +folio_unlock (folio); +folio_put (folio); + +return copied; +}
Re: [PATCH v6 1/2] KVM: guest_memfd: add generic population via write
Quoted 2 lines · Sean Christopherson
Quoted 1 line · Nikita Kalyazin
From: Nikita Kalyazin <[email protected]>
+ Vishal and Ackerley
Quoted 11 lines
Quoted 7 lines
write syscall populates
guest_memfdwith user-supplied data in a generic way, ie no vendor-specific preparation is performed. If the request is not page-aligned, the remaining bytes are initialised to 0.write is only supported for non-CoCo setups where guest memory is not hardware-encrypted.
Please include all of the "why". The code mostly communicates the "what", but it doesn't capture why
write()support is at all interesting, nor does it explain whyread()isn't supported.
Hi Sean,
Thanks for the review.
Do you think including the explanation from the cover letter would be sufficient? Shall I additionally say that read() isn't supported because there is no use case for it as of now or would it be obvious?
Quoted 7 lines
Quoted 4 lines
Signed-off-by Nikita Kalyazin <[email protected]>--- virt/kvm/guest_memfd.c | 48 ++++++++++++++++++++++++++++++++++++++++++There's a notable lack of uAPI and Documentation chanegs. I.e. this needs a
GUEST_MEMFD_FLAG_xxxalong with proper documentation.
Would the following be ok in the doc?
When the capability KVM_CAP_GUEST_MEMFD_WRITE is supported, the 'flags' field supports GUEST_MEMFD_FLAG_WRITE. Setting this flag on guest_memfd creation enables write() syscall operations to populate guest_memfd memory from host userspace.
When a write() operation is performed on a guest_memfd file descriptor with the GUEST_MEMFD_FLAG_WRITE set, the syscall will populate the guest memory with user-supplied data in a generic way, without any vendor-specific preparation. The write operation is only supported for non-CoCo (Confidential Computing) setups where guest memory is not hardware-encrypted. If the write request is not page-aligned, any remaining bytes within the page are initialized to zero.
Quoted 5 lines
And while it's definitely it's a-ok to land .write() in advance of the direct map changes, we do need to at least map out how we want the two to interact, e.g. so that we don't end up with constraints that are impossible to satisfy.
write() shall not attempt to access a page that is not in the direct map, which I believe can be achieved via kvm_kmem_gmem_write_begin() consulting the KVM_GMEM_FOLIO_NO_DIRECT_MAP in folio->private (introduced in [1]).
Do you think we should mention it in the commit message in some way? What particular constraint are you cautious about?
[1] https://lore.kernel.org/kvm/[email protected]/
Quoted 33 lines
Quoted 16 lines
1 file changed, 48 insertions(+)virt/kvm/guest_memfd.c +2
-0@@ -380,6 +380,8 @@ static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma)static struct file_operations kvm_gmem_fops = { .mmap = kvm_gmem_mmap, + .llseek = default_llseek, + .write_iter = generic_perform_write, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate,virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 399
Quoted 8 lines
virt/kvm/guest_memfd.c +5
-0@@ -390,6 +392,49 @@ void kvm_gmem_init(struct module *module) kvm_gmem_fops.owner = module; } +static int kvm_kmem_gmem_write_begin (const struct kiocb *kiocb, +struct address_space *mapping, +loff_t pos,unsigned int len, +struct folio **foliop, +void **fsdata)Over-aggressive wrapping, this can be
static int kvm_kmem_gmem_write_begin(const struct kiocb *kiocb,
structaddress_space*mapping,loff_tpos, unsigned int len, struct folio **folio, void **fsdata)
Quoted 8 lines
or
static int kvm_kmem_gmem_write_begin(const struct kiocb *kiocb,
structaddress_space*mapping,loff_tpos, unsigned int len, struct folio **folio, void **fsdata)if we want to bundle pos+len.
Ack.
Quoted 6 lines
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 401
Quoted 3 lines
virt/kvm/guest_memfd.c +2
-0+{ +struct file *file = kiocb->ki_filp;
ki_filpis already a file, and even if it were a "void *", there's no need for a local variable.
Ack.
Quoted 10 lines
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 406
Quoted 6 lines
virt/kvm/guest_memfd.c +5
-0+struct inode *inode =file_inode (file); +pgoff_t index = pos >>PAGE_SHIFT ; +struct folio *folio; + +if (!kvm_gmem_supports_mmap (inode))Checking for MMAP is neither sufficient nor strictly necessary. MMAP doesn't imply SHARED, and it's not clear to me that
mmap()support should be in any way tied to WRITE support.
As in my reply to the comment about doc, I plan to introduce KVM_CAP_GUEST_MEMFD_WRITE and GUEST_MEMFD_FLAG_WRITE. The kvm_arch_supports_gmem_write() will be a weak symbol and relying on !kvm_arch_has_private_mem() on x86, similar to kvm_arch_supports_gmem_mmap(). Does it look right?
Quoted 10 lines
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 412
Quoted 7 lines
virt/kvm/guest_memfd.c +6
-0+return -ENODEV ; + +if (pos + len >i_size_read (inode)) +return -EINVAL ; + + folio =kvm_gmem_get_folio (inode, index);Eh, since "index" is only used once, my vote is to use "pos" and do the shift here, so that it's obvious that the input to
kvm_gmem_get_folio()is being checked.
Ack.
Quoted 5 lines
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 414
Quoted 3 lines
virt/kvm/guest_memfd.c +2
-0+if (IS_ERR (folio)) +return -EFAULT ;Why EFAULT?
Will propagate the error like you suggest below.
Quoted 13 lines
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 416
Quoted 3 lines
virt/kvm/guest_memfd.c +2
-0+ + *foliop = folio;There shouldn't be any need for a local "folio". What about having the "out" param be just "folio"?
E.g.
static int kvm_kmem_gmem_write_begin(const struct kiocb *kiocb,
structaddress_space*mapping,loff_tpos, unsigned int len, struct folio **folio, void **fsdata)
Quoted 15 lines
{
struct inode *inode = file_inode(kiocb->ki_filp);
if (!kvm_gmem_supports_write(inode))
return -ENODEV;
if (pos + len > i_size_read(inode))
return -EINVAL;
*folio = kvm_gmem_get_folio(inode, pos >> PAGE_SHIFT);
if (IS_ERR(*folio))
return PTR_ERR(*folio);
return 0;
}Ack.
Quoted 16 lines
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 426
Quoted 12 lines
virt/kvm/guest_memfd.c +10
-0+return 0 ; +} + +static int kvm_kmem_gmem_write_end (const struct kiocb *kiocb, +struct address_space *mapping, +loff_t pos,unsigned int len, +unsigned int copied, +struct folio *folio,void *fsdata) +{ +if (copied && copied < len) {Why check if "copied" is non-zero? I don't see why KVM should behave differently with respect to unwritten bytes if
copy_folio_from_iter_atomic()fails on the first byte or the Nth byte.
No, I don't think there is a need for this check indeed. It looks like a leftover from my previous changes.
Quoted 5 lines
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 427
Quoted 2 lines
virt/kvm/guest_memfd.c +1
-0+unsigned int from = pos & ((1UL <<folio_order (folio)) -1 );Uh, isn't this just
offset_in_folio()?
Quoted 9 lines
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 429
Quoted 2 lines
virt/kvm/guest_memfd.c +2
-0+ +folio_zero_range (folio, from + copied, len - copied);I'd probably be in favor of omitting "from" entirely, e.g.
if (copied < len)
folio_zero_range(folio,offset_in_folio(pos)+ copied,
len - copied);
Ack.
Quoted 7 lines
Quoted 7 lines
virt/kvm/guest_memfd.c +7
-0+ } + +folio_unlock (folio); +folio_put (folio); + +return copied; +}
Sean Christopherson
maintainerRe: [PATCH v6 1/2] KVM: guest_memfd: add generic population via write
Quoted 20 lines · Nikita Kalyazin
Quoted 2 lines · Sean Christopherson
Quoted 1 line · Nikita Kalyazin
From: Nikita Kalyazin <[email protected]>
+ Vishal and Ackerley
Quoted 11 lines
Quoted 7 lines
write syscall populates
guest_memfdwith user-supplied data in a generic way, ie no vendor-specific preparation is performed. If the request is not page-aligned, the remaining bytes are initialised to 0.write is only supported for non-CoCo setups where guest memory is not hardware-encrypted.
Please include all of the "why". The code mostly communicates the "what", but it doesn't capture why
write()support is at all interesting, nor does it explain whyread()isn't supported.
Quoted 6 lines
Hi Sean,
Thanks for the review.
Do you think including the explanation from the cover letter would be sufficient?
It's pretty close. A few more details would be helpful, e.g. to explain that VMMs may use write() to populate the initial image
Quoted 2 lines
Shall I additionally say that
read()isn't supported because there is no use case for it as of now or would it be obvious?
Hmm, I think if you want to exclude read() support, the changelog should explicitly state why. E.g. "there's no use case" is quite different from "deliberately don't support read() for security reasons".
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
Signed-off-by Nikitia Kalyazin <[email protected]>--- virt/kvm/guest_memfd.c | 48 ++++++++++++++++++++++++++++++++++++++++++There's a notable lack of uAPI and Documentation chanegs. I.e. this needs a
GUEST_MEMFD_FLAG_xxxalong with proper documentation.Would the following be ok in the doc?
When the capability
KVM_CAP_GUEST_MEMFD_WRITEis supported, the 'flags'
No capability is necessary, see d2042d8f96dd ("KVM: Rework KVM_CAP_GUEST_MEMFD_MMAP into KVM_CAP_GUEST_MEMFD_FLAGS").
Quoted 5 lines
field
supportsGUEST_MEMFD_FLAG_WRITE. Setting this flag onguest_memfdcreation enableswrite()syscall operations to populateguest_memfdmemory from host userspace.
Quoted 7 lines
When a
write()operation is performed on aguest_memfdfile descriptor with theGUEST_MEMFD_FLAG_WRITEset, the syscall will populate the guest memory with user-supplied data in a generic way, without any vendor-specific preparation.
The write operation is only supported for non-CoCo (Confidential Computing) setups where guest memory is not hardware-encrypted.
The restriction should be that guest memory must be SHARED, i.e. not PRIVATE. Strictly speaking, guest memory can be encrypted, e.g. with SME and TME (I think TME is still a thing?), but with a shared key and thus accessible from the host.
Even if that weren't the case, we want to support this for CoCo VMs.
Quoted 2 lines
If the write request is not page-aligned, any remaining bytes within the page are initialized to zero.
Why? (Honest question, e.g. is that standard file semantics?)
Quoted 11 lines
Quoted 4 lines
And while it's definitely it's a-ok to land .write() in advance of the direct map changes, we do need to at least map out how we want the two to interact, e.g. so that we don't end up with constraints that are impossible to satisfy.
write()shall not attempt to access a page that is not in the direct map, which I believe can be achieved viakvm_kmem_gmem_write_begin()consulting theKVM_GMEM_FOLIO_NO_DIRECT_MAPinfolio->private(introduced in [1]).Do you think we should mention it in the commit message in some way? What particular constraint are you cautious about?
I want to be cautious with respect to the ABI/uAPI. Patrick's series also adds a flag, and guest_memfd doesn't currently provide a way to toggle flags after the file is created. That begs the question of how GUEST_MEMFD_FLAG_NO_DIRECT_MAP will co-exist with GUEST_MEMFD_FLAG_WRITE. Presumably the goal is to use write() to initialize memory, and _then_ nuke the direct map.
I want line of sight to understanding the exact semantics/flows. E.g. will KVM require userspace to clear GUEST_MEMFD_FLAG_WRITE before allowing NO_DIRECT_MAP? Or will the write() simply fail? How will the sequencing be achieved?
Quoted 10 lines
Quoted 9 lines
virt/kvm/guest_memfd.c void kvm_gmem_init(struct module *module) · line 406
Quoted 5 lines
virt/kvm/guest_memfd.c +5
-0+struct inode *inode =file_inode (file); +pgoff_t index = pos >>PAGE_SHIFT ; +struct folio *folio; + +if (!kvm_gmem_supports_mmap (inode))Checking for MMAP is neither sufficient nor strictly necessary. MMAP doesn't imply SHARED, and it's not clear to me that
mmap()support should be in any way tied to WRITE support.
Quoted 5 lines
As in my reply to the comment about doc, I plan to introduce
KVM_CAP_GUEST_MEMFD_WRITEandGUEST_MEMFD_FLAG_WRITE. Thekvm_arch_supports_gmem_write()will be a weak symbol and relying on !kvm_arch_has_private_mem() on x86, similar tokvm_arch_supports_gmem_mmap(). Does it look right?
No. As above, write() should be allowed iff memory is SHARED. Relevant commits that are now in Linus' tree:
44c6cb9fe9888b371e31165b2854bd0f4e2787d4 KVM: guest_memfd: Allow mmap() on guest_memfd for x86 VMs with private memory 9aef71c892a55e004419923ba7129abe3e58d9f1 KVM: Explicitly mark KVM_GUEST_MEMFD as depending on KVM_GENERIC_MMU_NOTIFIER 5d3341d684be80892d8f6f9812f90f9274b81177 KVM: guest_memfd: Invalidate SHARED GPAs if gmem supports INIT_SHARED
Re: [PATCH v6 1/2] KVM: guest_memfd: add generic population via write
Quoted 23 lines · Sean Christopherson
Quoted 22 lines · Nikita Kalyazin
Quoted 2 lines · Sean Christopherson
Quoted 1 line · Nikita Kalyazin
From: Nikita Kalyazin <[email protected]>
+ Vishal and Ackerley
Quoted 11 lines
Quoted 7 lines
write syscall populates
guest_memfdwith user-supplied data in a generic way, ie no vendor-specific preparation is performed. If the request is not page-aligned, the remaining bytes are initialised to 0.write is only supported for non-CoCo setups where guest memory is not hardware-encrypted.
Please include all of the "why". The code mostly communicates the "what", but it doesn't capture why
write()support is at all interesting, nor does it explain whyread()isn't supported.Hi Sean,
Quoted 7 lines
Quoted 4 lines
Thanks for the review.
Do you think including the explanation from the cover letter would be sufficient?
It's pretty close. A few more details would be helpful, e.g. to explain that VMMs may use
write()to populate the initial image
Ack.
Quoted 7 lines
Quoted 3 lines
Shall I additionally say that
read()isn't supported because there is no use case for it as of now or would it be obvious?Hmm, I think if you want to exclude
read()support, the changelog should explicitly state why. E.g. "there's no use case" is quite different from "deliberately don't supportread()for security reasons".
Ack.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Signed-off-by Nikitia Kalyazin <[email protected]>--- virt/kvm/guest_memfd.c | 48 ++++++++++++++++++++++++++++++++++++++++++There's a notable lack of uAPI and Documentation chanegs. I.e. this needs a
GUEST_MEMFD_FLAG_xxxalong with proper documentation.
Quoted 6 lines
Quoted 3 lines
Would the following be ok in the doc?
When the capability
KVM_CAP_GUEST_MEMFD_WRITEis supported, the 'flags'No capability is necessary, see
d2042d8f96dd("KVM: ReworkKVM_CAP_GUEST_MEMFD_MMAPintoKVM_CAP_GUEST_MEMFD_FLAGS").
Thanks, I didn't realise that kvm/next was behind kvm/master.
Quoted 14 lines
Quoted 6 lines
field
supportsGUEST_MEMFD_FLAG_WRITE. Setting this flag onguest_memfdcreation enableswrite()syscall operations to populateguest_memfdmemory from host userspace.Quoted 7 lines
When a
write()operation is performed on aguest_memfdfile descriptor with theGUEST_MEMFD_FLAG_WRITEset, the syscall will populate the guest memory with user-supplied data in a generic way, without any vendor-specific preparation.
The write operation is only supported for non-CoCo (Confidential Computing) setups where guest memory is not hardware-encrypted.
Quoted 5 lines
The restriction should be that guest memory must be SHARED, i.e. not PRIVATE. Strictly speaking, guest memory can be encrypted, e.g. with SME and TME (I think TME is still a thing?), but with a shared key and thus accessible from the host.
Even if that weren't the case, we want to support this for CoCo VMs.
To clarify, should it depend on GUEST_MEMFD_FLAG_INIT_SHARED for now?
include/linux/kvm_host.h +1 -1
@@ -736,7 +736,7 @@ static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm)u64 flags =GUEST_MEMFD_FLAG_MMAP ;if (!kvm ||kvm_arch_supports_gmem_init_shared (kvm)) - flags |=GUEST_MEMFD_FLAG_INIT_SHARED ; + flags |=GUEST_MEMFD_FLAG_INIT_SHARED |GUEST_MEMFD_FLAG_WRITE ;return flags; }
Quoted 5 lines
Quoted 3 lines
If the write request is not page-aligned, any remaining bytes within the page are initialized to zero.
Why? (Honest question, e.g. is that standard file semantics?)
The clause was originally suggested by James in v5 [1]. The behaviour shouldn't be deviating from the standard semantics though, so I will omit it. Moreover, when looking at the shmem implementation, I realised that I hadn't handled the case of clearing bytes _before_ written bytes properly. I will fix it in the next version.
[1] https://lore.kernel.org/kvm/CADrL8HUObfEd80sr783dB3dPWGSX7H5=0HCp9OjiL6D_Sp+2Ww@mail.gmail.com/
Quoted 19 lines
Quoted 6 lines
Quoted 5 lines
And while it's definitely it's a-ok to land .write() in advance of the direct map changes, we do need to at least map out how we want the two to interact, e.g. so that we don't end up with constraints that are impossible to satisfy.
Quoted 6 lines
write()shall not attempt to access a page that is not in the direct map, which I believe can be achieved viakvm_kmem_gmem_write_begin()consulting theKVM_GMEM_FOLIO_NO_DIRECT_MAPinfolio->private(introduced in [1]).Do you think we should mention it in the commit message in some way? What particular constraint are you cautious about?
I want to be cautious with respect to the ABI/uAPI. Patrick's series also adds a flag, and
guest_memfddoesn't currently provide a way to toggle flags after the file is created. That begs the question of howGUEST_MEMFD_FLAG_NO_DIRECT_MAPwill co-exist withGUEST_MEMFD_FLAG_WRITE. Presumably the goal is to usewrite()to initialize memory, and _then_ nuke the direct map.
Quoted 4 lines
I want line of sight to understanding the exact semantics/flows. E.g. will KVM require userspace to clear
GUEST_MEMFD_FLAG_WRITEbefore allowingNO_DIRECT_MAP? Or will thewrite()simply fail? How will the sequencing be achieved?
No, I don't think we can clear the GUEST_MEMFD_FLAG_WRITE as we expect faults and writes to different pages to be arriving interspersed: some pages will be populated by write() proactively, some will be allocated by faults in the user mapping on demand. Both write() and the fault handler, if they need to allocate a page, will be writing content to it and "sealing" by removing it from the direct map. If write() faces an already "sealed" page, it will fail (with EEXIST [1]).
Quoted 17 lines
Quoted 11 lines
Quoted 10 lines
Review
Quoted 6 lines
(quoted diff) +5
-0+ struct inode *inode = file_inode(file); + pgoff_t index = pos >> PAGE_SHIFT; + struct folio *folio; + + if (!kvm_gmem_supports_mmap(inode))Checking for MMAP is neither sufficient nor strictly necessary. MMAP doesn't imply SHARED, and it's not clear to me that
mmap()support should be in any way tied to WRITE support.Quoted 5 lines
As in my reply to the comment about doc, I plan to introduce
KVM_CAP_GUEST_MEMFD_WRITEandGUEST_MEMFD_FLAG_WRITE. Thekvm_arch_supports_gmem_write()will be a weak symbol and relying on !kvm_arch_has_private_mem() on x86, similar tokvm_arch_supports_gmem_mmap(). Does it look right?
Quoted 6 lines
No. As above,
write()should be allowed iff memory is SHARED. Relevant commits that are now in Linus' tree:
44c6cb9fe9888b371e31165b2854bd0f4e2787d4KVM:guest_memfd: Allowmmap()onguest_memfdfor x86 VMs with private memory9aef71c892a55e004419923ba7129abe3e58d9f1KVM: Explicitly markKVM_GUEST_MEMFDas depending onKVM_GENERIC_MMU_NOTIFIER5d3341d684be80892d8f6f9812f90f9274b81177KVM:guest_memfd: Invalidate SHARED GPAs if gmem supportsINIT_SHARED
Ack.
Re: [PATCH v6 1/2] KVM: guest_memfd: add generic population via write
Quoted 97 lines · Nikita Kalyazin
Quoted 23 lines · Sean Christopherson
Quoted 22 lines · Nikita Kalyazin
Quoted 2 lines · Sean Christopherson
Quoted 1 line · Nikita Kalyazin
From: Nikita Kalyazin <[email protected]>
+ Vishal and Ackerley
Quoted 11 lines
Quoted 7 lines
write syscall populates
guest_memfdwith user-supplied data in a generic way, ie no vendor-specific preparation is performed. If the request is not page-aligned, the remaining bytes are initialised to 0.write is only supported for non-CoCo setups where guest memory is not hardware-encrypted.
Please include all of the "why". The code mostly communicates the "what", but it doesn't capture why
write()support is at all interesting, nor does it explain whyread()isn't supported.Hi Sean,
Quoted 7 lines
Quoted 4 lines
Thanks for the review.
Do you think including the explanation from the cover letter would be sufficient?
It's pretty close. A few more details would be helpful, e.g. to explain that VMMs may use
write()to populate the initial imageAck.
Quoted 7 lines
Quoted 3 lines
Shall I additionally say that
read()isn't supported because there is no use case for it as of now or would it be obvious?Hmm, I think if you want to exclude
read()support, the changelog should explicitly state why. E.g. "there's no use case" is quite different from "deliberately don't supportread()for security reasons".Ack.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Signed-off-by Nikitia Kalyazin <[email protected]>--- virt/kvm/guest_memfd.c | 48 ++++++++++++++++++++++++++++++++++++++++++There's a notable lack of uAPI and Documentation chanegs. I.e. this needs a
GUEST_MEMFD_FLAG_xxxalong with proper documentation.Quoted 6 lines
Quoted 3 lines
Would the following be ok in the doc?
When the capability
KVM_CAP_GUEST_MEMFD_WRITEis supported, the 'flags'No capability is necessary, see
d2042d8f96dd("KVM: ReworkKVM_CAP_GUEST_MEMFD_MMAPintoKVM_CAP_GUEST_MEMFD_FLAGS").Thanks, I didn't realise that kvm/next was behind kvm/master.
Quoted 14 lines
Quoted 6 lines
field
supportsGUEST_MEMFD_FLAG_WRITE. Setting this flag onguest_memfdcreation enableswrite()syscall operations to populateguest_memfdmemory from host userspace.Quoted 7 lines
When a
write()operation is performed on aguest_memfdfile descriptor with theGUEST_MEMFD_FLAG_WRITEset, the syscall will populate the guest memory with user-supplied data in a generic way, without any vendor-specific preparation.
The write operation is only supported for non-CoCo (Confidential Computing) setups where guest memory is not hardware-encrypted.Quoted 5 lines
The restriction should be that guest memory must be SHARED, i.e. not PRIVATE. Strictly speaking, guest memory can be encrypted, e.g. with SME and TME (I think TME is still a thing?), but with a shared key and thus accessible from the host.
Even if that weren't the case, we want to support this for CoCo VMs.
To clarify, should it depend on
GUEST_MEMFD_FLAG_INIT_SHAREDfor now?include/linux/kvm_host.h +1
-1@@ -736,7 +736,7 @@ static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm) u64 flags = GUEST_MEMFD_FLAG_MMAP;if (!kvm ||kvm_arch_supports_gmem_init_shared (kvm)) - flags |=GUEST_MEMFD_FLAG_INIT_SHARED ; + flags |=GUEST_MEMFD_FLAG_INIT_SHARED |GUEST_MEMFD_FLAG_WRITE ;return flags;
Quoted 2 lines
}
Yup! It should depend on GUEST_MEMFD_FLAG_INIT_SHARED for now.
When conversion is supported then GUEST_MEMFD_FLAG_WRITE can always be a supported flag, and the shared/private check can then be shifted to .write_begin().
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
If the write request is not page-aligned, any remaining bytes within the page are initialized to zero.
Quoted 8 lines
Quoted 1 line
Why? (Honest question, e.g. is that standard file semantics?)
The clause was originally suggested by James in v5 [1]. The behaviour shouldn't be deviating from the standard semantics though, so I will omit it. Moreover, when looking at the shmem implementation, I realised that I hadn't handled the case of clearing bytes _before_ written bytes properly. I will fix it in the next version.
Was thinking about this a bit more. One way to think about this is that we could have more flexibility: Do we need to zero the parts of the page that were not written to? Maybe the user wanted to write only to byte offsets 10 to 20 within the page, and keep the rest intact? That should still be okay I think, there's no data leak since we're not allowing read().
Looking ahead to conversions on huge pages, I think I prefer being more restrictive though. For 4K pages we don't allow allocations or truncations that are not PAGE_ALIGNED. For huge pages the current stance is to return -EINVAL for allocations/truncations that are not huge page size aligned.
If we allow non-page-aligned writes, handling huge pages could be complicated. I don't see any way for guest_memfd to tell the caller generic_perform_write() function that it can only write to certain parts of a page. This means that if a huge page has mixed shared/private status, guest_memfd would have to split the folio just for generic_perform_write() to not write elsewhere.
Do you have any ideas here? Or maybe we will then use some other .write_iter() function.
My proposal is to impose a restriction that write()s offset/len must be PAGE_ALIGNED, we can check that in .write_begin(). Being more restrictive could be a good starting point that allows us to relax the constraint later. (Unless you already have a use case that requires writing less than a page)
That should also solve the zeroing issue, if the user must always provide full pages worth of data to write. :P
Quoted 11 lines
[1]
https://lore.kernel.org/kvm/CADrL8HUObfEd80sr783dB3dPWGSX7H5=0HCp9OjiL6D_Sp+2Ww@mail.gmail.com/Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
And while it's definitely it's a-ok to land .write() in advance of the direct map changes, we do need to at least map out how we want the two to interact, e.g. so that we don't end up with constraints that are impossible to satisfy.
write()shall not attempt to access a page that is not in the direct map, which I believe can be achieved viakvm_kmem_gmem_write_begin()consulting
Btw why not just kvm_gmem_write_begin() instead of having the additional kmem part in the name?
Quoted 16 lines
Quoted 11 lines
Quoted 4 lines
the
KVM_GMEM_FOLIO_NO_DIRECT_MAPinfolio->private(introduced in [1]).Do you think we should mention it in the commit message in some way? What particular constraint are you cautious about?
I want to be cautious with respect to the ABI/uAPI. Patrick's series also adds a flag, and
guest_memfddoesn't currently provide a way to toggle flags after the file is created. That begs the question of howGUEST_MEMFD_FLAG_NO_DIRECT_MAPwill co-exist withGUEST_MEMFD_FLAG_WRITE. Presumably the goal is to usewrite()to initialize memory, and _then_ nuke the direct map.Quoted 4 lines
I want line of sight to understanding the exact semantics/flows. E.g. will KVM require userspace to clear
GUEST_MEMFD_FLAG_WRITEbefore allowingNO_DIRECT_MAP? Or will thewrite()simply fail? How will the sequencing be achieved?
Quoted 8 lines
No, I don't think we can clear the
GUEST_MEMFD_FLAG_WRITEas we expect faults and writes to different pages to be arriving interspersed: some pages will be populated bywrite()proactively, some will be allocated by faults in the user mapping on demand. Bothwrite()and the fault handler, if they need to allocate a page, will be writing content to it and "sealing" by removing it from the direct map. Ifwrite()faces an already "sealed" page, it will fail (with EEXIST [1]).
IIUC this means a write() after fallocate(), or any unintended access to the memory before write, for a GUEST_MEMFD_FLAG_NO_DIRECT_MAP guest_memfd will necessarily fail.
The required ordering is kind of awkward, but I don't really have any good suggestions.
Quoted 25 lines
Quoted 17 lines
Quoted 11 lines
Quoted 10 lines
Review
Quoted 6 lines
(quoted diff) +5
-0+ struct inode *inode = file_inode(file); + pgoff_t index = pos >> PAGE_SHIFT; + struct folio *folio; + + if (!kvm_gmem_supports_mmap(inode))Checking for MMAP is neither sufficient nor strictly necessary. MMAP doesn't imply SHARED, and it's not clear to me that
mmap()support should be in any way tied to WRITE support.Quoted 5 lines
As in my reply to the comment about doc, I plan to introduce
KVM_CAP_GUEST_MEMFD_WRITEandGUEST_MEMFD_FLAG_WRITE. Thekvm_arch_supports_gmem_write()will be a weak symbol and relying on !kvm_arch_has_private_mem() on x86, similar tokvm_arch_supports_gmem_mmap(). Does it look right?Quoted 6 lines
No. As above,
write()should be allowed iff memory is SHARED. Relevant commits that are now in Linus' tree:
44c6cb9fe9888b371e31165b2854bd0f4e2787d4KVM:guest_memfd: Allowmmap()onguest_memfdfor x86 VMs with private memory9aef71c892a55e004419923ba7129abe3e58d9f1KVM: Explicitly markKVM_GUEST_MEMFDas depending onKVM_GENERIC_MMU_NOTIFIER5d3341d684be80892d8f6f9812f90f9274b81177KVM:guest_memfd: Invalidate SHARED GPAs if gmem supportsINIT_SHAREDAck.
Re: [PATCH v6 1/2] KVM: guest_memfd: add generic population via write
Quoted 104 lines · Ackerley Tng
Quoted 97 lines · Nikita Kalyazin
Quoted 23 lines · Sean Christopherson
Quoted 22 lines · Nikita Kalyazin
Quoted 2 lines · Sean Christopherson
Quoted 1 line · Nikita Kalyazin
From: Nikita Kalyazin <[email protected]>
+ Vishal and Ackerley
Quoted 11 lines
Quoted 7 lines
write syscall populates
guest_memfdwith user-supplied data in a generic way, ie no vendor-specific preparation is performed. If the request is not page-aligned, the remaining bytes are initialised to 0.write is only supported for non-CoCo setups where guest memory is not hardware-encrypted.
Please include all of the "why". The code mostly communicates the "what", but it doesn't capture why
write()support is at all interesting, nor does it explain whyread()isn't supported.Hi Sean,
Quoted 7 lines
Quoted 4 lines
Thanks for the review.
Do you think including the explanation from the cover letter would be sufficient?
It's pretty close. A few more details would be helpful, e.g. to explain that VMMs may use
write()to populate the initial imageAck.
Quoted 7 lines
Quoted 3 lines
Shall I additionally say that
read()isn't supported because there is no use case for it as of now or would it be obvious?Hmm, I think if you want to exclude
read()support, the changelog should explicitly state why. E.g. "there's no use case" is quite different from "deliberately don't supportread()for security reasons".Ack.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Signed-off-by Nikitia Kalyazin <[email protected]>--- virt/kvm/guest_memfd.c | 48 ++++++++++++++++++++++++++++++++++++++++++There's a notable lack of uAPI and Documentation chanegs. I.e. this needs a
GUEST_MEMFD_FLAG_xxxalong with proper documentation.Quoted 6 lines
Quoted 3 lines
Would the following be ok in the doc?
When the capability
KVM_CAP_GUEST_MEMFD_WRITEis supported, the 'flags'No capability is necessary, see
d2042d8f96dd("KVM: ReworkKVM_CAP_GUEST_MEMFD_MMAPintoKVM_CAP_GUEST_MEMFD_FLAGS").Thanks, I didn't realise that kvm/next was behind kvm/master.
Quoted 14 lines
Quoted 6 lines
field
supportsGUEST_MEMFD_FLAG_WRITE. Setting this flag onguest_memfdcreation enableswrite()syscall operations to populateguest_memfdmemory from host userspace.Quoted 7 lines
When a
write()operation is performed on aguest_memfdfile descriptor with theGUEST_MEMFD_FLAG_WRITEset, the syscall will populate the guest memory with user-supplied data in a generic way, without any vendor-specific preparation.
The write operation is only supported for non-CoCo (Confidential Computing) setups where guest memory is not hardware-encrypted.Quoted 5 lines
The restriction should be that guest memory must be SHARED, i.e. not PRIVATE. Strictly speaking, guest memory can be encrypted, e.g. with SME and TME (I think TME is still a thing?), but with a shared key and thus accessible from the host.
Even if that weren't the case, we want to support this for CoCo VMs.
To clarify, should it depend on
GUEST_MEMFD_FLAG_INIT_SHAREDfor now?include/linux/kvm_host.h +1
-1@@ -736,7 +736,7 @@ static inline u64 kvm_gmem_get_supported_flags(struct kvm *kvm) u64 flags = GUEST_MEMFD_FLAG_MMAP;if (!kvm ||kvm_arch_supports_gmem_init_shared (kvm)) - flags |=GUEST_MEMFD_FLAG_INIT_SHARED ; + flags |=GUEST_MEMFD_FLAG_INIT_SHARED |GUEST_MEMFD_FLAG_WRITE ;return flags;Quoted 2 lines
}
Yup! It should depend on
GUEST_MEMFD_FLAG_INIT_SHAREDfor now.
Quoted 3 lines
When conversion is supported then
GUEST_MEMFD_FLAG_WRITEcan always be a supported flag, and the shared/private check can then be shifted to .write_begin().
Thanks for the confirmation.
Quoted 36 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
If the write request is not page-aligned, any remaining bytes within the page are initialized to zero.
Quoted 8 lines
Quoted 1 line
Why? (Honest question, e.g. is that standard file semantics?)
The clause was originally suggested by James in v5 [1]. The behaviour shouldn't be deviating from the standard semantics though, so I will omit it. Moreover, when looking at the shmem implementation, I realised that I hadn't handled the case of clearing bytes _before_ written bytes properly. I will fix it in the next version.
Was thinking about this a bit more. One way to think about this is that we could have more flexibility: Do we need to zero the parts of the page that were not written to? Maybe the user wanted to write only to byte offsets 10 to 20 within the page, and keep the rest intact? That should still be okay I think, there's no data leak since we're not allowing
read().Looking ahead to conversions on huge pages, I think I prefer being more restrictive though. For 4K pages we don't allow allocations or truncations that are not
PAGE_ALIGNED. For huge pages the current stance is to return-EINVALfor allocations/truncations that are not huge page size aligned.If we allow non-page-aligned writes, handling huge pages could be complicated. I don't see any way for
guest_memfdto tell the callergeneric_perform_write()function that it can only write to certain parts of a page. This means that if a huge page has mixed shared/private status,guest_memfdwould have to split the folio just forgeneric_perform_write()to not write elsewhere.Do you have any ideas here? Or maybe we will then use some other .write_iter() function.
Quoted 8 lines
My proposal is to impose a restriction that write()s offset/len must be
PAGE_ALIGNED, we can check that in .write_begin(). Being more restrictive could be a good starting point that allows us to relax the constraint later. (Unless you already have a use case that requires writing less than a page)That should also solve the zeroing issue, if the user must always provide full pages worth of data to write. :P
We do not have a use case for partial writes as we always write full pages so I am totally open to applying the restriction, especially if it helps huge page conversion.
Quoted 11 lines
Quoted 11 lines
[1]
https://lore.kernel.org/kvm/CADrL8HUObfEd80sr783dB3dPWGSX7H5=0HCp9OjiL6D_Sp+2Ww@mail.gmail.com/Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
And while it's definitely it's a-ok to land .write() in advance of the direct map changes, we do need to at least map out how we want the two to interact, e.g. so that we don't end up with constraints that are impossible to satisfy.
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
write()shall not attempt to access a page that is not in the direct map, which I believe can be achieved viakvm_kmem_gmem_write_begin()consultingBtw why not just
kvm_gmem_write_begin()instead of having the additional kmem part in the name?
You are right, no reason for an extra kmem in the name. Thanks!
Quoted 26 lines
Quoted 17 lines
Quoted 12 lines
Quoted 5 lines
the
KVM_GMEM_FOLIO_NO_DIRECT_MAPinfolio->private(introduced in [1]).Do you think we should mention it in the commit message in some way? What particular constraint are you cautious about?
I want to be cautious with respect to the ABI/uAPI. Patrick's series also adds a flag, and
guest_memfddoesn't currently provide a way to toggle flags after the file is created. That begs the question of howGUEST_MEMFD_FLAG_NO_DIRECT_MAPwill co-exist withGUEST_MEMFD_FLAG_WRITE. Presumably the goal is to usewrite()to initialize memory, and _then_ nuke the direct map.Quoted 4 lines
I want line of sight to understanding the exact semantics/flows. E.g. will KVM require userspace to clear
GUEST_MEMFD_FLAG_WRITEbefore allowingNO_DIRECT_MAP? Or will thewrite()simply fail? How will the sequencing be achieved?Quoted 8 lines
No, I don't think we can clear the
GUEST_MEMFD_FLAG_WRITEas we expect faults and writes to different pages to be arriving interspersed: some pages will be populated bywrite()proactively, some will be allocated by faults in the user mapping on demand. Bothwrite()and the fault handler, if they need to allocate a page, will be writing content to it and "sealing" by removing it from the direct map. Ifwrite()faces an already "sealed" page, it will fail (with EEXIST [1]).
Quoted 6 lines
IIUC this means a
write()afterfallocate(), or any unintended access to the memory before write, for aGUEST_MEMFD_FLAG_NO_DIRECT_MAPguest_memfdwill necessarily fail.The required ordering is kind of awkward, but I don't really have any good suggestions.
I don't think fallocate() needs to "seal" the page, since it doesn't initialise the page content. So it should be allowed to write to a page that has previously been fallocated, unless there is something I'm missing that makes it impossible.
Quoted 26 lines
Quoted 26 lines
Quoted 18 lines
Quoted 12 lines
Quoted 11 lines
Review
Quoted 7 lines
(quoted diff) +5
-0+ struct inode *inode = file_inode(file); + pgoff_t index = pos >> PAGE_SHIFT; + struct folio *folio; + + if (!kvm_gmem_supports_mmap(inode))Checking for MMAP is neither sufficient nor strictly necessary. MMAP doesn't imply SHARED, and it's not clear to me that
mmap()support should be in any way tied to WRITE support.Quoted 5 lines
As in my reply to the comment about doc, I plan to introduce
KVM_CAP_GUEST_MEMFD_WRITEandGUEST_MEMFD_FLAG_WRITE. Thekvm_arch_supports_gmem_write()will be a weak symbol and relying on !kvm_arch_has_private_mem() on x86, similar tokvm_arch_supports_gmem_mmap(). Does it look right?Quoted 6 lines
No. As above,
write()should be allowed iff memory is SHARED. Relevant commits that are now in Linus' tree:44c6cb9fe9888b371e31165b2854bd0f4e2787d4 KVM: guest_memfd: Allow mmap() on guest_memfd for x86 VMs with private memory 9aef71c892a55e004419923ba7129abe3e58d9f1 KVM: Explicitly mark KVM_GUEST_MEMFD as depending on KVM_GENERIC_MMU_NOTIFIER 5d3341d684be80892d8f6f9812f90f9274b81177 KVM: guest_memfd: Invalidate SHARED GPAs if gmem supports INIT_SHAREDAck.
PATCH v6 2/2 KVM: selftests: update guest_memfd write tests
In reply to Kalyazin, NikitaFrom: Nikita Kalyazin <[email protected]>
This is to reflect that the write syscall is now implemented for guest_memfd.
--- .../testing/selftests/kvm/guest_memfd_test.c | 51 ++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/guest_memfd_test.c +45 -6
@@ -24,18 +24,55 @@#include "test_util.h" #include "ucall_common.h" -static void test_file_read_write (int fd) +static void test_file_read (int fd) {char buf[64 ];TEST_ASSERT (read (fd, buf,sizeof (buf)) <0 ,"read on a guest_mem fd should fail" ); -TEST_ASSERT (write (fd, buf,sizeof (buf)) <0 , -"write on a guest_mem fd should fail" );TEST_ASSERT (pread (fd, buf,sizeof (buf),0 ) <0 ,"pread on a guest_mem fd should fail" ); -TEST_ASSERT (pwrite (fd, buf,sizeof (buf),0 ) <0 , -"pwrite on a guest_mem fd should fail" ); +} + +static void test_write_supported (int fd,size_t total_size) +{ +size_t page_size =getpagesize (); +void *buf =NULL ; +int ret; + + ret =posix_memalign (&buf, page_size, total_size); +TEST_ASSERT_EQ (ret,0 ); + + ret =pwrite (fd, buf, page_size, total_size); +TEST_ASSERT (ret ==-1 ,"writing past the file size on a guest_mem fd should fail" ); +TEST_ASSERT_EQ (errno,EINVAL ); + + ret =pwrite (fd, buf, page_size,0 ); +TEST_ASSERT (ret == page_size,"write on a guest_mem fd should succeed" ); + + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE ,0 , page_size); +TEST_ASSERT (!ret,"fallocate(PUNCH_HOLE) should succeed" ); + +free (buf); +} + +static void test_write_not_supported (int fd,size_t total_size) +{ +size_t page_size =getpagesize (); +void *buf =NULL ; +int ret; + + ret =posix_memalign (&buf, page_size, total_size); +TEST_ASSERT_EQ (ret,0 ); + + ret =pwrite (fd, buf, page_size,0 ); +TEST_ASSERT (ret ==-1 ,"write on guest_mem fd should fail" ); +TEST_ASSERT_EQ (errno,ENODEV ); + + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE ,0 , page_size); +TEST_ASSERT (!ret,"fallocate(PUNCH_HOLE) should succeed" ); + +free (buf); }static void test_mmap_supported (int fd,size_t page_size,size_t total_size)
@@ -281,12 +318,14 @@ static void test_guest_memfd(unsigned long vm_type) fd =vm_create_guest_memfd (vm, total_size, flags); -test_file_read_write (fd); +test_file_read (fd);if (flags &GUEST_MEMFD_FLAG_MMAP ) { +test_write_supported (fd, total_size);test_mmap_supported (fd, page_size, total_size);test_fault_overflow (fd, page_size, total_size); }else { +test_write_not_supported (fd, total_size);test_mmap_not_supported (fd, page_size, total_size); }
Signature · 1 line
2.50.1