quicklist.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <linux/gfp.h>
  17. #include <linux/mm.h>
  18. #include <linux/mmzone.h>
  19. #include <linux/module.h>
  20. #include <linux/quicklist.h>
  21. DEFINE_PER_CPU(struct quicklist [CONFIG_NR_QUICK], quicklist);
  22. #define FRACTION_OF_NODE_MEM 16
  23. static unsigned long max_pages(unsigned long min_pages)
  24. {
  25. unsigned long node_free_pages, max;
  26. int node = numa_node_id();
  27. struct zone *zones = NODE_DATA(node)->node_zones;
  28. int num_cpus_on_node;
  29. node_free_pages =
  30. #ifdef CONFIG_ZONE_DMA
  31. zone_page_state(&zones[ZONE_DMA], NR_FREE_PAGES) +
  32. #endif
  33. #ifdef CONFIG_ZONE_DMA32
  34. zone_page_state(&zones[ZONE_DMA32], NR_FREE_PAGES) +
  35. #endif
  36. zone_page_state(&zones[ZONE_NORMAL], NR_FREE_PAGES);
  37. max = node_free_pages / FRACTION_OF_NODE_MEM;
  38. num_cpus_on_node = cpumask_weight(cpumask_of_node(node));
  39. max /= num_cpus_on_node;
  40. return max(max, min_pages);
  41. }
  42. static long min_pages_to_free(struct quicklist *q,
  43. unsigned long min_pages, long max_free)
  44. {
  45. long pages_to_free;
  46. pages_to_free = q->nr_pages - max_pages(min_pages);
  47. return min(pages_to_free, max_free);
  48. }
  49. /*
  50. * Trim down the number of pages in the quicklist
  51. */
  52. void quicklist_trim(int nr, void (*dtor)(void *),
  53. unsigned long min_pages, unsigned long max_free)
  54. {
  55. long pages_to_free;
  56. struct quicklist *q;
  57. q = &get_cpu_var(quicklist)[nr];
  58. if (q->nr_pages > min_pages) {
  59. pages_to_free = min_pages_to_free(q, min_pages, max_free);
  60. while (pages_to_free > 0) {
  61. /*
  62. * We pass a gfp_t of 0 to quicklist_alloc here
  63. * because we will never call into the page allocator.
  64. */
  65. void *p = quicklist_alloc(nr, 0, NULL);
  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. unsigned long quicklist_total_size(void)
  75. {
  76. unsigned long count = 0;
  77. int cpu;
  78. struct quicklist *ql, *q;
  79. for_each_online_cpu(cpu) {
  80. ql = per_cpu(quicklist, cpu);
  81. for (q = ql; q < ql + CONFIG_NR_QUICK; q++)
  82. count += q->nr_pages;
  83. }
  84. return count;
  85. }