Skip to contentSkip to conversation

soc: fsl: qe: Fix potential NULL pointer dereference in qe_reset()

PATCH In review Wang Jun 2 participants 6 messages Mar 10 – Mar 28

Subsystems (3)
lore
Share & tools
lore

Wang Jun

author
#0 lore raw

PATCH 1/1 soc: fsl: qe: Fix potential NULL pointer dereference in qe_reset()

The function qe_reset() uses qe_immr without checking if it is NULL, which could happen if ioremap() failed earlier. Add a NULL check and perform ioremap() if needed; if it still fails, print an error and return to avoid crashing the system.

---
 drivers/soc/fsl/qe/qe.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/soc/fsl/qe/qe.c +6 -1
index 70b6eddb867b..6dcfa340970a 100644
@@ -86,8 +86,13 @@ static phys_addr_t get_qe_base(void)
 
 void qe_reset(void)
 {
-	if (qe_immr == NULL)
+	if (qe_immr == NULL) {
 		qe_immr = ioremap(get_qe_base(), QE_IMMAP_SIZE);
+		if (qe_immr == NULL) {
+			pr_err("QE: cannot remap IMMR\n");
+			return;
+		}
+	}
 
 	qe_snums_init();
 
Signature · 1 line
2.43.0
#1 lore raw

Le 10/03/2026 à 13:11, Wang Jun a écrit :

Quoted 6 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

The function qe_reset() uses qe_immr without checking if it is NULL, which could happen if ioremap() failed earlier. Add a NULL check and perform ioremap() if needed; if it still fails, print an error and return to avoid crashing the system.

I don't understand what you are trying to say here. What you say is already what qe_reset() does: it does a NULL check and performs ioremap() when it is NULL:

	if (qe_immr == NULL)
		qe_immr = ioremap(get_qe_base(), QE_IMMAP_SIZE);

You are adding a second NULL check and return early from qe_reset(). But it doesn't really fix the problem because qe_immr is used in many other places so you are just delaying the problem.

What needs to be done is that if qe_immr remap fails, all drivers depending on it don't get probed.

Quoted 28 lines
---
  drivers/soc/fsl/qe/qe.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/soc/fsl/qe/qe.c +6 -2
index 70b6eddb867b..6dcfa340970a 100644
@@ -86,8 +86,13 @@ static phys_addr_t get_qe_base(void)
 
  void qe_reset(void)
  {
-       if (qe_immr == NULL)
+       if (qe_immr == NULL) {
                 qe_immr = ioremap(get_qe_base(), QE_IMMAP_SIZE);
+               if (qe_immr == NULL) {
+                       pr_err("QE: cannot remap IMMR\n");
+                       return;
+               }
+       }
 
         qe_snums_init();
 
--

2.43.0

author
#2 lore raw

Re: [PATCH] soc: fsl: qe: Fix potential NULL pointer dereference inqe_reset()

Hi,

Thank you for the detailed review. You are completely right.

My commit message was confusing, and returning early in qe_reset() just shifts the NULL pointer dereference to the dependent drivers later on, without actually fixing the root cause.

To achieve what you suggested ("if qe_immr remap fails, all drivers depending on it don't get probed"), I plan to do the following in the v2 patch:

1. Change the return type of qe_reset() from void to int.
2. Return -ENOMEM if the ioremap() fails.
3. Update the callers of qe_reset() (e.g., qe_probe() and other board-specific setup functions) to check this return value. If qe_reset() fails, the callers will abort their initialization/probing, which will properly prevent the child devices from being probed.

Does this approach sound correct to you? If so, I will prepare and submit the v2 patch accordingly.

Best regards,
Wang Jun

未君
[email protected]

Original

From: Christophe Leroy (CS GROUP) <[email protected]&gt;
Date: 2026-03-13 17:48
To: Wang Jun <[email protected]&gt;, Qiang Zhao <[email protected]&gt;, linuxppc-dev <[email protected]&gt;, linux-arm-kernel <[email protected]&gt;
Cc: linux-kernel <[email protected]&gt;, gszhai <[email protected]&gt;, 25125332 <[email protected]&gt;, 25125283 <[email protected]&gt;, 23120469 <[email protected]&gt;
Subject: Re: [PATCH] soc: fsl: qe: Fix potential NULL pointer dereference inqe_reset()

Le&nbsp;10/03/2026&nbsp;à&nbsp;13:11,&nbsp;Wang&nbsp;Jun&nbsp;a&nbsp;écrit&nbsp;:
&gt;&nbsp;[Vous&nbsp;ne&nbsp;recevez&nbsp;pas&nbsp;souvent&nbsp;de&nbsp;courriers&nbsp;de&nbsp;[email protected].&nbsp;Découvrez&nbsp;pourquoi&nbsp;ceci&nbsp;est&nbsp;important&nbsp;à&nbsp;https://aka.ms/LearnAboutSenderIdentification&nbsp;] &gt;&nbsp;
&gt;&nbsp;The&nbsp;function&nbsp;qe_reset()&nbsp;uses&nbsp;qe_immr&nbsp;without&nbsp;checking&nbsp;if&nbsp;it&nbsp;is&nbsp;NULL, &gt;&nbsp;which&nbsp;could&nbsp;happen&nbsp;if&nbsp;ioremap()&nbsp;failed&nbsp;earlier.&nbsp;Add&nbsp;a&nbsp;NULL&nbsp;check&nbsp;and &gt;&nbsp;perform&nbsp;ioremap()&nbsp;if&nbsp;needed;&nbsp;if&nbsp;it&nbsp;still&nbsp;fails,&nbsp;print&nbsp;an&nbsp;error&nbsp;and &gt;&nbsp;return&nbsp;to&nbsp;avoid&nbsp;crashing&nbsp;the&nbsp;system.

I&nbsp;don't&nbsp;understand&nbsp;what&nbsp;you&nbsp;are&nbsp;trying&nbsp;to&nbsp;say&nbsp;here.&nbsp;What&nbsp;you&nbsp;say&nbsp;is&nbsp; already&nbsp;what&nbsp;qe_reset()&nbsp;does:&nbsp;it&nbsp;does&nbsp;a&nbsp;NULL&nbsp;check&nbsp;and&nbsp;performs&nbsp; ioremap()&nbsp;when&nbsp;it&nbsp;is&nbsp;NULL:

	if&nbsp;(qe_immr&nbsp;==&nbsp;NULL)
		qe_immr&nbsp;=&nbsp;ioremap(get_qe_base(),&nbsp;QE_IMMAP_SIZE);

You&nbsp;are&nbsp;adding&nbsp;a&nbsp;second&nbsp;NULL&nbsp;check&nbsp;and&nbsp;return&nbsp;early&nbsp;from&nbsp;qe_reset().&nbsp;But&nbsp; it&nbsp;doesn't&nbsp;really&nbsp;fix&nbsp;the&nbsp;problem&nbsp;because&nbsp;qe_immr&nbsp;is&nbsp;used&nbsp;in&nbsp;many&nbsp;other&nbsp; places&nbsp;so&nbsp;you&nbsp;are&nbsp;just&nbsp;delaying&nbsp;the&nbsp;problem.

What&nbsp;needs&nbsp;to&nbsp;be&nbsp;done&nbsp;is&nbsp;that&nbsp;if&nbsp;qe_immr&nbsp;remap&nbsp;fails,&nbsp;all&nbsp;drivers&nbsp; depending&nbsp;on&nbsp;it&nbsp;don't&nbsp;get&nbsp;probed.

&gt;&nbsp;
&gt;&nbsp;Signed-off-by:&nbsp;Wang&nbsp;Jun&nbsp;<[email protected]&gt;
&gt;&nbsp;---
&gt;&nbsp;&nbsp;&nbsp;drivers/soc/fsl/qe/qe.c&nbsp;|&nbsp;7&nbsp;++++++- &gt;&nbsp;&nbsp;&nbsp;1&nbsp;file&nbsp;changed,&nbsp;6&nbsp;insertions(+),&nbsp;1&nbsp;deletion(-)
&gt;&nbsp;
&gt;&nbsp;diff&nbsp;--git&nbsp;a/drivers/soc/fsl/qe/qe.c&nbsp;b/drivers/soc/fsl/qe/qe.c
&gt;&nbsp;index&nbsp;70b6eddb867b..6dcfa340970a&nbsp;100644
&gt;&nbsp;---&nbsp;a/drivers/soc/fsl/qe/qe.c
&gt;&nbsp;+++&nbsp;b/drivers/soc/fsl/qe/qe.c
&gt;&nbsp;@@&nbsp;-86,8&nbsp;+86,13&nbsp;@@&nbsp;static&nbsp;phys_addr_t&nbsp;get_qe_base(void)
&gt;&nbsp;
&gt;&nbsp;&nbsp;&nbsp;void&nbsp;qe_reset(void)
&gt;&nbsp;&nbsp;&nbsp;{
&gt;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(qe_immr&nbsp;==&nbsp;NULL) &gt;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(qe_immr&nbsp;==&nbsp;NULL)&nbsp;{ &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;qe_immr&nbsp;=&nbsp;ioremap(get_qe_base(),&nbsp;QE_IMMAP_SIZE); &gt;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(qe_immr&nbsp;==&nbsp;NULL)&nbsp;{ &gt;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pr_err("QE:&nbsp;cannot&nbsp;remap&nbsp;IMMR\n"); &gt;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return; &gt;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&gt;&nbsp;+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&gt;&nbsp;
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;qe_snums_init();
&gt;&nbsp;
&gt;&nbsp;--
&gt;&nbsp;2.43.0
&gt;&nbsp;

#3 lore raw

Re: [PATCH] soc: fsl: qe: Fix potential NULL pointer dereference inqe_reset()

Hi,

Le 16/03/2026 à 04:28, 未君 a écrit :

Quoted 10 lines

Vous n’obtenez pas souvent d’e-mail à partir de [email protected]. Pourquoi c’est important <https://aka.ms/LearnAboutSenderIdentification>

Hi,

Thank you for the detailed review. You are completely right.

My commit message was confusing, and returning early in qe_reset() just shifts the NULL pointer dereference to the dependent drivers later on, without actually fixing the root cause.

Quoted 7 lines

To achieve what you suggested ("if qe_immr remap fails, all drivers depending on it don't get probed"), I plan to do the following in the v2 patch:

1. Change the return type of qe_reset() from void to int.
2. Return -ENOMEM if the ioremap() fails.
3. Update the callers of qe_reset() (e.g., qe_probe() and other board- specific setup functions) to check this return value. If qe_reset() fails, the callers will abort their initialization/probing, which will properly prevent the child devices from being probed.

Does this approach sound correct to you? If so, I will prepare and submit the v2 patch accordingly.

Well, it would probably work but is it worth it ?

If the board is already unable to get a few bytes of memory that early in the boot process it is unlikely that it will be able to do much more work.

Wouldn't it be good enough to just panic() when ioremap() fails, similar to what happens when qe_sdma_init() fails, see https://elixir.bootlin.com/linux/v7.0-rc5/source/drivers/soc/fsl/qe/qe.c#L101

Christophe

author
#4 lore raw

PATCH v2 1/1 soc: fsl: qe: panic on ioremap() failure in qe_reset()

When ioremap() fails in qe_reset(), the global pointer qe_immr remains NULL, leading to a subsequent NULL pointer dereference when the pointer is accessed. Since this happens early in the boot process, a failure to map a few bytes of I/O memory indicates a fatal error from which the system cannot recover.

Follow the same pattern as qe_sdma_init() and panic immediately when ioremap() fails. This avoids a silent NULL pointer dereference later and makes the error explicit.

Fixes 986585385131 ("[POWERPC] Add QUICC Engine (QE) infrastructure")
---
 drivers/soc/fsl/qe/qe.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/soc/fsl/qe/qe.c +5 -1
index 70b6eddb867b..9f6223043ee3 100644
@@ -86,8 +86,12 @@ static phys_addr_t get_qe_base(void)
 
 void qe_reset(void)
 {
-	if (qe_immr == NULL)
+	if (qe_immr == NULL) {
 		qe_immr = ioremap(get_qe_base(), QE_IMMAP_SIZE);
+		if (qe_immr == NULL) {
+			panic("QE:ioremap failed!");
+		}
+	}
 
 	qe_snums_init();
 
Signature · 1 line
2.43.0
maintainer Applied
#5 lore raw

Re: [PATCH v2] soc: fsl: qe: panic on ioremap() failure in qe_reset()

Le 27/03/2026 à 01:12, Wang Jun a écrit :

Quoted 27 lines

[Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

When ioremap() fails in qe_reset(), the global pointer qe_immr remains NULL, leading to a subsequent NULL pointer dereference when the pointer is accessed. Since this happens early in the boot process, a failure to map a few bytes of I/O memory indicates a fatal error from which the system cannot recover.

Follow the same pattern as qe_sdma_init() and panic immediately when ioremap() fails. This avoids a silent NULL pointer dereference later and makes the error explicit.

Fixes 986585385131 ("[POWERPC] Add QUICC Engine (QE) infrastructure")
---
  drivers/soc/fsl/qe/qe.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/soc/fsl/qe/qe.c +0 -0
index 70b6eddb867b..9f6223043ee3 100644
@@ -86,8 +86,12 @@ static phys_addr_t get_qe_base(void)
 
  void qe_reset(void)
  {

drivers/soc/fsl/qe/qe.c static phys_addr_t get_qe_base(void) · line 97

Quoted 10 lines
drivers/soc/fsl/qe/qe.c +5 -1
-       if (qe_immr == NULL)
+       if (qe_immr == NULL) {
                 qe_immr = ioremap(get_qe_base(), QE_IMMAP_SIZE);
+               if (qe_immr == NULL) {
+                       panic("QE:ioremap failed!");
+               }
+       }
 
         qe_snums_init();
 

Applied, thanks.