umem_rbtree.c 3.1 KB

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