Merge tag 'virtio-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux

Pull virtio update from Rusty Russell:
"Some nice cleanups, and even a patch my wife did as a "live" demo for
Latinoware 2012.

There's a slightly non-trivial merge in virtio-net, as we cleaned up
the virtio add_buf interface while DaveM accepted the mq virtio-net
patches."

* tag 'virtio-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux: (27 commits)
virtio_console: Add support for remoteproc serial
virtio_console: Merge struct buffer_token into struct port_buffer
virtio: add drv_to_virtio to make code clearly
virtio: use dev_to_virtio wrapper in virtio
virtio-mmio: Fix irq parsing in command line parameter
virtio_console: Free buffers from out-queue upon close
virtio: Convert dev_printk(KERN_<LEVEL> to dev_<level>(
virtio_console: Use kmalloc instead of kzalloc
virtio_console: Free buffer if splice fails
virtio: tools: make it clear that virtqueue_add_buf() no longer returns > 0
virtio: scsi: make it clear that virtqueue_add_buf() no longer returns > 0
virtio: rpmsg: make it clear that virtqueue_add_buf() no longer returns > 0
virtio: net: make it clear that virtqueue_add_buf() no longer returns > 0
virtio: console: make it clear that virtqueue_add_buf() no longer returns > 0
virtio: make virtqueue_add_buf() returning 0 on success, not capacity.
virtio: console: don't rely on virtqueue_add_buf() returning capacity.
virtio_net: don't rely on virtqueue_add_buf() returning capacity.
virtio-net: remove unused skb_vnet_hdr->num_sg field
virtio-net: correct capacity math on ring full
virtio: move queue_index and num_free fields into core struct virtqueue.
...

+230 -99
drivers/char/virtio_console.c
··· 37 37 #include <linux/wait.h> 38 38 #include <linux/workqueue.h> 39 39 #include <linux/module.h> 40 + #include <linux/dma-mapping.h> 41 + #include <linux/kconfig.h> 40 42 #include "../tty/hvc/hvc_console.h" 43 + 44 + #define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC) 41 45 42 46 /* 43 47 * This is a global struct for storing common data for all the devices ··· 115 111 size_t len; 116 112 /* offset in the buf from which to consume data */ 117 113 size_t offset; 114 + 115 + /* DMA address of buffer */ 116 + dma_addr_t dma; 117 + 118 + /* Device we got DMA memory from */ 119 + struct device *dev; 120 + 121 + /* List of pending dma buffers to free */ 122 + struct list_head list; 123 + 124 + /* If sgpages == 0 then buf is used */ 125 + unsigned int sgpages; 126 + 127 + /* sg is used if spages > 0. sg must be the last in is struct */ 128 + struct scatterlist sg[0]; 118 129 }; 119 130 120 131 /* ··· 344 325 return false; 345 326 } 346 327 328 + static bool is_rproc_serial(const struct virtio_device *vdev) 329 + { 330 + return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL; 331 + } 332 + 347 333 static inline bool use_multiport(struct ports_device *portdev) 348 334 { 349 335 /* ··· 360 336 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT); 361 337 } 362 338 363 - static void free_buf(struct port_buffer *buf) 339 + static DEFINE_SPINLOCK(dma_bufs_lock); 340 + static LIST_HEAD(pending_free_dma_bufs); 341 + 342 + static void free_buf(struct port_buffer *buf, bool can_sleep) 364 343 { 365 - kfree(buf->buf); 344 + unsigned int i; 345 + 346 + for (i = 0; i < buf->sgpages; i++) { 347 + struct page *page = sg_page(&buf->sg[i]); 348 + if (!page) 349 + break; 350 + put_page(page); 351 + } 352 + 353 + if (!buf->dev) { 354 + kfree(buf->buf); 355 + } else if (is_rproc_enabled) { 356 + unsigned long flags; 357 + 358 + /* dma_free_coherent requires interrupts to be enabled. */ 359 + if (!can_sleep) { 360 + /* queue up dma-buffers to be freed later */ 361 + spin_lock_irqsave(&dma_bufs_lock, flags); 362 + list_add_tail(&buf->list, &pending_free_dma_bufs); 363 + spin_unlock_irqrestore(&dma_bufs_lock, flags); 364 + return; 365 + } 366 + dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma); 367 + 368 + /* Release device refcnt and allow it to be freed */ 369 + put_device(buf->dev); 370 + } 371 + 366 372 kfree(buf); 367 373 } 368 374 369 - static struct port_buffer *alloc_buf(size_t buf_size) 375 + static void reclaim_dma_bufs(void) 376 + { 377 + unsigned long flags; 378 + struct port_buffer *buf, *tmp; 379 + LIST_HEAD(tmp_list); 380 + 381 + if (list_empty(&pending_free_dma_bufs)) 382 + return; 383 + 384 + /* Create a copy of the pending_free_dma_bufs while holding the lock */ 385 + spin_lock_irqsave(&dma_bufs_lock, flags); 386 + list_cut_position(&tmp_list, &pending_free_dma_bufs, 387 + pending_free_dma_bufs.prev); 388 + spin_unlock_irqrestore(&dma_bufs_lock, flags); 389 + 390 + /* Release the dma buffers, without irqs enabled */ 391 + list_for_each_entry_safe(buf, tmp, &tmp_list, list) { 392 + list_del(&buf->list); 393 + free_buf(buf, true); 394 + } 395 + } 396 + 397 + static struct port_buffer *alloc_buf(struct virtqueue *vq, size_t buf_size, 398 + int pages) 370 399 { 371 400 struct port_buffer *buf; 372 401 373 - buf = kmalloc(sizeof(*buf), GFP_KERNEL); 402 + reclaim_dma_bufs(); 403 + 404 + /* 405 + * Allocate buffer and the sg list. The sg list array is allocated 406 + * directly after the port_buffer struct. 407 + */ 408 + buf = kmalloc(sizeof(*buf) + sizeof(struct scatterlist) * pages, 409 + GFP_KERNEL); 374 410 if (!buf) 375 411 goto fail; 376 - buf->buf = kzalloc(buf_size, GFP_KERNEL); 412 + 413 + buf->sgpages = pages; 414 + if (pages > 0) { 415 + buf->dev = NULL; 416 + buf->buf = NULL; 417 + return buf; 418 + } 419 + 420 + if (is_rproc_serial(vq->vdev)) { 421 + /* 422 + * Allocate DMA memory from ancestor. When a virtio 423 + * device is created by remoteproc, the DMA memory is 424 + * associated with the grandparent device: 425 + * vdev => rproc => platform-dev. 426 + * The code here would have been less quirky if 427 + * DMA_MEMORY_INCLUDES_CHILDREN had been supported 428 + * in dma-coherent.c 429 + */ 430 + if (!vq->vdev->dev.parent || !vq->vdev->dev.parent->parent) 431 + goto free_buf; 432 + buf->dev = vq->vdev->dev.parent->parent; 433 + 434 + /* Increase device refcnt to avoid freeing it */ 435 + get_device(buf->dev); 436 + buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma, 437 + GFP_KERNEL); 438 + } else { 439 + buf->dev = NULL; 440 + buf->buf = kmalloc(buf_size, GFP_KERNEL); 441 + } 442 + 377 443 if (!buf->buf) 378 444 goto free_buf; 379 445 buf->len = 0; ··· 510 396 511 397 ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC); 512 398 virtqueue_kick(vq); 399 + if (!ret) 400 + ret = vq->num_free; 513 401 return ret; 514 402 } 515 403 ··· 532 416 port->stats.bytes_discarded += buf->len - buf->offset; 533 417 if (add_inbuf(port->in_vq, buf) < 0) { 534 418 err++; 535 - free_buf(buf); 419 + free_buf(buf, false); 536 420 } 537 421 port->inbuf = NULL; 538 422 buf = get_inbuf(port); ··· 575 459 vq = portdev->c_ovq; 576 460 577 461 sg_init_one(sg, &cpkt, sizeof(cpkt)); 578 - if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) >= 0) { 462 + if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) == 0) { 579 463 virtqueue_kick(vq); 580 464 while (!virtqueue_get_buf(vq, &len)) 581 465 cpu_relax(); ··· 592 476 return 0; 593 477 } 594 478 595 - struct buffer_token { 596 - union { 597 - void *buf; 598 - struct scatterlist *sg; 599 - } u; 600 - /* If sgpages == 0 then buf is used, else sg is used */ 601 - unsigned int sgpages; 602 - }; 603 - 604 - static void reclaim_sg_pages(struct scatterlist *sg, unsigned int nrpages) 605 - { 606 - int i; 607 - struct page *page; 608 - 609 - for (i = 0; i < nrpages; i++) { 610 - page = sg_page(&sg[i]); 611 - if (!page) 612 - break; 613 - put_page(page); 614 - } 615 - kfree(sg); 616 - } 617 479 618 480 /* Callers must take the port->outvq_lock */ 619 481 static void reclaim_consumed_buffers(struct port *port) 620 482 { 621 - struct buffer_token *tok; 483 + struct port_buffer *buf; 622 484 unsigned int len; 623 485 624 486 if (!port->portdev) { 625 487 /* Device has been unplugged. vqs are already gone. */ 626 488 return; 627 489 } 628 - while ((tok = virtqueue_get_buf(port->out_vq, &len))) { 629 - if (tok->sgpages) 630 - reclaim_sg_pages(tok->u.sg, tok->sgpages); 631 - else 632 - kfree(tok->u.buf); 633 - kfree(tok); 490 + while ((buf = virtqueue_get_buf(port->out_vq, &len))) { 491 + free_buf(buf, false); 634 492 port->outvq_full = false; 635 493 } 636 494 } 637 495 638 496 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg, 639 497 int nents, size_t in_count, 640 - struct buffer_token *tok, bool nonblock) 498 + void *data, bool nonblock) 641 499 { 642 500 struct virtqueue *out_vq; 643 - ssize_t ret; 501 + int err; 644 502 unsigned long flags; 645 503 unsigned int len; 646 504 ··· 624 534 625 535 reclaim_consumed_buffers(port); 626 536 627 - ret = virtqueue_add_buf(out_vq, sg, nents, 0, tok, GFP_ATOMIC); 537 + err = virtqueue_add_buf(out_vq, sg, nents, 0, data, GFP_ATOMIC); 628 538 629 539 /* Tell Host to go! */ 630 540 virtqueue_kick(out_vq); 631 541 632 - if (ret < 0) { 542 + if (err) { 633 543 in_count = 0; 634 544 goto done; 635 545 } 636 546 637 - if (ret == 0) 547 + if (out_vq->num_free == 0) 638 548 port->outvq_full = true; 639 549 640 550 if (nonblock) ··· 660 570 * of it 661 571 */ 662 572 return in_count; 663 - } 664 - 665 - static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count, 666 - bool nonblock) 667 - { 668 - struct scatterlist sg[1]; 669 - struct buffer_token *tok; 670 - 671 - tok = kmalloc(sizeof(*tok), GFP_ATOMIC); 672 - if (!tok) 673 - return -ENOMEM; 674 - tok->sgpages = 0; 675 - tok->u.buf = in_buf; 676 - 677 - sg_init_one(sg, in_buf, in_count); 678 - 679 - return __send_to_port(port, sg, 1, in_count, tok, nonblock); 680 - } 681 - 682 - static ssize_t send_pages(struct port *port, struct scatterlist *sg, int nents, 683 - size_t in_count, bool nonblock) 684 - { 685 - struct buffer_token *tok; 686 - 687 - tok = kmalloc(sizeof(*tok), GFP_ATOMIC); 688 - if (!tok) 689 - return -ENOMEM; 690 - tok->sgpages = nents; 691 - tok->u.sg = sg; 692 - 693 - return __send_to_port(port, sg, nents, in_count, tok, nonblock); 694 573 } 695 574 696 575 /* ··· 807 748 size_t count, loff_t *offp) 808 749 { 809 750 struct port *port; 810 - char *buf; 751 + struct port_buffer *buf; 811 752 ssize_t ret; 812 753 bool nonblock; 754 + struct scatterlist sg[1]; 813 755 814 756 /* Userspace could be out to fool us */ 815 757 if (!count) ··· 826 766 827 767 count = min((size_t)(32 * 1024), count); 828 768 829 - buf = kmalloc(count, GFP_KERNEL); 769 + buf = alloc_buf(port->out_vq, count, 0); 830 770 if (!buf) 831 771 return -ENOMEM; 832 772 833 - ret = copy_from_user(buf, ubuf, count); 773 + ret = copy_from_user(buf->buf, ubuf, count); 834 774 if (ret) { 835 775 ret = -EFAULT; 836 776 goto free_buf; ··· 844 784 * through to the host. 845 785 */ 846 786 nonblock = true; 847 - ret = send_buf(port, buf, count, nonblock); 787 + sg_init_one(sg, buf->buf, count); 788 + ret = __send_to_port(port, sg, 1, count, buf, nonblock); 848 789 849 790 if (nonblock && ret > 0) 850 791 goto out; 851 792 852 793 free_buf: 853 - kfree(buf); 794 + free_buf(buf, true); 854 795 out: 855 796 return ret; 856 797 } ··· 917 856 struct port *port = filp->private_data; 918 857 struct sg_list sgl; 919 858 ssize_t ret; 859 + struct port_buffer *buf; 920 860 struct splice_desc sd = { 921 861 .total_len = len, 922 862 .flags = flags, ··· 925 863 .u.data = &sgl, 926 864 }; 927 865 866 + /* 867 + * Rproc_serial does not yet support splice. To support splice 868 + * pipe_to_sg() must allocate dma-buffers and copy content from 869 + * regular pages to dma pages. And alloc_buf and free_buf must 870 + * support allocating and freeing such a list of dma-buffers. 871 + */ 872 + if (is_rproc_serial(port->out_vq->vdev)) 873 + return -EINVAL; 874 + 928 875 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK); 929 876 if (ret < 0) 930 877 return ret; 931 878 879 + buf = alloc_buf(port->out_vq, 0, pipe->nrbufs); 880 + if (!buf) 881 + return -ENOMEM; 882 + 932 883 sgl.n = 0; 933 884 sgl.len = 0; 934 885 sgl.size = pipe->nrbufs; 935 - sgl.sg = kmalloc(sizeof(struct scatterlist) * sgl.size, GFP_KERNEL); 936 - if (unlikely(!sgl.sg)) 937 - return -ENOMEM; 938 - 886 + sgl.sg = buf->sg; 939 887 sg_init_table(sgl.sg, sgl.size); 940 888 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg); 941 889 if (likely(ret > 0)) 942 - ret = send_pages(port, sgl.sg, sgl.n, sgl.len, true); 890 + ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true); 943 891 892 + if (unlikely(ret <= 0)) 893 + free_buf(buf, true); 944 894 return ret; 945 895 } 946 896 ··· 1001 927 reclaim_consumed_buffers(port); 1002 928 spin_unlock_irq(&port->outvq_lock); 1003 929 930 + reclaim_dma_bufs(); 1004 931 /* 1005 932 * Locks aren't necessary here as a port can't be opened after 1006 933 * unplug, and if a port isn't unplugged, a kref would already ··· 1106 1031 static int put_chars(u32 vtermno, const char *buf, int count) 1107 1032 { 1108 1033 struct port *port; 1034 + struct scatterlist sg[1]; 1109 1035 1110 1036 if (unlikely(early_put_chars)) 1111 1037 return early_put_chars(vtermno, buf, count); ··· 1115 1039 if (!port) 1116 1040 return -EPIPE; 1117 1041 1118 - return send_buf(port, (void *)buf, count, false); 1042 + sg_init_one(sg, buf, count); 1043 + return __send_to_port(port, sg, 1, count, (void *)buf, false); 1119 1044 } 1120 1045 1121 1046 /* ··· 1153 1076 return; 1154 1077 1155 1078 vdev = port->portdev->vdev; 1156 - if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) 1079 + 1080 + /* Don't test F_SIZE at all if we're rproc: not a valid feature! */ 1081 + if (!is_rproc_serial(vdev) && 1082 + virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) 1157 1083 hvc_resize(port->cons.hvc, port->cons.ws); 1158 1084 } 1159 1085 ··· 1340 1260 1341 1261 nr_added_bufs = 0; 1342 1262 do { 1343 - buf = alloc_buf(PAGE_SIZE); 1263 + buf = alloc_buf(vq, PAGE_SIZE, 0); 1344 1264 if (!buf) 1345 1265 break; 1346 1266 ··· 1348 1268 ret = add_inbuf(vq, buf); 1349 1269 if (ret < 0) { 1350 1270 spin_unlock_irq(lock); 1351 - free_buf(buf); 1271 + free_buf(buf, true); 1352 1272 break; 1353 1273 } 1354 1274 nr_added_bufs++; ··· 1436 1356 goto free_device; 1437 1357 } 1438 1358 1439 - /* 1440 - * If we're not using multiport support, this has to be a console port 1441 - */ 1442 - if (!use_multiport(port->portdev)) { 1359 + if (is_rproc_serial(port->portdev->vdev)) 1360 + /* 1361 + * For rproc_serial assume remote processor is connected. 1362 + * rproc_serial does not want the console port, only 1363 + * the generic port implementation. 1364 + */ 1365 + port->host_connected = true; 1366 + else if (!use_multiport(port->portdev)) { 1367 + /* 1368 + * If we're not using multiport support, 1369 + * this has to be a console port. 1370 + */ 1443 1371 err = init_port_console(port); 1444 1372 if (err) 1445 1373 goto free_inbufs; ··· 1480 1392 1481 1393 free_inbufs: 1482 1394 while ((buf = virtqueue_detach_unused_buf(port->in_vq))) 1483 - free_buf(buf); 1395 + free_buf(buf, true); 1484 1396 free_device: 1485 1397 device_destroy(pdrvdata.class, port->dev->devt); 1486 1398 free_cdev: ··· 1522 1434 1523 1435 /* Remove buffers we queued up for the Host to send us data in. */ 1524 1436 while ((buf = virtqueue_detach_unused_buf(port->in_vq))) 1525 - free_buf(buf); 1437 + free_buf(buf, true); 1438 + 1439 + /* Free pending buffers from the out-queue. */ 1440 + while ((buf = virtqueue_detach_unused_buf(port->out_vq))) 1441 + free_buf(buf, true); 1526 1442 } 1527 1443 1528 1444 /* ··· 1728 1636 if (add_inbuf(portdev->c_ivq, buf) < 0) { 1729 1637 dev_warn(&portdev->vdev->dev, 1730 1638 "Error adding buffer to queue\n"); 1731 - free_buf(buf); 1639 + free_buf(buf, false); 1732 1640 } 1733 1641 } 1734 1642 spin_unlock(&portdev->cvq_lock); ··· 1924 1832 return; 1925 1833 1926 1834 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len))) 1927 - free_buf(buf); 1835 + free_buf(buf, true); 1928 1836 1929 1837 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq))) 1930 - free_buf(buf); 1838 + free_buf(buf, true); 1931 1839 } 1932 1840 1933 1841 /* ··· 1974 1882 1975 1883 multiport = false; 1976 1884 portdev->config.max_nr_ports = 1; 1977 - if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT, 1978 - offsetof(struct virtio_console_config, 1979 - max_nr_ports), 1980 - &portdev->config.max_nr_ports) == 0) 1885 + 1886 + /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */ 1887 + if (!is_rproc_serial(vdev) && 1888 + virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT, 1889 + offsetof(struct virtio_console_config, 1890 + max_nr_ports), 1891 + &portdev->config.max_nr_ports) == 0) { 1981 1892 multiport = true; 1893 + } 1982 1894 1983 1895 err = init_vqs(portdev); 1984 1896 if (err < 0) { ··· 2092 1996 VIRTIO_CONSOLE_F_MULTIPORT, 2093 1997 }; 2094 1998 1999 + static struct virtio_device_id rproc_serial_id_table[] = { 2000 + #if IS_ENABLED(CONFIG_REMOTEPROC) 2001 + { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID }, 2002 + #endif 2003 + { 0 }, 2004 + }; 2005 + 2006 + static unsigned int rproc_serial_features[] = { 2007 + }; 2008 + 2095 2009 #ifdef CONFIG_PM 2096 2010 static int virtcons_freeze(struct virtio_device *vdev) 2097 2011 { ··· 2186 2080 #endif 2187 2081 }; 2188 2082 2083 + /* 2084 + * virtio_rproc_serial refers to __devinit function which causes 2085 + * section mismatch warnings. So use __refdata to silence warnings. 2086 + */ 2087 + static struct virtio_driver __refdata virtio_rproc_serial = { 2088 + .feature_table = rproc_serial_features, 2089 + .feature_table_size = ARRAY_SIZE(rproc_serial_features), 2090 + .driver.name = "virtio_rproc_serial", 2091 + .driver.owner = THIS_MODULE, 2092 + .id_table = rproc_serial_id_table, 2093 + .probe = virtcons_probe, 2094 + .remove = virtcons_remove, 2095 + }; 2096 + 2189 2097 static int __init init(void) 2190 2098 { 2191 2099 int err; ··· 2224 2104 pr_err("Error %d registering virtio driver\n", err); 2225 2105 goto free; 2226 2106 } 2107 + err = register_virtio_driver(&virtio_rproc_serial); 2108 + if (err < 0) { 2109 + pr_err("Error %d registering virtio rproc serial driver\n", 2110 + err); 2111 + goto unregister; 2112 + } 2227 2113 return 0; 2114 + unregister: 2115 + unregister_virtio_driver(&virtio_console); 2228 2116 free: 2229 2117 if (pdrvdata.debugfs_dir) 2230 2118 debugfs_remove_recursive(pdrvdata.debugfs_dir); ··· 2242 2114 2243 2115 static void __exit fini(void) 2244 2116 { 2117 + reclaim_dma_bufs(); 2118 + 2245 2119 unregister_virtio_driver(&virtio_console); 2120 + unregister_virtio_driver(&virtio_rproc_serial); 2246 2121 2247 2122 class_destroy(pdrvdata.class); 2248 2123 if (pdrvdata.debugfs_dir)
+1 -1
drivers/lguest/core.c
··· 225 225 * eventfd (ie. the appropriate virtqueue thread)? 226 226 */ 227 227 if (!send_notify_to_eventfd(cpu)) { 228 - /* OK, we tell the main Laucher. */ 228 + /* OK, we tell the main Launcher. */ 229 229 if (put_user(cpu->pending_notify, user)) 230 230 return -EFAULT; 231 231 return sizeof(cpu->pending_notify);
+18 -28
drivers/net/virtio_net.c
··· 130 130 struct virtio_net_hdr hdr; 131 131 struct virtio_net_hdr_mrg_rxbuf mhdr; 132 132 }; 133 - unsigned int num_sg; 134 133 }; 135 134 136 135 struct padded_vnet_hdr { ··· 529 530 err = add_recvbuf_small(rq, gfp); 530 531 531 532 oom = err == -ENOMEM; 532 - if (err < 0) 533 + if (err) 533 534 break; 534 535 ++rq->num; 535 - } while (err > 0); 536 + } while (rq->vq->num_free); 536 537 if (unlikely(rq->num > rq->max)) 537 538 rq->max = rq->num; 538 539 virtqueue_kick(rq->vq); ··· 639 640 return 0; 640 641 } 641 642 642 - static unsigned int free_old_xmit_skbs(struct send_queue *sq) 643 + static void free_old_xmit_skbs(struct send_queue *sq) 643 644 { 644 645 struct sk_buff *skb; 645 - unsigned int len, tot_sgs = 0; 646 + unsigned int len; 646 647 struct virtnet_info *vi = sq->vq->vdev->priv; 647 648 struct virtnet_stats *stats = this_cpu_ptr(vi->stats); 648 649 ··· 654 655 stats->tx_packets++; 655 656 u64_stats_update_end(&stats->tx_syncp); 656 657 657 - tot_sgs += skb_vnet_hdr(skb)->num_sg; 658 658 dev_kfree_skb_any(skb); 659 659 } 660 - return tot_sgs; 661 660 } 662 661 663 662 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb) ··· 663 666 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb); 664 667 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 665 668 struct virtnet_info *vi = sq->vq->vdev->priv; 669 + unsigned num_sg; 666 670 667 671 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 668 672 ··· 702 704 else 703 705 sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr); 704 706 705 - hdr->num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; 706 - return virtqueue_add_buf(sq->vq, sq->sg, hdr->num_sg, 707 + num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1; 708 + return virtqueue_add_buf(sq->vq, sq->sg, num_sg, 707 709 0, skb, GFP_ATOMIC); 708 710 } 709 711 ··· 712 714 struct virtnet_info *vi = netdev_priv(dev); 713 715 int qnum = skb_get_queue_mapping(skb); 714 716 struct send_queue *sq = &vi->sq[qnum]; 715 - int capacity; 717 + int err; 716 718 717 719 /* Free up any pending old buffers before queueing new ones. */ 718 720 free_old_xmit_skbs(sq); 719 721 720 722 /* Try to transmit */ 721 - capacity = xmit_skb(sq, skb); 723 + err = xmit_skb(sq, skb); 722 724 723 - /* This can happen with OOM and indirect buffers. */ 724 - if (unlikely(capacity < 0)) { 725 - if (likely(capacity == -ENOMEM)) { 726 - if (net_ratelimit()) 727 - dev_warn(&dev->dev, 728 - "TXQ (%d) failure: out of memory\n", 729 - qnum); 730 - } else { 731 - dev->stats.tx_fifo_errors++; 732 - if (net_ratelimit()) 733 - dev_warn(&dev->dev, 734 - "Unexpected TXQ (%d) failure: %d\n", 735 - qnum, capacity); 736 - } 725 + /* This should not happen! */ 726 + if (unlikely(err)) { 727 + dev->stats.tx_fifo_errors++; 728 + if (net_ratelimit()) 729 + dev_warn(&dev->dev, 730 + "Unexpected TXQ (%d) queue failure: %d\n", qnum, err); 737 731 dev->stats.tx_dropped++; 738 732 kfree_skb(skb); 739 733 return NETDEV_TX_OK; ··· 738 748 739 749 /* Apparently nice girls don't return TX_BUSY; stop the queue 740 750 * before it gets out of hand. Naturally, this wastes entries. */ 741 - if (capacity < 2+MAX_SKB_FRAGS) { 751 + if (sq->vq->num_free < 2+MAX_SKB_FRAGS) { 742 752 netif_stop_subqueue(dev, qnum); 743 753 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) { 744 754 /* More just got used, free them then recheck. */ 745 - capacity += free_old_xmit_skbs(sq); 746 - if (capacity >= 2+MAX_SKB_FRAGS) { 755 + free_old_xmit_skbs(sq); 756 + if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) { 747 757 netif_start_subqueue(dev, qnum); 748 758 virtqueue_disable_cb(sq->vq); 749 759 }
+2 -4
drivers/rpmsg/virtio_rpmsg_bus.c
··· 764 764 765 765 /* add message to the remote processor's virtqueue */ 766 766 err = virtqueue_add_buf(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL); 767 - if (err < 0) { 767 + if (err) { 768 768 /* 769 769 * need to reclaim the buffer here, otherwise it's lost 770 770 * (memory won't leak, but rpmsg won't use it again for TX). ··· 776 776 777 777 /* tell the remote processor it has a pending message to read */ 778 778 virtqueue_kick(vrp->svq); 779 - 780 - err = 0; 781 779 out: 782 780 mutex_unlock(&vrp->tx_lock); 783 781 return err; ··· 978 980 979 981 err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, cpu_addr, 980 982 GFP_KERNEL); 981 - WARN_ON(err < 0); /* sanity check; this can't really happen */ 983 + WARN_ON(err); /* sanity check; this can't really happen */ 982 984 } 983 985 984 986 /* suppress "tx-complete" interrupts */
+13 -11
drivers/scsi/virtio_scsi.c
··· 215 215 static int virtscsi_kick_event(struct virtio_scsi *vscsi, 216 216 struct virtio_scsi_event_node *event_node) 217 217 { 218 - int ret; 218 + int err; 219 219 struct scatterlist sg; 220 220 unsigned long flags; 221 221 ··· 223 223 224 224 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags); 225 225 226 - ret = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node, GFP_ATOMIC); 227 - if (ret >= 0) 226 + err = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node, 227 + GFP_ATOMIC); 228 + if (!err) 228 229 virtqueue_kick(vscsi->event_vq.vq); 229 230 230 231 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags); 231 232 232 - return ret; 233 + return err; 233 234 } 234 235 235 236 static int virtscsi_kick_event_all(struct virtio_scsi *vscsi) ··· 411 410 { 412 411 unsigned int out_num, in_num; 413 412 unsigned long flags; 414 - int ret; 413 + int err; 414 + bool needs_kick = false; 415 415 416 416 spin_lock_irqsave(&tgt->tgt_lock, flags); 417 417 virtscsi_map_cmd(tgt, cmd, &out_num, &in_num, req_size, resp_size); 418 418 419 419 spin_lock(&vq->vq_lock); 420 - ret = virtqueue_add_buf(vq->vq, tgt->sg, out_num, in_num, cmd, gfp); 420 + err = virtqueue_add_buf(vq->vq, tgt->sg, out_num, in_num, cmd, gfp); 421 421 spin_unlock(&tgt->tgt_lock); 422 - if (ret >= 0) 423 - ret = virtqueue_kick_prepare(vq->vq); 422 + if (!err) 423 + needs_kick = virtqueue_kick_prepare(vq->vq); 424 424 425 425 spin_unlock_irqrestore(&vq->vq_lock, flags); 426 426 427 - if (ret > 0) 427 + if (needs_kick) 428 428 virtqueue_notify(vq->vq); 429 - return ret; 429 + return err; 430 430 } 431 431 432 432 static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc) ··· 469 467 470 468 if (virtscsi_kick_cmd(tgt, &vscsi->req_vq, cmd, 471 469 sizeof cmd->req.cmd, sizeof cmd->resp.cmd, 472 - GFP_ATOMIC) >= 0) 470 + GFP_ATOMIC) == 0) 473 471 ret = 0; 474 472 else 475 473 mempool_free(cmd, virtscsi_cmd_pool);
+13 -17
drivers/virtio/virtio.c
··· 10 10 static ssize_t device_show(struct device *_d, 11 11 struct device_attribute *attr, char *buf) 12 12 { 13 - struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 13 + struct virtio_device *dev = dev_to_virtio(_d); 14 14 return sprintf(buf, "0x%04x\n", dev->id.device); 15 15 } 16 16 static ssize_t vendor_show(struct device *_d, 17 17 struct device_attribute *attr, char *buf) 18 18 { 19 - struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 19 + struct virtio_device *dev = dev_to_virtio(_d); 20 20 return sprintf(buf, "0x%04x\n", dev->id.vendor); 21 21 } 22 22 static ssize_t status_show(struct device *_d, 23 23 struct device_attribute *attr, char *buf) 24 24 { 25 - struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 25 + struct virtio_device *dev = dev_to_virtio(_d); 26 26 return sprintf(buf, "0x%08x\n", dev->config->get_status(dev)); 27 27 } 28 28 static ssize_t modalias_show(struct device *_d, 29 29 struct device_attribute *attr, char *buf) 30 30 { 31 - struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 32 - 31 + struct virtio_device *dev = dev_to_virtio(_d); 33 32 return sprintf(buf, "virtio:d%08Xv%08X\n", 34 33 dev->id.device, dev->id.vendor); 35 34 } 36 35 static ssize_t features_show(struct device *_d, 37 36 struct device_attribute *attr, char *buf) 38 37 { 39 - struct virtio_device *dev = container_of(_d, struct virtio_device, dev); 38 + struct virtio_device *dev = dev_to_virtio(_d); 40 39 unsigned int i; 41 40 ssize_t len = 0; 42 41 ··· 70 71 static int virtio_dev_match(struct device *_dv, struct device_driver *_dr) 71 72 { 72 73 unsigned int i; 73 - struct virtio_device *dev = container_of(_dv,struct virtio_device,dev); 74 + struct virtio_device *dev = dev_to_virtio(_dv); 74 75 const struct virtio_device_id *ids; 75 76 76 - ids = container_of(_dr, struct virtio_driver, driver)->id_table; 77 + ids = drv_to_virtio(_dr)->id_table; 77 78 for (i = 0; ids[i].device; i++) 78 79 if (virtio_id_match(dev, &ids[i])) 79 80 return 1; ··· 82 83 83 84 static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env) 84 85 { 85 - struct virtio_device *dev = container_of(_dv,struct virtio_device,dev); 86 + struct virtio_device *dev = dev_to_virtio(_dv); 86 87 87 88 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X", 88 89 dev->id.device, dev->id.vendor); ··· 97 98 unsigned int fbit) 98 99 { 99 100 unsigned int i; 100 - struct virtio_driver *drv = container_of(vdev->dev.driver, 101 - struct virtio_driver, driver); 101 + struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver); 102 102 103 103 for (i = 0; i < drv->feature_table_size; i++) 104 104 if (drv->feature_table[i] == fbit) ··· 109 111 static int virtio_dev_probe(struct device *_d) 110 112 { 111 113 int err, i; 112 - struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 113 - struct virtio_driver *drv = container_of(dev->dev.driver, 114 - struct virtio_driver, driver); 114 + struct virtio_device *dev = dev_to_virtio(_d); 115 + struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); 115 116 u32 device_features; 116 117 117 118 /* We have a driver! */ ··· 149 152 150 153 static int virtio_dev_remove(struct device *_d) 151 154 { 152 - struct virtio_device *dev = container_of(_d,struct virtio_device,dev); 153 - struct virtio_driver *drv = container_of(dev->dev.driver, 154 - struct virtio_driver, driver); 155 + struct virtio_device *dev = dev_to_virtio(_d); 156 + struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); 155 157 156 158 drv->remove(dev); 157 159
+3 -4
drivers/virtio/virtio_balloon.c
··· 139 139 struct page *page = balloon_page_enqueue(vb_dev_info); 140 140 141 141 if (!page) { 142 - if (printk_ratelimit()) 143 - dev_printk(KERN_INFO, &vb->vdev->dev, 144 - "Out of puff! Can't get %u pages\n", 145 - VIRTIO_BALLOON_PAGES_PER_PAGE); 142 + dev_info_ratelimited(&vb->vdev->dev, 143 + "Out of puff! Can't get %u pages\n", 144 + VIRTIO_BALLOON_PAGES_PER_PAGE); 146 145 /* Sleep for at least 1/5 of a second before retry. */ 147 146 msleep(200); 148 147 break;
+19 -11
drivers/virtio/virtio_mmio.c
··· 225 225 226 226 /* We write the queue's selector into the notification register to 227 227 * signal the other end */ 228 - writel(virtqueue_get_queue_index(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY); 228 + writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY); 229 229 } 230 230 231 231 /* Notify all virtqueues on an interrupt. */ ··· 266 266 struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev); 267 267 struct virtio_mmio_vq_info *info = vq->priv; 268 268 unsigned long flags, size; 269 - unsigned int index = virtqueue_get_queue_index(vq); 269 + unsigned int index = vq->index; 270 270 271 271 spin_lock_irqsave(&vm_dev->lock, flags); 272 272 list_del(&info->node); ··· 521 521 int err; 522 522 struct resource resources[2] = {}; 523 523 char *str; 524 - long long int base; 524 + long long int base, size; 525 + unsigned int irq; 525 526 int processed, consumed = 0; 526 527 struct platform_device *pdev; 527 528 528 - resources[0].flags = IORESOURCE_MEM; 529 - resources[1].flags = IORESOURCE_IRQ; 529 + /* Consume "size" part of the command line parameter */ 530 + size = memparse(device, &str); 530 531 531 - resources[0].end = memparse(device, &str) - 1; 532 - 532 + /* Get "@<base>:<irq>[:<id>]" chunks */ 533 533 processed = sscanf(str, "@%lli:%u%n:%d%n", 534 - &base, &resources[1].start, &consumed, 534 + &base, &irq, &consumed, 535 535 &vm_cmdline_id, &consumed); 536 536 537 - if (processed < 2 || processed > 3 || str[consumed]) 537 + /* 538 + * sscanf() must processes at least 2 chunks; also there 539 + * must be no extra characters after the last chunk, so 540 + * str[consumed] must be '\0' 541 + */ 542 + if (processed < 2 || str[consumed]) 538 543 return -EINVAL; 539 544 545 + resources[0].flags = IORESOURCE_MEM; 540 546 resources[0].start = base; 541 - resources[0].end += base; 542 - resources[1].end = resources[1].start; 547 + resources[0].end = base + size - 1; 548 + 549 + resources[1].flags = IORESOURCE_IRQ; 550 + resources[1].start = resources[1].end = irq; 543 551 544 552 if (!vm_cmdline_parent_registered) { 545 553 err = device_register(&vm_cmdline_parent);
+3 -17
drivers/virtio/virtio_pci.c
··· 203 203 204 204 /* we write the queue's selector into the notification register to 205 205 * signal the other end */ 206 - iowrite16(virtqueue_get_queue_index(vq), 207 - vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY); 206 + iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY); 208 207 } 209 208 210 209 /* Handle a configuration change: Tell driver if it wants to know. */ ··· 478 479 list_del(&info->node); 479 480 spin_unlock_irqrestore(&vp_dev->lock, flags); 480 481 481 - iowrite16(virtqueue_get_queue_index(vq), 482 - vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); 482 + iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); 483 483 484 484 if (vp_dev->msix_enabled) { 485 485 iowrite16(VIRTIO_MSI_NO_VECTOR, ··· 828 830 #endif 829 831 }; 830 832 831 - static int __init virtio_pci_init(void) 832 - { 833 - return pci_register_driver(&virtio_pci_driver); 834 - } 835 - 836 - module_init(virtio_pci_init); 837 - 838 - static void __exit virtio_pci_exit(void) 839 - { 840 - pci_unregister_driver(&virtio_pci_driver); 841 - } 842 - 843 - module_exit(virtio_pci_exit); 833 + module_pci_driver(virtio_pci_driver);
+19 -27
drivers/virtio/virtio_ring.c
··· 93 93 /* Host publishes avail event idx */ 94 94 bool event; 95 95 96 - /* Number of free buffers */ 97 - unsigned int num_free; 98 96 /* Head of free buffer list. */ 99 97 unsigned int free_head; 100 98 /* Number we've added since last sync. */ ··· 103 105 104 106 /* How to notify other side. FIXME: commonalize hcalls! */ 105 107 void (*notify)(struct virtqueue *vq); 106 - 107 - /* Index of the queue */ 108 - int queue_index; 109 108 110 109 #ifdef DEBUG 111 110 /* They're supposed to lock for us. */ ··· 130 135 unsigned head; 131 136 int i; 132 137 138 + /* 139 + * We require lowmem mappings for the descriptors because 140 + * otherwise virt_to_phys will give us bogus addresses in the 141 + * virtqueue. 142 + */ 143 + gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH); 144 + 133 145 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp); 134 146 if (!desc) 135 147 return -ENOMEM; ··· 162 160 desc[i-1].next = 0; 163 161 164 162 /* We're about to use a buffer */ 165 - vq->num_free--; 163 + vq->vq.num_free--; 166 164 167 165 /* Use a single buffer which doesn't continue */ 168 166 head = vq->free_head; ··· 176 174 return head; 177 175 } 178 176 179 - int virtqueue_get_queue_index(struct virtqueue *_vq) 180 - { 181 - struct vring_virtqueue *vq = to_vvq(_vq); 182 - return vq->queue_index; 183 - } 184 - EXPORT_SYMBOL_GPL(virtqueue_get_queue_index); 185 - 186 177 /** 187 178 * virtqueue_add_buf - expose buffer to other end 188 179 * @vq: the struct virtqueue we're talking about. ··· 188 193 * Caller must ensure we don't call this with other virtqueue operations 189 194 * at the same time (except where noted). 190 195 * 191 - * Returns remaining capacity of queue or a negative error 192 - * (ie. ENOSPC). Note that it only really makes sense to treat all 193 - * positive return values as "available": indirect buffers mean that 194 - * we can put an entire sg[] array inside a single queue entry. 196 + * Returns zero or a negative error (ie. ENOSPC, ENOMEM). 195 197 */ 196 198 int virtqueue_add_buf(struct virtqueue *_vq, 197 199 struct scatterlist sg[], ··· 220 228 221 229 /* If the host supports indirect descriptor tables, and we have multiple 222 230 * buffers, then go indirect. FIXME: tune this threshold */ 223 - if (vq->indirect && (out + in) > 1 && vq->num_free) { 231 + if (vq->indirect && (out + in) > 1 && vq->vq.num_free) { 224 232 head = vring_add_indirect(vq, sg, out, in, gfp); 225 233 if (likely(head >= 0)) 226 234 goto add_head; ··· 229 237 BUG_ON(out + in > vq->vring.num); 230 238 BUG_ON(out + in == 0); 231 239 232 - if (vq->num_free < out + in) { 240 + if (vq->vq.num_free < out + in) { 233 241 pr_debug("Can't add buf len %i - avail = %i\n", 234 - out + in, vq->num_free); 242 + out + in, vq->vq.num_free); 235 243 /* FIXME: for historical reasons, we force a notify here if 236 244 * there are outgoing parts to the buffer. Presumably the 237 245 * host should service the ring ASAP. */ ··· 242 250 } 243 251 244 252 /* We're about to use some buffers from the free list. */ 245 - vq->num_free -= out + in; 253 + vq->vq.num_free -= out + in; 246 254 247 255 head = vq->free_head; 248 256 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) { ··· 288 296 pr_debug("Added buffer head %i to %p\n", head, vq); 289 297 END_USE(vq); 290 298 291 - return vq->num_free; 299 + return 0; 292 300 } 293 301 EXPORT_SYMBOL_GPL(virtqueue_add_buf); 294 302 ··· 385 393 386 394 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) { 387 395 i = vq->vring.desc[i].next; 388 - vq->num_free++; 396 + vq->vq.num_free++; 389 397 } 390 398 391 399 vq->vring.desc[i].next = vq->free_head; 392 400 vq->free_head = head; 393 401 /* Plus final descriptor */ 394 - vq->num_free++; 402 + vq->vq.num_free++; 395 403 } 396 404 397 405 static inline bool more_used(const struct vring_virtqueue *vq) ··· 591 599 return buf; 592 600 } 593 601 /* That should have freed everything. */ 594 - BUG_ON(vq->num_free != vq->vring.num); 602 + BUG_ON(vq->vq.num_free != vq->vring.num); 595 603 596 604 END_USE(vq); 597 605 return NULL; ··· 645 653 vq->vq.callback = callback; 646 654 vq->vq.vdev = vdev; 647 655 vq->vq.name = name; 656 + vq->vq.num_free = num; 657 + vq->vq.index = index; 648 658 vq->notify = notify; 649 659 vq->weak_barriers = weak_barriers; 650 660 vq->broken = false; 651 661 vq->last_used_idx = 0; 652 662 vq->num_added = 0; 653 - vq->queue_index = index; 654 663 list_add_tail(&vq->vq.list, &vdev->vqs); 655 664 #ifdef DEBUG 656 665 vq->in_use = false; ··· 666 673 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT; 667 674 668 675 /* Put everything in free lists. */ 669 - vq->num_free = num; 670 676 vq->free_head = 0; 671 677 for (i = 0; i < num-1; i++) { 672 678 vq->vring.desc[i].next = i+1;
+23 -2
include/linux/virtio.h
··· 16 16 * @name: the name of this virtqueue (mainly for debugging) 17 17 * @vdev: the virtio device this queue was created for. 18 18 * @priv: a pointer for the virtqueue implementation to use. 19 + * @index: the zero-based ordinal number for this queue. 20 + * @num_free: number of elements we expect to be able to fit. 21 + * 22 + * A note on @num_free: with indirect buffers, each buffer needs one 23 + * element in the queue, otherwise a buffer will need one element per 24 + * sg element. 19 25 */ 20 26 struct virtqueue { 21 27 struct list_head list; 22 28 void (*callback)(struct virtqueue *vq); 23 29 const char *name; 24 30 struct virtio_device *vdev; 31 + unsigned int index; 32 + unsigned int num_free; 25 33 void *priv; 26 34 }; 27 35 ··· 58 50 59 51 unsigned int virtqueue_get_vring_size(struct virtqueue *vq); 60 52 61 - int virtqueue_get_queue_index(struct virtqueue *vq); 53 + /* FIXME: Obsolete accessor, but required for virtio_net merge. */ 54 + static inline unsigned int virtqueue_get_queue_index(struct virtqueue *vq) 55 + { 56 + return vq->index; 57 + } 62 58 63 59 /** 64 60 * virtio_device - representation of a device using virtio ··· 85 73 void *priv; 86 74 }; 87 75 88 - #define dev_to_virtio(dev) container_of(dev, struct virtio_device, dev) 76 + static inline struct virtio_device *dev_to_virtio(struct device *_dev) 77 + { 78 + return container_of(_dev, struct virtio_device, dev); 79 + } 80 + 89 81 int register_virtio_device(struct virtio_device *dev); 90 82 void unregister_virtio_device(struct virtio_device *dev); 91 83 ··· 118 102 int (*restore)(struct virtio_device *dev); 119 103 #endif 120 104 }; 105 + 106 + static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv) 107 + { 108 + return container_of(drv, struct virtio_driver, driver); 109 + } 121 110 122 111 int register_virtio_driver(struct virtio_driver *drv); 123 112 void unregister_virtio_driver(struct virtio_driver *drv);
+26 -2
include/linux/virtio_scsi.h
··· 1 + /* 2 + * This header is BSD licensed so anyone can use the definitions to implement 3 + * compatible drivers/servers. 4 + * 5 + * Redistribution and use in source and binary forms, with or without 6 + * modification, are permitted provided that the following conditions 7 + * are met: 8 + * 1. Redistributions of source code must retain the above copyright 9 + * notice, this list of conditions and the following disclaimer. 10 + * 2. Redistributions in binary form must reproduce the above copyright 11 + * notice, this list of conditions and the following disclaimer in the 12 + * documentation and/or other materials provided with the distribution. 13 + * 14 + * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 + * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 18 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 + * SUCH DAMAGE. 25 + */ 26 + 1 27 #ifndef _LINUX_VIRTIO_SCSI_H 2 28 #define _LINUX_VIRTIO_SCSI_H 3 - /* This header is BSD licensed so anyone can use the definitions to implement 4 - * compatible drivers/servers. */ 5 29 6 30 #define VIRTIO_SCSI_CDB_SIZE 32 7 31 #define VIRTIO_SCSI_SENSE_SIZE 96
+1
include/uapi/linux/virtio_ids.h
··· 37 37 #define VIRTIO_ID_RPMSG 7 /* virtio remote processor messaging */ 38 38 #define VIRTIO_ID_SCSI 8 /* virtio scsi */ 39 39 #define VIRTIO_ID_9P 9 /* 9p virtio console */ 40 + #define VIRTIO_ID_RPROC_SERIAL 11 /* virtio remoteproc serial link */ 40 41 41 42 #endif /* _LINUX_VIRTIO_IDS_H */
+1
mm/highmem.c
··· 105 105 106 106 return virt_to_page(addr); 107 107 } 108 + EXPORT_SYMBOL(kmap_to_page); 108 109 109 110 static void flush_all_zero_pkmaps(void) 110 111 {
+2 -1
net/9p/trans_virtio.c
··· 39 39 #include <linux/inet.h> 40 40 #include <linux/idr.h> 41 41 #include <linux/file.h> 42 + #include <linux/highmem.h> 42 43 #include <linux/slab.h> 43 44 #include <net/9p/9p.h> 44 45 #include <linux/parser.h> ··· 326 325 int count = nr_pages; 327 326 while (nr_pages) { 328 327 s = rest_of_page(data); 329 - pages[index++] = virt_to_page(data); 328 + pages[index++] = kmap_to_page(data); 330 329 data += s; 331 330 nr_pages--; 332 331 }
+35 -49
tools/lguest/lguest.c
··· 179 179 #define wmb() __asm__ __volatile__("" : : : "memory") 180 180 #define mb() __asm__ __volatile__("" : : : "memory") 181 181 182 - /* 183 - * Convert an iovec element to the given type. 184 - * 185 - * This is a fairly ugly trick: we need to know the size of the type and 186 - * alignment requirement to check the pointer is kosher. It's also nice to 187 - * have the name of the type in case we report failure. 188 - * 189 - * Typing those three things all the time is cumbersome and error prone, so we 190 - * have a macro which sets them all up and passes to the real function. 191 - */ 192 - #define convert(iov, type) \ 193 - ((type *)_convert((iov), sizeof(type), __alignof__(type), #type)) 194 - 195 - static void *_convert(struct iovec *iov, size_t size, size_t align, 196 - const char *name) 197 - { 198 - if (iov->iov_len != size) 199 - errx(1, "Bad iovec size %zu for %s", iov->iov_len, name); 200 - if ((unsigned long)iov->iov_base % align != 0) 201 - errx(1, "Bad alignment %p for %s", iov->iov_base, name); 202 - return iov->iov_base; 203 - } 204 - 205 182 /* Wrapper for the last available index. Makes it easier to change. */ 206 183 #define lg_last_avail(vq) ((vq)->last_avail_idx) 207 184 ··· 205 228 } 206 229 207 230 /* Take len bytes from the front of this iovec. */ 208 - static void iov_consume(struct iovec iov[], unsigned num_iov, unsigned len) 231 + static void iov_consume(struct iovec iov[], unsigned num_iov, 232 + void *dest, unsigned len) 209 233 { 210 234 unsigned int i; 211 235 ··· 214 236 unsigned int used; 215 237 216 238 used = iov[i].iov_len < len ? iov[i].iov_len : len; 239 + if (dest) { 240 + memcpy(dest, iov[i].iov_base, used); 241 + dest += used; 242 + } 217 243 iov[i].iov_base += used; 218 244 iov[i].iov_len -= used; 219 245 len -= used; 220 246 } 221 - assert(len == 0); 247 + if (len != 0) 248 + errx(1, "iovec too short!"); 222 249 } 223 250 224 251 /* The device virtqueue descriptors are followed by feature bitmasks. */ ··· 847 864 warn("Write to stdout gave %i (%d)", len, errno); 848 865 break; 849 866 } 850 - iov_consume(iov, out, len); 867 + iov_consume(iov, out, NULL, len); 851 868 } 852 869 853 870 /* ··· 1574 1591 { 1575 1592 struct vblk_info *vblk = vq->dev->priv; 1576 1593 unsigned int head, out_num, in_num, wlen; 1577 - int ret; 1594 + int ret, i; 1578 1595 u8 *in; 1579 - struct virtio_blk_outhdr *out; 1596 + struct virtio_blk_outhdr out; 1580 1597 struct iovec iov[vq->vring.num]; 1581 1598 off64_t off; 1582 1599 ··· 1586 1603 */ 1587 1604 head = wait_for_vq_desc(vq, iov, &out_num, &in_num); 1588 1605 1589 - /* 1590 - * Every block request should contain at least one output buffer 1591 - * (detailing the location on disk and the type of request) and one 1592 - * input buffer (to hold the result). 1593 - */ 1594 - if (out_num == 0 || in_num == 0) 1595 - errx(1, "Bad virtblk cmd %u out=%u in=%u", 1596 - head, out_num, in_num); 1606 + /* Copy the output header from the front of the iov (adjusts iov) */ 1607 + iov_consume(iov, out_num, &out, sizeof(out)); 1597 1608 1598 - out = convert(&iov[0], struct virtio_blk_outhdr); 1599 - in = convert(&iov[out_num+in_num-1], u8); 1609 + /* Find and trim end of iov input array, for our status byte. */ 1610 + in = NULL; 1611 + for (i = out_num + in_num - 1; i >= out_num; i--) { 1612 + if (iov[i].iov_len > 0) { 1613 + in = iov[i].iov_base + iov[i].iov_len - 1; 1614 + iov[i].iov_len--; 1615 + break; 1616 + } 1617 + } 1618 + if (!in) 1619 + errx(1, "Bad virtblk cmd with no room for status"); 1620 + 1600 1621 /* 1601 1622 * For historical reasons, block operations are expressed in 512 byte 1602 1623 * "sectors". 1603 1624 */ 1604 - off = out->sector * 512; 1625 + off = out.sector * 512; 1605 1626 1606 1627 /* 1607 1628 * In general the virtio block driver is allowed to try SCSI commands. 1608 1629 * It'd be nice if we supported eject, for example, but we don't. 1609 1630 */ 1610 - if (out->type & VIRTIO_BLK_T_SCSI_CMD) { 1631 + if (out.type & VIRTIO_BLK_T_SCSI_CMD) { 1611 1632 fprintf(stderr, "Scsi commands unsupported\n"); 1612 1633 *in = VIRTIO_BLK_S_UNSUPP; 1613 1634 wlen = sizeof(*in); 1614 - } else if (out->type & VIRTIO_BLK_T_OUT) { 1635 + } else if (out.type & VIRTIO_BLK_T_OUT) { 1615 1636 /* 1616 1637 * Write 1617 1638 * ··· 1623 1636 * if they try to write past end. 1624 1637 */ 1625 1638 if (lseek64(vblk->fd, off, SEEK_SET) != off) 1626 - err(1, "Bad seek to sector %llu", out->sector); 1639 + err(1, "Bad seek to sector %llu", out.sector); 1627 1640 1628 - ret = writev(vblk->fd, iov+1, out_num-1); 1629 - verbose("WRITE to sector %llu: %i\n", out->sector, ret); 1641 + ret = writev(vblk->fd, iov, out_num); 1642 + verbose("WRITE to sector %llu: %i\n", out.sector, ret); 1630 1643 1631 1644 /* 1632 1645 * Grr... Now we know how long the descriptor they sent was, we ··· 1642 1655 1643 1656 wlen = sizeof(*in); 1644 1657 *in = (ret >= 0 ? VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR); 1645 - } else if (out->type & VIRTIO_BLK_T_FLUSH) { 1658 + } else if (out.type & VIRTIO_BLK_T_FLUSH) { 1646 1659 /* Flush */ 1647 1660 ret = fdatasync(vblk->fd); 1648 1661 verbose("FLUSH fdatasync: %i\n", ret); ··· 1656 1669 * if they try to read past end. 1657 1670 */ 1658 1671 if (lseek64(vblk->fd, off, SEEK_SET) != off) 1659 - err(1, "Bad seek to sector %llu", out->sector); 1672 + err(1, "Bad seek to sector %llu", out.sector); 1660 1673 1661 - ret = readv(vblk->fd, iov+1, in_num-1); 1662 - verbose("READ from sector %llu: %i\n", out->sector, ret); 1674 + ret = readv(vblk->fd, iov + out_num, in_num); 1663 1675 if (ret >= 0) { 1664 1676 wlen = sizeof(*in) + ret; 1665 1677 *in = VIRTIO_BLK_S_OK; ··· 1744 1758 len = readv(rng_info->rfd, iov, in_num); 1745 1759 if (len <= 0) 1746 1760 err(1, "Read from /dev/random gave %i", len); 1747 - iov_consume(iov, in_num, len); 1761 + iov_consume(iov, in_num, NULL, len); 1748 1762 totlen += len; 1749 1763 } 1750 1764
+2 -2
tools/virtio/virtio_test.c
··· 164 164 r = virtqueue_add_buf(vq->vq, &sl, 1, 0, 165 165 dev->buf + started, 166 166 GFP_ATOMIC); 167 - if (likely(r >= 0)) { 167 + if (likely(r == 0)) { 168 168 ++started; 169 169 virtqueue_kick(vq->vq); 170 170 } ··· 177 177 r = 0; 178 178 } 179 179 180 - } while (r >= 0); 180 + } while (r == 0); 181 181 if (completed == completed_before) 182 182 ++spurious; 183 183 assert(completed <= bufs);