Providing complete blocking FIFO driver code
The plan is fully developed for a Linux char device driver implementing a virtual FIFO with proper locking, wait queues, and handling signals gracefully by propagating -ERESTARTSYS on interrupted waits; next I'll deliver the entire driver code as a standalone kernel module.
Below is a minimal example of a single–minor character driver that implements a virtual FIFO in kernel memory. Readers block in wait_event_interruptible() when the FIFO is empty; writers wake them up when they add data. If a reader is killed by a signal while asleep it will return -ERESTARTSYS/-EINTR cleanly without holding any locks.
// vfifo.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#include <linux/mutex.h>
#include <linux/sched/signal.h>
#define DEVICE_NAME "vfifo"
#define FIFO_SIZE 4096
struct vfifo_dev {
char *buf;
size_t size;
size_t head;
size_t tail;
size_t count;
struct mutex lock;
wait_queue_head_t readq;
struct cdev cdev;
};
static dev_t vfifo_devno;
static struct vfifo_dev *vfifo;
/* Helper to check if FIFO is empty without taking the mutex.
* We only use this in wait condition; final checks are done under lock. */
static inline bool vfifo_empty(struct vfifo_dev *d)
{
return d->count == 0;
}
static ssize_t vfifo_read(struct file *filp, char __user *ubuf,
size_t len, loff_t *ppos)
{
struct vfifo_dev *d = filp->private_data;
ssize_t ret;
size_t to_read;
/* Nothing to do for zero-length reads */
if (len == 0)
return 0;
/* Wait for data to become available */
for (;;) {
if (mutex_lock_interruptible(&d->lock))
return -ERESTARTSYS;
if (d->count > 0)
break; /* We have data */
mutex_unlock(&d->lock);
/* Sleep until someone writes or a signal arrives */
ret = wait_event_interruptible(d->readq, !vfifo_empty(d));
if (ret)
return ret; /* -ERESTARTSYS if interrupted */
}
/* Recompute to_read with lock held */
to_read = min(len, d->count);
/* Handle wrap-around */
if (d->head + to_read <= d->size) {
if (copy_to_user(ubuf, d->buf + d->head, to_read))
ret = -EFAULT;
else
ret = to_read;
d->head = (d->head + to_read) % d->size;
} else {
size_t first = d->size - d->head;
size_t second = to_read - first;
if (copy_to_user(ubuf, d->buf + d->head, first) ||
copy_to_user(ubuf + first, d->buf, second))
ret = -EFAULT;
else
ret = to_read;
d->head = second;
}
if (ret > 0)
d->count -= ret;
mutex_unlock(&d->lock);
return ret;
}
static ssize_t vfifo_write(struct file *filp, const char __user *ubuf,
size_t len, loff_t *ppos)
{
struct vfifo_dev *d = filp->private_data;
ssize_t ret;
size_t to_write;
bool was_empty;
if (len == 0)
return 0;
if (mutex_lock_interruptible(&d->lock))
return -ERESTARTSYS;
/* Clip write to available space */
to_write = min(len, d->size - d->count);
if (to_write == 0) {
/* FIFO full; simple implementation drops excess */
mutex_unlock(&d->lock);
return -ENOSPC;
}
was_empty = (d->count == 0);
/* Handle wrap-around */
if (d->tail + to_write <= d->size) {
if (copy_from_user(d->buf + d->tail, ubuf, to_write))
ret = -EFAULT;
else
ret = to_write;
d->tail = (d->tail + to_write) % d->size;
} else {
size_t first = d->size - d->tail;
size_t second = to_write - first;
if (copy_from_user(d->buf + d->tail, ubuf, first) ||
copy_from_user(d->buf, ubuf + first, second))
ret = -EFAULT;
else
ret = to_write;
d->tail = second;
}
if (ret > 0)
d->count += ret;
mutex_unlock(&d->lock);
/* Wake up any readers waiting for data */
if (ret > 0 && was_empty)
wake_up_interruptible(&d->readq);
return ret;
}
static int vfifo_open(struct inode *inode, struct file *filp)
{
filp->private_data = vfifo;
return 0;
}
static int vfifo_release(struct inode *inode, struct file *filp)
{
return 0;
}
static const struct file_operations vfifo_fops = {
.owner = THIS_MODULE,
.open = vfifo_open,
.release = vfifo_release,
.read = vfifo_read,
.write = vfifo_write,
};
static int __init vfifo_init(void)
{
int ret;
vfifo = kzalloc(sizeof(*vfifo), GFP_KERNEL);
if (!vfifo)
return -ENOMEM;
vfifo->size = FIFO_SIZE;
vfifo->buf = kzalloc(vfifo->size, GFP_KERNEL);
if (!vfifo->buf) {
ret = -ENOMEM;
goto err_free_dev;
}
mutex_init(&vfifo->lock);
init_waitqueue_head(&vfifo->readq);
ret = alloc_chrdev_region(&vfifo_devno, 0, 1, DEVICE_NAME);
if (ret)
goto err_free_buf;
cdev_init(&vfifo->cdev, &vfifo_fops);
vfifo->cdev.owner = THIS_MODULE;
ret = cdev_add(&vfifo->cdev, vfifo_devno, 1);
if (ret)
goto err_unregister;
pr_info(DEVICE_NAME ": registered at major %d minor %d\n",
MAJOR(vfifo_devno), MINOR(vfifo_devno));
return 0;
err_unregister:
unregister_chrdev_region(vfifo_devno, 1);
err_free_buf:
kfree(vfifo->buf);
err_free_dev:
kfree(vfifo);
return ret;
}
static void __exit vfifo_exit(void)
{
cdev_del(&vfifo->cdev);
unregister_chrdev_region(vfifo_devno, 1);
kfree(vfifo->buf);
kfree(vfifo);
pr_info(DEVICE_NAME ": unregistered\n");
}
module_init(vfifo_init);
module_exit(vfifo_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Virtual FIFO character device with blocking read");
MODULE_AUTHOR("Example");
The key parts are in vfifo_read(): if the FIFO is empty the code drops the mutex and calls wait_event_interruptible(&d->readq, !vfifo_empty(d));. If a signal such as SIGKILL is pending, wait_event_interruptible() will return -ERESTARTSYS; the driver propagates that error back to userspace without leaving the mutex held. Writers call wake_up_interruptible() when they add data so that sleeping readers are resumed.