Skip to content

Commit

Permalink
linuxkpi: Provide a non-NULL value for THIS_MODULE
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laffer1 committed Dec 1, 2024
1 parent fbe6276 commit 84838b7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sys/compat/linuxkpi/common/include/linux/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
21 changes: 21 additions & 0 deletions sys/compat/linuxkpi/common/include/linux/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion sys/compat/linuxkpi/common/include/linux/skbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions sys/sys/linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 84838b7

Please sign in to comment.