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; |
| 61 | }; |
| 62 | |
| 63 | static LIST_HEAD(mmu_rb_handlers); |
| 64 | static DEFINE_SPINLOCK(mmu_rb_lock); /* protect mmu_rb_handlers list */ |
| 65 | |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 66 | static unsigned long mmu_node_start(struct mmu_rb_node *); |
| 67 | static unsigned long mmu_node_last(struct mmu_rb_node *); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 68 | static struct mmu_rb_handler *find_mmu_handler(struct rb_root *); |
| 69 | static inline void mmu_notifier_page(struct mmu_notifier *, struct mm_struct *, |
| 70 | unsigned long); |
| 71 | static inline void mmu_notifier_range_start(struct mmu_notifier *, |
| 72 | struct mm_struct *, |
| 73 | unsigned long, unsigned long); |
| 74 | static void mmu_notifier_mem_invalidate(struct mmu_notifier *, |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 75 | struct mm_struct *, |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 76 | unsigned long, unsigned long); |
| 77 | static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *, |
| 78 | unsigned long, unsigned long); |
| 79 | |
| 80 | static struct mmu_notifier_ops mn_opts = { |
| 81 | .invalidate_page = mmu_notifier_page, |
| 82 | .invalidate_range_start = mmu_notifier_range_start, |
| 83 | }; |
| 84 | |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 85 | INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last, |
| 86 | mmu_node_start, mmu_node_last, static, __mmu_int_rb); |
| 87 | |
| 88 | static unsigned long mmu_node_start(struct mmu_rb_node *node) |
| 89 | { |
| 90 | return node->addr & PAGE_MASK; |
| 91 | } |
| 92 | |
| 93 | static unsigned long mmu_node_last(struct mmu_rb_node *node) |
| 94 | { |
Mitko Haralanov | de79093 | 2016-04-12 10:46:41 -0700 | [diff] [blame] | 95 | return PAGE_ALIGN(node->addr + node->len) - 1; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 98 | int hfi1_mmu_rb_register(struct rb_root *root, struct mmu_rb_ops *ops) |
| 99 | { |
| 100 | struct mmu_rb_handler *handlr; |
| 101 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 102 | handlr = kmalloc(sizeof(*handlr), GFP_KERNEL); |
| 103 | if (!handlr) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | handlr->root = root; |
| 107 | handlr->ops = ops; |
| 108 | INIT_HLIST_NODE(&handlr->mn.hlist); |
| 109 | spin_lock_init(&handlr->lock); |
| 110 | handlr->mn.ops = &mn_opts; |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 111 | spin_lock(&mmu_rb_lock); |
| 112 | list_add_tail_rcu(&handlr->list, &mmu_rb_handlers); |
| 113 | spin_unlock(&mmu_rb_lock); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 114 | |
| 115 | return mmu_notifier_register(&handlr->mn, current->mm); |
| 116 | } |
| 117 | |
| 118 | void hfi1_mmu_rb_unregister(struct rb_root *root) |
| 119 | { |
| 120 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 121 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 122 | |
Mitko Haralanov | 4b00d94 | 2016-03-08 11:14:31 -0800 | [diff] [blame] | 123 | if (!handler) |
| 124 | return; |
| 125 | |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 126 | /* Unregister first so we don't get any more notifications. */ |
| 127 | if (current->mm) |
| 128 | mmu_notifier_unregister(&handler->mn, current->mm); |
| 129 | |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 130 | spin_lock(&mmu_rb_lock); |
| 131 | list_del_rcu(&handler->list); |
| 132 | spin_unlock(&mmu_rb_lock); |
| 133 | synchronize_rcu(); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 134 | |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 135 | spin_lock_irqsave(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 136 | if (!RB_EMPTY_ROOT(root)) { |
| 137 | struct rb_node *node; |
| 138 | struct mmu_rb_node *rbnode; |
| 139 | |
| 140 | while ((node = rb_first(root))) { |
| 141 | rbnode = rb_entry(node, struct mmu_rb_node, node); |
Mitko Haralanov | eef9c89 | 2016-03-08 11:14:36 -0800 | [diff] [blame] | 142 | rb_erase(node, root); |
Dean Luick | c094664 | 2016-07-28 12:27:30 -0400 | [diff] [blame] | 143 | handler->ops->remove(root, rbnode, NULL); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 146 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 147 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 148 | kfree(handler); |
| 149 | } |
| 150 | |
| 151 | int hfi1_mmu_rb_insert(struct rb_root *root, struct mmu_rb_node *mnode) |
| 152 | { |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 153 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 154 | struct mmu_rb_node *node; |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 155 | unsigned long flags; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 156 | int ret = 0; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 157 | |
| 158 | if (!handler) |
| 159 | return -EINVAL; |
| 160 | |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 161 | spin_lock_irqsave(&handler->lock, flags); |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 162 | hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr, |
| 163 | mnode->len); |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 164 | node = __mmu_rb_search(handler, mnode->addr, mnode->len); |
| 165 | if (node) { |
| 166 | ret = -EINVAL; |
| 167 | goto unlock; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 168 | } |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 169 | __mmu_int_rb_insert(mnode, root); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 170 | |
Dean Luick | c094664 | 2016-07-28 12:27:30 -0400 | [diff] [blame] | 171 | ret = handler->ops->insert(root, mnode); |
| 172 | if (ret) |
| 173 | __mmu_int_rb_remove(mnode, root); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 174 | unlock: |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 175 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 176 | return ret; |
| 177 | } |
| 178 | |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 179 | /* Caller must hold handler lock */ |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 180 | static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler, |
| 181 | unsigned long addr, |
| 182 | unsigned long len) |
| 183 | { |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 184 | struct mmu_rb_node *node = NULL; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 185 | |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 186 | hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len); |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 187 | if (!handler->ops->filter) { |
| 188 | node = __mmu_int_rb_iter_first(handler->root, addr, |
| 189 | (addr + len) - 1); |
| 190 | } else { |
| 191 | for (node = __mmu_int_rb_iter_first(handler->root, addr, |
| 192 | (addr + len) - 1); |
| 193 | node; |
| 194 | node = __mmu_int_rb_iter_next(node, addr, |
| 195 | (addr + len) - 1)) { |
| 196 | if (handler->ops->filter(node, addr, len)) |
| 197 | return node; |
| 198 | } |
| 199 | } |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 200 | return node; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 203 | struct mmu_rb_node *hfi1_mmu_rb_extract(struct rb_root *root, |
| 204 | unsigned long addr, unsigned long len) |
| 205 | { |
| 206 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
| 207 | struct mmu_rb_node *node; |
| 208 | unsigned long flags; |
| 209 | |
| 210 | if (!handler) |
| 211 | return ERR_PTR(-EINVAL); |
| 212 | |
| 213 | spin_lock_irqsave(&handler->lock, flags); |
| 214 | node = __mmu_rb_search(handler, addr, len); |
| 215 | if (node) |
| 216 | __mmu_int_rb_remove(node, handler->root); |
| 217 | spin_unlock_irqrestore(&handler->lock, flags); |
| 218 | |
| 219 | return node; |
| 220 | } |
| 221 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 222 | void hfi1_mmu_rb_remove(struct rb_root *root, struct mmu_rb_node *node) |
| 223 | { |
Ira Weiny | 3c1091aa | 2016-07-28 12:27:31 -0400 | [diff] [blame^] | 224 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 225 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
| 226 | |
| 227 | if (!handler || !node) |
| 228 | return; |
| 229 | |
Ira Weiny | 3c1091aa | 2016-07-28 12:27:31 -0400 | [diff] [blame^] | 230 | /* Validity of handler and node pointers has been checked by caller. */ |
| 231 | hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr, |
| 232 | node->len); |
| 233 | spin_lock_irqsave(&handler->lock, flags); |
| 234 | __mmu_int_rb_remove(node, handler->root); |
| 235 | spin_unlock_irqrestore(&handler->lock, flags); |
| 236 | |
| 237 | handler->ops->remove(handler->root, node, NULL); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static struct mmu_rb_handler *find_mmu_handler(struct rb_root *root) |
| 241 | { |
| 242 | struct mmu_rb_handler *handler; |
| 243 | |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 244 | rcu_read_lock(); |
| 245 | list_for_each_entry_rcu(handler, &mmu_rb_handlers, list) { |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 246 | if (handler->root == root) |
| 247 | goto unlock; |
| 248 | } |
| 249 | handler = NULL; |
| 250 | unlock: |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 251 | rcu_read_unlock(); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 252 | return handler; |
| 253 | } |
| 254 | |
| 255 | static inline void mmu_notifier_page(struct mmu_notifier *mn, |
| 256 | struct mm_struct *mm, unsigned long addr) |
| 257 | { |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 258 | mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static inline void mmu_notifier_range_start(struct mmu_notifier *mn, |
| 262 | struct mm_struct *mm, |
| 263 | unsigned long start, |
| 264 | unsigned long end) |
| 265 | { |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 266 | mmu_notifier_mem_invalidate(mn, mm, start, end); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn, |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 270 | struct mm_struct *mm, |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 271 | unsigned long start, unsigned long end) |
| 272 | { |
| 273 | struct mmu_rb_handler *handler = |
| 274 | container_of(mn, struct mmu_rb_handler, mn); |
| 275 | struct rb_root *root = handler->root; |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 276 | struct mmu_rb_node *node, *ptr = NULL; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 277 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 278 | |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 279 | spin_lock_irqsave(&handler->lock, flags); |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 280 | for (node = __mmu_int_rb_iter_first(root, start, end - 1); |
| 281 | node; node = ptr) { |
| 282 | /* Guard against node removal. */ |
| 283 | ptr = __mmu_int_rb_iter_next(node, start, end - 1); |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 284 | hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u", |
| 285 | node->addr, node->len); |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 286 | if (handler->ops->invalidate(root, node)) { |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 287 | __mmu_int_rb_remove(node, root); |
Dean Luick | c094664 | 2016-07-28 12:27:30 -0400 | [diff] [blame] | 288 | handler->ops->remove(root, node, mm); |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 289 | } |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 290 | } |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 291 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 292 | } |