Re: [RFC PATCH v2 0/3] pmem memmap dump support

2023-05-24 Thread Li, Zhijian

Ping

Baoquan, Dan

Sorry to bother you again.

Could you further comment a word or two on this set?


Thanks
Zhijian


on 5/10/2023 6:41 PM, Zhijian Li (Fujitsu) wrote:

Hi Dan


on 5/8/2023 5:45 PM, Zhijian Li (Fujitsu) wrote:

Dan,


On 29/04/2023 02:59, Dan Williams wrote:

Li Zhijian wrote:

Hello folks,

About 2 months ago, we posted our first RFC[3] and received your kindly 
feedback. Thank you :)
Now, I'm back with the code.

Currently, this RFC has already implemented to supported case D*. And the case 
A is disabled
deliberately in makedumpfile. It includes changes in 3 source code as below:

I think the reason this patchkit is difficult to follow is that it
spends a lot of time describing a chosen solution, but not enough time
describing the problem and the tradeoffs.

For example why is updating /proc/vmcore with pmem metadata the chosen
solution? Why not leave the kernel out of it and have makedumpfile
tooling aware of how to parse persistent memory namespace info-blocks
and retrieve that dump itself? This is what I proposed here:

http://lore.kernel.org/r/641484f7ef780_a52e2...@dwillia2-mobl3.amr.corp.intel.com.notmuch

Sorry for the late reply. I'm just back from the vacation.
And sorry again for missing your previous *important* information in V1.

Your proposal also sounds to me with less kernel changes, but more ndctl 
coupling with makedumpfile tools.
In my current understanding, it will includes following source changes.

The kernel and makedumpfile has updated. It's still in a early stage, but in 
order to make sure I'm following your proposal.
i want to share the changes with you early. Alternatively, you are able to 
refer to my github for the full details.
https://github.com/zhijianli88/makedumpfile/commit/8ebfe38c015cfca0545cb3b1d7a6cc9a58fc9bb3

If I'm going the wrong way, fee free to let me know :)



---+---+
Source |  changes  |
---+---+
I. | 1. enter force_raw in kdump kernel automatically(avoid metadata 
being updated again)|

kernel should adapt it so that the metadata of pmem will be updated again in 
the kdump kernel:

diff --git a/drivers/nvdimm/namespace_devs.c b/drivers/nvdimm/namespace_devs.c
index c60ec0b373c5..2e59be8b9c78 100644
--- a/drivers/nvdimm/namespace_devs.c
+++ b/drivers/nvdimm/namespace_devs.c
@@ -8,6 +8,7 @@
   #include 
   #include 
   #include 
+#include 
   #include "nd-core.h"
   #include "pmem.h"
   #include "pfn.h"
@@ -1504,6 +1505,8 @@ struct nd_namespace_common 
*nvdimm_namespace_common_probe(struct device *dev)
  return ERR_PTR(-ENODEV);
  }
   
+   if (is_kdump_kernel())

+   ndns->force_raw = true;
  return ndns;
   }
   EXPORT_SYMBOL(nvdimm_namespace_common_probe);


kernel |   |
  | 2. mark the whole pmem's PT_LOAD for kexec_file_load(2) syscall 
  |
---+---+
II. kexec- | 1. mark the whole pmem's PT_LOAD for kexe_load(2) syscall |
tool   |   |
---+---+
III.   | 1. parse the infoblock and calculate the boundaries of userdata 
and metadata   |
makedump-  | 2. skip pmem userdata region  |
file   | 3. exclude pmem metadata region if needed |
---+---+

I will try rewrite it with your proposal ASAP

inspect_pmem_namespace() will walk the namespaces and the read its 
resource.start and infoblock. With this
information, we can calculate the boundaries of userdata and metadata easily. 
But currently this changes are
strongly coupling with the ndctl/pmem which looks a bit messy and ugly.

makedumpfile===

diff --git a/Makefile b/Makefile
index a289e41ef44d..4b4ded639cfd 100644
--- a/Makefile
+++ b/Makefile
@@ -50,7 +50,7 @@ OBJ_PART=$(patsubst %.c,%.o,$(SRC_PART))
   SRC_ARCH = arch/arm.c arch/arm64.c arch/x86.c arch/x86_64.c arch/ia64.c 
