7

I am writing a loadable kernel module for Linux. And I need to store some data for each task in Linux kernel (These data would be used in a scheduler callback).

I know that I can modify struct task_struct and insert my own fields. But since I am willing to write a relatively clean kernel module, I cannot modify any code resides in original Linux source tree.

It is also possible to maintain some sort of mapping from struct task_struct to my data in a hash table. But it seem to be a little too heavy-weight.

I've read the answer in Thread local data in linux kernel module. It mentioned using private_data in struct file. But it needs every thread to open it in order to get an struct file. And there's no way the query the per-task data with a struct task_struct. (As I need to use the data in a scheduler callback)

My question is: is there any simple and clean way that allows me registering per-task data-structure in Linux kernel without modifying struct task_struct?

Many thanks!

4
  • What is that per-task data and what is your module really doing? Explain much more! Commented Aug 28, 2013 at 2:00
  • @Basile Starynkevitch I need to store some data for each task_struct in kernel. And during scheduling these data might be accessed. Imagine that I am writing a module that counts #context-switch for each thread (I know this functionality exists in original kernel so it's just an example. It's difficult to explain what my module really doing here clearly in a few words.) Commented Aug 28, 2013 at 2:16
  • @Naruil: you have a whole question to say this. Very few kernel modules really need to keep track of per-process data; if you can say why this is needed, it will make it clearer how to proceed. Commented Aug 28, 2013 at 2:50
  • @nneonneo Ok, let me make it clearer. This module implements a high-performance kernel-assisted sync mechanism for user programs. Each user thread would register a data structure to kernel. And this data structure may be accessed in kernel space by a scheduler callback function and some sync primitives it exposed via a device. So do you have any ideas? Commented Aug 28, 2013 at 3:59

1 Answer 1

3

The only simple and clean way that allows you to register a per-task data structure is to modify struct task_struct.

Modules are designed for optional parts of the kernel; they can use only functions that are explicitly exported from the base kernel. if you have to change the base kernel, you can no longer use modules.

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

Comments

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.