Re: [Devel] [PATCH vz10] tests: add ve_printk selftest

2025-12-02 Thread Aleksei Oladko


On 12/2/25 11:32 AM, Konstantin Khorenko wrote:

On 11/26/25 21:04, Aleksei Oladko wrote:

Add selftests for the printk virtualization feature. The new tests cover
ve_printk, ve_printk_ratelimited, net_ve_ratelimited, and log buffer
overflow handling.

https://virtuozzo.atlassian.net/browse/VSTOR-114252

Signed-off-by: Aleksei Oladko 
---
  tools/testing/selftests/Makefile  |   1 +
  tools/testing/selftests/ve_printk/.gitignore  |   3 +
  tools/testing/selftests/ve_printk/Makefile    |   8 +
  tools/testing/selftests/ve_printk/test_segf.c |  12 +
  tools/testing/selftests/ve_printk/test_trap.c |   5 +
  .../selftests/ve_printk/ve_printk_test.c  | 593 ++
  6 files changed, 622 insertions(+)
  create mode 100644 tools/testing/selftests/ve_printk/.gitignore
  create mode 100644 tools/testing/selftests/ve_printk/Makefile
  create mode 100644 tools/testing/selftests/ve_printk/test_segf.c
  create mode 100644 tools/testing/selftests/ve_printk/test_trap.c
  create mode 100644 tools/testing/selftests/ve_printk/ve_printk_test.c

diff --git a/tools/testing/selftests/Makefile 
b/tools/testing/selftests/Makefile

index 363d031a16f7..7334fb207676 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -113,6 +113,7 @@ TARGETS += tty
  TARGETS += uevent
  TARGETS += user_events
  TARGETS += vDSO
+TARGETS += ve_printk
  TARGETS += mm
  TARGETS += x86
  TARGETS += zram
diff --git a/tools/testing/selftests/ve_printk/.gitignore 
b/tools/testing/selftests/ve_printk/.gitignore

new file mode 100644
index ..a4ad6620b803
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/.gitignore
@@ -0,0 +1,3 @@
+ve_printk_test
+test_segf
+test_trap
diff --git a/tools/testing/selftests/ve_printk/Makefile 
b/tools/testing/selftests/ve_printk/Makefile

new file mode 100644
index ..e3edcbacda1e
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for ve_printk selftests.
+CFLAGS = -g -I../../../../usr/include/ -Wall -O2
+
+TEST_GEN_PROGS += ve_printk_test
+TEST_GEN_FILES += test_segf test_trap
+
+include ../lib.mk
diff --git a/tools/testing/selftests/ve_printk/test_segf.c 
b/tools/testing/selftests/ve_printk/test_segf.c

new file mode 100644
index ..cdc89068ca06
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/test_segf.c
@@ -0,0 +1,12 @@
+#include 
+#include 
+
+int main(void)
+{
+    int *p = (int *)0xff00;
+
+    printf("%d\n", getpid());
+    fflush(stdout);
+    *p = 1;
+    return 0;
+}
diff --git a/tools/testing/selftests/ve_printk/test_trap.c 
b/tools/testing/selftests/ve_printk/test_trap.c

new file mode 100644
index ..b774e2b9484c
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/test_trap.c
@@ -0,0 +1,5 @@
+int main(void)
+{
+    __asm__("int3");
+    return 0;
+}
diff --git a/tools/testing/selftests/ve_printk/ve_printk_test.c 
b/tools/testing/selftests/ve_printk/ve_printk_test.c

new file mode 100644
index ..78475ff71faa
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/ve_printk_test.c
@@ -0,0 +1,593 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "../kselftest_harness.h"
+
+#define __STACK_SIZE (8 * 1024 * 1024)
+#define CTID_MIN 108
+#define CTID_MAX 200
+#define SEGFAULT_PROG "test_segf"
+#define TRAP_PROG "test_trap"
+#define TEST_RATELIMIT_BURST 10
+#define TEST_RATELIMIT 5
+
+static int has_substr(char *buf, const char *str)
+{
+    char *token;
+    char *str_ptr = buf;
+
+    while ((token = strsep(&str_ptr, ",")) != NULL) {
+    if (!strcmp(token, str))
+    return 1;
+    }
+    return 0;
+}
+
+static int get_mount_path(const char *fstype, const char *subsys, 
char *out, int size)

