Skip to content

Commit 9a4eae5

Browse files
committed
Move debugfs, rootfs and procfs documentation to README
1 parent 0cd1a2b commit 9a4eae5

File tree

11 files changed

+149
-98
lines changed

11 files changed

+149
-98
lines changed

README.adoc

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,20 +2879,117 @@ You should then look up if there is a branch that supports that kernel. Staying
28792879

28802880
Pseudo filesystems are filesystems that don't represent actual files in a hard disk, but rather allow us to do special operations on filesystem-related system calls.
28812881

2882-
Some notable examples include:
2883-
2884-
* procfs, often mounted at: `/proc`
2885-
* sysfs, often mounted at: `/sys`
2886-
* devtmpfs, often mounted at: `/dev`
2887-
* debugfs, often mounted at: `/sys/kernel/debug/`
2888-
28892882
What each pseudo-file does for each related system call does is defined by its <<file-operations>>.
28902883

28912884
Bibliography:
28922885

28932886
* https://superuser.com/questions/1198292/what-is-a-pseudo-file-system-in-linux
28942887
* https://en.wikipedia.org/wiki/Synthetic_file_system
28952888

2889+
==== debugfs
2890+
2891+
In guest:
2892+
2893+
....
2894+
/debugfs.sh
2895+
echo $?
2896+
....
2897+
2898+
Outcome: the test passes:
2899+
2900+
....
2901+
0
2902+
....
2903+
2904+
Sources:
2905+
2906+
* link:kernel_module/debugfs.c[]
2907+
* link:rootfs_overlay/debugfs.sh[]
2908+
2909+
Debugfs is the simplest pseudo filesystem to play around with, as it is made specifically to help test kernel stuff. Just mount, set <<file-operations>>, and we are done.
2910+
2911+
For this reason, it is the filesystem that we use whenever possible in our tests.
2912+
2913+
`debugfs.sh` explicitly mounts a debugfs at a custom location, but the most common mount point is `/sys/kernel/debug`.
2914+
2915+
This mount not done automatically by the kernel however: we, like most distros, do it from userland with our link:rootfs_overlay/etc/fstab[fstab].
2916+
2917+
Debugfs support requires the kernel to be compiled with `CONFIG_DEBUG_FS=y`.
2918+
2919+
Only the more basic file operations can be implemented in debugfs, e.g. `mmap` never gets called:
2920+
2921+
* https://patchwork.kernel.org/patch/9252557/
2922+
* https://github.com/torvalds/linux/blob/v4.9/fs/debugfs/file.c#L212
2923+
2924+
Bibliography: https://github.com/chadversary/debugfs-tutorial
2925+
2926+
==== procfs
2927+
2928+
In guest:
2929+
2930+
....
2931+
/procfs.sh
2932+
echo $?
2933+
....
2934+
2935+
Outcome: the test passes:
2936+
2937+
....
2938+
0
2939+
....
2940+
2941+
Sources:
2942+
2943+
* link:kernel_module/procfs.c[]
2944+
* link:rootfs_overlay/procfs.sh[]
2945+
2946+
Just another fops entry point.
2947+
2948+
Bibliography: https://stackoverflow.com/questions/8516021/proc-create-example-for-kernel-module/18924359#18924359
2949+
2950+
==== sysfs
2951+
2952+
In guest:
2953+
2954+
....
2955+
/sysfs.sh
2956+
echo $?
2957+
....
2958+
2959+
Outcome: the test passes:
2960+
2961+
....
2962+
0
2963+
....
2964+
2965+
Sources:
2966+
2967+
* link:kernel_module/sysfs.c[]
2968+
* link:rootfs_overlay/sysfs.sh[]
2969+
2970+
Vs procfs:
2971+
2972+
* https://unix.stackexchange.com/questions/4884/what-is-the-difference-between-procfs-and-sysfs
2973+
* https://stackoverflow.com/questions/37237835/how-to-attach-file-operations-to-sysfs-attribute-in-platform-driver
2974+
2975+
This example shows how sysfs is more restricted, as it does not take an arbitrary `file_operations`.
2976+
2977+
So you basically can only do `open`, `close`, `read`, `write`, and `lseek` on sysfs files.
2978+
2979+
It is similar to a `seq_file` file operation, except that write is also implemented.
2980+
2981+
TODO: what are those `kobject` structs? Make a more complex example that shows what they can do.
2982+
2983+
Bibliography:
2984+
2985+
* https://github.com/t3rm1n4l/kern-dev-tutorial/blob/1f036ef40fc4378f5c8d2842e55bcea7c6f8894a/05-sysfs/sysfs.c
2986+
* https://www.kernel.org/doc/Documentation/kobject.txt
2987+
* https://www.quora.com/What-are-kernel-objects-Kobj
2988+
* http://www.makelinux.net/ldd3/chp-14-sect-1
2989+
* https://www.win.tue.nl/~aeb/linux/lk/lk-13.html
2990+
2991+
=== Pseudo files
2992+
28962993
==== File operations
28972994

