Skip to contentSkip to conversation

mm: memory-failure: fix potential page refcnt leak in memory_failure()

PATCH Applied Miaohe Lin 2 participants 3 messages Jul 1, 2023 – Jul 3, 2023

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author maintainer
#0 lore raw

PATCH 1/1 mm: memory-failure: fix potential page refcnt leak in memory_failure()

put_ref_page() is not called to drop extra refcnt when comes from madvise in the case pfn is valid but pgmap is NULL leading to page refcnt leak.

Fixes 1e8aaedb182d ("mm,memory_failure: always pin the page in madvise_inject_error")
---
 mm/memory-failure.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/memory-failure.c +1 -2
index e245191e6b04..65e2d4c5b50d 100644
@@ -2080,8 +2080,6 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
 {
 	int rc = -ENXIO;
 
-	put_ref_page(pfn, flags);
-
 	/* device metadata space is not recoverable */
 	if (!pgmap_pfn_valid(pgmap, pfn))
 		goto out;
@@ -2157,6 +2155,7 @@ int memory_failure(unsigned long pfn, int flags)
 
 		if (pfn_valid(pfn)) {
 			pgmap = get_dev_pagemap(pfn, NULL);
+			put_ref_page(pfn, flags);
 			if (pgmap) {
 				res = memory_failure_dev_pagemap(pfn, flags,
 								 pgmap);
Signature · 1 line
2.33.0
Acked-by on v1 1/1
#1 lore raw
Quoted 2 lines · Miaohe Lin

put_ref_page() is not called to drop extra refcnt when comes from madvise in the case pfn is valid but pgmap is NULL leading to page refcnt leak.

Is this test scenario realistic one? I don't think that we can call madvise() for such a device memory page. If this is the case, this issue can be thought as potentioal one (so no need to send to stable).

Quoted 3 lines
Fixes 1e8aaedb182d ("mm,memory_failure: always pin the page in madvise_inject_error")

Anyway, the patch looks good to me. Thanks you.

Quoted 30 lines
---
 mm/memory-failure.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/memory-failure.c +1 -2
index e245191e6b04..65e2d4c5b50d 100644
@@ -2080,8 +2080,6 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags,
 {
 	int rc = -ENXIO;
 
-	put_ref_page(pfn, flags);
-
 	/* device metadata space is not recoverable */
 	if (!pgmap_pfn_valid(pgmap, pfn))
 		goto out;
@@ -2157,6 +2155,7 @@ int memory_failure(unsigned long pfn, int flags)
 
 		if (pfn_valid(pfn)) {
 			pgmap = get_dev_pagemap(pfn, NULL);
+			put_ref_page(pfn, flags);
 			if (pgmap) {
 				res = memory_failure_dev_pagemap(pfn, flags,
 								 pgmap);
Signature · 1 line
2.33.0
author maintainer
#2 lore raw
Quoted 7 lines · Naoya Horiguchi
Quoted 2 lines · Miaohe Lin

put_ref_page() is not called to drop extra refcnt when comes from madvise in the case pfn is valid but pgmap is NULL leading to page refcnt leak.

Is this test scenario realistic one? I don't think that we can call madvise() for such a device memory page. If this is the case, this issue can be thought as potentioal one (so no need to send to stable).

This is a theoretical problem. If pagemap can ever be NULL after page refcnt is increased, this would occur. I think it's not needed to send to stable too.

Quoted 8 lines
Quoted 4 lines
Fixes 1e8aaedb182d ("mm,memory_failure: always pin the page in madvise_inject_error")

Anyway, the patch looks good to me. Thanks you.

Thanks for your review and comment.