Skip to contentSkip to conversation

poll() in 2.6 and beyond

DISCUSSION Richard B. Johnson 3 participants 11 messages Mar 2, 2004

lore
Share & tools
lore
#0 lore raw

Poll in 2.6.0; when a driver routine calls poll_wait() it returns <<immediately>> to somewhere in the kernel, then waits for my wake_up_interuptible(), before returning control to a user sleeping in poll(). This means that the user gets the wrong poll return value! It doesn't get the value it was given as a result of the interrupt, but the value that existed (0) before the interrupt occurred.

Poll should not return from poll_wait() until it gets a wake_up_interruptible() call. The wait variable, info->pwait, below, has been initialized by executing init_waitqueue_head(&info->pwait) in the initialization code. This code works in 2.4.24.

What do I do to make it work in 2.6.0 and beyond? There are no hints in the 2.6 drivers as they all seem to be written like this and they presumably work.

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/ /*
* The interrupt service routine.
*/
static void pci_isr(int irq, void *p, struct pt_regs *regs)

{
    spin_lock(&info->lock);
    DEB(printk(KERN_INFO"%s : Interrupt!\n", devname));
    info->poll_flag |= POLLIN|DEF_POLL;
    wake_up_interruptible(&info->pwait);
    spin_unlock(&info->lock);
}

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/ /*
* Device poll routine.
*/
static size_t poll(struct file *fp, struct poll_table_struct *wait)

{
    size_t poll_flag;
    size_t flags;
    DEB(printk(KERN_INFO"%s : poll called\n", devname));
    poll_wait(fp, &info->pwait, wait);
    lockit(TRUE, &flags);
    poll_flag = info->poll_flag;
    info->poll_flag = 0;
    lockit(FALSE, &flags);
    DEB(printk(KERN_INFO"%s : poll returns\n", devname));
    return poll_flag;
}

Cheers,
Dick Johnson
Penguin : Linux version 2.4.24 on an i686 machine (797.90 BogoMips).
Note 96.31% of all statistics are fiction.

#1 lore raw

Richard> Poll in 2.6.0; when a driver routine calls poll_wait() it Richard> returns <<immediately>> to somewhere in the kernel, then Richard> waits for my wake_up_interuptible(), before returning Richard> control to a user sleeping in poll(). This means that the Richard> user gets the wrong poll return value! It doesn't get the Richard> value it was given as a result of the interrupt, but the Richard> value that existed (0) before the interrupt occurred.

Nothing has changed in 2.6 that I know of. poll_wait() always returned immediately to the driver. The driver's poll method is supposed to call poll_wait() on the wait queues that indicate a change in poll status, and then return with the current status.

Read the description of "poll and select" in LDD:

    <http://www.xml.com/ldd/chapter/book/ch05.html#t3>

- Roland

#2 lore raw
Quoted 13 lines · Roland Dreier

Richard> Poll in 2.6.0; when a driver routine calls poll_wait() it Richard> returns <<immediately>> to somewhere in the kernel, then Richard> waits for my wake_up_interuptible(), before returning Richard> control to a user sleeping in poll(). This means that the Richard> user gets the wrong poll return value! It doesn't get the Richard> value it was given as a result of the interrupt, but the Richard> value that existed (0) before the interrupt occurred.

Nothing has changed in 2.6 that I know of. poll_wait() always returned immediately to the driver. The driver's poll method is supposed to call poll_wait() on the wait queues that indicate a change in poll status, and then return with the current status.

Quoted 4 lines

Read the description of "poll and select" in LDD:

    <http://www.xml.com/ldd/chapter/book/ch05.html#t3>

- Roland

I'm talking about the driver! When a open fd called poll() or select(), in user-mode code, the driver's poll() was called, and the driver's poll() would call poll_wait(). Poll_wait() used to NOT return until the driver executed wake_up_interruptible() on that wait-queue. When poll_wait() returned, the driver would return to the caller with the new poll- status.

Now, when the driver calls poll_wait(), it returns immediately to the driver. The driver then returns with the wrong poll status because nothing changed yet. This return, doesn't go back to the user-mode caller, but remains within the kernel until a wake_up_interruptible() has been received. Then it returns to the original caller, with the (wrong) status that existed before the wake_up_interruptible().

When the cycle repeats, the status from the previous event gets returned, so if the events are all the same (POLLIN), only the first one is wrong and nobody is the wiser. However, when there are multiple events, they appear to the user out-of-sequence and muck up user-mode code.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.24 on an i686 machine (797.90 BogoMips).
Note 96.31% of all statistics are fiction.

