From: Wesley Cheng <wesley.cheng@oss.qualcomm.com>
To: Mathias Nyman <mathias.nyman@intel.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
Michal Pecio <michal.pecio@gmail.com>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-sound@vger.kernel.org,
Wesley Cheng <wesley.cheng@oss.qualcomm.com>
Subject: [PATCH v7 1/4] usb: xhci: sideband: fix ring sg table for sub-page TRB segments
Date: Tue, 01 Dec 2026 14:09:00 -0800 [thread overview]
Message-ID: <20261201-16k_offload_v1_b4-v7-1-ad9ddad2530d@oss.qualcomm.com> (raw)
In-Reply-To: <20261201-16k_offload_v1_b4-v7-0-ad9ddad2530d@oss.qualcomm.com>
xhci_ring_to_sgtable() populated its sg_table via dma_get_sgtable()
per segment and sg_alloc_table_from_pages(), both of which only
operate at whole PAGE_SIZE granularity. Since TRB_SEGMENT_SIZE (4096)
can be smaller than PAGE_SIZE, these helpers describe a full page for
each ring segment on larger-PAGE_SIZE kernels (16K/64K), and cannot
correctly represent segments that share the same physical page.
Build the sg_table directly instead: allocate one sg entry per ring
segment with sg_alloc_table(), and fill each entry explicitly with
sg_set_page() using the segment's own page, TRB_SEGMENT_SIZE as the
length, and offset_in_page() for the exact intra-page offset. Walk the
table with for_each_sg() so this also works for chained sg tables.
Fixes: de66754e9f80 ("xhci: sideband: add initial api to register a secondary interrupter entity")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Wesley Cheng <wesley.cheng@oss.qualcomm.com>
---
drivers/usb/host/xhci-sideband.c | 80 ++++++++++++++++++++++------------------
1 file changed, 45 insertions(+), 35 deletions(-)
diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c
index 6f7712f95033..35a7166aa974 100644
--- a/drivers/usb/host/xhci-sideband.c
+++ b/drivers/usb/host/xhci-sideband.c
@@ -8,68 +8,78 @@
* Author: Mathias Nyman
*/
+#include <linux/dma-mapping.h>
#include <linux/usb/xhci-sideband.h>
-#include <linux/dma-direct.h>
#include "xhci.h"
/* sideband internal helpers */
+static struct page *
+xhci_segment_to_page(struct device *dev, struct xhci_segment *seg)
+{
+ struct sg_table sgt;
+ struct page *page;
+
+ /*
+ * Remapped coherent memory is in the vmalloc area, where
+ * vmalloc_to_page() finds the page for any offset into the allocation.
+ * dma_get_sgtable() could describe the buffer from the start of its
+ * allocation instead.
+ */
+ if (is_vmalloc_addr(seg->trbs))
+ return vmalloc_to_page(seg->trbs);
+
+ if (dma_get_sgtable(dev, &sgt, seg->trbs, seg->dma, TRB_SEGMENT_SIZE))
+ return NULL;
+
+ page = sg_page(sgt.sgl);
+ sg_free_table(&sgt);
+
+ return page;
+}
+
static struct sg_table *
xhci_ring_to_sgtable(struct xhci_sideband *sb, struct xhci_ring *ring)
{
+ struct device *dev = xhci_to_hcd(sb->xhci)->self.sysdev;
struct xhci_segment *seg;
- struct sg_table *sgt;
- unsigned int n_pages;
- struct page **pages;
- struct device *dev;
- size_t sz;
+ struct sg_table *sgt;
+ struct scatterlist *sg;
+ struct page *page;
int i;
- dev = xhci_to_hcd(sb->xhci)->self.sysdev;
- sz = ring->num_segs * TRB_SEGMENT_SIZE;
- n_pages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
- pages = kvmalloc_objs(struct page *, n_pages);
- if (!pages)
+ seg = ring->first_seg;
+ if (!seg)
return NULL;
sgt = kzalloc_obj(*sgt);
- if (!sgt) {
- kvfree(pages);
+ if (!sgt)
return NULL;
- }
- seg = ring->first_seg;
- if (!seg)
- goto err;
- /*
- * Rings can potentially have multiple segments, create an array that
- * carries page references to allocated segments. Utilize the
- * sg_alloc_table_from_pages() to create the sg table, and to ensure
- * that page links are created.
- */
- for (i = 0; i < ring->num_segs; i++) {
- dma_get_sgtable(dev, sgt, seg->trbs, seg->dma,
- TRB_SEGMENT_SIZE);
- pages[i] = sg_page(sgt->sgl);
- sg_free_table(sgt);
- seg = seg->next;
+ if (sg_alloc_table(sgt, ring->num_segs, GFP_KERNEL)) {
+ kfree(sgt);
+ return NULL;
}
- if (sg_alloc_table_from_pages(sgt, pages, n_pages, 0, sz, GFP_KERNEL))
- goto err;
+ for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) {
+ page = xhci_segment_to_page(dev, seg);
+ if (!page)
+ goto err;
- kvfree(pages);
+ sg_set_page(sg, page, TRB_SEGMENT_SIZE, offset_in_page(seg->trbs));
+ seg = seg->next;
+ }
/*
- * Save first segment dma address to sg dma_address field for the sideband
- * client to have access to the IOVA of the ring.
+ * Save the first segment DMA address for the sideband client. The returned
+ * table is metadata and must not be passed to dma_map_sg().
*/
sg_dma_address(sgt->sgl) = ring->first_seg->dma;
return sgt;
err:
- kvfree(pages);
+ sg_free_table(sgt);
kfree(sgt);
return NULL;
--
2.43.0
next prev parent reply other threads:[~2026-12-01 22:09 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 22:08 [PATCH v7 0/4] Add larger page size support for USB audio offload path Wesley Cheng
2026-12-01 22:09 ` Wesley Cheng [this message]
2026-12-01 22:09 ` [PATCH v7 2/4] usb: xhci: Allocate sideband ring segments with dma_alloc_coherent() Wesley Cheng
2026-12-01 22:09 ` [PATCH v7 3/4] ALSA: usb-audio: qcom: tag sideband endpoints before ring allocation Wesley Cheng
2026-12-01 22:09 ` [PATCH v7 4/4] ALSA: usb-audio: qcom: fix xfer ring IOMMU unmap on 16K+ page kernels Wesley Cheng
2026-12-01 22:57 ` [PATCH v7 0/4] Add larger page size support for USB audio offload path Wesley Cheng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201-16k_offload_v1_b4-v7-1-ad9ddad2530d@oss.qualcomm.com \
--to=wesley.cheng@oss.qualcomm.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mathias.nyman@intel.com \
--cc=michal.pecio@gmail.com \
--cc=perex@perex.cz \
--cc=tiwai@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®