xref: /OK3568_Linux_fs/kernel/drivers/infiniband/hw/hfi1/mmu_rb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright(c) 2020 Cornelis Networks, Inc.
3  * Copyright(c) 2016 - 2017 Intel Corporation.
4  *
5  * This file is provided under a dual BSD/GPLv2 license.  When using or
6  * redistributing this file, you may do so under either license.
7  *
8  * GPL LICENSE SUMMARY
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * BSD LICENSE
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  *
25  *  - Redistributions of source code must retain the above copyright
26  *    notice, this list of conditions and the following disclaimer.
27  *  - Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in
29  *    the documentation and/or other materials provided with the
30  *    distribution.
31  *  - Neither the name of Intel Corporation nor the names of its
32  *    contributors may be used to endorse or promote products derived
33  *    from this software without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  */
48 #include <linux/list.h>
49 #include <linux/rculist.h>
50 #include <linux/mmu_notifier.h>
51 #include <linux/interval_tree_generic.h>
52 #include <linux/sched/mm.h>
53 
54 #include "mmu_rb.h"
55 #include "trace.h"
56 
57 static unsigned long mmu_node_start(struct mmu_rb_node *);
58 static unsigned long mmu_node_last(struct mmu_rb_node *);
59 static int mmu_notifier_range_start(struct mmu_notifier *,
60 		const struct mmu_notifier_range *);
61 static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *,
62 					   unsigned long, unsigned long);
63 static void do_remove(struct mmu_rb_handler *handler,
64 		      struct list_head *del_list);
65 static void handle_remove(struct work_struct *work);
66 
67 static const struct mmu_notifier_ops mn_opts = {
68 	.invalidate_range_start = mmu_notifier_range_start,
69 };
70 
71 INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
72 		     mmu_node_start, mmu_node_last, static, __mmu_int_rb);
73 
mmu_node_start(struct mmu_rb_node * node)74 static unsigned long mmu_node_start(struct mmu_rb_node *node)
75 {
76 	return node->addr & PAGE_MASK;
77 }
78 
mmu_node_last(struct mmu_rb_node * node)79 static unsigned long mmu_node_last(struct mmu_rb_node *node)
80 {
81 	return PAGE_ALIGN(node->addr + node->len) - 1;
82 }
83 
hfi1_mmu_rb_register(void * ops_arg,struct mmu_rb_ops * ops,struct workqueue_struct * wq,struct mmu_rb_handler ** handler)84 int hfi1_mmu_rb_register(void *ops_arg,
85 			 struct mmu_rb_ops *ops,
86 			 struct workqueue_struct *wq,
87 			 struct mmu_rb_handler **handler)
88 {
89 	struct mmu_rb_handler *h;
90 	int ret;
91 
92 	h = kzalloc(sizeof(*h), GFP_KERNEL);
93 	if (!h)
94 		return -ENOMEM;
95 
96 	h->root = RB_ROOT_CACHED;
97 	h->ops = ops;
98 	h->ops_arg = ops_arg;
99 	INIT_HLIST_NODE(&h->mn.hlist);
100 	spin_lock_init(&h->lock);
101 	h->mn.ops = &mn_opts;
102 	INIT_WORK(&h->del_work, handle_remove);
103 	INIT_LIST_HEAD(&h->del_list);
104 	INIT_LIST_HEAD(&h->lru_list);
105 	h->wq = wq;
106 
107 	ret = mmu_notifier_register(&h->mn, current->mm);
108 	if (ret) {
109 		kfree(h);
110 		return ret;
111 	}
112 
113 	*handler = h;
114 	return 0;
115 }
116 
hfi1_mmu_rb_unregister(struct mmu_rb_handler * handler)117 void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler)
118 {
119 	struct mmu_rb_node *rbnode;
120 	struct rb_node *node;
121 	unsigned long flags;
122 	struct list_head del_list;
123 
124 	/* Prevent freeing of mm until we are completely finished. */
125 	mmgrab(handler->mn.mm);
126 
127 	/* Unregister first so we don't get any more notifications. */
128 	mmu_notifier_unregister(&handler->mn, handler->mn.mm);
129 
130 	/*
131 	 * Make sure the wq delete handler is finished running.  It will not
132 	 * be triggered once the mmu notifiers are unregistered above.
133 	 */
134 	flush_work(&handler->del_work);
135 
136 	INIT_LIST_HEAD(&del_list);
137 
138 	spin_lock_irqsave(&handler->lock, flags);
139 	while ((node = rb_first_cached(&handler->root))) {
140 		rbnode = rb_entry(node, struct mmu_rb_node, node);
141 		rb_erase_cached(node, &handler->root);
142 		/* move from LRU list to delete list */
143 		list_move(&rbnode->list, &del_list);
144 	}
145 	spin_unlock_irqrestore(&handler->lock, flags);
146 
147 	do_remove(handler, &del_list);
148 
149 	/* Now the mm may be freed. */
150 	mmdrop(handler->mn.mm);
151 
152 	kfree(handler);
153 }
154 
hfi1_mmu_rb_insert(struct mmu_rb_handler * handler,struct mmu_rb_node * mnode)155 int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
156 		       struct mmu_rb_node *mnode)
157 {
158 	struct mmu_rb_node *node;
159 	unsigned long flags;
160 	int ret = 0;
161 
162 	trace_hfi1_mmu_rb_insert(mnode->addr, mnode->len);
163 
164 	if (current->mm != handler->mn.mm)
165 		return -EPERM;
166 
167 	spin_lock_irqsave(&handler->lock, flags);
168 	node = __mmu_rb_search(handler, mnode->addr, mnode->len);
169 	if (node) {
170 		ret = -EINVAL;
171 		goto unlock;
172 	}
173 	__mmu_int_rb_insert(mnode, &handler->root);
174 	list_add(&mnode->list, &handler->lru_list);
175 
176 	ret = handler->ops->insert(handler->ops_arg, mnode);
177 	if (ret) {
178 		__mmu_int_rb_remove(mnode, &handler->root);
179 		list_del(&mnode->list); /* remove from LRU list */
180 	}
181 	mnode->handler = handler;
182 unlock:
183 	spin_unlock_irqrestore(&handler->lock, flags);
184 	return ret;
185 }
186 
187 /* Caller must hold handler lock */
__mmu_rb_search(struct mmu_rb_handler * handler,unsigned long addr,unsigned long len)188 static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
189 					   unsigned long addr,
190 					   unsigned long len)
191 {
192 	struct mmu_rb_node *node = NULL;
193 
194 	trace_hfi1_mmu_rb_search(addr, len);
195 	if (!handler->ops->filter) {
196 		node = __mmu_int_rb_iter_first(&handler->root, addr,
197 					       (addr + len) - 1);
198 	} else {
199 		for (node = __mmu_int_rb_iter_first(&handler->root, addr,
200 						    (addr + len) - 1);
201 		     node;
202 		     node = __mmu_int_rb_iter_next(node, addr,
203 						   (addr + len) - 1)) {
204 			if (handler->ops->filter(node, addr, len))
205 				return node;
206 		}
207 	}
208 	return node;
209 }
210 
hfi1_mmu_rb_remove_unless_exact(struct mmu_rb_handler * handler,unsigned long addr,unsigned long len,struct mmu_rb_node ** rb_node)211 bool hfi1_mmu_rb_remove_unless_exact(struct mmu_rb_handler *handler,
212 				     unsigned long addr, unsigned long len,
213 				     struct mmu_rb_node **rb_node)
214 {
215 	struct mmu_rb_node *node;
216 	unsigned long flags;
217 	bool ret = false;
218 
219 	if (current->mm != handler->mn.mm)
220 		return ret;
221 
222 	spin_lock_irqsave(&handler->lock, flags);
223 	node = __mmu_rb_search(handler, addr, len);
224 	if (node) {
225 		if (node->addr == addr && node->len == len)
226 			goto unlock;
227 		__mmu_int_rb_remove(node, &handler->root);
228 		list_del(&node->list); /* remove from LRU list */
229 		ret = true;
230 	}
231 unlock:
232 	spin_unlock_irqrestore(&handler->lock, flags);
233 	*rb_node = node;
234 	return ret;
235 }
236 
hfi1_mmu_rb_evict(struct mmu_rb_handler * handler,void * evict_arg)237 void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg)
238 {
239 	struct mmu_rb_node *rbnode, *ptr;
240 	struct list_head del_list;
241 	unsigned long flags;
242 	bool stop = false;
243 
244 	if (current->mm != handler->mn.mm)
245 		return;
246 
247 	INIT_LIST_HEAD(&del_list);
248 
249 	spin_lock_irqsave(&handler->lock, flags);
250 	list_for_each_entry_safe_reverse(rbnode, ptr, &handler->lru_list,
251 					 list) {
252 		if (handler->ops->evict(handler->ops_arg, rbnode, evict_arg,
253 					&stop)) {
254 			__mmu_int_rb_remove(rbnode, &handler->root);
255 			/* move from LRU list to delete list */
256 			list_move(&rbnode->list, &del_list);
257 		}
258 		if (stop)
259 			break;
260 	}
261 	spin_unlock_irqrestore(&handler->lock, flags);
262 
263 	while (!list_empty(&del_list)) {
264 		rbnode = list_first_entry(&del_list, struct mmu_rb_node, list);
265 		list_del(&rbnode->list);
266 		handler->ops->remove(handler->ops_arg, rbnode);
267 	}
268 }
269 
270 /*
271  * It is up to the caller to ensure that this function does not race with the
272  * mmu invalidate notifier which may be calling the users remove callback on
273  * 'node'.
274  */
hfi1_mmu_rb_remove(struct mmu_rb_handler * handler,struct mmu_rb_node * node)275 void hfi1_mmu_rb_remove(struct mmu_rb_handler *handler,
276 			struct mmu_rb_node *node)
277 {
278 	unsigned long flags;
279 
280 	if (current->mm != handler->mn.mm)
281 		return;
282 
283 	/* Validity of handler and node pointers has been checked by caller. */
284 	trace_hfi1_mmu_rb_remove(node->addr, node->len);
285 	spin_lock_irqsave(&handler->lock, flags);
286 	__mmu_int_rb_remove(node, &handler->root);
287 	list_del(&node->list); /* remove from LRU list */
288 	spin_unlock_irqrestore(&handler->lock, flags);
289 
290 	handler->ops->remove(handler->ops_arg, node);
291 }
292 
mmu_notifier_range_start(struct mmu_notifier * mn,const struct mmu_notifier_range * range)293 static int mmu_notifier_range_start(struct mmu_notifier *mn,
294 		const struct mmu_notifier_range *range)
295 {
296 	struct mmu_rb_handler *handler =
297 		container_of(mn, struct mmu_rb_handler, mn);
298 	struct rb_root_cached *root = &handler->root;
299 	struct mmu_rb_node *node, *ptr = NULL;
300 	unsigned long flags;
301 	bool added = false;
302 
303 	spin_lock_irqsave(&handler->lock, flags);
304 	for (node = __mmu_int_rb_iter_first(root, range->start, range->end-1);
305 	     node; node = ptr) {
306 		/* Guard against node removal. */
307 		ptr = __mmu_int_rb_iter_next(node, range->start,
308 					     range->end - 1);
309 		trace_hfi1_mmu_mem_invalidate(node->addr, node->len);
310 		if (handler->ops->invalidate(handler->ops_arg, node)) {
311 			__mmu_int_rb_remove(node, root);
312 			/* move from LRU list to delete list */
313 			list_move(&node->list, &handler->del_list);
314 			added = true;
315 		}
316 	}
317 	spin_unlock_irqrestore(&handler->lock, flags);
318 
319 	if (added)
320 		queue_work(handler->wq, &handler->del_work);
321 
322 	return 0;
323 }
324 
325 /*
326  * Call the remove function for the given handler and the list.  This
327  * is expected to be called with a delete list extracted from handler.
328  * The caller should not be holding the handler lock.
329  */
do_remove(struct mmu_rb_handler * handler,struct list_head * del_list)330 static void do_remove(struct mmu_rb_handler *handler,
331 		      struct list_head *del_list)
332 {
333 	struct mmu_rb_node *node;
334 
335 	while (!list_empty(del_list)) {
336 		node = list_first_entry(del_list, struct mmu_rb_node, list);
337 		list_del(&node->list);
338 		handler->ops->remove(handler->ops_arg, node);
339 	}
340 }
341 
342 /*
343  * Work queue function to remove all nodes that have been queued up to
344  * be removed.  The key feature is that mm->mmap_lock is not being held
345  * and the remove callback can sleep while taking it, if needed.
346  */
handle_remove(struct work_struct * work)347 static void handle_remove(struct work_struct *work)
348 {
349 	struct mmu_rb_handler *handler = container_of(work,
350 						struct mmu_rb_handler,
351 						del_work);
352 	struct list_head del_list;
353 	unsigned long flags;
354 
355 	/* remove anything that is queued to get removed */
356 	spin_lock_irqsave(&handler->lock, flags);
357 	list_replace_init(&handler->del_list, &del_list);
358 	spin_unlock_irqrestore(&handler->lock, flags);
359 
360 	do_remove(handler, &del_list);
361 }
362