maple_tree: Fix MA_STATE_PREALLOC flag in mas_preallocate()
Subsystems (3)
RFC PATCH 1/1 maple_tree: Fix MA_STATE_PREALLOC flag in mas_preallocate()
Temporarily clear the preallocation flag when explicitly requesting allocations. Pre-existing allocations are already counted against the request through mas_node_count_gfp(), but the allocations will not happen if the MA_STATE_PREALLOC flag is set. This flag is meant to avoid re-allocating in bulk allocation mode, and to detect issues with preallocation calculations.
The MA_STATE_PREALLOC flag should also always be set on zero allocations so that detection of underflow allocations will print a WARN_ON() during consumption.
User visible effect of this flaw is a WARN_ON() followed by a null pointer dereference when subsequent requests for larger number of nodes is ignored, such as the vma merge retry in mmap_region() caused by drivers altering the vma flags.
--- lib/maple_tree.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
Only the MA_STATE_PREALLOC flag needs to be fixed to avoid the vma iterator issue. Doing the minimum change here mitigates risk in the stable kernels.
If this fixes the issue, I'll resend without the RFC and add Stable to the Cc list.
Thanks again, Hailong for the work on this issue. Your testcase helped me narrow the bug down in the end. I appreciate all the work and support provided by the Android partners, especially the involvement on the mailing list!
lib/maple_tree.c +9 -5
@@ -5508,7 +5508,7 @@ int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)/* At this point, we are at the leaf node that needs to be altered. */ /* Exact fit, no nodes needed. */ if (wr_mas.r_min == mas->index && wr_mas.r_max == mas->last) -return 0 ; + goto ask_zero;mas_wr_end_piv (&wr_mas); node_size =mas_wr_new_end (&wr_mas);
@@ -5517,10 +5517,11 @@ int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)if (node_size == wr_mas.node_end) {/* reuse node */ if (!mt_in_rcu (mas->tree)) -return 0 ; + goto ask_zero; +/* shifting boundary */ if (wr_mas.offset_end - mas->offset ==1 ) -return 0 ; + goto ask_zero; }if (node_size >= mt_slots[wr_mas.type]) {
@@ -5539,10 +5540,13 @@ int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)/* node store, slot store needs one node */ ask_now: + mas->mas_flags &= ~MA_STATE_PREALLOC ;mas_node_count_gfp (mas, request, gfp); - mas->mas_flags |=MA_STATE_PREALLOC ; -if (likely (!mas_is_err (mas))) +if (likely (!mas_is_err (mas))) { +ask_zero: + mas->mas_flags |=MA_STATE_PREALLOC ;return 0 ; + }mas_set_alloc_req (mas,0 ); ret =xa_err (mas->node);
Signature · 1 line
2.47.2
Quoted 37 lines · Liam R. Howlett
Temporarily clear the preallocation flag when explicitly requesting allocations. Pre-existing allocations are already counted against the request through
mas_node_count_gfp(), but the allocations will not happen if theMA_STATE_PREALLOCflag is set. This flag is meant to avoid re-allocating in bulk allocation mode, and to detect issues with preallocation calculations.The
MA_STATE_PREALLOCflag should also always be set on zero allocations so that detection of underflow allocations will print aWARN_ON()during consumption.User visible effect of this flaw is a
WARN_ON()followed by a null pointer dereference when subsequent requests for larger number of nodes is ignored, such as the vma merge retry inmmap_region()caused by drivers altering the vma flags.Reported-by Zhaoyang Huang <[email protected]>Reported-by Hailong Liu <[email protected]>Fixes 54a611b605901 ("Maple Tree: add new data structure")Cc Lorenzo Stoakes <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Hailong Liu <[email protected]>Cc Steve Kang <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Sidhartha Kumar <[email protected]>Signed-off-by Liam R. Howlett <[email protected]>--- lib/maple_tree.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)Only the
MA_STATE_PREALLOCflag needs to be fixed to avoid the vma iterator issue. Doing the minimum change here mitigates risk in the stable kernels.
Quoted 7 lines
If this fixes the issue, I'll resend without the RFC and add Stable to the Cc list.
Thanks again, Hailong for the work on this issue. Your testcase helped me narrow the bug down in the end. I appreciate all the work and support provided by the Android partners, especially the involvement on the mailing list!
That's my pleasure. I also learned a lot from you :)
Quoted 34 lines
lib/maple_tree.c +5
-3@@ -5508,7 +5508,7 @@ int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)/* At this point, we are at the leaf node that needs to be altered. */ /* Exact fit, no nodes needed. */ if (wr_mas.r_min == mas->index && wr_mas.r_max == mas->last) -return 0 ; + goto ask_zero;mas_wr_end_piv (&wr_mas); node_size =mas_wr_new_end (&wr_mas);@@ -5517,10 +5517,11 @@ int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)if (node_size == wr_mas.node_end) {/* reuse node */ if (!mt_in_rcu (mas->tree)) -return 0 ; + goto ask_zero; +/* shifting boundary */ if (wr_mas.offset_end - mas->offset ==1 ) -return 0 ; + goto ask_zero; }if (node_size >= mt_slots[wr_mas.type]) {@@ -5539,10 +5540,13 @@ int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)/* node store, slot store needs one node */ ask_now: + mas->mas_flags &= ~MA_STATE_PREALLOC ;mas_node_count_gfp (mas, request, gfp);
lib/maple_tree.c int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp) · line 5552
Quoted 10 lines
lib/maple_tree.c +4
-2- mas->mas_flags |=MA_STATE_PREALLOC ; -if (likely (!mas_is_err (mas))) +if (likely (!mas_is_err (mas))) { +ask_zero: + mas->mas_flags |=MA_STATE_PREALLOC ;return 0 ; + }mas_set_alloc_req (mas,0 ); ret =xa_err (mas->node);
Passed local test, planned to aging test.
Brs,
Hailong.
Quoted 89 lines · Hailong Liu
Quoted 37 lines · Liam R. Howlett
Temporarily clear the preallocation flag when explicitly requesting allocations. Pre-existing allocations are already counted against the request through
mas_node_count_gfp(), but the allocations will not happen if theMA_STATE_PREALLOCflag is set. This flag is meant to avoid re-allocating in bulk allocation mode, and to detect issues with preallocation calculations.The
MA_STATE_PREALLOCflag should also always be set on zero allocations so that detection of underflow allocations will print aWARN_ON()during consumption.User visible effect of this flaw is a
WARN_ON()followed by a null pointer dereference when subsequent requests for larger number of nodes is ignored, such as the vma merge retry inmmap_region()caused by drivers altering the vma flags.Reported-by Zhaoyang Huang <[email protected]>Reported-by Hailong Liu <[email protected]>Fixes 54a611b605901 ("Maple Tree: add new data structure")Cc Lorenzo Stoakes <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Hailong Liu <[email protected]>Cc Steve Kang <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Sidhartha Kumar <[email protected]>Signed-off-by Liam R. Howlett <[email protected]>--- lib/maple_tree.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)Only the
MA_STATE_PREALLOCflag needs to be fixed to avoid the vma iterator issue. Doing the minimum change here mitigates risk in the stable kernels.Quoted 7 lines
If this fixes the issue, I'll resend without the RFC and add Stable to the Cc list.
Thanks again, Hailong for the work on this issue. Your testcase helped me narrow the bug down in the end. I appreciate all the work and support provided by the Android partners, especially the involvement on the mailing list!
That's my pleasure. I also learned a lot from you :)
Quoted 42 lines
lib/maple_tree.c +9
-5@@ -5508,7 +5508,7 @@ int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)/* At this point, we are at the leaf node that needs to be altered. */ /* Exact fit, no nodes needed. */ if (wr_mas.r_min == mas->index && wr_mas.r_max == mas->last) -return 0 ; + goto ask_zero;mas_wr_end_piv (&wr_mas); node_size =mas_wr_new_end (&wr_mas);@@ -5517,10 +5517,11 @@ int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)if (node_size == wr_mas.node_end) {/* reuse node */ if (!mt_in_rcu (mas->tree)) -return 0 ; + goto ask_zero; +/* shifting boundary */ if (wr_mas.offset_end - mas->offset ==1 ) -return 0 ; + goto ask_zero; }if (node_size >= mt_slots[wr_mas.type]) {@@ -5539,10 +5540,13 @@ int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp)/* node store, slot store needs one node */ ask_now: + mas->mas_flags &= ~MA_STATE_PREALLOC ;mas_node_count_gfp (mas, request, gfp); - mas->mas_flags |=MA_STATE_PREALLOC ; -if (likely (!mas_is_err (mas))) +if (likely (!mas_is_err (mas))) { +ask_zero: + mas->mas_flags |=MA_STATE_PREALLOC ;return 0 ; + }
Quoted 3 lines
lib/maple_tree.c int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp) · line 5552
Quoted 2 lines
lib/maple_tree.c +0
-0mas_set_alloc_req (mas,0 ); ret =xa_err (mas->node);Passed local test, planned to aging test.
I also confirmed the issue is not reproducible with your patch. Thanks!
Quoted 4 lines
Brs,
Hailong.
Quoted 16 lines · Liam R. Howlett
Temporarily clear the preallocation flag when explicitly requesting allocations. Pre-existing allocations are already counted against the request through
mas_node_count_gfp(), but the allocations will not happen if theMA_STATE_PREALLOCflag is set. This flag is meant to avoid re-allocating in bulk allocation mode, and to detect issues with preallocation calculations.The
MA_STATE_PREALLOCflag should also always be set on zero allocations so that detection of underflow allocations will print aWARN_ON()during consumption.User visible effect of this flaw is a
WARN_ON()followed by a null pointer dereference when subsequent requests for larger number of nodes is ignored, such as the vma merge retry inmmap_region()caused by drivers altering the vma flags.
Quoted 13 lines
Reported-by Zhaoyang Huang <[email protected]>Reported-by Hailong Liu <[email protected]>Fixes 54a611b605901 ("Maple Tree: add new data structure")Cc Lorenzo Stoakes <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Hailong Liu <[email protected]>Cc Steve Kang <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Sidhartha Kumar <[email protected]>Signed-off-by Liam R. Howlett <[email protected]>
...
I have a version of this for mm-new and I'd like to send it out. Once this is upstream, it will be backported to the stable kernels with something that looks a lot like what I sent out here.
Did this fix the issue in the longer running tests?
Thanks,
Liam
Quoted 32 lines · Liam R. Howlett
Quoted 16 lines · Liam R. Howlett
Temporarily clear the preallocation flag when explicitly requesting allocations. Pre-existing allocations are already counted against the request through
mas_node_count_gfp(), but the allocations will not happen if theMA_STATE_PREALLOCflag is set. This flag is meant to avoid re-allocating in bulk allocation mode, and to detect issues with preallocation calculations.The
MA_STATE_PREALLOCflag should also always be set on zero allocations so that detection of underflow allocations will print aWARN_ON()during consumption.User visible effect of this flaw is a
WARN_ON()followed by a null pointer dereference when subsequent requests for larger number of nodes is ignored, such as the vma merge retry inmmap_region()caused by drivers altering the vma flags.Quoted 13 lines
Reported-by Zhaoyang Huang <[email protected]>Reported-by Hailong Liu <[email protected]>Fixes 54a611b605901 ("Maple Tree: add new data structure")Cc Lorenzo Stoakes <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Hailong Liu <[email protected]>Cc Steve Kang <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Sidhartha Kumar <[email protected]>Signed-off-by Liam R. Howlett <[email protected]>
Quoted 7 lines
...
I have a version of this for mm-new and I'd like to send it out. Once this is upstream, it will be backported to the stable kernels with something that looks a lot like what I sent out here.
Did this fix the issue in the longer running tests?
- everyone else
Hi Liam,
I think the delay is due to the holidays in China. I requested an update from the partners but they will probably provide it next week. Thanks,
Suren.
Quoted 3 lines
Thanks,
Liam
Quoted 36 lines · Suren Baghdasaryan
Quoted 34 lines · Liam R. Howlett
Quoted 29 lines · Liam R. Howlett
Temporarily clear the preallocation flag when explicitly requesting allocations. Pre-existing allocations are already counted against the request through
mas_node_count_gfp(), but the allocations will not happen if theMA_STATE_PREALLOCflag is set. This flag is meant to avoid re-allocating in bulk allocation mode, and to detect issues with preallocation calculations.The
MA_STATE_PREALLOCflag should also always be set on zero allocations so that detection of underflow allocations will print aWARN_ON()during consumption.User visible effect of this flaw is a
WARN_ON()followed by a null pointer dereference when subsequent requests for larger number of nodes is ignored, such as the vma merge retry inmmap_region()caused by drivers altering the vma flags.Reported-by Zhaoyang Huang <[email protected]>Reported-by Hailong Liu <[email protected]>Fixes 54a611b605901 ("Maple Tree: add new data structure")Cc Lorenzo Stoakes <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Hailong Liu <[email protected]>Cc Steve Kang <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Sidhartha Kumar <[email protected]>Signed-off-by Liam R. Howlett <[email protected]>...
Quoted 7 lines
Quoted 5 lines
I have a version of this for mm-new and I'd like to send it out. Once this is upstream, it will be backported to the stable kernels with something that looks a lot like what I sent out here.
Did this fix the issue in the longer running tests?
- everyone else
and of course I forgot to remove everyone else :) Sorry for the spam.
Quoted 10 lines
Hi Liam,
I think the delay is due to the holidays in China. I requested an update from the partners but they will probably provide it next week. Thanks,
Suren.Quoted 3 lines
Thanks,
Liam
Hailong Liu
Tested-byQuoted 39 lines · Suren Baghdasaryan
Quoted 38 lines · Liam R. Howlett
Quoted 16 lines · Liam R. Howlett
Temporarily clear the preallocation flag when explicitly requesting allocations. Pre-existing allocations are already counted against the request through
mas_node_count_gfp(), but the allocations will not happen if theMA_STATE_PREALLOCflag is set. This flag is meant to avoid re-allocating in bulk allocation mode, and to detect issues with preallocation calculations.The
MA_STATE_PREALLOCflag should also always be set on zero allocations so that detection of underflow allocations will print aWARN_ON()during consumption.User visible effect of this flaw is a
WARN_ON()followed by a null pointer dereference when subsequent requests for larger number of nodes is ignored, such as the vma merge retry inmmap_region()caused by drivers altering the vma flags.Quoted 13 lines
Reported-by Zhaoyang Huang <[email protected]>Reported-by Hailong Liu <[email protected]>Fixes 54a611b605901 ("Maple Tree: add new data structure")Cc Lorenzo Stoakes <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Hailong Liu <[email protected]>Cc Steve Kang <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Sidhartha Kumar <[email protected]>Signed-off-by Liam R. Howlett <[email protected]>...
I have a version of this for mm-new and I'd like to send it out. Once this is upstream, it will be backported to the stable kernels with something that looks a lot like what I sent out here.
Quoted 7 lines
Quoted 1 line
Did this fix the issue in the longer running tests?
- everyone else
Hi Liam,
I think the delay is due to the holidays in China. I requested an update from the partners but they will probably provide it next week.
Sorry for late reply. We applied this patch and verified it fix the issue.
Feel free to add
Thanks,
Hailong.
Quoted 6 lines
Thanks,
Suren.Quoted 3 lines
Thanks,
Liam
Quoted 47 lines · Hailong Liu
Quoted 43 lines · Suren Baghdasaryan
Quoted 32 lines · Liam R. Howlett
Quoted 16 lines · Liam R. Howlett
Temporarily clear the preallocation flag when explicitly requesting allocations. Pre-existing allocations are already counted against the request through
mas_node_count_gfp(), but the allocations will not happen if theMA_STATE_PREALLOCflag is set. This flag is meant to avoid re-allocating in bulk allocation mode, and to detect issues with preallocation calculations.The
MA_STATE_PREALLOCflag should also always be set on zero allocations so that detection of underflow allocations will print aWARN_ON()during consumption.User visible effect of this flaw is a
WARN_ON()followed by a null pointer dereference when subsequent requests for larger number of nodes is ignored, such as the vma merge retry inmmap_region()caused by drivers altering the vma flags.Quoted 13 lines
Reported-by Zhaoyang Huang <[email protected]>Reported-by Hailong Liu <[email protected]>Fixes 54a611b605901 ("Maple Tree: add new data structure")Cc Lorenzo Stoakes <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Hailong Liu <[email protected]>Cc Steve Kang <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Sidhartha Kumar <[email protected]>Signed-off-by Liam R. Howlett <[email protected]>Quoted 7 lines
...
I have a version of this for mm-new and I'd like to send it out. Once this is upstream, it will be backported to the stable kernels with something that looks a lot like what I sent out here.
Did this fix the issue in the longer running tests?
- everyone else
Quoted 8 lines
Quoted 3 lines
Hi Liam,
I think the delay is due to the holidays in China. I requested an update from the partners but they will probably provide it next week.Sorry for late reply. We applied this patch and verified it fix the issue.
Feel free to add
Tested-by Hailong Liu <[email protected]>
Thanks! Once Liam posts the official fix I'll apply it to the Android 6.6 branch.
Quoted 11 lines
Thanks,
Hailong.Quoted 6 lines
Thanks,
Suren.Quoted 3 lines
Thanks,
Liam