Skip to contentSkip to conversation

Introduce & Optimize compat-mode helpers

RFC PATCH In review Leonardo Bras 3 participants 16 messages Dec 22, 2023 – Dec 28, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Introduce & Optimize compat-mode helpers

I just saw the opportunity of optimizing the helper is_compat_task() by introducing a compile-time test, and it made possible to remove some #ifdef's without any loss of performance.

I also saw the possibility of removing the direct check of task flags from general code, and concentrated it in asm/compat.h by creating a few more helpers, which in the end helped optimize code.

Leonardo Bras (4):
riscv: Replace direct thread flag check with is_compat_task()
riscv: add compile-time test into is_compat_task()
riscv: Introduce is_compat_thread() into compat.h
riscv: Introduce set_compat_task() in asm/compat.h

 arch/riscv/include/asm/compat.h    | 19 +++++++++++++++++++
 arch/riscv/include/asm/elf.h       | 11 ++---------
 arch/riscv/include/asm/pgtable.h   |  8 +-------
 arch/riscv/include/asm/processor.h |  4 ++--
 arch/riscv/kernel/ptrace.c         |  6 +++---
 5 files changed, 27 insertions(+), 21 deletions(-)

base-commit: 24e0d2e527a39f64caeb2e6be39ad5396fb2da5e

Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH 1/4 riscv: Replace direct thread flag check with is_compat_task()

There is some code that detects compat mode into a task by checking the flag directly, and other code that check using the helper is_compat_task().

Since the helper already exists, use it instead of checking the flags directly.

---
 arch/riscv/include/asm/elf.h     | 2 +-
 arch/riscv/include/asm/pgtable.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/riscv/include/asm/elf.h +1 -1
index 06c236bfab53b..59a08367fddd7 100644
@@ -54,7 +54,7 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr);
 
 #ifdef CONFIG_64BIT
 #ifdef CONFIG_COMPAT
-#define STACK_RND_MASK		(test_thread_flag(TIF_32BIT) ? \
+#define STACK_RND_MASK		(is_compat_task() ? \
 				 0x7ff >> (PAGE_SHIFT - 12) : \
 				 0x3ffff >> (PAGE_SHIFT - 12))
 #else
arch/riscv/include/asm/pgtable.h +1 -1
index ab00235b018f8..1d472b31e0cfe 100644
@@ -882,7 +882,7 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)
 
 #ifdef CONFIG_COMPAT
 #define TASK_SIZE_32	(_AC(0x80000000, UL) - PAGE_SIZE)
-#define TASK_SIZE	(test_thread_flag(TIF_32BIT) ? \
+#define TASK_SIZE	(is_compat_task() ? \
 			 TASK_SIZE_32 : TASK_SIZE_64)
 #else
 #define TASK_SIZE	TASK_SIZE_64
Signature · 1 line
2.43.0
#2 lore raw

RFC PATCH 2/4 riscv: add compile-time test into is_compat_task()

In reply to Leonardo Bras

Currently several places will test for CONFIG_COMPAT before testing is_compat_task(), probably in order to avoid a run-time test into the task structure.

Since is_compat_task() is an inlined function, it would be helpful to add a compile-time test of CONFIG_COMPAT, making sure it always returns zero when the option is not enabled during the kernel build.

With this, the compiler is able to understand in build-time that is_compat_task() will always return 0, and optimize-out some of the extra code introduced by the option.

This will also allow removing a lot #ifdefs that were introduced, and make the code more clean.

---
 arch/riscv/include/asm/compat.h    | 3 +++
 arch/riscv/include/asm/elf.h       | 4 ----
 arch/riscv/include/asm/pgtable.h   | 6 ------
 arch/riscv/include/asm/processor.h | 4 ++--
 4 files changed, 5 insertions(+), 12 deletions(-)
arch/riscv/include/asm/compat.h +3 -0
index 2ac955b51148f..91517b51b8e27 100644
@@ -14,6 +14,9 @@
 
 static inline int is_compat_task(void)
 {
+	if (!IS_ENABLED(CONFIG_COMPAT))
+		return 0;
+
 	return test_thread_flag(TIF_32BIT);
 }
 
arch/riscv/include/asm/elf.h +0 -4
index 59a08367fddd7..2e88257cafaea 100644
@@ -53,13 +53,9 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr);
 #define ELF_ET_DYN_BASE		((DEFAULT_MAP_WINDOW / 3) * 2)
 
 #ifdef CONFIG_64BIT