#3 lore raw

Richard> I'm talking about the driver! When a open fd called Richard> poll() or select(), in user-mode code, the driver's Richard> poll() was called, and the driver's poll() would call Richard> poll_wait(). Poll_wait() used to NOT return until the Richard> driver executed wake_up_interruptible() on that Richard> wait-queue. When poll_wait() returned, the driver would Richard> return to the caller with the new poll- status.

I don't think so. Even in kernel 2.4, poll_wait() just calls __pollwait(). I don't see anything in __pollwait() that sleeps. Think about it. How would the kernel handle userspace calling poll() with more than one file descriptor if each individual driver slept?

I'll repeat my earlier suggestion. Read the description of "poll and select" in LDD:

     <http://www.xml.com/ldd/chapter/book/ch05.html#t3>

If you refuse to understand the documented interface, I don't think anyone can help you.

- Roland

#4 lore raw
Quoted 8 lines · Roland Dreier

Richard> I'm talking about the driver! When a open fd called Richard> poll() or select(), in user-mode code, the driver's Richard> poll() was called, and the driver's poll() would call Richard> poll_wait(). Poll_wait() used to NOT return until the Richard> driver executed wake_up_interruptible() on that Richard> wait-queue. When poll_wait() returned, the driver would Richard> return to the caller with the new poll- status.

Quoted 5 lines

I don't think so. Even in kernel 2.4, poll_wait() just calls __pollwait(). I don't see anything in __pollwait() that sleeps. Think about it. How would the kernel handle userspace calling poll() with more than one file descriptor if each individual driver slept?

Well what to you think they do? Spin?

Quoted 6 lines

I'll repeat my earlier suggestion. Read the description of "poll and select" in LDD:

     <http://www.xml.com/ldd/chapter/book/ch05.html#t3>

If you refuse to understand the documented interface, I don't think anyone can help you.

I am quite familiar with the operation of poll(), thank you.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.24 on an i686 machine (797.90 BogoMips).
Note 96.31% of all statistics are fiction.

#5 lore raw

Roland> I don't think so. Even in kernel 2.4, poll_wait() just Roland> calls __pollwait(). I don't see anything in __pollwait() Roland> that sleeps. Think about it. How would the kernel handle Roland> userspace calling poll() with more than one file Roland> descriptor if each individual driver slept?

Richard> Well what to you think they do? Spin?

I don't know why I continue this, but.... can you point out the line in the kernel 2.4 source for __pollwait() where it sleeps?

Or think about it. Suppose a user called poll() with two fds, each of which belonged to a different driver. Suppose each driver slept in its poll method. If the first driver never became ready (and stayed asleep), how would poll() return to user space if the second driver became ready?

What actually happens is that each driver registers with the kernel the wait queues that it will wake up when it becomes ready. But the core kernel is responsible for sleeping, outside of the driver code.

Really, read:

    <http://www.xml.com/ldd/chapter/book/ch05.html#t3>

You might believe you're familiar with poll() but I think it would help to read what the Linux Device Drivers book has to say. It answers the exact question you're asking, and if you don't believe me you might believe the definitive published book.

- Roland

#6 lore raw
Quoted 11 lines · Roland Dreier

Roland> I don't think so. Even in kernel 2.4, poll_wait() just Roland> calls __pollwait(). I don't see anything in __pollwait() Roland> that sleeps. Think about it. How would the kernel handle Roland> userspace calling poll() with more than one file Roland> descriptor if each individual driver slept?

Richard> Well what to you think they do? Spin?

I don't know why I continue this, but.... can you point out the line in the kernel 2.4 source for __pollwait() where it sleeps?

You are playing games with semantics because you are wrong. The code in fs/select.c about line 101, adds the current caller to the wait-queue. This wait-queue is the mechanism by which the current caller sleeps, i.e., gives the CPU up to somebody else. That caller's thread will not return past that line until a wake_up_interruptible() call has been made for/from the driver or interface handling that file descriptor. In this manner any number of file discriptors may be handled because the poll() routine for each of then makes its own entry into the wait-queue using the described mechanism.

Quoted 6 lines

Or think about it. Suppose a user called poll() with two fds, each of which belonged to a different driver. Suppose each driver slept in its poll method. If the first driver never became ready (and stayed asleep), how would poll() return to user space if the second driver became ready?

Explained above.
[SNIPPED bullshit]

Cheers,
Dick Johnson
Penguin : Linux version 2.4.24 on an i686 machine (797.90 BogoMips).
Note 96.31% of all statistics are fiction.

#7 lore raw
Quoted 8 lines · Richard B. Johnson
Quoted 2 lines · Roland Dreier

I don't know why I continue this, but.... can you point out the line in the kernel 2.4 source for __pollwait() where it sleeps?

The code in fs/select.c about line 101, adds the current caller to the wait-queue. This wait-queue is the mechanism by which the current caller sleeps, i.e., gives the CPU up to somebody else.

Actually, no, it does not work that way. The wait queue is the mechanism by which a process can be *woken up* . The call to add_wait_queue() does not put the process to sleep.

The process is put to sleep in do_poll(), using schedule_timeout() with the current state as TASK_INTERRUPTIBLE. This is on line 453.

Your driver will eventually have to wake up sleepers on the queue. do_poll() will also wake up when the timeout expires, or a signal is sent to the poll()'ing process.

These semantics have not changed since 2.4. The implementation has changed a bit, but the driver interface has not changed. Read the documentation the Roland pointed you to.

Signature · 1 line
Dave Dillow <[email protected]>
#8 lore raw
In reply to Richard B. Johnson

Richard> You are playing games with semantics because you are Richard> wrong. The code in fs/select.c about line 101, adds the Richard> current caller to the wait-queue.

I assume you mean the call to add_wait_queue() there. That does not sleep. Look at the implementation. add_wait_queue() is defined in kernel/fork.c -- it just does some locking and calls __add_wait_queue(). __add_wait_queue() is really nothing more than a list_add(). There's nothing more to it and nothing that goes to sleep. Where do you think add_wait_queue() goes to sleep?

Richard> This wait-queue is the mechanism by which the current Richard> caller sleeps, i.e., gives the CPU up to somebody else. Richard> That caller's thread will not return past that line until Richard> a wake_up_interruptible() call has been made for/from the Richard> driver or interface handling that file descriptor. In Richard> this manner any number of file discriptors may be handled Richard> because the poll() routine for each of then makes its own Richard> entry into the wait-queue using the described mechanism.

But there's only one thread around: the user space process that called into the kernel via poll(). If the first driver goes to sleep, which thread do you think is going to wake up and call into the second driver?

- Roland

#9 lore raw
Quoted 20 lines · Roland Dreier

Richard> You are playing games with semantics because you are Richard> wrong. The code in fs/select.c about line 101, adds the Richard> current caller to the wait-queue.

I assume you mean the call to add_wait_queue() there. That does not sleep. Look at the implementation. add_wait_queue() is defined in kernel/fork.c -- it just does some locking and calls __add_wait_queue(). __add_wait_queue() is really nothing more than a list_add(). There's nothing more to it and nothing that goes to sleep. Where do you think add_wait_queue() goes to sleep?

Richard> This wait-queue is the mechanism by which the current Richard> caller sleeps, i.e., gives the CPU up to somebody else. Richard> That caller's thread will not return past that line until Richard> a wake_up_interruptible() call has been made for/from the Richard> driver or interface handling that file descriptor. In Richard> this manner any number of file discriptors may be handled Richard> because the poll() routine for each of then makes its own Richard> entry into the wait-queue using the described mechanism.

Quoted 5 lines

But there's only one thread around: the user space process that called into the kernel via poll(). If the first driver goes to sleep, which thread do you think is going to wake up and call into the second driver?

There are two routines where the CPU is actually given up, do_select() and do_poll(). Search for schedule_timeout(). Once the scheduler has the CPU, it's available for any of the other drivers. It's also available for the timer queues, other tasks, and the interrupts.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.24 on an i686 machine (797.90 BogoMips).
Note 96.31% of all statistics are fiction.

#10 lore raw

Richard> There are two routines where the CPU is actually given Richard> up, do_select() and do_poll(). Search for Richard> schedule_timeout(). Once the scheduler has the CPU, it's Richard> available for any of the other drivers. It's also Richard> available for the timer queues, other tasks, and the Richard> interrupts.

OK, fine, I can't argue with that. But it has nothing to do with the discussion at hand.

You still haven't said where poll_wait() sleeps in kernel 2.4. You claimed it was in add_wait_queue(), but add_wait_queue() doesn't sleep (it's just a list_add() guarded by a lock).

Also, why would another driver for the same poll() call run? There's only one thread around that cares about this call to poll() -- the userspace process that originally called poll(). If one driver sleeps, no other drivers will run until that driver wakes up.

- Roland