02 | Exchange Data with the Kernel Through /dev

Create a new directory for these two files. The module creates /dev/demo-char, through which userspace can read and replace a message stored in the kernel.

Makefile

obj-m := demo_char.o

demo_char.c

#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/string.h>
#include <linux/uaccess.h>

#define DEMO_CAPACITY 128

static DEFINE_MUTEX(message_lock);
static char message[DEMO_CAPACITY] = "Hello from the kernel!\n";
static size_t message_len = sizeof("Hello from the kernel!\n") - 1;

static ssize_t demo_read(struct file *file, char __user *buf,
                         size_t count, loff_t *pos)
{
    ssize_t result;

    mutex_lock(&message_lock);
    result = simple_read_from_buffer(buf, count, pos, message, message_len);
    mutex_unlock(&message_lock);

    return result;
}

static ssize_t demo_write(struct file *file, const char __user *buf,
                          size_t count, loff_t *pos)
{
    char incoming[DEMO_CAPACITY];

    if (count == 0)
        return 0;

    if (count > sizeof(incoming))
        return -EMSGSIZE;

    if (copy_from_user(incoming, buf, count))
        return -EFAULT;

    mutex_lock(&message_lock);
    memcpy(message, incoming, count);
    message_len = count;
    mutex_unlock(&message_lock);

    return count;
}

static const struct file_operations demo_fops = {
    .owner = THIS_MODULE,
    .open  = nonseekable_open,
    .read  = demo_read,
    .write = demo_write,
};

static struct miscdevice demo_device = {
    .minor = MISC_DYNAMIC_MINOR,
    .name  = "demo-char",
    .fops  = &demo_fops,
};

static int __init demo_init(void)
{
    return misc_register(&demo_device);
}

static void __exit demo_exit(void)
{
    misc_deregister(&demo_device);
}

module_init(demo_init);
module_exit(demo_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Character device lab");

Build and verify

Run the following in the QEMU guest.

make -C "/lib/modules/$(uname -r)/build" M="$PWD" modules

sudo insmod ./demo_char.ko
ls -l /dev/demo-char
sudo cat /dev/demo-char

printf 'Message from userspace\n' | sudo tee /dev/demo-char >/dev/null
sudo cat /dev/demo-char
sudo dd if=/dev/demo-char bs=4 status=none

sudo rmmod demo_char

The first cat should print Hello from the kernel!. The second should print Message from userspace. The dd command should print the whole line in four-byte reads. The device node should disappear after unloading.

How the driver uses familiar C concepts

Code Purpose
.read = demo_read Stores the callback Linux invokes on a read request
copy_from_user() Copies userspace bytes into a temporary kernel buffer
mutex_lock() Protects the shared message and its length
*pos Tracks the offset so successive reads continue from the previous position

Each successful write replaces the whole message, up to 128 bytes. Once a reader reaches the end, its next read returns zero. This signals EOF to cat.

Exercise

Write a function with the same signature as demo_read() that always returns zero. Point .read at it, rebuild, and reload. Verify that cat exits immediately without printing a message.