xref: /openbmc/linux/drivers/infiniband/hw/hfi1/mmu_rb.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1145eba1aSCai Huoqing // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
2f48ad614SDennis Dalessandro /*
33d2a9d64SDennis Dalessandro  * Copyright(c) 2020 Cornelis Networks, Inc.
434ab4de7SMichael J. Ruhl  * Copyright(c) 2016 - 2017 Intel Corporation.
5f48ad614SDennis Dalessandro  */
6145eba1aSCai Huoqing 
7f48ad614SDennis Dalessandro #include <linux/list.h>
8f48ad614SDennis Dalessandro #include <linux/rculist.h>
9f48ad614SDennis Dalessandro #include <linux/mmu_notifier.h>
10f48ad614SDennis Dalessandro #include <linux/interval_tree_generic.h>
113d2a9d64SDennis Dalessandro #include <linux/sched/mm.h>
12f48ad614SDennis Dalessandro 
13f48ad614SDennis Dalessandro #include "mmu_rb.h"
14f48ad614SDennis Dalessandro #include "trace.h"
15f48ad614SDennis Dalessandro 
16f48ad614SDennis Dalessandro static unsigned long mmu_node_start(struct mmu_rb_node *);
17f48ad614SDennis Dalessandro static unsigned long mmu_node_last(struct mmu_rb_node *);
1893065ac7SMichal Hocko static int mmu_notifier_range_start(struct mmu_notifier *,
195d6527a7SJérôme Glisse 		const struct mmu_notifier_range *);
20f48ad614SDennis Dalessandro static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *,
21f48ad614SDennis Dalessandro 					   unsigned long, unsigned long);
22b85ced91SDean Luick static void release_immediate(struct kref *refcount);
23b85ced91SDean Luick static void handle_remove(struct work_struct *work);
24b85ced91SDean Luick 
25f48ad614SDennis Dalessandro static const struct mmu_notifier_ops mn_opts = {
260fc859a6SBhumika Goyal 	.invalidate_range_start = mmu_notifier_range_start,
27f48ad614SDennis Dalessandro };
28f48ad614SDennis Dalessandro 
29f48ad614SDennis Dalessandro INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last,
30f48ad614SDennis Dalessandro 		     mmu_node_start, mmu_node_last, static, __mmu_int_rb);
31f48ad614SDennis Dalessandro 
mmu_node_start(struct mmu_rb_node * node)32f48ad614SDennis Dalessandro static unsigned long mmu_node_start(struct mmu_rb_node *node)
33f48ad614SDennis Dalessandro {
34f48ad614SDennis Dalessandro 	return node->addr & PAGE_MASK;
35f48ad614SDennis Dalessandro }
36f48ad614SDennis Dalessandro 
mmu_node_last(struct mmu_rb_node * node)37f48ad614SDennis Dalessandro static unsigned long mmu_node_last(struct mmu_rb_node *node)
38f48ad614SDennis Dalessandro {
39f48ad614SDennis Dalessandro 	return PAGE_ALIGN(node->addr + node->len) - 1;
40f48ad614SDennis Dalessandro }
41f48ad614SDennis Dalessandro 
hfi1_mmu_rb_register(void * ops_arg,struct mmu_rb_ops * ops,struct workqueue_struct * wq,struct mmu_rb_handler ** handler)42f48ad614SDennis Dalessandro int hfi1_mmu_rb_register(void *ops_arg,
433d2a9d64SDennis Dalessandro 			 struct mmu_rb_ops *ops,
44e0b09ac5SDean Luick 			 struct workqueue_struct *wq,
45b85ced91SDean Luick 			 struct mmu_rb_handler **handler)
46e0b09ac5SDean Luick {
47f48ad614SDennis Dalessandro 	struct mmu_rb_handler *h;
483d2a9d64SDennis Dalessandro 	void *free_ptr;
49*866694afSPatrick Kelsey 	int ret;
503faa3d9aSIra Weiny 
51f48ad614SDennis Dalessandro 	free_ptr = kzalloc(sizeof(*h) + cache_line_size() - 1, GFP_KERNEL);
52*866694afSPatrick Kelsey 	if (!free_ptr)
53*866694afSPatrick Kelsey 		return -ENOMEM;
54f48ad614SDennis Dalessandro 
55f48ad614SDennis Dalessandro 	h = PTR_ALIGN(free_ptr, cache_line_size());
56*866694afSPatrick Kelsey 	h->root = RB_ROOT_CACHED;
573d2a9d64SDennis Dalessandro 	h->ops = ops;
583d2a9d64SDennis Dalessandro 	h->ops_arg = ops_arg;
593d2a9d64SDennis Dalessandro 	INIT_HLIST_NODE(&h->mn.hlist);
603d2a9d64SDennis Dalessandro 	spin_lock_init(&h->lock);
613d2a9d64SDennis Dalessandro 	h->mn.ops = &mn_opts;
623d2a9d64SDennis Dalessandro 	INIT_WORK(&h->del_work, handle_remove);
633d2a9d64SDennis Dalessandro 	INIT_LIST_HEAD(&h->del_list);
643d2a9d64SDennis Dalessandro 	INIT_LIST_HEAD(&h->lru_list);
653d2a9d64SDennis Dalessandro 	h->wq = wq;
663d2a9d64SDennis Dalessandro 	h->free_ptr = free_ptr;
67*866694afSPatrick Kelsey 
683faa3d9aSIra Weiny 	ret = mmu_notifier_register(&h->mn, current->mm);
693d2a9d64SDennis Dalessandro 	if (ret) {
703faa3d9aSIra Weiny 		kfree(free_ptr);
71*866694afSPatrick Kelsey 		return ret;
723faa3d9aSIra Weiny 	}
733faa3d9aSIra Weiny 
743faa3d9aSIra Weiny 	*handler = h;
753d2a9d64SDennis Dalessandro 	return 0;
76e0b09ac5SDean Luick }
77f48ad614SDennis Dalessandro 
hfi1_mmu_rb_unregister(struct mmu_rb_handler * handler)78f48ad614SDennis Dalessandro void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler)
79e0b09ac5SDean Luick {
80f48ad614SDennis Dalessandro 	struct mmu_rb_node *rbnode;
8120a42d08SDean Luick 	struct rb_node *node;
8220a42d08SDean Luick 	unsigned long flags;
83f48ad614SDennis Dalessandro 	struct list_head del_list;
84b85ced91SDean Luick 
85f48ad614SDennis Dalessandro 	/* Prevent freeing of mm until we are completely finished. */
862bbac98dSDouglas Miller 	mmgrab(handler->mn.mm);
872bbac98dSDouglas Miller 
882bbac98dSDouglas Miller 	/* Unregister first so we don't get any more notifications. */
89f48ad614SDennis Dalessandro 	mmu_notifier_unregister(&handler->mn, handler->mn.mm);
903d2a9d64SDennis Dalessandro 
91f48ad614SDennis Dalessandro 	/*
92b85ced91SDean Luick 	 * Make sure the wq delete handler is finished running.  It will not
93b85ced91SDean Luick 	 * be triggered once the mmu notifiers are unregistered above.
94b85ced91SDean Luick 	 */
95b85ced91SDean Luick 	flush_work(&handler->del_work);
96b85ced91SDean Luick 
97b85ced91SDean Luick 	INIT_LIST_HEAD(&del_list);
98b85ced91SDean Luick 
99b85ced91SDean Luick 	spin_lock_irqsave(&handler->lock, flags);
100f48ad614SDennis Dalessandro 	while ((node = rb_first_cached(&handler->root))) {
101f808c13fSDavidlohr Bueso 		rbnode = rb_entry(node, struct mmu_rb_node, node);
102f48ad614SDennis Dalessandro 		rb_erase_cached(node, &handler->root);
103f808c13fSDavidlohr Bueso 		/* move from LRU list to delete list */
1040636e9abSDean Luick 		list_move(&rbnode->list, &del_list);
1050636e9abSDean Luick 	}
106f48ad614SDennis Dalessandro 	spin_unlock_irqrestore(&handler->lock, flags);
107f48ad614SDennis Dalessandro 
108f48ad614SDennis Dalessandro 	while (!list_empty(&del_list)) {
109b85ced91SDean Luick 		rbnode = list_first_entry(&del_list, struct mmu_rb_node, list);
110b85ced91SDean Luick 		list_del(&rbnode->list);
1112bbac98dSDouglas Miller 		kref_put(&rbnode->refcount, release_immediate);
1122bbac98dSDouglas Miller 	}
1132bbac98dSDouglas Miller 
114*866694afSPatrick Kelsey 	/* Now the mm may be freed. */
115f48ad614SDennis Dalessandro 	mmdrop(handler->mn.mm);
116f48ad614SDennis Dalessandro 
117e0b09ac5SDean Luick 	kfree(handler->free_ptr);
118e0b09ac5SDean Luick }
119f48ad614SDennis Dalessandro 
hfi1_mmu_rb_insert(struct mmu_rb_handler * handler,struct mmu_rb_node * mnode)120f48ad614SDennis Dalessandro int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler,
121f48ad614SDennis Dalessandro 		       struct mmu_rb_node *mnode)
122f48ad614SDennis Dalessandro {
123f48ad614SDennis Dalessandro 	struct mmu_rb_node *node;
12434ab4de7SMichael J. Ruhl 	unsigned long flags;
1253d2a9d64SDennis Dalessandro 	int ret = 0;
1263d2a9d64SDennis Dalessandro 
1273d2a9d64SDennis Dalessandro 	trace_hfi1_mmu_rb_insert(mnode);
1283d2a9d64SDennis Dalessandro 
129f48ad614SDennis Dalessandro 	if (current->mm != handler->mn.mm)
130f48ad614SDennis Dalessandro 		return -EPERM;
131f48ad614SDennis Dalessandro 
13200cbce5cSPatrick Kelsey 	spin_lock_irqsave(&handler->lock, flags);
133f48ad614SDennis Dalessandro 	node = __mmu_rb_search(handler, mnode->addr, mnode->len);
134f48ad614SDennis Dalessandro 	if (node) {
135e0b09ac5SDean Luick 		ret = -EEXIST;
1369fe8fec5SPatrick Kelsey 		goto unlock;
137f48ad614SDennis Dalessandro 	}
138e0b09ac5SDean Luick 	__mmu_int_rb_insert(mnode, &handler->root);
1390636e9abSDean Luick 	list_add_tail(&mnode->list, &handler->lru_list);
140e0b09ac5SDean Luick 	mnode->handler = handler;
1410636e9abSDean Luick unlock:
1420636e9abSDean Luick 	spin_unlock_irqrestore(&handler->lock, flags);
1433d2a9d64SDennis Dalessandro 	return ret;
144f48ad614SDennis Dalessandro }
145f48ad614SDennis Dalessandro 
146f48ad614SDennis Dalessandro /* Caller must hold handler lock */
hfi1_mmu_rb_get_first(struct mmu_rb_handler * handler,unsigned long addr,unsigned long len)147f48ad614SDennis Dalessandro struct mmu_rb_node *hfi1_mmu_rb_get_first(struct mmu_rb_handler *handler,
148f48ad614SDennis Dalessandro 					  unsigned long addr, unsigned long len)
149f48ad614SDennis Dalessandro {
15000cbce5cSPatrick Kelsey 	struct mmu_rb_node *node;
15100cbce5cSPatrick Kelsey 
15200cbce5cSPatrick Kelsey 	trace_hfi1_mmu_rb_search(addr, len);
15300cbce5cSPatrick Kelsey 	node = __mmu_int_rb_iter_first(&handler->root, addr, (addr + len) - 1);
15400cbce5cSPatrick Kelsey 	if (node)
15500cbce5cSPatrick Kelsey 		list_move_tail(&node->list, &handler->lru_list);
15600cbce5cSPatrick Kelsey 	return node;
15700cbce5cSPatrick Kelsey }
15800cbce5cSPatrick Kelsey 
15900cbce5cSPatrick Kelsey /* Caller must hold handler lock */
__mmu_rb_search(struct mmu_rb_handler * handler,unsigned long addr,unsigned long len)16000cbce5cSPatrick Kelsey static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler,
16100cbce5cSPatrick Kelsey 					   unsigned long addr,
16200cbce5cSPatrick Kelsey 					   unsigned long len)
163f48ad614SDennis Dalessandro {
164f48ad614SDennis Dalessandro 	struct mmu_rb_node *node = NULL;
165f48ad614SDennis Dalessandro 
166f48ad614SDennis Dalessandro 	trace_hfi1_mmu_rb_search(addr, len);
167f48ad614SDennis Dalessandro 	if (!handler->ops->filter) {
168f48ad614SDennis Dalessandro 		node = __mmu_int_rb_iter_first(&handler->root, addr,
16934ab4de7SMichael J. Ruhl 					       (addr + len) - 1);
170f48ad614SDennis Dalessandro 	} else {
171e0b09ac5SDean Luick 		for (node = __mmu_int_rb_iter_first(&handler->root, addr,
172f48ad614SDennis Dalessandro 						    (addr + len) - 1);
173f48ad614SDennis Dalessandro 		     node;
174e0b09ac5SDean Luick 		     node = __mmu_int_rb_iter_next(node, addr,
175f48ad614SDennis Dalessandro 						   (addr + len) - 1)) {
176f48ad614SDennis Dalessandro 			if (handler->ops->filter(node, addr, len))
177f48ad614SDennis Dalessandro 				return node;
178f48ad614SDennis Dalessandro 		}
179f48ad614SDennis Dalessandro 	}
180f48ad614SDennis Dalessandro 	return node;
181f48ad614SDennis Dalessandro }
182f48ad614SDennis Dalessandro 
183f48ad614SDennis Dalessandro /*
184f48ad614SDennis Dalessandro  * Must NOT call while holding mnode->handler->lock.
185f48ad614SDennis Dalessandro  * mnode->handler->ops->remove() may sleep and mnode->handler->lock is a
18610345998SDean Luick  * spinlock.
18710345998SDean Luick  */
release_immediate(struct kref * refcount)1880636e9abSDean Luick static void release_immediate(struct kref *refcount)
18910345998SDean Luick {
19010345998SDean Luick 	struct mmu_rb_node *mnode =
19110345998SDean Luick 		container_of(refcount, struct mmu_rb_node, refcount);
19210345998SDean Luick 	trace_hfi1_mmu_release_node(mnode);
1933d2a9d64SDennis Dalessandro 	mnode->handler->ops->remove(mnode->handler->ops_arg, mnode);
1943d2a9d64SDennis Dalessandro }
1953d2a9d64SDennis Dalessandro 
19610345998SDean Luick /* Caller must hold mnode->handler->lock */
release_nolock(struct kref * refcount)19710345998SDean Luick static void release_nolock(struct kref *refcount)
19810345998SDean Luick {
1999fe8fec5SPatrick Kelsey 	struct mmu_rb_node *mnode =
20010345998SDean Luick 		container_of(refcount, struct mmu_rb_node, refcount);
20110345998SDean Luick 	list_move(&mnode->list, &mnode->handler->del_list);
20210345998SDean Luick 	queue_work(mnode->handler->wq, &mnode->handler->del_work);
2030636e9abSDean Luick }
2040636e9abSDean Luick 
20510345998SDean Luick /*
20610345998SDean Luick  * struct mmu_rb_node->refcount kref_put() callback.
20710345998SDean Luick  * Adds mmu_rb_node to mmu_rb_node->handler->del_list and queues
20810345998SDean Luick  * handler->del_work on handler->wq.
20910345998SDean Luick  * Does not remove mmu_rb_node from handler->lru_list or handler->rb_root.
21010345998SDean Luick  * Acquires mmu_rb_node->handler->lock; do not call while already holding
2119fe8fec5SPatrick Kelsey  * handler->lock.
212082b3532SDean Luick  */
hfi1_mmu_rb_release(struct kref * refcount)21310345998SDean Luick void hfi1_mmu_rb_release(struct kref *refcount)
21410345998SDean Luick {
21510345998SDean Luick 	struct mmu_rb_node *mnode =
21693065ac7SMichal Hocko 		container_of(refcount, struct mmu_rb_node, refcount);
2175d6527a7SJérôme Glisse 	struct mmu_rb_handler *handler = mnode->handler;
218f48ad614SDennis Dalessandro 	unsigned long flags;
219f48ad614SDennis Dalessandro 
220f48ad614SDennis Dalessandro 	spin_lock_irqsave(&handler->lock, flags);
221f808c13fSDavidlohr Bueso 	list_move(&mnode->list, &mnode->handler->del_list);
222f48ad614SDennis Dalessandro 	spin_unlock_irqrestore(&handler->lock, flags);
223f48ad614SDennis Dalessandro 	queue_work(handler->wq, &handler->del_work);
224b85ced91SDean Luick }
225f48ad614SDennis Dalessandro 
hfi1_mmu_rb_evict(struct mmu_rb_handler * handler,void * evict_arg)226f48ad614SDennis Dalessandro void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg)
2275d6527a7SJérôme Glisse {
228f48ad614SDennis Dalessandro 	struct mmu_rb_node *rbnode, *ptr;
229f48ad614SDennis Dalessandro 	struct list_head del_list;
2305d6527a7SJérôme Glisse 	unsigned long flags;
2315d6527a7SJérôme Glisse 	bool stop = false;
23234ab4de7SMichael J. Ruhl 
233e0b09ac5SDean Luick 	if (current->mm != handler->mn.mm)
234f48ad614SDennis Dalessandro 		return;
2350636e9abSDean Luick 
2360636e9abSDean Luick 	INIT_LIST_HEAD(&del_list);
237b85ced91SDean Luick 
238f48ad614SDennis Dalessandro 	spin_lock_irqsave(&handler->lock, flags);
239f48ad614SDennis Dalessandro 	list_for_each_entry_safe(rbnode, ptr, &handler->lru_list, list) {
240f48ad614SDennis Dalessandro 		/* refcount == 1 implies mmu_rb_handler has only rbnode ref */
241b85ced91SDean Luick 		if (kref_read(&rbnode->refcount) > 1)
242b85ced91SDean Luick 			continue;
243b85ced91SDean Luick 
24493065ac7SMichal Hocko 		if (handler->ops->evict(handler->ops_arg, rbnode, evict_arg,
24593065ac7SMichal Hocko 					&stop)) {
246b85ced91SDean Luick 			__mmu_int_rb_remove(rbnode, &handler->root);
247b85ced91SDean Luick 			/* move from LRU list to delete list */
248b85ced91SDean Luick 			list_move(&rbnode->list, &del_list);
249b85ced91SDean Luick 		}
250b85ced91SDean Luick 		if (stop)
251b85ced91SDean Luick 			break;
252b85ced91SDean Luick 	}
253b85ced91SDean Luick 	spin_unlock_irqrestore(&handler->lock, flags);
254b85ced91SDean Luick 
255b85ced91SDean Luick 	list_for_each_entry_safe(rbnode, ptr, &del_list, list) {
256b85ced91SDean Luick 		trace_hfi1_mmu_rb_evict(rbnode);
257b85ced91SDean Luick 		kref_put(&rbnode->refcount, release_immediate);
258b85ced91SDean Luick 	}
259b85ced91SDean Luick }
260b85ced91SDean Luick 
mmu_notifier_range_start(struct mmu_notifier * mn,const struct mmu_notifier_range * range)261082b3532SDean Luick static int mmu_notifier_range_start(struct mmu_notifier *mn,
262b85ced91SDean Luick 		const struct mmu_notifier_range *range)
263b85ced91SDean Luick {
264b85ced91SDean Luick 	struct mmu_rb_handler *handler =
265b85ced91SDean Luick 		container_of(mn, struct mmu_rb_handler, mn);
266b85ced91SDean Luick 	struct rb_root_cached *root = &handler->root;
267c1e8d7c6SMichel Lespinasse 	struct mmu_rb_node *node, *ptr = NULL;
268b85ced91SDean Luick 	unsigned long flags;
269b85ced91SDean Luick 
270b85ced91SDean Luick 	spin_lock_irqsave(&handler->lock, flags);
271b85ced91SDean Luick 	for (node = __mmu_int_rb_iter_first(root, range->start, range->end-1);
272b85ced91SDean Luick 	     node; node = ptr) {
273b85ced91SDean Luick 		/* Guard against node removal. */
274b85ced91SDean Luick 		ptr = __mmu_int_rb_iter_next(node, range->start,
275b85ced91SDean Luick 					     range->end - 1);
276b85ced91SDean Luick 		trace_hfi1_mmu_mem_invalidate(node);
277b85ced91SDean Luick 		/* Remove from rb tree and lru_list. */
278b85ced91SDean Luick 		__mmu_int_rb_remove(node, root);
279b85ced91SDean Luick 		list_del_init(&node->list);
280b85ced91SDean Luick 		kref_put(&node->refcount, release_nolock);
281b85ced91SDean Luick 	}
282b85ced91SDean Luick 	spin_unlock_irqrestore(&handler->lock, flags);
283b85ced91SDean Luick 
284f48ad614SDennis Dalessandro 	return 0;
285 }
286 
287 /*
288  * Work queue function to remove all nodes that have been queued up to
289  * be removed.  The key feature is that mm->mmap_lock is not being held
290  * and the remove callback can sleep while taking it, if needed.
291  */
handle_remove(struct work_struct * work)292 static void handle_remove(struct work_struct *work)
293 {
294 	struct mmu_rb_handler *handler = container_of(work,
295 						struct mmu_rb_handler,
296 						del_work);
297 	struct list_head del_list;
298 	unsigned long flags;
299 	struct mmu_rb_node *node;
300 
301 	/* remove anything that is queued to get removed */
302 	spin_lock_irqsave(&handler->lock, flags);
303 	list_replace_init(&handler->del_list, &del_list);
304 	spin_unlock_irqrestore(&handler->lock, flags);
305 
306 	while (!list_empty(&del_list)) {
307 		node = list_first_entry(&del_list, struct mmu_rb_node, list);
308 		list_del(&node->list);
309 		trace_hfi1_mmu_release_node(node);
310 		handler->ops->remove(handler->ops_arg, node);
311 	}
312 }
313