arch/ppc64.c arch/s390x.c arch/ppc.c arch/sparc64.c arch/mips64.c 
arch/loongarch64.c
   OBJ_ARCH=$(patsubst %.c,%.o,$(SRC_ARCH))
   
-LIBS = -ldw -lbz2 -ldl -lelf -lz

+LIBS = -ldw -lbz2 -ldl -lelf -lz -lndctl
   ifneq ($(LINKTYPE), dynamic)
   LIBS := -static $(LIBS) -llzma
   endif
diff --git a/makedumpfile.c b/makedumpfile.c
index 98c3b8c7ced9..db68d05a29f9 100644
--- a/makedumpfile.c
+++ b/makedumpfile.c
@@ -27,6 +27,8 @@
   #include 
   #include 
   #include 
+#include 
+#include 

+
+#define INFOBLOCK_SZ (8192)
+#define SZ_4K (4096)
+#define PFN_SIG_LEN 16
+
+typedef uint64_t u64;
+typedef int64_t 

Re: [PATCHv2 1/6] kexec: Change the image probe's prototype

2023-05-24 Thread Pingfan Liu
On Wed, May 24, 2023 at 7:53 PM Simon Horman  wrote:
>
> On Tue, May 16, 2023 at 03:31:19PM +0800, Pingfan Liu wrote:
> > As more complicated kernel format occurs such as zboot, where the
> > compressed kernel is stored as a payload. The straight forward
> > decompression can not meet the demand.
> >
> > A new image probe method is expected to read in the kernel file and decide
> > how to unfold the content by itself.
> >
> > This patch aims to change the image probe's prototype from
> > typedef int (probe_t)(const char *kernel_buf, off_t 
> > kernel_size);
> > to
> > typedef int (probe_t)(const char *kernel_buf, off_t 
> > kernel_size, struct kexec_info *info);
> >
> > Later, info can be used to return both the file descriptor and the
> > parsed kernel buffer.
> >
> > In case your are curious, the remaing part of the log describes the
> > process of the substitution of the new prototype, which can be divided
> > into three groups.
> >
> > 1. change function pointer and its callsites:
> >   sed -i 's/(probe_t)(const char \*kernel_buf, off_t 
> > kernel_size);/(probe_t)(const char \*kernel, off_t kernel_size, struct 
> > kexec_info \*info);/g' kexec/kexec.h
> >   sed -i 's/\.probe(\([^,]*\), \([^)]*\))/\.probe(\1, \2, NULL)/g' 
> > kexec/kexec.c
> >
> > 2. change the function declaration and definition of each 'probe'
> >instance by coccinelle because they may cross lines
> >
> > The cocci file looks like:
> >
> > @ rule1 @
> > identifier fn =~ "_probe";
> > identifier buf, size;
> > typedef off_t;
> > @@
> >
> > -int fn(const char *buf, off_t size)
> > +int fn(const char *buf, off_t size, struct kexec_info *info)
> > {
> > ...
> > }
> >
> > @ rule2 @
> > identifier fn =~ "_probe";
> > identifier buf, size;
> > typedef off_t;
> > @@
> >
> > +int fn(const char *buf, off_t size, struct kexec_info *info);
> > -int fn(const char *buf, off_t size);
> >
> > Then running the command
> > spatch --sp-file cocci/define.cocci --dir kexec --include-headers > 
> > ../define.patch
> > git apply --directory=kexec ../define.patch
> >
> > 3. change the direct calls to the probe instances
> >
> > Originally I planned to achieve this by coccinelle, but failed similar
> > to [1]. I have tried using "-I and --include" option for coccinelle, but it
> > still did not work.
> >
> > Checking the direct callsite by "git grep "_probe(" | grep -v const"
> > Fortunatelly, it turns out that only a few direct callsites exist, which
> > lies in i386, and easy to be amended manually.
> >
> > Anyway, just FYI, the cocci file looks like:
> > @ rule1 @
> > identifier fn =~ "_probe";
> > identifier buf, size;
> > identifier info;
> > typedef off_t;
> > @@
> >
> > int fn(const char *buf, off_t size, struct kexec_info *info);
> >
> > /* change the direct callsite of any probe */
> > @ rule2 @
> > identifier rule1.fn;
> > expression E1, E2;
> > @@
> >
> >  fn(E1, E2
> > +  ,NULL
> >)
> >
> > Then running the command:
> > spatch --sp-file cocci/direct.cocci --dir kexec --include-headers
> >
> > [1]: 
> > https://lore.kernel.org/all/alpine.DEB.2.22.394.2202280705080.3112@hadrien/T/
> >
> > Signed-off-by: Pingfan Liu 
> > To: kexec@lists.infradead.org
> > Cc: ho...@verge.net.au
> > Cc: a...@kernel.org
> > Cc: jeremy.lin...@arm.com
>
> Unfortunately I am seeing a build failure on (at least) sh4 with this change:
>
>  ../../kexec/arch/sh/kexec-zImage-sh.c:54:5: error: conflicting types for 
> ‘zImage_sh_probe’
> 54 | int zImage_sh_probe(const char *buf, off_t UNUSED(len))
>| ^~~

It may be because the macro UNUSED() is not tackled correctly by coccinelle.

And there are several other places, which use UNUSED()
git grep UNUSED | grep probe
kexec/arch/arm/kexec-zImage-arm.c:138:int zImage_arm_probe(const char
*UNUSED(buf), off_t UNUSED(len))
kexec/arch/s390/kexec-image.c:219:image_s390_probe(const char
*UNUSED(kernel_buf), off_t UNUSED(kernel_size))
kexec/arch/sh/kexec-netbsd-sh.c:38:int netbsd_sh_probe(const char
*buf, off_t UNUSED(len))
kexec/arch/sh/kexec-zImage-sh.c:54:int zImage_sh_probe(const char
*buf, off_t UNUSED(len))

I will see how to handle them.


Thanks,

Pingfan
>  In file included from ../../kexec/arch/sh/kexec-zImage-sh.c:27:
>  ../../kexec/arch/sh/kexec-sh.h:11:5: note: previous declaration of 
> ‘zImage_sh_probe’ was here
> 11 | int zImage_sh_probe(const char *buf, off_t len, struct kexec_info 
> *info);
>| ^~~
>  ../../kexec/arch/sh/kexec-zImage-sh.c:29:18: warning: ‘probe_debug’ defined 
> but not used [-Wunused-const-variable=]
> 29 | static const int probe_debug = 0;
>|  ^~~
>  make[1]: *** [Makefile:124: kexec/arch/sh/kexec-zImage-sh.o] Error 1
>  make[1]: *** Waiting for unfinished jobs
>  ../../kexec/arch/sh/kexec-netbsd-sh.c:28:18: warning: ‘probe_debug’ defined 
> but not used [-Wunused-const-variable=]
> 28 | static const int probe_debug = 0;
>|  ^~~
>
> 

Re: [PATCH v9 3/4] of: kexec: Refactor IMA buffer related functions to make them reusable

2023-05-24 Thread Stefan Berger




On 5/24/23 19:16, Jerry Snitselaar wrote:

On Tue, Apr 18, 2023 at 09:44:08AM -0400, Stefan Berger wrote:

Refactor IMA buffer related functions to make them reusable for carrying
TPM logs across kexec.

Signed-off-by: Stefan Berger 
Cc: Rob Herring 
Cc: Frank Rowand 
Cc: Mimi Zohar 
Reviewed-by: Mimi Zohar 
Reviewed-by: Rob Herring 
Tested-by: Nageswara R Sastry 
Tested-by: Coiby Xu 



Reviewed-by: Jerry Snitselaar 


Thanks a lot for looking at the patches. Unfortunately this series only 
resolves the issue with the newer kexec call but we have seen systems where the 
older one is used for setting a crash kernel and when the crash happens we're 
back to square one. I have been trying to embed the log into the memory of the 
flattened of-device tree but that also turns out to be not so straight forward.

Stefan

---
v6:
  - Add __init to get_kexec_buffer as suggested by Jonathan

v5:
  - Rebased on Jonathan McDowell's commit "b69a2afd5afc x86/kexec: Carry
forward IMA measurement log on kexec"
v4:
  - Move debug output into setup_buffer()
---
  drivers/of/kexec.c | 126 ++---
  1 file changed, 74 insertions(+), 52 deletions(-)

diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
index 1373d7e0a9b3..fa8c0c75adf9 100644
--- a/drivers/of/kexec.c
+++ b/drivers/of/kexec.c
@@ -117,45 +117,57 @@ static int do_get_kexec_buffer(const void *prop, int len, 
unsigned long *addr,
  }
  
  #ifdef CONFIG_HAVE_IMA_KEXEC

-/**
- * ima_get_kexec_buffer - get IMA buffer from the previous kernel
- * @addr:  On successful return, set to point to the buffer contents.
- * @size:  On successful return, set to the buffer size.
- *
- * Return: 0 on success, negative errno on error.
- */
-int __init ima_get_kexec_buffer(void **addr, size_t *size)
+static int __init get_kexec_buffer(const char *name, unsigned long *addr,
+  size_t *size)
  {
int ret, len;
-   unsigned long tmp_addr;
unsigned long start_pfn, end_pfn;
-   size_t tmp_size;
const void *prop;
  
-	prop = of_get_property(of_chosen, "linux,ima-kexec-buffer", );

+   prop = of_get_property(of_chosen, name, );
if (!prop)
return -ENOENT;
  
-	ret = do_get_kexec_buffer(prop, len, _addr, _size);

+   ret = do_get_kexec_buffer(prop, len, addr, size);
if (ret)
return ret;
  
-	/* Do some sanity on the returned size for the ima-kexec buffer */

-   if (!tmp_size)
+   /* Do some sanity on the returned size for the kexec buffer */
+   if (!*size)
return -ENOENT;
  
  	/*

 * Calculate the PFNs for the buffer and ensure
 * they are with in addressable memory.
 */
-   start_pfn = PHYS_PFN(tmp_addr);
-   end_pfn = PHYS_PFN(tmp_addr + tmp_size - 1);
+   start_pfn = PHYS_PFN(*addr);
+   end_pfn = PHYS_PFN(*addr + *size - 1);
if (!page_is_ram(start_pfn) || !page_is_ram(end_pfn)) {
-   pr_warn("IMA buffer at 0x%lx, size = 0x%zx beyond memory\n",
-   tmp_addr, tmp_size);
+   pr_warn("%s buffer at 0x%lx, size = 0x%zx beyond memory\n",
+   name, *addr, *size);
return -EINVAL;
}
  
+	return 0;

+}
+
+/**
+ * ima_get_kexec_buffer - get IMA buffer from the previous kernel
+ * @addr:  On successful return, set to point to the buffer contents.
+ * @size:  On successful return, set to the buffer size.
+ *
+ * Return: 0 on success, negative errno on error.
+ */
+int __init ima_get_kexec_buffer(void **addr, size_t *size)
+{
+   int ret;
+   unsigned long tmp_addr;
+   size_t tmp_size;
+
+   ret = get_kexec_buffer("linux,ima-kexec-buffer", _addr, _size);
+   if (ret)
+   return ret;
+
*addr = __va(tmp_addr);
*size = tmp_size;
  
@@ -188,72 +200,82 @@ int __init ima_free_kexec_buffer(void)

  }
  #endif
  
-/**

- * remove_ima_buffer - remove the IMA buffer property and reservation from @fdt
- *
- * @fdt: Flattened Device Tree to update
- * @chosen_node: Offset to the chosen node in the device tree
- *
- * The IMA measurement buffer is of no use to a subsequent kernel, so we always
- * remove it from the device tree.
- */
-static void remove_ima_buffer(void *fdt, int chosen_node)
+static int remove_buffer(void *fdt, int chosen_node, const char *name)
  {
int ret, len;
unsigned long addr;
size_t size;
const void *prop;
  
-	if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC))

