Tran Triet
Tran Triet

Reputation: 1307

How to include an arbitrary kernel header file in a module?

Situation

I'm writing a kernel module for learning purpose and want to use sched_class_highest and for_each_class as defined here.

I saw that if I want to use symbols in the file /include/linux/sched.h, I'll #include <linux/sched.h>. So in the same manner, since the file I'm trying to add now is at location /kernel/sched/sched.h, I tried to: #include:

None of these worked and all gave me the error No such file or directory.

I'm not very familiar with C, just trying to learn the kernel and pick up C as I go a long the way.

Here's my Makefile. I knew basics about Makefile's target but don't understand the compiling process or what files it needs to feed it...

MODULE_FILENAME=my-schedule

obj-m +=  $(MODULE_FILENAME).o
KO_FILE=$(MODULE_FILENAME).ko

export KROOT=/lib/modules/$(shell uname -r)/build

modules:
        @$(MAKE) -C $(KROOT) M=$(PWD) modules

modules_install:
        @$(MAKE) -C $(KROOT) M=$(PWD) modules_install

clean: 
        @$(MAKE) -C $(KROOT) M=$(PWD) clean
        rm -rf   Module.symvers modules.order

insert: modules
        sudo insmod $(KO_FILE)
        sudo dmesg -c

remove:
        sudo rmmod $(MODULE_FILENAME)
        sudo dmesg -c

printlog:
        sudo dmesg -c 
        sudo insmod $(KO_FILE)
        dmesg

Questions

  1. How do I reference those 2 symbols in my kernel module?
  2. Are those symbols supposed to be referenced directly by a standard module? If so, what is the standard way of doing it?
  3. Is the Makefile related to how I can import a file in kernel code into my module?

Upvotes: 0

Views: 1255

Answers (1)

Marco Bonelli
Marco Bonelli

Reputation: 69346

How do I reference those 2 symbols in my kernel module?

You should not need those in a "normal" kernel module. If you really do, re-define them in your module.

Are those symbols supposed to be referenced directly by a standard module?

They are not, that particular sched.h file is only supposed to be used by scheduler code (files in kernel/sched). That's why it is not exported (i.e. not in the include dir). You will not find them in your headers folder (/lib/modules/.../build).

Is the Makefile related to how I can import a file in kernel code into my module?

Unsure what this means really... if you want to know if there is some way to configure your Makefile such that it will be possible to include that file, then there is not. If you are building using the kernel headers exported by the kernel in /lib/modules (which is what you are doing when you hand over control to the kernel's Makefile with $(MAKE) -C $(KROOT)) then the file is simply not there.

Upvotes: 1

Related Questions