]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
sgl_alloc_order: fix memory leak
authorDouglas Gilbert <dgilbert@interlog.com>
Thu, 15 Oct 2020 18:57:35 +0000 (14:57 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 5 Nov 2020 10:06:58 +0000 (11:06 +0100)
[ Upstream commit b2a182a40278bc5849730e66bca01a762188ed86 ]

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 <bvanassche@acm.org>
Signed-off-by: Douglas Gilbert <dgilbert@interlog.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Sasha Levin <sashal@kernel.org>
lib/scatterlist.c

index 834c846c5af84f1552a81f2f9dc5b0b79ad4840b..2cf02a82d502b1a2e85ecf9beaa1672a27c7f140 100644 (file)
@@ -477,7 +477,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;
                }