Skip to contentSkip to conversation

Behavior of poll() within a module

DISCUSSION Manfred Spraul 2 participants 4 messages Oct 23, 2001

lore
Share & tools
lore
#0 lore raw
Quoted 4 lines

The following actual module code:

static unsigned int vxi_poll(struct file *fp, struct poll_table_struct *wait)

Quoted 14 lines
{
    unsigned long flags;
    unsigned int mask;
    DEB(printk("vxi_poll\n"));
    info->poll_active++;
    poll_wait(fp, &info->wait, wait);
    spin_lock_irqsave(&vxi_lock, flags);
    mask = info->poll_mask;
    if(!--info->poll_active)
        info->poll_mask = 0;
    spin_unlock_irqrestore(&vxi_lock, flags);
    DEB(printk("vxi_poll returns\n"));
    return mask;
}

Which module is that? I can't find it in Linus tree. Is "info" a global variable?

* poll is called without any SMP locking, "info->poll_active++" is not SMP safe. Use atomic_inc, or even better just delete that line.
* Clearing poll_mask during poll is wrong.
poll should return the events that are currently available, i.e. what would happen if read() or write() would be called now.

read() on a non-blocking file handle would return immediately with 1 or more bytes read --> set POLLIN write() on a non-blocking file handle would return immediately with a nonzero byte count written--> set POLLOUT. The clearing of poll_mask must occur during read() and write() if these conditions are not true anymore.

--
Manfred

#1 lore raw
Quoted 18 lines · Manfred Spraul
Quoted 4 lines

The following actual module code:

static unsigned int vxi_poll(struct file *fp, struct poll_table_struct *wait)

Quoted 14 lines
{
    unsigned long flags;
    unsigned int mask;
    DEB(printk("vxi_poll\n"));
    info->poll_active++;
    poll_wait(fp, &info->wait, wait);
    spin_lock_irqsave(&vxi_lock, flags);
    mask = info->poll_mask;
    if(!--info->poll_active)
        info->poll_mask = 0;
    spin_unlock_irqrestore(&vxi_lock, flags);
    DEB(printk("vxi_poll returns\n"));
    return mask;
}
Quoted 2 lines

Which module is that? I can't find it in Linus tree. Is "info" a global variable?

info is a global, a structure in allocated memory.

Quoted 2 lines

* poll is called without any SMP locking, "info->poll_active++" is

not SMP safe. Use atomic_inc, or even better just delete that

Quoted 6 lines

line.
* Clearing poll_mask during poll is wrong.

poll should return the events that are currently available, i.e. what would happen if read() or write() would be called now.

In this module, there isn't any read() or write() event that can clear the poll mask. Instead, the sole purpose of poll is to tell the user-mode caller that there is new status available as a result of an interrupt. This is a module that controls a motor. It runs <forever> on its own. It gets new parameters via an ioctl(). It reports exceptions (like overload conditions) using the poll mechanism.

The caller reads the cached status via an ioctl(). Any caller sleeping in poll must be awakened as a result of the interrupt event. Any caller can read the cached status at any time. If this was allowed to clear the poll mask, only one caller would be awakened.

If I don't clear the poll mask when the last caller has been awakened, then any subsequent call to poll results in an immediate bogus return.

Although the code may need to be changed, the logic is correct and it must work as specified. The problem is that sometimes it works and sometimes it doesn't. In particular, if one user is sleeping in poll() it works. If two users are sleeping in poll() sometimes one of the callers doesn't get awakened. If three users are sleeping in poll(), all bets are off and I get strange return values, not related to the poll mask. It four users are sleeping in poll(), they all return immediately <forever>. Going to a single poll-caller doesn't fix the problem, some history is left in the kernel. I have to remove the module, then insert the module to "fix" the problem.

Here is a little test program that shows the same problem when executed using /dev/console, (or any other device you chose) etc., for input multiple times..

