* [PATCH hotfix 6.12 v3 0/5] fix error handling in mmap_region() and refactor (hotfixes)
@ 2024-10-29 13:03 Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 1/5] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Lorenzo Stoakes
` (4 more replies)
0 siblings, 5 replies; 12+ messages in thread
From: Lorenzo Stoakes @ 2024-10-29 13:03 UTC (permalink / raw)
To: Andrew Morton
Cc: Liam R . Howlett, Vlastimil Babka, Jann Horn, linux-kernel,
linux-mm, Linus Torvalds, Peter Xu, Catalin Marinas, Will Deacon,
Mark Brown, David S . Miller, Andreas Larsson
NOTE: This should be applied on mm-hotfixes-unstable in Andrew's mm tree as
it relies on other pending hotfixes.
The mmap_region() function is somewhat terrifying, with spaghetti-like
control flow and numerous means by which issues can arise and incomplete
state, memory leaks and other unpleasantness can occur.
A large amount of the complexity arises from trying to handle errors late
in the process of mapping a VMA, which forms the basis of recently observed
issues with resource leaks and observable inconsistent state.
This series goes to great lengths to simplify how mmap_region() works and
to avoid unwinding errors late on in the process of setting up the VMA for
the new mapping, and equally avoids such operations occurring while the VMA
is in an inconsistent state.
The patches in this series comprise the minimal changes required to resolve
existing issues in mmap_region() error handling, in order that they can be
hotfixed and backported. There is additionally a follow up series which
goes further, separated out from the v1 series and sent and updated
separately.
v3:
* Added correct handling for arm64 MTE which was otherwise broken, as
reported by Mark Brown.
v2:
* Marked first 4 patches as hotfixes, the rest as not.
* Improved comment in vma_close() as per Vlastimil.
* Updated hole byte count as per Jann.
* Updated comment in map_deny_write_exec() as per Jann.
* Dropped unnecessary vma_iter_free() as per Vlastimil, Liam.
* Corrected vms_abort_munmap_vmas() mistaken assumption about nr_pages as
per Vlastimil.
* Changed order of initial checks in mmap_region() to avoid user-visible
side effects as per Vlastimil, Liam.
* Corrected silly incorrect use of vma field.
* Various style corrects as per Liam.
* Fix horrid mistake with merge VMA, reworked the logic to avoid that
nonsense altogether.
* Add fields to map state rather than using vmg fields to avoid
confusion/risk of vmg state changing breaking things.
* Replaced last commit removing merge retry with one that retries the
merge, only sanely.
https://lore.kernel.org/all/cover.1729715266.git.lorenzo.stoakes@oracle.com/
v1:
https://lore.kernel.org/all/cover.1729628198.git.lorenzo.stoakes@oracle.com/
Lorenzo Stoakes (5):
mm: avoid unsafe VMA hook invocation when error arises on mmap hook
mm: unconditionally close VMAs on error
mm: refactor map_deny_write_exec()
mm: refactor arch_validate_flags() and arm64 MTE handling
mm: resolve faulty mmap_region() error path behaviour
arch/arm64/include/asm/mman.h | 29 ++++++--
arch/sparc/include/asm/mman.h | 5 +-
include/linux/mman.h | 23 ++++--
mm/internal.h | 45 ++++++++++++
mm/mmap.c | 128 ++++++++++++++++++----------------
mm/mprotect.c | 4 +-
mm/nommu.c | 7 +-
mm/shmem.c | 3 -
mm/vma.c | 14 ++--
mm/vma.h | 6 +-
10 files changed, 173 insertions(+), 91 deletions(-)
--
2.47.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH hotfix 6.12 v3 1/5] mm: avoid unsafe VMA hook invocation when error arises on mmap hook
2024-10-29 13:03 [PATCH hotfix 6.12 v3 0/5] fix error handling in mmap_region() and refactor (hotfixes) Lorenzo Stoakes
@ 2024-10-29 13:03 ` Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 2/5] mm: unconditionally close VMAs on error Lorenzo Stoakes
` (3 subsequent siblings)
4 siblings, 0 replies; 12+ messages in thread
From: Lorenzo Stoakes @ 2024-10-29 13:03 UTC (permalink / raw)
To: Andrew Morton
Cc: Liam R . Howlett, Vlastimil Babka, Jann Horn, linux-kernel,
linux-mm, Linus Torvalds, Peter Xu, Catalin Marinas, Will Deacon,
Mark Brown, David S . Miller, Andreas Larsson
After an attempted mmap() fails, we are no longer in a situation where we
can safely interact with VMA hooks. This is currently not enforced, meaning
that we need complicated handling to ensure we do not incorrectly call
these hooks.
We can avoid the whole issue by treating the VMA as suspect the moment that
the file->f_ops->mmap() function reports an error by replacing whatever VMA
operations were installed with a dummy empty set of VMA operations.
We do so through a new helper function internal to mm - mmap_file() - which
is both more logically named than the existing call_mmap() function and
correctly isolates handling of the vm_op reassignment to mm.
All the existing invocations of call_mmap() outside of mm are ultimately
nested within the call_mmap() from mm, which we now replace.
It is therefore safe to leave call_mmap() in place as a convenience
function (and to avoid churn). The invokers are:
ovl_file_operations -> mmap -> ovl_mmap() -> backing_file_mmap()
coda_file_operations -> mmap -> coda_file_mmap()
shm_file_operations -> shm_mmap()
shm_file_operations_huge -> shm_mmap()
dma_buf_fops -> dma_buf_mmap_internal -> i915_dmabuf_ops
-> i915_gem_dmabuf_mmap()
None of these callers interact with vm_ops or mappings in a problematic way
on error, quickly exiting out.
Reported-by: Jann Horn <jannh@google.com>
Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
Cc: stable <stable@kernel.org>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Jann Horn <jannh@google.com>
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
mm/internal.h | 27 +++++++++++++++++++++++++++
mm/mmap.c | 6 +++---
mm/nommu.c | 4 ++--
3 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/mm/internal.h b/mm/internal.h
index 16c1f3cd599e..4eab2961e69c 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -108,6 +108,33 @@ static inline void *folio_raw_mapping(const struct folio *folio)
return (void *)(mapping & ~PAGE_MAPPING_FLAGS);
}
+/*
+ * This is a file-backed mapping, and is about to be memory mapped - invoke its
+ * mmap hook and safely handle error conditions. On error, VMA hooks will be
+ * mutated.
+ *
+ * @file: File which backs the mapping.
+ * @vma: VMA which we are mapping.
+ *
+ * Returns: 0 if success, error otherwise.
+ */
+static inline int mmap_file(struct file *file, struct vm_area_struct *vma)
+{
+ int err = call_mmap(file, vma);
+
+ if (likely(!err))
+ return 0;
+
+ /*
+ * OK, we tried to call the file hook for mmap(), but an error
+ * arose. The mapping is in an inconsistent state and we most not invoke
+ * any further hooks on it.
+ */
+ vma->vm_ops = &vma_dummy_vm_ops;
+
+ return err;
+}
+
#ifdef CONFIG_MMU
/* Flags for folio_pte_batch(). */
diff --git a/mm/mmap.c b/mm/mmap.c
index 9841b41e3c76..6e3b25f7728f 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1422,7 +1422,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
/*
* clear PTEs while the vma is still in the tree so that rmap
* cannot race with the freeing later in the truncate scenario.
- * This is also needed for call_mmap(), which is why vm_ops
+ * This is also needed for mmap_file(), which is why vm_ops
* close function is called.
*/
vms_clean_up_area(&vms, &mas_detach);
@@ -1447,7 +1447,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
if (file) {
vma->vm_file = get_file(file);
- error = call_mmap(file, vma);
+ error = mmap_file(file, vma);
if (error)
goto unmap_and_free_vma;
@@ -1470,7 +1470,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
vma_iter_config(&vmi, addr, end);
/*
- * If vm_flags changed after call_mmap(), we should try merge
+ * If vm_flags changed after mmap_file(), we should try merge
* vma again as we may succeed this time.
*/
if (unlikely(vm_flags != vma->vm_flags && vmg.prev)) {
diff --git a/mm/nommu.c b/mm/nommu.c
index 385b0c15add8..f9ccc02458ec 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -885,7 +885,7 @@ static int do_mmap_shared_file(struct vm_area_struct *vma)
{
int ret;
- ret = call_mmap(vma->vm_file, vma);
+ ret = mmap_file(vma->vm_file, vma);
if (ret == 0) {
vma->vm_region->vm_top = vma->vm_region->vm_end;
return 0;
@@ -918,7 +918,7 @@ static int do_mmap_private(struct vm_area_struct *vma,
* happy.
*/
if (capabilities & NOMMU_MAP_DIRECT) {
- ret = call_mmap(vma->vm_file, vma);
+ ret = mmap_file(vma->vm_file, vma);
/* shouldn't return success if we're not sharing */
if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags)))
ret = -ENOSYS;
--
2.47.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH hotfix 6.12 v3 2/5] mm: unconditionally close VMAs on error
2024-10-29 13:03 [PATCH hotfix 6.12 v3 0/5] fix error handling in mmap_region() and refactor (hotfixes) Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 1/5] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Lorenzo Stoakes
@ 2024-10-29 13:03 ` Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 3/5] mm: refactor map_deny_write_exec() Lorenzo Stoakes
` (2 subsequent siblings)
4 siblings, 0 replies; 12+ messages in thread
From: Lorenzo Stoakes @ 2024-10-29 13:03 UTC (permalink / raw)
To: Andrew Morton
Cc: Liam R . Howlett, Vlastimil Babka, Jann Horn, linux-kernel,
linux-mm, Linus Torvalds, Peter Xu, Catalin Marinas, Will Deacon,
Mark Brown, David S . Miller, Andreas Larsson
Incorrect invocation of VMA callbacks when the VMA is no longer in a
consistent state is bug prone and risky to perform.
With regards to the important vm_ops->close() callback We have gone to
great lengths to try to track whether or not we ought to close VMAs.
Rather than doing so and risking making a mistake somewhere, instead
unconditionally close and reset vma->vm_ops to an empty dummy operations
set with a NULL .close operator.
We introduce a new function to do so - vma_close() - and simplify existing
vms logic which tracked whether we needed to close or not.
This simplifies the logic, avoids incorrect double-calling of the .close()
callback and allows us to update error paths to simply call vma_close()
unconditionally - making VMA closure idempotent.
Reported-by: Jann Horn <jannh@google.com>
Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
Cc: stable <stable@kernel.org>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Jann Horn <jannh@google.com>
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
mm/internal.h | 18 ++++++++++++++++++
mm/mmap.c | 5 ++---
mm/nommu.c | 3 +--
mm/vma.c | 14 +++++---------
mm/vma.h | 4 +---
5 files changed, 27 insertions(+), 17 deletions(-)
diff --git a/mm/internal.h b/mm/internal.h
index 4eab2961e69c..64c2eb0b160e 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -135,6 +135,24 @@ static inline int mmap_file(struct file *file, struct vm_area_struct *vma)
return err;
}
+/*
+ * If the VMA has a close hook then close it, and since closing it might leave
+ * it in an inconsistent state which makes the use of any hooks suspect, clear
+ * them down by installing dummy empty hooks.
+ */
+static inline void vma_close(struct vm_area_struct *vma)
+{
+ if (vma->vm_ops && vma->vm_ops->close) {
+ vma->vm_ops->close(vma);
+
+ /*
+ * The mapping is in an inconsistent state, and no further hooks
+ * may be invoked upon it.
+ */
+ vma->vm_ops = &vma_dummy_vm_ops;
+ }
+}
+
#ifdef CONFIG_MMU
/* Flags for folio_pte_batch(). */
diff --git a/mm/mmap.c b/mm/mmap.c
index 6e3b25f7728f..ac0604f146f6 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1573,8 +1573,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
return addr;
close_and_free_vma:
- if (file && !vms.closed_vm_ops && vma->vm_ops && vma->vm_ops->close)
- vma->vm_ops->close(vma);
+ vma_close(vma);
if (file || vma->vm_file) {
unmap_and_free_vma:
@@ -1934,7 +1933,7 @@ void exit_mmap(struct mm_struct *mm)
do {
if (vma->vm_flags & VM_ACCOUNT)
nr_accounted += vma_pages(vma);
- remove_vma(vma, /* unreachable = */ true, /* closed = */ false);
+ remove_vma(vma, /* unreachable = */ true);
count++;
cond_resched();
vma = vma_next(&vmi);
diff --git a/mm/nommu.c b/mm/nommu.c
index f9ccc02458ec..635d028d647b 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -589,8 +589,7 @@ static int delete_vma_from_mm(struct vm_area_struct *vma)
*/
static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
{
- if (vma->vm_ops && vma->vm_ops->close)
- vma->vm_ops->close(vma);
+ vma_close(vma);
if (vma->vm_file)
fput(vma->vm_file);
put_nommu_region(vma->vm_region);
diff --git a/mm/vma.c b/mm/vma.c
index b21ffec33f8e..7621384d64cf 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -323,11 +323,10 @@ static bool can_vma_merge_right(struct vma_merge_struct *vmg,
/*
* Close a vm structure and free it.
*/
-void remove_vma(struct vm_area_struct *vma, bool unreachable, bool closed)
+void remove_vma(struct vm_area_struct *vma, bool unreachable)
{
might_sleep();
- if (!closed && vma->vm_ops && vma->vm_ops->close)
- vma->vm_ops->close(vma);
+ vma_close(vma);
if (vma->vm_file)
fput(vma->vm_file);
mpol_put(vma_policy(vma));
@@ -1115,9 +1114,7 @@ void vms_clean_up_area(struct vma_munmap_struct *vms,
vms_clear_ptes(vms, mas_detach, true);
mas_set(mas_detach, 0);
mas_for_each(mas_detach, vma, ULONG_MAX)
- if (vma->vm_ops && vma->vm_ops->close)
- vma->vm_ops->close(vma);
- vms->closed_vm_ops = true;
+ vma_close(vma);
}
/*
@@ -1160,7 +1157,7 @@ void vms_complete_munmap_vmas(struct vma_munmap_struct *vms,
/* Remove and clean up vmas */
mas_set(mas_detach, 0);
mas_for_each(mas_detach, vma, ULONG_MAX)
- remove_vma(vma, /* = */ false, vms->closed_vm_ops);
+ remove_vma(vma, /* unreachable = */ false);
vm_unacct_memory(vms->nr_accounted);
validate_mm(mm);
@@ -1684,8 +1681,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
return new_vma;
out_vma_link:
- if (new_vma->vm_ops && new_vma->vm_ops->close)
- new_vma->vm_ops->close(new_vma);
+ vma_close(new_vma);
if (new_vma->vm_file)
fput(new_vma->vm_file);
diff --git a/mm/vma.h b/mm/vma.h
index 55457cb68200..75558b5e9c8c 100644
--- a/mm/vma.h
+++ b/mm/vma.h
@@ -42,7 +42,6 @@ struct vma_munmap_struct {
int vma_count; /* Number of vmas that will be removed */
bool unlock; /* Unlock after the munmap */
bool clear_ptes; /* If there are outstanding PTE to be cleared */
- bool closed_vm_ops; /* call_mmap() was encountered, so vmas may be closed */
/* 1 byte hole */
unsigned long nr_pages; /* Number of pages being removed */
unsigned long locked_vm; /* Number of locked pages */
@@ -198,7 +197,6 @@ static inline void init_vma_munmap(struct vma_munmap_struct *vms,
vms->unmap_start = FIRST_USER_ADDRESS;
vms->unmap_end = USER_PGTABLES_CEILING;
vms->clear_ptes = false;
- vms->closed_vm_ops = false;
}
#endif
@@ -269,7 +267,7 @@ int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm,
unsigned long start, size_t len, struct list_head *uf,
bool unlock);
-void remove_vma(struct vm_area_struct *vma, bool unreachable, bool closed);
+void remove_vma(struct vm_area_struct *vma, bool unreachable);
void unmap_region(struct ma_state *mas, struct vm_area_struct *vma,
struct vm_area_struct *prev, struct vm_area_struct *next);
--
2.47.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH hotfix 6.12 v3 3/5] mm: refactor map_deny_write_exec()
2024-10-29 13:03 [PATCH hotfix 6.12 v3 0/5] fix error handling in mmap_region() and refactor (hotfixes) Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 1/5] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 2/5] mm: unconditionally close VMAs on error Lorenzo Stoakes
@ 2024-10-29 13:03 ` Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 5/5] mm: resolve faulty mmap_region() error path behaviour Lorenzo Stoakes
4 siblings, 0 replies; 12+ messages in thread
From: Lorenzo Stoakes @ 2024-10-29 13:03 UTC (permalink / raw)
To: Andrew Morton
Cc: Liam R . Howlett, Vlastimil Babka, Jann Horn, linux-kernel,
linux-mm, Linus Torvalds, Peter Xu, Catalin Marinas, Will Deacon,
Mark Brown, David S . Miller, Andreas Larsson
Refactor the map_deny_write_exec() to not unnecessarily require a VMA
parameter but rather to accept VMA flags parameters, which allows us to use
this function early in mmap_region() in a subsequent commit.
While we're here, we refactor the function to be more readable and add some
additional documentation.
Reported-by: Jann Horn <jannh@google.com>
Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
Cc: stable <stable@kernel.org>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Jann Horn <jannh@google.com>
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
include/linux/mman.h | 21 ++++++++++++++++++---
mm/mmap.c | 2 +-
mm/mprotect.c | 2 +-
mm/vma.h | 2 +-
4 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/include/linux/mman.h b/include/linux/mman.h
index bcb201ab7a41..8ddca62d6460 100644
--- a/include/linux/mman.h
+++ b/include/linux/mman.h
@@ -188,16 +188,31 @@ static inline bool arch_memory_deny_write_exec_supported(void)
*
* d) mmap(PROT_READ | PROT_EXEC)
* mmap(PROT_READ | PROT_EXEC | PROT_BTI)
+ *
+ * This is only applicable if the user has set the Memory-Deny-Write-Execute
+ * (MDWE) protection mask for the current process.
+ *
+ * @old specifies the VMA flags the VMA originally possessed, and @new the ones
+ * we propose to set.
+ *
+ * Return: false if proposed change is OK, true if not ok and should be denied.
*/
-static inline bool map_deny_write_exec(struct vm_area_struct *vma, unsigned long vm_flags)
+static inline bool map_deny_write_exec(unsigned long old, unsigned long new)
{
+ /* If MDWE is disabled, we have nothing to deny. */
if (!test_bit(MMF_HAS_MDWE, ¤t->mm->flags))
return false;
- if ((vm_flags & VM_EXEC) && (vm_flags & VM_WRITE))
+ /* If the new VMA is not executable, we have nothing to deny. */
+ if (!(new & VM_EXEC))
+ return false;
+
+ /* Under MDWE we do not accept newly writably executable VMAs... */
+ if (new & VM_WRITE)
return true;
- if (!(vma->vm_flags & VM_EXEC) && (vm_flags & VM_EXEC))
+ /* ...nor previously non-executable VMAs becoming executable. */
+ if (!(old & VM_EXEC))
return true;
return false;
diff --git a/mm/mmap.c b/mm/mmap.c
index ac0604f146f6..ab71d4c3464c 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1505,7 +1505,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
vma_set_anonymous(vma);
}
- if (map_deny_write_exec(vma, vma->vm_flags)) {
+ if (map_deny_write_exec(vma->vm_flags, vma->vm_flags)) {
error = -EACCES;
goto close_and_free_vma;
}
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 0c5d6d06107d..6f450af3252e 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -810,7 +810,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
break;
}
- if (map_deny_write_exec(vma, newflags)) {
+ if (map_deny_write_exec(vma->vm_flags, newflags)) {
error = -EACCES;
break;
}
diff --git a/mm/vma.h b/mm/vma.h
index 75558b5e9c8c..d58068c0ff2e 100644
--- a/mm/vma.h
+++ b/mm/vma.h
@@ -42,7 +42,7 @@ struct vma_munmap_struct {
int vma_count; /* Number of vmas that will be removed */
bool unlock; /* Unlock after the munmap */
bool clear_ptes; /* If there are outstanding PTE to be cleared */
- /* 1 byte hole */
+ /* 2 byte hole */
unsigned long nr_pages; /* Number of pages being removed */
unsigned long locked_vm; /* Number of locked pages */
unsigned long nr_accounted; /* Number of VM_ACCOUNT pages */
--
2.47.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling
2024-10-29 13:03 [PATCH hotfix 6.12 v3 0/5] fix error handling in mmap_region() and refactor (hotfixes) Lorenzo Stoakes
` (2 preceding siblings ...)
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 3/5] mm: refactor map_deny_write_exec() Lorenzo Stoakes
@ 2024-10-29 13:03 ` Lorenzo Stoakes
2024-10-29 14:38 ` Liam R. Howlett
` (2 more replies)
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 5/5] mm: resolve faulty mmap_region() error path behaviour Lorenzo Stoakes
4 siblings, 3 replies; 12+ messages in thread
From: Lorenzo Stoakes @ 2024-10-29 13:03 UTC (permalink / raw)
To: Andrew Morton
Cc: Liam R . Howlett, Vlastimil Babka, Jann Horn, linux-kernel,
linux-mm, Linus Torvalds, Peter Xu, Catalin Marinas, Will Deacon,
Mark Brown, David S . Miller, Andreas Larsson
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 to arch_validate_flags() itself.
This requires a modification to the arch_validate_flags() 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 sparc.
So this patch performs this adjustment and moves the check to
arch_validate_flags() which resolves the issue.
We'd ideally like to have eliminated the arch_calc_vm_flag_bits() case, but
we risk inadvertently changing behaviour as we do not have mmap() flags
available at the point of the arch_validate_flags() check and a MAP_ANON |
MAP_HUGETLB case would be accepted for MTE currently, but a MAP_SHARED |
MAP_HUGETLB would not.
This is likely an oversight but we want to try to keep behaviour identical
to before in this patch.
So continue to check VM_MTE_ALLOWED which arch_calc_vm_flag_bits() sets if
MAP_ANON.
Reported-by: Jann Horn <jannh@google.com>
Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
Cc: stable <stable@kernel.org>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Tested-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
arch/arm64/include/asm/mman.h | 29 +++++++++++++++++++++++++----
arch/sparc/include/asm/mman.h | 5 +++--
include/linux/mman.h | 2 +-
mm/mmap.c | 2 +-
mm/mprotect.c | 2 +-
mm/shmem.c | 3 ---
6 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h
index 9e39217b4afb..94925a0482e3 100644
--- a/arch/arm64/include/asm/mman.h
+++ b/arch/arm64/include/asm/mman.h
@@ -6,7 +6,9 @@
#ifndef BUILD_VDSO
#include <linux/compiler.h>
+#include <linux/fs.h>
#include <linux/types.h>
+#include <linux/shmem_fs.h>
static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
unsigned long pkey)
@@ -60,15 +62,34 @@ static inline bool arch_validate_prot(unsigned long prot,
}
#define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
-static inline bool arch_validate_flags(unsigned long vm_flags)
+static inline bool arch_validate_flags(struct file *file, unsigned long vm_flags)
{
if (!system_supports_mte())
return true;
- /* only allow VM_MTE if VM_MTE_ALLOWED has been set previously */
- return !(vm_flags & VM_MTE) || (vm_flags & VM_MTE_ALLOWED);
+ if (!(vm_flags & VM_MTE))
+ return true;
+
+ /*
+ * We explicitly permit MAP_ANONYMOUS and shmem backed mappings to use
+ * MTE. We check for shmem explicitly here.
+ *
+ * The MAP_ANONYMOUS case is handled by arch_calc_vm_flag_bits() which
+ * explicitly sets VM_MTE_ALLOWED which we check for here also.
+ *
+ * Ideally we'd perform both checks here but existing semantics support
+ * hugetlb in MAP_ANONYMOUS mode but not a MAP_SHARED mapping, which is
+ * likely unintended but we maintain identical behaviour for
+ * consistency.
+ */
+ if (vm_flags & VM_MTE_ALLOWED)
+ return true;
+ if (shmem_file(file))
+ return true;
+
+ return false;
}
-#define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
+#define arch_validate_flags(file, vm_flags) arch_validate_flags(file, vm_flags)
#endif /* !BUILD_VDSO */
diff --git a/arch/sparc/include/asm/mman.h b/arch/sparc/include/asm/mman.h
index af9c10c83dc5..d426e1f7c2c1 100644
--- a/arch/sparc/include/asm/mman.h
+++ b/arch/sparc/include/asm/mman.h
@@ -10,6 +10,7 @@ int sparc_mmap_check(unsigned long addr, unsigned long len);
#ifdef CONFIG_SPARC64
#include <asm/adi_64.h>
+#include <linux/fs.h>
static inline void ipi_set_tstate_mcde(void *arg)
{
@@ -54,11 +55,11 @@ static inline int sparc_validate_prot(unsigned long prot, unsigned long addr)
return 1;
}
-#define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
+#define arch_validate_flags(file, vm_flags) arch_validate_flags(file, vm_flags)
/* arch_validate_flags() - Ensure combination of flags is valid for a
* VMA.
*/
-static inline bool arch_validate_flags(unsigned long vm_flags)
+static inline bool arch_validate_flags(struct file *file, unsigned long vm_flags)
{
/* If ADI is being enabled on this VMA, check for ADI
* capability on the platform and ensure VMA is suitable
diff --git a/include/linux/mman.h b/include/linux/mman.h
index 8ddca62d6460..82e6488026b7 100644
--- a/include/linux/mman.h
+++ b/include/linux/mman.h
@@ -117,7 +117,7 @@ static inline bool arch_validate_prot(unsigned long prot, unsigned long addr)
*
* Returns true if the VM_* flags are valid.
*/
-static inline bool arch_validate_flags(unsigned long flags)
+static inline bool arch_validate_flags(struct file *file, unsigned long flags)
{
return true;
}
diff --git a/mm/mmap.c b/mm/mmap.c
index ab71d4c3464c..40b5858ae875 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1511,7 +1511,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
}
/* Allow architectures to sanity-check the vm_flags */
- if (!arch_validate_flags(vma->vm_flags)) {
+ if (!arch_validate_flags(file, vma->vm_flags)) {
error = -EINVAL;
goto close_and_free_vma;
}
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 6f450af3252e..c6db98b893fc 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -816,7 +816,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
}
/* Allow architectures to sanity-check the new flags */
- if (!arch_validate_flags(newflags)) {
+ if (!arch_validate_flags(vma->vm_file, newflags)) {
error = -EINVAL;
break;
}
diff --git a/mm/shmem.c b/mm/shmem.c
index 4ba1d00fabda..e87f5d6799a7 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2733,9 +2733,6 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
if (ret)
return ret;
- /* arm64 - allow memory tagging on RAM-based files */
- vm_flags_set(vma, VM_MTE_ALLOWED);
-
file_accessed(file);
/* This is anonymous shared memory if it is unlinked at the time of mmap */
if (inode->i_nlink)
--
2.47.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH hotfix 6.12 v3 5/5] mm: resolve faulty mmap_region() error path behaviour
2024-10-29 13:03 [PATCH hotfix 6.12 v3 0/5] fix error handling in mmap_region() and refactor (hotfixes) Lorenzo Stoakes
` (3 preceding siblings ...)
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling Lorenzo Stoakes
@ 2024-10-29 13:03 ` Lorenzo Stoakes
4 siblings, 0 replies; 12+ messages in thread
From: Lorenzo Stoakes @ 2024-10-29 13:03 UTC (permalink / raw)
To: Andrew Morton
Cc: Liam R . Howlett, Vlastimil Babka, Jann Horn, linux-kernel,
linux-mm, Linus Torvalds, Peter Xu, Catalin Marinas, Will Deacon,
Mark Brown, David S . Miller, Andreas Larsson
The mmap_region() function is somewhat terrifying, with spaghetti-like
control flow and numerous means by which issues can arise and incomplete
state, memory leaks and other unpleasantness can occur.
A large amount of the complexity arises from trying to handle errors late
in the process of mapping a VMA, which forms the basis of recently observed
issues with resource leaks and observable inconsistent state.
Taking advantage of previous patches in this series we move a number of
checks earlier in the code, simplifying things by moving the core of the
logic into a static internal function __mmap_region().
Doing this allows us to perform a number of checks up front before we do
any real work, and allows us to unwind the writable unmap check
unconditionally as required and to perform a CONFIG_DEBUG_VM_MAPLE_TREE
validation unconditionally also.
We move a number of things here:
1. We preallocate memory for the iterator before we call the file-backed
memory hook, allowing us to exit early and avoid having to perform
complicated and error-prone close/free logic. We carefully free
iterator state on both success and error paths.
2. The enclosing mmap_region() function handles the mapping_map_writable()
logic early. Previously the logic had the mapping_map_writable() at the
point of mapping a newly allocated file-backed VMA, and a matching
mapping_unmap_writable() on success and error paths.
We now do this unconditionally if this is a file-backed, shared writable
mapping. If a driver changes the flags to eliminate VM_MAYWRITE, however
doing so does not invalidate the seal check we just performed, and we in
any case always decrement the counter in the wrapper.
We perform a debug assert to ensure a driver does not attempt to do the
opposite.
3. We also move arch_validate_flags() up into the mmap_region()
function. This is only relevant on arm64 and sparc64, and the check is
only meaningful for SPARC with ADI enabled. We explicitly add a warning
for this arch if a driver invalidates this check, though the code ought
eventually to be fixed to eliminate the need for this.
With all of these measures in place, we no longer need to explicitly close
the VMA on error paths, as we place all checks which might fail prior to a
call to any driver mmap hook.
This eliminates an entire class of errors, makes the code easier to reason
about and more robust.
Reported-by: Jann Horn <jannh@google.com>
Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
Cc: stable <stable@kernel.org>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Tested-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
mm/mmap.c | 119 +++++++++++++++++++++++++++++-------------------------
1 file changed, 65 insertions(+), 54 deletions(-)
diff --git a/mm/mmap.c b/mm/mmap.c
index 40b5858ae875..c078c73e46ed 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1358,20 +1358,18 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len,
return do_vmi_munmap(&vmi, mm, start, len, uf, false);
}
-unsigned long mmap_region(struct file *file, unsigned long addr,
+static unsigned long __mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
struct mm_struct *mm = current->mm;
struct vm_area_struct *vma = NULL;
pgoff_t pglen = PHYS_PFN(len);
- struct vm_area_struct *merge;
unsigned long charged = 0;
struct vma_munmap_struct vms;
struct ma_state mas_detach;
struct maple_tree mt_detach;
unsigned long end = addr + len;
- bool writable_file_mapping = false;
int error;
VMA_ITERATOR(vmi, mm, addr);
VMG_STATE(vmg, mm, &vmi, addr, end, vm_flags, pgoff);
@@ -1445,28 +1443,26 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
vm_flags_init(vma, vm_flags);
vma->vm_page_prot = vm_get_page_prot(vm_flags);
+ if (vma_iter_prealloc(&vmi, vma)) {
+ error = -ENOMEM;
+ goto free_vma;
+ }
+
if (file) {
vma->vm_file = get_file(file);
error = mmap_file(file, vma);
if (error)
- goto unmap_and_free_vma;
-
- if (vma_is_shared_maywrite(vma)) {
- error = mapping_map_writable(file->f_mapping);
- if (error)
- goto close_and_free_vma;
-
- writable_file_mapping = true;
- }
+ goto unmap_and_free_file_vma;
+ /* Drivers cannot alter the address of the VMA. */
+ WARN_ON_ONCE(addr != vma->vm_start);
/*
- * Expansion is handled above, merging is handled below.
- * Drivers should not alter the address of the VMA.
+ * Drivers should not permit writability when previously it was
+ * disallowed.
*/
- if (WARN_ON((addr != vma->vm_start))) {
- error = -EINVAL;
- goto close_and_free_vma;
- }
+ VM_WARN_ON_ONCE(vm_flags != vma->vm_flags &&
+ !(vm_flags & VM_MAYWRITE) &&
+ (vma->vm_flags & VM_MAYWRITE));
vma_iter_config(&vmi, addr, end);
/*
@@ -1474,6 +1470,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
* vma again as we may succeed this time.
*/
if (unlikely(vm_flags != vma->vm_flags && vmg.prev)) {
+ struct vm_area_struct *merge;
+
vmg.flags = vma->vm_flags;
/* If this fails, state is reset ready for a reattempt. */
merge = vma_merge_new_range(&vmg);
@@ -1491,7 +1489,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
vma = merge;
/* Update vm_flags to pick up the change. */
vm_flags = vma->vm_flags;
- goto unmap_writable;
+ goto file_expanded;
}
vma_iter_config(&vmi, addr, end);
}
@@ -1500,26 +1498,15 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
} else if (vm_flags & VM_SHARED) {
error = shmem_zero_setup(vma);
if (error)
- goto free_vma;
+ goto free_iter_vma;
} else {
vma_set_anonymous(vma);
}
- if (map_deny_write_exec(vma->vm_flags, vma->vm_flags)) {
- error = -EACCES;
- goto close_and_free_vma;
- }
-
- /* Allow architectures to sanity-check the vm_flags */
- if (!arch_validate_flags(file, vma->vm_flags)) {
- error = -EINVAL;
- goto close_and_free_vma;
- }
-
- if (vma_iter_prealloc(&vmi, vma)) {
- error = -ENOMEM;
- goto close_and_free_vma;
- }
+#ifdef CONFIG_SPARC64
+ /* TODO: Fix SPARC ADI! */
+ WARN_ON_ONCE(!arch_validate_flags(file, vm_flags));
+#endif
/* Lock the VMA since it is modified after insertion into VMA tree */
vma_start_write(vma);
@@ -1533,10 +1520,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
*/
khugepaged_enter_vma(vma, vma->vm_flags);
- /* Once vma denies write, undo our temporary denial count */
-unmap_writable:
- if (writable_file_mapping)
- mapping_unmap_writable(file->f_mapping);
+file_expanded:
file = vma->vm_file;
ksm_add_vma(vma);
expanded:
@@ -1569,23 +1553,17 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
vma_set_page_prot(vma);
- validate_mm(mm);
return addr;
-close_and_free_vma:
- vma_close(vma);
-
- if (file || vma->vm_file) {
-unmap_and_free_vma:
- fput(vma->vm_file);
- vma->vm_file = NULL;
+unmap_and_free_file_vma:
+ fput(vma->vm_file);
+ vma->vm_file = NULL;
- vma_iter_set(&vmi, vma->vm_end);
- /* Undo any partial mapping done by a device driver. */
- unmap_region(&vmi.mas, vma, vmg.prev, vmg.next);
- }
- if (writable_file_mapping)
- mapping_unmap_writable(file->f_mapping);
+ vma_iter_set(&vmi, vma->vm_end);
+ /* Undo any partial mapping done by a device driver. */
+ unmap_region(&vmi.mas, vma, vmg.prev, vmg.next);
+free_iter_vma:
+ vma_iter_free(&vmi);
free_vma:
vm_area_free(vma);
unacct_error:
@@ -1595,10 +1573,43 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
abort_munmap:
vms_abort_munmap_vmas(&vms, &mas_detach);
gather_failed:
- validate_mm(mm);
return error;
}
+unsigned long mmap_region(struct file *file, unsigned long addr,
+ unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
+ struct list_head *uf)
+{
+ unsigned long ret;
+ bool writable_file_mapping = false;
+
+ /* Check to see if MDWE is applicable. */
+ if (map_deny_write_exec(vm_flags, vm_flags))
+ return -EACCES;
+
+ /* Allow architectures to sanity-check the vm_flags. */
+ if (!arch_validate_flags(file, vm_flags))
+ return -EINVAL;
+
+ /* Map writable and ensure this isn't a sealed memfd. */
+ if (file && is_shared_maywrite(vm_flags)) {
+ int error = mapping_map_writable(file->f_mapping);
+
+ if (error)
+ return error;
+ writable_file_mapping = true;
+ }
+
+ ret = __mmap_region(file, addr, len, vm_flags, pgoff, uf);
+
+ /* Clear our write mapping regardless of error. */
+ if (writable_file_mapping)
+ mapping_unmap_writable(file->f_mapping);
+
+ validate_mm(current->mm);
+ return ret;
+}
+
static int __vm_munmap(unsigned long start, size_t len, bool unlock)
{
int ret;
--
2.47.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling Lorenzo Stoakes
@ 2024-10-29 14:38 ` Liam R. Howlett
2024-10-29 15:23 ` Lorenzo Stoakes
2024-10-29 15:43 ` Yang Shi
2 siblings, 0 replies; 12+ messages in thread
From: Liam R. Howlett @ 2024-10-29 14:38 UTC (permalink / raw)
To: Lorenzo Stoakes
Cc: Andrew Morton, Vlastimil Babka, Jann Horn, linux-kernel,
linux-mm, Linus Torvalds, Peter Xu, Catalin Marinas, Will Deacon,
Mark Brown, David S . Miller, Andreas Larsson
* Lorenzo Stoakes <lorenzo.stoakes@oracle.com> [241029 09:04]:
> 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 to arch_validate_flags() itself.
>
> This requires a modification to the arch_validate_flags() 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 sparc.
>
> So this patch performs this adjustment and moves the check to
> arch_validate_flags() which resolves the issue.
>
> We'd ideally like to have eliminated the arch_calc_vm_flag_bits() case, but
> we risk inadvertently changing behaviour as we do not have mmap() flags
> available at the point of the arch_validate_flags() check and a MAP_ANON |
> MAP_HUGETLB case would be accepted for MTE currently, but a MAP_SHARED |
> MAP_HUGETLB would not.
>
> This is likely an oversight but we want to try to keep behaviour identical
> to before in this patch.
>
> So continue to check VM_MTE_ALLOWED which arch_calc_vm_flag_bits() sets if
> MAP_ANON.
>
> Reported-by: Jann Horn <jannh@google.com>
> Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
> Cc: stable <stable@kernel.org>
> Acked-by: Vlastimil Babka <vbabka@suse.cz>
> Tested-by: Mark Brown <broonie@kernel.org>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>
> ---
> arch/arm64/include/asm/mman.h | 29 +++++++++++++++++++++++++----
> arch/sparc/include/asm/mman.h | 5 +++--
> include/linux/mman.h | 2 +-
> mm/mmap.c | 2 +-
> mm/mprotect.c | 2 +-
> mm/shmem.c | 3 ---
> 6 files changed, 31 insertions(+), 12 deletions(-)
>
> diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h
> index 9e39217b4afb..94925a0482e3 100644
> --- a/arch/arm64/include/asm/mman.h
> +++ b/arch/arm64/include/asm/mman.h
> @@ -6,7 +6,9 @@
>
> #ifndef BUILD_VDSO
> #include <linux/compiler.h>
> +#include <linux/fs.h>
> #include <linux/types.h>
> +#include <linux/shmem_fs.h>
>
> static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
> unsigned long pkey)
> @@ -60,15 +62,34 @@ static inline bool arch_validate_prot(unsigned long prot,
> }
> #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
>
> -static inline bool arch_validate_flags(unsigned long vm_flags)
> +static inline bool arch_validate_flags(struct file *file, unsigned long vm_flags)
> {
> if (!system_supports_mte())
> return true;
>
> - /* only allow VM_MTE if VM_MTE_ALLOWED has been set previously */
> - return !(vm_flags & VM_MTE) || (vm_flags & VM_MTE_ALLOWED);
> + if (!(vm_flags & VM_MTE))
> + return true;
> +
> + /*
> + * We explicitly permit MAP_ANONYMOUS and shmem backed mappings to use
> + * MTE. We check for shmem explicitly here.
> + *
> + * The MAP_ANONYMOUS case is handled by arch_calc_vm_flag_bits() which
> + * explicitly sets VM_MTE_ALLOWED which we check for here also.
> + *
> + * Ideally we'd perform both checks here but existing semantics support
> + * hugetlb in MAP_ANONYMOUS mode but not a MAP_SHARED mapping, which is
> + * likely unintended but we maintain identical behaviour for
> + * consistency.
> + */
> + if (vm_flags & VM_MTE_ALLOWED)
> + return true;
> + if (shmem_file(file))
> + return true;
> +
> + return false;
> }
> -#define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
> +#define arch_validate_flags(file, vm_flags) arch_validate_flags(file, vm_flags)
>
> #endif /* !BUILD_VDSO */
>
> diff --git a/arch/sparc/include/asm/mman.h b/arch/sparc/include/asm/mman.h
> index af9c10c83dc5..d426e1f7c2c1 100644
> --- a/arch/sparc/include/asm/mman.h
> +++ b/arch/sparc/include/asm/mman.h
> @@ -10,6 +10,7 @@ int sparc_mmap_check(unsigned long addr, unsigned long len);
>
> #ifdef CONFIG_SPARC64
> #include <asm/adi_64.h>
> +#include <linux/fs.h>
>
> static inline void ipi_set_tstate_mcde(void *arg)
> {
> @@ -54,11 +55,11 @@ static inline int sparc_validate_prot(unsigned long prot, unsigned long addr)
> return 1;
> }
>
> -#define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
> +#define arch_validate_flags(file, vm_flags) arch_validate_flags(file, vm_flags)
> /* arch_validate_flags() - Ensure combination of flags is valid for a
> * VMA.
> */
> -static inline bool arch_validate_flags(unsigned long vm_flags)
> +static inline bool arch_validate_flags(struct file *file, unsigned long vm_flags)
> {
> /* If ADI is being enabled on this VMA, check for ADI
> * capability on the platform and ensure VMA is suitable
> diff --git a/include/linux/mman.h b/include/linux/mman.h
> index 8ddca62d6460..82e6488026b7 100644
> --- a/include/linux/mman.h
> +++ b/include/linux/mman.h
> @@ -117,7 +117,7 @@ static inline bool arch_validate_prot(unsigned long prot, unsigned long addr)
> *
> * Returns true if the VM_* flags are valid.
> */
> -static inline bool arch_validate_flags(unsigned long flags)
> +static inline bool arch_validate_flags(struct file *file, unsigned long flags)
> {
> return true;
> }
> diff --git a/mm/mmap.c b/mm/mmap.c
> index ab71d4c3464c..40b5858ae875 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -1511,7 +1511,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
> }
>
> /* Allow architectures to sanity-check the vm_flags */
> - if (!arch_validate_flags(vma->vm_flags)) {
> + if (!arch_validate_flags(file, vma->vm_flags)) {
> error = -EINVAL;
> goto close_and_free_vma;
> }
> diff --git a/mm/mprotect.c b/mm/mprotect.c
> index 6f450af3252e..c6db98b893fc 100644
> --- a/mm/mprotect.c
> +++ b/mm/mprotect.c
> @@ -816,7 +816,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
> }
>
> /* Allow architectures to sanity-check the new flags */
> - if (!arch_validate_flags(newflags)) {
> + if (!arch_validate_flags(vma->vm_file, newflags)) {
> error = -EINVAL;
> break;
> }
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 4ba1d00fabda..e87f5d6799a7 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2733,9 +2733,6 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
> if (ret)
> return ret;
>
> - /* arm64 - allow memory tagging on RAM-based files */
> - vm_flags_set(vma, VM_MTE_ALLOWED);
> -
> file_accessed(file);
> /* This is anonymous shared memory if it is unlinked at the time of mmap */
> if (inode->i_nlink)
> --
> 2.47.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling Lorenzo Stoakes
2024-10-29 14:38 ` Liam R. Howlett
@ 2024-10-29 15:23 ` Lorenzo Stoakes
2024-10-29 15:43 ` Yang Shi
2 siblings, 0 replies; 12+ messages in thread
From: Lorenzo Stoakes @ 2024-10-29 15:23 UTC (permalink / raw)
To: Andrew Morton
Cc: Liam R . Howlett, Vlastimil Babka, Jann Horn, linux-kernel,
linux-mm, Linus Torvalds, Peter Xu, Catalin Marinas, Will Deacon,
Mark Brown, David S . Miller, Andreas Larsson
Andrew - could we modify this patch please? I'd like to not include the change
to mm/shmem.c. Sorry to be a pain!
On Tue, Oct 29, 2024 at 01:03:46PM +0000, Lorenzo Stoakes wrote:
> 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 to arch_validate_flags() itself.
>
> This requires a modification to the arch_validate_flags() 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 sparc.
>
> So this patch performs this adjustment and moves the check to
> arch_validate_flags() which resolves the issue.
>
> We'd ideally like to have eliminated the arch_calc_vm_flag_bits() case, but
> we risk inadvertently changing behaviour as we do not have mmap() flags
> available at the point of the arch_validate_flags() check and a MAP_ANON |
> MAP_HUGETLB case would be accepted for MTE currently, but a MAP_SHARED |
> MAP_HUGETLB would not.
>
> This is likely an oversight but we want to try to keep behaviour identical
> to before in this patch.
>
> So continue to check VM_MTE_ALLOWED which arch_calc_vm_flag_bits() sets if
> MAP_ANON.
>
> Reported-by: Jann Horn <jannh@google.com>
> Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
> Cc: stable <stable@kernel.org>
> Acked-by: Vlastimil Babka <vbabka@suse.cz>
> Tested-by: Mark Brown <broonie@kernel.org>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> ---
> arch/arm64/include/asm/mman.h | 29 +++++++++++++++++++++++++----
> arch/sparc/include/asm/mman.h | 5 +++--
> include/linux/mman.h | 2 +-
> mm/mmap.c | 2 +-
> mm/mprotect.c | 2 +-
> mm/shmem.c | 3 ---
> 6 files changed, 31 insertions(+), 12 deletions(-)
>
> diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h
> index 9e39217b4afb..94925a0482e3 100644
> --- a/arch/arm64/include/asm/mman.h
> +++ b/arch/arm64/include/asm/mman.h
> @@ -6,7 +6,9 @@
>
> #ifndef BUILD_VDSO
> #include <linux/compiler.h>
> +#include <linux/fs.h>
> #include <linux/types.h>
> +#include <linux/shmem_fs.h>
>
> static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
> unsigned long pkey)
> @@ -60,15 +62,34 @@ static inline bool arch_validate_prot(unsigned long prot,
> }
> #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
>
> -static inline bool arch_validate_flags(unsigned long vm_flags)
> +static inline bool arch_validate_flags(struct file *file, unsigned long vm_flags)
> {
> if (!system_supports_mte())
> return true;
>
> - /* only allow VM_MTE if VM_MTE_ALLOWED has been set previously */
> - return !(vm_flags & VM_MTE) || (vm_flags & VM_MTE_ALLOWED);
> + if (!(vm_flags & VM_MTE))
> + return true;
> +
> + /*
> + * We explicitly permit MAP_ANONYMOUS and shmem backed mappings to use
> + * MTE. We check for shmem explicitly here.
> + *
> + * The MAP_ANONYMOUS case is handled by arch_calc_vm_flag_bits() which
> + * explicitly sets VM_MTE_ALLOWED which we check for here also.
> + *
> + * Ideally we'd perform both checks here but existing semantics support
> + * hugetlb in MAP_ANONYMOUS mode but not a MAP_SHARED mapping, which is
> + * likely unintended but we maintain identical behaviour for
> + * consistency.
> + */
> + if (vm_flags & VM_MTE_ALLOWED)
> + return true;
> + if (shmem_file(file))
> + return true;
> +
> + return false;
> }
> -#define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
> +#define arch_validate_flags(file, vm_flags) arch_validate_flags(file, vm_flags)
>
> #endif /* !BUILD_VDSO */
>
> diff --git a/arch/sparc/include/asm/mman.h b/arch/sparc/include/asm/mman.h
> index af9c10c83dc5..d426e1f7c2c1 100644
> --- a/arch/sparc/include/asm/mman.h
> +++ b/arch/sparc/include/asm/mman.h
> @@ -10,6 +10,7 @@ int sparc_mmap_check(unsigned long addr, unsigned long len);
>
> #ifdef CONFIG_SPARC64
> #include <asm/adi_64.h>
> +#include <linux/fs.h>
>
> static inline void ipi_set_tstate_mcde(void *arg)
> {
> @@ -54,11 +55,11 @@ static inline int sparc_validate_prot(unsigned long prot, unsigned long addr)
> return 1;
> }
>
> -#define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
> +#define arch_validate_flags(file, vm_flags) arch_validate_flags(file, vm_flags)
> /* arch_validate_flags() - Ensure combination of flags is valid for a
> * VMA.
> */
> -static inline bool arch_validate_flags(unsigned long vm_flags)
> +static inline bool arch_validate_flags(struct file *file, unsigned long vm_flags)
> {
> /* If ADI is being enabled on this VMA, check for ADI
> * capability on the platform and ensure VMA is suitable
> diff --git a/include/linux/mman.h b/include/linux/mman.h
> index 8ddca62d6460..82e6488026b7 100644
> --- a/include/linux/mman.h
> +++ b/include/linux/mman.h
> @@ -117,7 +117,7 @@ static inline bool arch_validate_prot(unsigned long prot, unsigned long addr)
> *
> * Returns true if the VM_* flags are valid.
> */
> -static inline bool arch_validate_flags(unsigned long flags)
> +static inline bool arch_validate_flags(struct file *file, unsigned long flags)
> {
> return true;
> }
> diff --git a/mm/mmap.c b/mm/mmap.c
> index ab71d4c3464c..40b5858ae875 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -1511,7 +1511,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
> }
>
> /* Allow architectures to sanity-check the vm_flags */
> - if (!arch_validate_flags(vma->vm_flags)) {
> + if (!arch_validate_flags(file, vma->vm_flags)) {
> error = -EINVAL;
> goto close_and_free_vma;
> }
> diff --git a/mm/mprotect.c b/mm/mprotect.c
> index 6f450af3252e..c6db98b893fc 100644
> --- a/mm/mprotect.c
> +++ b/mm/mprotect.c
> @@ -816,7 +816,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
> }
>
> /* Allow architectures to sanity-check the new flags */
> - if (!arch_validate_flags(newflags)) {
> + if (!arch_validate_flags(vma->vm_file, newflags)) {
> error = -EINVAL;
> break;
> }
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 4ba1d00fabda..e87f5d6799a7 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2733,9 +2733,6 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
> if (ret)
> return ret;
>
> - /* arm64 - allow memory tagging on RAM-based files */
> - vm_flags_set(vma, VM_MTE_ALLOWED);
> -
Andrew - We don't want to include this in change to mm/shmem.c in this
series as it turns out we always need to set this flag, hence the need to
not include this part of the patch.
Thanks!
> file_accessed(file);
> /* This is anonymous shared memory if it is unlinked at the time of mmap */
> if (inode->i_nlink)
> --
> 2.47.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling Lorenzo Stoakes
2024-10-29 14:38 ` Liam R. Howlett
2024-10-29 15:23 ` Lorenzo Stoakes
@ 2024-10-29 15:43 ` Yang Shi
2024-10-29 15:52 ` Lorenzo Stoakes
2 siblings, 1 reply; 12+ messages in thread
From: Yang Shi @ 2024-10-29 15:43 UTC (permalink / raw)
To: Lorenzo Stoakes
Cc: Andrew Morton, Liam R . Howlett, Vlastimil Babka, Jann Horn,
linux-kernel, linux-mm, Linus Torvalds, Peter Xu,
Catalin Marinas, Will Deacon, Mark Brown, David S . Miller,
Andreas Larsson
On Tue, Oct 29, 2024 at 6:04 AM Lorenzo Stoakes
<lorenzo.stoakes@oracle.com> wrote:
>
> 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 to arch_validate_flags() itself.
>
> This requires a modification to the arch_validate_flags() 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 sparc.
>
> So this patch performs this adjustment and moves the check to
> arch_validate_flags() which resolves the issue.
>
> We'd ideally like to have eliminated the arch_calc_vm_flag_bits() case, but
> we risk inadvertently changing behaviour as we do not have mmap() flags
> available at the point of the arch_validate_flags() check and a MAP_ANON |
> MAP_HUGETLB case would be accepted for MTE currently, but a MAP_SHARED |
> MAP_HUGETLB would not.
>
> This is likely an oversight but we want to try to keep behaviour identical
> to before in this patch.
>
> So continue to check VM_MTE_ALLOWED which arch_calc_vm_flag_bits() sets if
> MAP_ANON.
>
> Reported-by: Jann Horn <jannh@google.com>
> Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails")
> Cc: stable <stable@kernel.org>
> Acked-by: Vlastimil Babka <vbabka@suse.cz>
> Tested-by: Mark Brown <broonie@kernel.org>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> ---
> arch/arm64/include/asm/mman.h | 29 +++++++++++++++++++++++++----
> arch/sparc/include/asm/mman.h | 5 +++--
> include/linux/mman.h | 2 +-
> mm/mmap.c | 2 +-
> mm/mprotect.c | 2 +-
> mm/shmem.c | 3 ---
> 6 files changed, 31 insertions(+), 12 deletions(-)
>
> diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h
> index 9e39217b4afb..94925a0482e3 100644
> --- a/arch/arm64/include/asm/mman.h
> +++ b/arch/arm64/include/asm/mman.h
> @@ -6,7 +6,9 @@
>
> #ifndef BUILD_VDSO
> #include <linux/compiler.h>
> +#include <linux/fs.h>
> #include <linux/types.h>
> +#include <linux/shmem_fs.h>
>
> static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot,
> unsigned long pkey)
> @@ -60,15 +62,34 @@ static inline bool arch_validate_prot(unsigned long prot,
> }
> #define arch_validate_prot(prot, addr) arch_validate_prot(prot, addr)
>
> -static inline bool arch_validate_flags(unsigned long vm_flags)
> +static inline bool arch_validate_flags(struct file *file, unsigned long vm_flags)
> {
> if (!system_supports_mte())
> return true;
>
> - /* only allow VM_MTE if VM_MTE_ALLOWED has been set previously */
> - return !(vm_flags & VM_MTE) || (vm_flags & VM_MTE_ALLOWED);
> + if (!(vm_flags & VM_MTE))
> + return true;
> +
> + /*
> + * We explicitly permit MAP_ANONYMOUS and shmem backed mappings to use
> + * MTE. We check for shmem explicitly here.
> + *
> + * The MAP_ANONYMOUS case is handled by arch_calc_vm_flag_bits() which
> + * explicitly sets VM_MTE_ALLOWED which we check for here also.
> + *
> + * Ideally we'd perform both checks here but existing semantics support
> + * hugetlb in MAP_ANONYMOUS mode but not a MAP_SHARED mapping, which is
> + * likely unintended but we maintain identical behaviour for
> + * consistency.
> + */
> + if (vm_flags & VM_MTE_ALLOWED)
> + return true;
> + if (shmem_file(file))
> + return true;
Sorry for chiming in late. This looks problematic IIUC. The patch 5/5
moved arch_validate_flags() before call_mmap() as the commit log said.
But shmem_file() does this check:
mapping->a_ops == &shmem_aops
But mapping->a_ops is not initialized until shmem_mmap(), which is
called by call_mmap().
So shmem_file() should always return false here. Did I miss something?
> +
> + return false;
> }
> -#define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
> +#define arch_validate_flags(file, vm_flags) arch_validate_flags(file, vm_flags)
>
> #endif /* !BUILD_VDSO */
>
> diff --git a/arch/sparc/include/asm/mman.h b/arch/sparc/include/asm/mman.h
> index af9c10c83dc5..d426e1f7c2c1 100644
> --- a/arch/sparc/include/asm/mman.h
> +++ b/arch/sparc/include/asm/mman.h
> @@ -10,6 +10,7 @@ int sparc_mmap_check(unsigned long addr, unsigned long len);
>
> #ifdef CONFIG_SPARC64
> #include <asm/adi_64.h>
> +#include <linux/fs.h>
>
> static inline void ipi_set_tstate_mcde(void *arg)
> {
> @@ -54,11 +55,11 @@ static inline int sparc_validate_prot(unsigned long prot, unsigned long addr)
> return 1;
> }
>
> -#define arch_validate_flags(vm_flags) arch_validate_flags(vm_flags)
> +#define arch_validate_flags(file, vm_flags) arch_validate_flags(file, vm_flags)
> /* arch_validate_flags() - Ensure combination of flags is valid for a
> * VMA.
> */
> -static inline bool arch_validate_flags(unsigned long vm_flags)
> +static inline bool arch_validate_flags(struct file *file, unsigned long vm_flags)
> {
> /* If ADI is being enabled on this VMA, check for ADI
> * capability on the platform and ensure VMA is suitable
> diff --git a/include/linux/mman.h b/include/linux/mman.h
> index 8ddca62d6460..82e6488026b7 100644
> --- a/include/linux/mman.h
> +++ b/include/linux/mman.h
> @@ -117,7 +117,7 @@ static inline bool arch_validate_prot(unsigned long prot, unsigned long addr)
> *
> * Returns true if the VM_* flags are valid.
> */
> -static inline bool arch_validate_flags(unsigned long flags)
> +static inline bool arch_validate_flags(struct file *file, unsigned long flags)
> {
> return true;
> }
> diff --git a/mm/mmap.c b/mm/mmap.c
> index ab71d4c3464c..40b5858ae875 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -1511,7 +1511,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
> }
>
> /* Allow architectures to sanity-check the vm_flags */
> - if (!arch_validate_flags(vma->vm_flags)) {
> + if (!arch_validate_flags(file, vma->vm_flags)) {
> error = -EINVAL;
> goto close_and_free_vma;
> }
> diff --git a/mm/mprotect.c b/mm/mprotect.c
> index 6f450af3252e..c6db98b893fc 100644
> --- a/mm/mprotect.c
> +++ b/mm/mprotect.c
> @@ -816,7 +816,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
> }
>
> /* Allow architectures to sanity-check the new flags */
> - if (!arch_validate_flags(newflags)) {
> + if (!arch_validate_flags(vma->vm_file, newflags)) {
> error = -EINVAL;
> break;
> }
> diff --git a/mm/shmem.c b/mm/shmem.c
> index 4ba1d00fabda..e87f5d6799a7 100644
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2733,9 +2733,6 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
> if (ret)
> return ret;
>
> - /* arm64 - allow memory tagging on RAM-based files */
> - vm_flags_set(vma, VM_MTE_ALLOWED);
> -
> file_accessed(file);
> /* This is anonymous shared memory if it is unlinked at the time of mmap */
> if (inode->i_nlink)
> --
> 2.47.0
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling
2024-10-29 15:43 ` Yang Shi
@ 2024-10-29 15:52 ` Lorenzo Stoakes
2024-10-29 16:08 ` Yang Shi
0 siblings, 1 reply; 12+ messages in thread
From: Lorenzo Stoakes @ 2024-10-29 15:52 UTC (permalink / raw)
To: Yang Shi
Cc: Andrew Morton, Liam R . Howlett, Vlastimil Babka, Jann Horn,
linux-kernel, linux-mm, Linus Torvalds, Peter Xu,
Catalin Marinas, Will Deacon, Mark Brown, David S . Miller,
Andreas Larsson
On Tue, Oct 29, 2024 at 08:43:04AM -0700, Yang Shi wrote:
>
> Sorry for chiming in late. This looks problematic IIUC. The patch 5/5
> moved arch_validate_flags() before call_mmap() as the commit log said.
> But shmem_file() does this check:
> mapping->a_ops == &shmem_aops
>
> But mapping->a_ops is not initialized until shmem_mmap(), which is
> called by call_mmap().
>
> So shmem_file() should always return false here. Did I miss something?
Well firstly this is tested and works so you did miss something :)
Bu shmem_mmap() is not setting mapping->a_ops, it's setting vma->vm_ops.
__shmem_get_inode() and shmem_symlink() set mapping->a_ops.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling
2024-10-29 15:52 ` Lorenzo Stoakes
@ 2024-10-29 16:08 ` Yang Shi
2024-10-29 16:21 ` Lorenzo Stoakes
0 siblings, 1 reply; 12+ messages in thread
From: Yang Shi @ 2024-10-29 16:08 UTC (permalink / raw)
To: Lorenzo Stoakes
Cc: Andrew Morton, Liam R . Howlett, Vlastimil Babka, Jann Horn,
linux-kernel, linux-mm, Linus Torvalds, Peter Xu,
Catalin Marinas, Will Deacon, Mark Brown, David S . Miller,
Andreas Larsson
On Tue, Oct 29, 2024 at 8:52 AM Lorenzo Stoakes
<lorenzo.stoakes@oracle.com> wrote:
>
> On Tue, Oct 29, 2024 at 08:43:04AM -0700, Yang Shi wrote:
>
> >
> > Sorry for chiming in late. This looks problematic IIUC. The patch 5/5
> > moved arch_validate_flags() before call_mmap() as the commit log said.
> > But shmem_file() does this check:
> > mapping->a_ops == &shmem_aops
> >
> > But mapping->a_ops is not initialized until shmem_mmap(), which is
> > called by call_mmap().
> >
> > So shmem_file() should always return false here. Did I miss something?
>
> Well firstly this is tested and works so you did miss something :)
>
> Bu shmem_mmap() is not setting mapping->a_ops, it's setting vma->vm_ops.
>
> __shmem_get_inode() and shmem_symlink() set mapping->a_ops.
My bad, stared at "_ops", but missed "vm" prefix. And shmem_mmap()
can't touch a_ops. Sorry for the disruption.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling
2024-10-29 16:08 ` Yang Shi
@ 2024-10-29 16:21 ` Lorenzo Stoakes
0 siblings, 0 replies; 12+ messages in thread
From: Lorenzo Stoakes @ 2024-10-29 16:21 UTC (permalink / raw)
To: Yang Shi
Cc: Andrew Morton, Liam R . Howlett, Vlastimil Babka, Jann Horn,
linux-kernel, linux-mm, Linus Torvalds, Peter Xu,
Catalin Marinas, Will Deacon, Mark Brown, David S . Miller,
Andreas Larsson
On Tue, Oct 29, 2024 at 09:08:22AM -0700, Yang Shi wrote:
> On Tue, Oct 29, 2024 at 8:52 AM Lorenzo Stoakes
> <lorenzo.stoakes@oracle.com> wrote:
> >
> > On Tue, Oct 29, 2024 at 08:43:04AM -0700, Yang Shi wrote:
> >
> > >
> > > Sorry for chiming in late. This looks problematic IIUC. The patch 5/5
> > > moved arch_validate_flags() before call_mmap() as the commit log said.
> > > But shmem_file() does this check:
> > > mapping->a_ops == &shmem_aops
> > >
> > > But mapping->a_ops is not initialized until shmem_mmap(), which is
> > > called by call_mmap().
> > >
> > > So shmem_file() should always return false here. Did I miss something?
> >
> > Well firstly this is tested and works so you did miss something :)
> >
> > Bu shmem_mmap() is not setting mapping->a_ops, it's setting vma->vm_ops.
> >
> > __shmem_get_inode() and shmem_symlink() set mapping->a_ops.
>
> My bad, stared at "_ops", but missed "vm" prefix. And shmem_mmap()
> can't touch a_ops. Sorry for the disruption.
No worries, the various ops are quite confusing and precisely where what
happens, I had to double check this myself... :)
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2024-10-29 16:21 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-29 13:03 [PATCH hotfix 6.12 v3 0/5] fix error handling in mmap_region() and refactor (hotfixes) Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 1/5] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 2/5] mm: unconditionally close VMAs on error Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 3/5] mm: refactor map_deny_write_exec() Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 4/5] mm: refactor arch_validate_flags() and arm64 MTE handling Lorenzo Stoakes
2024-10-29 14:38 ` Liam R. Howlett
2024-10-29 15:23 ` Lorenzo Stoakes
2024-10-29 15:43 ` Yang Shi
2024-10-29 15:52 ` Lorenzo Stoakes
2024-10-29 16:08 ` Yang Shi
2024-10-29 16:21 ` Lorenzo Stoakes
2024-10-29 13:03 ` [PATCH hotfix 6.12 v3 5/5] mm: resolve faulty mmap_region() error path behaviour Lorenzo Stoakes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox