Skip to contentSkip to conversation

Add alloc_pages_exact() and free_pages_exact()

RFC PATCH In review Timur Tabi 3 participants 3 messages May 13, 2008 – May 14, 2008

Subsystems (3)
lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

RFC 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 5 lines · Timur Tabi

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.

FWIW, I wished I had something like this when doing the cafe_ccic driver. I can't really attest to the validity of the core VM stuff, but I will say that the interface is worth having.

Thanks for doing this!

jon

#2 lore raw
In reply to Timur Tabi

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

Quoted 11 lines · On Tue, 13 May 2008 16:26:46 -0500 Timur Tabi wrote:
 include/linux/gfp.h |    3 ++
 mm/page_alloc.c     |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)
mm/page_alloc.c +0 -0
index bdd5c43..2b685eb 100644
@@ -1736,6 +1736,59 @@ void free_pages(unsigned long addr, unsigned int order)
 
 EXPORT_SYMBOL(free_pages);

Hi Timur,

Please use kernel-doc notation/format for these functions as documented in Documentation/kernel-doc-nano-HOWTO.txt. Ask me if you have any questions/problems with it.

Quoted 42 lines
mm/page_alloc.c +42 -0
+/**
+ * Allocate an exact number of pages of physically-contiguous memory.
+ *
+ * This function is similar to alloc_pages(), except that it allocates the
+ * minimum number of pages to satisfy the request.  alloc_pages() can only
+ * allocate memory in power-of-two pages.
+ *
+ * This function is also limited by MAX_ORDER.
+ *
+ * Memory allocated by this function must be released by free_pages_exact().
+ */
+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);
+
+		split_page(virt_to_page(addr), order);
+		while (used < alloc_end) {
+			free_page(used);
+			used += PAGE_SIZE;
+		}
+	}
+
+	return (void *)addr;
+}
+EXPORT_SYMBOL(alloc_pages_exact);
+
+/**
+ * Releases memory allocated via alloc_pages_exact()
+ *
+ * Release the memory allocated by a previous call to alloc_pages_exact.
+ *
+ * The first parameter must be the value returned by alloc_pages_exact(),
+ * and the 'size' parameter must be the same as was passed to
+ * alloc_pages_exact().
+ */
+void free_pages_exact(void *virt, size_t size)

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

Quoted 10 lines
mm/page_alloc.c +10 -0
+{
+	unsigned long addr = (unsigned long)virt;
+	unsigned long end = addr + PAGE_ALIGN(size);
+
+	while (addr < end) {
+		free_page(addr);
+		addr += PAGE_SIZE;
+	}
+}
+EXPORT_SYMBOL(free_pages_exact);

---
~Randy