From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-174.mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) (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 4FF6A41F7E8 for ; Mon, 6 Jul 2026 11:44:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783338241; cv=none; b=AV3Ey/DUh/04WkOz/QrIhKtigSZw3465/QoyuHSvJVBH6BN1Da+mdNW9eoRCE46zyzCgg7OZ+tToyC/l7S6UcWCgd8NO6mqstIFCCjuXpcyugMIV/dR1CDMIM+ebJ+XM4Izh2ZfQUIOcjSE+K6dLi+jTsdbvKbIMARzGNLQW/MQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783338241; c=relaxed/simple; bh=kA+SYbk+6LBbNcuMCV36gvxFglP3XF6aSA0WjRYVv4g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sLgQXj/kIqKEwHi01T6Tj514yKna3LZxN00SWPM5BBFgbnl2Jfp72x727bJHS6y+oQK1Rj9ZWWNA9IX+1ThsThYFkn8zQR9UMR9X0OP2C+nqXOQAJbh4yWG/uF6vG6sF7OwFZGs3O9D6XjDTxt5dUVNZD2mpWUjhjpK+LA3ewU8= 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=f22GEhXg; arc=none smtp.client-ip=95.215.58.174 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="f22GEhXg" 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=1783338238; 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: in-reply-to:in-reply-to:references:references; bh=5TFw0gU1rn6tLxDQ8mBtqEUuGJ9pKHatkm4r6fL5Jdc=; b=f22GEhXge9OTzVk6yrelxk+myUj2VpVV/pEFZqawjxneUFVJvTUQigT06NAzmQfVa2N6hX 2xYjzCPALPlwm7p0A/pXNMH+kXw+NcXe957K5daLYo6+7UjSlV0braT+FtVo8zT/Ivi2Lw h6R39yepwxF0TaoEo9/BMFu8RJ28vdk= From: Usama Arif To: Andrew Morton , david@kernel.org, chrisl@kernel.org, kasong@tencent.com, ljs@kernel.org, ziy@nvidia.com, linux-mm@kvack.org Cc: ying.huang@linux.alibaba.com, Baoquan He , willy@infradead.org, youngjun.park@lge.com, hannes@cmpxchg.org, riel@surriel.com, shakeel.butt@linux.dev, alex@ghiti.fr, kas@kernel.org, baohua@kernel.org, dev.jain@arm.com, baolin.wang@linux.alibaba.com, npache@redhat.com, Liam R. Howlett , ryan.roberts@arm.com, Vlastimil Babka , lance.yang@linux.dev, linux-kernel@vger.kernel.org, nphamcs@gmail.com, shikemeng@huaweicloud.com, kernel-team@meta.com, Usama Arif Subject: [PATCH v2 5/6] mm/migrate_device: move softleaf_to_folio() inside device-private branch Date: Mon, 6 Jul 2026 04:42:07 -0700 Message-ID: <20260706114320.1643046-6-usama.arif@linux.dev> In-Reply-To: <20260706114320.1643046-1-usama.arif@linux.dev> References: <20260706114320.1643046-1-usama.arif@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: 8bit X-Migadu-Flow: FLOW_OUT migrate_vma_collect_pmd() calls softleaf_to_folio() on a non-present PMD before checking the entry's type. softleaf_to_folio() converts the entry's offset to a PFN, which is only meaningful for migration or device-private entries. A PMD swap entry's offset is a swap offset, not a PFN, so the lookup would either return a bogus folio pointer or trip pfn_to_page validation on a debug kernel. In the non-device-private path the returned folio is then unused (the OR short-circuits to migrate_vma_collect_skip()), but the lookup itself is already unsafe. Move the softleaf_to_folio() call inside the device-private branch where the folio is actually needed, mirroring the equivalent change_non_present_huge_pmd() fix. Reviewed-by: Zi Yan Acked-by: David Hildenbrand (Arm) Signed-off-by: Usama Arif --- mm/migrate_device.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index aa948db49501..36287e958b2a 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -166,11 +166,14 @@ static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start, } else if (!pmd_present(*pmdp)) { const softleaf_t entry = softleaf_from_pmd(*pmdp); - folio = softleaf_to_folio(entry); - if (!softleaf_is_device_private(entry) || - !(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_PRIVATE) || - (folio->pgmap->owner != migrate->pgmap_owner)) { + !(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_PRIVATE)) { + spin_unlock(ptl); + return migrate_vma_collect_skip(start, end, walk); + } + + folio = softleaf_to_folio(entry); + if (folio->pgmap->owner != migrate->pgmap_owner) { spin_unlock(ptl); return migrate_vma_collect_skip(start, end, walk); } -- 2.53.0-Meta