From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BD5347D941 for ; Fri, 15 May 2026 11:32:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778844750; cv=none; b=MtFXdXJbvzOLgrT17pZ3qIkKROOzHDRNEvHPrfynCN88+VuWP4o3b08f3hp5drDBoffkj2JrTB1KRhnIvjLPRa3rNgcQ4Ij17/0Hk69S+Hl3e1vENSzCdn4Pqko2BO+GlpPqwN81JANiQ4jPpNZOlnSyy39SPOAgwhs2kDJ6+zE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778844750; c=relaxed/simple; bh=ujR4hsLNHYn9j7AXlOLbsudzlh/dINkYYt+kfLWApW8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dsAEsKCyLiINV2CnDsk0RxerwiADTXmlFBHbwstP/DLOaM7L9ns6J2Cfl154j/YMAuOxT+QvzwhxjYhh7H1hU/GZz0lK4qKcLkYEe27SsCiSB6A01+SOoH5FEN4fA49MdhsBSG4e+dHd9c9b70NNnCV04hF/n5n9LXq18JkPXZQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ap9uKLdm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ap9uKLdm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B646C2BCB8; Fri, 15 May 2026 11:32:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778844750; bh=ujR4hsLNHYn9j7AXlOLbsudzlh/dINkYYt+kfLWApW8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ap9uKLdmrhwGId7LpEJdM6WnAQmNYDjXh/p+RLNiSS+LlMj4RFTtXrQpcpgckQ39R xCWNyG5zJQPUKi75bRXIJwhyxT3akKVSWCRgULz+5DqARfm2NI41Vddf83RmmshnGd WTr0762lARDqN81I9EwJRGstNuv3DT+AFO/pcL62JAWBnMU3TOiqm00SNVF9voYEcD m4+tc5GcMOV3E24Fo4JAEhIxD6v0u38r2bhPC1uKnCyG5bRQQrzdQgV5Eb6RD7Dcud 6f/YZ92qtRlbh8t6B4W/tMF1T1OACwtxZEhfmgst3eQ6S01GoTBeIEWqvT6sfIUVCl KyviuTXBHXZbA== From: Sasha Levin To: stable@vger.kernel.org Cc: "Masami Hiramatsu (Google)" , Sasha Levin Subject: [PATCH 6.18.y 3/4] tracing/fprobe: Unregister fprobe even if memory allocation fails Date: Fri, 15 May 2026 07:32:25 -0400 Message-ID: <20260515113226.2979191-3-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260515113226.2979191-1-sashal@kernel.org> References: <2026051206-mocker-bonfire-ac1c@gregkh> <20260515113226.2979191-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Masami Hiramatsu (Google)" [ Upstream commit 1aec9e5c3e31ce1e28f914427fb7f90b91d310df ] 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: 4346ba160409 ("fprobe: Rewrite fprobe on function-graph tracer") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) Stable-dep-of: 845947aca681 ("tracing/fprobe: Remove fprobe from hash in failure path") Signed-off-by: Sasha Levin --- kernel/trace/fprobe.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 66fa49b0cf27a..2e5c6b3cafc0b 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -323,9 +323,10 @@ static void fprobe_ftrace_remove_ips(unsigned long *addrs, int num) lockdep_assert_held(&fprobe_mutex); fprobe_ftrace_active--; - if (!fprobe_ftrace_active) + if (!fprobe_ftrace_active) { unregister_ftrace_function(&fprobe_ftrace_ops); - if (num) + ftrace_free_filter(&fprobe_ftrace_ops); + } else if (num) ftrace_set_filter_ips(&fprobe_ftrace_ops, addrs, num, 1, 0); } @@ -508,10 +509,10 @@ static void fprobe_graph_remove_ips(unsigned long *addrs, int num) fprobe_graph_active--; /* Q: should we unregister it ? */ - if (!fprobe_graph_active) + if (!fprobe_graph_active) { unregister_ftrace_graph(&fprobe_graph_ops); - - if (num) + ftrace_free_filter(&fprobe_graph_ops.ops); + } else if (num) ftrace_set_filter_ips(&fprobe_graph_ops.ops, addrs, num, 1, 0); } @@ -924,15 +925,19 @@ int unregister_fprobe(struct fprobe *fp) hlist_array = fp->hlist_array; addrs = kcalloc(hlist_array->size, sizeof(unsigned long), GFP_KERNEL); - if (!addrs) { - ret = -ENOMEM; /* TODO: Fallback to one-by-one loop */ - goto out; - } + /* + * This will remove fprobe_hash_node from the hash table even if + * memory allocation fails. However, ftrace_ops will not be updated. + * Anyway, when the last fprobe is unregistered, ftrace_ops is also + * unregistered. + */ + if (!addrs) + pr_warn("Failed to allocate working array. ftrace_ops may not sync.\n"); /* 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])) + if (!delete_fprobe_node(&hlist_array->array[i]) && addrs) addrs[count++] = hlist_array->array[i].addr; } del_fprobe_hash(fp); -- 2.53.0