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 { |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 56 | struct mmu_notifier mn; |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 57 | struct rb_root root; |
| 58 | void *ops_arg; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 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; |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 62 | struct work_struct del_work; |
| 63 | struct list_head del_list; |
| 64 | struct workqueue_struct *wq; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 65 | }; |
| 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 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); |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 79 | static void do_remove(struct mmu_rb_handler *handler, |
| 80 | struct list_head *del_list); |
| 81 | static void handle_remove(struct work_struct *work); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 82 | |
| 83 | static struct mmu_notifier_ops mn_opts = { |
| 84 | .invalidate_page = mmu_notifier_page, |
| 85 | .invalidate_range_start = mmu_notifier_range_start, |
| 86 | }; |
| 87 | |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 88 | INTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last, |
| 89 | mmu_node_start, mmu_node_last, static, __mmu_int_rb); |
| 90 | |
| 91 | static unsigned long mmu_node_start(struct mmu_rb_node *node) |
| 92 | { |
| 93 | return node->addr & PAGE_MASK; |
| 94 | } |
| 95 | |
| 96 | static unsigned long mmu_node_last(struct mmu_rb_node *node) |
| 97 | { |
Mitko Haralanov | de79093 | 2016-04-12 10:46:41 -0700 | [diff] [blame] | 98 | return PAGE_ALIGN(node->addr + node->len) - 1; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 101 | int hfi1_mmu_rb_register(void *ops_arg, struct mm_struct *mm, |
| 102 | struct mmu_rb_ops *ops, |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 103 | struct workqueue_struct *wq, |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 104 | struct mmu_rb_handler **handler) |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 105 | { |
| 106 | struct mmu_rb_handler *handlr; |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 107 | int ret; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 108 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 109 | handlr = kmalloc(sizeof(*handlr), GFP_KERNEL); |
| 110 | if (!handlr) |
| 111 | return -ENOMEM; |
| 112 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 113 | handlr->root = RB_ROOT; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 114 | handlr->ops = ops; |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 115 | handlr->ops_arg = ops_arg; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 116 | INIT_HLIST_NODE(&handlr->mn.hlist); |
| 117 | spin_lock_init(&handlr->lock); |
| 118 | handlr->mn.ops = &mn_opts; |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 119 | handlr->mm = mm; |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 120 | INIT_WORK(&handlr->del_work, handle_remove); |
| 121 | INIT_LIST_HEAD(&handlr->del_list); |
| 122 | handlr->wq = wq; |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 123 | |
| 124 | ret = mmu_notifier_register(&handlr->mn, handlr->mm); |
| 125 | if (ret) { |
| 126 | kfree(handlr); |
| 127 | return ret; |
| 128 | } |
| 129 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 130 | *handler = handlr; |
| 131 | return 0; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 134 | void hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler) |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 135 | { |
Dean Luick | 20a42d0 | 2016-07-28 12:27:36 -0400 | [diff] [blame] | 136 | struct mmu_rb_node *rbnode; |
| 137 | struct rb_node *node; |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 138 | unsigned long flags; |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 139 | struct list_head del_list; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 140 | |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 141 | /* Unregister first so we don't get any more notifications. */ |
Ira Weiny | 3faa3d9 | 2016-07-28 15:21:19 -0400 | [diff] [blame] | 142 | mmu_notifier_unregister(&handler->mn, handler->mm); |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 143 | |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 144 | /* |
| 145 | * Make sure the wq delete handler is finished running. It will not |
| 146 | * be triggered once the mmu notifiers are unregistered above. |
| 147 | */ |
| 148 | flush_work(&handler->del_work); |
| 149 | |
| 150 | INIT_LIST_HEAD(&del_list); |
| 151 | |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 152 | spin_lock_irqsave(&handler->lock, flags); |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 153 | while ((node = rb_first(&handler->root))) { |
Dean Luick | 20a42d0 | 2016-07-28 12:27:36 -0400 | [diff] [blame] | 154 | rbnode = rb_entry(node, struct mmu_rb_node, node); |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 155 | rb_erase(node, &handler->root); |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 156 | list_add(&rbnode->list, &del_list); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 157 | } |
Mitko Haralanov | 782f669 | 2016-04-12 10:46:35 -0700 | [diff] [blame] | 158 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 159 | |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 160 | do_remove(handler, &del_list); |
| 161 | |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 162 | kfree(handler); |
| 163 | } |
| 164 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 165 | int hfi1_mmu_rb_insert(struct mmu_rb_handler *handler, |
| 166 | struct mmu_rb_node *mnode) |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 167 | { |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 168 | struct mmu_rb_node *node; |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 169 | unsigned long flags; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 170 | int ret = 0; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 171 | |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 172 | spin_lock_irqsave(&handler->lock, flags); |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 173 | hfi1_cdbg(MMU, "Inserting node addr 0x%llx, len %u", mnode->addr, |
| 174 | mnode->len); |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 175 | node = __mmu_rb_search(handler, mnode->addr, mnode->len); |
| 176 | if (node) { |
| 177 | ret = -EINVAL; |
| 178 | goto unlock; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 179 | } |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 180 | __mmu_int_rb_insert(mnode, &handler->root); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 181 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 182 | ret = handler->ops->insert(handler->ops_arg, mnode); |
Dean Luick | c094664 | 2016-07-28 12:27:30 -0400 | [diff] [blame] | 183 | if (ret) |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 184 | __mmu_int_rb_remove(mnode, &handler->root); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 185 | unlock: |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 186 | spin_unlock_irqrestore(&handler->lock, flags); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 187 | return ret; |
| 188 | } |
| 189 | |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 190 | /* Caller must hold handler lock */ |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 191 | static struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler, |
| 192 | unsigned long addr, |
| 193 | unsigned long len) |
| 194 | { |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 195 | struct mmu_rb_node *node = NULL; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 196 | |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 197 | hfi1_cdbg(MMU, "Searching for addr 0x%llx, len %u", addr, len); |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 198 | if (!handler->ops->filter) { |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 199 | node = __mmu_int_rb_iter_first(&handler->root, addr, |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 200 | (addr + len) - 1); |
| 201 | } else { |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 202 | for (node = __mmu_int_rb_iter_first(&handler->root, addr, |
Mitko Haralanov | 0f310a00 | 2016-03-08 11:15:10 -0800 | [diff] [blame] | 203 | (addr + len) - 1); |
| 204 | node; |
| 205 | node = __mmu_int_rb_iter_next(node, addr, |
| 206 | (addr + len) - 1)) { |
| 207 | if (handler->ops->filter(node, addr, len)) |
| 208 | return node; |
| 209 | } |
| 210 | } |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 211 | return node; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 214 | struct mmu_rb_node *hfi1_mmu_rb_extract(struct mmu_rb_handler *handler, |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 215 | unsigned long addr, unsigned long len) |
| 216 | { |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 217 | struct mmu_rb_node *node; |
| 218 | unsigned long flags; |
| 219 | |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 220 | spin_lock_irqsave(&handler->lock, flags); |
| 221 | node = __mmu_rb_search(handler, addr, len); |
| 222 | if (node) |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 223 | __mmu_int_rb_remove(node, &handler->root); |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 224 | spin_unlock_irqrestore(&handler->lock, flags); |
| 225 | |
| 226 | return node; |
| 227 | } |
| 228 | |
Dean Luick | 1034599 | 2016-07-28 15:21:22 -0400 | [diff] [blame] | 229 | void hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg) |
| 230 | { |
| 231 | struct mmu_rb_node *rbnode; |
| 232 | struct rb_node *node, *next; |
| 233 | struct list_head del_list; |
| 234 | unsigned long flags; |
| 235 | bool stop = false; |
| 236 | |
| 237 | INIT_LIST_HEAD(&del_list); |
| 238 | |
| 239 | spin_lock_irqsave(&handler->lock, flags); |
| 240 | for (node = rb_first(&handler->root); node; node = next) { |
| 241 | next = rb_next(node); |
| 242 | rbnode = rb_entry(node, struct mmu_rb_node, node); |
| 243 | if (handler->ops->evict(handler->ops_arg, rbnode, evict_arg, |
| 244 | &stop)) { |
| 245 | __mmu_int_rb_remove(rbnode, &handler->root); |
| 246 | list_add(&rbnode->list, &del_list); |
| 247 | } |
| 248 | if (stop) |
| 249 | break; |
| 250 | } |
| 251 | spin_unlock_irqrestore(&handler->lock, flags); |
| 252 | |
Dean Luick | 1034599 | 2016-07-28 15:21:22 -0400 | [diff] [blame] | 253 | while (!list_empty(&del_list)) { |
| 254 | rbnode = list_first_entry(&del_list, struct mmu_rb_node, list); |
| 255 | list_del(&rbnode->list); |
| 256 | handler->ops->remove(handler->ops_arg, rbnode, |
| 257 | handler->mm); |
| 258 | } |
Dean Luick | 1034599 | 2016-07-28 15:21:22 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 261 | /* |
| 262 | * It is up to the caller to ensure that this function does not race with the |
| 263 | * mmu invalidate notifier which may be calling the users remove callback on |
| 264 | * 'node'. |
| 265 | */ |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 266 | void hfi1_mmu_rb_remove(struct mmu_rb_handler *handler, |
| 267 | struct mmu_rb_node *node) |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 268 | { |
Ira Weiny | 3c1091aa | 2016-07-28 12:27:31 -0400 | [diff] [blame] | 269 | unsigned long flags; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 270 | |
Ira Weiny | 3c1091aa | 2016-07-28 12:27:31 -0400 | [diff] [blame] | 271 | /* Validity of handler and node pointers has been checked by caller. */ |
| 272 | hfi1_cdbg(MMU, "Removing node addr 0x%llx, len %u", node->addr, |
| 273 | node->len); |
| 274 | spin_lock_irqsave(&handler->lock, flags); |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 275 | __mmu_int_rb_remove(node, &handler->root); |
Ira Weiny | 3c1091aa | 2016-07-28 12:27:31 -0400 | [diff] [blame] | 276 | spin_unlock_irqrestore(&handler->lock, flags); |
| 277 | |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 278 | handler->ops->remove(handler->ops_arg, node, NULL); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static inline void mmu_notifier_page(struct mmu_notifier *mn, |
| 282 | struct mm_struct *mm, unsigned long addr) |
| 283 | { |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 284 | mmu_notifier_mem_invalidate(mn, mm, addr, addr + PAGE_SIZE); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static inline void mmu_notifier_range_start(struct mmu_notifier *mn, |
| 288 | struct mm_struct *mm, |
| 289 | unsigned long start, |
| 290 | unsigned long end) |
| 291 | { |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 292 | mmu_notifier_mem_invalidate(mn, mm, start, end); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void mmu_notifier_mem_invalidate(struct mmu_notifier *mn, |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 296 | struct mm_struct *mm, |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 297 | unsigned long start, unsigned long end) |
| 298 | { |
| 299 | struct mmu_rb_handler *handler = |
| 300 | container_of(mn, struct mmu_rb_handler, mn); |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 301 | struct rb_root *root = &handler->root; |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 302 | struct mmu_rb_node *node, *ptr = NULL; |
Mitko Haralanov | df5a00f8 | 2016-03-08 11:14:53 -0800 | [diff] [blame] | 303 | unsigned long flags; |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 304 | bool added = false; |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 305 | |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 306 | spin_lock_irqsave(&handler->lock, flags); |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 307 | for (node = __mmu_int_rb_iter_first(root, start, end - 1); |
| 308 | node; node = ptr) { |
| 309 | /* Guard against node removal. */ |
| 310 | ptr = __mmu_int_rb_iter_next(node, start, end - 1); |
Mitko Haralanov | 353b71c | 2016-03-08 11:14:59 -0800 | [diff] [blame] | 311 | hfi1_cdbg(MMU, "Invalidating node addr 0x%llx, len %u", |
| 312 | node->addr, node->len); |
Dean Luick | e0b09ac | 2016-07-28 15:21:20 -0400 | [diff] [blame] | 313 | if (handler->ops->invalidate(handler->ops_arg, node)) { |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 314 | __mmu_int_rb_remove(node, root); |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 315 | list_add(&node->list, &handler->del_list); |
| 316 | added = true; |
Mitko Haralanov | de82bdf | 2016-04-12 10:46:03 -0700 | [diff] [blame] | 317 | } |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 318 | } |
Mitko Haralanov | c81e1f6 | 2016-03-08 11:14:25 -0800 | [diff] [blame] | 319 | spin_unlock_irqrestore(&handler->lock, flags); |
Dean Luick | b85ced9 | 2016-07-28 15:21:24 -0400 | [diff] [blame^] | 320 | |
| 321 | if (added) |
| 322 | queue_work(handler->wq, &handler->del_work); |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Call the remove function for the given handler and the list. This |
| 327 | * is expected to be called with a delete list extracted from handler. |
| 328 | * The caller should not be holding the handler lock. |
| 329 | */ |
| 330 | static void do_remove(struct mmu_rb_handler *handler, |
| 331 | struct list_head *del_list) |
| 332 | { |
| 333 | struct mmu_rb_node *node; |
| 334 | |
| 335 | while (!list_empty(del_list)) { |
| 336 | node = list_first_entry(del_list, struct mmu_rb_node, list); |
| 337 | list_del(&node->list); |
| 338 | handler->ops->remove(handler->ops_arg, node, handler->mm); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Work queue function to remove all nodes that have been queued up to |
| 344 | * be removed. The key feature is that mm->mmap_sem is not being held |
| 345 | * and the remove callback can sleep while taking it, if needed. |
| 346 | */ |
| 347 | static void handle_remove(struct work_struct *work) |
| 348 | { |
| 349 | struct mmu_rb_handler *handler = container_of(work, |
| 350 | struct mmu_rb_handler, |
| 351 | del_work); |
| 352 | struct list_head del_list; |
| 353 | unsigned long flags; |
| 354 | |
| 355 | /* remove anything that is queued to get removed */ |
| 356 | spin_lock_irqsave(&handler->lock, flags); |
| 357 | list_replace_init(&handler->del_list, &del_list); |
| 358 | spin_unlock_irqrestore(&handler->lock, flags); |
| 359 | |
| 360 | do_remove(handler, &del_list); |
Mitko Haralanov | 06e0ffa | 2016-03-08 11:14:20 -0800 | [diff] [blame] | 361 | } |