Skip to contentSkip to conversation

drm/print: Introduce drm_line_printer

RFC Superseded Michal Wajdeczko 3 participants 6 messages May 14, 2024 – May 24, 2024 1 message not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 drm/print: Introduce drm_line_printer

This drm printer wrapper can be used to increase the robustness of the captured output generated by any other drm_printer to make sure we didn't lost any intermediate lines of the output by adding line numbers to each output line. Helpful for capturing some crash data.

---
 drivers/gpu/drm/drm_print.c |  9 +++++++++
 include/drm/drm_print.h     | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
drivers/gpu/drm/drm_print.c +9 -0
index cf2efb44722c..d6fb50d3407a 100644
@@ -214,6 +214,15 @@ void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
 }
 EXPORT_SYMBOL(__drm_printfn_err);
 
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf)
+{
+	unsigned int line = (uintptr_t)(++p->prefix);
+	struct drm_printer *dp = p->arg;
+
+	drm_printf(dp, "%u: %pV", line, vaf);
+}
+EXPORT_SYMBOL(__drm_printfn_line);
+
 /**
  * drm_puts - print a const string to a &drm_printer stream
  * @p: the &drm printer
include/drm/drm_print.h +37 -0
index 089950ad8681..58cc73c53853 100644
@@ -186,6 +186,7 @@ void __drm_puts_seq_file(struct drm_printer *p, const char *str);
 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf);
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf);
 
 __printf(2, 3)
 void drm_printf(struct drm_printer *p, const char *f, ...);
@@ -357,6 +358,42 @@ static inline struct drm_printer drm_err_printer(struct drm_device *drm,
 	return p;
 }
 
+/**
+ * drm_line_printer - construct a &drm_printer that prefixes outputs with line numbers
+ * @dp: the &struct drm_printer which actually generates the output
+ *
+ * This printer can be used to increase the robustness of the captured output
+ * to make sure we didn't lost any intermediate lines of the output. Helpful
+ * while capturing some crash data.
+ *
+ * For example::
+ *
+ *	void crash_dump(struct drm_device *drm)
+ *	{
+ *		struct drm_printer dp = drm_err_printer(drm, "crash");
+ *		struct drm_printer lp = drm_line_printer(&dp);
+ *
+ *		drm_printf(&lp, "foo");
+ *		drm_printf(&lp, "bar");
+ *	}
+ *
+ * Above code will print into the dmesg something like::
+ *
+ *	[ ] 0000:00:00.0: [drm] *ERROR* crash 1: foo
+ *	[ ] 0000:00:00.0: [drm] *ERROR* crash 2: bar
+ *
+ * RETURNS:
+ * The &drm_printer object
+ */
+static inline struct drm_printer drm_line_printer(struct drm_printer *dp)
+{
+	struct drm_printer lp = {
+		.printfn = __drm_printfn_line,
+		.arg = dp,
+	};
+	return lp;
+}
+
 /*
  * struct device based logging
  *
Signature · 1 line
2.43.0
#1 lore raw
Quoted 4 lines · Michal Wajdeczko

This drm printer wrapper can be used to increase the robustness of the captured output generated by any other drm_printer to make sure we didn't lost any intermediate lines of the output by adding line numbers to each output line. Helpful for capturing some crash data.

Interesting. Nothing another level of abstraction can't solve. ;)

Except maybe it'll make adding function names to debug printers harder.

No strong feelings either way about it, I'll let others chime in.

A few comments inline.

BR,
Jani.

Quoted 12 lines
---
 drivers/gpu/drm/drm_print.c |  9 +++++++++
 include/drm/drm_print.h     | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
drivers/gpu/drm/drm_print.c +0 -0
index cf2efb44722c..d6fb50d3407a 100644

drivers/gpu/drm/drm_print.c void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf) · line 219

Quoted 6 lines
drivers/gpu/drm/drm_print.c +3 -0
@@ -214,6 +214,15 @@ void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
 }
 EXPORT_SYMBOL(__drm_printfn_err);
 
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf)
+{
+	unsigned int line = (uintptr_t)(++p->prefix);

Subtle. Might warrant adding a union in struct drm_printer for clarity.

Quoted 43 lines
drivers/gpu/drm/drm_print.c +6 -0
+	struct drm_printer *dp = p->arg;
+
+	drm_printf(dp, "%u: %pV", line, vaf);
+}
+EXPORT_SYMBOL(__drm_printfn_line);
+
 /**
  * drm_puts - print a const string to a &drm_printer stream
  * @p: the &drm printer
include/drm/drm_print.h +19 -0
index 089950ad8681..58cc73c53853 100644
@@ -186,6 +186,7 @@ void __drm_puts_seq_file(struct drm_printer *p, const char *str);
 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf);
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf);
 
 __printf(2, 3)
 void drm_printf(struct drm_printer *p, const char *f, ...);
@@ -357,6 +358,42 @@ static inline struct drm_printer drm_err_printer(struct drm_device *drm,
 	return p;
 }
 
+/**
+ * drm_line_printer - construct a &drm_printer that prefixes outputs with line numbers
+ * @dp: the &struct drm_printer which actually generates the output
+ *
+ * This printer can be used to increase the robustness of the captured output
+ * to make sure we didn't lost any intermediate lines of the output. Helpful
+ * while capturing some crash data.
+ *
+ * For example::
+ *
+ *	void crash_dump(struct drm_device *drm)
+ *	{
+ *		struct drm_printer dp = drm_err_printer(drm, "crash");
+ *		struct drm_printer lp = drm_line_printer(&dp);
+ *
+ *		drm_printf(&lp, "foo");
+ *		drm_printf(&lp, "bar");
+ *	}

include/drm/drm_print.h static inline struct drm_printer drm_err_printer(struct drm_device *drm, · line 388

Quoted 10 lines
include/drm/drm_print.h +10 -0
+ *
+ * Above code will print into the dmesg something like::
+ *
+ *	[ ] 0000:00:00.0: [drm] *ERROR* crash 1: foo
+ *	[ ] 0000:00:00.0: [drm] *ERROR* crash 2: bar
+ *
+ * RETURNS:
+ * The &drm_printer object
+ */
+static inline struct drm_printer drm_line_printer(struct drm_printer *dp)

