Fuad Tabba <tabba@xxxxxxxxxx> writes: > Not all use cases require guest_memfd() to be shared with the host when > first created. Add a new flag, GUEST_MEMFD_FLAG_INIT_SHARED, which when > set on KVM_CREATE_GUEST_MEMFD initializes the memory as shared with the > host, and therefore mappable by it. Otherwise, memory is private until > explicitly shared by the guest with the host. > > Signed-off-by: Fuad Tabba <tabba@xxxxxxxxxx> > --- > Documentation/virt/kvm/api.rst | 4 ++++ > include/uapi/linux/kvm.h | 1 + > tools/testing/selftests/kvm/guest_memfd_test.c | 7 +++++-- > virt/kvm/guest_memfd.c | 12 ++++++++++++ > 4 files changed, 22 insertions(+), 2 deletions(-) > > <snip> > > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c > index eec9d5e09f09..32e149478b04 100644 > --- a/virt/kvm/guest_memfd.c > +++ b/virt/kvm/guest_memfd.c > @@ -1069,6 +1069,15 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) > goto err_gmem; > } > > + if (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) && > + (flags & GUEST_MEMFD_FLAG_INIT_SHARED)) { > + err = kvm_gmem_offset_range_set_shared(file_inode(file), 0, size >> PAGE_SHIFT); I think if GUEST_MEMFD_FLAG_INIT_SHARED is not set, we should call kvm_gmem_offset_range_clear_shared(); so that there is always some shareability defined for all offsets in a file. Otherwise, when reading shareability, we'd have to check against GUEST_MEMFD_FLAG_INIT_SHARED to find out what to initialize it to. > + if (err) { > + fput(file); > + goto err_gmem; > + } > + } > + > kvm_get_kvm(kvm); > gmem->kvm = kvm; > xa_init(&gmem->bindings); > @@ -1090,6 +1099,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) > u64 flags = args->flags; > u64 valid_flags = 0; > > + if (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)) > + valid_flags |= GUEST_MEMFD_FLAG_INIT_SHARED; > + > if (flags & ~valid_flags) > return -EINVAL;