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=1796514624; b=By2ZEiKi3UKOsGNOQe5NHkYMHXh4a5m0Oge62CdQPZjK3cpvv+A7bOuqoiXc3BcUQime oezuwWIGKOwJ4Gm/aOzT7I8nxjsSvoPIeG/yYWIU+e3wfkjgVZuAXZ61ChXAqupy9IOfL Bthrpy5QZDdkZ867eOqXdUziimaUzhAe/XAWMjwgUPus0NFIzmCv5CaNWbGDVsVy26C1t +kDfChTnBNDuZs9Q0ptYdi7dUaHoHfpJG1u1mKCMwdk6oTHb8/M507Ed+KhilokigUCau rHBiBeM96aqcmslcHiGkJAtj+qMkbl0iAO3VjpshwQ2IoHrmeb0fp/2EqTT5QUf6Qsw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796514624; h=Received:DKIM-Signature:Date:From:To:Subject:Message-Id:In-Reply-To: References:X-Mailer:Mime-Version:Content-Type: Content-Transfer-Encoding; bh=dSnMON8dqwkXR9JgqYE9fOSObLO8JoWW0btD///fWzE=; b=llPhjj0MCWkyoTLRwmCAPPcEIUo1vwVPbcReNqKT6LdBJWG0FhEmtIVzauM8jNpemAnx 8uiRa1HIblp1XM2z+SUIoGZM7rhCH4bQhDOHTA/9pF0C/VoUzrBQ34A5ps2wkdy9Osv51 7OpE8cEz9EtISUAIrzJOoqrG8CT9XyIIPrLuLqumqFZ2nponyVmwoi52zFKrvr+qE6FwX G2SE+nNpMLtOBM8JChO8E+zqU84jSjGt0YcXwp8WWUoVIfEK6ePLrVNILNmd4Mvx7lTNa vnu2A1Ho2mQSNo+l/GIShCeRrSUpPImghLxvq6Zt3pcqR6VXLtHoT9FpDh8Hv8R8Xog== 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=NetoKGO8; 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 0FD8E606A31F; Sat, 5 Dec 2026 23:50:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=NetoKGO8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 870EA1F00A07; Sat, 5 Dec 2026 23:50:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796514623; bh=dSnMON8dqwkXR9JgqYE9fOSObLO8JoWW0btD///fWzE=; h=Date:From:To:Subject:In-Reply-To:References; b=NetoKGO8WFglrkF59zv79b6+dPzukj9p1NRZas/3se9X9RQ6Ot5/kJExdPpch/QTb cHtLHeCvf2qUEVm2Gkja2UVf1FYLFNxIEk3TVGHxrdRCEVIwNMNjw9ADQBVE8qK0nI UIUk9LIWVIjqwMmcSTUbTI6v5UtOxWpxqCURW92o= Date: Sat, 5 Dec 2026 15:50:23 -0800 From: Andrew Morton To: Ziheng Wei , Jason Gunthorpe , Leon Romanovsky , 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() Message-Id: <20261205155023.075eafd003c2d06fee0d88c3@linux-foundation.org> In-Reply-To: <20261205143401.46fc7200cfde7914ca7e34a6@linux-foundation.org> References: <20261205074354.83986-1-zihengw03@163.com> <20261205143401.46fc7200cfde7914ca7e34a6@linux-foundation.org> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sat, 5 Dec 2026 14:34:01 -0800 Andrew Morton wrote: > On Sat, 5 Dec 2026 15:43:54 +0800 Ziheng Wei 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 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); _