18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright(c) 2020 Cornelis Networks, Inc. 38c2ecf20Sopenharmony_ci * Copyright(c) 2016 - 2017 Intel Corporation. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license. When using or 68c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify 118c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as 128c2ecf20Sopenharmony_ci * published by the Free Software Foundation. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but 158c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of 168c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 178c2ecf20Sopenharmony_ci * General Public License for more details. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * BSD LICENSE 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 228c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions 238c2ecf20Sopenharmony_ci * are met: 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above copyright 268c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 278c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above copyright 288c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer in 298c2ecf20Sopenharmony_ci * the documentation and/or other materials provided with the 308c2ecf20Sopenharmony_ci * distribution. 318c2ecf20Sopenharmony_ci * - Neither the name of Intel Corporation nor the names of its 328c2ecf20Sopenharmony_ci * contributors may be used to endorse or promote products derived 338c2ecf20Sopenharmony_ci * from this software without specific prior written permission. 348c2ecf20Sopenharmony_ci * 358c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 368c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 378c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 388c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 398c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 408c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 418c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 428c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 438c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 448c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 458c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 468c2ecf20Sopenharmony_ci * 478c2ecf20Sopenharmony_ci */ 488c2ecf20Sopenharmony_ci#include <linux/list.h> 498c2ecf20Sopenharmony_ci#include <linux/rculist.h> 508c2ecf20Sopenharmony_ci#include <linux/mmu_notifier.h> 518c2ecf20Sopenharmony_ci#include <linux/interval_tree_generic.h> 528c2ecf20Sopenharmony_ci#include <linux/sched/mm.h> 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#include "mmu_rb.h" 558c2ecf20Sopenharmony_ci#include "trace.h" 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic unsigned long mmu_node_start(struct mmu_rb_node *); 588c2ecf20Sopenharmony_cistatic unsigned long mmu_node_last(struct mmu_rb_node *); 598c2ecf20Sopenharmony_cistatic int mmu_notifier_range_start(struct mmu_notifier *, 608c2ecf20Sopenharmony_ci const struct mmu_notifier_range *); 618c2ecf20Sopenharmony_cistatic struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *, 628c2ecf20Sopenharmony_ci unsigned long, unsigned long); 638c2ecf20Sopenharmony_cistatic void release_immediate(struct kref *refcount); 648c2ecf20Sopenharmony_cistatic void handle_remove(struct work_struct *work); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic const struct mmu_notifier_ops mn_opts = { 678c2ecf20Sopenharmony_ci .invalidate_range_start = mmu_notifier_range_start, 688c2ecf20Sopenharmony_ci}; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ciINTERVAL_TREE_DEFINE(struct mmu_rb_node, node, unsigned long, __last, 718c2ecf20Sopenharmony_ci mmu_node_start, mmu_node_last, static, __mmu_int_rb); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic unsigned long mmu_node_start(struct mmu_rb_node *node) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci return node->addr & PAGE_MASK; 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic unsigned long mmu_node_last(struct mmu_rb_node *node) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci return PAGE_ALIGN(node->addr + node->len) - 1; 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ciint hfi1_mmu_rb_register(void *ops_arg, 848c2ecf20Sopenharmony_ci struct mmu_rb_ops *ops, 858c2ecf20Sopenharmony_ci struct workqueue_struct *wq, 868c2ecf20Sopenharmony_ci struct mmu_rb_handler **handler) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci struct mmu_rb_handler *h; 898c2ecf20Sopenharmony_ci int ret; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci h = kzalloc(sizeof(*h), GFP_KERNEL); 928c2ecf20Sopenharmony_ci if (!h) 938c2ecf20Sopenharmony_ci return -ENOMEM; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci h->root = RB_ROOT_CACHED; 968c2ecf20Sopenharmony_ci h->ops = ops; 978c2ecf20Sopenharmony_ci h->ops_arg = ops_arg; 988c2ecf20Sopenharmony_ci INIT_HLIST_NODE(&h->mn.hlist); 998c2ecf20Sopenharmony_ci spin_lock_init(&h->lock); 1008c2ecf20Sopenharmony_ci h->mn.ops = &mn_opts; 1018c2ecf20Sopenharmony_ci INIT_WORK(&h->del_work, handle_remove); 1028c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&h->del_list); 1038c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&h->lru_list); 1048c2ecf20Sopenharmony_ci h->wq = wq; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci ret = mmu_notifier_register(&h->mn, current->mm); 1078c2ecf20Sopenharmony_ci if (ret) { 1088c2ecf20Sopenharmony_ci kfree(h); 1098c2ecf20Sopenharmony_ci return ret; 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci *handler = h; 1138c2ecf20Sopenharmony_ci return 0; 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_civoid hfi1_mmu_rb_unregister(struct mmu_rb_handler *handler) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci struct mmu_rb_node *rbnode; 1198c2ecf20Sopenharmony_ci struct rb_node *node; 1208c2ecf20Sopenharmony_ci unsigned long flags; 1218c2ecf20Sopenharmony_ci struct list_head del_list; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci /* Prevent freeing of mm until we are completely finished. */ 1248c2ecf20Sopenharmony_ci mmgrab(handler->mn.mm); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* Unregister first so we don't get any more notifications. */ 1278c2ecf20Sopenharmony_ci mmu_notifier_unregister(&handler->mn, handler->mn.mm); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci /* 1308c2ecf20Sopenharmony_ci * Make sure the wq delete handler is finished running. It will not 1318c2ecf20Sopenharmony_ci * be triggered once the mmu notifiers are unregistered above. 1328c2ecf20Sopenharmony_ci */ 1338c2ecf20Sopenharmony_ci flush_work(&handler->del_work); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&del_list); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci spin_lock_irqsave(&handler->lock, flags); 1388c2ecf20Sopenharmony_ci while ((node = rb_first_cached(&handler->root))) { 1398c2ecf20Sopenharmony_ci rbnode = rb_entry(node, struct mmu_rb_node, node); 1408c2ecf20Sopenharmony_ci rb_erase_cached(node, &handler->root); 1418c2ecf20Sopenharmony_ci /* move from LRU list to delete list */ 1428c2ecf20Sopenharmony_ci list_move(&rbnode->list, &del_list); 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&handler->lock, flags); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci while (!list_empty(&del_list)) { 1478c2ecf20Sopenharmony_ci rbnode = list_first_entry(&del_list, struct mmu_rb_node, list); 1488c2ecf20Sopenharmony_ci list_del(&rbnode->list); 1498c2ecf20Sopenharmony_ci kref_put(&rbnode->refcount, release_immediate); 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci /* Now the mm may be freed. */ 1538c2ecf20Sopenharmony_ci mmdrop(handler->mn.mm); 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci kfree(handler); 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ciint hfi1_mmu_rb_insert(struct mmu_rb_handler *handler, 1598c2ecf20Sopenharmony_ci struct mmu_rb_node *mnode) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci struct mmu_rb_node *node; 1628c2ecf20Sopenharmony_ci unsigned long flags; 1638c2ecf20Sopenharmony_ci int ret = 0; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci trace_hfi1_mmu_rb_insert(mnode->addr, mnode->len); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci if (current->mm != handler->mn.mm) 1688c2ecf20Sopenharmony_ci return -EPERM; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci spin_lock_irqsave(&handler->lock, flags); 1718c2ecf20Sopenharmony_ci node = __mmu_rb_search(handler, mnode->addr, mnode->len); 1728c2ecf20Sopenharmony_ci if (node) { 1738c2ecf20Sopenharmony_ci ret = -EEXIST; 1748c2ecf20Sopenharmony_ci goto unlock; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci __mmu_int_rb_insert(mnode, &handler->root); 1778c2ecf20Sopenharmony_ci list_add_tail(&mnode->list, &handler->lru_list); 1788c2ecf20Sopenharmony_ci mnode->handler = handler; 1798c2ecf20Sopenharmony_ciunlock: 1808c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&handler->lock, flags); 1818c2ecf20Sopenharmony_ci return ret; 1828c2ecf20Sopenharmony_ci} 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci/* Caller must hold handler lock */ 1858c2ecf20Sopenharmony_cistruct mmu_rb_node *hfi1_mmu_rb_get_first(struct mmu_rb_handler *handler, 1868c2ecf20Sopenharmony_ci unsigned long addr, unsigned long len) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci struct mmu_rb_node *node; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci trace_hfi1_mmu_rb_search(addr, len); 1918c2ecf20Sopenharmony_ci node = __mmu_int_rb_iter_first(&handler->root, addr, (addr + len) - 1); 1928c2ecf20Sopenharmony_ci if (node) 1938c2ecf20Sopenharmony_ci list_move_tail(&node->list, &handler->lru_list); 1948c2ecf20Sopenharmony_ci return node; 1958c2ecf20Sopenharmony_ci} 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci/* Caller must hold handler lock */ 1988c2ecf20Sopenharmony_cistatic struct mmu_rb_node *__mmu_rb_search(struct mmu_rb_handler *handler, 1998c2ecf20Sopenharmony_ci unsigned long addr, 2008c2ecf20Sopenharmony_ci unsigned long len) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci struct mmu_rb_node *node = NULL; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci trace_hfi1_mmu_rb_search(addr, len); 2058c2ecf20Sopenharmony_ci if (!handler->ops->filter) { 2068c2ecf20Sopenharmony_ci node = __mmu_int_rb_iter_first(&handler->root, addr, 2078c2ecf20Sopenharmony_ci (addr + len) - 1); 2088c2ecf20Sopenharmony_ci } else { 2098c2ecf20Sopenharmony_ci for (node = __mmu_int_rb_iter_first(&handler->root, addr, 2108c2ecf20Sopenharmony_ci (addr + len) - 1); 2118c2ecf20Sopenharmony_ci node; 2128c2ecf20Sopenharmony_ci node = __mmu_int_rb_iter_next(node, addr, 2138c2ecf20Sopenharmony_ci (addr + len) - 1)) { 2148c2ecf20Sopenharmony_ci if (handler->ops->filter(node, addr, len)) 2158c2ecf20Sopenharmony_ci return node; 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci } 2188c2ecf20Sopenharmony_ci return node; 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci/* 2228c2ecf20Sopenharmony_ci * Must NOT call while holding mnode->handler->lock. 2238c2ecf20Sopenharmony_ci * mnode->handler->ops->remove() may sleep and mnode->handler->lock is a 2248c2ecf20Sopenharmony_ci * spinlock. 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_cistatic void release_immediate(struct kref *refcount) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci struct mmu_rb_node *mnode = 2298c2ecf20Sopenharmony_ci container_of(refcount, struct mmu_rb_node, refcount); 2308c2ecf20Sopenharmony_ci mnode->handler->ops->remove(mnode->handler->ops_arg, mnode); 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci/* Caller must hold mnode->handler->lock */ 2348c2ecf20Sopenharmony_cistatic void release_nolock(struct kref *refcount) 2358c2ecf20Sopenharmony_ci{ 2368c2ecf20Sopenharmony_ci struct mmu_rb_node *mnode = 2378c2ecf20Sopenharmony_ci container_of(refcount, struct mmu_rb_node, refcount); 2388c2ecf20Sopenharmony_ci list_move(&mnode->list, &mnode->handler->del_list); 2398c2ecf20Sopenharmony_ci queue_work(mnode->handler->wq, &mnode->handler->del_work); 2408c2ecf20Sopenharmony_ci} 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci/* 2438c2ecf20Sopenharmony_ci * struct mmu_rb_node->refcount kref_put() callback. 2448c2ecf20Sopenharmony_ci * Adds mmu_rb_node to mmu_rb_node->handler->del_list and queues 2458c2ecf20Sopenharmony_ci * handler->del_work on handler->wq. 2468c2ecf20Sopenharmony_ci * Does not remove mmu_rb_node from handler->lru_list or handler->rb_root. 2478c2ecf20Sopenharmony_ci * Acquires mmu_rb_node->handler->lock; do not call while already holding 2488c2ecf20Sopenharmony_ci * handler->lock. 2498c2ecf20Sopenharmony_ci */ 2508c2ecf20Sopenharmony_civoid hfi1_mmu_rb_release(struct kref *refcount) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci struct mmu_rb_node *mnode = 2538c2ecf20Sopenharmony_ci container_of(refcount, struct mmu_rb_node, refcount); 2548c2ecf20Sopenharmony_ci struct mmu_rb_handler *handler = mnode->handler; 2558c2ecf20Sopenharmony_ci unsigned long flags; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci spin_lock_irqsave(&handler->lock, flags); 2588c2ecf20Sopenharmony_ci list_move(&mnode->list, &mnode->handler->del_list); 2598c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&handler->lock, flags); 2608c2ecf20Sopenharmony_ci queue_work(handler->wq, &handler->del_work); 2618c2ecf20Sopenharmony_ci} 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_civoid hfi1_mmu_rb_evict(struct mmu_rb_handler *handler, void *evict_arg) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci struct mmu_rb_node *rbnode, *ptr; 2668c2ecf20Sopenharmony_ci struct list_head del_list; 2678c2ecf20Sopenharmony_ci unsigned long flags; 2688c2ecf20Sopenharmony_ci bool stop = false; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci if (current->mm != handler->mn.mm) 2718c2ecf20Sopenharmony_ci return; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&del_list); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci spin_lock_irqsave(&handler->lock, flags); 2768c2ecf20Sopenharmony_ci list_for_each_entry_safe(rbnode, ptr, &handler->lru_list, list) { 2778c2ecf20Sopenharmony_ci /* refcount == 1 implies mmu_rb_handler has only rbnode ref */ 2788c2ecf20Sopenharmony_ci if (kref_read(&rbnode->refcount) > 1) 2798c2ecf20Sopenharmony_ci continue; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci if (handler->ops->evict(handler->ops_arg, rbnode, evict_arg, 2828c2ecf20Sopenharmony_ci &stop)) { 2838c2ecf20Sopenharmony_ci __mmu_int_rb_remove(rbnode, &handler->root); 2848c2ecf20Sopenharmony_ci /* move from LRU list to delete list */ 2858c2ecf20Sopenharmony_ci list_move(&rbnode->list, &del_list); 2868c2ecf20Sopenharmony_ci } 2878c2ecf20Sopenharmony_ci if (stop) 2888c2ecf20Sopenharmony_ci break; 2898c2ecf20Sopenharmony_ci } 2908c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&handler->lock, flags); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci list_for_each_entry_safe(rbnode, ptr, &del_list, list) { 2938c2ecf20Sopenharmony_ci kref_put(&rbnode->refcount, release_immediate); 2948c2ecf20Sopenharmony_ci } 2958c2ecf20Sopenharmony_ci} 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistatic int mmu_notifier_range_start(struct mmu_notifier *mn, 2988c2ecf20Sopenharmony_ci const struct mmu_notifier_range *range) 2998c2ecf20Sopenharmony_ci{ 3008c2ecf20Sopenharmony_ci struct mmu_rb_handler *handler = 3018c2ecf20Sopenharmony_ci container_of(mn, struct mmu_rb_handler, mn); 3028c2ecf20Sopenharmony_ci struct rb_root_cached *root = &handler->root; 3038c2ecf20Sopenharmony_ci struct mmu_rb_node *node, *ptr = NULL; 3048c2ecf20Sopenharmony_ci unsigned long flags; 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci spin_lock_irqsave(&handler->lock, flags); 3078c2ecf20Sopenharmony_ci for (node = __mmu_int_rb_iter_first(root, range->start, range->end-1); 3088c2ecf20Sopenharmony_ci node; node = ptr) { 3098c2ecf20Sopenharmony_ci /* Guard against node removal. */ 3108c2ecf20Sopenharmony_ci ptr = __mmu_int_rb_iter_next(node, range->start, 3118c2ecf20Sopenharmony_ci range->end - 1); 3128c2ecf20Sopenharmony_ci trace_hfi1_mmu_mem_invalidate(node->addr, node->len); 3138c2ecf20Sopenharmony_ci /* Remove from rb tree and lru_list. */ 3148c2ecf20Sopenharmony_ci __mmu_int_rb_remove(node, root); 3158c2ecf20Sopenharmony_ci list_del_init(&node->list); 3168c2ecf20Sopenharmony_ci kref_put(&node->refcount, release_nolock); 3178c2ecf20Sopenharmony_ci } 3188c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&handler->lock, flags); 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci return 0; 3218c2ecf20Sopenharmony_ci} 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci/* 3248c2ecf20Sopenharmony_ci * Work queue function to remove all nodes that have been queued up to 3258c2ecf20Sopenharmony_ci * be removed. The key feature is that mm->mmap_lock is not being held 3268c2ecf20Sopenharmony_ci * and the remove callback can sleep while taking it, if needed. 3278c2ecf20Sopenharmony_ci */ 3288c2ecf20Sopenharmony_cistatic void handle_remove(struct work_struct *work) 3298c2ecf20Sopenharmony_ci{ 3308c2ecf20Sopenharmony_ci struct mmu_rb_handler *handler = container_of(work, 3318c2ecf20Sopenharmony_ci struct mmu_rb_handler, 3328c2ecf20Sopenharmony_ci del_work); 3338c2ecf20Sopenharmony_ci struct list_head del_list; 3348c2ecf20Sopenharmony_ci unsigned long flags; 3358c2ecf20Sopenharmony_ci struct mmu_rb_node *node; 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci /* remove anything that is queued to get removed */ 3388c2ecf20Sopenharmony_ci spin_lock_irqsave(&handler->lock, flags); 3398c2ecf20Sopenharmony_ci list_replace_init(&handler->del_list, &del_list); 3408c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&handler->lock, flags); 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci while (!list_empty(&del_list)) { 3438c2ecf20Sopenharmony_ci node = list_first_entry(&del_list, struct mmu_rb_node, list); 3448c2ecf20Sopenharmony_ci list_del(&node->list); 3458c2ecf20Sopenharmony_ci handler->ops->remove(handler->ops_arg, node); 3468c2ecf20Sopenharmony_ci } 3478c2ecf20Sopenharmony_ci} 348