blob: ccbf52c8ff6f037a485060e9d78f66c3b7fe79e6 [file] [log] [blame]
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -08001/*
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>
Mitko Haralanov67caea12016-05-12 10:23:09 -070048#include <linux/rculist.h>
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080049#include <linux/mmu_notifier.h>
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080050#include <linux/interval_tree_generic.h>
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080051
52#include "mmu_rb.h"
53#include "trace.h"
54
55struct mmu_rb_handler {
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080056 struct mmu_notifier mn;
Dean Luicke0b09ac2016-07-28 15:21:20 -040057 struct rb_root root;
58 void *ops_arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080059 spinlock_t lock; /* protect the RB tree */
60 struct mmu_rb_ops *ops;
Ira Weiny3faa3d92016-07-28 15:21:19 -040061 struct mm_struct *mm;
Dean Luick0636e9ab2016-07-28 15:21:27 -040062 struct list_head lru_list;
Dean Luickb85ced92016-07-28 15:21:24 -040063 struct work_struct del_work;
64 struct list_head del_list;
65 struct workqueue_struct *wq;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080066};
67
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080068static unsigned long mmu_node_start(struct mmu_rb_node *);
69static unsigned long mmu_node_last(struct mmu_rb_node *);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080070static inline void mmu_notifier_page(struct mmu_notifier *, struct mm_struct *,
71 unsigned long);
72static inline void mmu_notifier_range_start(struct mmu_notifier *,
73 struct mm_struct *,
74 unsigned long, unsigned long);
75static void mmu_notifier_mem_invalidate(struct mmu_notifier *,
Mitko Haralanovf19bd642016-04-12 10:45:57 -070076 struct mm_struct *,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080077 unsigned long, unsigned long);
78static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *,
79 unsigned long, unsigned long);
Dean Luickb85ced92016-07-28 15:21:24 -040080static void do_remove(struct mmu_rb_handler *handler,
81 struct list_head *del_list);
82static void handle_remove(struct work_struct *work);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080083
Bhumika Goyal0fc859a2016-11-19 15:17:48 +053084static const struct mmu_notifier_ops mn_opts = {
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080085 .invalidate_page = mmu_notifier_page,
86 .invalidate_range_start = mmu_notifier_range_start,
87};
88
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080089INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
90 mmu_node_start, mmu_node_last, static, __mmu_int_rb);
91
92static unsigned long mmu_node_start(struct mmu_rb_node *node)
93{
94 return node->addr & PAGE_MASK;
95}
96
97static unsigned long mmu_node_last(struct mmu_rb_node *node)
98{
Mitko Haralanovde790932016-04-12 10:46:41 -070099 return PAGE_ALIGN(node->addr + node->len) - 1;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800100}
101
Dean Luicke0b09ac2016-07-28 15:21:20 -0400102int hfi1_mmu_rb_register(void *ops_arg, struct mm_struct *mm,
103 struct mmu_rb_ops *ops,
Dean Luickb85ced92016-07-28 15:21:24 -0400104 struct workqueue_struct *wq,
Dean Luicke0b09ac2016-07-28 15:21:20 -0400105 struct mmu_rb_handler **handler)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800106{
107 struct mmu_rb_handler *handlr;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400108 int ret;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800109
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800110 handlr = kmalloc(sizeof(*handlr), GFP_KERNEL);
111 if (!handlr)
112 return -ENOMEM;
113
Dean Luicke0b09ac2016-07-28 15:21:20 -0400114 handlr->root = RB_ROOT;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800115 handlr->ops = ops;
Dean Luicke0b09ac2016-07-28 15:21:20 -0400116 handlr->ops_arg = ops_arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800117 INIT_HLIST_NODE(&handlr->mn.hlist);
118 spin_lock_init(&handlr->lock);
119 handlr->mn.ops = &mn_opts;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400120 handlr->mm = mm;
Dean Luickb85ced92016-07-28 15:21:24 -0400121 INIT_WORK(&handlr->del_work, handle_remove);
122 INIT_LIST_HEAD(&handlr->del_list);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400123 INIT_LIST_HEAD(&handlr->lru_list);
Dean Luickb85ced92016-07-28 15:21:24 -0400124 handlr->wq = wq;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400125
126 ret = mmu_notifier_register(&handlr->mn, handlr->mm);
127 if (ret) {
128 kfree(handlr);
129 return ret;
130 }
131
Dean Luicke0b09ac2016-07-28 15:21:20 -0400132 *handler = handlr;
133 return 0;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800134}
135
Dean Luicke0b09ac2016-07-28 15:21:20 -0400136void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800137{
Dean Luick20a42d02016-07-28 12:27:36 -0400138 struct mmu_rb_node *rbnode;
139 struct rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800140 unsigned long flags;
Dean Luickb85ced92016-07-28 15:21:24 -0400141 struct list_head del_list;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800142
Mitko Haralanov782f6692016-04-12 10:46:35 -0700143 /* Unregister first so we don't get any more notifications. */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400144 mmu_notifier_unregister(&handler->mn, handler->mm);
Mitko Haralanov782f6692016-04-12 10:46:35 -0700145
Dean Luickb85ced92016-07-28 15:21:24 -0400146 /*
147 * Make sure the wq delete handler is finished running. It will not
148 * be triggered once the mmu notifiers are unregistered above.
149 */
150 flush_work(&handler->del_work);
151
152 INIT_LIST_HEAD(&del_list);
153
Mitko Haralanov782f6692016-04-12 10:46:35 -0700154 spin_lock_irqsave(&handler->lock, flags);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400155 while ((node = rb_first(&handler->root))) {
Dean Luick20a42d02016-07-28 12:27:36 -0400156 rbnode = rb_entry(node, struct mmu_rb_node, node);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400157 rb_erase(node, &handler->root);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400158 /* move from LRU list to delete list */
159 list_move(&rbnode->list, &del_list);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800160 }
Mitko Haralanov782f6692016-04-12 10:46:35 -0700161 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800162
Dean Luickb85ced92016-07-28 15:21:24 -0400163 do_remove(handler, &del_list);
164
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800165 kfree(handler);
166}
167
Dean Luicke0b09ac2016-07-28 15:21:20 -0400168int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
169 struct mmu_rb_node *mnode)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800170{
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800171 struct mmu_rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800172 unsigned long flags;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800173 int ret = 0;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800174
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800175 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800176 hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr,
177 mnode->len);
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800178 node = __mmu_rb_search(handler, mnode->addr, mnode->len);
179 if (node) {
180 ret = -EINVAL;
181 goto unlock;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800182 }
Dean Luicke0b09ac2016-07-28 15:21:20 -0400183 __mmu_int_rb_insert(mnode, &handler->root);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400184 list_add(&mnode->list, &handler->lru_list);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800185
Dean Luicke0b09ac2016-07-28 15:21:20 -0400186 ret = handler->ops->insert(handler->ops_arg, mnode);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400187 if (ret) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400188 __mmu_int_rb_remove(mnode, &handler->root);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400189 list_del(&mnode->list); /* remove from LRU list */
190 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800191unlock:
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800192 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800193 return ret;
194}
195
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700196/* Caller must hold handler lock */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800197static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
198 unsigned long addr,
199 unsigned long len)
200{
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800201 struct mmu_rb_node *node = NULL;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800202
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800203 hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len);
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800204 if (!handler->ops->filter) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400205 node = __mmu_int_rb_iter_first(&handler->root, addr,
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800206 (addr + len) - 1);
207 } else {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400208 for (node = __mmu_int_rb_iter_first(&handler->root, addr,
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800209 (addr + len) - 1);
210 node;
211 node = __mmu_int_rb_iter_next(node, addr,
212 (addr + len) - 1)) {
213 if (handler->ops->filter(node, addr, len))
214 return node;
215 }
216 }
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800217 return node;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800218}
219
Dean Luicke0b09ac2016-07-28 15:21:20 -0400220struct mmu_rb_node *hfi1_mmu_rb_extract(struct mmu_rb_handler *handler,
Mitko Haralanovf53af852016-04-12 10:46:47 -0700221 unsigned long addr, unsigned long len)
222{
Mitko Haralanovf53af852016-04-12 10:46:47 -0700223 struct mmu_rb_node *node;
224 unsigned long flags;
225
Mitko Haralanovf53af852016-04-12 10:46:47 -0700226 spin_lock_irqsave(&handler->lock, flags);
227 node = __mmu_rb_search(handler, addr, len);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400228 if (node) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400229 __mmu_int_rb_remove(node, &handler->root);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400230 list_del(&node->list); /* remove from LRU list */
231 }
Mitko Haralanovf53af852016-04-12 10:46:47 -0700232 spin_unlock_irqrestore(&handler->lock, flags);
233
234 return node;
235}
236
Dean Luick10345992016-07-28 15:21:22 -0400237void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg)
238{
Dean Luick0636e9ab2016-07-28 15:21:27 -0400239 struct mmu_rb_node *rbnode, *ptr;
Dean Luick10345992016-07-28 15:21:22 -0400240 struct list_head del_list;
241 unsigned long flags;
242 bool stop = false;
243
244 INIT_LIST_HEAD(&del_list);
245
246 spin_lock_irqsave(&handler->lock, flags);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400247 list_for_each_entry_safe_reverse(rbnode, ptr, &handler->lru_list,
248 list) {
Dean Luick10345992016-07-28 15:21:22 -0400249 if (handler->ops->evict(handler->ops_arg, rbnode, evict_arg,
250 &stop)) {
251 __mmu_int_rb_remove(rbnode, &handler->root);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400252 /* move from LRU list to delete list */
253 list_move(&rbnode->list, &del_list);
Dean Luick10345992016-07-28 15:21:22 -0400254 }
255 if (stop)
256 break;
257 }
258 spin_unlock_irqrestore(&handler->lock, flags);
259
Dean Luick10345992016-07-28 15:21:22 -0400260 while (!list_empty(&del_list)) {
261 rbnode = list_first_entry(&del_list, struct mmu_rb_node, list);
262 list_del(&rbnode->list);
Dean Luick082b3532016-07-28 15:21:25 -0400263 handler->ops->remove(handler->ops_arg, rbnode);
Dean Luick10345992016-07-28 15:21:22 -0400264 }
Dean Luick10345992016-07-28 15:21:22 -0400265}
266
Dean Luickb85ced92016-07-28 15:21:24 -0400267/*
268 * It is up to the caller to ensure that this function does not race with the
269 * mmu invalidate notifier which may be calling the users remove callback on
270 * 'node'.
271 */
Dean Luicke0b09ac2016-07-28 15:21:20 -0400272void hfi1_mmu_rb_remove(struct mmu_rb_handler *handler,
273 struct mmu_rb_node *node)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800274{
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400275 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800276
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400277 /* Validity of handler and node pointers has been checked by caller. */
278 hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr,
279 node->len);
280 spin_lock_irqsave(&handler->lock, flags);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400281 __mmu_int_rb_remove(node, &handler->root);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400282 list_del(&node->list); /* remove from LRU list */
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400283 spin_unlock_irqrestore(&handler->lock, flags);
284
Dean Luick082b3532016-07-28 15:21:25 -0400285 handler->ops->remove(handler->ops_arg, node);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800286}
287
288static inline void mmu_notifier_page(struct mmu_notifier *mn,
289 struct mm_struct *mm, unsigned long addr)
290{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700291 mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800292}
293
294static inline void mmu_notifier_range_start(struct mmu_notifier *mn,
295 struct mm_struct *mm,
296 unsigned long start,
297 unsigned long end)
298{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700299 mmu_notifier_mem_invalidate(mn, mm, start, end);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800300}
301
302static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn,
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700303 struct mm_struct *mm,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800304 unsigned long start, unsigned long end)
305{
306 struct mmu_rb_handler *handler =
307 container_of(mn, struct mmu_rb_handler, mn);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400308 struct rb_root *root = &handler->root;
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700309 struct mmu_rb_node *node, *ptr = NULL;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800310 unsigned long flags;
Dean Luickb85ced92016-07-28 15:21:24 -0400311 bool added = false;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800312
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800313 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700314 for (node = __mmu_int_rb_iter_first(root, start, end - 1);
315 node; node = ptr) {
316 /* Guard against node removal. */
317 ptr = __mmu_int_rb_iter_next(node, start, end - 1);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800318 hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u",
319 node->addr, node->len);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400320 if (handler->ops->invalidate(handler->ops_arg, node)) {
Mitko Haralanove88c9272016-04-12 10:46:53 -0700321 __mmu_int_rb_remove(node, root);
Dean Luick0636e9ab2016-07-28 15:21:27 -0400322 /* move from LRU list to delete list */
323 list_move(&node->list, &handler->del_list);
Dean Luickb85ced92016-07-28 15:21:24 -0400324 added = true;
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700325 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800326 }
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800327 spin_unlock_irqrestore(&handler->lock, flags);
Dean Luickb85ced92016-07-28 15:21:24 -0400328
329 if (added)
330 queue_work(handler->wq, &handler->del_work);
331}
332
333/*
334 * Call the remove function for the given handler and the list. This
335 * is expected to be called with a delete list extracted from handler.
336 * The caller should not be holding the handler lock.
337 */
338static void do_remove(struct mmu_rb_handler *handler,
339 struct list_head *del_list)
340{
341 struct mmu_rb_node *node;
342
343 while (!list_empty(del_list)) {
344 node = list_first_entry(del_list, struct mmu_rb_node, list);
345 list_del(&node->list);
Dean Luick082b3532016-07-28 15:21:25 -0400346 handler->ops->remove(handler->ops_arg, node);
Dean Luickb85ced92016-07-28 15:21:24 -0400347 }
348}
349
350/*
351 * Work queue function to remove all nodes that have been queued up to
352 * be removed. The key feature is that mm->mmap_sem is not being held
353 * and the remove callback can sleep while taking it, if needed.
354 */
355static void handle_remove(struct work_struct *work)
356{
357 struct mmu_rb_handler *handler = container_of(work,
358 struct mmu_rb_handler,
359 del_work);
360 struct list_head del_list;
361 unsigned long flags;
362
363 /* remove anything that is queued to get removed */
364 spin_lock_irqsave(&handler->lock, flags);
365 list_replace_init(&handler->del_list, &del_list);
366 spin_unlock_irqrestore(&handler->lock, flags);
367
368 do_remove(handler, &del_list);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800369}