Linux kernel module · out-of-tree · GPL-2.0

chardev/

A character device that works as a message queue. Each write() stores one message in a kernel buffer, each read() returns the oldest one whole, and a single mutex keeps concurrent callers from corrupting it.

$ ls -l /dev/chardev crw------- 1 root root 510, 0 Oct 25 17:13 /dev/chardev # major 510 is an example; it's assigned at load
Target Linux 7.0 Buffer 4096 B kmalloc Max message 256 B Build 0 warnings at W=1 checkpatch 0 errors, 0 warnings
Try it

Talk to the device

This runs in your browser. It follows the same rules as kernel/chardev.c: the same limits, the same error codes and the same log messages.

User space fd 3 → /dev/chardev, O_RDWR
20 / 256 bytes
bytes
Make it fail
System calls, as strace prints them

        
Kernel space msg_buf, 64 × 64 bytes
2-byte length header message payload free
Hover over or tap a byte to inspect it.
msg_count0
buf_used0 / 4096
open_count0
bytes_written0
Queue, oldest first
dmesg

        
Concurrency

What the mutex prevents

Three threads call write() at the same moment. Step through it with the lock, then without it, and watch the first 48 bytes of the buffer.

buf_used 0 msg_count 0 underline = inside buf_used
Step 0

Reference

Interface

System calls

CallWhenReturns
write1–256 bytes, room in buffern
write0 bytesEINVAL
writemore than 256 bytesEMSGSIZE
writebuffer fullENOSPC
readmessage queued, buffer big enoughlength
readqueue empty0 (EOF)
readbuffer smaller than message (message stays queued)EMSGSIZE
read / writebad user pointer (queue unchanged)EFAULT

ioctl commands

CommandEncodingValue
CHARDEV_IOC_GET_COUNT_IOR('k', 1, __u32)0x80046b01
CHARDEV_IOC_CLEAR_IO('k', 2)0x00006b02
anything elseENOTTY

The value packs direction (read = 2, top bits), argument size (4), the magic byte 'k' (0x6b) and the command number.

Where the code runs

User space
chardev_testcat /proc/chardev_stats
system call · copy_to_user / copy_from_user
Kernel space
VFS→ chardev_fops→ chardev_lock→ msg_buf
proc_ops→ seq_file show()→ chardev_lock

What happens at insmod

  1. kmalloc the 4096-byte buffer
  2. alloc_chrdev_region picks a free major number
  3. cdev_add connects that number to the file operations
  4. class_create + device_create; udev creates /dev/chardev
  5. proc_create adds /proc/chardev_stats

rmmod undoes these in reverse. If a step fails during load, only the steps before it are undone.

Verification

What the test suite checks

user/chardev_test.c prints PASS or FAIL for each of these, then scripts/run_tests.sh runs it again under valgrind and prints the kernel log.

Write then read back in orderThree messages come back byte for byte, oldest first.
Read on empty queueReturns 0, so cat stops cleanly.
ioctl GET_COUNTFive writes report a count of five.
ioctl CLEARCount drops to zero and nothing can be read.
Invalid sizes and pointersEINVAL, EMSGSIZE and EFAULT, and a message survives a too-small read.
Bad ioctl argumentsENOTTY for an unknown command, EFAULT for a NULL argument.
Buffer fullENOSPC after 15 messages of 256 bytes.
Concurrent readers and writers4 + 4 threads move 8,000 messages with none torn, duplicated or lost.
/proc stats matchopen_count and bytes_written grow by exactly what the test did.
CloseEvery close() return value is checked.
# build, load, test, valgrind, dmesg, unload
sudo apt install build-essential linux-headers-$(uname -r) valgrind
sudo scripts/run_tests.sh