blob: b845adf9fc0edf8ade48a059a8940939fd836d56 [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 {
56 struct list_head list;
57 struct mmu_notifier mn;
58 struct rb_root *root;
59 spinlock_t lock; /* protect the RB tree */
60 struct mmu_rb_ops *ops;
61};
62
63static LIST_HEAD(mmu_rb_handlers);
64static DEFINE_SPINLOCK(mmu_rb_lock); /* protect mmu_rb_handlers list */
65
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080066static unsigned long mmu_node_start(struct mmu_rb_node *);
67static unsigned long mmu_node_last(struct mmu_rb_node *);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080068static struct mmu_rb_handler *find_mmu_handler(struct rb_root *);
69static 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);
79
80static struct mmu_notifier_ops mn_opts = {
81 .invalidate_page = mmu_notifier_page,
82 .invalidate_range_start = mmu_notifier_range_start,
83};
84
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080085INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
86 mmu_node_start, mmu_node_last, static, __mmu_int_rb);
87
88static unsigned long mmu_node_start(struct mmu_rb_node *node)
89{
90 return node->addr & PAGE_MASK;
91}
92
93static unsigned long mmu_node_last(struct mmu_rb_node *node)
94{
Mitko Haralanovde790932016-04-12 10:46:41 -070095 return PAGE_ALIGN(node->addr + node->len) - 1;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080096}
97
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080098int hfi1_mmu_rb_register(struct rb_root *root, struct mmu_rb_ops *ops)
99{
100 struct mmu_rb_handler *handlr;
101
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800102 handlr = kmalloc(sizeof(*handlr), GFP_KERNEL);
103 if (!handlr)
104 return -ENOMEM;
105
106 handlr->root = root;
107 handlr->ops = ops;
108 INIT_HLIST_NODE(&handlr->mn.hlist);
109 spin_lock_init(&handlr->lock);
110 handlr->mn.ops = &mn_opts;
Mitko Haralanov67caea12016-05-12 10:23:09 -0700111 spin_lock(&mmu_rb_lock);
112 list_add_tail_rcu(&handlr->list, &mmu_rb_handlers);
113 spin_unlock(&mmu_rb_lock);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800114
115 return mmu_notifier_register(&handlr->mn, current->mm);
116}
117
118void hfi1_mmu_rb_unregister(struct rb_root *root)
119{
120 struct mmu_rb_handler *handler = find_mmu_handler(root);
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800121 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800122
Mitko Haralanov4b00d942016-03-08 11:14:31 -0800123 if (!handler)
124 return;
125
Mitko Haralanov782f6692016-04-12 10:46:35 -0700126 /* Unregister first so we don't get any more notifications. */
127 if (current->mm)
128 mmu_notifier_unregister(&handler->mn, current->mm);
129
Mitko Haralanov67caea12016-05-12 10:23:09 -0700130 spin_lock(&mmu_rb_lock);
131 list_del_rcu(&handler->list);
132 spin_unlock(&mmu_rb_lock);
133 synchronize_rcu();
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800134
Mitko Haralanov782f6692016-04-12 10:46:35 -0700135 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800136 if (!RB_EMPTY_ROOT(root)) {
137 struct rb_node *node;
138 struct mmu_rb_node *rbnode;
139
140 while ((node = rb_first(root))) {
141 rbnode = rb_entry(node, struct mmu_rb_node, node);
Mitko Haralanoveef9c892016-03-08 11:14:36 -0800142 rb_erase(node, root);
Dean Luickc0946642016-07-28 12:27:30 -0400143 handler->ops->remove(root, rbnode, NULL);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800144 }
145 }
Mitko Haralanov782f6692016-04-12 10:46:35 -0700146 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800147
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800148 kfree(handler);
149}
150
151int hfi1_mmu_rb_insert(struct rb_root *root, struct mmu_rb_node *mnode)
152{
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800153 struct mmu_rb_handler *handler = find_mmu_handler(root);
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800154 struct mmu_rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800155 unsigned long flags;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800156 int ret = 0;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800157
158 if (!handler)
159 return -EINVAL;
160
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800161 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800162 hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr,
163 mnode->len);
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800164 node = __mmu_rb_search(handler, mnode->addr, mnode->len);
165 if (node) {
166 ret = -EINVAL;
167 goto unlock;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800168 }
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800169 __mmu_int_rb_insert(mnode, root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800170
Dean Luickc0946642016-07-28 12:27:30 -0400171 ret = handler->ops->insert(root, mnode);
172 if (ret)
173 __mmu_int_rb_remove(mnode, root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800174unlock:
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800175 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800176 return ret;
177}
178
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700179/* Caller must hold handler lock */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800180static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
181 unsigned long addr,
182 unsigned long len)
183{
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800184 struct mmu_rb_node *node = NULL;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800185
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800186 hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len);
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800187 if (!handler->ops->filter) {
188 node = __mmu_int_rb_iter_first(handler->root, addr,
189 (addr + len) - 1);
190 } else {
191 for (node = __mmu_int_rb_iter_first(handler->root, addr,
192 (addr + len) - 1);
193 node;
194 node = __mmu_int_rb_iter_next(node, addr,
195 (addr + len) - 1)) {
196 if (handler->ops->filter(node, addr, len))
197 return node;
198 }
199 }
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800200 return node;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800201}
202
Mitko Haralanovf53af852016-04-12 10:46:47 -0700203struct mmu_rb_node *hfi1_mmu_rb_extract(struct rb_root *root,
204 unsigned long addr, unsigned long len)
205{
206 struct mmu_rb_handler *handler = find_mmu_handler(root);
207 struct mmu_rb_node *node;
208 unsigned long flags;
209
210 if (!handler)
211 return ERR_PTR(-EINVAL);
212
213 spin_lock_irqsave(&handler->lock, flags);
214 node = __mmu_rb_search(handler, addr, len);
215 if (node)
216 __mmu_int_rb_remove(node, handler->root);
217 spin_unlock_irqrestore(&handler->lock, flags);
218
219 return node;
220}
221
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800222void hfi1_mmu_rb_remove(struct rb_root *root, struct mmu_rb_node *node)
223{
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400224 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800225 struct mmu_rb_handler *handler = find_mmu_handler(root);
226
227 if (!handler || !node)
228 return;
229
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400230 /* Validity of handler and node pointers has been checked by caller. */
231 hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr,
232 node->len);
233 spin_lock_irqsave(&handler->lock, flags);
234 __mmu_int_rb_remove(node, handler->root);
235 spin_unlock_irqrestore(&handler->lock, flags);
236
237 handler->ops->remove(handler->root, node, NULL);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800238}
239
240static struct mmu_rb_handler *find_mmu_handler(struct rb_root *root)
241{
242 struct mmu_rb_handler *handler;
243
Mitko Haralanov67caea12016-05-12 10:23:09 -0700244 rcu_read_lock();
245 list_for_each_entry_rcu(handler, &mmu_rb_handlers, list) {
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800246 if (handler->root == root)
247 goto unlock;
248 }
249 handler = NULL;
250unlock:
Mitko Haralanov67caea12016-05-12 10:23:09 -0700251 rcu_read_unlock();
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800252 return handler;
253}
254
255static inline void mmu_notifier_page(struct mmu_notifier *mn,
256 struct mm_struct *mm, unsigned long addr)
257{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700258 mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800259}
260
261static inline void mmu_notifier_range_start(struct mmu_notifier *mn,
262 struct mm_struct *mm,
263 unsigned long start,
264 unsigned long end)
265{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700266 mmu_notifier_mem_invalidate(mn, mm, start, end);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800267}
268
269static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn,
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700270 struct mm_struct *mm,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800271 unsigned long start, unsigned long end)
272{
273 struct mmu_rb_handler *handler =
274 container_of(mn, struct mmu_rb_handler, mn);
275 struct rb_root *root = handler->root;
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700276 struct mmu_rb_node *node, *ptr = NULL;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800277 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800278
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800279 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700280 for (node = __mmu_int_rb_iter_first(root, start, end - 1);
281 node; node = ptr) {
282 /* Guard against node removal. */
283 ptr = __mmu_int_rb_iter_next(node, start, end - 1);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800284 hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u",
285 node->addr, node->len);
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700286 if (handler->ops->invalidate(root, node)) {
Mitko Haralanove88c9272016-04-12 10:46:53 -0700287 __mmu_int_rb_remove(node, root);
Dean Luickc0946642016-07-28 12:27:30 -0400288 handler->ops->remove(root, node, mm);
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700289 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800290 }
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800291 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800292}