]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
mshv: Fix sleeping under spinlock in mshv_portid_alloc
authorStanislav Kinsburskii <skinsburskii@linux.microsoft.com>
Thu, 7 May 2026 15:43:59 +0000 (15:43 +0000)
committerWei Liu <wei.liu@kernel.org>
Wed, 22 Jul 2026 23:51:30 +0000 (23:51 +0000)
idr_alloc() is called with GFP_KERNEL inside idr_lock(), which holds a
spinlock. GFP_KERNEL allows the allocator to sleep, triggering a
sleeping-while-atomic bug.

Fix by using idr_preload(GFP_KERNEL) before taking the lock to
pre-allocate memory in a sleepable context, then idr_alloc() with
GFP_NOWAIT inside the spinlock-protected section.

Fixes: 621191d709b1 ("Drivers: hv: Introduce mshv_root module to expose /dev/mshv to VMMs")
Signed-off-by: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>
Reviewed-by: Anirudh Rayabharam (Microsoft) <anirudh@anirudhrb.com>
Signed-off-by: Wei Liu <wei.liu@kernel.org>
drivers/hv/mshv_portid_table.c

index c349af1f0aaac7f38918ad8f9e65328fdf367c7d..6f59b3e37624738768f05e842cc510c6d26dfbc5 100644 (file)
@@ -40,12 +40,14 @@ mshv_port_table_fini(void)
 int
 mshv_portid_alloc(struct port_table_info *info)
 {
-       int ret = 0;
+       int ret;
 
+       idr_preload(GFP_KERNEL);
        idr_lock(&port_table_idr);
        ret = idr_alloc(&port_table_idr, info, PORTID_MIN,
-                       PORTID_MAX, GFP_KERNEL);
+                       PORTID_MAX, GFP_NOWAIT);
        idr_unlock(&port_table_idr);
+       idr_preload_end();
 
        return ret;
 }