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 --- 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