Skip to content

Commit 03e8e8a

Browse files
mhiramatgregkh
authored andcommitted
tracing/fprobe: Unregister fprobe even if memory allocation fails
commit 1aec9e5 upstream. unregister_fprobe() can fail under memory pressure because of memory allocation failure, but this maybe called from module unloading, and usually there is no way to retry it. Moreover. trace_fprobe does not check the return value. To fix this problem, unregister fprobe and fprobe_hash_node even if working memory allocation fails. Anyway, if the last fprobe is removed, the filter will be freed. Link: https://lore.kernel.org/all/177669365629.132053.8433032896213721288.stgit@mhiramat.tok.corp.google.com/ Fixes: 4346ba1 ("fprobe: Rewrite fprobe on function-graph tracer") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 0d90063 commit 03e8e8a

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

kernel/trace/fprobe.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,10 @@ static void fprobe_ftrace_remove_ips(unsigned long *addrs, int num)
331331
lockdep_assert_held(&fprobe_mutex);
332332

333333
fprobe_ftrace_active--;
334-
if (!fprobe_ftrace_active)
334+
if (!fprobe_ftrace_active) {
335335
unregister_ftrace_function(&fprobe_ftrace_ops);
336-
if (num)
336+
ftrace_free_filter(&fprobe_ftrace_ops);
337+
} else if (num)
337338
ftrace_set_filter_ips(&fprobe_ftrace_ops, addrs, num, 1, 0);
338339
}
339340

@@ -532,10 +533,10 @@ static void fprobe_graph_remove_ips(unsigned long *addrs, int num)
532533

533534
fprobe_graph_active--;
534535
/* Q: should we unregister it ? */
535-
if (!fprobe_graph_active)
536+
if (!fprobe_graph_active) {
536537
unregister_ftrace_graph(&fprobe_graph_ops);
537-
538-
if (num)
538+
ftrace_free_filter(&fprobe_graph_ops.ops);
539+
} else if (num)
539540
ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0);
540541
}
541542

@@ -922,15 +923,19 @@ static int unregister_fprobe_nolock(struct fprobe *fp)
922923
int i, count;
923924

924925
addrs = kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL);
925-
if (!addrs) {
926-
ret = -ENOMEM; /* TODO: Fallback to one-by-one loop */
927-
goto out;
928-
}
926+
/*
927+
* This will remove fprobe_hash_node from the hash table even if
928+
* memory allocation fails. However, ftrace_ops will not be updated.
929+
* Anyway, when the last fprobe is unregistered, ftrace_ops is also
930+
* unregistered.
931+
*/
932+
if (!addrs)
933+
pr_warn("Failed to allocate working array. ftrace_ops may not sync.\n");
929934

930935
/* Remove non-synonim ips from table and hash */
931936
count = 0;
932937
for (i = 0; i < hlist_array->size; i++) {
933-
if (!delete_fprobe_node(&hlist_array->array[i]))
938+
if (!delete_fprobe_node(&hlist_array->array[i]) && addrs)
934939
addrs[count++] = hlist_array->array[i].addr;
935940
}
936941
del_fprobe_hash(fp);

0 commit comments

Comments
 (0)