-#ifdef CONFIG_COMPAT
 #define STACK_RND_MASK		(is_compat_task() ? \
 				 0x7ff >> (PAGE_SHIFT - 12) : \
 				 0x3ffff >> (PAGE_SHIFT - 12))
-#else
-#define STACK_RND_MASK		(0x3ffff >> (PAGE_SHIFT - 12))
-#endif
 #endif
 
 /*
arch/riscv/include/asm/pgtable.h +0 -6
index 1d472b31e0cfe..ea5b269be223a 100644
@@ -127,16 +127,10 @@
 #define VA_USER_SV48 (UL(1) << (VA_BITS_SV48 - 1))
 #define VA_USER_SV57 (UL(1) << (VA_BITS_SV57 - 1))
 
-#ifdef CONFIG_COMPAT
 #define MMAP_VA_BITS_64 ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
 #define MMAP_MIN_VA_BITS_64 (VA_BITS_SV39)
 #define MMAP_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_VA_BITS_64)
 #define MMAP_MIN_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_MIN_VA_BITS_64)
-#else
-#define MMAP_VA_BITS ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
-#define MMAP_MIN_VA_BITS (VA_BITS_SV39)
-#endif /* CONFIG_COMPAT */
-
 #else
 #include <asm/pgtable-32.h>
 #endif /* CONFIG_64BIT */
arch/riscv/include/asm/processor.h +2 -2
index f19f861cda549..ed32e53e55999 100644
@@ -22,7 +22,7 @@
 ({								\
 	unsigned long mmap_end;					\
 	typeof(addr) _addr = (addr);				\
-	if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
+	if ((_addr) == 0 || is_compat_task())			\
 		mmap_end = STACK_TOP_MAX;			\
 	else if ((_addr) >= VA_USER_SV57)			\
 		mmap_end = STACK_TOP_MAX;			\
@@ -39,7 +39,7 @@
 	typeof(addr) _addr = (addr);				\
 	typeof(base) _base = (base);				\
 	unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base);	\
-	if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
+	if ((_addr) == 0 || is_compat_task())			\
 		mmap_base = (_base);				\
 	else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \
 		mmap_base = VA_USER_SV57 - rnd_gap;		\
Signature · 1 line
2.43.0
Reviewed-by on v1 2/4
#3 lore raw

Re: [RFC PATCH 2/4] riscv: add compile-time test into is_compat_task()

Quoted 100 lines · Leonardo Bras

Currently several places will test for CONFIG_COMPAT before testing is_compat_task(), probably in order to avoid a run-time test into the task structure.

Since is_compat_task() is an inlined function, it would be helpful to add a compile-time test of CONFIG_COMPAT, making sure it always returns zero when the option is not enabled during the kernel build.

With this, the compiler is able to understand in build-time that is_compat_task() will always return 0, and optimize-out some of the extra code introduced by the option.

This will also allow removing a lot #ifdefs that were introduced, and make the code more clean.

---
 arch/riscv/include/asm/compat.h    | 3 +++
 arch/riscv/include/asm/elf.h       | 4 ----
 arch/riscv/include/asm/pgtable.h   | 6 ------
 arch/riscv/include/asm/processor.h | 4 ++--
 4 files changed, 5 insertions(+), 12 deletions(-)
arch/riscv/include/asm/compat.h +3 -0
index 2ac955b51148f..91517b51b8e27 100644
@@ -14,6 +14,9 @@
 
 static inline int is_compat_task(void)
 {
+       if (!IS_ENABLED(CONFIG_COMPAT))
+               return 0;
+
        return test_thread_flag(TIF_32BIT);
 }
 
arch/riscv/include/asm/elf.h +0 -4
index 59a08367fddd7..2e88257cafaea 100644
@@ -53,13 +53,9 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr);
 #define ELF_ET_DYN_BASE                ((DEFAULT_MAP_WINDOW / 3) * 2)
 
 #ifdef CONFIG_64BIT
-#ifdef CONFIG_COMPAT
 #define STACK_RND_MASK         (is_compat_task() ? \
                                 0x7ff >> (PAGE_SHIFT - 12) : \
                                 0x3ffff >> (PAGE_SHIFT - 12))
