From nobody Tue Sep 29 01:39:04 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.2]) (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 373D0323417 for ; Mon, 17 Aug 2026 07:52:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786953165; cv=none; b=qwi0I8l2AlaYURh9Fp/oKWh9mCVGeKMJXh/JMEK6DaU1yvTJQPjpsppchO7pKE7RTx5qI/OvY5MqYd2RS5/zmODakSVsFfRsCVbB2tYr0nNipjNGg873qJakN68MXOrXqu6vh/pa3qw/lR/P+8VtGdsIo2raZ4nWsZw8zaq2scs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786953165; c=relaxed/simple; bh=dtBwoVMNqgWZzYanGJM3T5WZjr2jKEX7+IHupne6160=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=XYNRya52BWIT0PVtcLp10EWZyHfS+aLfIj+amarOQ4CiAlCNewGjkZKkVbO48EjRxmo89V3N43/iOe6hCvKxglc0qOyuhrTCZZNsAu92TRANiPZIazprLwNQFe8Wb8LWVWljrRXFGSYUP3sLJMyd1zBG1YDfMKz8b7vgxIz7owc= 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=MPcDCT93; arc=none smtp.client-ip=117.135.210.2 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="MPcDCT93" 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=9t 9jRj7/HCFVCvKEHPg2zTk5Ak8y48z3aFyjzy9sL2Q=; b=MPcDCT937sbS0kfEU8 fpGa+epsTdxlh/MMcVJluEWcH7Od7fh698QkiCpVzTM6RCbSBVwVrYNdXowVWM9r nw0IXe28T1JldNB8mR3par0kODh+hUCyzMYC2IqTzyzpJdIDre5N1zCJbAWKIKO9 lefZH7KEcBV8UljzD30m5MNHQ= Received: from localhost (unknown []) by gzga-smtp-mtada-g1-3 (Coremail) with SMTP id _____wAHzAKTvYJqe4poOA--.32S2; Mon, 17 Aug 2026 15:51:48 +0800 (CST) From: Hui Su To: akpm@linux-foundation.org, david@kernel.org Cc: ziy@nvidia.com, matthew.brost@intel.com, joshua.hahnjy@gmail.com, rakie.kim@sk.com, byungchul@sk.com, gourry@gourry.net, ying.huang@linux.alibaba.com, apopple@nvidia.com, balbirs@nvidia.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hui Su Subject: [PATCH] mm/migrate_device: avoid out-of-bounds writes for compound folios Date: Mon, 17 Aug 2026 15:43:51 +0800 Message-ID: <20260817074350.442493-2-sh_def@163.com> X-Mailer: git-send-email 2.54.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: _____wAHzAKTvYJqe4poOA--.32S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7try3Gr1xXrW5tF17Xw4DXFb_yoW8Kry3pF 4Fg3WUtrWUWry7GF13Zw1kZr13ur9xXa13GFnrJr929Fs8JF95ua4Iy3s8tr45J397tFyx ZFWaqa4xu3WUJw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0piHa0PUUUUU= X-CM-SenderInfo: xvkbvvri6rljoofrz/xtbC6RTPL2qCvZQA7gAA3y Content-Type: text/plain; charset="utf-8" migrate_device_range() and migrate_device_pfns() clear the entries following a compound folio so that the PFN arrays retain their page-granular representation. If a compound folio extends beyond the end of the caller-provided range, the loops clear all following folio entries without limiting them to the number of slots remaining in the npages-sized array, causing an out-of-bounds write. Limit the number of entries updated and consumed to the remaining array slots while keeping the actual number of pages in the folio unchanged. Observed with a KASAN x86 QEMU kernel using the HMM migrate_anon_huge_zero selftest. Closing /dev/hmm_dmirror0 after migrating an anonymous huge page to device memory exercises: dmirror_fops_release() -> dmirror_device_evict_chunk() -> migrate_device_range() Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone de= vice pages") Signed-off-by: Hui Su --- mm/migrate_device.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 908d2d4ec43a..67bfc1af0b29 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -1396,16 +1396,17 @@ int migrate_device_range(unsigned long *src_pfns, u= nsigned long start, for (pfn =3D start, i =3D 0; i < npages; pfn++, i++) { struct page *page =3D pfn_to_page(pfn); struct folio *folio =3D page_folio(page); - unsigned int nr =3D 1; + unsigned long nr, slots; =20 src_pfns[i] =3D migrate_device_pfn_lock(pfn); nr =3D folio_nr_pages(folio); + slots =3D min(nr, npages - i); if (nr > 1) { src_pfns[i] |=3D MIGRATE_PFN_COMPOUND; - for (j =3D 1; j < nr; j++) + for (j =3D 1; j < slots; j++) src_pfns[i+j] =3D 0; - i +=3D j - 1; - pfn +=3D j - 1; + i +=3D slots - 1; + pfn +=3D slots - 1; } } =20 @@ -1430,15 +1431,16 @@ int migrate_device_pfns(unsigned long *src_pfns, un= signed long npages) for (i =3D 0; i < npages; i++) { struct page *page =3D pfn_to_page(src_pfns[i]); struct folio *folio =3D page_folio(page); - unsigned int nr =3D 1; + unsigned long nr, slots; =20 src_pfns[i] =3D migrate_device_pfn_lock(src_pfns[i]); nr =3D folio_nr_pages(folio); + slots =3D min(nr, npages - i); if (nr > 1) { src_pfns[i] |=3D MIGRATE_PFN_COMPOUND; - for (j =3D 1; j < nr; j++) + for (j =3D 1; j < slots; j++) src_pfns[i+j] =3D 0; - i +=3D j - 1; + i +=3D slots - 1; } } =20 --=20 2.54.0