Skip to the content ForoHackers
Log inJoin

Kernel newbie, first module compiled, immediately panicked

Device driversIntroductionsKernels
#1163 · k3rn3l_k1tt3n · · Report

Hi all. I am learning Linux kernel programming and I am having far too much fun. This week I built my first loadable module: it prints a line with printk when loaded and another when removed. Total: about fifteen lines. Then I made a typo in the second version and took the whole virtual machine down with a very satisfying screen of text.

My setup is simple on purpose: everything runs in a throwaway VM with snapshots, never on my main machine.

Interests: how drivers talk to hardware, how the kernel schedules things, how memory is handed out. Current question: when I read a driver and see container_of, my brain does not accept it. It takes a pointer to a member and gives back a pointer to the whole struct. I have read the macro four times.

Be kind, I bounce.

#1961 · CafecitoConKernel · · Report

Tu enfoque de VM con snapshots es el correcto. Sobre container_of: es aritmética de punteros. Si sabes dónde está el miembro dentro de la estructura (offsetof), restas ese desplazamiento a la dirección del miembro y obtienes la dirección del principio. Dibuja la struct en una hoja con los offsets y se te enciende la bombilla.

#1962 · null_terminated_hopes · · Report

Ah, the only programming environment where a typo takes the whole machine with it. It builds character. And a taste for snapshots.

#1963 · k3rn3l_k1tt3n · · Report

Gracias, Cafecito. Drawing it now: struct, member at offset 16, subtract 16. I think I have it. The magic is that the kernel's linked lists embed the node inside your struct, so container_of is how you get back to your data.

#1964 · ghost_in_the_heap · · Report

When the VM falls over, read the first lines of the oops: the kind of fault, the instruction pointer, and the call trace. Match the instruction pointer to a function with your module's symbols and you are usually one line away from the bug. Typos in the unload path are classic. Keep the snapshot habit; it will pay for itself.

Log in or join to reply.