thrash.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. static DEFINE_SPINLOCK(swap_token_lock);
  26. struct mm_struct *swap_token_mm;
  27. struct mem_cgroup *swap_token_memcg;
  28. static unsigned int global_faults;
  29. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  30. static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
  31. {
  32. struct mem_cgroup *memcg;
  33. memcg = try_get_mem_cgroup_from_mm(mm);
  34. if (memcg)
  35. css_put(mem_cgroup_css(memcg));
  36. return memcg;
  37. }
  38. #else
  39. static struct mem_cgroup *swap_token_memcg_from_mm(struct mm_struct *mm)
  40. {
  41. return NULL;
  42. }
  43. #endif
  44. void grab_swap_token(struct mm_struct *mm)
  45. {
  46. int current_interval;
  47. unsigned int old_prio = mm->token_priority;
  48. global_faults++;
  49. current_interval = global_faults - mm->faultstamp;
  50. if (!spin_trylock(&swap_token_lock))
  51. return;
  52. /* First come first served */
  53. if (!swap_token_mm)
  54. goto replace_token;
  55. if (mm == swap_token_mm) {
  56. mm->token_priority += 2;
  57. goto update_priority;
  58. }
  59. if (current_interval < mm->last_interval)
  60. mm->token_priority++;
  61. else {
  62. if (likely(mm->token_priority > 0))
  63. mm->token_priority--;
  64. }
  65. /* Check if we deserve the token */
  66. if (mm->token_priority > swap_token_mm->token_priority)
  67. goto replace_token;
  68. update_priority:
  69. trace_update_swap_token_priority(mm, old_prio);
  70. out:
  71. mm->faultstamp = global_faults;
  72. mm->last_interval = current_interval;
  73. spin_unlock(&swap_token_lock);
  74. return;
  75. replace_token:
  76. mm->token_priority += 2;
  77. trace_replace_swap_token(swap_token_mm, mm);
  78. swap_token_mm = mm;
  79. swap_token_memcg = swap_token_memcg_from_mm(mm);
  80. goto out;
  81. }
  82. /* Called on process exit. */
  83. void __put_swap_token(struct mm_struct *mm)
  84. {
  85. spin_lock(&swap_token_lock);
  86. if (likely(mm == swap_token_mm)) {
  87. trace_put_swap_token(swap_token_mm);
  88. swap_token_mm = NULL;
  89. swap_token_memcg = NULL;
  90. }
  91. spin_unlock(&swap_token_lock);
  92. }
  93. static bool match_memcg(struct mem_cgroup *a, struct mem_cgroup *b)
  94. {
  95. if (!a)
  96. return true;
  97. if (!b)
  98. return true;
  99. if (a == b)
  100. return true;
  101. return false;
  102. }
  103. void disable_swap_token(struct mem_cgroup *memcg)
  104. {
  105. /* memcg reclaim don't disable unrelated mm token. */
  106. if (match_memcg(memcg, swap_token_memcg)) {
  107. spin_lock(&swap_token_lock);
  108. if (match_memcg(memcg, swap_token_memcg)) {
  109. trace_disable_swap_token(swap_token_mm);
  110. swap_token_mm = NULL;
  111. swap_token_memcg = NULL;
  112. }
  113. spin_unlock(&swap_token_lock);
  114. }
  115. }