Skip to contentSkip to conversation

riscv: Fix two vdso issue

PATCH In review Tong Tiangen 4 participants 8 messages Sep 1, 2021 – Oct 2, 2021 RISC-V

lore
Share & tools
lore
#0 lore raw

v3 cover riscv: Fix two vdso issue

v3->v2:
Adapt to the latest code.

v2->v1:
Add patch "Refactor asm/vdso.h" to avoid vdso.lds syntax error if CONFIG_GENERIC_TIME_VSYSCALL=n.

1) Move vdso data page up front and introduce enum vvar_pages, This makes it easier to introduce new feature TIME_NS.

2) In arch_setup_additional_pages(), make Wait for the lock in the killable mode and return with EINTR if the task got killed while waiting.

Tong Tiangen (3):
riscv/vdso: Refactor asm/vdso.h
riscv/vdso: Move vdso data page up front
riscv/vdso: make arch_setup_additional_pages wait for mmap_sem for
write killable

 arch/riscv/include/asm/syscall.h  |  1 +
 arch/riscv/include/asm/vdso.h     | 15 ++++-----
 arch/riscv/kernel/syscall_table.c |  1 -
 arch/riscv/kernel/vdso.c          | 53 +++++++++++++++++++------------
 arch/riscv/kernel/vdso/vdso.lds.S |  3 +-
 5 files changed, 42 insertions(+), 31 deletions(-)
Signature · 1 line
2.18.0.huawei.25
#1 lore raw

PATCH v3 1/3 riscv/vdso: Refactor asm/vdso.h

The asm/vdso.h will be included in vdso.lds.S in the next patch, the following cleanup is needed to avoid syntax error:

1.the declaration of sys_riscv_flush_icache() is moved into asm/syscall.h. 2.the definition of struct vdso_data is moved into kernel/vdso.c. 2.the definition of VDSO_SYMBOL is placed under "#ifndef __ASSEMBLY__".

Also remove the redundant linux/types.h include.

---
 arch/riscv/include/asm/syscall.h  |  1 +
 arch/riscv/include/asm/vdso.h     | 13 ++++---------
 arch/riscv/kernel/syscall_table.c |  1 -
 arch/riscv/kernel/vdso.c          |  5 ++++-
 4 files changed, 9 insertions(+), 11 deletions(-)
arch/riscv/include/asm/syscall.h +1 -0
index b933b1583c9f..34fbb3ea21d5 100644
@@ -82,4 +82,5 @@ static inline int syscall_get_arch(struct task_struct *task)
 #endif
 }
 
+asmlinkage long sys_riscv_flush_icache(uintptr_t, uintptr_t, uintptr_t);
 #endif	/* _ASM_RISCV_SYSCALL_H */
arch/riscv/include/asm/vdso.h +4 -9
index 893e47195e30..8a49f8ecfb52 100644
@@ -8,26 +8,21 @@
 #ifndef _ASM_RISCV_VDSO_H
 #define _ASM_RISCV_VDSO_H
 
-
 /*
  * All systems with an MMU have a VDSO, but systems without an MMU don't
  * support shared libraries and therefor don't have one.
  */
 #ifdef CONFIG_MMU
 
-#include <linux/types.h>
-#include <generated/vdso-offsets.h>
+#ifndef __ASSEMBLY__
 
-#ifndef CONFIG_GENERIC_TIME_VSYSCALL
-struct vdso_data {
-};
-#endif
+#include <generated/vdso-offsets.h>
 
 #define VDSO_SYMBOL(base, name)							\
 	(void __user *)((unsigned long)(base) + __vdso_##name##_offset)
 
-#endif /* CONFIG_MMU */
+#endif /* !__ASSEMBLY__ */
 
-asmlinkage long sys_riscv_flush_icache(uintptr_t, uintptr_t, uintptr_t);
+#endif /* CONFIG_MMU */
 
 #endif /* _ASM_RISCV_VDSO_H */
arch/riscv/kernel/syscall_table.c +0 -1
index a63c667c27b3..44b1420a2270 100644
@@ -7,7 +7,6 @@
 #include <linux/linkage.h>
 #include <linux/syscalls.h>
 #include <asm-generic/syscalls.h>
-#include <asm/vdso.h>
 #include <asm/syscall.h>
 
 #undef __SYSCALL
arch/riscv/kernel/vdso.c +4 -1
index 25a3b8849599..72e93d218335 100644
@@ -12,10 +12,13 @@
 #include <linux/binfmts.h>
 #include <linux/err.h>
 #include <asm/page.h>
+#include <asm/vdso.h>
+
 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
 #include <vdso/datapage.h>
 #else
-#include <asm/vdso.h>
+struct vdso_data {
+};
 #endif
 
 extern char vdso_start[], vdso_end[];
Signature · 1 line
2.18.0.huawei.25
#2 lore raw

PATCH v3 2/3 riscv/vdso: Move vdso data page up front

In reply to Tong Tiangen

As commit 601255ae3c98 ("arm64: vdso: move data page before code pages"), the same issue exists on riscv, testcase is shown below, make sure that vdso.so is bigger than page size,

  struct timespec tp;
  clock_gettime(5, &tp);
  printf("tv_sec: %ld, tv_nsec: %ld\n", tp.tv_sec, tp.tv_nsec);

without this patch, test result : tv_sec: 0, tv_nsec: 0
with this patch, test result : tv_sec: 1629271537, tv_nsec: 748000000

Move the vdso data page in front of the VDSO area to fix the issue.

Fixes ad5d1122b82fb ("riscv: use vDSO common flow to reduce the latency of the time-related functions")
---
 arch/riscv/include/asm/vdso.h     |  2 ++
 arch/riscv/kernel/vdso.c          | 44 ++++++++++++++++++-------------
 arch/riscv/kernel/vdso/vdso.lds.S |  3 ++-
 3 files changed, 30 insertions(+), 19 deletions(-)
arch/riscv/include/asm/vdso.h +2 -0
index 8a49f8ecfb52..34210b22ba91 100644
@@ -14,6 +14,8 @@
  */
 #ifdef CONFIG_MMU
 
+#define __VVAR_PAGES    1
+
 #ifndef __ASSEMBLY__
 
 #include <generated/vdso-offsets.h>
arch/riscv/kernel/vdso.c +26 -18

82 lines not on this page · raw message

arch/riscv/kernel/vdso/vdso.lds.S +2 -1
index e6f558bca71b..e9111f700af0 100644
@@ -3,12 +3,13 @@
  * Copyright (C) 2012 Regents of the University of California
  */
 #include <asm/page.h>
+#include <asm/vdso.h>
 
 OUTPUT_ARCH(riscv)
 
 SECTIONS
 {
-	PROVIDE(_vdso_data = . + PAGE_SIZE);
+	PROVIDE(_vdso_data = . - __VVAR_PAGES * PAGE_SIZE);
 	. = SIZEOF_HEADERS;
 
 	.hash		: { *(.hash) }			:text
Signature · 1 line
2.18.0.huawei.25
#3 lore raw

PATCH v3 3/3 riscv/vdso: make arch_setup_additional_pages wait for mmap_sem for write killable

In reply to Tong Tiangen

riscv architectures relying on mmap_sem for write in their arch_setup_additional_pages. If the waiting task gets killed by the oom killer it would block oom_reaper from asynchronous address space reclaim and reduce the chances of timely OOM resolving. Wait for the lock in the killable mode and return with EINTR if the task got killed while waiting.

---
 arch/riscv/kernel/vdso.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/kernel/vdso.c +3 -1
index e7bd92d8749b..b70956d80408 100644
@@ -77,7 +77,9 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,
 
 	vdso_len = (vdso_pages + VVAR_NR_PAGES) << PAGE_SHIFT;
 
-	mmap_write_lock(mm);
+	if (mmap_write_lock_killable(mm))
+		return -EINTR;
+
 	vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
 	if (IS_ERR_VALUE(vdso_base)) {
 		ret = vdso_base;
Signature · 1 line
2.18.0.huawei.25
#4 lore raw
In reply to Tong Tiangen
Quoted 19 lines · Tong Tiangen

v3->v2:
Adapt to the latest code.

v2->v1:
Add patch "Refactor asm/vdso.h" to avoid vdso.lds syntax error if CONFIG_GENERIC_TIME_VSYSCALL=n.

1) Move vdso data page up front and introduce enum vvar_pages, This makes it easier to introduce new feature TIME_NS.

2) In arch_setup_additional_pages(), make Wait for the lock in the killable mode and return with EINTR if the task got killed while waiting.

Tong Tiangen (3):
riscv/vdso: Refactor asm/vdso.h
riscv/vdso: Move vdso data page up front
riscv/vdso: make arch_setup_additional_pages wait for mmap_sem for
write killable

Quoted 7 lines
 arch/riscv/include/asm/syscall.h  |  1 +
 arch/riscv/include/asm/vdso.h     | 15 ++++-----
 arch/riscv/kernel/syscall_table.c |  1 -
 arch/riscv/kernel/vdso.c          | 53 +++++++++++++++++++------------
 arch/riscv/kernel/vdso/vdso.lds.S |  3 +-
 5 files changed, 42 insertions(+), 31 deletions(-)
#5 lore raw

kindly ping.

Quoted 31 lines · Liu Shixin
Quoted 19 lines · Tong Tiangen

v3->v2:
Adapt to the latest code.

v2->v1:
Add patch "Refactor asm/vdso.h" to avoid vdso.lds syntax error if CONFIG_GENERIC_TIME_VSYSCALL=n.

1) Move vdso data page up front and introduce enum vvar_pages, This makes it easier to introduce new feature TIME_NS.

