How is userspace able to write to sysfs

linux-kernel, sysfs

Solution

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.

Problem

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?

Original source