blob: e5c5ef4cf06ca8712640fa35632fd2309e8f9c6f [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;
Ira Weiny3faa3d92016-07-28 15:21:19 -040061 struct mm_struct *mm;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080062};
63
64static LIST_HEAD(mmu_rb_handlers);
65static DEFINE_SPINLOCK(mmu_rb_lock); /* protect mmu_rb_handlers list */
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 struct mmu_rb_handler *find_mmu_handler(struct rb_root *);
70static 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);
80
81static struct mmu_notifier_ops mn_opts = {
82 .invalidate_page = mmu_notifier_page,
83 .invalidate_range_start = mmu_notifier_range_start,
84};
85
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080086INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
87 mmu_node_start, mmu_node_last, static, __mmu_int_rb);
88
89static unsigned long mmu_node_start(struct mmu_rb_node *node)
90{
91 return node->addr & PAGE_MASK;
92}
93
94static unsigned long mmu_node_last(struct mmu_rb_node *node)
95{
Mitko Haralanovde790932016-04-12 10:46:41 -070096 return PAGE_ALIGN(node->addr + node->len) - 1;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080097}
98
Ira Weiny3faa3d92016-07-28 15:21:19 -040099int hfi1_mmu_rb_register(struct mm_struct *mm, struct rb_root *root,
100 struct mmu_rb_ops *ops)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800101{
102 struct mmu_rb_handler *handlr;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400103 int ret;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800104
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800105 handlr = kmalloc(sizeof(*handlr), GFP_KERNEL);
106 if (!handlr)
107 return -ENOMEM;
108
109 handlr->root = root;
110 handlr->ops = ops;
111 INIT_HLIST_NODE(&handlr->mn.hlist);
112 spin_lock_init(&handlr->lock);
113 handlr->mn.ops = &mn_opts;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400114 handlr->mm = mm;
115
116 ret = mmu_notifier_register(&handlr->mn, handlr->mm);
117 if (ret) {
118 kfree(handlr);
119 return ret;
120 }
121
Mitko Haralanov67caea12016-05-12 10:23:09 -0700122 spin_lock(&mmu_rb_lock);
123 list_add_tail_rcu(&handlr->list, &mmu_rb_handlers);
124 spin_unlock(&mmu_rb_lock);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800125
Ira Weiny3faa3d92016-07-28 15:21:19 -0400126 return ret;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800127}
128
129void hfi1_mmu_rb_unregister(struct rb_root *root)
130{
131 struct mmu_rb_handler *handler = find_mmu_handler(root);
Dean Luick20a42d02016-07-28 12:27:36 -0400132 struct mmu_rb_node *rbnode;
133 struct rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800134 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800135
Mitko Haralanov4b00d942016-03-08 11:14:31 -0800136 if (!handler)
137 return;
138
Mitko Haralanov782f6692016-04-12 10:46:35 -0700139 /* Unregister first so we don't get any more notifications. */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400140 mmu_notifier_unregister(&handler->mn, handler->mm);
Mitko Haralanov782f6692016-04-12 10:46:35 -0700141
Mitko Haralanov67caea12016-05-12 10:23:09 -0700142 spin_lock(&mmu_rb_lock);
143 list_del_rcu(&handler->list);
144 spin_unlock(&mmu_rb_lock);
145 synchronize_rcu();
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800146
Mitko Haralanov782f6692016-04-12 10:46:35 -0700147 spin_lock_irqsave(&handler->lock, flags);
Dean Luick20a42d02016-07-28 12:27:36 -0400148 while ((node = rb_first(root))) {
149 rbnode = rb_entry(node, struct mmu_rb_node, node);
150 rb_erase(node, root);
151 handler->ops->remove(root, rbnode, NULL);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800152 }
Mitko Haralanov782f6692016-04-12 10:46:35 -0700153 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800154
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800155 kfree(handler);
156}
157
158int hfi1_mmu_rb_insert(struct rb_root *root, struct mmu_rb_node *mnode)
159{
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800160 struct mmu_rb_handler *handler = find_mmu_handler(root);
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800161 struct mmu_rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800162 unsigned long flags;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800163 int ret = 0;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800164
165 if (!handler)
166 return -EINVAL;
167
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800168 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800169 hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr,
170 mnode->len);
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800171 node = __mmu_rb_search(handler, mnode->addr, mnode->len);
172 if (node) {
173 ret = -EINVAL;
174 goto unlock;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800175 }
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800176 __mmu_int_rb_insert(mnode, root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800177
Dean Luickc0946642016-07-28 12:27:30 -0400178 ret = handler->ops->insert(root, mnode);
179 if (ret)
180 __mmu_int_rb_remove(mnode, root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800181unlock:
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800182 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800183 return ret;
184}
185
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700186/* Caller must hold handler lock */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800187static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
188 unsigned long addr,
189 unsigned long len)
190{
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800191 struct mmu_rb_node *node = NULL;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800192
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800193 hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len);
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800194 if (!handler->ops->filter) {
195 node = __mmu_int_rb_iter_first(handler->root, addr,
196 (addr + len) - 1);
197 } else {
198 for (node = __mmu_int_rb_iter_first(handler->root, addr,
199 (addr + len) - 1);
200 node;
201 node = __mmu_int_rb_iter_next(node, addr,
202 (addr + len) - 1)) {
203 if (handler->ops->filter(node, addr, len))
204 return node;
205 }
206 }
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800207 return node;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800208}
209
Mitko Haralanovf53af852016-04-12 10:46:47 -0700210struct mmu_rb_node *hfi1_mmu_rb_extract(struct rb_root *root,
211 unsigned long addr, unsigned long len)
212{
213 struct mmu_rb_handler *handler = find_mmu_handler(root);
214 struct mmu_rb_node *node;
215 unsigned long flags;
216
217 if (!handler)
218 return ERR_PTR(-EINVAL);
219
220 spin_lock_irqsave(&handler->lock, flags);
221 node = __mmu_rb_search(handler, addr, len);
222 if (node)
223 __mmu_int_rb_remove(node, handler->root);
224 spin_unlock_irqrestore(&handler->lock, flags);
225
226 return node;
227}
228
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800229void hfi1_mmu_rb_remove(struct rb_root *root, struct mmu_rb_node *node)
230{
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400231 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800232 struct mmu_rb_handler *handler = find_mmu_handler(root);
233
234 if (!handler || !node)
235 return;
236
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400237 /* Validity of handler and node pointers has been checked by caller. */
238 hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr,
239 node->len);
240 spin_lock_irqsave(&handler->lock, flags);
241 __mmu_int_rb_remove(node, handler->root);
242 spin_unlock_irqrestore(&handler->lock, flags);
243
244 handler->ops->remove(handler->root, node, NULL);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800245}
246
247static struct mmu_rb_handler *find_mmu_handler(struct rb_root *root)
248{
249 struct mmu_rb_handler *handler;
250
Mitko Haralanov67caea12016-05-12 10:23:09 -0700251 rcu_read_lock();
252 list_for_each_entry_rcu(handler, &mmu_rb_handlers, list) {
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800253 if (handler->root == root)
254 goto unlock;
255 }
256 handler = NULL;
257unlock:
Mitko Haralanov67caea12016-05-12 10:23:09 -0700258 rcu_read_unlock();
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800259 return handler;
260}
261
262static inline void mmu_notifier_page(struct mmu_notifier *mn,
263 struct mm_struct *mm, unsigned long addr)
264{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700265 mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800266}
267
268static inline void mmu_notifier_range_start(struct mmu_notifier *mn,
269 struct mm_struct *mm,
270 unsigned long start,
271 unsigned long end)
272{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700273 mmu_notifier_mem_invalidate(mn, mm, start, end);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800274}
275
276static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn,
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700277 struct mm_struct *mm,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800278 unsigned long start, unsigned long end)
279{
280 struct mmu_rb_handler *handler =
281 container_of(mn, struct mmu_rb_handler, mn);
282 struct rb_root *root = handler->root;
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700283 struct mmu_rb_node *node, *ptr = NULL;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800284 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800285
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800286 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700287 for (node = __mmu_int_rb_iter_first(root, start, end - 1);
288 node; node = ptr) {
289 /* Guard against node removal. */
290 ptr = __mmu_int_rb_iter_next(node, start, end - 1);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800291 hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u",
292 node->addr, node->len);
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700293 if (handler->ops->invalidate(root, node)) {
Mitko Haralanove88c9272016-04-12 10:46:53 -0700294 __mmu_int_rb_remove(node, root);
Dean Luickc0946642016-07-28 12:27:30 -0400295 handler->ops->remove(root, node, mm);
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700296 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800297 }
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800298 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800299}