swap_cgroup.c 4.7 KB

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