mmu_rb.c 11 KB

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