blob: 76f7b04032077aa0cf9ebd898037a942dddc5241 [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 Luickb85ced92016-07-28 15:21:24 -040062 struct work_struct del_work;
63 struct list_head del_list;
64 struct workqueue_struct *wq;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080065};
66
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080067static unsigned long mmu_node_start(struct mmu_rb_node *);
68static unsigned long mmu_node_last(struct mmu_rb_node *);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080069static inline void mmu_notifier_page(struct mmu_notifier *, struct mm_struct *,
70 unsigned long);
71static inline void mmu_notifier_range_start(struct mmu_notifier *,
72 struct mm_struct *,
73 unsigned long, unsigned long);
74static void mmu_notifier_mem_invalidate(struct mmu_notifier *,
Mitko Haralanovf19bd642016-04-12 10:45:57 -070075 struct mm_struct *,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080076 unsigned long, unsigned long);
77static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *,
78 unsigned long, unsigned long);
Dean Luickb85ced92016-07-28 15:21:24 -040079static void do_remove(struct mmu_rb_handler *handler,
80 struct list_head *del_list);
81static void handle_remove(struct work_struct *work);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080082
83static struct mmu_notifier_ops mn_opts = {
84 .invalidate_page = mmu_notifier_page,
85 .invalidate_range_start = mmu_notifier_range_start,
86};
87
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080088INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
89 mmu_node_start, mmu_node_last, static, __mmu_int_rb);
90
91static unsigned long mmu_node_start(struct mmu_rb_node *node)
92{
93 return node->addr & PAGE_MASK;
94}
95
96static unsigned long mmu_node_last(struct mmu_rb_node *node)
97{
Mitko Haralanovde790932016-04-12 10:46:41 -070098 return PAGE_ALIGN(node->addr + node->len) - 1;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080099}
100
Dean Luicke0b09ac2016-07-28 15:21:20 -0400101int hfi1_mmu_rb_register(void *ops_arg, struct mm_struct *mm,
102 struct mmu_rb_ops *ops,
Dean Luickb85ced92016-07-28 15:21:24 -0400103 struct workqueue_struct *wq,
Dean Luicke0b09ac2016-07-28 15:21:20 -0400104 struct mmu_rb_handler **handler)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800105{
106 struct mmu_rb_handler *handlr;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400107 int ret;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800108
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800109 handlr = kmalloc(sizeof(*handlr), GFP_KERNEL);
110 if (!handlr)
111 return -ENOMEM;
112
Dean Luicke0b09ac2016-07-28 15:21:20 -0400113 handlr->root = RB_ROOT;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800114 handlr->ops = ops;
Dean Luicke0b09ac2016-07-28 15:21:20 -0400115 handlr->ops_arg = ops_arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800116 INIT_HLIST_NODE(&handlr->mn.hlist);
117 spin_lock_init(&handlr->lock);
118 handlr->mn.ops = &mn_opts;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400119 handlr->mm = mm;
Dean Luickb85ced92016-07-28 15:21:24 -0400120 INIT_WORK(&handlr->del_work, handle_remove);
121 INIT_LIST_HEAD(&handlr->del_list);
122 handlr->wq = wq;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400123
124 ret = mmu_notifier_register(&handlr->mn, handlr->mm);
125 if (ret) {
126 kfree(handlr);
127 return ret;
128 }
129
Dean Luicke0b09ac2016-07-28 15:21:20 -0400130 *handler = handlr;
131 return 0;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800132}
133
Dean Luicke0b09ac2016-07-28 15:21:20 -0400134void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800135{
Dean Luick20a42d02016-07-28 12:27:36 -0400136 struct mmu_rb_node *rbnode;
137 struct rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800138 unsigned long flags;
Dean Luickb85ced92016-07-28 15:21:24 -0400139 struct list_head del_list;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800140
Mitko Haralanov782f6692016-04-12 10:46:35 -0700141 /* Unregister first so we don't get any more notifications. */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400142 mmu_notifier_unregister(&handler->mn, handler->mm);
Mitko Haralanov782f6692016-04-12 10:46:35 -0700143
Dean Luickb85ced92016-07-28 15:21:24 -0400144 /*
145 * Make sure the wq delete handler is finished running. It will not
146 * be triggered once the mmu notifiers are unregistered above.
147 */
148 flush_work(&handler->del_work);
149
150 INIT_LIST_HEAD(&del_list);
151
Mitko Haralanov782f6692016-04-12 10:46:35 -0700152 spin_lock_irqsave(&handler->lock, flags);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400153 while ((node = rb_first(&handler->root))) {
Dean Luick20a42d02016-07-28 12:27:36 -0400154 rbnode = rb_entry(node, struct mmu_rb_node, node);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400155 rb_erase(node, &handler->root);
Dean Luickb85ced92016-07-28 15:21:24 -0400156 list_add(&rbnode->list, &del_list);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800157 }
Mitko Haralanov782f6692016-04-12 10:46:35 -0700158 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800159
Dean Luickb85ced92016-07-28 15:21:24 -0400160 do_remove(handler, &del_list);
161
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800162 kfree(handler);
163}
164
Dean Luicke0b09ac2016-07-28 15:21:20 -0400165int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
166 struct mmu_rb_node *mnode)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800167{
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800168 struct mmu_rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800169 unsigned long flags;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800170 int ret = 0;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800171
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800172 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800173 hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr,
174 mnode->len);
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800175 node = __mmu_rb_search(handler, mnode->addr, mnode->len);
176 if (node) {
177 ret = -EINVAL;
178 goto unlock;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800179 }
Dean Luicke0b09ac2016-07-28 15:21:20 -0400180 __mmu_int_rb_insert(mnode, &handler->root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800181
Dean Luicke0b09ac2016-07-28 15:21:20 -0400182 ret = handler->ops->insert(handler->ops_arg, mnode);
Dean Luickc0946642016-07-28 12:27:30 -0400183 if (ret)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400184 __mmu_int_rb_remove(mnode, &handler->root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800185unlock:
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800186 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800187 return ret;
188}
189
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700190/* Caller must hold handler lock */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800191static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
192 unsigned long addr,
193 unsigned long len)
194{
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800195 struct mmu_rb_node *node = NULL;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800196
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800197 hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len);
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800198 if (!handler->ops->filter) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400199 node = __mmu_int_rb_iter_first(&handler->root, addr,
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800200 (addr + len) - 1);
201 } else {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400202 for (node = __mmu_int_rb_iter_first(&handler->root, addr,
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800203 (addr + len) - 1);
204 node;
205 node = __mmu_int_rb_iter_next(node, addr,
206 (addr + len) - 1)) {
207 if (handler->ops->filter(node, addr, len))
208 return node;
209 }
210 }
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800211 return node;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800212}
213
Dean Luicke0b09ac2016-07-28 15:21:20 -0400214struct mmu_rb_node *hfi1_mmu_rb_extract(struct mmu_rb_handler *handler,
Mitko Haralanovf53af852016-04-12 10:46:47 -0700215 unsigned long addr, unsigned long len)
216{
Mitko Haralanovf53af852016-04-12 10:46:47 -0700217 struct mmu_rb_node *node;
218 unsigned long flags;
219
Mitko Haralanovf53af852016-04-12 10:46:47 -0700220 spin_lock_irqsave(&handler->lock, flags);
221 node = __mmu_rb_search(handler, addr, len);
222 if (node)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400223 __mmu_int_rb_remove(node, &handler->root);
Mitko Haralanovf53af852016-04-12 10:46:47 -0700224 spin_unlock_irqrestore(&handler->lock, flags);
225
226 return node;
227}
228
Dean Luick10345992016-07-28 15:21:22 -0400229void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg)
230{
231 struct mmu_rb_node *rbnode;
232 struct rb_node *node, *next;
233 struct list_head del_list;
234 unsigned long flags;
235 bool stop = false;
236
237 INIT_LIST_HEAD(&del_list);
238
239 spin_lock_irqsave(&handler->lock, flags);
240 for (node = rb_first(&handler->root); node; node = next) {
241 next = rb_next(node);
242 rbnode = rb_entry(node, struct mmu_rb_node, node);
243 if (handler->ops->evict(handler->ops_arg, rbnode, evict_arg,
244 &stop)) {
245 __mmu_int_rb_remove(rbnode, &handler->root);
246 list_add(&rbnode->list, &del_list);
247 }
248 if (stop)
249 break;
250 }
251 spin_unlock_irqrestore(&handler->lock, flags);
252
Dean Luick10345992016-07-28 15:21:22 -0400253 while (!list_empty(&del_list)) {
254 rbnode = list_first_entry(&del_list, struct mmu_rb_node, list);
255 list_del(&rbnode->list);
256 handler->ops->remove(handler->ops_arg, rbnode,
257 handler->mm);
258 }
Dean Luick10345992016-07-28 15:21:22 -0400259}
260
Dean Luickb85ced92016-07-28 15:21:24 -0400261/*
262 * It is up to the caller to ensure that this function does not race with the
263 * mmu invalidate notifier which may be calling the users remove callback on
264 * 'node'.
265 */
Dean Luicke0b09ac2016-07-28 15:21:20 -0400266void hfi1_mmu_rb_remove(struct mmu_rb_handler *handler,
267 struct mmu_rb_node *node)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800268{
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400269 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800270
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400271 /* Validity of handler and node pointers has been checked by caller. */
272 hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr,
273 node->len);
274 spin_lock_irqsave(&handler->lock, flags);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400275 __mmu_int_rb_remove(node, &handler->root);
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400276 spin_unlock_irqrestore(&handler->lock, flags);
277
Dean Luicke0b09ac2016-07-28 15:21:20 -0400278 handler->ops->remove(handler->ops_arg, node, NULL);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800279}
280
281static inline void mmu_notifier_page(struct mmu_notifier *mn,
282 struct mm_struct *mm, unsigned long addr)
283{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700284 mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800285}
286
287static inline void mmu_notifier_range_start(struct mmu_notifier *mn,
288 struct mm_struct *mm,
289 unsigned long start,
290 unsigned long end)
291{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700292 mmu_notifier_mem_invalidate(mn, mm, start, end);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800293}
294
295static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn,
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700296 struct mm_struct *mm,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800297 unsigned long start, unsigned long end)
298{
299 struct mmu_rb_handler *handler =
300 container_of(mn, struct mmu_rb_handler, mn);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400301 struct rb_root *root = &handler->root;
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700302 struct mmu_rb_node *node, *ptr = NULL;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800303 unsigned long flags;
Dean Luickb85ced92016-07-28 15:21:24 -0400304 bool added = false;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800305
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800306 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700307 for (node = __mmu_int_rb_iter_first(root, start, end - 1);
308 node; node = ptr) {
309 /* Guard against node removal. */
310 ptr = __mmu_int_rb_iter_next(node, start, end - 1);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800311 hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u",
312 node->addr, node->len);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400313 if (handler->ops->invalidate(handler->ops_arg, node)) {
Mitko Haralanove88c9272016-04-12 10:46:53 -0700314 __mmu_int_rb_remove(node, root);
Dean Luickb85ced92016-07-28 15:21:24 -0400315 list_add(&node->list, &handler->del_list);
316 added = true;
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700317 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800318 }
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800319 spin_unlock_irqrestore(&handler->lock, flags);
Dean Luickb85ced92016-07-28 15:21:24 -0400320
321 if (added)
322 queue_work(handler->wq, &handler->del_work);
323}
324
325/*
326 * Call the remove function for the given handler and the list. This
327 * is expected to be called with a delete list extracted from handler.
328 * The caller should not be holding the handler lock.
329 */
330static void do_remove(struct mmu_rb_handler *handler,
331 struct list_head *del_list)
332{
333 struct mmu_rb_node *node;
334
335 while (!list_empty(del_list)) {
336 node = list_first_entry(del_list, struct mmu_rb_node, list);
337 list_del(&node->list);
338 handler->ops->remove(handler->ops_arg, node, handler->mm);
339 }
340}
341
342/*
343 * Work queue function to remove all nodes that have been queued up to
344 * be removed. The key feature is that mm->mmap_sem is not being held
345 * and the remove callback can sleep while taking it, if needed.
346 */
347static void handle_remove(struct work_struct *work)
348{
349 struct mmu_rb_handler *handler = container_of(work,
350 struct mmu_rb_handler,
351 del_work);
352 struct list_head del_list;
353 unsigned long flags;
354
355 /* remove anything that is queued to get removed */
356 spin_lock_irqsave(&handler->lock, flags);
357 list_replace_init(&handler->del_list, &del_list);
358 spin_unlock_irqrestore(&handler->lock, flags);
359
360 do_remove(handler, &del_list);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800361}