diff mbox series

[RFC,4/4] powerpc: Use common of_kexec_setup_new_fdt()

Message ID 20201211221006.1052453-5-robh@kernel.org
State New
Headers show
Series Kexec FDT setup consolidation | expand

Commit Message

Rob Herring Dec. 11, 2020, 10:10 p.m. UTC
Signed-off-by: Rob Herring <robh@kernel.org>

---

After the IMA changes, delete_fdt_mem_rsv() can also be removed.

 arch/powerpc/kexec/file_load.c | 125 ++-------------------------------
 1 file changed, 6 insertions(+), 119 deletions(-)

--
2.25.1

Comments

nramas Dec. 12, 2020, 3:22 p.m. UTC | #1
On 12/11/20 2:10 PM, Rob Herring wrote:
> Signed-off-by: Rob Herring <robh@kernel.org>

> ---

> 

> After the IMA changes, delete_fdt_mem_rsv() can also be removed.

> 

>   arch/powerpc/kexec/file_load.c | 125 ++-------------------------------

>   1 file changed, 6 insertions(+), 119 deletions(-)

> 


This change looks good to me.

Reviewed-by: Lakshmi Ramasubramanian <nramas@linux.microsoft.com>


  -lakshmi

> diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c

> index e452b11df631..956bcb2d1ec2 100644

> --- a/arch/powerpc/kexec/file_load.c

> +++ b/arch/powerpc/kexec/file_load.c

> @@ -16,6 +16,7 @@

> 

>   #include <linux/slab.h>

>   #include <linux/kexec.h>

> +#include <linux/of.h>

>   #include <linux/of_fdt.h>

>   #include <linux/libfdt.h>

>   #include <asm/setup.h>

> @@ -156,132 +157,18 @@ int setup_new_fdt(const struct kimage *image, void *fdt,

>   		  unsigned long initrd_load_addr, unsigned long initrd_len,

>   		  const char *cmdline)

>   {

> -	int ret, chosen_node;

> -	const void *prop;

> -

> -	/* Remove memory reservation for the current device tree. */

> -	ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),

> -				 fdt_totalsize(initial_boot_params));

> -	if (ret == 0)

> -		pr_debug("Removed old device tree reservation.\n");

> -	else if (ret != -ENOENT)

> -		return ret;

> -

> -	chosen_node = fdt_path_offset(fdt, "/chosen");

> -	if (chosen_node == -FDT_ERR_NOTFOUND) {

> -		chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),

> -					      "chosen");

> -		if (chosen_node < 0) {

> -			pr_err("Error creating /chosen.\n");

> -			return -EINVAL;

> -		}

> -	} else if (chosen_node < 0) {

> -		pr_err("Malformed device tree: error reading /chosen.\n");

> -		return -EINVAL;

> -	}

> -

> -	/* Did we boot using an initrd? */

> -	prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);

> -	if (prop) {

> -		uint64_t tmp_start, tmp_end, tmp_size;

> -

> -		tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));

> -

> -		prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);

> -		if (!prop) {

> -			pr_err("Malformed device tree.\n");

> -			return -EINVAL;

> -		}

> -		tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));

> -

> -		/*

> -		 * kexec reserves exact initrd size, while firmware may

> -		 * reserve a multiple of PAGE_SIZE, so check for both.

> -		 */

> -		tmp_size = tmp_end - tmp_start;

> -		ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);

> -		if (ret == -ENOENT)

> -			ret = delete_fdt_mem_rsv(fdt, tmp_start,

> -						 round_up(tmp_size, PAGE_SIZE));

> -		if (ret == 0)

> -			pr_debug("Removed old initrd reservation.\n");

> -		else if (ret != -ENOENT)

> -			return ret;

> -

> -		/* If there's no new initrd, delete the old initrd's info. */

> -		if (initrd_len == 0) {

> -			ret = fdt_delprop(fdt, chosen_node,

> -					  "linux,initrd-start");

> -			if (ret) {

> -				pr_err("Error deleting linux,initrd-start.\n");

> -				return -EINVAL;

> -			}

> -

> -			ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");

> -			if (ret) {

> -				pr_err("Error deleting linux,initrd-end.\n");

> -				return -EINVAL;

> -			}

> -		}

> -	}

> -

> -	if (initrd_len) {

> -		ret = fdt_setprop_u64(fdt, chosen_node,

> -				      "linux,initrd-start",

> -				      initrd_load_addr);

> -		if (ret < 0)

> -			goto err;

> -

> -		/* initrd-end is the first address after the initrd image. */

> -		ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",

> -				      initrd_load_addr + initrd_len);

> -		if (ret < 0)

> -			goto err;

> -

> -		ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);

