Skip to contentSkip to conversation

Add alloc_pages_exact() and free_pages_exact()

PATCH In review Timur Tabi 2 participants 3 messages Jun 24, 2008

Subsystems (3)
lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

PATCH 1/1 Add alloc_pages_exact() and free_pages_exact()

alloc_pages_exact() is similar to alloc_pages(), except that it allocates the minimum number of pages to fulfill the request. This is useful if you want to allocate a very large buffer that is slightly larger than an even power-of-two number of pages. In that case, alloc_pages() will waste a lot of memory.

---

I have a video driver that wants to allocate a 5MB buffer. alloc_pages() will waste 3MB of physically-contiguous memory. Therefore, I would like to see alloc_pages_exact() added to 2.6.27.

Please note that I am not a Linux VM expert. I wrote these functions based on guidance from Andi Kleen. I have no familiarity with NUMA, so I don't know how to handle that. Any and all suggestions are welcome.

 include/linux/gfp.h |    3 ++
 mm/page_alloc.c     |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)
include/linux/gfp.h +3 -0
index b414be3..1054801 100644
@@ -215,6 +215,9 @@ extern struct page *alloc_page_vma(gfp_t gfp_mask,
 extern unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order);
 extern unsigned long get_zeroed_page(gfp_t gfp_mask);
 
+void *alloc_pages_exact(size_t size, gfp_t gfp_mask);
+void free_pages_exact(void *virt, size_t size);
+
 #define __get_free_page(gfp_mask) \
 		__get_free_pages((gfp_mask),0)
 
mm/page_alloc.c +53 -0

59 lines not on this page · raw message

Signature · 1 line
1.5.5
#1 lore raw
Quoted 9 lines · Timur Tabi
mm/page_alloc.c +9 -0
+void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
+{
+       unsigned int order = get_order(size);
+       unsigned long addr;
+
+       addr = __get_free_pages(gfp_mask, order);
+       if (addr) {
+               unsigned long alloc_end = addr + (PAGE_SIZE << order);
+               unsigned long used = addr + PAGE_ALIGN(size);

mm/page_alloc.c void free_pages(unsigned long addr, unsigned int order) · line 1745

Quoted 10 lines
mm/page_alloc.c +10 -0
+
+               split_page(virt_to_page(addr), order);
+               while (used < alloc_end) {
+                       free_page(used);
+                       used += PAGE_SIZE;
+               }
+       }
+
+       return (void *)addr;
+}

Hi Timur,

This looks like a really good idea. It looks pretty good to me, no functional problems. My brain had a really hard time parsing that code for some reason, though. Could just be a lack of coffee.

I think the thing that confused me was trying to figure out if 'alloc_end' was the end of what we *did* allocate from __get_free_pages() or if it was the *goal* allocation end.

'used' also seemed like a slightly strange variable name because it points to the memory which is about to be freed and ends up *unused*.

I'll offer this up just in case you like it better. For me, it is easier to parse, and should do the exact same thing. I also think it's slightly nicer to do the arithmetic on 'struct page *' rather than vaddrs in 'unsigned long'. It is _slightly_ cheaper not having to do a virt_to_page() on each free_page() call. The same would go for the free side as well.

All of the 'struct page *' arithmetic is OK since it is all done inside one MAX_ORDER area.

void *alloc_pages_exact(size_t size, gfp_t gfp_mask)

{
	unsigned int order = get_order(size);
	void *alloc;
	struct page *surplus_start;
	struct page *surplus_end;
	struct page *page;

	size = PAGE_ALIGN(size);

	alloc = (void *)__get_free_pages(gfp_mask, order);
	if (!alloc)
		return NULL;

	/* Turn the big allocation into a bunch of single pages */
	split_page(virt_to_page(alloc), order);

	surplus_start = virt_to_page(alloc + size);
	surplus_end = surplus_start + (1 << order);

	for (page = surplus_start; page < surplus_end; page++)
		__free_page(page);

	return alloc;
}

-- Dave

author
#2 lore raw
Quoted 2 lines · Dave Hansen

This looks like a really good idea. It looks pretty good to me, no functional problems.

Thanks.

Quoted 6 lines

I think the thing that confused me was trying to figure out if 'alloc_end' was the end of what we *did* allocate from __get_free_pages() or if it was the *goal* allocation end.

'used' also seemed like a slightly strange variable name because it points to the memory which is about to be freed and ends up *unused*.

This function is taken almost verbatim from alloc_large_system_hash(). I figured if the terminology was good for that function, it's good for mine.

Quoted 6 lines

I'll offer this up just in case you like it better. For me, it is easier to parse, and should do the exact same thing. I also think it's slightly nicer to do the arithmetic on 'struct page *' rather than vaddrs in 'unsigned long'. It is _slightly_ cheaper not having to do a virt_to_page() on each free_page() call. The same would go for the free side as well.

It does seem to be an improvement, although AKPM just accepted this patch. I'd hate to bother him with a replacement patch for something so minor.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale