为什么对sysfs设备属性文件的`poll`调用不能正确阻塞?

11

我有一个简单的sysfs设备属性,显示在我的sysfs目录下,在调用read时返回内核空间变量的值。我想在此属性上调用poll,以允许我的用户空间线程阻塞,直到属性所显示的值发生更改。

问题在于,poll似乎无法在我的属性上阻塞--即使属性所显示的值没有更改,它仍然返回POLLPRI。事实上,在内核模块中根本没有任何对sysfs_notify的调用,但是用户空间的poll调用仍然不会阻塞。

也许我应该检查除POLLPRI之外的其他返回值--但是根据Linux内核文档sysfs_poll应该返回POLLERR|POLLPRI

/* ... When the content changes (assuming the
 * manager for the kobject supports notification), poll will
 * return POLLERR|POLLPRI ...
 */

我是否忘记了与 poll 相关的某些操作?


  1. 设备属性位于:/sys/class/vilhelm/foo/blah

  2. 我加载一个名为foo的内核模块,它注册了一个设备,并创建了一个类和这个设备属性。

  3. 名为bar的用户空间应用程序生成一个线程,调用poll检查设备属性,以检查POLLPRI

    • 如果poll返回正数,则已返回POLLPRI
    • 使用fopenfscan从设备属性文件中读取值。
    • 如果该值为42,则打印FROM THREAD!!!

问题在于当我期望调用poll无限期地阻塞时,消息会不停地打印。问题必须出在poll上(其他调用成功地从设备属性获取了正确的42值)。


用户空间应用程序 - bar.c:

#include <stdio.h>

#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <unistd.h>

static void handle_val(unsigned val, FILE *fp);
void * start_val_service(void *arg);

int main(void){
        pthread_t val_serv;
        pthread_create(&val_serv, NULL, &start_val_service, NULL);

        pthread_exit(NULL);
        return 0;

}

static void handle_val(unsigned val, FILE *fp){
        switch(val){
                case 42:
                {
                        printf("FROM THREAD!!!\n");
                        break;
                }

                default:
                        break;
        }
}


void * start_val_service(void *arg){
        struct pollfd fds;
        fds.fd = open("/sys/class/vilhelm/foo/blah", O_RDONLY);
        fds.events = POLLPRI;

        do{
                int ret = poll(&fds, 1, -1);
                if(ret > 0){
                        FILE *fp = fopen("/sys/class/vilhelm/foo/blah", "r");

                        unsigned val;
                        fscanf(fp, "%u", &val);
                        
                        handle_val(val, fp);

                        fclose(fp);
                }
        }while(1);

        close(fds.fd);

        pthread_exit(NULL);
}

内核模块 - foo.c:

#include <linux/device.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kernel.h>

static dev_t foo_dev;
static struct class *vilhelm;

static unsigned myvar = 42;

static ssize_t unsigned_dev_attr_show(struct device *dev, struct device_attribute *attr, char *buf);

struct unsigned_device_attribute{
        struct device_attribute dev_attr;
        unsigned *ptr;
};

static struct unsigned_device_attribute unsigned_dev_attr_blah = {
        .dev_attr = __ATTR(blah, S_IRUGO, unsigned_dev_attr_show, NULL)
};

static int __init foo_init(void){
        int retval = 0;

        printk(KERN_INFO "HELLO FROM MODULE 1");
        
        if(alloc_chrdev_region(&foo_dev, 0, 1, "vilhelm") < 0){
                printk(KERN_ERR "foo: unable to register device");
                retval = -1;
                goto out_alloc_chrdev_region;
        }
        
        vilhelm = class_create(THIS_MODULE, "vilhelm");
        if(IS_ERR(vilhelm)){
                printk(KERN_ERR "foo: unable to create device class");
                retval = PTR_ERR(vilhelm);
                goto out_class_create;
        }
        
        struct device *foo_device = device_create(vilhelm, NULL, foo_dev, NULL, "foo");
        if(IS_ERR(foo_device)){
                printk(KERN_ERR "foo: unable to create device file");
                retval = PTR_ERR(foo_device);
                goto out_device_create;
        }

        unsigned_dev_attr_blah.ptr = &myvar;
        retval = device_create_file(foo_device, &unsigned_dev_attr_blah.dev_attr);
        if(retval){
                printk(KERN_ERR "foo: unable to create device attribute files");
                goto out_create_foo_dev_attr_files;
        }

        return 0;

        out_create_foo_dev_attr_files:
                device_destroy(vilhelm, foo_dev);
        out_device_create:
                class_destroy(vilhelm);
        out_class_create:
                unregister_chrdev_region(foo_dev, 1);
        out_alloc_chrdev_region:
                return retval;
}

static void __exit foo_exit(void){
        printk(KERN_INFO "BYE FROM MODULE 1");

        device_destroy(vilhelm, foo_dev);
        class_destroy(vilhelm);
        unregister_chrdev_region(foo_dev, 1);
}

static ssize_t unsigned_dev_attr_show(struct device *dev, struct device_attribute *attr, char *buf){
        struct unsigned_device_attribute *tmp = container_of(attr, struct unsigned_device_attribute, dev_attr);

        unsigned value = *(tmp->ptr);

        return scnprintf(buf, PAGE_SIZE, "%u\n", value);
}

module_init(foo_init);
module_exit(foo_exit);

MODULE_LICENSE("GPL");

参见

使用Linux的sysfs_notify调用

1个回答

10

引用你所引用的评论中的一些内容:

一旦轮询/选择指示值已更改,您需要关闭和重新打开文件,或者搜索到0并再次读取。

但您没有对 fds.fd 进行任何操作。

此外,在调用 poll() 之前进行虚拟 read();任何新打开的文件都被视为已更改。


我在fopen行上面添加了close(fds.fd),并在fclose调用后再次调用了open,但没有任何变化。使用lseek(fds.fd, 0, SEEK_SET)也没有产生结果。 - Vilhelm Gray
1
在调用“poll”之前,您必须从轮询的fd中读取当前值。 - CL.
2
虚拟读取是解决问题的关键。我创建了一个名为dummybufchar变量,并在open之后以及fclose之后立即插入了以下行:read(fds.fd, &dummybuf, 1); - Vilhelm Gray
我有一个后续问题:当打开文件时返回POLLPRI是否正常(因此需要一个虚拟的read)? - Vilhelm Gray
1
对于 sysfs 文件,这是正常的。(POLLIN 表示文件可读,这总是成立的。) - CL.
虚拟读取是因为新打开的文件也被认为是已更改的,这也是我需要轮询“cgroups.events”文件的(未记录的!)缺失的部分。 - mtraceur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接