[PATCH 2/2] x86/process: Use storesegment() when saving segment selectors

From: Uros Bizjak

Date: Tue Mar 31 2026 - 02:39:55 EST


Switch segment save sites in copy_thread() and save_fsgs() from
savesegment() to the newly introduced storesegment() helper.

These call sites only store the segment selector into a u16 field
and do not require the value in a general-purpose register. Using
storesegment() avoids the unnecessary register intermediate and
better matches the intended use.

The generated code is improved from, e.g.:

34f: 8c c0 mov %es,%eax
351: 66 89 83 08 0c 00 00 mov %ax,0xc08(%rbx)

to a single direct store:

34f: 8c 83 08 0c 00 00 mov %es,0xc08(%rbx)

This is a mechanical follow-up to the introduction of
storesegment(), with no functional change.

Signed-off-by: Uros Bizjak <ubizjak@xxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Borislav Petkov <bp@xxxxxxxxx>
Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
---
arch/x86/kernel/process.c | 4 ++--
arch/x86/kernel/process_64.c | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index 4c718f8adc59..10e2ba668a14 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -197,8 +197,8 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
p->thread.gsindex = current->thread.gsindex;
p->thread.gsbase = current->thread.gsbase;

- savesegment(es, p->thread.es);
- savesegment(ds, p->thread.ds);
+ storesegment(es, p->thread.es);
+ storesegment(ds, p->thread.ds);

if (p->mm && (clone_flags & (CLONE_VM | CLONE_VFORK)) == CLONE_VM)
set_bit(MM_CONTEXT_LOCK_LAM, &p->mm->context.flags);
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index b85e715ebb30..fe391961ed86 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -275,8 +275,8 @@ static __always_inline void save_base_legacy(struct task_struct *prev_p,

static __always_inline void save_fsgs(struct task_struct *task)
{
- savesegment(fs, task->thread.fsindex);
- savesegment(gs, task->thread.gsindex);
+ storesegment(fs, task->thread.fsindex);
+ storesegment(gs, task->thread.gsindex);
if (static_cpu_has(X86_FEATURE_FSGSBASE)) {
/*
* If FSGSBASE is enabled, we can't make any useful guesses
--
2.53.0