Skip to content

Commit

Permalink
mm: cma: fix CMA aligned offset calculation
Browse files Browse the repository at this point in the history
The CMA aligned offset calculation is incorrect for non-zero order_per_bit
values.

For example, if cma->order_per_bit=1, cma->base_pfn= 0x2f800000 and
align_order=12, the function returns a value of 0x17c00 instead of 0x400.

This patch fixes the CMA aligned offset calculation.

The previous calculation was wrong and would return too-large values for
the offset, so that when cma_alloc looks for free pages in the bitmap with
the requested alignment > order_per_bit, it starts too far into the bitmap
and so CMA allocations will fail despite there actually being plenty of
free pages remaining.  It will also probably have the wrong alignment.
With this change, we will get the correct offset into the bitmap.

One affected user is powerpc KVM, which has kvm_cma->order_per_bit set to
KVM_CMA_CHUNK_ORDER - PAGE_SHIFT, or 18 - 12 = 6.

[[email protected]: changelog additions]
Signed-off-by: Danesh Petigara <[email protected]>
Reviewed-by: Gregory Fong <[email protected]>
Acked-by: Michal Nazarewicz <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Danesh Petigara authored and torvalds committed Mar 13, 2015
1 parent 44fc805 commit 850fc43
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions mm/cma.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ static unsigned long cma_bitmap_aligned_mask(struct cma *cma, int align_order)
return (1UL << (align_order - cma->order_per_bit)) - 1;
}

/*
* Find a PFN aligned to the specified order and return an offset represented in
* order_per_bits.
*/
static unsigned long cma_bitmap_aligned_offset(struct cma *cma, int align_order)
{
unsigned int alignment;

if (align_order <= cma->order_per_bit)
return 0;
alignment = 1UL << (align_order - cma->order_per_bit);
return ALIGN(cma->base_pfn, alignment) -
(cma->base_pfn >> cma->order_per_bit);

return (ALIGN(cma->base_pfn, (1UL << align_order))
- cma->base_pfn) >> cma->order_per_bit;
}

static unsigned long cma_bitmap_maxno(struct cma *cma)
Expand Down

0 comments on commit 850fc43

Please sign in to comment.