/* * fprobe_table: hold 'fprobe_hlist::hlist' for checking the fprobe still * exists. The key is the address of fprobe instance. * fprobe_ip_table: hold 'fprobe_hlist::array[*]' for searching the fprobe * instance related to the funciton address. The key is the ftrace IP * address. * * When unregistering the fprobe, fprobe_hlist::fp and fprobe_hlist::array[*].fp * are set NULL and delete those from both hash tables (by hlist_del_rcu). * After an RCU grace period, the fprobe_hlist itself will be released. * * fprobe_table and fprobe_ip_table can be accessed from either * - Normal hlist traversal and RCU add/del under 'fprobe_mutex' is held. * - RCU hlist traversal under disabling preempt
*/ staticstruct hlist_head fprobe_table[FPROBE_TABLE_SIZE]; staticstruct hlist_head fprobe_ip_table[FPROBE_IP_TABLE_SIZE]; static DEFINE_MUTEX(fprobe_mutex);
/* * Find first fprobe in the hlist. It will be iterated twice in the entry * probe, once for correcting the total required size, the second time is * calling back the user handlers. * Thus the hlist in the fprobe_table must be sorted and new probe needs to * be added *before* the first fprobe.
*/ staticstruct fprobe_hlist_node *find_first_fprobe_node(unsignedlong ip)
{ struct fprobe_hlist_node *node; struct hlist_head *head;
/* * fprobe shadow stack management: * Since fprobe shares a single fgraph_ops, it needs to share the stack entry * among the probes on the same function exit. Note that a new probe can be * registered before a target function is returning, we can not use the hash * table to find the corresponding probes. Thus the probe address is stored on * the shadow stack with its entry data size. *
*/ staticinlineint __fprobe_handler(unsignedlong ip, unsignedlong parent_ip, struct fprobe *fp, struct ftrace_regs *fregs, void *data)
{ if (!fp->entry_handler) return 0;
staticinlineint __fprobe_kprobe_handler(unsignedlong ip, unsignedlong parent_ip, struct fprobe *fp, struct ftrace_regs *fregs, void *data)
{ int ret; /* * This user handler is shared with other kprobes and is not expected to be * called recursively. So if any other kprobe handler is running, this will * exit as kprobe does. See the section 'Share the callbacks with kprobes' * in Documentation/trace/fprobe.rst for more information.
*/ if (unlikely(kprobe_running())) {
fp->nmissed++; return 0;
}
first = node = find_first_fprobe_node(func); if (unlikely(!first)) return 0;
reserved_words = 0;
hlist_for_each_entry_from_rcu(node, hlist) { if (node->addr != func) break;
fp = READ_ONCE(node->fp); if (!fp || !fp->exit_handler) continue; /* * Since fprobe can be enabled until the next loop, we ignore the * fprobe's disabled flag in this loop.
*/
reserved_words +=
FPROBE_HEADER_SIZE_IN_LONG + SIZE_IN_LONG(fp->entry_data_size);
}
node = first; if (reserved_words) {
fgraph_data = fgraph_reserve_data(gops->idx, reserved_words * sizeof(long)); if (unlikely(!fgraph_data)) {
hlist_for_each_entry_from_rcu(node, hlist) { if (node->addr != func) break;
fp = READ_ONCE(node->fp); if (fp && !fprobe_disabled(fp))
fp->nmissed++;
} return 0;
}
}
/* * TODO: recursion detection has been done in the fgraph. Thus we need * to add a callback to increment missed counter.
*/
ret_ip = ftrace_regs_get_return_address(fregs);
used = 0;
hlist_for_each_entry_from_rcu(node, hlist) { int data_size; void *data;
if (node->addr != func) break;
fp = READ_ONCE(node->fp); if (!fp || fprobe_disabled(fp)) continue;
data_size = fp->entry_data_size; if (data_size && fp->exit_handler)
data = fgraph_data + used + FPROBE_HEADER_SIZE_IN_LONG; else
data = NULL;
if (fprobe_shared_with_kprobes(fp))
ret = __fprobe_kprobe_handler(func, ret_ip, fp, fregs, data); else
ret = __fprobe_handler(func, ret_ip, fp, fregs, data);
/* If entry_handler returns !0, nmissed is not counted but skips exit_handler. */ if (!ret && fp->exit_handler) { int size_words = SIZE_IN_LONG(data_size);
if (write_fprobe_header(&fgraph_data[used], fp, size_words))
used += FPROBE_HEADER_SIZE_IN_LONG + size_words;
}
} if (used < reserved_words)
memset(fgraph_data + used, 0, reserved_words - used);
/* If any exit_handler is set, data must be used. */ return used != 0;
}
NOKPROBE_SYMBOL(fprobe_entry);
/* Add @addrs to the ftrace filter and register fgraph if needed. */ staticint fprobe_graph_add_ips(unsignedlong *addrs, int num)
{ int ret;
lockdep_assert_held(&fprobe_mutex);
ret = ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 0, 0); if (ret) return ret;
if (!fprobe_graph_active) {
ret = register_ftrace_graph(&fprobe_graph_ops); if (WARN_ON_ONCE(ret)) {
ftrace_free_filter(&fprobe_graph_ops.ops); return ret;
}
}
fprobe_graph_active++; return 0;
}
/* Remove @addrs from the ftrace filter and unregister fgraph if possible. */ staticvoid fprobe_graph_remove_ips(unsignedlong *addrs, int num)
{
lockdep_assert_held(&fprobe_mutex);
fprobe_graph_active--; /* Q: should we unregister it ? */ if (!fprobe_graph_active)
unregister_ftrace_graph(&fprobe_graph_ops);
if (num)
ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0);
}
#ifdef CONFIG_MODULES
#define FPROBE_IPS_BATCH_INIT 8 /* instruction pointer address list */ struct fprobe_addr_list { int index; int size; unsignedlong *addrs;
};
staticvoid fprobe_remove_node_in_module(struct module *mod, struct hlist_head *head, struct fprobe_addr_list *alist)
{ struct fprobe_hlist_node *node; int ret = 0;
hlist_for_each_entry_rcu(node, head, hlist,
lockdep_is_held(&fprobe_mutex)) { if (!within_module(node->addr, mod)) continue; if (delete_fprobe_node(node)) continue; /* * If failed to update alist, just continue to update hlist. * Therefore, at list user handler will not hit anymore.
*/ if (!ret)
ret = fprobe_addr_list_add(alist, node->addr);
}
}
if (val != MODULE_STATE_GOING) return NOTIFY_DONE;
alist.addrs = kcalloc(alist.size, sizeof(*alist.addrs), GFP_KERNEL); /* If failed to alloc memory, we can not remove ips from hash. */ if (!alist.addrs) return NOTIFY_DONE;
mutex_lock(&fprobe_mutex); for (i = 0; i < FPROBE_IP_TABLE_SIZE; i++)
fprobe_remove_node_in_module(mod, &fprobe_ip_table[i], &alist);
if (alist.index > 0)
ftrace_set_filter_ips(&fprobe_graph_ops.ops,
alist.addrs, alist.index, 1, 0);
mutex_unlock(&fprobe_mutex);
/* * Make IP list from the filter/no-filter glob patterns. * Return the number of matched symbols, or errno. * If @addrs == NULL, this just counts the number of matched symbols. If @addrs * is passed with an array, we need to pass the an @mods array of the same size * to increment the module refcount for each symbol. * This means we also need to call `module_put` for each element of @mods after * using the @addrs.
*/ staticint get_ips_from_filter(constchar *filter, constchar *notfilter, unsignedlong *addrs, struct module **mods,
size_t size)
{ struct filter_match_data match = { .filter = filter, .notfilter = notfilter,
.index = 0, .size = size, .addrs = addrs, .mods = mods}; int ret;
if (addrs && !mods) return -EINVAL;
ret = kallsyms_on_each_symbol(filter_match_callback, &match); if (ret < 0) return ret; if (IS_ENABLED(CONFIG_MODULES)) {
ret = module_kallsyms_on_each_symbol(NULL, filter_match_callback, &match); if (ret < 0) return ret;
}
/** * register_fprobe() - Register fprobe to ftrace by pattern. * @fp: A fprobe data structure to be registered. * @filter: A wildcard pattern of probed symbols. * @notfilter: A wildcard pattern of NOT probed symbols. * * Register @fp to ftrace for enabling the probe on the symbols matched to @filter. * If @notfilter is not NULL, the symbols matched the @notfilter are not probed. * * Return 0 if @fp is registered successfully, -errno if not.
*/ int register_fprobe(struct fprobe *fp, constchar *filter, constchar *notfilter)
{ unsignedlong *addrs __free(kfree) = NULL; struct module **mods __free(kfree) = NULL; int ret, num;
if (!fp || !filter) return -EINVAL;
num = get_ips_from_filter(filter, notfilter, NULL, NULL, FPROBE_IPS_MAX); if (num < 0) return num;
addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL); if (!addrs) return -ENOMEM;
mods = kcalloc(num, sizeof(*mods), GFP_KERNEL); if (!mods) return -ENOMEM;
ret = get_ips_from_filter(filter, notfilter, addrs, mods, num); if (ret < 0) return ret;
ret = register_fprobe_ips(fp, addrs, ret);
for (int i = 0; i < num; i++) { if (mods[i])
module_put(mods[i]);
} return ret;
}
EXPORT_SYMBOL_GPL(register_fprobe);
/** * register_fprobe_ips() - Register fprobe to ftrace by address. * @fp: A fprobe data structure to be registered. * @addrs: An array of target function address. * @num: The number of entries of @addrs. * * Register @fp to ftrace for enabling the probe on the address given by @addrs. * The @addrs must be the addresses of ftrace location address, which may be * the symbol address + arch-dependent offset. * If you unsure what this mean, please use other registration functions. * * Return 0 if @fp is registered successfully, -errno if not.
*/ int register_fprobe_ips(struct fprobe *fp, unsignedlong *addrs, int num)
{ struct fprobe_hlist *hlist_array; int ret, i;
ret = fprobe_init(fp, addrs, num); if (ret) return ret;
mutex_lock(&fprobe_mutex);
hlist_array = fp->hlist_array;
ret = fprobe_graph_add_ips(addrs, num); if (!ret) {
add_fprobe_hash(fp); for (i = 0; i < hlist_array->size; i++)
insert_fprobe_node(&hlist_array->array[i]);
}
mutex_unlock(&fprobe_mutex);
/** * register_fprobe_syms() - Register fprobe to ftrace by symbols. * @fp: A fprobe data structure to be registered. * @syms: An array of target symbols. * @num: The number of entries of @syms. * * Register @fp to the symbols given by @syms array. This will be useful if * you are sure the symbols exist in the kernel. * * Return 0 if @fp is registered successfully, -errno if not.
*/ int register_fprobe_syms(struct fprobe *fp, constchar **syms, int num)
{ unsignedlong *addrs; int ret;
if (!fp || !syms || num <= 0) return -EINVAL;
addrs = get_ftrace_locations(syms, num); if (IS_ERR(addrs)) return PTR_ERR(addrs);
/** * unregister_fprobe() - Unregister fprobe. * @fp: A fprobe data structure to be unregistered. * * Unregister fprobe (and remove ftrace hooks from the function entries). * * Return 0 if @fp is unregistered successfully, -errno if not.
*/ int unregister_fprobe(struct fprobe *fp)
{ struct fprobe_hlist *hlist_array; unsignedlong *addrs = NULL; int ret = 0, i, count;
mutex_lock(&fprobe_mutex); if (!fp || !is_fprobe_still_exist(fp)) {
ret = -EINVAL; goto out;
}
hlist_array = fp->hlist_array;
addrs = kcalloc(hlist_array->size, sizeof(unsignedlong), GFP_KERNEL); if (!addrs) {
ret = -ENOMEM; /* TODO: Fallback to one-by-one loop */ goto out;
}
/* Remove non-synonim ips from table and hash */
count = 0; for (i = 0; i < hlist_array->size; i++) { if (!delete_fprobe_node(&hlist_array->array[i]))
addrs[count++] = hlist_array->array[i].addr;
}
del_fprobe_hash(fp);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.