thrash.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * mm/thrash.c
  3. *
  4. * Copyright (C) 2004, Red Hat, Inc.
  5. * Copyright (C) 2004, Rik van Riel <riel@redhat.com>
  6. * Released under the GPL, see the file COPYING for details.
  7. *
  8. * Simple token based thrashing protection, using the algorithm
  9. * described in: http://www.cs.wm.edu/~sjiang/token.pdf
  10. *
  11. * Sep 2006, Ashwin Chaugule <ashwin.chaugule@celunite.com>
  12. * Improved algorithm to pass token:
  13. * Each task has a priority which is incremented if it contended
  14. * for the token in an interval less than its previous attempt.
  15. * If the token is acquired, that task's priority is boosted to prevent
  16. * the token from bouncing around too often and to let the task make
  17. * some progress in its execution.
  18. */
  19. #include <linux/jiffies.h>
  20. #include <linux/mm.h>
  21. #include <linux/sched.h>
  22. #include <linux/swap.h>
  23. #include <linux/memcontrol.h>
  24. #include <trace/events/vmscan.h>
  25. #define TOKEN_AGING_INTERVAL (0xFF)
  26. static DEFINE_SPINLOCK(swap_token_lock);
  27. struct mm_struct *swap_token_mm;
  28. struct mem_cgroup *swap_token_memcg;
  29. static unsigned int global_faults;
  30. static unsigned int last_aging;
  31. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  32. static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
  33. {
  34. struct mem_cgroup *memcg;
  35. memcg = try_get_mem_cgroup_from_mm(mm);
  36. if (memcg)
  37. css_put(mem_cgroup_css(memcg));
  38. return memcg;
  39. }
  40. #else
  41. static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
  42. {
  43. return NULL;
  44. }
  45. #endif
  46. void grab_swap_token(struct mm_struct *mm)
  47. {
  48. int current_interval;
  49. unsigned int old_prio = mm->token_priority;
  50. global_faults++;
  51. current_interval = global_faults - mm->faultstamp;
  52. if (!spin_trylock(&swap_token_lock))
  53. return;
  54. /* First come first served */
  55. if (!swap_token_mm)
  56. goto replace_token;
  57. if ((global_faults - last_aging) > TOKEN_AGING_INTERVAL) {
  58. swap_token_mm->token_priority /= 2;
  59. last_aging = global_faults;
  60. }
  61. if (mm == swap_token_mm) {
  62. mm->token_priority += 2;
  63. goto update_priority;
  64. }
  65. if (current_interval < mm->last_interval)
  66. mm->token_priority++;
  67. else {
  68. if (likely(mm->token_priority > 0))
  69. mm->token_priority--;
  70. }
  71. /* Check if we deserve the token */
  72. if (mm->token_priority > swap_token_mm->token_priority)
  73. goto replace_token;
  74. update_priority:
  75. trace_update_swap_token_priority(mm, old_prio, swap_token_mm);
  76. out:
  77. mm->faultstamp = global_faults;
  78. mm->last_interval = current_interval;
  79. spin_unlock(&swap_token_lock);
  80. return;
  81. replace_token:
  82. mm->token_priority += 2;
  83. trace_replace_swap_token(swap_token_mm, mm);
  84. swap_token_mm = mm;
  85. swap_token_memcg = swap_token_memcg_from_mm(mm);
  86. last_aging = global_faults;
  87. goto out;
  88. }
  89. /* Called on process exit. */
  90. void __put_swap_token(struct mm_struct *mm)
  91. {
  92. spin_lock(&swap_token_lock);
  93. if (likely(mm == swap_token_mm)) {
  94. trace_put_swap_token(swap_token_mm);
  95. swap_token_mm = NULL;
  96. swap_token_memcg = NULL;
  97. }
  98. spin_unlock(&swap_token_lock);
  99. }
  100. static bool match_memcg(struct mem_cgroup *a, struct mem_cgroup *b)
  101. {
  102. if (!a)
  103. return true;
  104. if (!b)
  105. return true;
  106. if (a == b)
  107. return true;
  108. return false;
  109. }
  110. void disable_swap_token(struct mem_cgroup *memcg)
  111. {
  112. /* memcg reclaim don't disable unrelated mm token. */
  113. if (match_memcg(memcg, swap_token_memcg)) {
  114. spin_lock(&swap_token_lock);
  115. if (match_memcg(memcg, swap_token_memcg)) {
  116. trace_disable_swap_token(swap_token_mm);
  117. swap_token_mm = NULL;
  118. swap_token_memcg = NULL;
  119. }
  120. spin_unlock(&swap_token_lock);
  121. }
  122. }