2 * Copyright(c) 2016 Intel Corporation.
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
47 #include <linux/list.h>
48 #include <linux/mmu_notifier.h>
49 #include <linux/interval_tree_generic.h>
54 struct mmu_rb_handler {
55 struct list_head list;
56 struct mmu_notifier mn;
58 spinlock_t lock; /* protect the RB tree */
59 struct mmu_rb_ops *ops;
62 static LIST_HEAD(mmu_rb_handlers);
63 static DEFINE_SPINLOCK(mmu_rb_lock); /* protect mmu_rb_handlers list */
65 static unsigned long mmu_node_start(struct mmu_rb_node *);
66 static unsigned long mmu_node_last(struct mmu_rb_node *);
67 static struct mmu_rb_handler *find_mmu_handler(struct rb_root *);
68 static inline void mmu_notifier_page(struct mmu_notifier *, struct mm_struct *,
70 static inline void mmu_notifier_range_start(struct mmu_notifier *,
72 unsigned long, unsigned long);
73 static void mmu_notifier_mem_invalidate(struct mmu_notifier *,
75 unsigned long, unsigned long);
76 static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *,
77 unsigned long, unsigned long);
79 static struct mmu_notifier_ops mn_opts = {
80 .invalidate_page = mmu_notifier_page,
81 .invalidate_range_start = mmu_notifier_range_start,
84 INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
85 mmu_node_start, mmu_node_last, static, __mmu_int_rb);
87 static unsigned long mmu_node_start(struct mmu_rb_node *node)
89 return node->addr & PAGE_MASK;
92 static unsigned long mmu_node_last(struct mmu_rb_node *node)
94 return PAGE_ALIGN(node->addr + node->len) - 1;
97 int hfi1_mmu_rb_register(struct rb_root *root, struct mmu_rb_ops *ops)
99 struct mmu_rb_handler *handlr;
102 if (!ops->invalidate)
105 handlr = kmalloc(sizeof(*handlr), GFP_KERNEL);
111 INIT_HLIST_NODE(&handlr->mn.hlist);
112 spin_lock_init(&handlr->lock);
113 handlr->mn.ops = &mn_opts;
114 spin_lock_irqsave(&mmu_rb_lock, flags);
115 list_add_tail(&handlr->list, &mmu_rb_handlers);
116 spin_unlock_irqrestore(&mmu_rb_lock, flags);
118 return mmu_notifier_register(&handlr->mn, current->mm);
121 void hfi1_mmu_rb_unregister(struct rb_root *root)
123 struct mmu_rb_handler *handler = find_mmu_handler(root);
129 /* Unregister first so we don't get any more notifications. */
131 mmu_notifier_unregister(&handler->mn, current->mm);
133 spin_lock_irqsave(&mmu_rb_lock, flags);
134 list_del(&handler->list);
135 spin_unlock_irqrestore(&mmu_rb_lock, flags);
137 spin_lock_irqsave(&handler->lock, flags);
138 if (!RB_EMPTY_ROOT(root)) {
139 struct rb_node *node;
140 struct mmu_rb_node *rbnode;
142 while ((node = rb_first(root))) {
143 rbnode = rb_entry(node, struct mmu_rb_node, node);
144 rb_erase(node, root);
145 if (handler->ops->remove)
146 handler->ops->remove(root, rbnode, NULL);
149 spin_unlock_irqrestore(&handler->lock, flags);
154 int hfi1_mmu_rb_insert(struct rb_root *root, struct mmu_rb_node *mnode)
156 struct mmu_rb_handler *handler = find_mmu_handler(root);
157 struct mmu_rb_node *node;
164 spin_lock_irqsave(&handler->lock, flags);
165 hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr,
167 node = __mmu_rb_search(handler, mnode->addr, mnode->len);
172 __mmu_int_rb_insert(mnode, root);
174 if (handler->ops->insert) {
175 ret = handler->ops->insert(root, mnode);
177 __mmu_int_rb_remove(mnode, root);
180 spin_unlock_irqrestore(&handler->lock, flags);
184 /* Caller must hold handler lock */
185 static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
189 struct mmu_rb_node *node = NULL;
191 hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len);
192 if (!handler->ops->filter) {
193 node = __mmu_int_rb_iter_first(handler->root, addr,
196 for (node = __mmu_int_rb_iter_first(handler->root, addr,
199 node = __mmu_int_rb_iter_next(node, addr,
201 if (handler->ops->filter(node, addr, len))
208 /* Caller must *not* hold handler lock. */
209 static void __mmu_rb_remove(struct mmu_rb_handler *handler,
210 struct mmu_rb_node *node, struct mm_struct *mm)
214 /* Validity of handler and node pointers has been checked by caller. */
215 hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr,
217 spin_lock_irqsave(&handler->lock, flags);
218 __mmu_int_rb_remove(node, handler->root);
219 spin_unlock_irqrestore(&handler->lock, flags);
221 if (handler->ops->remove)
222 handler->ops->remove(handler->root, node, mm);
225 struct mmu_rb_node *hfi1_mmu_rb_search(struct rb_root *root, unsigned long addr,
228 struct mmu_rb_handler *handler = find_mmu_handler(root);
229 struct mmu_rb_node *node;
233 return ERR_PTR(-EINVAL);
235 spin_lock_irqsave(&handler->lock, flags);
236 node = __mmu_rb_search(handler, addr, len);
237 spin_unlock_irqrestore(&handler->lock, flags);
242 struct mmu_rb_node *hfi1_mmu_rb_extract(struct rb_root *root,
243 unsigned long addr, unsigned long len)
245 struct mmu_rb_handler *handler = find_mmu_handler(root);
246 struct mmu_rb_node *node;
250 return ERR_PTR(-EINVAL);
252 spin_lock_irqsave(&handler->lock, flags);
253 node = __mmu_rb_search(handler, addr, len);
255 __mmu_int_rb_remove(node, handler->root);
256 spin_unlock_irqrestore(&handler->lock, flags);
261 void hfi1_mmu_rb_remove(struct rb_root *root, struct mmu_rb_node *node)
263 struct mmu_rb_handler *handler = find_mmu_handler(root);
265 if (!handler || !node)
268 __mmu_rb_remove(handler, node, NULL);
271 static struct mmu_rb_handler *find_mmu_handler(struct rb_root *root)
273 struct mmu_rb_handler *handler;
276 spin_lock_irqsave(&mmu_rb_lock, flags);
277 list_for_each_entry(handler, &mmu_rb_handlers, list) {
278 if (handler->root == root)
283 spin_unlock_irqrestore(&mmu_rb_lock, flags);
287 static inline void mmu_notifier_page(struct mmu_notifier *mn,
288 struct mm_struct *mm, unsigned long addr)
290 mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE);
293 static inline void mmu_notifier_range_start(struct mmu_notifier *mn,
294 struct mm_struct *mm,
298 mmu_notifier_mem_invalidate(mn, mm, start, end);
301 static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn,
302 struct mm_struct *mm,
303 unsigned long start, unsigned long end)
305 struct mmu_rb_handler *handler =
306 container_of(mn, struct mmu_rb_handler, mn);
307 struct rb_root *root = handler->root;
308 struct mmu_rb_node *node, *ptr = NULL;
311 spin_lock_irqsave(&handler->lock, flags);
312 for (node = __mmu_int_rb_iter_first(root, start, end - 1);
314 /* Guard against node removal. */
315 ptr = __mmu_int_rb_iter_next(node, start, end - 1);
316 hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u",
317 node->addr, node->len);
318 if (handler->ops->invalidate(root, node)) {
319 __mmu_int_rb_remove(node, root);
320 if (handler->ops->remove)
321 handler->ops->remove(root, node, mm);
324 spin_unlock_irqrestore(&handler->lock, flags);