You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Also:
* fix fops.c on both kernels:
* 5.9: the out of space error code was 1 not 8
* 6.6: for whatever reason we can't read the user buffer as before on the
diagnostic print, it leads to segfault and oops
* create memfile.c which is like fops.c but of unlimited size
and the new `pr_info` message should now show on the terminal at the end of the boot.
340
340
341
+
If you are simultaneously developing the test script and the kernel module, some smart test scripts should take the kernel module as first argument so you can directly run:
File operations are the main method of userland driver communication.
7693
+
File operations are the main method of userland driver communication that uses common file system calls such as read and write.
7694
+
7695
+
Through `struct file_operations` drivers tell the kernel what it should do on filesystem system calls of <<pseudo-filesystems>>.
7696
+
7697
+
[[fops]]
7698
+
===== fops.c
7699
+
7700
+
This example illustrates the most basic system calls: `open`, `read`, `write`, `close` and `lseek`.
7686
7701
7687
-
`struct file_operations` determines what the kernel will do on filesystem system calls of <<pseudo-filesystems>>.
7702
+
* link:kernel_modules/fops.c[]
7703
+
* link:rootfs_overlay/lkmc/fops.sh[]
7688
7704
7689
-
This example illustrates the most basic system calls: `open`, `read`, `write`, `close` and `lseek`:
7705
+
In it we create a debugfs special file that behaves like a regular file, except that it is stored in memory for as long as the kernel module is loaded, and it has a fixed lengh of 4 bytes. Any longer `write` attempt gets simply truncated up at the end:
7690
7706
7691
7707
....
7692
7708
./fops.sh
@@ -7699,11 +7715,6 @@ Outcome: the test passes:
7699
7715
0
7700
7716
....
7701
7717
7702
-
Sources:
7703
-
7704
-
* link:kernel_modules/fops.c[]
7705
-
* link:rootfs_overlay/lkmc/fops.sh[]
7706
-
7707
7718
Then give this a try:
7708
7719
7709
7720
....
@@ -7714,6 +7725,14 @@ We have put printks on each fop, so this allows you to see which system calls ar
7714
7725
7715
7726
No, there no official documentation: https://stackoverflow.com/questions/15213932/what-are-the-struct-file-operations-arguments
7716
7727
7728
+
[[memfile]]
7729
+
====== memfile.c
7730
+
7731
+
This example behaves the same as <<fops>>, except that the in-memory virtual file has unlimited size. In the kernel module we have therefore to so a bit of memory management and somehow increase the size of the buffer as needed.
This kernel module is a port of scull example from LDD3. It was tested on LKMC e1834763088b8a7532b5fae800039de880471f2d + 1 with Linux kernel 6.8.12.
10020
+
10021
+
"Scull" is an acronym for "Simple Character Utility for Loading Localities". This expansion is mostly meaningless however, but there you are.
10022
+
10023
+
Source code:
10024
+
10025
+
* link:kernel_modules/scull.c[]
10026
+
* link:rootfs_overlay/lkmc/scull.sh[]
10027
+
10028
+
Create the devices and test them:
10029
+
10030
+
....
10031
+
scull.sh
10032
+
....
10033
+
10034
+
scull creates several character devices.
10035
+
10036
+
The most "basic" one is `/dev/scull0`, which acts a bit as an in-memory file, except that it has weird quantizations applied to it so that you can't append as normal and it doesn't really look like a regular file. What it actually is more like is an object pool.
10037
+
10038
+
The original scull interface is very weird and would erase all data on write-only `O_WRONLY`, but not on read/write `O_RDWR`, which doesn't make much sense:
10039
+
10040
+
....
10041
+
int scull_open(struct inode *inode, struct file *filp) {
10042
+
if ( (filp->f_flags & O_ACCMODE) == O_WRONLY)
10043
+
scull_trim(dev); /* ignore errors */
10044
+
....
10045
+
10046
+
We have modified that to the much more reasonable:
10047
+
10048
+
....
10049
+
if ((filp->f_flags & O_TRUNC)) {
10050
+
....
10051
+
10052
+
The old weird truncation condition makes the code hard to test as there is no way to write to two different blocks like it and keep them both in memory, unless you are able to find a CLI tool that supports `O_RDWR` or you write a C program to test things.
10053
+
10054
+
With our new inferface, we can differentiate clear all vs don't clear all in the usual manner, e.g. this clears:
10055
+
10056
+
....
10057
+
echo asdf > /dev/scull0
10058
+
....
10059
+
10060
+
but this doesn't:
10061
+
10062
+
....
10063
+
echo asdf >> /dev/scull0
10064
+
....
10065
+
10066
+
The examples from our test should make its weird behavior clearer e.g.:
10067
+
10068
+
....
10069
+
# Append starts writing from the start of the 4k block, not like the usual semantic.
10070
+
printf asdf > "$f"
10071
+
printf qw >> "$f"
10072
+
[ "$(cat "$f")" = qwdf ]
10073
+
10074
+
# Overwrite first clears everything, then writes to start of 4k block.
printf we | dd of="$f" bs=1 seek=1 conv=notrunc status=none
10086
+
[ "$(cat "$f")" = aqwf ]
10087
+
...
10088
+
10089
+
It is also worth noting that the implementation of scull is meant to be "readable" but not optimal:
10090
+
10091
+
____
10092
+
kmalloc is not the most efficient way to allocate large areas of memory (see Chapter 8), so the implementation chosen for scull is not a particularly smart one. The source code for a smart implementation would be more difficult to read, and the aim of this section is to show read and write, not memory management. That’s why the code just uses kmalloc and kfree without resorting to allocation of whole pages, although that approach would be more efficient.
10093
+
____
10094
+
10095
+
Another shortcoming of the example is that it uses mutexes, where rwsem would be the clearly superior choice.
10096
+
10097
+
This module was derived from https://github.com/martinezjavier/ldd3/tree/30f801cd0157e8dfb41193f471dc00d8ca10239f/scull which had already ported it to much more recent kernel versions for us. Ideally we should just use that repo as a submodule, but we were lazy to setup the buildroot properly for now, and decided to dump it all into a single file to start with.
10098
+
9997
10099
== FreeBSD
9998
10100
9999
10101
https://en.wikipedia.org/wiki/FreeBSD
@@ -28112,6 +28214,14 @@ The `--linux-build-id` option should be passed to all scripts that support it, m
28112
28214
28113
28215
To run both kernels simultaneously, one on each QEMU instance, see: xref:simultaneous-runs[xrefstyle=full].
28114
28216
28217
+
You can also build <<kernel-modules>> against a specific prebuilt kernel with:
28218
+
28219
+
....
28220
+
./build-modules --linux-build-id v4.16
28221
+
....
28222
+
28223
+
This will then allow you to insmod the kernel modules on your newly built kernel.
28224
+
28115
28225
==== QEMU build variants
28116
28226
28117
28227
Analogous to the <<linux-kernel-build-variants>> but with the `--qemu-build-id` option instead:
0 commit comments