blob: 97f2d3680751a20be72cd084bcdd4a717d61ec98 [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;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080062};
63
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080064static unsigned long mmu_node_start(struct mmu_rb_node *);
65static unsigned long mmu_node_last(struct mmu_rb_node *);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080066static inline void mmu_notifier_page(struct mmu_notifier *, struct mm_struct *,
67 unsigned long);
68static inline void mmu_notifier_range_start(struct mmu_notifier *,
69 struct mm_struct *,
70 unsigned long, unsigned long);
71static void mmu_notifier_mem_invalidate(struct mmu_notifier *,
Mitko Haralanovf19bd642016-04-12 10:45:57 -070072 struct mm_struct *,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080073 unsigned long, unsigned long);
74static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *,
75 unsigned long, unsigned long);
76
77static struct mmu_notifier_ops mn_opts = {
78 .invalidate_page = mmu_notifier_page,
79 .invalidate_range_start = mmu_notifier_range_start,
80};
81
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080082INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
83 mmu_node_start, mmu_node_last, static, __mmu_int_rb);
84
85static unsigned long mmu_node_start(struct mmu_rb_node *node)
86{
87 return node->addr & PAGE_MASK;
88}
89
90static unsigned long mmu_node_last(struct mmu_rb_node *node)
91{
Mitko Haralanovde790932016-04-12 10:46:41 -070092 return PAGE_ALIGN(node->addr + node->len) - 1;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -080093}
94
Dean Luicke0b09ac2016-07-28 15:21:20 -040095int hfi1_mmu_rb_register(void *ops_arg, struct mm_struct *mm,
96 struct mmu_rb_ops *ops,
97 struct mmu_rb_handler **handler)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -080098{
99 struct mmu_rb_handler *handlr;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400100 int ret;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800101
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800102 handlr = kmalloc(sizeof(*handlr), GFP_KERNEL);
103 if (!handlr)
104 return -ENOMEM;
105
Dean Luicke0b09ac2016-07-28 15:21:20 -0400106 handlr->root = RB_ROOT;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800107 handlr->ops = ops;
Dean Luicke0b09ac2016-07-28 15:21:20 -0400108 handlr->ops_arg = ops_arg;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800109 INIT_HLIST_NODE(&handlr->mn.hlist);
110 spin_lock_init(&handlr->lock);
111 handlr->mn.ops = &mn_opts;
Ira Weiny3faa3d92016-07-28 15:21:19 -0400112 handlr->mm = mm;
113
114 ret = mmu_notifier_register(&handlr->mn, handlr->mm);
115 if (ret) {
116 kfree(handlr);
117 return ret;
118 }
119
Dean Luicke0b09ac2016-07-28 15:21:20 -0400120 *handler = handlr;
121 return 0;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800122}
123
Dean Luicke0b09ac2016-07-28 15:21:20 -0400124void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800125{
Dean Luick20a42d02016-07-28 12:27:36 -0400126 struct mmu_rb_node *rbnode;
127 struct rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800128 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800129
Mitko Haralanov782f6692016-04-12 10:46:35 -0700130 /* Unregister first so we don't get any more notifications. */
Ira Weiny3faa3d92016-07-28 15:21:19 -0400131 mmu_notifier_unregister(&handler->mn, handler->mm);
Mitko Haralanov782f6692016-04-12 10:46:35 -0700132
Mitko Haralanov782f6692016-04-12 10:46:35 -0700133 spin_lock_irqsave(&handler->lock, flags);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400134 while ((node = rb_first(&handler->root))) {
Dean Luick20a42d02016-07-28 12:27:36 -0400135 rbnode = rb_entry(node, struct mmu_rb_node, node);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400136 rb_erase(node, &handler->root);
137 handler->ops->remove(handler->ops_arg, rbnode,
138 NULL);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800139 }
Mitko Haralanov782f6692016-04-12 10:46:35 -0700140 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800141
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800142 kfree(handler);
143}
144
Dean Luicke0b09ac2016-07-28 15:21:20 -0400145int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
146 struct mmu_rb_node *mnode)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800147{
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800148 struct mmu_rb_node *node;
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800149 unsigned long flags;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800150 int ret = 0;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800151
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800152 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800153 hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr,
154 mnode->len);
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800155 node = __mmu_rb_search(handler, mnode->addr, mnode->len);
156 if (node) {
157 ret = -EINVAL;
158 goto unlock;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800159 }
Dean Luicke0b09ac2016-07-28 15:21:20 -0400160 __mmu_int_rb_insert(mnode, &handler->root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800161
Dean Luicke0b09ac2016-07-28 15:21:20 -0400162 ret = handler->ops->insert(handler->ops_arg, mnode);
Dean Luickc0946642016-07-28 12:27:30 -0400163 if (ret)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400164 __mmu_int_rb_remove(mnode, &handler->root);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800165unlock:
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800166 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800167 return ret;
168}
169
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700170/* Caller must hold handler lock */
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800171static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
172 unsigned long addr,
173 unsigned long len)
174{
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800175 struct mmu_rb_node *node = NULL;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800176
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800177 hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len);
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800178 if (!handler->ops->filter) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400179 node = __mmu_int_rb_iter_first(&handler->root, addr,
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800180 (addr + len) - 1);
181 } else {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400182 for (node = __mmu_int_rb_iter_first(&handler->root, addr,
Mitko Haralanov0f310a002016-03-08 11:15:10 -0800183 (addr + len) - 1);
184 node;
185 node = __mmu_int_rb_iter_next(node, addr,
186 (addr + len) - 1)) {
187 if (handler->ops->filter(node, addr, len))
188 return node;
189 }
190 }
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800191 return node;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800192}
193
Dean Luicke0b09ac2016-07-28 15:21:20 -0400194struct mmu_rb_node *hfi1_mmu_rb_extract(struct mmu_rb_handler *handler,
Mitko Haralanovf53af852016-04-12 10:46:47 -0700195 unsigned long addr, unsigned long len)
196{
Mitko Haralanovf53af852016-04-12 10:46:47 -0700197 struct mmu_rb_node *node;
198 unsigned long flags;
199
Mitko Haralanovf53af852016-04-12 10:46:47 -0700200 spin_lock_irqsave(&handler->lock, flags);
201 node = __mmu_rb_search(handler, addr, len);
202 if (node)
Dean Luicke0b09ac2016-07-28 15:21:20 -0400203 __mmu_int_rb_remove(node, &handler->root);
Mitko Haralanovf53af852016-04-12 10:46:47 -0700204 spin_unlock_irqrestore(&handler->lock, flags);
205
206 return node;
207}
208
Dean Luick10345992016-07-28 15:21:22 -0400209void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg)
210{
211 struct mmu_rb_node *rbnode;
212 struct rb_node *node, *next;
213 struct list_head del_list;
214 unsigned long flags;
215 bool stop = false;
216
217 INIT_LIST_HEAD(&del_list);
218
219 spin_lock_irqsave(&handler->lock, flags);
220 for (node = rb_first(&handler->root); node; node = next) {
221 next = rb_next(node);
222 rbnode = rb_entry(node, struct mmu_rb_node, node);
223 if (handler->ops->evict(handler->ops_arg, rbnode, evict_arg,
224 &stop)) {
225 __mmu_int_rb_remove(rbnode, &handler->root);
226 list_add(&rbnode->list, &del_list);
227 }
228 if (stop)
229 break;
230 }
231 spin_unlock_irqrestore(&handler->lock, flags);
232
233 down_write(&handler->mm->mmap_sem);
234 while (!list_empty(&del_list)) {
235 rbnode = list_first_entry(&del_list, struct mmu_rb_node, list);
236 list_del(&rbnode->list);
237 handler->ops->remove(handler->ops_arg, rbnode,
238 handler->mm);
239 }
240 up_write(&handler->mm->mmap_sem);
241}
242
Dean Luicke0b09ac2016-07-28 15:21:20 -0400243void hfi1_mmu_rb_remove(struct mmu_rb_handler *handler,
244 struct mmu_rb_node *node)
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800245{
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400246 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800247
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400248 /* Validity of handler and node pointers has been checked by caller. */
249 hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr,
250 node->len);
251 spin_lock_irqsave(&handler->lock, flags);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400252 __mmu_int_rb_remove(node, &handler->root);
Ira Weiny3c1091aa2016-07-28 12:27:31 -0400253 spin_unlock_irqrestore(&handler->lock, flags);
254
Dean Luicke0b09ac2016-07-28 15:21:20 -0400255 handler->ops->remove(handler->ops_arg, node, NULL);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800256}
257
258static inline void mmu_notifier_page(struct mmu_notifier *mn,
259 struct mm_struct *mm, unsigned long addr)
260{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700261 mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800262}
263
264static inline void mmu_notifier_range_start(struct mmu_notifier *mn,
265 struct mm_struct *mm,
266 unsigned long start,
267 unsigned long end)
268{
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700269 mmu_notifier_mem_invalidate(mn, mm, start, end);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800270}
271
272static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn,
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700273 struct mm_struct *mm,
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800274 unsigned long start, unsigned long end)
275{
276 struct mmu_rb_handler *handler =
277 container_of(mn, struct mmu_rb_handler, mn);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400278 struct rb_root *root = &handler->root;
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700279 struct mmu_rb_node *node, *ptr = NULL;
Mitko Haralanovdf5a00f82016-03-08 11:14:53 -0800280 unsigned long flags;
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800281
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800282 spin_lock_irqsave(&handler->lock, flags);
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700283 for (node = __mmu_int_rb_iter_first(root, start, end - 1);
284 node; node = ptr) {
285 /* Guard against node removal. */
286 ptr = __mmu_int_rb_iter_next(node, start, end - 1);
Mitko Haralanov353b71c2016-03-08 11:14:59 -0800287 hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u",
288 node->addr, node->len);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400289 if (handler->ops->invalidate(handler->ops_arg, node)) {
Mitko Haralanove88c9272016-04-12 10:46:53 -0700290 __mmu_int_rb_remove(node, root);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400291 handler->ops->remove(handler->ops_arg, node, mm);
Mitko Haralanovde82bdf2016-04-12 10:46:03 -0700292 }
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800293 }
Mitko Haralanovc81e1f62016-03-08 11:14:25 -0800294 spin_unlock_irqrestore(&handler->lock, flags);
Mitko Haralanov06e0ffa2016-03-08 11:14:20 -0800295}