Skip to contentSkip to conversation

mm: change size_t to unsigned int for cma_alloc

PATCH In review Minchan Kim 3 participants 6 messages Mar 29, 2021 – Mar 30, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: change size_t to unsigned int for cma_alloc

size_t in cma_alloc is confusing since it makes people think it's byte count, not pages. Fix it.

---
 include/linux/cma.h | 2 +-
 mm/cma.c            | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
include/linux/cma.h +1 -1
index 217999c8a762..a873edc20ca2 100644
@@ -44,7 +44,7 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
 					unsigned int order_per_bit,
 					const char *name,
 					struct cma **res_cma);
-extern struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
+extern struct page *cma_alloc(struct cma *cma, unsigned int count, unsigned int align,
 			      bool no_warn);
 extern bool cma_release(struct cma *cma, const struct page *pages, unsigned int count);
 
mm/cma.c +4 -4
index 08c45157911a..24dc01e26d45 100644
@@ -423,21 +423,21 @@ static inline void cma_debug_show_areas(struct cma *cma) { }
  * This function allocates part of contiguous memory on specific
  * contiguous memory area.
  */
-struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
+struct page *cma_alloc(struct cma *cma, unsigned int count, unsigned int align,
 		       bool no_warn)
 {
 	unsigned long mask, offset;
 	unsigned long pfn = -1;
 	unsigned long start = 0;
 	unsigned long bitmap_maxno, bitmap_no, bitmap_count;
-	size_t i;
+	unsigned int i;
 	struct page *page = NULL;
 	int ret = -ENOMEM;
 
 	if (!cma || !cma->count || !cma->bitmap)
 		goto out;
 
-	pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
+	pr_debug("%s(cma %p, count %u, align %d)\n", __func__, (void *)cma,
 		 count, align);
 
 	if (!count)
@@ -500,7 +500,7 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
 	}
 
 	if (ret && !no_warn) {
-		pr_err_ratelimited("%s: %s: alloc failed, req-size: %zu pages, ret: %d\n",
+		pr_err_ratelimited("%s: %s: alloc failed, req-size: %u pages, ret: %d\n",
 				   __func__, cma->name, count, ret);
 		cma_debug_show_areas(cma);
 	}
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#1 lore raw
Quoted 2 lines · Minchan Kim

size_t in cma_alloc is confusing since it makes people think it's byte count, not pages. Fix it.

i think it has to be unsigned long.

67a2e213e7e937c41c52ab5bc46bf3f4de469f6e

Quoted 63 lines
---
 include/linux/cma.h | 2 +-
 mm/cma.c            | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
include/linux/cma.h +1 -1
index 217999c8a762..a873edc20ca2 100644
@@ -44,7 +44,7 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
 					unsigned int order_per_bit,
 					const char *name,
 					struct cma **res_cma);
-extern struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
+extern struct page *cma_alloc(struct cma *cma, unsigned int count, unsigned int align,
 			      bool no_warn);
 extern bool cma_release(struct cma *cma, const struct page *pages, unsigned int count);
 
mm/cma.c +4 -4
index 08c45157911a..24dc01e26d45 100644
@@ -423,21 +423,21 @@ static inline void cma_debug_show_areas(struct cma *cma) { }
  * This function allocates part of contiguous memory on specific
  * contiguous memory area.
  */
-struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
+struct page *cma_alloc(struct cma *cma, unsigned int count, unsigned int align,
 		       bool no_warn)
 {
 	unsigned long mask, offset;
 	unsigned long pfn = -1;
 	unsigned long start = 0;
 	unsigned long bitmap_maxno, bitmap_no, bitmap_count;
-	size_t i;
+	unsigned int i;
 	struct page *page = NULL;
 	int ret = -ENOMEM;
 
 	if (!cma || !cma->count || !cma->bitmap)
 		goto out;
 
-	pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
+	pr_debug("%s(cma %p, count %u, align %d)\n", __func__, (void *)cma,
 		 count, align);
 
 	if (!count)
@@ -500,7 +500,7 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
 	}
 
 	if (ret && !no_warn) {
-		pr_err_ratelimited("%s: %s: alloc failed, req-size: %zu pages, ret: %d\n",
+		pr_err_ratelimited("%s: %s: alloc failed, req-size: %u pages, ret: %d\n",
 				   __func__, cma->name, count, ret);
 		cma_debug_show_areas(cma);
 	}
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#2 lore raw
Quoted 7 lines · Matthew Wilcox
Quoted 2 lines · Minchan Kim

size_t in cma_alloc is confusing since it makes people think it's byte count, not pages. Fix it.

i think it has to be unsigned long.

67a2e213e7e937c41c52ab5bc46bf3f4de469f6e

Thanks for the pinter. I wanted to have the smallest change. The commit leads me to change cma_release, trace_cma_alloc, cma_clear_bitmap and front_contig_range as well.(Not sure we have more. Will check).

Ccing [email protected] for upcoming changing free_contig_range.

Quoted 64 lines
Quoted 64 lines
---
 include/linux/cma.h | 2 +-
 mm/cma.c            | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
include/linux/cma.h +1 -1
index 217999c8a762..a873edc20ca2 100644
@@ -44,7 +44,7 @@ extern int cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
 					unsigned int order_per_bit,
 					const char *name,
 					struct cma **res_cma);
-extern struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
+extern struct page *cma_alloc(struct cma *cma, unsigned int count, unsigned int align,
 			      bool no_warn);
 extern bool cma_release(struct cma *cma, const struct page *pages, unsigned int count);
 
mm/cma.c +4 -4
index 08c45157911a..24dc01e26d45 100644
@@ -423,21 +423,21 @@ static inline void cma_debug_show_areas(struct cma *cma) { }
  * This function allocates part of contiguous memory on specific
  * contiguous memory area.
  */
-struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
+struct page *cma_alloc(struct cma *cma, unsigned int count, unsigned int align,
 		       bool no_warn)
 {
 	unsigned long mask, offset;
 	unsigned long pfn = -1;
 	unsigned long start = 0;
 	unsigned long bitmap_maxno, bitmap_no, bitmap_count;
-	size_t i;
+	unsigned int i;
 	struct page *page = NULL;
 	int ret = -ENOMEM;
 
 	if (!cma || !cma->count || !cma->bitmap)
 		goto out;
 
-	pr_debug("%s(cma %p, count %zu, align %d)\n", __func__, (void *)cma,
+	pr_debug("%s(cma %p, count %u, align %d)\n", __func__, (void *)cma,
 		 count, align);
 
 	if (!count)
@@ -500,7 +500,7 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
 	}
 
 	if (ret && !no_warn) {
-		pr_err_ratelimited("%s: %s: alloc failed, req-size: %zu pages, ret: %d\n",
+		pr_err_ratelimited("%s: %s: alloc failed, req-size: %u pages, ret: %d\n",
 				   __func__, cma->name, count, ret);
 		cma_debug_show_areas(cma);
 	}
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#3 lore raw
Quoted 8 lines · Minchan Kim
Quoted 7 lines · Matthew Wilcox
Quoted 2 lines · Minchan Kim

size_t in cma_alloc is confusing since it makes people think it's byte count, not pages. Fix it.

i think it has to be unsigned long.

67a2e213e7e937c41c52ab5bc46bf3f4de469f6e

Right.

Fortunately, we don't have such large allocations yet via CMA/alloc_contig_range

Quoted 7 lines

Thanks for the pinter. I wanted to have the smallest change. The commit leads me to change cma_release, trace_cma_alloc, cma_clear_bitmap and front_contig_range as well.(Not sure we have more. Will check).

Ccing [email protected] for upcoming changing free_contig_range.

While at it, we might want to convert free_contig_range() to eat
"unsigned long start, unsigned long end" like alloc_contig_range(), instead of "unsigned long pfn, unsigned int nr_pages" like alloc_contig_pages() ...

Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw
Quoted 21 lines · David Hildenbrand
Quoted 8 lines · Minchan Kim
Quoted 7 lines · Matthew Wilcox
Quoted 2 lines · Minchan Kim

size_t in cma_alloc is confusing since it makes people think it's byte count, not pages. Fix it.

i think it has to be unsigned long.

67a2e213e7e937c41c52ab5bc46bf3f4de469f6e

Right.

Fortunately, we don't have such large allocations yet via CMA/alloc_contig_range

Quoted 6 lines

Thanks for the pinter. I wanted to have the smallest change. The commit leads me to change cma_release, trace_cma_alloc, cma_clear_bitmap and front_contig_range as well.(Not sure we have more. Will check).

Quoted 5 lines
Quoted 1 line

Ccing [email protected] for upcoming changing free_contig_range.

While at it, we might want to convert free_contig_range() to eat "unsigned long start, unsigned long end" like alloc_contig_range(), instead of "unsigned long pfn, unsigned int nr_pages" like alloc_contig_pages() ...

Well, I personllay tempted to change alloc_contig_range, not free_contig_range because base_pfn with nr_pages was more straightforward than base_pfn and end_pfn in that we don't need to tell whether end_pfn is inclusive or exclusive.

When I look at callers of [alloc|free]_contig_range, many of them already have used nr_pages based approach rather than start_pfn, end_pfn. If your suggestion come from that "it's *range* API", I'd like to rename it with "alloc_contig_pages|free_contig_pages".

Since it's beyond the goal of this patch and might be controversial, I will not deal with it in this patch.

#5 lore raw
Quoted 24 lines · Minchan Kim
Quoted 23 lines · David Hildenbrand
Quoted 8 lines · Minchan Kim
Quoted 7 lines · Matthew Wilcox
Quoted 2 lines · Minchan Kim

size_t in cma_alloc is confusing since it makes people think it's byte count, not pages. Fix it.

i think it has to be unsigned long.

67a2e213e7e937c41c52ab5bc46bf3f4de469f6e

Right.

Fortunately, we don't have such large allocations yet via CMA/alloc_contig_range

Quoted 7 lines

Thanks for the pinter. I wanted to have the smallest change. The commit leads me to change cma_release, trace_cma_alloc, cma_clear_bitmap and front_contig_range as well.(Not sure we have more. Will check).

Ccing [email protected] for upcoming changing free_contig_range.

Quoted 9 lines
Quoted 3 lines

While at it, we might want to convert free_contig_range() to eat "unsigned long start, unsigned long end" like alloc_contig_range(), instead of "unsigned long pfn, unsigned int nr_pages" like alloc_contig_pages() ...

Well, I personllay tempted to change alloc_contig_range, not free_contig_range because base_pfn with nr_pages was more straightforward than base_pfn and end_pfn in that we don't need to tell whether end_pfn is inclusive or exclusive.

That's right.

Quoted 3 lines

When I look at callers of [alloc|free]_contig_range, many of them already have used nr_pages based approach rather than start_pfn, end_pfn. If your suggestion come from that "it's *range* API",

Right you are, teaching alloc_contig_range() to eat "nr_pages" might actually be even better and more consistent.

Quoted 1 line

I'd like to rename it with "alloc_contig_pages|free_contig_pages".

alloc_contig_pages is just a wrapper for alloc_contig_range(), so free_contig_range() is a better fit; OTOH, having both would also somehow make sense.

Quoted 3 lines

Since it's beyond the goal of this patch and might be controversial, I will not deal with it in this patch.

Sure, but feel free to send a patch to make that consistent. It's been bugging me already (having to always remember if to pass in nr_pages or end).

Signature · 3 lines
Thanks,

David / dhildenb