5

In userspace I can just echo noop > /sys/block/sda/queue/scheduler.

How to do the same inside a kernel module?

I expect something like this (pseudocode):

struct dentry* e = sysfs_get_root(); vfs_path_lookup(e, ????, "block/sda/queue/scheduler", ???); ????; struct something* q = ????; ????->store(q, "noop", 1); /* some cleanup */ 

How to implement it properly?

My kernel module just registers SysRQ handler and should configure the io scheduler when that SysRQ is triggered (userspace programs can be hung at that time because of the bad io-scheduler)

1
  • NO. Whatever you're doing, this is almost certainly the wrong way to do it. Commented Dec 10, 2010 at 4:34

3 Answers 3

4

There is just no way to implement it properly. If you want to do it anyway, and also understand the reason why it is a Bad Idea (tm), see this article

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

1 Comment

The article is primarily about writing and reading files in kernel. (I've already tried to implement stackoverflow.com/questions/1184274/… , but it panicked). It is told somewhere that sysfs content consist of kobjects. So it should be better manageable from inside the kernel.
2

If you want to configure something for your kernel module, you can do that in a wrapper script which inserts your kernel module using insmod command.

And have a look at this article where it tell "Why it is bad to write files from Kernel"

1 Comment

No, it is not configuration of my kernel module. My module wants to configure something else.
1

Wrong wrong wrong. sysfs is an interface to userspace, you should not be using it inside the kernel.

If your module wants to change the block scheduler then you should work out how to do that inside the kernel, ie. when a user writes to /sys/block/sda/queue/scheduler some kernel code is run, you should be calling that code directly.

Having said that this seems like a Bad Idea, how will you handle multiple block devices for example?

2 Comments

Yes, proper way probably will be iterating over devices and changing the scheduler from special to usual. (Actually I ended up with user-space helper and it seems not to hang when most of the system is stuck). P.S. the io scheduler deliberately hangs the block device to "spin down HDD and not spin up until explicit command")
If you want to make sure the userspace helper won't hang, you can mlockall() it, and keep a handle open to the sysfs file in question (so you won't hang on traversal across /)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.