Note that there are some explanatory texts on larger screens.

plurals
  1. POrace condition between wait_event and wake_up
    text
    copied!<p>I have a driver that wants to send notification to the user about a status change. In the current implementation it uses the proc filesystem to do so. The read process loops around a <code>read()</code> to the proc filesystem. The <code>read()</code> blocks with <code>wait_event_interruptible()</code> until the kernel gets an interrupt which causes the <code>write_new_data()</code> function to <code>call wake_up_interruptible()</code>. Here's the basic code (removed all unneeded clutter):</p> <pre><code>static int flag=0; DECLARE_WAIT_QUEUE_HEAD(info_wq); //user process call read() on /proc/myfile to get to this function int my_proc_read (struct file *filp, char *buf, size_t count, loff_t *pos) { wait_event_interruptible(info_wq, flag != 0); flag = 0; //copy buffers to user return 0; } //when an interrupt comes it schedules this function on the systems' work queue void write_new_data () { //fill buffer with data flag = 1; wake_up_interruptible(&amp;info_wq); } </code></pre> <p>Now consider the following flow:</p> <ol> <li>User process calls <code>read()</code>, then waits.</li> <li>interrupt occurs -> <code>write_new_data()</code> is called. writes data and calls <code>wake_up_interruptible()</code>.</li> <li><code>read()</code> is awaken, reads data but process has not rerun read (wasn't scheduled to run, didn't get to it because of the next interrupt...).</li> <li>interrupt occurs -> <code>write_new_data()</code> is triggered again, calls <code>wake_up_interruptible()</code> but no waiting thread is waiting...</li> <li>process calls read and blocks.</li> </ol> <p>Note: this all happens on a uni-processor system. Also there is only one thread reading and one thread writing new data.</p> <p>How can I avoid missing the second interrupt? (One solution is to use netlink sockets but I was wondering if there is a way to do it in /proc land)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload