diff mbox series

of/kexec: save pa of initial_boot_params for arm64 and use it at kexec

Message ID 20241003113840.2972416-1-usamaarif642@gmail.com (mailing list archive)
State New
Headers show
Series of/kexec: save pa of initial_boot_params for arm64 and use it at kexec | expand

Commit Message

Usama Arif Oct. 3, 2024, 11:38 a.m. UTC
__pa() is only intended to be used for linear map addresses and using
it for initial_boot_params which is in fixmap for arm64 will give an
incorrect value. Hence stash the physical address when it is known at
boot time and use it at kexec time instead of converting the virtual
address using __pa().

Reported-by: Breno Leitao <leitao@debian.org>
Suggested-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Usama Arif <usamaarif642@gmail.com>
Fixes: ac10be5cdbfa ("arm64: Use common of_kexec_alloc_and_setup_fdt()")
---
 arch/arm64/kernel/setup.c | 8 ++++++++
 drivers/of/fdt.c          | 6 ++++++
 drivers/of/kexec.c        | 8 ++++++--
 include/linux/of_fdt.h    | 2 ++
 4 files changed, 22 insertions(+), 2 deletions(-)

Comments

Saravana Kannan Oct. 3, 2024, 8:20 p.m. UTC | #1
On Thu, Oct 3, 2024 at 4:38 AM Usama Arif <usamaarif642@gmail.com> wrote:
>
>  __pa() is only intended to be used for linear map addresses and using
> it for initial_boot_params which is in fixmap for arm64 will give an
> incorrect value. Hence stash the physical address when it is known at
> boot time and use it at kexec time instead of converting the virtual
> address using __pa().
>
> Reported-by: Breno Leitao <leitao@debian.org>
> Suggested-by: Mark Rutland <mark.rutland@arm.com>
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> Fixes: ac10be5cdbfa ("arm64: Use common of_kexec_alloc_and_setup_fdt()")
> ---
>  arch/arm64/kernel/setup.c | 8 ++++++++
>  drivers/of/fdt.c          | 6 ++++++
>  drivers/of/kexec.c        | 8 ++++++--
>  include/linux/of_fdt.h    | 2 ++
>  4 files changed, 22 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
> index b22d28ec8028..a4d96f5e2e05 100644
> --- a/arch/arm64/kernel/setup.c
> +++ b/arch/arm64/kernel/setup.c
> @@ -194,6 +194,14 @@ static void __init setup_machine_fdt(phys_addr_t dt_phys)
>         /* Early fixups are done, map the FDT as read-only now */
>         fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
>
> +       /*
> +        * Save dt_phys address so that it can be used later for kexec. This
> +        * is done as __pa() is only intended to be used for linear map addresses
> +        * and using it for initial_boot_params which is in fixmap will give an
> +        * incorrect value.
> +        */
> +       set_initial_boot_params_pa(dt_phys);
> +
>         name = of_flat_dt_get_machine_name();
>         if (!name)
>                 return;
> diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
> index 4d528c10df3a..9e312b7c246e 100644
> --- a/drivers/of/fdt.c
> +++ b/drivers/of/fdt.c
> @@ -457,6 +457,7 @@ int __initdata dt_root_addr_cells;
>  int __initdata dt_root_size_cells;
>
>  void *initial_boot_params __ro_after_init;
> +phys_addr_t initial_boot_params_pa __ro_after_init;
>
>  #ifdef CONFIG_OF_EARLY_FLATTREE
>
> @@ -1185,6 +1186,11 @@ bool __init early_init_dt_scan(void *params)
>         return true;
>  }
>
> +void __init set_initial_boot_params_pa(phys_addr_t params)
> +{
> +       initial_boot_params_pa = params;
> +}
> +
>  static void *__init copy_device_tree(void *fdt)
>  {
>         int size;
> diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
> index 9ccde2fd77cb..ca9f27b27f71 100644
> --- a/drivers/of/kexec.c
> +++ b/drivers/of/kexec.c
> @@ -300,8 +300,12 @@ void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
>                 goto out;
>         }
>
> -       /* Remove memory reservation for the current device tree. */
> -       ret = fdt_find_and_del_mem_rsv(fdt, __pa(initial_boot_params),
> +       /* Remove memory reservation for the current device tree.
> +        * For arm64, initial_boot_params is a fixmap address, hence __pa(),
> +        * can't be used to get the physical address.
> +        */
> +       ret = fdt_find_and_del_mem_rsv(fdt, IS_ENABLED(CONFIG_ARM64) ?
> +                                      initial_boot_params_pa : __pa(initial_boot_params),
>                                        fdt_totalsize(initial_boot_params));

Not sure about the correctness of the patch (not a kexec expert) but
no need to do all of this inside a function parameter. Just create a
variable and use it here.

-Saravana

>         if (ret == -EINVAL) {
>                 pr_err("Error removing memory reservation.\n");
> diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h
> index d69ad5bb1eb1..dbd99bf21ac8 100644
> --- a/include/linux/of_fdt.h
> +++ b/include/linux/of_fdt.h
> @@ -31,6 +31,7 @@ extern void *of_fdt_unflatten_tree(const unsigned long *blob,
>  extern int __initdata dt_root_addr_cells;
>  extern int __initdata dt_root_size_cells;
>  extern void *initial_boot_params;
> +extern phys_addr_t initial_boot_params_pa;
>
>  extern char __dtb_start[];
>  extern char __dtb_end[];
> @@ -73,6 +74,7 @@ extern int early_init_dt_scan_root(void);
>  extern bool early_init_dt_scan(void *params);
>  extern bool early_init_dt_verify(void *params);
>  extern void early_init_dt_scan_nodes(void);
> +extern void set_initial_boot_params_pa(phys_addr_t params);
>
>  extern const char *of_flat_dt_get_machine_name(void);
>  extern const void *of_flat_dt_match_machine(const void *default_match,
> --
> 2.43.5
>
Rob Herring Oct. 4, 2024, 12:03 a.m. UTC | #2
On Thu, Oct 03, 2024 at 12:38:40PM +0100, Usama Arif wrote:
>  __pa() is only intended to be used for linear map addresses and using
> it for initial_boot_params which is in fixmap for arm64 will give an
> incorrect value. Hence stash the physical address when it is known at
> boot time and use it at kexec time instead of converting the virtual
> address using __pa().
> 
> Reported-by: Breno Leitao <leitao@debian.org>
> Suggested-by: Mark Rutland <mark.rutland@arm.com>
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> Fixes: ac10be5cdbfa ("arm64: Use common of_kexec_alloc_and_setup_fdt()")
> ---
>  arch/arm64/kernel/setup.c | 8 ++++++++
>  drivers/of/fdt.c          | 6 ++++++
>  drivers/of/kexec.c        | 8 ++++++--
>  include/linux/of_fdt.h    | 2 ++
>  4 files changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
> index b22d28ec8028..a4d96f5e2e05 100644
> --- a/arch/arm64/kernel/setup.c
> +++ b/arch/arm64/kernel/setup.c
> @@ -194,6 +194,14 @@ static void __init setup_machine_fdt(phys_addr_t dt_phys)
>  	/* Early fixups are done, map the FDT as read-only now */
>  	fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
>  
> +	/*
> +	 * Save dt_phys address so that it can be used later for kexec. This
> +	 * is done as __pa() is only intended to be used for linear map addresses
> +	 * and using it for initial_boot_params which is in fixmap will give an
> +	 * incorrect value.
> +	 */
> +	set_initial_boot_params_pa(dt_phys);

No new arch->dt functions please. If we need to save off the PA, then do 
that when we set initial_boot_params.

Rob
diff mbox series

Patch

diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
index b22d28ec8028..a4d96f5e2e05 100644
--- a/arch/arm64/kernel/setup.c
+++ b/arch/arm64/kernel/setup.c
@@ -194,6 +194,14 @@  static void __init setup_machine_fdt(phys_addr_t dt_phys)
 	/* Early fixups are done, map the FDT as read-only now */
 	fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
 
+	/*
+	 * Save dt_phys address so that it can be used later for kexec. This
+	 * is done as __pa() is only intended to be used for linear map addresses
+	 * and using it for initial_boot_params which is in fixmap will give an
+	 * incorrect value.
+	 */
+	set_initial_boot_params_pa(dt_phys);
+
 	name = of_flat_dt_get_machine_name();
 	if (!name)
 		return;
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 4d528c10df3a..9e312b7c246e 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -457,6 +457,7 @@  int __initdata dt_root_addr_cells;
 int __initdata dt_root_size_cells;
 
 void *initial_boot_params __ro_after_init;
+phys_addr_t initial_boot_params_pa __ro_after_init;
 
 #ifdef CONFIG_OF_EARLY_FLATTREE
 
@@ -1185,6 +1186,11 @@  bool __init early_init_dt_scan(void *params)
 	return true;
 }
 
+void __init set_initial_boot_params_pa(phys_addr_t params)
+{
+	initial_boot_params_pa = params;
+}
+
 static void *__init copy_device_tree(void *fdt)
 {
 	int size;
diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index 9ccde2fd77cb..ca9f27b27f71 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.c
@@ -300,8 +300,12 @@  void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
 		goto out;
 	}
 
-	/* Remove memory reservation for the current device tree. */
-	ret = fdt_find_and_del_mem_rsv(fdt, __pa(initial_boot_params),
+	/* Remove memory reservation for the current device tree.
+	 * For arm64, initial_boot_params is a fixmap address, hence __pa(),
+	 * can't be used to get the physical address.
+	 */
+	ret = fdt_find_and_del_mem_rsv(fdt, IS_ENABLED(CONFIG_ARM64) ?
+				       initial_boot_params_pa : __pa(initial_boot_params),
 				       fdt_totalsize(initial_boot_params));
 	if (ret == -EINVAL) {
 		pr_err("Error removing memory reservation.\n");
diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h
index d69ad5bb1eb1..dbd99bf21ac8 100644
--- a/include/linux/of_fdt.h
+++ b/include/linux/of_fdt.h
@@ -31,6 +31,7 @@  extern void *of_fdt_unflatten_tree(const unsigned long *blob,
 extern int __initdata dt_root_addr_cells;
 extern int __initdata dt_root_size_cells;
 extern void *initial_boot_params;
+extern phys_addr_t initial_boot_params_pa;
 
 extern char __dtb_start[];
 extern char __dtb_end[];
@@ -73,6 +74,7 @@  extern int early_init_dt_scan_root(void);
 extern bool early_init_dt_scan(void *params);
 extern bool early_init_dt_verify(void *params);
 extern void early_init_dt_scan_nodes(void);
+extern void set_initial_boot_params_pa(phys_addr_t params);
 
 extern const char *of_flat_dt_get_machine_name(void);
 extern const void *of_flat_dt_match_machine(const void *default_match,