From: Hengqi Chen <hengqi.chen@gmail.com>
To: Huacai Chen <chenhuacai@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>,
linux-arch <linux-arch@vger.kernel.org>,
loongarch@lists.linux.dev, kernel@xen0n.name
Subject: Re: [PATCH] LoongArch: Store syscall nr in thread_info
Date: Thu, 23 Nov 2023 13:49:36 +0800 [thread overview]
Message-ID: <CAEyhmHRFZqH=4K8WUO6tPFd0NoWMHm_iLan+4EPiqovO03w1xw@mail.gmail.com> (raw)
In-Reply-To: <CAAhV-H4Jr_a4j+Qnitd7ay9Vkcw5g0W_W3LNJwUzQ1BWXoGpoA@mail.gmail.com>
On Wed, Nov 22, 2023 at 3:58 PM Huacai Chen <chenhuacai@kernel.org> wrote:
>
> Hi, Hengqi,
>
> On Wed, Nov 22, 2023 at 3:34 PM Hengqi Chen <hengqi.chen@gmail.com> wrote:
> >
> > Hi, Huacai,
> >
> > On Wed, Nov 22, 2023 at 2:32 PM Huacai Chen <chenhuacai@kernel.org> wrote:
> > >
> > > Hi, Hengqi,
> > >
> > > On Wed, Nov 22, 2023 at 1:14 PM Hengqi Chen <hengqi.chen@gmail.com> wrote:
> > > >
> > > > Currently, we store syscall number in pt_regs::regs[11] and it may be
> > > > changed during syscall execution. Take `execve` as an example:
> > > >
> > > > sys_execve
> > > > -> do_execve
> > > > -> do_execveat_common
> > > > -> bprm_execve
> > > > -> exec_binprm
> > > > -> search_binary_handler
> > > > -> load_elf_binary
> > > > -> ELF_PLAT_INIT
> > > >
> > > > ELF_PLAT_INIT reset regs[11] to 0, later in syscall_exit_to_user_mode
> > > > we get a wrong syscall nr.
> > > >
> > > > Known affected syscalls includes execve/execveat/rt_sigreturn. Tools
> > > > like execsnoop do not work properly because the sys_exit_* tracepoints
> > > > does not trigger at all.
> > > >
> > > > Let's store syscall nr in thread_info instead.
> > > Can we just modify ELF_PLAT_INIT and not clear regs[11]?
> > >
> >
> > I am uncertain about the side effects of changing ELF_PLAT_INIT.
> > From a completeness perspective, changing ELF_PLAT_INIT is suboptimal,
> > rt_sigreturn is affected in another code path, and there may be other
> > syscalls that I am unaware of.
> Save syscall number in thread_info has more side effects, because
> ptrace allows us to change the number during syscall, then we should
> keep consistency between syscall and regs[11].
>
How about the change below:
diff --git a/arch/loongarch/include/asm/syscall.h
b/arch/loongarch/include/asm/syscall.h
index e286dc58476e..954ba53bcc9a 100644
--- a/arch/loongarch/include/asm/syscall.h
+++ b/arch/loongarch/include/asm/syscall.h
@@ -23,7 +23,9 @@ extern void *sys_call_table[];
static inline long syscall_get_nr(struct task_struct *task,
struct pt_regs *regs)
{
- return regs->regs[11];
+ long nr = task_thread_info(task)->syscall;
+
+ return nr ? : regs->regs[11];
}
static inline void syscall_rollback(struct task_struct *task,
diff --git a/arch/loongarch/kernel/syscall.c b/arch/loongarch/kernel/syscall.c
index b4c5acd7aa3b..553ab0d624cb 100644
--- a/arch/loongarch/kernel/syscall.c
+++ b/arch/loongarch/kernel/syscall.c
@@ -53,6 +53,7 @@ void noinstr do_syscall(struct pt_regs *regs)
regs->regs[4] = -ENOSYS;
nr = syscall_enter_from_user_mode(regs, nr);
+ current_thread_info()->syscall = nr;
if (nr < NR_syscalls) {
syscall_fn = sys_call_table[nr];
@@ -61,4 +62,5 @@ void noinstr do_syscall(struct pt_regs *regs)
}
syscall_exit_to_user_mode(regs);
+ current_thread_info()->syscall = 0;
}
* allow ptrace to change syscall nr
* sys_exit_* will also see the right syscall nr
* this works even if rt_sigreturn clobbers all pt_regs::regs
> And about ELF_PLAT_INIT, maybe Arnd can give us some more information.
>
> Hi, Arnd,
>
> I found some new architectures, such as ARM64 and RISC-V, just do
> nearly nothing in ELF_PLAT_INIT, while some old architectures, such as
> x86 and MIPS, clear most of the registers, do you know why?
>
> Huacai
>
> >
> > > Huacai
> > >
> > > >
> > > > Fixes: be769645a2aef ("LoongArch: Add system call support")
> > > > Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> > > > ---
> > > > arch/loongarch/include/asm/syscall.h | 2 +-
> > > > arch/loongarch/kernel/syscall.c | 1 +
> > > > 2 files changed, 2 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/arch/loongarch/include/asm/syscall.h b/arch/loongarch/include/asm/syscall.h
> > > > index e286dc58476e..2317d674b92a 100644
> > > > --- a/arch/loongarch/include/asm/syscall.h
> > > > +++ b/arch/loongarch/include/asm/syscall.h
> > > > @@ -23,7 +23,7 @@ extern void *sys_call_table[];
> > > > static inline long syscall_get_nr(struct task_struct *task,
> > > > struct pt_regs *regs)
> > > > {
> > > > - return regs->regs[11];
> > > > + return task_thread_info(task)->syscall;
> > > > }
> > > >
> > > > static inline void syscall_rollback(struct task_struct *task,
> > > > diff --git a/arch/loongarch/kernel/syscall.c b/arch/loongarch/kernel/syscall.c
> > > > index b4c5acd7aa3b..2783e33cf276 100644
> > > > --- a/arch/loongarch/kernel/syscall.c
> > > > +++ b/arch/loongarch/kernel/syscall.c
> > > > @@ -52,6 +52,7 @@ void noinstr do_syscall(struct pt_regs *regs)
> > > > regs->orig_a0 = regs->regs[4];
> > > > regs->regs[4] = -ENOSYS;
> > > >
> > > > + task_thread_info(current)->syscall = nr;
> > > > nr = syscall_enter_from_user_mode(regs, nr);
> > > >
> > > > if (nr < NR_syscalls) {
> > > > --
> > > > 2.42.0
> > > >
next prev parent reply other threads:[~2023-11-23 5:49 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20231121070209.210934-1-hengqi.chen@gmail.com>
[not found] ` <CAAhV-H7SwSRDh8Ui2xVb1ncoaEQVd=dugphcBemkeaPNGQX2qw@mail.gmail.com>
[not found] ` <CAEyhmHRYghT5iFiLByUmC=AjdygiBWU8TH3joSyyWibu0Ki2xw@mail.gmail.com>
2023-11-22 7:58 ` [PATCH] LoongArch: Store syscall nr in thread_info Huacai Chen
2023-11-23 5:49 ` Hengqi Chen [this message]
2023-11-23 6:13 ` Huacai Chen
2023-11-23 8:08 ` Hengqi Chen
2023-11-23 8:25 ` Huacai Chen
2023-11-23 14:39 ` Hengqi Chen
2023-12-03 3:17 ` Huacai Chen
2023-12-04 1:55 ` Hengqi Chen
2023-12-04 2:16 ` Huacai Chen
2023-12-04 5:39 ` Hengqi Chen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAEyhmHRFZqH=4K8WUO6tPFd0NoWMHm_iLan+4EPiqovO03w1xw@mail.gmail.com' \
--to=hengqi.chen@gmail.com \
--cc=arnd@arndb.de \
--cc=chenhuacai@kernel.org \
--cc=kernel@xen0n.name \
--cc=linux-arch@vger.kernel.org \
--cc=loongarch@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).