Linear Address Masking enabling
Subsystems (28)
v4 cover Linear Address Masking enabling
Linear Address Masking[1] (LAM) modifies the checking that is applied to 64-bit linear addresses, allowing software to use of the untranslated address bits for metadata.
The patchset brings support for LAM for userspace addresses.
LAM_U48 enabling is controversial since it competes for bits with 5-level paging. Its enabling isolated into an optional last patch that can be applied at maintainer's discretion.
Please review and consider applying.
v4:
- Fix untagged_addr() for LAM_U48;
- Remove no-threads restriction on LAM enabling;
- Fix mm_struct access from /proc/$PID/arch_status
- Fix LAM handling in initialize_tlbstate_and_flush()
- Pack tlb_state better;
- Comments and commit messages;
v3:
- Rebased onto v5.19-rc1
- Per-process enabling;
- API overhaul (again);
- Avoid branches and costly computations in the fast path;
- LAM_U48 is in optional patch.
v2:
- Rebased onto v5.18-rc1
- New arch_prctl(2)-based API
- Expose status of LAM (or other thread features) in /proc/$PID/arch_status
[1] ISE, Chapter 14.
https://software.intel.com/content/dam/develop/external/us/en/documents-tps/architecture-instruction-set-extensions-programming-reference.pdf
Kirill A. Shutemov (8):
x86/mm: Fix CR3_ADDR_MASK
x86: CPUID and CR3/CR4 flags for Linear Address Masking
mm: Pass down mm_struct to untagged_addr()
x86/mm: Handle LAM on context switch
x86/uaccess: Provide untagged_addr() and remove tags before address
check
x86/mm: Provide ARCH_GET_UNTAG_MASK and ARCH_ENABLE_TAGGED_ADDR
x86: Expose untagging mask in /proc/$PID/arch_status x86/mm: Extend LAM to support to LAM_U48
arch/arm64/include/asm/memory.h | 4 +- arch/arm64/include/asm/signal.h | 2 +- arch/arm64/include/asm/uaccess.h | 4 +- arch/arm64/kernel/hw_breakpoint.c | 2 +- arch/arm64/kernel/traps.c | 4 +- arch/arm64/mm/fault.c | 10 +-- arch/sparc/include/asm/pgtable_64.h | 2 +- arch/sparc/include/asm/uaccess_64.h | 2 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/elf.h | 3 +- arch/x86/include/asm/mmu.h | 2 + arch/x86/include/asm/mmu_context.h | 58 ++++++++++++++ arch/x86/include/asm/processor-flags.h | 2 +- arch/x86/include/asm/tlbflush.h | 4 + arch/x86/include/asm/uaccess.h | 42 +++++++++- arch/x86/include/uapi/asm/prctl.h | 3 + arch/x86/include/uapi/asm/processor-flags.h | 6 ++ arch/x86/kernel/Makefile | 2 + arch/x86/kernel/fpu/xstate.c | 47 ----------- arch/x86/kernel/proc.c | 60 ++++++++++++++ arch/x86/kernel/process.c | 3 + arch/x86/kernel/process_64.c | 78 ++++++++++++++++++- arch/x86/kernel/sys_x86_64.c | 5 +- arch/x86/mm/hugetlbpage.c | 6 +- arch/x86/mm/mmap.c | 9 ++- arch/x86/mm/tlb.c | 68 ++++++++++++---- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +- drivers/gpu/drm/radeon/radeon_gem.c | 2 +- drivers/infiniband/hw/mlx4/mr.c | 2 +- drivers/media/common/videobuf2/frame_vector.c | 2 +- drivers/media/v4l2-core/videobuf-dma-contig.c | 2 +- .../staging/media/atomisp/pci/hmm/hmm_bo.c | 2 +- drivers/tee/tee_shm.c | 2 +- drivers/vfio/vfio_iommu_type1.c | 2 +- fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 11 --- include/linux/uaccess.h | 15 ++++ lib/strncpy_from_user.c | 2 +- lib/strnlen_user.c | 2 +- mm/gup.c | 6 +- mm/madvise.c | 2 +- mm/mempolicy.c | 6 +- mm/migrate.c | 2 +- mm/mincore.c | 2 +- mm/mlock.c | 4 +- mm/mmap.c | 2 +- mm/mprotect.c | 2 +- mm/mremap.c | 2 +- mm/msync.c | 2 +- virt/kvm/kvm_main.c | 2 +- 51 files changed, 385 insertions(+), 126 deletions(-) create mode 100644 arch/x86/kernel/proc.c
Signature · 1 line
2.35.1
PATCH v4 1/8 x86/mm: Fix CR3_ADDR_MASK
The mask must not include bits above physical address mask. These bits are reserved and can be used for other things. Bits 61 and 62 are used for Linear Address Masking.
--- arch/x86/include/asm/processor-flags.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/processor-flags.h +1 -1
@@ -35,7 +35,7 @@*/ #ifdef CONFIG_X86_64 /* Mask off the address space ID and SME encryption bits. */ -#define CR3_ADDR_MASK __sme_clr(0x7FFFFFFFFFFFF000ull) +#define CR3_ADDR_MASK __sme_clr(PHYSICAL_PAGE_MASK)#define CR3_PCID_MASK 0xFFFull#define CR3_NOFLUSH BIT_ULL(63)
Signature · 1 line
2.35.1
PATCH v4 2/8 x86: CPUID and CR3/CR4 flags for Linear Address Masking
In reply to Kirill A. ShutemovEnumerate Linear Address Masking and provide defines for CR3 and CR4 flags.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/uapi/asm/processor-flags.h | 6 ++++++ 2 files changed, 7 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -300,6 +300,7 @@/* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */ #define X86_FEATURE_AVX_VNNI (12*32+ 4)/* AVX VNNI instructions */ #define X86_FEATURE_AVX512_BF16 (12*32+ 5)/* AVX512 BFLOAT16 instructions */ +#define X86_FEATURE_LAM (12*32+26)/* Linear Address Masking */ /* AMD-defined CPU features, CPUID level 0x80000008 (EBX), word 13 */ #define X86_FEATURE_CLZERO (13*32+ 0)/* CLZERO instruction */
arch/x86/include/uapi/asm/processor-flags.h +6 -0
@@ -82,6 +82,10 @@#define X86_CR3_PCID_BITS 12#define X86_CR3_PCID_MASK (_AC((1UL << X86_CR3_PCID_BITS) - 1, UL)) +#define X86_CR3_LAM_U57_BIT 61/* Activate LAM for userspace, 62:57 bits masked */ +#define X86_CR3_LAM_U57 _BITULL(X86_CR3_LAM_U57_BIT) +#define X86_CR3_LAM_U48_BIT 62/* Activate LAM for userspace, 62:48 bits masked */ +#define X86_CR3_LAM_U48 _BITULL(X86_CR3_LAM_U48_BIT)#define X86_CR3_PCID_NOFLUSH_BIT 63/* Preserve old PCID */ #define X86_CR3_PCID_NOFLUSH _BITULL(X86_CR3_PCID_NOFLUSH_BIT)
@@ -132,6 +136,8 @@#define X86_CR4_PKE _BITUL(X86_CR4_PKE_BIT)#define X86_CR4_CET_BIT 23/* enable Control-flow Enforcement Technology */ #define X86_CR4_CET _BITUL(X86_CR4_CET_BIT) +#define X86_CR4_LAM_SUP_BIT 28/* LAM for supervisor pointers */ +#define X86_CR4_LAM_SUP _BITUL(X86_CR4_LAM_SUP_BIT)/* * x86-64 Task Priority Register, CR8
Signature · 1 line
2.35.1
PATCH v4 3/8 mm: Pass down mm_struct to untagged_addr()
In reply to Kirill A. ShutemovIntel Linear Address Masking (LAM) brings per-mm untagging rules. Pass down mm_struct to the untagging helper. It will help to apply untagging policy correctly.
In most cases, current->mm is the one to use, but there are some exceptions, such as get_user_page_remote().
Move dummy implementation of untagged_addr() from <linux/mm.h> to <linux/uaccess.h>. <asm/uaccess.h> can override the implementation. Moving the dummy header outside <linux/mm.h> helps to avoid header hell if you need to defer mm_struct within the helper.
--- arch/arm64/include/asm/memory.h | 4 ++-- arch/arm64/include/asm/signal.h | 2 +- arch/arm64/include/asm/uaccess.h | 4 ++-- arch/arm64/kernel/hw_breakpoint.c | 2 +- arch/arm64/kernel/traps.c | 4 ++-- arch/arm64/mm/fault.c | 10 +++++----- arch/sparc/include/asm/pgtable_64.h | 2 +- arch/sparc/include/asm/uaccess_64.h | 2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 +- drivers/gpu/drm/radeon/radeon_gem.c | 2 +- drivers/infiniband/hw/mlx4/mr.c | 2 +- drivers/media/common/videobuf2/frame_vector.c | 2 +- drivers/media/v4l2-core/videobuf-dma-contig.c | 2 +- drivers/staging/media/atomisp/pci/hmm/hmm_bo.c | 2 +- drivers/tee/tee_shm.c | 2 +- drivers/vfio/vfio_iommu_type1.c | 2 +- fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 11 ----------- include/linux/uaccess.h | 15 +++++++++++++++ lib/strncpy_from_user.c | 2 +- lib/strnlen_user.c | 2 +- mm/gup.c | 6 +++--- mm/madvise.c | 2 +- mm/mempolicy.c | 6 +++--- mm/migrate.c | 2 +- mm/mincore.c | 2 +- mm/mlock.c | 4 ++-- mm/mmap.c | 2 +- mm/mprotect.c | 2 +- mm/mremap.c | 2 +- mm/msync.c | 2 +- virt/kvm/kvm_main.c | 2 +- 33 files changed, 59 insertions(+), 53 deletions(-)
arch/arm64/include/asm/memory.h +2 -2
@@ -215,8 +215,8 @@ static inline unsigned long kaslr_offset(void)#define __untagged_addr (addr) \ ((__force __typeof__(addr))sign_extend64((__force u64)(addr), 55)) -#define untagged_addr (addr) ({ \ - u64 __addr = (__force u64)(addr); \ +#define untagged_addr (mm, addr) ({ \ + u64 __addr = (__force u64)(addr); \ __addr &= __untagged_addr(__addr); \ (__force __typeof__(addr))__addr; \ })
arch/arm64/include/asm/signal.h +1 -1
@@ -18,7 +18,7 @@ static inline void __user *arch_untagged_si_addr(void __user *addr,if (sig ==SIGTRAP && si_code ==TRAP_BRKPT )return addr; -return untagged_addr (addr); +return untagged_addr (current->mm, addr); }#define arch_untagged_si_addr arch_untagged_si_addr
arch/arm64/include/asm/uaccess.h +2 -2
@@ -44,7 +44,7 @@ static inline int access_ok(const void __user *addr, unsigned long size)*/ if (IS_ENABLED (CONFIG_ARM64_TAGGED_ADDR_ABI ) && (current->flags &PF_KTHREAD ||test_thread_flag (TIF_TAGGED_ADDR ))) - addr =untagged_addr (addr); + addr =untagged_addr (current->mm, addr);return likely (__access_ok (addr, size)); }
@@ -217,7 +217,7 @@ static inline void __user *__uaccess_mask_ptr(const void __user *ptr) " csel %0, %1, xzr, eq\n" : "=&r" (safe_ptr) : "r" (ptr), "r" (TASK_SIZE_MAX - 1), - "r" (untagged_addr(ptr)) + "r" (untagged_addr(current->mm, ptr)) : "cc");csdb ();
arch/arm64/kernel/hw_breakpoint.c +1 -1
@@ -715,7 +715,7 @@ static u64 get_distance_from_watchpoint(unsigned long addr, u64 val,u64 wp_low, wp_high;u32 lens, lene; - addr =untagged_addr (addr); + addr =untagged_addr (current->mm, addr); lens =__ffs (ctrl->len); lene =__fls (ctrl->len);
arch/arm64/kernel/traps.c +2 -2
@@ -476,7 +476,7 @@ void arm64_notify_segfault(unsigned long addr)int code;mmap_read_lock (current->mm); -if (find_vma (current->mm,untagged_addr (addr)) ==NULL ) +if (find_vma (current->mm,untagged_addr (current->mm, addr)) ==NULL ) code =SEGV_MAPERR ;else code =SEGV_ACCERR ;
@@ -540,7 +540,7 @@ static void user_cache_maint_handler(unsigned long esr, struct pt_regs *regs)int ret =0 ; tagged_address =pt_regs_read_reg (regs, rt); - address =untagged_addr (tagged_address); + address =untagged_addr (current->mm, tagged_address);switch (crm) {case ESR_ELx_SYS64_ISS_CRM_DC_CVAU:/* DC CVAU, gets promoted */
arch/arm64/mm/fault.c +5 -5
@@ -454,7 +454,7 @@ static void set_thread_esr(unsigned long address, unsigned long esr)static void do_bad_area (unsigned long far,unsigned long esr,struct pt_regs *regs) { -unsigned long addr =untagged_addr (far); +unsigned long addr =untagged_addr (current->mm, far);/* * If we are in kernel mode at this point, we have no context to
@@ -524,7 +524,7 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr,vm_fault_t fault;unsigned long vm_flags;unsigned int mm_flags =FAULT_FLAG_DEFAULT ; -unsigned long addr =untagged_addr (far); +unsigned long addr =untagged_addr (mm, far);if (kprobe_page_fault (regs, esr))return 0 ;
@@ -675,7 +675,7 @@ static int __kprobes do_translation_fault(unsigned long far,unsigned long esr, struct pt_regs *regs) { - unsigned long addr = untagged_addr(far); + unsigned long addr = untagged_addr(current->mm, far);if (is_ttbr0_addr (addr))return do_page_fault (far, esr, regs);
@@ -719,7 +719,7 @@ static int do_sea(unsigned long far, unsigned long esr, struct pt_regs *regs)* UNKNOWN for synchronous external aborts. Mask them out now * so that userspace doesn't see them. */ - siaddr =untagged_addr (far); + siaddr =untagged_addr (current->mm, far); }arm64_notify_die (inf->name, regs, inf->sig, inf->code, siaddr, esr);
@@ -809,7 +809,7 @@ static const struct fault_info fault_info[] = {void do_mem_abort (unsigned long far,unsigned long esr,struct pt_regs *regs) {const struct fault_info *inf =esr_to_fault_info (esr); -unsigned long addr =untagged_addr (far); +unsigned long addr =untagged_addr (current->mm, far);if (!inf->fn (far, esr, regs))return ;
arch/sparc/include/asm/pgtable_64.h +1 -1
@@ -1071,7 +1071,7 @@ static inline unsigned long __untagged_addr(unsigned long start)return start; } -#define untagged_addr (addr) \ +#define untagged_addr (mm, addr) \ ((__typeof__(addr))(__untagged_addr((unsigned long)(addr))))static inline bool pte_access_permitted (pte_t pte,bool write)
arch/sparc/include/asm/uaccess_64.h +2 -0
@@ -8,8 +8,10 @@#include <linux/compiler.h> #include <linux/string.h> +#include <linux/mm_types.h> #include <asm/asi.h> #include <asm/spitfire.h> +#include <asm/pgtable.h> #include <asm/processor.h> #include <asm-generic/access_ok.h>
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -1
@@ -1491,7 +1491,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(if (flags &KFD_IOC_ALLOC_MEM_FLAGS_USERPTR ) {if (!offset || !*offset)return -EINVAL ; - user_addr =untagged_addr (*offset); + user_addr =untagged_addr (current->mm, *offset); }else if (flags & (KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL |KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP )) { bo_type = ttm_bo_type_sg;
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
@@ -382,7 +382,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,uint32_t handle;int r; - args->addr =untagged_addr (args->addr); + args->addr =untagged_addr (current->mm, args->addr);if (offset_in_page (args->addr | args->size))return -EINVAL ;
drivers/gpu/drm/radeon/radeon_gem.c +1 -1
@@ -371,7 +371,7 @@ int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data,uint32_t handle;int r; - args->addr =untagged_addr (args->addr); + args->addr =untagged_addr (current->mm, args->addr);if (offset_in_page (args->addr | args->size))return -EINVAL ;
drivers/infiniband/hw/mlx4/mr.c +1 -1
@@ -379,7 +379,7 @@ static struct ib_umem *mlx4_get_umem_mr(struct ib_device *device, u64 start,* again */ if (!ib_access_writable (access_flags)) { -unsigned long untagged_start =untagged_addr (start); +unsigned long untagged_start =untagged_addr (current->mm, start);struct vm_area_struct *vma;mmap_read_lock (current->mm);
drivers/media/common/videobuf2/frame_vector.c +1 -1
@@ -47,7 +47,7 @@ int get_vaddr_frames(unsigned long start, unsigned int nr_frames,if (WARN_ON_ONCE (nr_frames > vec->nr_allocated)) nr_frames = vec->nr_allocated; - start =untagged_addr (start); + start =untagged_addr (mm, start); ret = pin_user_pages_fast(start, nr_frames, FOLL_FORCE | FOLL_WRITE | FOLL_LONGTERM,
drivers/media/v4l2-core/videobuf-dma-contig.c +1 -1
@@ -157,8 +157,8 @@ static void videobuf_dma_contig_user_put(struct videobuf_dma_contig_memory *mem)static int videobuf_dma_contig_user_get (struct videobuf_dma_contig_memory *mem,struct videobuf_buffer *vb) { -unsigned long untagged_baddr =untagged_addr (vb->baddr);struct mm_struct *mm = current->mm; +unsigned long untagged_baddr =untagged_addr (mm, vb->baddr);struct vm_area_struct *vma;unsigned long prev_pfn, this_pfn;unsigned long pages_done, user_address;
drivers/staging/media/atomisp/pci/hmm/hmm_bo.c +1 -1
@@ -913,7 +913,7 @@ static int alloc_user_pages(struct hmm_buffer_object *bo,* and map to user space */ - userptr =untagged_addr (userptr); + userptr =untagged_addr (current->mm, userptr); bo->pages = pages;
drivers/tee/tee_shm.c +1 -1
@@ -261,7 +261,7 @@ register_shm_helper(struct tee_context *ctx, unsigned long addr, shm->flags = flags; shm->ctx = ctx; shm->id = id; - addr =untagged_addr (addr); + addr =untagged_addr (current->mm, addr); start =rounddown (addr,PAGE_SIZE ); shm->offset = addr - start; shm->size = length;
drivers/vfio/vfio_iommu_type1.c +1 -1
@@ -561,7 +561,7 @@ static int vaddr_get_pfns(struct mm_struct *mm, unsigned long vaddr, goto done; } - vaddr =untagged_addr (vaddr); + vaddr =untagged_addr (mm, vaddr); retry: vma =vma_lookup (mm, vaddr);
fs/proc/task_mmu.c +1 -1
@@ -1659,7 +1659,7 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,/* watch out for wraparound */ start_vaddr = end_vaddr;if (svpfn <= (ULONG_MAX >>PAGE_SHIFT )) - start_vaddr =untagged_addr (svpfn <<PAGE_SHIFT ); + start_vaddr =untagged_addr (mm, svpfn <<PAGE_SHIFT );/* Ensure the address is inside the task */ if (start_vaddr > mm->task_size)
include/linux/mm.h +0 -11
@@ -94,17 +94,6 @@ extern int mmap_rnd_compat_bits __read_mostly;#include <asm/page.h> #include <asm/processor.h> -/* -* Architectures that support memory tagging (assigning tags to memory regions, -* embedding these tags into addresses that point to these memory regions, and -* checking that the memory and the pointer tags match on memory accesses) -* redefine this macro to strip tags from pointers. -* It's defined as noop for architectures that don't support memory tagging. -*/ -#ifndef untagged_addr -#define untagged_addr (addr) (addr) -#endif -#ifndef __pa_symbol#define __pa_symbol (x) __pa(RELOC_HIDE((unsigned long)(x), 0))#endif
include/linux/uaccess.h +15 -0
@@ -10,6 +10,21 @@#include <asm/uaccess.h> +/* +* Architectures that support memory tagging (assigning tags to memory regions, +* embedding these tags into addresses that point to these memory regions, and +* checking that the memory and the pointer tags match on memory accesses) +* redefine this macro to strip tags from pointers. +* +* Passing down mm_struct allows to define untagging rules on per-process +* basis. +* +* It's defined as noop for architectures that don't support memory tagging. +*/ +#ifndef untagged_addr +#define untagged_addr (mm, addr) (addr) +#endif +/* * Architectures should provide two primitives (raw_copy_{to,from}_user()) * and get rid of their private instances of copy_{to,from}_user() and
lib/strncpy_from_user.c +1 -1
@@ -121,7 +121,7 @@ long strncpy_from_user(char *dst, const char __user *src, long count)return 0 ; max_addr =TASK_SIZE_MAX ; - src_addr = (unsigned long )untagged_addr (src); + src_addr = (unsigned long )untagged_addr (current->mm, src);if (likely (src_addr < max_addr)) {unsigned long max = max_addr - src_addr;long retval;
lib/strnlen_user.c +1 -1
@@ -97,7 +97,7 @@ long strnlen_user(const char __user *str, long count)return 0 ; max_addr =TASK_SIZE_MAX ; - src_addr = (unsigned long )untagged_addr (str); + src_addr = (unsigned long )untagged_addr (current->mm, str);if (likely (src_addr < max_addr)) {unsigned long max = max_addr - src_addr;long retval;
mm/gup.c +3 -3
@@ -1104,7 +1104,7 @@ static long __get_user_pages(struct mm_struct *mm,if (!nr_pages)return 0 ; - start =untagged_addr (start); + start =untagged_addr (mm, start);VM_BUG_ON (!!pages != !!(gup_flags & (FOLL_GET |FOLL_PIN )));
@@ -1285,7 +1285,7 @@ int fixup_user_fault(struct mm_struct *mm,struct vm_area_struct *vma;vm_fault_t ret; - address =untagged_addr (address); + address =untagged_addr (mm, address);if (unlocked) fault_flags |=FAULT_FLAG_ALLOW_RETRY |FAULT_FLAG_KILLABLE ;
@@ -2865,7 +2865,7 @@ static int internal_get_user_pages_fast(unsigned long start,if (!(gup_flags &FOLL_FAST_ONLY ))might_lock_read (¤t->mm->mmap_lock); - start =untagged_addr (start) &PAGE_MASK ; + start =untagged_addr (current->mm, start) &PAGE_MASK ; len = nr_pages <<PAGE_SHIFT ;if (check_add_overflow (start, len, &end))return 0 ;
mm/madvise.c +1 -1
@@ -1373,7 +1373,7 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int behsize_t len;struct blk_plug plug; - start =untagged_addr (start); + start =untagged_addr (mm, start);if (!madvise_behavior_valid (behavior))return -EINVAL ;
mm/mempolicy.c +3 -3
@@ -1458,7 +1458,7 @@ static long kernel_mbind(unsigned long start, unsigned long len,int lmode = mode;int err; - start =untagged_addr (start); + start =untagged_addr (current->mm, start); err =sanitize_mpol_flags (&lmode, &mode_flags);if (err)return err;
@@ -1481,7 +1481,7 @@ SYSCALL_DEFINE4(set_mempolicy_home_node, unsigned long, start, unsigned long, leunsigned long end;int err = -ENOENT ; - start =untagged_addr (start); + start =untagged_addr (mm, start);if (start & ~PAGE_MASK )return -EINVAL ;/*
@@ -1684,7 +1684,7 @@ static int kernel_get_mempolicy(int __user *policy,if (nmask !=NULL && maxnode < nr_node_ids)return -EINVAL ; - addr =untagged_addr (addr); + addr =untagged_addr (current->mm, addr); err =do_get_mempolicy (&pval, &nodes, addr, flags);
mm/migrate.c +1 -1
@@ -1714,7 +1714,7 @@ static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes, goto out_flush;if (get_user (node, nodes + i)) goto out_flush; - addr = (unsigned long )untagged_addr (p); + addr = (unsigned long )untagged_addr (mm, p); err = -ENODEV ;if (node <0 || node >=MAX_NUMNODES )
mm/mincore.c +1 -1
@@ -236,7 +236,7 @@ SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,unsigned long pages;unsigned char *tmp; - start =untagged_addr (start); + start =untagged_addr (current->mm, start);/* Check the start address: needs to be page-aligned.. */ if (start & ~PAGE_MASK )
mm/mlock.c +2 -2
@@ -571,7 +571,7 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t flaunsigned long lock_limit;int error = -ENOMEM ; - start =untagged_addr (start); + start =untagged_addr (current->mm, start);if (!can_do_mlock ())return -EPERM ;
@@ -634,7 +634,7 @@ SYSCALL_DEFINE2(munlock, unsigned long, start, size_t, len) {int ret; - start =untagged_addr (start); + start =untagged_addr (current->mm, start); len =PAGE_ALIGN (len + (offset_in_page (start))); start &=PAGE_MASK ;
mm/mmap.c +1 -1
@@ -2926,7 +2926,7 @@ EXPORT_SYMBOL(vm_munmap); SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len) { - addr =untagged_addr (addr); + addr =untagged_addr (current->mm, addr);return __vm_munmap (addr, len, true); }
mm/mprotect.c +1 -1
@@ -622,7 +622,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len, (prot &PROT_READ );struct mmu_gather tlb; - start =untagged_addr (start); + start =untagged_addr (current->mm, start); prot &= ~(PROT_GROWSDOWN |PROT_GROWSUP );if (grows == (PROT_GROWSDOWN |PROT_GROWSUP ))/* can't be both */
mm/mremap.c +1 -1
@@ -906,7 +906,7 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,* * See Documentation/arm64/tagged-address-abi.rst for more information. */ - addr =untagged_addr (addr); + addr =untagged_addr (mm, addr);if (flags & ~(MREMAP_FIXED |MREMAP_MAYMOVE |MREMAP_DONTUNMAP ))return ret;
mm/msync.c +1 -1
@@ -37,7 +37,7 @@ SYSCALL_DEFINE3(msync, unsigned long, start, size_t, len, int, flags)int unmapped_error =0 ;int error = -EINVAL ; - start =untagged_addr (start); + start =untagged_addr (mm, start);if (flags & ~(MS_ASYNC |MS_INVALIDATE |MS_SYNC )) goto out;
virt/kvm/kvm_main.c +1 -1
@@ -1876,7 +1876,7 @@ int __kvm_set_memory_region(struct kvm *kvm,return -EINVAL ;/* We can read the guest memory with __xxx_user() later on. */ if ((mem->userspace_addr & (PAGE_SIZE -1 )) || - (mem->userspace_addr !=untagged_addr (mem->userspace_addr)) || + (mem->userspace_addr !=untagged_addr (kvm->mm, mem->userspace_addr)) || !access_ok ((void __user *)(unsigned long )mem->userspace_addr, mem->memory_size))return -EINVAL ;
Signature · 1 line
2.35.1
Re: [PATCHv4 3/8] mm: Pass down mm_struct to untagged_addr()
Kirill,
lib/strnlen_user.c long strnlen_user(const char __user *str, long count) · line 100
Quoted 10 lines
lib/strnlen_user.c +1
-1@@ -97,7 +97,7 @@ long strnlen_user(const char __user *str, long count)return 0 ; max_addr =TASK_SIZE_MAX ; - src_addr = (unsigned long )untagged_addr (str); + src_addr = (unsigned long )untagged_addr (current->mm, str);
In a downstream kernel with LAM disabled I'm seeing current->mm being NULL at this point, because strnlen_user() is being called by kdevtmpfs.
IIUC current->mm is only guaranteed to be non-NULL in the userspace process context, whereas untagged_addr() may get called in random places.
Am I missing something?
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Liana Sebastian Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCHv4 3/8] mm: Pass down mm_struct to untagged_addr()
Quoted 21 lines · Alexander Potapenko
Kirill,
lib/strnlen_user.c long strnlen_user(const char __user *str, long count) · line 100
Quoted 10 lines
lib/strnlen_user.c +1
-1@@ -97,7 +97,7 @@ long strnlen_user(const char __user *str, long count)return 0 ; max_addr =TASK_SIZE_MAX ; - src_addr = (unsigned long )untagged_addr (str); + src_addr = (unsigned long )untagged_addr (current->mm, str);In a downstream kernel with LAM disabled I'm seeing
current->mmbeing NULL at this point, becausestrnlen_user()is being called by kdevtmpfs.
IIUCcurrent->mmis only guaranteed to be non-NULL in the userspace process context, whereasuntagged_addr()may get called in random places.
Quoted 1 line
Am I missing something?
Hm. Could you show a traceback?
As strnlen_user() intended to be used on an user string I expected it to be called from a process context. I guess I'm wrong, but I don't yet understand why.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv4 3/8] mm: Pass down mm_struct to untagged_addr()
Quoted 23 lines · Kirill A. Shutemov
Quoted 21 lines · Alexander Potapenko
Kirill,
lib/strnlen_user.c long strnlen_user(const char __user *str, long count) · line 100
Quoted 10 lines
lib/strnlen_user.c +1
-1@@ -97,7 +97,7 @@ long strnlen_user(const char __user *str, long count)return 0 ; max_addr =TASK_SIZE_MAX ; - src_addr = (unsigned long )untagged_addr (str); + src_addr = (unsigned long )untagged_addr (current->mm, str);In a downstream kernel with LAM disabled I'm seeing
current->mmbeing NULL at this point, becausestrnlen_user()is being called by kdevtmpfs.
IIUCcurrent->mmis only guaranteed to be non-NULL in the userspace process context, whereasuntagged_addr()may get called in random places.
Quoted 7 lines
Quoted 1 line
Am I missing something?
Hm. Could you show a traceback?
As
strnlen_user()intended to be used on an user string I expected it to be called from a process context. I guess I'm wrong, but I don't yet understand why.
Oh, I see now. The old implementation of devtmpfsd()
(https://elixir.bootlin.com/linux/v5.4/source/drivers/base/devtmpfs.c#L397)
uses ksys_mount(), which assumes that the strings must be copied from the userspace, whereas they are actually constants in kernel .rodata
Wonder if the validity of mm->current for userspace accesses is actually enforced anyhow in newer kernels.
Quoted 2 lines
--
Kirill A. Shutemov
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Liana Sebastian Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCHv4 3/8] mm: Pass down mm_struct to untagged_addr()
Quoted 33 lines · Alexander Potapenko
Quoted 23 lines · Kirill A. Shutemov
Quoted 21 lines · Alexander Potapenko
Kirill,
lib/strnlen_user.c long strnlen_user(const char __user *str, long count) · line 100
Quoted 10 lines
lib/strnlen_user.c +1
-1@@ -97,7 +97,7 @@ long strnlen_user(const char __user *str, long count)return 0 ; max_addr =TASK_SIZE_MAX ; - src_addr = (unsigned long )untagged_addr (str); + src_addr = (unsigned long )untagged_addr (current->mm, str);In a downstream kernel with LAM disabled I'm seeing
current->mmbeing NULL at this point, becausestrnlen_user()is being called by kdevtmpfs.
IIUCcurrent->mmis only guaranteed to be non-NULL in the userspace process context, whereasuntagged_addr()may get called in random places.Quoted 7 lines
Quoted 1 line
Am I missing something?
Hm. Could you show a traceback?
As
strnlen_user()intended to be used on an user string I expected it to be called from a process context. I guess I'm wrong, but I don't yet understand why.
Quoted 7 lines
Oh, I see now. The old implementation of
devtmpfsd()
(https://elixir.bootlin.com/linux/v5.4/source/drivers/base/devtmpfs.c#L397)
usesksys_mount(), which assumes that the strings must be copied from the userspace, whereas they are actually constants in kernel .rodataWonder if the validity of
mm->currentfor userspace accesses is actually enforced anyhow in newer kernels.
I think it is.
See 967747bbc084 and how it changes strnlen_user(). With max_addr equal to TASK_SIZE_MAX, strnlen_user() will always fail on a kernel string.
Signature · 1 line
Kirill A. Shutemov
PATCH v4 4/8 x86/mm: Handle LAM on context switch
In reply to Kirill A. ShutemovLinear Address Masking mode for userspace pointers encoded in CR3 bits. The mode is selected per-thread. Add new thread features indicate that the thread has Linear Address Masking enabled.
switch_mm_irqs_off() now respects these flags and constructs CR3 accordingly.
The active LAM mode gets recorded in the tlb_state.
--- arch/x86/include/asm/mmu.h | 1 + arch/x86/include/asm/mmu_context.h | 24 +++++++++++ arch/x86/include/asm/tlbflush.h | 4 ++ arch/x86/mm/tlb.c | 68 +++++++++++++++++++++++------- 4 files changed, 82 insertions(+), 15 deletions(-)
arch/x86/include/asm/mmu.h +1 -0
@@ -40,6 +40,7 @@ typedef struct {#ifdef CONFIG_X86_64 unsigned short flags; +u64 lam_cr3_mask;#endif struct mutex lock;
arch/x86/include/asm/mmu_context.h +24 -0
@@ -91,6 +91,29 @@ static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next) }#endif +#ifdef CONFIG_X86_64 +static inline u64 mm_cr3_lam_mask (struct mm_struct *mm) +{ +return mm->context.lam_cr3_mask; +} + +static inline void dup_lam (struct mm_struct *oldmm,struct mm_struct *mm) +{ + mm->context.lam_cr3_mask = oldmm->context.lam_cr3_mask; +} + +#else + +static inline u64 mm_cr3_lam_mask (struct mm_struct *mm) +{ +return 0 ; +} + +static inline void dup_lam (struct mm_struct *oldmm,struct mm_struct *mm) +{ +} +#endif +#define enter_lazy_tlb enter_lazy_tlbextern void enter_lazy_tlb (struct mm_struct *mm,struct task_struct *tsk);
@@ -168,6 +191,7 @@ static inline int arch_dup_mmap(struct mm_struct *oldmm, struct mm_struct *mm) {arch_dup_pkeys (oldmm, mm);paravirt_arch_dup_mmap (oldmm, mm); +dup_lam (oldmm, mm);return ldt_dup_context (oldmm, mm); }
arch/x86/include/asm/tlbflush.h +4 -0
@@ -100,6 +100,10 @@ struct tlb_state {*/ bool invalidate_other; +#ifdef CONFIG_X86_64 +u8 lam; +#endif +/* * Mask that contains TLB_NR_DYN_ASIDS+1 bits to indicate * the corresponding user PCID needs a flush next time we
arch/x86/mm/tlb.c +53 -15
165 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCHv4 4/8] x86/mm: Handle LAM on context switch
Quoted 23 lines · Kirill A. Shutemov
Linear Address Masking mode for userspace pointers encoded in CR3 bits. The mode is selected per-thread. Add new thread features indicate that the thread has Linear Address Masking enabled.
switch_mm_irqs_off()now respects these flags and constructs CR3 accordingly.The active LAM mode gets recorded in the
tlb_state.Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/include/asm/mmu.h | 1 + arch/x86/include/asm/mmu_context.h | 24 +++++++++++ arch/x86/include/asm/tlbflush.h | 4 ++ arch/x86/mm/tlb.c | 68 +++++++++++++++++++++++------- 4 files changed, 82 insertions(+), 15 deletions(-)arch/x86/include/asm/mmu.h +0
-0
arch/x86/include/asm/mmu.h typedef struct { · line 44
Quoted 5 lines
arch/x86/include/asm/mmu.h +1
-0@@ -40,6 +40,7 @@ typedef struct {#ifdef CONFIG_X86_64 unsigned short flags; +u64 lam_cr3_mask;#endif
Can you please add a comment for this field?
Quoted 7 lines
arch/x86/include/asm/mmu.h +0
-0struct mutex lock;arch/x86/include/asm/mmu_context.h +0
-0
arch/x86/include/asm/mmu_context.h static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next) · line 98
Quoted 8 lines
arch/x86/include/asm/mmu_context.h +5
-0@@ -91,6 +91,29 @@ static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next) }#endif +#ifdef CONFIG_X86_64 +static inline u64 mm_cr3_lam_mask (struct mm_struct *mm) +{ +return mm->context.lam_cr3_mask; +}
For the sake of uniformity, can it be either lam_cr3_mask or cr3_lam_mask everywhere?
arch/x86/include/asm/tlbflush.h struct tlb_state { · line 106
Quoted 13 lines
arch/x86/include/asm/tlbflush.h +4
-0@@ -100,6 +100,10 @@ struct tlb_state {*/ bool invalidate_other; +#ifdef CONFIG_X86_64 +u8 lam; +#endif +
Comment here as well, please.
arch/x86/mm/tlb.c static inline u16 user_pcid(u16 asid) · line 157
Quoted 13 lines
arch/x86/include/asm/tlbflush.h +0
-0/* * Mask that contains TLB_NR_DYN_ASIDS+1 bits to indicate * the corresponding user PCID needs a flush next time we arch/x86/mm/tlb.c +1
-1@@ -154,17 +154,17 @@ static inline u16 user_pcid(u16 asid)return ret; } -static inline unsigned long build_cr3 (pgd_t *pgd,u16 asid) +static inline unsigned long build_cr3 (pgd_t *pgd,u16 asid,u64 lam)
I think it's more natural to make lam an unsigned long, because cr3 is treated as unsigned long everywhere.
arch/x86/mm/tlb.c static inline u16 user_pcid(u16 asid) · line 167
Quoted 13 lines
arch/x86/mm/tlb.c +3
-3{if (static_cpu_has (X86_FEATURE_PCID )) { -return __sme_pa (pgd) |kern_pcid (asid); +return __sme_pa (pgd) |kern_pcid (asid) | lam; }else {VM_WARN_ON_ONCE (asid !=0 ); -return __sme_pa (pgd); +return __sme_pa (pgd) | lam; } } -static inline unsigned long build_cr3_noflush (pgd_t *pgd,u16 asid) +static inline unsigned long build_cr3_noflush (pgd_t *pgd,u16 asid,u64 lam)
ditto
Quoted 32 lines
arch/x86/mm/tlb.c +4
-4{VM_WARN_ON_ONCE (asid >MAX_ASID_AVAILABLE );/* @@ -173,7 +173,7 @@ static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)* boot because all CPU's the have same capabilities: */ VM_WARN_ON_ONCE (!boot_cpu_has (X86_FEATURE_PCID )); -return __sme_pa (pgd) |kern_pcid (asid) |CR3_NOFLUSH ; +return __sme_pa (pgd) |kern_pcid (asid) | lam |CR3_NOFLUSH ; }/* @@ -274,15 +274,15 @@ static inline void invalidate_user_asid(u16 asid) (unsigned long *)this_cpu_ptr(&cpu_tlbstate.user_pcid_flush_mask)); } -static void load_new_mm_cr3 (pgd_t *pgdir,u16 new_asid,bool need_flush) +static void load_new_mm_cr3 (pgd_t *pgdir,u16 new_asid,u64 lam,bool need_flush) {unsigned long new_mm_cr3;if (need_flush) {invalidate_user_asid (new_asid); - new_mm_cr3 =build_cr3 (pgdir, new_asid); + new_mm_cr3 =build_cr3 (pgdir, new_asid, lam); }else { - new_mm_cr3 =build_cr3_noflush (pgdir, new_asid); + new_mm_cr3 =build_cr3_noflush (pgdir, new_asid, lam); }/*
arch/x86/mm/tlb.c void cr4_update_pce(void *ignored) · line 490
Quoted 5 lines
arch/x86/mm/tlb.c +2
-0@@ -486,11 +486,38 @@ void cr4_update_pce(void *ignored)static inline void cr4_update_pce_mm (struct mm_struct *mm) { }#endif +#ifdef CONFIG_X86_64 +static inline u64 tlbstate_lam_cr3_mask (void )
Please add comments for these methods.
Quoted 20 lines
arch/x86/mm/tlb.c +20
-0+{ +u64 lam =this_cpu_read (cpu_tlbstate.lam); + +return lam <<X86_CR3_LAM_U57_BIT ; +} + +static inline void set_tlbstate_lam_cr3_mask (u64 mask) +{ +this_cpu_write (cpu_tlbstate.lam, mask >>X86_CR3_LAM_U57_BIT ); +} + +#else + +static inline u64 tlbstate_lam_cr3_mask (void ) +{ +return 0 ; +} + +static inline void set_tlbstate_lam_cr3_mask (u64 mask) +{
arch/x86/mm/tlb.c void cr4_update_pce(void *ignored) · line 520
Quoted 10 lines
arch/x86/mm/tlb.c +5
-0+} +#endif +void switch_mm_irqs_off (struct mm_struct *prev,struct mm_struct *next,struct task_struct *tsk) {struct mm_struct *real_prev =this_cpu_read (cpu_tlbstate.loaded_mm);u16 prev_asid =this_cpu_read (cpu_tlbstate.loaded_mm_asid); +u64 prev_lam =tlbstate_lam_cr3_mask (); +u64 new_lam =mm_cr3_lam_mask (next);
Ditto.
Quoted 23 lines
arch/x86/mm/tlb.c +4
-1bool was_lazy =this_cpu_read (cpu_tlbstate_shared.is_lazy);unsigned cpu =smp_processor_id ();u64 next_tlb_gen;@@ -504,6 +531,9 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,* cpu_tlbstate.loaded_mm) matches next. * * NB: leave_mm() calls us with prev == NULL and tsk == NULL. +* +* NB: Initial LAM enabling calls us with prev == next. We must update +* CR3 if prev_lam doesn't match the new one. */ /* We don't want flush_tlb_func() to run concurrently with us. */ @@ -520,7 +550,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,* isn't free. */ #ifdef CONFIG_DEBUG_VM -if (WARN_ON_ONCE (__read_cr3 () !=build_cr3 (real_prev->pgd, prev_asid))) { +if (WARN_ON_ONCE (__read_cr3 () !=build_cr3 (real_prev->pgd, prev_asid, prev_lam))) {/* * If we were to BUG here, we'd be very likely to kill * the system so hard that we don't see the call trace.
arch/x86/mm/tlb.c void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, · line 584
Quoted 5 lines
arch/x86/mm/tlb.c +1
-1@@ -551,7 +581,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,* provides that full memory barrier and core serializing * instruction. */ -if (real_prev == next) { +if (real_prev == next && prev_lam == new_lam) {
Do we want the warning checks below to only happen if prev_lam==new_lam?
Quoted 34 lines
arch/x86/mm/tlb.c +7
-2VM_WARN_ON (this_cpu_read (cpu_tlbstate.ctxs[prev_asid].ctx_id) != next->context.ctx_id);@@ -622,15 +652,16 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,barrier (); } +set_tlbstate_lam_cr3_mask (new_lam);if (need_flush) {this_cpu_write (cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id);this_cpu_write (cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen); -load_new_mm_cr3 (next->pgd, new_asid, true); +load_new_mm_cr3 (next->pgd, new_asid, new_lam, true);trace_tlb_flush (TLB_FLUSH_ON_TASK_SWITCH ,TLB_FLUSH_ALL ); }else {/* The new ASID is already up to date. */ -load_new_mm_cr3 (next->pgd, new_asid, false); +load_new_mm_cr3 (next->pgd, new_asid, new_lam, false);trace_tlb_flush (TLB_FLUSH_ON_TASK_SWITCH ,0 ); }@@ -691,6 +722,10 @@ void initialize_tlbstate_and_flush(void)/* Assert that CR3 already references the right mm. */ WARN_ON ((cr3 &CR3_ADDR_MASK ) !=__pa (mm->pgd)); +/* LAM expected to be disabled in CR3 and init_mm */ +WARN_ON (cr3 & (X86_CR3_LAM_U48 |X86_CR3_LAM_U57 )); +WARN_ON (mm_cr3_lam_mask (&init_mm)); +/* * Assert that CR4.PCIDE is set if needed. (CR4.PCIDE initialization * doesn't work like other CR4 bits because it can only be set from
arch/x86/mm/tlb.c void initialize_tlbstate_and_flush(void) · line 737
Quoted 3 lines
arch/x86/mm/tlb.c +0
-0@@ -700,7 +735,7 @@ void initialize_tlbstate_and_flush(void) !(cr4_read_shadow() & X86_CR4_PCIDE));/* Force ASID 0 and force a TLB flush. */
Please update this comment.
Quoted 29 lines
arch/x86/mm/tlb.c +6
-4-write_cr3 (build_cr3 (mm->pgd,0 )); +write_cr3 (build_cr3 (mm->pgd,0 ,0 ));/* Reinitialize tlbstate. */ this_cpu_write (cpu_tlbstate.last_user_mm_spec,LAST_USER_MM_INIT );@@ -708,6 +743,7 @@ void initialize_tlbstate_and_flush(void)this_cpu_write (cpu_tlbstate.next_asid,1 );this_cpu_write (cpu_tlbstate.ctxs[0 ].ctx_id, mm->context.ctx_id);this_cpu_write (cpu_tlbstate.ctxs[0 ].tlb_gen, tlb_gen); +set_tlbstate_lam_cr3_mask (0 );for (i =1 ; i <TLB_NR_DYN_ASIDS ; i++)this_cpu_write (cpu_tlbstate.ctxs[i].ctx_id,0 );@@ -1047,8 +1083,10 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)*/ unsigned long __get_current_cr3_fast (void ) { -unsigned long cr3 =build_cr3 (this_cpu_read (cpu_tlbstate.loaded_mm)->pgd, -this_cpu_read (cpu_tlbstate.loaded_mm_asid)); +unsigned long cr3 = +build_cr3 (this_cpu_read (cpu_tlbstate.loaded_mm)->pgd, +this_cpu_read (cpu_tlbstate.loaded_mm_asid), +tlbstate_lam_cr3_mask ());/* For now, be very restrictive about when this can be called. */ VM_WARN_ON (in_nmi () ||preemptible ()); --2.35.1
Signature · 21 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Liana Sebastian Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Diese E-Mail ist vertraulich. Falls Sie diese fälschlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen, dass die E-Mail an die falsche Person gesendet wurde. This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.
PATCH v4 5/8 x86/uaccess: Provide untagged_addr() and remove tags before address check
In reply to Kirill A. Shutemovuntagged_addr() is a helper used by the core-mm to strip tag bits and get the address to the canonical shape. In only handles userspace addresses. The untagging mask is stored in mmu_context and will be set on enabling LAM for the process.
The tags must not be included into check whether it's okay to access the userspace address.
Strip tags in access_ok().
get_user() and put_user() don't use access_ok(), but check access against TASK_SIZE directly in assembly. Strip tags, before calling into the assembly helper.
--- arch/x86/include/asm/mmu.h | 1 + arch/x86/include/asm/mmu_context.h | 11 ++++++++ arch/x86/include/asm/uaccess.h | 42 +++++++++++++++++++++++++++--- arch/x86/kernel/process.c | 3 +++ 4 files changed, 54 insertions(+), 3 deletions(-)
arch/x86/include/asm/mmu.h +1 -0
@@ -41,6 +41,7 @@ typedef struct {#ifdef CONFIG_X86_64 unsigned short flags;u64 lam_cr3_mask; +u64 untag_mask;#endif struct mutex lock;
arch/x86/include/asm/mmu_context.h +11 -0
@@ -100,6 +100,12 @@ static inline u64 mm_cr3_lam_mask(struct mm_struct *mm)static inline void dup_lam (struct mm_struct *oldmm,struct mm_struct *mm) { mm->context.lam_cr3_mask = oldmm->context.lam_cr3_mask; + mm->context.untag_mask = oldmm->context.untag_mask; +} + +static inline void mm_reset_untag_mask (struct mm_struct *mm) +{ + mm->context.untag_mask =-1UL ; }#else
@@ -112,6 +118,10 @@ static inline u64 mm_cr3_lam_mask(struct mm_struct *mm)static inline void dup_lam (struct mm_struct *oldmm,struct mm_struct *mm) { } + +static inline void mm_reset_untag_mask (struct mm_struct *mm) +{ +}#endif #define enter_lazy_tlb enter_lazy_tlb
@@ -138,6 +148,7 @@ static inline int init_new_context(struct task_struct *tsk, mm->context.execute_only_pkey = -1; }#endif +mm_reset_untag_mask (mm);init_new_context_ldt (mm);return 0 ; }
arch/x86/include/asm/uaccess.h +39 -3
72 lines not on this page · raw message
arch/x86/kernel/process.c +3 -0
@@ -47,6 +47,7 @@#include <asm/frame.h> #include <asm/unwind.h> #include <asm/tdx.h> +#include <asm/mmu_context.h> #include "process.h"
@@ -367,6 +368,8 @@ void arch_setup_new_exec(void)task_clear_spec_ssb_noexec (current);speculation_ctrl_update (read_thread_flags ()); } + +mm_reset_untag_mask (current->mm); }#ifdef CONFIG_X86_IOPL_IOPERM
Signature · 1 line
2.35.1
PATCH v4 6/8 x86/mm: Provide ARCH_GET_UNTAG_MASK and ARCH_ENABLE_TAGGED_ADDR
In reply to Kirill A. ShutemovAdd a couple of arch_prctl() handles:
- ARCH_ENABLE_TAGGED_ADDR enabled LAM. The argument is required number of tag bits. It is rounded up to the nearest LAM mode that can provide it. For now only LAM_U57 is supported, with 6 tag bits.
- ARCH_GET_UNTAG_MASK returns untag mask. It can indicates where tag bits located in the address.
--- arch/x86/include/uapi/asm/prctl.h | 3 ++ arch/x86/kernel/process_64.c | 55 ++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-)
arch/x86/include/uapi/asm/prctl.h +3 -0
@@ -20,4 +20,7 @@#define ARCH_MAP_VDSO_32 0x2002#define ARCH_MAP_VDSO_64 0x2003 +#define ARCH_GET_UNTAG_MASK 0x4001 +#define ARCH_ENABLE_TAGGED_ADDR 0x4002 +#endif /* _ASM_X86_PRCTL_H */
arch/x86/kernel/process_64.c +54 -1
67 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCHv4 6/8] x86/mm: Provide ARCH_GET_UNTAG_MASK and ARCH_ENABLE_TAGGED_ADDR
Quoted 10 lines · Kirill A. Shutemov
Add a couple of
arch_prctl()handles:-
ARCH_ENABLE_TAGGED_ADDRenabled LAM. The argument is required number of tag bits. It is rounded up to the nearest LAM mode that can provide it. For now onlyLAM_U57is supported, with 6 tag bits.-
ARCH_GET_UNTAG_MASKreturns untag mask. It can indicates where tag bits located in the address.
Am I right that the desired way to detect the presence of LAM without enabling it is to check that arch_prctl(ARCH_GET_UNTAG_MASK, ...) returns zero?
Overall, I think these new arch_prctls should be documented following the spirit of PR_SET_TAGGED_ADDR_CTRL/PR_GET_TAGGED_ADDR_CTRL somewhere.
Quoted 8 lines
arch/x86/kernel/process_64.c +8
-0+ +static int prctl_enable_tagged_addr (struct mm_struct *mm,unsigned long nr_bits) +{ +int ret =0 ; + +if (!cpu_feature_enabled (X86_FEATURE_LAM )) +return -ENODEV ; +
arch/x86/kernel/process_64.c static long prctl_map_vdso(const struct vdso_image *image, unsigned long addr) · line 775
Quoted 10 lines
arch/x86/kernel/process_64.c +10
-0+mutex_lock (&mm->context.lock); + +/* Already enabled? */ +if (mm->context.lam_cr3_mask) { + ret = -EBUSY ; + goto out; + } + +if (!nr_bits) { + ret = -EINVAL ;
One would expect that arch_prctl(ARCH_ENABLE_TAGGED_ADDR, 0) disables tagging for the current process.
Shouldn't this workflow be supported as well?
Re: [PATCHv4 6/8] x86/mm: Provide ARCH_GET_UNTAG_MASK and ARCH_ENABLE_TAGGED_ADDR
Quoted 9 lines · Alexander Potapenko
Quoted 7 lines · Kirill A. Shutemov
Add a couple of
arch_prctl()handles:-
ARCH_ENABLE_TAGGED_ADDRenabled LAM. The argument is required number of tag bits. It is rounded up to the nearest LAM mode that can provide it. For now onlyLAM_U57is supported, with 6 tag bits.
Quoted 6 lines
Quoted 3 lines
-
ARCH_GET_UNTAG_MASKreturns untag mask. It can indicates where tag bits located in the address.Am I right that the desired way to detect the presence of LAM without enabling it is to check that
arch_prctl(ARCH_GET_UNTAG_MASK, ...)returns zero?
Returns -1UL, but yes.
Quoted 3 lines
Overall, I think these new
arch_prctlsshould be documented following the spirit of PR_SET_TAGGED_ADDR_CTRL/PR_GET_TAGGED_ADDR_CTRL somewhere.
The plan is to update man page for the syscall once the interface is upstream.
Quoted 14 lines
Quoted 14 lines
arch/x86/kernel/process_64.c +14
-0+ +static int prctl_enable_tagged_addr (struct mm_struct *mm,unsigned long nr_bits) +{ +int ret =0 ; + +if (!cpu_feature_enabled (X86_FEATURE_LAM )) +return -ENODEV ; + +mutex_lock (&mm->context.lock); + +/* Already enabled? */ +if (mm->context.lam_cr3_mask) { + ret = -EBUSY ; + goto out;
Quoted 8 lines
arch/x86/kernel/process_64.c static long prctl_map_vdso(const struct vdso_image *image, unsigned long addr) · line 775
Quoted 4 lines
arch/x86/kernel/process_64.c +4
-0+ } + +if (!nr_bits) { + ret = -EINVAL ;One would expect that
arch_prctl(ARCH_ENABLE_TAGGED_ADDR, 0)disables tagging for the current process.
Shouldn't this workflow be supported as well?
Is there an use-case for it?
I would rather keep the interface minimal. We can always add this in the future if an use-case comes.
Signature · 1 line
Kirill A. Shutemov
Re: [PATCHv4 6/8] x86/mm: Provide ARCH_GET_UNTAG_MASK and ARCH_ENABLE_TAGGED_ADDR
Quoted 11 lines · Kirill A. Shutemov
Quoted 9 lines · Alexander Potapenko
Quoted 7 lines · Kirill A. Shutemov
Add a couple of
arch_prctl()handles:-
ARCH_ENABLE_TAGGED_ADDRenabled LAM. The argument is required number of tag bits. It is rounded up to the nearest LAM mode that can provide it. For now onlyLAM_U57is supported, with 6 tag bits.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
-
ARCH_GET_UNTAG_MASKreturns untag mask. It can indicates where tag bits located in the address.Am I right that the desired way to detect the presence of LAM without enabling it is to check that
arch_prctl(ARCH_GET_UNTAG_MASK, ...)returns zero?Returns -1UL, but yes.
No, I meant the return value of arch_prctl(), but in fact neither seems to be true.
Right now e.g. for the 5.17 kernel arch_prctl(ARCH_GET_UNTAG_MASK, &bits) returns -EINVAL regardless of the underlying hardware. A new kernel with your patches will return 0 and set bits=-1UL on both non-LAM and LAM-enabled machines. How can we distinguish those?
Quoted 9 lines
Quoted 4 lines
One would expect that
arch_prctl(ARCH_ENABLE_TAGGED_ADDR, 0)disables tagging for the current process.
Shouldn't this workflow be supported as well?Is there an use-case for it?
I would rather keep the interface minimal. We can always add this in the future if an use-case comes.
As discussed offline, we don't have a use-case for this yet, so I don't insist.
Quoted 2 lines
--
Kirill A. Shutemov
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Liana Sebastian Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCHv4 6/8] x86/mm: Provide ARCH_GET_UNTAG_MASK and ARCH_ENABLE_TAGGED_ADDR
Quoted 22 lines · Alexander Potapenko
Quoted 11 lines · Kirill A. Shutemov
Quoted 9 lines · Alexander Potapenko
Quoted 7 lines · Kirill A. Shutemov
Add a couple of
arch_prctl()handles:-
ARCH_ENABLE_TAGGED_ADDRenabled LAM. The argument is required number of tag bits. It is rounded up to the nearest LAM mode that can provide it. For now onlyLAM_U57is supported, with 6 tag bits.Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
-
ARCH_GET_UNTAG_MASKreturns untag mask. It can indicates where tag bits located in the address.Am I right that the desired way to detect the presence of LAM without enabling it is to check that
arch_prctl(ARCH_GET_UNTAG_MASK, ...)returns zero?Returns -1UL, but yes.
Quoted 7 lines
No, I meant the return value of
arch_prctl(), but in fact neither seems to be true.Right now e.g. for the 5.17 kernel
arch_prctl(ARCH_GET_UNTAG_MASK, &bits)returns-EINVALregardless of the underlying hardware. A new kernel with your patches will return 0 and set bits=-1UL on both non-LAM and LAM-enabled machines. How can we distinguish those?
With CPUID?
Signature · 1 line
Kirill A. Shutemov
PATCH v4 7/8 x86: Expose untagging mask in /proc/$PID/arch_status
In reply to Kirill A. ShutemovAdd a line in /proc/$PID/arch_status to report untag_mask. It can be used to find out LAM status of the process from the outside. It is useful for debuggers.
--- arch/x86/include/asm/mmu_context.h | 10 +++++ arch/x86/kernel/Makefile | 2 + arch/x86/kernel/fpu/xstate.c | 47 ----------------------- arch/x86/kernel/proc.c | 60 ++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 arch/x86/kernel/proc.c
arch/x86/include/asm/mmu_context.h +10 -0
@@ -103,6 +103,11 @@ static inline void dup_lam(struct mm_struct *oldmm, struct mm_struct *mm) mm->context.untag_mask = oldmm->context.untag_mask; } +static inline unsigned long mm_untag_mask (struct mm_struct *mm) +{ +return mm->context.untag_mask; +} +static inline void mm_reset_untag_mask (struct mm_struct *mm) { mm->context.untag_mask =-1UL ;
@@ -119,6 +124,11 @@ static inline void dup_lam(struct mm_struct *oldmm, struct mm_struct *mm) { } +static inline unsigned long mm_untag_mask (struct mm_struct *mm) +{ +return -1UL ; +} +static inline void mm_reset_untag_mask (struct mm_struct *mm) { }
arch/x86/kernel/Makefile +2 -0
@@ -141,6 +141,8 @@ obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += sev.o +obj-$(CONFIG_PROC_FS) += proc.o + ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y)
arch/x86/kernel/fpu/xstate.c +0 -47
56 lines not on this page · raw message
arch/x86/kernel/proc.c new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v4 8/8 x86/mm: Extend LAM to support to LAM_U48
In reply to Kirill A. ShutemovLAM_U48 allows to encode 15 bits of tags into address.
LAM_U48 steals bits above 47-bit for tags and makes it impossible for userspace to use full address space on 5-level paging machine.
Make these features mutually exclusive: whichever gets enabled first blocks the other one.
--- arch/x86/include/asm/elf.h | 3 ++- arch/x86/include/asm/mmu_context.h | 13 +++++++++++++ arch/x86/kernel/process_64.c | 23 +++++++++++++++++++++++ arch/x86/kernel/sys_x86_64.c | 5 +++-- arch/x86/mm/hugetlbpage.c | 6 ++++-- arch/x86/mm/mmap.c | 9 ++++++++- 6 files changed, 53 insertions(+), 6 deletions(-)
arch/x86/include/asm/elf.h +2 -1
@@ -317,7 +317,8 @@ static inline int mmap_is_ia32(void)extern unsigned long task_size_32bit (void );extern unsigned long task_size_64bit (int full_addr_space);extern unsigned long get_mmap_base (int is_legacy); -extern bool mmap_address_hint_valid (unsigned long addr,unsigned long len); +extern bool mmap_address_hint_valid (struct mm_struct *mm, +unsigned long addr,unsigned long len);extern unsigned long get_sigframe_size (void );#ifdef CONFIG_X86_32
arch/x86/include/asm/mmu_context.h +13 -0
@@ -263,6 +263,19 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,unsigned long __get_current_cr3_fast (void ); +#ifdef CONFIG_X86_5LEVEL +static inline bool full_va_allowed (struct mm_struct *mm) +{ +/* LAM_U48 steals VA bits above 47-bit for tags */ +return mm->context.lam_cr3_mask !=X86_CR3_LAM_U48 ; +} +#else +static inline bool full_va_allowed (struct mm_struct *mm) +{ +return false; +} +#endif +#include <asm-generic/mmu_context.h> #endif /* _ASM_X86_MMU_CONTEXT_H */
arch/x86/kernel/process_64.c +23 -0
@@ -756,6 +756,16 @@ static void enable_lam_func(void *mm)switch_mm (loaded_mm, loaded_mm, current); } +static bool lam_u48_allowed (void ) +{ +struct mm_struct *mm = current->mm; + +if (!full_va_allowed (mm)) +return true; + +return find_vma (mm,DEFAULT_MAP_WINDOW ) ==NULL ; +} +static int prctl_enable_tagged_addr (struct mm_struct *mm,unsigned long nr_bits) {int ret =0 ;
@@ -763,6 +773,10 @@ static int prctl_enable_tagged_addr(struct mm_struct *mm, unsigned long nr_bits)if (!cpu_feature_enabled (X86_FEATURE_LAM ))return -ENODEV ; +/* lam_u48_allowed() requires mmap_lock */ +if (mmap_write_lock_killable (mm)) +return -EINTR ; +mutex_lock (&mm->context.lock);/* Already enabled? */
@@ -777,6 +791,14 @@ static int prctl_enable_tagged_addr(struct mm_struct *mm, unsigned long nr_bits) }else if (nr_bits <=6 ) { mm->context.lam_cr3_mask =X86_CR3_LAM_U57 ; mm->context.untag_mask = ~GENMASK (62 ,57 ); + }else if (nr_bits <=15 ) { +if (!lam_u48_allowed ()) { + ret = -EBUSY ; + goto out; + } + + mm->context.lam_cr3_mask =X86_CR3_LAM_U48 ; + mm->context.untag_mask = ~GENMASK (62 ,48 ); }else { ret = -EINVAL ; goto out;
@@ -788,6 +810,7 @@ static int prctl_enable_tagged_addr(struct mm_struct *mm, unsigned long nr_bits)on_each_cpu_mask (mm_cpumask (mm), enable_lam_func, mm, true); out:mutex_unlock (&mm->context.lock); +mmap_write_unlock (mm);return ret; }
arch/x86/kernel/sys_x86_64.c +3 -2
@@ -21,6 +21,7 @@#include <asm/elf.h> #include <asm/ia32.h> +#include <asm/mmu_context.h> /* * Align a virtual address to avoid aliasing in the I$ on AMD F15h.
@@ -182,7 +183,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,/* requesting a specific address */ if (addr) { addr &=PAGE_MASK ; -if (!mmap_address_hint_valid (addr, len)) +if (!mmap_address_hint_valid (mm, addr, len)) goto get_unmapped_area; vma =find_vma (mm, addr);
@@ -203,7 +204,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,* !in_32bit_syscall() check to avoid high addresses for x32 * (and make it no op on native i386). */ -if (addr >DEFAULT_MAP_WINDOW && !in_32bit_syscall ()) +if (addr >DEFAULT_MAP_WINDOW && !in_32bit_syscall () &&full_va_allowed (mm)) info.high_limit +=TASK_SIZE_MAX -DEFAULT_MAP_WINDOW ; info.align_mask =0 ;
arch/x86/mm/hugetlbpage.c +4 -2
@@ -18,6 +18,7 @@#include <asm/tlb.h> #include <asm/tlbflush.h> #include <asm/elf.h> +#include <asm/mmu_context.h> #if 0 /* This is just for testing */ struct page *
@@ -103,6 +104,7 @@ static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file, unsigned long pgoff, unsigned long flags) {struct hstate *h =hstate_file (file); +struct mm_struct *mm = current->mm;struct vm_unmapped_area_info info; info.flags =VM_UNMAPPED_AREA_TOPDOWN ;
@@ -114,7 +116,7 @@ static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,* If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area * in the full address space. */ -if (addr >DEFAULT_MAP_WINDOW && !in_32bit_syscall ()) +if (addr >DEFAULT_MAP_WINDOW && !in_32bit_syscall () &&full_va_allowed (mm)) info.high_limit +=TASK_SIZE_MAX -DEFAULT_MAP_WINDOW ; info.align_mask =PAGE_MASK & ~huge_page_mask (h);
@@ -161,7 +163,7 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,if (addr) { addr &=huge_page_mask (h); -if (!mmap_address_hint_valid (addr, len)) +if (!mmap_address_hint_valid (mm, addr, len)) goto get_unmapped_area; vma =find_vma (mm, addr);
arch/x86/mm/mmap.c +8 -1
@@ -21,6 +21,7 @@#include <linux/elf-randomize.h> #include <asm/elf.h> #include <asm/io.h> +#include <asm/mmu_context.h> #include "physaddr.h"
@@ -35,6 +36,8 @@ unsigned long task_size_32bit(void)unsigned long task_size_64bit (int full_addr_space) { +if (!full_va_allowed (current->mm)) +return DEFAULT_MAP_WINDOW ;return full_addr_space ?TASK_SIZE_MAX :DEFAULT_MAP_WINDOW ; }
@@ -206,11 +209,15 @@ const char *arch_vma_name(struct vm_area_struct *vma)* the failure of such a fixed mapping request, so the restriction is not * applied. */ -bool mmap_address_hint_valid (unsigned long addr,unsigned long len) +bool mmap_address_hint_valid (struct mm_struct *mm, +unsigned long addr,unsigned long len) {if (TASK_SIZE - len < addr)return false; +if (addr + len >DEFAULT_MAP_WINDOW && !full_va_allowed (mm)) +return false; +return (addr >DEFAULT_MAP_WINDOW ) == (addr + len >DEFAULT_MAP_WINDOW ); }
Signature · 1 line
2.35.1
Re: [PATCHv4 OPTIONAL 8/8] x86/mm: Extend LAM to support to LAM_U48
Quoted 13 lines · Kirill A. Shutemov
LAM_U48allows to encode 15 bits of tags into address.
LAM_U48steals bits above 47-bit for tags and makes it impossible for userspace to use full address space on 5-level paging machine.Make these features mutually exclusive: whichever gets enabled first blocks the other one.
Signed-off-by Kirill A. Shutemov <[email protected]>
arch/x86/mm/mmap.c const char *arch_vma_name(struct vm_area_struct *vma) · line 213
Quoted 6 lines
arch/x86/mm/mmap.c +2
-1@@ -206,11 +209,15 @@ const char *arch_vma_name(struct vm_area_struct *vma)* the failure of such a fixed mapping request, so the restriction is not * applied. */ -bool mmap_address_hint_valid (unsigned long addr,unsigned long len) +bool mmap_address_hint_valid (struct mm_struct *mm, +unsigned long addr,unsigned long len)
Please make sure to reflect the newly added parameter in the doc comment. This may also apply to other patches in the series.
Signature · 21 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Liana Sebastian Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg Diese E-Mail ist vertraulich. Falls Sie diese fälschlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen, dass die E-Mail an die falsche Person gesendet wurde. This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.