[关闭]
@zifehng 2017-04-24T07:14:06.000000Z 字数 4067 阅读 2092

input子系统事件处理层(evdev)的环形缓冲区

linux input evdev circular_buffer


kernel version: linux-4.9.13

在事件处理层()中结构体evdev_client定义了一个环形缓冲区(circular buffer),其原理是用数组的方式实现了一个先进先出的循环队列(circular queue),用以缓存内核驱动上报给用户层的input_event事件。

  1. struct evdev_client {
  2. unsigned int head; // 头指针
  3. unsigned int tail; // 尾指针
  4. unsigned int packet_head; // 包指针
  5. spinlock_t buffer_lock;
  6. struct fasync_struct *fasync;
  7. struct evdev *evdev;
  8. struct list_head node;
  9. unsigned int clk_type;
  10. bool revoked;
  11. unsigned long *evmasks[EV_CNT];
  12. unsigned int bufsize; // 循环队列大小
  13. struct input_event buffer[]; // 循环队列数组
  14. };

evdev_client对象维护了三个偏移量:head、tail以及packet_head。head、tail作为循环队列的头尾指针记录入口与出口偏移,那么包指针packet_head有什么作用呢?

packet_head
内核驱动处理一次输入,可能上报一到多个input_event事件,为表示处理完成,会在上报这些input_event事件后再上报一次同步事件。头指针head以input_event事件为单位,记录缓冲区的入口偏移量,而包指针packet_head则以“数据包”(一到多个input_event事件)为单位,记录缓冲区的入口偏移量。


circular buffer

1. 环形缓冲区的工作机制

  1. head++;
  2. head &= bufsize - 1;
  1. tail++;
  2. tail &= bufsize - 1;
  1. head == tail
  1. packet_head == tail

“求余”和“求与”
为解决头尾指针的上溢和下溢现象,使队列的元素空间可重复使用,一般循环队列的出入队算法都采用“求余”操作:
    head = (head + 1) % bufsize; // 入队
    tail = (tail + 1) % bufsize; // 出队
为避免计算代价高昂的“求余”操作,使内核运作更高效,input子系统的环形缓冲区采用了“求与”算法,这要求bufsize必须为2的幂,在后文中可以看到bufsize的值实际上是为64或者8的n倍,符合“求与”运算的要求。

2. 环形缓冲区的构造以及初始化

用户层通过open()函数打开input设备节点时,调用过程如下:

open() -> sys_open() -> evdev_open()

在evdev_open()函数中完成了对evdev_client对象的构造以及初始化,每一个打开input设备节点的用户都在内核中维护了一个evdev_client对象,这些evdev_client对象通过evdev_attach_client()函数注册在evdev[1]对象的内核链表上。


evdev_client

接下来我们具体分析evdev_open()函数:

  1. static int evdev_open(struct inode *inode, struct file *file)
  2. {
  3. struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
  4. // 1.计算环形缓冲区大小bufsize以及evdev_client对象大小size
  5. unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
  6. unsigned int size = sizeof(struct evdev_client) +
  7. bufsize * sizeof(struct input_event);
  8. struct evdev_client *client;
  9. int error;
  10. // 2. 分配内核空间
  11. client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
  12. if (!client)
  13. client = vzalloc(size);
  14. if (!client)
  15. return -ENOMEM;
  16. client->bufsize = bufsize;
  17. spin_lock_init(&client->buffer_lock);
  18. client->evdev = evdev;
  19. // 3. 注册到内核链表
  20. evdev_attach_client(evdev, client);
  21. error = evdev_open_device(evdev);
  22. if (error)
  23. goto err_free_client;
  24. file->private_data = client;
  25. nonseekable_open(inode, file);
  26. return 0;
  27. err_free_client:
  28. evdev_detach_client(evdev, client);
  29. kvfree(client);
  30. return error;
  31. }

在evdev_open()函数中,我们看到了evdev_client对象从构造到注册到内核链表的过程,然而它是在哪里初始化的呢?其实kzalloc()函数在分配空间的同时就通过__GFP_ZERO标志做了初始化:

  1. static inline void *kzalloc(size_t size, gfp_t flags)
  2. {
  3. return kmalloc(size, flags | __GFP_ZERO);
  4. }

3. 生产者/消费者模型

内核驱动与用户程序就是典型的生产者/消费者模型,内核驱动产生input_event事件,然后通过input_event()函数写入环形缓冲区,用户程序通过read()函数从环形缓冲区中获取input_event事件。


producer-consumer

3.1 环形缓冲区的生产者

内核驱动作为生产者,通过input_event()上报input_event事件时,最终调用___pass_event()函数将事件写入环形缓冲区:

  1. static void __pass_event(struct evdev_client *client,
  2. const struct input_event *event)
  3. {
  4. // 将input_event事件存入缓冲区,队头head自增指向下一个元素空间
  5. client->buffer[client->head++] = *event;
  6. client->head &= client->bufsize - 1;
  7. // 当队头head与队尾tail相等时,说明缓冲区空间已满
  8. if (unlikely(client->head == client->tail)) {
  9. /*
  10. * This effectively "drops" all unconsumed events, leaving
  11. * EV_SYN/SYN_DROPPED plus the newest event in the queue.
  12. */
  13. client->tail = (client->head - 2) & (client->bufsize - 1);
  14. client->buffer[client->tail].time = event->time;
  15. client->buffer[client->tail].type = EV_SYN;
  16. client->buffer[client->tail].code = SYN_DROPPED;
  17. client->buffer[client->tail].value = 0;
  18. client->packet_head = client->tail;
  19. }
  20. // 当遇到EV_SYN/SYN_REPORT同步事件时,packet_head移动到队头head位置
  21. if (event->type == EV_SYN && event->code == SYN_REPORT) {
  22. client->packet_head = client->head;
  23. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  24. }
  25. }

3.2 环形缓冲区的消费者

用户程序作为消费者,通过read()函数读取input设备节点时,最终在内核调用evdev_fetch_next_event()函数从环形缓冲区中读取input_event事件:

  1. static int evdev_fetch_next_event(struct evdev_client *client,
  2. struct input_event *event)
  3. {
  4. int have_event;
  5. spin_lock_irq(&client->buffer_lock);
  6. // 判缓冲区中是否有input_event事件
  7. have_event = client->packet_head != client->tail;
  8. if (have_event) {
  9. // 从缓冲区中读取一次input_event事件,队尾tail自增指向下一个元素空间
  10. *event = client->buffer[client->tail++];
  11. client->tail &= client->bufsize - 1;
  12. if (client->use_wake_lock &&
  13. client->packet_head == client->tail)
  14. wake_unlock(&client->wake_lock);
  15. }
  16. spin_unlock_irq(&client->buffer_lock);
  17. return have_event;
  18. }

[1] 结构体evdev成员client_list为内核链表表头,通过该表头遍历链表可以访问所有挂在其上的evdev_client对象
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注