On Mon, Jul 31, 2023 at 12:21:57PM -0400, Xiaoyao Li <xiaoyao.li@xxxxxxxxx> wrote: > From: Chao Peng <chao.p.peng@xxxxxxxxxxxxxxx> > > Currently only KVM_MEMORY_EXIT_FLAG_PRIVATE in flags is valid when > KVM_EXIT_MEMORY_FAULT happens. It indicates userspace needs to do > the memory conversion on the RAMBlock to turn the memory into desired > attribute, i.e., private/shared. > > Note, KVM_EXIT_MEMORY_FAULT makes sense only when the RAMBlock has > gmem memory backend. > > Signed-off-by: Chao Peng <chao.p.peng@xxxxxxxxxxxxxxx> > Signed-off-by: Xiaoyao Li <xiaoyao.li@xxxxxxxxx> > --- > accel/kvm/kvm-all.c | 52 +++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 52 insertions(+) > > diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c > index f9b5050b8885..72d50b923bf2 100644 > --- a/accel/kvm/kvm-all.c > +++ b/accel/kvm/kvm-all.c > @@ -3040,6 +3040,48 @@ static void kvm_eat_signals(CPUState *cpu) > } while (sigismember(&chkset, SIG_IPI)); > } > > +static int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private) > +{ > + MemoryRegionSection section; > + void *addr; > + RAMBlock *rb; > + ram_addr_t offset; > + int ret = -1; > + > + section = memory_region_find(get_system_memory(), start, size); > + if (!section.mr) { > + return ret; > + } > + > + if (memory_region_can_be_private(section.mr)) { > + if (to_private) { > + ret = kvm_set_memory_attributes_private(start, size); > + } else { > + ret = kvm_set_memory_attributes_shared(start, size); > + } > + > + if (ret) { > + return ret; > + } > + > + addr = memory_region_get_ram_ptr(section.mr) + > + section.offset_within_region; > + rb = qemu_ram_block_from_host(addr, false, &offset); Here we have already section. section.mr->ram_block. We don't have to scan the existing RAMBlocks. Except that, looks good to me. Reviewed-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx> -- Isaku Yamahata <isaku.yamahata@xxxxxxxxx>