Just p is customary for the drm_printer. "dp" gives me too much Display Port vibes.

Quoted 11 lines
include/drm/drm_print.h +8 -0
+{
+	struct drm_printer lp = {
+		.printfn = __drm_printfn_line,
+		.arg = dp,
+	};
+	return lp;
+}
+
 /*
  * struct device based logging
  *
Signature · 1 line
Jani Nikula, Intel
#2 lore raw
Quoted 9 lines · Jani Nikula
Quoted 4 lines · Michal Wajdeczko

This drm printer wrapper can be used to increase the robustness of the captured output generated by any other drm_printer to make sure we didn't lost any intermediate lines of the output by adding line numbers to each output line. Helpful for capturing some crash data.

Interesting. Nothing another level of abstraction can't solve. ;)

Except maybe it'll make adding function names to debug printers harder.

but why? primary printer should work in the same way with or without this line printer

Quoted 2 lines

No strong feelings either way about it, I'll let others chime in.

forgot to mention that this new printer was aimed to simplify the manual and error prone work done as part of [1] but I'm afraid there is little traction to have any kind of generic solution at all, since it is considered as 'over engineering', even at the cost of trashing other printers that don't need extra robustness

[1] https://patchwork.freedesktop.org/patch/593223/?series=133349&rev=1

Quoted 22 lines

A few comments inline.

BR,
Jani.

Quoted 15 lines
---
 drivers/gpu/drm/drm_print.c |  9 +++++++++
 include/drm/drm_print.h     | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
drivers/gpu/drm/drm_print.c +0 -0
index cf2efb44722c..d6fb50d3407a 100644
@@ -214,6 +214,15 @@ void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
 }
 EXPORT_SYMBOL(__drm_printfn_err);
 
Quoted 5 lines

drivers/gpu/drm/drm_print.c void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf) · line 219

Quoted 3 lines
drivers/gpu/drm/drm_print.c +3 -0
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf)
+{
+	unsigned int line = (uintptr_t)(++p->prefix);

Subtle. Might warrant adding a union in struct drm_printer for clarity.

good idea

Quoted 49 lines
Quoted 49 lines
drivers/gpu/drm/drm_print.c +6 -0
+	struct drm_printer *dp = p->arg;
+
+	drm_printf(dp, "%u: %pV", line, vaf);
+}
+EXPORT_SYMBOL(__drm_printfn_line);
+
 /**
  * drm_puts - print a const string to a &drm_printer stream
  * @p: the &drm printer
include/drm/drm_print.h +24 -0
index 089950ad8681..58cc73c53853 100644
@@ -186,6 +186,7 @@ void __drm_puts_seq_file(struct drm_printer *p, const char *str);
 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf);
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf);
 
 __printf(2, 3)
 void drm_printf(struct drm_printer *p, const char *f, ...);
@@ -357,6 +358,42 @@ static inline struct drm_printer drm_err_printer(struct drm_device *drm,
 	return p;
 }
 
+/**
+ * drm_line_printer - construct a &drm_printer that prefixes outputs with line numbers
+ * @dp: the &struct drm_printer which actually generates the output
+ *
+ * This printer can be used to increase the robustness of the captured output
+ * to make sure we didn't lost any intermediate lines of the output. Helpful
+ * while capturing some crash data.
+ *
+ * For example::
+ *
+ *	void crash_dump(struct drm_device *drm)
+ *	{
+ *		struct drm_printer dp = drm_err_printer(drm, "crash");
+ *		struct drm_printer lp = drm_line_printer(&dp);
+ *
+ *		drm_printf(&lp, "foo");
+ *		drm_printf(&lp, "bar");
+ *	}
+ *
+ * Above code will print into the dmesg something like::
+ *
+ *	[ ] 0000:00:00.0: [drm] *ERROR* crash 1: foo
+ *	[ ] 0000:00:00.0: [drm] *ERROR* crash 2: bar
Quoted 8 lines

include/drm/drm_print.h static inline struct drm_printer drm_err_printer(struct drm_device *drm, · line 388

Quoted 5 lines
include/drm/drm_print.h +5 -0
+ *
+ * RETURNS:
+ * The &drm_printer object
+ */
+static inline struct drm_printer drm_line_printer(struct drm_printer *dp)

Just p is customary for the drm_printer. "dp" gives me too much Display Port vibes.

ha, it was 'p' initially, but then it was still asymmetric compared to __drm_printfn_line where 'p' was actually referring to the line printer

maybe best option would be to don't have local vars at all:

drm_printf((struct drm_printer *)p->arg, "%u: %pV", p->line, vaf);

Quoted 13 lines
Quoted 12 lines
include/drm/drm_print.h +8 -0
+{
+	struct drm_printer lp = {
+		.printfn = __drm_printfn_line,
+		.arg = dp,
+	};
+	return lp;
+}
+
 /*
  * struct device based logging
  *
#3 lore raw
Quoted 7 lines · Michal Wajdeczko
Quoted 6 lines · Jani Nikula
Quoted 4 lines · Michal Wajdeczko

This drm printer wrapper can be used to increase the robustness of the captured output generated by any other drm_printer to make sure we didn't lost any intermediate lines of the output by adding line numbers to each output line. Helpful for capturing some crash data.

Quoted 6 lines
Quoted 3 lines

Interesting. Nothing another level of abstraction can't solve. ;)

Except maybe it'll make adding function names to debug printers harder.

but why? primary printer should work in the same way with or without this line printer

Because of __builtin_return_address(0). But currently __drm_printfn_dbg() doesn't use that anyway, because it would already be off...

Not really a meaningful argument against this patch, to be fair.

Quoted 99 lines
Quoted 2 lines

No strong feelings either way about it, I'll let others chime in.

forgot to mention that this new printer was aimed to simplify the manual and error prone work done as part of [1] but I'm afraid there is little traction to have any kind of generic solution at all, since it is considered as 'over engineering', even at the cost of trashing other printers that don't need extra robustness

[1] https://patchwork.freedesktop.org/patch/593223/?series=133349&rev=1

Quoted 22 lines

A few comments inline.

BR,
Jani.

Quoted 15 lines
---
 drivers/gpu/drm/drm_print.c |  9 +++++++++
 include/drm/drm_print.h     | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
drivers/gpu/drm/drm_print.c +0 -0
index cf2efb44722c..d6fb50d3407a 100644
@@ -214,6 +214,15 @@ void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf)
 }
 EXPORT_SYMBOL(__drm_printfn_err);
 
Quoted 5 lines

drivers/gpu/drm/drm_print.c void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf) · line 219

Quoted 3 lines
drivers/gpu/drm/drm_print.c +3 -0
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf)
+{
+	unsigned int line = (uintptr_t)(++p->prefix);

Subtle. Might warrant adding a union in struct drm_printer for clarity.

good idea

Quoted 49 lines
Quoted 49 lines
drivers/gpu/drm/drm_print.c +6 -0
+	struct drm_printer *dp = p->arg;
+
+	drm_printf(dp, "%u: %pV", line, vaf);
+}
+EXPORT_SYMBOL(__drm_printfn_line);
+
 /**
  * drm_puts - print a const string to a &drm_printer stream
  * @p: the &drm printer
include/drm/drm_print.h +24 -0
index 089950ad8681..58cc73c53853 100644
@@ -186,6 +186,7 @@ void __drm_puts_seq_file(struct drm_printer *p, const char *str);
 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf);
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf);
 
 __printf(2, 3)
 void drm_printf(struct drm_printer *p, const char *f, ...);
@@ -357,6 +358,42 @@ static inline struct drm_printer drm_err_printer(struct drm_device *drm,
 	return p;
 }
 
+/**
+ * drm_line_printer - construct a &drm_printer that prefixes outputs with line numbers
+ * @dp: the &struct drm_printer which actually generates the output
+ *
+ * This printer can be used to increase the robustness of the captured output
+ * to make sure we didn't lost any intermediate lines of the output. Helpful
+ * while capturing some crash data.
+ *
+ * For example::
+ *
+ *	void crash_dump(struct drm_device *drm)
+ *	{
+ *		struct drm_printer dp = drm_err_printer(drm, "crash");
+ *		struct drm_printer lp = drm_line_printer(&dp);
+ *
+ *		drm_printf(&lp, "foo");
+ *		drm_printf(&lp, "bar");
+ *	}
+ *
+ * Above code will print into the dmesg something like::
+ *
+ *	[ ] 0000:00:00.0: [drm] *ERROR* crash 1: foo
+ *	[ ] 0000:00:00.0: [drm] *ERROR* crash 2: bar
Quoted 8 lines

include/drm/drm_print.h static inline struct drm_printer drm_err_printer(struct drm_device *drm, · line 388

Quoted 5 lines
include/drm/drm_print.h +5 -0
+ *
+ * RETURNS:
+ * The &drm_printer object
+ */
+static inline struct drm_printer drm_line_printer(struct drm_printer *dp)

Just p is customary for the drm_printer. "dp" gives me too much Display Port vibes.

Quoted 6 lines

ha, it was 'p' initially, but then it was still asymmetric compared to __drm_printfn_line where 'p' was actually referring to the line printer

maybe best option would be to don't have local vars at all:

drm_printf((struct drm_printer *)p->arg, "%u: %pV", p->line, vaf);

The cast is actually not required.

BR,
Jani.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
include/drm/drm_print.h +8 -0
+{
+	struct drm_printer lp = {
+		.printfn = __drm_printfn_line,
+		.arg = dp,
+	};
+	return lp;
+}
+
 /*
  * struct device based logging
  *
Signature · 1 line
Jani Nikula, Intel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw
Quoted 24 lines · Daniele Ceraolo Spurio

-------- Forwarded Message --------
Subject: [RFC] drm/print: Introduce drm_line_printer
Date: Tue, 14 May 2024 16:56:31 +0200
From: Michal Wajdeczko <[email protected]>
To: [email protected]

This drm printer wrapper can be used to increase the robustness of the captured output generated by any other drm_printer to make sure we didn't lost any intermediate lines of the output by adding line numbers to each output line. Helpful for capturing some crash data.

---
drivers/gpu/drm/drm_print.c | 9 +++++++++ include/drm/drm_print.h | 37 +++++++++++++++++++++++++++++++++++++

2 files changed, 46 insertions(+)
drivers/gpu/drm/drm_print.c +0 -0
index cf2efb44722c..d6fb50d3407a 100644

drivers/gpu/drm/drm_print.c void __drm_printfn_err(struct drm_printer *p, · line 219

Quoted 6 lines
drivers/gpu/drm/drm_print.c +3 -0
@@ -214,6 +214,15 @@ void __drm_printfn_err(struct drm_printer *p, 
 struct va_format *vaf)
 }
 EXPORT_SYMBOL(__drm_printfn_err);
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf)
+{
+ unsigned int line = (uintptr_t)(++p->prefix);

The prefix field is officially supposed to be a const char *. There is no documentation to say that this is intended to be used as a private data field by random printer wrappers. So overloading it like this feels very hacky and dangerous. Also, you are mixing types - uintptr_t then uint. So an arch with 64-bit pointers but only 32-bit ints would hit a truncated compiler warning?

drivers/gpu/drm/drm_print.c void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf) · line 222

Quoted 3 lines
drivers/gpu/drm/drm_print.c +3 -0
+ struct drm_printer *dp = p->arg;
+
+ drm_printf(dp, "%u: %pV", line, vaf);

This is insufficient. As previously commented, there needs to be a global counter as well as a local line counter. The global count must be global to at least whatever entity is generating a specific set of prints. Being global to a higher level, e.g. kernel global, is fine. But without that, two concurrent dumps that get interleaved can be impossible to separate resulting in a useless bug report/log.

The prefix field could potentially be split into a 16:16 global:local index with the global master just being a static u16 inside that function. With the first print call to a given drm_printer object being defined by the global value being zero. And it then sets the global value to the next increment skipping over zero on a 16-bit wrap around. But see above about prefix not being intended for such purposes. So now you are just piling hacks upon hacks.

Plus it would be much nicer output to have the ability to put an arbitrary prefix in front of the G.L number, as per the original implementation. The whole point of this is to aid identification of otherwise uniform data such as hexdumps. So anything that makes it less clear is bad.

John.

Quoted 67 lines
drivers/gpu/drm/drm_print.c +3 -0
+}
+EXPORT_SYMBOL(__drm_printfn_line);
+
 /**
 * drm_puts - print a const string to a &drm_printer stream
 * @p: the &drm printer
include/drm/drm_print.h +3 -0
index 089950ad8681..58cc73c53853 100644
@@ -186,6 +186,7 @@ void __drm_puts_seq_file(struct drm_printer *p, 
 const char *str);
 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf);
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf);
 __printf(2, 3)
 void drm_printf(struct drm_printer *p, const char *f, ...);
@@ -357,6 +358,42 @@ static inline struct drm_printer 
 drm_err_printer(struct drm_device *drm,
 return p;
 }
+/**
+ * drm_line_printer - construct a &drm_printer that prefixes outputs 

with line numbers
+ * @dp: the &struct drm_printer which actually generates the output
+ *
+ * This printer can be used to increase the robustness of the captured output
+ * to make sure we didn't lost any intermediate lines of the output. Helpful
+ * while capturing some crash data.
+ *
+ * For example::
+ *
+ * void crash_dump(struct drm_device *drm)
+ * {
+ * struct drm_printer dp = drm_err_printer(drm, "crash");
+ * struct drm_printer lp = drm_line_printer(&dp);
+ *
+ * drm_printf(&lp, "foo");
+ * drm_printf(&lp, "bar");
+ * }
+ *
+ * Above code will print into the dmesg something like::
+ *
+ * [ ] 0000:00:00.0: [drm] *ERROR* crash 1: foo
+ * [ ] 0000:00:00.0: [drm] *ERROR* crash 2: bar
+ *
+ * RETURNS:
+ * The &drm_printer object
+ */