28982995
In guest:
@@ -2919,7 +3016,7 @@ File operations is the main method of userland driver communication.
29193016

29203017
No, there no official documentation: http://stackoverflow.com/questions/15213932/what-are-the-struct-file-operations-arguments
29213018

2922-
==== Character device
3019+
==== Character devices
29233020

29243021
In guest:
29253022

@@ -2940,7 +3037,7 @@ Sources:
29403037
* link:rootfs_overlay/mknoddev.sh[]
29413038
* link:kernel_module/character_device.c[]
29423039

2943-
Charcter device files are created with:
3040+
Character device files are created with:
29443041

29453042
....
29463043
mknod </dev/path_to_dev> c <major> <minor>
@@ -2967,7 +3064,7 @@ which means:
29673064
* `c` (first letter): this is a character device. Would be `b` for a block device.
29683065
* `1, 9`: the major number is `1`, and the minor `9`
29693066

2970-
To avoid device number conflicts when registring the driver we:
3067+
To avoid device number conflicts when registering the driver we:
29713068

29723069
* ask the kernel to allocate a free major number for us with: `register_chrdev(0`
29733070
* find ouf which number was assigned by grepping `/proc/devices` for the kernel module name

kernel_module/README.adoc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@
2020
... link:dep2.c[]
2121
. Pseudo filesystems
2222
.. link:anonymous_inode.c[]
23-
.. link:debugfs.c[]
2423
.. link:ioctl.c[]
2524
.. link:mmap.c[]
2625
.. link:poll.c[]
27-
.. link:procfs.c[]
2826
.. link:seq_file.c[]
2927
.. link:seq_file_inode.c[]
30-
.. link:sysfs.c[]
3128
. Asynchronous
3229
.. link:irq.c[]
3330
.. link:kthread.c[]

kernel_module/character_device.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#character-devices */
2+
13
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */
24
#include <linux/module.h>
35
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */

kernel_module/character_device_create.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#automatically-create-character-device-file-on-insmod */
2+
13
#include <linux/cdev.h>
24
#include <linux/device.h>
35
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */

kernel_module/debugfs.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
/*
2-
Adapted from: https://github.com/chadversary/debugfs-tutorial/blob/47b3cf7ca47208c61ccb51b27aac6f9f932bfe0b/example1/debugfs_example1.c
3-
4-
Usage:
5-
6-
/debugfs.sh
7-
8-
Requires `CONFIG_DEBUG_FS=y`.
9-
10-
Only the more basic fops can be implemented in debugfs, e.g. mmap is never called:
11-
12-
- https://patchwork.kernel.org/patch/9252557/
13-
- https://github.com/torvalds/linux/blob/v4.9/fs/debugfs/file.c#L212
14-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#debugfs */
152

163
#include <linux/debugfs.h>
174
#include <linux/kernel.h>

