Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

vcs: add poll/fasync support

The /dev/vcs* devices are used, amongst other things, by accessibility
applications such as BRLTTY to display the screen content onto refreshable
braille displays. Currently this is performed by constantly reading from
/dev/vcsa0 whether or not the screen content has changed. Given the
default braille refresh rate of 25 times per second, this easily qualifies
as the biggest source of wake-up events preventing laptops from entering
deeper power saving states.

To avoid this periodic polling, let's add support for select()/poll() and
SIGIO with the /dev/vcs* devices. The implemented semantic is to report
data availability whenever the corresponding vt has seen some update after
the last read() operation. The application still has to lseek() back
as usual in order to read() the new data.

Not to create unwanted overhead, the needed data structure is allocated
and the vt notification callback is registered only when the poll or
fasync method is invoked for the first time per file instance.

Signed-off-by: Nicolas Pitre <nicolas.pitre@canonical.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Nicolas Pitre and committed by
Greg Kroah-Hartman
47725ac7 f4a3e0bc

+133
+133
drivers/char/vc_screen.c
··· 35 35 #include <linux/console.h> 36 36 #include <linux/device.h> 37 37 #include <linux/smp_lock.h> 38 + #include <linux/sched.h> 39 + #include <linux/fs.h> 40 + #include <linux/poll.h> 41 + #include <linux/signal.h> 42 + #include <linux/slab.h> 43 + #include <linux/notifier.h> 38 44 39 45 #include <asm/uaccess.h> 40 46 #include <asm/byteorder.h> ··· 50 44 #undef org 51 45 #undef addr 52 46 #define HEADER_SIZE 4 47 + 48 + struct vcs_poll_data { 49 + struct notifier_block notifier; 50 + unsigned int cons_num; 51 + bool seen_last_update; 52 + wait_queue_head_t waitq; 53 + struct fasync_struct *fasync; 54 + }; 55 + 56 + static int 57 + vcs_notifier(struct notifier_block *nb, unsigned long code, void *_param) 58 + { 59 + struct vt_notifier_param *param = _param; 60 + struct vc_data *vc = param->vc; 61 + struct vcs_poll_data *poll = 62 + container_of(nb, struct vcs_poll_data, notifier); 63 + int currcons = poll->cons_num; 64 + 65 + if (code != VT_UPDATE) 66 + return NOTIFY_DONE; 67 + 68 + if (currcons == 0) 69 + currcons = fg_console; 70 + else 71 + currcons--; 72 + if (currcons != vc->vc_num) 73 + return NOTIFY_DONE; 74 + 75 + poll->seen_last_update = false; 76 + wake_up_interruptible(&poll->waitq); 77 + kill_fasync(&poll->fasync, SIGIO, POLL_IN); 78 + return NOTIFY_OK; 79 + } 80 + 81 + static void 82 + vcs_poll_data_free(struct vcs_poll_data *poll) 83 + { 84 + unregister_vt_notifier(&poll->notifier); 85 + kfree(poll); 86 + } 87 + 88 + static struct vcs_poll_data * 89 + vcs_poll_data_get(struct file *file) 90 + { 91 + struct vcs_poll_data *poll = file->private_data; 92 + 93 + if (poll) 94 + return poll; 95 + 96 + poll = kzalloc(sizeof(*poll), GFP_KERNEL); 97 + if (!poll) 98 + return NULL; 99 + poll->cons_num = iminor(file->f_path.dentry->d_inode) & 127; 100 + init_waitqueue_head(&poll->waitq); 101 + poll->notifier.notifier_call = vcs_notifier; 102 + if (register_vt_notifier(&poll->notifier) != 0) { 103 + kfree(poll); 104 + return NULL; 105 + } 106 + 107 + /* 108 + * This code may be called either through ->poll() or ->fasync(). 109 + * If we have two threads using the same file descriptor, they could 110 + * both enter this function, both notice that the structure hasn't 111 + * been allocated yet and go ahead allocating it in parallel, but 112 + * only one of them must survive and be shared otherwise we'd leak 113 + * memory with a dangling notifier callback. 114 + */ 115 + spin_lock(&file->f_lock); 116 + if (!file->private_data) { 117 + file->private_data = poll; 118 + } else { 119 + /* someone else raced ahead of us */ 120 + vcs_poll_data_free(poll); 121 + poll = file->private_data; 122 + } 123 + spin_unlock(&file->f_lock); 124 + 125 + return poll; 126 + } 53 127 54 128 static int 55 129 vcs_size(struct inode *inode) ··· 188 102 struct inode *inode = file->f_path.dentry->d_inode; 189 103 unsigned int currcons = iminor(inode); 190 104 struct vc_data *vc; 105 + struct vcs_poll_data *poll; 191 106 long pos; 192 107 long viewed, attr, read; 193 108 int col, maxcol; ··· 221 134 ret = -EINVAL; 222 135 if (pos < 0) 223 136 goto unlock_out; 137 + poll = file->private_data; 138 + if (count && poll) 139 + poll->seen_last_update = true; 224 140 read = 0; 225 141 ret = 0; 226 142 while (count) { ··· 547 457 return ret; 548 458 } 549 459 460 + static unsigned int 461 + vcs_poll(struct file *file, poll_table *wait) 462 + { 463 + struct vcs_poll_data *poll = vcs_poll_data_get(file); 464 + int ret = 0; 465 + 466 + if (poll) { 467 + poll_wait(file, &poll->waitq, wait); 468 + if (!poll->seen_last_update) 469 + ret = POLLIN | POLLRDNORM; 470 + } 471 + return ret; 472 + } 473 + 474 + static int 475 + vcs_fasync(int fd, struct file *file, int on) 476 + { 477 + struct vcs_poll_data *poll = file->private_data; 478 + 479 + if (!poll) { 480 + /* don't allocate anything if all we want is disable fasync */ 481 + if (!on) 482 + return 0; 483 + poll = vcs_poll_data_get(file); 484 + if (!poll) 485 + return -ENOMEM; 486 + } 487 + 488 + return fasync_helper(fd, file, on, &poll->fasync); 489 + } 490 + 550 491 static int 551 492 vcs_open(struct inode *inode, struct file *filp) 552 493 { ··· 591 470 return ret; 592 471 } 593 472 473 + static int vcs_release(struct inode *inode, struct file *file) 474 + { 475 + struct vcs_poll_data *poll = file->private_data; 476 + 477 + if (poll) 478 + vcs_poll_data_free(poll); 479 + return 0; 480 + } 481 + 594 482 static const struct file_operations vcs_fops = { 595 483 .llseek = vcs_lseek, 596 484 .read = vcs_read, 597 485 .write = vcs_write, 486 + .poll = vcs_poll, 487 + .fasync = vcs_fasync, 598 488 .open = vcs_open, 489 + .release = vcs_release, 599 490 }; 600 491 601 492 static struct class *vc_class;