-#else
-#define STACK_RND_MASK         (0x3ffff >> (PAGE_SHIFT - 12))
-#endif
 #endif
 
 /*
arch/riscv/include/asm/pgtable.h +0 -6
index 1d472b31e0cfe..ea5b269be223a 100644
@@ -127,16 +127,10 @@
 #define VA_USER_SV48 (UL(1) << (VA_BITS_SV48 - 1))
 #define VA_USER_SV57 (UL(1) << (VA_BITS_SV57 - 1))
 
-#ifdef CONFIG_COMPAT
 #define MMAP_VA_BITS_64 ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
 #define MMAP_MIN_VA_BITS_64 (VA_BITS_SV39)
 #define MMAP_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_VA_BITS_64)
 #define MMAP_MIN_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_MIN_VA_BITS_64)
-#else
-#define MMAP_VA_BITS ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
-#define MMAP_MIN_VA_BITS (VA_BITS_SV39)
-#endif /* CONFIG_COMPAT */
-
 #else
 #include <asm/pgtable-32.h>
 #endif /* CONFIG_64BIT */
arch/riscv/include/asm/processor.h +2 -3
index f19f861cda549..ed32e53e55999 100644
@@ -22,7 +22,7 @@
 ({                                                             \
        unsigned long mmap_end;                                 \
        typeof(addr) _addr = (addr);                            \
-       if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
+       if ((_addr) == 0 || is_compat_task())                   \
                mmap_end = STACK_TOP_MAX;                       \
        else if ((_addr) >= VA_USER_SV57)                       \
                mmap_end = STACK_TOP_MAX;                       \
@@ -39,7 +39,7 @@
        typeof(addr) _addr = (addr);                            \
        typeof(base) _base = (base);                            \
        unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base);   \
-       if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
+       if ((_addr) == 0 || is_compat_task())                   \
                mmap_base = (_base);                            \
        else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \
                mmap_base = VA_USER_SV57 - rnd_gap;             \
--
Quoted 2 lines

2.43.0

Signature · 2 lines
Best Regards
 Guo Ren
#4 lore raw

Re: [RFC PATCH 2/4] riscv: add compile-time test into is_compat_task()

Quoted 96 lines · Guo Ren
Quoted 95 lines · Leonardo Bras

Currently several places will test for CONFIG_COMPAT before testing is_compat_task(), probably in order to avoid a run-time test into the task structure.

Since is_compat_task() is an inlined function, it would be helpful to add a compile-time test of CONFIG_COMPAT, making sure it always returns zero when the option is not enabled during the kernel build.

With this, the compiler is able to understand in build-time that is_compat_task() will always return 0, and optimize-out some of the extra code introduced by the option.

This will also allow removing a lot #ifdefs that were introduced, and make the code more clean.

---
 arch/riscv/include/asm/compat.h    | 3 +++
 arch/riscv/include/asm/elf.h       | 4 ----
 arch/riscv/include/asm/pgtable.h   | 6 ------
 arch/riscv/include/asm/processor.h | 4 ++--
 4 files changed, 5 insertions(+), 12 deletions(-)
arch/riscv/include/asm/compat.h +3 -0
index 2ac955b51148f..91517b51b8e27 100644
@@ -14,6 +14,9 @@
 
 static inline int is_compat_task(void)
 {
+       if (!IS_ENABLED(CONFIG_COMPAT))
+               return 0;
+
        return test_thread_flag(TIF_32BIT);
 }
 
arch/riscv/include/asm/elf.h +0 -4
index 59a08367fddd7..2e88257cafaea 100644
@@ -53,13 +53,9 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr);
 #define ELF_ET_DYN_BASE                ((DEFAULT_MAP_WINDOW / 3) * 2)
 
 #ifdef CONFIG_64BIT
-#ifdef CONFIG_COMPAT
 #define STACK_RND_MASK         (is_compat_task() ? \
                                 0x7ff >> (PAGE_SHIFT - 12) : \
                                 0x3ffff >> (PAGE_SHIFT - 12))
