From nobody Sun May 24 22:39:33 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0DCE357D0F for ; Thu, 21 May 2026 02:19:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779330013; cv=none; b=jnoPaaiUvB2N0xxOhDrWfbrTF1GDao5MyfW3d6koMKHOiX8g2HcmrvARm0l8GhTu2AaC1iLWNXnR/+yQjVLdzappUninzKsYjinPrcWesY8Oul1IrnGPNcgwtqvsLfzzXFqnOeZScLhp2iE37KD9epb0MvS9AXe28/vuzj/6N1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779330013; c=relaxed/simple; bh=pdN1SoZNHojP88qrJpuFyNuzpw+gfEfCO8lMlVnA5w8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Tip9tBmxymez4ZIpAAfaRc1VlV3VfSJXLmsfRGkBHNcJxiA+lICebr2ex+CGOJp91QtojSHuqbnVlBcoT7JblQS6BvQ/LjlKB2iaW75IwFZ8oi1OkWcmnD9mFfzvUdXqZfHP5T4/0OPexaGCMMGGcNujR0PLQSv0jVdkchFpXmc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=FXCoV6EU; arc=none smtp.client-ip=220.197.31.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="FXCoV6EU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=ak rxasQlRBwMUes7le78cPpBIDsElzjkYQ3k/XPa7rw=; b=FXCoV6EUT1audiz4wV 1b7t15ggWx0vUkygTFOfy6uYQ0YFQEv7F25Wpzh7i/p+oceYrzxNpC5rQTYgz9Sm Pjgh56EUHMcUsett5Dhis8ao5So3eTu1BW8sko6AlUH2dO/GHeyotJcilvRMUTMi 48kN3evU7C/bYnuWVfTyYaiFg= Received: from neo-TianYi510Pro-15ICK.. (unknown []) by gzsmtp4 (Coremail) with SMTP id PygvCgBHbzOVaw5qG00ZEg--.62491S2; Thu, 21 May 2026 10:19:05 +0800 (CST) From: liuqiangneo@163.com To: apopple@nvidia.com, jgg@ziepe.ca, leon@kernel.org, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qiang Liu Subject: [PATCH v4] lib/test_hmm: Check alloc_page_vma() return value and handle OOM Date: Thu, 21 May 2026 10:18:58 +0800 Message-ID: <20260521021858.21511-1-liuqiangneo@163.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: PygvCgBHbzOVaw5qG00ZEg--.62491S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxGrWkKFykKw17Ww48KrWrAFb_yoW5GF45pF 48JwnI9rW3Xr1aqr13Jr1v9w1Sk3yfta9rCFnxur18ZrZ8Xry5t3srKF40vr13Aayxta1f ZF4rtr1UuFn8uF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jzMKZUUUUU= X-CM-SenderInfo: 5olx1xxdqj0vrr6rljoofrz/xtbC4BofgGoOa5oGJgAA3Q Content-Type: text/plain; charset="utf-8" From: Qiang Liu Check alloc_page_vma() return status for page allocation failures, free allocated pages and return VM_FAULT_OOM on error. Handle return codes of dmirror_devmem_fault_alloc_and_copy(), call migrate_vma_finalize() to remove migration entries from migrate_vma_setup(). Signed-off-by: Qiang Liu --- v4: - Resolve code conflicts. --- v3: - Clear dst[i] to avoid migration in subsequent migrate_vma_pages/finalize(= ) calls - Handle return codes of dmirror_devmem_fault_alloc_and_copy() - call migrate_vma_finalize() to remove migration entries from migrate_vma_= setup(). v2: - Add unlock and free allocated pages before return. - https://lore.kernel.org/all/20260514032345.32256-1-liuqiangneo@163.com/ --- --- lib/test_hmm.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 38996c4baa40..b086034e8443 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -1063,6 +1063,25 @@ static vm_fault_t dmirror_devmem_fault_alloc_and_cop= y(struct migrate_vma *args, /* Try with smaller pages if large allocation fails */ if (!dpage && order) { dpage =3D alloc_page_vma(GFP_HIGHUSER_MOVABLE, args->vma, addr); + if (!dpage) { + /* Unlock and free pages already allocated. */ + while (i > 0) { + struct page *fpage; + + fpage =3D migrate_pfn_to_page(dst[--i]); + unlock_page(fpage); + __free_page(fpage); + } + /* Clear remaining dst entries to avoid + * migrate_vma_pages/finalize() using + * uninitialized values. + */ + while (i < (1 << order)) { + dst[i] =3D 0; + i++; + } + return VM_FAULT_OOM; + } lock_page(dpage); dst[i] =3D migrate_pfn(page_to_pfn(dpage)); dst_page =3D pfn_to_page(page_to_pfn(dpage)); @@ -1148,7 +1167,11 @@ static int dmirror_migrate_to_system(struct dmirror = *dmirror, goto out; =20 pr_debug("Migrating from device mem to sys mem\n"); - dmirror_devmem_fault_alloc_and_copy(&args, dmirror); + ret =3D dmirror_devmem_fault_alloc_and_copy(&args, dmirror); + if (ret) { + migrate_vma_finalize(&args); + goto out; + } =20 migrate_vma_pages(&args); cmd->cpages +=3D dmirror_successful_migrated_pages(&args); @@ -1683,8 +1706,10 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fau= lt *vmf) return VM_FAULT_SIGBUS; =20 ret =3D dmirror_devmem_fault_alloc_and_copy(&args, dmirror); - if (ret) + if (ret) { + migrate_vma_finalize(&args); goto err; + } migrate_vma_pages(&args); /* * No device finalize step is needed since --=20 2.43.0