From 3c46ddc134621dba65030263aa321dd6bdae3ba3 Mon Sep 17 00:00:00 2001
From: Michael Jeanson <mjeanson@efficios.com>
Date: Mon, 4 Apr 2022 15:02:10 -0400
Subject: [PATCH 07/10] fix: kprobes: Use rethook for kretprobe if possible
 (v5.18)

See upstream commit :

  commit 73f9b911faa74ac5107879de05c9489c419f41bb
  Author: Masami Hiramatsu <mhiramat@kernel.org>
  Date:   Sat Mar 26 11:27:05 2022 +0900

    kprobes: Use rethook for kretprobe if possible

    Use rethook for kretprobe function return hooking if the arch sets
    CONFIG_HAVE_RETHOOK=y. In this case, CONFIG_KRETPROBE_ON_RETHOOK is
    set to 'y' automatically, and the kretprobe internal data fields
    switches to use rethook. If not, it continues to use kretprobe
    specific function return hooks.

Upstream-Status: Backport

Change-Id: I2b7670dc04e4769c1e3c372582ad2f555f6d7a66
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
 include/wrapper/kprobes.h     | 17 +++++++++++++++++
 src/probes/lttng-kretprobes.c |  2 +-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/include/wrapper/kprobes.h b/include/wrapper/kprobes.h
index b546d615..51d32b7c 100644
--- a/include/wrapper/kprobes.h
+++ b/include/wrapper/kprobes.h
@@ -29,4 +29,21 @@ struct kretprobe *lttng_get_kretprobe(struct kretprobe_instance *ri)
 
 #endif /* LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,11,0) */
 
+
+#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,18,0))
+static inline
+unsigned long lttng_get_kretprobe_retaddr(struct kretprobe_instance *ri)
+{
+	return get_kretprobe_retaddr(ri);
+}
+
+#else
+
+static inline
+unsigned long lttng_get_kretprobe_retaddr(struct kretprobe_instance *ri)
+{
+	return (unsigned long) ri->ret_addr;
+}
+#endif
+
 #endif /* _LTTNG_WRAPPER_KPROBES_H */
diff --git a/src/probes/lttng-kretprobes.c b/src/probes/lttng-kretprobes.c
index 5cb2e953..565df739 100644
--- a/src/probes/lttng-kretprobes.c
+++ b/src/probes/lttng-kretprobes.c
@@ -81,7 +81,7 @@ int _lttng_kretprobes_handler(struct kretprobe_instance *krpi,
 		int ret;
 
 		payload.ip = (unsigned long) lttng_get_kretprobe(krpi)->kp.addr;
-		payload.parent_ip = (unsigned long) krpi->ret_addr;
+		payload.parent_ip = lttng_get_kretprobe_retaddr(krpi);
 
 		lib_ring_buffer_ctx_init(&ctx, event_recorder, sizeof(payload),
 					 lttng_alignof(payload), &lttng_probe_ctx);
-- 
2.19.1

