Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Modules tree lookup
0004  *
0005  * Copyright (C) 2015 Peter Zijlstra
0006  * Copyright (C) 2015 Rusty Russell
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/rbtree_latch.h>
0011 #include "internal.h"
0012 
0013 /*
0014  * Use a latched RB-tree for __module_address(); this allows us to use
0015  * RCU-sched lookups of the address from any context.
0016  *
0017  * This is conditional on PERF_EVENTS || TRACING because those can really hit
0018  * __module_address() hard by doing a lot of stack unwinding; potentially from
0019  * NMI context.
0020  */
0021 
0022 static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
0023 {
0024     struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
0025 
0026     return (unsigned long)layout->base;
0027 }
0028 
0029 static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
0030 {
0031     struct module_layout *layout = container_of(n, struct module_layout, mtn.node);
0032 
0033     return (unsigned long)layout->size;
0034 }
0035 
0036 static __always_inline bool
0037 mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
0038 {
0039     return __mod_tree_val(a) < __mod_tree_val(b);
0040 }
0041 
0042 static __always_inline int
0043 mod_tree_comp(void *key, struct latch_tree_node *n)
0044 {
0045     unsigned long val = (unsigned long)key;
0046     unsigned long start, end;
0047 
0048     start = __mod_tree_val(n);
0049     if (val < start)
0050         return -1;
0051 
0052     end = start + __mod_tree_size(n);
0053     if (val >= end)
0054         return 1;
0055 
0056     return 0;
0057 }
0058 
0059 static const struct latch_tree_ops mod_tree_ops = {
0060     .less = mod_tree_less,
0061     .comp = mod_tree_comp,
0062 };
0063 
0064 static noinline void __mod_tree_insert(struct mod_tree_node *node, struct mod_tree_root *tree)
0065 {
0066     latch_tree_insert(&node->node, &tree->root, &mod_tree_ops);
0067 }
0068 
0069 static void __mod_tree_remove(struct mod_tree_node *node, struct mod_tree_root *tree)
0070 {
0071     latch_tree_erase(&node->node, &tree->root, &mod_tree_ops);
0072 }
0073 
0074 /*
0075  * These modifications: insert, remove_init and remove; are serialized by the
0076  * module_mutex.
0077  */
0078 void mod_tree_insert(struct module *mod)
0079 {
0080     mod->core_layout.mtn.mod = mod;
0081     mod->init_layout.mtn.mod = mod;
0082 
0083     __mod_tree_insert(&mod->core_layout.mtn, &mod_tree);
0084     if (mod->init_layout.size)
0085         __mod_tree_insert(&mod->init_layout.mtn, &mod_tree);
0086 
0087 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
0088     mod->data_layout.mtn.mod = mod;
0089     __mod_tree_insert(&mod->data_layout.mtn, &mod_data_tree);
0090 #endif
0091 }
0092 
0093 void mod_tree_remove_init(struct module *mod)
0094 {
0095     if (mod->init_layout.size)
0096         __mod_tree_remove(&mod->init_layout.mtn, &mod_tree);
0097 }
0098 
0099 void mod_tree_remove(struct module *mod)
0100 {
0101     __mod_tree_remove(&mod->core_layout.mtn, &mod_tree);
0102     mod_tree_remove_init(mod);
0103 #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
0104     __mod_tree_remove(&mod->data_layout.mtn, &mod_data_tree);
0105 #endif
0106 }
0107 
0108 struct module *mod_find(unsigned long addr, struct mod_tree_root *tree)
0109 {
0110     struct latch_tree_node *ltn;
0111 
0112     ltn = latch_tree_find((void *)addr, &tree->root, &mod_tree_ops);
0113     if (!ltn)
0114         return NULL;
0115 
0116     return container_of(ltn, struct mod_tree_node, node)->mod;
0117 }