From nobody Wed Dec 17 16:10:13 2025 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 AD7F12F2619; Mon, 15 Dec 2025 21:14:57 +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=1765833297; cv=none; b=s7UXrvX22g686CUyhYk/3atAxiV+J9+VEKm3dlqgVthowLdX1XGSNsDKWlbN+Lq7iQCqldYZ0GHwgcZHHWUCOhB3mQZaNfO4EFiIY4Xq2Yd6Pmzdqy0JoiSN3yQu3u4EGkdH35CD7KU1791HSD2x1FA6iP9W9gDdT/55xuxdxjA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765833297; c=relaxed/simple; bh=+hKBmBIFauyQkmIee7qfHOjxpGBb8y8EuvA0vNN3TfQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JuEFOBQ+6jqHMW1ykr0XcP8zVEUqDHy1jGfGs6RJwLffWUW2hISFCz25RnRP0lv7uHXE+jNGvcJetf3yv7aJfc/INVn8ZK/jA4fngaIL3iHGqvZoYYUxnmJ+RiksDtqbhZQjqe5pWKvtYSoWg8CefTBfbF/CHBOl8Rl6Q1lh4rg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kxTD2oYi; 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="kxTD2oYi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3C141C4CEF5; Mon, 15 Dec 2025 21:14:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1765833297; bh=+hKBmBIFauyQkmIee7qfHOjxpGBb8y8EuvA0vNN3TfQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kxTD2oYi+yGKVq8m3lSojVMxcSBXkoKZHJz2drr5YCG1yjC9OLvMK4I5P6WVDwEn1 PtA3NS5jjRdUfyIRKJRpGo734GKs+ZfGnoeIChKNFNHZ8Z5dYZSWahCTQy/bQ6ClJW 37CzKAIKuEfIgm9Qx0qtDf+JcnRorT4WNFtZ4DfEgZqBpQhEEiW5+Yf/6H9jRyqWBO OrbfaNcCExcNpZzn/xEPH2u2K3888r8MTTIsDSOD+u7fJZH1YxGdpTvEseXg9/BV1j lb264eKoOXe2fFa4u5BT/yD04PWNaNiondkDxoePzniCsPIHlgxdgkp3bglAhWX33A F8XdBPF1XVuHA== From: Jiri Olsa To: Steven Rostedt , Florent Revest , Mark Rutland Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Menglong Dong , Song Liu Subject: [PATCHv5 bpf-next 4/9] ftrace: Add update_ftrace_direct_add function Date: Mon, 15 Dec 2025 22:13:57 +0100 Message-ID: <20251215211402.353056-5-jolsa@kernel.org> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251215211402.353056-1-jolsa@kernel.org> References: <20251215211402.353056-1-jolsa@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adding update_ftrace_direct_add function that adds all entries (ip -> addr) provided in hash argument to direct ftrace ops and updates its attachments. The difference to current register_ftrace_direct is - hash argument that allows to register multiple ip -> direct entries at once - we can call update_ftrace_direct_add multiple times on the same ftrace_ops object, becase after first registration with register_ftrace_function_nolock, it uses ftrace_update_ops to update the ftrace_ops object This change will allow us to have simple ftrace_ops for all bpf direct interface users in following changes. Signed-off-by: Jiri Olsa --- include/linux/ftrace.h | 7 +++ kernel/trace/ftrace.c | 128 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index c0a72fcae1f6..5cf151cb8e6d 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -543,6 +543,8 @@ int unregister_ftrace_direct(struct ftrace_ops *ops, un= signed long addr, int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr); int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr= ); =20 +int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace_hash *h= ash); + void ftrace_stub_direct_tramp(void); =20 #else @@ -569,6 +571,11 @@ static inline int modify_ftrace_direct_nolock(struct f= trace_ops *ops, unsigned l return -ENODEV; } =20 +static inline int update_ftrace_direct_add(struct ftrace_ops *ops, struct = ftrace_hash *hash) +{ + return -ENODEV; +} + /* * This must be implemented by the architecture. * It is the way the ftrace direct_ops helper, when called diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 84aee9096a9e..ec7d93460dac 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -6249,6 +6249,134 @@ int modify_ftrace_direct(struct ftrace_ops *ops, un= signed long addr) return err; } EXPORT_SYMBOL_GPL(modify_ftrace_direct); + +static unsigned long hash_count(struct ftrace_hash *hash) +{ + return hash ? hash->count : 0; +} + +/** + * hash_add - adds two struct ftrace_hash and returns the result + * @a: struct ftrace_hash object + * @b: struct ftrace_hash object + * + * Returns struct ftrace_hash object on success, NULL on error. + */ +static struct ftrace_hash *hash_add(struct ftrace_hash *a, struct ftrace_h= ash *b) +{ + struct ftrace_func_entry *entry; + struct ftrace_hash *add; + int size, i; + + size =3D hash_count(a) + hash_count(b); + if (size > 32) + size =3D 32; + + add =3D alloc_and_copy_ftrace_hash(fls(size), a); + if (!add) + goto error; + + size =3D 1 << b->size_bits; + for (i =3D 0; i < size; i++) { + hlist_for_each_entry(entry, &b->buckets[i], hlist) { + if (add_hash_entry_direct(add, entry->ip, entry->direct) =3D=3D NULL) + goto error; + } + } + return add; + + error: + free_ftrace_hash(add); + return NULL; +} + +int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace_hash *h= ash) +{ + struct ftrace_hash *old_direct_functions =3D NULL, *new_direct_functions; + struct ftrace_hash *old_filter_hash, *new_filter_hash =3D NULL; + struct ftrace_func_entry *entry; + int i, size, err =3D -EINVAL; + bool reg; + + if (!hash_count(hash)) + return -EINVAL; + + mutex_lock(&direct_mutex); + + /* Make sure requested entries are not already registered. */ + size =3D 1 << hash->size_bits; + for (i =3D 0; i < size; i++) { + hlist_for_each_entry(entry, &hash->buckets[i], hlist) { + if (__ftrace_lookup_ip(direct_functions, entry->ip)) + goto out_unlock; + } + } + + old_filter_hash =3D ops->func_hash ? ops->func_hash->filter_hash : NULL; + + /* If there's nothing in filter_hash we need to register the ops. */ + reg =3D hash_count(old_filter_hash) =3D=3D 0; + if (reg) { + if (ops->func || ops->trampoline) + goto out_unlock; + if (ops->flags & FTRACE_OPS_FL_ENABLED) + goto out_unlock; + } + + err =3D -ENOMEM; + new_filter_hash =3D hash_add(old_filter_hash, hash); + if (!new_filter_hash) + goto out_unlock; + + new_direct_functions =3D hash_add(direct_functions, hash); + if (!new_direct_functions) + goto out_unlock; + + old_direct_functions =3D direct_functions; + rcu_assign_pointer(direct_functions, new_direct_functions); + + if (reg) { + ops->func =3D call_direct_funcs; + ops->flags |=3D MULTI_FLAGS; + ops->trampoline =3D FTRACE_REGS_ADDR; + ops->local_hash.filter_hash =3D new_filter_hash; + + err =3D register_ftrace_function_nolock(ops); + if (err) { + /* restore old filter on error */ + ops->local_hash.filter_hash =3D old_filter_hash; + + /* cleanup for possible another register call */ + ops->func =3D NULL; + ops->trampoline =3D 0; + } else { + new_filter_hash =3D old_filter_hash; + } + } else { + err =3D ftrace_update_ops(ops, new_filter_hash, EMPTY_HASH); + /* + * new_filter_hash is dup-ed, so we need to release it anyway, + * old_filter_hash either stays on error or is already released + */ + } + + if (err) { + /* reset direct_functions and free the new one */ + rcu_assign_pointer(direct_functions, old_direct_functions); + old_direct_functions =3D new_direct_functions; + } + + out_unlock: + mutex_unlock(&direct_mutex); + + if (old_direct_functions && old_direct_functions !=3D EMPTY_HASH) + call_rcu_tasks(&old_direct_functions->rcu, register_ftrace_direct_cb); + if (new_filter_hash) + free_ftrace_hash(new_filter_hash); + + return err; +} + #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ =20 /** --=20 2.52.0