Skip to content

Commit 728e7ea

Browse files
Merge patch series "riscv: Introduce compat-mode helpers & improve arch_get_mmap_end()"
Leonardo Bras <leobras@redhat.com> says: 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. arch_get_mmap_end() just got a simple improvement and some extra docs. * b4-shazam-merge: riscv: Introduce set_compat_task() in asm/compat.h riscv: Introduce is_compat_thread() into compat.h riscv: add compile-time test into is_compat_task() riscv: Replace direct thread flag check with is_compat_task() riscv: Improve arch_get_mmap_end() macro Link: https://lore.kernel.org/r/20240103160024.70305-2-leobras@redhat.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2 parents 700c2d9 + 2a8986f commit 728e7ea

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

arch/riscv/include/asm/compat.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,28 @@
1414

1515
static inline int is_compat_task(void)
1616
{
17+
if (!IS_ENABLED(CONFIG_COMPAT))
18+
return 0;
19+
1720
return test_thread_flag(TIF_32BIT);
1821
}
1922

23+
static inline int is_compat_thread(struct thread_info *thread)
24+
{
25+
if (!IS_ENABLED(CONFIG_COMPAT))
26+
return 0;
27+
28+
return test_ti_thread_flag(thread, TIF_32BIT);
29+
}
30+
31+
static inline void set_compat_task(bool is_compat)
32+
{
33+
if (is_compat)
34+
set_thread_flag(TIF_32BIT);
35+
else
36+
clear_thread_flag(TIF_32BIT);
37+
}
38+
2039
struct compat_user_regs_struct {
2140
compat_ulong_t pc;
2241
compat_ulong_t ra;

arch/riscv/include/asm/elf.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,9 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr);
5353
#define ELF_ET_DYN_BASE ((DEFAULT_MAP_WINDOW / 3) * 2)
5454

5555
#ifdef CONFIG_64BIT
56-
#ifdef CONFIG_COMPAT
57-
#define STACK_RND_MASK (test_thread_flag(TIF_32BIT) ? \
56+
#define STACK_RND_MASK (is_compat_task() ? \
5857
0x7ff >> (PAGE_SHIFT - 12) : \
5958
0x3ffff >> (PAGE_SHIFT - 12))
60-
#else
61-
#define STACK_RND_MASK (0x3ffff >> (PAGE_SHIFT - 12))
62-
#endif
6359
#endif
6460

6561
/*
@@ -139,10 +135,7 @@ do { \
139135
#ifdef CONFIG_COMPAT
140136

141137
#define SET_PERSONALITY(ex) \
142-
do { if ((ex).e_ident[EI_CLASS] == ELFCLASS32) \
143-
set_thread_flag(TIF_32BIT); \
144-
else \
145-
clear_thread_flag(TIF_32BIT); \
138+
do { set_compat_task((ex).e_ident[EI_CLASS] == ELFCLASS32); \
146139
if (personality(current->personality) != PER_LINUX32) \
147140
set_personality(PER_LINUX | \
148141
(current->personality & (~PER_MASK))); \

arch/riscv/include/asm/pgtable.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,10 @@
127127
#define VA_USER_SV48 (UL(1) << (VA_BITS_SV48 - 1))
128128
#define VA_USER_SV57 (UL(1) << (VA_BITS_SV57 - 1))
129129

130-
#ifdef CONFIG_COMPAT
131130
#define MMAP_VA_BITS_64 ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
132131
#define MMAP_MIN_VA_BITS_64 (VA_BITS_SV39)
133132
#define MMAP_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_VA_BITS_64)
134133
#define MMAP_MIN_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_MIN_VA_BITS_64)
135-
#else
136-
#define MMAP_VA_BITS ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS)
137-
#define MMAP_MIN_VA_BITS (VA_BITS_SV39)
138-
#endif /* CONFIG_COMPAT */
139-
140134
#else
141135
#include <asm/pgtable-32.h>
142136
#endif /* CONFIG_64BIT */
@@ -877,8 +871,8 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)
877871
#define TASK_SIZE_MIN (PGDIR_SIZE_L3 * PTRS_PER_PGD / 2)
878872

879873
#ifdef CONFIG_COMPAT
880-
#define TASK_SIZE_32 (_AC(0x80000000, UL))
881-
#define TASK_SIZE (test_thread_flag(TIF_32BIT) ? \
874+
#define TASK_SIZE_32 (_AC(0x80000000, UL) - PAGE_SIZE)
875+
#define TASK_SIZE (is_compat_task() ? \
882876
TASK_SIZE_32 : TASK_SIZE_64)
883877
#else
884878
#define TASK_SIZE TASK_SIZE_64

arch/riscv/include/asm/processor.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414

1515
#include <asm/ptrace.h>
1616

17+
/*
18+
* addr is a hint to the maximum userspace address that mmap should provide, so
19+
* this macro needs to return the largest address space available so that
20+
* mmap_end < addr, being mmap_end the top of that address space.
21+
* See Documentation/arch/riscv/vm-layout.rst for more details.
22+
*/
1723
#define arch_get_mmap_end(addr, len, flags) \
1824
({ \
1925
unsigned long mmap_end; \
2026
typeof(addr) _addr = (addr); \
21-
if ((_addr) == 0 || \
22-
(IS_ENABLED(CONFIG_COMPAT) && is_compat_task()) || \
27+
if ((_addr) == 0 || is_compat_task() || \
2328
((_addr + len) > BIT(VA_BITS - 1))) \
2429
mmap_end = STACK_TOP_MAX; \
2530
else \
@@ -33,8 +38,7 @@
3338
typeof(addr) _addr = (addr); \
3439
typeof(base) _base = (base); \
3540
unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base); \
36-
if ((_addr) == 0 || \
37-
(IS_ENABLED(CONFIG_COMPAT) && is_compat_task()) || \
41+
if ((_addr) == 0 || is_compat_task() || \
3842
((_addr + len) > BIT(VA_BITS - 1))) \
3943
mmap_base = (_base); \
4044
else \

arch/riscv/kernel/ptrace.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,14 @@ long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
377377

378378
return ret;
379379
}
380+
#else
381+
static const struct user_regset_view compat_riscv_user_native_view = {};
380382
#endif /* CONFIG_COMPAT */
381383

382384
const struct user_regset_view *task_user_regset_view(struct task_struct *task)
383385
{
384-
#ifdef CONFIG_COMPAT
385-
if (test_tsk_thread_flag(task, TIF_32BIT))
386+
if (is_compat_thread(&task->thread_info))
386387
return &compat_riscv_user_native_view;
387388
else
388-
#endif
389389
return &riscv_user_native_view;
390390
}

0 commit comments

Comments
 (0)