Skip to content

Commit

Permalink
mm/cma: add cma_pages_valid to determine if pages are in CMA
Browse files Browse the repository at this point in the history
Add new interface cma_pages_valid() which indicates if the specified
pages are part of a CMA region.  This interface will be used in a
subsequent patch by hugetlb code.

In order to keep the same amount of DEBUG information, a pr_debug() call
was added to cma_pages_valid().  In the case where the page passed to
cma_release is not in cma region, the debug message will be printed from
cma_pages_valid as opposed to cma_release.

Link: https://lkml.kernel.org/r/20211007181918.136982-3-mike.kravetz@oracle.com
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Acked-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Oscar Salvador <osalvador@suse.de>
Cc: "Aneesh Kumar K . V" <aneesh.kumar@linux.ibm.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Muchun Song <songmuchun@bytedance.com>
Cc: Naoya Horiguchi <naoya.horiguchi@linux.dev>
Cc: Nghia Le <nghialm78@gmail.com>
Cc: Zi Yan <ziy@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Mike Kravetz authored and Linus Torvalds committed Nov 6, 2021
1 parent 79dfc69 commit 9871e2d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/linux/cma.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
struct cma **res_cma);
extern struct page *cma_alloc(struct cma *cma, unsigned long count, unsigned int align,
bool no_warn);
extern bool cma_pages_valid(struct cma *cma, const struct page *pages, unsigned long count);
extern bool cma_release(struct cma *cma, const struct page *pages, unsigned long count);

extern int cma_for_each_area(int (*it)(struct cma *cma, void *data), void *data);
Expand Down
24 changes: 20 additions & 4 deletions mm/cma.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,25 @@ struct page *cma_alloc(struct cma *cma, unsigned long count,
return page;
}

bool cma_pages_valid(struct cma *cma, const struct page *pages,
unsigned long count)
{
unsigned long pfn;

if (!cma || !pages)
return false;

pfn = page_to_pfn(pages);

if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count) {
pr_debug("%s(page %p, count %lu)\n", __func__,
(void *)pages, count);
return false;
}

return true;
}

/**
* cma_release() - release allocated pages
* @cma: Contiguous memory region for which the allocation is performed.
Expand All @@ -539,16 +558,13 @@ bool cma_release(struct cma *cma, const struct page *pages,
{
unsigned long pfn;

if (!cma || !pages)
if (!cma_pages_valid(cma, pages, count))
return false;

pr_debug("%s(page %p, count %lu)\n", __func__, (void *)pages, count);

pfn = page_to_pfn(pages);

if (pfn < cma->base_pfn || pfn >= cma->base_pfn + cma->count)
return false;

VM_BUG_ON(pfn + count > cma->base_pfn + cma->count);

free_contig_range(pfn, count);
Expand Down

0 comments on commit 9871e2d

Please sign in to comment.