1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  * $FreeBSD$
35  */
36 
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/sched.h>
40 #include <linux/gfp.h>
41 #include <rdma/ib_umem_odp.h>
42 
43 /*
44  * The ib_umem list keeps track of memory regions for which the HW
45  * device request to receive notification when the related memory
46  * mapping is changed.
47  *
48  * ib_umem_lock protects the list.
49  */
50 
51 static inline u64 node_start(struct umem_odp_node *n)
52 {
53 	struct ib_umem_odp *umem_odp =
54 			container_of(n, struct ib_umem_odp, interval_tree);
55 
56 	return ib_umem_start(umem_odp->umem);
57 }
58 
59 /* Note that the representation of the intervals in the interval tree
60  * considers the ending point as contained in the interval, while the
61  * function ib_umem_end returns the first address which is not contained
62  * in the umem.
63  */
64 static inline u64 node_last(struct umem_odp_node *n)
65 {
66 	struct ib_umem_odp *umem_odp =
67 			container_of(n, struct ib_umem_odp, interval_tree);
68 
69 	return ib_umem_end(umem_odp->umem) - 1;
70 }
71 
72 INTERVAL_TREE_DEFINE(struct umem_odp_node, rb, u64, __subtree_last,
73 		     node_start, node_last, , rbt_ib_umem)
74 
75 /* @last is not a part of the interval. See comment for function
76  * node_last.
77  */
78 int rbt_ib_umem_for_each_in_range(struct rb_root *root,
79 				  u64 start, u64 last,
80 				  umem_call_back cb,
81 				  void *cookie)
82 {
83 	int ret_val = 0;
84 	struct umem_odp_node *node;
85 	struct ib_umem_odp *umem;
86 
87 	if (unlikely(start == last))
88 		return ret_val;
89 
90 	for (node = rbt_ib_umem_iter_first(root, start, last - 1); node;
91 			node = rbt_ib_umem_iter_next(node, start, last - 1)) {
92 		umem = container_of(node, struct ib_umem_odp, interval_tree);
93 		ret_val = cb(umem->umem, start, last, cookie) || ret_val;
94 	}
95 
96 	return ret_val;
97 }
98