1
2
3
4
5
6
7
8
9
10
11
12
13
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
22DEFINE_PER_CPU(struct quicklist, quicklist)[CONFIG_NR_QUICK];
23
24#define FRACTION_OF_NODE_MEM 16
25
26static unsigned long max_pages(unsigned long min_pages)
27{
28 unsigned long node_free_pages, max;
29 int node = numa_node_id();
30 struct zone *zones = NODE_DATA(node)->node_zones;
31 int num_cpus_on_node;
32 node_to_cpumask_ptr(cpumask_on_node, node);
33
34 node_free_pages =
35#ifdef CONFIG_ZONE_DMA
36 zone_page_state(&zones[ZONE_DMA], NR_FREE_PAGES) +
37#endif
38#ifdef CONFIG_ZONE_DMA32
39 zone_page_state(&zones[ZONE_DMA32], NR_FREE_PAGES) +
40#endif
41 zone_page_state(&zones[ZONE_NORMAL], NR_FREE_PAGES);
42
43 max = node_free_pages / FRACTION_OF_NODE_MEM;
44
45 num_cpus_on_node = cpus_weight_nr(*cpumask_on_node);
46 max /= num_cpus_on_node;
47
48 return max(max, min_pages);
49}
50
51static long min_pages_to_free(struct quicklist *q,
52 unsigned long min_pages, long max_free)
53{
54 long pages_to_free;
55
56 pages_to_free = q->nr_pages - max_pages(min_pages);
57
58 return min(pages_to_free, max_free);
59}
60
61
62
63
64void quicklist_trim(int nr, void (*dtor)(void *),
65 unsigned long min_pages, unsigned long max_free)
66{
67 long pages_to_free;
68 struct quicklist *q;
69
70 q = &get_cpu_var(quicklist)[nr];
71 if (q->nr_pages > min_pages) {
72 pages_to_free = min_pages_to_free(q, min_pages, max_free);
73
74 while (pages_to_free > 0) {
75
76
77
78
79 void *p = quicklist_alloc(nr, 0, NULL);
80
81 if (dtor)
82 dtor(p);
83 free_page((unsigned long)p);
84 pages_to_free--;
85 }
86 }
87 put_cpu_var(quicklist);
88}
89
90unsigned long quicklist_total_size(void)
91{
92 unsigned long count = 0;
93 int cpu;
94 struct quicklist *ql, *q;
95
96 for_each_online_cpu(cpu) {
97 ql = per_cpu(quicklist, cpu);
98 for (q = ql; q < ql + CONFIG_NR_QUICK; q++)
99 count += q->nr_pages;
100 }
101 return count;
102}
103
104