lkml.org 
[lkml]   [2026]   [May]   [18]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH v2] lib/test_hmm: Check alloc_page_vma() return value
Date
From: Qiang Liu <liuqiang@kylinos.cn>

Return VM_FAULT_OOM if page allocation fails, which
avoids a NULL pointer dereference when calling lock_page().

Signed-off-by: Qiang Liu <liuqiang@kylinos.cn>
---
v2:
- Add unlock and free allocated pages before return.
- https://lore.kernel.org/all/20260514032345.32256-1-liuqiangneo@163.com/
---
lib/test_hmm.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/lib/test_hmm.c b/lib/test_hmm.c
index 213504915737..90aec4ca2e01 100644
--- a/lib/test_hmm.c
+++ b/lib/test_hmm.c
@@ -1063,6 +1063,17 @@ static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args,
/* Try with smaller pages if large allocation fails */
if (!dpage && order) {
dpage = alloc_page_vma(GFP_HIGHUSER_MOVABLE, args->vma, addr);
+ if (!dpage) {
+ /* Unlock and free pages already allocated. */
+ while (i > 0) {
+ struct page *fpage;
+
+ fpage = migrate_pfn_to_page(dst[--i]);
+ unlock_page(fpage);
+ __free_page(fpage);
+ }
+ return VM_FAULT_OOM;
+ }
lock_page(dpage);
dst[i] = migrate_pfn(page_to_pfn(dpage));
dst_page = pfn_to_page(page_to_pfn(dpage));
--
2.43.0

\
 
 \ /
  Last update: 2026-05-18 05:21    [W:0.061 / U:5.864 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and my Meterkast|Read the blog