The patch titled Subject: maple_tree: clean up after bulk allocation failure in mas_alloc_nodes() has been added to the -mm tree. Its filename is maple-tree-add-new-data-structure-fix.patch This patch should soon appear at https://ozlabs.org/~akpm/mmots/broken-out/maple-tree-add-new-data-structure-fix.patch and later at https://ozlabs.org/~akpm/mmotm/broken-out/maple-tree-add-new-data-structure-fix.patch Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next and is updated there every 3-4 working days ------------------------------------------------------ From: Liam Howlett <liam.howlett@xxxxxxxxxx> Subject: maple_tree: clean up after bulk allocation failure in mas_alloc_nodes() Link: https://lkml.kernel.org/r/20220419155055.qf52xpcftqb3r5nj@revolver Cc: Yu Zhao <yuzhao@xxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- lib/maple_tree.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) --- a/lib/maple_tree.c~maple-tree-add-new-data-structure-fix +++ a/lib/maple_tree.c @@ -1206,6 +1206,8 @@ static inline void mas_alloc_nodes(struc unsigned long success = allocated; unsigned int requested = mas_alloc_req(mas); unsigned int count; + void **slots = NULL; + unsigned int max_req = 0; if (!requested) return; @@ -1214,7 +1216,7 @@ static inline void mas_alloc_nodes(struc if (!allocated || mas->alloc->node_count == MAPLE_ALLOC_SLOTS - 1) { node = (struct maple_alloc *)mt_alloc_one(gfp); if (!node) - goto nomem; + goto nomem_one; if (allocated) node->slot[0] = mas->alloc; @@ -1226,20 +1228,20 @@ static inline void mas_alloc_nodes(struc node = mas->alloc; while (requested) { - void **slots = (void **)&node->slot; - unsigned int max_req = MAPLE_NODE_SLOTS - 1; - + max_req = MAPLE_NODE_SLOTS - 1; if (node->slot[0]) { unsigned int offset = node->node_count + 1; slots = (void **)&node->slot[offset]; max_req -= offset; + } else { + slots = (void **)&node->slot; } - count = mt_alloc_bulk(gfp, min(requested, max_req), - slots); + max_req = min(requested, max_req); + count = mt_alloc_bulk(gfp, max_req, slots); if (!count) - goto nomem; + goto nomem_bulk; node->node_count += count; /* zero indexed. */ @@ -1253,7 +1255,11 @@ static inline void mas_alloc_nodes(struc } mas->alloc->total = success; return; -nomem: + +nomem_bulk: + /* Clean up potential freed allocations on bulk failure */ + memset(slots, 0, max_req * sizeof(unsigned long)); +nomem_one: mas_set_alloc_req(mas, requested); if (mas->alloc && !(((unsigned long)mas->alloc & 0x1))) mas->alloc->total = success; _ Patches currently in -mm which might be from liam.howlett@xxxxxxxxxx are radix-tree-test-suite-add-lockdep_is_held-to-header.patch mips-rename-mt_init-to-mips_mt_init.patch maple-tree-add-new-data-structure-fix.patch lib-test_maple_tree-add-testing-for-maple-tree.patch mmap-change-zeroing-of-maple-tree-in-__vma_adjust.patch riscv-use-vma-iterator-for-vdso.patch mm-mmapc-pass-in-mapping-to-__vma_link_file.patch mglru-vs-maple-tree-fix.patch