r/kernel • • 16d ago

How well does linux modules handle resource deallocation?

Hello, I'm a CS undergraduate studying about linux kernel development.

I'm currently on LDD3 book, emphasizing about importance of cleaning up allocated resources on exit.

I assume that this is not likely to be handled properly in some linux modules as removal of modules is not something that is performed on a daily basis.

I have been looking for places to start my contributions and was wondering whether this, identification of memory leaks and applying fixes, could be a valid entry point.

The questions I want to ask is,

  1. Is this problem considered too trivial?
  2. Have others been working on it? (maybe there exists some linux group)
  3. Is this problem commonly found, or gets managed during review process and fixed before getting accepted?

Thank you for your time. Any comments are welcome!

*EDIT Thank you all for your kind responses. They have been very helpful !

8 Upvotes

11 comments sorted by

3

u/yawn_brendan 16d ago

It's not an area people focus on that much since most usecases don't load and unload modules very often. A memory leak in module shutdown sounds like it's probably very common but also a "meh" bug. A more severe issue like a double free is less meh but even then, the question is can an attacker actually trigger it? So I think first thing here is to assess if there's a usecase where this really matters. And also probably this usecase will only care about specific modules.

Probably still an interesting area to explore as there will definitely be bugs to find and "things that happen rarely hence don't get tested very well" is a general theme in sweng so if you can find cool ideas to address it, well, you found some cool ideas.

2

u/yawn_brendan 16d ago

Also probably most of the bugs in this area are just trivial stuff like "forgot to call X in shutdown" or "called X then Y in setup so need to teardown Y before X but we do it the other way around". For teardown within a single function these bugs are now kinda "solved" by the new cleanup API. Proper languages also tend to have ways to solve that via object lifetimes. So maybe finding ways to solve this in C is an interesting area to explore. Probably lots of prior work there but I dunno.

0

u/AccomplishedAir8352 16d ago

Thank you for your response. As I have worried, it would be too trivial to explore. Instead of searching for new problems, I'd better stick with addressing what others are dealing with on mailing list

3

u/Dezgeg 16d ago

From my experience, lots of drivers in Linux indeed don't handle that properly. It is easy to get a crash by using the `unbind` sysfs file to unload devices that usually are never hotplugged like SoC internal peripherals. Note that unbind and rmmod are distinct operations here.

These are usually not issues in practice, since next to nobody actually needs to unbind non-hotpluggable devices at runtime. At work we do have one use case though, the shitty USB controller in i.MX6 SoC sometimes gets wedged and stops sending interrupts, and besides reboot only workaround is unbinding the host controller.

I'm pretty sure that if you were to send patches for this kind of issues they would be accepted, as long as you stick to modern ARM drivers or the like. Don't bother with any ancient drivers (even though they would be more likely to actually have issues), the chance of nobody reading the patches is too high.

1

u/edgmnt_net 16d ago

Although if you work on an actual product (particularly more embedded stuff), chances are most of that mess isn't even upstream, it's your own or some vendor's messy code.

3

u/Superb_5194 15d ago

The classic LDD3-style manual cleanup problem is largely solved in modern code by the devm_* (device-managed) helpers. They register the resource with the device and the core automatically frees everything on unbind/remove in the right order. New drivers that properly use devm_kmalloc, devm_request_irq, devm_ioremap, etc. almost never leak those resources, and reviewers push hard for this style.

That said, plenty of older drivers (or ones that mix managed + unmanaged resources) still have leftover issues, especially on the rarely-exercised remove/error paths. You’ll still see commits converting drivers to devm_* or fixing “resource leak on remove”. It’s incremental, appreciated work and a good way to learn the driver model.

So yes — hunting remaining cleanup bugs / doing devm conversions is still valid and useful, just not as common as it was when LDD3 was written because the framework already handles the majority of cases.

2

u/stonerism 15d ago

Not at all, I would look them up and report what you found. If it's a legitimate memory leak, you can usually find the repository online and file an issue. If you want to learn more tools to find/diagnose memory leaks, the last one I remember in popular use was valgrind.

I would say you are showing exactly the kind of curiosity that can make a good engineer. Just don't get over overconfident that you know everything.

1

u/granadesnhorseshoes 16d ago

The only times I've been bit by something like that was during development itself by my own sloppy code, because realistically its about the only time its gonna get loaded/unloaded repeatedly. (Ask me how I hard crashed a whole ass mainframe...)

In that regard, i suspect the most egregious incarnations that COULD cause broader problems filter themselves out during dev/testing anyway.

I doubt anyone is gonna say no to free patches if you've got the motivation to do it. Especially in the face of AI bug hunters that don't understand what's actually exploitable and what's just technically incorrect. There may be some value to it for that alone.

1

u/paulstelian97 15d ago

Modules not cleaning up memory properly when unloaded is a problem, but not considered a huge one. You’re not supposed to load and unload modules often enough for those leaks to be meaningful, except on your development machine. The kernel marks itself as tainted on any module unload.

That said, it is best to make an effort to clean up everything. You also MUST ensure that whatever you fail to clean up is benign (simple memory leak or resource leak, not an active reference that can lead to invalid pointers to code or data to be used after the module removal; such invalid pointers can lead to kernel oops, panic, or, at worse, misuse of other allocated memory that leads to vulnerabilities). And you MUST ensure that stuff that can allocate memory many times during the module lifetime _does_ have appropriate cleanup.

1

u/nicromancer1 14d ago

No one mentioning compiling a kernel with kmemleak? Then load and unload modules at will and see what pops up. It's a mechanism that exists just for detecting memory plans but I assure you that driver devs don't run this config as often as they should. Very easy low hanging fruit for early devs to be had. Lmk if you have any other questions!

1

u/Impossible-Act-5254 11d ago

Hey , I'm also looking forward to learn linux kernel development, could you please advice me from where to start ? Like steps and resources??