-2

This is question is quite similar to the question: How can Linux kernel modules be loaded from C code?

The difference is that I want to load the kernel module from inside the kernel code without going out to userspace.

The case is that I have two kernel modules that I should load and unload dynamically. They cannot be loaded together, and I need to load one and unload the other as fast as possible. The trigger that tells me which one to load or unload is coming from the hardware.
That is the reason I want to do it from inside the kernel.

Community
  • 1
  • 1
A. Sarid
  • 3,916
  • 2
  • 31
  • 56
  • **Why do you ask**, and what is the actual use case and motivation? Please **edit your question** to improve it! – Basile Starynkevitch Jan 25 '17 at 08:23
  • @BasileStarynkevitch added my specific case. Thanks. – A. Sarid Jan 25 '17 at 08:29
  • BTW, even with the edit, I find your question too broad (and it probably has been downvoted - not by me - because of that). You need to name the kernel modules, and to explain what exact event is triggering their loading (or unloading) and what is the concrete role of those modules. – Basile Starynkevitch Jan 25 '17 at 08:36
  • @BasileStarynkevitch I appreciate your effort. I understand that it might be too broad. I am working on some DSL device, that should load and unload a module depending if it is a VDSL or ADSL connection. I am not a kernel expert and kernel modules are something new to me. I honestly thought that there might be a simple solution to this, but I guess I was wrong. Thanks. – A. Sarid Jan 25 '17 at 08:43
  • My current guess is that you have an architecture design bug, and they are the most costly bugs. Ask your manager for several more man months. – Basile Starynkevitch Jan 25 '17 at 08:47
  • 2
    you should **edit your question** not comment it. Basically, comments are for others. You should not comment your question, but edit it. – Basile Starynkevitch Jan 25 '17 at 08:48

2 Answers2

2

For loading a module from within the kernel you can use request_module.

See example here.

Yosef Arbiv
  • 360
  • 2
  • 9
Rami Rosen
  • 330
  • 2
  • 2
  • Thanks Rami. Is there also a similar way for removing a module? Couldn't find it in the reference you gave. – A. Sarid Jan 26 '17 at 07:32
  • 1
    I did not encounter anywhere where removing a kernel module is done from within the kernel. If there was such a method, the natural place for it will be in http://lxr.free-electrons.com/source/include/linux/kmod.h, but there is nothing like that in that header. Rami Rosen – Rami Rosen Jan 26 '17 at 23:00
1

I believe you cannot do that directly, but I could be wrong.

I would suggest to write some data which goes into some user-space application (perhaps systemd, or your own daemon) thru some file descriptor (maybe some socket(7), probably of netlink(7) family).

I don't know well all the details, but I do question the approach of loading a kernel module directly from kernel code, because kernel modules have been invented to "increase" the kernel from user-land applications. If you know what exact kernel code you need, consider linking all of it together. See also modprobe(8)

(your question needs a concrete use case and some motivation; I don't understand why you need to avoid going to userspace; and I would like to understand what actual kernel modules you are thinking of, and what is their exact roles)

BTW, kernel modules should not be loaded frequently. If you have to do that (e.g. loading a kernel module every few milliseconds) it means that you have a design error. A kernel module (or several of them) is generally loaded once and should not disturb the kernel when it is useless. So you should improve your overall design to avoid the need of unloading and reloading frequently your kernel modules. Conceptually at least, you should think of loading all your kernel modules at boot time and unloading them (in the appropriate order) at shutdown time.

The case is that I have two kernel modules that I should load and unload dynamically.

This is probably a symptom of a huge architecture design mistake. These are the most costly bugs. You should consider budgeting several months of efforts to avoid it.

Probably you should describe more the dependencies between [your particular] kernel modules. AFAIK, the modprobe command manages such dependencies (e.g. won't unload a module without having first unloaded the required dependencies)

If you are designing & developing both kernel modules, you should consider merging them into a single kernel module (and budget several months of effort for that task). Of course, you may need to change its "API", e.g. some device drivers or ioctl to be able to enable or disable some part of it.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks for the answer. Those kernel modules are not going to be loaded / unloaded frequently. They are hardware dependent, but once I unloaded one and loaded the other they will stay like this. – A. Sarid Jan 25 '17 at 08:34