ALSA: memalloc: Workaround for Xen PV
[ Upstream commit 53466ebdec614f915c691809b0861acecb941e30 ] 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 <marmarek@invisiblethingslab.com> Tested-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com> Fixes: a8d302a0b770 ("ALSA: memalloc: Revive x86-specific WC page allocations again") Fixes: 9736a325137b ("ALSA: memalloc: Don't fall back for SG-buffer with IOMMU") Link: https://lore.kernel.org/r/87tu256lqs.wl-tiwai@suse.de Link: https://lore.kernel.org/r/20230125153104.5527-1-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
parent
3331d34160
commit
0b49da857d
@ -542,16 +542,15 @@ static void *snd_dma_noncontig_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|||||||
struct sg_table *sgt;
|
struct sg_table *sgt;
|
||||||
void *p;
|
void *p;
|
||||||
|
|
||||||
|
#ifdef CONFIG_SND_DMA_SGBUF
|
||||||
|
if (cpu_feature_enabled(X86_FEATURE_XENPV))
|
||||||
|
return snd_dma_sg_fallback_alloc(dmab, size);
|
||||||
|
#endif
|
||||||
sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
|
sgt = dma_alloc_noncontiguous(dmab->dev.dev, size, dmab->dev.dir,
|
||||||
DEFAULT_GFP, 0);
|
DEFAULT_GFP, 0);
|
||||||
#ifdef CONFIG_SND_DMA_SGBUF
|
#ifdef CONFIG_SND_DMA_SGBUF
|
||||||
if (!sgt && !get_dma_ops(dmab->dev.dev)) {
|
if (!sgt && !get_dma_ops(dmab->dev.dev))
|
||||||
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
|
|
||||||
dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
|
|
||||||
else
|
|
||||||
dmab->dev.type = SNDRV_DMA_TYPE_DEV_SG_FALLBACK;
|
|
||||||
return snd_dma_sg_fallback_alloc(dmab, size);
|
return snd_dma_sg_fallback_alloc(dmab, size);
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
if (!sgt)
|
if (!sgt)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -718,19 +717,38 @@ static const struct snd_malloc_ops snd_dma_sg_wc_ops = {
|
|||||||
|
|
||||||
/* Fallback SG-buffer allocations for x86 */
|
/* Fallback SG-buffer allocations for x86 */
|
||||||
struct snd_dma_sg_fallback {
|
struct snd_dma_sg_fallback {
|
||||||
|
bool use_dma_alloc_coherent;
|
||||||
size_t count;
|
size_t count;
|
||||||
struct page **pages;
|
struct page **pages;
|
||||||
|
/* DMA address array; the first page contains #pages in ~PAGE_MASK */
|
||||||
|
dma_addr_t *addrs;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void __snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab,
|
static void __snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab,
|
||||||
struct snd_dma_sg_fallback *sgbuf)
|
struct snd_dma_sg_fallback *sgbuf)
|
||||||
{
|
{
|
||||||
bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
|
size_t i, size;
|
||||||
size_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < sgbuf->count && sgbuf->pages[i]; i++)
|
if (sgbuf->pages && sgbuf->addrs) {
|
||||||
do_free_pages(page_address(sgbuf->pages[i]), PAGE_SIZE, wc);
|
i = 0;
|
||||||
|
while (i < sgbuf->count) {
|
||||||
|
if (!sgbuf->pages[i] || !sgbuf->addrs[i])
|
||||||
|
break;
|
||||||
|
size = sgbuf->addrs[i] & ~PAGE_MASK;
|
||||||
|
if (WARN_ON(!size))
|
||||||
|
break;
|
||||||
|
if (sgbuf->use_dma_alloc_coherent)
|
||||||
|
dma_free_coherent(dmab->dev.dev, size << PAGE_SHIFT,
|
||||||
|
page_address(sgbuf->pages[i]),
|
||||||
|
sgbuf->addrs[i] & PAGE_MASK);
|
||||||
|
else
|
||||||
|
do_free_pages(page_address(sgbuf->pages[i]),
|
||||||
|
size << PAGE_SHIFT, false);
|
||||||
|
i += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
kvfree(sgbuf->pages);
|
kvfree(sgbuf->pages);
|
||||||
|
kvfree(sgbuf->addrs);
|
||||||
kfree(sgbuf);
|
kfree(sgbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -739,24 +757,36 @@ static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|||||||
struct snd_dma_sg_fallback *sgbuf;
|
struct snd_dma_sg_fallback *sgbuf;
|
||||||
struct page **pagep, *curp;
|
struct page **pagep, *curp;
|
||||||
size_t chunk, npages;
|
size_t chunk, npages;
|
||||||
|
dma_addr_t *addrp;
|
||||||
dma_addr_t addr;
|
dma_addr_t addr;
|
||||||
void *p;
|
void *p;
|
||||||
bool wc = dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
|
|
||||||
|
/* correct the type */
|
||||||
|
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_SG)
|
||||||
|
dmab->dev.type = SNDRV_DMA_TYPE_DEV_SG_FALLBACK;
|
||||||
|
else if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG)
|
||||||
|
dmab->dev.type = SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK;
|
||||||
|
|
||||||
sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
|
sgbuf = kzalloc(sizeof(*sgbuf), GFP_KERNEL);
|
||||||
if (!sgbuf)
|
if (!sgbuf)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
sgbuf->use_dma_alloc_coherent = cpu_feature_enabled(X86_FEATURE_XENPV);
|
||||||
size = PAGE_ALIGN(size);
|
size = PAGE_ALIGN(size);
|
||||||
sgbuf->count = size >> PAGE_SHIFT;
|
sgbuf->count = size >> PAGE_SHIFT;
|
||||||
sgbuf->pages = kvcalloc(sgbuf->count, sizeof(*sgbuf->pages), GFP_KERNEL);
|
sgbuf->pages = kvcalloc(sgbuf->count, sizeof(*sgbuf->pages), GFP_KERNEL);
|
||||||
if (!sgbuf->pages)
|
sgbuf->addrs = kvcalloc(sgbuf->count, sizeof(*sgbuf->addrs), GFP_KERNEL);
|
||||||
|
if (!sgbuf->pages || !sgbuf->addrs)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
pagep = sgbuf->pages;
|
pagep = sgbuf->pages;
|
||||||
chunk = size;
|
addrp = sgbuf->addrs;
|
||||||
|
chunk = (PAGE_SIZE - 1) << PAGE_SHIFT; /* to fit in low bits in addrs */
|
||||||
while (size > 0) {
|
while (size > 0) {
|
||||||
chunk = min(size, chunk);
|
chunk = min(size, chunk);
|
||||||
p = do_alloc_pages(dmab->dev.dev, chunk, &addr, wc);
|
if (sgbuf->use_dma_alloc_coherent)
|
||||||
|
p = dma_alloc_coherent(dmab->dev.dev, chunk, &addr, DEFAULT_GFP);
|
||||||
|
else
|
||||||
|
p = do_alloc_pages(dmab->dev.dev, chunk, &addr, false);
|
||||||
if (!p) {
|
if (!p) {
|
||||||
if (chunk <= PAGE_SIZE)
|
if (chunk <= PAGE_SIZE)
|
||||||
goto error;
|
goto error;
|
||||||
@ -768,17 +798,25 @@ static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|||||||
size -= chunk;
|
size -= chunk;
|
||||||
/* fill pages */
|
/* fill pages */
|
||||||
npages = chunk >> PAGE_SHIFT;
|
npages = chunk >> PAGE_SHIFT;
|
||||||
|
*addrp = npages; /* store in lower bits */
|
||||||
curp = virt_to_page(p);
|
curp = virt_to_page(p);
|
||||||
while (npages--)
|
while (npages--) {
|
||||||
*pagep++ = curp++;
|
*pagep++ = curp++;
|
||||||
|
*addrp++ |= addr;
|
||||||
|
addr += PAGE_SIZE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p = vmap(sgbuf->pages, sgbuf->count, VM_MAP, PAGE_KERNEL);
|
p = vmap(sgbuf->pages, sgbuf->count, VM_MAP, PAGE_KERNEL);
|
||||||
if (!p)
|
if (!p)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
|
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
|
||||||
|
set_pages_array_wc(sgbuf->pages, sgbuf->count);
|
||||||
|
|
||||||
dmab->private_data = sgbuf;
|
dmab->private_data = sgbuf;
|
||||||
/* store the first page address for convenience */
|
/* store the first page address for convenience */
|
||||||
dmab->addr = snd_sgbuf_get_addr(dmab, 0);
|
dmab->addr = sgbuf->addrs[0] & PAGE_MASK;
|
||||||
return p;
|
return p;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
@ -788,10 +826,23 @@ static void *snd_dma_sg_fallback_alloc(struct snd_dma_buffer *dmab, size_t size)
|
|||||||
|
|
||||||
static void snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab)
|
static void snd_dma_sg_fallback_free(struct snd_dma_buffer *dmab)
|
||||||
{
|
{
|
||||||
|
struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
|
||||||
|
|
||||||
|
if (dmab->dev.type == SNDRV_DMA_TYPE_DEV_WC_SG_FALLBACK)
|
||||||
|
set_pages_array_wb(sgbuf->pages, sgbuf->count);
|
||||||
vunmap(dmab->area);
|
vunmap(dmab->area);
|
||||||
__snd_dma_sg_fallback_free(dmab, dmab->private_data);
|
__snd_dma_sg_fallback_free(dmab, dmab->private_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static dma_addr_t snd_dma_sg_fallback_get_addr(struct snd_dma_buffer *dmab,
|
||||||
|
size_t offset)
|
||||||
|
{
|
||||||
|
struct snd_dma_sg_fallback *sgbuf = dmab->private_data;
|
||||||
|
size_t index = offset >> PAGE_SHIFT;
|
||||||
|
|
||||||
|
return (sgbuf->addrs[index] & PAGE_MASK) | (offset & ~PAGE_MASK);
|
||||||
|
}
|
||||||
|
|
||||||
static int snd_dma_sg_fallback_mmap(struct snd_dma_buffer *dmab,
|
static int snd_dma_sg_fallback_mmap(struct snd_dma_buffer *dmab,
|
||||||
struct vm_area_struct *area)
|
struct vm_area_struct *area)
|
||||||
{
|
{
|
||||||
@ -806,8 +857,8 @@ static const struct snd_malloc_ops snd_dma_sg_fallback_ops = {
|
|||||||
.alloc = snd_dma_sg_fallback_alloc,
|
.alloc = snd_dma_sg_fallback_alloc,
|
||||||
.free = snd_dma_sg_fallback_free,
|
.free = snd_dma_sg_fallback_free,
|
||||||
.mmap = snd_dma_sg_fallback_mmap,
|
.mmap = snd_dma_sg_fallback_mmap,
|
||||||
|
.get_addr = snd_dma_sg_fallback_get_addr,
|
||||||
/* reuse vmalloc helpers */
|
/* reuse vmalloc helpers */
|
||||||
.get_addr = snd_dma_vmalloc_get_addr,
|
|
||||||
.get_page = snd_dma_vmalloc_get_page,
|
.get_page = snd_dma_vmalloc_get_page,
|
||||||
.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
|
.get_chunk_size = snd_dma_vmalloc_get_chunk_size,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user