blob: 1c7e25b90a2cd6cf527d804676bf68163d2ced13 [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);
Dean Luick20a42d02016-07-28 12:27:36 -0400121 struct mmu_rb_node *rbnode;
122 struct rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800123 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800124
Mitko Haralanov4b00d942016-03-08 11:14:31 -0800125 if (!handler)
126 return;
127
Mitko Haralanov782f6692016-04-12 10:46:35 -0700128 /* Unregister first so we don't get any more notifications. */
129 if (current->mm)
130 mmu_notifier_unregister(&handler->mn, current->mm);
131
Mitko Haralanov67caea12016-05-12 10:23:09 -0700132 spin_lock(&mmu_rb_lock);
133 list_del_rcu(&handler->list);
134 spin_unlock(&mmu_rb_lock);
135 synchronize_rcu();
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800136
Mitko Haralanov782f6692016-04-12 10:46:35 -0700137 spin_lock_irqsave(&handler->lock, flags);
Dean Luick20a42d02016-07-28 12:27:36 -0400138 while ((node = rb_first(root))) {
139 rbnode = rb_entry(node, struct mmu_rb_node, node);
140 rb_erase(node, root);
141 handler->ops->remove(root, rbnode, NULL);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800142 }
Mitko Haralanov782f6692016-04-12 10:46:35 -0700143 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800144
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800145 kfree(handler);
146}
147
148int hfi1_mmu_rb_insert(struct rb_root *root, struct mmu_rb_node *mnode)
149{
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800150 struct mmu_rb_handler *handler = find_mmu_handler(root);
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800151 struct mmu_rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800152 unsigned long flags;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800153 int ret = 0;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800154
155 if (!handler)
156 return -EINVAL;
157
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800158 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800159 hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr,
160 mnode->len);
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800161 node = __mmu_rb_search(handler, mnode->addr, mnode->len);
162 if (node) {
163 ret = -EINVAL;
164 goto unlock;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800165 }
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800166 __mmu_int_rb_insert(mnode, root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800167
Dean Luickc0946642016-07-28 12:27:30 -0400168 ret = handler->ops->insert(root, mnode);
169 if (ret)
170 __mmu_int_rb_remove(mnode, root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800171unlock:
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800172 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800173 return ret;
174}
175
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700176/* Caller must hold handler lock */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800177static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
178 unsigned long addr,
179 unsigned long len)
180{
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800181 struct mmu_rb_node *node = NULL;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800182
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800183 hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len);
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800184 if (!handler->ops->filter) {
185 node = __mmu_int_rb_iter_first(handler->root, addr,
186 (addr + len) - 1);
187 } else {
188 for (node = __mmu_int_rb_iter_first(handler->root, addr,
189 (addr + len) - 1);
190 node;
191 node = __mmu_int_rb_iter_next(node, addr,
192 (addr + len) - 1)) {
193 if (handler->ops->filter(node, addr, len))
194 return node;
195 }
196 }
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800197 return node;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800198}
199
Mitko Haralanovf53af852016-04-12 10:46:47 -0700200struct mmu_rb_node *hfi1_mmu_rb_extract(struct rb_root *root,
201 unsigned long addr, unsigned long len)
202{
203 struct mmu_rb_handler *handler = find_mmu_handler(root);
204 struct mmu_rb_node *node;
205 unsigned long flags;
206
207 if (!handler)
208 return ERR_PTR(-EINVAL);
209
210 spin_lock_irqsave(&handler->lock, flags);
211 node = __mmu_rb_search(handler, addr, len);
212 if (node)
213 __mmu_int_rb_remove(node, handler->root);
214 spin_unlock_irqrestore(&handler->lock, flags);
215
216 return node;
217}
218
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800219void hfi1_mmu_rb_remove(struct rb_root *root, struct mmu_rb_node *node)
220{
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400221 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800222 struct mmu_rb_handler *handler = find_mmu_handler(root);
223
224 if (!handler || !node)
225 return;
226
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400227 /* Validity of handler and node pointers has been checked by caller. */
228 hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr,
229 node->len);
230 spin_lock_irqsave(&handler->lock, flags);
231 __mmu_int_rb_remove(node, handler->root);
232 spin_unlock_irqrestore(&handler->lock, flags);
233
234 handler->ops->remove(handler->root, node, NULL);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800235}
236
237static struct mmu_rb_handler *find_mmu_handler(struct rb_root *root)
238{
239 struct mmu_rb_handler *handler;
240
Mitko Haralanov67caea12016-05-12 10:23:09 -0700241 rcu_read_lock();
242 list_for_each_entry_rcu(handler, &mmu_rb_handlers, list) {
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800243 if (handler->root == root)
244 goto unlock;
245 }
246 handler = NULL;
247unlock:
Mitko Haralanov67caea12016-05-12 10:23:09 -0700248 rcu_read_unlock();
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800249 return handler;
250}
251
252static inline void mmu_notifier_page(struct mmu_notifier *mn,
253 struct mm_struct *mm, unsigned long addr)
254{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700255 mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800256}
257
258static inline void mmu_notifier_range_start(struct mmu_notifier *mn,
259 struct mm_struct *mm,
260 unsigned long start,
261 unsigned long end)
262{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700263 mmu_notifier_mem_invalidate(mn, mm, start, end);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800264}
265
266static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn,
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700267 struct mm_struct *mm,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800268 unsigned long start, unsigned long end)
269{
270 struct mmu_rb_handler *handler =
271 container_of(mn, struct mmu_rb_handler, mn);
272 struct rb_root *root = handler->root;
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700273 struct mmu_rb_node *node, *ptr = NULL;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800274 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800275
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800276 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700277 for (node = __mmu_int_rb_iter_first(root, start, end - 1);
278 node; node = ptr) {
279 /* Guard against node removal. */
280 ptr = __mmu_int_rb_iter_next(node, start, end - 1);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800281 hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u",
282 node->addr, node->len);
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700283 if (handler->ops->invalidate(root, node)) {
Mitko Haralanove88c9272016-04-12 10:46:53 -0700284 __mmu_int_rb_remove(node, root);
Dean Luickc0946642016-07-28 12:27:30 -0400285 handler->ops->remove(root, node, mm);
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700286 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800287 }
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800288 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800289}