From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796514683; b=yOfXR3CGFqEGPlKMi638Oz+cIEM58ITb81MWYH9MPgtDxqDZ8IloDjfLN0PmU/Omq6Es Op2DV5DlqW0iSPCTS34MHCBbQCsrB5VGKrEddtiUe4E1cbvYR5G7VKWQpoGtBe0bGYKOl s6IpdUsvR9DNsC6+/DtLBvPsQYAHEKI0FxCUC3L6CGaoi6ogYyd1B3yQ1/Tpe/PlCuMlq nwmZlDlVWZWgqLwdSh01WPmkKH5j1KeN4hkDun22GDB6JYBrWopUZrFz4GLhRYOXV/nq1 J4O8WgtoYX3nao4w+JipbW4h/Q3okPHGpCsFWA01VHArL4msPXjtU2eFrS3GnpEYxdg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796514683; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=TJRNRY6KxaFLJpcls1cOT4CVT7FvulP/0YRIwUpDFO8=; b=BbD4Q+0xRXr3fkGvhXYmt9nZHx+DDo28Q6QG6mntcnN0tPp6D3Ezj11GGBM2Ds8d4cMx 9SE6azOiGKn6qxEC1LRs7O+dVUOh0vXxE3bIxn7ygVhjtKiWPQ63GqE33L8bawW5OjxzG jieJ8u1KVaDurCYS1eX9bsRSnvE5sNuxRzgOL3QEPCZlUKFaSkL1Ixqjhczl5MaaB398E tZWGsdRVPpFABPPPNrN628YqjWng5ujCOqHB7pxmJX18eFfWGrWt/Keos5mHlWuilhLsL rkFuKY7asHSfef52+J7klq0kS4//u73RpMIBUSTDdhfVg1d66g/Lt1WTkE08OW/08QA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=cZWYuUDQ; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83F7C606A322 for ; Sat, 5 Dec 2026 23:51:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=cZWYuUDQ Received: by smtp.kernel.org (Postfix) with ESMTPSA id E5DC31F0092B; Sat, 5 Dec 2026 23:51:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796514683; bh=TJRNRY6KxaFLJpcls1cOT4CVT7FvulP/0YRIwUpDFO8=; h=Date:To:From:In-Reply-To:References:Subject; b=cZWYuUDQE+E4j0ML3HaPOqkYf4k83huVoth2yToLzx67z5jmOKrwZ/iUA6/sjKe9P i+YOKdiA+S1wn0ivmTvyhpTc0M5gDzrYbQ6Ib7+AmR7Xepr2QP/fM9Qlkijco88mpX Wkzxj8NcuS5m0UXMG3GlCLpQgTiErx2i3fys+Mc0= Date: Sat, 05 Dec 2026 15:51:22 -0800 To: mm-commits@vger.kernel.org,zihengw03@163.com,leon@kernel.org,jgg@ziepe.ca, apopple@nvidia.com,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261205074354.83986-1-zihengw03@163.com> References: <20261205074354.83986-1-zihengw03@163.com> Subject: + lib-test_hmm-serialize-chunk-removal-with-migration.patch added to mm-unstable branch Message-Id: <20261205235122.E5DC31F0092B@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: lib/test_hmm: serialize chunk removal with migration has been added to the -mm mm-unstable branch. Its filename is lib-test_hmm-serialize-chunk-removal-with-migration.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/lib-test_hmm-serialize-chunk-removal-with-migration.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Andrew Morton 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 Cc: Ziheng Wei Cc: Alistair Popple Cc: Jason Gunthorpe Cc: Leon Romanovsky 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); _ Patches currently in -mm which might be from akpm@linux-foundation.org are kselftest-mm-rename-read_pmd_pagesize-to-pmd_psize-fix.patch selftests-mm-check-madv_cold-and-madv_pageout-on-partly-advised-thps-fix.patch selftests-mm-retain-move-pmd-coverage-and-verify-huge-pmds.patch lib-test_xarray-check-unused-spare-node-after-range-creation.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache.patch selftests-mm-memory-failure-truncate-poisoned-fuse-page-cache-before-teardown.patch selftests-mm-memory-failure-skip-poisoned-dirty-page-cache-on-nilfs2.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup-fix.patch selftests-mm-memory-failure-skip-hard-dirty-page-cache-test-on-cephfs.patch selftests-mm-memory-failure-close-dirty-page-cache-files-before-unlinking-them.patch selftests-mm-memory-failure-extend-page-cache-test-files-with-pwrite.patch selftests-mm-memory-failure-write-the-whole-page-cache-test-file-for-omfs.patch selftests-mm-memory-failure-accept-eio-from-close-after-poisoning-dirty-data.patch selftests-mm-test-mlock-and-munlock-range-normalisation-fix.patch selftests-mm-free-the-test-context-when-uffd_test_ctx_init-fails-fix.patch lib-test_hmm-serialize-chunk-removal-with-migration.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix-fix.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target-fix.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault-fix.patch hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit-fix.patch mm-memory-failure-dont-leave-a-filtered-hugetlb-page-poisoned.patch ipc-msg-clamp-negative-msg_info-usage-counts.patch squashfs-fix-leaked-page-locks-when-page-actor-allocation-fails-fix.patch ocfs2-skip-network-flush-when-no-domain-handlers-are-registered.patch selftests-ptrace-check-that-ptrace_set_syscall_info-rejects-invalid-error-values-fix.patch lib-atomic64-separate-hashed-locks-by-smp-cache-line.patch nvdimm-btt-separate-map-locks-by-smp-cache-line.patch percpu_ref-replace-the-global-switch-lock-with-hashed-locks-fix.patch block-dont-defer-the-fput-in-disk_scan_partitions.patch block-preserve-rebound-queue-disk.patch block-clear-the-queues-disk-pointer-in-del_gendisk.patch blk-throttle-dont-leave-a-stale-throtl_data-pointer-in-the-queue.patch block-free-the-gendisk-when-its-queue-kobject-is-released.patch x86-mm-remove-the-cpa_debug-set_memory_rw-test-from-mark_rodata_ro.patch hrtimer-suppress-kcov-coverage-during-deferred-rearm.patch sched-kcov-suppress-sched-switch-tracing.patch x86-kgdb-mark-hardware-breakpoints-active-for-perf.patch of-avoid-formatting-zero-refcount-nodes-in-release.patch of-keep-detached-overlay-nodes-alive-until-last-user.patch of-unittest-skip-fatal-refcount-underflow.patch of-unittest-count-debug-driver-reprobes.patch of-unittest-count-debug-pci-reprobes-and-ignore-nonof-children.patch mm-hugetlb-run-deferred-free-on-unbound-workqueue.patch drm-fbdev-helper-run-damage-work-on-unbound-workqueue.patch lib-once-run-deferred-disable-on-unbound-workqueue.patch afs-do-not-put-error-pointer-after-address-lookup-failure.patch drivers-media-v4l2-core-v4l2-vp9c-reduce-inlining.patch