2023-02-09 15:37:44

by Masami Hiramatsu

[permalink] [raw]
Subject: [PATCH -tip] kprobes/x86: Remove unneeded casting from immediate value

From: Masami Hiramatsu (Google) <[email protected]>

Remove unneeded casting from immediate value assignments for relative
jump offset. Since the immediate values in the 'insn' data structure are
assigned from immediate bytes as a signed value to insn.immediate.value
by insn_field_set(). Thus, if we need to access that value as a signed
value (in this kprobe's case), we don't need to cast it.
This is a kind of clean up (should not change behavior) follows Nadav's
bugfix.

Link: https://lore.kernel.org/all/[email protected]/

Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
---
arch/x86/kernel/kprobes/core.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 695873c0f50b..2440e736d0e4 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -607,19 +607,19 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
case 0xe8: /* near call relative */
p->ainsn.emulate_op = kprobe_emulate_call;
if (insn->immediate.nbytes == 2)
- p->ainsn.rel32 = *(s16 *)&insn->immediate.value;
+ p->ainsn.rel32 = insn->immediate.value;
else
- p->ainsn.rel32 = *(s32 *)&insn->immediate.value;
+ p->ainsn.rel32 = insn->immediate.value;
break;
case 0xeb: /* short jump relative */
case 0xe9: /* near jump relative */
p->ainsn.emulate_op = kprobe_emulate_jmp;
if (insn->immediate.nbytes == 1)
- p->ainsn.rel32 = *(s8 *)&insn->immediate.value;
+ p->ainsn.rel32 = insn->immediate.value;
else if (insn->immediate.nbytes == 2)
- p->ainsn.rel32 = *(s16 *)&insn->immediate.value;
+ p->ainsn.rel32 = insn->immediate.value;
else
- p->ainsn.rel32 = *(s32 *)&insn->immediate.value;
+ p->ainsn.rel32 = insn->immediate.value;
break;
case 0x70 ... 0x7f:
/* 1 byte conditional jump */
@@ -634,9 +634,9 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
p->ainsn.emulate_op = kprobe_emulate_jcc;
p->ainsn.jcc.type = opcode & 0xf;
if (insn->immediate.nbytes == 2)
- p->ainsn.rel32 = *(s16 *)&insn->immediate.value;
+ p->ainsn.rel32 = insn->immediate.value;
else
- p->ainsn.rel32 = *(s32 *)&insn->immediate.value;
+ p->ainsn.rel32 = insn->immediate.value;
} else if (opcode == 0x01 &&
X86_MODRM_REG(insn->modrm.bytes[0]) == 0 &&
X86_MODRM_MOD(insn->modrm.bytes[0]) == 3) {
@@ -651,7 +651,7 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
p->ainsn.emulate_op = kprobe_emulate_loop;
p->ainsn.loop.type = opcode & 0x3;
p->ainsn.loop.asize = insn->addr_bytes * 8;
- p->ainsn.rel32 = *(s8 *)&insn->immediate.value;
+ p->ainsn.rel32 = insn->immediate.value;
break;
case 0xff:
/*



2023-02-09 15:41:21

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH -tip] kprobes/x86: Remove unneeded casting from immediate value

On Fri, Feb 10, 2023 at 12:36:36AM +0900, Masami Hiramatsu (Google) wrote:
> From: Masami Hiramatsu (Google) <[email protected]>
>
> Remove unneeded casting from immediate value assignments for relative
> jump offset. Since the immediate values in the 'insn' data structure are
> assigned from immediate bytes as a signed value to insn.immediate.value
> by insn_field_set(). Thus, if we need to access that value as a signed
> value (in this kprobe's case), we don't need to cast it.
> This is a kind of clean up (should not change behavior) follows Nadav's
> bugfix.
>
> Link: https://lore.kernel.org/all/[email protected]/
> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>

Acked-by: Peter Zijlstra (Intel) <[email protected]>

2023-02-10 00:02:09

by Nadav Amit

[permalink] [raw]
Subject: Re: [PATCH -tip] kprobes/x86: Remove unneeded casting from immediate value


On 2/9/23 5:36 PM, Masami Hiramatsu (Google) wrote:
> From: Masami Hiramatsu (Google) <[email protected]>
>
> Remove unneeded casting from immediate value assignments for relative
> jump offset. Since the immediate values in the 'insn' data structure are
> assigned from immediate bytes as a signed value to insn.immediate.value
> by insn_field_set(). Thus, if we need to access that value as a signed
> value (in this kprobe's case), we don't need to cast it.
> This is a kind of clean up (should not change behavior) follows Nadav's
> bugfix.
>
> Link: https://lore.kernel.org/all/[email protected]/
>
> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
> ---
> arch/x86/kernel/kprobes/core.c | 16 ++++++++--------
> 1 file changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
> index 695873c0f50b..2440e736d0e4 100644
> --- a/arch/x86/kernel/kprobes/core.c
> +++ b/arch/x86/kernel/kprobes/core.c
> @@ -607,19 +607,19 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
> case 0xe8: /* near call relative */
> p->ainsn.emulate_op = kprobe_emulate_call;
> if (insn->immediate.nbytes == 2)
> - p->ainsn.rel32 = *(s16 *)&insn->immediate.value;
> + p->ainsn.rel32 = insn->immediate.value;
> else
> - p->ainsn.rel32 = *(s32 *)&insn->immediate.value;
> + p->ainsn.rel32 = insn->immediate.value;

Hmm.. I don't get it. What the purpose of keeping the duplicated code
(after your change)?


2023-02-14 23:58:01

by Masami Hiramatsu

[permalink] [raw]
Subject: Re: [PATCH -tip] kprobes/x86: Remove unneeded casting from immediate value

On Fri, 10 Feb 2023 02:01:54 +0200
Nadav Amit <[email protected]> wrote:

>
> On 2/9/23 5:36 PM, Masami Hiramatsu (Google) wrote:
> > From: Masami Hiramatsu (Google) <[email protected]>
> >
> > Remove unneeded casting from immediate value assignments for relative
> > jump offset. Since the immediate values in the 'insn' data structure are
> > assigned from immediate bytes as a signed value to insn.immediate.value
> > by insn_field_set(). Thus, if we need to access that value as a signed
> > value (in this kprobe's case), we don't need to cast it.
> > This is a kind of clean up (should not change behavior) follows Nadav's
> > bugfix.
> >
> > Link: https://lore.kernel.org/all/[email protected]/
> >
> > Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
> > ---
> > arch/x86/kernel/kprobes/core.c | 16 ++++++++--------
> > 1 file changed, 8 insertions(+), 8 deletions(-)
> >
> > diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
> > index 695873c0f50b..2440e736d0e4 100644
> > --- a/arch/x86/kernel/kprobes/core.c
> > +++ b/arch/x86/kernel/kprobes/core.c
> > @@ -607,19 +607,19 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
> > case 0xe8: /* near call relative */
> > p->ainsn.emulate_op = kprobe_emulate_call;
> > if (insn->immediate.nbytes == 2)
> > - p->ainsn.rel32 = *(s16 *)&insn->immediate.value;
> > + p->ainsn.rel32 = insn->immediate.value;
> > else
> > - p->ainsn.rel32 = *(s32 *)&insn->immediate.value;
> > + p->ainsn.rel32 = insn->immediate.value;
>
> Hmm.. I don't get it. What the purpose of keeping the duplicated code
> (after your change)?
>

Oops, good catch! I think p->ainsn.rel32 can be set without any check in general.
Let me update it.

Thank you!


--
Masami Hiramatsu (Google) <[email protected]>