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

scsi: target: core: Change the way target_xcopy_do_work() sets restiction on max I/O

To determine how many blocks sends in one command, the minimum value is
selected from the hw_max_sectors of both devices. In target_xcopy_do_work,
hw_max_sectors are used as blocks, not sectors; it also ignores the fact
that sectors can be of different sizes, for example 512 and 4096
bytes. Because of this, a number of blocks can be transmitted that the
device will not be able to accept.

Change the selection of max transmission size into bytes.

Reviewed-by: Konstantin Shelekhin <k.shelekhin@yadro.com>
Reviewed-by: Dmitriy Bogdanov <d.bogdanov@yadro.com>
Signed-off-by: Anastasia Kovaleva <a.kovaleva@yadro.com>
Link: https://lore.kernel.org/r/20221114102500.88892-4-a.kovaleva@yadro.com
Reviewed-by: Mike Christie <michael.christie@oracle.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Anastasia Kovaleva and committed by
Martin K. Petersen
689d94ec 9375031e

+54 -45
+53 -44
drivers/target/target_core_xcopy.c
··· 582 582 struct xcopy_op *xop, 583 583 struct se_device *src_dev, 584 584 sector_t src_lba, 585 - u32 src_sectors) 585 + u32 src_bytes) 586 586 { 587 587 struct xcopy_pt_cmd xpt_cmd; 588 588 struct se_cmd *se_cmd = &xpt_cmd.se_cmd; 589 - u32 length = (src_sectors * src_dev->dev_attrib.block_size); 589 + u32 transfer_length_block = src_bytes / src_dev->dev_attrib.block_size; 590 590 int rc; 591 591 unsigned char cdb[16]; 592 592 bool remote_port = (xop->op_origin == XCOL_DEST_RECV_OP); ··· 597 597 memset(&cdb[0], 0, 16); 598 598 cdb[0] = READ_16; 599 599 put_unaligned_be64(src_lba, &cdb[2]); 600 - put_unaligned_be32(src_sectors, &cdb[10]); 601 - pr_debug("XCOPY: Built READ_16: LBA: %llu Sectors: %u Length: %u\n", 602 - (unsigned long long)src_lba, src_sectors, length); 600 + put_unaligned_be32(transfer_length_block, &cdb[10]); 601 + pr_debug("XCOPY: Built READ_16: LBA: %llu Blocks: %u Length: %u\n", 602 + (unsigned long long)src_lba, transfer_length_block, src_bytes); 603 603 604 - __target_init_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, length, 604 + __target_init_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, src_bytes, 605 605 DMA_FROM_DEVICE, 0, &xpt_cmd.sense_buffer[0], 0); 606 606 607 607 rc = target_xcopy_setup_pt_cmd(&xpt_cmd, xop, src_dev, &cdb[0], ··· 627 627 struct xcopy_op *xop, 628 628 struct se_device *dst_dev, 629 629 sector_t dst_lba, 630 - u32 dst_sectors) 630 + u32 dst_bytes) 631 631 { 632 632 struct xcopy_pt_cmd xpt_cmd; 633 633 struct se_cmd *se_cmd = &xpt_cmd.se_cmd; 634 - u32 length = (dst_sectors * dst_dev->dev_attrib.block_size); 634 + u32 transfer_length_block = dst_bytes / dst_dev->dev_attrib.block_size; 635 635 int rc; 636 636 unsigned char cdb[16]; 637 637 bool remote_port = (xop->op_origin == XCOL_SOURCE_RECV_OP); ··· 642 642 memset(&cdb[0], 0, 16); 643 643 cdb[0] = WRITE_16; 644 644 put_unaligned_be64(dst_lba, &cdb[2]); 645 - put_unaligned_be32(dst_sectors, &cdb[10]); 646 - pr_debug("XCOPY: Built WRITE_16: LBA: %llu Sectors: %u Length: %u\n", 647 - (unsigned long long)dst_lba, dst_sectors, length); 645 + put_unaligned_be32(transfer_length_block, &cdb[10]); 646 + pr_debug("XCOPY: Built WRITE_16: LBA: %llu Blocks: %u Length: %u\n", 647 + (unsigned long long)dst_lba, transfer_length_block, dst_bytes); 648 648 649 - __target_init_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, length, 649 + __target_init_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, dst_bytes, 650 650 DMA_TO_DEVICE, 0, &xpt_cmd.sense_buffer[0], 0); 651 651 652 652 rc = target_xcopy_setup_pt_cmd(&xpt_cmd, xop, dst_dev, &cdb[0], ··· 670 670 struct se_cmd *ec_cmd = xop->xop_se_cmd; 671 671 struct se_device *src_dev, *dst_dev; 672 672 sector_t src_lba, dst_lba, end_lba; 673 - unsigned int max_sectors; 673 + unsigned long long max_bytes, max_bytes_src, max_bytes_dst, max_blocks; 674 674 int rc = 0; 675 - unsigned short nolb, max_nolb, copied_nolb = 0; 675 + unsigned short nolb; 676 + unsigned int copied_bytes = 0; 676 677 sense_reason_t sense_rc; 677 678 678 679 sense_rc = target_parse_xcopy_cmd(xop); ··· 692 691 nolb = xop->nolb; 693 692 end_lba = src_lba + nolb; 694 693 /* 695 - * Break up XCOPY I/O into hw_max_sectors sized I/O based on the 696 - * smallest max_sectors between src_dev + dev_dev, or 694 + * Break up XCOPY I/O into hw_max_sectors * hw_block_size sized 695 + * I/O based on the smallest max_bytes between src_dev + dst_dev 697 696 */ 698 - max_sectors = min(src_dev->dev_attrib.hw_max_sectors, 699 - dst_dev->dev_attrib.hw_max_sectors); 700 - max_sectors = min_t(u32, max_sectors, XCOPY_MAX_SECTORS); 697 + max_bytes_src = (unsigned long long) src_dev->dev_attrib.hw_max_sectors * 698 + src_dev->dev_attrib.hw_block_size; 699 + max_bytes_dst = (unsigned long long) dst_dev->dev_attrib.hw_max_sectors * 700 + dst_dev->dev_attrib.hw_block_size; 701 701 702 - max_nolb = min_t(u16, max_sectors, ((u16)(~0U))); 702 + max_bytes = min_t(u64, max_bytes_src, max_bytes_dst); 703 + max_bytes = min_t(u64, max_bytes, XCOPY_MAX_BYTES); 703 704 704 - pr_debug("target_xcopy_do_work: nolb: %hu, max_nolb: %hu end_lba: %llu\n", 705 - nolb, max_nolb, (unsigned long long)end_lba); 706 - pr_debug("target_xcopy_do_work: Starting src_lba: %llu, dst_lba: %llu\n", 705 + /* 706 + * Using shift instead of the division because otherwise GCC 707 + * generates __udivdi3 that is missing on i386 708 + */ 709 + max_blocks = max_bytes >> ilog2(src_dev->dev_attrib.block_size); 710 + 711 + pr_debug("%s: nolb: %u, max_blocks: %llu end_lba: %llu\n", __func__, 712 + nolb, max_blocks, (unsigned long long)end_lba); 713 + pr_debug("%s: Starting src_lba: %llu, dst_lba: %llu\n", __func__, 707 714 (unsigned long long)src_lba, (unsigned long long)dst_lba); 708 715 709 - while (src_lba < end_lba) { 710 - unsigned short cur_nolb = min(nolb, max_nolb); 711 - u32 cur_bytes = cur_nolb * src_dev->dev_attrib.block_size; 716 + while (nolb) { 717 + u32 cur_bytes = min_t(u64, max_bytes, nolb * src_dev->dev_attrib.block_size); 718 + unsigned short cur_nolb = cur_bytes / src_dev->dev_attrib.block_size; 712 719 713 720 if (cur_bytes != xop->xop_data_bytes) { 714 721 /* ··· 733 724 xop->xop_data_bytes = cur_bytes; 734 725 } 735 726 736 - pr_debug("target_xcopy_do_work: Calling read src_dev: %p src_lba: %llu," 737 - " cur_nolb: %hu\n", src_dev, (unsigned long long)src_lba, cur_nolb); 727 + pr_debug("%s: Calling read src_dev: %p src_lba: %llu, cur_nolb: %hu\n", 728 + __func__, src_dev, (unsigned long long)src_lba, cur_nolb); 738 729 739 - rc = target_xcopy_read_source(ec_cmd, xop, src_dev, src_lba, cur_nolb); 730 + rc = target_xcopy_read_source(ec_cmd, xop, src_dev, src_lba, cur_bytes); 740 731 if (rc < 0) 741 732 goto out; 742 733 743 - src_lba += cur_nolb; 744 - pr_debug("target_xcopy_do_work: Incremented READ src_lba to %llu\n", 734 + src_lba += cur_bytes / src_dev->dev_attrib.block_size; 735 + pr_debug("%s: Incremented READ src_lba to %llu\n", __func__, 745 736 (unsigned long long)src_lba); 746 737 747 - pr_debug("target_xcopy_do_work: Calling write dst_dev: %p dst_lba: %llu," 748 - " cur_nolb: %hu\n", dst_dev, (unsigned long long)dst_lba, cur_nolb); 738 + pr_debug("%s: Calling write dst_dev: %p dst_lba: %llu, cur_nolb: %u\n", 739 + __func__, dst_dev, (unsigned long long)dst_lba, cur_nolb); 749 740 750 741 rc = target_xcopy_write_destination(ec_cmd, xop, dst_dev, 751 - dst_lba, cur_nolb); 742 + dst_lba, cur_bytes); 752 743 if (rc < 0) 753 744 goto out; 754 745 755 - dst_lba += cur_nolb; 756 - pr_debug("target_xcopy_do_work: Incremented WRITE dst_lba to %llu\n", 746 + dst_lba += cur_bytes / dst_dev->dev_attrib.block_size; 747 + pr_debug("%s: Incremented WRITE dst_lba to %llu\n", __func__, 757 748 (unsigned long long)dst_lba); 758 749 759 - copied_nolb += cur_nolb; 760 - nolb -= cur_nolb; 750 + copied_bytes += cur_bytes; 751 + nolb -= cur_bytes / src_dev->dev_attrib.block_size; 761 752 } 762 753 763 754 xcopy_pt_undepend_remotedev(xop); 764 755 target_free_sgl(xop->xop_data_sg, xop->xop_data_nents); 765 756 kfree(xop); 766 757 767 - pr_debug("target_xcopy_do_work: Final src_lba: %llu, dst_lba: %llu\n", 758 + pr_debug("%s: Final src_lba: %llu, dst_lba: %llu\n", __func__, 768 759 (unsigned long long)src_lba, (unsigned long long)dst_lba); 769 - pr_debug("target_xcopy_do_work: Blocks copied: %hu, Bytes Copied: %u\n", 770 - copied_nolb, copied_nolb * dst_dev->dev_attrib.block_size); 760 + pr_debug("%s: Blocks copied: %u, Bytes Copied: %u\n", __func__, 761 + copied_bytes / dst_dev->dev_attrib.block_size, copied_bytes); 771 762 772 - pr_debug("target_xcopy_do_work: Setting X-COPY GOOD status -> sending response\n"); 763 + pr_debug("%s: Setting X-COPY GOOD status -> sending response\n", __func__); 773 764 target_complete_cmd(ec_cmd, SAM_STAT_GOOD); 774 765 return; 775 766 ··· 785 776 786 777 err_free: 787 778 kfree(xop); 788 - pr_warn_ratelimited("target_xcopy_do_work: rc: %d, sense: %u, XCOPY operation failed\n", 789 - rc, sense_rc); 779 + pr_warn_ratelimited("%s: rc: %d, sense: %u, XCOPY operation failed\n", 780 + __func__, rc, sense_rc); 790 781 target_complete_cmd_with_sense(ec_cmd, SAM_STAT_CHECK_CONDITION, sense_rc); 791 782 } 792 783
+1 -1
drivers/target/target_core_xcopy.h
··· 5 5 #define XCOPY_TARGET_DESC_LEN 32 6 6 #define XCOPY_SEGMENT_DESC_LEN 28 7 7 #define XCOPY_NAA_IEEE_REGEX_LEN 16 8 - #define XCOPY_MAX_SECTORS 4096 8 + #define XCOPY_MAX_BYTES 16777216 /* 16 MB */ 9 9 10 10 /* 11 11 * SPC4r37 6.4.6.1