This is my first experience of implementing a driver in linux kernel & facing this problem.
I am trying to implement "poll()" in my character driver. I have called poll_wait() & passed a wait queue.
When the device file of this driver is opened from the user space program & called "poll" system call on this device file descriptor (fd), poll system call exits immediately even when there is no data to be send to the user space.
It does not wait for even timeout period.
I am unable to figure out what is causing this problem.
Can anyone suggest any solution ?
When someone calls poll() system call on device file then vfs layer handles it this way.
It calls your driver's poll handler (henceforth referred as my_poll).
poll_wait() then calling process will be added to wait queue.Do not get deceived by the API's name poll_wait(). It doesn't actually wait or sleeps. It merely adds your process to the list of waiting processes for receiving event notification.
After returning from poll_wait(), the entire behavior depends on the mask returned. Value of this mask is used by VFS layer. If the mask returned is zero, it'll put the calling process to sleep. Now the process is waiting for the event to happen.
When someone awakens this wait queue, all waiting processes gets notification. VFS layer again calls my_poll() & checks returned value of mask.
Above process continues until VFS layer receives a non-zero mask.
It means my_poll() will be called multiple times. So anyone who wants to implement poll/read in his device driver, should check whether device is actually ready for read/write operation before returning readiness mask.
Important thing to note here is that do not assume that poll_wait() will sleep until someone awakens that queue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With