4

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)

Vi.
  • 37,014
  • 18
  • 93
  • 148

3 Answers3

3

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

shodanex
  • 14,975
  • 11
  • 57
  • 91
  • 1
    The article is primarily about writing and reading files in kernel. (I've already tried to implement http://stackoverflow.com/questions/1184274/kernelhow-to-read-write-files-within-kernel-module/1184346#1184346 , but it panicked). It is told somewhere that sysfs content consist of kobjects. So it should be better manageable from inside the kernel. – Vi. Dec 10 '10 at 12:10
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"

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
Huang F. Lei
  • 1,835
  • 15
  • 23
  • No, it is not configuration of my kernel module. My module wants to configure something else. – Vi. Dec 10 '10 at 12:07
0

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?

mpe
  • 2,640
  • 1
  • 19
  • 17
  • 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") – Vi. Dec 12 '10 at 12:12
  • 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 `/`) – bdonlan Jan 11 '11 at 09:57