xen/arm: Don't use copy_from_paddr for DTB relocation
PATCH 1/1 xen/arm: Don't use copy_from_paddr for DTB relocation
Currently the early stage of the Arm boot maps the DTB using early_fdt_map() using PAGE_HYPERVISOR_RO which is cacheable read-only memory, later during DTB relocation the function copy_from_paddr() is used to map pages in the same range on the fixmap but using PAGE_HYPERVISOR_WC which is non-cacheable read-write memory.
The Arm specifications, ARM DDI0487L.a, section B2.11 "Mismatched memory attributes" discourage using mismatched attributes for aliases of the same location.
Given that there is nothing preventing the relocation since the region is already mapped, fix that by open-coding copy_from_paddr inside relocate_fdt, without mapping on the fixmap.
--- xen/arch/arm/setup.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
xen/arch/arm/setup.c +4 -3
@@ -237,14 +237,15 @@ void __init discard_initial_modules(void) }/* Relocate the FDT in Xen heap */ -static void *__init relocate_fdt (paddr_t dtb_paddr,size_t dtb_size) +static void *__init relocate_fdt (const void *dtb_vaddr,size_t dtb_size) {void *fdt =xmalloc_bytes (dtb_size);if ( !fdt )panic ("Unable to allocate memory for relocating the Device-Tree.\n" ); -copy_from_paddr (fdt, dtb_paddr, dtb_size); +memcpy (fdt, dtb_vaddr, dtb_size); +clean_dcache_va_range (dtb_vaddr, dtb_size);return fdt; }
@@ -362,7 +363,7 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)if ( acpi_disabled ) {printk ("Booting using Device Tree\n" ); - device_tree_flattened =relocate_fdt (fdt_paddr, fdt_size); + device_tree_flattened =relocate_fdt (device_tree_flattened, fdt_size);dt_unflatten_host_device_tree (); }else
Signature · 1 line
2.34.1
Quoted 13 lines · Luca Fancellu
Currently the early stage of the Arm boot maps the DTB using
early_fdt_map()usingPAGE_HYPERVISOR_ROwhich is cacheable read-only memory, later during DTB relocation the functioncopy_from_paddr()is used to map pages in the same range on the fixmap but usingPAGE_HYPERVISOR_WCwhich is non-cacheable read-write memory.The Arm specifications, ARM DDI0487L.a, section B2.11 "Mismatched memory attributes" discourage using mismatched attributes for aliases of the same location.
Quoted 5 lines
Given that there is nothing preventing the relocation since the region is already mapped, fix that by open-coding
copy_from_paddrinsiderelocate_fdt, without mapping on the fixmap.Fixes 1bdc81dac816 ("arm: setup MM using information from the device tree")
Why Fixes tag? I don't see it as a bug and something we need to backport...
Quoted 14 lines
Signed-off-by Luca Fancellu <[email protected]>--- xen/arch/arm/setup.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)xen/arch/arm/setup.c +0
-1@@ -237,14 +237,15 @@ void __init discard_initial_modules(void) }/* Relocate the FDT in Xen heap */ -static void *__init relocate_fdt (paddr_t dtb_paddr,size_t dtb_size)
xen/arch/arm/setup.c void __init discard_initial_modules(void) · line 248
Quoted 10 lines
xen/arch/arm/setup.c +3
-1+static void *__init relocate_fdt (const void *dtb_vaddr,size_t dtb_size) {void *fdt =xmalloc_bytes (dtb_size);if ( !fdt )panic ("Unable to allocate memory for relocating the Device-Tree.\n" ); -copy_from_paddr (fdt, dtb_paddr, dtb_size); +memcpy (fdt, dtb_vaddr, dtb_size); +clean_dcache_va_range (dtb_vaddr, dtb_size);
The purpose of cleaning dcache after memcpy is to clean the new area i.e. destination == fdt, not source region.
xen/arch/arm/setup.c void asmlinkage __init start_xen(unsigned long fdt_paddr) · line 366
Quoted 9 lines
xen/arch/arm/setup.c +1
-1return fdt; }@@ -362,7 +363,7 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)if ( acpi_disabled ) {printk ("Booting using Device Tree\n" ); - device_tree_flattened =relocate_fdt (fdt_paddr, fdt_size); + device_tree_flattened =relocate_fdt (device_tree_flattened, fdt_size);
NIT: It can be just my PoV but it reads odd. Why can't relocate_fdt modify device_tree_flattened pointer directly in the function?
Quoted 6 lines
xen/arch/arm/setup.c +0
-1dt_unflatten_host_device_tree (); }else --2.34.1
~Michal
Hi Michal,
Quoted 18 lines · Michal
Quoted 13 lines · Luca Fancellu
Currently the early stage of the Arm boot maps the DTB using
early_fdt_map()usingPAGE_HYPERVISOR_ROwhich is cacheable read-only memory, later during DTB relocation the functioncopy_from_paddr()is used to map pages in the same range on the fixmap but usingPAGE_HYPERVISOR_WCwhich is non-cacheable read-write memory.The Arm specifications, ARM DDI0487L.a, section B2.11 "Mismatched memory attributes" discourage using mismatched attributes for aliases of the same location.
Quoted 6 lines
Quoted 5 lines
Given that there is nothing preventing the relocation since the region is already mapped, fix that by open-coding
copy_from_paddrinsiderelocate_fdt, without mapping on the fixmap.Fixes 1bdc81dac816 ("arm: setup MM using information from the device tree")Why Fixes tag? I don't see it as a bug and something we need to backport...
ok I’ll remove it
Quoted 19 lines
Quoted 19 lines
Signed-off-by Luca Fancellu <[email protected]>---
xen/arch/arm/setup.c| 7 ++++---1 file changed, 4 insertions(+), 3 deletions(-)xen/arch/arm/setup.c +1
-1@@ -237,14 +237,15 @@ void __init discard_initial_modules(void) }/* Relocate the FDT in Xen heap */ -static void *__init relocate_fdt (paddr_t dtb_paddr,size_t dtb_size) +static void *__init relocate_fdt (const void *dtb_vaddr,size_t dtb_size) {void *fdt =xmalloc_bytes (dtb_size);
Quoted 8 lines
xen/arch/arm/setup.c void __init discard_initial_modules(void) · line 248
Quoted 6 lines
xen/arch/arm/setup.c +2
-1if ( !fdt )panic ("Unable to allocate memory for relocating the Device-Tree.\n" ); -copy_from_paddr (fdt, dtb_paddr, dtb_size); +memcpy (fdt, dtb_vaddr, dtb_size); +clean_dcache_va_range (dtb_vaddr, dtb_size);The purpose of cleaning dcache after memcpy is to clean the new area i.e. destination == fdt, not source region.
woops, my bad, I’ll fix
Quoted 12 lines
xen/arch/arm/setup.c void asmlinkage __init start_xen(unsigned long fdt_paddr) · line 366
Quoted 10 lines
xen/arch/arm/setup.c +1
-1return fdt; }@@ -362,7 +363,7 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)if ( acpi_disabled ) {printk ("Booting using Device Tree\n" ); - device_tree_flattened =relocate_fdt (fdt_paddr, fdt_size); + device_tree_flattened =relocate_fdt (device_tree_flattened, fdt_size);NIT: It can be just my PoV but it reads odd. Why can't
relocate_fdtmodifydevice_tree_flattenedpointer directly in the function?
you mean something like:
static void * __init relocate_fdt(size_t dtb_size) {
void *fdt = xmalloc_bytes(dtb_size);
if ( !fdt )panic("Unable to allocate memory for relocating the Device-Tree.\n");
memcpy(fdt, device_tree_flattened, dtb_size);clean_dcache_va_range(fdt, dtb_size);
return fdt;
}
Cheers,
Luca
Quoted 76 lines · Luca Fancellu
Hi Michal,
Quoted 18 lines · Michal
Quoted 13 lines · Luca Fancellu
Currently the early stage of the Arm boot maps the DTB using
early_fdt_map()usingPAGE_HYPERVISOR_ROwhich is cacheable read-only memory, later during DTB relocation the functioncopy_from_paddr()is used to map pages in the same range on the fixmap but usingPAGE_HYPERVISOR_WCwhich is non-cacheable read-write memory.The Arm specifications, ARM DDI0487L.a, section B2.11 "Mismatched memory attributes" discourage using mismatched attributes for aliases of the same location.
Quoted 6 lines
Quoted 5 lines
Given that there is nothing preventing the relocation since the region is already mapped, fix that by open-coding
copy_from_paddrinsiderelocate_fdt, without mapping on the fixmap.Fixes 1bdc81dac816 ("arm: setup MM using information from the device tree")Why Fixes tag? I don't see it as a bug and something we need to backport...
ok I’ll remove it
Quoted 19 lines
Quoted 19 lines
Signed-off-by Luca Fancellu <[email protected]>---
xen/arch/arm/setup.c| 7 ++++---1 file changed, 4 insertions(+), 3 deletions(-)xen/arch/arm/setup.c +1
-1@@ -237,14 +237,15 @@ void __init discard_initial_modules(void) }/* Relocate the FDT in Xen heap */ -static void *__init relocate_fdt (paddr_t dtb_paddr,size_t dtb_size) +static void *__init relocate_fdt (const void *dtb_vaddr,size_t dtb_size) {void *fdt =xmalloc_bytes (dtb_size);Quoted 8 lines
xen/arch/arm/setup.c void __init discard_initial_modules(void) · line 248
Quoted 6 lines
xen/arch/arm/setup.c +2
-1if ( !fdt )panic ("Unable to allocate memory for relocating the Device-Tree.\n" ); -copy_from_paddr (fdt, dtb_paddr, dtb_size); +memcpy (fdt, dtb_vaddr, dtb_size); +clean_dcache_va_range (dtb_vaddr, dtb_size);The purpose of cleaning dcache after memcpy is to clean the new area i.e. destination == fdt, not source region.
woops, my bad, I’ll fix
Quoted 12 lines
xen/arch/arm/setup.c void asmlinkage __init start_xen(unsigned long fdt_paddr) · line 366
Quoted 10 lines
xen/arch/arm/setup.c +1
-1return fdt; }@@ -362,7 +363,7 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)if ( acpi_disabled ) {printk ("Booting using Device Tree\n" ); - device_tree_flattened =relocate_fdt (fdt_paddr, fdt_size); + device_tree_flattened =relocate_fdt (device_tree_flattened, fdt_size);NIT: It can be just my PoV but it reads odd. Why can't
relocate_fdtmodifydevice_tree_flattenedpointer directly in the function?you mean something like:
Quoted 8 lines
static void *
__initrelocate_fdt(size_t dtb_size){
void *fdt =xmalloc_bytes(dtb_size);if ( !fdt )
panic("Unable to allocate memory for relocating the Device-Tree.\n");
memcpy(fdt, device_tree_flattened, dtb_size);
You already make assumption about device_tree_flattened being global, so why not assigning device_tree_flattened = fdt in the function as well?
~Michal
Quoted 20 lines
Quoted 20 lines
Quoted 13 lines
xen/arch/arm/setup.c void asmlinkage __init start_xen(unsigned long fdt_paddr) · line 366
Quoted 11 lines
return fdt; }xen/arch/arm/setup.c +1
-1@@ -362,7 +363,7 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)if ( acpi_disabled ) {printk ("Booting using Device Tree\n" ); - device_tree_flattened =relocate_fdt (fdt_paddr, fdt_size); + device_tree_flattened =relocate_fdt (device_tree_flattened, fdt_size);NIT: It can be just my PoV but it reads odd. Why can't
relocate_fdtmodifydevice_tree_flattenedpointer directly in the function?you mean something like:
static void *
__initrelocate_fdt(size_t dtb_size){
void *fdt =xmalloc_bytes(dtb_size);
Quoted 6 lines
Quoted 4 lines
if ( !fdt )
panic("Unable to allocate memory for relocating the Device-Tree.\n");
memcpy(fdt, device_tree_flattened, dtb_size);You already make assumption about
device_tree_flattenedbeing global, so why not assigningdevice_tree_flattened= fdt in the function as well?
just because it’s more easy to follow the global variable changes when reading the start_xen(…) code as the function is the only one modifying it.
If you strongly oppose to that I’ll change, but imo it’s easier to follow the code in this way
Cheers,
Luca
Quoted 25 lines · Luca Fancellu
Quoted 25 lines
Quoted 25 lines
Quoted 5 lines
Quoted 5 lines
Quoted 10 lines
xen/arch/arm/setup.c void asmlinkage __init start_xen(unsigned long fdt_paddr) · line 366
Quoted 8 lines
return fdt; }xen/arch/arm/setup.c +1
-1@@ -362,7 +363,7 @@ void asmlinkage __init start_xen(unsigned long fdt_paddr)if ( acpi_disabled ) {printk ("Booting using Device Tree\n" ); - device_tree_flattened =relocate_fdt (fdt_paddr, fdt_size); + device_tree_flattened =relocate_fdt (device_tree_flattened, fdt_size);NIT: It can be just my PoV but it reads odd. Why can't
relocate_fdtmodifydevice_tree_flattenedpointer directly in the function?you mean something like:
static void *
__initrelocate_fdt(size_t dtb_size){
void *fdt =xmalloc_bytes(dtb_size);if ( !fdt )
panic("Unable to allocate memory for relocating the Device-Tree.\n");
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
memcpy(fdt, device_tree_flattened, dtb_size);You already make assumption about
device_tree_flattenedbeing global, so why not assigningdevice_tree_flattened= fdt in the function as well?just because it’s more easy to follow the global variable changes when reading the
start_xen(…)code as the function is the only one modifying it.If you strongly oppose to that I’ll change, but imo it’s easier to follow the code in this way
How about:
static void __init relocate_fdt(void *dtb_vaddr, size_t dtb_size)
{
void *fdt = xmalloc_bytes(dtb_size);
if ( !fdt )
panic("Unable to allocate memory for relocating the Device-Tree.\n");
memcpy(fdt, dtb_vaddr, dtb_size);
clean_dcache_va_range(fdt, dtb_size);
dtb_vaddr = fdt;
}This would be best IMO. That said, I don't care that much. Choose whatever makes sense.
~Michal