swap_cgroup.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <linux/swap_cgroup.h>
  2. #include <linux/vmalloc.h>
  3. #include <linux/mm.h>
  4. #include <linux/swapops.h> /* depends on mm.h include */
  5. static DEFINE_MUTEX(swap_cgroup_mutex);
  6. struct swap_cgroup_ctrl {
  7. struct page **map;
  8. unsigned long length;
  9. spinlock_t lock;
  10. };
  11. static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
  12. struct swap_cgroup {
  13. unsigned short id;
  14. };
  15. #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
  16. /*
  17. * SwapCgroup implements "lookup" and "exchange" operations.
  18. * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
  19. * against SwapCache. At swap_free(), this is accessed directly from swap.
  20. *
  21. * This means,
  22. * - we have no race in "exchange" when we're accessed via SwapCache because
  23. * SwapCache(and its swp_entry) is under lock.
  24. * - When called via swap_free(), there is no user of this entry and no race.
  25. * Then, we don't need lock around "exchange".
  26. *
  27. * TODO: we can push these buffers out to HIGHMEM.
  28. */
  29. /*
  30. * allocate buffer for swap_cgroup.
  31. */
  32. static int swap_cgroup_prepare(int type)
  33. {
  34. struct page *page;
  35. struct swap_cgroup_ctrl *ctrl;
  36. unsigned long idx, max;
  37. ctrl = &swap_cgroup_ctrl[type];
  38. for (idx = 0; idx < ctrl->length; idx++) {
  39. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  40. if (!page)
  41. goto not_enough_page;
  42. ctrl->map[idx] = page;
  43. }
  44. return 0;
  45. not_enough_page:
  46. max = idx;
  47. for (idx = 0; idx < max; idx++)
  48. __free_page(ctrl->map[idx]);
  49. return -ENOMEM;
  50. }
  51. static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
  52. struct swap_cgroup_ctrl **ctrlp)
  53. {
  54. pgoff_t offset = swp_offset(ent);
  55. struct swap_cgroup_ctrl *ctrl;
  56. struct page *mappage;
  57. struct swap_cgroup *sc;
  58. ctrl = &swap_cgroup_ctrl[swp_type(ent)];
  59. if (ctrlp)
  60. *ctrlp = ctrl;
  61. mappage = ctrl->map[offset / SC_PER_PAGE];
  62. sc = page_address(mappage);
  63. return sc + offset % SC_PER_PAGE;
  64. }
  65. /**
  66. * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
  67. * @ent: swap entry to be cmpxchged
  68. * @old: old id
  69. * @new: new id
  70. *
  71. * Returns old id at success, 0 at failure.
  72. * (There is no mem_cgroup using 0 as its id)
  73. */
  74. unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
  75. unsigned short old, unsigned short new)
  76. {
  77. struct swap_cgroup_ctrl *ctrl;
  78. struct swap_cgroup *sc;
  79. unsigned long flags;
  80. unsigned short retval;
  81. sc = lookup_swap_cgroup(ent, &ctrl);
  82. spin_lock_irqsave(&ctrl->lock, flags);
  83. retval = sc->id;
  84. if (retval == old)
  85. sc->id = new;
  86. else
  87. retval = 0;
  88. spin_unlock_irqrestore(&ctrl->lock, flags);
  89. return retval;
  90. }
  91. /**
  92. * swap_cgroup_record - record mem_cgroup for this swp_entry.
  93. * @ent: swap entry to be recorded into
  94. * @id: mem_cgroup to be recorded
  95. *
  96. * Returns old value at success, 0 at failure.
  97. * (Of course, old value can be 0.)
  98. */
  99. unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
  100. {
  101. struct swap_cgroup_ctrl *ctrl;
  102. struct swap_cgroup *sc;
  103. unsigned short old;
  104. unsigned long flags;
  105. sc = lookup_swap_cgroup(ent, &ctrl);
  106. spin_lock_irqsave(&ctrl->lock, flags);
  107. old = sc->id;
  108. sc->id = id;
  109. spin_unlock_irqrestore(&ctrl->lock, flags);
  110. return old;
  111. }
  112. /**
  113. * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
  114. * @ent: swap entry to be looked up.
  115. *
  116. * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
  117. */
  118. unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
  119. {
  120. return lookup_swap_cgroup(ent, NULL)->id;
  121. }
  122. int swap_cgroup_swapon(int type, unsigned long max_pages)
  123. {
  124. void *array;
  125. unsigned long array_size;
  126. unsigned long length;
  127. struct swap_cgroup_ctrl *ctrl;
  128. if (!do_swap_account)
  129. return 0;
  130. length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
  131. array_size = length * sizeof(void *);
  132. array = vzalloc(array_size);
  133. if (!array)
  134. goto nomem;
  135. ctrl = &swap_cgroup_ctrl[type];
  136. mutex_lock(&swap_cgroup_mutex);
  137. ctrl->length = length;
  138. ctrl->map = array;
  139. spin_lock_init(&ctrl->lock);
  140. if (swap_cgroup_prepare(type)) {
  141. /* memory shortage */
  142. ctrl->map = NULL;
  143. ctrl->length = 0;
  144. mutex_unlock(&swap_cgroup_mutex);
  145. vfree(array);
  146. goto nomem;
  147. }
  148. mutex_unlock(&swap_cgroup_mutex);
  149. return 0;
  150. nomem:
  151. pr_info("couldn't allocate enough memory for swap_cgroup\n");
  152. pr_info("swap_cgroup can be disabled by swapaccount=0 boot option\n");
  153. return -ENOMEM;
  154. }
  155. void swap_cgroup_swapoff(int type)
  156. {
  157. struct page **map;
  158. unsigned long i, length;
  159. struct swap_cgroup_ctrl *ctrl;
  160. if (!do_swap_account)
  161. return;
  162. mutex_lock(&swap_cgroup_mutex);
  163. ctrl = &swap_cgroup_ctrl[type];
  164. map = ctrl->map;
  165. length = ctrl->length;
  166. ctrl->map = NULL;
  167. ctrl->length = 0;
  168. mutex_unlock(&swap_cgroup_mutex);
  169. if (map) {
  170. for (i = 0; i < length; i++) {
  171. struct page *page = map[i];
  172. if (page)
  173. __free_page(page);
  174. }
  175. vfree(map);
  176. }
  177. }