/*
  * Memory types available.
+ *
+ * IMPORTANT: MT_NORMAL must be index 0 since vm_get_page_prot() may 'or' in
+ *           the MT_NORMAL_TAGGED memory type for PROT_MTE mappings. Note
+ *           that protection_map[] only contains MT_NORMAL attributes.
  */
-#define MT_DEVICE_nGnRnE       0
-#define MT_DEVICE_nGnRE                1
-#define MT_DEVICE_GRE          2
-#define MT_NORMAL_NC           3
-#define MT_NORMAL              4
-#define MT_NORMAL_WT           5
-#define MT_NORMAL_TAGGED       6
+#define MT_NORMAL              0
+#define MT_NORMAL_TAGGED       1
+#define MT_NORMAL_NC           2
+#define MT_NORMAL_WT           3
+#define MT_DEVICE_nGnRnE       4
+#define MT_DEVICE_nGnRE                5
+#define MT_DEVICE_GRE          6
 
 /*
  * Memory types for Stage-2 translation
 
 static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
        unsigned long pkey __always_unused)
 {
+       unsigned long ret = 0;
+
        if (system_supports_bti() && (prot & PROT_BTI))
-               return VM_ARM64_BTI;
+               ret |= VM_ARM64_BTI;
 
-       return 0;
+       if (system_supports_mte() && (prot & PROT_MTE))
+               ret |= VM_MTE;
+
+       return ret;
 }
 #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey)
 
+static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags)
+{
+       /*
+        * Only allow MTE on anonymous mappings as these are guaranteed to be
+        * backed by tags-capable memory. The vm_flags may be overridden by a
+        * filesystem supporting MTE (RAM-based).
+        */
+       if (system_supports_mte() && (flags & MAP_ANONYMOUS))
+               return VM_MTE_ALLOWED;
+
+       return 0;
+}
+#define arch_calc_vm_flag_bits(flags) arch_calc_vm_flag_bits(flags)
+
 static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags)
 {
-       return (vm_flags & VM_ARM64_BTI) ? __pgprot(PTE_GP) : __pgprot(0);
+       pteval_t prot = 0;
+
+       if (vm_flags & VM_ARM64_BTI)
+               prot |= PTE_GP;
+
+       /*
+        * There are two conditions required for returning a Normal Tagged
+        * memory type: (1) the user requested it via PROT_MTE passed to
+        * mmap() or mprotect() and (2) the corresponding vma supports MTE. We
+        * register (1) as VM_MTE in the vma->vm_flags and (2) as
+        * VM_MTE_ALLOWED. Note that the latter can only be set during the
+        * mmap() call since mprotect() does not accept MAP_* flags.
+        */
+       if ((vm_flags & VM_MTE) && (vm_flags & VM_MTE_ALLOWED))
+               prot |= PTE_ATTRINDX(MT_NORMAL_TAGGED);
+
+       return __pgprot(prot);
 }
 #define arch_vm_get_page_prot(vm_flags) arch_vm_get_page_prot(vm_flags)
 
        if (system_supports_bti())
                supported |= PROT_BTI;
 
+       if (system_supports_mte())
+               supported |= PROT_MTE;
+
        return (prot & ~supported) == 0;
 }
 #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
 
 
 #endif /* !__ASSEMBLY__ */
 
-#define VM_DATA_DEFAULT_FLAGS  VM_DATA_FLAGS_TSK_EXEC
+#define VM_DATA_DEFAULT_FLAGS  (VM_DATA_FLAGS_TSK_EXEC | VM_MTE_ALLOWED)
 
 #include <asm-generic/getorder.h>
 
 
 
 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
 {
+       /*
+        * Normal and Normal-Tagged are two different memory types and indices
+        * in MAIR_EL1. The mask below has to include PTE_ATTRINDX_MASK.
+        */
        const pteval_t mask = PTE_USER | PTE_PXN | PTE_UXN | PTE_RDONLY |
-                             PTE_PROT_NONE | PTE_VALID | PTE_WRITE | PTE_GP;
+                             PTE_PROT_NONE | PTE_VALID | PTE_WRITE | PTE_GP |
+                             PTE_ATTRINDX_MASK;
        /* preserve the hardware dirty information */
        if (pte_hw_dirty(pte))
                pte = pte_mkdirty(pte);
 
 #include <asm-generic/mman.h>
 
 #define PROT_BTI       0x10            /* BTI guarded page */
+#define PROT_MTE       0x20            /* Normal Tagged mapping */
 
 #endif /* ! _UAPI__ASM_MMAN_H */
 
                [ilog2(VM_MERGEABLE)]   = "mg",
                [ilog2(VM_UFFD_MISSING)]= "um",
                [ilog2(VM_UFFD_WP)]     = "uw",
+#ifdef CONFIG_ARM64_MTE
+               [ilog2(VM_MTE)]         = "mt",
+               [ilog2(VM_MTE_ALLOWED)] = "",
+#endif
 #ifdef CONFIG_ARCH_HAS_PKEYS
                /* These come out via ProtectionKey: */
                [ilog2(VM_PKEY_BIT0)]   = "",
 
 # define VM_MAPPED_COPY        VM_ARCH_1       /* T if mapped copy of data (nommu mmap) */
 #endif
 
+#if defined(CONFIG_ARM64_MTE)
+# define VM_MTE                VM_HIGH_ARCH_0  /* Use Tagged memory for access control */
+# define VM_MTE_ALLOWED        VM_HIGH_ARCH_1  /* Tagged memory permitted */
+#else
+# define VM_MTE                VM_NONE
+# define VM_MTE_ALLOWED        VM_NONE
+#endif
+
 #ifndef VM_GROWSUP
 # define VM_GROWSUP    VM_NONE
 #endif