drivers/char/mem: mmap readonly MAP_SHARED-/dev/zero correctly
Subsystems (2)
PATCH 1/1 drivers/char/mem: mmap readonly MAP_SHARED-/dev/zero correctly
Rather surprisingly, opening /dev/zero read-only then mmap()'ing it MAP_SHARED gets you true anonymous memory (albeit in a VMA with non-NULL vma->vm_file).
This is a by-product of MAP_PRIVATE-/dev/zero being how anonymous memory was mapped in Linux's distant past.
It happens because mmap_zero_prepare() gates on VMA_SHARED_BIT and when mapping a read-only file MAP_SHARED, do_mmap() clears VMA_SHARED_BIT and VMA_MAYWRITE_BIT.
The gating is incorrect - the (poorly named) VMA_MAYSHARE_BIT flag exists explicitly to tell you if something was originally mapped MAP_SHARED.
So the fix is simple - gate on this instead.
This isn't exactly a common use case, but it's unexpected behaviour which now causes an assert if CONFIG_DEBUG_VM is set.
While this bug has existed since the dawn of time for linux (or at least since 2.6.12), it hasn't caused issues in the past, so while it's incorrect behaviour, it doesn't seem necessary to backport that far.
The mapping is now accounted at mmap time and can fail with -ENOMEM under strict overcommit, and read faults allocate folios. However this is normal behaviour for a read-only shmem mapping.
Commit 93c0c8dc87f6 ("mm/rmap: use anon pgoff to track MAP_PRIVATE file-backed anon folios") is the first patch at which the debug assert fires, so target that instead.
--- drivers/char/mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/mem.c +1 -1
@@ -503,7 +503,7 @@ static int mmap_zero_prepare(struct vm_area_desc *desc)#ifndef CONFIG_MMU return -ENOSYS ;#endif -if (vma_desc_test (desc,VMA_SHARED_BIT )) +if (vma_desc_test (desc,VMA_MAYSHARE_BIT ))return shmem_zero_setup_desc (desc);/*
---
base-commit: 62f4c998b297cf233997a2b4cd6fc2d2df0319c9
change-id: 20260924-fix-dev-zero-readonly-shared-f2d46c156ce2
Best regards,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Greg Kroah-Hartman
maintainerQuoted 44 lines · Lorenzo Stoakes (ARM)
Rather surprisingly, opening
/dev/zeroread-only then mmap()'ing itMAP_SHAREDgets you true anonymous memory (albeit in a VMA with non-NULLvma->vm_file).This is a by-product of MAP_PRIVATE-/dev/zero being how anonymous memory was mapped in Linux's distant past.
It happens because
mmap_zero_prepare()gates onVMA_SHARED_BITand when mapping a read-only fileMAP_SHARED,do_mmap()clearsVMA_SHARED_BITandVMA_MAYWRITE_BIT.The gating is incorrect - the (poorly named)
VMA_MAYSHARE_BITflag exists explicitly to tell you if something was originally mappedMAP_SHARED.So the fix is simple - gate on this instead.
This isn't exactly a common use case, but it's unexpected behaviour which now causes an assert if
CONFIG_DEBUG_VMis set.While this bug has existed since the dawn of time for linux (or at least since 2.6.12), it hasn't caused issues in the past, so while it's incorrect behaviour, it doesn't seem necessary to backport that far.
The mapping is now accounted at mmap time and can fail with
-ENOMEMunder strict overcommit, and read faults allocate folios. However this is normal behaviour for a read-only shmem mapping.Commit
93c0c8dc87f6("mm/rmap: use anon pgoff to trackMAP_PRIVATEfile-backed anon folios") is the first patch at which the debug assert fires, so target that instead.Fixes 93c0c8dc87f6 ("mm/rmap: use anon pgoff to track MAP_PRIVATE file-backed anon folios")Reported-by [email protected]Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- drivers/char/mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/char/mem.c +0
-0
drivers/char/mem.c static int mmap_zero_prepare(struct vm_area_desc *desc) · line 507
Quoted 6 lines
drivers/char/mem.c +1
-1@@ -503,7 +503,7 @@ static int mmap_zero_prepare(struct vm_area_desc *desc)#ifndef CONFIG_MMU return -ENOSYS ;#endif -if (vma_desc_test (desc,VMA_SHARED_BIT )) +if (vma_desc_test (desc,VMA_MAYSHARE_BIT ))return shmem_zero_setup_desc (desc);
What's the odds that something "big" actually relies on this?
I'm all for the change, but be prepared in about 5 years for people to start complaining :)
Want me to take it through my tree?
thanks,
greg k-h
Quoted 40 lines · Greg Kroah-Hartman
Quoted 39 lines · Lorenzo Stoakes (ARM)
Rather surprisingly, opening
/dev/zeroread-only then mmap()'ing itMAP_SHAREDgets you true anonymous memory (albeit in a VMA with non-NULLvma->vm_file).This is a by-product of MAP_PRIVATE-/dev/zero being how anonymous memory was mapped in Linux's distant past.
It happens because
mmap_zero_prepare()gates onVMA_SHARED_BITand when mapping a read-only fileMAP_SHARED,do_mmap()clearsVMA_SHARED_BITandVMA_MAYWRITE_BIT.The gating is incorrect - the (poorly named)
VMA_MAYSHARE_BITflag exists explicitly to tell you if something was originally mappedMAP_SHARED.So the fix is simple - gate on this instead.
This isn't exactly a common use case, but it's unexpected behaviour which now causes an assert if
CONFIG_DEBUG_VMis set.While this bug has existed since the dawn of time for linux (or at least since 2.6.12), it hasn't caused issues in the past, so while it's incorrect behaviour, it doesn't seem necessary to backport that far.
The mapping is now accounted at mmap time and can fail with
-ENOMEMunder strict overcommit, and read faults allocate folios. However this is normal behaviour for a read-only shmem mapping.Commit
93c0c8dc87f6("mm/rmap: use anon pgoff to trackMAP_PRIVATEfile-backed anon folios") is the first patch at which the debug assert fires, so target that instead.Fixes 93c0c8dc87f6 ("mm/rmap: use anon pgoff to track MAP_PRIVATE file-backed anon folios")Reported-by [email protected]Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- drivers/char/mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines
drivers/char/mem.c static int mmap_zero_prepare(struct vm_area_desc *desc) · line 507
Quoted 11 lines
drivers/char/mem.c +1
-1@@ -503,7 +503,7 @@ static int mmap_zero_prepare(struct vm_area_desc *desc)#ifndef CONFIG_MMU return -ENOSYS ;#endif -if (vma_desc_test (desc,VMA_SHARED_BIT )) +if (vma_desc_test (desc,VMA_MAYSHARE_BIT ))return shmem_zero_setup_desc (desc);What's the odds that something "big" actually relies on this?
I think it'd be fairly crazy to, unless people really care about read-faulting a lot of zero pages which they want to somehow not account properly.
Anything's possible but I think on this, right to take a (v. small) risk!
Quoted 5 lines
I'm all for the change, but be prepared in about 5 years for people to start complaining :)
Want me to take it through my tree?
Sure, thanks, though there will be a conflict against a patch in mm.
Though it's really that the file is moved (to mm/), so actually should be an easy resolution?
Quoted 4 lines
thanks,
greg k-h
--
Cheers, Lorenzo
Greg Kroah-Hartman
maintainerQuoted 64 lines · Lorenzo Stoakes (ARM)
Quoted 40 lines · Greg Kroah-Hartman
Quoted 39 lines · Lorenzo Stoakes (ARM)
Rather surprisingly, opening
/dev/zeroread-only then mmap()'ing itMAP_SHAREDgets you true anonymous memory (albeit in a VMA with non-NULLvma->vm_file).This is a by-product of MAP_PRIVATE-/dev/zero being how anonymous memory was mapped in Linux's distant past.
It happens because
mmap_zero_prepare()gates onVMA_SHARED_BITand when mapping a read-only fileMAP_SHARED,do_mmap()clearsVMA_SHARED_BITandVMA_MAYWRITE_BIT.The gating is incorrect - the (poorly named)
VMA_MAYSHARE_BITflag exists explicitly to tell you if something was originally mappedMAP_SHARED.So the fix is simple - gate on this instead.
This isn't exactly a common use case, but it's unexpected behaviour which now causes an assert if
CONFIG_DEBUG_VMis set.While this bug has existed since the dawn of time for linux (or at least since 2.6.12), it hasn't caused issues in the past, so while it's incorrect behaviour, it doesn't seem necessary to backport that far.
The mapping is now accounted at mmap time and can fail with
-ENOMEMunder strict overcommit, and read faults allocate folios. However this is normal behaviour for a read-only shmem mapping.Commit
93c0c8dc87f6("mm/rmap: use anon pgoff to trackMAP_PRIVATEfile-backed anon folios") is the first patch at which the debug assert fires, so target that instead.Fixes 93c0c8dc87f6 ("mm/rmap: use anon pgoff to track MAP_PRIVATE file-backed anon folios")Reported-by [email protected]Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- drivers/char/mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 13 lines
drivers/char/mem.c static int mmap_zero_prepare(struct vm_area_desc *desc) · line 507
Quoted 11 lines
drivers/char/mem.c +1
-1@@ -503,7 +503,7 @@ static int mmap_zero_prepare(struct vm_area_desc *desc)#ifndef CONFIG_MMU return -ENOSYS ;#endif -if (vma_desc_test (desc,VMA_SHARED_BIT )) +if (vma_desc_test (desc,VMA_MAYSHARE_BIT ))return shmem_zero_setup_desc (desc);What's the odds that something "big" actually relies on this?
I think it'd be fairly crazy to, unless people really care about read-faulting a lot of zero pages which they want to somehow not account properly.
Anything's possible but I think on this, right to take a (v. small) risk!
Quoted 4 lines
I'm all for the change, but be prepared in about 5 years for people to start complaining :)
Quoted 6 lines
Quoted 1 line
Want me to take it through my tree?
Sure, thanks, though there will be a conflict against a patch in mm.
Though it's really that the file is moved (to
mm/), so actually should be an easy resolution?
No problem, you can take it if that makes it easier:
Quoted 44 lines · Lorenzo Stoakes (ARM)
Rather surprisingly, opening
/dev/zeroread-only then mmap()'ing itMAP_SHAREDgets you true anonymous memory (albeit in a VMA with non-NULLvma->vm_file).This is a by-product of MAP_PRIVATE-/dev/zero being how anonymous memory was mapped in Linux's distant past.
It happens because
mmap_zero_prepare()gates onVMA_SHARED_BITand when mapping a read-only fileMAP_SHARED,do_mmap()clearsVMA_SHARED_BITandVMA_MAYWRITE_BIT.The gating is incorrect - the (poorly named)
VMA_MAYSHARE_BITflag exists explicitly to tell you if something was originally mappedMAP_SHARED.So the fix is simple - gate on this instead.
This isn't exactly a common use case, but it's unexpected behaviour which now causes an assert if
CONFIG_DEBUG_VMis set.While this bug has existed since the dawn of time for linux (or at least since 2.6.12), it hasn't caused issues in the past, so while it's incorrect behaviour, it doesn't seem necessary to backport that far.
The mapping is now accounted at mmap time and can fail with
-ENOMEMunder strict overcommit, and read faults allocate folios. However this is normal behaviour for a read-only shmem mapping.Commit
93c0c8dc87f6("mm/rmap: use anon pgoff to trackMAP_PRIVATEfile-backed anon folios") is the first patch at which the debug assert fires, so target that instead.Fixes 93c0c8dc87f6 ("mm/rmap: use anon pgoff to track MAP_PRIVATE file-backed anon folios")Reported-by [email protected]Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>--- drivers/char/mem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/char/mem.c +0
-0
drivers/char/mem.c static int mmap_zero_prepare(struct vm_area_desc *desc) · line 508
Quoted 7 lines
drivers/char/mem.c +1
-1@@ -503,7 +503,7 @@ static int mmap_zero_prepare(struct vm_area_desc *desc)#ifndef CONFIG_MMU return -ENOSYS ;#endif -if (vma_desc_test (desc,VMA_SHARED_BIT )) +if (vma_desc_test (desc,VMA_MAYSHARE_BIT ))return shmem_zero_setup_desc (desc);
So instead of shared zeropages we'd now get zero-filled shmem pages.
The alternative would be to just convert it to a proper read-only COW mapping in mmap code:
* Not clearing VM_MAYWRITE, but keeping VM_WRITE clear
* Clearing VMA_SHARED and VMA_MAYSHARE
Sure, someone could then mprotect(PROT_WRITE that thing) or FOLL_FORCE|FOLL_WRITE to get anonymous memory. Just raising that as an alternative.
Signature · 3 lines
Cheers, David
Andrew Morton
maintainerQuoted 8 lines · On Thu, 24 Sep 2026 17:37:29 +0200 "David Hildenbrand (Arm)"…
Quoted 4 lines · Lorenzo Stoakes (ARM)
Rather surprisingly, opening
/dev/zeroread-only then mmap()'ing itMAP_SHAREDgets you true anonymous memory (albeit in a VMA with non-NULLvma->vm_file)....
Quoted 12 lines
drivers/char/mem.c static int mmap_zero_prepare(struct vm_area_desc *desc) · line 508
Quoted 10 lines
drivers/char/mem.c +1
-1@@ -503,7 +503,7 @@ static int mmap_zero_prepare(struct vm_area_desc *desc)#ifndef CONFIG_MMU return -ENOSYS ;#endif -if (vma_desc_test (desc,VMA_SHARED_BIT )) +if (vma_desc_test (desc,VMA_MAYSHARE_BIT ))return shmem_zero_setup_desc (desc);So instead of shared zeropages we'd now get zero-filled shmem pages.
"zeropage". Singular. Used to be!
The accounting differences, possible changes in reclaim, memcg charging, maybe swap behavior. Switching to a different fault handler. It's hard to foresee all the effects of this.
Quoted 7 lines
The alternative would be to just convert it to a proper read-only COW mapping in mmap code:
* Not clearingVM_MAYWRITE, but keepingVM_WRITEclear
* ClearingVMA_SHAREDandVMA_MAYSHARESure, someone could then
mprotect(PROT_WRITE that thing)or FOLL_FORCE|FOLL_WRITE to get anonymous memory. Just raising that as an alternative.
I dunno, the whole thing feels imprudent. To alter such longstanding core(ish) behavior. And why? Because a shiny new assertion said "hey, that isn't quite right". Wouldn't it be better to squish the warning somehow and to set about this change in a very careful way?
Quoted 23 lines · Andrew Morton
Quoted 8 lines · On Thu, 24 Sep 2026 17:37:29 +0200 "David Hildenbrand (Arm)"…
Quoted 4 lines · Lorenzo Stoakes (ARM)
Rather surprisingly, opening
/dev/zeroread-only then mmap()'ing itMAP_SHAREDgets you true anonymous memory (albeit in a VMA with non-NULLvma->vm_file)....
Quoted 12 lines
drivers/char/mem.c static int mmap_zero_prepare(struct vm_area_desc *desc) · line 508
Quoted 10 lines
drivers/char/mem.c +1
-1@@ -503,7 +503,7 @@ static int mmap_zero_prepare(struct vm_area_desc *desc)#ifndef CONFIG_MMU return -ENOSYS ;#endif -if (vma_desc_test (desc,VMA_SHARED_BIT )) +if (vma_desc_test (desc,VMA_MAYSHARE_BIT ))return shmem_zero_setup_desc (desc);So instead of shared zeropages we'd now get zero-filled shmem pages.
Quoted 1 line
"zeropage". Singular. Used to be!
Hey, leave that German native speaker alone! :P
Yes, you'd get the shared zeropage multiple times. (some architectures like s390x do have multiple ones .... likely you could even get the huge zero folio here)
... unless the MM has the shared zeropage disabled, and fallback to anonymous memory:
-> mm_forbids_zeropage()
... we end up using THPs and the huge zero folio is disallowed, so we fallback to a anonymous THPs
-> transparent_hugepage_use_zero_page()
Quoted 10 lines
The accounting differences, possible changes in reclaim, memcg charging, maybe swap behavior. Switching to a different fault handler. It's hard to foresee all the effects of this.
Quoted 5 lines
The alternative would be to just convert it to a proper read-only COW mapping in mmap code:
* Not clearingVM_MAYWRITE, but keepingVM_WRITEclear
* ClearingVMA_SHAREDandVMA_MAYSHARE
Quoted 7 lines
Quoted 2 lines
Sure, someone could then
mprotect(PROT_WRITE that thing)or FOLL_FORCE|FOLL_WRITE to get anonymous memory. Just raising that as an alternative.I dunno, the whole thing feels imprudent. To alter such longstanding core(ish) behavior. And why? Because a shiny new assertion said "hey, that isn't quite right". Wouldn't it be better to squish the warning somehow and to set about this change in a very careful way?
We really shouldn't allow anonymous pages in non-cow mappings.
We can
a) Disallow allocating an anon_vma and fail gracefully. So only a shared zeropage could ever get mapped there. Might break the s390x mm_forbids_zeropage(). But given that's only used in hypervisors like QEMU, unlikely.
b) Do what Lorenzo proposes. This will allocate real memory. Someone decided to use MAP_SHARED, for unknown reasons, so I'd assume it's unlikely that something breaks, but you have a point.
c) Convert them to proper COW mappings. After all, having the file read-only is absolutely irrelevant, because we will never ever use that file. It's anonymous memory.
Signature · 3 lines
Cheers, David
Quoted 54 lines · David Hildenbrand (Arm)
Quoted 23 lines · Andrew Morton
Quoted 8 lines · On Thu, 24 Sep 2026 17:37:29 +0200 "David Hildenbrand (Arm)"…
Quoted 4 lines · Lorenzo Stoakes (ARM)
Rather surprisingly, opening
/dev/zeroread-only then mmap()'ing itMAP_SHAREDgets you true anonymous memory (albeit in a VMA with non-NULLvma->vm_file)....
Quoted 12 lines
drivers/char/mem.c static int mmap_zero_prepare(struct vm_area_desc *desc) · line 508
Quoted 10 lines
drivers/char/mem.c +1
-1@@ -503,7 +503,7 @@ static int mmap_zero_prepare(struct vm_area_desc *desc)#ifndef CONFIG_MMU return -ENOSYS ;#endif -if (vma_desc_test (desc,VMA_SHARED_BIT )) +if (vma_desc_test (desc,VMA_MAYSHARE_BIT ))return shmem_zero_setup_desc (desc);So instead of shared zeropages we'd now get zero-filled shmem pages.
Quoted 1 line
"zeropage". Singular. Used to be!
Hey, leave that German native speaker alone! :P
Yes, you'd get the shared zeropage multiple times. (some architectures like s390x do have multiple ones .... likely you could even get the huge zero folio here)
... unless the MM has the shared zeropage disabled, and fallback to anonymous memory:
->
mm_forbids_zeropage()... we end up using THPs and the huge zero folio is disallowed, so we fallback to a anonymous THPs
->
transparent_hugepage_use_zero_page()Quoted 13 lines
The accounting differences, possible changes in reclaim, memcg charging, maybe swap behavior. Switching to a different fault handler. It's hard to foresee all the effects of this.
Quoted 7 lines
The alternative would be to just convert it to a proper read-only COW mapping in mmap code:
* Not clearingVM_MAYWRITE, but keepingVM_WRITEclear
* ClearingVMA_SHAREDandVMA_MAYSHARESure, someone could then
mprotect(PROT_WRITE that thing)or FOLL_FORCE|FOLL_WRITE to get anonymous memory. Just raising that as an alternative.
Quoted 6 lines
Quoted 4 lines
I dunno, the whole thing feels imprudent. To alter such longstanding core(ish) behavior. And why? Because a shiny new assertion said "hey, that isn't quite right". Wouldn't it be better to squish the warning somehow and to set about this change in a very careful way?
We really shouldn't allow anonymous pages in non-cow mappings.
Yes agreed entirely.
It then becomes a question about how to get readonly memory.
Quoted 7 lines
We can
a) Disallow allocating an
anon_vmaand fail gracefully. So only a shared zeropage could ever get mapped there. Might break the s390xmm_forbids_zeropage(). But given that's only used in hypervisors like QEMU, unlikely.
Something like:
/* about to maybe prep anon_vma */
if (!vma_cow_mapping(vma) && vma_test(vma, VMA_MAYSHARE_BIT)) {
/* don't prep anon give zero page */
}?
I think though it's surely the only case (I hope!) where you can possibly be both anon (as in missing vm_ops) and !CoW? I hope? :)
So it feels better to fix it at the source.
OTOH maybe it's worth special-casing so we don't allocate on read.
But that brings me to c)...
Quoted 4 lines
b) Do what Lorenzo proposes. This will allocate real memory. Someone decided to use
MAP_SHARED, for unknown reasons, so I'd assume it's unlikely that something breaks, but you have a point.
I would say this patch is the right fix for the moment to fix the assert, and we can chase up with other approaches afterwards.
Quoted 5 lines
c) Convert them to proper COW mappings. After all, having the file read-only is absolutely irrelevant, because we will never ever use that file. It's anonymous memory.
...My idea for the next step for /dev/zero is to remove the mmap handler and have some specific code in the mmap logic for it solely.
Like we already have:
if (map->vm_file) error = __mmap_new_file_vma(map, vma); else if (!is_anon) error = shmem_zero_setup(vma);
And there's already specific file_is_dev_zero() code, so there you could simply decide:
CoW /dev/zero -> R/W anon
shared readonly /dev/zero -> R/O CoW (i.e. with VMA_MAYWRITE_BIT set)
As a special case because somebody really probably does want that.
But for the purposes of a 7.3 fix I think let's go with b) [i.e. this patch] and follow up if that makes sense to you?
Quoted 4 lines
--
Cheers,David
--
Cheers, Lorenzo
David Hildenbrand (Arm)
Acked-byQuoted 14 lines
It then becomes a question about how to get readonly memory.
Quoted 7 lines
We can
a) Disallow allocating an
anon_vmaand fail gracefully. So only a shared zeropage could ever get mapped there. Might break the s390xmm_forbids_zeropage(). But given that's only used in hypervisors like QEMU, unlikely.Something like:
Quoted 7 lines
/* about to maybe prep anon_vma */ if (!vma_cow_mapping(vma) && vma_test(vma, VMA_MAYSHARE_BIT)) { /* don't prep anon give zero page */ }?
Something like that, but really just failing directly from anon-vma preparation in a !vma_cow_mapping(vma), maybe using a special error code.
But the less we have to hack around this special case, the better.
Quoted 12 lines
I think though it's surely the only case (I hope!) where you can possibly be both anon (as in missing
vm_ops) and !CoW? I hope? :)So it feels better to fix it at the source.
OTOH maybe it's worth special-casing so we don't allocate on read.
But that brings me to c)...
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
b) Do what Lorenzo proposes. This will allocate real memory. Someone decided to use
MAP_SHARED, for unknown reasons, so I'd assume it's unlikely that something breaks, but you have a point.I would say this patch is the right fix for the moment to fix the assert, and we can chase up with other approaches afterwards.
It will get backported, so we better be careful.
IIUC, the real change is only for /dev/zero users that
fd = open("/dev/zero", O_RDONLY);mmap(fd, MAP_SHARED, PROT_READ);
Which is rather something odd to do indeed.
Letting AI do some digging ... it says there are not known users (I don't trust it, but I suspect at least there are no prominent users, which makes sense ...).
Getting shared memory when requesting MAP_SHARED does sound like the right thing to do ...
So let's try this:
Signature · 3 lines
Cheers, David
Quoted 59 lines · David Hildenbrand (Arm)
Quoted 14 lines
It then becomes a question about how to get readonly memory.
Quoted 7 lines
We can
a) Disallow allocating an
anon_vmaand fail gracefully. So only a shared zeropage could ever get mapped there. Might break the s390xmm_forbids_zeropage(). But given that's only used in hypervisors like QEMU, unlikely.Something like:
Quoted 7 lines
/* about to maybe prep anon_vma */ if (!vma_cow_mapping(vma) && vma_test(vma, VMA_MAYSHARE_BIT)) { /* don't prep anon give zero page */ }?
Something like that, but really just failing directly from anon-vma preparation in a !vma_cow_mapping(vma), maybe using a special error code.
But the less we have to hack around this special case, the better.
Quoted 12 lines
I think though it's surely the only case (I hope!) where you can possibly be both anon (as in missing
vm_ops) and !CoW? I hope? :)So it feels better to fix it at the source.
OTOH maybe it's worth special-casing so we don't allocate on read.
But that brings me to c)...
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
b) Do what Lorenzo proposes. This will allocate real memory. Someone decided to use
MAP_SHARED, for unknown reasons, so I'd assume it's unlikely that something breaks, but you have a point.I would say this patch is the right fix for the moment to fix the assert, and we can chase up with other approaches afterwards.
It will get backported, so we better be careful.
IIUC, the real change is only for
/dev/zerousers thatfd =
open("/dev/zero", O_RDONLY);mmap(fd, MAP_SHARED, PROT_READ);Which is rather something odd to do indeed.
Letting AI do some digging ... it says there are not known users (I don't trust it, but I suspect at least there are no prominent users, which makes sense ...).
Quoted 6 lines
Getting shared memory when requesting
MAP_SHAREDdoes sound like the right thing to do ...So let's try this:
Thanks!
Yeah let's revisit this one later more broadly.
Quoted 5 lines
--
Cheers,David
--
Cheers, Lorenzo
Andrew Morton
maintainerQuoted 11 lines · On Mon, 28 Sep 2026 13:59:47 +0200 "David Hildenbrand (Arm)"…
Quoted 9 lines
Quoted 6 lines
b) Do what Lorenzo proposes. This will allocate real memory. Someone decided to use
MAP_SHARED, for unknown reasons, so I'd assume it's unlikely that something breaks, but you have a point.I would say this patch is the right fix for the moment to fix the assert, and we can chase up with other approaches afterwards.
It will get backported, so we better be careful.
I don't think it'll get backported. I hope not.
The Fixes: target of 93c0c8dc87f6 ("mm/rmap: use anon pgoff to track MAP_PRIVATE file-backed anon folios") is new in v7.3-rc1, so this will go in as a hotfix against this -rc cycle only.
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Mon, 28 Sep 2026 13:59:47 +0200 "David Hildenbrand (Arm)"…
Quoted 5 lines
I would say this patch is the right fix for the moment to fix the assert, and we can chase up with other approaches afterwards.
Quoted 7 lines
Quoted 1 line
It will get backported, so we better be careful.
I don't think it'll get backported. I hope not.
The Fixes: target of
93c0c8dc87f6("mm/rmap: use anon pgoff to trackMAP_PRIVATEfile-backed anon folios") is new in v7.3-rc1, so this will go in as a hotfix against this -rc cycle only.
Ah, ok. I was assuming for a second that we can trigger some otherwise unpleasant conditions with anon folios in non-cow mappings. If we're not aware of any, it is indeed not relevant to backport.
Signature · 3 lines
Cheers, David
Quoted 31 lines · Lorenzo Stoakes (ARM)
Rather surprisingly, opening
/dev/zeroread-only then mmap()'ing itMAP_SHAREDgets you true anonymous memory (albeit in a VMA with non-NULLvma->vm_file).This is a by-product of MAP_PRIVATE-/dev/zero being how anonymous memory was mapped in Linux's distant past.
It happens because
mmap_zero_prepare()gates onVMA_SHARED_BITand when mapping a read-only fileMAP_SHARED,do_mmap()clearsVMA_SHARED_BITandVMA_MAYWRITE_BIT.The gating is incorrect - the (poorly named)
VMA_MAYSHARE_BITflag exists explicitly to tell you if something was originally mappedMAP_SHARED.So the fix is simple - gate on this instead.
This isn't exactly a common use case, but it's unexpected behaviour which now causes an assert if
CONFIG_DEBUG_VMis set.While this bug has existed since the dawn of time for linux (or at least since 2.6.12), it hasn't caused issues in the past, so while it's incorrect behaviour, it doesn't seem necessary to backport that far.
The mapping is now accounted at mmap time and can fail with
-ENOMEMunder strict overcommit, and read faults allocate folios. However this is normal behaviour for a read-only shmem mapping.Commit
93c0c8dc87f6("mm/rmap: use anon pgoff to trackMAP_PRIVATEfile-backed anon folios") is the first patch at which the debug assert fires, so target that instead.
Quoted 5 lines
Fixes 93c0c8dc87f6 ("mm/rmap: use anon pgoff to track MAP_PRIVATE file-backed anon folios")Reported-by [email protected]Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>---