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); |
Dean Luick | 20a42d0 | 2016-07-28 12:27:36 -0400 | [diff] [blame^] | 121 | struct mmu_rb_node *rbnode; |
| 122 | struct rb_node *node; |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 123 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 124 | |
Mitko Haralanov | 4b00d94 | 2016-03-08 11:14:31 -0800 | [diff] [blame] | 125 | if (!handler) |
| 126 | return; |
| 127 | |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 128 | /* Unregister first so we don't get any more notifications. */ |
| 129 | if (current->mm) |
| 130 | mmu_notifier_unregister(&handler->mn, current->mm); |
| 131 | |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 132 | spin_lock(&mmu_rb_lock); |
| 133 | list_del_rcu(&handler->list); |
| 134 | spin_unlock(&mmu_rb_lock); |
| 135 | synchronize_rcu(); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 136 | |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 137 | spin_lock_irqsave(&handler->lock, flags); |
Dean Luick | 20a42d0 | 2016-07-28 12:27:36 -0400 | [diff] [blame^] | 138 | while ((node = rb_first(root))) { |
| 139 | rbnode = rb_entry(node, struct mmu_rb_node, node); |
| 140 | rb_erase(node, root); |
| 141 | handler->ops->remove(root, rbnode, NULL); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 142 | } |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 143 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 144 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 145 | kfree(handler); |
| 146 | } |
| 147 | |
| 148 | int hfi1_mmu_rb_insert(struct rb_root *root, struct mmu_rb_node *mnode) |
| 149 | { |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 150 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 151 | struct mmu_rb_node *node; |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 152 | unsigned long flags; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 153 | int ret = 0; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 154 | |
| 155 | if (!handler) |
| 156 | return -EINVAL; |
| 157 | |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 158 | spin_lock_irqsave(&handler->lock, flags); |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 159 | hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr, |
| 160 | mnode->len); |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 161 | node = __mmu_rb_search(handler, mnode->addr, mnode->len); |
| 162 | if (node) { |
| 163 | ret = -EINVAL; |
| 164 | goto unlock; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 165 | } |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 166 | __mmu_int_rb_insert(mnode, root); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 167 | |
Dean Luick | c094664 | 2016-07-28 12:27:30 -0400 | [diff] [blame] | 168 | ret = handler->ops->insert(root, mnode); |
| 169 | if (ret) |
| 170 | __mmu_int_rb_remove(mnode, root); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 171 | unlock: |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 172 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 176 | /* Caller must hold handler lock */ |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 177 | static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler, |
| 178 | unsigned long addr, |
| 179 | unsigned long len) |
| 180 | { |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 181 | struct mmu_rb_node *node = NULL; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 182 | |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 183 | hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len); |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 184 | if (!handler->ops->filter) { |
| 185 | node = __mmu_int_rb_iter_first(handler->root, addr, |
| 186 | (addr + len) - 1); |
| 187 | } else { |
| 188 | for (node = __mmu_int_rb_iter_first(handler->root, addr, |
| 189 | (addr + len) - 1); |
| 190 | node; |
| 191 | node = __mmu_int_rb_iter_next(node, addr, |
| 192 | (addr + len) - 1)) { |
| 193 | if (handler->ops->filter(node, addr, len)) |
| 194 | return node; |
| 195 | } |
| 196 | } |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 197 | return node; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 200 | struct mmu_rb_node *hfi1_mmu_rb_extract(struct rb_root *root, |
| 201 | unsigned long addr, unsigned long len) |
| 202 | { |
| 203 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
| 204 | struct mmu_rb_node *node; |
| 205 | unsigned long flags; |
| 206 | |
| 207 | if (!handler) |
| 208 | return ERR_PTR(-EINVAL); |
| 209 | |
| 210 | spin_lock_irqsave(&handler->lock, flags); |
| 211 | node = __mmu_rb_search(handler, addr, len); |
| 212 | if (node) |
| 213 | __mmu_int_rb_remove(node, handler->root); |
| 214 | spin_unlock_irqrestore(&handler->lock, flags); |
| 215 | |
| 216 | return node; |
| 217 | } |
| 218 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 219 | void hfi1_mmu_rb_remove(struct rb_root *root, struct mmu_rb_node *node) |
| 220 | { |
Ira Weiny | 3c1091aa | 2016-07-28 12:27:31 -0400 | [diff] [blame] | 221 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 222 | struct mmu_rb_handler *handler = find_mmu_handler(root); |
| 223 | |
| 224 | if (!handler || !node) |
| 225 | return; |
| 226 | |
Ira Weiny | 3c1091aa | 2016-07-28 12:27:31 -0400 | [diff] [blame] | 227 | /* Validity of handler and node pointers has been checked by caller. */ |
| 228 | hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr, |
| 229 | node->len); |
| 230 | spin_lock_irqsave(&handler->lock, flags); |
| 231 | __mmu_int_rb_remove(node, handler->root); |
| 232 | spin_unlock_irqrestore(&handler->lock, flags); |
| 233 | |
| 234 | handler->ops->remove(handler->root, node, NULL); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static struct mmu_rb_handler *find_mmu_handler(struct rb_root *root) |
| 238 | { |
| 239 | struct mmu_rb_handler *handler; |
| 240 | |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 241 | rcu_read_lock(); |
| 242 | list_for_each_entry_rcu(handler, &mmu_rb_handlers, list) { |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 243 | if (handler->root == root) |
| 244 | goto unlock; |
| 245 | } |
| 246 | handler = NULL; |
| 247 | unlock: |
Mitko Haralanov | 67caea1 | 2016-05-12 10:23:09 -0700 | [diff] [blame] | 248 | rcu_read_unlock(); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 249 | return handler; |
| 250 | } |
| 251 | |
| 252 | static inline void mmu_notifier_page(struct mmu_notifier *mn, |
| 253 | struct mm_struct *mm, unsigned long addr) |
| 254 | { |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 255 | mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static inline void mmu_notifier_range_start(struct mmu_notifier *mn, |
| 259 | struct mm_struct *mm, |
| 260 | unsigned long start, |
| 261 | unsigned long end) |
| 262 | { |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 263 | mmu_notifier_mem_invalidate(mn, mm, start, end); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn, |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 267 | struct mm_struct *mm, |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 268 | unsigned long start, unsigned long end) |
| 269 | { |
| 270 | struct mmu_rb_handler *handler = |
| 271 | container_of(mn, struct mmu_rb_handler, mn); |
| 272 | struct rb_root *root = handler->root; |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 273 | struct mmu_rb_node *node, *ptr = NULL; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 274 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 275 | |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 276 | spin_lock_irqsave(&handler->lock, flags); |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 277 | for (node = __mmu_int_rb_iter_first(root, start, end - 1); |
| 278 | node; node = ptr) { |
| 279 | /* Guard against node removal. */ |
| 280 | ptr = __mmu_int_rb_iter_next(node, start, end - 1); |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 281 | hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u", |
| 282 | node->addr, node->len); |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 283 | if (handler->ops->invalidate(root, node)) { |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 284 | __mmu_int_rb_remove(node, root); |
Dean Luick | c094664 | 2016-07-28 12:27:30 -0400 | [diff] [blame] | 285 | handler->ops->remove(root, node, mm); |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 286 | } |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 287 | } |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 288 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 289 | } |