mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sohil Mehta <sohil.mehta@intel.com>
To: x86@kernel.org, Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"H . Peter Anvin" <hpa@zytor.com>,
	Josh Poimboeuf <jpoimboe@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Sohil Mehta <sohil.mehta@intel.com>,
	"Kirill A . Shutemov" <kirill.shutemov@linux.intel.com>,
	Kai Huang <kai.huang@intel.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Mike Rapoport <rppt@kernel.org>, Petr Mladek <pmladek@suse.com>,
	Jani Nikula <jani.nikula@intel.com>,
	Tony Luck <tony.luck@intel.com>, Xin Li <xin@zytor.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 6/9] x86/nmi: Improve and relocate NMI handler comments
Date: Thu, 27 Mar 2025 23:46:26 +0000	[thread overview]
Message-ID: <20250327234629.3953536-7-sohil.mehta@intel.com> (raw)
In-Reply-To: <20250327234629.3953536-1-sohil.mehta@intel.com>

Some of the comments in the default NMI handling code are out of place
or inadequate. Move them to the appropriate locations and update them as
needed.

Move the comment related to CPU-specific NMIs closer to the actual code.
Also, add more details about how back-to-back NMIs are detected since
that isn't immediately obvious.

Opportunistically, replace an #ifdef section in the vicinity with an
IS_ENABLED() check to make the code easier to read.

Signed-off-by: Sohil Mehta <sohil.mehta@intel.com>
---
 arch/x86/kernel/nmi.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
index 2a07c9adc6a6..59ed74ec010e 100644
--- a/arch/x86/kernel/nmi.c
+++ b/arch/x86/kernel/nmi.c
@@ -359,17 +359,18 @@ static noinstr void default_do_nmi(struct pt_regs *regs)
 	bool b2b = false;
 
 	/*
-	 * CPU-specific NMI must be processed before non-CPU-specific
-	 * NMI, otherwise we may lose it, because the CPU-specific
-	 * NMI can not be detected/processed on other CPUs.
-	 */
-
-	/*
-	 * Back-to-back NMIs are interesting because they can either
-	 * be two NMI or more than two NMIs (any thing over two is dropped
-	 * due to NMI being edge-triggered).  If this is the second half
-	 * of the back-to-back NMI, assume we dropped things and process
-	 * more handlers.  Otherwise reset the 'swallow' NMI behaviour
+	 * Back-to-back NMIs are detected by comparing the RIP of the
+	 * current NMI with that of the previous NMI. If it is the same,
+	 * it is assumed that the CPU did not have a chance to jump back
+	 * into a non-NMI context and execute code in between the two
+	 * NMIs.
+	 *
+	 * They are interesting because even if there are more than two,
+	 * only a maximum of two can be detected (anything over two is
+	 * dropped due to NMI being edge-triggered). If this is the
+	 * second half of the back-to-back NMI, assume we dropped things
+	 * and process more handlers. Otherwise, reset the 'swallow' NMI
+	 * behavior.
 	 */
 	if (regs->ip == __this_cpu_read(last_nmi_rip))
 		b2b = true;
@@ -383,6 +384,11 @@ static noinstr void default_do_nmi(struct pt_regs *regs)
 	if (microcode_nmi_handler_enabled() && microcode_nmi_handler())
 		goto out;
 
