Re: kvm memslot questions

Radim Krčmář
2016-12-14T18:49:40Z

2016-12-14 22:43+0800, jack:
> thank you very very much teacher.
> I want to ask 3 questions:
> 1.the way you said can alloc VM memory without any operation in VM,and VM can use it just like a normal system address?

Well, I might have been misinterpreted ...

* KVM can add slots (= guest physical addresses) and if you add a new
memory slot, then the guest is able to use any additional memory
described by it.

But if you just add a new slot, then the guest won't know about it;
you need some way of passing that information into the guest.
Guests usually use ACPI memory hotplug protocols for dynamic physical
memory and you definitely don't want to be doing that from KVM!
For this reason, it it is easier to let the guest choose memory that
is already available in other slots.

Really, don't start by adding memory slots.

* The guest can use physical addresses that are added by memory slots.

The guest should do something (e.g. allocation) to reserve physical
memory to safely work with it, but that is just bookkeeping in the
VM.

> 2.just like what you said,The memory need to join in the management of VM mmu,so vm will not alloc it again.the way you said can do it auto?

Userspace (QEMU) just configures KVM to provide some physical memory to
the guest -- the guest doesn't ask for memory. The guest uses standard
protocols for memory detection on boot/at memory hotplug.
KVM doesn't know or care how the guest uses the memory it was given.

Just let the guest report what memory to use from KVM, it is simpler.

> 3.can you point to me some place to learn the mechanism you said,just kvm source code?

Start from kvm_write_guest(), This function allows KVM to write into
guest physical memory. Read Documentation/virtual/kvm/ and git commit
messages when something isn't clear.