+{
+    FILE *fp;
+    int n;
+    char buf[PATH_MAX];
+    char target[4096];
+    char ops[4096];
+    char format[4096];
+    int ret = 1;
+
+    snprintf(format, sizeof(format), "%%*s %%4095s %s %%4095s", 
fstype);

+
+    fp = fopen("/proc/mounts", "r");
+    if (fp == NULL)
+    return -1;
+
+    while (fgets(buf, sizeof(buf), fp)) {
+    n = sscanf(buf, format, target, ops);
+    if (n != 2)
+    continue;
+    if (subsys == NULL || has_substr(ops, subsys)) {
+    strncpy(out, target, size);
+    out[size-1] = '\0';
+    ret = 0;
+    break;
+    }
+    }
+    fclose(fp);
+
+    return ret;
+}
+
+FIXTURE(ve_printk)
+{
+    char cgv2_path[PATH_MAX];
+    char cgve_path[PATH_MAX];
+    int ctid;
+};
+
+FIXTURE_SETUP(ve_printk)
+{
+    char path[PATH_MAX * 2];
+
+    ASSERT_EQ(get_mount_path("cgroup2", NULL, self->cgv2_path, 
sizeof(self->cgv2_path)), 0);
+    ASSERT_EQ(get_mount_path("cgroup", "ve", self->cgve_path, 
sizeof(self->cgve_path)), 0);

+
+    self->ctid = CTID_MIN;
+   

Re: [Devel] [PATCH vz10] tests: add ve_printk selftest

2025-12-02 Thread Konstantin Khorenko

i would also ask you to put more comments both to the commit message and to the 
test code.

When someone opens the file, he should find very quickly the information what does this test checks 
overall, on a high level, without carefully reading all the code.


i have sent a generated example as a separate patch, please review if those comments are valid and use 
them or write your own ones.


Thank you.

--
Best regards,

Konstantin Khorenko,
Virtuozzo Linux Kernel Team

On 11/26/25 21:04, Aleksei Oladko wrote:

Add selftests for the printk virtualization feature. The new tests cover
ve_printk, ve_printk_ratelimited, net_ve_ratelimited, and log buffer
overflow handling.

https://virtuozzo.atlassian.net/browse/VSTOR-114252

Signed-off-by: Aleksei Oladko 
---
  tools/testing/selftests/Makefile  |   1 +
  tools/testing/selftests/ve_printk/.gitignore  |   3 +
  tools/testing/selftests/ve_printk/Makefile|   8 +
  tools/testing/selftests/ve_printk/test_segf.c |  12 +
  tools/testing/selftests/ve_printk/test_trap.c |   5 +
  .../selftests/ve_printk/ve_printk_test.c  | 593 ++
  6 files changed, 622 insertions(+)
  create mode 100644 tools/testing/selftests/ve_printk/.gitignore
  create mode 100644 tools/testing/selftests/ve_printk/Makefile
  create mode 100644 tools/testing/selftests/ve_printk/test_segf.c
  create mode 100644 tools/testing/selftests/ve_printk/test_trap.c
  create mode 100644 tools/testing/selftests/ve_printk/ve_printk_test.c

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 363d031a16f7..7334fb207676 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -113,6 +113,7 @@ TARGETS += tty
  TARGETS += uevent
  TARGETS += user_events
  TARGETS += vDSO
+TARGETS += ve_printk
  TARGETS += mm
  TARGETS += x86
  TARGETS += zram
diff --git a/tools/testing/selftests/ve_printk/.gitignore 
b/tools/testing/selftests/ve_printk/.gitignore
new file mode 100644
index ..a4ad6620b803
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/.gitignore
@@ -0,0 +1,3 @@
+ve_printk_test
+test_segf
+test_trap
diff --git a/tools/testing/selftests/ve_printk/Makefile 
b/tools/testing/selftests/ve_printk/Makefile
new file mode 100644
index ..e3edcbacda1e
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for ve_printk selftests.
+CFLAGS = -g -I../../../../usr/include/ -Wall -O2
+
+TEST_GEN_PROGS += ve_printk_test
+TEST_GEN_FILES += test_segf test_trap
+
+include ../lib.mk
diff --git a/tools/testing/selftests/ve_printk/test_segf.c 
b/tools/testing/selftests/ve_printk/test_segf.c
new file mode 100644
index ..cdc89068ca06
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/test_segf.c
@@ -0,0 +1,12 @@
+#include 
+#include 
+
+int main(void)
+{
+   int *p = (int *)0xff00;
+
+   printf("%d\n", getpid());
+   fflush(stdout);
+   *p = 1;
+   return 0;
+}
diff --git a/tools/testing/selftests/ve_printk/test_trap.c 
b/tools/testing/selftests/ve_printk/test_trap.c
new file mode 100644
index ..b774e2b9484c
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/test_trap.c
@@ -0,0 +1,5 @@
+int main(void)
+{
+   __asm__("int3");
+   return 0;
+}
diff --git a/tools/testing/selftests/ve_printk/ve_printk_test.c 
b/tools/testing/selftests/ve_printk/ve_printk_test.c
new file mode 100644
index ..78475ff71faa
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/ve_printk_test.c
@@ -0,0 +1,593 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "../kselftest_harness.h"
+
+#define __STACK_SIZE (8 * 1024 * 1024)
+#define CTID_MIN 108
+#define CTID_MAX 200
+#define SEGFAULT_PROG "test_segf"
+#define TRAP_PROG "test_trap"
+#define TEST_RATELIMIT_BURST 10
+#define TEST_RATELIMIT 5
+
+static int has_substr(char *buf, const char *str)
+{
+   char *token;
+   char *str_ptr = buf;
+
+   while ((token = strsep(&str_ptr, ",")) != NULL) {
+   if (!strcmp(token, str))
+   return 1;
+   }
+   return 0;
+}
+
+static int get_mount_path(const char *fstype, const char *subsys, char *out, 
int size)
+{
+   FILE *fp;
+   int n;
+   char buf[PATH_MAX];
+   char target[4096];
+   char ops[4096];
+   char format[4096];
+   int ret = 1;
+
+   snprintf(format, sizeof(format), "%%*s %%4095s %s %%4095s", fstype);
+
+   fp = fopen("/proc/mounts", "r");
+   if (fp == NULL)
+   return -1;
+
+   while (fgets(buf, sizeof(buf), fp)) {
+   n = sscanf(buf, format, target, ops);
+   if (n != 2)
+   continue;
+   if (subsys == NULL || has_substr(ops, subsys)) {
+  

Re: [Devel] [PATCH vz10] tests: add ve_printk selftest

2025-12-02 Thread Konstantin Khorenko

On 11/26/25 21:04, Aleksei Oladko wrote:

Add selftests for the printk virtualization feature. The new tests cover
ve_printk, ve_printk_ratelimited, net_ve_ratelimited, and log buffer
overflow handling.

https://virtuozzo.atlassian.net/browse/VSTOR-114252

Signed-off-by: Aleksei Oladko 
---
  tools/testing/selftests/Makefile  |   1 +
  tools/testing/selftests/ve_printk/.gitignore  |   3 +
  tools/testing/selftests/ve_printk/Makefile|   8 +
  tools/testing/selftests/ve_printk/test_segf.c |  12 +
  tools/testing/selftests/ve_printk/test_trap.c |   5 +
  .../selftests/ve_printk/ve_printk_test.c  | 593 ++
  6 files changed, 622 insertions(+)
  create mode 100644 tools/testing/selftests/ve_printk/.gitignore
  create mode 100644 tools/testing/selftests/ve_printk/Makefile
  create mode 100644 tools/testing/selftests/ve_printk/test_segf.c
  create mode 100644 tools/testing/selftests/ve_printk/test_trap.c
  create mode 100644 tools/testing/selftests/ve_printk/ve_printk_test.c

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 363d031a16f7..7334fb207676 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -113,6 +113,7 @@ TARGETS += tty
  TARGETS += uevent
  TARGETS += user_events
  TARGETS += vDSO
+TARGETS += ve_printk
  TARGETS += mm
  TARGETS += x86
  TARGETS += zram
diff --git a/tools/testing/selftests/ve_printk/.gitignore 
b/tools/testing/selftests/ve_printk/.gitignore
new file mode 100644
index ..a4ad6620b803
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/.gitignore
@@ -0,0 +1,3 @@
+ve_printk_test
+test_segf
+test_trap
diff --git a/tools/testing/selftests/ve_printk/Makefile 
b/tools/testing/selftests/ve_printk/Makefile
new file mode 100644
index ..e3edcbacda1e
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/Makefile
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+# Makefile for ve_printk selftests.
+CFLAGS = -g -I../../../../usr/include/ -Wall -O2
+
+TEST_GEN_PROGS += ve_printk_test
+TEST_GEN_FILES += test_segf test_trap
+
+include ../lib.mk
diff --git a/tools/testing/selftests/ve_printk/test_segf.c 
b/tools/testing/selftests/ve_printk/test_segf.c
new file mode 100644
index ..cdc89068ca06
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/test_segf.c
@@ -0,0 +1,12 @@
+#include 
+#include 
+
+int main(void)
+{
+   int *p = (int *)0xff00;
+
+   printf("%d\n", getpid());
+   fflush(stdout);
+   *p = 1;
+   return 0;
+}
diff --git a/tools/testing/selftests/ve_printk/test_trap.c 
b/tools/testing/selftests/ve_printk/test_trap.c
new file mode 100644
index ..b774e2b9484c
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/test_trap.c
@@ -0,0 +1,5 @@
+int main(void)
+{
+   __asm__("int3");
+   return 0;
+}
diff --git a/tools/testing/selftests/ve_printk/ve_printk_test.c 
b/tools/testing/selftests/ve_printk/ve_printk_test.c
new file mode 100644
index ..78475ff71faa
--- /dev/null
+++ b/tools/testing/selftests/ve_printk/ve_printk_test.c
@@ -0,0 +1,593 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "../kselftest_harness.h"
+
+#define __STACK_SIZE (8 * 1024 * 1024)
+#define CTID_MIN 108
+#define CTID_MAX 200
+#define SEGFAULT_PROG "test_segf"
+#define TRAP_PROG "test_trap"
+#define TEST_RATELIMIT_BURST 10
+#define TEST_RATELIMIT 5
+
+static int has_substr(char *buf, const char *str)
+{
+   char *token;
+   char *str_ptr = buf;
+
+   while ((token = strsep(&str_ptr, ",")) != NULL) {
+   if (!strcmp(token, str))
+   return 1;
+   }
+   return 0;
+}
+
+static int get_mount_path(const char *fstype, const char *subsys, char *out, 
int size)
+{
+   FILE *fp;
+   int n;
+   char buf[PATH_MAX];
+   char target[4096];
+   char ops[4096];
+   char format[4096];
+   int ret = 1;
+
+   snprintf(format, sizeof(format), "%%*s %%4095s %s %%4095s", fstype);
+
+   fp = fopen("/proc/mounts", "r");
+   if (fp == NULL)
+   return -1;
+
+   while (fgets(buf, sizeof(buf), fp)) {
+   n = sscanf(buf, format, target, ops);
+   if (n != 2)
+   continue;
+   if (subsys == NULL || has_substr(ops, subsys)) {
+   strncpy(out, target, size);
+   out[size-1] = '\0';
+   ret = 0;
+   break;
+   }
+   }
+   fclose(fp);
+
+   return ret;
+}
+
+FIXTURE(ve_printk)
+{
+   char cgv2_path[PATH_MAX];
+   char cgve_path[PATH_MAX];
+   int ctid;
+};
+
+FIXTURE_SETUP(ve_printk)
+{
+   char path[PATH_MAX * 2];
+
+   ASSERT_EQ(get_mount_path("cgroup2", NULL, self->cgv2_path, 
sizeof(self->cgv2_