* [PATCH] x86/ptrace: Use savesegment() in get_segment_reg() instead of inline asm
@ 2026-04-02 6:22 Uros Bizjak
2026-04-02 12:48 ` Oleg Nesterov
0 siblings, 1 reply; 5+ messages in thread
From: Uros Bizjak @ 2026-04-02 6:22 UTC (permalink / raw)
To: x86, linux-kernel
Cc: Uros Bizjak, Oleg Nesterov, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, H. Peter Anvin
Replace direct 'movl' instructions for DS, ES, FS, and GS read in
get_segment_reg() with the savesegment() helper. This improves
readability, consistency, and ensures proper handling of
segment registers on x86_64.
No functional change intended.
Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
---
arch/x86/kernel/ptrace.c | 19 +++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index 3dcadc13f09a..4cb00aa0645f 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -251,32 +251,31 @@ static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
/*
* Returning the value truncates it to 16 bits.
*/
- unsigned int seg;
+ unsigned int retval;
switch (offset) {
case offsetof(struct user_regs_struct, fs):
if (task == current) {
- /* Older gas can't assemble movq %?s,%r?? */
- asm("movl %%fs,%0" : "=r" (seg));
- return seg;
+ savesegment(fs, retval);
+ return retval;
}
return task->thread.fsindex;
case offsetof(struct user_regs_struct, gs):
if (task == current) {
- asm("movl %%gs,%0" : "=r" (seg));
- return seg;
+ savesegment(gs, retval);
+ return retval;
}
return task->thread.gsindex;
case offsetof(struct user_regs_struct, ds):
if (task == current) {
- asm("movl %%ds,%0" : "=r" (seg));
- return seg;
+ savesegment(ds, retval);
+ return retval;
}
return task->thread.ds;
case offsetof(struct user_regs_struct, es):
if (task == current) {
- asm("movl %%es,%0" : "=r" (seg));
- return seg;
+ savesegment(es, retval);
+ return retval;
}
return task->thread.es;
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] x86/ptrace: Use savesegment() in get_segment_reg() instead of inline asm
2026-04-02 6:22 [PATCH] x86/ptrace: Use savesegment() in get_segment_reg() instead of inline asm Uros Bizjak
@ 2026-04-02 12:48 ` Oleg Nesterov
2026-04-02 13:09 ` Uros Bizjak
0 siblings, 1 reply; 5+ messages in thread
From: Oleg Nesterov @ 2026-04-02 12:48 UTC (permalink / raw)
To: Uros Bizjak
Cc: x86, linux-kernel, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin
On 04/02, Uros Bizjak wrote:
>
> @@ -251,32 +251,31 @@ static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
> /*
> * Returning the value truncates it to 16 bits.
> */
> - unsigned int seg;
> + unsigned int retval;
LGTM, but perhaps it would be better to use "u16 retval" ? and remove the
comment.
Oleg.
>
> switch (offset) {
> case offsetof(struct user_regs_struct, fs):
> if (task == current) {
> - /* Older gas can't assemble movq %?s,%r?? */
> - asm("movl %%fs,%0" : "=r" (seg));
> - return seg;
> + savesegment(fs, retval);
> + return retval;
> }
> return task->thread.fsindex;
> case offsetof(struct user_regs_struct, gs):
> if (task == current) {
> - asm("movl %%gs,%0" : "=r" (seg));
> - return seg;
> + savesegment(gs, retval);
> + return retval;
> }
> return task->thread.gsindex;
> case offsetof(struct user_regs_struct, ds):
> if (task == current) {
> - asm("movl %%ds,%0" : "=r" (seg));
> - return seg;
> + savesegment(ds, retval);
> + return retval;
> }
> return task->thread.ds;
> case offsetof(struct user_regs_struct, es):
> if (task == current) {
> - asm("movl %%es,%0" : "=r" (seg));
> - return seg;
> + savesegment(es, retval);
> + return retval;
> }
> return task->thread.es;
>
> --
> 2.53.0
>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] x86/ptrace: Use savesegment() in get_segment_reg() instead of inline asm
2026-04-02 12:48 ` Oleg Nesterov
@ 2026-04-02 13:09 ` Uros Bizjak
2026-04-02 13:40 ` Uros Bizjak
0 siblings, 1 reply; 5+ messages in thread
From: Uros Bizjak @ 2026-04-02 13:09 UTC (permalink / raw)
To: Oleg Nesterov
Cc: x86, linux-kernel, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin
On Thu, Apr 2, 2026 at 2:48 PM Oleg Nesterov <oleg@redhat.com> wrote:
>
> On 04/02, Uros Bizjak wrote:
> >
> > @@ -251,32 +251,31 @@ static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
> > /*
> > * Returning the value truncates it to 16 bits.
> > */
> > - unsigned int seg;
> > + unsigned int retval;
>
> LGTM, but perhaps it would be better to use "u16 retval" ? and remove the
> comment.
With the new definition of savesegment(), this is actually NOP from
the compiler PoV.
There is a corresponding x86_32 get_segment_reg() function that has
the same definition of retval, I can prepare a follow-up patch that
changes both.
Thanks,
Uros.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] x86/ptrace: Use savesegment() in get_segment_reg() instead of inline asm
2026-04-02 13:09 ` Uros Bizjak
@ 2026-04-02 13:40 ` Uros Bizjak
2026-04-02 14:03 ` Oleg Nesterov
0 siblings, 1 reply; 5+ messages in thread
From: Uros Bizjak @ 2026-04-02 13:40 UTC (permalink / raw)
To: Oleg Nesterov
Cc: x86, linux-kernel, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin
[-- Attachment #1: Type: text/plain, Size: 908 bytes --]
On Thu, Apr 2, 2026 at 3:09 PM Uros Bizjak <ubizjak@gmail.com> wrote:
>
> On Thu, Apr 2, 2026 at 2:48 PM Oleg Nesterov <oleg@redhat.com> wrote:
> >
> > On 04/02, Uros Bizjak wrote:
> > >
> > > @@ -251,32 +251,31 @@ static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
> > > /*
> > > * Returning the value truncates it to 16 bits.
> > > */
> > > - unsigned int seg;
> > > + unsigned int retval;
> >
> > LGTM, but perhaps it would be better to use "u16 retval" ? and remove the
> > comment.
>
> With the new definition of savesegment(), this is actually NOP from
> the compiler PoV.
>
> There is a corresponding x86_32 get_segment_reg() function that has
> the same definition of retval, I can prepare a follow-up patch that
> changes both.
Something like the attached patch that also slightly unifies x86_32 with x86_64.
Uros.
[-- Attachment #2: ptrace.diff.txt --]
[-- Type: text/plain, Size: 1302 bytes --]
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index 4cb00aa0645f..5fda7619fca6 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):
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] x86/ptrace: Use savesegment() in get_segment_reg() instead of inline asm
2026-04-02 13:40 ` Uros Bizjak
@ 2026-04-02 14:03 ` Oleg Nesterov
0 siblings, 0 replies; 5+ messages in thread
From: Oleg Nesterov @ 2026-04-02 14:03 UTC (permalink / raw)
To: Uros Bizjak
Cc: x86, linux-kernel, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, H. Peter Anvin
On 04/02, Uros Bizjak wrote:
>
> On Thu, Apr 2, 2026 at 3:09 PM Uros Bizjak <ubizjak@gmail.com> wrote:
> >
> > There is a corresponding x86_32 get_segment_reg() function that has
> > the same definition of retval, I can prepare a follow-up patch that
> > changes both.
>
> Something like the attached patch that also slightly unifies x86_32 with x86_64.
OK, agreed. And this cleanup looks good to me as well.
Oleg.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-04-02 14:04 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-02 6:22 [PATCH] x86/ptrace: Use savesegment() in get_segment_reg() instead of inline asm Uros Bizjak
2026-04-02 12:48 ` Oleg Nesterov
2026-04-02 13:09 ` Uros Bizjak
2026-04-02 13:40 ` Uros Bizjak
2026-04-02 14:03 ` Oleg Nesterov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox