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