-   return;
-
-   prop = fdt_getprop(fdt, chosen_node, "linux,ima-kexec-buffer", );
+   prop = fdt_getprop(fdt, chosen_node, name, );
if (!prop)
-   return;
+   return -ENOENT;
  
  	ret = do_get_kexec_buffer(prop, len, , );

-   fdt_delprop(fdt, chosen_node, "linux,ima-kexec-buffer");
+   fdt_delprop(fdt, chosen_node, name);
if (ret)
-   

Re: [PATCH v9 3/4] of: kexec: Refactor IMA buffer related functions to make them reusable

2023-05-24 Thread Jerry Snitselaar
On Tue, Apr 18, 2023 at 09:44:08AM -0400, Stefan Berger wrote:
> Refactor IMA buffer related functions to make them reusable for carrying
> TPM logs across kexec.
> 
> Signed-off-by: Stefan Berger 
> Cc: Rob Herring 
> Cc: Frank Rowand 
> Cc: Mimi Zohar 
> Reviewed-by: Mimi Zohar 
> Reviewed-by: Rob Herring 
> Tested-by: Nageswara R Sastry 
> Tested-by: Coiby Xu 
> 

Reviewed-by: Jerry Snitselaar 

> ---
> v6:
>  - Add __init to get_kexec_buffer as suggested by Jonathan
> 
> v5:
>  - Rebased on Jonathan McDowell's commit "b69a2afd5afc x86/kexec: Carry
>forward IMA measurement log on kexec"
> v4:
>  - Move debug output into setup_buffer()
> ---
>  drivers/of/kexec.c | 126 ++---
>  1 file changed, 74 insertions(+), 52 deletions(-)
> 
> diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
> index 1373d7e0a9b3..fa8c0c75adf9 100644
> --- a/drivers/of/kexec.c
> +++ b/drivers/of/kexec.c
> @@ -117,45 +117,57 @@ static int do_get_kexec_buffer(const void *prop, int 
> len, unsigned long *addr,
>  }
>  
>  #ifdef CONFIG_HAVE_IMA_KEXEC
> -/**
> - * ima_get_kexec_buffer - get IMA buffer from the previous kernel
> - * @addr:On successful return, set to point to the buffer contents.
> - * @size:On successful return, set to the buffer size.
> - *
> - * Return: 0 on success, negative errno on error.
> - */
> -int __init ima_get_kexec_buffer(void **addr, size_t *size)
> +static int __init get_kexec_buffer(const char *name, unsigned long *addr,
> +size_t *size)
>  {
>   int ret, len;
> - unsigned long tmp_addr;
>   unsigned long start_pfn, end_pfn;
> - size_t tmp_size;
>   const void *prop;
>  
> - prop = of_get_property(of_chosen, "linux,ima-kexec-buffer", );
> + prop = of_get_property(of_chosen, name, );
>   if (!prop)
>   return -ENOENT;
>  
> - ret = do_get_kexec_buffer(prop, len, _addr, _size);
> + ret = do_get_kexec_buffer(prop, len, addr, size);
>   if (ret)
>   return ret;
>  
> - /* Do some sanity on the returned size for the ima-kexec buffer */
> - if (!tmp_size)
> + /* Do some sanity on the returned size for the kexec buffer */
> + if (!*size)
>   return -ENOENT;
>  
>   /*
>* Calculate the PFNs for the buffer and ensure
>* they are with in addressable memory.
>*/
> - start_pfn = PHYS_PFN(tmp_addr);
> - end_pfn = PHYS_PFN(tmp_addr + tmp_size - 1);
> + start_pfn = PHYS_PFN(*addr);
> + end_pfn = PHYS_PFN(*addr + *size - 1);
>   if (!page_is_ram(start_pfn) || !page_is_ram(end_pfn)) {
> - pr_warn("IMA buffer at 0x%lx, size = 0x%zx beyond memory\n",
> - tmp_addr, tmp_size);
> + pr_warn("%s buffer at 0x%lx, size = 0x%zx beyond memory\n",
> + name, *addr, *size);
>   return -EINVAL;
>   }
>  
> + return 0;
> +}
> +
> +/**
> + * ima_get_kexec_buffer - get IMA buffer from the previous kernel
> + * @addr:On successful return, set to point to the buffer contents.
> + * @size:On successful return, set to the buffer size.
> + *
> + * Return: 0 on success, negative errno on error.
> + */
> +int __init ima_get_kexec_buffer(void **addr, size_t *size)
> +{
> + int ret;
> + unsigned long tmp_addr;
> + size_t tmp_size;
> +
> + ret = get_kexec_buffer("linux,ima-kexec-buffer", _addr, _size);
> + if (ret)
> + return ret;
> +
>   *addr = __va(tmp_addr);
>   *size = tmp_size;
>  
> @@ -188,72 +200,82 @@ int __init ima_free_kexec_buffer(void)
>  }
>  #endif
>  
> -/**
> - * remove_ima_buffer - remove the IMA buffer property and reservation from 
> @fdt
> - *
> - * @fdt: Flattened Device Tree to update
> - * @chosen_node: Offset to the chosen node in the device tree
> - *
> - * The IMA measurement buffer is of no use to a subsequent kernel, so we 
> always
> - * remove it from the device tree.
> - */
> -static void remove_ima_buffer(void *fdt, int chosen_node)
> +static int remove_buffer(void *fdt, int chosen_node, const char *name)
>  {
>   int ret, len;
>   unsigned long addr;
>   size_t size;
>   const void *prop;
>  
> - if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC))
> - return;
> -
> - prop = fdt_getprop(fdt, chosen_node, "linux,ima-kexec-buffer", );
> + prop = fdt_getprop(fdt, chosen_node, name, );
>   if (!prop)
> - return;
> + return -ENOENT;
>  
>   ret = do_get_kexec_buffer(prop, len, , );
> - fdt_delprop(fdt, chosen_node, "linux,ima-kexec-buffer");
> + fdt_delprop(fdt, chosen_node, name);
>   if (ret)
> - return;
> + return ret;
>  
>   ret = fdt_find_and_del_mem_rsv(fdt, addr, size);
>   if (!ret)
> - pr_debug("Removed old IMA buffer reservation.\n");
> + pr_debug("Remove old %s buffer reserveration", name);
> + return ret;
>  }
>  
> 

