kernel modules: add a quick scull port from LDD3

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
This commit is contained in:
Ciro Santilli
2025-04-28 15:23:44 +01:00
parent 3d84eccc43
commit e4847e4b40
16 changed files with 2133 additions and 50 deletions

View File

@@ -1,6 +0,0 @@
{
"[c]": {
"editor.tabSize": 8,
"editor.insertSpaces": false
}
}

View File

@@ -1,4 +1,4 @@
/* https://cirosantilli.com/linux-kernel-module-cheat#file-operations */
/* https://cirosantilli.com/linux-kernel-module-cheat#fops */
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
@@ -10,7 +10,15 @@
#include <uapi/linux/stat.h> /* S_IRUSR */
static struct dentry *debugfs_file;
// The buffer can be stored in two ways: static module data or kmalloc.
#define STATIC 1
#if STATIC
static char data[] = {'a', 'b', 'c', 'd'};
#define BUFLEN sizeof(data)
#else
static char *data;
#define BUFLEN 4
#endif
static int open(struct inode *inode, struct file *filp)
{
@@ -19,7 +27,7 @@ static int open(struct inode *inode, struct file *filp)
}
/* @param[in,out] off: gives the initial position into the buffer.
* We must increment this by the ammount of bytes read.
* We must increment this by the amount of bytes read.
* Then when userland reads the same file descriptor again,
* we start from that point instead.
*/
@@ -27,21 +35,18 @@ static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off
{
ssize_t ret;
pr_info("read\n");
pr_info("len = %zu\n", len);
pr_info("off = %lld\n", (long long)*off);
if (sizeof(data) <= *off) {
pr_info("read len=%zu off=%lld\n", len, (long long)*off);
if (BUFLEN <= *off) {
ret = 0;
} else {
ret = min(len, sizeof(data) - (size_t)*off);
ret = min(len, BUFLEN - (size_t)*off);
if (copy_to_user(buf, data + *off, ret)) {
ret = -EFAULT;
} else {
*off += ret;
}
}
pr_info("buf = %.*s\n", (int)len, buf);
pr_info("ret = %lld\n", (long long)ret);
pr_info("ret=%lld\n", (long long)ret);
return ret;
}
@@ -54,13 +59,11 @@ static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff
{
ssize_t ret;
pr_info("write\n");
pr_info("len = %zu\n", len);
pr_info("off = %lld\n", (long long)*off);
if (sizeof(data) <= *off) {
pr_info("write len=%zu off=%lld\n", len, (long long)*off);
if (BUFLEN <= *off) {
ret = 0;
} else {
if (sizeof(data) - (size_t)*off < len) {
if (BUFLEN - (size_t)*off < len) {
ret = -ENOSPC;
} else {
if (copy_from_user(data + *off, buf, len)) {
@@ -89,9 +92,7 @@ static loff_t llseek(struct file *filp, loff_t off, int whence)
{
loff_t newpos;
pr_info("llseek\n");
pr_info("off = %lld\n", (long long)off);
pr_info("whence = %lld\n", (long long)whence);
pr_info("llseek off=%lld whence=%lld\n", (long long)off, (long long)whence);
switch(whence) {
case SEEK_SET:
newpos = off;
@@ -100,7 +101,7 @@ static loff_t llseek(struct file *filp, loff_t off, int whence)
newpos = filp->f_pos + off;
break;
case SEEK_END:
newpos = sizeof(data) + off;
newpos = BUFLEN + off;
break;
default:
return -EINVAL;
@@ -124,12 +125,24 @@ static const struct file_operations fops = {
static int myinit(void)
{
#if STATIC == 0
data = kmalloc(BUFLEN, GFP_KERNEL);
if (!data)
return -ENOMEM;
data[0] = 'a';
data[1] = 'b';
data[2] = 'c';
data[3] = 'd';
#endif
debugfs_file = debugfs_create_file("lkmc_fops", S_IRUSR | S_IWUSR, NULL, NULL, &fops);
return 0;
}
static void myexit(void)
{
#if STATIC == 0
kfree(data);
#endif
debugfs_remove_recursive(debugfs_file);
}

View File

@@ -1,7 +0,0 @@
{
"folders": [
{
"path": "."
}
],
}

189
kernel_modules/memfile.c Normal file
View File

@@ -0,0 +1,189 @@
/* https://cirosantilli.com/linux-kernel-module-cheat#fops */
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h> /* file_operations */
#include <linux/kernel.h> /* min */
#include <linux/module.h>
#include <linux/printk.h> /* printk */
#include <linux/string.h> /* strcpy */
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/rwsem.h>
#include <uapi/linux/stat.h> /* S_IRUSR */
/* Params */
static int log = 0;
module_param(log, int, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(log, "enable logging");
/* Dynamic array: https://stackoverflow.com/questions/3536153/c-dynamically-growing-array */
typedef struct {
char *buf;
size_t used;
size_t _size;
} dyn_arr_t;
int dyn_arr_init(dyn_arr_t *a, size_t size);
int dyn_arr_init(dyn_arr_t *a, size_t size)
{
a->buf = kvzalloc(size, GFP_KERNEL);
if (!a->buf)
return -ENOMEM;
a->used = 0;
a->_size = size;
return 0;
}
/* Reserve the required space for a future data insertion of size len at offset off.
* We don't do the actual insertion here as there are multiple possible insertion methods
* e.g. copy_from_user or strcpy.
*/
int dyn_arr_reserve(dyn_arr_t *a, size_t off, size_t len);
int dyn_arr_reserve(dyn_arr_t *a, size_t off, size_t len)
{
size_t new_used, new_size;
new_used = off + len;
if (new_used > a->_size) {
new_size = new_used * 2;
a->buf = kvrealloc(a->buf, a->_size, new_size, GFP_KERNEL);
if (!a->buf)
return -ENOMEM;
a->_size = new_size;
}
if (off > a->used)
memset(a->buf + a->used, '\0', off - a->used);
if (new_used > a->used)
a->used = new_used;
if (log) pr_info("dyn_arr_reserve _size:=%zu used:=%zu\n", a->_size, a->used);
return 0;
}
void dyn_arr_free(dyn_arr_t *a);
void dyn_arr_free(dyn_arr_t *a)
{
kvfree(a->buf);
a->buf = NULL;
a->used = 0;
a->_size = 0;
}
/* Globals. */
static dyn_arr_t data;
static struct dentry *debugfs_file;
struct rw_semaphore rwsem;
static int open(struct inode *inode, struct file *filp)
{
if (log) pr_info("open\n");
if ((filp->f_flags & O_TRUNC)) {
if (log) pr_info("open O_TRUNC\n");
data.used = 0;
} else if ((filp->f_flags & O_APPEND)) {
if (log) pr_info("open O_APPEND\n");
filp->f_pos = data.used;
}
return 0;
}
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
if (log) pr_info("read len=%zu off=%lld\n", len, (long long)*off);
down_read(&rwsem);
if (data.used <= *off) {
ret = 0;
} else {
ret = min(len, data.used - (size_t)*off);
if (copy_to_user(buf, data.buf + *off, ret)) {
ret = -EFAULT;
} else {
*off += ret;
}
}
up_read(&rwsem);
if (log) pr_info("read ret:=%lld\n", (long long)ret);
return ret;
}
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
if (log) pr_info("write len=%zu off=%lld\n", len, (long long)*off);
down_write(&rwsem);
dyn_arr_reserve(&data, *off, len);
if (copy_from_user(data.buf + *off, buf, len)) {
ret = -EFAULT;
} else {
ret = len;
*off += ret;
}
up_write(&rwsem);
if (log) pr_info("write ret:=%lld\n", (long long)ret);
return ret;
}
static int release(struct inode *inode, struct file *filp)
{
if (log) pr_info("release\n");
return 0;
}
static loff_t llseek(struct file *filp, loff_t off, int whence)
{
loff_t newpos;
if (log) pr_info("llseek off=%lld whence=%lld\n", (long long)off, (long long)whence);
switch(whence) {
case SEEK_SET:
newpos = off;
break;
case SEEK_CUR:
newpos = filp->f_pos + off;
break;
case SEEK_END:
newpos = data.used + off;
break;
default:
return -EINVAL;
}
if (newpos < 0) return -EINVAL;
filp->f_pos = newpos;
if (log) pr_info("llseek newpos:=%lld\n", (long long)newpos);
return newpos;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.llseek = llseek,
.open = open,
.read = read,
.release = release,
.write = write,
};
static int myinit(void)
{
int ret;
ret = dyn_arr_init(&data, 1);
if (ret)
return ret;
init_rwsem(&rwsem);
debugfs_file = debugfs_create_file("lkmc_memfile",
S_IRUSR | S_IWUSR, NULL, NULL, &fops);
return 0;
}
static void myexit(void)
{
dyn_arr_free(&data);
debugfs_remove_recursive(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

1617
kernel_modules/scull.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -17,7 +17,7 @@ static struct dentry *debugfs_file;
/* Called at the beginning of every read.
*
* The return value is passsed to the first show.
* The return value is passed to the first show.
* It normally represents the current position of the iterator.
* It could be any struct, but we use just a single integer here.
*
@@ -28,7 +28,7 @@ static void *start(struct seq_file *s, loff_t *pos)
{
loff_t *spos;
pr_info("start pos = %llx\n", (unsigned long long)*pos);
pr_info("start pos=%llx\n", (unsigned long long)*pos);
spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
if (!spos || *pos >= max)
return NULL;
@@ -46,7 +46,7 @@ static void *next(struct seq_file *s, void *v, loff_t *pos)
loff_t *spos;
spos = v;
pr_info("next pos = %llx\n", (unsigned long long)*pos);
pr_info("next pos=%llx\n", (unsigned long long)*pos);
if (*pos >= max)
return NULL;
*pos = ++*spos;
@@ -66,7 +66,7 @@ static int show(struct seq_file *s, void *v)
loff_t *spos;
spos = v;
pr_info("show pos = %llx\n", (unsigned long long)*spos);
pr_info("show pos=%llx\n", (unsigned long long)*spos);
seq_printf(s, "%llx\n", (long long unsigned)*spos);
return 0;
}