0

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:

  • First try: <kernel/sched.h>
  • Second try: <kernel/sched/sched.h>
  • Third try: <../kernel/sched/sched.h> - just copying paste from a comment in this question

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?
2
  • you should mention you need linux module, not bsd one Commented Oct 27, 2020 at 14:30
  • @АлексейНеудачин it's tagged linux-kernel for a reason. Commented Oct 27, 2020 at 14:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. Could you help me with follow up questions: 1) I'm not sure what you mean by redefine them. Are you suggesting I provide the logic for those functions by myself? I really don't want to do that because I'm looking at what the kernel has to offer. Essentially, I want to look into each sched class and see its list of tasks. However, I'm interested in how one might rewrite the for_each_class such that it provides the same capability as the kernel's version. 2) What kernel code files can I directly include in my module then? Is it just limited to those in the /include directory?
3) Unsure what this means really... if you want to know if there is some way to configure your Makefile... I want to know if there are anyways at all to write a Makefile that allows me to include those files.
1) redefine as in: directly copy-paste them into your module, that's how you would "rewrite" those 2) you can only include those that are under include directories 3) no there is no way, as I said the file is simply not there. The kernel does not export it to be used by modules.
There are a lot of them, a classic example is /arch/<SOMEARCH>/include, which are include dirs with architecture specific header files. Nonetheless, all the files in those are available to #include directly because the kernel Makefile specifically tells the compiler to look in all those directories by using the -I<path> option, so you usually don't have to worry about it.
@TranTriet Yep, exactly. Here's an example. Of course, for files under arch you will only be able to #include those that match the architecture you are compiling for.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.