mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] lib/test_hmm: hold devmem_lock in dmirror_fops_release()
@ 2026-12-05  7:43 Ziheng Wei
  2026-12-05 22:34 ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Ziheng Wei @ 2026-12-05  7:43 UTC (permalink / raw)
  To: Andrew Morton, Jason Gunthorpe, Leon Romanovsky
  Cc: linux-mm, linux-kernel, stable

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. Compile-tested on x86_64.

Fixes: 744dd97752ef ("lib: test_hmm: evict device pages on file close to avoid use-after-free")
Cc: stable@vger.kernel.org
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---
 lib/test_hmm.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lib/test_hmm.c b/lib/test_hmm.c
index a09a766b1..cd6e30138 100644
--- a/lib/test_hmm.c
+++ b/lib/test_hmm.c
@@ -243,6 +243,7 @@ static int dmirror_fops_release(struct inode *inode, struct file *filp)
 
 	mmu_interval_notifier_remove(&dmirror->notifier);
 
+	mutex_lock(&mdevice->devmem_lock);
 	if (mdevice->devmem_chunks) {
 		for (i = 0; i < mdevice->devmem_count; i++) {
 			struct dmirror_chunk *devmem =
@@ -251,6 +252,7 @@ static int dmirror_fops_release(struct inode *inode, struct file *filp)
 			dmirror_device_evict_chunk(devmem);
 		}
 	}
+	mutex_unlock(&mdevice->devmem_lock);
 
 	xa_destroy(&dmirror->pt);
 	kfree(dmirror);

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-06  2:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-05  7:43 [PATCH] lib/test_hmm: hold devmem_lock in dmirror_fops_release() Ziheng Wei
2026-12-05 22:34 ` Andrew Morton
2026-12-05 23:50   ` Andrew Morton
2026-12-06  2:47     ` Ziheng Wei

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®