NUMA: move numa related code to new file numa.c

Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: check if the total numa memory size is equal to ram_size

If the total number of the assigned numa nodes memory is not
equal to the assigned ram size, it will write the wrong data
to ACPI talb, then the guest will ignore the wrong ACPI table
and recognize all memory to one node. It's buggy, we should
check it to ensure that we write the right data to ACPI table.

Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: Add numa_info structure to contain numa nodes info

Add the numa_info structure to contain the numa nodes memory,
VCPUs information and the future added numa nodes host memory
policies.

Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
Signed-off-by: Andre Przywara <andre.przywara@amd.com>
Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: convert -numa option to use OptsVisitor

Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: introduce NumaMemOptions

Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: add "-numa mem," options

Add "-numa mem," option like following as Paolo suggested:

    -numa mem,nodeid=0,size=1G

This new option will make later coming memory hotplug better.

We will use the new options to specify nodes memory info,
and just remain "-numa node,mem=xx" as legacy.

Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: expand MAX_NODES from 64 to 128

libnuma choosed 128 for MAX_NODES, so we follow libnuma here.

Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: parse guest numa nodes memory policy

The memory policy setting format is like:
    policy={default|membind|interleave|preferred}[,relative=true],host-nodes=N-N
And we are adding this setting as a suboption of "-numa mem,",
the memory policy then can be set like following:
    -numa node,nodeid=0,cpus=0 \
    -numa node,nodeid=1,cpus=1 \
    -numa mem,nodeid=0,size=1G,policy=membind,host-nodes=0-1 \
    -numa mem,nodeid=1,size=1G,policy=interleave,relative=true,host-nodes=1

Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: set guest numa nodes memory policy

Set the guest numa nodes memory policies using the mbind(2)
system call node by node.
After this patch, we are able to set guest nodes memory policies
through the QEMU options, this arms to solve the guest cross
nodes memory access performance issue.
And as you all know, if PCI-passthrough is used,
direct-attached-device uses DMA transfer between device and qemu process.
All pages of the guest will be pinned by get_user_pages().

KVM_ASSIGN_PCI_DEVICE ioctl
  kvm_vm_ioctl_assign_device()
    =>kvm_assign_device()
      => kvm_iommu_map_memslots()
        => kvm_iommu_map_pages()
           => kvm_pin_pages()

So, with direct-attached-device, all guest page's page count will be +1 and
any page migration will not work. AutoNUMA won't too.

So, we should set the guest nodes memory allocation policies before
the pages are really mapped.

Signed-off-by: Andre Przywara <andre.przywara@amd.com>
Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: add qmp command set-mem-policy to set memory policy for NUMA node

This QMP command allows user set guest node's memory policy
through the QMP protocol. The qmp-shell command is like:
    set-mem-policy nodeid=0 policy=membind relative=true host-nodes=0-1

Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: add hmp command set-mem-policy

Add hmp command set-mem-policy to set host memory policy for a guest
NUMA node. Then we can also set node's memory policy using
the monitor command like:
    (qemu) set-mem-policy 0 policy=membind,relative=false,host-nodes=0-1

Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: add qmp command query-numa

Add qmp command query-numa to show guest NUMA information.

Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>
NUMA: convert hmp command info_numa to use qmp command query_numa

Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Wanlong Gao <gaowanlong@cn.fujitsu.com>