Skip to contentSkip to conversation

RFC: arm64: Handling reserved memory nodes

DISCUSSION Leo Yan 3 participants 11 messages Sep 14, 2023 – Sep 26, 2023

lore
Share & tools
lore

Leo Yan

author
#0 lore raw

Hi all,

I'd like to discuss for how to handle the reserved memory nodes in DT binding on Xen / Arm64. Note, now I am using DTB when boot Xen but not UEFI/ACPI (ACPI is disabled in this case).

## Failure

I ported Xen on a platform, after the kernel booting, the Xen hypervisor reports error:

(XEN) arch/arm/p2m.c:2202: d0v0: Failing to acquire the MFN 0x1a02dc

This error is caused by kernel using an invalid memory frame number 0x1a02dc, we can convert it to the address:

0x1a02dc << PAGE_SHIFT = 0x1_a02d_c000

## Reason

Two important things we need to check. One is what's the DT binding passed from the bootloader to Xen hypervisor, and the second thing is what's the DT binding passed from Xen hypervisor to kernel.

We can see the bootloader passes below memory nodes to Xen hypervisor:

(XEN) RAM: 0000000020000000 - 00000000bfffffff
(XEN) RAM: 00000001a0000000 - 00000002ffffffff
(XEN)
(XEN) MODULE[0]: 0000000020100000 - 0000000020265000 Xen (XEN) MODULE[1]: 0000000023000000 - 0000000023024000 Device Tree (XEN) MODULE[2]: 0000000024000000 - 0000000028000000 Kernel

  (XEN)  RESVD[0]: 0000000020000000 - 000000002000ffff
  (XEN)  RESVD[1]: 0000000040000000 - 000000005fffffff
  (XEN)  RESVD[2]: 00000001a0000000 - 00000001bfffffff
  (XEN)  RESVD[3]: 000000002e000000 - 000000002fffffff

We can see the second DDR section is:

[0x0000_0001_a000_0000 .. 0x0000_0002_ffff_ffff]

And there have reserved memory section is:

[0x0000_0001_a000_0000 .. 0x0000_0001_bfff_ffff]

When register the boot memory sections, dt_unreserved_regions() will remove all reserved memory sections, which means the section [0x0000_0001_a000_0000 .. 0x0000_0001_bfff_ffff] is not managed by Xen hypervisor at all. If later kernel uses any pages in this range, Xen will report the error.

But Dom0 kernel's memory nodes are:

  [    0.000000] Early memory node ranges
  [    0.000000]   node   0: [mem 0x0000000020000000-0x000000002000ffff]
  [    0.000000]   node   0: [mem 0x000000002e000000-0x000000002fffffff]
  [    0.000000]   node   0: [mem 0x0000000040000000-0x000000005fffffff]
  [    0.000000]   node   0: [mem 0x0000000060000000-0x0000000077bfffff]
  [    0.000000]   node   0: [mem 0x00000001a0000000-0x00000001bfffffff]

Based on this log, we can know Xen hypervisor passes the reserved memory regions to Dom0 kernel, and when Dom0 kernel tries to allocate pages from these reserved memory regions, Xen hypervisor reports error.

