From nobody Mon Jun 8 16:28:13 2026 Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) (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 195B715746F for ; Thu, 28 May 2026 01:14:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779930880; cv=none; b=ppe+uN0PihJKA+Ybh0LufPxtOZABvGV4Cwt+fSKwwPSiDNEY9BTM2i/a/jPwjb1iAs45Jt0xL2Lahyaon6liyVQvcEtN/EbiheCjiI7iRqGw1uuN637eiheyobWJIuUuZ1UdkVsk5fFly0FOLKmlwkZ73zj0JF0q+C4+tSqqxbU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779930880; c=relaxed/simple; bh=6jMi6W4YXND2koL5Riz6Z8iyyInSmq8i0vuRh1qbLqw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=JwMZyCnL8XkHZbMMSe/gAlUiJjOJRibMbFxT27VmNKznOMXg8Ls7aSGXrjmCv4BVQmpIc/3ugf5xtsIxbEfq/pq2ivs3k6rQ107WXmFeG0keS5mCSDH/IS8p79SkT7/pNkeaqzEXLPJiWoS+kkEUs6Zd8Hy5CUIq0vEEy07ECMQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=jdOmy2Dw; arc=none smtp.client-ip=91.218.175.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="jdOmy2Dw" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779930875; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5fu1dDsnMhKF93AieSigFxenLRg3U0YzuqKbVaS1vss=; b=jdOmy2Dwho+MmvdxHhF/EulRjLNea1P8ngmdi+mXKjWa4JzyL0Ja88ejVyohRPGvM/HO5Q y4t/WGVMmYk42atY5egxaFtDdKP47gcVX3rJ1kF36XvZP6ayPc2Rx274D1f9ZzRDveRqGR HsJWQnlmldJ05xNEgZyO3DEmNYQL+JQ= From: Hao Ge To: Andrew Morton , Jason Gunthorpe , Leon Romanovsky , Balbir Singh Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Hao Ge , Sashiko Subject: [PATCH] lib/test_hmm: fix memory leak in dmirror_migrate_to_system() Date: Thu, 28 May 2026 09:13:36 +0800 Message-Id: <20260528011336.20797-1-hao.ge@linux.dev> 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-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Move the kvcalloc() calls after the early return checks to avoid leaking src_pfns and dst_pfns when end < start or mmget_not_zero() fails. Fixes: 775465fd26a3 ("lib/test_hmm: add zone device private THP test infras= tructure") Reported-by: Sashiko Signed-off-by: Hao Ge Reviewed-by: Alistair Popple Reviewed-by: Balbir Singh --- Sashiko report: https://sashiko.dev/#/patchset/20260513082525.154036-1-hao.= ge@linux.dev It looks like the other issue reported by Sashiko (dmirror_devmem_fault() leaking args.src/args.dst on migrate_vma_setup() failure) may have already been addressed in mm-new by 84808b0e9f75 ("lib/test_hmm: fix error path in dmirror_devmem_fault()"). --- lib/test_hmm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 213504915737..0c02871b97ba 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -1111,9 +1111,6 @@ static int dmirror_migrate_to_system(struct dmirror *= dmirror, unsigned long *src_pfns; unsigned long *dst_pfns; =20 - src_pfns =3D kvcalloc(PTRS_PER_PTE, sizeof(*src_pfns), GFP_KERNEL | __GFP= _NOFAIL); - dst_pfns =3D kvcalloc(PTRS_PER_PTE, sizeof(*dst_pfns), GFP_KERNEL | __GFP= _NOFAIL); - start =3D cmd->addr; end =3D start + size; if (end < start) @@ -1123,6 +1120,9 @@ static int dmirror_migrate_to_system(struct dmirror *= dmirror, if (!mmget_not_zero(mm)) return -EINVAL; =20 + src_pfns =3D kvcalloc(PTRS_PER_PTE, sizeof(*src_pfns), GFP_KERNEL | __GFP= _NOFAIL); + dst_pfns =3D kvcalloc(PTRS_PER_PTE, sizeof(*dst_pfns), GFP_KERNEL | __GFP= _NOFAIL); + cmd->cpages =3D 0; mmap_read_lock(mm); for (addr =3D start; addr < end; addr =3D next) { --=20 2.25.1