clean up address limit helpers
Subsystems (26)
Hi all,
in preparation for eventually phasing out direct use of set_fs(), this series removes the segment_eq() arch helper that is only used to implement or duplicate the uaccess_kernel() API, and then adds descriptive helpers to force the kernel address limit.
Diffstat:
arch/alpha/include/asm/uaccess.h | 2 +- arch/arc/include/asm/segment.h | 3 +-- arch/arm/include/asm/uaccess.h | 4 ++-- arch/arm64/include/asm/uaccess.h | 2 +- arch/arm64/kernel/sdei.c | 2 +- arch/csky/include/asm/segment.h | 2 +- arch/h8300/include/asm/segment.h | 2 +- arch/ia64/include/asm/uaccess.h | 2 +- arch/m68k/include/asm/segment.h | 2 +- arch/m68k/include/asm/tlbflush.h | 12 ++++++------ arch/microblaze/include/asm/uaccess.h | 2 +- arch/mips/include/asm/uaccess.h | 2 +- arch/mips/kernel/unaligned.c | 27 +++++++++++++-------------- arch/nds32/include/asm/uaccess.h | 2 +- arch/nds32/kernel/process.c | 2 +- arch/nds32/mm/alignment.c | 7 +++---- arch/nios2/include/asm/uaccess.h | 2 +- arch/openrisc/include/asm/uaccess.h | 2 +- arch/parisc/include/asm/uaccess.h | 2 +- arch/powerpc/include/asm/uaccess.h | 3 +-- arch/riscv/include/asm/uaccess.h | 6 +++--- arch/s390/include/asm/uaccess.h | 2 +- arch/sh/include/asm/segment.h | 3 +-- arch/sh/kernel/traps_32.c | 18 ++++++++---------- arch/sparc/include/asm/uaccess_32.h | 2 +- arch/sparc/include/asm/uaccess_64.h | 2 +- arch/x86/include/asm/uaccess.h | 2 +- arch/xtensa/include/asm/uaccess.h | 2 +- drivers/firmware/arm_sdei.c | 5 ++--- fs/exec.c | 7 ++++++- include/asm-generic/uaccess.h | 4 ++-- include/linux/syscalls.h | 2 +- include/linux/uaccess.h | 20 ++++++++++++++++++-- kernel/events/callchain.c | 5 ++--- kernel/events/core.c | 5 ++--- kernel/exit.c | 2 +- kernel/kthread.c | 5 ++--- kernel/stacktrace.c | 5 ++--- mm/maccess.c | 22 ++++++++++------------ 39 files changed, 105 insertions(+), 98 deletions(-)
PATCH 1/6 syscalls: use uaccess_kernel in addr_limit_user_check
Use the uaccess_kernel helper instead of duplicating it.
--- include/linux/syscalls.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/syscalls.h +1 -1
@@ -263,7 +263,7 @@ static inline void addr_limit_user_check(void)return ;#endif -if (CHECK_DATA_CORRUPTION (!segment_eq (get_fs (),USER_DS ), +if (CHECK_DATA_CORRUPTION (uaccess_kernel (),"Invalid address limit on user-mode return" ))force_sig (SIGKILL );
Signature · 1 line
2.26.2
PATCH 2/6 nds32: use uaccess_kernel in show_regs
In reply to Christoph HellwigUse the uaccess_kernel helper instead of duplicating it.
--- arch/nds32/kernel/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/nds32/kernel/process.c +1 -1
@@ -121,7 +121,7 @@ void show_regs(struct pt_regs *regs) regs->uregs[3], regs->uregs[2], regs->uregs[1], regs->uregs[0]);pr_info (" IRQs o%s Segment %s\n" ,interrupts_enabled (regs) ?"n" :"ff" , -segment_eq (get_fs (),KERNEL_DS )?"kernel" :"user" ); +uaccess_kernel () ?"kernel" :"user" ); }EXPORT_SYMBOL (show_regs);
Signature · 1 line
2.26.2
Re: [PATCH 2/6] nds32: use uaccess_kernel in show_regs
Christoph Hellwig <[email protected]> 於 2020年7月10日 週五 下午9:57寫道:
Quoted 13 lines
Use the
uaccess_kernelhelper instead of duplicating it.Signed-off-by Christoph Hellwig <[email protected]>--- arch/nds32/kernel/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/nds32/kernel/process.c +0
-0
arch/nds32/kernel/process.c void show_regs(struct pt_regs *regs) · line 127
Quoted 8 lines
arch/nds32/kernel/process.c +1
-1@@ -121,7 +121,7 @@ void show_regs(struct pt_regs *regs) regs->uregs[3], regs->uregs[2], regs->uregs[1], regs->uregs[0]);pr_info (" IRQs o%s Segment %s\n" ,interrupts_enabled (regs) ?"n" :"ff" , -segment_eq (get_fs (),KERNEL_DS )?"kernel" :"user" ); +uaccess_kernel () ?"kernel" :"user" ); }EXPORT_SYMBOL (show_regs);
Hi Christoph, Thank you.
PATCH 3/6 riscv: include <asm/pgtable.h> in <asm/uaccess.h>
In reply to Christoph HellwigTo ensure TASK_SIZE is defined for USER_DS.
--- arch/riscv/include/asm/uaccess.h | 2 ++ 1 file changed, 2 insertions(+)
arch/riscv/include/asm/uaccess.h +2 -0
@@ -8,6 +8,8 @@#ifndef _ASM_RISCV_UACCESS_H#define _ASM_RISCV_UACCESS_H +#include <asm/pgtable.h> /* for TASK_SIZE */ +/* * User space memory access functions */
Signature · 1 line
2.26.2
Re: [PATCH 3/6] riscv: include <asm/pgtable.h> in <asm/uaccess.h>
Quoted 12 lines · Christoph Hellwig
To ensure
TASK_SIZEis defined forUSER_DS.Signed-off-by Christoph Hellwig <[email protected]>--- arch/riscv/include/asm/uaccess.h | 2 ++ 1 file changed, 2 insertions(+)arch/riscv/include/asm/uaccess.h +0
-0
arch/riscv/include/asm/uaccess.h line 15
Quoted 8 lines
arch/riscv/include/asm/uaccess.h +2
-0@@ -8,6 +8,8 @@#ifndef _ASM_RISCV_UACCESS_H#define _ASM_RISCV_UACCESS_H +#include <asm/pgtable.h> /* for TASK_SIZE */ +/* * User space memory access functions */
Thanks!
PATCH 4/6 uaccess: remove segment_eq
In reply to Christoph Hellwigsegment_eq is only used to implement uaccess_kernel. Just open code uaccess_kernel in the arch uaccess headers and remove one layer of indirection.
--- arch/alpha/include/asm/uaccess.h | 2 +- arch/arc/include/asm/segment.h | 3 +-- arch/arm/include/asm/uaccess.h | 4 ++-- arch/arm64/include/asm/uaccess.h | 2 +- arch/csky/include/asm/segment.h | 2 +- arch/h8300/include/asm/segment.h | 2 +- arch/ia64/include/asm/uaccess.h | 2 +- arch/m68k/include/asm/segment.h | 2 +- arch/microblaze/include/asm/uaccess.h | 2 +- arch/mips/include/asm/uaccess.h | 2 +- arch/nds32/include/asm/uaccess.h | 2 +- arch/nios2/include/asm/uaccess.h | 2 +- arch/openrisc/include/asm/uaccess.h | 2 +- arch/parisc/include/asm/uaccess.h | 2 +- arch/powerpc/include/asm/uaccess.h | 3 +-- arch/riscv/include/asm/uaccess.h | 4 +--- arch/s390/include/asm/uaccess.h | 2 +- arch/sh/include/asm/segment.h | 3 +-- arch/sparc/include/asm/uaccess_32.h | 2 +- arch/sparc/include/asm/uaccess_64.h | 2 +- arch/x86/include/asm/uaccess.h | 2 +- arch/xtensa/include/asm/uaccess.h | 2 +- include/asm-generic/uaccess.h | 4 ++-- include/linux/uaccess.h | 2 -- 24 files changed, 25 insertions(+), 32 deletions(-)
arch/alpha/include/asm/uaccess.h +1 -1
@@ -20,7 +20,7 @@#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)/* * Is a address valid? This does a straightforward calculation rather
arch/arc/include/asm/segment.h +1 -2
@@ -14,8 +14,7 @@ typedef unsigned long mm_segment_t;#define KERNEL_DS MAKE_MM_SEG(0)#define USER_DS MAKE_MM_SEG(TASK_SIZE) - -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)#endif /* __ASSEMBLY__ */ #endif /* __ASMARC_SEGMENT_H */
arch/arm/include/asm/uaccess.h +2 -2
@@ -76,7 +76,7 @@ static inline void set_fs(mm_segment_t fs)modify_domain (DOMAIN_KERNEL , fs ?DOMAIN_CLIENT :DOMAIN_MANAGER ); } -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)/* We use 33-bit arithmetic here... */ #define __range_ok (addr, size) ({ \
@@ -263,7 +263,7 @@ extern int __put_user_8(void *, unsigned long long);*/ #define USER_DS KERNEL_DS -#define segment_eq (a, b) (1) +#define uaccess_kernel () (true)#define __addr_ok (addr) ((void)(addr), 1)#define __range_ok (addr, size) ((void)(addr), 0)#define get_fs () (KERNEL_DS)
arch/arm64/include/asm/uaccess.h +1 -1
@@ -49,7 +49,7 @@ static inline void set_fs(mm_segment_t fs) CONFIG_ARM64_UAO)); } -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)/* * Test whether a block of memory is a valid user space address.
arch/csky/include/asm/segment.h +1 -1
@@ -13,6 +13,6 @@ typedef struct {#define USER_DS ((mm_segment_t) { 0x80000000UL })#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#endif /* __ASM_CSKY_SEGMENT_H */
arch/h8300/include/asm/segment.h +1 -1
@@ -33,7 +33,7 @@ static inline mm_segment_t get_fs(void) return USER_DS; } -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#endif /* __ASSEMBLY__ */
arch/ia64/include/asm/uaccess.h +1 -1
@@ -50,7 +50,7 @@#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)/* * When accessing user memory, we need to make sure the entire area really is in
arch/m68k/include/asm/segment.h +1 -1
@@ -52,7 +52,7 @@ static inline void set_fs(mm_segment_t val)#define set_fs (x) (current_thread_info()->addr_limit = (x))#endif -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#endif /* __ASSEMBLY__ */
arch/microblaze/include/asm/uaccess.h +1 -1
@@ -41,7 +41,7 @@# define get_fs () (current_thread_info()->addr_limit)# define set_fs (val) (current_thread_info()->addr_limit = (val)) -# define segment_eq (a, b) ((a).seg == (b).seg) +# define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#ifndef CONFIG_MMU
arch/mips/include/asm/uaccess.h +1 -1
@@ -72,7 +72,7 @@ extern u64 __ua_limit;#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)/* * eva_kernel_access() - determine whether kernel memory access on an EVA system
arch/nds32/include/asm/uaccess.h +1 -1
@@ -44,7 +44,7 @@ static inline void set_fs(mm_segment_t fs)current_thread_info ()->addr_limit = fs; } -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)#define __range_ok (addr, size) (size <= get_fs() && addr <= (get_fs() -size))
arch/nios2/include/asm/uaccess.h +1 -1
@@ -30,7 +30,7 @@#define get_fs () (current_thread_info()->addr_limit)#define set_fs (seg) (current_thread_info()->addr_limit = (seg)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define __access_ok (addr, len) \ (((signed long)(((long)get_fs().seg) & \
arch/openrisc/include/asm/uaccess.h +1 -1
@@ -43,7 +43,7 @@#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x)) -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)/* Ensure that the range from addr to addr+size is all within the process' * address space
arch/parisc/include/asm/uaccess.h +1 -1
@@ -14,7 +14,7 @@#define KERNEL_DS ((mm_segment_t){0})#define USER_DS ((mm_segment_t){1}) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x))
arch/powerpc/include/asm/uaccess.h +1 -2
@@ -38,8 +38,7 @@ static inline void set_fs(mm_segment_t fs)set_thread_flag (TIF_FSCHECK ); } -#define segment_eq (a, b) ((a).seg == (b).seg) - +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define user_addr_max () (get_fs().seg)#ifdef __powerpc64__
arch/riscv/include/asm/uaccess.h +1 -3
@@ -64,11 +64,9 @@ static inline void set_fs(mm_segment_t fs)current_thread_info ()->addr_limit = fs; } -#define segment_eq (a, b) ((a).seg == (b).seg) - +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define user_addr_max () (get_fs().seg) -/** * access_ok: - Checks if a user space pointer is valid * @addr: User space pointer to start of block to check
arch/s390/include/asm/uaccess.h +1 -1
@@ -32,7 +32,7 @@#define USER_DS_SACF (3)#define get_fs () (current->thread.mm_segment) -#define segment_eq (a,b) (((a) & 2) == ((b) & 2)) +#define uaccess_kernel () ((get_fs() & 2) == KERNEL_DS)void set_fs (mm_segment_t fs);
arch/sh/include/asm/segment.h +1 -2
@@ -24,8 +24,7 @@ typedef struct {#define USER_DS KERNEL_DS#endif -#define segment_eq (a, b) ((a).seg == (b).seg) - +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define get_fs () (current_thread_info()->addr_limit)#define set_fs (x) (current_thread_info()->addr_limit = (x))
arch/sparc/include/asm/uaccess_32.h +1 -1
@@ -28,7 +28,7 @@#define get_fs () (current->thread.current_ds)#define set_fs (val) ((current->thread.current_ds) = (val)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)/* We have there a nice not-mapped page at PAGE_OFFSET - PAGE_SIZE, so that this test * can be fairly lightweight.
arch/sparc/include/asm/uaccess_64.h +1 -1
@@ -32,7 +32,7 @@#define get_fs () ((mm_segment_t){(current_thread_info()->current_ds)}) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define set_fs (val) \ do { \
arch/x86/include/asm/uaccess.h +1 -1
@@ -33,7 +33,7 @@ static inline void set_fs(mm_segment_t fs)set_thread_flag (TIF_FSCHECK ); } -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define user_addr_max () (current->thread.addr_limit.seg)/*
arch/xtensa/include/asm/uaccess.h +1 -1
@@ -35,7 +35,7 @@#define get_fs () (current->thread.current_ds)#define set_fs (val) (current->thread.current_ds = (val)) -#define segment_eq (a, b) ((a).seg == (b).seg) +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#define __kernel_ok (uaccess_kernel())#define __user_ok (addr, size) \
include/asm-generic/uaccess.h +2 -2
@@ -86,8 +86,8 @@ static inline void set_fs(mm_segment_t fs) }#endif -#ifndef segment_eq -#define segment_eq (a, b) ((a).seg == (b).seg) +#ifndef uaccess_kernel +#define uaccess_kernel () (get_fs().seg == KERNEL_DS.seg)#endif #define access_ok (addr, size) __access_ok((unsigned long)(addr),(size))
include/linux/uaccess.h +0 -2
@@ -6,8 +6,6 @@#include <linux/sched.h> #include <linux/thread_info.h> -#define uaccess_kernel () segment_eq(get_fs(), KERNEL_DS) -#include <asm/uaccess.h> /*
Signature · 1 line
2.26.2
Re: [PATCH 4/6] uaccess: remove segment_eq
Quoted 7 lines · Christoph Hellwig
segment_eqis only used to implementuaccess_kernel. Just open codeuaccess_kernelin the arch uaccess headers and remove one layer of indirection.Signed-off-by Christoph Hellwig <[email protected]>arch/m68k/include/asm/segment.h | 2 +-
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH 4/6] uaccess: remove segment_eq
In reply to Christoph HellwigChristoph Hellwig <[email protected]> 於 2020年7月10日 週五 下午9:57寫道:
Quoted 33 lines
segment_eqis only used to implementuaccess_kernel. Just open codeuaccess_kernelin the arch uaccess headers and remove one layer of indirection.Signed-off-by Christoph Hellwig <[email protected]>--- arch/alpha/include/asm/uaccess.h | 2 +- arch/arc/include/asm/segment.h | 3 +-- arch/arm/include/asm/uaccess.h | 4 ++-- arch/arm64/include/asm/uaccess.h | 2 +- arch/csky/include/asm/segment.h | 2 +- arch/h8300/include/asm/segment.h | 2 +- arch/ia64/include/asm/uaccess.h | 2 +- arch/m68k/include/asm/segment.h | 2 +- arch/microblaze/include/asm/uaccess.h | 2 +- arch/mips/include/asm/uaccess.h | 2 +- arch/nds32/include/asm/uaccess.h | 2 +- arch/nios2/include/asm/uaccess.h | 2 +- arch/openrisc/include/asm/uaccess.h | 2 +- arch/parisc/include/asm/uaccess.h | 2 +- arch/powerpc/include/asm/uaccess.h | 3 +-- arch/riscv/include/asm/uaccess.h | 4 +--- arch/s390/include/asm/uaccess.h | 2 +- arch/sh/include/asm/segment.h | 3 +-- arch/sparc/include/asm/uaccess_32.h | 2 +- arch/sparc/include/asm/uaccess_64.h | 2 +- arch/x86/include/asm/uaccess.h | 2 +- arch/xtensa/include/asm/uaccess.h | 2 +- include/asm-generic/uaccess.h | 4 ++-- include/linux/uaccess.h | 2 -- 24 files changed, 25 insertions(+), 32 deletions(-)
[...]
arch/nds32/include/asm/uaccess.h static inline void set_fs(mm_segment_t fs) · line 50
Quoted 13 lines
arch/nds32/include/asm/uaccess.h +1
-1@@ -44,7 +44,7 @@ static inline void set_fs(mm_segment_t fs)current_thread_info ()->addr_limit = fs; } -#define segment_eq (a, b) ((a) == (b)) +#define uaccess_kernel () (get_fs() == KERNEL_DS)#define __range_ok (addr, size) (size <= get_fs() && addr <= (get_fs() -size))
Hi Christoph, Thank you.
PATCH 5/6 uaccess: add force_uaccess_{begin,end} helpers
In reply to Christoph HellwigAdd helpers to wraper the get_fs/set_fs magic for undoing any damange done by set_fs(KERNEL_DS). There is no real functional benefit, but this documents the intent of these calls better, and will allow stubbing the functions out easily for kernels builds that do not allow address space overrides in the future.
--- arch/arm64/kernel/sdei.c | 2 +- arch/m68k/include/asm/tlbflush.h | 12 ++++++------ arch/mips/kernel/unaligned.c | 27 +++++++++++++-------------- arch/nds32/mm/alignment.c | 7 +++---- arch/sh/kernel/traps_32.c | 18 ++++++++---------- drivers/firmware/arm_sdei.c | 5 ++--- include/linux/uaccess.h | 18 ++++++++++++++++++ kernel/events/callchain.c | 5 ++--- kernel/events/core.c | 5 ++--- kernel/kthread.c | 5 ++--- kernel/stacktrace.c | 5 ++--- mm/maccess.c | 22 ++++++++++------------ 12 files changed, 69 insertions(+), 62 deletions(-)
arch/arm64/kernel/sdei.c +1 -1
@@ -180,7 +180,7 @@ static __kprobes unsigned long _sdei_handler(struct pt_regs *regs,/* * We didn't take an exception to get here, set PAN. UAO will be cleared -* by sdei_event_handler()s set_fs(USER_DS) call. +* by sdei_event_handler()s force_uaccess_begin() call. */ __uaccess_enable_hw_pan ();
arch/m68k/include/asm/tlbflush.h +6 -6
@@ -13,13 +13,13 @@ static inline void flush_tlb_kernel_page(void *addr)if (CPU_IS_COLDFIRE ) {mmu_write (MMUOR ,MMUOR_CNL ); }else if (CPU_IS_040_OR_060 ) { -mm_segment_t old_fs =get_fs (); -set_fs (KERNEL_DS ); +mm_segment_t old_fs =force_uaccess_begin (); + __asm__ __volatile__(".chip 68040\n\t" "pflush (%0)\n\t" ".chip 68k" : :"a" (addr)); -set_fs (old_fs); +force_uaccess_end (old_fs); }else if (CPU_IS_020_OR_030 ) __asm__ __volatile__("pflush #4,#4,(%0)" : :"a" (addr)); }
@@ -85,10 +85,10 @@ static inline void flush_tlb_mm(struct mm_struct *mm)static inline void flush_tlb_page (struct vm_area_struct *vma,unsigned long addr) {if (vma->vm_mm == current->active_mm) { -mm_segment_t old_fs =get_fs (); -set_fs (USER_DS ); +mm_segment_t old_fs =force_uaccess_begin (); +__flush_tlb_one (addr); -set_fs (old_fs); +force_uaccess_end (old_fs); } }
arch/mips/kernel/unaligned.c +13 -14
@@ -191,17 +191,16 @@ static void emulate_load_store_insn(struct pt_regs *regs,* memory, so we need to "switch" the address limit to * user space, so that address check can work properly. */ - seg =get_fs (); -set_fs (USER_DS ); + seg =force_uaccess_begin ();switch (insn.spec3_format.func) {case lhe_op:if (!access_ok (addr,2 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }LoadHWE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }compute_return_epc (regs);
@@ -209,12 +208,12 @@ static void emulate_load_store_insn(struct pt_regs *regs,break ;case lwe_op:if (!access_ok (addr,4 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }LoadWE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }compute_return_epc (regs);
@@ -222,12 +221,12 @@ static void emulate_load_store_insn(struct pt_regs *regs,break ;case lhue_op:if (!access_ok (addr,2 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }LoadHWUE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }compute_return_epc (regs);
@@ -235,35 +234,35 @@ static void emulate_load_store_insn(struct pt_regs *regs,break ;case she_op:if (!access_ok (addr,2 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }compute_return_epc (regs); value = regs->regs[insn.spec3_format.rt];StoreHWE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }break ;case swe_op:if (!access_ok (addr,4 )) { -set_fs (seg); +force_uaccess_end (seg); goto sigbus; }compute_return_epc (regs); value = regs->regs[insn.spec3_format.rt];StoreWE (addr, value, res);if (res) { -set_fs (seg); +force_uaccess_end (seg); goto fault; }break ;default : -set_fs (seg); +force_uaccess_end (seg); goto sigill; } -set_fs (seg); +force_uaccess_end (seg); }#endif break ;
arch/nds32/mm/alignment.c +3 -4
@@ -512,7 +512,7 @@ int do_unaligned_access(unsigned long addr, struct pt_regs *regs) {unsigned long inst;int ret = -EFAULT ; -mm_segment_t seg =get_fs (); +mm_segment_t seg; inst =get_inst (regs->ipc);
@@ -520,13 +520,12 @@ int do_unaligned_access(unsigned long addr, struct pt_regs *regs)"Faulting addr: 0x%08lx, pc: 0x%08lx [inst: 0x%08lx ]\n" , addr, regs->ipc, inst); -set_fs (USER_DS ); - + seg =force_uaccess_begin ();if (inst &NDS32_16BIT_INSTRUCTION ) ret =do_16 ((inst >>16 ) &0xffff , regs);else ret =do_32 (inst, regs); -set_fs (seg); +force_uaccess_end (seg);return ret; }
arch/sh/kernel/traps_32.c +8 -10
@@ -482,8 +482,6 @@ asmlinkage void do_address_error(struct pt_regs *regs, error_code =lookup_exception_vector ();#endif - oldfs =get_fs (); -if (user_mode (regs)) {int si_code =BUS_ADRERR ;unsigned int user_action;
@@ -491,13 +489,13 @@ asmlinkage void do_address_error(struct pt_regs *regs,local_irq_enable ();inc_unaligned_user_access (); -set_fs (USER_DS ); + oldfs =force_uaccess_begin ();if (copy_from_user (&instruction, (insn_size_t *)(regs->pc & ~1 ),sizeof (instruction))) { -set_fs (oldfs); +force_uaccess_end (oldfs); goto uspace_segv; } -set_fs (oldfs); +force_uaccess_end (oldfs);/* shout about userspace fixups */ unaligned_fixups_notify (current, instruction, regs);
@@ -520,11 +518,11 @@ asmlinkage void do_address_error(struct pt_regs *regs, goto uspace_segv; } -set_fs (USER_DS ); + oldfs =force_uaccess_begin (); tmp =handle_unaligned_access (instruction, regs, &user_mem_access,0 , address); -set_fs (oldfs); +force_uaccess_end (oldfs);if (tmp ==0 )return ;/* sorted */
@@ -540,13 +538,13 @@ asmlinkage void do_address_error(struct pt_regs *regs,if (regs->pc &1 )die ("unaligned program counter" , regs, error_code); -set_fs (KERNEL_DS ); + oldfs =force_uaccess_begin ();if (copy_from_user (&instruction, (void __user *)(regs->pc),sizeof (instruction))) {/* Argh. Fault on the instruction itself. This should never happen non-SMP */ -set_fs (oldfs); +force_uaccess_end (oldfs);die ("insn faulting in do_address_error" , regs,0 ); }
@@ -554,7 +552,7 @@ asmlinkage void do_address_error(struct pt_regs *regs,handle_unaligned_access (instruction, regs, &user_mem_access,0 , address); -set_fs (oldfs); +force_uaccess_end (oldfs); } }
drivers/firmware/arm_sdei.c +2 -3
@@ -1136,15 +1136,14 @@ int sdei_event_handler(struct pt_regs *regs,* access kernel memory. * Do the same here because this doesn't come via the same entry code. */ - orig_addr_limit =get_fs (); -set_fs (USER_DS ); + orig_addr_limit =force_uaccess_begin (); err = arg->callback (event_num, regs, arg->callback_arg);if (err)pr_err_ratelimited ("event %u on CPU %u failed with error: %d\n" , event_num,smp_processor_id (), err); -set_fs (orig_addr_limit); +force_uaccess_end (orig_addr_limit);return err; }
include/linux/uaccess.h +18 -0
@@ -8,6 +8,24 @@#include <asm/uaccess.h> +/* +* Force the uaccess routines to be wired up for actual userspace access, +* overriding any possible set_fs(KERNEL_DS) still lingering around. Undone +* using force_uaccess_end below. +*/ +static inline mm_segment_t force_uaccess_begin (void ) +{ +mm_segment_t fs =get_fs (); + +set_fs (USER_DS ); +return fs; +} + +static inline void force_uaccess_end (mm_segment_t oldfs) +{ +set_fs (oldfs); +} +/* * Architectures should provide two primitives (raw_copy_{to,from}_user()) * and get rid of their private instances of copy_{to,from}_user() and
kernel/events/callchain.c +2 -3
@@ -218,10 +218,9 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,if (add_mark)perf_callchain_store_context (&ctx,PERF_CONTEXT_USER ); - fs =get_fs (); -set_fs (USER_DS ); + fs =force_uaccess_begin ();perf_callchain_user (&ctx, regs); -set_fs (fs); +force_uaccess_end (fs); } }
kernel/events/core.c +2 -3
@@ -6436,10 +6436,9 @@ perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,/* Data. */ sp =perf_user_stack_pointer (regs); - fs =get_fs (); -set_fs (USER_DS ); + fs =force_uaccess_begin (); rem =__output_copy_user (handle, (void *) sp, dump_size); -set_fs (fs); +force_uaccess_end (fs); dyn_size = dump_size - rem;perf_output_skip (handle, rem);
kernel/kthread.c +2 -3
@@ -1254,8 +1254,7 @@ void kthread_use_mm(struct mm_struct *mm)if (active_mm != mm)mmdrop (active_mm); -to_kthread (tsk)->oldfs =get_fs (); -set_fs (USER_DS ); +to_kthread (tsk)->oldfs =force_uaccess_begin (); }EXPORT_SYMBOL_GPL (kthread_use_mm);
@@ -1270,7 +1269,7 @@ void kthread_unuse_mm(struct mm_struct *mm)WARN_ON_ONCE (!(tsk->flags &PF_KTHREAD ));WARN_ON_ONCE (!tsk->mm); -set_fs (to_kthread (tsk)->oldfs); +force_uaccess_end (to_kthread (tsk)->oldfs);task_lock (tsk);sync_mm_rss (mm);
kernel/stacktrace.c +2 -3
@@ -233,10 +233,9 @@ unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)if (current->flags &PF_KTHREAD )return 0 ; - fs =get_fs (); -set_fs (USER_DS ); + fs =force_uaccess_begin ();arch_stack_walk_user (consume_entry, &c,task_pt_regs (current)); -set_fs (fs); +force_uaccess_end (fs);return c.len; }
mm/maccess.c +10 -12
@@ -205,15 +205,14 @@ long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)long copy_from_user_nofault (void *dst,const void __user *src,size_t size) {long ret = -EFAULT ; -mm_segment_t old_fs =get_fs (); +mm_segment_t old_fs =force_uaccess_begin (); -set_fs (USER_DS );if (access_ok (src, size)) {pagefault_disable (); ret =__copy_from_user_inatomic (dst, src, size);pagefault_enable (); } -set_fs (old_fs); +force_uaccess_end (old_fs);if (ret)return -EFAULT ;
@@ -233,15 +232,14 @@ EXPORT_SYMBOL_GPL(copy_from_user_nofault);long copy_to_user_nofault (void __user *dst,const void *src,size_t size) {long ret = -EFAULT ; -mm_segment_t old_fs =get_fs (); +mm_segment_t old_fs =force_uaccess_begin (); -set_fs (USER_DS );if (access_ok (dst, size)) {pagefault_disable (); ret =__copy_to_user_inatomic (dst, src, size);pagefault_enable (); } -set_fs (old_fs); +force_uaccess_end (old_fs);if (ret)return -EFAULT ;
@@ -270,17 +268,17 @@ EXPORT_SYMBOL_GPL(copy_to_user_nofault);long strncpy_from_user_nofault (char *dst,const void __user *unsafe_addr,long count) { -mm_segment_t old_fs =get_fs (); +mm_segment_t old_fs;long ret;if (unlikely (count <=0 ))return 0 ; -set_fs (USER_DS ); + old_fs =force_uaccess_begin ();pagefault_disable (); ret =strncpy_from_user (dst, unsafe_addr, count);pagefault_enable (); -set_fs (old_fs); +force_uaccess_end (old_fs);if (ret >= count) { ret = count;
@@ -310,14 +308,14 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,*/ long strnlen_user_nofault (const void __user *unsafe_addr,long count) { -mm_segment_t old_fs =get_fs (); +mm_segment_t old_fs;int ret; -set_fs (USER_DS ); + old_fs =force_uaccess_begin ();pagefault_disable (); ret =strnlen_user (unsafe_addr, count);pagefault_enable (); -set_fs (old_fs); +force_uaccess_end (old_fs);return ret; }
Signature · 1 line
2.26.2
Re: [PATCH 5/6] uaccess: add force_uaccess_{begin,end} helpers
Quoted 1 line · Christoph Hellwig
Add helpers to wraper the get_fs/set_fs magic for undoing any damage
to wrap
Quoted 8 lines
done by
set_fs(KERNEL_DS). There is no real functional benefit, but this documents the intent of these calls better, and will allow stubbing the functions out easily for kernels builds that do not allow address space overrides in the future.Signed-off-by Christoph Hellwig <[email protected]>arch/m68k/include/asm/tlbflush.h | 12 ++++++------
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH 5/6] uaccess: add force_uaccess_{begin,end} helpers
In reply to Christoph HellwigChristoph Hellwig <[email protected]> 於 2020年7月10日 週五 下午9:57寫道:
Quoted 23 lines
Add helpers to wraper the get_fs/set_fs magic for undoing any damange done by
set_fs(KERNEL_DS). There is no real functional benefit, but this documents the intent of these calls better, and will allow stubbing the functions out easily for kernels builds that do not allow address space overrides in the future.Signed-off-by Christoph Hellwig <[email protected]>--- arch/arm64/kernel/sdei.c | 2 +- arch/m68k/include/asm/tlbflush.h | 12 ++++++------ arch/mips/kernel/unaligned.c | 27 +++++++++++++-------------- arch/nds32/mm/alignment.c | 7 +++---- arch/sh/kernel/traps_32.c | 18 ++++++++---------- drivers/firmware/arm_sdei.c | 5 ++--- include/linux/uaccess.h | 18 ++++++++++++++++++ kernel/events/callchain.c | 5 ++--- kernel/events/core.c | 5 ++--- kernel/kthread.c | 5 ++--- kernel/stacktrace.c | 5 ++--- mm/maccess.c | 22 ++++++++++------------ 12 files changed, 69 insertions(+), 62 deletions(-)
[...]
Quoted 19 lines
arch/nds32/mm/alignment.c +1
-3@@ -512,7 +512,7 @@ int do_unaligned_access(unsigned long addr, struct pt_regs *regs) {unsigned long inst;int ret = -EFAULT ; -mm_segment_t seg =get_fs (); +mm_segment_t seg; inst =get_inst (regs->ipc);@@ -520,13 +520,12 @@ int do_unaligned_access(unsigned long addr, struct pt_regs *regs)"Faulting addr: 0x%08lx, pc: 0x%08lx [inst: 0x%08lx ]\n" , addr, regs->ipc, inst); -set_fs (USER_DS ); -
arch/nds32/mm/alignment.c int do_unaligned_access(unsigned long addr, struct pt_regs *regs) · line 531
Quoted 10 lines
arch/nds32/mm/alignment.c +2
-1+ seg =force_uaccess_begin ();if (inst &NDS32_16BIT_INSTRUCTION ) ret =do_16 ((inst >>16 ) &0xffff , regs);else ret =do_32 (inst, regs); -set_fs (seg); +force_uaccess_end (seg);return ret; }
Hi Christoph, Thank you.
Mark Rutland
reviewerRe: [PATCH 5/6] uaccess: add force_uaccess_{begin,end} helpers
In reply to Christoph HellwigQuoted 11 lines · Christoph Hellwig
Add helpers to wraper the get_fs/set_fs magic for undoing any damange done by
set_fs(KERNEL_DS). There is no real functional benefit, but this documents the intent of these calls better, and will allow stubbing the functions out easily for kernels builds that do not allow address space overrides in the future.arch/m68k/include/asm/tlbflush.h +0
-0
arch/m68k/include/asm/tlbflush.h static inline void flush_tlb_kernel_page(void *addr) · line 17
Quoted 7 lines
arch/m68k/include/asm/tlbflush.h +2
-2@@ -13,13 +13,13 @@ static inline void flush_tlb_kernel_page(void *addr)if (CPU_IS_COLDFIRE ) {mmu_write (MMUOR ,MMUOR_CNL ); }else if (CPU_IS_040_OR_060 ) { -mm_segment_t old_fs =get_fs (); -set_fs (KERNEL_DS ); +mm_segment_t old_fs =force_uaccess_begin (); +
This used to set KERNEL_DS, and now it sets USER_DS, which looks wrong superficially.
If the new behaviour is fine it suggests that the old behaviour was wrong, or that this is superfluous and could go entirely.
Geert?
Mark.
Quoted 26 lines
arch/m68k/include/asm/tlbflush.h +18
-1__asm__ __volatile__(".chip 68040\n\t" "pflush (%0)\n\t" ".chip 68k" : :"a" (addr)); -set_fs (old_fs); +force_uaccess_end (old_fs); }else if (CPU_IS_020_OR_030 ) __asm__ __volatile__("pflush #4,#4,(%0)" : :"a" (addr)); +/* +* Force the uaccess routines to be wired up for actual userspace access, +* overriding any possible set_fs(KERNEL_DS) still lingering around. Undone +* using force_uaccess_end below. +*/ +static inline mm_segment_t force_uaccess_begin (void ) +{ +mm_segment_t fs =get_fs (); + +set_fs (USER_DS ); +return fs; +} + +static inline void force_uaccess_end (mm_segment_t oldfs) +{ +set_fs (oldfs); +}
Geert Uytterhoeven
maintainerRe: [PATCH 5/6] uaccess: add force_uaccess_{begin,end} helpers
Hi Mark,
Quoted 7 lines · Mark Rutland
Quoted 5 lines · Christoph Hellwig
Add helpers to wraper the get_fs/set_fs magic for undoing any damange done by
set_fs(KERNEL_DS). There is no real functional benefit, but this documents the intent of these calls better, and will allow stubbing the functions out easily for kernels builds that do not allow address space overrides in the future.
Quoted 15 lines
arch/m68k/include/asm/tlbflush.h static inline void flush_tlb_kernel_page(void *addr) · line 17
Quoted 12 lines
arch/m68k/include/asm/tlbflush.h +2
-2@@ -13,13 +13,13 @@ static inline void flush_tlb_kernel_page(void *addr)if (CPU_IS_COLDFIRE ) {mmu_write (MMUOR ,MMUOR_CNL ); }else if (CPU_IS_040_OR_060 ) { -mm_segment_t old_fs =get_fs (); -set_fs (KERNEL_DS ); +mm_segment_t old_fs =force_uaccess_begin (); +This used to set
KERNEL_DS, and now it setsUSER_DS, which looks wrong superficially.
Thanks for noticing, and sorry for missing that myself.
The same issue is present for SuperH:
- set_fs(KERNEL_DS);
+ oldfs = force_uaccess_begin();
So the patch description should be:
"Add helpers to wraper the get_fs/set_fs magic for undoing any damage
done by set_fs(USER_DS)."
and leave alone users setting KERNEL_DS?
Quoted 4 lines
If the new behaviour is fine it suggests that the old behaviour was wrong, or that this is superfluous and could go entirely.
Geert?
Nope, on m68k, TLB cache operations operate on the current address space. Hence to flush a kernel TLB entry, you have to switch to KERNEL_DS first.
If we're guaranteed to be already using KERNEL_DS, I guess the address space handling can be removed. But can we be sure?
Quoted 18 lines
Quoted 18 lines
arch/m68k/include/asm/tlbflush.h +10
-1__asm__ __volatile__(".chip 68040\n\t" "pflush (%0)\n\t" ".chip 68k" : :"a" (addr)); -set_fs (old_fs); +force_uaccess_end (old_fs); }else if (CPU_IS_020_OR_030 ) __asm__ __volatile__("pflush #4,#4,(%0)" : :"a" (addr)); +/* +* Force the uaccess routines to be wired up for actual userspace access, +* overriding any possible set_fs(KERNEL_DS) still lingering around. Undone +* using force_uaccess_end below. +*/ +static inline mm_segment_t force_uaccess_begin (void ) +{ +mm_segment_t fs =get_fs (); +
Quoted 8 lines
arch/m68k/include/asm/tlbflush.h
Quoted 8 lines
arch/m68k/include/asm/tlbflush.h +8
-0+set_fs (USER_DS ); +return fs; +} + +static inline void force_uaccess_end (mm_segment_t oldfs) +{ +set_fs (oldfs); +}
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Re: [PATCH 5/6] uaccess: add force_uaccess_{begin,end} helpers
Quoted 10 lines · Geert Uytterhoeven
Quoted 2 lines
This used to set
KERNEL_DS, and now it setsUSER_DS, which looks wrong superficially.Thanks for noticing, and sorry for missing that myself.
The same issue is present for SuperH:
-
set_fs(KERNEL_DS);
+ oldfs =force_uaccess_begin();
Quoted 6 lines
So the patch description should be:
"Add helpers to wraper the get_fs/set_fs magic for undoing any damage
done byset_fs(USER_DS)."and leave alone users setting
KERNEL_DS?
Yes, this was broken. Fixed for the next version.
Quoted 10 lines
Quoted 4 lines
If the new behaviour is fine it suggests that the old behaviour was wrong, or that this is superfluous and could go entirely.
Geert?
Nope, on m68k, TLB cache operations operate on the current address space. Hence to flush a kernel TLB entry, you have to switch to
KERNEL_DSfirst.If we're guaranteed to be already using
KERNEL_DS, I guess the address space handling can be removed. But can we be sure?
We can't be sure yet. But with a lot of my pending work we should be able to get there in the not too far future.
Re: [PATCH 5/6] uaccess: add force_uaccess_{begin,end} helpers
In reply to Christoph HellwigQuoted 7 lines · Christoph Hellwig
Add helpers to wraper the get_fs/set_fs magic for undoing any damange done by
set_fs(KERNEL_DS). There is no real functional benefit, but this documents the intent of these calls better, and will allow stubbing the functions out easily for kernels builds that do not allow address space overrides in the future.Signed-off-by Christoph Hellwig <[email protected]>
Quoted 14 lines
--- arch/arm64/kernel/sdei.c | 2 +- arch/m68k/include/asm/tlbflush.h | 12 ++++++------ arch/mips/kernel/unaligned.c | 27 +++++++++++++-------------- arch/nds32/mm/alignment.c | 7 +++---- arch/sh/kernel/traps_32.c | 18 ++++++++---------- drivers/firmware/arm_sdei.c | 5 ++--- include/linux/uaccess.h | 18 ++++++++++++++++++ kernel/events/callchain.c | 5 ++--- kernel/events/core.c | 5 ++--- kernel/kthread.c | 5 ++--- kernel/stacktrace.c | 5 ++--- mm/maccess.c | 22 ++++++++++------------ 12 files changed, 69 insertions(+), 62 deletions(-)
The perf core and arm64/sdei bits look sound. FWIW:
Mark.
PATCH 6/6 exec: use force_uaccess_begin during exec and exit
In reply to Christoph HellwigBoth exec and exit want to ensure that the uaccess routines actually do access user pointers. Use the newly added force_uaccess_begin helper instead of an open coded set_fs for that to prepare for kernel builds where set_fs() does not exist.
--- fs/exec.c | 7 ++++++- kernel/exit.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-)
fs/exec.c +6 -1
@@ -1380,7 +1380,12 @@ int begin_new_exec(struct linux_binprm * bprm)if (retval) goto out_unlock; -set_fs (USER_DS ); +/* +* Ensure that the uaccess routines can actually operate on userspace +* pointers: +*/ +force_uaccess_begin (); + me->flags &= ~(PF_RANDOMIZE |PF_FORKNOEXEC |PF_KTHREAD |PF_NOFREEZE |PF_NO_SETAFFINITY );flush_thread ();
kernel/exit.c +1 -1
@@ -731,7 +731,7 @@ void __noreturn do_exit(long code)* mm_release()->clear_child_tid() from writing to a user-controlled * kernel address. */ -set_fs (USER_DS ); +force_uaccess_begin ();if (unlikely (in_atomic ())) { pr_info("note: %s[%d] exited with preempt_count %d\n",
Signature · 1 line
2.26.2
Quoted 5 lines · Christoph Hellwig
in preparation for eventually phasing out direct use of
set_fs(), this series removes thesegment_eq()arch helper that is only used to implement or duplicate theuaccess_kernel()API, and then adds descriptive helpers to force the kernel address limit.
Ack. All the patches looked like no-ops to me, but with better naming and clarity.
Linus
Quoted 9 lines · Linus Torvalds
Quoted 5 lines · Christoph Hellwig
in preparation for eventually phasing out direct use of
set_fs(), this series removes thesegment_eq()arch helper that is only used to implement or duplicate theuaccess_kernel()API, and then adds descriptive helpers to force the kernel address limit.Ack. All the patches looked like no-ops to me, but with better naming and clarity.
Is that a formal Acked-by?
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines · Linus Torvalds
Ack. All the patches looked like no-ops to me, but with better naming and clarity.
Is that a formal Acked-by?
Yup, the patch series looks fine to me, and it looks like you fixed up the things people noticed about off m68k behavior.
Linus