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

Merge branch 'stable/for-linus-5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/swiotlb

Pull swiotlb updates from Konrad Rzeszutek Wilk:
"Cleanups in the swiotlb code and extra debugfs knobs to help with the
field diagnostics"

* 'stable/for-linus-5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/swiotlb:
swiotlb-xen: ensure we have a single callsite for xen_dma_map_page
swiotlb-xen: simplify the DMA sync method implementations
swiotlb-xen: use ->map_page to implement ->map_sg
swiotlb-xen: make instances match their method names
swiotlb: save io_tlb_used to local variable before leaving critical section
swiotlb: dump used and total slots when swiotlb buffer is full

+64 -138
+59 -137
drivers/xen/swiotlb-xen.c
··· 391 391 if (dma_capable(dev, dev_addr, size) && 392 392 !range_straddles_page_boundary(phys, size) && 393 393 !xen_arch_need_swiotlb(dev, phys, dev_addr) && 394 - (swiotlb_force != SWIOTLB_FORCE)) { 395 - /* we are not interested in the dma_addr returned by 396 - * xen_dma_map_page, only in the potential cache flushes executed 397 - * by the function. */ 398 - xen_dma_map_page(dev, page, dev_addr, offset, size, dir, attrs); 399 - return dev_addr; 400 - } 394 + swiotlb_force != SWIOTLB_FORCE) 395 + goto done; 401 396 402 397 /* 403 398 * Oh well, have to allocate and map a bounce buffer. ··· 405 410 return DMA_MAPPING_ERROR; 406 411 407 412 dev_addr = xen_phys_to_bus(map); 408 - xen_dma_map_page(dev, pfn_to_page(map >> PAGE_SHIFT), 409 - dev_addr, map & ~PAGE_MASK, size, dir, attrs); 410 413 411 414 /* 412 415 * Ensure that the address returned is DMA'ble 413 416 */ 414 - if (dma_capable(dev, dev_addr, size)) 415 - return dev_addr; 417 + if (unlikely(!dma_capable(dev, dev_addr, size))) { 418 + swiotlb_tbl_unmap_single(dev, map, size, dir, 419 + attrs | DMA_ATTR_SKIP_CPU_SYNC); 420 + return DMA_MAPPING_ERROR; 421 + } 416 422 417 - attrs |= DMA_ATTR_SKIP_CPU_SYNC; 418 - swiotlb_tbl_unmap_single(dev, map, size, dir, attrs); 419 - 420 - return DMA_MAPPING_ERROR; 423 + page = pfn_to_page(map >> PAGE_SHIFT); 424 + offset = map & ~PAGE_MASK; 425 + done: 426 + /* 427 + * we are not interested in the dma_addr returned by xen_dma_map_page, 428 + * only in the potential cache flushes executed by the function. 429 + */ 430 + xen_dma_map_page(dev, page, dev_addr, offset, size, dir, attrs); 431 + return dev_addr; 421 432 } 422 433 423 434 /* ··· 456 455 xen_unmap_single(hwdev, dev_addr, size, dir, attrs); 457 456 } 458 457 459 - /* 460 - * Make physical memory consistent for a single streaming mode DMA translation 461 - * after a transfer. 462 - * 463 - * If you perform a xen_swiotlb_map_page() but wish to interrogate the buffer 464 - * using the cpu, yet do not wish to teardown the dma mapping, you must 465 - * call this function before doing so. At the next point you give the dma 466 - * address back to the card, you must first perform a 467 - * xen_swiotlb_dma_sync_for_device, and then the device again owns the buffer 468 - */ 469 458 static void 470 - xen_swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr, 471 - size_t size, enum dma_data_direction dir, 472 - enum dma_sync_target target) 459 + xen_swiotlb_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, 460 + size_t size, enum dma_data_direction dir) 473 461 { 474 - phys_addr_t paddr = xen_bus_to_phys(dev_addr); 462 + phys_addr_t paddr = xen_bus_to_phys(dma_addr); 475 463 476 - BUG_ON(dir == DMA_NONE); 464 + xen_dma_sync_single_for_cpu(dev, dma_addr, size, dir); 477 465 478 - if (target == SYNC_FOR_CPU) 479 - xen_dma_sync_single_for_cpu(hwdev, dev_addr, size, dir); 480 - 481 - /* NOTE: We use dev_addr here, not paddr! */ 482 - if (is_xen_swiotlb_buffer(dev_addr)) 483 - swiotlb_tbl_sync_single(hwdev, paddr, size, dir, target); 484 - 485 - if (target == SYNC_FOR_DEVICE) 486 - xen_dma_sync_single_for_device(hwdev, dev_addr, size, dir); 466 + if (is_xen_swiotlb_buffer(dma_addr)) 467 + swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_CPU); 487 468 } 488 469 489 - void 490 - xen_swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr, 491 - size_t size, enum dma_data_direction dir) 470 + static void 471 + xen_swiotlb_sync_single_for_device(struct device *dev, dma_addr_t dma_addr, 472 + size_t size, enum dma_data_direction dir) 492 473 { 493 - xen_swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU); 494 - } 474 + phys_addr_t paddr = xen_bus_to_phys(dma_addr); 495 475 496 - void 497 - xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, 498 - size_t size, enum dma_data_direction dir) 499 - { 500 - xen_swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE); 476 + if (is_xen_swiotlb_buffer(dma_addr)) 477 + swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_DEVICE); 478 + 479 + xen_dma_sync_single_for_device(dev, dma_addr, size, dir); 501 480 } 502 481 503 482 /* ··· 485 504 * concerning calls here are the same as for swiotlb_unmap_page() above. 486 505 */ 487 506 static void 488 - xen_swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl, 489 - int nelems, enum dma_data_direction dir, 490 - unsigned long attrs) 507 + xen_swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems, 508 + enum dma_data_direction dir, unsigned long attrs) 491 509 { 492 510 struct scatterlist *sg; 493 511 int i; ··· 498 518 499 519 } 500 520 501 - /* 502 - * Map a set of buffers described by scatterlist in streaming mode for DMA. 503 - * This is the scatter-gather version of the above xen_swiotlb_map_page 504 - * interface. Here the scatter gather list elements are each tagged with the 505 - * appropriate dma address and length. They are obtained via 506 - * sg_dma_{address,length}(SG). 507 - * 508 - * NOTE: An implementation may be able to use a smaller number of 509 - * DMA address/length pairs than there are SG table elements. 510 - * (for example via virtual mapping capabilities) 511 - * The routine returns the number of addr/length pairs actually 512 - * used, at most nents. 513 - * 514 - * Device ownership issues as mentioned above for xen_swiotlb_map_page are the 515 - * same here. 516 - */ 517 521 static int 518 - xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, 519 - int nelems, enum dma_data_direction dir, 520 - unsigned long attrs) 522 + xen_swiotlb_map_sg(struct device *dev, struct scatterlist *sgl, int nelems, 523 + enum dma_data_direction dir, unsigned long attrs) 521 524 { 522 525 struct scatterlist *sg; 523 526 int i; ··· 508 545 BUG_ON(dir == DMA_NONE); 509 546 510 547 for_each_sg(sgl, sg, nelems, i) { 511 - phys_addr_t paddr = sg_phys(sg); 512 - dma_addr_t dev_addr = xen_phys_to_bus(paddr); 513 - 514 - if (swiotlb_force == SWIOTLB_FORCE || 515 - xen_arch_need_swiotlb(hwdev, paddr, dev_addr) || 516 - !dma_capable(hwdev, dev_addr, sg->length) || 517 - range_straddles_page_boundary(paddr, sg->length)) { 518 - phys_addr_t map = swiotlb_tbl_map_single(hwdev, 519 - start_dma_addr, 520 - sg_phys(sg), 521 - sg->length, 522 - dir, attrs); 523 - if (map == DMA_MAPPING_ERROR) { 524 - dev_warn(hwdev, "swiotlb buffer is full\n"); 525 - /* Don't panic here, we expect map_sg users 526 - to do proper error handling. */ 527 - attrs |= DMA_ATTR_SKIP_CPU_SYNC; 528 - xen_swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir, 529 - attrs); 530 - sg_dma_len(sgl) = 0; 531 - return 0; 532 - } 533 - dev_addr = xen_phys_to_bus(map); 534 - xen_dma_map_page(hwdev, pfn_to_page(map >> PAGE_SHIFT), 535 - dev_addr, 536 - map & ~PAGE_MASK, 537 - sg->length, 538 - dir, 539 - attrs); 540 - sg->dma_address = dev_addr; 541 - } else { 542 - /* we are not interested in the dma_addr returned by 543 - * xen_dma_map_page, only in the potential cache flushes executed 544 - * by the function. */ 545 - xen_dma_map_page(hwdev, pfn_to_page(paddr >> PAGE_SHIFT), 546 - dev_addr, 547 - paddr & ~PAGE_MASK, 548 - sg->length, 549 - dir, 550 - attrs); 551 - sg->dma_address = dev_addr; 552 - } 548 + sg->dma_address = xen_swiotlb_map_page(dev, sg_page(sg), 549 + sg->offset, sg->length, dir, attrs); 550 + if (sg->dma_address == DMA_MAPPING_ERROR) 551 + goto out_unmap; 553 552 sg_dma_len(sg) = sg->length; 554 553 } 554 + 555 555 return nelems; 556 + out_unmap: 557 + xen_swiotlb_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC); 558 + sg_dma_len(sgl) = 0; 559 + return 0; 556 560 } 557 561 558 - /* 559 - * Make physical memory consistent for a set of streaming mode DMA translations 560 - * after a transfer. 561 - * 562 - * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules 563 - * and usage. 564 - */ 565 562 static void 566 - xen_swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl, 567 - int nelems, enum dma_data_direction dir, 568 - enum dma_sync_target target) 563 + xen_swiotlb_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl, 564 + int nelems, enum dma_data_direction dir) 569 565 { 570 566 struct scatterlist *sg; 571 567 int i; 572 568 573 - for_each_sg(sgl, sg, nelems, i) 574 - xen_swiotlb_sync_single(hwdev, sg->dma_address, 575 - sg_dma_len(sg), dir, target); 569 + for_each_sg(sgl, sg, nelems, i) { 570 + xen_swiotlb_sync_single_for_cpu(dev, sg->dma_address, 571 + sg->length, dir); 572 + } 576 573 } 577 574 578 575 static void 579 - xen_swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg, 580 - int nelems, enum dma_data_direction dir) 581 - { 582 - xen_swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU); 583 - } 584 - 585 - static void 586 - xen_swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg, 576 + xen_swiotlb_sync_sg_for_device(struct device *dev, struct scatterlist *sgl, 587 577 int nelems, enum dma_data_direction dir) 588 578 { 589 - xen_swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE); 579 + struct scatterlist *sg; 580 + int i; 581 + 582 + for_each_sg(sgl, sg, nelems, i) { 583 + xen_swiotlb_sync_single_for_device(dev, sg->dma_address, 584 + sg->length, dir); 585 + } 590 586 } 591 587 592 588 /* ··· 612 690 .sync_single_for_device = xen_swiotlb_sync_single_for_device, 613 691 .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu, 614 692 .sync_sg_for_device = xen_swiotlb_sync_sg_for_device, 615 - .map_sg = xen_swiotlb_map_sg_attrs, 616 - .unmap_sg = xen_swiotlb_unmap_sg_attrs, 693 + .map_sg = xen_swiotlb_map_sg, 694 + .unmap_sg = xen_swiotlb_unmap_sg, 617 695 .map_page = xen_swiotlb_map_page, 618 696 .unmap_page = xen_swiotlb_unmap_page, 619 697 .dma_supported = xen_swiotlb_dma_supported,
+5 -1
kernel/dma/swiotlb.c
··· 452 452 unsigned long mask; 453 453 unsigned long offset_slots; 454 454 unsigned long max_slots; 455 + unsigned long tmp_io_tlb_used; 455 456 456 457 if (no_iotlb_memory) 457 458 panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer"); ··· 539 538 } while (index != wrap); 540 539 541 540 not_found: 541 + tmp_io_tlb_used = io_tlb_used; 542 + 542 543 spin_unlock_irqrestore(&io_tlb_lock, flags); 543 544 if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit()) 544 - dev_warn(hwdev, "swiotlb buffer is full (sz: %zd bytes)\n", size); 545 + dev_warn(hwdev, "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n", 546 + size, io_tlb_nslabs, tmp_io_tlb_used); 545 547 return DMA_MAPPING_ERROR; 546 548 found: 547 549 io_tlb_used += nslots;