Back to home page

LXR

 
 

    


0001 /*
0002  * Quicklist support.
0003  *
0004  * Quicklists are light weight lists of pages that have a defined state
0005  * on alloc and free. Pages must be in the quicklist specific defined state
0006  * (zero by default) when the page is freed. It seems that the initial idea
0007  * for such lists first came from Dave Miller and then various other people
0008  * improved on it.
0009  *
0010  * Copyright (C) 2007 SGI,
0011  *  Christoph Lameter <cl@linux.com>
0012  *      Generalized, added support for multiple lists and
0013  *      constructors / destructors.
0014  */
0015 #include <linux/kernel.h>
0016 
0017 #include <linux/gfp.h>
0018 #include <linux/mm.h>
0019 #include <linux/mmzone.h>
0020 #include <linux/quicklist.h>
0021 
0022 DEFINE_PER_CPU(struct quicklist [CONFIG_NR_QUICK], quicklist);
0023 
0024 #define FRACTION_OF_NODE_MEM    16
0025 
0026 static unsigned long max_pages(unsigned long min_pages)
0027 {
0028     unsigned long node_free_pages, max;
0029     int node = numa_node_id();
0030     struct zone *zones = NODE_DATA(node)->node_zones;
0031     int num_cpus_on_node;
0032 
0033     node_free_pages =
0034 #ifdef CONFIG_ZONE_DMA
0035         zone_page_state(&zones[ZONE_DMA], NR_FREE_PAGES) +
0036 #endif
0037 #ifdef CONFIG_ZONE_DMA32
0038         zone_page_state(&zones[ZONE_DMA32], NR_FREE_PAGES) +
0039 #endif
0040         zone_page_state(&zones[ZONE_NORMAL], NR_FREE_PAGES);
0041 
0042     max = node_free_pages / FRACTION_OF_NODE_MEM;
0043 
0044     num_cpus_on_node = cpumask_weight(cpumask_of_node(node));
0045     max /= num_cpus_on_node;
0046 
0047     return max(max, min_pages);
0048 }
0049 
0050 static long min_pages_to_free(struct quicklist *q,
0051     unsigned long min_pages, long max_free)
0052 {
0053     long pages_to_free;
0054 
0055     pages_to_free = q->nr_pages - max_pages(min_pages);
0056 
0057     return min(pages_to_free, max_free);
0058 }
0059 
0060 /*
0061  * Trim down the number of pages in the quicklist
0062  */
0063 void quicklist_trim(int nr, void (*dtor)(void *),
0064     unsigned long min_pages, unsigned long max_free)
0065 {
0066     long pages_to_free;
0067     struct quicklist *q;
0068 
0069     q = &get_cpu_var(quicklist)[nr];
0070     if (q->nr_pages > min_pages) {
0071         pages_to_free = min_pages_to_free(q, min_pages, max_free);
0072 
0073         while (pages_to_free > 0) {
0074             /*
0075              * We pass a gfp_t of 0 to quicklist_alloc here
0076              * because we will never call into the page allocator.
0077              */
0078             void *p = quicklist_alloc(nr, 0, NULL);
0079 
0080             if (dtor)
0081                 dtor(p);
0082             free_page((unsigned long)p);
0083             pages_to_free--;
0084         }
0085     }
0086     put_cpu_var(quicklist);
0087 }
0088 
0089 unsigned long quicklist_total_size(void)
0090 {
0091     unsigned long count = 0;
0092     int cpu;
0093     struct quicklist *ql, *q;
0094 
0095     for_each_online_cpu(cpu) {
0096         ql = per_cpu(quicklist, cpu);
0097         for (q = ql; q < ql + CONFIG_NR_QUICK; q++)
0098             count += q->nr_pages;
0099     }
0100     return count;
0101 }
0102