diff mbox series

[RFC,3/4] riscv: Introduce is_compat_thread() into compat.h

Message ID 20231222074605.452452-4-leobras@redhat.com (mailing list archive)
State New
Headers show
Series Introduce & Optimize compat-mode helpers | expand

Commit Message

Leonardo Bras Dec. 22, 2023, 7:46 a.m. UTC
task_user_regset_view() makes use of a function very similar to
is_compat_task(), but pointing to a any thread.

In arm64 asm/compat.h there is a function very similar to that:
is_compat_thread(struct thread_info *thread)

Copy this function to riscv asm/compat.h and make use of it into
task_user_regset_view().

Also, introduce a compile-time test for CONFIG_COMPAT and simplify the
function code by removing the #ifdef.

Signed-off-by: Leonardo Bras <leobras@redhat.com>
---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/kernel/ptrace.c      | 6 +++---
 2 files changed, 11 insertions(+), 3 deletions(-)

Comments

Guo Ren Dec. 22, 2023, 11:20 a.m. UTC | #1
On Fri, Dec 22, 2023 at 3:47 PM Leonardo Bras <leobras@redhat.com> wrote:
>
> task_user_regset_view() makes use of a function very similar to
> is_compat_task(), but pointing to a any thread.
>
> In arm64 asm/compat.h there is a function very similar to that:
> is_compat_thread(struct thread_info *thread)
>
> Copy this function to riscv asm/compat.h and make use of it into
> task_user_regset_view().
>
> Also, introduce a compile-time test for CONFIG_COMPAT and simplify the
> function code by removing the #ifdef.
>
> Signed-off-by: Leonardo Bras <leobras@redhat.com>
> ---
>  arch/riscv/include/asm/compat.h | 8 ++++++++
>  arch/riscv/kernel/ptrace.c      | 6 +++---
>  2 files changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/arch/riscv/include/asm/compat.h b/arch/riscv/include/asm/compat.h
> index 91517b51b8e27..da4b28cd01a95 100644
> --- a/arch/riscv/include/asm/compat.h
> +++ b/arch/riscv/include/asm/compat.h
> @@ -20,6 +20,14 @@ static inline int is_compat_task(void)
>         return test_thread_flag(TIF_32BIT);
>  }
>
> +static inline int is_compat_thread(struct thread_info *thread)
> +{
> +       if (!IS_ENABLED(CONFIG_COMPAT))
> +               return 0;
We also could put this into is_compat_task().

> +
> +       return test_ti_thread_flag(thread, TIF_32BIT);
> +}
> +
>  struct compat_user_regs_struct {
>         compat_ulong_t pc;
>         compat_ulong_t ra;
> diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c
> index 2afe460de16a6..f362832123616 100644
> --- a/arch/riscv/kernel/ptrace.c
> +++ b/arch/riscv/kernel/ptrace.c
> @@ -374,14 +374,14 @@ long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
>
>         return ret;
>  }
> +#else
> +static const struct user_regset_view compat_riscv_user_native_view = {};
>  #endif /* CONFIG_COMPAT */
>
>  const struct user_regset_view *task_user_regset_view(struct task_struct *task)
>  {
> -#ifdef CONFIG_COMPAT
> -       if (test_tsk_thread_flag(task, TIF_32BIT))
> +       if (is_compat_thread(&task->thread_info))
>                 return &compat_riscv_user_native_view;
>         else
> -#endif
>                 return &riscv_user_native_view;
>  }
> --
> 2.43.0
>
LGTM

Reviewed-by: Guo Ren <guoren@kernel.org>
Leonardo Bras Dec. 22, 2023, 5:14 p.m. UTC | #2
On Fri, Dec 22, 2023 at 07:20:36PM +0800, Guo Ren wrote:
> On Fri, Dec 22, 2023 at 3:47 PM Leonardo Bras <leobras@redhat.com> wrote:
> >
> > task_user_regset_view() makes use of a function very similar to
> > is_compat_task(), but pointing to a any thread.
> >
> > In arm64 asm/compat.h there is a function very similar to that:
> > is_compat_thread(struct thread_info *thread)
> >
> > Copy this function to riscv asm/compat.h and make use of it into
> > task_user_regset_view().
> >
> > Also, introduce a compile-time test for CONFIG_COMPAT and simplify the
> > function code by removing the #ifdef.
> >
> > Signed-off-by: Leonardo Bras <leobras@redhat.com>
> > ---
> >  arch/riscv/include/asm/compat.h | 8 ++++++++
> >  arch/riscv/kernel/ptrace.c      | 6 +++---
> >  2 files changed, 11 insertions(+), 3 deletions(-)
> >
> > diff --git a/arch/riscv/include/asm/compat.h b/arch/riscv/include/asm/compat.h
> > index 91517b51b8e27..da4b28cd01a95 100644
> > --- a/arch/riscv/include/asm/compat.h
> > +++ b/arch/riscv/include/asm/compat.h
> > @@ -20,6 +20,14 @@ static inline int is_compat_task(void)
> >         return test_thread_flag(TIF_32BIT);
> >  }
> >
> > +static inline int is_compat_thread(struct thread_info *thread)
> > +{
> > +       if (!IS_ENABLED(CONFIG_COMPAT))
> > +               return 0;
> We also could put this into is_compat_task().
> 

Yeah! it's done in a previous patch. :)

> > +
> > +       return test_ti_thread_flag(thread, TIF_32BIT);
> > +}
> > +
> >  struct compat_user_regs_struct {
> >         compat_ulong_t pc;
> >         compat_ulong_t ra;
> > diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c
> > index 2afe460de16a6..f362832123616 100644
> > --- a/arch/riscv/kernel/ptrace.c
> > +++ b/arch/riscv/kernel/ptrace.c
> > @@ -374,14 +374,14 @@ long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
> >
> >         return ret;
> >  }
> > +#else
> > +static const struct user_regset_view compat_riscv_user_native_view = {};
> >  #endif /* CONFIG_COMPAT */
> >
> >  const struct user_regset_view *task_user_regset_view(struct task_struct *task)
> >  {
> > -#ifdef CONFIG_COMPAT
> > -       if (test_tsk_thread_flag(task, TIF_32BIT))
> > +       if (is_compat_thread(&task->thread_info))
> >                 return &compat_riscv_user_native_view;
> >         else
> > -#endif
> >                 return &riscv_user_native_view;
> >  }
> > --
> > 2.43.0
> >
> LGTM
> 
> Reviewed-by: Guo Ren <guoren@kernel.org>