> 4.thank you very much!
>
>
> 来自 魅族 MX5
>
> -------- 原始邮件 --------
> 发件人：Radim Krčmář <rkrcmar@redhat.com>
> 时间：周三 12月14日 22:07
> 收件人："jack.chen" <zhunxun@gmail.com>
> 抄送：KVM maillist <kvm@vger.kernel.org>
> 主题：Re: kvm memslot questions
>
>>2016-12-14 10:50+0800, jack.chen:
>>> thanks teacher!
>>> 1、
>>> I do know little to kvm,the MSR_KVM_STEAL_TIME and
>>> MSR_KVM_SYSTEM_TIME_NEW  seems relevanted to clock,and I find little
>>> information,so can you explain to me about it or give me some
>>> information in your convenience!!thank you very much.
>>
>>What you want to copy from those MSRs is the mechanism to get an address
>>from the guest and write to that address from KVM -- it doesn't matter
>>what they do, but what they use to achieve that.
>>
>>Basically, you can use a paravirtual MSRs that allows the guest to write
>>some configuration and part of that configuration is a memory address.
>>The guest first reserves memory at that address and then passes the
>>address to KVM.  KVM can then write to that memory.
>>
>>KVM MSRs use a simple communication protocol to guarantee that the data
>>read by the guest are consistent even though KVM can overwrite them at
>>any time.  You might want to do something different depending on your
>>application.
>>
>>> 2、
>>> as to the bitmap,in fact I found there are several slots for a vm ,and
>>> there is a main slot,I do not know if it appropriate about my
>>> words.but system address space of vm  which has been mapped to
>>> GPA,always use the settled slot,I printk some information in
>>> kvm_destroy_dirty_bitmap,found that some bitmap did destoryed after VM
>>> started,but the main slot haven't.and I only use the bitmap one
>>> time!so do you think it still  infeasible??
>>
>>Yes, looking at the dirty bitmap doesn't help at all ... you could just
>>randomly assign memory from the slot to get the same result.
>>Dirty bitmap doesn't say that the guest isn't going to use some memory;
>>it just says whether some pages have already been modified.
>>
>>If you pick some memory in KVM, then you need the guest to acknowledge
>>it and at that point, it is simpler to just let the guest pick.
>>
>>> I do not now how to do ,so  I　can only ask you for help,thank you for
>>> your kind help!
>>>
>>> 2016-12-13 22:32 GMT+08:00 Radim Krčmář <rkrcmar@redhat.com>:
>>>> 2016-12-13 09:23+0800, jack.chen:
>>>>> thank you very very much!
>>>>> 1、
>>>>> I want to alloc VM memory in KVM ,not installing anything in VM,and  I
>>>>> want to write data to that memory then let VM use it! So in order to
>>>>> know which memory is not used by VM,I need check the bitmap,but at
>>>>> first,I found the dirty_bitmap of slot I want to use is NULL,so I have
>>>>> to set KVM_MEM_LOG_DIRTY_PAGES flags in KVM matually.
>>>>
>>>> The bitmap doesn't say if the memory is unused and userspace can zero
>>>> the bitmap at any time, so KVM cannot depend on it for this purpose.
>>>> VM uses everything that is configured with KVM_SET_USER_MEMORY_REGION.
>>>>
>>>> A simple way to get unused memory for KVM<->guest communication is to
>>>> let the guest reserve some memory for KVM -- see MSR_KVM_STEAL_TIME or
>>>> MSR_KVM_SYSTEM_TIME_NEW for an example.
>>>> Guest will provide an address and KVM will fill the address with data.
>>>>
>>>>> 2、
>>>>> And I have another question,the function in KVM to set dirty_bitmap call
>>>>> static inline void set_bit_le(int nr, void *addr)
>>>>> {
>>>>> set_bit(nr ^ BITOP_LE_SWIZZLE, addr);
>>>>> }
>>>>>
>>>>> when test the bit,I do not know if I should use BITOP_LE_SWIZZLE,It
>>>>> seems that that right without SWIZZLE.
>>>>
>>>> BITOP_LE_SWIZZLE is 0 on x86 so it doesn't matter -- I assume your code
>>>> isn't going to be useable on different endianess.
>>>> (And cannot use the bitmap for what you are doing anyway. ;])
>>>>
>>>>
>>>>> 2016-12-12 23:17 GMT+08:00 Radim Krčmář <rkrcmar@redhat.com>:
>>>>>> 2016-12-12 17:18+0800, jack.chen:
>>>>>>> hello,now I want to know which GFN of VM  is available,so I set
>>>>>>> kvm_userspace_mem->flags |= 0x1; in kvm_vm_ioctl function ,so KVM can
>>>>>>> create dirty_bitmap for slots
>>>>>>
>>>>>> The caller of KVM_SET_USER_MEMORY_REGION knows which GFNs are
>>>>>> "available", because it configures that into
>>>>>> kvm_userspace_mem->guest_phys_addr and kvm_userspace_mem->size.
>>>>>>
>>>>>> KVM knows which GFNs are "available", because it remembers all memory
>>>>>> regions that were configured.
>>>>>>
>>>>>>> case KVM_SET_USER_MEMORY_REGION: {
>>>>>>> struct kvm_userspace_memory_region kvm_userspace_mem;
>>>>>>>
>>>>>>> ……
>>>>>>> kvm_userspace_mem->flags |= 0x1;
>>>>>>
>>>>>> Why do you want to use the dirty page bitmap in KVM?
>>>>>>
>>>>>>> r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
>>>>>>> break;
>>>>>>>
>>>>>>> and I have 2 questions:
>>>>>>> 1.
>>>>>>> if corresponding bit is setted,the GFN is used by VM,other than it is
>>>>>>> available!right?
>>>>>>
>>>>>> The GFN is always available.
>>>>>> If the corresponding bit in dirty log is set, then the GFN was modified
>>>>>> since userspace did the last KVM_GET_DIRTY_LOG ioctl.
>>>>>>
>>>>>>> 2、
>>>>>>> how to check wheather the bit is 1 or not,the test_bit function
>>>>>>> returns -1 to me,I do not know if I use wrong function!
>>>>>>> thanks in advance!!!
>>>>>>
>>>>>> test_bit() from arch/x86/include/asm/bitops.h returns bool so there is
>>>>>> something wrong with your test_bit().
>>>>>>
>>>>>> In any case, see kvm