Skip to content

Commit 28a5910

Browse files
amshaferbsdjhb
authored andcommitted
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
1 parent 65fd76b commit 28a5910

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

sys/compat/linuxkpi/common/include/linux/module.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <sys/types.h>
3333
#include <sys/param.h>
3434
#include <sys/module.h>
35+
#include <sys/queue.h>
36+
#include <sys/linker.h>
3537

3638
#include <linux/list.h>
3739
#include <linux/compiler.h>
@@ -51,7 +53,26 @@
5153
#define MODULE_SUPPORTED_DEVICE(name)
5254
#define MODULE_IMPORT_NS(_name)
5355

56+
/*
57+
* THIS_MODULE is used to differentiate modules on Linux. We currently
58+
* completely stub out any Linux struct module usage, but THIS_MODULE is still
59+
* used to populate the "owner" fields of various drivers. Even though we
60+
* don't actually dereference these "owner" fields they are still used by
61+
* drivers to check if devices/dmabufs/etc come from different modules. For
62+
* example, during DRM GEM import some drivers check if the dmabuf's owner
63+
* matches the dev's owner. If they match because they are both NULL drivers
64+
* may incorrectly think two resources come from the same module.
65+
*
66+
* To handle this we specify an undefined symbol __this_linker_file, which
67+
* will get special treatment from the linker when resolving. This will
68+
* populate the usages of __this_linker_file with the linker_file_t of the
69+
* module.
70+
*/
71+
#ifdef KLD_MODULE
72+
#define THIS_MODULE ((struct module *)&__this_linker_file)
73+
#else
5474
#define THIS_MODULE ((struct module *)0)
75+
#endif
5576

5677
#define __MODULE_STRING(x) __stringify(x)
5778

sys/kern/kern_linker.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,20 @@ linker_file_lookup_symbol_internal(linker_file_t file, const char *name,
906906
KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n",
907907
file, name, deps));
908908

909+
/*
910+
* Treat the __this_linker_file as a special symbol. This is a
911+
* global that linuxkpi uses to populate the THIS_MODULE
912+
* value. In this case we can simply return the linker_file_t.
913+
*
914+
* Modules compiled statically into the kernel are assigned NULL.
915+
*/
916+
if (strcmp(name, "__this_linker_file") == 0) {
917+
address = (file == linker_kernel_file) ? NULL : (caddr_t)file;
918+
KLD_DPF(SYM, ("linker_file_lookup_symbol: resolving special "
919+
"symbol __this_linker_file to %p\n", address));
920+
return (address);
921+
}
922+
909923
if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) {
910924
LINKER_SYMBOL_VALUES(file, sym, &symval);
911925
if (symval.value == 0)

sys/sys/linker.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ typedef int linker_predicate_t(linker_file_t, void *);
130130
*/
131131
extern linker_file_t linker_kernel_file;
132132

133+
/*
134+
* Special symbol which will be replaced by a reference to the linker_file_t
135+
* of the module it is used in.
136+
*/
137+
extern linker_file_t __this_linker_file;
138+
133139
/*
134140
* Obtain a reference to a module, loading it if required.
135141
*/

0 commit comments

Comments
 (0)