Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: mpenttil@redhat.com
Cc: dri-devel@lists.freedesktop.org, intel-xe@lists.freedesktop.org
Subject: Re: [PATCH v14 06/12] mm/hmm: migrate collection in HMM pagewalk - pte level
Date: Tue, 22 Sep 2026 05:47:41 +0000	[thread overview]
Message-ID: <20260922054742.045201F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260922053421.4092027-7-mpenttil@redhat.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] mm: hmm: double unlock of caller-locked fault_folio on rmap share failure
- [High] mm: hmm: livelock in large folio split due to refcount inflation before locking
- [High] mm: hmm: kernel crash due to split_folio() on non-folio compound pages
--

commit 5d5a85160b145735bf86c066185b4f70abea7ca4
Author: Mika Penttilä <mpenttil@redhat.com>

mm/hmm: migrate collection in HMM pagewalk - pte level
    
Implement hmm_vma_handle_migrate_prepare() and migrate_vma_split_folio() for
HMM pagewalk-based migration. The hmm_vma_handle_migrate_prepare() function
is responsible for migration decisions, possibly splitting folios, and
inserting migration entries.

> diff --git a/mm/hmm.c b/mm/hmm.c
> index e6469ef4ae6ad..b3b79d13c7978 100644
> --- a/mm/hmm.c
> +++ b/mm/hmm.c
[ ... ]
> +static int migrate_vma_split_folio(struct folio *folio,
> +				   struct page *fault_page,
> +				   struct hmm_vma_walk *hmm_vma_walk,
> +				   pte_t *ptep)
> +{
> +	int ret;
> +	struct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL;
> +	struct folio *new_fault_folio = NULL;
> +
> +	if (folio != fault_folio)
> +		folio_get(folio);
> +
> +	pte_unmap_unlock(ptep, hmm_vma_walk->ptl);
> +	hmm_vma_walk->ptelocked = false;
> +
> +	if (folio != fault_folio)
> +		folio_lock(folio);
> +
> +	ret = split_folio(folio);

[Severity: High]
Does taking the reference via folio_get() before blocking on folio_lock()
create a livelock when multiple threads fault on the same large folio?

If multiple threads execute migrate_vma_split_folio() simultaneously, they
would all elevate the refcount and block on the lock. When the lock is finally
acquired, split_folio() expects the refcount to be exactly
folio_expected_ref_count() + 1. The extra references held by the waiting
threads would cause split_folio() to return -EAGAIN repeatedly, preventing
forward progress.

[ ... ]
> +static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk,
> +					  pmd_t *pmdp,
> +					  pte_t *ptep,
> +					  unsigned long addr,
> +					  unsigned long *hmm_pfn,
> +					  bool *unmapped)
> +{
[ ... ]
> +	} else {
> +		pfn = pte_pfn(pte);
> +		if (is_zero_pfn(pfn) &&
> +		    (minfo & MIGRATE_VMA_SELECT_SYSTEM)) {
> +			*hmm_pfn = HMM_PFN_MIGRATE;
> +			goto out;
> +		}
> +		page = vm_normal_page(walk->vma, addr, pte);
> +		if (page && !is_zone_device_page(page) &&
> +		    !(minfo & MIGRATE_VMA_SELECT_SYSTEM)) {
> +			goto out;
> +		} else if (page && is_device_coherent_page(page)) {
> +			pgmap = page_pgmap(page);
> +
> +			if (!(minfo &
> +			      MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
> +			    pgmap->owner != migrate->pgmap_owner)
> +				goto out;
> +		}
> +
> +		folio = page ? page_folio(page) : NULL;
> +		if (folio && folio_test_large(folio)) {
> +			int ret;
> +
> +			ret = migrate_vma_split_folio(folio,
> +						      migrate->fault_page,
> +						      hmm_vma_walk,
> +						      ptep);

[Severity: High]
Can this split_folio() crash if a driver mapped a raw compound page into the
VMA using vm_insert_page()?

The large folio check here uses folio_test_large() which returns true for any
compound page (since PG_head is set). However, page_folio() blindly casts
compound pages. If the compound page lacks initialized folio metadata (like a
valid mapping or LRU state), splitting it will corrupt memory. The structural
validation gate checking for a valid mapping (!page || !page->mapping) is only
performed after this split attempt.

> +			if (ret)
> +				goto out_error;
> +			return -EAGAIN;
> +		}
> +
> +		writable = pte_write(pte);
> +	}
> +
> +	if (!page || !page->mapping)
> +		goto out;
[ ... ]
> +	if (fault_folio == folio || folio_trylock(folio)) {
> +		anon_exclusive = folio_test_anon(folio) &&
> +			PageAnonExclusive(page);
> +
> +		if (pte_present(pte))
> +			flush_cache_page(walk->vma, addr, pfn);
> +
> +		if (anon_exclusive) {
> +			pte = ptep_clear_flush(walk->vma, addr, ptep);
> +
> +			if (folio_try_share_anon_rmap_pte(folio, page)) {
> +				set_pte_at(mm, addr, ptep, pte);
> +				folio_unlock(folio);

[Severity: High]
Does this error path unconditionally unlock a folio that the caller might hold
the lock for?

When entering this block, if fault_folio == folio, the code bypasses calling
folio_trylock(folio) because the caller already owns the lock. If
folio_try_share_anon_rmap_pte() subsequently fails, calling folio_unlock()
here would destroy the caller's lock state, causing a double-unlock crash
when the caller attempts to unlock it later.

> +				folio_put(folio);
> +				goto out;
> +			}
> +		} else {
> +			pte = ptep_get_and_clear(mm, addr, ptep);
> +		}
[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260922053421.4092027-1-mpenttil@redhat.com?part=6

  reply	other threads:[~2026-09-22  5:47 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-22  5:34 [PATCH 00/12] [PATCH v14 00/12] migrate on fault for device pages mpenttil
2026-09-22  5:34 ` [PATCH v14 01/12] mm/Kconfig: changes for " mpenttil
2026-09-22  5:44   ` sashiko-bot
2026-09-22 22:27   ` Balbir Singh
2026-09-23  5:42     ` Mika Penttilä
2026-09-22  5:34 ` [PATCH v14 02/12] mm: add helper to convert HMM pfn to migrate pfn mpenttil
2026-09-22  5:34 ` [PATCH v14 03/12] mm/hmm: preparations for HMM to participate in migration mpenttil
2026-09-22  5:49   ` sashiko-bot
2026-09-22  5:34 ` [PATCH v14 04/12] mm/hmm: do the plumbing " mpenttil
2026-09-22  5:50   ` sashiko-bot
2026-09-22  5:34 ` [PATCH v14 05/12] mm/hmm: implement folio split for migrate needs in HMM pagewalk mpenttil
2026-09-22  5:47   ` sashiko-bot
2026-09-22  5:34 ` [PATCH v14 06/12] mm/hmm: migrate collection in HMM pagewalk - pte level mpenttil
2026-09-22  5:47   ` sashiko-bot [this message]
2026-09-22  5:34 ` [PATCH v14 07/12] mm/hmm: migrate collection in HMM pagewalk - pmd level mpenttil
2026-09-22  5:50   ` sashiko-bot
2026-09-22  5:34 ` [PATCH v14 08/12] mm/hmm: add lazy MMU mode support for migration in HMM pagewalk mpenttil
2026-09-22  5:51   ` sashiko-bot
2026-09-22  5:34 ` [PATCH v14 09/12] mm/hmm: implement rollback for device page " mpenttil
2026-09-22  5:34 ` [PATCH v14 10/12] mm: enable device page migration from " mpenttil
2026-09-22  5:58   ` sashiko-bot
2026-09-22  5:34 ` [PATCH v14 11/12] lib/test_hmm: add a new testcase for the migrate on fault mpenttil
2026-09-22  6:09   ` sashiko-bot
2026-09-22  5:34 ` [PATCH v14 12/12] Documentation/mm/hmm: document migration through hmm_range_fault() mpenttil
2026-09-22  5:41 ` ✗ CI.checkpatch: warning for Migrate on fault for device pages (rev6) Patchwork
2026-09-22  5:43 ` ✓ CI.KUnit: success " Patchwork
2026-09-22  6:00 ` ✗ CI.checksparse: warning " Patchwork
2026-09-22  7:08 ` ✓ Xe.CI.BAT: success " Patchwork
2026-09-22 15:03 ` ✗ Xe.CI.FULL: failure " Patchwork
2026-09-23  2:27 ` [PATCH 00/12] [PATCH v14 00/12] migrate on fault for device pages Andrew Morton
2026-09-23  5:29   ` Mika Penttilä
2026-09-23 21:19     ` Andrew Morton
2026-09-23 23:24       ` Jason Gunthorpe
2026-09-24  0:14         ` Mika Penttilä
2026-09-24  0:10       ` Mika Penttilä

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260922054742.045201F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=mpenttil@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox