Skip to contentSkip to conversation

safe_mmap: Avoid using TDEBUG before IPC is initialized

PATCH In review Li Wang via ltp 2 participants 6 messages Jun 12, 2025 – Jun 17, 2025 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 safe_mmap: Avoid using TDEBUG before IPC is initialized

After commit bf9589d5bd ("lib: move test infrastructure states into a shared context structure"), each test began printing unexpected TDEBUG messages like:

tst_test.c:142: TDEBUG: mmap((nil), 4096, PROT_READ | PROT_WRITE(3), 1, 3, 0)
...
tst_test.c:199: TDEBUG: mmap((nil), 4096, PROT_READ | PROT_WRITE(3), 1, 3, 0)

This happens because the logic in tst_res_():

  if (ttype == TDEBUG && context && !context->tdebug)

allows TDEBUG messages to be printed even the context is not yet initialized. During early test setup (such as in SAFE_MMAP), the shared context may not be initialized yet, causing debug logs to be emitted unintentionally.

This patch removes the TDEBUG logging from SAFE_MMAP to avoid these misleading logs during early initialization.

Follow-up: commit bf9589d5bd ("lib: move test infrastructure states into a shared context structure")

---

Notes:
I admit that this removal may result in the loss of early debug output from the LTP library. However, after reviewing the codebase, it appears that this was the only place using TDEBUG during early initialization, so removing it simplifies the behavior without significant loss of functionality.

But, if someone argues that we need this, I guess we can add a macro tst_early_debug(fmt, ...) to replace the tst_res_(TDEBUG, ...).

 include/tst_safe_macros_inline.h | 4 ----
 1 file changed, 4 deletions(-)
include/tst_safe_macros_inline.h +0 -4
index 15b756860..15500b20a 100644
@@ -237,10 +237,6 @@ static inline void *safe_mmap(const char *file, const int lineno,
 
 	tst_prot_to_str(prot, prot_buf);
 
-	tst_res_(file, lineno, TDEBUG,
-		"mmap(%p, %zu, %s(%x), %d, %d, %lld)",
-		addr, length, prot_buf, prot, flags, fd, (long long int)offset);
-
 	rval = mmap(addr, length, prot, flags, fd, offset);
 	if (rval == MAP_FAILED) {
 		tst_brk_(file, lineno, TBROK | TERRNO,
Signature · 5 lines
2.49.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi!

Quoted 9 lines

Notes:
I admit that this removal may result in the loss of early debug output from the LTP library. However, after reviewing the codebase, it appears that this was the only place using TDEBUG during early initialization, so removing it simplifies the behavior without significant loss of functionality.

But, if someone argues that we need this, I guess we can add a macro tst_early_debug(fmt, ...) to replace the tst_res_(TDEBUG, ...).

Actually I was thinking of adding TDEBUG messages into more SAFE_MACROS() in order to make test debugging easier.

Maybe we can just skip the TDEBUG messages for the test library process I do not think that we are interested SAFE_MACROS() being called from there.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 11 lines · Cyril Hrubis

Hi!

Quoted 9 lines

Notes:
I admit that this removal may result in the loss of early debug output from the LTP library. However, after reviewing the codebase, it appears that this was the only place using TDEBUG during early initialization, so removing it simplifies the behavior without significant loss of functionality.

But, if someone argues that we need this, I guess we can add a macro tst_early_debug(fmt, ...) to replace the tst_res_(TDEBUG, ...).

Quoted 7 lines

Actually I was thinking of adding TDEBUG messages into more SAFE_MACROS() in order to make test debugging easier.

Maybe we can just skip the TDEBUG messages for the test library process I do not think that we are interested SAFE_MACROS() being called from there.

Good point, maybe refine the patch like:

lib/tst_test.c +0 -0
@@ -476,8 +476,22 @@ void tst_res_(const char *file, const int lineno, int

ttype,

 {
        va_list va;

- if (ttype == TDEBUG && context && !context->tdebug)
- return;
+ /*

+        * Suppress TDEBUG output in these cases:
+        * 1. No context available (e.g., called before IPC initialization)
+        * 2. Called from the library process, unless explicitly enabled
+        * 3. Debug output is not enabled (context->tdebug == 0)
+        */

+ if (ttype == TDEBUG) {
+ if (!context)
+ return;
+
+ if (getpid() == context->lib_pid)
+ return;
+
+ if (!context->tdebug)
+ return;
+ }

        va_start(va, fmt);
        tst_vres_(file, lineno, ttype, fmt, va);
Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v2 1/1 lib: suppress early TDEBUG output before context initialization

In reply to Li Wang via ltp

After commit bf9589d5bd ("lib: move test infrastructure states into a shared context structure"), each test began printing unexpected TDEBUG messages like:

tst_test.c:142: TDEBUG: mmap((nil), 4096, PROT_READ | PROT_WRITE(3), 1, 3, 0) tst_test.c:199: TDEBUG: mmap((nil), 4096, PROT_READ | PROT_WRITE(3), 1, 3, 0)

This happens because the logic in tst_res_():

  if (ttype == TDEBUG && context && !context->tdebug)

allows TDEBUG messages to be printed even the context is not yet initialized. During early test setup (such as in SAFE_MMAP), the shared context may not be initialized yet, causing debug logs to be emitted unintentionally.

This patch refines the suppression logic in tst_res_() by explicitly checking:
- Whether context has been initialized
- Whether current is the library test process (lib_pid)
- Whether tdebug is enabled

This improves test log clarity and avoids misleading noise during test setup.

Follow-up: commit bf9589d5bd ("lib: move test infrastructure states into a shared context structure")

---
 lib/tst_test.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
lib/tst_test.c +16 -2
index 6fcd40030..495e022f7 100644
@@ -476,8 +476,22 @@ void tst_res_(const char *file, const int lineno, int ttype,
 {
 	va_list va;
 
-	if (ttype == TDEBUG && context && !context->tdebug)
-		return;
+	/*
+	 * Suppress TDEBUG output in these cases:
+	 * 1. No context available (e.g., called before IPC initialization)
+	 * 2. Called from the library process, unless explicitly enabled
+	 * 3. Debug output is not enabled (context->tdebug == 0)
+	 */
+	if (ttype == TDEBUG) {
+		if (!context)
+			return;
+
+		if (context->lib_pid == getpid())
+			return;
+
+		if (!context->tdebug)
+			return;
+	}
 
 	va_start(va, fmt);
 	tst_vres_(file, lineno, ttype, fmt, va);
Signature · 5 lines
2.49.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v2] lib: suppress early TDEBUG output before context initialization

Patch merged.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp