Skip to content

Commit 53466eb

Browse files
committed
ALSA: memalloc: Workaround for Xen PV
We change recently the memalloc helper to use dma_alloc_noncontiguous() and the fallback to get_pages(). Although lots of issues with IOMMU (or non-IOMMU) have been addressed, but there seems still a regression on Xen PV. Interestingly, the only proper way to work is use dma_alloc_coherent(). The use of dma_alloc_coherent() for SG buffer was dropped as it's problematic on IOMMU systems. OTOH, Xen PV has a different way, and it's fine to use the dma_alloc_coherent(). This patch is a workaround for Xen PV. It consists of the following changes: - For Xen PV, use only the fallback allocation without dma_alloc_noncontiguous() - In the fallback allocation, use dma_alloc_coherent(); the DMA address from dma_alloc_coherent() is returned in get_addr ops - The DMA addresses are stored in an array; the first entry stores the number of allocated pages in lower bits, which are referred at releasing pages again Reported-by: Marek Marczykowski-Górecki <[email protected]> Tested-by: Marek Marczykowski-Górecki <[email protected]> Fixes: a8d302a ("ALSA: memalloc: Revive x86-specific WC page allocations again") Fixes: 9736a32 ("ALSA: memalloc: Don't fall back for SG-buffer with IOMMU") Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Takashi Iwai <[email protected]>
1 parent 6a28a25 commit 53466eb

File tree

1 file changed

+69
-18
lines changed

1 file changed

+69
-18
lines changed

sound/core/memalloc.c

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -541,16 +541,15 @@ static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
541541
struct sg_table *sgt;
542542
void *p;
543543

544+
#ifdef CONFIG_SND_DMA_SGBUF
545+
if (cpu_feature_enabled(X86_FEATURE_XENPV))
546+
return snd_dma_sg_fallback_alloc(dmab, size);
547+
#endif
544548
sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
545549
DEFAULT_GFP, 0);
546550
#ifdef CONFIG_SND_DMA_SGBUF
547-
if (!sgt && !get_dma_ops(dmab->dev.dev)) {
548-
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
549-
dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
550-
else
551-
dmab->dev.type = SNDRV_DMA_TYPE_DEV_SG_FALLBACK;
551+
if (!sgt && !get_dma_ops(dmab->dev.dev))
552552
return snd_dma_sg_fallback_alloc(dmab, size);
553-
}
554553
#endif
555554
if (!sgt)
556555
return NULL;
@@ -717,19 +716,38 @@ static const struct snd_malloc_ops snd_dma_sg_wc_ops = {
717716

718717
/* Fallback SG-buffer allocations for x86 */
719718
struct snd_dma_sg_fallback {
719+
bool use_dma_alloc_coherent;
720720
size_t count;
721721
struct page **pages;
722+
/* DMA address array; the first page contains #pages in ~PAGE_MASK */
723+
dma_addr_t *addrs;
722724
};
723725

724726
static void __snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab,
725727
struct snd_dma_sg_fallback *sgbuf)
726728
{
727-
bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
728-
size_t i;
729-
730-
for (i = 0; i < sgbuf->count && sgbuf->pages[i]; i++)
731-
do_free_pages(page_address(sgbuf->pages[i]), PAGE_SIZE, wc);
729+
size_t i, size;
730+
731+
if (sgbuf->pages && sgbuf->addrs) {
732+
i = 0;
733+
while (i < sgbuf->count) {
734+
if (!sgbuf->pages[i] || !sgbuf->addrs[i])
735+
break;
736+
size = sgbuf->addrs[i] & ~PAGE_MASK;
737+
if (WARN_ON(!size))
738+
break;
739+
if (sgbuf->use_dma_alloc_coherent)
740+
dma_free_coherent(dmab->dev.dev, size << PAGE_SHIFT,
741+
page_address(sgbuf->pages[i]),
742+
sgbuf->addrs[i] & PAGE_MASK);
743+
else
744+
do_free_pages(page_address(sgbuf->pages[i]),
745+
size << PAGE_SHIFT, false);
746+
i += size;
747+
}
748+
}
732749
kvfree(sgbuf->pages);
750+
kvfree(sgbuf->addrs);
733751
kfree(sgbuf);
734752
}
735753

@@ -738,24 +756,36 @@ static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
738756
struct snd_dma_sg_fallback *sgbuf;
739757
struct page **pagep, *curp;
740758
size_t chunk, npages;
759+
dma_addr_t *addrp;
741760
dma_addr_t addr;
742761
void *p;
743-
bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
762+
763+
/* correct the type */
764+
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_SG)
765+
dmab->dev.type = SNDRV_DMA_TYPE_DEV_SG_FALLBACK;
766+
else if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
767+
dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
744768

745769
sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
746770
if (!sgbuf)
747771
return NULL;
772+
sgbuf->use_dma_alloc_coherent = cpu_feature_enabled(X86_FEATURE_XENPV);
748773
size = PAGE_ALIGN(size);
749774
sgbuf->count = size >> PAGE_SHIFT;
750775
sgbuf->pages = kvcalloc(sgbuf->count, sizeof(*sgbuf->pages), GFP_KERNEL);
751-
if (!sgbuf->pages)
776+
sgbuf->addrs = kvcalloc(sgbuf->count, sizeof(*sgbuf->addrs), GFP_KERNEL);
777+
if (!sgbuf->pages || !sgbuf->addrs)
752778
goto error;
753779

754780
pagep = sgbuf->pages;
755-
chunk = size;
781+
addrp = sgbuf->addrs;
782+
chunk = (PAGE_SIZE - 1) << PAGE_SHIFT; /* to fit in low bits in addrs */
756783
while (size > 0) {
757784
chunk = min(size, chunk);
758-
p = do_alloc_pages(dmab->dev.dev, chunk, &addr, wc);
785+
if (sgbuf->use_dma_alloc_coherent)
786+
p = dma_alloc_coherent(dmab->dev.dev, chunk, &addr, DEFAULT_GFP);
787+
else
788+
p = do_alloc_pages(dmab->dev.dev, chunk, &addr, false);
759789
if (!p) {
760790
if (chunk <= PAGE_SIZE)
761791
goto error;
@@ -767,17 +797,25 @@ static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
767797
size -= chunk;
768798
/* fill pages */
769799
npages = chunk >> PAGE_SHIFT;
800+
*addrp = npages; /* store in lower bits */
770801
curp = virt_to_page(p);
771-
while (npages--)
802+
while (npages--) {
772803
*pagep++ = curp++;
804+
*addrp++ |= addr;
805+
addr += PAGE_SIZE;
806+
}
773807
}
774808

775809
p = vmap(sgbuf->pages, sgbuf->count, VM_MAP, PAGE_KERNEL);
776810
if (!p)
777811
goto error;
812+
813+
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
814+
set_pages_array_wc(sgbuf->pages, sgbuf->count);
815+
778816
dmab->private_data = sgbuf;
779817
/* store the first page address for convenience */
780-
dmab->addr = snd_sgbuf_get_addr(dmab, 0);
818+
dmab->addr = sgbuf->addrs[0] & PAGE_MASK;
781819
return p;
782820

783821
error:
@@ -787,10 +825,23 @@ static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
787825

788826
static void snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab)
789827
{
828+
struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
829+
830+
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
831+
set_pages_array_wb(sgbuf->pages, sgbuf->count);
790832
vunmap(dmab->area);
791833
__snd_dma_sg_fallback_free(dmab, dmab->private_data);
792834
}
793835

836+
static dma_addr_t snd_dma_sg_fallback_get_addr(struct snd_dma_buffer *dmab,
837+
size_t offset)
838+
{
839+
struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
840+
size_t index = offset >> PAGE_SHIFT;
841+
842+
return (sgbuf->addrs[index] & PAGE_MASK) | (offset & ~PAGE_MASK);
843+
}
844+
794845
static int snd_dma_sg_fallback_mmap(struct snd_dma_buffer *dmab,
795846
struct vm_area_struct *area)
796847
{
@@ -805,8 +856,8 @@ static const struct snd_malloc_ops snd_dma_sg_fallback_ops = {
805856
.alloc = snd_dma_sg_fallback_alloc,
806857
.free = snd_dma_sg_fallback_free,
807858
.mmap = snd_dma_sg_fallback_mmap,
859+
.get_addr = snd_dma_sg_fallback_get_addr,
808860
/* reuse vmalloc helpers */
809-
.get_addr = snd_dma_vmalloc_get_addr,
810861
.get_page = snd_dma_vmalloc_get_page,
811862
.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
812863
};

0 commit comments

Comments
 (0)