Aurelian_15
Posts: 9
Joined: Sun Nov 11, 2012 4:59 pm

Can no longer compile kernel modules on Raspbian with RPi 1B

Sun Jan 13, 2019 8:22 pm

I have trouble compiling out-of-tree kernel modules on my 2012 Raspberry Pi 1B.

The system is a fresh installation of Raspbian. I have updated and upgraded all packages on the system, and installed the raspberrypi-kernel-headers package. uname -r gives 4.14.79+. The directory /usr/src/linux-headers-4.14.79+ exists, and /lib/modules/4.14.79+/build correctly symlinks to the former.

However, I get very weird error messages when trying to compile a kernel module.

Here is a minimal example:

Code: Select all

// module.c

#include <linux/module.h>

MODULE_LICENSE("GPL v2");

Code: Select all

# Makefile

KVERSION := $(shell uname -r)
PWD := $(shell pwd)

obj-m := module.o

all:
	+make -C /lib/modules/$(KVERSION)/build/ M=$(PWD) modules
clean:
	+make -C /lib/modules/$(KVERSION)/build/ M=$(PWD) clean
Running make in a directory with these two files results in the following messages:

Code: Select all

make -C /lib/modules/4.14.79+/build/ M=/home/pi modules
make[1]: Entering directory '/usr/src/linux-headers-4.14.79+'
  CC [M]  /home/pi/module.o
/home/pi/module.c:3:16: error: expected declaration specifiers or ‘...’ before string constant
 MODULE_LICENSE("GPL v2");
                ^~~~~~~~
scripts/Makefile.build:334: recipe for target '/home/pi/module.o' failed
make[2]: *** [/home/pi/module.o] Error 1
Makefile:1527: recipe for target '_module_/home/pi' failed
make[1]: *** [_module_/home/pi] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.14.79+'
Makefile:7: recipe for target 'all' failed
make: *** [all] Error 2
Compiling this minimal kernel module works without any problem on other computers I tested. I can however reproduce the same issue with Arch Linux ARM on the same Raspberry Pi, so my guess would be that this is a problem with the raspberrypi-kernel-headers package provided by the Raspberry Pi foundation.

So, my question, is this really a bug or am I just missing something?

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Can no longer compile kernel modules on Raspbian with RPi 1B

Sun Jan 13, 2019 10:09 pm

Try this: https://github.com/notro/rpi-source/wiki
rpi-source will get you a current kernel source tree that matches your running kernel.

I created ~/module.c and ~/Makefile from your samples and they worked OK.

Code: Select all

root@pioneer:~ # vi module.c
root@pioneer:~ # vi Makefile
root@pioneer:~ # make
make -C /lib/modules/4.14.92+/build/ M=/root modules
make[1]: Entering directory '/root/linux-4de3f6305841c2121257b8fdbda5c3c0bf77e669'
  CC [M]  /root/module.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/module.mod.o
  LD [M]  /root/module.ko
make[1]: Leaving directory '/root/linux-4de3f6305841c2121257b8fdbda5c3c0bf77e669'
root@pioneer:~ #
I even managed to get it loaded.

Code: Select all

[191058.250549] module: loading out-of-tree module taints kernel.
[191058.250786] module: module is already loaded
[191074.748224] module: module is already loaded
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Aurelian_15
Posts: 9
Joined: Sun Nov 11, 2012 4:59 pm

Re: Can no longer compile kernel modules on Raspbian with RPi 1B

Sun Jan 13, 2019 11:02 pm

Thank you! Indeed, with the kernel source code fetched by rpi-source I can successfully compile my module.

However, my understanding was that rpi-source should no longer be necessary for building kernel modules now that raspberrypi-kernel-headers exists (unless someone is using a newer kernel fetched via rpi-update). So my guess would be that this is indeed a problem with raspberrypi-kernel-headers.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Can no longer compile kernel modules on Raspbian with RPi 1B

Mon Jan 14, 2019 1:25 am

The kernel headers package seems to follow the raspberrypi-kernel package (current stable kernel). My systems are always a long way ahead of that (some are running 4.19 with BRANCH=next rpi-update). I like rpi-source as it's a simple way to get something that seems to work more often than not.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Advanced users”