From nobody Tue Feb 10 20:06:05 2026 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A44525A321 for ; Mon, 9 Feb 2026 16:17:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770653846; cv=none; b=nPnOrSV3zBNH2kOtBYJ0Jc+n6TMP17Wilnldcw1lNax6WO9YRbaz94W32hxtbCLDtJRmsIPw02eB4tCUCwyB/t66tC2SjYUEskst+FjtGvov3cJbBqLe/T3LjV6sksu6v/ghCRXkELxv7UjGCEJjG32bHZagQSss+3qxCx6eNr8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770653846; c=relaxed/simple; bh=QLn9UJBSWT6jlcMbbaZa68IHkR+F3SHMyeahLxtfE60=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=iIjmlG5PrSuH4dGYoBjH46g7SgRjR2i0CiHxARQy0Ycytl2slRO0HhWjDkVlV46szUOQRQSZKMxRhI2zw79IuxfZuLA7PDzEVgru8EI0Nesq1WTmToKmKPXsztIQKmgj0yHnTUq4acDCKHOKKEBFaT3V9+8h5fljniTJzRdcR/M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=S7Swxfqq; arc=none smtp.client-ip=209.85.215.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S7Swxfqq" Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-bc274b8b15bso2844784a12.1 for ; Mon, 09 Feb 2026 08:17:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770653845; x=1771258645; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=fYDQcjBg2vU5GI5xW0lALxWdgLiVRXFArD5ZQ3GcGuo=; b=S7SwxfqqMrxKSQQtGTaXq87sQn+2KFaEZvosOmlT8RgUIPnma/+CXHWaWlZFuHyMVc hc6DrjZpl7SSjLKxT8DGz4u8MeHq8wBQ7USoGO4UJWy8xqGwrnMHrAxkrL6JDKnK7JLl WzxFHf9bhSpM56nwotucL/+pcH1Y7TdzKzNDp5xyRJSzrrKMgapqs8JzSH1ikcS9DNcM JbtDZBOBdN/K+1pTiaxesS1ALO6tpKtq4YbjnqMcfM2rYkDayaXlAp0dGOndtWHvn6nE rmUrLfvDv6+l+O1uKhYwdSRY4EQ1QErEIJThIY7gxe3QoC0BT0k0fU7h3p1PctTHjry5 mB6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770653845; x=1771258645; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=fYDQcjBg2vU5GI5xW0lALxWdgLiVRXFArD5ZQ3GcGuo=; b=KBFsOwwqIjTmrcNPBx8+jI3pVGgeAALhJQlt74/+4qsEKre1tr06iDU4ZToNHW8t6a r9UxfL87jgSw1BvDLU5hHp5ZgX6k3kXPajQ2hvByfLhi3YltYt8jEUbrtoYUdCVOz5So HEebQtNE6M/tSh2/Euy6UOIoBbgVPr719nEjGyWTT6LAIcVUBv7B1ZVuD1eOAiXtZSNe LONPbnYVdSX364c0ywrkmvXpJvpvvSSCQRAgzKsLAvrl61ODxVZL6z/XVewRKVK/wOwz mH++tdmh0cWk8rjdA7TDaWyMo95cGdd3JTKvbg1kWSH1zVxsq9cMN6FjEyIJfzfQzP+/ 4R6Q== X-Forwarded-Encrypted: i=1; AJvYcCXgGse5N6XXVKQnOQVu9NKs98g6NV97evbp8pLqhytcAMqa288f4y65s6DCHpQRhY8dYMkRY2TXujwEYk0=@vger.kernel.org X-Gm-Message-State: AOJu0Ywo9z8vT099RlEwADsAP0uAAKik8hMJ/efg+UgMGtmdsBUBR5Yf KSgQUElVsBCaJN3IIKtxz1SEHVdjDZDOVE4dSom3g2cdq0YQymicnCvl X-Gm-Gg: AZuq6aK53qEBmrgfkeCYQ3ucer9dLPtegQwDEWpyLEH+ADZDRFP55r2joVfV3gPNHMA boCAGKr7WOjhLywuMJvG+yIKmXOVufx9K5uXOMfF4dcG+SA78Ibx14VNzr55GXt87O3RNlHbqaU F5DVOkFVATxk83sBq149b+ZKRkIdcBUtfvQg2aele/W4YSC06PGspf78QK6ybZmnpStTxoRrgC0 zwvZPaZeKBhLBeuiGUNhuHd873T/zbuoJIVjVkdfeiNPfEL9lQCyBUtqYnWOvDY+MS5squsyErk 9fHt7iIt1lCdntqKJ7hwD3D9mtrJVyL6W2EKI89wHFfLBbnz0tYo+ew7S7nlB4oEIXaX53RlR6N b4dtPz9At7wtpf0NaEtf4yLNL9FMidH/RFHA207Iw6AX0TKRMFfJ4+uaT2Eyn+fNHny92wr5ema yj/qjcM0eB+uFn9Nb0kI2RW/5ZAqz6jOXYTdcx+w== X-Received: by 2002:a17:902:e54f:b0:2a9:6165:6e87 with SMTP id d9443c01a7336-2a96165754dmr82914955ad.59.1770653845268; Mon, 09 Feb 2026 08:17:25 -0800 (PST) Received: from acer-nitro-anv15-41.. ([27.7.91.133]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a95bff42f7sm91648125ad.68.2026.02.09.08.17.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Feb 2026 08:17:24 -0800 (PST) From: "shaikh.kamal" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev Cc: "shaikh.kamal" Subject: [PATCH] KVM: mmu_notifier: make mn_invalidate_lock non-sleeping for non-blocking invalidations Date: Mon, 9 Feb 2026 21:45:27 +0530 Message-ID: <20260209161527.31978-1-shaikhkamal2012@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" mmu_notifier_invalidate_range_start() may be invoked via mmu_notifier_invalidate_range_start_nonblock(), e.g. from oom_reaper(), where sleeping is explicitly forbidden. KVM's mmu_notifier invalidate_range_start currently takes mn_invalidate_lock using spin_lock(). On PREEMPT_RT, spin_lock() maps to rt_mutex and may sleep, triggering: BUG: sleeping function called from invalid context This violates the MMU notifier contract regardless of PREEMPT_RT; RT kernels merely make the issue deterministic. Fix by converting mn_invalidate_lock to a raw spinlock so that invalidate_range_start() remains non-sleeping while preserving the existing serialization between invalidate_range_start() and invalidate_range_end(). Signed-off-by: shaikh.kamal --- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index d93f75b05ae2..77a6d4833eda 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -797,7 +797,7 @@ struct kvm { atomic_t nr_memslots_dirty_logging; =20 /* Used to wait for completion of MMU notifiers. */ - spinlock_t mn_invalidate_lock; + raw_spinlock_t mn_invalidate_lock; unsigned long mn_active_invalidate_count; struct rcuwait mn_memslots_update_rcuwait; =20 diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 5fcd401a5897..7a9c33f01a37 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -747,9 +747,9 @@ static int kvm_mmu_notifier_invalidate_range_start(stru= ct mmu_notifier *mn, * * Pairs with the decrement in range_end(). */ - spin_lock(&kvm->mn_invalidate_lock); + raw_spin_lock(&kvm->mn_invalidate_lock); kvm->mn_active_invalidate_count++; - spin_unlock(&kvm->mn_invalidate_lock); + raw_spin_unlock(&kvm->mn_invalidate_lock); =20 /* * Invalidate pfn caches _before_ invalidating the secondary MMUs, i.e. @@ -817,11 +817,11 @@ static void kvm_mmu_notifier_invalidate_range_end(str= uct mmu_notifier *mn, kvm_handle_hva_range(kvm, &hva_range); =20 /* Pairs with the increment in range_start(). */ - spin_lock(&kvm->mn_invalidate_lock); + raw_spin_lock(&kvm->mn_invalidate_lock); if (!WARN_ON_ONCE(!kvm->mn_active_invalidate_count)) --kvm->mn_active_invalidate_count; wake =3D !kvm->mn_active_invalidate_count; - spin_unlock(&kvm->mn_invalidate_lock); + raw_spin_unlock(&kvm->mn_invalidate_lock); =20 /* * There can only be one waiter, since the wait happens under @@ -1129,7 +1129,7 @@ static struct kvm *kvm_create_vm(unsigned long type, = const char *fdname) mutex_init(&kvm->irq_lock); mutex_init(&kvm->slots_lock); mutex_init(&kvm->slots_arch_lock); - spin_lock_init(&kvm->mn_invalidate_lock); + raw_spin_lock_init(&kvm->mn_invalidate_lock); rcuwait_init(&kvm->mn_memslots_update_rcuwait); xa_init(&kvm->vcpu_array); #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES @@ -1635,17 +1635,17 @@ static void kvm_swap_active_memslots(struct kvm *kv= m, int as_id) * progress, otherwise the locking in invalidate_range_start and * invalidate_range_end will be unbalanced. */ - spin_lock(&kvm->mn_invalidate_lock); + raw_spin_lock(&kvm->mn_invalidate_lock); prepare_to_rcuwait(&kvm->mn_memslots_update_rcuwait); while (kvm->mn_active_invalidate_count) { set_current_state(TASK_UNINTERRUPTIBLE); - spin_unlock(&kvm->mn_invalidate_lock); + raw_spin_unlock(&kvm->mn_invalidate_lock); schedule(); - spin_lock(&kvm->mn_invalidate_lock); + raw_spin_lock(&kvm->mn_invalidate_lock); } finish_rcuwait(&kvm->mn_memslots_update_rcuwait); rcu_assign_pointer(kvm->memslots[as_id], slots); - spin_unlock(&kvm->mn_invalidate_lock); + raw_spin_unlock(&kvm->mn_invalidate_lock); =20 /* * Acquired in kvm_set_memslot. Must be released before synchronize --=20 2.43.0