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 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 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 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 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 */ 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 337 static bool is_console_port(struct port *port) 338 { 339 if (port->cons.hvc) 340 return true; 341 return false; 342 } 343 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 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 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 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 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 */ 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 */ 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. */ 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 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 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 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 */ 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 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 */ 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 */ 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 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 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 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 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 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 struct page *page; 895 char *src; 896 897 if (sgl->n == sgl->size) 898 return 0; 899 900 page = alloc_page(GFP_KERNEL); 901 if (!page) 902 return -ENOMEM; 903 904 offset = sd->pos & ~PAGE_MASK; 905 906 len = sd->len; 907 if (len + offset > PAGE_SIZE) 908 len = PAGE_SIZE - offset; 909 910 src = kmap_local_page(buf->page); 911 memcpy(page_address(page) + offset, src + buf->offset, len); 912 kunmap_local(src); 913 914 sg_set_page(&sgl->sg[sgl->n], page, len, offset); 915 sgl->n++; 916 sgl->len += len; 917 918 return len; 919 } 920 921 /* Write by splicing, copying the data from the pipe */ 922 static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe, 923 struct file *filp, loff_t *ppos, 924 size_t len, unsigned int flags) 925 { 926 struct port *port = filp->private_data; 927 struct sg_list sgl; 928 ssize_t ret; 929 struct port_buffer *buf; 930 struct splice_desc sd = { 931 .total_len = len, 932 .flags = flags, 933 .pos = *ppos, 934 .u.data = &sgl, 935 }; 936 unsigned int occupancy; 937 938 /* 939 * Rproc_serial does not yet support splice. To support splice 940 * pipe_to_sg() must allocate dma-buffers and copy content from 941 * regular pages to dma pages. And alloc_buf and free_buf must 942 * support allocating and freeing such a list of dma-buffers. 943 */ 944 if (is_rproc_serial(port->out_vq->vdev)) 945 return -EINVAL; 946 947 pipe_lock(pipe); 948 ret = 0; 949 if (pipe_is_empty(pipe)) 950 goto error_out; 951 952 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK); 953 if (ret < 0) 954 goto error_out; 955 956 occupancy = pipe_buf_usage(pipe); 957 buf = alloc_buf(port->portdev->vdev, 0, occupancy, GFP_KERNEL); 958 959 if (!buf) { 960 ret = -ENOMEM; 961 goto error_out; 962 } 963 964 sgl.n = 0; 965 sgl.len = 0; 966 sgl.size = occupancy; 967 sgl.sg = buf->sg; 968 sg_init_table(sgl.sg, sgl.size); 969 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg); 970 pipe_unlock(pipe); 971 972 if (likely(ret > 0)) 973 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true); 974 else 975 free_buf(buf, true); 976 977 return ret; 978 979 error_out: 980 pipe_unlock(pipe); 981 return ret; 982 } 983 984 static __poll_t port_fops_poll(struct file *filp, poll_table *wait) 985 { 986 struct port *port; 987 __poll_t ret; 988 989 port = filp->private_data; 990 poll_wait(filp, &port->waitqueue, wait); 991 992 if (!port->guest_connected) { 993 /* Port got unplugged */ 994 return EPOLLHUP; 995 } 996 ret = 0; 997 if (!will_read_block(port)) 998 ret |= EPOLLIN | EPOLLRDNORM; 999 if (!will_write_block(port)) 1000 ret |= EPOLLOUT; 1001 if (!port->host_connected) 1002 ret |= EPOLLHUP; 1003 1004 return ret; 1005 } 1006 1007 static void remove_port(struct kref *kref); 1008 1009 static int port_fops_release(struct inode *inode, struct file *filp) 1010 { 1011 struct port *port; 1012 1013 port = filp->private_data; 1014 1015 /* Notify host of port being closed */ 1016 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0); 1017 1018 spin_lock_irq(&port->inbuf_lock); 1019 port->guest_connected = false; 1020 1021 discard_port_data(port); 1022 1023 spin_unlock_irq(&port->inbuf_lock); 1024 1025 spin_lock_irq(&port->outvq_lock); 1026 reclaim_consumed_buffers(port); 1027 spin_unlock_irq(&port->outvq_lock); 1028 1029 reclaim_dma_bufs(); 1030 /* 1031 * Locks aren't necessary here as a port can't be opened after 1032 * unplug, and if a port isn't unplugged, a kref would already 1033 * exist for the port. Plus, taking ports_lock here would 1034 * create a dependency on other locks taken by functions 1035 * inside remove_port if we're the last holder of the port, 1036 * creating many problems. 1037 */ 1038 kref_put(&port->kref, remove_port); 1039 1040 return 0; 1041 } 1042 1043 static int port_fops_open(struct inode *inode, struct file *filp) 1044 { 1045 struct cdev *cdev = inode->i_cdev; 1046 struct port *port; 1047 int ret; 1048 1049 /* We get the port with a kref here */ 1050 port = find_port_by_devt(cdev->dev); 1051 if (!port) { 1052 /* Port was unplugged before we could proceed */ 1053 return -ENXIO; 1054 } 1055 filp->private_data = port; 1056 1057 /* 1058 * Don't allow opening of console port devices -- that's done 1059 * via /dev/hvc 1060 */ 1061 if (is_console_port(port)) { 1062 ret = -ENXIO; 1063 goto out; 1064 } 1065 1066 /* Allow only one process to open a particular port at a time */ 1067 spin_lock_irq(&port->inbuf_lock); 1068 if (port->guest_connected) { 1069 spin_unlock_irq(&port->inbuf_lock); 1070 ret = -EBUSY; 1071 goto out; 1072 } 1073 1074 port->guest_connected = true; 1075 spin_unlock_irq(&port->inbuf_lock); 1076 1077 spin_lock_irq(&port->outvq_lock); 1078 /* 1079 * There might be a chance that we missed reclaiming a few 1080 * buffers in the window of the port getting previously closed 1081 * and opening now. 1082 */ 1083 reclaim_consumed_buffers(port); 1084 spin_unlock_irq(&port->outvq_lock); 1085 1086 nonseekable_open(inode, filp); 1087 1088 /* Notify host of port being opened */ 1089 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1); 1090 1091 return 0; 1092 out: 1093 kref_put(&port->kref, remove_port); 1094 return ret; 1095 } 1096 1097 static int port_fops_fasync(int fd, struct file *filp, int mode) 1098 { 1099 struct port *port; 1100 1101 port = filp->private_data; 1102 return fasync_helper(fd, filp, mode, &port->async_queue); 1103 } 1104 1105 /* 1106 * The file operations that we support: programs in the guest can open 1107 * a console device, read from it, write to it, poll for data and 1108 * close it. The devices are at 1109 * /dev/vport<device number>p<port number> 1110 */ 1111 static const struct file_operations port_fops = { 1112 .owner = THIS_MODULE, 1113 .open = port_fops_open, 1114 .read = port_fops_read, 1115 .write = port_fops_write, 1116 .splice_write = port_fops_splice_write, 1117 .poll = port_fops_poll, 1118 .release = port_fops_release, 1119 .fasync = port_fops_fasync, 1120 }; 1121 1122 /* 1123 * The put_chars() callback is pretty straightforward. 1124 * 1125 * We turn the characters into a scatter-gather list, add it to the 1126 * output queue and then kick the Host. Then we sit here waiting for 1127 * it to finish: inefficient in theory, but in practice 1128 * implementations will do it immediately. 1129 */ 1130 static ssize_t put_chars(u32 vtermno, const u8 *buf, size_t count) 1131 { 1132 struct port *port; 1133 struct scatterlist sg[1]; 1134 struct port_buffer *pbuf; 1135 struct ports_device *portdev; 1136 1137 port = find_port_by_vtermno(vtermno); 1138 if (!port) 1139 return -EPIPE; 1140 1141 /* 1142 * Silently drop output in two cases, both by returning count so 1143 * that the hvc layer does not spin-retry: 1144 * 1145 * 1. Device hot-unplug (!portdev): portdev was NULLed by 1146 * unplug_port() after hvc_remove() was already called, so 1147 * the hvc layer will stop invoking put_chars() very soon. 1148 * Returning count avoids a pointless retry loop in the 1149 * interim. 1150 * 1151 * 2. PM freeze (pm_freezing): the hvc console stays active 1152 * under no_console_suspend but virtqueues are being torn 1153 * down. Drop the output silently so the hvc layer does not 1154 * stall suspend. 1155 * 1156 * This early check avoids a pointless GFP_ATOMIC allocation; 1157 * __send_to_port() rechecks under outvq_lock for correctness. 1158 * Pairs with smp_store_release() in virtcons_freeze/restore. 1159 */ 1160 portdev = READ_ONCE(port->portdev); 1161 if (!portdev || 1162 smp_load_acquire(&portdev->pm_freezing)) /* pairs with freeze/restore */ 1163 return count; 1164 1165 pbuf = alloc_buf(portdev->vdev, count, 0, GFP_ATOMIC); 1166 if (!pbuf) 1167 return -ENOMEM; 1168 1169 memcpy(pbuf->buf, buf, count); 1170 pbuf->len = count; 1171 sg_init_one(sg, pbuf->buf, count); 1172 1173 /* 1174 * Ownership of pbuf is transferred to __send_to_port(). 1175 * Do not touch or free pbuf after this call. 1176 */ 1177 return __send_to_port(port, sg, 1, count, pbuf, false); 1178 } 1179 1180 /* 1181 * get_chars() is the callback from the hvc_console infrastructure 1182 * when an interrupt is received. 1183 * 1184 * We call out to fill_readbuf that gets us the required data from the 1185 * buffers that are queued up. 1186 */ 1187 static ssize_t get_chars(u32 vtermno, u8 *buf, size_t count) 1188 { 1189 struct port *port; 1190 1191 port = find_port_by_vtermno(vtermno); 1192 if (!port) 1193 return -EPIPE; 1194 1195 /* If we don't have an input queue yet, we can't get input. */ 1196 BUG_ON(!port->in_vq); 1197 1198 return fill_readbuf(port, (__force u8 __user *)buf, count, false); 1199 } 1200 1201 static void resize_console(struct port *port) 1202 { 1203 struct virtio_device *vdev; 1204 1205 /* The port could have been hot-unplugged */ 1206 if (!port || !is_console_port(port)) 1207 return; 1208 1209 vdev = port->portdev->vdev; 1210 1211 /* Don't test F_SIZE at all if we're rproc: not a valid feature! */ 1212 if (!is_rproc_serial(vdev) && 1213 virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) 1214 hvc_resize(port->cons.hvc, port->cons.ws); 1215 } 1216 1217 /* We set the configuration at this point, since we now have a tty */ 1218 static int notifier_add_vio(struct hvc_struct *hp, int data) 1219 { 1220 struct port *port; 1221 1222 port = find_port_by_vtermno(hp->vtermno); 1223 if (!port) 1224 return -EINVAL; 1225 1226 hp->irq_requested = 1; 1227 resize_console(port); 1228 1229 return 0; 1230 } 1231 1232 static void notifier_del_vio(struct hvc_struct *hp, int data) 1233 { 1234 hp->irq_requested = 0; 1235 } 1236 1237 /* The operations for console ports. */ 1238 static const struct hv_ops hv_ops = { 1239 .get_chars = get_chars, 1240 .put_chars = put_chars, 1241 .notifier_add = notifier_add_vio, 1242 .notifier_del = notifier_del_vio, 1243 .notifier_hangup = notifier_del_vio, 1244 }; 1245 1246 static int init_port_console(struct port *port) 1247 { 1248 int ret; 1249 1250 /* 1251 * The Host's telling us this port is a console port. Hook it 1252 * up with an hvc console. 1253 * 1254 * To set up and manage our virtual console, we call 1255 * hvc_alloc(). 1256 * 1257 * The first argument of hvc_alloc() is the virtual console 1258 * number. The second argument is the parameter for the 1259 * notification mechanism (like irq number). We currently 1260 * leave this as zero, virtqueues have implicit notifications. 1261 * 1262 * The third argument is a "struct hv_ops" containing the 1263 * put_chars() get_chars(), notifier_add() and notifier_del() 1264 * pointers. The final argument is the output buffer size: we 1265 * can do any size, so we put PAGE_SIZE here. 1266 */ 1267 ret = ida_alloc_min(&vtermno_ida, 1, GFP_KERNEL); 1268 if (ret < 0) 1269 return ret; 1270 1271 port->cons.vtermno = ret; 1272 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE); 1273 if (IS_ERR(port->cons.hvc)) { 1274 ret = PTR_ERR(port->cons.hvc); 1275 dev_err(port->dev, 1276 "error %d allocating hvc for port\n", ret); 1277 port->cons.hvc = NULL; 1278 ida_free(&vtermno_ida, port->cons.vtermno); 1279 return ret; 1280 } 1281 spin_lock_irq(&pdrvdata_lock); 1282 list_add_tail(&port->cons.list, &pdrvdata.consoles); 1283 spin_unlock_irq(&pdrvdata_lock); 1284 port->guest_connected = true; 1285 1286 /* Notify host of port being opened */ 1287 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1); 1288 1289 return 0; 1290 } 1291 1292 static ssize_t show_port_name(struct device *dev, 1293 struct device_attribute *attr, char *buffer) 1294 { 1295 struct port *port; 1296 1297 port = dev_get_drvdata(dev); 1298 1299 return sprintf(buffer, "%s\n", port->name); 1300 } 1301 1302 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL); 1303 1304 static struct attribute *port_sysfs_entries[] = { 1305 &dev_attr_name.attr, 1306 NULL 1307 }; 1308 1309 static const struct attribute_group port_attribute_group = { 1310 .name = NULL, /* put in device directory */ 1311 .attrs = port_sysfs_entries, 1312 }; 1313 1314 static int port_debugfs_show(struct seq_file *s, void *data) 1315 { 1316 struct port *port = s->private; 1317 1318 seq_printf(s, "name: %s\n", port->name ? port->name : ""); 1319 seq_printf(s, "guest_connected: %d\n", port->guest_connected); 1320 seq_printf(s, "host_connected: %d\n", port->host_connected); 1321 seq_printf(s, "outvq_full: %d\n", port->outvq_full); 1322 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent); 1323 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received); 1324 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded); 1325 seq_printf(s, "is_console: %s\n", str_yes_no(is_console_port(port))); 1326 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno); 1327 1328 return 0; 1329 } 1330 1331 DEFINE_SHOW_ATTRIBUTE(port_debugfs); 1332 1333 static void set_console_size(struct port *port, u16 rows, u16 cols) 1334 { 1335 if (!port || !is_console_port(port)) 1336 return; 1337 1338 port->cons.ws.ws_row = rows; 1339 port->cons.ws.ws_col = cols; 1340 } 1341 1342 static int fill_queue(struct virtqueue *vq, spinlock_t *lock) 1343 { 1344 struct port_buffer *buf; 1345 int nr_added_bufs; 1346 int ret; 1347 1348 nr_added_bufs = 0; 1349 do { 1350 buf = alloc_buf(vq->vdev, PAGE_SIZE, 0, GFP_KERNEL); 1351 if (!buf) 1352 return -ENOMEM; 1353 1354 spin_lock_irq(lock); 1355 ret = add_inbuf(vq, buf); 1356 if (ret < 0) { 1357 spin_unlock_irq(lock); 1358 free_buf(buf, true); 1359 return ret; 1360 } 1361 nr_added_bufs++; 1362 spin_unlock_irq(lock); 1363 } while (ret > 0); 1364 1365 return nr_added_bufs; 1366 } 1367 1368 static void send_sigio_to_port(struct port *port) 1369 { 1370 if (port->async_queue && port->guest_connected) 1371 kill_fasync(&port->async_queue, SIGIO, POLL_OUT); 1372 } 1373 1374 static int add_port(struct ports_device *portdev, u32 id) 1375 { 1376 struct port *port; 1377 dev_t devt; 1378 int err; 1379 1380 port = kmalloc_obj(*port); 1381 if (!port) { 1382 err = -ENOMEM; 1383 goto fail; 1384 } 1385 kref_init(&port->kref); 1386 1387 port->portdev = portdev; 1388 port->id = id; 1389 1390 port->name = NULL; 1391 port->inbuf = NULL; 1392 port->cons.hvc = NULL; 1393 port->async_queue = NULL; 1394 1395 port->cons.ws.ws_row = port->cons.ws.ws_col = 0; 1396 port->cons.vtermno = 0; 1397 1398 port->host_connected = port->guest_connected = false; 1399 port->stats = (struct port_stats) { 0 }; 1400 1401 port->outvq_full = false; 1402 1403 port->in_vq = portdev->in_vqs[port->id]; 1404 port->out_vq = portdev->out_vqs[port->id]; 1405 1406 port->cdev = cdev_alloc(); 1407 if (!port->cdev) { 1408 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n"); 1409 err = -ENOMEM; 1410 goto free_port; 1411 } 1412 port->cdev->ops = &port_fops; 1413 1414 devt = MKDEV(portdev->chr_major, id); 1415 err = cdev_add(port->cdev, devt, 1); 1416 if (err < 0) { 1417 dev_err(&port->portdev->vdev->dev, 1418 "Error %d adding cdev for port %u\n", err, id); 1419 goto free_cdev; 1420 } 1421 port->dev = device_create(&port_class, &port->portdev->vdev->dev, 1422 devt, port, "vport%up%u", 1423 port->portdev->vdev->index, id); 1424 if (IS_ERR(port->dev)) { 1425 err = PTR_ERR(port->dev); 1426 dev_err(&port->portdev->vdev->dev, 1427 "Error %d creating device for port %u\n", 1428 err, id); 1429 goto free_cdev; 1430 } 1431 1432 spin_lock_init(&port->inbuf_lock); 1433 spin_lock_init(&port->outvq_lock); 1434 init_waitqueue_head(&port->waitqueue); 1435 1436 /* We can safely ignore ENOSPC because it means 1437 * the queue already has buffers. Buffers are removed 1438 * only by virtcons_remove(), not by unplug_port() 1439 */ 1440 err = fill_queue(port->in_vq, &port->inbuf_lock); 1441 if (err < 0 && err != -ENOSPC) { 1442 dev_err(port->dev, "Error allocating inbufs\n"); 1443 goto free_device; 1444 } 1445 1446 if (is_rproc_serial(port->portdev->vdev)) 1447 /* 1448 * For rproc_serial assume remote processor is connected. 1449 * rproc_serial does not want the console port, only 1450 * the generic port implementation. 1451 */ 1452 port->host_connected = true; 1453 else if (!use_multiport(port->portdev)) { 1454 /* 1455 * If we're not using multiport support, 1456 * this has to be a console port. 1457 */ 1458 err = init_port_console(port); 1459 if (err) 1460 goto free_inbufs; 1461 } 1462 1463 spin_lock_irq(&portdev->ports_lock); 1464 list_add_tail(&port->list, &port->portdev->ports); 1465 spin_unlock_irq(&portdev->ports_lock); 1466 1467 /* 1468 * Tell the Host we're set so that it can send us various 1469 * configuration parameters for this port (eg, port name, 1470 * caching, whether this is a console port, etc.) 1471 */ 1472 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1); 1473 1474 /* 1475 * Finally, create the debugfs file that we can use to 1476 * inspect a port's state at any time 1477 */ 1478 port->debugfs_file = debugfs_create_file(dev_name(port->dev), 0444, 1479 pdrvdata.debugfs_dir, 1480 port, &port_debugfs_fops); 1481 return 0; 1482 1483 free_inbufs: 1484 free_device: 1485 device_destroy(&port_class, port->dev->devt); 1486 free_cdev: 1487 cdev_del(port->cdev); 1488 free_port: 1489 kfree(port); 1490 fail: 1491 /* The host might want to notify management sw about port add failure */ 1492 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0); 1493 return err; 1494 } 1495 1496 /* No users remain, remove all port-specific data. */ 1497 static void remove_port(struct kref *kref) 1498 { 1499 struct port *port; 1500 1501 port = container_of(kref, struct port, kref); 1502 1503 kfree(port); 1504 } 1505 1506 static void remove_port_data(struct port *port) 1507 { 1508 spin_lock_irq(&port->inbuf_lock); 1509 /* Remove unused data this port might have received. */ 1510 discard_port_data(port); 1511 spin_unlock_irq(&port->inbuf_lock); 1512 1513 spin_lock_irq(&port->outvq_lock); 1514 reclaim_consumed_buffers(port); 1515 spin_unlock_irq(&port->outvq_lock); 1516 } 1517 1518 /* 1519 * Port got unplugged. Remove port from portdev's list and drop the 1520 * kref reference. If no userspace has this port opened, it will 1521 * result in immediate removal the port. 1522 */ 1523 static void unplug_port(struct port *port) 1524 { 1525 spin_lock_irq(&port->portdev->ports_lock); 1526 list_del(&port->list); 1527 spin_unlock_irq(&port->portdev->ports_lock); 1528 1529 spin_lock_irq(&port->inbuf_lock); 1530 if (port->guest_connected) { 1531 /* Let the app know the port is going down. */ 1532 send_sigio_to_port(port); 1533 1534 /* Do this after sigio is actually sent */ 1535 port->guest_connected = false; 1536 port->host_connected = false; 1537 1538 wake_up_interruptible(&port->waitqueue); 1539 } 1540 spin_unlock_irq(&port->inbuf_lock); 1541 1542 if (is_console_port(port)) { 1543 spin_lock_irq(&pdrvdata_lock); 1544 list_del(&port->cons.list); 1545 spin_unlock_irq(&pdrvdata_lock); 1546 hvc_remove(port->cons.hvc); 1547 ida_free(&vtermno_ida, port->cons.vtermno); 1548 } 1549 1550 remove_port_data(port); 1551 1552 /* 1553 * Null out portdev under outvq_lock so that __send_to_port() 1554 * cannot race: it checks port->portdev inside the same lock 1555 * and bails out if NULL, preventing any buffer from being 1556 * enqueued to an already torn-down virtqueue. Also prevents 1557 * a close on an open port later from sending a stale control 1558 * message. 1559 */ 1560 spin_lock_irq(&port->outvq_lock); 1561 port->portdev = NULL; 1562 spin_unlock_irq(&port->outvq_lock); 1563 1564 sysfs_remove_group(&port->dev->kobj, &port_attribute_group); 1565 device_destroy(&port_class, port->dev->devt); 1566 cdev_del(port->cdev); 1567 1568 debugfs_remove(port->debugfs_file); 1569 kfree(port->name); 1570 1571 /* 1572 * Locks around here are not necessary - a port can't be 1573 * opened after we removed the port struct from ports_list 1574 * above. 1575 */ 1576 kref_put(&port->kref, remove_port); 1577 } 1578 1579 /* Any private messages that the Host and Guest want to share */ 1580 static void handle_control_message(struct virtio_device *vdev, 1581 struct ports_device *portdev, 1582 struct port_buffer *buf) 1583 { 1584 struct virtio_console_control *cpkt; 1585 struct port *port; 1586 size_t name_size; 1587 int err; 1588 1589 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset); 1590 1591 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id)); 1592 if (!port && 1593 cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) { 1594 /* No valid header at start of buffer. Drop it. */ 1595 dev_dbg(&portdev->vdev->dev, 1596 "Invalid index %u in control packet\n", 1597 virtio32_to_cpu(vdev, cpkt->id)); 1598 return; 1599 } 1600 1601 switch (virtio16_to_cpu(vdev, cpkt->event)) { 1602 case VIRTIO_CONSOLE_PORT_ADD: 1603 if (port) { 1604 dev_dbg(&portdev->vdev->dev, 1605 "Port %u already added\n", port->id); 1606 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1); 1607 break; 1608 } 1609 if (virtio32_to_cpu(vdev, cpkt->id) >= 1610 portdev->max_nr_ports) { 1611 dev_warn(&portdev->vdev->dev, 1612 "Request for adding port with " 1613 "out-of-bound id %u, max. supported id: %u\n", 1614 virtio32_to_cpu(vdev, cpkt->id), 1615 portdev->max_nr_ports - 1); 1616 break; 1617 } 1618 add_port(portdev, virtio32_to_cpu(vdev, cpkt->id)); 1619 break; 1620 case VIRTIO_CONSOLE_PORT_REMOVE: 1621 unplug_port(port); 1622 break; 1623 case VIRTIO_CONSOLE_CONSOLE_PORT: 1624 if (!cpkt->value) 1625 break; 1626 if (is_console_port(port)) 1627 break; 1628 1629 init_port_console(port); 1630 complete(&early_console_added); 1631 /* 1632 * Could remove the port here in case init fails - but 1633 * have to notify the host first. 1634 */ 1635 break; 1636 case VIRTIO_CONSOLE_RESIZE: { 1637 struct { 1638 __virtio16 cols; 1639 __virtio16 rows; 1640 } size; 1641 1642 if (!is_console_port(port)) 1643 break; 1644 1645 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt), 1646 sizeof(size)); 1647 set_console_size(port, virtio16_to_cpu(vdev, size.rows), 1648 virtio16_to_cpu(vdev, size.cols)); 1649 1650 port->cons.hvc->irq_requested = 1; 1651 resize_console(port); 1652 break; 1653 } 1654 case VIRTIO_CONSOLE_PORT_OPEN: 1655 port->host_connected = virtio16_to_cpu(vdev, cpkt->value); 1656 wake_up_interruptible(&port->waitqueue); 1657 /* 1658 * If the host port got closed and the host had any 1659 * unconsumed buffers, we'll be able to reclaim them 1660 * now. 1661 */ 1662 spin_lock_irq(&port->outvq_lock); 1663 reclaim_consumed_buffers(port); 1664 spin_unlock_irq(&port->outvq_lock); 1665 1666 /* 1667 * If the guest is connected, it'll be interested in 1668 * knowing the host connection state changed. 1669 */ 1670 spin_lock_irq(&port->inbuf_lock); 1671 send_sigio_to_port(port); 1672 spin_unlock_irq(&port->inbuf_lock); 1673 break; 1674 case VIRTIO_CONSOLE_PORT_NAME: 1675 /* 1676 * If we woke up after hibernation, we can get this 1677 * again. Skip it in that case. 1678 */ 1679 if (port->name) 1680 break; 1681 1682 /* 1683 * Skip the size of the header and the cpkt to get the size 1684 * of the name that was sent 1685 */ 1686 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1; 1687 1688 port->name = kmalloc(name_size, GFP_KERNEL); 1689 if (!port->name) { 1690 dev_err(port->dev, 1691 "Not enough space to store port name\n"); 1692 break; 1693 } 1694 strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt), 1695 name_size); 1696 1697 /* 1698 * Since we only have one sysfs attribute, 'name', 1699 * create it only if we have a name for the port. 1700 */ 1701 err = sysfs_create_group(&port->dev->kobj, 1702 &port_attribute_group); 1703 if (err) { 1704 dev_err(port->dev, 1705 "Error %d creating sysfs device attributes\n", 1706 err); 1707 } else { 1708 /* 1709 * Generate a udev event so that appropriate 1710 * symlinks can be created based on udev 1711 * rules. 1712 */ 1713 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE); 1714 } 1715 break; 1716 } 1717 } 1718 1719 static void control_work_handler(struct work_struct *work) 1720 { 1721 struct ports_device *portdev; 1722 struct virtqueue *vq; 1723 struct port_buffer *buf; 1724 unsigned int len; 1725 1726 portdev = container_of(work, struct ports_device, control_work); 1727 vq = portdev->c_ivq; 1728 1729 spin_lock(&portdev->c_ivq_lock); 1730 while ((buf = virtqueue_get_buf(vq, &len))) { 1731 spin_unlock(&portdev->c_ivq_lock); 1732 1733 buf->len = min_t(size_t, len, buf->size); 1734 buf->offset = 0; 1735 1736 handle_control_message(vq->vdev, portdev, buf); 1737 1738 spin_lock(&portdev->c_ivq_lock); 1739 if (add_inbuf(portdev->c_ivq, buf) < 0) { 1740 dev_warn(&portdev->vdev->dev, 1741 "Error adding buffer to queue\n"); 1742 free_buf(buf, false); 1743 } 1744 } 1745 spin_unlock(&portdev->c_ivq_lock); 1746 } 1747 1748 static void flush_bufs(struct virtqueue *vq, bool can_sleep) 1749 { 1750 struct port_buffer *buf; 1751 unsigned int len; 1752 1753 while ((buf = virtqueue_get_buf(vq, &len))) 1754 free_buf(buf, can_sleep); 1755 } 1756 1757 static void out_intr(struct virtqueue *vq) 1758 { 1759 struct port *port; 1760 1761 port = find_port_by_vq(vq->vdev->priv, vq); 1762 if (!port) { 1763 flush_bufs(vq, false); 1764 return; 1765 } 1766 1767 wake_up_interruptible(&port->waitqueue); 1768 kref_put(&port->kref, remove_port); 1769 } 1770 1771 static void in_intr(struct virtqueue *vq) 1772 { 1773 struct port *port; 1774 unsigned long flags; 1775 1776 port = find_port_by_vq(vq->vdev->priv, vq); 1777 if (!port) { 1778 flush_bufs(vq, false); 1779 return; 1780 } 1781 1782 spin_lock_irqsave(&port->inbuf_lock, flags); 1783 port->inbuf = get_inbuf(port); 1784 1785 /* 1786 * Normally the port should not accept data when the port is 1787 * closed. For generic serial ports, the host won't (shouldn't) 1788 * send data till the guest is connected. But this condition 1789 * can be reached when a console port is not yet connected (no 1790 * tty is spawned) and the other side sends out data over the 1791 * vring, or when a remote devices start sending data before 1792 * the ports are opened. 1793 * 1794 * A generic serial port will discard data if not connected, 1795 * while console ports and rproc-serial ports accepts data at 1796 * any time. rproc-serial is initiated with guest_connected to 1797 * false because port_fops_open expects this. Console ports are 1798 * hooked up with an HVC console and is initialized with 1799 * guest_connected to true. 1800 */ 1801 1802 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev)) 1803 discard_port_data(port); 1804 1805 /* Send a SIGIO indicating new data in case the process asked for it */ 1806 send_sigio_to_port(port); 1807 1808 spin_unlock_irqrestore(&port->inbuf_lock, flags); 1809 1810 wake_up_interruptible(&port->waitqueue); 1811 1812 if (is_console_port(port) && hvc_poll(port->cons.hvc)) 1813 hvc_kick(); 1814 1815 kref_put(&port->kref, remove_port); 1816 } 1817 1818 static void control_intr(struct virtqueue *vq) 1819 { 1820 struct ports_device *portdev; 1821 1822 portdev = vq->vdev->priv; 1823 schedule_work(&portdev->control_work); 1824 } 1825 1826 static void config_intr(struct virtio_device *vdev) 1827 { 1828 struct ports_device *portdev; 1829 1830 portdev = vdev->priv; 1831 1832 if (!use_multiport(portdev)) 1833 schedule_work(&portdev->config_work); 1834 } 1835 1836 static void update_size_from_config(struct ports_device *portdev) 1837 { 1838 struct virtio_device *vdev; 1839 struct port *port; 1840 u16 rows, cols; 1841 1842 vdev = portdev->vdev; 1843 1844 /* 1845 * We'll use this way of resizing only for legacy support. 1846 * For multiport devices, use control messages to indicate 1847 * console size changes so that it can be done per-port. 1848 * 1849 * Don't test F_SIZE at all if we're rproc: not a valid feature. 1850 */ 1851 if (is_rproc_serial(vdev) || 1852 use_multiport(portdev) || 1853 !virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) 1854 return; 1855 1856 virtio_cread(vdev, struct virtio_console_config, cols, &cols); 1857 virtio_cread(vdev, struct virtio_console_config, rows, &rows); 1858 1859 port = find_port_by_id(portdev, 0); 1860 set_console_size(port, rows, cols); 1861 resize_console(port); 1862 } 1863 1864 static void config_work_handler(struct work_struct *work) 1865 { 1866 struct ports_device *portdev; 1867 1868 portdev = container_of(work, struct ports_device, config_work); 1869 update_size_from_config(portdev); 1870 } 1871 1872 static int init_vqs(struct ports_device *portdev) 1873 { 1874 struct virtqueue_info *vqs_info; 1875 struct virtqueue **vqs; 1876 u32 i, j, nr_ports, nr_queues; 1877 int err; 1878 1879 nr_ports = portdev->max_nr_ports; 1880 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2; 1881 1882 vqs = kmalloc_objs(struct virtqueue *, nr_queues); 1883 vqs_info = kzalloc_objs(*vqs_info, nr_queues); 1884 portdev->in_vqs = kmalloc_objs(struct virtqueue *, nr_ports); 1885 portdev->out_vqs = kmalloc_objs(struct virtqueue *, nr_ports); 1886 if (!vqs || !vqs_info || !portdev->in_vqs || !portdev->out_vqs) { 1887 err = -ENOMEM; 1888 goto free; 1889 } 1890 1891 /* 1892 * For backward compat (newer host but older guest), the host 1893 * spawns a console port first and also inits the vqs for port 1894 * 0 before others. 1895 */ 1896 j = 0; 1897 vqs_info[j].callback = in_intr; 1898 vqs_info[j + 1].callback = out_intr; 1899 vqs_info[j].name = "input"; 1900 vqs_info[j + 1].name = "output"; 1901 j += 2; 1902 1903 if (use_multiport(portdev)) { 1904 vqs_info[j].callback = control_intr; 1905 vqs_info[j].name = "control-i"; 1906 vqs_info[j + 1].name = "control-o"; 1907 1908 for (i = 1; i < nr_ports; i++) { 1909 j += 2; 1910 vqs_info[j].callback = in_intr; 1911 vqs_info[j + 1].callback = out_intr; 1912 vqs_info[j].name = "input"; 1913 vqs_info[j + 1].name = "output"; 1914 } 1915 } 1916 /* Find the queues. */ 1917 err = virtio_find_vqs(portdev->vdev, nr_queues, vqs, vqs_info, NULL); 1918 if (err) 1919 goto free; 1920 1921 j = 0; 1922 portdev->in_vqs[0] = vqs[0]; 1923 portdev->out_vqs[0] = vqs[1]; 1924 j += 2; 1925 if (use_multiport(portdev)) { 1926 portdev->c_ivq = vqs[j]; 1927 portdev->c_ovq = vqs[j + 1]; 1928 1929 for (i = 1; i < nr_ports; i++) { 1930 j += 2; 1931 portdev->in_vqs[i] = vqs[j]; 1932 portdev->out_vqs[i] = vqs[j + 1]; 1933 } 1934 } 1935 kfree(vqs_info); 1936 kfree(vqs); 1937 1938 return 0; 1939 1940 free: 1941 kfree(portdev->out_vqs); 1942 kfree(portdev->in_vqs); 1943 kfree(vqs_info); 1944 kfree(vqs); 1945 1946 return err; 1947 } 1948 1949 static const struct file_operations portdev_fops = { 1950 .owner = THIS_MODULE, 1951 }; 1952 1953 static void remove_vqs(struct ports_device *portdev) 1954 { 1955 struct virtqueue *vq; 1956 bool multiport = use_multiport(portdev); 1957 1958 virtio_device_for_each_vq(portdev->vdev, vq) { 1959 struct port_buffer *buf; 1960 unsigned int len; 1961 1962 /* 1963 * c_ovq cookies are &portdev->cpkt, not port_buffer. 1964 * Detach them but do not free_buf(). 1965 */ 1966 if (multiport && vq == portdev->c_ovq) { 1967 spin_lock(&portdev->c_ovq_lock); 1968 while (virtqueue_get_buf(vq, &len)) 1969 ; 1970 while (virtqueue_detach_unused_buf(vq)) 1971 ; 1972 spin_unlock(&portdev->c_ovq_lock); 1973 } else { 1974 flush_bufs(vq, true); 1975 while ((buf = virtqueue_detach_unused_buf(vq))) 1976 free_buf(buf, true); 1977 } 1978 cond_resched(); 1979 } 1980 portdev->vdev->config->del_vqs(portdev->vdev); 1981 kfree(portdev->in_vqs); 1982 kfree(portdev->out_vqs); 1983 } 1984 1985 static void virtcons_remove(struct virtio_device *vdev) 1986 { 1987 struct ports_device *portdev; 1988 struct port *port, *port2; 1989 1990 portdev = vdev->priv; 1991 1992 spin_lock_irq(&pdrvdata_lock); 1993 list_del(&portdev->list); 1994 spin_unlock_irq(&pdrvdata_lock); 1995 1996 /* Device is going away, exit any polling for buffers */ 1997 virtio_break_device(vdev); 1998 if (use_multiport(portdev)) 1999 flush_work(&portdev->control_work); 2000 else 2001 flush_work(&portdev->config_work); 2002 2003 /* Disable interrupts for vqs */ 2004 virtio_reset_device(vdev); 2005 /* Finish up work that's lined up */ 2006 if (use_multiport(portdev)) 2007 cancel_work_sync(&portdev->control_work); 2008 else 2009 cancel_work_sync(&portdev->config_work); 2010 2011 list_for_each_entry_safe(port, port2, &portdev->ports, list) 2012 unplug_port(port); 2013 2014 unregister_chrdev(portdev->chr_major, "virtio-portsdev"); 2015 2016 /* 2017 * When yanking out a device, we immediately lose the 2018 * (device-side) queues. So there's no point in keeping the 2019 * guest side around till we drop our final reference. This 2020 * also means that any ports which are in an open state will 2021 * have to just stop using the port, as the vqs are going 2022 * away. 2023 */ 2024 remove_vqs(portdev); 2025 kfree(portdev); 2026 } 2027 2028 /* 2029 * Once we're further in boot, we get probed like any other virtio 2030 * device. 2031 * 2032 * If the host also supports multiple console ports, we check the 2033 * config space to see how many ports the host has spawned. We 2034 * initialize each port found. 2035 */ 2036 static int virtcons_probe(struct virtio_device *vdev) 2037 { 2038 struct ports_device *portdev; 2039 int err; 2040 bool multiport; 2041 2042 /* We only need a config space if features are offered */ 2043 if (!vdev->config->get && 2044 (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE) 2045 || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) { 2046 dev_err(&vdev->dev, "%s failure: config access disabled\n", 2047 __func__); 2048 return -EINVAL; 2049 } 2050 2051 portdev = kmalloc_obj(*portdev); 2052 if (!portdev) { 2053 err = -ENOMEM; 2054 goto fail; 2055 } 2056 2057 /* Attach this portdev to this virtio_device, and vice-versa. */ 2058 portdev->vdev = vdev; 2059 vdev->priv = portdev; 2060 portdev->pm_freezing = false; 2061 2062 portdev->chr_major = register_chrdev(0, "virtio-portsdev", 2063 &portdev_fops); 2064 if (portdev->chr_major < 0) { 2065 dev_err(&vdev->dev, 2066 "Error %d registering chrdev for device %u\n", 2067 portdev->chr_major, vdev->index); 2068 err = portdev->chr_major; 2069 goto free; 2070 } 2071 2072 multiport = false; 2073 portdev->max_nr_ports = 1; 2074 2075 /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */ 2076 if (!is_rproc_serial(vdev) && 2077 virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT, 2078 struct virtio_console_config, max_nr_ports, 2079 &portdev->max_nr_ports) == 0) { 2080 if (portdev->max_nr_ports == 0 || 2081 portdev->max_nr_ports > VIRTCONS_MAX_PORTS) { 2082 dev_err(&vdev->dev, 2083 "Invalidate max_nr_ports %d", 2084 portdev->max_nr_ports); 2085 err = -EINVAL; 2086 goto free; 2087 } 2088 multiport = true; 2089 } 2090 2091 spin_lock_init(&portdev->ports_lock); 2092 INIT_LIST_HEAD(&portdev->ports); 2093 INIT_LIST_HEAD(&portdev->list); 2094 2095 INIT_WORK(&portdev->config_work, &config_work_handler); 2096 INIT_WORK(&portdev->control_work, &control_work_handler); 2097 2098 if (multiport) { 2099 spin_lock_init(&portdev->c_ivq_lock); 2100 spin_lock_init(&portdev->c_ovq_lock); 2101 } 2102 2103 err = init_vqs(portdev); 2104 if (err < 0) { 2105 dev_err(&vdev->dev, "Error %d initializing vqs\n", err); 2106 goto free_chrdev; 2107 } 2108 2109 virtio_device_ready(portdev->vdev); 2110 2111 if (multiport) { 2112 err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock); 2113 if (err < 0) { 2114 dev_err(&vdev->dev, 2115 "Error allocating buffers for control queue\n"); 2116 /* 2117 * The host might want to notify mgmt sw about device 2118 * add failure. 2119 */ 2120 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID, 2121 VIRTIO_CONSOLE_DEVICE_READY, 0); 2122 /* Device was functional: we need full cleanup. */ 2123 virtcons_remove(vdev); 2124 return err; 2125 } 2126 } else { 2127 /* 2128 * For backward compatibility: Create a console port 2129 * if we're running on older host. 2130 */ 2131 add_port(portdev, 0); 2132 } 2133 2134 spin_lock_irq(&pdrvdata_lock); 2135 list_add_tail(&portdev->list, &pdrvdata.portdevs); 2136 spin_unlock_irq(&pdrvdata_lock); 2137 2138 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID, 2139 VIRTIO_CONSOLE_DEVICE_READY, 1); 2140 2141 update_size_from_config(portdev); 2142 2143 return 0; 2144 2145 free_chrdev: 2146 unregister_chrdev(portdev->chr_major, "virtio-portsdev"); 2147 free: 2148 kfree(portdev); 2149 fail: 2150 return err; 2151 } 2152 2153 static const struct virtio_device_id id_table[] = { 2154 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID }, 2155 { 0 }, 2156 }; 2157 MODULE_DEVICE_TABLE(virtio, id_table); 2158 2159 static const unsigned int features[] = { 2160 VIRTIO_CONSOLE_F_SIZE, 2161 VIRTIO_CONSOLE_F_MULTIPORT, 2162 }; 2163 2164 static const struct virtio_device_id rproc_serial_id_table[] = { 2165 #if IS_ENABLED(CONFIG_REMOTEPROC) 2166 { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID }, 2167 #endif 2168 { 0 }, 2169 }; 2170 MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table); 2171 2172 static const unsigned int rproc_serial_features[] = { 2173 }; 2174 2175 #ifdef CONFIG_PM_SLEEP 2176 static int virtcons_freeze(struct virtio_device *vdev) 2177 { 2178 struct ports_device *portdev; 2179 struct port *port; 2180 unsigned long flags; 2181 2182 portdev = vdev->priv; 2183 2184 /* 2185 * Block TX paths (put_chars, __send_to_port) before resetting the 2186 * device and tearing down virtqueues. This prevents races with 2187 * hvc console writes that remain active under no_console_suspend. 2188 */ 2189 smp_store_release(&portdev->pm_freezing, true); 2190 2191 /* 2192 * Synchronize with any concurrent __send_to_port() that may have 2193 * passed the pm_freezing check. By acquiring and releasing the 2194 * outvq_lock for each port, we ensure all active TX paths have 2195 * completed before we reset the device. 2196 */ 2197 spin_lock_irqsave(&portdev->ports_lock, flags); 2198 list_for_each_entry(port, &portdev->ports, list) { 2199 spin_lock(&port->outvq_lock); 2200 spin_unlock(&port->outvq_lock); 2201 } 2202 spin_unlock_irqrestore(&portdev->ports_lock, flags); 2203 2204 virtio_reset_device(vdev); 2205 2206 if (use_multiport(portdev)) 2207 virtqueue_disable_cb(portdev->c_ivq); 2208 cancel_work_sync(&portdev->control_work); 2209 cancel_work_sync(&portdev->config_work); 2210 /* 2211 * Once more: if control_work_handler() was running, it would 2212 * enable the cb as the last step. 2213 */ 2214 if (use_multiport(portdev)) 2215 virtqueue_disable_cb(portdev->c_ivq); 2216 2217 list_for_each_entry(port, &portdev->ports, list) { 2218 virtqueue_disable_cb(port->in_vq); 2219 virtqueue_disable_cb(port->out_vq); 2220 /* 2221 * We'll ask the host later if the new invocation has 2222 * the port opened or closed. 2223 */ 2224 port->host_connected = false; 2225 remove_port_data(port); 2226 } 2227 remove_vqs(portdev); 2228 2229 return 0; 2230 } 2231 2232 static int virtcons_restore(struct virtio_device *vdev) 2233 { 2234 struct ports_device *portdev; 2235 struct port *port; 2236 int ret; 2237 2238 portdev = vdev->priv; 2239 2240 ret = init_vqs(portdev); 2241 if (ret) 2242 return ret; 2243 2244 virtio_device_ready(portdev->vdev); 2245 2246 list_for_each_entry(port, &portdev->ports, list) { 2247 port->in_vq = portdev->in_vqs[port->id]; 2248 port->out_vq = portdev->out_vqs[port->id]; 2249 2250 fill_queue(port->in_vq, &port->inbuf_lock); 2251 2252 /* Get port open/close status on the host */ 2253 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1); 2254 2255 /* 2256 * If a port was open at the time of suspending, we 2257 * have to let the host know that it's still open. 2258 */ 2259 if (port->guest_connected) 2260 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1); 2261 } 2262 2263 /* 2264 * Populate the control receive queue only after the list iteration 2265 * is complete. If we fill this queue before iterating, the host could 2266 * immediately deliver a VIRTIO_CONSOLE_PORT_REMOVE message. 2267 * This would trigger the control workqueue, which modifies the 2268 * portdev->ports list concurrently with the unprotected loop above, 2269 * leading to a Use-After-Free and list corruption. 2270 */ 2271 if (use_multiport(portdev)) 2272 fill_queue(portdev->c_ivq, &portdev->c_ivq_lock); 2273 2274 /* 2275 * Allow TX paths only after all port->out_vq pointers have 2276 * been reassigned to the newly allocated virtqueues. 2277 */ 2278 smp_store_release(&portdev->pm_freezing, false); 2279 2280 return 0; 2281 } 2282 #endif 2283 2284 static struct virtio_driver virtio_console = { 2285 .feature_table = features, 2286 .feature_table_size = ARRAY_SIZE(features), 2287 .driver.name = KBUILD_MODNAME, 2288 .id_table = id_table, 2289 .probe = virtcons_probe, 2290 .remove = virtcons_remove, 2291 .config_changed = config_intr, 2292 #ifdef CONFIG_PM_SLEEP 2293 .freeze = virtcons_freeze, 2294 .restore = virtcons_restore, 2295 #endif 2296 }; 2297 2298 static struct virtio_driver virtio_rproc_serial = { 2299 .feature_table = rproc_serial_features, 2300 .feature_table_size = ARRAY_SIZE(rproc_serial_features), 2301 .driver.name = "virtio_rproc_serial", 2302 .id_table = rproc_serial_id_table, 2303 .probe = virtcons_probe, 2304 .remove = virtcons_remove, 2305 }; 2306 2307 static int __init virtio_console_init(void) 2308 { 2309 int err; 2310 2311 err = class_register(&port_class); 2312 if (err) 2313 return err; 2314 2315 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL); 2316 INIT_LIST_HEAD(&pdrvdata.consoles); 2317 INIT_LIST_HEAD(&pdrvdata.portdevs); 2318 2319 err = register_virtio_driver(&virtio_console); 2320 if (err < 0) { 2321 pr_err("Error %d registering virtio driver\n", err); 2322 goto free; 2323 } 2324 err = register_virtio_driver(&virtio_rproc_serial); 2325 if (err < 0) { 2326 pr_err("Error %d registering virtio rproc serial driver\n", 2327 err); 2328 goto unregister; 2329 } 2330 return 0; 2331 unregister: 2332 unregister_virtio_driver(&virtio_console); 2333 free: 2334 debugfs_remove_recursive(pdrvdata.debugfs_dir); 2335 class_unregister(&port_class); 2336 return err; 2337 } 2338 2339 static void __exit virtio_console_fini(void) 2340 { 2341 reclaim_dma_bufs(); 2342 2343 unregister_virtio_driver(&virtio_console); 2344 unregister_virtio_driver(&virtio_rproc_serial); 2345 2346 class_unregister(&port_class); 2347 debugfs_remove_recursive(pdrvdata.debugfs_dir); 2348 } 2349 module_init(virtio_console_init); 2350 module_exit(virtio_console_fini); 2351 2352 MODULE_DESCRIPTION("Virtio console driver"); 2353 MODULE_LICENSE("GPL"); 2354