4 * Quicklists are light weight lists of pages that have a defined state
5 * on alloc and free. Pages must be in the quicklist specific defined state
6 * (zero by default) when the page is freed. It seems that the initial idea
7 * for such lists first came from Dave Miller and then various other people
10 * Copyright (C) 2007 SGI,
11 * Christoph Lameter <clameter@sgi.com>
12 * Generalized, added support for multiple lists and
13 * constructors / destructors.
15 #include <linux/kernel.h>
18 #include <linux/mmzone.h>
19 #include <linux/module.h>
20 #include <linux/quicklist.h>
22 DEFINE_PER_CPU(struct quicklist, quicklist)[CONFIG_NR_QUICK];
24 #define FRACTION_OF_NODE_MEM 16
26 static unsigned long max_pages(unsigned long min_pages)
28 unsigned long node_free_pages, max;
29 struct zone *zones = NODE_DATA(numa_node_id())->node_zones;
32 #ifdef CONFIG_ZONE_DMA
33 zone_page_state(&zones[ZONE_DMA], NR_FREE_PAGES) +
35 #ifdef CONFIG_ZONE_DMA32
36 zone_page_state(&zones[ZONE_DMA32], NR_FREE_PAGES) +
38 zone_page_state(&zones[ZONE_NORMAL], NR_FREE_PAGES);
40 max = node_free_pages / FRACTION_OF_NODE_MEM;
41 return max(max, min_pages);
44 static long min_pages_to_free(struct quicklist *q,
45 unsigned long min_pages, long max_free)
49 pages_to_free = q->nr_pages - max_pages(min_pages);
51 return min(pages_to_free, max_free);
55 * Trim down the number of pages in the quicklist
57 void quicklist_trim(int nr, void (*dtor)(void *),
58 unsigned long min_pages, unsigned long max_free)
63 q = &get_cpu_var(quicklist)[nr];
64 if (q->nr_pages > min_pages) {
65 pages_to_free = min_pages_to_free(q, min_pages, max_free);
67 while (pages_to_free > 0) {
69 * We pass a gfp_t of 0 to quicklist_alloc here
70 * because we will never call into the page allocator.
72 void *p = quicklist_alloc(nr, 0, NULL);
76 free_page((unsigned long)p);
80 put_cpu_var(quicklist);
83 unsigned long quicklist_total_size(void)
85 unsigned long count = 0;
87 struct quicklist *ql, *q;
89 for_each_online_cpu(cpu) {
90 ql = per_cpu(quicklist, cpu);
91 for (q = ql; q < ql + CONFIG_NR_QUICK; q++)