Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

poll exiting immidiately from driver

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 ?

like image 351
AnotherDeveloper Avatar asked Mar 07 '26 20:03

AnotherDeveloper


1 Answers

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).

  1. If you do not call poll_wait then upper layer will just return mask without analyzing its value.
  2. If you add your driver/calling process to a wait queue using 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.

like image 98
John_Avi_09 Avatar answered Mar 10 '26 12:03

John_Avi_09



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!