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