Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2016 Intel Corporation. |
| 3 | * |
| 4 | * This file is provided under a dual BSD/GPLv2 license. When using or |
| 5 | * redistributing this file, you may do so under either license. |
| 6 | * |
| 7 | * GPL LICENSE SUMMARY |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of version 2 of the GNU General Public License as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * BSD LICENSE |
| 19 | * |
| 20 | * Redistribution and use in source and binary forms, with or without |
| 21 | * modification, are permitted provided that the following conditions |
| 22 | * are met: |
| 23 | * |
| 24 | * - Redistributions of source code must retain the above copyright |
| 25 | * notice, this list of conditions and the following disclaimer. |
| 26 | * - Redistributions in binary form must reproduce the above copyright |
| 27 | * notice, this list of conditions and the following disclaimer in |
| 28 | * the documentation and/or other materials provided with the |
| 29 | * distribution. |
| 30 | * - Neither the name of Intel Corporation nor the names of its |
| 31 | * contributors may be used to endorse or promote products derived |
| 32 | * from this software without specific prior written permission. |
| 33 | * |
| 34 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 35 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 36 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 37 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 38 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 39 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 40 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 41 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 42 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 43 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 44 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 45 | * |
| 46 | */ |
| 47 | #include <linux/list.h> |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 48 | #include <linux/rculist.h> |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 49 | #include <linux/mmu_notifier.h> |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 50 | #include <linux/interval_tree_generic.h> |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 51 | |
| 52 | #include "mmu_rb.h" |
| 53 | #include "trace.h" |
| 54 | |
| 55 | struct mmu_rb_handler { |
| 56 | struct list_head list; |
| 57 | struct mmu_notifier mn; |
| 58 | struct rb_root *root; |
| 59 | spinlock_t lock; /* protect the RB tree */ |
| 60 | struct mmu_rb_ops *ops; |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame^] | 61 | struct mm_struct *mm; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static LIST_HEAD(mmu_rb_handlers); |
| 65 | static DEFINE_SPINLOCK(mmu_rb_lock); /* protect mmu_rb_handlers list */ |
| 66 | |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 67 | static unsigned long mmu_node_start(struct mmu_rb_node *); |
| 68 | static unsigned long mmu_node_last(struct mmu_rb_node *); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 69 | static struct mmu_rb_handler *find_mmu_handler(struct rb_root *); |
| 70 | static inline void mmu_notifier_page(struct mmu_notifier *, struct mm_struct *, |
| 71 | unsigned long); |
| 72 | static inline void mmu_notifier_range_start(struct mmu_notifier *, |
| 73 | struct mm_struct *, |
| 74 | unsigned long, unsigned long); |
| 75 | static void mmu_notifier_mem_invalidate(struct mmu_notifier *, |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 76 | struct mm_struct *, |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 77 | unsigned long, unsigned long); |
| 78 | static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *, |
| 79 | unsigned long, unsigned long); |
| 80 | |
| 81 | static struct mmu_notifier_ops mn_opts = { |
| 82 | .invalidate_page = mmu_notifier_page, |
| 83 | .invalidate_range_start = mmu_notifier_range_start, |
| 84 | }; |
| 85 | |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 86 | INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last, |
| 87 | mmu_node_start, mmu_node_last, static, __mmu_int_rb); |
| 88 | |
| 89 | static unsigned long mmu_node_start(struct mmu_rb_node *node) |
| 90 | { |
| 91 | return node->addr & PAGE_MASK; |
| 92 | } |
| 93 | |
| 94 | static unsigned long mmu_node_last(struct mmu_rb_node *node) |
| 95 | { |
Mitko Haralanov | de79093 | 2016-04-12 10:46:41 -0700 | [diff] [blame] | 96 | return PAGE_ALIGN(node->addr + node->len) - 1; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame^] | 99 | int hfi1_mmu_rb_register(struct mm_struct *mm, struct rb_root *root, |
| 100 | struct mmu_rb_ops *ops) |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 101 | { |
| 102 | struct mmu_rb_handler *handlr; |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame^] | 103 | int ret; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 104 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 105 | handlr = kmalloc(sizeof(*handlr), GFP_KERNEL); |
| 106 | if (!handlr) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | handlr->root = root; |
| 110 | handlr->ops = ops; |
| 111 | INIT_HLIST_NODE(&handlr->mn.hlist); |
| 112 | spin_lock_init(&handlr->lock); |
| 113 | handlr->mn.ops = &mn_opts; |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame^] | 114 | handlr->mm = mm; |
| 115 | |
| 116 | ret = mmu_notifier_register(&handlr->mn, handlr->mm); |
| 117 | if (ret) { |
| 118 | kfree(handlr); |
| 119 | return ret; |
| 120 | } |
| 121 | |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 122 | spin_lock(&mmu_rb_lock); |
| 123 | list_add_tail_rcu(&handlr->list, &mmu_rb_handlers); |
| 124 | spin_unlock(&mmu_rb_lock); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 125 | |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame^] | 126 | return ret; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void hfi1_mmu_rb_unregister(struct rb_root *root) |
| 130 | { |
| 131 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
Dean Luick | 20a42d0 | 2016-07-28 12:27:36 -0400 | [diff] [blame] | 132 | struct mmu_rb_node *rbnode; |
| 133 | struct rb_node *node; |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 134 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 135 | |
Mitko Haralanov | 4b00d94 | 2016-03-08 11:14:31 -0800 | [diff] [blame] | 136 | if (!handler) |
| 137 | return; |
| 138 | |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 139 | /* Unregister first so we don't get any more notifications. */ |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame^] | 140 | mmu_notifier_unregister(&handler->mn, handler->mm); |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 141 | |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 142 | spin_lock(&mmu_rb_lock); |
| 143 | list_del_rcu(&handler->list); |
| 144 | spin_unlock(&mmu_rb_lock); |
| 145 | synchronize_rcu(); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 146 | |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 147 | spin_lock_irqsave(&handler->lock, flags); |
Dean Luick | 20a42d0 | 2016-07-28 12:27:36 -0400 | [diff] [blame] | 148 | while ((node = rb_first(root))) { |
| 149 | rbnode = rb_entry(node, struct mmu_rb_node, node); |
| 150 | rb_erase(node, root); |
| 151 | handler->ops->remove(root, rbnode, NULL); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 152 | } |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 153 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 154 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 155 | kfree(handler); |
| 156 | } |
| 157 | |
| 158 | int hfi1_mmu_rb_insert(struct rb_root *root, struct mmu_rb_node *mnode) |
| 159 | { |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 160 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 161 | struct mmu_rb_node *node; |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 162 | unsigned long flags; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 163 | int ret = 0; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 164 | |
| 165 | if (!handler) |
| 166 | return -EINVAL; |
| 167 | |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 168 | spin_lock_irqsave(&handler->lock, flags); |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 169 | hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr, |
| 170 | mnode->len); |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 171 | node = __mmu_rb_search(handler, mnode->addr, mnode->len); |
| 172 | if (node) { |
| 173 | ret = -EINVAL; |
| 174 | goto unlock; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 175 | } |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 176 | __mmu_int_rb_insert(mnode, root); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 177 | |
Dean Luick | c094664 | 2016-07-28 12:27:30 -0400 | [diff] [blame] | 178 | ret = handler->ops->insert(root, mnode); |
| 179 | if (ret) |
| 180 | __mmu_int_rb_remove(mnode, root); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 181 | unlock: |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 182 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 183 | return ret; |
| 184 | } |
| 185 | |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 186 | /* Caller must hold handler lock */ |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 187 | static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler, |
| 188 | unsigned long addr, |
| 189 | unsigned long len) |
| 190 | { |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 191 | struct mmu_rb_node *node = NULL; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 192 | |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 193 | hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len); |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 194 | if (!handler->ops->filter) { |
| 195 | node = __mmu_int_rb_iter_first(handler->root, addr, |
| 196 | (addr + len) - 1); |
| 197 | } else { |
| 198 | for (node = __mmu_int_rb_iter_first(handler->root, addr, |
| 199 | (addr + len) - 1); |
| 200 | node; |
| 201 | node = __mmu_int_rb_iter_next(node, addr, |
| 202 | (addr + len) - 1)) { |
| 203 | if (handler->ops->filter(node, addr, len)) |
| 204 | return node; |
| 205 | } |
| 206 | } |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 207 | return node; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 210 | struct mmu_rb_node *hfi1_mmu_rb_extract(struct rb_root *root, |
| 211 | unsigned long addr, unsigned long len) |
| 212 | { |
| 213 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
| 214 | struct mmu_rb_node *node; |
| 215 | unsigned long flags; |
| 216 | |
| 217 | if (!handler) |
| 218 | return ERR_PTR(-EINVAL); |
| 219 | |
| 220 | spin_lock_irqsave(&handler->lock, flags); |
| 221 | node = __mmu_rb_search(handler, addr, len); |
| 222 | if (node) |
| 223 | __mmu_int_rb_remove(node, handler->root); |
| 224 | spin_unlock_irqrestore(&handler->lock, flags); |
| 225 | |
| 226 | return node; |
| 227 | } |
| 228 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 229 | void hfi1_mmu_rb_remove(struct rb_root *root, struct mmu_rb_node *node) |
| 230 | { |
Ira Weiny | 3c1091aa | 2016-07-28 12:27:31 -0400 | [diff] [blame] | 231 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 232 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
| 233 | |
| 234 | if (!handler || !node) |
| 235 | return; |
| 236 | |
Ira Weiny | 3c1091aa | 2016-07-28 12:27:31 -0400 | [diff] [blame] | 237 | /* Validity of handler and node pointers has been checked by caller. */ |
| 238 | hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr, |
| 239 | node->len); |
| 240 | spin_lock_irqsave(&handler->lock, flags); |
| 241 | __mmu_int_rb_remove(node, handler->root); |
| 242 | spin_unlock_irqrestore(&handler->lock, flags); |
| 243 | |
| 244 | handler->ops->remove(handler->root, node, NULL); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static struct mmu_rb_handler *find_mmu_handler(struct rb_root *root) |
| 248 | { |
| 249 | struct mmu_rb_handler *handler; |
| 250 | |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 251 | rcu_read_lock(); |
| 252 | list_for_each_entry_rcu(handler, &mmu_rb_handlers, list) { |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 253 | if (handler->root == root) |
| 254 | goto unlock; |
| 255 | } |
| 256 | handler = NULL; |
| 257 | unlock: |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 258 | rcu_read_unlock(); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 259 | return handler; |
| 260 | } |
| 261 | |
| 262 | static inline void mmu_notifier_page(struct mmu_notifier *mn, |
| 263 | struct mm_struct *mm, unsigned long addr) |
| 264 | { |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 265 | mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static inline void mmu_notifier_range_start(struct mmu_notifier *mn, |
| 269 | struct mm_struct *mm, |
| 270 | unsigned long start, |
| 271 | unsigned long end) |
| 272 | { |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 273 | mmu_notifier_mem_invalidate(mn, mm, start, end); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn, |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 277 | struct mm_struct *mm, |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 278 | unsigned long start, unsigned long end) |
| 279 | { |
| 280 | struct mmu_rb_handler *handler = |
| 281 | container_of(mn, struct mmu_rb_handler, mn); |
| 282 | struct rb_root *root = handler->root; |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 283 | struct mmu_rb_node *node, *ptr = NULL; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 284 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 285 | |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 286 | spin_lock_irqsave(&handler->lock, flags); |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 287 | for (node = __mmu_int_rb_iter_first(root, start, end - 1); |
| 288 | node; node = ptr) { |
| 289 | /* Guard against node removal. */ |
| 290 | ptr = __mmu_int_rb_iter_next(node, start, end - 1); |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 291 | hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u", |
| 292 | node->addr, node->len); |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 293 | if (handler->ops->invalidate(root, node)) { |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 294 | __mmu_int_rb_remove(node, root); |
Dean Luick | c094664 | 2016-07-28 12:27:30 -0400 | [diff] [blame] | 295 | handler->ops->remove(root, node, mm); |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 296 | } |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 297 | } |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 298 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 299 | } |