> -		if (ret) {

> -			pr_err("Error reserving initrd memory: %s\n",

> -			       fdt_strerror(ret));

> -			return -EINVAL;

> -		}

> -	}

> -

> -	if (cmdline != NULL) {

> -		ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);

> -		if (ret < 0)

> -			goto err;

> -	} else {

> -		ret = fdt_delprop(fdt, chosen_node, "bootargs");

> -		if (ret && ret != -FDT_ERR_NOTFOUND) {

> -			pr_err("Error deleting bootargs.\n");

> -			return -EINVAL;

> -		}

> -	}

> +	int ret;

> 

> -	if (image->type == KEXEC_TYPE_CRASH) {

> -		/*

> -		 * Avoid elfcorehdr from being stomped on in kdump kernel by

> -		 * setting up memory reserve map.

> -		 */

> -		ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem,

> -				      image->arch.elf_headers_sz);

> -		if (ret) {

> -			pr_err("Error reserving elfcorehdr memory: %s\n",

> -			       fdt_strerror(ret));

> -			goto err;

> -		}

> -	}

> +	ret = of_kexec_setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline);

> +	if (ret)

> +		goto err;

> 

> -	ret = setup_ima_buffer(image, fdt, chosen_node);

> +	ret = setup_ima_buffer(image, fdt, fdt_path_offset(fdt, "/chosen"));

>   	if (ret) {

>   		pr_err("Error setting up the new device tree.\n");

>   		return ret;

>   	}

> 

> -	ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);

> -	if (ret)

> -		goto err;

> -

>   	return 0;

> 

>   err:

> --

> 2.25.1

>
Thiago Jung Bauermann Dec. 22, 2020, 9:55 p.m. UTC | #2
Rob Herring <robh@kernel.org> writes:

> Signed-off-by: Rob Herring <robh@kernel.org>

> ---

>

> After the IMA changes, delete_fdt_mem_rsv() can also be removed.

>

>  arch/powerpc/kexec/file_load.c | 125 ++-------------------------------

>  1 file changed, 6 insertions(+), 119 deletions(-)


Reviewed-by: Thiago Jung Bauermann <bauerman@linux.ibm.com>


Shouldn't this series also be Cc'd to the linuxppc-dev mailing list?

I just noticed that the ARM64 IMA kexec series hasn't been copying the
linuxppc-dev mailing list, so perhaps this is why this series isn't
doing that, either.

> diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c

> index e452b11df631..956bcb2d1ec2 100644

> --- a/arch/powerpc/kexec/file_load.c

> +++ b/arch/powerpc/kexec/file_load.c

> @@ -16,6 +16,7 @@

>

>  #include <linux/slab.h>

>  #include <linux/kexec.h>

> +#include <linux/of.h>

>  #include <linux/of_fdt.h>

>  #include <linux/libfdt.h>

>  #include <asm/setup.h>


It's possible to remove the <linux/of_fdt.h> include now.

-- 
Thiago Jung Bauermann
IBM Linux Technology Center
nramas Dec. 22, 2020, 11:33 p.m. UTC | #3
On 12/22/20 1:55 PM, Thiago Jung Bauermann wrote:
> 

> Rob Herring <robh@kernel.org> writes:

> 

>> Signed-off-by: Rob Herring <robh@kernel.org>

>> ---

>>

>> After the IMA changes, delete_fdt_mem_rsv() can also be removed.

>>

>>   arch/powerpc/kexec/file_load.c | 125 ++-------------------------------

>>   1 file changed, 6 insertions(+), 119 deletions(-)

> 

> Reviewed-by: Thiago Jung Bauermann <bauerman@linux.ibm.com>

> 

> Shouldn't this series also be Cc'd to the linuxppc-dev mailing list?

> 

> I just noticed that the ARM64 IMA kexec series hasn't been copying the

> linuxppc-dev mailing list, so perhaps this is why this series isn't

> doing that, either.


Thanks for pointing that out Thiago.
I will copy linuxppc-dev mailing list (linuxppc-dev@vger.kernel.org) 
when I post v14 of my patch set.

  -lakshmi

> 

>> diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c

>> index e452b11df631..956bcb2d1ec2 100644

>> --- a/arch/powerpc/kexec/file_load.c

>> +++ b/arch/powerpc/kexec/file_load.c

>> @@ -16,6 +16,7 @@

>>

>>   #include <linux/slab.h>

>>   #include <linux/kexec.h>

>> +#include <linux/of.h>

>>   #include <linux/of_fdt.h>

>>   #include <linux/libfdt.h>

>>   #include <asm/setup.h>

> 

> It's possible to remove the <linux/of_fdt.h> include now.

>
diff mbox series

Patch