-#else
-#define STACK_RND_MASK         (0x3ffff >> (PAGE_SHIFT - 12))
-#endif
 #endif
 
 /*
arch/riscv/include/asm/pgtable.h +0 -6
index 1d472b31e0cfe..ea5b269be223a 100644
@@ -127,16 +127,10 @@
 #define VA_USER_SV48 (UL(1) << (VA_BITS_SV48 - 1))
 #define VA_USER_SV57 (UL(1) << (VA_BITS_SV57 - 1))
 
-#ifdef CONFIG_COMPAT
 #define MMAP_VA_BITS_64 ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
 #define MMAP_MIN_VA_BITS_64 (VA_BITS_SV39)
 #define MMAP_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_VA_BITS_64)
 #define MMAP_MIN_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_MIN_VA_BITS_64)
-#else
-#define MMAP_VA_BITS ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
-#define MMAP_MIN_VA_BITS (VA_BITS_SV39)
-#endif /* CONFIG_COMPAT */
-
 #else
 #include <asm/pgtable-32.h>
 #endif /* CONFIG_64BIT */
arch/riscv/include/asm/processor.h +1 -2
index f19f861cda549..ed32e53e55999 100644
@@ -22,7 +22,7 @@
 ({                                                             \
        unsigned long mmap_end;                                 \
        typeof(addr) _addr = (addr);                            \
-       if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
+       if ((_addr) == 0 || is_compat_task())                   \
                mmap_end = STACK_TOP_MAX;                       \
        else if ((_addr) >= VA_USER_SV57)                       \
                mmap_end = STACK_TOP_MAX;                       \
@@ -39,7 +39,7 @@
        typeof(addr) _addr = (addr);                            \
        typeof(base) _base = (base);                            \
        unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base);   \
-       if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
Quoted 8 lines
Quoted 7 lines
arch/riscv/include/asm/processor.h +1 -1
+       if ((_addr) == 0 || is_compat_task())                   \
                mmap_base = (_base);                            \
        else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \
                mmap_base = VA_USER_SV57 - rnd_gap;             \
--

2.43.0

Thanks!
Leo

Quoted 5 lines
Signature · 3 lines
-- 
Best Regards
 Guo Ren
Reviewed-by
#5 lore raw

Re: [RFC PATCH 2/4] riscv: add compile-time test into is_compat_task()

Quoted 107 lines · Leonardo Bras
Quoted 96 lines · Guo Ren
Quoted 95 lines · Leonardo Bras

Currently several places will test for CONFIG_COMPAT before testing is_compat_task(), probably in order to avoid a run-time test into the task structure.

Since is_compat_task() is an inlined function, it would be helpful to add a compile-time test of CONFIG_COMPAT, making sure it always returns zero when the option is not enabled during the kernel build.

With this, the compiler is able to understand in build-time that is_compat_task() will always return 0, and optimize-out some of the extra code introduced by the option.

This will also allow removing a lot #ifdefs that were introduced, and make the code more clean.

---
 arch/riscv/include/asm/compat.h    | 3 +++
 arch/riscv/include/asm/elf.h       | 4 ----
 arch/riscv/include/asm/pgtable.h   | 6 ------
 arch/riscv/include/asm/processor.h | 4 ++--
 4 files changed, 5 insertions(+), 12 deletions(-)
arch/riscv/include/asm/compat.h +3 -0
index 2ac955b51148f..91517b51b8e27 100644
@@ -14,6 +14,9 @@
 
 static inline int is_compat_task(void)
 {
+       if (!IS_ENABLED(CONFIG_COMPAT))
+               return 0;
+
        return test_thread_flag(TIF_32BIT);
 }
 
arch/riscv/include/asm/elf.h +0 -4
index 59a08367fddd7..2e88257cafaea 100644
@@ -53,13 +53,9 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr);
 #define ELF_ET_DYN_BASE                ((DEFAULT_MAP_WINDOW / 3) * 2)
 
 #ifdef CONFIG_64BIT
-#ifdef CONFIG_COMPAT
 #define STACK_RND_MASK         (is_compat_task() ? \
                                 0x7ff >> (PAGE_SHIFT - 12) : \
                                 0x3ffff >> (PAGE_SHIFT - 12))
-#else
-#define STACK_RND_MASK         (0x3ffff >> (PAGE_SHIFT - 12))
-#endif
 #endif
 
 /*
arch/riscv/include/asm/pgtable.h +0 -6
index 1d472b31e0cfe..ea5b269be223a 100644
@@ -127,16 +127,10 @@
 #define VA_USER_SV48 (UL(1) << (VA_BITS_SV48 - 1))
 #define VA_USER_SV57 (UL(1) << (VA_BITS_SV57 - 1))
 
-#ifdef CONFIG_COMPAT
 #define MMAP_VA_BITS_64 ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
 #define MMAP_MIN_VA_BITS_64 (VA_BITS_SV39)
 #define MMAP_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_VA_BITS_64)
 #define MMAP_MIN_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_MIN_VA_BITS_64)
-#else
-#define MMAP_VA_BITS ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
-#define MMAP_MIN_VA_BITS (VA_BITS_SV39)
-#endif /* CONFIG_COMPAT */
-
 #else
 #include <asm/pgtable-32.h>
 #endif /* CONFIG_64BIT */
