Re: [PATCH] lib/test_hmm: hold devmem_lock in dmirror_fops_release()
From: Andrew Morton
Date: Sat Dec 05 2026 - 18:50:30 EST
On Sat, 5 Dec 2026 14:34:01 -0800 Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:
> On Sat, 5 Dec 2026 15:43:54 +0800 Ziheng Wei <zihengw03@xxxxxxx> wrote:
>
> > dmirror_fops_release() iterates mdevice->devmem_chunks[0..devmem_count-1]
> > and calls dmirror_device_evict_chunk() without holding
> > mdevice->devmem_lock. A concurrent HMM_DMIRROR_RELEASE ioctl calling
> > dmirror_device_remove_chunks() under devmem_lock can free each chunk and
> > the devmem_chunks array while dmirror_fops_release() is walking them,
> > causing a use-after-free.
> >
> > Protect the devmem_chunks walk in dmirror_fops_release() with
> > mdevice->devmem_lock, matching dmirror_device_remove_chunks().
> >
> > Found by LLM-assisted static analysis.
>
> lgtm, thanks.
>
> > Compile-tested on x86_64.
>
> That's a bit concerning. Please find a way to runtime test things.
> Your LLM might already be able to do this in its own sandbox.
>
I A/B tested it.
: On A, close entered the chunk walk, then a concurrent RELEASE freed the
: chunk before close resumed. The test detected the unsafe access and
: stopped before dereferencing freed memory. On B, the new lock kept
: RELEASE out until close finished. Both operations returned
: successfully.
LLM found and fixed a similar race in there:
From: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Subject: lib/test_hmm: serialize chunk removal with migration
Date: Sat, 05 Dec 2026 14:37:30 -0800
Concurrent HMM_DMIRROR_MIGRATE_TO_DEV and HMM_DMIRROR_RELEASE ioctls on
the same HMM test device can crash the kernel. Chunk allocation publishes
the new chunk under devmem_lock, then drops that lock before adding its
pages to the free list and using one for migration. The release ioctl can
unmap and free the chunk in that interval. The migration ioctl then uses
pages which no longer belong to the device.
Serialize device migration with chunk removal through a separate mutex.
Take it before migration acquires mmap_read_lock(), and hold it through
the migration and its result handling. Chunk allocation still takes
devmem_lock internally; removal takes the new mutex before devmem_lock.
This keeps removal from overtaking a migration without holding devmem_lock
across the migration path.
This affects only kernels which load CONFIG_TEST_HMM and processes with
access to the HMM test device. The race was found by source inspection;
no runtime reproducer has been run.
A/B testig: on A, RELEASE freed a newly published chunk before migration
prepared its page. The test detected this and stopped before the unsafe
access; the migration ioctl returned an error. On B, migration completed
successfully before RELEASE removed the chunk.
Link: https://lore.kernel.org/r/20261205074354.83986-1-zihengw03@xxxxxxx
Fixes: ad4c365221b0 ("hmm-tests: add test for migrate_device_range()")
Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Ziheng Wei <zihengw03@xxxxxxx>
Cc: Alistair Popple <apopple@xxxxxxxxxx>
Cc: Jason Gunthorpe <jgg@xxxxxxxx>
Cc: Leon Romanovsky <leon@xxxxxxxxxx>
lib/test_hmm.c | 6 ++++++
1 file changed, 6 insertions(+)
--- a/lib/test_hmm.c~lib-test_hmm-serialize-chunk-removal-with-migration
+++ a/lib/test_hmm.c
@@ -116,6 +116,7 @@ struct dmirror_device {
unsigned int devmem_count;
struct dmirror_chunk **devmem_chunks;
struct mutex devmem_lock; /* protects the above */
+ struct mutex migration_lock; /* excludes chunk removal */
unsigned long calloc;
unsigned long cfree;
@@ -1586,6 +1587,7 @@ static void dmirror_device_remove_chunks
{
unsigned int i;
+ mutex_lock(&mdevice->migration_lock);
mutex_lock(&mdevice->devmem_lock);
if (mdevice->devmem_chunks) {
for (i = 0; i < mdevice->devmem_count; i++) {
@@ -1612,6 +1614,7 @@ static void dmirror_device_remove_chunks
mdevice->devmem_chunks = NULL;
}
mutex_unlock(&mdevice->devmem_lock);
+ mutex_unlock(&mdevice->migration_lock);
}
static long dmirror_fops_unlocked_ioctl(struct file *filp,
@@ -1650,7 +1653,9 @@ static long dmirror_fops_unlocked_ioctl(
break;
case HMM_DMIRROR_MIGRATE_TO_DEV:
+ mutex_lock(&dmirror->mdevice->migration_lock);
ret = dmirror_migrate_to_device(dmirror, &cmd);
+ mutex_unlock(&dmirror->mdevice->migration_lock);
break;
case HMM_DMIRROR_MIGRATE_TO_SYS:
@@ -1879,6 +1884,7 @@ static int dmirror_device_init(struct dm
dev = MKDEV(MAJOR(dmirror_dev), id);
mutex_init(&mdevice->devmem_lock);
+ mutex_init(&mdevice->migration_lock);
spin_lock_init(&mdevice->lock);
cdev_init(&mdevice->cdevice, &dmirror_fops);
_