[PATCH -tip v2 2/2] x86/ptrace: Use unsigned short for segment values in get_segment_reg()
From: Uros Bizjak
Date: Wed Apr 08 2026 - 04:32:23 EST
Change the temporary variable in get_segment_reg() from unsigned int
to unsigned short to match the function’s return type, making the
16-bit semantics explicit instead of relying on implicit truncation.
Also restructure the GS handling for x86_32 to make the special case
explicit and easier to follow.
This clarifies the control flow and makes the 16-bit semantics
explicit without changing behavior.
No functional change intended.
Signed-off-by: Uros Bizjak <ubizjak@xxxxxxxxx>
Acked-by: Oleg Nesterov <oleg@xxxxxxxxxx>
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>
---
v2: New patch.
---
arch/x86/kernel/ptrace.c | 24 +++++++++---------------
1 file changed, 9 insertions(+), 15 deletions(-)
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index 4cb00aa0645f..58e09a764bcf 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -182,19 +182,16 @@ static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
{
- /*
- * Returning the value truncates it to 16 bits.
- */
- unsigned int retval;
- if (offset != offsetof(struct user_regs_struct, gs))
- retval = *pt_regs_access(task_pt_regs(task), offset);
- else {
- if (task == current)
+ unsigned short retval;
+
+ if (offset == offsetof(struct user_regs_struct, gs)) {
+ if (task == current) {
savesegment(gs, retval);
- else
- retval = task->thread.gs;
+ return retval;
+ }
+ return task->thread.gs;
}
- return retval;
+ return *pt_regs_access(task_pt_regs(task), offset);
}
static int set_segment_reg(struct task_struct *task,
@@ -248,10 +245,7 @@ static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
{
- /*
- * Returning the value truncates it to 16 bits.
- */
- unsigned int retval;
+ unsigned short retval;
switch (offset) {
case offsetof(struct user_regs_struct, fs):
--
2.53.0