简体   繁体   English

如何在内核模块中使用sysfs?

[英]How to use sysfs inside kernel module?

In userspace I can just echo noop > /sys/block/sda/queue/scheduler . 在用户空间中,我只能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) 我的内核模块仅注册SysRQ处理程序,并应在触发SysRQ时配置io调度程序(由于io-scheduler错误,此时可以挂载用户空间程序)

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 如果您仍然想这样做,并且也了解它是一个坏主意(tm)的原因,请参阅本文

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. 如果要为内核模块配置某些内容,则可以在包装脚本中执行此操作,该脚本使用insmod命令插入您的内核模块。

And have a look at this article where it tell "Why it is bad to write files from Kernel" 并查看这篇文章,它告诉“为什么从内核写入文件很糟糕”

Wrong wrong wrong. 错错错错。 sysfs is an interface to userspace , you should not be using it inside the kernel. sysfsuserspace的接口,您不应在内核内部使用它。

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. 当用户写入/sys/block/sda/queue/scheduler某些内核代码正在运行,您应该直接调用该代码。

Having said that this seems like a Bad Idea, how will you handle multiple block devices for example? 说了这似乎是个坏主意,例如,您将如何处理多个块设备?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM