When one of the requested allocations failed, do - free the pages that were previously allocated as part of the request. - remove the files that were created as part of the request. - clean the kmem cache. - Erase the struct page_alloc objects from the xarray that were inserted as part of the request. Signed-off-by: Juan Yescas --- mm/page_alloc_hogger.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/mm/page_alloc_hogger.c b/mm/page_alloc_hogger.c index 203b71a25e64..e65aac881ec7 100644 --- a/mm/page_alloc_hogger.c +++ b/mm/page_alloc_hogger.c @@ -253,6 +253,23 @@ static int make_alloc(struct req_alloc *req, return ret; } +static int free_alloc_helper(unsigned long alloc_id) +{ + struct page_alloc *pa; + + pa = xa_erase(&allocs_xa, alloc_id); + if (!pa) { + pr_err("The alloc_id %lu was not found!", alloc_id); + return -EINVAL; + } + + __free_pages(pa->page, pa->req_alloc->order); + debugfs_remove(pa->alloc_dentry); + kmem_cache_free(page_alloc_cache, pa); + + return 0; +} + /** * req_page_alloc_write() - Allocates the pages on the requested node, zone, * order and migrate type. Once the allocation is performed, a file is created @@ -294,6 +311,14 @@ static ssize_t req_page_alloc_write(struct file *file, const char __user *ubuf, return cnt; free_allocs: + /* Free all the pages previously allocated. */ + for (int j = 0; j < i; j++) { + int ret2 = free_alloc_helper(allocs_ids[j]); + + if (ret2) + pr_err("Unable to free pages associated with file %lu", + allocs_ids[j]); + } kfree(allocs_ids); -- 2.55.0.229.g6434b31f56-goog