include/drm/drm_print.h +9 -0
+static inline struct drm_printer drm_line_printer(struct drm_printer *dp)
+{
+ struct drm_printer lp = {
+ .printfn = __drm_printfn_line,
+ .arg = dp,
+ };
+ return lp;
+}
+

/*
* struct device based logging
*

Signature · 1 line
2.43.0
#6 lore raw
Quoted 29 lines · John Harrison
Quoted 28 lines · Daniele Ceraolo Spurio

-------- Forwarded Message --------
Subject: [RFC] drm/print: Introduce drm_line_printer
Date: Tue, 14 May 2024 16:56:31 +0200
From: Michal Wajdeczko <[email protected]>
To: [email protected]

This drm printer wrapper can be used to increase the robustness of the captured output generated by any other drm_printer to make sure we didn't lost any intermediate lines of the output by adding line numbers to each output line. Helpful for capturing some crash data.

---
drivers/gpu/drm/drm_print.c | 9 +++++++++ include/drm/drm_print.h | 37 +++++++++++++++++++++++++++++++++++++

2 files changed, 46 insertions(+)
drivers/gpu/drm/drm_print.c +1 -0
index cf2efb44722c..d6fb50d3407a 100644
@@ -214,6 +214,15 @@ void __drm_printfn_err(struct drm_printer *p, 
 struct va_format *vaf)
 }
 EXPORT_SYMBOL(__drm_printfn_err);
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf)
Quoted 8 lines

drivers/gpu/drm/drm_print.c void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf) · line 219

Quoted 2 lines
drivers/gpu/drm/drm_print.c +2 -0
+{
+ unsigned int line = (uintptr_t)(++p->prefix);

The prefix field is officially supposed to be a const char *. There is no documentation to say that this is intended to be used as a private data field by random printer wrappers. So overloading it like this feels very hacky and dangerous. Also, you are mixing types - uintptr_t then uint. So an arch with 64-bit pointers but only 32-bit ints would hit a truncated compiler warning?

I already commented on abusing the type in another reply. I think making prefix a union would dodge that issue. Otherwise, I think each printer can pretty much use the arg and prefix members as they see fit. It's not mucking the printer being wrapped.

Quoted 11 lines

drivers/gpu/drm/drm_print.c void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf) · line 222

Quoted 4 lines
drivers/gpu/drm/drm_print.c +3 -0
+ struct drm_printer *dp = p->arg;
+
+ drm_printf(dp, "%u: %pV", line, vaf);

This is insufficient. As previously commented, there needs to be a global counter as well as a local line counter. The global count must be global to at least whatever entity is generating a specific set of prints. Being global to a higher level, e.g. kernel global, is fine. But without that, two concurrent dumps that get interleaved can be impossible to separate resulting in a useless bug report/log.

Quoted 7 lines

The prefix field could potentially be split into a 16:16 global:local index with the global master just being a static u16 inside that function. With the first print call to a given drm_printer object being defined by the global value being zero. And it then sets the global value to the next increment skipping over zero on a 16-bit wrap around. But see above about prefix not being intended for such purposes. So now you are just piling hacks upon hacks.

To tackle that issue, I think I'd add a "unique id" kind of parameter to drm_line_printer(). If the user needs more uniqueness, they can maintain it locally (in a data structure or static or whatever) and id++ it. Could even skip it in printing if set to 0 if it's not likely the caller needs it. This dodges the issue of having to store global stuff in the printer, and keeps output lean when not needed.

For example:

        static int id;
	struct drm_printer dp = drm_err_printer(drm, "crash");
	struct drm_printer lp = drm_line_printer(&dp, ++id);
Quoted 5 lines

Plus it would be much nicer output to have the ability to put an arbitrary prefix in front of the G.L number, as per the original implementation. The whole point of this is to aid identification of otherwise uniform data such as hexdumps. So anything that makes it less clear is bad.

The prefix in the printer being wrapped is intact, so you could add it there. In the above example, it's "crash".

BR,
Jani.

Quoted 71 lines

John.

Quoted 67 lines
drivers/gpu/drm/drm_print.c +3 -0
+}
+EXPORT_SYMBOL(__drm_printfn_line);
+
 /**
 * drm_puts - print a const string to a &drm_printer stream
 * @p: the &drm printer
include/drm/drm_print.h +3 -0
index 089950ad8681..58cc73c53853 100644
@@ -186,6 +186,7 @@ void __drm_puts_seq_file(struct drm_printer *p, 
 const char *str);
 void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_dbg(struct drm_printer *p, struct va_format *vaf);
 void __drm_printfn_err(struct drm_printer *p, struct va_format *vaf);
+void __drm_printfn_line(struct drm_printer *p, struct va_format *vaf);
 __printf(2, 3)
 void drm_printf(struct drm_printer *p, const char *f, ...);
@@ -357,6 +358,42 @@ static inline struct drm_printer 
 drm_err_printer(struct drm_device *drm,
 return p;
 }
+/**
+ * drm_line_printer - construct a &drm_printer that prefixes outputs 

with line numbers
+ * @dp: the &struct drm_printer which actually generates the output
+ *
+ * This printer can be used to increase the robustness of the captured output
+ * to make sure we didn't lost any intermediate lines of the output. Helpful
+ * while capturing some crash data.
+ *
+ * For example::
+ *
+ * void crash_dump(struct drm_device *drm)
+ * {
+ * struct drm_printer dp = drm_err_printer(drm, "crash");
+ * struct drm_printer lp = drm_line_printer(&dp);
+ *
+ * drm_printf(&lp, "foo");
+ * drm_printf(&lp, "bar");
+ * }
+ *
+ * Above code will print into the dmesg something like::
+ *
+ * [ ] 0000:00:00.0: [drm] *ERROR* crash 1: foo
+ * [ ] 0000:00:00.0: [drm] *ERROR* crash 2: bar
+ *
+ * RETURNS:
+ * The &drm_printer object
+ */

include/drm/drm_print.h +9 -0
+static inline struct drm_printer drm_line_printer(struct drm_printer *dp)
+{
+ struct drm_printer lp = {
+ .printfn = __drm_printfn_line,
+ .arg = dp,
+ };
+ return lp;
+}
+

/*
* struct device based logging
*

Signature · 1 line
2.43.0
Signature · 1 line
Jani Nikula, Intel