Skip to contentSkip to conversation

x86/elf: Use savesegment() for segment register reads in ELF core dump

PATCH Applied Uros Bizjak 2 participants 8 messages Mar 30 – Mar 31

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

PATCH 1/4 x86/elf: Use savesegment() for segment register reads in ELF core dump

ELF_CORE_COPY_REGS() currently reads %ds, %es, %fs, and %gs using inline assembly and manual zero-extension. This results in redundant instructions like mov %eax,%eax.

Replace the inline assembly with the savesegment() helper, which automatically zero-extends the value to the full register width, eliminating unnecessary instructions.

For example, the %ds load sequence changes from:

  d03: 8c d8                 mov    %ds,%eax
  d05: 89 c0                 mov    %eax,%eax
  d07: 48 89 84 24 38 01 00  mov    %rax,0x138(%rsp)

d0e: 00

to:

  ce8: 8c d8                 mov    %ds,%eax
  cea: 48 89 84 24 38 01 00  mov    %rax,0x138(%rsp)

cf1: 00

thus eliminating the unnecessary zero-extending mov %eax,%eax.

No functional change intended.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/elf.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/include/asm/elf.h +4 -5
index 2ba5f166e58f..c7f98977663c 100644
@@ -187,7 +187,6 @@ void set_personality_ia32(bool);
 
 #define ELF_CORE_COPY_REGS(pr_reg, regs)			\
 do {								\
-	unsigned v;						\
 	(pr_reg)[0] = (regs)->r15;				\
 	(pr_reg)[1] = (regs)->r14;				\
 	(pr_reg)[2] = (regs)->r13;				\
@@ -211,10 +210,10 @@ do {								\
 	(pr_reg)[20] = (regs)->ss;				\
 	(pr_reg)[21] = x86_fsbase_read_cpu();			\
 	(pr_reg)[22] = x86_gsbase_read_cpu_inactive();		\
-	asm("movl %%ds,%0" : "=r" (v)); (pr_reg)[23] = v;	\
-	asm("movl %%es,%0" : "=r" (v)); (pr_reg)[24] = v;	\
-	asm("movl %%fs,%0" : "=r" (v)); (pr_reg)[25] = v;	\
-	asm("movl %%gs,%0" : "=r" (v)); (pr_reg)[26] = v;	\
+	savesegment(ds, (pr_reg)[23]);				\
+	savesegment(es, (pr_reg)[24]);				\
+	savesegment(fs, (pr_reg)[25]);				\
+	savesegment(gs, (pr_reg)[26]);				\
 } while (0);
 
 /* I'm not sure if we can use '-' here */
Signature · 1 line
2.53.0
#1 lore raw

PATCH 2/4 x86/process/64: Use savesegment() in __show_regs() instead of inline asm

Replace direct 'movl' instructions for DS, ES, FS, and GS read in __show_regs() with the savesegment() helper. This improves readability, consistency, and ensures proper handling of segment registers on x86_64.

No functional change intended.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/kernel/process_64.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/process_64.c +4 -4
index 08e72f429870..b85e715ebb30 100644
@@ -104,10 +104,10 @@ void __show_regs(struct pt_regs *regs, enum show_regs_mode mode,
 		return;
 	}
 
-	asm("movl %%ds,%0" : "=r" (ds));
-	asm("movl %%es,%0" : "=r" (es));
-	asm("movl %%fs,%0" : "=r" (fsindex));
-	asm("movl %%gs,%0" : "=r" (gsindex));
+	savesegment(ds, ds);
+	savesegment(es, es);
+	savesegment(fs, fsindex);
+	savesegment(gs, gsindex);
 
 	rdmsrq(MSR_FS_BASE, fs);
 	rdmsrq(MSR_GS_BASE, gs);
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/4 x86/process/32: Use correct type for 'gs' variable in __show_regs() to avoid zero-extension

In reply to Uros Bizjak

Change the type of 'gs' variable in __show_regs() from 'unsigned short' to 'unsigned int'. This prevents unwanted zero-extension when storing the 16-bit segment register into a wider general purpose register.

The code improves from:

50: 8c ee mov %gs,%esi
52: 0f b7 f6 movzwl %si,%esi
...
be: 89 74 24 14 mov %esi,0x14(%esp)

to:

50: 8c ef mov %gs,%edi
...
bb: 89 7c 24 14 mov %edi,0x14(%esp)

No functional change intended.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/kernel/process_32.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/process_32.c +1 -1
index 3ef15c2f152f..168dabf9853f 100644
@@ -61,7 +61,7 @@ void __show_regs(struct pt_regs *regs, enum show_regs_mode mode,
 {
 	unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
 	unsigned long d0, d1, d2, d3, d6, d7;
-	unsigned short gs;
+	unsigned int gs;
 
 	savesegment(gs, gs);
 
Signature · 1 line
2.53.0
#5 lore raw

PATCH 4/4 x86/tls: Clean up 'sel' variable usage in do_set_thread_area

In reply to Uros Bizjak

The top-level 'sel' variable in do_set_thread_area() was previously marked __maybe_unused, but it is now only needed locally when updating the current task.

Remove the unused top-level declaration and introduce a local 'sel' variable where it is actually used

No functional change intended.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/kernel/tls.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/tls.c +3 -1
index 3ffbab0081f4..86b4186a0d4f 100644
@@ -117,7 +117,7 @@ int do_set_thread_area(struct task_struct *p, int idx,
 		       int can_allocate)
 {
 	struct user_desc info;
-	unsigned short __maybe_unused sel, modified_sel;
+	unsigned short modified_sel;
 
 	if (copy_from_user(&info, u_info, sizeof(info)))
 		return -EFAULT;
@@ -153,6 +153,8 @@ int do_set_thread_area(struct task_struct *p, int idx,
 	modified_sel = (idx << 3) | 3;
 
 	if (p == current) {
+		unsigned short sel;
+
 #ifdef CONFIG_X86_64
 		savesegment(ds, sel);
 		if (sel == modified_sel)
Signature · 1 line
2.53.0
merge bot Applied
#6 lore raw

[tip: x86/asm] x86/tls: Clean up 'sel' variable usage in do_set_thread_area()

The following commit has been merged into the x86/asm branch of tip:

The following commit has been merged into the x86/asm branch of tip:

Commit-ID:     3b19e22cffe61bcdf10ee5e7584cfa3c1c54dc92
Gitweb:        https://git.kernel.org/tip/3b19e22cffe61bcdf10ee5e7584cfa3c1c54dc92
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Mon, 30 Mar 2026 10:59:23 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 31 Mar 2026 09:50:11 +02:00

x86/tls: Clean up 'sel' variable usage in do_set_thread_area()

The top-level 'sel' variable in do_set_thread_area() was previously marked __maybe_unused, but it is now only needed locally when updating the current task.

Remove the unused top-level declaration and introduce a local 'sel' variable where it is actually used

No functional change intended.

---
 arch/x86/kernel/tls.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kernel/tls.c +3 -1
index 3ffbab0..86b4186 100644
@@ -117,7 +117,7 @@ int do_set_thread_area(struct task_struct *p, int idx,
 		       int can_allocate)
 {
 	struct user_desc info;
-	unsigned short __maybe_unused sel, modified_sel;
+	unsigned short modified_sel;
 
 	if (copy_from_user(&info, u_info, sizeof(info)))
 		return -EFAULT;
@@ -153,6 +153,8 @@ int do_set_thread_area(struct task_struct *p, int idx,
 	modified_sel = (idx << 3) | 3;
 
 	if (p == current) {
+		unsigned short sel;
+
 #ifdef CONFIG_X86_64
 		savesegment(ds, sel);
 		if (sel == modified_sel)
merge bot Applied
#4 lore raw

[tip: x86/asm] x86/process/32: Use correct type for 'gs' variable in __show_regs() to avoid zero-extension

In reply to Uros Bizjak
The following commit has been merged into the x86/asm branch of tip:

The following commit has been merged into the x86/asm branch of tip:

Commit-ID:     6829f19810e96903d7b532145c3d1701b4d9a0f6
Gitweb:        https://git.kernel.org/tip/6829f19810e96903d7b532145c3d1701b4d9a0f6
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Mon, 30 Mar 2026 10:59:22 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 31 Mar 2026 09:50:10 +02:00

x86/process/32: Use correct type for 'gs' variable in __show_regs() to avoid zero-extension

Change the type of 'gs' variable in __show_regs() from 'unsigned short' to 'unsigned int'. This prevents unwanted zero-extension when storing the 16-bit segment register into a wider general purpose register.

The code improves from:

50: 8c ee mov %gs,%esi
52: 0f b7 f6 movzwl %si,%esi
...
be: 89 74 24 14 mov %esi,0x14(%esp)

to:

50: 8c ef mov %gs,%edi
...
bb: 89 7c 24 14 mov %edi,0x14(%esp)

No functional change intended.

---
 arch/x86/kernel/process_32.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/process_32.c +1 -1
index 3ef15c2..168dabf 100644
@@ -61,7 +61,7 @@ void __show_regs(struct pt_regs *regs, enum show_regs_mode mode,
 {
 	unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
 	unsigned long d0, d1, d2, d3, d6, d7;
-	unsigned short gs;
+	unsigned int gs;
 
 	savesegment(gs, gs);
 
merge bot Applied
#2 lore raw

[tip: x86/asm] x86/process/64: Use savesegment() in __show_regs() instead of inline asm

In reply to Uros Bizjak
The following commit has been merged into the x86/asm branch of tip:

The following commit has been merged into the x86/asm branch of tip:

Commit-ID:     81310ce4287a23b8ff6e8684de73cfaae88782ca
Gitweb:        https://git.kernel.org/tip/81310ce4287a23b8ff6e8684de73cfaae88782ca
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Mon, 30 Mar 2026 10:59:21 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 31 Mar 2026 09:50:10 +02:00

x86/process/64: Use savesegment() in __show_regs() instead of inline asm

Replace direct 'movl' instructions for DS, ES, FS, and GS read in __show_regs() with the savesegment() helper. This improves readability, consistency, and ensures proper handling of segment registers on x86_64.

No functional change intended.

---
 arch/x86/kernel/process_64.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/process_64.c +4 -4
index 08e72f4..b85e715 100644
@@ -104,10 +104,10 @@ void __show_regs(struct pt_regs *regs, enum show_regs_mode mode,
 		return;
 	}
 
-	asm("movl %%ds,%0" : "=r" (ds));
-	asm("movl %%es,%0" : "=r" (es));
-	asm("movl %%fs,%0" : "=r" (fsindex));
-	asm("movl %%gs,%0" : "=r" (gsindex));
+	savesegment(ds, ds);
+	savesegment(es, es);
+	savesegment(fs, fsindex);
+	savesegment(gs, gsindex);
 
 	rdmsrq(MSR_FS_BASE, fs);
 	rdmsrq(MSR_GS_BASE, gs);
merge bot Applied
#7 lore raw
In reply to Uros Bizjak
The following commit has been merged into the x86/asm branch of tip:

The following commit has been merged into the x86/asm branch of tip:

Commit-ID:     47d2f007615ace34c5ec9026cd5f286833c62c1b
Gitweb:        https://git.kernel.org/tip/47d2f007615ace34c5ec9026cd5f286833c62c1b
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Mon, 30 Mar 2026 10:59:20 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Tue, 31 Mar 2026 09:50:10 +02:00

x86/elf: Use savesegment() for segment register reads in ELF core dump

ELF_CORE_COPY_REGS() currently reads %ds, %es, %fs, and %gs using inline assembly and manual zero-extension. This results in redundant instructions like mov %eax,%eax.

Replace the inline assembly with the savesegment() helper, which automatically zero-extends the value to the full register width, eliminating unnecessary instructions.

For example, the %ds load sequence changes from:

  d03: 8c d8                 mov    %ds,%eax
  d05: 89 c0                 mov    %eax,%eax
  d07: 48 89 84 24 38 01 00  mov    %rax,0x138(%rsp)

d0e: 00

to:

  ce8: 8c d8                 mov    %ds,%eax
  cea: 48 89 84 24 38 01 00  mov    %rax,0x138(%rsp)

cf1: 00

thus eliminating the unnecessary zero-extending mov %eax,%eax.

No functional change intended.

---
 arch/x86/include/asm/elf.h |  9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
arch/x86/include/asm/elf.h +4 -5
index 2ba5f16..c7f9897 100644
@@ -187,7 +187,6 @@ void set_personality_ia32(bool);
 
 #define ELF_CORE_COPY_REGS(pr_reg, regs)			\
 do {								\
-	unsigned v;						\
 	(pr_reg)[0] = (regs)->r15;				\
 	(pr_reg)[1] = (regs)->r14;				\
 	(pr_reg)[2] = (regs)->r13;				\
@@ -211,10 +210,10 @@ do {								\
 	(pr_reg)[20] = (regs)->ss;				\
 	(pr_reg)[21] = x86_fsbase_read_cpu();			\
 	(pr_reg)[22] = x86_gsbase_read_cpu_inactive();		\
-	asm("movl %%ds,%0" : "=r" (v)); (pr_reg)[23] = v;	\
-	asm("movl %%es,%0" : "=r" (v)); (pr_reg)[24] = v;	\
-	asm("movl %%fs,%0" : "=r" (v)); (pr_reg)[25] = v;	\
-	asm("movl %%gs,%0" : "=r" (v)); (pr_reg)[26] = v;	\
+	savesegment(ds, (pr_reg)[23]);				\
+	savesegment(es, (pr_reg)[24]);				\
+	savesegment(fs, (pr_reg)[25]);				\
+	savesegment(gs, (pr_reg)[26]);				\
 } while (0);
 
 /* I'm not sure if we can use '-' here */