Skip to contentSkip to conversation

s390/crash: support multi-segment iterators

PATCH Superseded Alexander Gordeev 2 participants 7 messages Jul 18, 2022

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Alexander Gordeev

author maintainer
#0 lore raw

v3 cover s390/crash: support multi-segment iterators

Hi Matthew et al,

This series completes 5d8de293c224 ("vmcore: convert copy_oldmem_page() to take an iov_iter") for s390.

@Matthew,
Please, let me know if you are not okay with Suggested-by tag in patch 3.

Changes since v2:
- Matthew Wilcox suggestion is adopted, with that...
- copy_to_iter() is used instead of custom implementation;

Changes since v1:
- number of bytes left to copy on fail fixed;

Thanks!

Alexander Gordeev (3):
s390/crash: move copy_to_user_real() to crash_dump.c s390/crash: use static swap buffer for copy_to_user_real() s390/crash: support multi-segment iterators

 arch/s390/include/asm/os_info.h |  17 ++++-
 arch/s390/include/asm/sclp.h    |   4 +-
 arch/s390/include/asm/uaccess.h |   1 -
 arch/s390/kernel/crash_dump.c   | 111 +++++++-------------------------
 arch/s390/mm/maccess.c          |  26 --------
 drivers/s390/char/zcore.c       |  59 +++++++----------
 6 files changed, 65 insertions(+), 153 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v3 1/3 s390/crash: move copy_to_user_real() to crash_dump.c

Function copy_to_user_real() does not really belong to maccess.c. It is only used for copying oldmem to user space, so let's move it to the friends.

---
 arch/s390/include/asm/uaccess.h |  1 -
 arch/s390/kernel/crash_dump.c   | 26 ++++++++++++++++++++++++++
 arch/s390/mm/maccess.c          | 26 --------------------------
 3 files changed, 26 insertions(+), 27 deletions(-)
arch/s390/include/asm/uaccess.h +0 -1
index f4511e21d646..2a067315fe59 100644
@@ -285,7 +285,6 @@ static inline unsigned long __must_check clear_user(void __user *to, unsigned lo
 	return __clear_user(to, n);
 }
 
-int copy_to_user_real(void __user *dest, unsigned long src, unsigned long count);
 void *s390_kernel_write(void *dst, const void *src, size_t size);
 
 int __noreturn __put_kernel_bad(void);
arch/s390/kernel/crash_dump.c +26 -0
index 0efee5c49b1e..8d7332d4444c 100644
@@ -173,6 +173,32 @@ int copy_oldmem_kernel(void *dst, unsigned long src, size_t count)
 	return 0;
 }
 
+/*
+ * Copy memory from kernel (real) to user (virtual)
+ */
+static int copy_to_user_real(void __user *dest, unsigned long src, unsigned long count)
+{
+	int offs = 0, size, rc;
+	char *buf;
+
+	buf = (char *)__get_free_page(GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+	rc = -EFAULT;
+	while (offs < count) {
+		size = min(PAGE_SIZE, count - offs);
+		if (memcpy_real(buf, src + offs, size))
+			goto out;
+		if (copy_to_user(dest + offs, buf, size))
+			goto out;
+		offs += size;
+	}
+	rc = 0;
+out:
+	free_page((unsigned long)buf);
+	return rc;
+}
+
 /*
  * Copy memory of the old, dumped system to a user space virtual address
  */
arch/s390/mm/maccess.c +0 -26
index 421efa46946b..d6d84e02f35a 100644
@@ -171,32 +171,6 @@ void memcpy_absolute(void *dest, void *src, size_t count)
 	arch_local_irq_restore(flags);
 }
 
