1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
4 * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
5 * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
6 */
7 #include <linux/cdev.h>
8 #include <linux/debugfs.h>
9 #include <linux/completion.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/freezer.h>
13 #include <linux/fs.h>
14 #include <linux/splice.h>
15 #include <linux/pagemap.h>
16 #include <linux/idr.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/poll.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_console.h>
25 #include <linux/wait.h>
26 #include <linux/workqueue.h>
27 #include <linux/module.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/string_choices.h>
30 #include "../tty/hvc/hvc_console.h"
31
32 #define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
33 #define VIRTCONS_MAX_PORTS 0x8000
34
35 /*
36 * This is a global struct for storing common data for all the devices
37 * this driver handles.
38 *
39 * Mainly, it has a linked list for all the consoles in one place so
40 * that callbacks from hvc for get_chars(), put_chars() work properly
41 * across multiple devices and multiple ports per device.
42 */
43 struct ports_driver_data {
44 /* Used for exporting per-port information to debugfs */
45 struct dentry *debugfs_dir;
46
47 /* List of all the devices we're handling */
48 struct list_head portdevs;
49
50 /* All the console devices handled by this driver */
51 struct list_head consoles;
52 };
53
54 static struct ports_driver_data pdrvdata;
55
56 static const struct class port_class = {
57 .name = "virtio-ports",
58 };
59
60 static DEFINE_SPINLOCK(pdrvdata_lock);
61 static DECLARE_COMPLETION(early_console_added);
62
63 /* This struct holds information that's relevant only for console ports */
64 struct console {
65 /* We'll place all consoles in a list in the pdrvdata struct */
66 struct list_head list;
67
68 /* The hvc device associated with this console port */
69 struct hvc_struct *hvc;
70
71 /* The size of the console */
72 struct winsize ws;
73
74 /*
75 * This number identifies the number that we used to register
76 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
77 * number passed on by the hvc callbacks to us to
78 * differentiate between the other console ports handled by
79 * this driver
80 */
81 u32 vtermno;
82 };
83
84 static DEFINE_IDA(vtermno_ida);
85
86 struct port_buffer {
87 char *buf;
88
89 /* size of the buffer in *buf above */
90 size_t size;
91
92 /* used length of the buffer */
93 size_t len;
94 /* offset in the buf from which to consume data */
95 size_t offset;
96
97 /* DMA address of buffer */
98 dma_addr_t dma;
99
100 /* Device we got DMA memory from */
101 struct device *dev;
102
103 /* List of pending dma buffers to free */
104 struct list_head list;
105
106 /* If sgpages == 0 then buf is used */
107 unsigned int sgpages;
108
109 /* sg is used if spages > 0. sg must be the last in is struct */
110 struct scatterlist sg[] __counted_by(sgpages);
111 };
112
113 /*
114 * This is a per-device struct that stores data common to all the
115 * ports for that device (vdev->priv).
116 */
117 struct ports_device {
118 /* Next portdev in the list, head is in the pdrvdata struct */
119 struct list_head list;
120
121 /*
122 * Workqueue handlers where we process deferred work after
123 * notification
124 */
125 struct work_struct control_work;
126 struct work_struct config_work;
127
128 struct list_head ports;
129
130 /* To protect the list of ports */
131 spinlock_t ports_lock;
132
133 /* To protect the vq operations for the control channel */
134 spinlock_t c_ivq_lock;
135 spinlock_t c_ovq_lock;
136
137 /* max. number of ports this device can hold */
138 u32 max_nr_ports;
139
140 /* The virtio device we're associated with */
141 struct virtio_device *vdev;
142
143 /*
144 * A couple of virtqueues for the control channel: one for
145 * guest->host transfers, one for host->guest transfers
146 */
147 struct virtqueue *c_ivq, *c_ovq;
148
149 /*
150 * A control packet buffer for guest->host requests, protected
151 * by c_ovq_lock.
152 */
153 struct virtio_console_control cpkt;
154
155 /* Array of per-port IO virtqueues */
156 struct virtqueue **in_vqs, **out_vqs;
157
158 /* Major number for this device. Ports will be created as minors. */
159 int chr_major;
160
161 /*
162 * Set to true during PM freeze to block TX paths that may race
163 * with virtqueue teardown (e.g. hvc put_chars with no_console_suspend).
164 */
165 bool pm_freezing;
166 };
167
168 struct port_stats {
169 unsigned long bytes_sent, bytes_received, bytes_discarded;
170 };
171
172 /* This struct holds the per-port data */
173 struct port {
174 /* Next port in the list, head is in the ports_device */
175 struct list_head list;
176
177 /* Pointer to the parent virtio_console device */
178 struct ports_device *portdev;
179
180 /* The current buffer from which data has to be fed to readers */
181 struct port_buffer *inbuf;
182
183 /*
184 * To protect the operations on the in_vq associated with this
185 * port. Has to be a spinlock because it can be called from
186 * interrupt context (get_char()).
187 */
188 spinlock_t inbuf_lock;
189
190 /* Protect the operations on the out_vq. */
191 spinlock_t outvq_lock;
192
193 /* The IO vqs for this port */
194 struct virtqueue *in_vq, *out_vq;
195
196 /* File in the debugfs directory that exposes this port's information */
197 struct dentry *debugfs_file;
198
199 /*
200 * Keep count of the bytes sent, received and discarded for
201 * this port for accounting and debugging purposes. These
202 * counts are not reset across port open / close events.
203 */
204 struct port_stats stats;
205
206 /*
207 * The entries in this struct will be valid if this port is
208 * hooked up to an hvc console
209 */
210 struct console cons;
211
212 /* Each port associates with a separate char device */
213 struct cdev *cdev;
214 struct device *dev;
215
216 /* Reference-counting to handle port hot-unplugs and file operations */
217 struct kref kref;
218
219 /* A waitqueue for poll() or blocking read operations */
220 wait_queue_head_t waitqueue;
221
222 /* The 'name' of the port that we expose via sysfs properties */
223 char *name;
224
225 /* We can notify apps of host connect / disconnect events via SIGIO */
226 struct fasync_struct *async_queue;
227
228 /* The 'id' to identify the port with the Host */
229 u32 id;
230
231 bool outvq_full;
232
233 /* Is the host device open */
234 bool host_connected;
235
236 /* We should allow only one process to open a port */
237 bool guest_connected;
238 };
239
find_port_by_vtermno(u32 vtermno)240 static struct port *find_port_by_vtermno(u32 vtermno)
241 {
242 struct port *port;
243 struct console *cons;
244 unsigned long flags;
245
246 spin_lock_irqsave(&pdrvdata_lock, flags);
247 list_for_each_entry(cons, &pdrvdata.consoles, list) {
248 if (cons->vtermno == vtermno) {
249 port = container_of(cons, struct port, cons);
250 goto out;
251 }
252 }
253 port = NULL;
254 out:
255 spin_unlock_irqrestore(&pdrvdata_lock, flags);
256 return port;
257 }
258
find_port_by_devt_in_portdev(struct ports_device * portdev,dev_t dev)259 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
260 dev_t dev)
261 {
262 struct port *port;
263 unsigned long flags;
264
265 spin_lock_irqsave(&portdev->ports_lock, flags);
266 list_for_each_entry(port, &portdev->ports, list) {
267 if (port->cdev->dev == dev) {
268 kref_get(&port->kref);
269 goto out;
270 }
271 }
272 port = NULL;
273 out:
274 spin_unlock_irqrestore(&portdev->ports_lock, flags);
275
276 return port;
277 }
278
find_port_by_devt(dev_t dev)279 static struct port *find_port_by_devt(dev_t dev)
280 {
281 struct ports_device *portdev;
282 struct port *port;
283 unsigned long flags;
284
285 spin_lock_irqsave(&pdrvdata_lock, flags);
286 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
287 port = find_port_by_devt_in_portdev(portdev, dev);
288 if (port)
289 goto out;
290 }
291 port = NULL;
292 out:
293 spin_unlock_irqrestore(&pdrvdata_lock, flags);
294 return port;
295 }
296
find_port_by_id(struct ports_device * portdev,u32 id)297 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
298 {
299 struct port *port;
300 unsigned long flags;
301
302 spin_lock_irqsave(&portdev->ports_lock, flags);
303 list_for_each_entry(port, &portdev->ports, list)
304 if (port->id == id)
305 goto out;
306 port = NULL;
307 out:
308 spin_unlock_irqrestore(&portdev->ports_lock, flags);
309
310 return port;
311 }
312
313 /*
314 * Finds a port by the virtqueue and returns a pointer to struct port
315 * with the reference count incremented.
316 *
317 * Callers MUST decrement it when finished.
318 */
find_port_by_vq(struct ports_device * portdev,struct virtqueue * vq)319 static struct port *find_port_by_vq(struct ports_device *portdev,
320 struct virtqueue *vq)
321 {
322 struct port *port;
323 unsigned long flags;
324
325 spin_lock_irqsave(&portdev->ports_lock, flags);
326 list_for_each_entry(port, &portdev->ports, list)
327 if (port->in_vq == vq || port->out_vq == vq) {
328 kref_get(&port->kref);
329 goto out;
330 }
331 port = NULL;
332 out:
333 spin_unlock_irqrestore(&portdev->ports_lock, flags);
334 return port;
335 }
336
is_console_port(struct port * port)337 static bool is_console_port(struct port *port)
338 {
339 if (port->cons.hvc)
340 return true;
341 return false;
342 }
343
is_rproc_serial(const struct virtio_device * vdev)344 static bool is_rproc_serial(const struct virtio_device *vdev)
345 {
346 return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
347 }
348
use_multiport(struct ports_device * portdev)349 static inline bool use_multiport(struct ports_device *portdev)
350 {
351 /*
352 * This condition can be true when put_chars is called from
353 * early_init
354 */
355 if (!portdev->vdev)
356 return false;
357 return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
358 }
359
360 static DEFINE_SPINLOCK(dma_bufs_lock);
361 static LIST_HEAD(pending_free_dma_bufs);
362
free_buf(struct port_buffer * buf,bool can_sleep)363 static void free_buf(struct port_buffer *buf, bool can_sleep)
364 {
365 unsigned int i;
366
367 for (i = 0; i < buf->sgpages; i++) {
368 struct page *page = sg_page(&buf->sg[i]);
369 if (!page)
370 break;
371 put_page(page);
372 }
373
374 if (!buf->dev) {
375 kfree(buf->buf);
376 } else if (is_rproc_enabled) {
377 unsigned long flags;
378
379 /* dma_free_coherent requires interrupts to be enabled. */
380 if (!can_sleep) {
381 /* queue up dma-buffers to be freed later */
382 spin_lock_irqsave(&dma_bufs_lock, flags);
383 list_add_tail(&buf->list, &pending_free_dma_bufs);
384 spin_unlock_irqrestore(&dma_bufs_lock, flags);
385 return;
386 }
387 dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
388
389 /* Release device refcnt and allow it to be freed */
390 put_device(buf->dev);
391 }
392
393 kfree(buf);
394 }
395
reclaim_dma_bufs(void)396 static void reclaim_dma_bufs(void)
397 {
398 unsigned long flags;
399 struct port_buffer *buf, *tmp;
400 LIST_HEAD(tmp_list);
401
402 if (list_empty(&pending_free_dma_bufs))
403 return;
404
405 /* Create a copy of the pending_free_dma_bufs while holding the lock */
406 spin_lock_irqsave(&dma_bufs_lock, flags);
407 list_cut_position(&tmp_list, &pending_free_dma_bufs,
408 pending_free_dma_bufs.prev);
409 spin_unlock_irqrestore(&dma_bufs_lock, flags);
410
411 /* Release the dma buffers, without irqs enabled */
412 list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
413 list_del(&buf->list);
414 free_buf(buf, true);
415 }
416 }
417
alloc_buf(struct virtio_device * vdev,size_t buf_size,int pages,gfp_t gfp)418 static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
419 int pages, gfp_t gfp)
420 {
421 struct port_buffer *buf;
422
423 reclaim_dma_bufs();
424
425 /*
426 * Allocate buffer and the sg list. The sg list array is allocated
427 * directly after the port_buffer struct.
428 */
429 buf = kmalloc_flex(*buf, sg, pages, gfp);
430 if (!buf)
431 goto fail;
432
433 buf->sgpages = pages;
434 if (pages > 0) {
435 buf->dev = NULL;
436 buf->buf = NULL;
437 return buf;
438 }
439
440 if (is_rproc_serial(vdev)) {
441 /*
442 * Allocate DMA memory from ancestor. When a virtio
443 * device is created by remoteproc, the DMA memory is
444 * associated with the parent device:
445 * virtioY => remoteprocX#vdevYbuffer.
446 */
447 buf->dev = vdev->dev.parent;
448 if (!buf->dev)
449 goto free_buf;
450
451 /* Increase device refcnt to avoid freeing it */
452 get_device(buf->dev);
453 buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma, gfp);
454 } else {
455 buf->dev = NULL;
456 buf->buf = kmalloc(buf_size, gfp);
457 }
458
459 if (!buf->buf)
460 goto free_buf;
461 buf->len = 0;
462 buf->offset = 0;
463 buf->size = buf_size;
464 return buf;
465
466 free_buf:
467 kfree(buf);
468 fail:
469 return NULL;
470 }
471
472 /* Callers should take appropriate locks */
get_inbuf(struct port * port)473 static struct port_buffer *get_inbuf(struct port *port)
474 {
475 struct port_buffer *buf;
476 unsigned int len;
477
478 if (port->inbuf)
479 return port->inbuf;
480
481 buf = virtqueue_get_buf(port->in_vq, &len);
482 if (buf) {
483 buf->len = min_t(size_t, len, buf->size);
484 buf->offset = 0;
485 port->stats.bytes_received += len;
486 }
487 return buf;
488 }
489
490 /*
491 * Create a scatter-gather list representing our input buffer and put
492 * it in the queue.
493 *
494 * Callers should take appropriate locks.
495 */
add_inbuf(struct virtqueue * vq,struct port_buffer * buf)496 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
497 {
498 struct scatterlist sg[1];
499 int ret;
500
501 sg_init_one(sg, buf->buf, buf->size);
502
503 ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
504 virtqueue_kick(vq);
505 if (!ret)
506 ret = vq->num_free;
507 return ret;
508 }
509
510 /* Discard any unread data this port has. Callers lockers. */
discard_port_data(struct port * port)511 static void discard_port_data(struct port *port)
512 {
513 struct port_buffer *buf;
514 unsigned int err;
515
516 if (!port->portdev) {
517 /* Device has been unplugged. vqs are already gone. */
518 return;
519 }
520 buf = get_inbuf(port);
521
522 err = 0;
523 while (buf) {
524 port->stats.bytes_discarded += buf->len - buf->offset;
525 if (add_inbuf(port->in_vq, buf) < 0) {
526 err++;
527 free_buf(buf, false);
528 }
529 port->inbuf = NULL;
530 buf = get_inbuf(port);
531 }
532 if (err)
533 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
534 err);
535 }
536
port_has_data(struct port * port)537 static bool port_has_data(struct port *port)
538 {
539 unsigned long flags;
540 bool ret;
541
542 ret = false;
543 spin_lock_irqsave(&port->inbuf_lock, flags);
544 port->inbuf = get_inbuf(port);
545 if (port->inbuf)
546 ret = true;
547
548 spin_unlock_irqrestore(&port->inbuf_lock, flags);
549 return ret;
550 }
551
__send_control_msg(struct ports_device * portdev,u32 port_id,unsigned int event,unsigned int value)552 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
553 unsigned int event, unsigned int value)
554 {
555 struct scatterlist sg[1];
556 struct virtqueue *vq;
557 unsigned int len;
558
559 if (!use_multiport(portdev))
560 return 0;
561
562 vq = portdev->c_ovq;
563
564 spin_lock(&portdev->c_ovq_lock);
565
566 portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
567 portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
568 portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
569
570 sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
571
572 if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
573 virtqueue_kick(vq);
574 while (!virtqueue_get_buf(vq, &len)
575 && !virtqueue_is_broken(vq))
576 cpu_relax();
577 }
578
579 spin_unlock(&portdev->c_ovq_lock);
580 return 0;
581 }
582
send_control_msg(struct port * port,unsigned int event,unsigned int value)583 static ssize_t send_control_msg(struct port *port, unsigned int event,
584 unsigned int value)
585 {
586 /* Did the port get unplugged before userspace closed it? */
587 if (port->portdev)
588 return __send_control_msg(port->portdev, port->id, event, value);
589 return 0;
590 }
591
592
593 /* Callers must take the port->outvq_lock */
reclaim_consumed_buffers(struct port * port)594 static void reclaim_consumed_buffers(struct port *port)
595 {
596 struct port_buffer *buf;
597 unsigned int len;
598
599 if (!port->portdev) {
600 /* Device has been unplugged. vqs are already gone. */
601 return;
602 }
603 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
604 free_buf(buf, false);
605 port->outvq_full = false;
606 }
607 }
608
__send_to_port(struct port * port,struct scatterlist * sg,int nents,size_t in_count,struct port_buffer * buf,bool nonblock)609 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
610 int nents, size_t in_count,
611 struct port_buffer *buf, bool nonblock)
612 {
613 struct virtqueue *out_vq;
614 int err;
615 unsigned long flags;
616 unsigned int len;
617 struct ports_device *portdev;
618
619 spin_lock_irqsave(&port->outvq_lock, flags);
620
621 portdev = READ_ONCE(port->portdev);
622
623 if (!portdev) {
624 in_count = 0;
625 goto free_and_done;
626 }
627
628 /*
629 * Check freeze flag under the lock so that the flag check and
630 * virtqueue_add_outbuf() are atomic with respect to
631 * remove_port_data() which also takes outvq_lock. This
632 * guarantees that once remove_port_data() returns, no new
633 * buffers can be added before remove_vqs() tears down the vq.
634 * Pairs with smp_store_release() in virtcons_freeze/restore.
635 */
636 if (smp_load_acquire(&portdev->pm_freezing)) /* pairs with freeze/restore */
637 goto free_and_done;
638
639 out_vq = port->out_vq;
640
641 reclaim_consumed_buffers(port);
642
643 err = virtqueue_add_outbuf(out_vq, sg, nents, buf, GFP_ATOMIC);
644
645 /* Tell Host to go! */
646 virtqueue_kick(out_vq);
647
648 if (err) {
649 in_count = 0;
650 goto free_and_done;
651 }
652
653 if (out_vq->num_free == 0)
654 port->outvq_full = true;
655
656 if (nonblock)
657 goto done;
658
659 /*
660 * Wait till the host acknowledges it pushed out the data we
661 * sent. This is done for data from the hvc_console; the tty
662 * operations are performed with spinlocks held so we can't
663 * sleep here. An alternative would be to copy the data to a
664 * buffer and relax the spinning requirement. The downside is
665 * we need to kmalloc a GFP_ATOMIC buffer each time the
666 * console driver writes something out.
667 *
668 * Spin until host returns the buffer.
669 * Capture the returned buf so we can free it.
670 * If broken, buf == NULL and buf stays in the vq;
671 * remove_vqs() will call virtqueue_detach_unused_buf() -> free_buf().
672 */
673 while (!(buf = virtqueue_get_buf(out_vq, &len))
674 && !virtqueue_is_broken(out_vq))
675 cpu_relax();
676
677 free_and_done:
678 if (buf)
679 free_buf(buf, false);
680 done:
681 spin_unlock_irqrestore(&port->outvq_lock, flags);
682
683 port->stats.bytes_sent += in_count;
684 /*
685 * We're expected to return the amount of data we wrote -- all
686 * of it
687 */
688 return in_count;
689 }
690
691 /*
692 * Give out the data that's requested from the buffer that we have
693 * queued up.
694 */
fill_readbuf(struct port * port,u8 __user * out_buf,size_t out_count,bool to_user)695 static ssize_t fill_readbuf(struct port *port, u8 __user *out_buf,
696 size_t out_count, bool to_user)
697 {
698 struct port_buffer *buf;
699 unsigned long flags;
700
701 if (!out_count || !port_has_data(port))
702 return 0;
703
704 buf = port->inbuf;
705 out_count = min(out_count, buf->len - buf->offset);
706
707 if (to_user) {
708 ssize_t ret;
709
710 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
711 if (ret)
712 return -EFAULT;
713 } else {
714 memcpy((__force u8 *)out_buf, buf->buf + buf->offset,
715 out_count);
716 }
717
718 buf->offset += out_count;
719
720 if (buf->offset == buf->len) {
721 /*
722 * We're done using all the data in this buffer.
723 * Re-queue so that the Host can send us more data.
724 */
725 spin_lock_irqsave(&port->inbuf_lock, flags);
726 port->inbuf = NULL;
727
728 if (add_inbuf(port->in_vq, buf) < 0)
729 dev_warn(port->dev, "failed add_buf\n");
730
731 spin_unlock_irqrestore(&port->inbuf_lock, flags);
732 }
733 /* Return the number of bytes actually copied */
734 return out_count;
735 }
736
737 /* The condition that must be true for polling to end */
will_read_block(struct port * port)738 static bool will_read_block(struct port *port)
739 {
740 if (!port->guest_connected) {
741 /* Port got hot-unplugged. Let's exit. */
742 return false;
743 }
744 return !port_has_data(port) && port->host_connected;
745 }
746
will_write_block(struct port * port)747 static bool will_write_block(struct port *port)
748 {
749 bool ret;
750
751 if (!port->guest_connected) {
752 /* Port got hot-unplugged. Let's exit. */
753 return false;
754 }
755 if (!port->host_connected)
756 return true;
757
758 spin_lock_irq(&port->outvq_lock);
759 /*
760 * Check if the Host has consumed any buffers since we last
761 * sent data (this is only applicable for nonblocking ports).
762 */
763 reclaim_consumed_buffers(port);
764 ret = port->outvq_full;
765 spin_unlock_irq(&port->outvq_lock);
766
767 return ret;
768 }
769
port_fops_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)770 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
771 size_t count, loff_t *offp)
772 {
773 struct port *port;
774 ssize_t ret;
775
776 port = filp->private_data;
777
778 /* Port is hot-unplugged. */
779 if (!port->guest_connected)
780 return -ENODEV;
781
782 if (!port_has_data(port)) {
783 /*
784 * If nothing's connected on the host just return 0 in
785 * case of list_empty; this tells the userspace app
786 * that there's no connection
787 */
788 if (!port->host_connected)
789 return 0;
790 if (filp->f_flags & O_NONBLOCK)
791 return -EAGAIN;
792
793 ret = wait_event_freezable(port->waitqueue,
794 !will_read_block(port));
795 if (ret < 0)
796 return ret;
797 }
798 /* Port got hot-unplugged while we were waiting above. */
799 if (!port->guest_connected)
800 return -ENODEV;
801 /*
802 * We could've received a disconnection message while we were
803 * waiting for more data.
804 *
805 * This check is not clubbed in the if() statement above as we
806 * might receive some data as well as the host could get
807 * disconnected after we got woken up from our wait. So we
808 * really want to give off whatever data we have and only then
809 * check for host_connected.
810 */
811 if (!port_has_data(port) && !port->host_connected)
812 return 0;
813
814 return fill_readbuf(port, ubuf, count, true);
815 }
816
wait_port_writable(struct port * port,bool nonblock)817 static int wait_port_writable(struct port *port, bool nonblock)
818 {
819 int ret;
820
821 if (will_write_block(port)) {
822 if (nonblock)
823 return -EAGAIN;
824
825 ret = wait_event_freezable(port->waitqueue,
826 !will_write_block(port));
827 if (ret < 0)
828 return ret;
829 }
830 /* Port got hot-unplugged. */
831 if (!port->guest_connected)
832 return -ENODEV;
833
834 return 0;
835 }
836
port_fops_write(struct file * filp,const char __user * ubuf,size_t count,loff_t * offp)837 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
838 size_t count, loff_t *offp)
839 {
840 struct port *port;
841 struct port_buffer *buf;
842 ssize_t ret;
843 bool nonblock;
844 struct scatterlist sg[1];
845
846 /* Userspace could be out to fool us */
847 if (!count)
848 return 0;
849
850 port = filp->private_data;
851
852 nonblock = filp->f_flags & O_NONBLOCK;
853
854 ret = wait_port_writable(port, nonblock);
855 if (ret < 0)
856 return ret;
857
858 count = min((size_t)(32 * 1024), count);
859
860 buf = alloc_buf(port->portdev->vdev, count, 0, GFP_KERNEL);
861 if (!buf)
862 return -ENOMEM;
863
864 ret = copy_from_user(buf->buf, ubuf, count);
865 if (ret) {
866 free_buf(buf, true);
867 return -EFAULT;
868 }
869
870 /*
871 * We now ask send_buf() to not spin for generic ports -- we
872 * can re-use the same code path that non-blocking file
873 * descriptors take for blocking file descriptors since the
874 * wait is already done and we're certain the write will go
875 * through to the host.
876 */
877 nonblock = true;
878 sg_init_one(sg, buf->buf, count);
879 return __send_to_port(port, sg, 1, count, buf, nonblock);
880 }
881
882 struct sg_list {
883 unsigned int n;
884 unsigned int size;
885 size_t len;
886 struct scatterlist *sg;
887 };
888
pipe_to_sg(struct pipe_inode_info * pipe,struct pipe_buffer * buf,struct splice_desc * sd)889 static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
890 struct splice_desc *sd)
891 {
892 struct sg_list *sgl = sd->u.data;
893 unsigned int offset, len;
894
895 if (sgl->n == sgl->size)
896 return 0;
897
898 /* Try lock this page */
899 if (pipe_buf_try_steal(pipe, buf)) {
900 /* Get reference and unlock page for moving */
901 get_page(buf->page);
902 unlock_page(buf->page);
903
904 len = min(buf->len, sd->len);
905 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
906 } else {
907 /* Failback to copying a page */
908 struct page *page = alloc_page(GFP_KERNEL);
909 char *src;
910
911 if (!page)
912 return -ENOMEM;
913
914 offset = sd->pos & ~PAGE_MASK;
915
916 len = sd->len;
917 if (len + offset > PAGE_SIZE)
918 len = PAGE_SIZE - offset;
919
920 src = kmap_local_page(buf->page);
921 memcpy(page_address(page) + offset, src + buf->offset, len);
922 kunmap_local(src);
923
924 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
925 }
926 sgl->n++;
927 sgl->len += len;
928
929 return len;
930 }
931
932 /* Faster zero-copy write by splicing */
port_fops_splice_write(struct pipe_inode_info * pipe,struct file * filp,loff_t * ppos,size_t len,unsigned int flags)933 static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
934 struct file *filp, loff_t *ppos,
935 size_t len, unsigned int flags)
936 {
937 struct port *port = filp->private_data;
938 struct sg_list sgl;
939 ssize_t ret;
940 struct port_buffer *buf;
941 struct splice_desc sd = {
942 .total_len = len,
943 .flags = flags,
944 .pos = *ppos,
945 .u.data = &sgl,
946 };
947 unsigned int occupancy;
948
949 /*
950 * Rproc_serial does not yet support splice. To support splice
951 * pipe_to_sg() must allocate dma-buffers and copy content from
952 * regular pages to dma pages. And alloc_buf and free_buf must
953 * support allocating and freeing such a list of dma-buffers.
954 */
955 if (is_rproc_serial(port->out_vq->vdev))
956 return -EINVAL;
957
958 pipe_lock(pipe);
959 ret = 0;
960 if (pipe_is_empty(pipe))
961 goto error_out;
962
963 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
964 if (ret < 0)
965 goto error_out;
966
967 occupancy = pipe_buf_usage(pipe);
968 buf = alloc_buf(port->portdev->vdev, 0, occupancy, GFP_KERNEL);
969
970 if (!buf) {
971 ret = -ENOMEM;
972 goto error_out;
973 }
974
975 sgl.n = 0;
976 sgl.len = 0;
977 sgl.size = occupancy;
978 sgl.sg = buf->sg;
979 sg_init_table(sgl.sg, sgl.size);
980 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
981 pipe_unlock(pipe);
982
983 if (likely(ret > 0))
984 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
985 else
986 free_buf(buf, true);
987
988 return ret;
989
990 error_out:
991 pipe_unlock(pipe);
992 return ret;
993 }
994
port_fops_poll(struct file * filp,poll_table * wait)995 static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
996 {
997 struct port *port;
998 __poll_t ret;
999
1000 port = filp->private_data;
1001 poll_wait(filp, &port->waitqueue, wait);
1002
1003 if (!port->guest_connected) {
1004 /* Port got unplugged */
1005 return EPOLLHUP;
1006 }
1007 ret = 0;
1008 if (!will_read_block(port))
1009 ret |= EPOLLIN | EPOLLRDNORM;
1010 if (!will_write_block(port))
1011 ret |= EPOLLOUT;
1012 if (!port->host_connected)
1013 ret |= EPOLLHUP;
1014
1015 return ret;
1016 }
1017
1018 static void remove_port(struct kref *kref);
1019
port_fops_release(struct inode * inode,struct file * filp)1020 static int port_fops_release(struct inode *inode, struct file *filp)
1021 {
1022 struct port *port;
1023
1024 port = filp->private_data;
1025
1026 /* Notify host of port being closed */
1027 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1028
1029 spin_lock_irq(&port->inbuf_lock);
1030 port->guest_connected = false;
1031
1032 discard_port_data(port);
1033
1034 spin_unlock_irq(&port->inbuf_lock);
1035
1036 spin_lock_irq(&port->outvq_lock);
1037 reclaim_consumed_buffers(port);
1038 spin_unlock_irq(&port->outvq_lock);
1039
1040 reclaim_dma_bufs();
1041 /*
1042 * Locks aren't necessary here as a port can't be opened after
1043 * unplug, and if a port isn't unplugged, a kref would already
1044 * exist for the port. Plus, taking ports_lock here would
1045 * create a dependency on other locks taken by functions
1046 * inside remove_port if we're the last holder of the port,
1047 * creating many problems.
1048 */
1049 kref_put(&port->kref, remove_port);
1050
1051 return 0;
1052 }
1053
port_fops_open(struct inode * inode,struct file * filp)1054 static int port_fops_open(struct inode *inode, struct file *filp)
1055 {
1056 struct cdev *cdev = inode->i_cdev;
1057 struct port *port;
1058 int ret;
1059
1060 /* We get the port with a kref here */
1061 port = find_port_by_devt(cdev->dev);
1062 if (!port) {
1063 /* Port was unplugged before we could proceed */
1064 return -ENXIO;
1065 }
1066 filp->private_data = port;
1067
1068 /*
1069 * Don't allow opening of console port devices -- that's done
1070 * via /dev/hvc
1071 */
1072 if (is_console_port(port)) {
1073 ret = -ENXIO;
1074 goto out;
1075 }
1076
1077 /* Allow only one process to open a particular port at a time */
1078 spin_lock_irq(&port->inbuf_lock);
1079 if (port->guest_connected) {
1080 spin_unlock_irq(&port->inbuf_lock);
1081 ret = -EBUSY;
1082 goto out;
1083 }
1084
1085 port->guest_connected = true;
1086 spin_unlock_irq(&port->inbuf_lock);
1087
1088 spin_lock_irq(&port->outvq_lock);
1089 /*
1090 * There might be a chance that we missed reclaiming a few
1091 * buffers in the window of the port getting previously closed
1092 * and opening now.
1093 */
1094 reclaim_consumed_buffers(port);
1095 spin_unlock_irq(&port->outvq_lock);
1096
1097 nonseekable_open(inode, filp);
1098
1099 /* Notify host of port being opened */
1100 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
1101
1102 return 0;
1103 out:
1104 kref_put(&port->kref, remove_port);
1105 return ret;
1106 }
1107
port_fops_fasync(int fd,struct file * filp,int mode)1108 static int port_fops_fasync(int fd, struct file *filp, int mode)
1109 {
1110 struct port *port;
1111
1112 port = filp->private_data;
1113 return fasync_helper(fd, filp, mode, &port->async_queue);
1114 }
1115
1116 /*
1117 * The file operations that we support: programs in the guest can open
1118 * a console device, read from it, write to it, poll for data and
1119 * close it. The devices are at
1120 * /dev/vport<device number>p<port number>
1121 */
1122 static const struct file_operations port_fops = {
1123 .owner = THIS_MODULE,
1124 .open = port_fops_open,
1125 .read = port_fops_read,
1126 .write = port_fops_write,
1127 .splice_write = port_fops_splice_write,
1128 .poll = port_fops_poll,
1129 .release = port_fops_release,
1130 .fasync = port_fops_fasync,
1131 };
1132
1133 /*
1134 * The put_chars() callback is pretty straightforward.
1135 *
1136 * We turn the characters into a scatter-gather list, add it to the
1137 * output queue and then kick the Host. Then we sit here waiting for
1138 * it to finish: inefficient in theory, but in practice
1139 * implementations will do it immediately.
1140 */
put_chars(u32 vtermno,const u8 * buf,size_t count)1141 static ssize_t put_chars(u32 vtermno, const u8 *buf, size_t count)
1142 {
1143 struct port *port;
1144 struct scatterlist sg[1];
1145 struct port_buffer *pbuf;
1146 struct ports_device *portdev;
1147
1148 port = find_port_by_vtermno(vtermno);
1149 if (!port)
1150 return -EPIPE;
1151
1152 /*
1153 * Silently drop output in two cases, both by returning count so
1154 * that the hvc layer does not spin-retry:
1155 *
1156 * 1. Device hot-unplug (!portdev): portdev was NULLed by
1157 * unplug_port() after hvc_remove() was already called, so
1158 * the hvc layer will stop invoking put_chars() very soon.
1159 * Returning count avoids a pointless retry loop in the
1160 * interim.
1161 *
1162 * 2. PM freeze (pm_freezing): the hvc console stays active
1163 * under no_console_suspend but virtqueues are being torn
1164 * down. Drop the output silently so the hvc layer does not
1165 * stall suspend.
1166 *
1167 * This early check avoids a pointless GFP_ATOMIC allocation;
1168 * __send_to_port() rechecks under outvq_lock for correctness.
1169 * Pairs with smp_store_release() in virtcons_freeze/restore.
1170 */
1171 portdev = READ_ONCE(port->portdev);
1172 if (!portdev ||
1173 smp_load_acquire(&portdev->pm_freezing)) /* pairs with freeze/restore */
1174 return count;
1175
1176 pbuf = alloc_buf(portdev->vdev, count, 0, GFP_ATOMIC);
1177 if (!pbuf)
1178 return -ENOMEM;
1179
1180 memcpy(pbuf->buf, buf, count);
1181 pbuf->len = count;
1182 sg_init_one(sg, pbuf->buf, count);
1183
1184 /*
1185 * Ownership of pbuf is transferred to __send_to_port().
1186 * Do not touch or free pbuf after this call.
1187 */
1188 return __send_to_port(port, sg, 1, count, pbuf, false);
1189 }
1190
1191 /*
1192 * get_chars() is the callback from the hvc_console infrastructure
1193 * when an interrupt is received.
1194 *
1195 * We call out to fill_readbuf that gets us the required data from the
1196 * buffers that are queued up.
1197 */
get_chars(u32 vtermno,u8 * buf,size_t count)1198 static ssize_t get_chars(u32 vtermno, u8 *buf, size_t count)
1199 {
1200 struct port *port;
1201
1202 port = find_port_by_vtermno(vtermno);
1203 if (!port)
1204 return -EPIPE;
1205
1206 /* If we don't have an input queue yet, we can't get input. */
1207 BUG_ON(!port->in_vq);
1208
1209 return fill_readbuf(port, (__force u8 __user *)buf, count, false);
1210 }
1211
resize_console(struct port * port)1212 static void resize_console(struct port *port)
1213 {
1214 struct virtio_device *vdev;
1215
1216 /* The port could have been hot-unplugged */
1217 if (!port || !is_console_port(port))
1218 return;
1219
1220 vdev = port->portdev->vdev;
1221
1222 /* Don't test F_SIZE at all if we're rproc: not a valid feature! */
1223 if (!is_rproc_serial(vdev) &&
1224 virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1225 hvc_resize(port->cons.hvc, port->cons.ws);
1226 }
1227
1228 /* We set the configuration at this point, since we now have a tty */
notifier_add_vio(struct hvc_struct * hp,int data)1229 static int notifier_add_vio(struct hvc_struct *hp, int data)
1230 {
1231 struct port *port;
1232
1233 port = find_port_by_vtermno(hp->vtermno);
1234 if (!port)
1235 return -EINVAL;
1236
1237 hp->irq_requested = 1;
1238 resize_console(port);
1239
1240 return 0;
1241 }
1242
notifier_del_vio(struct hvc_struct * hp,int data)1243 static void notifier_del_vio(struct hvc_struct *hp, int data)
1244 {
1245 hp->irq_requested = 0;
1246 }
1247
1248 /* The operations for console ports. */
1249 static const struct hv_ops hv_ops = {
1250 .get_chars = get_chars,
1251 .put_chars = put_chars,
1252 .notifier_add = notifier_add_vio,
1253 .notifier_del = notifier_del_vio,
1254 .notifier_hangup = notifier_del_vio,
1255 };
1256
init_port_console(struct port * port)1257 static int init_port_console(struct port *port)
1258 {
1259 int ret;
1260
1261 /*
1262 * The Host's telling us this port is a console port. Hook it
1263 * up with an hvc console.
1264 *
1265 * To set up and manage our virtual console, we call
1266 * hvc_alloc().
1267 *
1268 * The first argument of hvc_alloc() is the virtual console
1269 * number. The second argument is the parameter for the
1270 * notification mechanism (like irq number). We currently
1271 * leave this as zero, virtqueues have implicit notifications.
1272 *
1273 * The third argument is a "struct hv_ops" containing the
1274 * put_chars() get_chars(), notifier_add() and notifier_del()
1275 * pointers. The final argument is the output buffer size: we
1276 * can do any size, so we put PAGE_SIZE here.
1277 */
1278 ret = ida_alloc_min(&vtermno_ida, 1, GFP_KERNEL);
1279 if (ret < 0)
1280 return ret;
1281
1282 port->cons.vtermno = ret;
1283 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1284 if (IS_ERR(port->cons.hvc)) {
1285 ret = PTR_ERR(port->cons.hvc);
1286 dev_err(port->dev,
1287 "error %d allocating hvc for port\n", ret);
1288 port->cons.hvc = NULL;
1289 ida_free(&vtermno_ida, port->cons.vtermno);
1290 return ret;
1291 }
1292 spin_lock_irq(&pdrvdata_lock);
1293 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1294 spin_unlock_irq(&pdrvdata_lock);
1295 port->guest_connected = true;
1296
1297 /* Notify host of port being opened */
1298 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1299
1300 return 0;
1301 }
1302
show_port_name(struct device * dev,struct device_attribute * attr,char * buffer)1303 static ssize_t show_port_name(struct device *dev,
1304 struct device_attribute *attr, char *buffer)
1305 {
1306 struct port *port;
1307
1308 port = dev_get_drvdata(dev);
1309
1310 return sprintf(buffer, "%s\n", port->name);
1311 }
1312
1313 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1314
1315 static struct attribute *port_sysfs_entries[] = {
1316 &dev_attr_name.attr,
1317 NULL
1318 };
1319
1320 static const struct attribute_group port_attribute_group = {
1321 .name = NULL, /* put in device directory */
1322 .attrs = port_sysfs_entries,
1323 };
1324
port_debugfs_show(struct seq_file * s,void * data)1325 static int port_debugfs_show(struct seq_file *s, void *data)
1326 {
1327 struct port *port = s->private;
1328
1329 seq_printf(s, "name: %s\n", port->name ? port->name : "");
1330 seq_printf(s, "guest_connected: %d\n", port->guest_connected);
1331 seq_printf(s, "host_connected: %d\n", port->host_connected);
1332 seq_printf(s, "outvq_full: %d\n", port->outvq_full);
1333 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
1334 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
1335 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
1336 seq_printf(s, "is_console: %s\n", str_yes_no(is_console_port(port)));
1337 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
1338
1339 return 0;
1340 }
1341
1342 DEFINE_SHOW_ATTRIBUTE(port_debugfs);
1343
set_console_size(struct port * port,u16 rows,u16 cols)1344 static void set_console_size(struct port *port, u16 rows, u16 cols)
1345 {
1346 if (!port || !is_console_port(port))
1347 return;
1348
1349 port->cons.ws.ws_row = rows;
1350 port->cons.ws.ws_col = cols;
1351 }
1352
fill_queue(struct virtqueue * vq,spinlock_t * lock)1353 static int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1354 {
1355 struct port_buffer *buf;
1356 int nr_added_bufs;
1357 int ret;
1358
1359 nr_added_bufs = 0;
1360 do {
1361 buf = alloc_buf(vq->vdev, PAGE_SIZE, 0, GFP_KERNEL);
1362 if (!buf)
1363 return -ENOMEM;
1364
1365 spin_lock_irq(lock);
1366 ret = add_inbuf(vq, buf);
1367 if (ret < 0) {
1368 spin_unlock_irq(lock);
1369 free_buf(buf, true);
1370 return ret;
1371 }
1372 nr_added_bufs++;
1373 spin_unlock_irq(lock);
1374 } while (ret > 0);
1375
1376 return nr_added_bufs;
1377 }
1378
send_sigio_to_port(struct port * port)1379 static void send_sigio_to_port(struct port *port)
1380 {
1381 if (port->async_queue && port->guest_connected)
1382 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1383 }
1384
add_port(struct ports_device * portdev,u32 id)1385 static int add_port(struct ports_device *portdev, u32 id)
1386 {
1387 struct port *port;
1388 dev_t devt;
1389 int err;
1390
1391 port = kmalloc_obj(*port);
1392 if (!port) {
1393 err = -ENOMEM;
1394 goto fail;
1395 }
1396 kref_init(&port->kref);
1397
1398 port->portdev = portdev;
1399 port->id = id;
1400
1401 port->name = NULL;
1402 port->inbuf = NULL;
1403 port->cons.hvc = NULL;
1404 port->async_queue = NULL;
1405
1406 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1407 port->cons.vtermno = 0;
1408
1409 port->host_connected = port->guest_connected = false;
1410 port->stats = (struct port_stats) { 0 };
1411
1412 port->outvq_full = false;
1413
1414 port->in_vq = portdev->in_vqs[port->id];
1415 port->out_vq = portdev->out_vqs[port->id];
1416
1417 port->cdev = cdev_alloc();
1418 if (!port->cdev) {
1419 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1420 err = -ENOMEM;
1421 goto free_port;
1422 }
1423 port->cdev->ops = &port_fops;
1424
1425 devt = MKDEV(portdev->chr_major, id);
1426 err = cdev_add(port->cdev, devt, 1);
1427 if (err < 0) {
1428 dev_err(&port->portdev->vdev->dev,
1429 "Error %d adding cdev for port %u\n", err, id);
1430 goto free_cdev;
1431 }
1432 port->dev = device_create(&port_class, &port->portdev->vdev->dev,
1433 devt, port, "vport%up%u",
1434 port->portdev->vdev->index, id);
1435 if (IS_ERR(port->dev)) {
1436 err = PTR_ERR(port->dev);
1437 dev_err(&port->portdev->vdev->dev,
1438 "Error %d creating device for port %u\n",
1439 err, id);
1440 goto free_cdev;
1441 }
1442
1443 spin_lock_init(&port->inbuf_lock);
1444 spin_lock_init(&port->outvq_lock);
1445 init_waitqueue_head(&port->waitqueue);
1446
1447 /* We can safely ignore ENOSPC because it means
1448 * the queue already has buffers. Buffers are removed
1449 * only by virtcons_remove(), not by unplug_port()
1450 */
1451 err = fill_queue(port->in_vq, &port->inbuf_lock);
1452 if (err < 0 && err != -ENOSPC) {
1453 dev_err(port->dev, "Error allocating inbufs\n");
1454 goto free_device;
1455 }
1456
1457 if (is_rproc_serial(port->portdev->vdev))
1458 /*
1459 * For rproc_serial assume remote processor is connected.
1460 * rproc_serial does not want the console port, only
1461 * the generic port implementation.
1462 */
1463 port->host_connected = true;
1464 else if (!use_multiport(port->portdev)) {
1465 /*
1466 * If we're not using multiport support,
1467 * this has to be a console port.
1468 */
1469 err = init_port_console(port);
1470 if (err)
1471 goto free_inbufs;
1472 }
1473
1474 spin_lock_irq(&portdev->ports_lock);
1475 list_add_tail(&port->list, &port->portdev->ports);
1476 spin_unlock_irq(&portdev->ports_lock);
1477
1478 /*
1479 * Tell the Host we're set so that it can send us various
1480 * configuration parameters for this port (eg, port name,
1481 * caching, whether this is a console port, etc.)
1482 */
1483 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1484
1485 /*
1486 * Finally, create the debugfs file that we can use to
1487 * inspect a port's state at any time
1488 */
1489 port->debugfs_file = debugfs_create_file(dev_name(port->dev), 0444,
1490 pdrvdata.debugfs_dir,
1491 port, &port_debugfs_fops);
1492 return 0;
1493
1494 free_inbufs:
1495 free_device:
1496 device_destroy(&port_class, port->dev->devt);
1497 free_cdev:
1498 cdev_del(port->cdev);
1499 free_port:
1500 kfree(port);
1501 fail:
1502 /* The host might want to notify management sw about port add failure */
1503 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1504 return err;
1505 }
1506
1507 /* No users remain, remove all port-specific data. */
remove_port(struct kref * kref)1508 static void remove_port(struct kref *kref)
1509 {
1510 struct port *port;
1511
1512 port = container_of(kref, struct port, kref);
1513
1514 kfree(port);
1515 }
1516
remove_port_data(struct port * port)1517 static void remove_port_data(struct port *port)
1518 {
1519 spin_lock_irq(&port->inbuf_lock);
1520 /* Remove unused data this port might have received. */
1521 discard_port_data(port);
1522 spin_unlock_irq(&port->inbuf_lock);
1523
1524 spin_lock_irq(&port->outvq_lock);
1525 reclaim_consumed_buffers(port);
1526 spin_unlock_irq(&port->outvq_lock);
1527 }
1528
1529 /*
1530 * Port got unplugged. Remove port from portdev's list and drop the
1531 * kref reference. If no userspace has this port opened, it will
1532 * result in immediate removal the port.
1533 */
unplug_port(struct port * port)1534 static void unplug_port(struct port *port)
1535 {
1536 spin_lock_irq(&port->portdev->ports_lock);
1537 list_del(&port->list);
1538 spin_unlock_irq(&port->portdev->ports_lock);
1539
1540 spin_lock_irq(&port->inbuf_lock);
1541 if (port->guest_connected) {
1542 /* Let the app know the port is going down. */
1543 send_sigio_to_port(port);
1544
1545 /* Do this after sigio is actually sent */
1546 port->guest_connected = false;
1547 port->host_connected = false;
1548
1549 wake_up_interruptible(&port->waitqueue);
1550 }
1551 spin_unlock_irq(&port->inbuf_lock);
1552
1553 if (is_console_port(port)) {
1554 spin_lock_irq(&pdrvdata_lock);
1555 list_del(&port->cons.list);
1556 spin_unlock_irq(&pdrvdata_lock);
1557 hvc_remove(port->cons.hvc);
1558 ida_free(&vtermno_ida, port->cons.vtermno);
1559 }
1560
1561 remove_port_data(port);
1562
1563 /*
1564 * Null out portdev under outvq_lock so that __send_to_port()
1565 * cannot race: it checks port->portdev inside the same lock
1566 * and bails out if NULL, preventing any buffer from being
1567 * enqueued to an already torn-down virtqueue. Also prevents
1568 * a close on an open port later from sending a stale control
1569 * message.
1570 */
1571 spin_lock_irq(&port->outvq_lock);
1572 port->portdev = NULL;
1573 spin_unlock_irq(&port->outvq_lock);
1574
1575 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1576 device_destroy(&port_class, port->dev->devt);
1577 cdev_del(port->cdev);
1578
1579 debugfs_remove(port->debugfs_file);
1580 kfree(port->name);
1581
1582 /*
1583 * Locks around here are not necessary - a port can't be
1584 * opened after we removed the port struct from ports_list
1585 * above.
1586 */
1587 kref_put(&port->kref, remove_port);
1588 }
1589
1590 /* Any private messages that the Host and Guest want to share */
handle_control_message(struct virtio_device * vdev,struct ports_device * portdev,struct port_buffer * buf)1591 static void handle_control_message(struct virtio_device *vdev,
1592 struct ports_device *portdev,
1593 struct port_buffer *buf)
1594 {
1595 struct virtio_console_control *cpkt;
1596 struct port *port;
1597 size_t name_size;
1598 int err;
1599
1600 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1601
1602 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
1603 if (!port &&
1604 cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) {
1605 /* No valid header at start of buffer. Drop it. */
1606 dev_dbg(&portdev->vdev->dev,
1607 "Invalid index %u in control packet\n",
1608 virtio32_to_cpu(vdev, cpkt->id));
1609 return;
1610 }
1611
1612 switch (virtio16_to_cpu(vdev, cpkt->event)) {
1613 case VIRTIO_CONSOLE_PORT_ADD:
1614 if (port) {
1615 dev_dbg(&portdev->vdev->dev,
1616 "Port %u already added\n", port->id);
1617 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1618 break;
1619 }
1620 if (virtio32_to_cpu(vdev, cpkt->id) >=
1621 portdev->max_nr_ports) {
1622 dev_warn(&portdev->vdev->dev,
1623 "Request for adding port with "
1624 "out-of-bound id %u, max. supported id: %u\n",
1625 virtio32_to_cpu(vdev, cpkt->id),
1626 portdev->max_nr_ports - 1);
1627 break;
1628 }
1629 add_port(portdev, virtio32_to_cpu(vdev, cpkt->id));
1630 break;
1631 case VIRTIO_CONSOLE_PORT_REMOVE:
1632 unplug_port(port);
1633 break;
1634 case VIRTIO_CONSOLE_CONSOLE_PORT:
1635 if (!cpkt->value)
1636 break;
1637 if (is_console_port(port))
1638 break;
1639
1640 init_port_console(port);
1641 complete(&early_console_added);
1642 /*
1643 * Could remove the port here in case init fails - but
1644 * have to notify the host first.
1645 */
1646 break;
1647 case VIRTIO_CONSOLE_RESIZE: {
1648 struct {
1649 __virtio16 cols;
1650 __virtio16 rows;
1651 } size;
1652
1653 if (!is_console_port(port))
1654 break;
1655
1656 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1657 sizeof(size));
1658 set_console_size(port, virtio16_to_cpu(vdev, size.rows),
1659 virtio16_to_cpu(vdev, size.cols));
1660
1661 port->cons.hvc->irq_requested = 1;
1662 resize_console(port);
1663 break;
1664 }
1665 case VIRTIO_CONSOLE_PORT_OPEN:
1666 port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
1667 wake_up_interruptible(&port->waitqueue);
1668 /*
1669 * If the host port got closed and the host had any
1670 * unconsumed buffers, we'll be able to reclaim them
1671 * now.
1672 */
1673 spin_lock_irq(&port->outvq_lock);
1674 reclaim_consumed_buffers(port);
1675 spin_unlock_irq(&port->outvq_lock);
1676
1677 /*
1678 * If the guest is connected, it'll be interested in
1679 * knowing the host connection state changed.
1680 */
1681 spin_lock_irq(&port->inbuf_lock);
1682 send_sigio_to_port(port);
1683 spin_unlock_irq(&port->inbuf_lock);
1684 break;
1685 case VIRTIO_CONSOLE_PORT_NAME:
1686 /*
1687 * If we woke up after hibernation, we can get this
1688 * again. Skip it in that case.
1689 */
1690 if (port->name)
1691 break;
1692
1693 /*
1694 * Skip the size of the header and the cpkt to get the size
1695 * of the name that was sent
1696 */
1697 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1698
1699 port->name = kmalloc(name_size, GFP_KERNEL);
1700 if (!port->name) {
1701 dev_err(port->dev,
1702 "Not enough space to store port name\n");
1703 break;
1704 }
1705 strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1706 name_size);
1707
1708 /*
1709 * Since we only have one sysfs attribute, 'name',
1710 * create it only if we have a name for the port.
1711 */
1712 err = sysfs_create_group(&port->dev->kobj,
1713 &port_attribute_group);
1714 if (err) {
1715 dev_err(port->dev,
1716 "Error %d creating sysfs device attributes\n",
1717 err);
1718 } else {
1719 /*
1720 * Generate a udev event so that appropriate
1721 * symlinks can be created based on udev
1722 * rules.
1723 */
1724 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1725 }
1726 break;
1727 }
1728 }
1729
control_work_handler(struct work_struct * work)1730 static void control_work_handler(struct work_struct *work)
1731 {
1732 struct ports_device *portdev;
1733 struct virtqueue *vq;
1734 struct port_buffer *buf;
1735 unsigned int len;
1736
1737 portdev = container_of(work, struct ports_device, control_work);
1738 vq = portdev->c_ivq;
1739
1740 spin_lock(&portdev->c_ivq_lock);
1741 while ((buf = virtqueue_get_buf(vq, &len))) {
1742 spin_unlock(&portdev->c_ivq_lock);
1743
1744 buf->len = min_t(size_t, len, buf->size);
1745 buf->offset = 0;
1746
1747 handle_control_message(vq->vdev, portdev, buf);
1748
1749 spin_lock(&portdev->c_ivq_lock);
1750 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1751 dev_warn(&portdev->vdev->dev,
1752 "Error adding buffer to queue\n");
1753 free_buf(buf, false);
1754 }
1755 }
1756 spin_unlock(&portdev->c_ivq_lock);
1757 }
1758
flush_bufs(struct virtqueue * vq,bool can_sleep)1759 static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1760 {
1761 struct port_buffer *buf;
1762 unsigned int len;
1763
1764 while ((buf = virtqueue_get_buf(vq, &len)))
1765 free_buf(buf, can_sleep);
1766 }
1767
out_intr(struct virtqueue * vq)1768 static void out_intr(struct virtqueue *vq)
1769 {
1770 struct port *port;
1771
1772 port = find_port_by_vq(vq->vdev->priv, vq);
1773 if (!port) {
1774 flush_bufs(vq, false);
1775 return;
1776 }
1777
1778 wake_up_interruptible(&port->waitqueue);
1779 kref_put(&port->kref, remove_port);
1780 }
1781
in_intr(struct virtqueue * vq)1782 static void in_intr(struct virtqueue *vq)
1783 {
1784 struct port *port;
1785 unsigned long flags;
1786
1787 port = find_port_by_vq(vq->vdev->priv, vq);
1788 if (!port) {
1789 flush_bufs(vq, false);
1790 return;
1791 }
1792
1793 spin_lock_irqsave(&port->inbuf_lock, flags);
1794 port->inbuf = get_inbuf(port);
1795
1796 /*
1797 * Normally the port should not accept data when the port is
1798 * closed. For generic serial ports, the host won't (shouldn't)
1799 * send data till the guest is connected. But this condition
1800 * can be reached when a console port is not yet connected (no
1801 * tty is spawned) and the other side sends out data over the
1802 * vring, or when a remote devices start sending data before
1803 * the ports are opened.
1804 *
1805 * A generic serial port will discard data if not connected,
1806 * while console ports and rproc-serial ports accepts data at
1807 * any time. rproc-serial is initiated with guest_connected to
1808 * false because port_fops_open expects this. Console ports are
1809 * hooked up with an HVC console and is initialized with
1810 * guest_connected to true.
1811 */
1812
1813 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
1814 discard_port_data(port);
1815
1816 /* Send a SIGIO indicating new data in case the process asked for it */
1817 send_sigio_to_port(port);
1818
1819 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1820
1821 wake_up_interruptible(&port->waitqueue);
1822
1823 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1824 hvc_kick();
1825
1826 kref_put(&port->kref, remove_port);
1827 }
1828
control_intr(struct virtqueue * vq)1829 static void control_intr(struct virtqueue *vq)
1830 {
1831 struct ports_device *portdev;
1832
1833 portdev = vq->vdev->priv;
1834 schedule_work(&portdev->control_work);
1835 }
1836
config_intr(struct virtio_device * vdev)1837 static void config_intr(struct virtio_device *vdev)
1838 {
1839 struct ports_device *portdev;
1840
1841 portdev = vdev->priv;
1842
1843 if (!use_multiport(portdev))
1844 schedule_work(&portdev->config_work);
1845 }
1846
update_size_from_config(struct ports_device * portdev)1847 static void update_size_from_config(struct ports_device *portdev)
1848 {
1849 struct virtio_device *vdev;
1850 struct port *port;
1851 u16 rows, cols;
1852
1853 vdev = portdev->vdev;
1854
1855 /*
1856 * We'll use this way of resizing only for legacy support.
1857 * For multiport devices, use control messages to indicate
1858 * console size changes so that it can be done per-port.
1859 *
1860 * Don't test F_SIZE at all if we're rproc: not a valid feature.
1861 */
1862 if (is_rproc_serial(vdev) ||
1863 use_multiport(portdev) ||
1864 !virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1865 return;
1866
1867 virtio_cread(vdev, struct virtio_console_config, cols, &cols);
1868 virtio_cread(vdev, struct virtio_console_config, rows, &rows);
1869
1870 port = find_port_by_id(portdev, 0);
1871 set_console_size(port, rows, cols);
1872 resize_console(port);
1873 }
1874
config_work_handler(struct work_struct * work)1875 static void config_work_handler(struct work_struct *work)
1876 {
1877 struct ports_device *portdev;
1878
1879 portdev = container_of(work, struct ports_device, config_work);
1880 update_size_from_config(portdev);
1881 }
1882
init_vqs(struct ports_device * portdev)1883 static int init_vqs(struct ports_device *portdev)
1884 {
1885 struct virtqueue_info *vqs_info;
1886 struct virtqueue **vqs;
1887 u32 i, j, nr_ports, nr_queues;
1888 int err;
1889
1890 nr_ports = portdev->max_nr_ports;
1891 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1892
1893 vqs = kmalloc_objs(struct virtqueue *, nr_queues);
1894 vqs_info = kzalloc_objs(*vqs_info, nr_queues);
1895 portdev->in_vqs = kmalloc_objs(struct virtqueue *, nr_ports);
1896 portdev->out_vqs = kmalloc_objs(struct virtqueue *, nr_ports);
1897 if (!vqs || !vqs_info || !portdev->in_vqs || !portdev->out_vqs) {
1898 err = -ENOMEM;
1899 goto free;
1900 }
1901
1902 /*
1903 * For backward compat (newer host but older guest), the host
1904 * spawns a console port first and also inits the vqs for port
1905 * 0 before others.
1906 */
1907 j = 0;
1908 vqs_info[j].callback = in_intr;
1909 vqs_info[j + 1].callback = out_intr;
1910 vqs_info[j].name = "input";
1911 vqs_info[j + 1].name = "output";
1912 j += 2;
1913
1914 if (use_multiport(portdev)) {
1915 vqs_info[j].callback = control_intr;
1916 vqs_info[j].name = "control-i";
1917 vqs_info[j + 1].name = "control-o";
1918
1919 for (i = 1; i < nr_ports; i++) {
1920 j += 2;
1921 vqs_info[j].callback = in_intr;
1922 vqs_info[j + 1].callback = out_intr;
1923 vqs_info[j].name = "input";
1924 vqs_info[j + 1].name = "output";
1925 }
1926 }
1927 /* Find the queues. */
1928 err = virtio_find_vqs(portdev->vdev, nr_queues, vqs, vqs_info, NULL);
1929 if (err)
1930 goto free;
1931
1932 j = 0;
1933 portdev->in_vqs[0] = vqs[0];
1934 portdev->out_vqs[0] = vqs[1];
1935 j += 2;
1936 if (use_multiport(portdev)) {
1937 portdev->c_ivq = vqs[j];
1938 portdev->c_ovq = vqs[j + 1];
1939
1940 for (i = 1; i < nr_ports; i++) {
1941 j += 2;
1942 portdev->in_vqs[i] = vqs[j];
1943 portdev->out_vqs[i] = vqs[j + 1];
1944 }
1945 }
1946 kfree(vqs_info);
1947 kfree(vqs);
1948
1949 return 0;
1950
1951 free:
1952 kfree(portdev->out_vqs);
1953 kfree(portdev->in_vqs);
1954 kfree(vqs_info);
1955 kfree(vqs);
1956
1957 return err;
1958 }
1959
1960 static const struct file_operations portdev_fops = {
1961 .owner = THIS_MODULE,
1962 };
1963
remove_vqs(struct ports_device * portdev)1964 static void remove_vqs(struct ports_device *portdev)
1965 {
1966 struct virtqueue *vq;
1967
1968 virtio_device_for_each_vq(portdev->vdev, vq) {
1969 struct port_buffer *buf;
1970
1971 flush_bufs(vq, true);
1972 while ((buf = virtqueue_detach_unused_buf(vq)))
1973 free_buf(buf, true);
1974 cond_resched();
1975 }
1976 portdev->vdev->config->del_vqs(portdev->vdev);
1977 kfree(portdev->in_vqs);
1978 kfree(portdev->out_vqs);
1979 }
1980
virtcons_remove(struct virtio_device * vdev)1981 static void virtcons_remove(struct virtio_device *vdev)
1982 {
1983 struct ports_device *portdev;
1984 struct port *port, *port2;
1985
1986 portdev = vdev->priv;
1987
1988 spin_lock_irq(&pdrvdata_lock);
1989 list_del(&portdev->list);
1990 spin_unlock_irq(&pdrvdata_lock);
1991
1992 /* Device is going away, exit any polling for buffers */
1993 virtio_break_device(vdev);
1994 if (use_multiport(portdev))
1995 flush_work(&portdev->control_work);
1996 else
1997 flush_work(&portdev->config_work);
1998
1999 /* Disable interrupts for vqs */
2000 virtio_reset_device(vdev);
2001 /* Finish up work that's lined up */
2002 if (use_multiport(portdev))
2003 cancel_work_sync(&portdev->control_work);
2004 else
2005 cancel_work_sync(&portdev->config_work);
2006
2007 list_for_each_entry_safe(port, port2, &portdev->ports, list)
2008 unplug_port(port);
2009
2010 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
2011
2012 /*
2013 * When yanking out a device, we immediately lose the
2014 * (device-side) queues. So there's no point in keeping the
2015 * guest side around till we drop our final reference. This
2016 * also means that any ports which are in an open state will
2017 * have to just stop using the port, as the vqs are going
2018 * away.
2019 */
2020 remove_vqs(portdev);
2021 kfree(portdev);
2022 }
2023
2024 /*
2025 * Once we're further in boot, we get probed like any other virtio
2026 * device.
2027 *
2028 * If the host also supports multiple console ports, we check the
2029 * config space to see how many ports the host has spawned. We
2030 * initialize each port found.
2031 */
virtcons_probe(struct virtio_device * vdev)2032 static int virtcons_probe(struct virtio_device *vdev)
2033 {
2034 struct ports_device *portdev;
2035 int err;
2036 bool multiport;
2037
2038 /* We only need a config space if features are offered */
2039 if (!vdev->config->get &&
2040 (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)
2041 || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) {
2042 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2043 __func__);
2044 return -EINVAL;
2045 }
2046
2047 portdev = kmalloc_obj(*portdev);
2048 if (!portdev) {
2049 err = -ENOMEM;
2050 goto fail;
2051 }
2052
2053 /* Attach this portdev to this virtio_device, and vice-versa. */
2054 portdev->vdev = vdev;
2055 vdev->priv = portdev;
2056 portdev->pm_freezing = false;
2057
2058 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
2059 &portdev_fops);
2060 if (portdev->chr_major < 0) {
2061 dev_err(&vdev->dev,
2062 "Error %d registering chrdev for device %u\n",
2063 portdev->chr_major, vdev->index);
2064 err = portdev->chr_major;
2065 goto free;
2066 }
2067
2068 multiport = false;
2069 portdev->max_nr_ports = 1;
2070
2071 /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
2072 if (!is_rproc_serial(vdev) &&
2073 virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
2074 struct virtio_console_config, max_nr_ports,
2075 &portdev->max_nr_ports) == 0) {
2076 if (portdev->max_nr_ports == 0 ||
2077 portdev->max_nr_ports > VIRTCONS_MAX_PORTS) {
2078 dev_err(&vdev->dev,
2079 "Invalidate max_nr_ports %d",
2080 portdev->max_nr_ports);
2081 err = -EINVAL;
2082 goto free;
2083 }
2084 multiport = true;
2085 }
2086
2087 spin_lock_init(&portdev->ports_lock);
2088 INIT_LIST_HEAD(&portdev->ports);
2089 INIT_LIST_HEAD(&portdev->list);
2090
2091 INIT_WORK(&portdev->config_work, &config_work_handler);
2092 INIT_WORK(&portdev->control_work, &control_work_handler);
2093
2094 if (multiport) {
2095 spin_lock_init(&portdev->c_ivq_lock);
2096 spin_lock_init(&portdev->c_ovq_lock);
2097 }
2098
2099 err = init_vqs(portdev);
2100 if (err < 0) {
2101 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
2102 goto free_chrdev;
2103 }
2104
2105 virtio_device_ready(portdev->vdev);
2106
2107 if (multiport) {
2108 err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2109 if (err < 0) {
2110 dev_err(&vdev->dev,
2111 "Error allocating buffers for control queue\n");
2112 /*
2113 * The host might want to notify mgmt sw about device
2114 * add failure.
2115 */
2116 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2117 VIRTIO_CONSOLE_DEVICE_READY, 0);
2118 /* Device was functional: we need full cleanup. */
2119 virtcons_remove(vdev);
2120 return err;
2121 }
2122 } else {
2123 /*
2124 * For backward compatibility: Create a console port
2125 * if we're running on older host.
2126 */
2127 add_port(portdev, 0);
2128 }
2129
2130 spin_lock_irq(&pdrvdata_lock);
2131 list_add_tail(&portdev->list, &pdrvdata.portdevs);
2132 spin_unlock_irq(&pdrvdata_lock);
2133
2134 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2135 VIRTIO_CONSOLE_DEVICE_READY, 1);
2136
2137 update_size_from_config(portdev);
2138
2139 return 0;
2140
2141 free_chrdev:
2142 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
2143 free:
2144 kfree(portdev);
2145 fail:
2146 return err;
2147 }
2148
2149 static const struct virtio_device_id id_table[] = {
2150 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
2151 { 0 },
2152 };
2153 MODULE_DEVICE_TABLE(virtio, id_table);
2154
2155 static const unsigned int features[] = {
2156 VIRTIO_CONSOLE_F_SIZE,
2157 VIRTIO_CONSOLE_F_MULTIPORT,
2158 };
2159
2160 static const struct virtio_device_id rproc_serial_id_table[] = {
2161 #if IS_ENABLED(CONFIG_REMOTEPROC)
2162 { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
2163 #endif
2164 { 0 },
2165 };
2166 MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table);
2167
2168 static const unsigned int rproc_serial_features[] = {
2169 };
2170
2171 #ifdef CONFIG_PM_SLEEP
virtcons_freeze(struct virtio_device * vdev)2172 static int virtcons_freeze(struct virtio_device *vdev)
2173 {
2174 struct ports_device *portdev;
2175 struct port *port;
2176 unsigned long flags;
2177
2178 portdev = vdev->priv;
2179
2180 /*
2181 * Block TX paths (put_chars, __send_to_port) before resetting the
2182 * device and tearing down virtqueues. This prevents races with
2183 * hvc console writes that remain active under no_console_suspend.
2184 */
2185 smp_store_release(&portdev->pm_freezing, true);
2186
2187 /*
2188 * Synchronize with any concurrent __send_to_port() that may have
2189 * passed the pm_freezing check. By acquiring and releasing the
2190 * outvq_lock for each port, we ensure all active TX paths have
2191 * completed before we reset the device.
2192 */
2193 spin_lock_irqsave(&portdev->ports_lock, flags);
2194 list_for_each_entry(port, &portdev->ports, list) {
2195 spin_lock(&port->outvq_lock);
2196 spin_unlock(&port->outvq_lock);
2197 }
2198 spin_unlock_irqrestore(&portdev->ports_lock, flags);
2199
2200 virtio_reset_device(vdev);
2201
2202 if (use_multiport(portdev))
2203 virtqueue_disable_cb(portdev->c_ivq);
2204 cancel_work_sync(&portdev->control_work);
2205 cancel_work_sync(&portdev->config_work);
2206 /*
2207 * Once more: if control_work_handler() was running, it would
2208 * enable the cb as the last step.
2209 */
2210 if (use_multiport(portdev))
2211 virtqueue_disable_cb(portdev->c_ivq);
2212
2213 list_for_each_entry(port, &portdev->ports, list) {
2214 virtqueue_disable_cb(port->in_vq);
2215 virtqueue_disable_cb(port->out_vq);
2216 /*
2217 * We'll ask the host later if the new invocation has
2218 * the port opened or closed.
2219 */
2220 port->host_connected = false;
2221 remove_port_data(port);
2222 }
2223 remove_vqs(portdev);
2224
2225 return 0;
2226 }
2227
virtcons_restore(struct virtio_device * vdev)2228 static int virtcons_restore(struct virtio_device *vdev)
2229 {
2230 struct ports_device *portdev;
2231 struct port *port;
2232 int ret;
2233
2234 portdev = vdev->priv;
2235
2236 ret = init_vqs(portdev);
2237 if (ret)
2238 return ret;
2239
2240 virtio_device_ready(portdev->vdev);
2241
2242 list_for_each_entry(port, &portdev->ports, list) {
2243 port->in_vq = portdev->in_vqs[port->id];
2244 port->out_vq = portdev->out_vqs[port->id];
2245
2246 fill_queue(port->in_vq, &port->inbuf_lock);
2247
2248 /* Get port open/close status on the host */
2249 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2250
2251 /*
2252 * If a port was open at the time of suspending, we
2253 * have to let the host know that it's still open.
2254 */
2255 if (port->guest_connected)
2256 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
2257 }
2258
2259 /*
2260 * Populate the control receive queue only after the list iteration
2261 * is complete. If we fill this queue before iterating, the host could
2262 * immediately deliver a VIRTIO_CONSOLE_PORT_REMOVE message.
2263 * This would trigger the control workqueue, which modifies the
2264 * portdev->ports list concurrently with the unprotected loop above,
2265 * leading to a Use-After-Free and list corruption.
2266 */
2267 if (use_multiport(portdev))
2268 fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2269
2270 /*
2271 * Allow TX paths only after all port->out_vq pointers have
2272 * been reassigned to the newly allocated virtqueues.
2273 */
2274 smp_store_release(&portdev->pm_freezing, false);
2275
2276 return 0;
2277 }
2278 #endif
2279
2280 static struct virtio_driver virtio_console = {
2281 .feature_table = features,
2282 .feature_table_size = ARRAY_SIZE(features),
2283 .driver.name = KBUILD_MODNAME,
2284 .id_table = id_table,
2285 .probe = virtcons_probe,
2286 .remove = virtcons_remove,
2287 .config_changed = config_intr,
2288 #ifdef CONFIG_PM_SLEEP
2289 .freeze = virtcons_freeze,
2290 .restore = virtcons_restore,
2291 #endif
2292 };
2293
2294 static struct virtio_driver virtio_rproc_serial = {
2295 .feature_table = rproc_serial_features,
2296 .feature_table_size = ARRAY_SIZE(rproc_serial_features),
2297 .driver.name = "virtio_rproc_serial",
2298 .id_table = rproc_serial_id_table,
2299 .probe = virtcons_probe,
2300 .remove = virtcons_remove,
2301 };
2302
virtio_console_init(void)2303 static int __init virtio_console_init(void)
2304 {
2305 int err;
2306
2307 err = class_register(&port_class);
2308 if (err)
2309 return err;
2310
2311 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
2312 INIT_LIST_HEAD(&pdrvdata.consoles);
2313 INIT_LIST_HEAD(&pdrvdata.portdevs);
2314
2315 err = register_virtio_driver(&virtio_console);
2316 if (err < 0) {
2317 pr_err("Error %d registering virtio driver\n", err);
2318 goto free;
2319 }
2320 err = register_virtio_driver(&virtio_rproc_serial);
2321 if (err < 0) {
2322 pr_err("Error %d registering virtio rproc serial driver\n",
2323 err);
2324 goto unregister;
2325 }
2326 return 0;
2327 unregister:
2328 unregister_virtio_driver(&virtio_console);
2329 free:
2330 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2331 class_unregister(&port_class);
2332 return err;
2333 }
2334
virtio_console_fini(void)2335 static void __exit virtio_console_fini(void)
2336 {
2337 reclaim_dma_bufs();
2338
2339 unregister_virtio_driver(&virtio_console);
2340 unregister_virtio_driver(&virtio_rproc_serial);
2341
2342 class_unregister(&port_class);
2343 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2344 }
2345 module_init(virtio_console_init);
2346 module_exit(virtio_console_fini);
2347
2348 MODULE_DESCRIPTION("Virtio console driver");
2349 MODULE_LICENSE("GPL");
2350