From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0E3CC106ACE2 for ; Thu, 12 Mar 2026 19:21:41 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A4E8810EAA2; Thu, 12 Mar 2026 19:21:38 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="BDN30Ajf"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) by gabe.freedesktop.org (Postfix) with ESMTPS id 68FBE10EA9E; Thu, 12 Mar 2026 19:21:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1773343297; x=1804879297; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=I/BPtVZqG+5QTTRQtRRb4HUhXTC0CvJo12PParp22Ko=; b=BDN30AjfwUPzNIHC0I3dYHJTwQDTstiiXW+h6vOILOBLoflYC7b3hYcO 3zbDeO8F8m8xgzgKRvVjPOB4MTf3OSG+c7IyzGrZKuhwnV/6ioa9icOTH Ni3C4StlmaIcJ7TpxO+AIDi8XWgZpAQ1cKe1w/u7lmqcDmK0yD3dLteBe moRIlApyiLGEKFVub2RNl472V6Po2Y8Mlyw7GfCcg+PCZNcR+AcV8cLxV 8AHXTDiBy1+l4pBEJJzrdaISk8lh6V7BuiQl0cNt5a4AfAlu3FfbbvexM 8V4QUWWVd+enqlvIXe0aQ6JAX5tIeUR2KgwjNWKom0YgB54422MO8dFA7 Q==; X-CSE-ConnectionGUID: LjiABDrVRQuxnGZ8T4hVlA== X-CSE-MsgGUID: RadE+OOFT6KBcSqiNRrg8A== X-IronPort-AV: E=McAfee;i="6800,10657,11727"; a="78052713" X-IronPort-AV: E=Sophos;i="6.23,116,1770624000"; d="scan'208";a="78052713" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2026 12:21:37 -0700 X-CSE-ConnectionGUID: 5gG8AE5DSuKh/tuNns5q2A== X-CSE-MsgGUID: j+6ApdSfSR+WeYF+bmLpQg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,116,1770624000"; d="scan'208";a="221105090" Received: from smoticic-mobl1.ger.corp.intel.com (HELO fdugast-desk.home) ([10.245.245.89]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2026 12:21:37 -0700 From: Francois Dugast To: intel-xe@lists.freedesktop.org Cc: dri-devel@lists.freedesktop.org Subject: [PATCH v9 3/4] drm/pagemap: Correct cpages calculation for migrate_vma_setup Date: Thu, 12 Mar 2026 20:20:13 +0100 Message-ID: <20260312192126.2024853-4-francois.dugast@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260312192126.2024853-1-francois.dugast@intel.com> References: <20260312192126.2024853-1-francois.dugast@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Matthew Brost cpages returned from migrate_vma_setup represents the total number of individual pages found, not the number of 4K pages. The math in drm_pagemap_migrate_to_devmem for npages is based on the number of 4K pages, so cpages != npages can fail even if the entire memory range is found in migrate_vma_setup (e.g., when a single 2M page is found). Add drm_pagemap_cpages, which converts cpages to the number of 4K pages found. Cc: Andrew Morton Cc: David Hildenbrand Cc: Lorenzo Stoakes Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Suren Baghdasaryan Cc: Michal Hocko Cc: Zi Yan Cc: Alistair Popple Cc: Balbir Singh Cc: linux-mm@kvack.org Signed-off-by: Matthew Brost Reviewed-by: Francois Dugast Signed-off-by: Francois Dugast Reviewed-by: Balbir Singh --- drivers/gpu/drm/drm_pagemap.c | 38 ++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/drm_pagemap.c b/drivers/gpu/drm/drm_pagemap.c index 733a3857947c..837b881883f9 100644 --- a/drivers/gpu/drm/drm_pagemap.c +++ b/drivers/gpu/drm/drm_pagemap.c @@ -452,6 +452,41 @@ static int drm_pagemap_migrate_range(struct drm_pagemap_devmem *devmem, return ret; } +/** + * drm_pagemap_cpages() - Count collected pages + * @migrate_pfn: Array of migrate_pfn entries to account + * @npages: Number of entries in @migrate_pfn + * + * Compute the total number of minimum-sized pages represented by the + * collected entries in @migrate_pfn. The total is derived from the + * order encoded in each entry. + * + * Return: Total number of minimum-sized pages. + */ +static int drm_pagemap_cpages(unsigned long *migrate_pfn, unsigned long npages) +{ + unsigned long i, cpages = 0; + + for (i = 0; i < npages;) { + struct page *page = migrate_pfn_to_page(migrate_pfn[i]); + struct folio *folio; + unsigned int order = 0; + + if (page) { + folio = page_folio(page); + order = folio_order(folio); + cpages += NR_PAGES(order); + } else if (migrate_pfn[i] & MIGRATE_PFN_COMPOUND) { + order = HPAGE_PMD_ORDER; + cpages += NR_PAGES(order); + } + + i += NR_PAGES(order); + } + + return cpages; +} + /** * drm_pagemap_migrate_to_devmem() - Migrate a struct mm_struct range to device memory * @devmem_allocation: The device memory allocation to migrate to. @@ -554,7 +589,8 @@ int drm_pagemap_migrate_to_devmem(struct drm_pagemap_devmem *devmem_allocation, goto err_free; } - if (migrate.cpages != npages) { + if (migrate.cpages != npages && + drm_pagemap_cpages(migrate.src, npages) != npages) { /* * Some pages to migrate. But we want to migrate all or * nothing. Raced or unknown device pages. -- 2.43.0