Skip to content

Commit 1aec9e5

Browse files
committed
tracing/fprobe: Unregister fprobe even if memory allocation fails
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>
1 parent 6ad51ad commit 1aec9e5

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
@@ -324,9 +324,10 @@ static void fprobe_ftrace_remove_ips(unsigned long *addrs, int num)
324324
lockdep_assert_held(&fprobe_mutex);
325325

326326
fprobe_ftrace_active--;
327-
if (!fprobe_ftrace_active)
327+
if (!fprobe_ftrace_active) {
328328
unregister_ftrace_function(&fprobe_ftrace_ops);
329-
if (num)
329+
ftrace_free_filter(&fprobe_ftrace_ops);
330+
} else if (num)
330331
ftrace_set_filter_ips(&fprobe_ftrace_ops, addrs, num, 1, 0);
331332
}
332333

@@ -525,10 +526,10 @@ static void fprobe_graph_remove_ips(unsigned long *addrs, int num)
525526

526527
fprobe_graph_active--;
527528
/* Q: should we unregister it ? */
528-
if (!fprobe_graph_active)
529+
if (!fprobe_graph_active) {
529530
unregister_ftrace_graph(&fprobe_graph_ops);
530-
531-
if (num)
531+
ftrace_free_filter(&fprobe_graph_ops.ops);
532+
} else if (num)
532533
ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0);
533534
}
534535

@@ -932,15 +933,19 @@ int unregister_fprobe(struct fprobe *fp)
932933

933934
hlist_array = fp->hlist_array;
934935
addrs = kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL);
935-
if (!addrs) {
936-
ret = -ENOMEM; /* TODO: Fallback to one-by-one loop */
937-
goto out;
938-
}
936+
/*
937+
* This will remove fprobe_hash_node from the hash table even if
938+
* memory allocation fails. However, ftrace_ops will not be updated.
939+
* Anyway, when the last fprobe is unregistered, ftrace_ops is also
940+
* unregistered.
941+
*/
942+
if (!addrs)
943+
pr_warn("Failed to allocate working array. ftrace_ops may not sync.\n");
939944

940945
/* Remove non-synonim ips from table and hash */
941946
count = 0;
942947
for (i = 0; i < hlist_array->size; i++) {
943-
if (!delete_fprobe_node(&hlist_array->array[i]))
948+
if (!delete_fprobe_node(&hlist_array->array[i]) && addrs)
944949
addrs[count++] = hlist_array->array[i].addr;
945950
}
946951
del_fprobe_hash(fp);

0 commit comments

Comments
 (0)