Re: [PATCH v9 2/4] tpm: of: Make of-tree specific function commonly available

2023-05-24 Thread Jerry Snitselaar
On Tue, Apr 18, 2023 at 09:44:07AM -0400, Stefan Berger wrote:
> Simplify tpm_read_log_of() by moving reusable parts of the code into
> an inline function that makes it commonly available so it can be
> used also for kexec support. Call the new of_tpm_get_sml_parameters()
> function from the TPM Open Firmware driver.
> 
> Signed-off-by: Stefan Berger 
> Cc: Jarkko Sakkinen 
> Cc: Jason Gunthorpe 
> Cc: Rob Herring 
> Cc: Frank Rowand 
> Reviewed-by: Mimi Zohar 
> Tested-by: Nageswara R Sastry 
> Tested-by: Coiby Xu 
> Acked-by: Jarkko Sakkinen 
> 

Reviewed-by: Jerry Snitselaar 

> ---
> v9:
>  - Rebased on 6.3-rc7; call tpm_read_log_memory_region if -ENODEV returned
> 
> v7:
>  - Added original comment back into inlined function
> 
> v4:
>  - converted to inline function
> ---
>  drivers/char/tpm/eventlog/of.c | 30 +---
>  include/linux/tpm.h| 36 ++
>  2 files changed, 41 insertions(+), 25 deletions(-)
> 
> diff --git a/drivers/char/tpm/eventlog/of.c b/drivers/char/tpm/eventlog/of.c
> index 930fe43d5daf..41688d9cbd3b 100644
> --- a/drivers/char/tpm/eventlog/of.c
> +++ b/drivers/char/tpm/eventlog/of.c
> @@ -51,11 +51,10 @@ static int tpm_read_log_memory_region(struct tpm_chip 
> *chip)
>  int tpm_read_log_of(struct tpm_chip *chip)
>  {
>   struct device_node *np;
> - const u32 *sizep;
> - const u64 *basep;
>   struct tpm_bios_log *log;
>   u32 size;
>   u64 base;
> + int ret;
>  
>   log = >log;
>   if (chip->dev.parent && chip->dev.parent->of_node)
> @@ -66,30 +65,11 @@ int tpm_read_log_of(struct tpm_chip *chip)
>   if (of_property_read_bool(np, "powered-while-suspended"))
>   chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
>  
> - sizep = of_get_property(np, "linux,sml-size", NULL);
> - basep = of_get_property(np, "linux,sml-base", NULL);
> - if (sizep == NULL && basep == NULL)
> + ret = of_tpm_get_sml_parameters(np, , );
> + if (ret == -ENODEV)
>   return tpm_read_log_memory_region(chip);
> - if (sizep == NULL || basep == NULL)
> - return -EIO;
> -
> - /*
> -  * For both vtpm/tpm, firmware has log addr and log size in big
> -  * endian format. But in case of vtpm, there is a method called
> -  * sml-handover which is run during kernel init even before
> -  * device tree is setup. This sml-handover function takes care
> -  * of endianness and writes to sml-base and sml-size in little
> -  * endian format. For this reason, vtpm doesn't need conversion
> -  * but physical tpm needs the conversion.
> -  */
> - if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
> - of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
> - size = be32_to_cpup((__force __be32 *)sizep);
> - base = be64_to_cpup((__force __be64 *)basep);
> - } else {
> - size = *sizep;
> - base = *basep;
> - }
> + if (ret < 0)
> + return ret;
>  
>   if (size == 0) {
>   dev_warn(>dev, "%s: Event log area empty\n", __func__);
> diff --git a/include/linux/tpm.h b/include/linux/tpm.h
> index 4dc97b9f65fb..dd9a376a1dbb 100644
> --- a/include/linux/tpm.h
> +++ b/include/linux/tpm.h
> @@ -461,4 +461,40 @@ static inline struct tpm_chip *tpm_default_chip(void)
>   return NULL;
>  }
>  #endif
> +
> +#ifdef CONFIG_OF
> +static inline int of_tpm_get_sml_parameters(struct device_node *np,
> + u64 *base, u32 *size)
> +{
> + const u32 *sizep;
> + const u64 *basep;
> +
> + sizep = of_get_property(np, "linux,sml-size", NULL);
> + basep = of_get_property(np, "linux,sml-base", NULL);
> + if (sizep == NULL && basep == NULL)
> + return -ENODEV;
> + if (sizep == NULL || basep == NULL)
> + return -EIO;
> +
> + /*
> +  * For both vtpm/tpm, firmware has log addr and log size in big
> +  * endian format. But in case of vtpm, there is a method called
> +  * sml-handover which is run during kernel init even before
> +  * device tree is setup. This sml-handover function takes care
> +  * of endianness and writes to sml-base and sml-size in little
> +  * endian format. For this reason, vtpm doesn't need conversion
> +  * but physical tpm needs the conversion.
> +  */
> + if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
> + of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
> + *size = be32_to_cpup((__force __be32 *)sizep);
> + *base = be64_to_cpup((__force __be64 *)basep);
> + } else {
> + *size = *sizep;
> + *base = *basep;
> + }
> + return 0;
> +}
> +#endif
> +
>  #endif
> -- 
> 2.38.1
> 


