Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 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 | |
| 33 | #ifndef IB_UMEM_ODP_H |
| 34 | #define IB_UMEM_ODP_H |
| 35 | |
| 36 | #include <rdma/ib_umem.h> |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 37 | #include <rdma/ib_verbs.h> |
| 38 | #include <linux/interval_tree.h> |
| 39 | |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 40 | struct ib_umem_odp { |
Jason Gunthorpe | 41b4deea | 2018-09-16 20:48:05 +0300 | [diff] [blame] | 41 | struct ib_umem umem; |
Jason Gunthorpe | c9990ab | 2018-09-16 20:48:07 +0300 | [diff] [blame] | 42 | struct ib_ucontext_per_mm *per_mm; |
| 43 | |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 44 | /* |
| 45 | * An array of the pages included in the on-demand paging umem. |
| 46 | * Indices of pages that are currently not mapped into the device will |
| 47 | * contain NULL. |
| 48 | */ |
| 49 | struct page **page_list; |
| 50 | /* |
| 51 | * An array of the same size as page_list, with DMA addresses mapped |
| 52 | * for pages the pages in page_list. The lower two bits designate |
| 53 | * access permissions. See ODP_READ_ALLOWED_BIT and |
| 54 | * ODP_WRITE_ALLOWED_BIT. |
| 55 | */ |
| 56 | dma_addr_t *dma_list; |
| 57 | /* |
| 58 | * The umem_mutex protects the page_list and dma_list fields of an ODP |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 59 | * umem, allowing only a single thread to map/unmap pages. The mutex |
| 60 | * also protects access to the mmu notifier counters. |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 61 | */ |
| 62 | struct mutex umem_mutex; |
| 63 | void *private; /* for the HW driver to use. */ |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 64 | |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 65 | int notifiers_seq; |
| 66 | int notifiers_count; |
Shiraz Saleem | d10bcf9 | 2019-04-02 14:52:52 -0500 | [diff] [blame] | 67 | int npages; |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 68 | |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 69 | /* Tree tracking */ |
Jason Gunthorpe | 7cc2e18 | 2019-08-19 14:16:59 +0300 | [diff] [blame] | 70 | struct interval_tree_node interval_tree; |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 71 | |
Jason Gunthorpe | fd7dbf0 | 2019-08-19 14:17:01 +0300 | [diff] [blame] | 72 | /* |
| 73 | * An implicit odp umem cannot be DMA mapped, has 0 length, and serves |
| 74 | * only as an anchor for the driver to hold onto the per_mm. FIXME: |
| 75 | * This should be removed and drivers should work with the per_mm |
| 76 | * directly. |
| 77 | */ |
| 78 | bool is_implicit_odp; |
| 79 | |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 80 | struct completion notifier_completion; |
| 81 | int dying; |
Jason Gunthorpe | d2183c6 | 2019-05-20 09:05:25 +0300 | [diff] [blame] | 82 | unsigned int page_shift; |
Artemy Kovalyov | d07d1d7 | 2017-01-18 16:58:07 +0200 | [diff] [blame] | 83 | struct work_struct work; |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 86 | static inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem) |
| 87 | { |
Jason Gunthorpe | 41b4deea | 2018-09-16 20:48:05 +0300 | [diff] [blame] | 88 | return container_of(umem, struct ib_umem_odp, umem); |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 89 | } |
| 90 | |
Jason Gunthorpe | d2183c6 | 2019-05-20 09:05:25 +0300 | [diff] [blame] | 91 | /* Returns the first page of an ODP umem. */ |
| 92 | static inline unsigned long ib_umem_start(struct ib_umem_odp *umem_odp) |
| 93 | { |
| 94 | return ALIGN_DOWN(umem_odp->umem.address, 1UL << umem_odp->page_shift); |
| 95 | } |
| 96 | |
| 97 | /* Returns the address of the page after the last one of an ODP umem. */ |
| 98 | static inline unsigned long ib_umem_end(struct ib_umem_odp *umem_odp) |
| 99 | { |
| 100 | return ALIGN(umem_odp->umem.address + umem_odp->umem.length, |
| 101 | 1UL << umem_odp->page_shift); |
| 102 | } |
| 103 | |
| 104 | static inline size_t ib_umem_odp_num_pages(struct ib_umem_odp *umem_odp) |
| 105 | { |
| 106 | return (ib_umem_end(umem_odp) - ib_umem_start(umem_odp)) >> |
| 107 | umem_odp->page_shift; |
| 108 | } |
| 109 | |
Leon Romanovsky | 13859d5d | 2019-01-08 16:07:26 +0200 | [diff] [blame] | 110 | /* |
| 111 | * The lower 2 bits of the DMA address signal the R/W permissions for |
| 112 | * the entry. To upgrade the permissions, provide the appropriate |
| 113 | * bitmask to the map_dma_pages function. |
| 114 | * |
| 115 | * Be aware that upgrading a mapped address might result in change of |
| 116 | * the DMA address for the page. |
| 117 | */ |
| 118 | #define ODP_READ_ALLOWED_BIT (1<<0ULL) |
| 119 | #define ODP_WRITE_ALLOWED_BIT (1<<1ULL) |
| 120 | |
| 121 | #define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT)) |
| 122 | |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 123 | #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING |
| 124 | |
Jason Gunthorpe | f27a0d5 | 2018-09-16 20:48:08 +0300 | [diff] [blame] | 125 | struct ib_ucontext_per_mm { |
| 126 | struct ib_ucontext *context; |
| 127 | struct mm_struct *mm; |
| 128 | struct pid *tgid; |
Jason Gunthorpe | be7a57b | 2018-09-16 20:48:10 +0300 | [diff] [blame] | 129 | bool active; |
Jason Gunthorpe | f27a0d5 | 2018-09-16 20:48:08 +0300 | [diff] [blame] | 130 | |
| 131 | struct rb_root_cached umem_tree; |
| 132 | /* Protects umem_tree */ |
| 133 | struct rw_semaphore umem_rwsem; |
Jason Gunthorpe | f27a0d5 | 2018-09-16 20:48:08 +0300 | [diff] [blame] | 134 | |
| 135 | struct mmu_notifier mn; |
Jason Gunthorpe | f27a0d5 | 2018-09-16 20:48:08 +0300 | [diff] [blame] | 136 | unsigned int odp_mrs_count; |
| 137 | |
| 138 | struct list_head ucontext_list; |
Jason Gunthorpe | 56ac9dd | 2018-09-16 20:48:11 +0300 | [diff] [blame] | 139 | struct rcu_head rcu; |
Jason Gunthorpe | f27a0d5 | 2018-09-16 20:48:08 +0300 | [diff] [blame] | 140 | }; |
| 141 | |
Jason Gunthorpe | 261dc53 | 2019-08-19 14:17:04 +0300 | [diff] [blame^] | 142 | struct ib_umem_odp *ib_umem_odp_get(struct ib_udata *udata, unsigned long addr, |
| 143 | size_t size, int access); |
Jason Gunthorpe | f20bef6 | 2019-08-19 14:17:03 +0300 | [diff] [blame] | 144 | struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_udata *udata, |
| 145 | int access); |
| 146 | struct ib_umem_odp *ib_umem_odp_alloc_child(struct ib_umem_odp *root_umem, |
| 147 | unsigned long addr, size_t size); |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 148 | void ib_umem_odp_release(struct ib_umem_odp *umem_odp); |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 149 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 150 | int ib_umem_odp_map_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset, |
| 151 | u64 bcnt, u64 access_mask, |
| 152 | unsigned long current_seq); |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 153 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 154 | void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset, |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 155 | u64 bound); |
| 156 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 157 | typedef int (*umem_call_back)(struct ib_umem_odp *item, u64 start, u64 end, |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 158 | void *cookie); |
| 159 | /* |
| 160 | * Call the callback on each ib_umem in the range. Returns the logical or of |
| 161 | * the return values of the functions called. |
| 162 | */ |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 163 | int rbt_ib_umem_for_each_in_range(struct rb_root_cached *root, |
| 164 | u64 start, u64 end, |
Michal Hocko | 93065ac | 2018-08-21 21:52:33 -0700 | [diff] [blame] | 165 | umem_call_back cb, |
| 166 | bool blockable, void *cookie); |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 167 | |
Artemy Kovalyov | d07d1d7 | 2017-01-18 16:58:07 +0200 | [diff] [blame] | 168 | /* |
| 169 | * Find first region intersecting with address range. |
| 170 | * Return NULL if not found |
| 171 | */ |
Jason Gunthorpe | 7cc2e18 | 2019-08-19 14:16:59 +0300 | [diff] [blame] | 172 | static inline struct ib_umem_odp * |
| 173 | rbt_ib_umem_lookup(struct rb_root_cached *root, u64 addr, u64 length) |
| 174 | { |
| 175 | struct interval_tree_node *node; |
| 176 | |
| 177 | node = interval_tree_iter_first(root, addr, addr + length - 1); |
| 178 | if (!node) |
| 179 | return NULL; |
| 180 | return container_of(node, struct ib_umem_odp, interval_tree); |
| 181 | |
| 182 | } |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 183 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 184 | static inline int ib_umem_mmu_notifier_retry(struct ib_umem_odp *umem_odp, |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 185 | unsigned long mmu_seq) |
| 186 | { |
| 187 | /* |
| 188 | * This code is strongly based on the KVM code from |
| 189 | * mmu_notifier_retry. Should be called with |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 190 | * the relevant locks taken (umem_odp->umem_mutex |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 191 | * and the ucontext umem_mutex semaphore locked for read). |
| 192 | */ |
| 193 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 194 | if (unlikely(umem_odp->notifiers_count)) |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 195 | return 1; |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 196 | if (umem_odp->notifiers_seq != mmu_seq) |
Haggai Eran | 882214e | 2014-12-11 17:04:18 +0200 | [diff] [blame] | 197 | return 1; |
| 198 | return 0; |
| 199 | } |
| 200 | |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 201 | #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */ |
| 202 | |
Jason Gunthorpe | 261dc53 | 2019-08-19 14:17:04 +0300 | [diff] [blame^] | 203 | static inline struct ib_umem_odp *ib_umem_odp_get(struct ib_udata *udata, |
| 204 | unsigned long addr, |
| 205 | size_t size, int access) |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 206 | { |
Jason Gunthorpe | 261dc53 | 2019-08-19 14:17:04 +0300 | [diff] [blame^] | 207 | return ERR_PTR(-EINVAL); |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Jason Gunthorpe | b5231b0 | 2018-09-16 20:48:04 +0300 | [diff] [blame] | 210 | static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {} |
Shachar Raindel | 8ada2c1 | 2014-12-11 17:04:17 +0200 | [diff] [blame] | 211 | |
| 212 | #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */ |
| 213 | |
| 214 | #endif /* IB_UMEM_ODP_H */ |