Re: [PATCH 5/5 V4] Help to dump the old memory encrypted into vmcore file

2018-06-28 Thread kbuild test robot
Hi Lianbo,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.18-rc2 next-20180628]
[cannot apply to tip/x86/core]
[if your patch is applied to the wrong git tree, please drop us a note to help 
improve the system]

url:
https://github.com/0day-ci/linux/commits/Lianbo-Jiang/Add-a-function-ioremap_encrypted-for-kdump-when-AMD-sme-enabled/20180628-173357
config: arm64-defconfig (attached as .config)
compiler: aarch64-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
wget 
https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O 
~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.2.0 make.cross ARCH=arm64 

All errors (new ones prefixed by >>):

   fs/proc/vmcore.c: In function 'elfcorehdr_read':
>> fs/proc/vmcore.c:180:9: error: implicit declaration of function 'memremap'; 
>> did you mean 'memset_p'? [-Werror=implicit-function-declaration]
 kbuf = memremap(offset, count, MEMREMAP_WB);
^~~~
memset_p
   fs/proc/vmcore.c:180:33: error: 'MEMREMAP_WB' undeclared (first use in this 
function)
 kbuf = memremap(offset, count, MEMREMAP_WB);
^~~
   fs/proc/vmcore.c:180:33: note: each undeclared identifier is reported only 
once for each function it appears in
   fs/proc/vmcore.c:185:2: error: implicit declaration of function 'memunmap'; 
did you mean 'vm_munmap'? [-Werror=implicit-function-declaration]
 memunmap(kbuf);
 ^~~~
 vm_munmap
   cc1: some warnings being treated as errors

vim +180 fs/proc/vmcore.c

   158  
   159  /*
   160   * Architectures may override this function to read from ELF header.
   161   * The kexec-tools will allocated the memory and build the elf header
   162   * in the first kernel, subsequently, we will copy the data in the
   163   * memory to the reserved crash memory. In kdump mode, we will read the
   164   * elf header from the reserved crash memory, from this point of view,
   165   * which is not an old memory, the original function called may mislead
   166   * and do unnecessary things.
   167   * For SME, it copies the elf header from the memory encrypted(user 
space)
   168   * to the memory unencrypted(kernel space) when SME is activated in the
   169   * first kernel, this operation just leads to decryption.
   170   */
   171  ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
   172  {
   173  char *kbuf;
   174  resource_size_t offset;
   175  
   176  if (!count)
   177  return 0;
   178  
   179  offset = (resource_size_t)*ppos;
 > 180  kbuf = memremap(offset, count, MEMREMAP_WB);
   181  if (!kbuf)
   182  return 0;
   183  
   184  memcpy(buf, kbuf, count);
   185  memunmap(kbuf);
   186  
   187  return count;
   188  }
   189  

---
0-DAY kernel test infrastructureOpen Source Technology Center
https://lists.01.org/pipermail/kbuild-all   Intel Corporation


.config.gz
Description: application/gzip
___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


Re: [PATCH 5/5 V4] Help to dump the old memory encrypted into vmcore file

2018-06-28 Thread kbuild test robot
Hi Lianbo,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.18-rc2 next-20180628]
[cannot apply to tip/x86/core]
[if your patch is applied to the wrong git tree, please drop us a note to help 
improve the system]

url:
https://github.com/0day-ci/linux/commits/Lianbo-Jiang/Add-a-function-ioremap_encrypted-for-kdump-when-AMD-sme-enabled/20180628-173357
config: s390-allyesconfig (attached as .config)
compiler: s390x-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
wget 
https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O 
~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.2.0 make.cross ARCH=s390 

All errors (new ones prefixed by >>):

   fs/proc/vmcore.c: In function 'elfcorehdr_read':
>> fs/proc/vmcore.c:180:9: error: implicit declaration of function 'memremap'; 
>> did you mean 'ioremap'? [-Werror=implicit-function-declaration]
 kbuf = memremap(offset, count, MEMREMAP_WB);
^~~~
ioremap
>> fs/proc/vmcore.c:180:33: error: 'MEMREMAP_WB' undeclared (first use in this 
>> function)
 kbuf = memremap(offset, count, MEMREMAP_WB);
^~~
   fs/proc/vmcore.c:180:33: note: each undeclared identifier is reported only 
once for each function it appears in
>> fs/proc/vmcore.c:185:2: error: implicit declaration of function 'memunmap'; 
>> did you mean 'vm_munmap'? [-Werror=implicit-function-declaration]
 memunmap(kbuf);
 ^~~~
 vm_munmap
   cc1: some warnings being treated as errors

vim +180 fs/proc/vmcore.c

   158  
   159  /*
   160   * Architectures may override this function to read from ELF header.
   161   * The kexec-tools will allocated the memory and build the elf header
   162   * in the first kernel, subsequently, we will copy the data in the
   163   * memory to the reserved crash memory. In kdump mode, we will read the
   164   * elf header from the reserved crash memory, from this point of view,
   165   * which is not an old memory, the original function called may mislead
   166   * and do unnecessary things.
   167   * For SME, it copies the elf header from the memory encrypted(user 
space)
   168   * to the memory unencrypted(kernel space) when SME is activated in the
   169   * first kernel, this operation just leads to decryption.
   170   */
   171  ssize_t __weak elfcorehdr_read(char *buf, size_t count, u64 *ppos)
   172  {
   173  char *kbuf;
   174  resource_size_t offset;
   175  
   176  if (!count)
   177  return 0;
   178  
   179  offset = (resource_size_t)*ppos;
 > 180  kbuf = memremap(offset, count, MEMREMAP_WB);
   181  if (!kbuf)
   182  return 0;
   183  
   184  memcpy(buf, kbuf, count);
 > 185  memunmap(kbuf);
   186  
   187  return count;
   188  }
   189  

---
0-DAY kernel test infrastructureOpen Source Technology Center
https://lists.01.org/pipermail/kbuild-all   Intel Corporation


.config.gz
Description: application/gzip
___
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec


[PATCH 5/5 V4] Help to dump the old memory encrypted into vmcore file

2018-06-28 Thread Lianbo Jiang
In kdump mode, we need to dump the old memory into vmcore file,
if SME is enabled in the first kernel, we must remap the old
memory in encrypted manner, which will be automatically decrypted
when we read from DRAM. It helps to parse the vmcore for some tools.

Signed-off-by: Lianbo Jiang 
---
Some changes:
1. add a new file and modify Makefile.
2. revert some code about previously using sev_active().
3. modify elfcorehdr_read().

 arch/x86/kernel/Makefile |  1 +
 arch/x86/kernel/crash_dump_encrypt.c | 53 
 fs/proc/vmcore.c | 45 +-
 include/linux/crash_dump.h   | 12 
 4 files changed, 104 insertions(+), 7 deletions(-)
 create mode 100644 arch/x86/kernel/crash_dump_encrypt.c

diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 02d6f5c..afb5bad 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -96,6 +96,7 @@ obj-$(CONFIG_KEXEC_CORE)  += machine_kexec_$(BITS).o
 obj-$(CONFIG_KEXEC_CORE)   += relocate_kernel_$(BITS).o crash.o
 obj-$(CONFIG_KEXEC_FILE)   += kexec-bzimage64.o
 obj-$(CONFIG_CRASH_DUMP)   += crash_dump_$(BITS).o
+obj-$(CONFIG_AMD_MEM_ENCRYPT)  += crash_dump_encrypt.o
 obj-y  += kprobes/
 obj-$(CONFIG_MODULES)  += module.o
 obj-$(CONFIG_DOUBLEFAULT)  += doublefault.o
diff --git a/arch/x86/kernel/crash_dump_encrypt.c 
b/arch/x86/kernel/crash_dump_encrypt.c
new file mode 100644
index 000..e1b1a57
--- /dev/null
+++ b/arch/x86/kernel/crash_dump_encrypt.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Memory preserving reboot related code.
+ *
+ * Created by: Lianbo Jiang (liji...@redhat.com)
+ * Copyright (C) RedHat Corporation, 2018. All rights reserved
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+/**
+ * copy_oldmem_page_encrypted - copy one page from "oldmem encrypted"
+ * @pfn: page frame number to be copied
+ * @buf: target memory address for the copy; this can be in kernel address
+ * space or user address space (see @userbuf)
+ * @csize: number of bytes to copy
+ * @offset: offset in bytes into the page (based on pfn) to begin the copy
+ * @userbuf: if set, @buf is in user address space, use copy_to_user(),
+ * otherwise @buf is in kernel address space, use memcpy().
+ *
+ * Copy a page from "oldmem encrypted". For this page, there is no pte
+ * mapped in the current kernel. We stitch up a pte, similar to
+ * kmap_atomic.
+ */
+
+ssize_t copy_oldmem_page_encrypted(unsigned long pfn, char *buf,
+   size_t csize, unsigned long offset, int userbuf)
+{
+   void  *vaddr;
+
+   if (!csize)
+   return 0;
+
+   vaddr = (__force void *)ioremap_encrypted(pfn << PAGE_SHIFT,
+ PAGE_SIZE);
+   if (!vaddr)
+   return -ENOMEM;
+
+   if (userbuf) {
+   if (copy_to_user((void __user *)buf, vaddr + offset, csize)) {
+   iounmap((void __iomem *)vaddr);
+   return -EFAULT;
+   }
+   } else
+   memcpy(buf, vaddr + offset, csize);
+
+   set_iounmap_nonlazy();
+   iounmap((void __iomem *)vaddr);
+   return csize;
+}
diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c
index cfb6674..5fef489 100644
--- a/fs/proc/vmcore.c
+++ b/fs/proc/vmcore.c
@@ -26,6 +26,8 @@
 #include 
 #include 
 #include "internal.h"
+#include 
+#include 
 
 /* List representing chunks of contiguous memory areas and their offsets in
  * vmcore file.
@@ -98,7 +100,8 @@ static int pfn_is_ram(unsigned long pfn)
 
 /* Reads a page from the oldmem device from given offset. */
 static ssize_t read_from_oldmem(char *buf, size_t count,
-   u64 *ppos, int userbuf)
+   u64 *ppos, int userbuf,
+   bool encrypted)
 {
unsigned long pfn, offset;
size_t nr_bytes;
@@ -120,8 +123,11 @@ static ssize_t read_from_oldmem(char *buf, size_t count,
if (pfn_is_ram(pfn) == 0)
memset(buf, 0, nr_bytes);
else {
-   tmp = copy_oldmem_page(pfn, buf, nr_bytes,
-   offset, userbuf);
+   tmp = encrypted ? copy_oldmem_page_encrypted(pfn,
+   buf, nr_bytes, offset, userbuf)
+   : copy_oldmem_page(pfn, buf, nr_bytes,
+  offset, userbuf);
+
if (tmp < 0)
return tmp;
}
@@ -151,11 +157,34 @@ void __weak elfcorehdr_free(unsigned long long addr)
 {}
 
 /*
- * Architectures may override this function to read from ELF header
+ * Architectures may override this function to read from ELF header.
+ * The kexec-tools will