Skip to contentSkip to conversation

riscv: Ensure the value of FP registers in the core dump file is up to date

PATCH In review Vincent Chen 3 participants 3 messages Aug 3, 2021 – Aug 25, 2021

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

PATCH v2 1/1 riscv: Ensure the value of FP registers in the core dump file is up to date

The value of FP registers in the core dump file comes from the thread.fstate. However, kernel saves the FP registers to the thread.fstate only before scheduling out the process. If no process switch happens during the exception handling process, kernel will not have a chance to save the latest value of FP registers to thread.fstate. It will cause the value of FP registers in the core dump file may be incorrect. To solve this problem, this patch force lets kernel save the FP register into the thread.fstate if the target task_struct equals the current.

---
Changes since the v1 patch
1. Include switch_to.h to avoid compiling error.

---
 arch/riscv/kernel/ptrace.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/ptrace.c +4 -0
index 1a85305720e8..9c0511119bad 100644
@@ -10,6 +10,7 @@
 #include <asm/ptrace.h>
 #include <asm/syscall.h>
 #include <asm/thread_info.h>
+#include <asm/switch_to.h>
 #include <linux/audit.h>
 #include <linux/ptrace.h>
 #include <linux/elf.h>
@@ -56,6 +57,9 @@ static int riscv_fpr_get(struct task_struct *target,
 {
 	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
 
+	if (target == current)
+		fstate_save(current, task_pt_regs(current));
+
 	membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
 	membuf_store(&to, fstate->fcsr);
 	return membuf_zero(&to, 4);	// explicitly pad
Signature · 7 lines
2.32.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · On Tue, 3 Aug 2021 17:27:51 +0800 Vincent Chen <[email protected]> wrote:

The value of FP registers in the core dump file comes from the thread.fstate. However, kernel saves the FP registers to the thread.fstate only before scheduling out the process. If no process switch happens during the exception handling process, kernel will not have a chance to save the latest value of FP registers to thread.fstate. It will cause the value of FP registers in the core dump file may be incorrect. To solve this problem, this patch force lets kernel save the FP register into the thread.fstate if the target task_struct equals the current.

Nice catch! I think there's another case too: since last kernel save, the FP registers has been modified by the userspace, we also need to force fstate_save() This patch fixes both cases.

Quoted 2 lines
Quoted 22 lines

---
Changes since the v1 patch
1. Include switch_to.h to avoid compiling error.

---
 arch/riscv/kernel/ptrace.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/ptrace.c +1 -0
index 1a85305720e8..9c0511119bad 100644
@@ -10,6 +10,7 @@
 #include <asm/ptrace.h>
 #include <asm/syscall.h>
 #include <asm/thread_info.h>
+#include <asm/switch_to.h>
 #include <linux/audit.h>
 #include <linux/ptrace.h>
 #include <linux/elf.h>

arch/riscv/kernel/ptrace.c static int riscv_fpr_get(struct task_struct *target, · line 65

Quoted 9 lines
arch/riscv/kernel/ptrace.c +3 -0
@@ -56,6 +57,9 @@ static int riscv_fpr_get(struct task_struct *target,
 {
 	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
 
+	if (target == current)
+		fstate_save(current, task_pt_regs(current));
+
 	membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
 	membuf_store(&to, fstate->fcsr);
 	return membuf_zero(&to, 4);	// explicitly pad

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

maintainer
#2 lore raw
In reply to Vincent Chen
Quoted 32 lines · On Tue, 03 Aug 2021 02:27:51 PDT (-0700), [email protected] wrote:

The value of FP registers in the core dump file comes from the thread.fstate. However, kernel saves the FP registers to the thread.fstate only before scheduling out the process. If no process switch happens during the exception handling process, kernel will not have a chance to save the latest value of FP registers to thread.fstate. It will cause the value of FP registers in the core dump file may be incorrect. To solve this problem, this patch force lets kernel save the FP register into the thread.fstate if the target task_struct equals the current.

---
Changes since the v1 patch
1. Include switch_to.h to avoid compiling error.

---
 arch/riscv/kernel/ptrace.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/ptrace.c +1 -0
index 1a85305720e8..9c0511119bad 100644
@@ -10,6 +10,7 @@
 #include <asm/ptrace.h>
 #include <asm/syscall.h>
 #include <asm/thread_info.h>
+#include <asm/switch_to.h>
 #include <linux/audit.h>
 #include <linux/ptrace.h>
 #include <linux/elf.h>

arch/riscv/kernel/ptrace.c static int riscv_fpr_get(struct task_struct *target, · line 65

Quoted 9 lines
arch/riscv/kernel/ptrace.c +3 -0
@@ -56,6 +57,9 @@ static int riscv_fpr_get(struct task_struct *target,
 {
 	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
 
+	if (target == current)
+		fstate_save(current, task_pt_regs(current));
+
 	membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
 	membuf_store(&to, fstate->fcsr);
 	return membuf_zero(&to, 4);	// explicitly pad

Thanks, this is on fixes.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv