release_module_tags() removes the area of a module, and the padding in front of it, from mod_area_mt with mas_store(&mas, NULL). Storing NULL can merge the range with its neighbours and rebalance the tree, which needs new maple tree node allocations (with GFP_NOWAIT) and therefore can fail. If the allocation fails, the area stays in the tree and still points to the module that is going away. The maple state is left in the error state, so mas_prev_range() returns NULL and the padding is not removed either. If the allocation fails when the padding is removed, the padding is leaked. Use mas_erase() to remove the entries. It retries the allocation with the tree lock dropped and cannot fail. Dropping the lock is fine because all the changes to mod_area_mt are serialized by codetag_lock. Marking the area with unloaded_mod replaces the entry in place using mas_store() which does not allocate and therefore can't fail. Fixes: 0db6f8d7820a ("alloc_tag: load module tags into separate contiguous memory") Closes: https://sashiko.dev/#/bug/linux-55c2bd22-02fc-49f6-9ee7-e43117224340 Signed-off-by: Suren Baghdasaryan --- mm/alloc_tag.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mm/alloc_tag.c b/mm/alloc_tag.c index b4e55bbf57f6..7c12053445fe 100644 --- a/mm/alloc_tag.c +++ b/mm/alloc_tag.c @@ -1087,10 +1087,20 @@ static void release_module_tags(struct module *mod, bool used) used = false; } release_area: - mas_store(&mas, used ? &unloaded_mod : NULL); + /* + * Replacing the entry in place does not need new maple tree nodes and + * cannot fail. Removing it might, and mas_store() allocates them with + * GFP_NOWAIT. A failure would leave the area reserved forever, with a + * stale module pointer in it. Use mas_erase(), which does not fail. + */ + if (used) + mas_store(&mas, &unloaded_mod); + else + mas_erase(&mas); + val = mas_prev_range(&mas, 0); if (val == &prepend_mod) - mas_store(&mas, NULL); + mas_erase(&mas); out: mas_unlock(&mas); } -- 2.56.1.987.g2ec0ab4260-goog