2) In arch_setup_additional_pages(), make Wait for the lock in the killable mode and return with EINTR if the task got killed while waiting.

Tong Tiangen (3):
riscv/vdso: Refactor asm/vdso.h
riscv/vdso: Move vdso data page up front
riscv/vdso: make arch_setup_additional_pages wait for mmap_sem for
write killable

Quoted 7 lines
 arch/riscv/include/asm/syscall.h  |  1 +
 arch/riscv/include/asm/vdso.h     | 15 ++++-----
 arch/riscv/kernel/syscall_table.c |  1 -
 arch/riscv/kernel/vdso.c          | 53 +++++++++++++++++++------------
 arch/riscv/kernel/vdso/vdso.lds.S |  3 +-
 5 files changed, 42 insertions(+), 31 deletions(-)

.

Reviewed-by on the v3 series
#6 lore raw
In reply to Tong Tiangen

Hi Palmer, could this patchset be merged into v5.15, it fixes the serious problems about the basic time api.

Sergey[1] does meet same issue too. Thanks.

[1] https://patchwork.kernel.org/project/linux-riscv/patch/[email protected]/

Quoted 7 lines · Tong Tiangen

v3->v2:
Adapt to the latest code.

v2->v1:
Add patch "Refactor asm/vdso.h" to avoid vdso.lds syntax error if CONFIG_GENERIC_TIME_VSYSCALL=n.

Quoted 5 lines

1) Move vdso data page up front and introduce enum vvar_pages, This makes it easier to introduce new feature TIME_NS.

2) In arch_setup_additional_pages(), make Wait for the lock in the killable mode and return with EINTR if the task got killed while waiting.

maintainer
#7 lore raw
In reply to Tong Tiangen
Quoted 19 lines · On Tue, 31 Aug 2021 19:46:18 PDT (-0700), [email protected] wrote:

v3->v2:
Adapt to the latest code.

v2->v1:
Add patch "Refactor asm/vdso.h" to avoid vdso.lds syntax error if CONFIG_GENERIC_TIME_VSYSCALL=n.

1) Move vdso data page up front and introduce enum vvar_pages, This makes it easier to introduce new feature TIME_NS.

2) In arch_setup_additional_pages(), make Wait for the lock in the killable mode and return with EINTR if the task got killed while waiting.

Tong Tiangen (3):
riscv/vdso: Refactor asm/vdso.h
riscv/vdso: Move vdso data page up front
riscv/vdso: make arch_setup_additional_pages wait for mmap_sem for
write killable

Quoted 6 lines
 arch/riscv/include/asm/syscall.h  |  1 +
 arch/riscv/include/asm/vdso.h     | 15 ++++-----
 arch/riscv/kernel/syscall_table.c |  1 -
 arch/riscv/kernel/vdso.c          | 53 +++++++++++++++++++------------
 arch/riscv/kernel/vdso/vdso.lds.S |  3 +-
 5 files changed, 42 insertions(+), 31 deletions(-)

Sorry, I thought this was targeted at for-next. I've put this on fixes (not sure if my last copy went out, my mail client crashed).