mmu_rb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright(c) 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/list.h>
  48. #include <linux/rculist.h>
  49. #include <linux/mmu_notifier.h>
  50. #include <linux/interval_tree_generic.h>
  51. #include "mmu_rb.h"
  52. #include "trace.h"
  53. struct mmu_rb_handler {
  54. struct mmu_notifier mn;
  55. struct rb_root root;
  56. void *ops_arg;
  57. spinlock_t lock; /* protect the RB tree */
  58. struct mmu_rb_ops *ops;
  59. struct mm_struct *mm;
  60. struct list_head lru_list;
  61. struct work_struct del_work;
  62. struct list_head del_list;
  63. struct workqueue_struct *wq;
  64. };
  65. static unsigned long mmu_node_start(struct mmu_rb_node *);
  66. static unsigned long mmu_node_last(struct mmu_rb_node *);
  67. static inline void mmu_notifier_page(struct mmu_notifier *, struct mm_struct *,
  68. unsigned long);
  69. static inline void mmu_notifier_range_start(struct mmu_notifier *,
  70. struct mm_struct *,
  71. unsigned long, unsigned long);
  72. static void mmu_notifier_mem_invalidate(struct mmu_notifier *,
  73. struct mm_struct *,
  74. unsigned long, unsigned long);
  75. static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *,
  76. unsigned long, unsigned long);
  77. static void do_remove(struct mmu_rb_handler *handler,
  78. struct list_head *del_list);
  79. static void handle_remove(struct work_struct *work);
  80. static const struct mmu_notifier_ops mn_opts = {
  81. .invalidate_page = mmu_notifier_page,
  82. .invalidate_range_start = mmu_notifier_range_start,
  83. };
  84. INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
  85. mmu_node_start, mmu_node_last, static, __mmu_int_rb);
  86. static unsigned long mmu_node_start(struct mmu_rb_node *node)
  87. {
  88. return node->addr & PAGE_MASK;
  89. }
  90. static unsigned long mmu_node_last(struct mmu_rb_node *node)
  91. {
  92. return PAGE_ALIGN(node->addr + node->len) - 1;
  93. }
  94. int hfi1_mmu_rb_register(void *ops_arg, struct mm_struct *mm,
  95. struct mmu_rb_ops *ops,
  96. struct workqueue_struct *wq,
  97. struct mmu_rb_handler **handler)
  98. {
  99. struct mmu_rb_handler *handlr;
  100. int ret;
  101. handlr = kmalloc(sizeof(*handlr), GFP_KERNEL);
  102. if (!handlr)
  103. return -ENOMEM;
  104. handlr->root = RB_ROOT;
  105. handlr->ops = ops;
  106. handlr->ops_arg = ops_arg;
  107. INIT_HLIST_NODE(&handlr->mn.hlist);
  108. spin_lock_init(&handlr->lock);
  109. handlr->mn.ops = &mn_opts;
  110. handlr->mm = mm;
  111. INIT_WORK(&handlr->del_work, handle_remove);
  112. INIT_LIST_HEAD(&handlr->del_list);
  113. INIT_LIST_HEAD(&handlr->lru_list);
  114. handlr->wq = wq;
  115. ret = mmu_notifier_register(&handlr->mn, handlr->mm);
  116. if (ret) {
  117. kfree(handlr);
  118. return ret;
  119. }
  120. *handler = handlr;
  121. return 0;
  122. }
  123. void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler)
  124. {
  125. struct mmu_rb_node *rbnode;
  126. struct rb_node *node;
  127. unsigned long flags;
  128. struct list_head del_list;
  129. /* Unregister first so we don't get any more notifications. */
  130. mmu_notifier_unregister(&handler->mn, handler->mm);
  131. /*
  132. * Make sure the wq delete handler is finished running. It will not
  133. * be triggered once the mmu notifiers are unregistered above.
  134. */
  135. flush_work(&handler->del_work);
  136. INIT_LIST_HEAD(&del_list);
  137. spin_lock_irqsave(&handler->lock, flags);
  138. while ((node = rb_first(&handler->root))) {
  139. rbnode = rb_entry(node, struct mmu_rb_node, node);
  140. rb_erase(node, &handler->root);
  141. /* move from LRU list to delete list */
  142. list_move(&rbnode->list, &del_list);
  143. }
  144. spin_unlock_irqrestore(&handler->lock, flags);
  145. do_remove(handler, &del_list);
  146. kfree(handler);
  147. }
  148. int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
  149. struct mmu_rb_node *mnode)
  150. {
  151. struct mmu_rb_node *node;
  152. unsigned long flags;
  153. int ret = 0;
  154. spin_lock_irqsave(&handler->lock, flags);
  155. hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr,
  156. mnode->len);
  157. node = __mmu_rb_search(handler, mnode->addr, mnode->len);
  158. if (node) {
  159. ret = -EINVAL;
  160. goto unlock;
  161. }
  162. __mmu_int_rb_insert(mnode, &handler->root);
  163. list_add(&mnode->list, &handler->lru_list);
  164. ret = handler->ops->insert(handler->ops_arg, mnode);
  165. if (ret) {
  166. __mmu_int_rb_remove(mnode, &handler->root);
  167. list_del(&mnode->list); /* remove from LRU list */
  168. }
  169. unlock:
  170. spin_unlock_irqrestore(&handler->lock, flags);
  171. return ret;
  172. }
  173. /* Caller must hold handler lock */
  174. static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
  175. unsigned long addr,
  176. unsigned long len)
  177. {
  178. struct mmu_rb_node *node = NULL;
  179. hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len);
  180. if (!handler->ops->filter) {
  181. node = __mmu_int_rb_iter_first(&handler->root, addr,
  182. (addr + len) - 1);
  183. } else {
  184. for (node = __mmu_int_rb_iter_first(&handler->root, addr,
  185. (addr + len) - 1);
  186. node;
  187. node = __mmu_int_rb_iter_next(node, addr,
  188. (addr + len) - 1)) {
  189. if (handler->ops->filter(node, addr, len))
  190. return node;
  191. }
  192. }
  193. return node;
  194. }
  195. struct mmu_rb_node *hfi1_mmu_rb_extract(struct mmu_rb_handler *handler,
  196. unsigned long addr, unsigned long len)
  197. {
  198. struct mmu_rb_node *node;
  199. unsigned long flags;
  200. spin_lock_irqsave(&handler->lock, flags);
  201. node = __mmu_rb_search(handler, addr, len);
  202. if (node) {
  203. __mmu_int_rb_remove(node, &handler->root);
  204. list_del(&node->list); /* remove from LRU list */
  205. }
  206. spin_unlock_irqrestore(&handler->lock, flags);
  207. return node;
  208. }
  209. void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg)
  210. {
  211. struct mmu_rb_node *rbnode, *ptr;
  212. struct list_head del_list;
  213. unsigned long flags;
  214. bool stop = false;
  215. INIT_LIST_HEAD(&del_list);
  216. spin_lock_irqsave(&handler->lock, flags);
  217. list_for_each_entry_safe_reverse(rbnode, ptr, &handler->lru_list,
  218. list) {
  219. if (handler->ops->evict(handler->ops_arg, rbnode, evict_arg,
  220. &stop)) {
  221. __mmu_int_rb_remove(rbnode, &handler->root);
  222. /* move from LRU list to delete list */
  223. list_move(&rbnode->list, &del_list);
  224. }
  225. if (stop)
  226. break;
  227. }
  228. spin_unlock_irqrestore(&handler->lock, flags);
  229. while (!list_empty(&del_list)) {
  230. rbnode = list_first_entry(&del_list, struct mmu_rb_node, list);
  231. list_del(&rbnode->list);
  232. handler->ops->remove(handler->ops_arg, rbnode);
  233. }
  234. }
  235. /*
  236. * It is up to the caller to ensure that this function does not race with the
  237. * mmu invalidate notifier which may be calling the users remove callback on
  238. * 'node'.
  239. */
  240. void hfi1_mmu_rb_remove(struct mmu_rb_handler *handler,
  241. struct mmu_rb_node *node)
  242. {
  243. unsigned long flags;
  244. /* Validity of handler and node pointers has been checked by caller. */
  245. hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr,
  246. node->len);
  247. spin_lock_irqsave(&handler->lock, flags);
  248. __mmu_int_rb_remove(node, &handler->root);
  249. list_del(&node->list); /* remove from LRU list */
  250. spin_unlock_irqrestore(&handler->lock, flags);
  251. handler->ops->remove(handler->ops_arg, node);
  252. }
  253. static inline void mmu_notifier_page(struct mmu_notifier *mn,
  254. struct mm_struct *mm, unsigned long addr)
  255. {
  256. mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE);
  257. }
  258. static inline void mmu_notifier_range_start(struct mmu_notifier *mn,
  259. struct mm_struct *mm,
  260. unsigned long start,
  261. unsigned long end)
  262. {
  263. mmu_notifier_mem_invalidate(mn, mm, start, end);
  264. }
  265. static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn,
  266. struct mm_struct *mm,
  267. unsigned long start, unsigned long end)
  268. {
  269. struct mmu_rb_handler *handler =
  270. container_of(mn, struct mmu_rb_handler, mn);
  271. struct rb_root *root = &handler->root;
  272. struct mmu_rb_node *node, *ptr = NULL;
  273. unsigned long flags;
  274. bool added = false;
  275. spin_lock_irqsave(&handler->lock, flags);
  276. for (node = __mmu_int_rb_iter_first(root, start, end - 1);
  277. node; node = ptr) {
  278. /* Guard against node removal. */
  279. ptr = __mmu_int_rb_iter_next(node, start, end - 1);
  280. hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u",
  281. node->addr, node->len);
  282. if (handler->ops->invalidate(handler->ops_arg, node)) {
  283. __mmu_int_rb_remove(node, root);
  284. /* move from LRU list to delete list */
  285. list_move(&node->list, &handler->del_list);
  286. added = true;
  287. }
  288. }
  289. spin_unlock_irqrestore(&handler->lock, flags);
  290. if (added)
  291. queue_work(handler->wq, &handler->del_work);
  292. }
  293. /*
  294. * Call the remove function for the given handler and the list. This
  295. * is expected to be called with a delete list extracted from handler.
  296. * The caller should not be holding the handler lock.
  297. */
  298. static void do_remove(struct mmu_rb_handler *handler,
  299. struct list_head *del_list)
  300. {
  301. struct mmu_rb_node *node;
  302. while (!list_empty(del_list)) {
  303. node = list_first_entry(del_list, struct mmu_rb_node, list);
  304. list_del(&node->list);
  305. handler->ops->remove(handler->ops_arg, node);
  306. }
  307. }
  308. /*
  309. * Work queue function to remove all nodes that have been queued up to
  310. * be removed. The key feature is that mm->mmap_sem is not being held
  311. * and the remove callback can sleep while taking it, if needed.
  312. */
  313. static void handle_remove(struct work_struct *work)
  314. {
  315. struct mmu_rb_handler *handler = container_of(work,
  316. struct mmu_rb_handler,
  317. del_work);
  318. struct list_head del_list;
  319. unsigned long flags;
  320. /* remove anything that is queued to get removed */
  321. spin_lock_irqsave(&handler->lock, flags);
  322. list_replace_init(&handler->del_list, &del_list);
  323. spin_unlock_irqrestore(&handler->lock, flags);
  324. do_remove(handler, &del_list);
  325. }