Thanks!
Leo

> 
> -- 
> Best Regards
>  Guo Ren
>
Andy Chiu Dec. 23, 2023, 4:25 a.m. UTC | #3
On Fri, Dec 22, 2023 at 3:46 PM Leonardo Bras <leobras@redhat.com> wrote:
>
> task_user_regset_view() makes use of a function very similar to
> is_compat_task(), but pointing to a any thread.
>
> In arm64 asm/compat.h there is a function very similar to that:
> is_compat_thread(struct thread_info *thread)
>
> Copy this function to riscv asm/compat.h and make use of it into
> task_user_regset_view().
>
> Also, introduce a compile-time test for CONFIG_COMPAT and simplify the
> function code by removing the #ifdef.
>
> Signed-off-by: Leonardo Bras <leobras@redhat.com>
> ---
>  arch/riscv/include/asm/compat.h | 8 ++++++++
>  arch/riscv/kernel/ptrace.c      | 6 +++---
>  2 files changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/arch/riscv/include/asm/compat.h b/arch/riscv/include/asm/compat.h
> index 91517b51b8e27..da4b28cd01a95 100644
> --- a/arch/riscv/include/asm/compat.h
> +++ b/arch/riscv/include/asm/compat.h
> @@ -20,6 +20,14 @@ static inline int is_compat_task(void)
>         return test_thread_flag(TIF_32BIT);
>  }
>
> +static inline int is_compat_thread(struct thread_info *thread)
> +{
> +       if (!IS_ENABLED(CONFIG_COMPAT))
> +               return 0;
> +
> +       return test_ti_thread_flag(thread, TIF_32BIT);
> +}
> +

Does it make sense to use a #ifdef CONFIG_COMPAT clause to group
is_compat_thread() and is_compat_flag()? For example,

#ifdef CONFIG_COMPAT
static inline int is_compat_thread(struct thread_info *thread)
{
 return test_ti_thread_flag(thread, TIF_32BIT);
}
static inline int is_compat_task(void)
{
 return is_compat_thread(current);
}
#else
static inline int is_compat_thread(struct thread_info *thread) { return 0; }
static inline int is_compat_task(void) { return 0; }
#endif

>  struct compat_user_regs_struct {
>         compat_ulong_t pc;
>         compat_ulong_t ra;
> diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c
> index 2afe460de16a6..f362832123616 100644
> --- a/arch/riscv/kernel/ptrace.c
> +++ b/arch/riscv/kernel/ptrace.c
> @@ -374,14 +374,14 @@ long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
>
>         return ret;
>  }
> +#else
> +static const struct user_regset_view compat_riscv_user_native_view = {};
>  #endif /* CONFIG_COMPAT */
>
>  const struct user_regset_view *task_user_regset_view(struct task_struct *task)
>  {
> -#ifdef CONFIG_COMPAT
> -       if (test_tsk_thread_flag(task, TIF_32BIT))
> +       if (is_compat_thread(&task->thread_info))
>                 return &compat_riscv_user_native_view;
>         else
> -#endif
>                 return &riscv_user_native_view;
>  }
> --
> 2.43.0
>
Andy Chiu Dec. 28, 2023, 3:10 a.m. UTC | #4
On Wed, Dec 27, 2023 at 3:25 AM Leonardo Bras Soares Passos
<leobras@redhat.com> wrote:
>
> On Sat, Dec 23, 2023 at 1:26 AM Andy Chiu <andy.chiu@sifive.com> wrote:
> >
> > On Fri, Dec 22, 2023 at 3:46 PM Leonardo Bras <leobras@redhat.com> wrote:
> > >
> > > task_user_regset_view() makes use of a function very similar to
> > > is_compat_task(), but pointing to a any thread.
> > >
> > > In arm64 asm/compat.h there is a function very similar to that:
> > > is_compat_thread(struct thread_info *thread)
> > >
> > > Copy this function to riscv asm/compat.h and make use of it into
> > > task_user_regset_view().
> > >
> > > Also, introduce a compile-time test for CONFIG_COMPAT and simplify the
> > > function code by removing the #ifdef.
> > >
> > > Signed-off-by: Leonardo Bras <leobras@redhat.com>
> > > ---
> > >  arch/riscv/include/asm/compat.h | 8 ++++++++
> > >  arch/riscv/kernel/ptrace.c      | 6 +++---
> > >  2 files changed, 11 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/arch/riscv/include/asm/compat.h b/arch/riscv/include/asm/compat.h
> > > index 91517b51b8e27..da4b28cd01a95 100644
> > > --- a/arch/riscv/include/asm/compat.h
> > > +++ b/arch/riscv/include/asm/compat.h
> > > @@ -20,6 +20,14 @@ static inline int is_compat_task(void)
> > >         return test_thread_flag(TIF_32BIT);
> > >  }
> > >
> > > +static inline int is_compat_thread(struct thread_info *thread)
> > > +{
> > > +       if (!IS_ENABLED(CONFIG_COMPAT))
> > > +               return 0;
> > > +
> > > +       return test_ti_thread_flag(thread, TIF_32BIT);
> > > +}
> > > +
> >
> > Does it make sense to use a #ifdef CONFIG_COMPAT clause to group
> > is_compat_thread() and is_compat_flag()? For example,
>
> Hello Andy,
>
> Sure, it does make sense.
>
> But I honestly think that using IS_ENABLED() instead of #ifdef +
> multiple same-named functions works better for code reading, at least
> for small functions such as these.
>
> Does this make sense?
>
> Thanks for reviewing!
> Leo

Hi, yes!

It makes sense to me.

Reviewed-by: Andy Chiu <andy.chiu@sifive.com>

Thanks,
Andy

>
>
> >
> > #ifdef CONFIG_COMPAT
> > static inline int is_compat_thread(struct thread_info *thread)
> > {
> >  return test_ti_thread_flag(thread, TIF_32BIT);
> > }
> > static inline int is_compat_task(void)
> > {
> >  return is_compat_thread(current);
> > }
> > #else
> > static inline int is_compat_thread(struct thread_info *thread) { return 0; }
> > static inline int is_compat_task(void) { return 0; }
> > #endif
> >
> > >  struct compat_user_regs_struct {
> > >         compat_ulong_t pc;
> > >         compat_ulong_t ra;
> > > diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c
> > > index 2afe460de16a6..f362832123616 100644
> > > --- a/arch/riscv/kernel/ptrace.c
> > > +++ b/arch/riscv/kernel/ptrace.c
> > > @@ -374,14 +374,14 @@ long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
> > >
> > >         return ret;
> > >  }
> > > +#else
> > > +static const struct user_regset_view compat_riscv_user_native_view = {};
> > >  #endif /* CONFIG_COMPAT */
> > >
> > >  const struct user_regset_view *task_user_regset_view(struct task_struct *task)
> > >  {
> > > -#ifdef CONFIG_COMPAT
> > > -       if (test_tsk_thread_flag(task, TIF_32BIT))
> > > +       if (is_compat_thread(&task->thread_info))
> > >                 return &compat_riscv_user_native_view;
> > >         else
> > > -#endif
> > >                 return &riscv_user_native_view;
> > >  }
> > > --
> > > 2.43.0
> > >
> >
>
diff mbox series

Patch

diff --git a/arch/riscv/include/asm/compat.h b/arch/riscv/include/asm/compat.h
index 91517b51b8e27..da4b28cd01a95 100644
--- a/arch/riscv/include/asm/compat.h
+++ b/arch/riscv/include/asm/compat.h
@@ -20,6 +20,14 @@  static inline int is_compat_task(void)
 	return test_thread_flag(TIF_32BIT);
 }
 
+static inline int is_compat_thread(struct thread_info *thread)
+{
+	if (!IS_ENABLED(CONFIG_COMPAT))
+		return 0;
+
+	return test_ti_thread_flag(thread, TIF_32BIT);
+}
+
 struct compat_user_regs_struct {
 	compat_ulong_t pc;
 	compat_ulong_t ra;
diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c
index 2afe460de16a6..f362832123616 100644
--- a/arch/riscv/kernel/ptrace.c
+++ b/arch/riscv/kernel/ptrace.c
@@ -374,14 +374,14 @@  long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
 
 	return ret;
 }
+#else
+static const struct user_regset_view compat_riscv_user_native_view = {};
 #endif /* CONFIG_COMPAT */
 
 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
 {
-#ifdef CONFIG_COMPAT
-	if (test_tsk_thread_flag(task, TIF_32BIT))
+	if (is_compat_thread(&task->thread_info))
 		return &compat_riscv_user_native_view;
 	else
-#endif
 		return &riscv_user_native_view;
 }