diff options
-rw-r--r-- | include/linux/quicklist.h | 94 | ||||
-rw-r--r-- | mm/Kconfig | 5 | ||||
-rw-r--r-- | mm/Makefile | 2 | ||||
-rw-r--r-- | mm/quicklist.c | 88 |
4 files changed, 189 insertions, 0 deletions
diff --git a/include/linux/quicklist.h b/include/linux/quicklist.h new file mode 100644 index 000000000000..9371c6116df3 --- /dev/null +++ b/include/linux/quicklist.h | |||
@@ -0,0 +1,94 @@ | |||
1 | #ifndef LINUX_QUICKLIST_H | ||
2 | #define LINUX_QUICKLIST_H | ||
3 | /* | ||
4 | * Fast allocations and disposal of pages. Pages must be in the condition | ||
5 | * as needed after allocation when they are freed. Per cpu lists of pages | ||
6 | * are kept that only contain node local pages. | ||
7 | * | ||
8 | * (C) 2007, SGI. Christoph Lameter <clameter@sgi.com> | ||
9 | */ | ||
10 | #include <linux/kernel.h> | ||
11 | #include <linux/gfp.h> | ||
12 | #include <linux/percpu.h> | ||
13 | |||
14 | #ifdef CONFIG_QUICKLIST | ||
15 | |||
16 | struct quicklist { | ||
17 | void *page; | ||
18 | int nr_pages; | ||
19 | }; | ||
20 | |||
21 | DECLARE_PER_CPU(struct quicklist, quicklist)[CONFIG_NR_QUICK]; | ||
22 | |||
23 | /* | ||
24 | * The two key functions quicklist_alloc and quicklist_free are inline so | ||
25 | * that they may be custom compiled for the platform. | ||
26 | * Specifying a NULL ctor can remove constructor support. Specifying | ||
27 | * a constant quicklist allows the determination of the exact address | ||
28 | * in the per cpu area. | ||
29 | * | ||
30 | * The fast patch in quicklist_alloc touched only a per cpu cacheline and | ||
31 | * the first cacheline of the page itself. There is minmal overhead involved. | ||
32 | */ | ||
33 | static inline void *quicklist_alloc(int nr, gfp_t flags, void (*ctor)(void *)) | ||
34 | { | ||
35 | struct quicklist *q; | ||
36 | void **p = NULL; | ||
37 | |||
38 | q =&get_cpu_var(quicklist)[nr]; | ||
39 | p = q->page; | ||
40 | if (likely(p)) { | ||
41 | q->page = p[0]; | ||
42 | p[0] = NULL; | ||
43 | q->nr_pages--; | ||
44 | } | ||
45 | put_cpu_var(quicklist); | ||
46 | if (likely(p)) | ||
47 | return p; | ||
48 | |||
49 | p = (void *)__get_free_page(flags | __GFP_ZERO); | ||
50 | if (ctor && p) | ||
51 | ctor(p); | ||
52 | return p; | ||
53 | } | ||
54 | |||
55 | static inline void __quicklist_free(int nr, void (*dtor)(void *), void *p, | ||
56 | struct page *page) | ||
57 | { | ||
58 | struct quicklist *q; | ||
59 | int nid = page_to_nid(page); | ||
60 | |||
61 | if (unlikely(nid != numa_node_id())) { | ||
62 | if (dtor) | ||
63 | dtor(p); | ||
64 | __free_page(page); | ||
65 | return; | ||
66 | } | ||
67 | |||
68 | q = &get_cpu_var(quicklist)[nr]; | ||
69 | *(void **)p = q->page; | ||
70 | q->page = p; | ||
71 | q->nr_pages++; | ||
72 | put_cpu_var(quicklist); | ||
73 | } | ||
74 | |||
75 | static inline void quicklist_free(int nr, void (*dtor)(void *), void *pp) | ||
76 | { | ||
77 | __quicklist_free(nr, dtor, pp, virt_to_page(pp)); | ||
78 | } | ||
79 | |||
80 | static inline void quicklist_free_page(int nr, void (*dtor)(void *), | ||
81 | struct page *page) | ||
82 | { | ||
83 | __quicklist_free(nr, dtor, page_address(page), page); | ||
84 | } | ||
85 | |||
86 | void quicklist_trim(int nr, void (*dtor)(void *), | ||
87 | unsigned long min_pages, unsigned long max_free); | ||
88 | |||
89 | unsigned long quicklist_total_size(void); | ||
90 | |||
91 | #endif | ||
92 | |||
93 | #endif /* LINUX_QUICKLIST_H */ | ||
94 | |||
diff --git a/mm/Kconfig b/mm/Kconfig index 7942b333e46c..1ac718f636ec 100644 --- a/mm/Kconfig +++ b/mm/Kconfig | |||
@@ -163,3 +163,8 @@ config ZONE_DMA_FLAG | |||
163 | default "0" if !ZONE_DMA | 163 | default "0" if !ZONE_DMA |
164 | default "1" | 164 | default "1" |
165 | 165 | ||
166 | config NR_QUICK | ||
167 | int | ||
168 | depends on QUICKLIST | ||
169 | default "1" | ||
170 | |||
diff --git a/mm/Makefile b/mm/Makefile index 1887148e44e7..a9148ea329aa 100644 --- a/mm/Makefile +++ b/mm/Makefile | |||
@@ -30,3 +30,5 @@ obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o | |||
30 | obj-$(CONFIG_FS_XIP) += filemap_xip.o | 30 | obj-$(CONFIG_FS_XIP) += filemap_xip.o |
31 | obj-$(CONFIG_MIGRATION) += migrate.o | 31 | obj-$(CONFIG_MIGRATION) += migrate.o |
32 | obj-$(CONFIG_SMP) += allocpercpu.o | 32 | obj-$(CONFIG_SMP) += allocpercpu.o |
33 | obj-$(CONFIG_QUICKLIST) += quicklist.o | ||
34 | |||
diff --git a/mm/quicklist.c b/mm/quicklist.c new file mode 100644 index 000000000000..ae8189c2799e --- /dev/null +++ b/mm/quicklist.c | |||
@@ -0,0 +1,88 @@ | |||
1 | /* | ||
2 | * Quicklist support. | ||
3 | * | ||
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 | ||
8 | * improved on it. | ||
9 | * | ||
10 | * Copyright (C) 2007 SGI, | ||
11 | * Christoph Lameter <clameter@sgi.com> | ||
12 | * Generalized, added support for multiple lists and | ||
13 | * constructors / destructors. | ||
14 | */ | ||
15 | #include <linux/kernel.h> | ||
16 | |||
17 | #include <linux/mm.h> | ||
18 | #include <linux/mmzone.h> | ||
19 | #include <linux/module.h> | ||
20 | #include <linux/quicklist.h> | ||
21 | |||
22 | DEFINE_PER_CPU(struct quicklist, quicklist)[CONFIG_NR_QUICK]; | ||
23 | |||
24 | #define FRACTION_OF_NODE_MEM 16 | ||
25 | |||
26 | static unsigned long max_pages(unsigned long min_pages) | ||
27 | { | ||
28 | unsigned long node_free_pages, max; | ||
29 | |||
30 | node_free_pages = node_page_state(numa_node_id(), | ||
31 | NR_FREE_PAGES); | ||
32 | max = node_free_pages / FRACTION_OF_NODE_MEM; | ||
33 | return max(max, min_pages); | ||
34 | } | ||
35 | |||
36 | static long min_pages_to_free(struct quicklist *q, | ||
37 | unsigned long min_pages, long max_free) | ||
38 | { | ||
39 | long pages_to_free; | ||
40 | |||
41 | pages_to_free = q->nr_pages - max_pages(min_pages); | ||
42 | |||
43 | return min(pages_to_free, max_free); | ||
44 | } | ||
45 | |||
46 | /* | ||
47 | * Trim down the number of pages in the quicklist | ||
48 | */ | ||
49 | void quicklist_trim(int nr, void (*dtor)(void *), | ||
50 | unsigned long min_pages, unsigned long max_free) | ||
51 | { | ||
52 | long pages_to_free; | ||
53 | struct quicklist *q; | ||
54 | |||
55 | q = &get_cpu_var(quicklist)[nr]; | ||
56 | if (q->nr_pages > min_pages) { | ||
57 | pages_to_free = min_pages_to_free(q, min_pages, max_free); | ||
58 | |||
59 | while (pages_to_free > 0) { | ||
60 | /* | ||
61 | * We pass a gfp_t of 0 to quicklist_alloc here | ||
62 | * because we will never call into the page allocator. | ||
63 | */ | ||
64 | void *p = quicklist_alloc(nr, 0, NULL); | ||
65 | |||
66 | if (dtor) | ||
67 | dtor(p); | ||
68 | free_page((unsigned long)p); | ||
69 | pages_to_free--; | ||
70 | } | ||
71 | } | ||
72 | put_cpu_var(quicklist); | ||
73 | } | ||
74 | |||
75 | unsigned long quicklist_total_size(void) | ||
76 | { | ||
77 | unsigned long count = 0; | ||
78 | int cpu; | ||
79 | struct quicklist *ql, *q; | ||
80 | |||
81 | for_each_online_cpu(cpu) { | ||
82 | ql = per_cpu(quicklist, cpu); | ||
83 | for (q = ql; q < ql + CONFIG_NR_QUICK; q++) | ||
84 | count += q->nr_pages; | ||
85 | } | ||
86 | return count; | ||
87 | } | ||
88 | |||