-/*
- * Copy memory from kernel (real) to user (virtual)
- */
-int copy_to_user_real(void __user *dest, unsigned long src, unsigned long count)
-{
-	int offs = 0, size, rc;
-	char *buf;
-
-	buf = (char *) __get_free_page(GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
-	rc = -EFAULT;
-	while (offs < count) {
-		size = min(PAGE_SIZE, count - offs);
-		if (memcpy_real(buf, src + offs, size))
-			goto out;
-		if (copy_to_user(dest + offs, buf, size))
-			goto out;
-		offs += size;
-	}
-	rc = 0;
-out:
-	free_page((unsigned long) buf);
-	return rc;
-}
-
 /*
  * Check if physical address is within prefix or zero page
  */
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH v3 2/3 s390/crash: use static swap buffer for copy_to_user_real()

In reply to Alexander Gordeev

Currently a temporary page-size buffer is allocated for copying oldmem to user space. That limits copy_to_user_real() operation only to stages when virtual memory is available and still makes it possible to fail while the system is being dumped.

Instead of reallocating single page on each copy_oldmem_page() iteration use a statically allocated buffer.

This also paves the way for a further memcpy_real() rework where no swap buffer is needed altogether.

---
 arch/s390/kernel/crash_dump.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
arch/s390/kernel/crash_dump.c +3 -7
index 8d7332d4444c..fd9fe93e48b6 100644
@@ -53,6 +53,7 @@ struct save_area {
 };
 
 static LIST_HEAD(dump_save_areas);
+static char memcpy_real_buf[PAGE_SIZE];
 
 /*
  * Allocate a save area
@@ -179,23 +180,18 @@ int copy_oldmem_kernel(void *dst, unsigned long src, size_t count)
 static int copy_to_user_real(void __user *dest, unsigned long src, unsigned long count)
 {
 	int offs = 0, size, rc;
-	char *buf;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 	rc = -EFAULT;
 	while (offs < count) {
 		size = min(PAGE_SIZE, count - offs);
-		if (memcpy_real(buf, src + offs, size))
+		if (memcpy_real(memcpy_real_buf, src + offs, size))
 			goto out;
-		if (copy_to_user(dest + offs, buf, size))
+		if (copy_to_user(dest + offs, memcpy_real_buf, size))
 			goto out;
 		offs += size;
 	}
 	rc = 0;
 out:
-	free_page((unsigned long)buf);
 	return rc;
 }
 
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v3 2/3] s390/crash: use static swap buffer for copy_to_user_real()

arch/s390/kernel/crash_dump.c struct save_area { · line 55

Quoted 5 lines · Alexander Gordeev

+++ b/arch/s390/kernel/crash_dump.c

arch/s390/kernel/crash_dump.c +0 -0
@@ -53,6 +53,7 @@ struct save_area {
 };
 
 static LIST_HEAD(dump_save_areas);

I'd suggest you need a mutex here so that simultaneous calls to copy_to_user_real() don't corrupt each others data.

Quoted 33 lines
arch/s390/kernel/crash_dump.c +3 -7
+static char memcpy_real_buf[PAGE_SIZE];
 
 /*
  * Allocate a save area
@@ -179,23 +180,18 @@ int copy_oldmem_kernel(void *dst, unsigned long src, size_t count)
 static int copy_to_user_real(void __user *dest, unsigned long src, unsigned long count)
 {
 	int offs = 0, size, rc;
-	char *buf;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 	rc = -EFAULT;
 	while (offs < count) {
 		size = min(PAGE_SIZE, count - offs);
-		if (memcpy_real(buf, src + offs, size))
+		if (memcpy_real(memcpy_real_buf, src + offs, size))
 			goto out;
-		if (copy_to_user(dest + offs, buf, size))
+		if (copy_to_user(dest + offs, memcpy_real_buf, size))
 			goto out;
 		offs += size;
 	}
 	rc = 0;
 out:
-	free_page((unsigned long)buf);
 	return rc;
 }
 
Signature · 1 line
2.34.1
author maintainer
#4 lore raw

Re: [PATCH v3 2/3] s390/crash: use static swap buffer for copy_to_user_real()

Quoted 9 lines · Matthew Wilcox

arch/s390/kernel/crash_dump.c struct save_area { · line 55

Quoted 5 lines · Alexander Gordeev

+++ b/arch/s390/kernel/crash_dump.c

arch/s390/kernel/crash_dump.c +0 -0
@@ -53,6 +53,7 @@ struct save_area {
 };
 
 static LIST_HEAD(dump_save_areas);

I'd suggest you need a mutex here so that simultaneous calls to copy_to_user_real() don't corrupt each others data.

We stop all (but one) CPUs before calling into the capture kernel - one that calls these functions. Similarily to racy hsa_buf[] access from memcpy_hsa_iter() this should not hit.

As you noticed last time, it is a pre-existing race and I was actually going to address it in a separate fix - if the problem really exists.

Quoted 21 lines
Quoted 21 lines
arch/s390/kernel/crash_dump.c +3 -6
+static char memcpy_real_buf[PAGE_SIZE];
 
 /*
  * Allocate a save area
@@ -179,23 +180,18 @@ int copy_oldmem_kernel(void *dst, unsigned long src, size_t count)
 static int copy_to_user_real(void __user *dest, unsigned long src, unsigned long count)
 {
 	int offs = 0, size, rc;
-	char *buf;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 	rc = -EFAULT;
 	while (offs < count) {
 		size = min(PAGE_SIZE, count - offs);
-		if (memcpy_real(buf, src + offs, size))
+		if (memcpy_real(memcpy_real_buf, src + offs, size))
 			goto out;
-		if (copy_to_user(dest + offs, buf, size))
+		if (copy_to_user(dest + offs, memcpy_real_buf, size))
Quoted 8 lines

arch/s390/kernel/crash_dump.c int copy_oldmem_kernel(void *dst, unsigned long src, size_t count) · line 196

Quoted 8 lines
arch/s390/kernel/crash_dump.c +0 -1
 			goto out;
 		offs += size;
 	}
 	rc = 0;
 out:
-	free_page((unsigned long)buf);
 	return rc;
 }

Thanks!

#5 lore raw

Re: [PATCH v3 2/3] s390/crash: use static swap buffer for copy_to_user_real()

Quoted 6 lines · Alexander Gordeev
Quoted 5 lines · Matthew Wilcox
Quoted 4 lines · Alexander Gordeev

+++ b/arch/s390/kernel/crash_dump.c

(quoted diff) +0 -0
@@ -53,6 +53,7 @@ struct save_area {
 };
 
Quoted 8 lines
Quoted 4 lines

arch/s390/kernel/crash_dump.c struct save_area { · line 55

Quoted 1 line
arch/s390/kernel/crash_dump.c +0 -0
 static LIST_HEAD(dump_save_areas);

I'd suggest you need a mutex here so that simultaneous calls to copy_to_user_real() don't corrupt each others data.

We stop all (but one) CPUs before calling into the capture kernel - one that calls these functions. Similarily to racy hsa_buf[] access from memcpy_hsa_iter() this should not hit.

Could you show me how that works when two processes read from /proc/vmcore at the same time?

Quoted 35 lines

As you noticed last time, it is a pre-existing race and I was actually going to address it in a separate fix - if the problem really exists.

Quoted 21 lines
Quoted 21 lines
arch/s390/kernel/crash_dump.c +3 -6
+static char memcpy_real_buf[PAGE_SIZE];
 
 /*
  * Allocate a save area
@@ -179,23 +180,18 @@ int copy_oldmem_kernel(void *dst, unsigned long src, size_t count)
 static int copy_to_user_real(void __user *dest, unsigned long src, unsigned long count)
 {
 	int offs = 0, size, rc;
-	char *buf;
 
-	buf = (char *)__get_free_page(GFP_KERNEL);
-	if (!buf)
-		return -ENOMEM;
 	rc = -EFAULT;
 	while (offs < count) {
 		size = min(PAGE_SIZE, count - offs);
-		if (memcpy_real(buf, src + offs, size))
+		if (memcpy_real(memcpy_real_buf, src + offs, size))
 			goto out;
-		if (copy_to_user(dest + offs, buf, size))
+		if (copy_to_user(dest + offs, memcpy_real_buf, size))
Quoted 8 lines

arch/s390/kernel/crash_dump.c int copy_oldmem_kernel(void *dst, unsigned long src, size_t count) · line 196

Quoted 8 lines
arch/s390/kernel/crash_dump.c +0 -1
 			goto out;
 		offs += size;
 	}
 	rc = 0;
 out:
-	free_page((unsigned long)buf);
 	return rc;
 }

Thanks!

author maintainer
#6 lore raw

PATCH v3 3/3 s390/crash: support multi-segment iterators

In reply to Alexander Gordeev

Make it possible to handle not only single-, but also multi- segment iterators in copy_oldmem_iter() callback. Change the semantics of called functions to match the iterator model - instead of an error code the exact number of bytes copied is returned.

The swap page used to copy data to user space is adopted for kernel space too. That does not bring any performance impact.

Fixes 49b11524d648 ("s390/crash: add missing iterator advance in copy_oldmem_page()")
---
 arch/s390/include/asm/os_info.h |  17 ++++-
 arch/s390/include/asm/sclp.h    |   4 +-
 arch/s390/kernel/crash_dump.c   | 131 ++++++--------------------------
 drivers/s390/char/zcore.c       |  59 ++++++--------
 4 files changed, 64 insertions(+), 147 deletions(-)
arch/s390/include/asm/os_info.h +16 -1
index 147a8d547ef9..85248d8fee0c 100644
@@ -8,6 +8,8 @@
 #ifndef _ASM_S390_OS_INFO_H
 #define _ASM_S390_OS_INFO_H
 
+#include <linux/uio.h>
+
 #define OS_INFO_VERSION_MAJOR	1
 #define OS_INFO_VERSION_MINOR	1
 #define OS_INFO_MAGIC		0x4f53494e464f535aULL /* OSINFOSZ */
@@ -39,7 +41,20 @@ u32 os_info_csum(struct os_info *os_info);
 
 #ifdef CONFIG_CRASH_DUMP
 void *os_info_old_entry(int nr, unsigned long *size);
-int copy_oldmem_kernel(void *dst, unsigned long src, size_t count);
+size_t copy_oldmem_iter(struct iov_iter *iter, unsigned long src, size_t count);
+
+static inline int copy_oldmem_kernel(void *dst, unsigned long src, size_t count)
+{
+	struct iov_iter iter;
+	struct kvec kvec;
+
+	kvec.iov_base = dst;
+	kvec.iov_len = count;
+	iov_iter_kvec(&iter, WRITE, &kvec, 1, count);
+	if (copy_oldmem_iter(&iter, src, count) < count)
+		return -EFAULT;
+	return 0;
+}
 #else
 static inline void *os_info_old_entry(int nr, unsigned long *size)
 {
arch/s390/include/asm/sclp.h +2 -2
index addefe8ccdba..9d4c7f71e070 100644
@@ -17,6 +17,7 @@
 #define EXT_SCCB_READ_CPU	(3 * PAGE_SIZE)
 
 #ifndef __ASSEMBLY__
+#include <linux/uio.h>
 #include <asm/chpid.h>
 #include <asm/cpu.h>
 
@@ -146,8 +147,7 @@ int sclp_pci_deconfigure(u32 fid);
 int sclp_ap_configure(u32 apid);
 int sclp_ap_deconfigure(u32 apid);
 int sclp_pci_report(struct zpci_report_error_header *report, u32 fh, u32 fid);
-int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count);
-int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count);
+size_t memcpy_hsa_iter(struct iov_iter *iter, unsigned long src, size_t count);
 void sclp_ocf_cpc_name_copy(char *dst);
 
 static inline int sclp_get_core_info(struct sclp_core_info *info, int early)
arch/s390/kernel/crash_dump.c +23 -108

165 lines not on this page · raw message

drivers/s390/char/zcore.c +23 -36

90 lines not on this page · raw message

Signature · 1 line
2.34.1