/*
* This tests select() in various modules.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <errno.h>
#include <string.h>

int main(int args, char *argv[])

{
    int fd, retval;
    fd_set rfds;
    char buf[0x1000];

    if(args < 2)
    {
        fprintf(stderr,"Usage:\n%s device\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if((fd = open(argv[1], O_RDWR)) < 0)
    {
        fprintf(stderr, "Can't open device, %s\n", argv[1]);
        exit(EXIT_FAILURE);
    }
    for(;;)
    {
        FD_ZERO(&rfds);
        FD_SET(fd, &rfds);
        if((retval = select(fd+1, &rfds, NULL, NULL, NULL)) < 0) 
            fprintf(stderr, "Error was %s\n", strerror(errno));
        else
        {
//            printf("Return = %d\n", retval);
            if(FD_ISSET(fd, &rfds))
            {
                  fprintf(stderr,".");
//                  (void)read(fd, buf, sizeof(buf));
            }
//                printf("Input is available\n");
        }
    }
    if(close(fd) < 0)
    {
        fprintf(stderr, "Can't close device, %s\n", argv[1]);
        exit(EXIT_FAILURE);
    }
    return 0;
}

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be attributed to Microsoft. Once I realized that Ctrl-Alt-Del was handled in the BIOS, I found that there aren't any.

#2 lore raw

From: "Richard B. Johnson" <[email protected]>

Quoted 9 lines

In this module, there isn't any read() or write() event that can clear the poll mask. Instead, the sole purpose of poll is to tell the user-mode caller that there is new status available as a result of an interrupt. This is a module that controls a motor. It runs <forever> on its own. It gets new parameters via an ioctl(). It reports exceptions (like overload conditions) using the poll mechanism.

Quoted 5 lines

The caller reads the cached status via an ioctl(). Any caller sleeping in poll must be awakened as a result of the interrupt event. Any caller can read the cached status at any time. If this was allowed to clear the poll mask, only one caller would be awakened.

Ugh.
->poll must never change any state. The kernel is free to call poll multiple times (common are once, twice and three times).

Can you use a per-filp pollmask?
* remove poll_active
* remove poll_mask
* add event counters for every possible event.
poll_count_POLLIN, poll_count_POLLOUT, etc.
* interrupt handler increases the counter.
* ioctl() copies the value of the counters into

    filp->private_data->event_handled_POLL{IN,OUT}
* poll sets the pollmask if
    filp->private_data->event_seen != info->poll_count

If filps (file descriptors) are shared between apps, then I have no idea how to fix your design.

--
Manfred

#3 lore raw
Quoted 10 lines · Manfred Spraul

From: "Richard B. Johnson" <[email protected]>

Quoted 9 lines

In this module, there isn't any read() or write() event that can clear the poll mask. Instead, the sole purpose of poll is to tell the user-mode caller that there is new status available as a result of an interrupt. This is a module that controls a motor. It runs <forever> on its own. It gets new parameters via an ioctl(). It reports exceptions (like overload conditions) using the poll mechanism.

Quoted 8 lines
Quoted 5 lines

The caller reads the cached status via an ioctl(). Any caller sleeping in poll must be awakened as a result of the interrupt event. Any caller can read the cached status at any time. If this was allowed to clear the poll mask, only one caller would be awakened.

Ugh.
->poll must never change any state. The kernel is free to call poll multiple times (common are once, twice and three times).

That's the problem. It calls poll even when there is not any user-mode select/poll active.

Quoted 2 lines

Can you use a per-filp pollmask?

Yes. You've got it. Private data is a place to put the per-process poll mask.

Quoted 2 lines

* remove poll_active
* remove poll_mask

Yes.

Quoted 8 lines

* add event counters for every possible event.
poll_count_POLLIN, poll_count_POLLOUT, etc.
* interrupt handler increases the counter.
* ioctl() copies the value of the counters into

    filp->private_data->event_handled_POLL{IN,OUT}
* poll sets the pollmask if
    filp->private_data->event_seen != info->poll_count

Yes. May not actually need counters because each interrupt will cache a status that can be read by anybody.

The struct file_pointer.private_data is the savior.

Quoted 3 lines

If filps (file descriptors) are shared between apps, then I have no idea how to fix your design.

Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be attributed to Microsoft. Once I realized that Ctrl-Alt-Del was handled in the BIOS, I found that there aren't any.