From 84838b79cc97d5b27d5624c4d573fe52bacb1ab6 Mon Sep 17 00:00:00 2001 From: Lucas Holt Date: Sun, 1 Dec 2024 14:32:13 -0500 Subject: [PATCH] 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. --- .../linuxkpi/common/include/linux/kernel.h | 2 +- .../linuxkpi/common/include/linux/module.h | 21 +++++++++++++++++++ .../linuxkpi/common/include/linux/skbuff.h | 2 +- sys/sys/linker.h | 6 ++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/kernel.h b/sys/compat/linuxkpi/common/include/linux/kernel.h index f6d3216e3b..0e248db760 100644 --- a/sys/compat/linuxkpi/common/include/linux/kernel.h +++ b/sys/compat/linuxkpi/common/include/linux/kernel.h @@ -718,7 +718,7 @@ static inline bool mac_pton(const char *macin, uint8_t *macout) { const char *s, *d; - uint8_t mac[6], hx, lx;; + uint8_t mac[6], hx, lx; int i; if (strlen(macin) < (3 * 6 - 1)) diff --git a/sys/compat/linuxkpi/common/include/linux/module.h b/sys/compat/linuxkpi/common/include/linux/module.h index 3018ba2fb8..df34843657 100644 --- a/sys/compat/linuxkpi/common/include/linux/module.h +++ b/sys/compat/linuxkpi/common/include/linux/module.h @@ -33,6 +33,8 @@ #include #include #include +#include +#include #include #include @@ -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/compat/linuxkpi/common/include/linux/skbuff.h b/sys/compat/linuxkpi/common/include/linux/skbuff.h index ee3f427aa6..73ad1404b1 100644 --- a/sys/compat/linuxkpi/common/include/linux/skbuff.h +++ b/sys/compat/linuxkpi/common/include/linux/skbuff.h @@ -606,7 +606,7 @@ static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *head) { SKB_TRACE2(skb, head); - struct sk_buff *p, *n;; + struct sk_buff *p, *n; head->qlen--; p = skb->prev; diff --git a/sys/sys/linker.h b/sys/sys/linker.h index b94cd9f274..4a3a8f27ae 100644 --- a/sys/sys/linker.h +++ b/sys/sys/linker.h @@ -125,6 +125,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. */