Re: [PATCH 4/5] mm: gup: fix potential pgmap refcnt leak in __gup_device_huge()

From: John Hubbard
Date: Sun Aug 08 2021 - 17:19:13 EST


On 8/7/21 11:45 AM, Andrew Morton wrote:
On Sat, 7 Aug 2021 11:41:12 -0700 Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:

We can simplify further, and remove the troublesome multiple return points?


oops.

I sent a reviewed by to the "+" fixup email, but just realized that that did not
hit the main
mailing list. So:



For the end result of these stacked fixes to this file:



Reviewed-by: John Hubbard <jhubbard@xxxxxxxxxx>



thanks,

--

John Hubbard

NVIDIA

--- a/mm/gup.c~mm-gup-fix-potential-pgmap-refcnt-leak-in-__gup_device_huge-fix-fix
+++ a/mm/gup.c
@@ -2240,6 +2240,7 @@ static int __gup_device_huge(unsigned lo
{
int nr_start = *nr;
struct dev_pagemap *pgmap = NULL;
+ int ret = 1;
do {
struct page *page = pfn_to_page(pfn);
@@ -2247,12 +2248,14 @@ static int __gup_device_huge(unsigned lo
pgmap = get_dev_pagemap(pfn, pgmap);
if (unlikely(!pgmap)) {
undo_dev_pagemap(nr, nr_start, flags, pages);
+ ret = 0;
break;
}
SetPageReferenced(page);
pages[*nr] = page;
if (unlikely(!try_grab_page(page, flags))) {
undo_dev_pagemap(nr, nr_start, flags, pages);
+ ret = 0;
break;
}
(*nr)++;
@@ -2260,7 +2263,7 @@ static int __gup_device_huge(unsigned lo
} while (addr += PAGE_SIZE, addr != end);
put_dev_pagemap(pgmap);
- return 1;
+ return ret;
}
static int __gup_device_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,

Not sure if it's worth bothering, really...