For more specific, this issue is cause by the commit 248faa637d ("xen/arm:
add reserved-memory regions to the dom0 memory node"). When Xen hypervisor synthesizes DT nodes, it calls below function to generate _normal_ memory nodes for the reserved memory regions.

make_memory_node()

  {
     /*
      * Create a second memory node to store the ranges covering
      * reserved-memory regions.
      */
     res = make_memory_node(d, kinfo->fdt, addrcells, sizecells,
                            &bootinfo.reserved_mem);
     if ( res )
         return res;

     ...

}

## Fixes

I think it's wrong to add the reserved memory regions into the DT binding as normal memory nodes for Dom0 kernel. On the other hand, we cannot simply remove these reserved memory regions and don't pass to Dom0 kernel - we might reserve memory for specific purpose, for example, ramoops [1] for kernel debugging.

The right thing to do is to keep these reserved memory nodes to Dom0 kernel. So one task is to record properties for these reserved memory node name and properties and pass to Dom0 kernel.

The difficulty is how we can avoid allocate these reserved memory regions in Xen hypervisor. We cannot register the reserved memory into the boot pages, otherwise, the reserved memory might be allocated in the early phase. But we need to register these pages into the frame management framework and reserve them in the first place, so that we can allow Dom0 kernel to use them. (I checked a bit the static memory mechanism, seems to me we cannot use it to resolve this issue).

Before I proceed, I'd like to check if Xen community has related discussion or not? Or any suggestions or input will be appreciate!

Leo

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/boot/dts/hisilicon/hi6220-hikey.dts#n54

#1 lore raw
Quoted 1 line · Leo Yan

Hi all,

Hi Leo,

Quoted 9 lines

I'd like to discuss for how to handle the reserved memory nodes in DT binding on Xen / Arm64. Note, now I am using DTB when boot Xen but not UEFI/ACPI (ACPI is disabled in this case).

## Failure

I ported Xen on a platform, after the kernel booting, the Xen hypervisor reports error:

Quoted 6 lines

(XEN) arch/arm/p2m.c:2202: d0v0: Failing to acquire the MFN 0x1a02dc

This error is caused by kernel using an invalid memory frame number 0x1a02dc, we can convert it to the address:

   0x1a02dc << PAGE_SHIFT = 0x1_a02d_c000

This error is coming from get_page_from_gva(). The use of the function is usually an indication that Xen is trying to access the page. Can you use WARN() to provide a full trace?

Quoted 26 lines

## Reason

Two important things we need to check. One is what's the DT binding passed from the bootloader to Xen hypervisor, and the second thing is what's the DT binding passed from Xen hypervisor to kernel.

We can see the bootloader passes below memory nodes to Xen hypervisor:

   (XEN) RAM: 0000000020000000 - 00000000bfffffff
   (XEN) RAM: 00000001a0000000 - 00000002ffffffff
   (XEN)
   (XEN) MODULE[0]: 0000000020100000 - 0000000020265000 Xen
   (XEN) MODULE[1]: 0000000023000000 - 0000000023024000 Device Tree
   (XEN) MODULE[2]: 0000000024000000 - 0000000028000000 Kernel
   (XEN)  RESVD[0]: 0000000020000000 - 000000002000ffff
   (XEN)  RESVD[1]: 0000000040000000 - 000000005fffffff
   (XEN)  RESVD[2]: 00000001a0000000 - 00000001bfffffff
   (XEN)  RESVD[3]: 000000002e000000 - 000000002fffffff

We can see the second DDR section is:

   [0x0000_0001_a000_0000 .. 0x0000_0002_ffff_ffff]

And there have reserved memory section is:

Quoted 7 lines
   [0x0000_0001_a000_0000 .. 0x0000_0001_bfff_ffff]

When register the boot memory sections, dt_unreserved_regions() will remove all reserved memory sections, which means the section [0x0000_0001_a000_0000 .. 0x0000_0001_bfff_ffff] is not managed by Xen hypervisor at all. If later kernel uses any pages in this range, Xen will report the error.

It is perfectly fine for dom0 to access a page that are not managed by Xen (a good example is MMIO regions are not managed). Now, some problems can occur if the page then needs to be access by Xen (e.g. if they contain hypercall buffers).

[...]

Quoted 12 lines

## Fixes

I think it's wrong to add the reserved memory regions into the DT binding as normal memory nodes for Dom0 kernel. On the other hand, we cannot simply remove these reserved memory regions and don't pass to Dom0 kernel - we might reserve memory for specific purpose, for example, ramoops [1] for kernel debugging.

The right thing to do is to keep these reserved memory nodes to Dom0 kernel. So one task is to record properties for these reserved memory node name and properties and pass to Dom0 kernel.

Quoted 7 lines

The difficulty is how we can avoid allocate these reserved memory regions in Xen hypervisor. We cannot register the reserved memory into the boot pages, otherwise, the reserved memory might be allocated in the early phase. But we need to register these pages into the frame management framework and reserve them in the first place, so that we can allow Dom0 kernel to use them. (I checked a bit the static memory mechanism, seems to me we cannot use it to resolve this issue).

From my understanding reserved region are normal RAM which have been carved out for specific purpose. They may expect different caching policy (e.g. non-cachable). AFAIK, Xen doesn't have the capability to know the memory attribute (the DT binding only tell whether the region should not mapped. See the property "no-map"), hence why they were excluded from the memory management.

It would be good to understand why Xen may try to get a reference on the page.

Also, can you find the associated reserved-region and content of the Device-Tree?

Cheers,

Signature · 1 line
Julien Grall
author
#2 lore raw

Hi Julien,

On Thu, Sep 14, 2023 at 10:37:05AM +0100, Julien Grall wrote:

[...]

Quoted 8 lines
Quoted 4 lines

This error is caused by kernel using an invalid memory frame number 0x1a02dc, we can convert it to the address:

   0x1a02dc << PAGE_SHIFT = 0x1_a02d_c000

This error is coming from get_page_from_gva(). The use of the function is usually an indication that Xen is trying to access the page. Can you use WARN() to provide a full trace?

With adding WARN(), I can get below trace log:

(XEN) arch/arm/p2m.c:2206: d0v0: Failing to acquire the MFN 0x1a0171 (XEN) Xen WARN at arch/arm/p2m.c:2208
(XEN) ----[ Xen-4.18-unstable arm64 debug=y Tainted: S ]----

  (XEN) CPU:    0
  (XEN) PC:     00007f000027540c get_page_from_gva+0x30c/0x334
  (XEN) LR:     00007f000027540c
  (XEN) SP:     00008002ffee7a20
  (XEN) CPSR:   0000000080000249 MODE:64-bit EL2h (Hypervisor, handler)
  (XEN)      X0: 00007f0000318038  X1: 0000000000000000  X2: 0000000000000000
  (XEN)      X3: 0000000000000001  X4: 00007f00002bf956  X5: 0000000000000000
  (XEN)      X6: 00008002ffff2150  X7: 00008002ffff2150  X8: 7f7f7f7f7f7f7f7f
  (XEN)      X9: 0000000000000080 X10: 7f7f7f7f7f7f7f7f X11: 0101010101010101
  (XEN)     X12: 0000000000000008 X13: 0000000000000030 X14: 0000000000000000
  (XEN)     X15: 000000003be6b29d X16: 000000000000000c X17: 000000009c4542e5
  (XEN)     X18: 0000000000000014 X19: 00008002fffcd000 X20: 00008002ffeef000
  (XEN)     X21: 00007f08054050b8 X22: 0000000000000000 X23: 00000000001a0171
  (XEN)     X24: 00000001a0171d80 X25: 0000000000000004 X26: 0000000000000000
  (XEN)     X27: 00008002ffeef000 X28: 0000000000001000  FP: 00008002ffee7a20
  (XEN) 
  (XEN)   VTCR_EL2: 00000000800a3558
  (XEN)  VTTBR_EL2: 00010002fffca000
  (XEN) 
  (XEN)  SCTLR_EL2: 0000000030cd183d
  (XEN)    HCR_EL2: 00000000807c663f
  (XEN)  TTBR0_EL2: 0000000020251000
  (XEN) 
  (XEN)    ESR_EL2: 00000000f2000001
  (XEN)  HPFAR_EL2: 0000000000176800
  (XEN)    FAR_EL2: ffffffc011900000
  (XEN) 
  (XEN) Xen stack trace from sp=00008002ffee7a20:
  (XEN)    00008002ffee7a80 00007f000026cc44 0000000000000d80 0000000000000018
  (XEN)    0000000000000007 0000000000000018 00008002ffee7cf8 ffffffc01187bd80
  (XEN)    00008002ffee7ad0 00000000ffffffc8 00008002ffee7ab0 00007f000032211e
  (XEN)    00008002ffee7b00 00007f000026ce54 ffffffffffffffdf ffffffc01187bd80
  (XEN)    0000000000000007 0000000000000000 00008002fffcd000 ffffffc0117cc000
  (XEN)    00000001a01db000 0000000000000000 0000000000000000 0000000000000000
  (XEN)    0000000000000007 00007f00002b0018 00008002ffeefe16 0000000000000000
  (XEN)    00008002ffee7b10 00007f000021ab3c 00008002ffee7e40 00007f0000277c84
  (XEN)    00008002ffee7ea0 000000005a000ea1 00008002ffee7f20 00008002ffeef000
  (XEN)    000000005a000ea1 ffffffc0117cc000 00007f00003190a4 00007f0000319080
  (XEN)    000000000000001b 00008002ffee7c20 00008002ffee7b90 00007f000026e27c
  (XEN)    0000000000000000 00007f000026e384 00008002ffee7bd0 00007f0000269f88
  (XEN)    000000000000001b 00007f0000329000 00008002ffee7c20 0000000000000000
  (XEN)    0000000000001fff 0000000080000249 00008002ffee7c10 00007f00002797cc
  (XEN)    0000000000000010 000000005a000ea1 00008002ffee7d28 0000000080000249
  (XEN)    000000005a000ea1 ffffffc011603ba8 00008002ffee7d40 00007f000026232c
  (XEN)    00008002ffee7c50 00007f0000270a68 00008002fffcd000 00008002ffeef000
  (XEN)    00007f0800e4d0a8 00008002ffeef000 00008002ffee7c70 00007f0000223f48
  (XEN)    00008002ffee7c90 00007f0000224264 00007f00002bfa20 00008002ffee7dca
  (XEN)    0000000000000000 00008002ffee7dca 00008002ffee7cb0 00007f00002242dc
  (XEN)    00000000000002c0 00007f000024b068 00008002ffee7cd0 00007f000024b0c4
  (XEN)    000000000000000a 00007f000026ce54 00008002ffee7d40 00007f00002484c0
  (XEN)    000000000000000a 000000005a000ea1 00008002ffee7d20 00007f0000224264
  (XEN)    00007f00002bf950 000000005a000ea1 000000000000000a 000000000000000a
  (XEN)    00008002ffee7d40 00007f00002242bc 000000000000000a 00000000000002c0
  (XEN)    00008002ffee7d50 00007f000024946c 00008002ffee7e40 00007f0000277cc0
  (XEN)    00008002ffee7ea0 000000005a000ea1 00008002ffee7f20 00008002ffeef000
  (XEN)    000000005a000ea1 ffffffc011613d40 ffffffc011713878 0000000000000059
  (XEN)    00008002ffee7dc0 00007f0000223f48 00008002ffee7de0 00007f0000224264
  (XEN)    00008002ffee7de0 00007f0000223f48 00008002ffee7e00 00007f0000224264
  (XEN)    00008002ffeefe10 00008002ffeef000 00007f0000318288 0000000000000004
  (XEN)    00008002ffee7e20 00007f00002242dc 0000000000000240 00008002ffeef000
  (XEN)    00008002ffee7e40 00007f000027c484 0000000000000004 0000000000000000
  (XEN)    00008002ffee7e70 00007f0000279400 00008002ffee7ea0 000000005a000ea1
  (XEN)    00008002ffee7fa8 0000000080000005 ffffffc01187bd30 00007f0000262480
  (XEN)    0000000000000000 ffffffc0117d2000 ffffffc01187bd30 00007f0000262474
  (XEN)    0000000000000007 ffffffc01187bd80 00000000001a01db 0000000000000005
  (XEN)    0000000000000005 ffffffc011802880 ffffffc0118028e0 0000000000000018
  (XEN)    0000000020000000 ffffffc010021d1c ffffff81bfcabb90 0000000000000040
  (XEN)    0000000000000000 00000000000003da ffffffffffffffff 000000003be6b29d
  (XEN) Xen call trace:
  (XEN)    [<00007f000027540c>] get_page_from_gva+0x30c/0x334 (PC)
  (XEN)    [<00007f000027540c>] get_page_from_gva+0x30c/0x334 (LR)
  (XEN)    [<00007f000026cc44>] guestcopy.c#copy_guest+0x198/0x2ec
  (XEN)    [<00007f000026ce54>] raw_copy_from_guest+0x20/0x28
  (XEN)    [<00007f000021ab3c>] do_memory_op+0x1104/0x24c0
  (XEN)    [<00007f0000277c84>] traps.c#do_trap_hypercall+0x120/0x288
  (XEN)    [<00007f0000279400>] do_trap_guest_sync+0x448/0x63c
  (XEN)    [<00007f0000262480>] entry.o#guest_sync_slowpath+0xa8/0xd8

(XEN)

I can match the hypervisor trap with the Linux kernel function xen_guest_init() in arch/arm/xen/enlighten.c:

  static int __init xen_guest_init(void)
  {
    struct xen_add_to_physmap xatp;
    ...
    shared_info_page = (struct shared_info *)get_zeroed_page(GFP_KERNEL);

    if (!shared_info_page) {
            pr_err("not enough memory\n");
            return -ENOMEM;
    }
    xatp.domid = DOMID_SELF;
    xatp.idx = 0;
    xatp.space = XENMAPSPACE_shared_info;
    xatp.gpfn = virt_to_gfn(shared_info_page);

    if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
            BUG();
    ...

}

My understanding is the local variable 'xatp' is located in the stack and the stack is located in the intermediate physical address 0x1_a017_1000. Thus, when copies the structure 'xatp' from Dom0 to the Xen hypervisor, the hypervisor detects the IPA is not managed by its frame table.

This is because the memory range [00000001a0000000 - 00000001bfffffff] is the reserved region so Xen hypervisor doesn't populate this region and the frame table doesn't initialize for it. On the other hand, this memory range is passed to Dom0 Kernel as _normal_ memory region and the kernel allocates pages from this memory region, same with other memory regions.

[...]

Quoted 20 lines
Quoted 12 lines

## Fixes

I think it's wrong to add the reserved memory regions into the DT binding as normal memory nodes for Dom0 kernel. On the other hand, we cannot simply remove these reserved memory regions and don't pass to Dom0 kernel - we might reserve memory for specific purpose, for example, ramoops [1] for kernel debugging.

The right thing to do is to keep these reserved memory nodes to Dom0 kernel. So one task is to record properties for these reserved memory node name and properties and pass to Dom0 kernel.

Quoted 7 lines

The difficulty is how we can avoid allocate these reserved memory regions in Xen hypervisor. We cannot register the reserved memory into the boot pages, otherwise, the reserved memory might be allocated in the early phase. But we need to register these pages into the frame management framework and reserve them in the first place, so that we can allow Dom0 kernel to use them. (I checked a bit the static memory mechanism, seems to me we cannot use it to resolve this issue).

Quoted 3 lines

From my understanding reserved region are normal RAM which have been carved out for specific purpose. They may expect different caching policy (e.g. non-cachable).

Yes, I agree, but we cannot assume the mapping attribution is always non-cachable. It can be mapped as normal type, or device type (with and different variants, like strong ordered, write-combined, etc).

Quoted 4 lines

AFAIK, Xen doesn't have the capability to know the memory attribute (the DT binding only tell whether the region should not mapped. See the property "no-map"), hence why they were excluded from the memory management.

I think it's right to exclude the reserved memory regions from the normal memory management.

Here the problem is these reserved memory regions are passed as normal memory nodes to Dom0 kernel, then Dom0 kernel allocates pages from these reserved memory regions. Apparently, this might lead to conflict, e.g. the reserved memory is used by Dom0 kernel, at the meantime the memory is used by another purpose (e.g. by MCU in the system).

Here I am a bit confused for "Xen doesn't have the capability to know the memory attribute". I looked into the file arch/arm/guest_walk.c, IIUC, it walks through the stage 1's page tables for the virtual machine and get the permission for the mapping, we also can get to know the mapping attribute, right?

Another question for the attribute for MMIO regions. For mapping MMIO regions, prepare_dtb_hwdom() sets the attribute 'p2m_mmio_direct_c' for the stage 2, but in the Linux kernel the MMIO's attribute can be one of below variants:

- ioremap(): device type with nGnRE;
- ioremap_np(): device type with nGnRnE (strong-ordered);
- ioremap_wc(): normal non-cachable.

If Xen hypervisor can handle these MMIO types in stage 2, then we should can use the same way to map stage 2 tables for the reserved memory. A difference for the reserved memory is it can be mapped as normal memory with cacheable.

Quoted 2 lines

It would be good to understand why Xen may try to get a reference on the page.

I think the above log can answer this question.

Quoted 2 lines

Also, can you find the associated reserved-region and content of the Device-Tree?

The DT binding is something like (I tweaked a bit for readable):

	memory@20000000 {
		#address-cells = <0x02>;
		#size-cells = <0x02>;
		device_type = "memory";
		reg = <0x00 0x20000000 0x00 0xa0000000>,
                      <0x01 0xa0000000 0x01 0x60000000>;
	};


	reserved-memory {
		#address-cells = <0x02>;
		#size-cells = <0x02>;
		ranges;

		reserved_mem1 {
			reg = <0x00 0x20000000 0x00 0x00010000>;
			no-map;
		};

		reserved_mem2 {
			reg = <0x00 0x40000000 0x00 0x20000000>;
			no-map;
		};

		reserved_mem3 {
			reg = <0x01 0xa0000000 0x00 0x20000000>;
			no-map;
		};

		reserved_mem4 {
			reg = <0x00 0x2e000000 0x00 0x02000000>;
			no-map;
		};
        };

Thanks,
Leo

#3 lore raw
Quoted 1 line · Leo Yan

Hi Julien,

Hi Leo,

Quoted 6 lines · Julien Grall

My understanding is the local variable 'xatp' is located in the stack and the stack is located in the intermediate physical address 0x1_a017_1000. Thus, when copies the structure 'xatp' from Dom0 to the Xen hypervisor, the hypervisor detects the IPA is not managed by its frame table.

Thanks for the log and the analysis (see below).

Quoted 4 lines

This is because the memory range [00000001a0000000 - 00000001bfffffff] is the reserved region so Xen hypervisor doesn't populate this region and the frame table doesn't initialize for it.

I agree with this statement however...

Quoted 4 lines

On the other hand,
this memory range is passed to Dom0 Kernel as _normal_ memory region and the kernel allocates pages from this memory region, same with other memory regions.

... from my understanding reserved-memory are just normal memory that are set aside for a specific purpose. So Xen has to create a 'memory' node *and* a 'reserved-memory' region.

With that the kernel is supposed to exclude all the 'reserved-memory' from normal usage unless they have the node contains the property 'reusable'. This was more clear before the binding was converted to YAML in [1].

Quoted 23 lines

[...]

Quoted 20 lines
Quoted 19 lines

## Fixes

I think it's wrong to add the reserved memory regions into the DT binding as normal memory nodes for Dom0 kernel. On the other hand, we cannot simply remove these reserved memory regions and don't pass to Dom0 kernel - we might reserve memory for specific purpose, for example, ramoops [1] for kernel debugging.

The right thing to do is to keep these reserved memory nodes to Dom0 kernel. So one task is to record properties for these reserved memory node name and properties and pass to Dom0 kernel.

The difficulty is how we can avoid allocate these reserved memory regions in Xen hypervisor. We cannot register the reserved memory into the boot pages, otherwise, the reserved memory might be allocated in the early phase. But we need to register these pages into the frame management framework and reserve them in the first place, so that we can allow Dom0 kernel to use them. (I checked a bit the static memory mechanism, seems to me we cannot use it to resolve this issue).

Quoted 7 lines
Quoted 3 lines

From my understanding reserved region are normal RAM which have been carved
out for specific purpose. They may expect different caching policy (e.g. non-cachable).

Yes, I agree, but we cannot assume the mapping attribution is always non-cachable. It can be mapped as normal type, or device type (with and different variants, like strong ordered, write-combined, etc).

For clarification, I didn't suggest it would always be cachable. It can be anything and I only provided an example (hence the 'e.g.').

Quoted 6 lines
Quoted 5 lines

AFAIK, Xen doesn't have the capability to know the memory attribute (the DT binding only tell whether the region should not mapped. See the property "no-map"), hence why they were excluded from the memory management.

Quoted 8 lines

I think it's right to exclude the reserved memory regions from the normal memory management.

Here the problem is these reserved memory regions are passed as normal memory nodes to Dom0 kernel, then Dom0 kernel allocates pages from these reserved memory regions. Apparently, this might lead to conflict, e.g. the reserved memory is used by Dom0 kernel, at the meantime the memory is used by another purpose (e.g. by MCU in the system).

See above. I think this is correct to pass both 'memory' and 'reserved-memory'. Now, it is possible that Xen may not create the device-tree correctly.

I would suggest to look how Linux is populating the memory and whether it actually skipped the regions.

Quoted 6 lines

Here I am a bit confused for "Xen doesn't have the capability to know the memory attribute". I looked into the file arch/arm/guest_walk.c, IIUC, it walks through the stage 1's page tables for the virtual machine and get the permission for the mapping, we also can get to know the mapping attribute, right?

Most of the time, Xen will use the HW to translate the guest virtual address to an intermediation physical address. Looking at the specification, it looks like that PAR_EL1 will contain the memory attribute which I didn't know.

We would then need to read MAIR_EL1 to find the attribute and also the memory attribute in the stage-2 to figure out the final memory attribute. This is feasible but the Xen ABI mandates that region passed to Xen have a specific memory attributes (see the comment at the top of xen/include/public/arch-arm.h).

Anyway, in your case, Linux is using the buffer is on the stack. So the region must have been mapped with the proper attribute.

Quoted 9 lines

Another question for the attribute for MMIO regions. For mapping MMIO regions, prepare_dtb_hwdom() sets the attribute 'p2m_mmio_direct_c' for the stage 2, but in the Linux kernel the MMIO's attribute can be one of below variants:

- ioremap(): device type with nGnRE;
- ioremap_np(): device type with nGnRnE (strong-ordered);
- ioremap_wc(): normal non-cachable.

The stage-2 memory attribute is used to restrict the final memory attribute. In this case, p2m_mmio_direct_c allows the domain to set pretty much any memory attribute.

Quoted 5 lines

If Xen hypervisor can handle these MMIO types in stage 2, then we should can use the same way to map stage 2 tables for the reserved memory. A difference for the reserved memory is it can be mapped as normal memory with cacheable.

I am a bit confused. I read this as you think the region is not mapped in the P2M (aka stage-2 page-tables for Arm). But from the logs you provided, the regions are already mapped (you have an MFN in hand). So to me the error is most likely in how we create the Device-Tree.

Quoted 10 lines
Quoted 3 lines

It would be good to understand why Xen may try to get a reference on the page.

I think the above log can answer this question.

Quoted 2 lines

Also, can you find the associated reserved-region and content of the Device-Tree?

The DT binding is something like (I tweaked a bit for readable):

Just to confirm this is the host device tree, right? If so...

Quoted 8 lines
	memory@20000000 {
		#address-cells = <0x02>;
		#size-cells = <0x02>;
		device_type = "memory";
		reg = <0x00 0x20000000 0x00 0xa0000000>,
                       <0x01 0xa0000000 0x01 0x60000000>;
	};

... you can see the reserved-regions are described in the normal memory. In fact...

Quoted 13 lines

	reserved-memory {
		#address-cells = <0x02>;
		#size-cells = <0x02>;
		ranges;

		reserved_mem1 {
			reg = <0x00 0x20000000 0x00 0x00010000>;
			no-map;
		};

		reserved_mem2 {
Quoted 8 lines
			reg = <0x00 0x40000000 0x00 0x20000000>;
			no-map;
		};

		reserved_mem3 {
			reg = <0x01 0xa0000000 0x00 0x20000000>;
			no-map;
		};

... no-map should tell the kernel to not use the memory at all. So I am a bit puzzled why it is trying to use it.

I would suggest to check if somehow Linux doesn't understand the reserved-memory nodes we wrote.

Cheers,

[1] https://lore.kernel.org/all/[email protected]/

Signature · 1 line
Julien Grall
author
#4 lore raw

Hi Julien,

On Mon, Sep 18, 2023 at 08:26:21PM +0100, Julien Grall wrote:

[...]

Quoted 3 lines

... from my understanding reserved-memory are just normal memory that are set aside for a specific purpose. So Xen has to create a 'memory' node *and* a 'reserved-memory' region.

To be clear, Xen passes the 'reserved-memory' regions as normal memory nodes, see [1].

Quoted 3 lines

With that the kernel is supposed to exclude all the 'reserved-memory' from normal usage unless they have the node contains the property 'reusable'. This was more clear before the binding was converted to YAML in [1].

Linux kernel reserves pages for memory ranges in the 'reserved-memory' node, no matter the 'no-map' property for a range is set or not (see the function memmap_init_reserved_pages() -> __SetPageReserved() in Linux kernel).

If a reserved memory range is set with 'no-map' property, the memory region will be not mapped in the kernel's identical address space. This avoids the data corruption caused between the memory speculative fetch with cachable mapping and the same memory region is used by devices (e.g. for DMA transferring).

[...]

Quoted 9 lines
Quoted 5 lines

Here the problem is these reserved memory regions are passed as normal memory nodes to Dom0 kernel, then Dom0 kernel allocates pages from these reserved memory regions. Apparently, this might lead to conflict, e.g. the reserved memory is used by Dom0 kernel, at the meantime the memory is used by another purpose (e.g. by MCU in the system).

See above. I think this is correct to pass both 'memory' and 'reserved-memory'. Now, it is possible that Xen may not create the device-tree correctly.

Agreed that now Xen wrongly create DT binding for 'reserved-memory' node, more specific, the reserved memory nodes are wrongly passed as normal memory nodes (again, see [1]).

Quoted 2 lines

I would suggest to look how Linux is populating the memory and whether it actually skipped the regions.

The Linux kernel reserves the corresponding pages for all reserved memory regions, which means the kernel page management (buddy alrogithm) doesn't allocate these pages at all.

With 'no-map' property, the memory range will not be mapped into the kernel identical address space.

Quoted 11 lines
Quoted 5 lines

Here I am a bit confused for "Xen doesn't have the capability to know the memory attribute". I looked into the file arch/arm/guest_walk.c, IIUC, it walks through the stage 1's page tables for the virtual machine and get the permission for the mapping, we also can get to know the mapping attribute, right?

Most of the time, Xen will use the HW to translate the guest virtual address to an intermediation physical address. Looking at the specification, it looks like that PAR_EL1 will contain the memory attribute which I didn't know.

Quoted 6 lines

We would then need to read MAIR_EL1 to find the attribute and also the memory attribute in the stage-2 to figure out the final memory attribute.

This is feasible but the Xen ABI mandates that region passed to Xen have a specific memory attributes (see the comment at the top of xen/include/public/arch-arm.h).

If you refer to the comment "All memory which is shared with other entities in the system ... which is mapped as Normal Inner Write-Back Outer Write-Back Inner-Shareable", I don't think it's relevant with current issue. I will explain in details in below.

Quoted 2 lines

Anyway, in your case, Linux is using the buffer is on the stack. So the region must have been mapped with the proper attribute.

I think you may misunderstand the issue. I would like to divide the issue into two parts:

- The first question is about how to pass reserved memory node from Xen hypervisor to Dom0 Linux kernel. Currently, Xen hypervisor coverts the reserved memory ranges and add them into the normal memory node.

Xen hypervisor should keep the reserved memory node and pass it to Dom0 Linux kernel. With this change, the Dom0 kernel will only allocate pages from normal memory node and the data in these pages can be shared by Xen hypervisor and Dom0 Linux kernel.

- The second question is for memory attribute for the reserved memory node. Note, the reserved memory ranges are not necessarily _shared_ between the Xen hypervisor and Dom0 Linux kernel. I think in most cases, the reserved memory will be ioremaped by drivers (for stage-1); and the Xen hypervisor should map P2M with the attribute p2m_mmio_direct_c, or we can explore a bit based on different properties, e.g. for 'no-map' memory range, we map P2M with p2m_mmio_direct_c; for 'reusable' memory range, we map with attribute 'p2m_ram_rw'.

To simplify the discussion, I think we can firstly finalize the fixing for the fist question and hold on the second question. After we fix the first one, we can come back to think about the second issue.

Quoted 5 lines
Quoted 5 lines

Another question for the attribute for MMIO regions. For mapping MMIO regions, prepare_dtb_hwdom() sets the attribute 'p2m_mmio_direct_c' for the stage 2, but in the Linux kernel the MMIO's attribute can be one of below variants:

Quoted 7 lines
Quoted 3 lines

- ioremap(): device type with nGnRE;
- ioremap_np(): device type with nGnRnE (strong-ordered);
- ioremap_wc(): normal non-cachable.

The stage-2 memory attribute is used to restrict the final memory attribute. In this case, p2m_mmio_direct_c allows the domain to set pretty much any memory attribute.

Thanks for confirmation. If so, I think the Xen hypervisor should follow the same attribute to map the reserved regions with attribute p2m_mmio_direct_c.

Quoted 8 lines
Quoted 4 lines

If Xen hypervisor can handle these MMIO types in stage 2, then we should can use the same way to map stage 2 tables for the reserved memory. A difference for the reserved memory is it can be mapped as normal memory with cacheable.

I am a bit confused. I read this as you think the region is not mapped in the P2M (aka stage-2 page-tables for Arm). But from the logs you provided, the regions are already mapped (you have an MFN in hand).

You are right. The reserved memory regions have been mapped in P2M.

Quoted 1 line

So to me the error is most likely in how we create the Device-Tree.

Yeah, let's firstly focus on the DT binding for reserved memory nodes.

Quoted 3 lines
Quoted 1 line

The DT binding is something like (I tweaked a bit for readable):

Just to confirm this is the host device tree, right? If so...

Yes.

Quoted 28 lines
Quoted 7 lines
	memory@20000000 {
		#address-cells = <0x02>;
		#size-cells = <0x02>;
		device_type = "memory";
		reg = <0x00 0x20000000 0x00 0xa0000000>,
                       <0x01 0xa0000000 0x01 0x60000000>;
	};

... you can see the reserved-regions are described in the normal memory. In fact...

Quoted 17 lines

	reserved-memory {
		#address-cells = <0x02>;
		#size-cells = <0x02>;
		ranges;

		reserved_mem1 {
			reg = <0x00 0x20000000 0x00 0x00010000>;
			no-map;
		};

		reserved_mem2 {
			reg = <0x00 0x40000000 0x00 0x20000000>;
			no-map;
		};
Quoted 7 lines
Quoted 4 lines
		reserved_mem3 {
			reg = <0x01 0xa0000000 0x00 0x20000000>;
			no-map;
		};

... no-map should tell the kernel to not use the memory at all. So I am a bit puzzled why it is trying to use it.

No, 'no-map' doesn't mean the Linux kernel doesn't use it, I quote from the kernel documentation Documentation/devicetree/bindings/reserved-memory/reserved-memory.yaml:
'no-map' means the kernel "must not create a virtual mapping of the region". The reserved memory regions are still "under the control of the device driver using the region".

Quoted 2 lines

I would suggest to check if somehow Linux doesn't understand the reserved-memory nodes we wrote.

Could you confirm the Xen does write reserved memory nodes? Or Xen converts the reserved memory nodes to normal memory nodes as I describe above :)

Thanks,
Leo

[1] https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/arch/arm/domain_build.c;h=24c9019cc43ca2d4963c3a6f8645939e26056cd0;hb=HEAD#l2476

#5 lore raw
Quoted 10 lines · Leo Yan

On Mon, Sep 18, 2023 at 08:26:21PM +0100, Julien Grall wrote:

[...]

Quoted 3 lines

... from my understanding reserved-memory are just normal memory that are set aside for a specific purpose. So Xen has to create a 'memory' node *and* a 'reserved-memory' region.

To be clear, Xen passes the 'reserved-memory' regions as normal memory nodes, see [1].

The memory nodes need to be explicitely written because they are excluded in handle_node(). If a node is not excluded, then it should be created in the dom0 Device-Tree.

AFAICT, the 'reserved-memory' node is not excluded and therefore should be copied to the dom0 DT.

[...]

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Here the problem is these reserved memory regions are passed as normal memory nodes to Dom0 kernel, then Dom0 kernel allocates pages from these reserved memory regions. Apparently, this might lead to conflict, e.g. the reserved memory is used by Dom0 kernel, at the meantime the memory is used by another purpose (e.g. by MCU in the system).

Quoted 7 lines
Quoted 3 lines

See above. I think this is correct to pass both 'memory' and 'reserved-memory'. Now, it is possible that Xen may not create the device-tree correctly.

Agreed that now Xen wrongly create DT binding for 'reserved-memory' node, more specific, the reserved memory nodes are wrongly passed as normal memory nodes (again, see [1]).

See above. You could dump the dom0 Device-Tree to confirm that 'reserved-memory' is created.

Quoted 25 lines
Quoted 3 lines

I would suggest to look how Linux is populating the memory and whether it actually skipped the regions.

The Linux kernel reserves the corresponding pages for all reserved memory regions, which means the kernel page management (buddy alrogithm) doesn't allocate these pages at all.

With 'no-map' property, the memory range will not be mapped into the kernel identical address space.

Quoted 13 lines
Quoted 5 lines

Here I am a bit confused for "Xen doesn't have the capability to know the memory attribute". I looked into the file arch/arm/guest_walk.c, IIUC, it walks through the stage 1's page tables for the virtual machine and get the permission for the mapping, we also can get to know the mapping attribute, right?

Most of the time, Xen will use the HW to translate the guest virtual address to an intermediation physical address. Looking at the specification, it looks like that PAR_EL1 will contain the memory attribute which I didn't know.

We would then need to read MAIR_EL1 to find the attribute and also the memory attribute in the stage-2 to figure out the final memory attribute.

Quoted 8 lines
Quoted 3 lines

This is feasible but the Xen ABI mandates that region passed to Xen have a specific memory attributes (see the comment at the top of xen/include/public/arch-arm.h).

If you refer to the comment "All memory which is shared with other entities in the system ... which is mapped as Normal Inner Write-Back Outer Write-Back Inner-Shareable", I don't think it's relevant with current issue. I will explain in details in below.

It is relevant if you intend to allocate hypercall buffer in a non-reusable reserved-region.

Quoted 7 lines
Quoted 3 lines

Anyway, in your case, Linux is using the buffer is on the stack. So the region must have been mapped with the proper attribute.

I think you may misunderstand the issue. I would like to divide the issue into two parts:

Quoted 8 lines

- The first question is about how to pass reserved memory node from Xen hypervisor to Dom0 Linux kernel. Currently, Xen hypervisor coverts the reserved memory ranges and add them into the normal memory node.

Xen hypervisor should keep the reserved memory node and pass it to Dom0 Linux kernel. With this change, the Dom0 kernel will only allocate pages from normal memory node and the data in these pages can be shared by Xen hypervisor and Dom0 Linux kernel.

This should be the case. See above.

[...]

Quoted 71 lines
Quoted 5 lines
Quoted 5 lines

Another question for the attribute for MMIO regions. For mapping MMIO regions, prepare_dtb_hwdom() sets the attribute 'p2m_mmio_direct_c' for the stage 2, but in the Linux kernel the MMIO's attribute can be one of below variants:

Quoted 7 lines
Quoted 3 lines

- ioremap(): device type with nGnRE;
- ioremap_np(): device type with nGnRnE (strong-ordered);
- ioremap_wc(): normal non-cachable.

The stage-2 memory attribute is used to restrict the final memory attribute. In this case, p2m_mmio_direct_c allows the domain to set pretty much any memory attribute.

Thanks for confirmation. If so, I think the Xen hypervisor should follow the same attribute to map the reserved regions with attribute p2m_mmio_direct_c.

Quoted 8 lines
Quoted 4 lines

If Xen hypervisor can handle these MMIO types in stage 2, then we should can use the same way to map stage 2 tables for the reserved memory. A difference for the reserved memory is it can be mapped as normal memory with cacheable.

I am a bit confused. I read this as you think the region is not mapped in the P2M (aka stage-2 page-tables for Arm). But from the logs you provided, the regions are already mapped (you have an MFN in hand).

You are right. The reserved memory regions have been mapped in P2M.

Quoted 1 line

So to me the error is most likely in how we create the Device-Tree.

Yeah, let's firstly focus on the DT binding for reserved memory nodes.

Quoted 3 lines
Quoted 1 line

The DT binding is something like (I tweaked a bit for readable):

Just to confirm this is the host device tree, right? If so...

Yes.

Quoted 33 lines
Quoted 7 lines
	memory@20000000 {
		#address-cells = <0x02>;
		#size-cells = <0x02>;
		device_type = "memory";
		reg = <0x00 0x20000000 0x00 0xa0000000>,
                        <0x01 0xa0000000 0x01 0x60000000>;
	};

... you can see the reserved-regions are described in the normal memory. In fact...

Quoted 21 lines

	reserved-memory {
		#address-cells = <0x02>;
		#size-cells = <0x02>;
		ranges;

		reserved_mem1 {
			reg = <0x00 0x20000000 0x00 0x00010000>;
			no-map;
		};

		reserved_mem2 {
			reg = <0x00 0x40000000 0x00 0x20000000>;
			no-map;
		};

		reserved_mem3 {
			reg = <0x01 0xa0000000 0x00 0x20000000>;
			no-map;
		};
Quoted 5 lines
Quoted 2 lines

... no-map should tell the kernel to not use the memory at all. So I am a bit puzzled why it is trying to use it.

No, 'no-map' doesn't mean the Linux kernel doesn't use it, I quote from the kernel documentation

I am under the impression that we have a different meaning for 'using' here. I am referring to the fact that when 'no-map' is specificed, then the kernel cannot use the region for other purpose (e.g. stack).

So the fact that the stack seemsm to resides in a reserved-region implies that Linux didn't detect the 'no-map'.

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw
In reply to Leo Yan

Hello,

Quoted 189 lines · Leo Yan

Hi Julien,

On Mon, Sep 18, 2023 at 08:26:21PM +0100, Julien Grall wrote:

[...]

Quoted 3 lines

... from my understanding reserved-memory are just normal memory that are set aside for a specific purpose. So Xen has to create a 'memory' node *and* a 'reserved-memory' region.

To be clear, Xen passes the 'reserved-memory' regions as normal memory nodes, see [1].

Quoted 3 lines

With that the kernel is supposed to exclude all the 'reserved-memory' from normal usage unless they have the node contains the property 'reusable'. This was more clear before the binding was converted to YAML in [1].

Linux kernel reserves pages for memory ranges in the 'reserved-memory' node, no matter the 'no-map' property for a range is set or not (see the function memmap_init_reserved_pages() -> __SetPageReserved() in Linux kernel).

If a reserved memory range is set with 'no-map' property, the memory region will be not mapped in the kernel's identical address space. This avoids the data corruption caused between the memory speculative fetch with cachable mapping and the same memory region is used by devices (e.g. for DMA transferring).

[...]

Quoted 9 lines
Quoted 5 lines

Here the problem is these reserved memory regions are passed as normal memory nodes to Dom0 kernel, then Dom0 kernel allocates pages from these reserved memory regions. Apparently, this might lead to conflict, e.g. the reserved memory is used by Dom0 kernel, at the meantime the memory is used by another purpose (e.g. by MCU in the system).

See above. I think this is correct to pass both 'memory' and 'reserved-memory'. Now, it is possible that Xen may not create the device-tree correctly.

Agreed that now Xen wrongly create DT binding for 'reserved-memory' node, more specific, the reserved memory nodes are wrongly passed as normal memory nodes (again, see [1]).

Quoted 2 lines

I would suggest to look how Linux is populating the memory and whether it actually skipped the regions.

The Linux kernel reserves the corresponding pages for all reserved memory regions, which means the kernel page management (buddy alrogithm) doesn't allocate these pages at all.

With 'no-map' property, the memory range will not be mapped into the kernel identical address space.

Quoted 11 lines
Quoted 5 lines

Here I am a bit confused for "Xen doesn't have the capability to know the memory attribute". I looked into the file arch/arm/guest_walk.c, IIUC, it walks through the stage 1's page tables for the virtual machine and get the permission for the mapping, we also can get to know the mapping attribute, right?

Most of the time, Xen will use the HW to translate the guest virtual address to an intermediation physical address. Looking at the specification, it looks like that PAR_EL1 will contain the memory attribute which I didn't know.

Quoted 6 lines

We would then need to read MAIR_EL1 to find the attribute and also the memory attribute in the stage-2 to figure out the final memory attribute.

This is feasible but the Xen ABI mandates that region passed to Xen have a specific memory attributes (see the comment at the top of xen/include/public/arch-arm.h).

If you refer to the comment "All memory which is shared with other entities in the system ... which is mapped as Normal Inner Write-Back Outer Write-Back Inner-Shareable", I don't think it's relevant with current issue. I will explain in details in below.

Quoted 2 lines

Anyway, in your case, Linux is using the buffer is on the stack. So the region must have been mapped with the proper attribute.

I think you may misunderstand the issue. I would like to divide the issue into two parts:

- The first question is about how to pass reserved memory node from Xen hypervisor to Dom0 Linux kernel. Currently, Xen hypervisor coverts the reserved memory ranges and add them into the normal memory node.

Xen hypervisor should keep the reserved memory node and pass it to Dom0 Linux kernel. With this change, the Dom0 kernel will only allocate pages from normal memory node and the data in these pages can be shared by Xen hypervisor and Dom0 Linux kernel.

- The second question is for memory attribute for the reserved memory node. Note, the reserved memory ranges are not necessarily _shared_ between the Xen hypervisor and Dom0 Linux kernel. I think in most cases, the reserved memory will be ioremaped by drivers (for stage-1); and the Xen hypervisor should map P2M with the attribute p2m_mmio_direct_c, or we can explore a bit based on different properties, e.g. for 'no-map' memory range, we map P2M with p2m_mmio_direct_c; for 'reusable' memory range, we map with attribute 'p2m_ram_rw'.

To simplify the discussion, I think we can firstly finalize the fixing for the fist question and hold on the second question. After we fix the first one, we can come back to think about the second issue.

Quoted 5 lines
Quoted 5 lines

Another question for the attribute for MMIO regions. For mapping MMIO regions, prepare_dtb_hwdom() sets the attribute 'p2m_mmio_direct_c' for the stage 2, but in the Linux kernel the MMIO's attribute can be one of below variants:

Quoted 7 lines
Quoted 3 lines

- ioremap(): device type with nGnRE;
- ioremap_np(): device type with nGnRnE (strong-ordered);
- ioremap_wc(): normal non-cachable.

The stage-2 memory attribute is used to restrict the final memory attribute. In this case, p2m_mmio_direct_c allows the domain to set pretty much any memory attribute.

Thanks for confirmation. If so, I think the Xen hypervisor should follow the same attribute to map the reserved regions with attribute p2m_mmio_direct_c.

Quoted 8 lines
Quoted 4 lines

If Xen hypervisor can handle these MMIO types in stage 2, then we should can use the same way to map stage 2 tables for the reserved memory. A difference for the reserved memory is it can be mapped as normal memory with cacheable.

I am a bit confused. I read this as you think the region is not mapped in the P2M (aka stage-2 page-tables for Arm). But from the logs you provided, the regions are already mapped (you have an MFN in hand).

You are right. The reserved memory regions have been mapped in P2M.

Quoted 1 line

So to me the error is most likely in how we create the Device-Tree.

Yeah, let's firstly focus on the DT binding for reserved memory nodes.

Quoted 3 lines
Quoted 1 line

The DT binding is something like (I tweaked a bit for readable):

Just to confirm this is the host device tree, right? If so...

Yes.

Quoted 28 lines
Quoted 7 lines
    memory@20000000 {
            #address-cells = <0x02>;
            #size-cells = <0x02>;
            device_type = "memory";
            reg = <0x00 0x20000000 0x00 0xa0000000>,
                       <0x01 0xa0000000 0x01 0x60000000>;
    };

... you can see the reserved-regions are described in the normal memory. In fact...

Quoted 17 lines

    reserved-memory {
            #address-cells = <0x02>;
            #size-cells = <0x02>;
            ranges;

            reserved_mem1 {
                    reg = <0x00 0x20000000 0x00 0x00010000>;
                    no-map;
            };

            reserved_mem2 {
                    reg = <0x00 0x40000000 0x00 0x20000000>;
                    no-map;
            };
Quoted 7 lines
Quoted 4 lines
            reserved_mem3 {
                    reg = <0x01 0xa0000000 0x00 0x20000000>;
                    no-map;
            };

... no-map should tell the kernel to not use the memory at all. So I am a bit puzzled why it is trying to use it.

No, 'no-map' doesn't mean the Linux kernel doesn't use it, I quote from the kernel documentation Documentation/devicetree/bindings/reserved-memory/reserved-memory.yaml:
'no-map' means the kernel "must not create a virtual mapping of the region". The reserved memory regions are still "under the control of the device driver using the region".

Quoted 6 lines
Quoted 2 lines

I would suggest to check if somehow Linux doesn't understand the reserved-memory nodes we wrote.

Could you confirm the Xen does write reserved memory nodes? Or Xen converts the reserved memory nodes to normal memory nodes as I describe above :)

Xen passes the /reserved-memory node unchanged from host device tree to dom0 fdt. Apart from that it creates an additional memory node covering the reserved ranges. Take a look at this example run(based on qemu):

Host dt:

memory@40000000 {
    reg = <0x00 0x40000000 0x01 0x00>;
    device_type = "memory";
};
reserved-memory {
    #size-cells = <0x02>;
    #address-cells = <0x02>;
    ranges;

    test@50000000 {
        reg = <0x00 0x50000000 0x00 0x10000000>;
        no-map;
    };
};

Xen:
(XEN) MODULE[0]: 000000004ac00000 - 000000004ad65000 Xen (XEN) MODULE[1]: 000000004ae00000 - 000000004ae03000 Device Tree (XEN) MODULE[2]: 0000000042c00000 - 000000004aa8ea8b Ramdisk (XEN) MODULE[3]: 0000000040400000 - 0000000042b30000 Kernel (XEN) RESVD[0]: 0000000050000000 - 000000005fffffff
...
(XEN) BANK[0] 0x000000c0000000-0x00000100000000 (1024MB)

Linux dom0:

[    0.000000] OF: reserved mem: 0x0000000050000000..0x000000005fffffff (262144 KiB) nomap non-reusable test@50000000

cat /proc/iomem:
50000000-5fffffff : reserved
c0000000-ffffffff : System RAM

dtc from Linux dom0:

memory@c0000000 {
    device_type = "memory";
    reg = <0x00 0xc0000000 0x00 0x40000000>;
};
memory@50000000 {
    device_type = "memory";
    reg = <0x00 0x50000000 0x00 0x10000000>;
};
reserved-memory {
    #address-cells = <0x02>;
    #size-cells = <0x02>;
    ranges;

    test@50000000 {
        reg = <0x00 0x50000000 0x00 0x10000000>;
        no-map;
    };
};

~Michal

#7 lore raw
Quoted 1 line · Michal Orzel

Hello,

Hi Michal,

Quoted 193 lines · Leo Yan
Quoted 192 lines

Hi Julien,

On Mon, Sep 18, 2023 at 08:26:21PM +0100, Julien Grall wrote:

[...]

Quoted 3 lines

... from my understanding reserved-memory are just normal memory that are set aside for a specific purpose. So Xen has to create a 'memory' node *and* a 'reserved-memory' region.

To be clear, Xen passes the 'reserved-memory' regions as normal memory nodes, see [1].

Quoted 3 lines

With that the kernel is supposed to exclude all the 'reserved-memory' from normal usage unless they have the node contains the property 'reusable'. This was more clear before the binding was converted to YAML in [1].

Linux kernel reserves pages for memory ranges in the 'reserved-memory' node, no matter the 'no-map' property for a range is set or not (see the function memmap_init_reserved_pages() -> __SetPageReserved() in Linux kernel).

If a reserved memory range is set with 'no-map' property, the memory region will be not mapped in the kernel's identical address space. This avoids the data corruption caused between the memory speculative fetch with cachable mapping and the same memory region is used by devices (e.g. for DMA transferring).

[...]

Quoted 9 lines
Quoted 5 lines

Here the problem is these reserved memory regions are passed as normal memory nodes to Dom0 kernel, then Dom0 kernel allocates pages from these reserved memory regions. Apparently, this might lead to conflict, e.g. the reserved memory is used by Dom0 kernel, at the meantime the memory is used by another purpose (e.g. by MCU in the system).

See above. I think this is correct to pass both 'memory' and 'reserved-memory'. Now, it is possible that Xen may not create the device-tree correctly.

Agreed that now Xen wrongly create DT binding for 'reserved-memory' node, more specific, the reserved memory nodes are wrongly passed as normal memory nodes (again, see [1]).

Quoted 2 lines

I would suggest to look how Linux is populating the memory and whether it actually skipped the regions.

The Linux kernel reserves the corresponding pages for all reserved memory regions, which means the kernel page management (buddy alrogithm) doesn't allocate these pages at all.

With 'no-map' property, the memory range will not be mapped into the kernel identical address space.

Quoted 11 lines
Quoted 5 lines

Here I am a bit confused for "Xen doesn't have the capability to know the memory attribute". I looked into the file arch/arm/guest_walk.c, IIUC, it walks through the stage 1's page tables for the virtual machine and get the permission for the mapping, we also can get to know the mapping attribute, right?

Most of the time, Xen will use the HW to translate the guest virtual address to an intermediation physical address. Looking at the specification, it looks like that PAR_EL1 will contain the memory attribute which I didn't know.

Quoted 6 lines

We would then need to read MAIR_EL1 to find the attribute and also the memory attribute in the stage-2 to figure out the final memory attribute.

This is feasible but the Xen ABI mandates that region passed to Xen have a specific memory attributes (see the comment at the top of xen/include/public/arch-arm.h).

If you refer to the comment "All memory which is shared with other entities in the system ... which is mapped as Normal Inner Write-Back Outer Write-Back Inner-Shareable", I don't think it's relevant with current issue. I will explain in details in below.

Quoted 2 lines

Anyway, in your case, Linux is using the buffer is on the stack. So the region must have been mapped with the proper attribute.

I think you may misunderstand the issue. I would like to divide the issue into two parts:

- The first question is about how to pass reserved memory node from Xen hypervisor to Dom0 Linux kernel. Currently, Xen hypervisor coverts the reserved memory ranges and add them into the normal memory node.

Xen hypervisor should keep the reserved memory node and pass it to Dom0 Linux kernel. With this change, the Dom0 kernel will only allocate pages from normal memory node and the data in these pages can be shared by Xen hypervisor and Dom0 Linux kernel.

- The second question is for memory attribute for the reserved memory node. Note, the reserved memory ranges are not necessarily _shared_ between the Xen hypervisor and Dom0 Linux kernel. I think in most cases, the reserved memory will be ioremaped by drivers (for stage-1); and the Xen hypervisor should map P2M with the attribute p2m_mmio_direct_c, or we can explore a bit based on different properties, e.g. for 'no-map' memory range, we map P2M with p2m_mmio_direct_c; for 'reusable' memory range, we map with attribute 'p2m_ram_rw'.

To simplify the discussion, I think we can firstly finalize the fixing for the fist question and hold on the second question. After we fix the first one, we can come back to think about the second issue.

Quoted 5 lines
Quoted 5 lines

Another question for the attribute for MMIO regions. For mapping MMIO regions, prepare_dtb_hwdom() sets the attribute 'p2m_mmio_direct_c' for the stage 2, but in the Linux kernel the MMIO's attribute can be one of below variants:

Quoted 7 lines
Quoted 3 lines

- ioremap(): device type with nGnRE;
- ioremap_np(): device type with nGnRnE (strong-ordered);
- ioremap_wc(): normal non-cachable.

The stage-2 memory attribute is used to restrict the final memory attribute. In this case, p2m_mmio_direct_c allows the domain to set pretty much any memory attribute.

Thanks for confirmation. If so, I think the Xen hypervisor should follow the same attribute to map the reserved regions with attribute p2m_mmio_direct_c.

Quoted 8 lines
Quoted 4 lines

If Xen hypervisor can handle these MMIO types in stage 2, then we should can use the same way to map stage 2 tables for the reserved memory. A difference for the reserved memory is it can be mapped as normal memory with cacheable.

I am a bit confused. I read this as you think the region is not mapped in the P2M (aka stage-2 page-tables for Arm). But from the logs you provided, the regions are already mapped (you have an MFN in hand).

You are right. The reserved memory regions have been mapped in P2M.

Quoted 1 line

So to me the error is most likely in how we create the Device-Tree.

Yeah, let's firstly focus on the DT binding for reserved memory nodes.

Quoted 3 lines
Quoted 1 line

The DT binding is something like (I tweaked a bit for readable):

Just to confirm this is the host device tree, right? If so...

Yes.

Quoted 28 lines
Quoted 7 lines
     memory@20000000 {
             #address-cells = <0x02>;
             #size-cells = <0x02>;
             device_type = "memory";
             reg = <0x00 0x20000000 0x00 0xa0000000>,
                        <0x01 0xa0000000 0x01 0x60000000>;
     };

... you can see the reserved-regions are described in the normal memory. In fact...

Quoted 17 lines

     reserved-memory {
             #address-cells = <0x02>;
             #size-cells = <0x02>;
             ranges;

             reserved_mem1 {
                     reg = <0x00 0x20000000 0x00 0x00010000>;
                     no-map;
             };

             reserved_mem2 {
                     reg = <0x00 0x40000000 0x00 0x20000000>;
                     no-map;
             };
Quoted 7 lines
Quoted 4 lines
             reserved_mem3 {
                     reg = <0x01 0xa0000000 0x00 0x20000000>;
                     no-map;
             };

... no-map should tell the kernel to not use the memory at all. So I am a bit puzzled why it is trying to use it.

No, 'no-map' doesn't mean the Linux kernel doesn't use it, I quote from the kernel documentation Documentation/devicetree/bindings/reserved-memory/reserved-memory.yaml:
'no-map' means the kernel "must not create a virtual mapping of the region". The reserved memory regions are still "under the control of the device driver using the region".

Quoted 2 lines

I would suggest to check if somehow Linux doesn't understand the reserved-memory nodes we wrote.

Quoted 7 lines
Quoted 3 lines

Could you confirm the Xen does write reserved memory nodes? Or Xen converts the reserved memory nodes to normal memory nodes as I describe above :)

Xen passes the /reserved-memory node unchanged from host device tree to dom0 fdt. Apart from that it creates an additional memory node covering the reserved ranges. Take a look at this example run(based on qemu):

Thanks for providing an example! This is quite handy.

Quoted 27 lines

Host dt:

memory@40000000 {
     reg = <0x00 0x40000000 0x01 0x00>;
     device_type = "memory";
};
reserved-memory {
     #size-cells = <0x02>;
     #address-cells = <0x02>;
     ranges;

     test@50000000 {
         reg = <0x00 0x50000000 0x00 0x10000000>;
         no-map;
     };
};

Xen:
(XEN) MODULE[0]: 000000004ac00000 - 000000004ad65000 Xen (XEN) MODULE[1]: 000000004ae00000 - 000000004ae03000 Device Tree (XEN) MODULE[2]: 0000000042c00000 - 000000004aa8ea8b Ramdisk (XEN) MODULE[3]: 0000000040400000 - 0000000042b30000 Kernel (XEN) RESVD[0]: 0000000050000000 - 000000005fffffff
...
(XEN) BANK[0] 0x000000c0000000-0x00000100000000 (1024MB)

Quoted 2 lines

Linux dom0:

[    0.000000] OF: reserved mem: 0x0000000050000000..0x000000005fffffff (262144 KiB) nomap non-reusable test@50000000

So Linux should tell whether a region has been reserved. @Leo, can you share with us the serial console? Can you confirm the version of Xen you are using?

Cheers,

Signature · 1 line
Julien Grall
author
#8 lore raw

On Wed, Sep 20, 2023 at 11:36:24AM +0100, Julien Grall wrote:

[...]

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

Could you confirm the Xen does write reserved memory nodes? Or Xen converts the reserved memory nodes to normal memory nodes as I describe above :)

Xen passes the /reserved-memory node unchanged from host device tree to dom0 fdt. Apart from that it creates an additional memory node covering the reserved ranges. Take a look at this example run(based on qemu):

Thanks for providing an example! This is quite handy.

Yeah, thanks a lot for sharing the DT binding, Michal!

Quoted 26 lines
Quoted 26 lines

Host dt:

memory@40000000 {
     reg = <0x00 0x40000000 0x01 0x00>;
     device_type = "memory";
};
reserved-memory {
     #size-cells = <0x02>;
     #address-cells = <0x02>;
     ranges;

     test@50000000 {
         reg = <0x00 0x50000000 0x00 0x10000000>;
         no-map;
     };
};

Xen:
(XEN) MODULE[0]: 000000004ac00000 - 000000004ad65000 Xen (XEN) MODULE[1]: 000000004ae00000 - 000000004ae03000 Device Tree (XEN) MODULE[2]: 0000000042c00000 - 000000004aa8ea8b Ramdisk (XEN) MODULE[3]: 0000000040400000 - 0000000042b30000 Kernel (XEN) RESVD[0]: 0000000050000000 - 000000005fffffff
...
(XEN) BANK[0] 0x000000c0000000-0x00000100000000 (1024MB)

Quoted 6 lines
Quoted 2 lines

Linux dom0:

[    0.000000] OF: reserved mem: 0x0000000050000000..0x000000005fffffff (262144 KiB) nomap non-reusable test@50000000

So Linux should tell whether a region has been reserved. @Leo, can you share with us the serial console? Can you confirm the version of Xen you are using?

Sure, I will share the logs and DTS in Dom0 kernel when I can access my machine in next week (due to I am on vacation in this week).

I am a bit suspicious that I am not aware something in my working kernel. If have any finding, I will share back.

Thanks,
Leo

author
#9 lore raw
In reply to Julien Grall

On Wed, Sep 20, 2023 at 11:36:24AM +0100, Julien Grall wrote:

[...]

Quoted 1 line

Can you confirm the version of Xen you are using?

For this question, I use a very new Xen repo with staging branch which I git pulled at last week.

Thanks,
Leo

author
#10 lore raw
In reply to Julien Grall

Hi Julien,

On Wed, Sep 20, 2023 at 11:36:24AM +0100, Julien Grall wrote:

[...]

Quoted 26 lines
Quoted 26 lines

Host dt:

memory@40000000 {
     reg = <0x00 0x40000000 0x01 0x00>;
     device_type = "memory";
};
reserved-memory {
     #size-cells = <0x02>;
     #address-cells = <0x02>;
     ranges;

     test@50000000 {
         reg = <0x00 0x50000000 0x00 0x10000000>;
         no-map;
     };
};

Xen:
(XEN) MODULE[0]: 000000004ac00000 - 000000004ad65000 Xen (XEN) MODULE[1]: 000000004ae00000 - 000000004ae03000 Device Tree (XEN) MODULE[2]: 0000000042c00000 - 000000004aa8ea8b Ramdisk (XEN) MODULE[3]: 0000000040400000 - 0000000042b30000 Kernel (XEN) RESVD[0]: 0000000050000000 - 000000005fffffff
...
(XEN) BANK[0] 0x000000c0000000-0x00000100000000 (1024MB)

Quoted 6 lines
Quoted 2 lines

Linux dom0:

[    0.000000] OF: reserved mem: 0x0000000050000000..0x000000005fffffff (262144 KiB) nomap non-reusable test@50000000

So Linux should tell whether a region has been reserved. @Leo, can you share with us the serial console? Can you confirm the version of Xen you are using?

Finally, I located the issue is caused by the reserved memory node with "disabled" status. In the end, it mismatches for handling the disabled memory nodes between the Xen hypervisor and the Linux kernel.

I think I should set the status property from the reserved memory nodes for my debugging platform. But for the case that the memory nodes (for both normal and reserved nodes) which are disabled, the Xen hypervisor should ignore them. So I sent a new patch to address the issue for disabled memory nodes:

https://lore.kernel.org/xen-devel/[email protected]/T/#u

Hope now we are clear for the issue. If not, please let me know.

Thanks,
Leo