From: Andrew Morton <akpm@linux-foundation.org>
To: Ziheng Wei <zihengw03@163.com>, Jason Gunthorpe <jgg@ziepe.ca>,
Leon Romanovsky <leon@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] lib/test_hmm: hold devmem_lock in dmirror_fops_release()
Date: Sat, 5 Dec 2026 15:50:23 -0800 [thread overview]
Message-ID: <20261205155023.075eafd003c2d06fee0d88c3@linux-foundation.org> (raw)
In-Reply-To: <20261205143401.46fc7200cfde7914ca7e34a6@linux-foundation.org>
On Sat, 5 Dec 2026 14:34:01 -0800 Andrew Morton <akpm@linux-foundation.org> wrote:
> On Sat, 5 Dec 2026 15:43:54 +0800 Ziheng Wei <zihengw03@163.com> 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@linux-foundation.org>
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@163.com
Fixes: ad4c365221b0 ("hmm-tests: add test for migrate_device_range()")
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Ziheng Wei <zihengw03@163.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Leon Romanovsky <leon@kernel.org>
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);
_
next prev parent reply other threads:[~2026-12-05 23:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 7:43 Ziheng Wei
2026-12-05 22:34 ` Andrew Morton
2026-12-05 23:50 ` Andrew Morton [this message]
2026-12-06 2:47 ` Ziheng Wei
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=20261205155023.075eafd003c2d06fee0d88c3@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=jgg@ziepe.ca \
--cc=leon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=stable@vger.kernel.org \
--cc=zihengw03@163.com \
/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
all inboxes | Powered by JetHome®