All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: mm-commits@vger.kernel.org,zihengw03@163.com,leon@kernel.org,jgg@ziepe.ca,
	apopple@nvidia.com,akpm@linux-foundation.org
Subject: + lib-test_hmm-serialize-chunk-removal-with-migration.patch added to mm-unstable branch
Date: Sat, 05 Dec 2026 15:51:22 -0800	[thread overview]
Message-ID: <20261205235122.E5DC31F0092B@smtp.kernel.org> (raw)
In-Reply-To: <20261205074354.83986-1-zihengw03@163.com>


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 <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);
_

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


      parent reply	other threads:[~2026-12-05 23:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
2026-12-05 22:34 ` + lib-test_hmm-hold-devmem_lock-in-dmirror_fops_release.patch added to mm-unstable branch Andrew Morton
2026-12-05 23:51 ` Andrew Morton [this message]

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=20261205235122.E5DC31F0092B@smtp.kernel.org \
    --to=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=jgg@ziepe.ca \
    --cc=leon@kernel.org \
    --cc=mm-commits@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.