Skip to contentSkip to conversation

Improve logging on missing uclass

PATCH In review Julien Stephan 3 participants 9 messages Aug 24 – Oct 21

lore
Share & tools
lore
#0 lore raw

v2 cover Improve logging on missing uclass

While writing a MISC driver, I forgot to enable CONFIG_MISC. The only thing I got at boot was the following obscure error, with no hint about the root cause:

initcall_run_r(): initcall initr_dm() failed
### ERROR ### Please RESET the board ###

Commit 3346c87625b8 ("dm: Improve handling of a missing uclass") already tried to improve this case by returning a strange error code and adding a debug message. But since neither the debug message nor the return code are displayed by default, it was not very helpful in practice.

This series improves the situation in a few ways:
- a new dm_err() helper logs driver model errors at ERROR level, and the boolean DM_WARN option becomes a per-stage log verbosity choice (DM_NONE / DM_ERR / DM_WARN) with errors emitted by default. Note that a disabled level is not removed entirely but degraded to DEBUG level, which is compiled out by default, so even DM_NONE can still emit these messages as debug output when debug logging is enabled;
- the missing-uclass message now uses dm_err(), so it shows up by default, with a shorter string to limit the code-size overhead;
- the initcall failure now prints the actual error code (and its string when ERRNO_STR is enabled), for both INITCALL() and INITCALL_EVT().

The existing DM_DEBUG option is left as-is for now: it works differently (it adds -DDEBUG to drivers/core/ via the Makefile and is not phase-aware). Unifying it into the new choice, along with converting the DM core debug() statements to log() as suggested in the "To Do" section of doc/develop/logging.rst, could be a follow-up series.

---
Changes in v2:
- dm: replace the boolean DM_WARN option with a per-stage log verbosity
choice (DM_NONE / DM_ERR / DM_WARN) and add a dm_err() helper, rather
than logging the missing-uclass message with a hardcoded log() call
(suggested by Simon)
- dm: de-duplicate the util.h helpers to only select the log level
(suggested by Simon)
- dm: split the change into the generic mechanism and its user in uclass.c
- dm: shorten the missing-uclass message to limit the SPL/TPL size overhead
- initcall: use the %dE format so no dangling ": " is printed when
ERRNO_STR is disabled, and give INITCALL_EVT() the same treatment
- Link to v1: https://patch.msgid.link/20260806-improve-logging-on-missing-uclass-v1-0-d477f0054f48@baylibre.com

To: [email protected]
Cc: GSS_MTK_Uboot_upstream <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: "Markus Schneider-Pargmann (TI)" <[email protected]>
Cc: Julien Stephan <[email protected]>

---
Julien Stephan (3):
dm: add dm_err() and a driver model log verbosity choice
dm: uclass: use dm_err() for a missing uclass
initcall: initcall_evt: display error code on error

 drivers/core/Kconfig  | 75 ++++++++++++++++++++++++++++++++++++++++++---------
 drivers/core/uclass.c |  3 +--
 include/dm/util.h     | 19 +++++++++++--
 include/initcall.h    | 14 +++++-----
 4 files changed, 88 insertions(+), 23 deletions(-)

---
base-commit: f562ed02aa7ae4dc2775d90d2e6c40bab82e2715
change-id: 20260806-improve-logging-on-missing-uclass-a76c46acfa13

Best regards,

Signature · 1 line
Julien Stephan <[email protected]>
#1 lore raw

PATCH v2 1/3 dm: add dm_err() and a driver model log verbosity choice

When a fatal driver model error occurs the board often cannot boot, yet without any extra debug option enabled such errors are not shown: driver model only had dm_warn(), emitted at WARNING level (or DEBUG when DM_WARN was disabled). A fatal error deserves a message that is visible out of the box.

Add a dm_err() helper, logging at ERROR level, and replace the DM_WARN / SPL_DM_WARN boolean options with a per-stage verbosity choice:

