git: a7b2c455505a - stable/13 - kern linker: Do not touch userrefs of the kernel file

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Tue, 09 Apr 2024 04:14:03 UTC
The branch stable/13 has been updated by zlei:

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

commit a7b2c455505ab472dabe84788ceb2109138664f1
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2024-03-26 08:47:02 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2024-04-09 04:13:19 +0000

    kern linker: Do not touch userrefs of the kernel file
    
    A nonzero `userrefs` of a linker file indicates that the file, either
    loaded from kldload(2) or preloaded, can be unloaded via kldunload(2).
    As for the kernel file, it can be unloaded by the loader but should not
    be after initialization.
    
    This change fixes regression from d9ce8a41eac9 which incidentally
    increases `userrefs` of the kernel file.
    
    Reviewed by:    dfr, dab, jhb
    Fixes:  d9ce8a41eac9 kern_linker: Handle module-loading failures in preloaded .ko files
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D42530
    
    (cherry picked from commit 39450eba8e6cd5b83def724481127b58ecc1fcc1)
    (cherry picked from commit 7da45efb0921e9937353992226f34b25ae197545)
---
 sys/kern/kern_linker.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/sys/kern/kern_linker.c b/sys/kern/kern_linker.c
index 8f83fc4c474f..5271d002fba4 100644
--- a/sys/kern/kern_linker.c
+++ b/sys/kern/kern_linker.c
@@ -1771,6 +1771,9 @@ linker_preload_finish(void *arg)
 
 	sx_xlock(&kld_sx);
 	TAILQ_FOREACH_SAFE(lf, &linker_files, link, nlf) {
+		if (lf == linker_kernel_file)
+			continue;
+
 		/*
 		 * If all of the modules in this file failed to load, unload
 		 * the file and return an error of ENOEXEC.  (Parity with