kernel_module/procfs.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
/*
2-
Yet another fops entrypoint.
3-
4-
https://stackoverflow.com/questions/8516021/proc-create-example-for-kernel-module
5-
6-
insmod /procfs.ko
7-
cat /proc/lkmc_procfs
8-
9-
Output:
10-
11-
abcd
12-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#procfs */
132

143
#include <linux/debugfs.h>
154
#include <linux/module.h>

kernel_module/sysfs.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,4 @@
1-
/*
2-
Adapted from: https://github.com/t3rm1n4l/kern-dev-tutorial/blob/1f036ef40fc4378f5c8d2842e55bcea7c6f8894a/05-sysfs/sysfs.c
3-
4-
Vs procfs:
5-
6-
- https://unix.stackexchange.com/questions/4884/what-is-the-difference-between-procfs-and-sysfs
7-
- https://stackoverflow.com/questions/37237835/how-to-attach-file-operations-to-sysfs-attribute-in-platform-driver
8-
9-
This example shows how sysfs is more restricted, as it does not take a file_operations.
10-
11-
So you basically can only do open, close, read, write, and lseek on sysfs files.
12-
13-
It is kind of similar to a seq_file file_operations, except that write is also implemented.
14-
15-
TODO: what are those kobject structs? Make a more complex example that shows what they can do.
16-
17-
- https://www.kernel.org/doc/Documentation/kobject.txt
18-
- https://www.quora.com/What-are-kernel-objects-Kobj
19-
- http://www.makelinux.net/ldd3/chp-14-sect-1
20-
- https://www.win.tue.nl/~aeb/linux/lk/lk-13.html
21-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#sysfs */
222

233
#include <linux/init.h>
244
#include <linux/kobject.h>

rootfs_overlay/debugfs.sh

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
#!/bin/sh
2-
set -ex
2+
set -e
33
d=/debugfs
44
mkdir -p "$d"
5-
# We also added a fstab entry that mounts this under /sys/kernel/debug autmoatically.
6-
# That is the most common place to mount it.
7-
# The /sys/kernel/debug directory gets created automatically when debugfs is
8-
# compiled into the kernel, but it does not get mounted automatically.
9-
mount -t debugfs none /debugfs
5+
mount -t debugfs none "$d"
106
insmod /debugfs.ko
11-
cd "${d}/lkmc_debugfs"
12-
13-
cat myfile
14-
# => 42
15-
16-
echo 13 > myfile
17-
cat myfile
18-
# => 13
19-
7+
[ "$(cat "${d}/lkmc_debugfs/myfile")" = 42 ]
8+
echo 13 > "${d}/lkmc_debugfs/myfile"
9+
[ "$(cat "${d}/lkmc_debugfs/myfile")" = 13 ]
2010
echo 666 > "${d}/lkmc_debugfs_file"
21-
cat myfile
22-
# => 666
11+
[ "$(cat "${d}/lkmc_debugfs/myfile")" = 666 ]
12+
rmmod debugfs
13+
umount "$d"

rootfs_overlay/procfs.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
set -e
3+
insmod /procfs.ko
4+
[ "$(cat "/proc/lkmc_procfs")" = abcd ]
5+
rmmod procfs

rootfs_overlay/sysfs.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/bin/sh
2-
3-
set -x
2+
set -e
43
insmod /sysfs.ko
5-
cd /sys/kernel/lkmc_sysfs
6-
printf 12345 >foo
7-
cat foo
8-
# => 1234
9-
dd if=foo bs=1 count=2 skip=1 status=none
10-
# => 23
4+
f=/sys/kernel/lkmc_sysfs/foo
5+
# write
6+
printf 12345 > "$f"
7+
# read
8+
[ "$(cat "$f")" = 1234 ]
9+
# seek
10+
[ "$(dd if="$f" bs=1 count=2 skip=1 status=none)" = 23 ]
1111
rmmod sysfs

0 commit comments

Comments
 (0)