Skip to contentSkip to conversation

Add and use a generic copy_oldmem_page()

PATCH In review Palmer Dabbelt 6 participants 11 messages Jul 11, 2020 – Jul 16, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

While adding support for kexec, Nick recently copied the arm64 copy_oldmem_page() into the RISC-V port. Since this is shared verbatim with arm and arm64 already, I'd like to add a generic version and so we can use it instead. I haven't converted over the MIPS, PPC, or SH ports: while I think we could figure out how to share a version, they're not exactly the same right now. S/390 and x86 are definitely meaningfully different.

Unless there are any objections I'll include the first patch along with the RISC-V kexec support, which I'm hoping to have for 5.9. The code, based on 5.8-rc4, is at ssh://[email protected]/pub/scm/linux/kernel/git/palmer/linux.git -b copy_oldmem_page .

#1 lore raw

PATCH 1/3 lib: Add a generic copy_oldmem_page()

From: Palmer Dabbelt <[email protected]>

A version of this that is identical to the arm64 version was recently copied into the RISC-V port while adding kexec() support. Instead I'd like to put a shared copy in lib/ and use it from the various ports.

---
 lib/Kconfig            |  3 +++
 lib/Makefile           |  2 ++
 lib/copy_oldmem_page.c | 51 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)
 create mode 100644 lib/copy_oldmem_page.c
lib/Kconfig +3 -0
index df3f3da95990..25544abc9547 100644
@@ -676,3 +676,6 @@ config GENERIC_LIB_CMPDI2
 
 config GENERIC_LIB_UCMPDI2
 	bool
+
+config GENERIC_LIB_COPY_OLDMEM_PAGE
+	bool
lib/Makefile +2 -0
index b1c42c10073b..30d57d8b32b1 100644
@@ -318,3 +318,5 @@ obj-$(CONFIG_OBJAGG) += objagg.o
 # KUnit tests
 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
+
+obj-$(CONFIG_GENERIC_LIB_COPY_OLDMEM_PAGE) += copy_oldmem_page.o
lib/copy_oldmem_page.c new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.27.0.383.g050319c2ae-goog
#2 lore raw

Re: [PATCH 1/3] lib: Add a generic copy_oldmem_page()

lib/copy_oldmem_page.c line 37