- DM_NONE: degrade all messages to DEBUG level (smallest size);
- DM_ERR: emit dm_err() at ERROR level only (default);
- DM_WARN: emit both dm_warn() and dm_err().

Each level includes the ones below it, so selecting warnings never silences errors. A disabled level is not removed entirely: it degrades to DEBUG level, which is compiled out by default but can still be displayed when debug logging is enabled (e.g. DEBUG defined or a high enough log level). So even DM_NONE can still show these messages as debug output. Production builds can drop the strings by selecting a lower level, while a fatal error is shown by default during development, including in SPL/TPL.

Note this changes the default behaviour: driver model errors are now emitted by default (previously nothing was shown unless DM_WARN was enabled).

The existing DM_DEBUG option is deliberately left untouched here. It works
differently: it adds -DDEBUG to drivers/core/ via the Makefile rather than selecting a log level, and it is not phase-aware. Unifying it into this choice, along with converting the DM core debug() statements to log()/dm_dbg() (as recommended in the "To Do" section of doc/develop/logging.rst), could be done as a follow-up series.

---
 drivers/core/Kconfig | 75 +++++++++++++++++++++++++++++++++++++++++++---------
 include/dm/util.h    | 19 +++++++++++--
 2 files changed, 79 insertions(+), 15 deletions(-)
drivers/core/Kconfig +62 -13
index ae0c3466772..5e276474b4b 100644
@@ -45,28 +45,77 @@ config VPL_DM
 	  full malloc() enabled by CFG_TPL_SYS_MALLOC_START,
 	  consider using CONFIG_TPL_SYS_MALLOC_SIMPLE.
 
-config DM_WARN
-	bool "Enable warnings in driver model"
+choice
+	prompt "Driver model log verbosity"
 	depends on DM
+	default DM_ERR
+	help
+	  Select which driver model messages are compiled into the binary.
+	  Each level includes the levels below it. Lower verbosity saves code
+	  size by compiling the message strings out (useful for production
+	  builds), while higher verbosity helps with debugging.
+
+config DM_NONE
+	bool "No messages"
 	help
-	  Enable this to see warnings related to driver model.
+	  Compile out all driver model messages. This gives the smallest code
+	  size, but a fatal condition gives no hint about its root cause.
+
+config DM_ERR
+	bool "Errors"
+	help
+	  Emit driver model error messages via dm_err(), at ERROR level.
+	  These report fatal conditions that typically prevent the board from
+	  booting. Warnings are compiled out.
+
+config DM_WARN
+	bool "Warnings and errors"
+	help
+	  Emit driver model warnings via dm_warn() (WARNING level) in addition
+	  to errors. This is the most verbose and uses the most code space.
 
 	  Warnings may help with debugging, such as when expected devices do
-	  not bind correctly. If the option is disabled, dm_warn() is compiled
-	  out - it will do nothing when called.
+	  not bind correctly.
 
-config SPL_DM_WARN
-	bool "Enable warnings in driver model in SPL"
+endchoice
+
+choice
+	prompt "Driver model log verbosity in SPL"
 	depends on SPL_DM
+	default SPL_DM_ERR
 	help
-	  Enable this to see warnings related to driver model in SPL
+	  Select which driver model messages are compiled into the SPL binary.
+	  See the "Driver model log verbosity" choice for details.
 
-	  The dm_warn() function can use up quite a bit of space for its
-	  strings. By default this is disabled for SPL builds to save space.
+config SPL_DM_NONE
+	bool "No messages"
 
-	  Warnings may help with debugging, such as when expected devices do
-	  not bind correctly. If the option is disabled, dm_warn() is compiled
-	  out - it will do nothing when called.
+config SPL_DM_ERR
+	bool "Errors"
+
+config SPL_DM_WARN
+	bool "Warnings and errors"
+
+endchoice
+
+choice
+	prompt "Driver model log verbosity in TPL"
+	depends on TPL_DM
+	default TPL_DM_ERR
+	help
+	  Select which driver model messages are compiled into the TPL binary.
+	  See the "Driver model log verbosity" choice for details.
+
+config TPL_DM_NONE
+	bool "No messages"
+
+config TPL_DM_ERR
+	bool "Errors"
+
+config TPL_DM_WARN
+	bool "Warnings and errors"
+
+endchoice
 
 config DM_DEBUG
 	bool "Enable debug messages in driver model core"
