git: d95675349a67 - stable/14 - linuxkpi: Provide a non-NULL value for THIS_MODULE

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Sat, 30 Nov 2024 16:51:07 UTC
The branch stable/14 has been updated by jhb:

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

commit d95675349a67b884db47b7861191bc3246a0d772
Author:     Austin Shafer <ashafer@badland.io>
AuthorDate: 2024-03-06 17:48:07 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-11-30 13:55:57 +0000

    linuxkpi: Provide a non-NULL value for THIS_MODULE
    
    THIS_MODULE is used to differentiate modules on Linux. We currently
    completely stub out any Linux struct module usage, but THIS_MODULE
    is still used to populate the "owner" fields of various drivers.
    Even though we don't actually dereference these "owner" fields they
    are still used by drivers to check if devices/dmabufs/etc come
    from different modules. For example, during DRM GEM import some
    drivers check if the dmabuf's owner matches the dev's owner. If
    they match because they are both NULL drivers may incorrectly think
    two resources come from the same module.
    
    This adds a general purpose __this_linker_file which will point to
    the linker file of the module that uses it. We can then use that
    pointer to have a valid value for THIS_MODULE.
    
    Reviewed by:    bz, jhb
    Differential Revision:  https://reviews.freebsd.org/D44306
    
    (cherry picked from commit 28a59100b54ff245a45fbd328266266f1c14eb8c)
---
 sys/compat/linuxkpi/common/include/linux/module.h | 21 +++++++++++++++++++++
 sys/kern/kern_linker.c                            | 14 ++++++++++++++
 sys/sys/linker.h                                  |  6 ++++++
 3 files changed, 41 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/module.h b/sys/compat/linuxkpi/common/include/linux/module.h
index 3018ba2fb8a4..df34843657db 100644
--- a/sys/compat/linuxkpi/common/include/linux/module.h
+++ b/sys/compat/linuxkpi/common/include/linux/module.h
@@ -33,6 +33,8 @@
 #include <sys/types.h>
 #include <sys/param.h>
 #include <sys/module.h>
+#include <sys/queue.h>
+#include <sys/linker.h>
 
 #include <linux/list.h>
 #include <linux/compiler.h>
@@ -52,7 +54,26 @@
 #define	MODULE_SUPPORTED_DEVICE(name)
 #define	MODULE_IMPORT_NS(_name)
 
+/*
+ * THIS_MODULE is used to differentiate modules on Linux. We currently
+ * completely stub out any Linux struct module usage, but THIS_MODULE is still
+ * used to populate the "owner" fields of various drivers.  Even though we
+ * don't actually dereference these "owner" fields they are still used by
+ * drivers to check if devices/dmabufs/etc come from different modules. For
+ * example, during DRM GEM import some drivers check if the dmabuf's owner
+ * matches the dev's owner. If they match because they are both NULL drivers
+ * may incorrectly think two resources come from the same module.
+ *
+ * To handle this we specify an undefined symbol __this_linker_file, which
+ * will get special treatment from the linker when resolving. This will
+ * populate the usages of __this_linker_file with the linker_file_t of the
+ * module.
+ */
+#ifdef KLD_MODULE
+#define	THIS_MODULE	((struct module *)&__this_linker_file)
+#else
 #define	THIS_MODULE	((struct module *)0)
+#endif
 
 #define	__MODULE_STRING(x) __stringify(x)
 
diff --git a/sys/kern/kern_linker.c b/sys/kern/kern_linker.c
index 53a743f29459..a04dd4cec563 100644
--- a/sys/kern/kern_linker.c
+++ b/sys/kern/kern_linker.c
@@ -849,6 +849,20 @@ linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
 	KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
 	    file, name, deps));
 
+	/*
+	 * Treat the __this_linker_file as a special symbol. This is a
+	 * global that linuxkpi uses to populate the THIS_MODULE
+	 * value.  In this case we can simply return the linker_file_t.
+	 *
+	 * Modules compiled statically into the kernel are assigned NULL.
+	 */
+	if (strcmp(name, "__this_linker_file") == 0) {
+		address = (file == linker_kernel_file) ? NULL : (caddr_t)file;
+		KLD_DPF(SYM, ("linker_file_lookup_symbol: resolving special "
+		    "symbol __this_linker_file to %p\n", address));
+		return (address);
+	}
+
 	if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
 		LINKER_SYMBOL_VALUES(file, sym, &symval);
 		if (symval.value == 0)
diff --git a/sys/sys/linker.h b/sys/sys/linker.h
index e9073dab236c..664df2147bec 100644
--- a/sys/sys/linker.h
+++ b/sys/sys/linker.h
@@ -132,6 +132,12 @@ typedef int linker_predicate_t(linker_file_t, void *);
  */
 extern linker_file_t	linker_kernel_file;
 
+/*
+ * Special symbol which will be replaced by a reference to the linker_file_t
+ * of the module it is used in.
+ */
+extern linker_file_t __this_linker_file;
+
 /*
  * Obtain a reference to a module, loading it if required.
  */