Quoted 12 lines · Palmer Dabbelt
lib/copy_oldmem_page.c new file +12 -0
+ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
+			 size_t csize, unsigned long offset,
+			 int userbuf)
+{
+	void *vaddr;
+
+	if (!csize)
+		return 0;
+
+	vaddr = memremap(__pfn_to_phys(pfn), PAGE_SIZE, MEMREMAP_WB);
+	if (!vaddr)
+		return -ENOMEM;

Doing a memremap for every page is very inefficient. Also I don't see why you'd want to even do that. All memory is in the direct mapping for RISC-V. For other architecture that support highmem kmap_atomic_pfn would do the job, which is what I'd use in a generic version.

#3 lore raw

Re: [PATCH 1/3] lib: Add a generic copy_oldmem_page()

Quoted 10 lines · Christoph Hellwig
Quoted 9 lines · Palmer Dabbelt
lib/copy_oldmem_page.c new file +9 -0
+ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
+                      size_t csize, unsigned long offset,
+                      int userbuf)
+{
+     void *vaddr;
+
+     if (!csize)
+             return 0;
+
Quoted 8 lines

lib/copy_oldmem_page.c line 37

Quoted 3 lines
lib/copy_oldmem_page.c new file +3 -0
+     vaddr = memremap(__pfn_to_phys(pfn), PAGE_SIZE, MEMREMAP_WB);
+     if (!vaddr)
+             return -ENOMEM;

Doing a memremap for every page is very inefficient. Also I don't see why you'd want to even do that. All memory is in the direct mapping for RISC-V. For other architecture that support highmem kmap_atomic_pfn would do the job, which is what I'd use in a generic version.

I would expect the 'oldmem' data to not have a 'struct page', which would be a problem at least for the generic implementation of kmap_atomic_pfn()

include/linux/highmem.h:#define kmap_atomic_pfn(pfn) kmap_atomic(pfn_to_page(pfn))

kmap_atomic() might still work with a bogus page pointer if it only transforms it back into a pfn, but some implementations just have this one that cannot work:
static inline void *page_address(const struct page *page)

{
        return page->virtual;
}

I have not checked how the crash dump code works though, maybe it does work after all.

Arnd

#4 lore raw

Re: [PATCH 1/3] lib: Add a generic copy_oldmem_page()

Quoted 20 lines · Arnd Bergmann
Quoted 10 lines · Christoph Hellwig
Quoted 9 lines · Palmer Dabbelt
lib/copy_oldmem_page.c new file +9 -0
+ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
+                      size_t csize, unsigned long offset,
+                      int userbuf)
+{
+     void *vaddr;
+
+     if (!csize)
+             return 0;
+
Quoted 8 lines

lib/copy_oldmem_page.c line 37

Quoted 3 lines
lib/copy_oldmem_page.c new file +3 -0
+     vaddr = memremap(__pfn_to_phys(pfn), PAGE_SIZE, MEMREMAP_WB);
+     if (!vaddr)
+             return -ENOMEM;

Doing a memremap for every page is very inefficient. Also I don't see why you'd want to even do that. All memory is in the direct mapping for RISC-V. For other architecture that support highmem kmap_atomic_pfn would do the job, which is what I'd use in a generic version.

Quoted 2 lines

I would expect the 'oldmem' data to not have a 'struct page', which would be a problem at least for the generic implementation of kmap_atomic_pfn()

Why do you expect it to not have a struct page?

#5 lore raw

Re: [PATCH 1/3] lib: Add a generic copy_oldmem_page()

Quoted 21 lines · Christoph Hellwig
Quoted 20 lines · Arnd Bergmann
Quoted 18 lines · Christoph Hellwig

lib/copy_oldmem_page.c line 37

Quoted 12 lines · Palmer Dabbelt
lib/copy_oldmem_page.c new file +12 -0
+ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
+                      size_t csize, unsigned long offset,
+                      int userbuf)
+{
+     void *vaddr;
+
+     if (!csize)
+             return 0;
+
+     vaddr = memremap(__pfn_to_phys(pfn), PAGE_SIZE, MEMREMAP_WB);
+     if (!vaddr)
+             return -ENOMEM;

Doing a memremap for every page is very inefficient. Also I don't see why you'd want to even do that. All memory is in the direct mapping for RISC-V. For other architecture that support highmem kmap_atomic_pfn would do the job, which is what I'd use in a generic version.

Quoted 4 lines
Quoted 2 lines

I would expect the 'oldmem' data to not have a 'struct page', which would be a problem at least for the generic implementation of kmap_atomic_pfn()

Why do you expect it to not have a struct page?

I was under the impression that the kdump kernel only accesses a small amount of memory itself and would allocate its mem_map to fit that but not pages of that were used by the kernel it is dumping.

Arnd

#6 lore raw

Re: [PATCH 1/3] lib: Add a generic copy_oldmem_page()

In reply to Christoph Hellwig
Quoted 16 lines · Christoph Hellwig

lib/copy_oldmem_page.c line 37

Quoted 12 lines · Palmer Dabbelt
lib/copy_oldmem_page.c new file +12 -0
+ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
+                      size_t csize, unsigned long offset,
+                      int userbuf)
+{
+     void *vaddr;
+
+     if (!csize)
+             return 0;
+
+     vaddr = memremap(__pfn_to_phys(pfn), PAGE_SIZE, MEMREMAP_WB);
+     if (!vaddr)
+             return -ENOMEM;

Doing a memremap for every page is very inefficient.

memremap(MEMREMAP_WB) will reuse the existing linear address if the PA is covered by the linear mapping.

Quoted 4 lines

Also I don't see
why you'd want to even do that. All memory is in the direct mapping for RISC-V. For other architecture that support highmem kmap_atomic_pfn would do the job, which is what I'd use in a generic version.

#7 lore raw

PATCH 2/3 arm: Use the new generic copy_oldmem_page()

In reply to Palmer Dabbelt

From: Palmer Dabbelt <[email protected]>

This is exactly the same as the arm64 code, which I just into lib/ to avoid copying it into the RISC-V port. This builds with defconfig and with CRASH_DUMP=y.

---
 arch/arm/Kconfig             |  1 +
 arch/arm/kernel/Makefile     |  1 -
 arch/arm/kernel/crash_dump.c | 54 ------------------------------------
 3 files changed, 1 insertion(+), 55 deletions(-)
 delete mode 100644 arch/arm/kernel/crash_dump.c
arch/arm/Kconfig +1 -0
index 2ac74904a3ce..dfbeb14e9673 100644
@@ -1933,6 +1933,7 @@ config ATAGS_PROC
 
 config CRASH_DUMP
 	bool "Build kdump crash kernel (EXPERIMENTAL)"
+	select GENERIC_LIB_COPY_OLDMEM_PAGE
 	help
 	  Generate crash dump after being started by kexec. This should
 	  be normally only set in special crash dump kernels which are
arch/arm/kernel/Makefile +0 -1
index 89e5d864e923..b5310a90dfe4 100644
@@ -65,7 +65,6 @@ obj-$(CONFIG_KGDB)		+= kgdb.o patch.o
 obj-$(CONFIG_ARM_UNWIND)	+= unwind.o
 obj-$(CONFIG_HAVE_TCM)		+= tcm.o
 obj-$(CONFIG_OF)		+= devtree.o
-obj-$(CONFIG_CRASH_DUMP)	+= crash_dump.o
 obj-$(CONFIG_SWP_EMULATE)	+= swp_emulate.o
 CFLAGS_swp_emulate.o		:= -Wa,-march=armv7-a
 obj-$(CONFIG_HAVE_HW_BREAKPOINT)	+= hw_breakpoint.o
arch/arm/kernel/crash_dump.c deleted +0 -54

54 lines not on this page · raw message

Signature · 1 line
2.27.0.383.g050319c2ae-goog
#9 lore raw

PATCH 3/3 arm64: Use the new generic copy_oldmem_page()

In reply to Palmer Dabbelt

From: Palmer Dabbelt <[email protected]>

This is exactly the same as the arm64 code, which I just into lib/ to avoid copying it into the RISC-V port. This builds with defconfig.

---
 arch/arm64/Kconfig             |  1 +
 arch/arm64/kernel/crash_dump.c | 39 ----------------------------------
 2 files changed, 1 insertion(+), 39 deletions(-)
arch/arm64/Kconfig +1 -0
index 66dc41fd49f2..55b27d56b163 100644
@@ -1109,6 +1109,7 @@ comment "Support for PE file signature verification disabled"
 
 config CRASH_DUMP
 	bool "Build kdump crash kernel"
+	select GENERIC_LIB_COPY_OLDMEM_PAGE
 	help
 	  Generate crash dump after being started by kexec. This should
 	  be normally only set in special crash dump kernels which are
arch/arm64/kernel/crash_dump.c +0 -39
index e6e284265f19..197b92c249ba 100644
@@ -13,45 +13,6 @@
 #include <linux/uaccess.h>
 #include <asm/memory.h>
 
-/**
- * copy_oldmem_page() - copy one page from old kernel memory
- * @pfn: page frame number to be copied
- * @buf: buffer where the copied page is placed
- * @csize: number of bytes to copy
- * @offset: offset in bytes into the page
- * @userbuf: if set, @buf is in a user address space
- *
- * This function copies one page from old kernel memory into buffer pointed by
- * @buf. If @buf is in userspace, set @userbuf to %1. Returns number of bytes
- * copied or negative error in case of failure.
- */
-ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
-			 size_t csize, unsigned long offset,
-			 int userbuf)
-{
-	void *vaddr;
-
-	if (!csize)
-		return 0;
-
-	vaddr = memremap(__pfn_to_phys(pfn), PAGE_SIZE, MEMREMAP_WB);
-	if (!vaddr)
-		return -ENOMEM;
-
-	if (userbuf) {
-		if (copy_to_user((char __user *)buf, vaddr + offset, csize)) {
-			memunmap(vaddr);
-			return -EFAULT;
-		}
-	} else {
-		memcpy(buf, vaddr + offset, csize);
-	}
-
-	memunmap(vaddr);
-
-	return csize;
-}
-
 /**
  * elfcorehdr_read - read from ELF core header
  * @buf: buffer where the data is placed
Signature · 1 line
2.27.0.383.g050319c2ae-goog