arch/riscv/include/asm/processor.h +1 -2
index f19f861cda549..ed32e53e55999 100644
@@ -22,7 +22,7 @@
 ({                                                             \
        unsigned long mmap_end;                                 \
        typeof(addr) _addr = (addr);                            \
-       if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
+       if ((_addr) == 0 || is_compat_task())                   \
                mmap_end = STACK_TOP_MAX;                       \
        else if ((_addr) >= VA_USER_SV57)                       \
                mmap_end = STACK_TOP_MAX;                       \
@@ -39,7 +39,7 @@
        typeof(addr) _addr = (addr);                            \
        typeof(base) _base = (base);                            \
        unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base);   \
-       if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
Quoted 8 lines
Quoted 7 lines
arch/riscv/include/asm/processor.h +1 -1
+       if ((_addr) == 0 || is_compat_task())                   \
                mmap_base = (_base);                            \
        else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \
                mmap_base = VA_USER_SV57 - rnd_gap;             \
--

2.43.0

Quoted 9 lines

Thanks!
Leo

Quoted 5 lines

--
Best Regards
Guo Ren

#6 lore raw

Re: [RFC PATCH 2/4] riscv: add compile-time test into is_compat_task()

Quoted 113 lines · Andy Chiu
Quoted 111 lines · Leonardo Bras
Quoted 96 lines · Guo Ren
Quoted 95 lines · Leonardo Bras

Currently several places will test for CONFIG_COMPAT before testing is_compat_task(), probably in order to avoid a run-time test into the task structure.

Since is_compat_task() is an inlined function, it would be helpful to add a compile-time test of CONFIG_COMPAT, making sure it always returns zero when the option is not enabled during the kernel build.

With this, the compiler is able to understand in build-time that is_compat_task() will always return 0, and optimize-out some of the extra code introduced by the option.

This will also allow removing a lot #ifdefs that were introduced, and make the code more clean.

---
 arch/riscv/include/asm/compat.h    | 3 +++
 arch/riscv/include/asm/elf.h       | 4 ----
 arch/riscv/include/asm/pgtable.h   | 6 ------
 arch/riscv/include/asm/processor.h | 4 ++--
 4 files changed, 5 insertions(+), 12 deletions(-)
arch/riscv/include/asm/compat.h +3 -0
index 2ac955b51148f..91517b51b8e27 100644
@@ -14,6 +14,9 @@
 
 static inline int is_compat_task(void)
 {
+       if (!IS_ENABLED(CONFIG_COMPAT))
+               return 0;
+
        return test_thread_flag(TIF_32BIT);
 }
 
arch/riscv/include/asm/elf.h +0 -4
index 59a08367fddd7..2e88257cafaea 100644
@@ -53,13 +53,9 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr);
 #define ELF_ET_DYN_BASE                ((DEFAULT_MAP_WINDOW / 3) * 2)
 
 #ifdef CONFIG_64BIT
-#ifdef CONFIG_COMPAT
 #define STACK_RND_MASK         (is_compat_task() ? \
                                 0x7ff >> (PAGE_SHIFT - 12) : \
                                 0x3ffff >> (PAGE_SHIFT - 12))
-#else
-#define STACK_RND_MASK         (0x3ffff >> (PAGE_SHIFT - 12))
-#endif
 #endif
 
 /*
arch/riscv/include/asm/pgtable.h +0 -6
index 1d472b31e0cfe..ea5b269be223a 100644
@@ -127,16 +127,10 @@
 #define VA_USER_SV48 (UL(1) << (VA_BITS_SV48 - 1))
 #define VA_USER_SV57 (UL(1) << (VA_BITS_SV57 - 1))
 
-#ifdef CONFIG_COMPAT
 #define MMAP_VA_BITS_64 ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
 #define MMAP_MIN_VA_BITS_64 (VA_BITS_SV39)
 #define MMAP_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_VA_BITS_64)
 #define MMAP_MIN_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_MIN_VA_BITS_64)
-#else
-#define MMAP_VA_BITS ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
-#define MMAP_MIN_VA_BITS (VA_BITS_SV39)
-#endif /* CONFIG_COMPAT */
-
 #else
 #include <asm/pgtable-32.h>
 #endif /* CONFIG_64BIT */
