Skip to content
Learn Netverks
0

Does the Linux kernel interrupt any running code to call the __exit function when unloading a kernel module?

asked 8 hours ago by @qa-9b0f9v4weaoxauryvll4 0 rep · 65 views

c linux linux kernel kernel module

I have a cleanup function that I call in my code. The __exit function also goes through the same routine, but we will have a global flag for that to not run it twice. My question is, if my cleanup function is mid-execution (not using kthreads, directly on the CPU) and a SYS_delete_module syscall is made (like by rmmod), will it just interrupt and eventually exit with my cleanup function only having partially executed?

LLMs are giving mixed answers. One says the kernel checks if the instruction pointer is in the module's memory and waits for it, other says there is a global mutex lock used by the kernel and all.

Can I, and if so, should I increment the reference count of my module for safety?

Comments on this question (0)

Use comments to ask for clarification — answers go in the answer box below.

Log in to comment on this question.

3 answers

0

The source code for the kernel is available, ask the LLM to point you to the code that does "checks if the instruction pointer is in the module's memory" and the alleged global lock. I don't think either is a thing.

If a process is currently using the module, it should have a non-zero reference count.

Riley Brooks · 0 rep · 8 hours ago

0

if my cleanup function is mid-execution (not using kthreads, directly on the CPU)

What's the context here? How did it get to be executed, what triggered its execution then?

will it just interrupt

Fairly sure there is no interruption going on. That would be extremely dangerous to do. The kernel just calls your module_exit function and that's it. It is up to you to ensure your module stops doing things before that functione exits.

Reese Nguyen · 0 rep · 8 hours ago

0

I think you need to either bump your reference count or mutex out your cleanup routine to make sure it us not called simultaneously by two different threads of execution (whether they are managed by kthreads or not). Pretty sure there's no interruption. LLMs seem to be on some hard drugs. Program counter? Come on. What if you call some function that is not in your module? Will the kernel unroll the stack? This is ridiculous.

Quinn Hayes · 0 rep · 8 hours ago

Your answer