]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
xhci: update bounce buffer with correct sg num
authorHenry Lin <henryl@nvidia.com>
Wed, 22 May 2019 11:33:57 +0000 (14:33 +0300)
committerKleber Sacilotto de Souza <kleber.souza@canonical.com>
Wed, 14 Aug 2019 09:18:49 +0000 (11:18 +0200)
BugLink: https://bugs.launchpad.net/bugs/1838700
commit 597c56e372dab2c7f79b8d700aad3a5deebf9d1b upstream.

This change fixes a data corruption issue occurred on USB hard disk for
the case that bounce buffer is used during transferring data.

While updating data between sg list and bounce buffer, current
implementation passes mapped sg number (urb->num_mapped_sgs) to
sg_pcopy_from_buffer() and sg_pcopy_to_buffer(). This causes data
not get copied if target buffer is located in the elements after
mapped sg elements. This change passes sg number for full list to
fix issue.

Besides, for copying data from bounce buffer, calling dma_unmap_single()
on the bounce buffer before copying data to sg list can avoid cache issue.

Fixes: f9c589e142d0 ("xhci: TD-fragment, align the unsplittable case with a bounce buffer")
Cc: <stable@vger.kernel.org> # v4.8+
Signed-off-by: Henry Lin <henryl@nvidia.com>
Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Khalid Elmously <khalid.elmously@canonical.com>
drivers/usb/host/xhci-ring.c

index f68f090aac900200521889db73b8b15b12b5b56c..12ab2c6ead905a57ed4bc14cfa1927a484f0923e 100644 (file)
@@ -656,6 +656,7 @@ static void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci,
        struct device *dev = xhci_to_hcd(xhci)->self.controller;
        struct xhci_segment *seg = td->bounce_seg;
        struct urb *urb = td->urb;
+       size_t len;
 
        if (!ring || !seg || !urb)
                return;
@@ -666,11 +667,14 @@ static void xhci_unmap_td_bounce_buffer(struct xhci_hcd *xhci,
                return;
        }
 
-       /* for in tranfers we need to copy the data from bounce to sg */
-       sg_pcopy_from_buffer(urb->sg, urb->num_mapped_sgs, seg->bounce_buf,
-                            seg->bounce_len, seg->bounce_offs);
        dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len,
                         DMA_FROM_DEVICE);
+       /* for in tranfers we need to copy the data from bounce to sg */
+       len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, seg->bounce_buf,
+                            seg->bounce_len, seg->bounce_offs);
+       if (len != seg->bounce_len)
+               xhci_warn(xhci, "WARN Wrong bounce buffer read length: %ld != %d\n",
+                               len, seg->bounce_len);
        seg->bounce_len = 0;
        seg->bounce_offs = 0;
 }
@@ -3177,6 +3181,7 @@ static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len,
        unsigned int unalign;
        unsigned int max_pkt;
        u32 new_buff_len;
+       size_t len;
 
        max_pkt = usb_endpoint_maxp(&urb->ep->desc);
        unalign = (enqd_len + *trb_buff_len) % max_pkt;
@@ -3207,8 +3212,12 @@ static int xhci_align_td(struct xhci_hcd *xhci, struct urb *urb, u32 enqd_len,
 
        /* create a max max_pkt sized bounce buffer pointed to by last trb */
        if (usb_urb_dir_out(urb)) {
-               sg_pcopy_to_buffer(urb->sg, urb->num_mapped_sgs,
+               len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs,
                                   seg->bounce_buf, new_buff_len, enqd_len);
+               if (len != seg->bounce_len)
+                       xhci_warn(xhci,
+                               "WARN Wrong bounce buffer write length: %ld != %d\n",
+                               len, seg->bounce_len);
                seg->bounce_dma = dma_map_single(dev, seg->bounce_buf,
                                                 max_pkt, DMA_TO_DEVICE);
        } else {