___
kexec mailing list
kexec@lists.infradead.org

Re: [PATCH v9 1/4] drivers: of: kexec ima: Support 32-bit platforms

2023-05-24 Thread Jerry Snitselaar
On Tue, Apr 18, 2023 at 09:44:06AM -0400, Stefan Berger wrote:
> From: Palmer Dabbelt 
> 
> RISC-V recently added kexec_file() support, which uses enables kexec
> IMA.  We're the first 32-bit platform to support this, so we found a
> build bug.
> 
> Acked-by: Rob Herring 
> Signed-off-by: Palmer Dabbelt 
> Reviewed-by: Mimi Zohar 

Reviewed-by: Jerry Snitselaar 

> ---
>  drivers/of/kexec.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c
> index f26d2ba8a371..1373d7e0a9b3 100644
> --- a/drivers/of/kexec.c
> +++ b/drivers/of/kexec.c
> @@ -250,8 +250,8 @@ static int setup_ima_buffer(const struct kimage *image, 
> void *fdt,
>   if (ret)
>   return -EINVAL;
>  
> - pr_debug("IMA buffer at 0x%llx, size = 0x%zx\n",
> -  image->ima_buffer_addr, image->ima_buffer_size);
> + pr_debug("IMA buffer at 0x%pa, size = 0x%zx\n",
> +  >ima_buffer_addr, image->ima_buffer_size);
>  
>   return 0;
>  }
> -- 
> 2.38.1
> 


