swap_cgroup.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. if (!(idx % SWAP_CLUSTER_MAX))
  44. cond_resched();
  45. }
  46. return 0;
  47. not_enough_page:
  48. max = idx;
  49. for (idx = 0; idx < max; idx++)
  50. __free_page(ctrl->map[idx]);
  51. return -ENOMEM;
  52. }
  53. static struct swap_cgroup *__lookup_swap_cgroup(struct swap_cgroup_ctrl *ctrl,
  54. pgoff_t offset)
  55. {
  56. struct page *mappage;
  57. struct swap_cgroup *sc;
  58. mappage = ctrl->map[offset / SC_PER_PAGE];
  59. sc = page_address(mappage);
  60. return sc + offset % SC_PER_PAGE;
  61. }
  62. static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
  63. struct swap_cgroup_ctrl **ctrlp)
  64. {
  65. pgoff_t offset = swp_offset(ent);
  66. struct swap_cgroup_ctrl *ctrl;
  67. ctrl = &swap_cgroup_ctrl[swp_type(ent)];
  68. if (ctrlp)
  69. *ctrlp = ctrl;
  70. return __lookup_swap_cgroup(ctrl, offset);
  71. }
  72. /**
  73. * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
  74. * @ent: swap entry to be cmpxchged
  75. * @old: old id
  76. * @new: new id
  77. *
  78. * Returns old id at success, 0 at failure.
  79. * (There is no mem_cgroup using 0 as its id)
  80. */
  81. unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
  82. unsigned short old, unsigned short new)
  83. {
  84. struct swap_cgroup_ctrl *ctrl;
  85. struct swap_cgroup *sc;
  86. unsigned long flags;
  87. unsigned short retval;
  88. sc = lookup_swap_cgroup(ent, &ctrl);
  89. spin_lock_irqsave(&ctrl->lock, flags);
  90. retval = sc->id;
  91. if (retval == old)
  92. sc->id = new;
  93. else
  94. retval = 0;
  95. spin_unlock_irqrestore(&ctrl->lock, flags);
  96. return retval;
  97. }
  98. /**
  99. * swap_cgroup_record - record mem_cgroup for a set of swap entries
  100. * @ent: the first swap entry to be recorded into
  101. * @id: mem_cgroup to be recorded
  102. * @nr_ents: number of swap entries to be recorded
  103. *
  104. * Returns old value at success, 0 at failure.
  105. * (Of course, old value can be 0.)
  106. */
  107. unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
  108. unsigned int nr_ents)
  109. {
  110. struct swap_cgroup_ctrl *ctrl;
  111. struct swap_cgroup *sc;
  112. unsigned short old;
  113. unsigned long flags;
  114. pgoff_t offset = swp_offset(ent);
  115. pgoff_t end = offset + nr_ents;
  116. sc = lookup_swap_cgroup(ent, &ctrl);
  117. spin_lock_irqsave(&ctrl->lock, flags);
  118. old = sc->id;
  119. for (;;) {
  120. VM_BUG_ON(sc->id != old);
  121. sc->id = id;
  122. offset++;
  123. if (offset == end)
  124. break;
  125. if (offset % SC_PER_PAGE)
  126. sc++;
  127. else
  128. sc = __lookup_swap_cgroup(ctrl, offset);
  129. }
  130. spin_unlock_irqrestore(&ctrl->lock, flags);
  131. return old;
  132. }
  133. /**
  134. * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
  135. * @ent: swap entry to be looked up.
  136. *
  137. * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
  138. */
  139. unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
  140. {
  141. return lookup_swap_cgroup(ent, NULL)->id;
  142. }
  143. int swap_cgroup_swapon(int type, unsigned long max_pages)
  144. {
  145. void *array;
  146. unsigned long array_size;
  147. unsigned long length;
  148. struct swap_cgroup_ctrl *ctrl;
  149. if (!do_swap_account)
  150. return 0;
  151. length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
  152. array_size = length * sizeof(void *);
  153. array = vzalloc(array_size);
  154. if (!array)
  155. goto nomem;
  156. ctrl = &swap_cgroup_ctrl[type];
  157. mutex_lock(&swap_cgroup_mutex);
  158. ctrl->length = length;
  159. ctrl->map = array;
  160. spin_lock_init(&ctrl->lock);
  161. if (swap_cgroup_prepare(type)) {
  162. /* memory shortage */
  163. ctrl->map = NULL;
  164. ctrl->length = 0;
  165. mutex_unlock(&swap_cgroup_mutex);
  166. vfree(array);
  167. goto nomem;
  168. }
  169. mutex_unlock(&swap_cgroup_mutex);
  170. return 0;
  171. nomem:
  172. pr_info("couldn't allocate enough memory for swap_cgroup\n");
  173. pr_info("swap_cgroup can be disabled by swapaccount=0 boot option\n");
  174. return -ENOMEM;
  175. }
  176. void swap_cgroup_swapoff(int type)
  177. {
  178. struct page **map;
  179. unsigned long i, length;
  180. struct swap_cgroup_ctrl *ctrl;
  181. if (!do_swap_account)
  182. return;
  183. mutex_lock(&swap_cgroup_mutex);
  184. ctrl = &swap_cgroup_ctrl[type];
  185. map = ctrl->map;
  186. length = ctrl->length;
  187. ctrl->map = NULL;
  188. ctrl->length = 0;
  189. mutex_unlock(&swap_cgroup_mutex);
  190. if (map) {
  191. for (i = 0; i < length; i++) {
  192. struct page *page = map[i];
  193. if (page)
  194. __free_page(page);
  195. if (!(i % SWAP_CLUSTER_MAX))
  196. cond_resched();
  197. }
  198. vfree(map);
  199. }
  200. }