arch/riscv/include/asm/processor.h +1 -2
index f19f861cda549..ed32e53e55999 100644
@@ -22,7 +22,7 @@
 ({                                                             \
        unsigned long mmap_end;                                 \
        typeof(addr) _addr = (addr);                            \
-       if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
+       if ((_addr) == 0 || is_compat_task())                   \
                mmap_end = STACK_TOP_MAX;                       \
        else if ((_addr) >= VA_USER_SV57)                       \
                mmap_end = STACK_TOP_MAX;                       \
@@ -39,7 +39,7 @@
        typeof(addr) _addr = (addr);                            \
        typeof(base) _base = (base);                            \
        unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base);   \
-       if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \
Quoted 8 lines
Quoted 7 lines
arch/riscv/include/asm/processor.h +1 -1
+       if ((_addr) == 0 || is_compat_task())                   \
                mmap_base = (_base);                            \
        else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \
                mmap_base = VA_USER_SV57 - rnd_gap;             \
--

2.43.0

Thanks!
Leo

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

--
Best Regards
Guo Ren

Thanks Andy!

#7 lore raw

RFC PATCH 3/4 riscv: Introduce is_compat_thread() into compat.h

In reply to Leonardo Bras

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.

---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/kernel/ptrace.c      | 6 +++---
 2 files changed, 11 insertions(+), 3 deletions(-)
arch/riscv/include/asm/compat.h +8 -0
index 91517b51b8e27..da4b28cd01a95 100644
@@ -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;
arch/riscv/kernel/ptrace.c +3 -3
index 2afe460de16a6..f362832123616 100644
@@ -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;
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 3/4
#8 lore raw

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

Quoted 24 lines · Leonardo Bras

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.

---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/kernel/ptrace.c      | 6 +++---
 2 files changed, 11 insertions(+), 3 deletions(-)
arch/riscv/include/asm/compat.h +0 -0
index 91517b51b8e27..da4b28cd01a95 100644

arch/riscv/include/asm/compat.h static inline int is_compat_task(void) · line 26

Quoted 7 lines
arch/riscv/include/asm/compat.h +4 -0
@@ -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().

Quoted 30 lines
arch/riscv/include/asm/compat.h +4 -0
+
+       return test_ti_thread_flag(thread, TIF_32BIT);
+}
+
 struct compat_user_regs_struct {
        compat_ulong_t pc;
        compat_ulong_t ra;
arch/riscv/kernel/ptrace.c +3 -4
index 2afe460de16a6..f362832123616 100644
@@ -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;
 }
--
Quoted 2 lines

2.43.0

LGTM

Signature · 2 lines
Best Regards
 Guo Ren
#9 lore raw

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

Quoted 28 lines · Guo Ren
Quoted 27 lines · Leonardo Bras

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.

---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/kernel/ptrace.c      | 6 +++---
 2 files changed, 11 insertions(+), 3 deletions(-)
arch/riscv/include/asm/compat.h +0 -0
index 91517b51b8e27..da4b28cd01a95 100644
@@ -20,6 +20,14 @@ static inline int is_compat_task(void)
        return test_thread_flag(TIF_32BIT);
 }
 
Quoted 6 lines

arch/riscv/include/asm/compat.h static inline int is_compat_task(void) · line 26

Quoted 4 lines
arch/riscv/include/asm/compat.h +4 -0
+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. :)

Quoted 34 lines
Quoted 30 lines
arch/riscv/include/asm/compat.h +4 -0
+
+       return test_ti_thread_flag(thread, TIF_32BIT);
+}
+
 struct compat_user_regs_struct {
        compat_ulong_t pc;
        compat_ulong_t ra;
arch/riscv/kernel/ptrace.c +3 -4
index 2afe460de16a6..f362832123616 100644
@@ -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;
 }
--
Quoted 2 lines

2.43.0

LGTM

Quoted 1 line

Thanks!
Leo

Quoted 5 lines
Signature · 3 lines
-- 
Best Regards
 Guo Ren
#10 lore raw

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

In reply to Leonardo Bras
Quoted 25 lines · Leonardo Bras

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.

---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/kernel/ptrace.c      | 6 +++---
 2 files changed, 11 insertions(+), 3 deletions(-)
