git: 8376d05279ee - releng/15.0 - x86: Reduce amount of time the MCA lock is held while emitting records

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Tue, 28 Oct 2025 21:23:40 UTC
The branch releng/15.0 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=8376d05279ee78e39f8face7b307772c8c306fa7

commit 8376d05279ee78e39f8face7b307772c8c306fa7
Author:     Jonathan T. Looney <jtl@FreeBSD.org>
AuthorDate: 2025-10-06 15:07:33 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2025-10-28 21:23:05 +0000

    x86: Reduce amount of time the MCA lock is held while emitting records
    
    The MCA spin lock is acquired in the hardware interrupt context to
    record MCA messages. It is also acquired by a task handler to emit
    those messages.
    
    Reduce the amount of time the task handler holds the lock to reduce
    the maximum amount of time the hardware interrupt handler may need to
    spin on the lock.
    
    Approved by:    re (cperciva)
    Reviewed by:    glebius, markj
    Sponsored by:   Netflix
    Differential Revision:  https://reviews.freebsd.org/D52938
    
    (cherry picked from commit e770e32aa3a017b35fcf24e7c6f14fc2a209bad5)
    (cherry picked from commit d83dee356dce39b0282bce1f8d7d1ba976995d57)
---
 sys/x86/x86/mca.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/sys/x86/x86/mca.c b/sys/x86/x86/mca.c
index e43c88b3a27b..7ee22895e0a7 100644
--- a/sys/x86/x86/mca.c
+++ b/sys/x86/x86/mca.c
@@ -796,9 +796,9 @@ mca_record_entry(enum scan_mode mode, const struct mca_record *record)
 		mtx_lock_spin(&mca_lock);
 		rec = STAILQ_FIRST(&mca_freelist);
 		if (rec == NULL) {
+			mtx_unlock_spin(&mca_lock);
 			printf("MCA: Unable to allocate space for an event.\n");
 			mca_log(record);
-			mtx_unlock_spin(&mca_lock);
 			return;
 		}
 		STAILQ_REMOVE_HEAD(&mca_freelist, link);
@@ -1017,6 +1017,7 @@ static void
 mca_process_records(enum scan_mode mode)
 {
 	struct mca_internal *mca;
+	STAILQ_HEAD(, mca_internal) tmplist;
 
 	/*
 	 * If in an interrupt context, defer the post-scan activities to a
@@ -1028,10 +1029,21 @@ mca_process_records(enum scan_mode mode)
 		return;
 	}
 
+	/*
+	 * Copy the pending list to the stack so we can drop the spin lock
+	 * while we are emitting logs.
+	 */
+	STAILQ_INIT(&tmplist);
 	mtx_lock_spin(&mca_lock);
-	while ((mca = STAILQ_FIRST(&mca_pending)) != NULL) {
-		STAILQ_REMOVE_HEAD(&mca_pending, link);
+	STAILQ_SWAP(&mca_pending, &tmplist, mca_internal);
+	mtx_unlock_spin(&mca_lock);
+
+	STAILQ_FOREACH(mca, &tmplist, link)
 		mca_log(&mca->rec);
+
+	mtx_lock_spin(&mca_lock);
+	while ((mca = STAILQ_FIRST(&tmplist)) != NULL) {
+		STAILQ_REMOVE_HEAD(&tmplist, link);
 		mca_store_record(mca);
 	}
 	mtx_unlock_spin(&mca_lock);