* [PATCH] riscv: kprobe: Fixup misaligned load text
@ 2023-02-01 6:46 guoren
2023-02-01 9:40 ` Björn Töpel
0 siblings, 1 reply; 7+ messages in thread
From: guoren @ 2023-02-01 6:46 UTC (permalink / raw)
To: guoren, palmer, conor.dooley, liaochang1, bjorn
Cc: linux-arch, linux-kernel, linux-riscv, Guo Ren, Bjorn Topel
From: Guo Ren <guoren@linux.alibaba.com>
The current kprobe would cause a misaligned load for the probe point.
This patch fixup it with two half-word loads instead.
Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported")
Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
Signed-off-by: Guo Ren <guoren@kernel.org>
Link: https://lore.kernel.org/linux-riscv/878rhig9zj.fsf@all.your.base.are.belong.to.us/
Reported-by: Bjorn Topel <bjorn.topel@gmail.com>
---
arch/riscv/kernel/probes/kprobes.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
index 41c7481afde3..c1160629cef4 100644
--- a/arch/riscv/kernel/probes/kprobes.c
+++ b/arch/riscv/kernel/probes/kprobes.c
@@ -74,7 +74,9 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
return -EILSEQ;
/* copy instruction */
- p->opcode = *p->addr;
+ p->opcode = (kprobe_opcode_t)(*(u16 *)probe_addr);
+ if (GET_INSN_LENGTH(p->opcode) == 4)
+ p->opcode |= (kprobe_opcode_t)(*(u16 *)(probe_addr + 2)) << 16;
/* decode instruction */
switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) {
--
2.36.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] riscv: kprobe: Fixup misaligned load text
2023-02-01 6:46 [PATCH] riscv: kprobe: Fixup misaligned load text guoren
@ 2023-02-01 9:40 ` Björn Töpel
2023-02-02 6:17 ` Guo Ren
0 siblings, 1 reply; 7+ messages in thread
From: Björn Töpel @ 2023-02-01 9:40 UTC (permalink / raw)
To: guoren, guoren, palmer, conor.dooley, liaochang1
Cc: linux-arch, linux-kernel, linux-riscv, Guo Ren, Bjorn Topel
guoren@kernel.org writes:
> From: Guo Ren <guoren@linux.alibaba.com>
>
> The current kprobe would cause a misaligned load for the probe point.
> This patch fixup it with two half-word loads instead.
>
> Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported")
> Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
> Signed-off-by: Guo Ren <guoren@kernel.org>
> Link: https://lore.kernel.org/linux-riscv/878rhig9zj.fsf@all.your.base.are.belong.to.us/
> Reported-by: Bjorn Topel <bjorn.topel@gmail.com>
> ---
> arch/riscv/kernel/probes/kprobes.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
> index 41c7481afde3..c1160629cef4 100644
> --- a/arch/riscv/kernel/probes/kprobes.c
> +++ b/arch/riscv/kernel/probes/kprobes.c
> @@ -74,7 +74,9 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
> return -EILSEQ;
>
> /* copy instruction */
> - p->opcode = *p->addr;
> + p->opcode = (kprobe_opcode_t)(*(u16 *)probe_addr);
> + if (GET_INSN_LENGTH(p->opcode) == 4)
> + p->opcode |= (kprobe_opcode_t)(*(u16 *)(probe_addr + 2))
> << 16;
Ugh, those casts. :-( What about the memcpy variant you had in the other
thread?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] riscv: kprobe: Fixup misaligned load text
2023-02-01 9:40 ` Björn Töpel
@ 2023-02-02 6:17 ` Guo Ren
2023-02-02 9:48 ` Björn Töpel
0 siblings, 1 reply; 7+ messages in thread
From: Guo Ren @ 2023-02-02 6:17 UTC (permalink / raw)
To: Björn Töpel
Cc: palmer, conor.dooley, liaochang1, linux-arch, linux-kernel,
linux-riscv, Guo Ren, Bjorn Topel
On Wed, Feb 1, 2023 at 5:40 PM Björn Töpel <bjorn@kernel.org> wrote:
>
> guoren@kernel.org writes:
>
> > From: Guo Ren <guoren@linux.alibaba.com>
> >
> > The current kprobe would cause a misaligned load for the probe point.
> > This patch fixup it with two half-word loads instead.
> >
> > Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported")
> > Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
> > Signed-off-by: Guo Ren <guoren@kernel.org>
> > Link: https://lore.kernel.org/linux-riscv/878rhig9zj.fsf@all.your.base.are.belong.to.us/
> > Reported-by: Bjorn Topel <bjorn.topel@gmail.com>
> > ---
> > arch/riscv/kernel/probes/kprobes.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
> > index 41c7481afde3..c1160629cef4 100644
> > --- a/arch/riscv/kernel/probes/kprobes.c
> > +++ b/arch/riscv/kernel/probes/kprobes.c
> > @@ -74,7 +74,9 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
> > return -EILSEQ;
> >
> > /* copy instruction */
> > - p->opcode = *p->addr;
> > + p->opcode = (kprobe_opcode_t)(*(u16 *)probe_addr);
> > + if (GET_INSN_LENGTH(p->opcode) == 4)
> > + p->opcode |= (kprobe_opcode_t)(*(u16 *)(probe_addr + 2))
> > << 16;
>
> Ugh, those casts. :-( What about the memcpy variant you had in the other
> thread?
The memcpy version would force load probe_addr + 2. This one would
save an lh operation. The code text guarantees half-word alignment. No
misaligned load happened. Second, kprobe wouldn't write the last half
of 32b instruction.
--
Best Regards
Guo Ren
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] riscv: kprobe: Fixup misaligned load text
2023-02-02 6:17 ` Guo Ren
@ 2023-02-02 9:48 ` Björn Töpel
2023-02-02 10:59 ` Jessica Clarke
0 siblings, 1 reply; 7+ messages in thread
From: Björn Töpel @ 2023-02-02 9:48 UTC (permalink / raw)
To: Guo Ren
Cc: palmer, conor.dooley, liaochang1, linux-arch, linux-kernel,
linux-riscv, Guo Ren, Bjorn Topel
Guo Ren <guoren@kernel.org> writes:
> On Wed, Feb 1, 2023 at 5:40 PM Björn Töpel <bjorn@kernel.org> wrote:
>>
>> guoren@kernel.org writes:
>>
>> > From: Guo Ren <guoren@linux.alibaba.com>
>> >
>> > The current kprobe would cause a misaligned load for the probe point.
>> > This patch fixup it with two half-word loads instead.
>> >
>> > Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported")
>> > Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
>> > Signed-off-by: Guo Ren <guoren@kernel.org>
>> > Link: https://lore.kernel.org/linux-riscv/878rhig9zj.fsf@all.your.base.are.belong.to.us/
>> > Reported-by: Bjorn Topel <bjorn.topel@gmail.com>
>> > ---
>> > arch/riscv/kernel/probes/kprobes.c | 4 +++-
>> > 1 file changed, 3 insertions(+), 1 deletion(-)
>> >
>> > diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
>> > index 41c7481afde3..c1160629cef4 100644
>> > --- a/arch/riscv/kernel/probes/kprobes.c
>> > +++ b/arch/riscv/kernel/probes/kprobes.c
>> > @@ -74,7 +74,9 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
>> > return -EILSEQ;
>> >
>> > /* copy instruction */
>> > - p->opcode = *p->addr;
>> > + p->opcode = (kprobe_opcode_t)(*(u16 *)probe_addr);
>> > + if (GET_INSN_LENGTH(p->opcode) == 4)
>> > + p->opcode |= (kprobe_opcode_t)(*(u16 *)(probe_addr + 2))
>> > << 16;
>>
>> Ugh, those casts. :-( What about the memcpy variant you had in the other
>> thread?
> The memcpy version would force load probe_addr + 2. This one would
> save an lh operation. The code text guarantees half-word alignment. No
> misaligned load happened. Second, kprobe wouldn't write the last half
> of 32b instruction.
Ok, something more readable, like:
diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
index f21592d20306..3602352ba175 100644
--- a/arch/riscv/kernel/probes/kprobes.c
+++ b/arch/riscv/kernel/probes/kprobes.c
@@ -50,14 +50,16 @@ static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
int __kprobes arch_prepare_kprobe(struct kprobe *p)
{
- unsigned long probe_addr = (unsigned long)p->addr;
+ u16 *insn = (u16 *)p->addr;
- if (probe_addr & 0x1)
+ if ((uintptr_t)insn & 0x1)
return -EILSEQ;
/* copy instruction */
- p->opcode = *p->addr;
-
+ p->opcode = *insn++;
+ if (GET_INSN_LENGTH(p->opcode) == 4)
+ p->opcode |= *insn << 16;
+
/* decode instruction */
switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) {
case INSN_REJECTED: /* insn not supported */
Björn
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] riscv: kprobe: Fixup misaligned load text
2023-02-02 9:48 ` Björn Töpel
@ 2023-02-02 10:59 ` Jessica Clarke
2023-02-02 14:36 ` Björn Töpel
0 siblings, 1 reply; 7+ messages in thread
From: Jessica Clarke @ 2023-02-02 10:59 UTC (permalink / raw)
To: Björn Töpel
Cc: Guo Ren, Palmer Dabbelt, Conor Dooley, liaochang1, linux-arch,
Linux Kernel Mailing List, linux-riscv, Guo Ren, Bjorn Topel
On 2 Feb 2023, at 09:48, Björn Töpel <bjorn@kernel.org> wrote:
>
> Guo Ren <guoren@kernel.org> writes:
>
>> On Wed, Feb 1, 2023 at 5:40 PM Björn Töpel <bjorn@kernel.org> wrote:
>>>
>>> guoren@kernel.org writes:
>>>
>>>> From: Guo Ren <guoren@linux.alibaba.com>
>>>>
>>>> The current kprobe would cause a misaligned load for the probe point.
>>>> This patch fixup it with two half-word loads instead.
>>>>
>>>> Fixes: c22b0bcb1dd0 ("riscv: Add kprobes supported")
>>>> Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
>>>> Signed-off-by: Guo Ren <guoren@kernel.org>
>>>> Link: https://lore.kernel.org/linux-riscv/878rhig9zj.fsf@all.your.base.are.belong.to.us/
>>>> Reported-by: Bjorn Topel <bjorn.topel@gmail.com>
>>>> ---
>>>> arch/riscv/kernel/probes/kprobes.c | 4 +++-
>>>> 1 file changed, 3 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
>>>> index 41c7481afde3..c1160629cef4 100644
>>>> --- a/arch/riscv/kernel/probes/kprobes.c
>>>> +++ b/arch/riscv/kernel/probes/kprobes.c
>>>> @@ -74,7 +74,9 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
>>>> return -EILSEQ;
>>>>
>>>> /* copy instruction */
>>>> - p->opcode = *p->addr;
>>>> + p->opcode = (kprobe_opcode_t)(*(u16 *)probe_addr);
>>>> + if (GET_INSN_LENGTH(p->opcode) == 4)
>>>> + p->opcode |= (kprobe_opcode_t)(*(u16 *)(probe_addr + 2))
>>>> << 16;
>>>
>>> Ugh, those casts. :-( What about the memcpy variant you had in the other
>>> thread?
>> The memcpy version would force load probe_addr + 2. This one would
>> save an lh operation. The code text guarantees half-word alignment. No
>> misaligned load happened. Second, kprobe wouldn't write the last half
>> of 32b instruction.
>
> Ok, something more readable, like:
>
> diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
> index f21592d20306..3602352ba175 100644
> --- a/arch/riscv/kernel/probes/kprobes.c
> +++ b/arch/riscv/kernel/probes/kprobes.c
> @@ -50,14 +50,16 @@ static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
>
> int __kprobes arch_prepare_kprobe(struct kprobe *p)
> {
> - unsigned long probe_addr = (unsigned long)p->addr;
> + u16 *insn = (u16 *)p->addr;
>
> - if (probe_addr & 0x1)
> + if ((uintptr_t)insn & 0x1)
> return -EILSEQ;
>
> /* copy instruction */
> - p->opcode = *p->addr;
> -
> + p->opcode = *insn++;
> + if (GET_INSN_LENGTH(p->opcode) == 4)
> + p->opcode |= *insn << 16;
*insn gets promoted to int not unsigned so this is UB if bit 15 is set.
Jess
> +
> /* decode instruction */
> switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) {
> case INSN_REJECTED: /* insn not supported */
>
>
> Björn
>
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] riscv: kprobe: Fixup misaligned load text
2023-02-02 10:59 ` Jessica Clarke
@ 2023-02-02 14:36 ` Björn Töpel
2023-02-06 3:10 ` Guo Ren
0 siblings, 1 reply; 7+ messages in thread
From: Björn Töpel @ 2023-02-02 14:36 UTC (permalink / raw)
To: Jessica Clarke
Cc: Guo Ren, Palmer Dabbelt, Conor Dooley, liaochang1, linux-arch,
Linux Kernel Mailing List, linux-riscv, Guo Ren, Bjorn Topel
Jessica Clarke <jrtc27@jrtc27.com> writes:
>> + p->opcode = *insn++;
>> + if (GET_INSN_LENGTH(p->opcode) == 4)
>> + p->opcode |= *insn << 16;
>
> *insn gets promoted to int not unsigned so this is UB if bit 15 is set.
Ugh. Good catch! I guess we can't get rid of *that* explicit cast to
kprobe_opcode_t here...
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] riscv: kprobe: Fixup misaligned load text
2023-02-02 14:36 ` Björn Töpel
@ 2023-02-06 3:10 ` Guo Ren
0 siblings, 0 replies; 7+ messages in thread
From: Guo Ren @ 2023-02-06 3:10 UTC (permalink / raw)
To: Björn Töpel
Cc: Jessica Clarke, Palmer Dabbelt, Conor Dooley, liaochang1,
linux-arch, Linux Kernel Mailing List, linux-riscv, Guo Ren,
Bjorn Topel
On Thu, Feb 2, 2023 at 10:36 PM Björn Töpel <bjorn@kernel.org> wrote:
>
> Jessica Clarke <jrtc27@jrtc27.com> writes:
>
> >> + p->opcode = *insn++;
> >> + if (GET_INSN_LENGTH(p->opcode) == 4)
> >> + p->opcode |= *insn << 16;
> >
> > *insn gets promoted to int not unsigned so this is UB if bit 15 is set.
>
> Ugh. Good catch! I guess we can't get rid of *that* explicit cast to
> kprobe_opcode_t here...
Hi Bjorn & Jessica,
Thx for reviewing.
The new version came out:
https://lore.kernel.org/linux-riscv/20230204063531.740220-1-guoren@kernel.org/
--
Best Regards
Guo Ren
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-02-06 3:10 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-01 6:46 [PATCH] riscv: kprobe: Fixup misaligned load text guoren
2023-02-01 9:40 ` Björn Töpel
2023-02-02 6:17 ` Guo Ren
2023-02-02 9:48 ` Björn Töpel
2023-02-02 10:59 ` Jessica Clarke
2023-02-02 14:36 ` Björn Töpel
2023-02-06 3:10 ` Guo Ren
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).