Skip to contentSkip to conversation

cobalt/posix/syscall: Account for changes in kernel 7.0

PATCH In review Jan Kiszka 2 participants 4 messages Apr 14

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 cobalt/posix/syscall: Account for changes in kernel 7.0

From: Jan Kiszka <[email protected]>

With 7.0, we no longer have a access to trace print function that takes the string size directly. [__]trace_puts will now always invoke strlen on the passed string. We therefore need to terminate our string properly, even if a larger one was passed.

We could keep the micro-optimized variant for older kernels, but as that is phasing out and the gain is likely minimal (the string is still in L1 cache after copy_from_user), simply switch all versions to the same pattern.

---
 kernel/cobalt/posix/syscall.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/cobalt/posix/syscall.c +9 -2
index a26da154c4..bc1825676f 100644
@@ -46,6 +46,12 @@
 #include "../debug.h"
 #include <trace/events/cobalt-posix.h>
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(7,0,0)
+#include <linux/trace_printk.h>
+#else
+#include <linux/kernel.h>
+#endif
+
 /* Syscall must run into the Linux domain. */
 #define __xn_exec_lostage    0x1
 /* Syscall must run into the Xenomai domain. */
@@ -194,12 +200,13 @@ static COBALT_SYSCALL(ftrace_puts, current,
 	char buf[256];
 	unsigned len;
 
-	len = cobalt_strncpy_from_user(buf, str, sizeof(buf));
+	len = cobalt_strncpy_from_user(buf, str, sizeof(buf) - 1);
 	if (len < 0)
 		return -EFAULT;
 
 #ifdef CONFIG_TRACING
-	__trace_puts(_THIS_IP_, buf, len);
+	buf[sizeof(buf) - 1] = 0;
+	trace_puts(buf);
 #endif
 
 	return 0;
Signature · 1 line
2.47.3
#1 lore raw
Quoted 36 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

With 7.0, we no longer have a access to trace print function that takes the string size directly. [__]trace_puts will now always invoke strlen on the passed string. We therefore need to terminate our string properly, even if a larger one was passed.

We could keep the micro-optimized variant for older kernels, but as that is phasing out and the gain is likely minimal (the string is still in L1 cache after copy_from_user), simply switch all versions to the same pattern.

---
 kernel/cobalt/posix/syscall.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/cobalt/posix/syscall.c +6 -0
index a26da154c4..bc1825676f 100644
@@ -46,6 +46,12 @@
 #include "../debug.h"
 #include <trace/events/cobalt-posix.h>
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(7,0,0)
+#include <linux/trace_printk.h>
+#else
+#include <linux/kernel.h>
+#endif
+
 /* Syscall must run into the Linux domain. */
 #define __xn_exec_lostage    0x1
 /* Syscall must run into the Xenomai domain. */
@@ -194,12 +200,13 @@ static COBALT_SYSCALL(ftrace_puts, current,
 	char buf[256];

kernel/cobalt/posix/syscall.c static COBALT_SYSCALL(ftrace_puts, current, · line 208

Quoted 10 lines
kernel/cobalt/posix/syscall.c +2 -2
 	unsigned len;
 
-	len = cobalt_strncpy_from_user(buf, str, sizeof(buf));
+	len = cobalt_strncpy_from_user(buf, str, sizeof(buf) - 1);
 	if (len < 0)
 		return -EFAULT;
 
 #ifdef CONFIG_TRACING
-	__trace_puts(_THIS_IP_, buf, len);
+	buf[sizeof(buf) - 1] = 0;

Shouldn't we terminate at buf[len]?

buf[len+1] to buf[sizeof(buf)-1] might be garbage, no?

Quoted 6 lines
kernel/cobalt/posix/syscall.c +1 -0
+	trace_puts(buf);
 #endif
 
 	return 0;
Signature · 1 line
2.47.3
#2 lore raw
Quoted 44 lines · Florian Bezdeka
Quoted 43 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

With 7.0, we no longer have a access to trace print function that takes the string size directly. [__]trace_puts will now always invoke strlen on the passed string. We therefore need to terminate our string properly, even if a larger one was passed.

We could keep the micro-optimized variant for older kernels, but as that is phasing out and the gain is likely minimal (the string is still in L1 cache after copy_from_user), simply switch all versions to the same pattern.

---
 kernel/cobalt/posix/syscall.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/cobalt/posix/syscall.c +7 -1
index a26da154c4..bc1825676f 100644
@@ -46,6 +46,12 @@
 #include "../debug.h"
 #include <trace/events/cobalt-posix.h>
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(7,0,0)
+#include <linux/trace_printk.h>
+#else
+#include <linux/kernel.h>
+#endif
+
 /* Syscall must run into the Linux domain. */
 #define __xn_exec_lostage    0x1
 /* Syscall must run into the Xenomai domain. */
@@ -194,12 +200,13 @@ static COBALT_SYSCALL(ftrace_puts, current,
 	char buf[256];
 	unsigned len;
 
-	len = cobalt_strncpy_from_user(buf, str, sizeof(buf));
+	len = cobalt_strncpy_from_user(buf, str, sizeof(buf) - 1);
 	if (len < 0)
 		return -EFAULT;
 
Quoted 7 lines

kernel/cobalt/posix/syscall.c static COBALT_SYSCALL(ftrace_puts, current, · line 208

Quoted 3 lines
kernel/cobalt/posix/syscall.c +1 -1
 #ifdef CONFIG_TRACING
-	__trace_puts(_THIS_IP_, buf, len);
+	buf[sizeof(buf) - 1] = 0;

Shouldn't we terminate at buf[len]?

buf[len+1] to buf[sizeof(buf)-1] might be garbage, no?

Ignore that. Pressed Send to fast... That is just the worst case as strncpy does not terminate in case the full buffer was used but does so if less then the full size was needed.

Quoted 7 lines
Quoted 7 lines
kernel/cobalt/posix/syscall.c +1 -0
+	trace_puts(buf);
 #endif
 
 	return 0;
Signature · 1 line
2.47.3
author
#3 lore raw
In reply to Florian Bezdeka
Quoted 44 lines · Florian Bezdeka
Quoted 43 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

With 7.0, we no longer have a access to trace print function that takes the string size directly. [__]trace_puts will now always invoke strlen on the passed string. We therefore need to terminate our string properly, even if a larger one was passed.

We could keep the micro-optimized variant for older kernels, but as that is phasing out and the gain is likely minimal (the string is still in L1 cache after copy_from_user), simply switch all versions to the same pattern.

---
 kernel/cobalt/posix/syscall.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/cobalt/posix/syscall.c +7 -1
index a26da154c4..bc1825676f 100644
@@ -46,6 +46,12 @@
 #include "../debug.h"
 #include <trace/events/cobalt-posix.h>
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(7,0,0)
+#include <linux/trace_printk.h>
+#else
+#include <linux/kernel.h>
+#endif
+
 /* Syscall must run into the Linux domain. */
 #define __xn_exec_lostage    0x1
 /* Syscall must run into the Xenomai domain. */
@@ -194,12 +200,13 @@ static COBALT_SYSCALL(ftrace_puts, current,
 	char buf[256];
 	unsigned len;
 
-	len = cobalt_strncpy_from_user(buf, str, sizeof(buf));
+	len = cobalt_strncpy_from_user(buf, str, sizeof(buf) - 1);
 	if (len < 0)
 		return -EFAULT;
 
Quoted 8 lines

kernel/cobalt/posix/syscall.c static COBALT_SYSCALL(ftrace_puts, current, · line 208

Quoted 3 lines
kernel/cobalt/posix/syscall.c +1 -1
 #ifdef CONFIG_TRACING
-	__trace_puts(_THIS_IP_, buf, len);
+	buf[sizeof(buf) - 1] = 0;

Shouldn't we terminate at buf[len]?

buf[len+1] to buf[sizeof(buf)-1] might be garbage, no?

This is a protective termination in case len == sizeof(buf) -1. We could also terminate at len, correct, but so is this version.

Jan

Quoted 6 lines
Quoted 6 lines
kernel/cobalt/posix/syscall.c +1 -0
+	trace_puts(buf);
 #endif
 
 	return 0;
Signature · 1 line
2.47.3
Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center