简体   繁体   English

Kernel 模块使用导出的符号,具体取决于加载的其他模块

[英]Kernel module using exported symbols depending on which other module is loaded

By definition a module won't load (or complains) if required symbols do not exist.根据定义,如果所需的符号不存在,模块将不会加载(或抱怨)。 I'd like to ask if there is a way to somehow overcome the following scenario:我想问是否有办法以某种方式克服以下情况:

I have module X which uses symbols from modules Y1 or Y2.我有模块 X,它使用模块 Y1 或 Y2 中的符号。 Only one of Y1 or Y2 is ever loaded at the same time.在同一时间只加载 Y1 或 Y2 中的一个。 Like, for one system configuration I would load Y1, for other Y2.就像,对于一个系统配置,我会加载 Y1,对于其他 Y2。 The problem is, that X won't load since it would be complaining about missing symbols from other (not loaded) Y driver.问题是,X 不会加载,因为它会抱怨其他(未加载的)Y 驱动程序缺少符号。

Example: Y1 exports a symbol: EXPORT_SYMBOL_GPL(y1);示例:Y1 导出一个交易品种: EXPORT_SYMBOL_GPL(y1); Y2 exports a symbol: EXPORT_SYMBOL_GPL(y2); Y2 导出一个交易品种: EXPORT_SYMBOL_GPL(y2);

X would do: X会这样做:

if (config1)
   y1(); // Available only when Y1 is loaded.
else
   y2(); // Available only when Y2 is loaded.

At boot, only one of Y1 or Y2 is loaded depending on configuration.启动时,根据配置仅加载 Y1 或 Y2 之一。 This all compiles, but when loading X in config1 it would say:这一切都可以编译,但是在 config1 中加载 X 时会说:

Unknown symbol y2

Does anyone have an idea if this can be solved somehow?有谁知道这是否可以以某种方式解决? Or I will just have to build two X modules, X1 and X2 depending on configuration?还是我只需要根据配置构建两个 X 模块 X1 和 X2?

Thank you very much, your comments are much appreciated.非常感谢,非常感谢您的意见。

You can achieve this by not using exported symbols directly, but by looking for them in the runtime using kallsyms_lookup_name() .您可以通过不直接使用导出的符号来实现这一点,而是使用kallsyms_lookup_name()在运行时查找它们。 That way your module X will not have explicit dependencies on symbols exported by modules Y1 and Y2, so it will be able to load even if one (or even both) of those modules are not loaded.这样,您的模块 X 将不会显式依赖模块 Y1 和 Y2 导出的符号,因此即使其中一个(或什至两个)模块未加载,它也能够加载。

This also means that your module will be responsible for checking that all the symbols required for its operation are present.这也意味着您的模块将负责检查其操作所需的所有符号是否存在。 It will also need to manually increment refcount for modules providing those symbols via try_module_get() , and then decrement it on module exit via module_put() .它还需要手动增加通过try_module_get()提供这些符号的模块的引用计数,然后通过module_put()在模块退出时减少它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM