arm64: pageattr: Explicitly bail out when changing permissions for vmalloc_huge mappings
PATCH 1/1 arm64: pageattr: Explicitly bail out when changing permissions for vmalloc_huge mappings
arm64 uses apply_to_page_range to change permissions for kernel VA mappings, which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for VM_ALLOW_HUGE_VMAP mappings.
--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/mm/pageattr.c +2 -2
@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that * splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely
@@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)
Signature · 1 line
2.30.2
Quoted 2 lines · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings, which does not support changing permissions for leaf mappings. This function
I think you mean "block" mappings here? A leaf mapping refers to a page table entry that maps a piece of memory at any level in the pgtable (i.e. a present entry that does not map a table).
A block mapping is an Arm ARM term used to mean a leaf mapping at a level other than the last level (e.g. pmd, pud). A page mapping is an Arm ARM term used to mean a leaf mapping at the last level (e.g. pte).
Quoted 1 line
will change permissions until it encounters a leaf mapping, and will bail
block mapping
Quoted 2 lines
out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.
It will also emit a warning. Since there are no reports of this triggering, it implies that there are currently no cases of code doing a vmalloc_huge() followed by partial permission change, at least on arm64 (I'm told BPF does do this on x86 though). But this is a footgun waiting to go off, so let's detect it early and avoid the possibility of permissions in an intermediate state. (It might be worth wordsmithing this into the commit log).
Quoted 2 lines
Signed-off-by Dev Jain <[email protected]>
With the commit log fixed up:
Quoted 26 lines
--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +2
-2@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that * splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)
Quoted 19 lines · Ryan Roberts
Quoted 2 lines · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings, which does not support changing permissions for leaf mappings. This functionI think you mean "block" mappings here? A leaf mapping refers to a page table entry that maps a piece of memory at any level in the pgtable (i.e. a present entry that does not map a table).
A block mapping is an Arm ARM term used to mean a leaf mapping at a level other than the last level (e.g. pmd, pud). A page mapping is an Arm ARM term used to mean a leaf mapping at the last level (e.g. pte).
Quoted 1 line
will change permissions until it encounters a leaf mapping, and will bail
block mapping
Quoted 2 lines
out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.
Quoted 6 lines
It will also emit a warning. Since there are no reports of this triggering, it implies that there are currently no cases of code doing a
vmalloc_huge()followed by partial permission change, at least on arm64 (I'm told BPF does do this on x86 though). But this is a footgun waiting to go off, so let's detect it early and avoid the possibility of permissions in an intermediate state. (It might be worth wordsmithing this into the commit log).
Thanks, I will add this.
Quoted 7 lines
Quoted 3 lines
Signed-off-by Dev Jain <[email protected]>With the commit log fixed up:
Reviewed-by Ryan Roberts <[email protected]>
Thanks!
Quoted 28 lines
Quoted 27 lines
--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +2
-2@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that * splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,
for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Quoted 15 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.Signed-off-by Dev Jain <[email protected]>--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +0
-0
arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int numpages, · line 99
Quoted 5 lines
arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that
I'd keep mention of page mappings in the comment, e.g
* Disallow VM_ALLOW_HUGE_VMAP mappings to guarantee that only page
* mappings are updated and splitting is never needed.
With this and changelog updates Ryan asked for
Quoted 15 lines
arch/arm64/mm/pageattr.c +1
-1* splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)Signature · 1 line
2.30.2
Signature · 2 lines
Sincerely yours, Mike.
Quoted 13 lines · Mike Rapoport
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Quoted 6 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.Signed-off-by Dev Jain <[email protected]>
I wonder if we want a Fixes: tag here? It's certainly a *latent* bug.
Quoted 41 lines
Quoted 9 lines
--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +0
-0arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int numpages, · line 99
Quoted 5 lines
arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that I'd keep mention of page mappings in the comment, e.g
* Disallow
VM_ALLOW_HUGE_VMAPmappings to guarantee that only page
* mappings are updated and splitting is never needed.With this and changelog updates Ryan asked for
Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Quoted 15 lines
arch/arm64/mm/pageattr.c +1
-1* splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)Signature · 1 line
2.30.2
Quoted 8 lines · Ryan Roberts
Quoted 7 lines · Mike Rapoport
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.Signed-off-by Dev Jain <[email protected]>I wonder if we want a Fixes: tag here? It's certainly a *latent* bug.
I am struggling to find the commit till which we want to backport. Should it be e920722 (arm64: support huge vmalloc mappings)?
Quoted 43 lines
Quoted 42 lines
Quoted 10 lines
--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +0
-0arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int numpages, · line 99
Quoted 5 lines
arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that I'd keep mention of page mappings in the comment, e.g
* Disallow
VM_ALLOW_HUGE_VMAPmappings to guarantee that only page
* mappings are updated and splitting is never needed.With this and changelog updates Ryan asked for
Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Quoted 15 lines
arch/arm64/mm/pageattr.c +1
-1* splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)Signature · 1 line
2.30.2
Quoted 8 lines · Ryan Roberts
Quoted 7 lines · Mike Rapoport
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.Signed-off-by Dev Jain <[email protected]>I wonder if we want a Fixes: tag here? It's certainly a *latent* bug.
We have only a few places that use vmalloc_huge() or VM_ALLOW_HUGE_VMAP and if there was a code that plays permission games on these allocations, x86 set_memory would blow up immediately, so I don't think Fixes: is needed here.
Quoted 42 lines
Quoted 41 lines
Quoted 9 lines
--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +0
-0arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int numpages, · line 99
Quoted 5 lines
arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that I'd keep mention of page mappings in the comment, e.g
* Disallow
VM_ALLOW_HUGE_VMAPmappings to guarantee that only page
* mappings are updated and splitting is never needed.With this and changelog updates Ryan asked for
Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Quoted 15 lines
arch/arm64/mm/pageattr.c +1
-1* splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)Signature · 1 line
2.30.2
Signature · 2 lines
Sincerely yours, Mike.
Quoted 14 lines · Mike Rapoport
Quoted 13 lines · Ryan Roberts
Quoted 12 lines · Mike Rapoport
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Quoted 5 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Signed-off-by Dev Jain <[email protected]>I wonder if we want a Fixes: tag here? It's certainly a *latent* bug.
We have only a few places that use
vmalloc_huge()orVM_ALLOW_HUGE_VMAPand if there was a code that plays permission games on these allocations, x86set_memorywould blow up immediately, so I don't think Fixes: is needed here.
But I think x86 can handle this (split_large_page() in __change_page_attr()) ?
Quoted 44 lines
Quoted 43 lines
Quoted 42 lines
Quoted 10 lines
--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +0
-0arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int numpages, · line 99
Quoted 5 lines
arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that I'd keep mention of page mappings in the comment, e.g
* Disallow
VM_ALLOW_HUGE_VMAPmappings to guarantee that only page
* mappings are updated and splitting is never needed.With this and changelog updates Ryan asked for
Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Quoted 15 lines
arch/arm64/mm/pageattr.c +1
-1* splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)Signature · 1 line
2.30.2
Quoted 21 lines · Dev Jain
Quoted 18 lines · Mike Rapoport
Quoted 16 lines · Ryan Roberts
Quoted 13 lines · Mike Rapoport
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Quoted 6 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.Signed-off-by Dev Jain <[email protected]>I wonder if we want a Fixes: tag here? It's certainly a *latent* bug.
Quoted 7 lines
Quoted 4 lines
We have only a few places that use
vmalloc_huge()orVM_ALLOW_HUGE_VMAPand if there was a code that plays permission games on these allocations, x86set_memorywould blow up immediately, so I don't think Fixes: is needed here.But I think x86 can handle this (
split_large_page()in__change_page_attr()) ?
Yes, but it also updates corresponding direct map entries when vmalloc permissions change and the direct map update presumes physical contiguity of the range.
Quoted 43 lines
Quoted 42 lines
Quoted 41 lines
Quoted 40 lines
Quoted 9 lines
--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +0
-0arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int numpages, · line 99
Quoted 5 lines
arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that I'd keep mention of page mappings in the comment, e.g
* Disallow
VM_ALLOW_HUGE_VMAPmappings to guarantee that only page
* mappings are updated and splitting is never needed.With this and changelog updates Ryan asked for
Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Quoted 14 lines
arch/arm64/mm/pageattr.c +1
-1* splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)Signature · 1 line
2.30.2
Signature · 2 lines
Sincerely yours, Mike.
Quoted 14 lines · Mike Rapoport
Quoted 13 lines · Ryan Roberts
Quoted 12 lines · Mike Rapoport
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Quoted 5 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Signed-off-by Dev Jain <[email protected]>I wonder if we want a Fixes: tag here? It's certainly a *latent* bug.
We have only a few places that use
vmalloc_huge()orVM_ALLOW_HUGE_VMAPand if there was a code that plays permission games on these allocations, x86set_memorywould blow up immediately, so I don't think Fixes: is needed here.
Hi Mike,
I think I may have misunderstood your comments when we spoke at LSF/MM the other day, as this statement seems to contradict. I thought you said that on x86 BPF allocates memory using vmalloc_huge()/VM_ALLOW_HUGE_VMAP and then it's sub-allocator will set_memory_*() on a sub-region of that allocation? (And we then agreed that it would be good for arm64 to eventually support this with BBML2).
Anyway, regardless, I think this change is useful first step to improving vmalloc as it makes us more defensive against any future attempt to change permissions on a huge allocation. In the long term I'd like to get to the point where arm64 (with BBML2) can map with VM_ALLOW_HUGE_VMAP by default.
Thanks,
Ryan
Quoted 44 lines
Quoted 43 lines
Quoted 42 lines
Quoted 10 lines
--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +0
-0arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int numpages, · line 99
Quoted 5 lines
arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that I'd keep mention of page mappings in the comment, e.g
* Disallow
VM_ALLOW_HUGE_VMAPmappings to guarantee that only page
* mappings are updated and splitting is never needed.With this and changelog updates Ryan asked for
Reviewed-by Mike Rapoport (Microsoft) <[email protected]>Quoted 15 lines
arch/arm64/mm/pageattr.c +1
-1* splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)Signature · 1 line
2.30.2
Hi Ryan,
Quoted 24 lines · Ryan Roberts
Quoted 14 lines · Mike Rapoport
Quoted 13 lines · Ryan Roberts
Quoted 12 lines · Mike Rapoport
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Quoted 5 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Signed-off-by Dev Jain <[email protected]>I wonder if we want a Fixes: tag here? It's certainly a *latent* bug.
We have only a few places that use
vmalloc_huge()orVM_ALLOW_HUGE_VMAPand if there was a code that plays permission games on these allocations, x86set_memorywould blow up immediately, so I don't think Fixes: is needed here.
Quoted 7 lines
Hi Mike,
I think I may have misunderstood your comments when we spoke at LSF/MM the other day, as this statement seems to contradict. I thought you said that on x86 BPF allocates memory using vmalloc_huge()/VM_ALLOW_HUGE_VMAP and then it's sub-allocator will set_memory_*() on a sub-region of that allocation? (And we then agreed that it would be good for arm64 to eventually support this with BBML2).
I misremembered :)
They do allocate several PMD_SIZE chunks at once, but they don't use vmalloc_huge(), so everything there is mapped with base pages.
And now they are using execmem rather than vmalloc directly, and execmem doesn't use VM_ALLOW_HUGE_VMAP anywhere except modules on x86.
Quoted 7 lines
Anyway, regardless, I think this change is useful first step to improving vmalloc as it makes us more defensive against any future attempt to change permissions on a huge allocation. In the long term I'd like to get to the point where arm64 (with BBML2) can map with
VM_ALLOW_HUGE_VMAPby default.Thanks,
Ryan
Signature · 2 lines
Sincerely yours, Mike.
Quoted 35 lines · Mike Rapoport
Hi Ryan,
Quoted 24 lines · Ryan Roberts
Quoted 14 lines · Mike Rapoport
Quoted 13 lines · Ryan Roberts
Quoted 12 lines · Mike Rapoport
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Quoted 5 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Signed-off-by Dev Jain <[email protected]>I wonder if we want a Fixes: tag here? It's certainly a *latent* bug.
We have only a few places that use
vmalloc_huge()orVM_ALLOW_HUGE_VMAPand if there was a code that plays permission games on these allocations, x86set_memorywould blow up immediately, so I don't think Fixes: is needed here.Quoted 7 lines
Hi Mike,
I think I may have misunderstood your comments when we spoke at LSF/MM the other day, as this statement seems to contradict. I thought you said that on x86 BPF allocates memory using vmalloc_huge()/VM_ALLOW_HUGE_VMAP and then it's sub-allocator will set_memory_*() on a sub-region of that allocation? (And we then agreed that it would be good for arm64 to eventually support this with BBML2).
Quoted 6 lines
I misremembered :)
They do allocate severalPMD_SIZEchunks at once, but they don't usevmalloc_huge(), so everything there is mapped with base pages.And now they are using execmem rather than vmalloc directly, and execmem doesn't use
VM_ALLOW_HUGE_VMAPanywhere except modules on x86.
Ahh OK! Like I said, I don't think it reduces the value of this patch though.
Quoted 9 lines
Quoted 8 lines
Anyway, regardless, I think this change is useful first step to improving vmalloc as it makes us more defensive against any future attempt to change permissions on a huge allocation. In the long term I'd like to get to the point where arm64 (with BBML2) can map with
VM_ALLOW_HUGE_VMAPby default.Thanks,
Ryan
Quoted 6 lines · Mike Rapoport
Quoted 1 line · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings,for vmalloc mappings ^
arm64 does not allow changing permissions to any VA address right now.
Sorry, mistakenly used them interchangeably. I'll fix this.
Quoted 24 lines
Quoted 16 lines
which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions for
VM_ALLOW_HUGE_VMAPmappings.Signed-off-by Dev Jain <[email protected]>--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +0
-0arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int numpages, · line 99
Quoted 5 lines
arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that I'd keep mention of page mappings in the comment, e.g
Quoted 6 lines
* Disallow
VM_ALLOW_HUGE_VMAPmappings to guarantee that only page
* mappings are updated and splitting is never needed.With this and changelog updates Ryan asked for
Reviewed-by Mike Rapoport (Microsoft) <[email protected]>
Thanks!
Quoted 18 lines
Quoted 17 lines
arch/arm64/mm/pageattr.c +1
-1* splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely @@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;if (!numpages)Signature · 1 line
2.30.2
Quoted 25 lines · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings, which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions forVM_ALLOW_HUGE_VMAPmappings.Signed-off-by Dev Jain <[email protected]>--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages,* we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). -* Those are guaranteed to consist entirely of page mappings, and +* Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that * splitting is never needed. * * So check whether the [addr, addr + size) interval is entirely
arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int numpages, · line 109
Quoted 6 lines
arch/arm64/mm/pageattr.c +1
-1@@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area =find_vm_area ((void *)addr);if (!area || end > (unsigned long )kasan_reset_tag (area->addr) + area->size || - !(area->flags &VM_ALLOC )) + ((area->flags & (VM_ALLOC |VM_ALLOW_HUGE_VMAP )) !=VM_ALLOC ))return -EINVAL ;
I happened to find this patch when I was looking into fixing "splitting is never needed" comment to reflect the latest change with BBML2_NOABORT and tried to relax this restriction. I agree with the justification for this patch to make the code more robust for permission update on partial range. But the following linear mapping permission update code seems still assume partial range update never happens:
for (i = 0; i < area->nr_pages; i++) {
It iterates all pages for this vm area from the first page then update their permissions. So I think we should do the below to make it more robust to partial range update like this patch did:
arch/arm64/mm/pageattr.c +0 -0
@@ -185,8 +185,9 @@ static int change_memory_common(unsigned long addr, int numpages,
*/
if (rodata_full && (pgprot_val(set_mask) == PTE_RDONLY || pgprot_val(clear_mask) == PTE_RDONLY)) {
- for (i = 0; i < area->nr_pages; i++) {
- __change_memory_common((u64)page_address(area->pages[i]),
+ unsigned long idx = (start - (unsigned long)area->addr) >> PAGE_SHIFT; + for (i = 0; i < numpages; i++) {
+ __change_memory_common((u64)page_address(area->pages[idx++]), PAGE_SIZE, set_mask, clear_mask); }
}
Just build tested. Does it look reasonable?
Thanks,
Yang
Quoted 2 lines
arch/arm64/mm/pageattr.c +0
-0if (!numpages)
Hi Yang,
Quoted 44 lines · Yang Shi
Quoted 28 lines · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings, which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions forVM_ALLOW_HUGE_VMAPmappings.Signed-off-by Dev Jain <[email protected]>---
arch/arm64/mm/pageattr.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages, * we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). - * Those are guaranteed to consist entirely of page mappings, and + * Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that * splitting is never needed. ** So check whether the [addr, addr + size) interval is entirely
arch/arm64/mm/pageattr.c +0
-0@@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area = find_vm_area((void *)addr);Quoted 5 lines
if (!area ||
end > (unsigned long)kasan_reset_tag(area->addr) +area->size|| - !(area->flags &VM_ALLOC))
+ ((area->flags& (VM_ALLOC|VM_ALLOW_HUGE_VMAP)) !=VM_ALLOC)) return-EINVAL;I happened to find this patch when I was looking into fixing "splitting is never needed" comment to reflect the latest change with
BBML2_NOABORTand tried to relax this restriction. I agree with the justification for this patch to make the code more robust for permission update on partial range. But the following linear mapping permission update code seems still assume partial range update never happens:
Quoted 5 lines
for (i = 0; i <
area->nr_pages; i++) {It iterates all pages for this vm area from the first page then update their permissions. So I think we should do the below to make it more robust to partial range update like this patch did:
Ahh so the issue is that [addr, addr + numpages * PAGE_SIZE) may only cover a portion of the vm area? But the current code updates the permissions for the whole vm area? Ouch...
Quoted 18 lines
arch/arm64/mm/pageattr.c +0
-0@@ -185,8 +185,9 @@ static int change_memory_common(unsigned long addr, intnumpages, */ if (
rodata_full&& (pgprot_val(set_mask)==PTE_RDONLY||pgprot_val(clear_mask)==PTE_RDONLY)) { - for (i = 0; i <area->nr_pages; i++) {
- __change_memory_common((u64)page_address(area->pages[i]),
+ unsigned long idx = (start - (unsigned long)area->addr) >>PAGE_SHIFT;
+ for (i = 0; i < numpages; i++) {
+ __change_memory_common((u64)page_address(area->pages[idx++]),PAGE_SIZE,set_mask,clear_mask); }
}
Quoted 1 line
Just build tested. Does it look reasonable?
Yes that looks correct to me! Will you submit a patch?
Thanks,
Ryan
Quoted 7 lines
Thanks,
YangQuoted 1 line
if (!numpages)
Quoted 46 lines · Ryan Roberts
Hi Yang,
Quoted 42 lines · Yang Shi
Quoted 26 lines · Dev Jain
arm64 uses
apply_to_page_rangeto change permissions for kernel VA mappings, which does not support changing permissions for leaf mappings. This function will change permissions until it encounters a leaf mapping, and will bail out. To avoid this partial change, explicitly disallow changing permissions forVM_ALLOW_HUGE_VMAPmappings.Signed-off-by Dev Jain <[email protected]>--- arch/arm64/mm/pageattr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/arm64/mm/pageattr.c +1
-1@@ -96,7 +96,7 @@ static int change_memory_common(unsigned long addr, int numpages, * we are operating on does not result in such splitting. * * Let's restrict ourselves to mappings created by vmalloc (or vmap). - * Those are guaranteed to consist entirely of page mappings, and + * Disallow VM_ALLOW_HUGE_VMAP vmalloc mappings so that * splitting is never needed. ** So check whether the [addr, addr + size) interval is entirely
arch/arm64/mm/pageattr.c static int change_memory_common(unsigned long addr, int · line 110
Quoted 7 lines
arch/arm64/mm/pageattr.c +1
-1@@ -105,7 +105,7 @@ static int change_memory_common(unsigned long addr, int numpages, area = find_vm_area((void *)addr); if (!area || end > (unsigned long)kasan_reset_tag(area->addr) + area->size || - !(area->flags & VM_ALLOC)) + ((area->flags & (VM_ALLOC | VM_ALLOW_HUGE_VMAP)) != VM_ALLOC)) return -EINVAL;I happened to find this patch when I was looking into fixing "splitting is never needed" comment to reflect the latest change with
BBML2_NOABORTand tried to relax this restriction. I agree with the justification for this patch to make the code more robust for permission update on partial range. But the following linear mapping permission update code seems still assume partial range update never happens:
Quoted 8 lines
Quoted 5 lines
for (i = 0; i <
area->nr_pages; i++) {It iterates all pages for this vm area from the first page then update their permissions. So I think we should do the below to make it more robust to partial range update like this patch did:
Ahh so the issue is that [addr, addr + numpages *
PAGE_SIZE) may only cover a portion of the vm area? But the current code updates the permissions for the whole vm area? Ouch...
Yes. I didn't see anyone actually does partial range update as the earlier discussion said, but this is another "footgun waiting to go off" too. We'd better to get aligned with this patch.
Quoted 18 lines
Quoted 18 lines
arch/arm64/mm/pageattr.c +3
-2@@ -185,8 +185,9 @@ static int change_memory_common(unsigned long addr, intnumpages, */ if (rodata_full && (pgprot_val(set_mask) == PTE_RDONLY || pgprot_val(clear_mask) == PTE_RDONLY)) { - for (i = 0; i < area->nr_pages; i++) { - __change_memory_common((u64)page_address(area->pages[i]), + unsigned long idx = (start - (unsigned long)area->addr) >>PAGE_SHIFT ; + for (i = 0; i < numpages; i++) { + __change_memory_common((u64)page_address(area->pages[idx++]), PAGE_SIZE, set_mask, clear_mask);}
}
Quoted 2 lines
Quoted 1 line
Just build tested. Does it look reasonable?
Yes that looks correct to me! Will you submit a patch?
Yes. I will prepare the patches once -rc1 is available.
Thanks,
Yang
Quoted 9 lines
Thanks,
RyanQuoted 5 lines
Thanks,
YangQuoted 1 line
arch/arm64/mm/pageattr.c +0
-0if (!numpages)