简体   繁体   English

如何在两个 Linux 模块之间共享代码?

[英]How to share code between two Linux modules?

I received the code from a proprietary Linux driver, which has the following structure:我收到了来自专有 Linux 驱动程序的代码,该驱动程序具有以下结构:

common/common.c
moduleA/Makefile
moduleB/Makefile

The issue is that both moduleA and moduleB contain the statement moduleX-objs +=../common/common.o .问题是 moduleA 和 moduleB 都包含语句moduleX-objs +=../common/common.o This is obviously racy, because when doing a multithread compilation two threads are reading/writing to the file common/common.o at the same time.这显然很不合时宜,因为在进行多线程编译时,两个线程同时读取/写入文件 common/common.o。

What is the clean way to solve this?解决这个问题的干净方法是什么? Create a separate module common.ko?创建一个单独的模块 common.ko?
What is the standard way to do such things with the kernel build system?使用 kernel 构建系统执行此类操作的标准方法是什么?

Modules depend on common.o模块依赖于 common.o

One way is building common.o prior building the modules by making common.oa prerequisite to the modules as both modules will consider common.o up to date, though there is still a race condition if building with --always-make since the modules won't check dates then.一种方法是在构建模块之前构建 common.o,方法是将 common.oa 作为模块的先决条件,因为两个模块都会认为 common.o 是最新的,但如果使用 --always-make 构建仍然存在竞争条件,因为模块那时不会检查日期。 You can prevent it from happening by making common read-only during module build, failing any attempt of --always-make (assuming module makefile doesn't override permissions, which would be a weird thing to do).您可以通过在模块构建期间将 common 设为只读来防止它发生,使 --always-make 的任何尝试失败(假设模块 makefile 不会覆盖权限,这将是一件奇怪的事情)。 You do have to keep track of common contents used by modules though.但是,您确实必须跟踪模块使用的常见内容。 Simple solution, best performance.简单的解决方案,最佳性能。

All modules depend on one module所有模块都依赖于一个模块

If you're feeling lazy you could make a bogus dependency between modules, eg all modules depend on moduleA.如果你觉得懒惰,你可以在模块之间建立一个虚假的依赖关系,例如所有模块都依赖于moduleA。 All modules will be built multithreaded, but the module all depend on won't be built at the same time as all the others.所有模块都将构建多线程,但所有依赖的模块不会与所有其他模块同时构建。 There is build performance degradation, true, but I'd expect it to be insignificant in most cases.确实存在构建性能下降,但我希望在大多数情况下它是微不足道的。 For better performance you could experiment depending on modules with short build time or efficient internal multithreading.为了获得更好的性能,您可以根据构建时间短或内部多线程高效的模块进行试验。 Simplest solution, good performance.最简单的解决方案,良好的性能。 Doesn't work with --always-make, resort to mentioned read-only strategy.不适用于 --always-make,请使用提到的只读策略。

Separate source trees分离源树

I've had some success building in separate source trees by copying modules and common to separate (build) folders and then build from there, but it complicates other things, and build performance could be degraded is modules contain lots of files.通过复制模块和通用到单独的(构建)文件夹然后从那里构建,我已经在单独的源代码树中构建了一些成功,但是它使其他事情复杂化,并且构建性能可能会降低,因为模块包含大量文件。 If you decide to do it, remember to copy as hard links: cp -lr .如果您决定这样做,请记住复制为硬链接: cp -lr Least simple solution (of which I mention).最简单的解决方案(我提到过)。 Notable is that modules may want to build common with different build flags, in which case you'd need separate.o-files for each module.值得注意的是,模块可能希望使用不同的构建标志共同构建,在这种情况下,每个模块都需要单独的.o 文件。 If that is the case, this solution will work where the others won't.如果是这种情况,此解决方案将适用于其他解决方案。

Above are designs I've learnt over time.以上是我随着时间的推移学到的设计。 Not pretending it's state of the art or anything.不要假装它是艺术的 state 或任何东西。

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

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