Skip to content

Commit dfef01e

Browse files
committed
ALSA: memalloc: Don't exceed over the requested size
snd_dma_alloc_pages_fallback() tries to allocate pages again when the allocation fails with reduced size. But the first try actually *increases* the size to power-of-two, which may give back a larger chunk than the requested size. This confuses the callers, e.g. sgbuf assumes that the size is equal or less, and it may result in a bad loop due to the underflow and eventually lead to Oops. The code of this function seems incorrectly assuming the usage of get_order(). We need to decrease at first, then align to power-of-two. Reported-and-tested-by: he, bo <bo.he@intel.com> Reported-by: zhang jun <jun.zhang@intel.com> Cc: <stable@vger.kernel.org> Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent f3d737b commit dfef01e

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

sound/core/memalloc.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,12 @@ int snd_dma_alloc_pages_fallback(int type, struct device *device, size_t size,
242242
int err;
243243

244244
while ((err = snd_dma_alloc_pages(type, device, size, dmab)) < 0) {
245-
size_t aligned_size;
246245
if (err != -ENOMEM)
247246
return err;
248247
if (size <= PAGE_SIZE)
249248
return -ENOMEM;
250-
aligned_size = PAGE_SIZE << get_order(size);
251-
if (size != aligned_size)
252-
size = aligned_size;
253-
else
254-
size >>= 1;
249+
size >>= 1;
250+
size = PAGE_SIZE << get_order(size);
255251
}
256252
if (! dmab->area)
257253
return -ENOMEM;

0 commit comments

Comments
 (0)