+	/*
+	 * CPU-specific NMI must be processed before non-CPU-specific
+	 * NMI, otherwise we may lose it, because the CPU-specific
+	 * NMI can not be detected/processed on other CPUs.
+	 */
 	handled = nmi_handle(NMI_LOCAL, regs);
 	__this_cpu_add(nmi_stats.normal, handled);
 	if (handled) {
@@ -419,13 +425,14 @@ static noinstr void default_do_nmi(struct pt_regs *regs)
 			pci_serr_error(reason, regs);
 		else if (reason & NMI_REASON_IOCHK)
 			io_check_error(reason, regs);
-#ifdef CONFIG_X86_32
+
 		/*
 		 * Reassert NMI in case it became active
 		 * meanwhile as it's edge-triggered:
 		 */
-		reassert_nmi();
-#endif
+		if (IS_ENABLED(CONFIG_X86_32))
+			reassert_nmi();
+
 		__this_cpu_add(nmi_stats.external, 1);
 		raw_spin_unlock(&nmi_reason_lock);
 		goto out;
-- 
2.43.0


  parent reply	other threads:[~2025-03-27 23:49 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-27 23:46 [PATCH 0/9] x86: Cleanup NMI handling Sohil Mehta
2025-03-27 23:46 ` [PATCH 1/9] x86/nmi: Simplify unknown NMI panic handling Sohil Mehta
2025-03-31 22:52   ` Huang, Kai
2025-03-31 23:01     ` Sohil Mehta
2025-04-01 15:00   ` Nikolay Borisov
2025-04-01 15:54     ` Sohil Mehta
2025-04-03 13:33   ` [tip: x86/nmi] " tip-bot2 for Sohil Mehta
2025-03-27 23:46 ` [PATCH 2/9] x86/nmi: Consolidate NMI panic variables Sohil Mehta
2025-03-31 22:43   ` Huang, Kai
2025-03-31 22:50     ` Sohil Mehta
2025-03-31 23:05       ` Huang, Kai
2025-03-31 23:20         ` Sohil Mehta
2025-04-01  1:06           ` Huang, Kai
2025-04-01  5:46             ` Sohil Mehta
2025-04-01  8:08               ` Huang, Kai
2025-04-03 13:33   ` [tip: x86/nmi] " tip-bot2 for Sohil Mehta
2025-03-27 23:46 ` [PATCH 3/9] x86/nmi: Use a macro to initialize NMI descriptors Sohil Mehta
2025-03-31 23:46   ` Huang, Kai
2025-04-03 13:33   ` [tip: x86/nmi] " tip-bot2 for Sohil Mehta
2025-03-27 23:46 ` [PATCH 4/9] x86/nmi: Remove export of local_touch_nmi() Sohil Mehta
2025-03-31 23:47   ` Huang, Kai
2025-04-03 13:33   ` [tip: x86/nmi] " tip-bot2 for Sohil Mehta
2025-03-27 23:46 ` [PATCH 5/9] x86/nmi: Fix comment in unknown NMI handling Sohil Mehta
2025-04-01  0:17   ` Huang, Kai
2025-04-01  5:28     ` Sohil Mehta
2025-04-01  5:45     ` H. Peter Anvin
2025-04-01  5:53       ` Sohil Mehta
2025-04-03 13:33   ` [tip: x86/nmi] x86/nmi: Fix comment in unknown_nmi_error() tip-bot2 for Sohil Mehta
2025-03-27 23:46 ` Sohil Mehta [this message]
2025-04-01  1:03   ` [PATCH 6/9] x86/nmi: Improve and relocate NMI handler comments Huang, Kai
2025-04-03 13:33   ` [tip: x86/nmi] " tip-bot2 for Sohil Mehta
2025-03-27 23:46 ` [PATCH 7/9] x86/nmi: Improve NMI header documentation Sohil Mehta
2025-03-31 10:47   ` Ingo Molnar
2025-03-31 16:04     ` Sohil Mehta
2025-03-31 21:36       ` Sohil Mehta
2025-04-01  8:08         ` Ingo Molnar
2025-04-01 16:11           ` Sohil Mehta
2025-04-03 13:33   ` [tip: x86/nmi] x86/nmi: Add missing description x86_platform_ops::get_nmi_reason to <asm/x86_init.h> tip-bot2 for Sohil Mehta
2025-04-03 13:33   ` [tip: x86/nmi] x86/nmi: Improve <asm/nmi.h> documentation tip-bot2 for Sohil Mehta
2025-03-27 23:46 ` [PATCH 8/9] x86/nmi: Clean up NMI selftest Sohil Mehta
2025-04-03 13:33   ` [tip: x86/nmi] " tip-bot2 for Sohil Mehta
2025-03-27 23:46 ` [PATCH 9/9] x86/nmi: Improve NMI duration console print Sohil Mehta
2025-04-01  0:42   ` Huang, Kai
2025-04-03 13:33   ` [tip: x86/nmi] x86/nmi: Improve NMI duration console printouts tip-bot2 for Sohil Mehta
2025-04-01 14:54 ` [PATCH 0/9] x86: Cleanup NMI handling Peter Zijlstra
2025-04-01 16:00 ` Nikolay Borisov
2025-04-02 15:13 ` H. Peter Anvin

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=20250327234629.3953536-7-sohil.mehta@intel.com \
    --to=sohil.mehta@intel.com \
    --cc=bigeasy@linutronix.de \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jani.nikula@intel.com \
    --cc=jpoimboe@kernel.org \
    --cc=kai.huang@intel.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=rppt@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=tony.luck@intel.com \
    --cc=x86@kernel.org \
    --cc=xin@zytor.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®