diff --git a/arch/powerpc/kexec/file_load.c b/arch/powerpc/kexec/file_load.c
index e452b11df631..956bcb2d1ec2 100644
--- a/arch/powerpc/kexec/file_load.c
+++ b/arch/powerpc/kexec/file_load.c
@@ -16,6 +16,7 @@ 

 #include <linux/slab.h>
 #include <linux/kexec.h>
+#include <linux/of.h>
 #include <linux/of_fdt.h>
 #include <linux/libfdt.h>
 #include <asm/setup.h>
@@ -156,132 +157,18 @@  int setup_new_fdt(const struct kimage *image, void *fdt,
 		  unsigned long initrd_load_addr, unsigned long initrd_len,
 		  const char *cmdline)
 {
-	int ret, chosen_node;
-	const void *prop;
-
-	/* Remove memory reservation for the current device tree. */
-	ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
-				 fdt_totalsize(initial_boot_params));
-	if (ret == 0)
-		pr_debug("Removed old device tree reservation.\n");
-	else if (ret != -ENOENT)
-		return ret;
-
-	chosen_node = fdt_path_offset(fdt, "/chosen");
-	if (chosen_node == -FDT_ERR_NOTFOUND) {
-		chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
-					      "chosen");
-		if (chosen_node < 0) {
-			pr_err("Error creating /chosen.\n");
-			return -EINVAL;
-		}
-	} else if (chosen_node < 0) {
-		pr_err("Malformed device tree: error reading /chosen.\n");
-		return -EINVAL;
-	}
-
-	/* Did we boot using an initrd? */
-	prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
-	if (prop) {
-		uint64_t tmp_start, tmp_end, tmp_size;
-
-		tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
-
-		prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
-		if (!prop) {
-			pr_err("Malformed device tree.\n");
-			return -EINVAL;
-		}
-		tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
-
-		/*
-		 * kexec reserves exact initrd size, while firmware may
-		 * reserve a multiple of PAGE_SIZE, so check for both.
-		 */
-		tmp_size = tmp_end - tmp_start;
-		ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
-		if (ret == -ENOENT)
-			ret = delete_fdt_mem_rsv(fdt, tmp_start,
-						 round_up(tmp_size, PAGE_SIZE));
-		if (ret == 0)
-			pr_debug("Removed old initrd reservation.\n");
-		else if (ret != -ENOENT)
-			return ret;
-
-		/* If there's no new initrd, delete the old initrd's info. */
-		if (initrd_len == 0) {
-			ret = fdt_delprop(fdt, chosen_node,
-					  "linux,initrd-start");
-			if (ret) {
-				pr_err("Error deleting linux,initrd-start.\n");
-				return -EINVAL;
-			}
-
-			ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
-			if (ret) {
-				pr_err("Error deleting linux,initrd-end.\n");
-				return -EINVAL;
-			}
-		}
-	}
-
-	if (initrd_len) {
-		ret = fdt_setprop_u64(fdt, chosen_node,
-				      "linux,initrd-start",
-				      initrd_load_addr);
-		if (ret < 0)
-			goto err;
-
-		/* initrd-end is the first address after the initrd image. */
-		ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
-				      initrd_load_addr + initrd_len);
-		if (ret < 0)
-			goto err;
-
-		ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
-		if (ret) {
-			pr_err("Error reserving initrd memory: %s\n",
-			       fdt_strerror(ret));
-			return -EINVAL;
-		}
-	}
-
-	if (cmdline != NULL) {
-		ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
-		if (ret < 0)
-			goto err;
-	} else {
-		ret = fdt_delprop(fdt, chosen_node, "bootargs");
-		if (ret && ret != -FDT_ERR_NOTFOUND) {
-			pr_err("Error deleting bootargs.\n");
-			return -EINVAL;
-		}
-	}
+	int ret;

-	if (image->type == KEXEC_TYPE_CRASH) {
-		/*
-		 * Avoid elfcorehdr from being stomped on in kdump kernel by
-		 * setting up memory reserve map.
-		 */
-		ret = fdt_add_mem_rsv(fdt, image->arch.elf_headers_mem,
-				      image->arch.elf_headers_sz);
-		if (ret) {
-			pr_err("Error reserving elfcorehdr memory: %s\n",
-			       fdt_strerror(ret));
-			goto err;
-		}
-	}
+	ret = of_kexec_setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline);
+	if (ret)
+		goto err;

-	ret = setup_ima_buffer(image, fdt, chosen_node);
+	ret = setup_ima_buffer(image, fdt, fdt_path_offset(fdt, "/chosen"));
 	if (ret) {
 		pr_err("Error setting up the new device tree.\n");
 		return ret;
 	}

-	ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
-	if (ret)
-		goto err;
-
 	return 0;

 err: