Skip to content

Commit 0ac0058

Browse files
committed
tracing/fprobe: Check the same type fprobe on table as the unregistered one
Commit 2c67dc4 ("tracing: fprobe: optimization for entry only case") introduced a different ftrace_ops for entry-only fprobes. However, when unregistering an fprobe, the kernel only checks if another fprobe exists at the same address, without checking which type of fprobe it is. If different fprobes are registered at the same address, the same address will be registered in both fgraph_ops and ftrace_ops, but only one of them will be deleted when unregistering. (the one removed first will not be deleted from the ops). This results in junk entries remaining in either fgraph_ops or ftrace_ops. For example: ======= cd /sys/kernel/tracing # 'Add entry and exit events on the same place' echo 'f:event1 vfs_read' >> dynamic_events echo 'f:event2 vfs_read%return' >> dynamic_events # 'Enable both of them' echo 1 > events/fprobes/enable cat enabled_functions vfs_read (2) ->arch_ftrace_ops_list_func+0x0/0x210 # 'Disable and remove exit event' echo 0 > events/fprobes/event2/enable echo -:event2 >> dynamic_events # 'Disable and remove all events' echo 0 > events/fprobes/enable echo > dynamic_events # 'Add another event' echo 'f:event3 vfs_open%return' > dynamic_events cat dynamic_events f:fprobes/event3 vfs_open%return echo 1 > events/fprobes/enable cat enabled_functions vfs_open (1) tramp: 0xffffffffa0001000 (ftrace_graph_func+0x0/0x60) ->ftrace_graph_func+0x0/0x60 subops: {ent:fprobe_fgraph_entry+0x0/0x620 ret:fprobe_return+0x0/0x150} vfs_read (1) tramp: 0xffffffffa0001000 (ftrace_graph_func+0x0/0x60) ->ftrace_graph_func+0x0/0x60 subops: {ent:fprobe_fgraph_entry+0x0/0x620 ret:fprobe_return+0x0/0x150} ======= As you can see, an entry for the vfs_read remains. To fix this issue, when unregistering, the kernel should also check if there is the same type of fprobes still exist at the same address, and if not, delete its entry from either fgraph_ops or ftrace_ops. Link: https://lore.kernel.org/all/177669367993.132053.10553046138528674802.stgit@mhiramat.tok.corp.google.com/ Fixes: 2c67dc4 ("tracing: fprobe: optimization for entry only case") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
1 parent aa72812 commit 0ac0058

1 file changed

Lines changed: 65 additions & 17 deletions

File tree

kernel/trace/fprobe.c

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,8 @@ static int insert_fprobe_node(struct fprobe_hlist_node *node, struct fprobe *fp)
9292
return ret;
9393
}
9494

95-
/* Return true if there are synonims */
96-
static bool delete_fprobe_node(struct fprobe_hlist_node *node)
95+
static void delete_fprobe_node(struct fprobe_hlist_node *node)
9796
{
98-
bool ret;
99-
10097
lockdep_assert_held(&fprobe_mutex);
10198

10299
/* Avoid double deleting and non-inserted nodes */
@@ -105,13 +102,6 @@ static bool delete_fprobe_node(struct fprobe_hlist_node *node)
105102
rhltable_remove(&fprobe_ip_table, &node->hlist,
106103
fprobe_rht_params);
107104
}
108-
109-
rcu_read_lock();
110-
ret = !!rhltable_lookup(&fprobe_ip_table, &node->addr,
111-
fprobe_rht_params);
112-
rcu_read_unlock();
113-
114-
return ret;
115105
}
116106

117107
/* Check existence of the fprobe */
@@ -343,6 +333,32 @@ static bool fprobe_is_ftrace(struct fprobe *fp)
343333
return !fp->exit_handler;
344334
}
345335

336+
static bool fprobe_exists_on_hash(unsigned long ip, bool ftrace)
337+
{
338+
struct rhlist_head *head, *pos;
339+
struct fprobe_hlist_node *node;
340+
struct fprobe *fp;
341+
342+
guard(rcu)();
343+
head = rhltable_lookup(&fprobe_ip_table, &ip,
344+
fprobe_rht_params);
345+
if (!head)
346+
return false;
347+
/* We have to check the same type on the list. */
348+
rhl_for_each_entry_rcu(node, pos, head, hlist) {
349+
if (node->addr != ip)
350+
break;
351+
fp = READ_ONCE(node->fp);
352+
if (likely(fp)) {
353+
if ((!ftrace && fp->exit_handler) ||
354+
(ftrace && !fp->exit_handler))
355+
return true;
356+
}
357+
}
358+
359+
return false;
360+
}
361+
346362
#ifdef CONFIG_MODULES
347363
static void fprobe_remove_ips(unsigned long *ips, unsigned int cnt)
348364
{
@@ -365,6 +381,29 @@ static bool fprobe_is_ftrace(struct fprobe *fp)
365381
return false;
366382
}
367383

384+
static bool fprobe_exists_on_hash(unsigned long ip, bool ftrace __maybe_unused)
385+
{
386+
struct rhlist_head *head, *pos;
387+
struct fprobe_hlist_node *node;
388+
struct fprobe *fp;
389+
390+
guard(rcu)();
391+
head = rhltable_lookup(&fprobe_ip_table, &ip,
392+
fprobe_rht_params);
393+
if (!head)
394+
return false;
395+
/* We only need to check fp is there. */
396+
rhl_for_each_entry_rcu(node, pos, head, hlist) {
397+
if (node->addr != ip)
398+
break;
399+
fp = READ_ONCE(node->fp);
400+
if (likely(fp))
401+
return true;
402+
}
403+
404+
return false;
405+
}
406+
368407
#ifdef CONFIG_MODULES
369408
static void fprobe_remove_ips(unsigned long *ips, unsigned int cnt)
370409
{
@@ -551,18 +590,25 @@ struct fprobe_addr_list {
551590
static int fprobe_remove_node_in_module(struct module *mod, struct fprobe_hlist_node *node,
552591
struct fprobe_addr_list *alist)
553592
{
593+
lockdep_assert_in_rcu_read_lock();
594+
554595
if (!within_module(node->addr, mod))
555596
return 0;
556597

557-
if (delete_fprobe_node(node))
558-
return 0;
598+
delete_fprobe_node(node);
559599
/* If no address list is available, we can't track this address. */
560600
if (!alist->addrs)
561601
return 0;
602+
/*
603+
* Don't care the type here, because all fprobes on the same
604+
* address must be removed eventually.
605+
*/
606+
if (!rhltable_lookup(&fprobe_ip_table, &node->addr, fprobe_rht_params)) {
607+
alist->addrs[alist->index++] = node->addr;
608+
if (alist->index == alist->size)
609+
return -ENOSPC;
610+
}
562611

563-
alist->addrs[alist->index++] = node->addr;
564-
if (alist->index == alist->size)
565-
return -ENOSPC;
566612
return 0;
567613
}
568614

@@ -933,7 +979,9 @@ static int unregister_fprobe_nolock(struct fprobe *fp)
933979
/* Remove non-synonim ips from table and hash */
934980
count = 0;
935981
for (i = 0; i < hlist_array->size; i++) {
936-
if (!delete_fprobe_node(&hlist_array->array[i]) && addrs)
982+
delete_fprobe_node(&hlist_array->array[i]);
983+
if (addrs && !fprobe_exists_on_hash(hlist_array->array[i].addr,
984+
fprobe_is_ftrace(fp)))
937985
addrs[count++] = hlist_array->array[i].addr;
938986
}
939987
del_fprobe_hash(fp);

0 commit comments

Comments
 (0)