Skip to content

Commit 5baf8b0

Browse files
lorenzo-stoakesakpm00
authored andcommitted
mm: refactor arch_calc_vm_flag_bits() and arm64 MTE handling
Currently MTE is permitted in two circumstances (desiring to use MTE having been specified by the VM_MTE flag) - where MAP_ANONYMOUS is specified, as checked by arch_calc_vm_flag_bits() and actualised by setting the VM_MTE_ALLOWED flag, or if the file backing the mapping is shmem, in which case we set VM_MTE_ALLOWED in shmem_mmap() when the mmap hook is activated in mmap_region(). The function that checks that, if VM_MTE is set, VM_MTE_ALLOWED is also set is the arm64 implementation of arch_validate_flags(). Unfortunately, we intend to refactor mmap_region() to perform this check earlier, meaning that in the case of a shmem backing we will not have invoked shmem_mmap() yet, causing the mapping to fail spuriously. It is inappropriate to set this architecture-specific flag in general mm code anyway, so a sensible resolution of this issue is to instead move the check somewhere else. We resolve this by setting VM_MTE_ALLOWED much earlier in do_mmap(), via the arch_calc_vm_flag_bits() call. This is an appropriate place to do this as we already check for the MAP_ANONYMOUS case here, and the shmem file case is simply a variant of the same idea - we permit RAM-backed memory. This requires a modification to the arch_calc_vm_flag_bits() signature to pass in a pointer to the struct file associated with the mapping, however this is not too egregious as this is only used by two architectures anyway - arm64 and parisc. So this patch performs this adjustment and removes the unnecessary assignment of VM_MTE_ALLOWED in shmem_mmap(). [[email protected]: fix whitespace, per Catalin] Link: https://lkml.kernel.org/r/ec251b20ba1964fb64cf1607d2ad80c47f3873df.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f65 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") Signed-off-by: Lorenzo Stoakes <[email protected]> Suggested-by: Catalin Marinas <[email protected]> Reported-by: Jann Horn <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Reviewed-by: Vlastimil Babka <[email protected]> Cc: Andreas Larsson <[email protected]> Cc: David S. Miller <[email protected]> Cc: Helge Deller <[email protected]> Cc: James E.J. Bottomley <[email protected]> Cc: Liam R. Howlett <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Mark Brown <[email protected]> Cc: Peter Xu <[email protected]> Cc: Will Deacon <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 0fb4a7a commit 5baf8b0

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

arch/arm64/include/asm/mman.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#ifndef BUILD_VDSO
88
#include <linux/compiler.h>
9+
#include <linux/fs.h>
10+
#include <linux/shmem_fs.h>
911
#include <linux/types.h>
1012

1113
static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
@@ -31,19 +33,21 @@ static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
3133
}
3234
#define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey)
3335

34-
static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags)
36+
static inline unsigned long arch_calc_vm_flag_bits(struct file *file,
37+
unsigned long flags)
3538
{
3639
/*
3740
* Only allow MTE on anonymous mappings as these are guaranteed to be
3841
* backed by tags-capable memory. The vm_flags may be overridden by a
3942
* filesystem supporting MTE (RAM-based).
4043
*/
41-
if (system_supports_mte() && (flags & MAP_ANONYMOUS))
44+
if (system_supports_mte() &&
45+
((flags & MAP_ANONYMOUS) || shmem_file(file)))
4246
return VM_MTE_ALLOWED;
4347

4448
return 0;
4549
}
46-
#define arch_calc_vm_flag_bits(flags) arch_calc_vm_flag_bits(flags)
50+
#define arch_calc_vm_flag_bits(file, flags) arch_calc_vm_flag_bits(file, flags)
4751

4852
static inline bool arch_validate_prot(unsigned long prot,
4953
unsigned long addr __always_unused)

arch/parisc/include/asm/mman.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef __ASM_MMAN_H__
33
#define __ASM_MMAN_H__
44

5+
#include <linux/fs.h>
56
#include <uapi/asm/mman.h>
67

78
/* PARISC cannot allow mdwe as it needs writable stacks */
@@ -11,7 +12,7 @@ static inline bool arch_memory_deny_write_exec_supported(void)
1112
}
1213
#define arch_memory_deny_write_exec_supported arch_memory_deny_write_exec_supported
1314

14-
static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags)
15+
static inline unsigned long arch_calc_vm_flag_bits(struct file *file, unsigned long flags)
1516
{
1617
/*
1718
* The stack on parisc grows upwards, so if userspace requests memory
@@ -23,6 +24,6 @@ static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags)
2324

2425
return 0;
2526
}
26-
#define arch_calc_vm_flag_bits(flags) arch_calc_vm_flag_bits(flags)
27+
#define arch_calc_vm_flag_bits(file, flags) arch_calc_vm_flag_bits(file, flags)
2728

2829
#endif /* __ASM_MMAN_H__ */

include/linux/mman.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef _LINUX_MMAN_H
33
#define _LINUX_MMAN_H
44

5+
#include <linux/fs.h>
56
#include <linux/mm.h>
67
#include <linux/percpu_counter.h>
78

@@ -94,7 +95,7 @@ static inline void vm_unacct_memory(long pages)
9495
#endif
9596

9697
#ifndef arch_calc_vm_flag_bits
97-
#define arch_calc_vm_flag_bits(flags) 0
98+
#define arch_calc_vm_flag_bits(file, flags) 0
9899
#endif
99100

100101
#ifndef arch_validate_prot
@@ -151,13 +152,13 @@ calc_vm_prot_bits(unsigned long prot, unsigned long pkey)
151152
* Combine the mmap "flags" argument into "vm_flags" used internally.
152153
*/
153154
static inline unsigned long
154-
calc_vm_flag_bits(unsigned long flags)
155+
calc_vm_flag_bits(struct file *file, unsigned long flags)
155156
{
156157
return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) |
157158
_calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED ) |
158159
_calc_vm_trans(flags, MAP_SYNC, VM_SYNC ) |
159160
_calc_vm_trans(flags, MAP_STACK, VM_NOHUGEPAGE) |
160-
arch_calc_vm_flag_bits(flags);
161+
arch_calc_vm_flag_bits(file, flags);
161162
}
162163

163164
unsigned long vm_commit_limit(void);

mm/mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
344344
* to. we assume access permissions have been handled by the open
345345
* of the memory object, so we don't do any here.
346346
*/
347-
vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) |
347+
vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(file, flags) |
348348
mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
349349

350350
/* Obtain the address to map to. we verify (or select) it and ensure

mm/nommu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ static unsigned long determine_vm_flags(struct file *file,
842842
{
843843
unsigned long vm_flags;
844844

845-
vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
845+
vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(file, flags);
846846

847847
if (!file) {
848848
/*

mm/shmem.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,9 +2733,6 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
27332733
if (ret)
27342734
return ret;
27352735

2736-
/* arm64 - allow memory tagging on RAM-based files */
2737-
vm_flags_set(vma, VM_MTE_ALLOWED);
2738-
27392736
file_accessed(file);
27402737
/* This is anonymous shared memory if it is unlinked at the time of mmap */
27412738
if (inode->i_nlink)

0 commit comments

Comments
 (0)