3

Recently I was looking through the kernel at kobjects and sysfs.

I know/understand the following..

  • All kernel objects use addresses > 0x80000000
  • kobjects should be no exception to this rule
  • The sysfs is nothing but a hierarchy of kobjects (maybe includes ksets and other k* stuff..not sure)

Given this information, I'm not sure I understand exactly what happens when I run echo ondemand >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

I can see that the cpufreq module has a function called store_scaling_governor which handles writes to this 'file'..but how does usermode transcend into kernelmode with this simple echo?

Guru Prasad
  • 4,053
  • 2
  • 25
  • 43

2 Answers2

6

When you execute command echo ondemand >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor, your shell calls write system call, then kernel dispatch it for corresponding handler.

The cpufreq setups struct kobj_type ktype_cpufreq with sysfs_ops. Then cpufreq register it in cpufreq_add_dev_interface(). After that, kernel can get corresponding handler to execute on write syscall.

Alexey Shmalko
  • 3,678
  • 1
  • 19
  • 35
  • 3
    To be precise, your shell calls the open() syscall on /sys/devices/system/cpu/cpufreq/scaling_governor, then forks a child process, moves the file descriptor to stdout (1), then execs the "echo" program, which calls the write() system call to write "ondemand\n" to stdout (which gets passed to the driver code). – jtchitty Feb 15 '14 at 00:03
0

I can tell you one implementation which I have used for accessing kernel space variables from sysfs (user-space in shell prompt).Basically each set of variables which are exposed to user-space in sys file system appear as a separate file under /sys/.Now when you issue an echo value > /sys/file-path in shell prompt (user-space).When you do so the respective method which gets called in kernel space in .store method.Additionally when you issue cat /sys/file-path the respective method which gets called is .show in kernel.You can see more information about here: http://lwn.net/Articles/31220/

a.saurabh
  • 1,163
  • 10
  • 15