linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V3] MIPS: Add get_thread_area syscall
@ 2019-12-16  2:17 Guoyun Sun
  2019-12-16 19:33 ` Paul Burton
  0 siblings, 1 reply; 3+ messages in thread
From: Guoyun Sun @ 2019-12-16  2:17 UTC (permalink / raw)
  To: Ralf Baechle, Paul Burton, James Hogan, Peter Zijlstra,
	Christian Brauner, Arnd Bergmann, Heiko Carstens, David Howells,
	Geert Uytterhoeven, Catalin Marinas, linux-mips, linux-kernel
  Cc: Tiezhu Yang, Xuefeng Li, Guoyun Sun

CRIU(https://criu.org/) tools will dump TLS(Thread Local Storage) by
get_thread_area during checkpoint and restore TLS by set_thread_area during
restore. without this syscall, criu restore will fail on MIPS platform
because a variable with GCC __thread will be invalid.

The following function will be called when criu restore

static inline void restore_tls(tls_t *ptls) {
        asm volatile(
                     "move $4, %0                                   \n"
                     "li $2,  "__stringify(__NR_set_thread_area)"  \n"
                     "syscall                                       \n"
                     :
                     : "r"(*ptls)
                     : "$4","$2","memory");

the *ptls can be obtained by get_thread_area on MIPS platform when criu
checkpoint. just like this:

static inline void arch_get_tls(tls_t *ptls)
{
	asm volatile(
		     "move $4, %0				    \n"
		     "li $2,  "__stringify(__NR_get_thread_area)"  \n"
		     "syscall					    \n"
		     :
		     : "r"(ptls)
		     : "$4","$2","memory");

}

Signed-off-by: Guoyun Sun <sunguoyun@loongson.cn>
---
 arch/mips/kernel/syscall.c                | 11 +++++++++++
 arch/mips/kernel/syscalls/syscall_n32.tbl |  2 ++
 arch/mips/kernel/syscalls/syscall_n64.tbl |  2 ++
 arch/mips/kernel/syscalls/syscall_o32.tbl |  2 ++
 4 files changed, 17 insertions(+)

diff --git a/arch/mips/kernel/syscall.c b/arch/mips/kernel/syscall.c
index c333e57..7ac78bf 100644
--- a/arch/mips/kernel/syscall.c
+++ b/arch/mips/kernel/syscall.c
@@ -27,6 +27,7 @@
 #include <linux/slab.h>
 #include <linux/elf.h>
 #include <linux/sched/task_stack.h>
+#include <linux/compat.h>
 
 #include <asm/asm.h>
 #include <asm/asm-eva.h>
@@ -94,6 +95,16 @@ SYSCALL_DEFINE1(set_thread_area, unsigned long, addr)
 	return 0;
 }
 
+SYSCALL_DEFINE1(get_thread_area, unsigned long __user *, u_info)
+{
+	struct thread_info *ti = task_thread_info(current);
+
+	if (in_compat_syscall())
+		return put_user(ti->tp_value, (__u32 *)u_info);
+
+	return put_user(ti->tp_value, u_info);
+}
+
 static inline int mips_atomic_set(unsigned long addr, unsigned long new)
 {
 	unsigned long old, tmp;
diff --git a/arch/mips/kernel/syscalls/syscall_n32.tbl b/arch/mips/kernel/syscalls/syscall_n32.tbl
index e7c5ab3..3aa4858 100644
--- a/arch/mips/kernel/syscalls/syscall_n32.tbl
+++ b/arch/mips/kernel/syscalls/syscall_n32.tbl
@@ -341,6 +341,8 @@
 330	n32	statx				sys_statx
 331	n32	rseq				sys_rseq
 332	n32	io_pgetevents			compat_sys_io_pgetevents
+333	n32	get_thread_area			sys_get_thread_area
+
 # 333 through 402 are unassigned to sync up with generic numbers
 403	n32	clock_gettime64			sys_clock_gettime
 404	n32	clock_settime64			sys_clock_settime
diff --git a/arch/mips/kernel/syscalls/syscall_n64.tbl b/arch/mips/kernel/syscalls/syscall_n64.tbl
index 13cd665..14b6796 100644
--- a/arch/mips/kernel/syscalls/syscall_n64.tbl
+++ b/arch/mips/kernel/syscalls/syscall_n64.tbl
@@ -337,6 +337,8 @@
 326	n64	statx				sys_statx
 327	n64	rseq				sys_rseq
 328	n64	io_pgetevents			sys_io_pgetevents
+329	n64	get_thread_area			sys_get_thread_area
+
 # 329 through 423 are reserved to sync up with other architectures
 424	n64	pidfd_send_signal		sys_pidfd_send_signal
 425	n64	io_uring_setup			sys_io_uring_setup
diff --git a/arch/mips/kernel/syscalls/syscall_o32.tbl b/arch/mips/kernel/syscalls/syscall_o32.tbl
index 353539e..bb4e050 100644
--- a/arch/mips/kernel/syscalls/syscall_o32.tbl
+++ b/arch/mips/kernel/syscalls/syscall_o32.tbl
@@ -380,6 +380,8 @@
 366	o32	statx				sys_statx
 367	o32	rseq				sys_rseq
 368	o32	io_pgetevents			sys_io_pgetevents_time32	compat_sys_io_pgetevents
+369	o32	get_thread_area			sys_get_thread_area
+
 # room for arch specific calls
 393	o32	semget				sys_semget
 394	o32	semctl				sys_semctl			compat_sys_semctl
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH V3] MIPS: Add get_thread_area syscall
  2019-12-16  2:17 [PATCH V3] MIPS: Add get_thread_area syscall Guoyun Sun
@ 2019-12-16 19:33 ` Paul Burton
  2019-12-17  1:47   ` 孙国云
  0 siblings, 1 reply; 3+ messages in thread
From: Paul Burton @ 2019-12-16 19:33 UTC (permalink / raw)
  To: Guoyun Sun
  Cc: Ralf Baechle, James Hogan, Peter Zijlstra, Christian Brauner,
	Arnd Bergmann, Heiko Carstens, David Howells, Geert Uytterhoeven,
	Catalin Marinas, linux-mips, linux-kernel, Tiezhu Yang,
	Xuefeng Li

Hi Guoyun,

On Mon, Dec 16, 2019 at 10:17:51AM +0800, Guoyun Sun wrote:
> CRIU(https://criu.org/) tools will dump TLS(Thread Local Storage) by
> get_thread_area during checkpoint and restore TLS by set_thread_area during
> restore. without this syscall, criu restore will fail on MIPS platform
> because a variable with GCC __thread will be invalid.
> 
> The following function will be called when criu restore
> 
> static inline void restore_tls(tls_t *ptls) {
>         asm volatile(
>                      "move $4, %0                                   \n"
>                      "li $2,  "__stringify(__NR_set_thread_area)"  \n"
>                      "syscall                                       \n"
>                      :
>                      : "r"(*ptls)
>                      : "$4","$2","memory");
> 
> the *ptls can be obtained by get_thread_area on MIPS platform when criu
> checkpoint. just like this:
> 
> static inline void arch_get_tls(tls_t *ptls)
> {
> 	asm volatile(
> 		     "move $4, %0				    \n"
> 		     "li $2,  "__stringify(__NR_get_thread_area)"  \n"
> 		     "syscall					    \n"
> 		     :
> 		     : "r"(ptls)
> 		     : "$4","$2","memory");
> 
> }

Why would you need a syscall for this?

Why not retrieve the value using the rdhwr instruction just like every
other bit of code using thread-local storage does?

ie. something like:

static inline void arch_get_tls(tls_t *ptls)
{
	asm("rdhwr %0, $29" : "=r"(*ptls));
}

Thanks,
    Paul

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Re: [PATCH V3] MIPS: Add get_thread_area syscall
  2019-12-16 19:33 ` Paul Burton
@ 2019-12-17  1:47   ` 孙国云
  0 siblings, 0 replies; 3+ messages in thread
From: 孙国云 @ 2019-12-17  1:47 UTC (permalink / raw)
  To: Paul Burton
  Cc: Ralf Baechle, James Hogan, Peter Zijlstra, Christian Brauner,
	Arnd Bergmann, Heiko Carstens, David Howells, Geert Uytterhoeven,
	Catalin Marinas, linux-mips, linux-kernel, Tiezhu Yang,
	Xuefeng Li

Dear Paul,

Thank you very much.
I didn`t know this way asm("rdhwr %0, $29" : "=r"(*ptls)) before.Now I have a test for it and it realy can work normally.
I just find get_thread_area syscall at path arch/x86.so I have a implement on MIPS platform. And I think set_thread_area and get_thread_area is matched in pairs.
Now it seems that the get_thread_area syscall is not necessary for criu.

Thanks,
    Guoyun Sun

&gt; -----原始邮件-----
&gt; 发件人: "Paul Burton" <paulburton@kernel.org>
&gt; 发送时间: 2019-12-17 03:33:33 (星期二)
&gt; 收件人: "Guoyun Sun" <sunguoyun@loongson.cn>
&gt; 抄送: "Ralf Baechle" <ralf@linux-mips.org>, "James Hogan" <jhogan@kernel.org>, "Peter Zijlstra" <peterz@infradead.org>, "Christian Brauner" <christian.brauner@ubuntu.com>, "Arnd Bergmann" <arnd@arndb.de>, "Heiko Carstens" <heiko.carstens@de.ibm.com>, "David Howells" <dhowells@redhat.com>, "Geert Uytterhoeven" <geert@linux-m68k.org>, "Catalin Marinas" <catalin.marinas@arm.com>, linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, "Tiezhu Yang" <yangtiezhu@loongson.cn>, "Xuefeng Li" <lixuefeng@loongson.cn>
&gt; 主题: Re: [PATCH V3] MIPS: Add get_thread_area syscall
&gt; 
&gt; Hi Guoyun,
&gt; 
&gt; On Mon, Dec 16, 2019 at 10:17:51AM +0800, Guoyun Sun wrote:
&gt; &gt; CRIU(https://criu.org/) tools will dump TLS(Thread Local Storage) by
&gt; &gt; get_thread_area during checkpoint and restore TLS by set_thread_area during
&gt; &gt; restore. without this syscall, criu restore will fail on MIPS platform
&gt; &gt; because a variable with GCC __thread will be invalid.
&gt; &gt; 
&gt; &gt; The following function will be called when criu restore
&gt; &gt; 
&gt; &gt; static inline void restore_tls(tls_t *ptls) {
&gt; &gt;         asm volatile(
&gt; &gt;                      "move $4, %0                                   \n"
&gt; &gt;                      "li $2,  "__stringify(__NR_set_thread_area)"  \n"
&gt; &gt;                      "syscall                                       \n"
&gt; &gt;                      :
&gt; &gt;                      : "r"(*ptls)
&gt; &gt;                      : "$4","$2","memory");
&gt; &gt; 
&gt; &gt; the *ptls can be obtained by get_thread_area on MIPS platform when criu
&gt; &gt; checkpoint. just like this:
&gt; &gt; 
&gt; &gt; static inline void arch_get_tls(tls_t *ptls)
&gt; &gt; {
&gt; &gt; 	asm volatile(
&gt; &gt; 		     "move $4, %0				    \n"
&gt; &gt; 		     "li $2,  "__stringify(__NR_get_thread_area)"  \n"
&gt; &gt; 		     "syscall					    \n"
&gt; &gt; 		     :
&gt; &gt; 		     : "r"(ptls)
&gt; &gt; 		     : "$4","$2","memory");
&gt; &gt; 
&gt; &gt; }
&gt; 
&gt; Why would you need a syscall for this?
&gt; 
&gt; Why not retrieve the value using the rdhwr instruction just like every
&gt; other bit of code using thread-local storage does?
&gt; 
&gt; ie. something like:
&gt; 
&gt; static inline void arch_get_tls(tls_t *ptls)
&gt; {
&gt; 	asm("rdhwr %0, $29" : "=r"(*ptls));
&gt; }
&gt; 
&gt; Thanks,
&gt;     Paul
</lixuefeng@loongson.cn></yangtiezhu@loongson.cn></catalin.marinas@arm.com></geert@linux-m68k.org></dhowells@redhat.com></heiko.carstens@de.ibm.com></arnd@arndb.de></christian.brauner@ubuntu.com></peterz@infradead.org></jhogan@kernel.org></ralf@linux-mips.org></sunguoyun@loongson.cn></paulburton@kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-12-17  1:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-16  2:17 [PATCH V3] MIPS: Add get_thread_area syscall Guoyun Sun
2019-12-16 19:33 ` Paul Burton
2019-12-17  1:47   ` 孙国云

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).