Linux character driver: What if flush() data has nowhere to go?

This post was written by eli on June 7, 2011
Posted Under: Linux kernel

I know that most character drivers don’t implement the flush() method, and the more I deal with it, the more I understand why. But unfortunately, in certain modes of operation flushing is necessary, so I implemented it.

One of the problems with flush() is that it’s called when the file is closed automatically as the process exits. If the device can’t take the remaining data, flushing is stuck, and the process remains in the process table in interruptible sleep mode (which is how I chose to wait in the driver). But in reality, attempting to kill the process results in no response whatsoever, and there’s another interesting thing about this process: Its /proc/fd is empty! So it’s a kind-of zombie, only it isn’t marked as such. But in essence it’s a process waiting for someone to take some data from it, so it can rest in peace.

The only way out is a timeout. The waiting should be something like this:

if (wait_event_interruptible_timeout(dev->wait,
                                     HZ) == 0) { /* 1 second timeout */
 printk(KERN_WARNING "mydriver: Timed out while flushing. Output data was lost.\n");
 return -ETIMEDOUT;

(Relevant Linux kernel: vanilla 2.6.37)

Add a Comment

required, use real name
required, will not be published
optional, your blog address