mmu_rb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright(c) 2016 - 2017 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_cached 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 int mmu_notifier_range_start(struct mmu_notifier *,
  68. struct mm_struct *,
  69. unsigned long, unsigned long, bool);
  70. static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *,
  71. unsigned long, unsigned long);
  72. static void do_remove(struct mmu_rb_handler *handler,
  73. struct list_head *del_list);
  74. static void handle_remove(struct work_struct *work);
  75. static const struct mmu_notifier_ops mn_opts = {
  76. .flags = MMU_INVALIDATE_DOES_NOT_BLOCK,
  77. .invalidate_range_start = mmu_notifier_range_start,
  78. };
  79. INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
  80. mmu_node_start, mmu_node_last, static, __mmu_int_rb);
  81. static unsigned long mmu_node_start(struct mmu_rb_node *node)
  82. {
  83. return node->addr & PAGE_MASK;
  84. }
  85. static unsigned long mmu_node_last(struct mmu_rb_node *node)
  86. {
  87. return PAGE_ALIGN(node->addr + node->len) - 1;
  88. }
  89. int hfi1_mmu_rb_register(void *ops_arg, struct mm_struct *mm,
  90. struct mmu_rb_ops *ops,
  91. struct workqueue_struct *wq,
  92. struct mmu_rb_handler **handler)
  93. {
  94. struct mmu_rb_handler *handlr;
  95. int ret;
  96. handlr = kmalloc(sizeof(*handlr), GFP_KERNEL);
  97. if (!handlr)
  98. return -ENOMEM;
  99. handlr->root = RB_ROOT_CACHED;
  100. handlr->ops = ops;
  101. handlr->ops_arg = ops_arg;
  102. INIT_HLIST_NODE(&handlr->mn.hlist);
  103. spin_lock_init(&handlr->lock);
  104. handlr->mn.ops = &mn_opts;
  105. handlr->mm = mm;
  106. INIT_WORK(&handlr->del_work, handle_remove);
  107. INIT_LIST_HEAD(&handlr->del_list);
  108. INIT_LIST_HEAD(&handlr->lru_list);
  109. handlr->wq = wq;
  110. ret = mmu_notifier_register(&handlr->mn, handlr->mm);
  111. if (ret) {
  112. kfree(handlr);
  113. return ret;
  114. }
  115. *handler = handlr;
  116. return 0;
  117. }
  118. void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler)
  119. {
  120. struct mmu_rb_node *rbnode;
  121. struct rb_node *node;
  122. unsigned long flags;
  123. struct list_head del_list;
  124. /* Unregister first so we don't get any more notifications. */
  125. mmu_notifier_unregister(&handler->mn, handler->mm);
  126. /*
  127. * Make sure the wq delete handler is finished running. It will not
  128. * be triggered once the mmu notifiers are unregistered above.
  129. */
  130. flush_work(&handler->del_work);
  131. INIT_LIST_HEAD(&del_list);
  132. spin_lock_irqsave(&handler->lock, flags);
  133. while ((node = rb_first_cached(&handler->root))) {
  134. rbnode = rb_entry(node, struct mmu_rb_node, node);
  135. rb_erase_cached(node, &handler->root);
  136. /* move from LRU list to delete list */
  137. list_move(&rbnode->list, &del_list);
  138. }
  139. spin_unlock_irqrestore(&handler->lock, flags);
  140. do_remove(handler, &del_list);
  141. kfree(handler);
  142. }
  143. int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
  144. struct mmu_rb_node *mnode)
  145. {
  146. struct mmu_rb_node *node;
  147. unsigned long flags;
  148. int ret = 0;
  149. trace_hfi1_mmu_rb_insert(mnode->addr, mnode->len);
  150. spin_lock_irqsave(&handler->lock, flags);
  151. node = __mmu_rb_search(handler, mnode->addr, mnode->len);
  152. if (node) {
  153. ret = -EINVAL;
  154. goto unlock;
  155. }
  156. __mmu_int_rb_insert(mnode, &handler->root);
  157. list_add(&mnode->list, &handler->lru_list);
  158. ret = handler->ops->insert(handler->ops_arg, mnode);
  159. if (ret) {
  160. __mmu_int_rb_remove(mnode, &handler->root);
  161. list_del(&mnode->list); /* remove from LRU list */
  162. }
  163. unlock:
  164. spin_unlock_irqrestore(&handler->lock, flags);
  165. return ret;
  166. }
  167. /* Caller must hold handler lock */
  168. static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
  169. unsigned long addr,
  170. unsigned long len)
  171. {
  172. struct mmu_rb_node *node = NULL;
  173. trace_hfi1_mmu_rb_search(addr, len);
  174. if (!handler->ops->filter) {
  175. node = __mmu_int_rb_iter_first(&handler->root, addr,
  176. (addr + len) - 1);
  177. } else {
  178. for (node = __mmu_int_rb_iter_first(&handler->root, addr,
  179. (addr + len) - 1);
  180. node;
  181. node = __mmu_int_rb_iter_next(node, addr,
  182. (addr + len) - 1)) {
  183. if (handler->ops->filter(node, addr, len))
  184. return node;
  185. }
  186. }
  187. return node;
  188. }
  189. bool hfi1_mmu_rb_remove_unless_exact(struct mmu_rb_handler *handler,
  190. unsigned long addr, unsigned long len,
  191. struct mmu_rb_node **rb_node)
  192. {
  193. struct mmu_rb_node *node;
  194. unsigned long flags;
  195. bool ret = false;
  196. spin_lock_irqsave(&handler->lock, flags);
  197. node = __mmu_rb_search(handler, addr, len);
  198. if (node) {
  199. if (node->addr == addr && node->len == len)
  200. goto unlock;
  201. __mmu_int_rb_remove(node, &handler->root);
  202. list_del(&node->list); /* remove from LRU list */
  203. ret = true;
  204. }
  205. unlock:
  206. spin_unlock_irqrestore(&handler->lock, flags);
  207. *rb_node = node;
  208. return ret;
  209. }
  210. void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg)
  211. {
  212. struct mmu_rb_node *rbnode, *ptr;
  213. struct list_head del_list;
  214. unsigned long flags;
  215. bool stop = false;
  216. INIT_LIST_HEAD(&del_list);
  217. spin_lock_irqsave(&handler->lock, flags);
  218. list_for_each_entry_safe_reverse(rbnode, ptr, &handler->lru_list,
  219. list) {
  220. if (handler->ops->evict(handler->ops_arg, rbnode, evict_arg,
  221. &stop)) {
  222. __mmu_int_rb_remove(rbnode, &handler->root);
  223. /* move from LRU list to delete list */
  224. list_move(&rbnode->list, &del_list);
  225. }
  226. if (stop)
  227. break;
  228. }
  229. spin_unlock_irqrestore(&handler->lock, flags);
  230. while (!list_empty(&del_list)) {
  231. rbnode = list_first_entry(&del_list, struct mmu_rb_node, list);
  232. list_del(&rbnode->list);
  233. handler->ops->remove(handler->ops_arg, rbnode);
  234. }
  235. }
  236. /*
  237. * It is up to the caller to ensure that this function does not race with the
  238. * mmu invalidate notifier which may be calling the users remove callback on
  239. * 'node'.
  240. */
  241. void hfi1_mmu_rb_remove(struct mmu_rb_handler *handler,
  242. struct mmu_rb_node *node)
  243. {
  244. unsigned long flags;
  245. /* Validity of handler and node pointers has been checked by caller. */
  246. trace_hfi1_mmu_rb_remove(node->addr, 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 int mmu_notifier_range_start(struct mmu_notifier *mn,
  254. struct mm_struct *mm,
  255. unsigned long start,
  256. unsigned long end,
  257. bool blockable)
  258. {
  259. struct mmu_rb_handler *handler =
  260. container_of(mn, struct mmu_rb_handler, mn);
  261. struct rb_root_cached *root = &handler->root;
  262. struct mmu_rb_node *node, *ptr = NULL;
  263. unsigned long flags;
  264. bool added = false;
  265. spin_lock_irqsave(&handler->lock, flags);
  266. for (node = __mmu_int_rb_iter_first(root, start, end - 1);
  267. node; node = ptr) {
  268. /* Guard against node removal. */
  269. ptr = __mmu_int_rb_iter_next(node, start, end - 1);
  270. trace_hfi1_mmu_mem_invalidate(node->addr, node->len);
  271. if (handler->ops->invalidate(handler->ops_arg, node)) {
  272. __mmu_int_rb_remove(node, root);
  273. /* move from LRU list to delete list */
  274. list_move(&node->list, &handler->del_list);
  275. added = true;
  276. }
  277. }
  278. spin_unlock_irqrestore(&handler->lock, flags);
  279. if (added)
  280. queue_work(handler->wq, &handler->del_work);
  281. return 0;
  282. }
  283. /*
  284. * Call the remove function for the given handler and the list. This
  285. * is expected to be called with a delete list extracted from handler.
  286. * The caller should not be holding the handler lock.
  287. */
  288. static void do_remove(struct mmu_rb_handler *handler,
  289. struct list_head *del_list)
  290. {
  291. struct mmu_rb_node *node;
  292. while (!list_empty(del_list)) {
  293. node = list_first_entry(del_list, struct mmu_rb_node, list);
  294. list_del(&node->list);
  295. handler->ops->remove(handler->ops_arg, node);
  296. }
  297. }
  298. /*
  299. * Work queue function to remove all nodes that have been queued up to
  300. * be removed. The key feature is that mm->mmap_sem is not being held
  301. * and the remove callback can sleep while taking it, if needed.
  302. */
  303. static void handle_remove(struct work_struct *work)
  304. {
  305. struct mmu_rb_handler *handler = container_of(work,
  306. struct mmu_rb_handler,
  307. del_work);
  308. struct list_head del_list;
  309. unsigned long flags;
  310. /* remove anything that is queued to get removed */
  311. spin_lock_irqsave(&handler->lock, flags);
  312. list_replace_init(&handler->del_list, &del_list);
  313. spin_unlock_irqrestore(&handler->lock, flags);
  314. do_remove(handler, &del_list);
  315. }