quicklist.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/mm.h>
  17. #include <linux/mmzone.h>
  18. #include <linux/module.h>
  19. #include <linux/quicklist.h>
  20. DEFINE_PER_CPU(struct quicklist, quicklist)[CONFIG_NR_QUICK];
  21. #define FRACTION_OF_NODE_MEM 16
  22. static unsigned long max_pages(unsigned long min_pages)
  23. {
  24. unsigned long node_free_pages, max;
  25. node_free_pages = node_page_state(numa_node_id(),
  26. NR_FREE_PAGES);
  27. max = node_free_pages / FRACTION_OF_NODE_MEM;
  28. return max(max, min_pages);
  29. }
  30. static long min_pages_to_free(struct quicklist *q,
  31. unsigned long min_pages, long max_free)
  32. {
  33. long pages_to_free;
  34. pages_to_free = q->nr_pages - max_pages(min_pages);
  35. return min(pages_to_free, max_free);
  36. }
  37. /*
  38. * Trim down the number of pages in the quicklist
  39. */
  40. void quicklist_trim(int nr, void (*dtor)(void *),
  41. unsigned long min_pages, unsigned long max_free)
  42. {
  43. long pages_to_free;
  44. struct quicklist *q;
  45. q = &get_cpu_var(quicklist)[nr];
  46. if (q->nr_pages > min_pages) {
  47. pages_to_free = min_pages_to_free(q, min_pages, max_free);
  48. while (pages_to_free > 0) {
  49. /*
  50. * We pass a gfp_t of 0 to quicklist_alloc here
  51. * because we will never call into the page allocator.
  52. */
  53. void *p = quicklist_alloc(nr, 0, NULL);
  54. if (dtor)
  55. dtor(p);
  56. free_page((unsigned long)p);
  57. pages_to_free--;
  58. }
  59. }
  60. put_cpu_var(quicklist);
  61. }
  62. unsigned long quicklist_total_size(void)
  63. {
  64. unsigned long count = 0;
  65. int cpu;
  66. struct quicklist *ql, *q;
  67. for_each_online_cpu(cpu) {
  68. ql = per_cpu(quicklist, cpu);
  69. for (q = ql; q < ql + CONFIG_NR_QUICK; q++)
  70. count += q->nr_pages;
  71. }
  72. return count;
  73. }