Merge tag 'x86-urgent-2020-05-31' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull x86 fixes from Thomas Gleixner:
"A pile of x86 fixes:

- Prevent a memory leak in ioperm which was caused by the stupid
assumption that the exit cleanup is always called for current,
which is not the case when fork fails after taking a reference on
the ioperm bitmap.

- Fix an arithmething overflow in the DMA code on 32bit systems

- Fill gaps in the xstate copy with defaults instead of leaving them
uninitialized

- Revert: "Make __X32_SYSCALL_BIT be unsigned long" as it turned out
that existing user space fails to build"

* tag 'x86-urgent-2020-05-31' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
x86/ioperm: Prevent a memory leak when fork fails
x86/dma: Fix max PFN arithmetic overflow on 32 bit systems
copy_xstate_to_kernel(): don't leave parts of destination uninitialized
x86/syscalls: Revert "x86/syscalls: Make __X32_SYSCALL_BIT be unsigned long"

Changed files
+74 -57
arch
x86
include
asm
uapi
asm
kernel
tools
arch
x86
include
uapi
asm
+1 -1
arch/x86/include/asm/dma.h
··· 74 74 #define MAX_DMA_PFN ((16UL * 1024 * 1024) >> PAGE_SHIFT) 75 75 76 76 /* 4GB broken PCI/AGP hardware bus master zone */ 77 - #define MAX_DMA32_PFN ((4UL * 1024 * 1024 * 1024) >> PAGE_SHIFT) 77 + #define MAX_DMA32_PFN (1UL << (32 - PAGE_SHIFT)) 78 78 79 79 #ifdef CONFIG_X86_32 80 80 /* The maximum address that we can perform a DMA transfer to on this platform */
+2 -2
arch/x86/include/asm/io_bitmap.h
··· 17 17 18 18 #ifdef CONFIG_X86_IOPL_IOPERM 19 19 void io_bitmap_share(struct task_struct *tsk); 20 - void io_bitmap_exit(void); 20 + void io_bitmap_exit(struct task_struct *tsk); 21 21 22 22 void native_tss_update_io_bitmap(void); 23 23 ··· 29 29 30 30 #else 31 31 static inline void io_bitmap_share(struct task_struct *tsk) { } 32 - static inline void io_bitmap_exit(void) { } 32 + static inline void io_bitmap_exit(struct task_struct *tsk) { } 33 33 static inline void tss_update_io_bitmap(void) { } 34 34 #endif 35 35
+9 -2
arch/x86/include/uapi/asm/unistd.h
··· 2 2 #ifndef _UAPI_ASM_X86_UNISTD_H 3 3 #define _UAPI_ASM_X86_UNISTD_H 4 4 5 - /* x32 syscall flag bit */ 6 - #define __X32_SYSCALL_BIT 0x40000000UL 5 + /* 6 + * x32 syscall flag bit. Some user programs expect syscall NR macros 7 + * and __X32_SYSCALL_BIT to have type int, even though syscall numbers 8 + * are, for practical purposes, unsigned long. 9 + * 10 + * Fortunately, expressions like (nr & ~__X32_SYSCALL_BIT) do the right 11 + * thing regardless. 12 + */ 13 + #define __X32_SYSCALL_BIT 0x40000000 7 14 8 15 #ifndef __KERNEL__ 9 16 # ifdef __i386__
+48 -38
arch/x86/kernel/fpu/xstate.c
··· 957 957 return true; 958 958 } 959 959 960 - /* 961 - * This is similar to user_regset_copyout(), but will not add offset to 962 - * the source data pointer or increment pos, count, kbuf, and ubuf. 963 - */ 964 - static inline void 965 - __copy_xstate_to_kernel(void *kbuf, const void *data, 966 - unsigned int offset, unsigned int size, unsigned int size_total) 960 + static void fill_gap(unsigned to, void **kbuf, unsigned *pos, unsigned *count) 967 961 { 968 - if (offset < size_total) { 969 - unsigned int copy = min(size, size_total - offset); 962 + if (*pos < to) { 963 + unsigned size = to - *pos; 970 964 971 - memcpy(kbuf + offset, data, copy); 965 + if (size > *count) 966 + size = *count; 967 + memcpy(*kbuf, (void *)&init_fpstate.xsave + *pos, size); 968 + *kbuf += size; 969 + *pos += size; 970 + *count -= size; 971 + } 972 + } 973 + 974 + static void copy_part(unsigned offset, unsigned size, void *from, 975 + void **kbuf, unsigned *pos, unsigned *count) 976 + { 977 + fill_gap(offset, kbuf, pos, count); 978 + if (size > *count) 979 + size = *count; 980 + if (size) { 981 + memcpy(*kbuf, from, size); 982 + *kbuf += size; 983 + *pos += size; 984 + *count -= size; 972 985 } 973 986 } 974 987 ··· 994 981 */ 995 982 int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int offset_start, unsigned int size_total) 996 983 { 997 - unsigned int offset, size; 998 984 struct xstate_header header; 985 + const unsigned off_mxcsr = offsetof(struct fxregs_state, mxcsr); 986 + unsigned count = size_total; 999 987 int i; 1000 988 1001 989 /* ··· 1012 998 header.xfeatures = xsave->header.xfeatures; 1013 999 header.xfeatures &= ~XFEATURE_MASK_SUPERVISOR; 1014 1000 1001 + if (header.xfeatures & XFEATURE_MASK_FP) 1002 + copy_part(0, off_mxcsr, 1003 + &xsave->i387, &kbuf, &offset_start, &count); 1004 + if (header.xfeatures & (XFEATURE_MASK_SSE | XFEATURE_MASK_YMM)) 1005 + copy_part(off_mxcsr, MXCSR_AND_FLAGS_SIZE, 1006 + &xsave->i387.mxcsr, &kbuf, &offset_start, &count); 1007 + if (header.xfeatures & XFEATURE_MASK_FP) 1008 + copy_part(offsetof(struct fxregs_state, st_space), 128, 1009 + &xsave->i387.st_space, &kbuf, &offset_start, &count); 1010 + if (header.xfeatures & XFEATURE_MASK_SSE) 1011 + copy_part(xstate_offsets[XFEATURE_MASK_SSE], 256, 1012 + &xsave->i387.xmm_space, &kbuf, &offset_start, &count); 1013 + /* 1014 + * Fill xsave->i387.sw_reserved value for ptrace frame: 1015 + */ 1016 + copy_part(offsetof(struct fxregs_state, sw_reserved), 48, 1017 + xstate_fx_sw_bytes, &kbuf, &offset_start, &count); 1015 1018 /* 1016 1019 * Copy xregs_state->header: 1017 1020 */ 1018 - offset = offsetof(struct xregs_state, header); 1019 - size = sizeof(header); 1021 + copy_part(offsetof(struct xregs_state, header), sizeof(header), 1022 + &header, &kbuf, &offset_start, &count); 1020 1023 1021 - __copy_xstate_to_kernel(kbuf, &header, offset, size, size_total); 1022 - 1023 - for (i = 0; i < XFEATURE_MAX; i++) { 1024 + for (i = FIRST_EXTENDED_XFEATURE; i < XFEATURE_MAX; i++) { 1024 1025 /* 1025 1026 * Copy only in-use xstates: 1026 1027 */ 1027 1028 if ((header.xfeatures >> i) & 1) { 1028 1029 void *src = __raw_xsave_addr(xsave, i); 1029 1030 1030 - offset = xstate_offsets[i]; 1031 - size = xstate_sizes[i]; 1032 - 1033 - /* The next component has to fit fully into the output buffer: */ 1034 - if (offset + size > size_total) 1035 - break; 1036 - 1037 - __copy_xstate_to_kernel(kbuf, src, offset, size, size_total); 1031 + copy_part(xstate_offsets[i], xstate_sizes[i], 1032 + src, &kbuf, &offset_start, &count); 1038 1033 } 1039 1034 1040 1035 } 1041 - 1042 - if (xfeatures_mxcsr_quirk(header.xfeatures)) { 1043 - offset = offsetof(struct fxregs_state, mxcsr); 1044 - size = MXCSR_AND_FLAGS_SIZE; 1045 - __copy_xstate_to_kernel(kbuf, &xsave->i387.mxcsr, offset, size, size_total); 1046 - } 1047 - 1048 - /* 1049 - * Fill xsave->i387.sw_reserved value for ptrace frame: 1050 - */ 1051 - offset = offsetof(struct fxregs_state, sw_reserved); 1052 - size = sizeof(xstate_fx_sw_bytes); 1053 - 1054 - __copy_xstate_to_kernel(kbuf, xstate_fx_sw_bytes, offset, size, size_total); 1036 + fill_gap(size_total, &kbuf, &offset_start, &count); 1055 1037 1056 1038 return 0; 1057 1039 }
+11 -11
arch/x86/kernel/ioport.c
··· 33 33 set_tsk_thread_flag(tsk, TIF_IO_BITMAP); 34 34 } 35 35 36 - static void task_update_io_bitmap(void) 36 + static void task_update_io_bitmap(struct task_struct *tsk) 37 37 { 38 - struct thread_struct *t = &current->thread; 38 + struct thread_struct *t = &tsk->thread; 39 39 40 40 if (t->iopl_emul == 3 || t->io_bitmap) { 41 41 /* TSS update is handled on exit to user space */ 42 - set_thread_flag(TIF_IO_BITMAP); 42 + set_tsk_thread_flag(tsk, TIF_IO_BITMAP); 43 43 } else { 44 - clear_thread_flag(TIF_IO_BITMAP); 44 + clear_tsk_thread_flag(tsk, TIF_IO_BITMAP); 45 45 /* Invalidate TSS */ 46 46 preempt_disable(); 47 47 tss_update_io_bitmap(); ··· 49 49 } 50 50 } 51 51 52 - void io_bitmap_exit(void) 52 + void io_bitmap_exit(struct task_struct *tsk) 53 53 { 54 - struct io_bitmap *iobm = current->thread.io_bitmap; 54 + struct io_bitmap *iobm = tsk->thread.io_bitmap; 55 55 56 - current->thread.io_bitmap = NULL; 57 - task_update_io_bitmap(); 56 + tsk->thread.io_bitmap = NULL; 57 + task_update_io_bitmap(tsk); 58 58 if (iobm && refcount_dec_and_test(&iobm->refcnt)) 59 59 kfree(iobm); 60 60 } ··· 102 102 if (!iobm) 103 103 return -ENOMEM; 104 104 refcount_set(&iobm->refcnt, 1); 105 - io_bitmap_exit(); 105 + io_bitmap_exit(current); 106 106 } 107 107 108 108 /* ··· 134 134 } 135 135 /* All permissions dropped? */ 136 136 if (max_long == UINT_MAX) { 137 - io_bitmap_exit(); 137 + io_bitmap_exit(current); 138 138 return 0; 139 139 } 140 140 ··· 192 192 } 193 193 194 194 t->iopl_emul = level; 195 - task_update_io_bitmap(); 195 + task_update_io_bitmap(current); 196 196 197 197 return 0; 198 198 }
+2 -2
arch/x86/kernel/process.c
··· 96 96 } 97 97 98 98 /* 99 - * Free current thread data structures etc.. 99 + * Free thread data structures etc.. 100 100 */ 101 101 void exit_thread(struct task_struct *tsk) 102 102 { ··· 104 104 struct fpu *fpu = &t->fpu; 105 105 106 106 if (test_thread_flag(TIF_IO_BITMAP)) 107 - io_bitmap_exit(); 107 + io_bitmap_exit(tsk); 108 108 109 109 free_vm86(t); 110 110
+1 -1
tools/arch/x86/include/uapi/asm/unistd.h
··· 3 3 #define _UAPI_ASM_X86_UNISTD_H 4 4 5 5 /* x32 syscall flag bit */ 6 - #define __X32_SYSCALL_BIT 0x40000000UL 6 + #define __X32_SYSCALL_BIT 0x40000000 7 7 8 8 #ifndef __KERNEL__ 9 9 # ifdef __i386__