___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


Re: [PATCHv2 1/6] kexec: Change the image probe's prototype

2023-05-24 Thread Simon Horman
On Tue, May 16, 2023 at 03:31:19PM +0800, Pingfan Liu wrote:
> As more complicated kernel format occurs such as zboot, where the
> compressed kernel is stored as a payload. The straight forward
> decompression can not meet the demand.
> 
> A new image probe method is expected to read in the kernel file and decide
> how to unfold the content by itself.
> 
> This patch aims to change the image probe's prototype from
> typedef int (probe_t)(const char *kernel_buf, off_t kernel_size);
> to
> typedef int (probe_t)(const char *kernel_buf, off_t kernel_size, 
> struct kexec_info *info);
> 
> Later, info can be used to return both the file descriptor and the
> parsed kernel buffer.
> 
> In case your are curious, the remaing part of the log describes the
> process of the substitution of the new prototype, which can be divided
> into three groups.
> 
> 1. change function pointer and its callsites:
>   sed -i 's/(probe_t)(const char \*kernel_buf, off_t 
> kernel_size);/(probe_t)(const char \*kernel, off_t kernel_size, struct 
> kexec_info \*info);/g' kexec/kexec.h
>   sed -i 's/\.probe(\([^,]*\), \([^)]*\))/\.probe(\1, \2, NULL)/g' 
> kexec/kexec.c
> 
> 2. change the function declaration and definition of each 'probe'
>instance by coccinelle because they may cross lines
> 
> The cocci file looks like:
> 
> @ rule1 @
> identifier fn =~ "_probe";
> identifier buf, size;
> typedef off_t;
> @@
> 
> -int fn(const char *buf, off_t size)
> +int fn(const char *buf, off_t size, struct kexec_info *info)
> {
> ...
> }
> 
> @ rule2 @
> identifier fn =~ "_probe";
> identifier buf, size;
> typedef off_t;
> @@
> 
> +int fn(const char *buf, off_t size, struct kexec_info *info);
> -int fn(const char *buf, off_t size);
> 
> Then running the command
> spatch --sp-file cocci/define.cocci --dir kexec --include-headers > 
> ../define.patch
> git apply --directory=kexec ../define.patch
> 
> 3. change the direct calls to the probe instances
> 
> Originally I planned to achieve this by coccinelle, but failed similar
> to [1]. I have tried using "-I and --include" option for coccinelle, but it
> still did not work.
> 
> Checking the direct callsite by "git grep "_probe(" | grep -v const"
> Fortunatelly, it turns out that only a few direct callsites exist, which
> lies in i386, and easy to be amended manually.
> 
> Anyway, just FYI, the cocci file looks like:
> @ rule1 @
> identifier fn =~ "_probe";
> identifier buf, size;
> identifier info;
> typedef off_t;
> @@
> 
> int fn(const char *buf, off_t size, struct kexec_info *info);
> 
> /* change the direct callsite of any probe */
> @ rule2 @
> identifier rule1.fn;
> expression E1, E2;
> @@
> 
>  fn(E1, E2
> +  ,NULL
>)
> 
> Then running the command:
> spatch --sp-file cocci/direct.cocci --dir kexec --include-headers
> 
> [1]: 
> https://lore.kernel.org/all/alpine.DEB.2.22.394.2202280705080.3112@hadrien/T/
> 
> Signed-off-by: Pingfan Liu 
> To: kexec@lists.infradead.org
> Cc: ho...@verge.net.au
> Cc: a...@kernel.org
> Cc: jeremy.lin...@arm.com

Unfortunately I am seeing a build failure on (at least) sh4 with this change:

 ../../kexec/arch/sh/kexec-zImage-sh.c:54:5: error: conflicting types for 
‘zImage_sh_probe’
54 | int zImage_sh_probe(const char *buf, off_t UNUSED(len))
   | ^~~
 In file included from ../../kexec/arch/sh/kexec-zImage-sh.c:27:
 ../../kexec/arch/sh/kexec-sh.h:11:5: note: previous declaration of 
‘zImage_sh_probe’ was here
11 | int zImage_sh_probe(const char *buf, off_t len, struct kexec_info 
*info);
   | ^~~
 ../../kexec/arch/sh/kexec-zImage-sh.c:29:18: warning: ‘probe_debug’ defined 
but not used [-Wunused-const-variable=]
29 | static const int probe_debug = 0;
   |  ^~~
 make[1]: *** [Makefile:124: kexec/arch/sh/kexec-zImage-sh.o] Error 1
 make[1]: *** Waiting for unfinished jobs
 ../../kexec/arch/sh/kexec-netbsd-sh.c:28:18: warning: ‘probe_debug’ defined 
but not used [-Wunused-const-variable=]
28 | static const int probe_debug = 0;
   |  ^~~

Link: 
https://github.com/horms/kexec-tools/actions/runs/5068250493/jobs/9100319093#step:5:215

___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec