2020-10-15 22:21:02

by Douglas Gilbert

[permalink] [raw]
Subject: [RESEND PATCH] sgl_alloc_order: fix memory leak

sgl_alloc_order() can fail when 'length' is large on a memory
constrained system. When order > 0 it will potentially be
making several multi-page allocations with the later ones more
likely to fail than the earlier one. So it is important that
sgl_alloc_order() frees up any pages it has obtained before
returning NULL. In the case when order > 0 it calls the wrong
free page function and leaks. In testing the leak was
sufficient to bring down my 8 GiB laptop with OOM.

Reviewed-by: Bart Van Assche <[email protected]>
Signed-off-by: Douglas Gilbert <[email protected]>
---
lib/scatterlist.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index 5d63a8857f36..c448642e0f78 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -514,7 +514,7 @@ struct scatterlist *sgl_alloc_order(unsigned long long length,
elem_len = min_t(u64, length, PAGE_SIZE << order);
page = alloc_pages(gfp, order);
if (!page) {
- sgl_free(sgl);
+ sgl_free_order(sgl, order);
return NULL;
}

--
2.25.1


2020-10-16 16:20:54

by Jens Axboe

[permalink] [raw]
Subject: Re: [RESEND PATCH] sgl_alloc_order: fix memory leak

On 10/15/20 12:57 PM, Douglas Gilbert wrote:
> sgl_alloc_order() can fail when 'length' is large on a memory
> constrained system. When order > 0 it will potentially be
> making several multi-page allocations with the later ones more
> likely to fail than the earlier one. So it is important that
> sgl_alloc_order() frees up any pages it has obtained before
> returning NULL. In the case when order > 0 it calls the wrong
> free page function and leaks. In testing the leak was
> sufficient to bring down my 8 GiB laptop with OOM.

I've picked this one up, thanks.

--
Jens Axboe