include/dm/util.h +17 -2
index ec518c51d93..89ffaa467aa 100644
@@ -8,12 +8,27 @@
 
 struct dm_stats;
 
+/*
+ * Pick the log level for each helper based on the DM log verbosity choice.
+ * A disabled level falls back to LOGL_DEBUG, which is compiled out by
+ * default. Each level includes the ones below it, so DM_WARN also enables
+ * errors.
+ */
 #if CONFIG_IS_ENABLED(DM_WARN)
-#define dm_warn(fmt...) log(LOGC_DM, LOGL_WARNING, ##fmt)
+#define _DM_WARN_LEVEL	LOGL_WARNING
+#else
+#define _DM_WARN_LEVEL	LOGL_DEBUG
+#endif
+
+#if CONFIG_IS_ENABLED(DM_WARN) || CONFIG_IS_ENABLED(DM_ERR)
+#define _DM_ERR_LEVEL	LOGL_ERR
 #else
-#define dm_warn(fmt...) log(LOGC_DM, LOGL_DEBUG, ##fmt)
+#define _DM_ERR_LEVEL	LOGL_DEBUG
 #endif
 
+#define dm_warn(fmt...) log(LOGC_DM, _DM_WARN_LEVEL, ##fmt)
+#define dm_err(fmt...) log(LOGC_DM, _DM_ERR_LEVEL, ##fmt)
+
 struct list_head;
 
 /**
Signature · 1 line
2.54.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] dm: add dm_err() and a driver model log verbosity choice

Quoted 34 lines · Julien Stephan

dm: add dm_err() and a driver model log verbosity choice

When a fatal driver model error occurs the board often cannot boot, yet without any extra debug option enabled such errors are not shown: driver model only had dm_warn(), emitted at WARNING level (or DEBUG when DM_WARN was disabled). A fatal error deserves a message that is visible out of the box.

Add a dm_err() helper, logging at ERROR level, and replace the DM_WARN / SPL_DM_WARN boolean options with a per-stage verbosity choice:

- DM_NONE: degrade all messages to DEBUG level (smallest size);
- DM_ERR: emit dm_err() at ERROR level only (default);
- DM_WARN: emit both dm_warn() and dm_err().

Each level includes the ones below it, so selecting warnings never silences errors. A disabled level is not removed entirely: it degrades to DEBUG level, which is compiled out by default but can still be displayed when debug logging is enabled (e.g. DEBUG defined or a high enough log level). So even DM_NONE can still show these messages as debug output. Production builds can drop the strings by selecting a lower level, while a fatal error is shown by default during development, including in SPL/TPL.

Note this changes the default behaviour: driver model errors are now emitted by default (previously nothing was shown unless DM_WARN was enabled).

The existing DM_DEBUG option is deliberately left untouched here. It works
differently: it adds -DDEBUG to drivers/core/ via the Makefile rather than selecting a log level, and it is not phase-aware. Unifying it into this choice, along with converting the DM core debug() statements to log()/dm_dbg() (as recommended in the "To Do" section of doc/develop/logging.rst), could be done as a follow-up series.

Quoted 5 lines
 drivers/core/Kconfig | 75 +++++++++++++++++++++++++++++++++++++++++++---------
 include/dm/util.h    | 19 +++++++++++--
 2 files changed, 79 insertions(+), 15 deletions(-)
#3 lore raw

PATCH v2 2/3 dm: uclass: use dm_err() for a missing uclass

In reply to Julien Stephan

When a uclass definition is missing for an enabled driver, the board cannot boot, and without any extra debug option enabled the only output is an obscure message with no hint about the root cause:

initcall_run_r(): initcall initr_dm() failed
### ERROR ### Please RESET the board ###

The existing message used dm_warn(), which is not shown by default. Since this is a fatal error preventing the board from booting, use the new dm_err() helper so it is emitted at ERROR level and visible by default. Shorten the message as well to limit the code size overhead.

---
 drivers/core/uclass.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/core/uclass.c +1 -2
index 5365ac68f9e..83a67dfb4de 100644
@@ -60,8 +60,7 @@ static int uclass_add(enum uclass_id id, struct uclass **ucp)
 	*ucp = NULL;
 	uc_drv = lists_uclass_lookup(id);
 	if (!uc_drv) {
-		dm_warn("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
-			id);
+		dm_err("uclass %d: no UCLASS_DRIVER()\n", id);
 		/*
 		 * Use a strange error to make this case easier to find. When
 		 * a uclass is not available it can prevent driver model from
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH v2 2/3] dm: uclass: use dm_err() for a missing uclass

Hi Julien,

Quoted 6 lines · Julien Stephan

dm: uclass: use dm_err() for a missing uclass

When a uclass definition is missing for an enabled driver, the board cannot boot, and without any extra debug option enabled the only output is an obscure message with no hint about the root cause:

Quoted 6 lines

initcall_run_r(): initcall initr_dm() failed
### ERROR ### Please RESET the board ###

The existing message used dm_warn(), which is not shown by default. Since this is a fatal error preventing the board from booting, use the new dm_err() helper so it is emitted at ERROR level and visible by default.

But a missing uclass is not always fatal. Any uclass_get() can reach uclass_add(), and some callers treat -EPFNOSUPPORT as a normal 'not present' result. For example, nvmem_cell_get_by_index() tries UCLASS_I2C_EEPROM, UCLASS_MISC and UCLASS_RTC in turn and skips -EPFNOSUPPORT, and board/samsung/common/board.c does the same for UCLASS_CROS_EC, too. With this patch, a board without one of those uclasses would print an error on every lookup even though nothing is wrong. Please can you check whether this causes the CI failures Tom mentioned? It may be better to report the error only on the path that is actually fatal (e.g. binding a driver whose uclass is missing during dm_init_and_scan()), or to adjust these callers so that the message stays accurate.

Quoted 6 lines

Shorten the message as well to limit the code size overhead.

 drivers/core/uclass.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Regards,
Simon

#5 lore raw

PATCH v2 3/3 initcall: initcall_evt: display error code on error

In reply to Julien Stephan

Currently when an initcall/initcall_evt fails the error code is not displayed. Display it, along with the corresponding error string if ERRNO_STR is enabled.

---
 include/initcall.h | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
include/initcall.h +8 -6
index 220a55ad84d..12e5235f506 100644
@@ -14,18 +14,20 @@ _Static_assert(EVT_COUNT < 256, "Can only support 256 event types with 8 bits");
 
 #define INITCALL(_call) \
 	do { \
-		if (_call()) { \
-			printf("%s(): initcall %s() failed\n", __func__, \
-			       #_call); \
+		int _ret = _call(); \
+		if (_ret) { \
+			printf("%s(): initcall %s() failed (err=%dE)\n", \
+			       __func__, #_call, _ret); \
 			hang(); \
 		} \
 	} while (0)
 
 #define INITCALL_EVT(_evt) \
 	do { \
-		if (event_notify_null(_evt)) { \
-			printf("%s(): event %d/%s failed\n", __func__, _evt, \
-			       event_type_name(_evt)) ; \
+		int _ret = event_notify_null(_evt); \
+		if (_ret) { \
+			printf("%s(): event %d/%s failed (err=%dE)\n", \
+			       __func__, _evt, event_type_name(_evt), _ret); \
 			hang(); \
 		} \
 	} while (0)
Signature · 1 line
2.54.0
Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] initcall: initcall_evt: display error code on error

Quoted 10 lines · Julien Stephan

initcall: initcall_evt: display error code on error

Currently when an initcall/initcall_evt fails the error code is not displayed. Display it, along with the corresponding error string if ERRNO_STR is enabled.

 include/initcall.h | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
#7 lore raw
In reply to Julien Stephan
Quoted 24 lines · Julien Stephan

While writing a MISC driver, I forgot to enable CONFIG_MISC. The only thing I got at boot was the following obscure error, with no hint about the root cause:

initcall_run_r(): initcall initr_dm() failed
### ERROR ### Please RESET the board ###

Commit 3346c87625b8 ("dm: Improve handling of a missing uclass") already tried to improve this case by returning a strange error code and adding a debug message. But since neither the debug message nor the return code are displayed by default, it was not very helpful in practice.

This series improves the situation in a few ways:
- a new dm_err() helper logs driver model errors at ERROR level, and the boolean DM_WARN option becomes a per-stage log verbosity choice (DM_NONE / DM_ERR / DM_WARN) with errors emitted by default. Note that a disabled level is not removed entirely but degraded to DEBUG level, which is compiled out by default, so even DM_NONE can still emit these messages as debug output when debug logging is enabled;
- the missing-uclass message now uses dm_err(), so it shows up by default, with a shorter string to limit the code-size overhead;
- the initcall failure now prints the actual error code (and its string when ERRNO_STR is enabled), for both INITCALL() and INITCALL_EVT().

Quoted 5 lines

The existing DM_DEBUG option is left as-is for now: it works differently (it adds -DDEBUG to drivers/core/ via the Makefile and is not phase-aware). Unifying it into the new choice, along with converting the DM core debug() statements to log() as suggested in the "To Do" section of doc/develop/logging.rst, could be a follow-up series.

There are two issues here. One issue is that tests need to be adjusted, as CI fails:
https://git.u-boot-project.org/u-boot/u-boot/-/jobs/83678
for example.
The second issue is that am335x_evm overflows in SPL due to size growth. I am currently seeing if we can enable LTO on that family now, or if it still has a run time problem. If the latter, then defaulting to SPL_DM_ERR_NONE might be how that has to be handled.

For re-testing this in CI, please feel free to use a WIP branch or something in the mediatek tree, or just a tree under your own username. Thanks.

Signature · 1 line
Tom
#8 lore raw
In reply to Julien Stephan

Hi Julien,

Quoted 4 lines · Julien Stephan

This series improves the situation in a few ways:
- a new dm_err() helper logs driver model errors at ERROR level, and the boolean DM_WARN option becomes a per-stage log verbosity choice (DM_NONE / DM_ERR / DM_WARN) with errors emitted by default.

This changes the default for U-Boot proper, SPL and TPL all at once. Tom has already hit the size problem on am335x_evm, and I suspect other boards are close to their SPL/TPL limits too. It would be worth turning errors on by default only in U-Boot proper and leaving SPL and TPL at DM_NONE, so that boards with room can opt in. Before v3, please can you run a size comparison with buildman (e.g. 'buildman -b <branch> -sS') so we can see the actual cost. What do you think?

Quoted 3 lines

Unifying it into the new choice, along with converting the DM core debug() statements to log() as suggested in the "To Do" section of doc/develop/logging.rst, could be a follow-up series.

That is fine as a follow-up, but this series should still document what it adds. The new choice and dm_err() are not mentioned anywhere under doc/, and doc/develop/driver-model/design.rst still points SPL users at CONFIG_DM_WARN as a plain option. Please can you add a short doc patch, perhaps a few lines in doc/develop/logging.rst on how the DM log levels map onto the log levels.

Apart from the CI failures Tom mentioned, a test that checks the missing-uclass message appears with the default config would be good, since that is the motivation for the series. It can be a follow-up if it is awkward to set up in sandbox.

Regards,
Simon