arch/riscv/include/asm/compat.h +0 -0
index 91517b51b8e27..da4b28cd01a95 100644
@@ -20,6 +20,14 @@ static inline int is_compat_task(void)
        return test_thread_flag(TIF_32BIT);

arch/riscv/include/asm/compat.h static inline int is_compat_task(void) · line 30

Quoted 10 lines
arch/riscv/include/asm/compat.h +8 -0
 }
 
+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

Quoted 28 lines
arch/riscv/include/asm/compat.h +0 -0
 struct compat_user_regs_struct {
        compat_ulong_t pc;
        compat_ulong_t ra;
arch/riscv/kernel/ptrace.c +3 -4
index 2afe460de16a6..f362832123616 100644
@@ -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

#11 lore raw

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

Quoted 29 lines · Andy Chiu
Quoted 27 lines · Leonardo Bras

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.

---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/kernel/ptrace.c      | 6 +++---
 2 files changed, 11 insertions(+), 3 deletions(-)
arch/riscv/include/asm/compat.h +0 -0
index 91517b51b8e27..da4b28cd01a95 100644
@@ -20,6 +20,14 @@ static inline int is_compat_task(void)
        return test_thread_flag(TIF_32BIT);
 }
 
Quoted 11 lines

arch/riscv/include/asm/compat.h static inline int is_compat_task(void) · line 30

Quoted 8 lines
arch/riscv/include/asm/compat.h +8 -0
+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

Quoted 44 lines

#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

Quoted 28 lines
arch/riscv/include/asm/compat.h +0 -0
 struct compat_user_regs_struct {
        compat_ulong_t pc;
        compat_ulong_t ra;
arch/riscv/kernel/ptrace.c +3 -4
index 2afe460de16a6..f362832123616 100644
@@ -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

Reviewed-by
#12 lore raw

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

Quoted 47 lines · Leonardo Bras Soares Passos
Quoted 29 lines · Andy Chiu
Quoted 27 lines · Leonardo Bras

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.

---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/kernel/ptrace.c      | 6 +++---
 2 files changed, 11 insertions(+), 3 deletions(-)
arch/riscv/include/asm/compat.h +0 -0
index 91517b51b8e27..da4b28cd01a95 100644
@@ -20,6 +20,14 @@ static inline int is_compat_task(void)
        return test_thread_flag(TIF_32BIT);
 }
 
Quoted 11 lines

arch/riscv/include/asm/compat.h static inline int is_compat_task(void) · line 30

Quoted 8 lines
arch/riscv/include/asm/compat.h +8 -0
+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.

Quoted 8 lines

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.

Thanks,
Andy

Quoted 47 lines
Quoted 46 lines

#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

Quoted 28 lines
arch/riscv/include/asm/compat.h +0 -0
 struct compat_user_regs_struct {
        compat_ulong_t pc;
        compat_ulong_t ra;
arch/riscv/kernel/ptrace.c +3 -4
index 2afe460de16a6..f362832123616 100644
@@ -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

#13 lore raw

RFC PATCH 4/4 riscv: Introduce set_compat_task() in asm/compat.h

In reply to Leonardo Bras

In order to have all task compat bit access directly in compat.h, introduce set_compat_task() to set/reset those when needed.

Also, since it's only used on an if/else scenario, simplify the macro using it.

---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/include/asm/elf.h    | 5 +----
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/riscv/include/asm/compat.h +8 -0
index da4b28cd01a95..aa103530a5c83 100644
@@ -28,6 +28,14 @@ static inline int is_compat_thread(struct thread_info *thread)
 	return test_ti_thread_flag(thread, TIF_32BIT);
 }
 
+static inline void set_compat_task(bool is_compat)
+{
+	if (is_compat)
+		set_thread_flag(TIF_32BIT);
+	else
+		clear_thread_flag(TIF_32BIT);
+}
+
 struct compat_user_regs_struct {
 	compat_ulong_t pc;
 	compat_ulong_t ra;
arch/riscv/include/asm/elf.h +1 -4
index 2e88257cafaea..c7aea7886d22a 100644
@@ -135,10 +135,7 @@ do {							\
 #ifdef CONFIG_COMPAT
 
 #define SET_PERSONALITY(ex)					\
-do {    if ((ex).e_ident[EI_CLASS] == ELFCLASS32)		\
-		set_thread_flag(TIF_32BIT);			\
-	else							\
-		clear_thread_flag(TIF_32BIT);			\
+do {	set_compat_task((ex).e_ident[EI_CLASS] == ELFCLASS32);	\
 	if (personality(current->personality) != PER_LINUX32)	\
 		set_personality(PER_LINUX |			\
 			(current->personality & (~PER_MASK)));	\
Signature · 1 line
2.43.0
Reviewed-by on v1 4/4
#14 lore raw

Re: [RFC PATCH 4/4] riscv: Introduce set_compat_task() in asm/compat.h

Quoted 49 lines · Leonardo Bras

In order to have all task compat bit access directly in compat.h, introduce set_compat_task() to set/reset those when needed.

Also, since it's only used on an if/else scenario, simplify the macro using it.

---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/include/asm/elf.h    | 5 +----
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/riscv/include/asm/compat.h +8 -0
index da4b28cd01a95..aa103530a5c83 100644
@@ -28,6 +28,14 @@ static inline int is_compat_thread(struct thread_info *thread)
        return test_ti_thread_flag(thread, TIF_32BIT);
 }
 
+static inline void set_compat_task(bool is_compat)
+{
+       if (is_compat)
+               set_thread_flag(TIF_32BIT);
+       else
+               clear_thread_flag(TIF_32BIT);
+}
+
 struct compat_user_regs_struct {
        compat_ulong_t pc;
        compat_ulong_t ra;
arch/riscv/include/asm/elf.h +1 -5
index 2e88257cafaea..c7aea7886d22a 100644
@@ -135,10 +135,7 @@ do {                                                       \
 #ifdef CONFIG_COMPAT
 
 #define SET_PERSONALITY(ex)                                    \
-do {    if ((ex).e_ident[EI_CLASS] == ELFCLASS32)              \
-               set_thread_flag(TIF_32BIT);                     \
-       else                                                    \
-               clear_thread_flag(TIF_32BIT);                   \
+do {   set_compat_task((ex).e_ident[EI_CLASS] == ELFCLASS32);  \
        if (personality(current->personality) != PER_LINUX32)   \
                set_personality(PER_LINUX |                     \
                        (current->personality & (~PER_MASK)));  \
--
Quoted 2 lines

2.43.0

LGTM

Signature · 2 lines
Best Regards
 Guo Ren
#15 lore raw

Re: [RFC PATCH 4/4] riscv: Introduce set_compat_task() in asm/compat.h

Quoted 54 lines · Guo Ren
Quoted 49 lines · Leonardo Bras

In order to have all task compat bit access directly in compat.h, introduce set_compat_task() to set/reset those when needed.

Also, since it's only used on an if/else scenario, simplify the macro using it.

---
 arch/riscv/include/asm/compat.h | 8 ++++++++
 arch/riscv/include/asm/elf.h    | 5 +----
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/riscv/include/asm/compat.h +8 -0
index da4b28cd01a95..aa103530a5c83 100644
@@ -28,6 +28,14 @@ static inline int is_compat_thread(struct thread_info *thread)
        return test_ti_thread_flag(thread, TIF_32BIT);
 }
 
+static inline void set_compat_task(bool is_compat)
+{
+       if (is_compat)
+               set_thread_flag(TIF_32BIT);
+       else
+               clear_thread_flag(TIF_32BIT);
+}
+
 struct compat_user_regs_struct {
        compat_ulong_t pc;
        compat_ulong_t ra;
arch/riscv/include/asm/elf.h +1 -5
index 2e88257cafaea..c7aea7886d22a 100644
@@ -135,10 +135,7 @@ do {                                                       \
 #ifdef CONFIG_COMPAT
 
 #define SET_PERSONALITY(ex)                                    \
-do {    if ((ex).e_ident[EI_CLASS] == ELFCLASS32)              \
-               set_thread_flag(TIF_32BIT);                     \
-       else                                                    \
-               clear_thread_flag(TIF_32BIT);                   \
+do {   set_compat_task((ex).e_ident[EI_CLASS] == ELFCLASS32);  \
        if (personality(current->personality) != PER_LINUX32)   \
                set_personality(PER_LINUX |                     \
                        (current->personality & (~PER_MASK)));  \
--
Quoted 2 lines

2.43.0

LGTM

Quoted 1 line

Thanks!
Leo

Quoted 5 lines
Signature · 3 lines
-- 
Best Regards
 Guo Ren