Skip to contentSkip to conversation

powerpc/eeh: Add RTAS-based error injection support on pSeries

PATCH Superseded Narayana Murty N 2 participants 19 messages Aug 31 – Oct 19

Subsystems (4)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover powerpc/eeh: Add RTAS-based error injection support on pSeries

The pSeries EEH error-injection backend currently implements a limited software-generated MMIO failure and does not use the error-injection services provided by RTAS.

This series replaces that implementation with the PAPR-defined RTAS workflow based on:

- ibm,open-errinjct
- ibm,errinjct
- ibm,close-errinjct

The implementation opens an RTAS error-injection session, prepares the firmware work buffer, performs the requested injection, and closes the session on both success and failure paths.

The existing EEH userspace ABI is preserved. EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 continue to represent generic 32-bit and 64-bit IOA bus-error injection requests. The pSeries backend maps these values to the corresponding RTAS error types, while the PowerNV backend explicitly maps them to the corresponding OPAL types.

Additional generic EEH error types (EEH_ERR_TYPE_RECOVERED_SPECIAL_EVENT, EEH_ERR_TYPE_CORRUPTED_PAGE, and the cache/TLB corruption types) are now defined in the UAPI header and mapped explicitly to RTAS firmware encodings by the pSeries backend. Platform backends that do not support a valid generic type return -EOPNOTSUPP.

No existing userspace ABI values are changed.

The current injection path can be exercised for VFIO-assigned devices through VFIO_EEH_PE_INJECT_ERR. The guest or userspace VFIO application continues to use the same generic EEH type and function values, independent of whether the host platform uses RTAS or OPAL.

The series also handles the unusual return format of ibm,open-errinjct:

rets[0] = error-injection session token
rets[1] = RTAS status

rtas_call() now returns rets[1] as the status and places the session token in outputs[0], preserving the normal kernel rtas_call() convention.

sys_rtas() is intentionally unchanged because it exposes the raw RTAS return cells to userspace. Userspace therefore continues to receive the session token and status in their PAPR-defined positions.

The RTAS work buffer is allocated during RTAS initialization below:

min(ppc64_rma_size, RTAS_INSTANTIATE_MAX)

using the same accessible-memory limit used for rtas_rmo_buf. The kernel populates the buffer through its virtual mapping but passes its physical address to firmware.

The complete open, inject and close sequence is serialized with a mutex. RTAS busy and extended-delay return values are handled for all three calls. A session token value of zero is accepted, and session state is tracked independently from the token value.

The patches are organised as follows:

Handle the special ibm,open-errinjct return format in rtas_call(). Allocate an RTAS-accessible error-injection work buffer. Add pSeries RTAS parameter validation and buffer encoding helpers. Implement RTAS-based pSeries EEH error injection. Explicitly map generic EEH error types to OPAL types on PowerNV.

Testing was performed on PowerVM with firmware providing the RTAS error-injection services and with the corresponding QEMU support:

https://lore.kernel.org/qemu-devel/[email protected]/

Narayana Murty N (5):
powerpc/rtas: Handle ibm,open-errinjct return format vfio/spapr_tce: Normalize EEH IOA error injection addresses powerpc/pseries/eeh: Add RTAS error validation helpers powerpc/pseries/eeh: Implement RTAS-based EEH error injection powerpc/powernv/eeh: Map VFIO EEH error injection to OPAL

 arch/powerpc/kernel/rtas.c                   |  78 ++++-
 arch/powerpc/platforms/powernv/eeh-powernv.c |  27 +-
 arch/powerpc/platforms/pseries/eeh_pseries.c | 328 +++++++++++++++++--
 drivers/vfio/vfio_iommu_spapr_tce.c          |  90 +++++
 4 files changed, 484 insertions(+), 39 deletions(-)
Signature · 1 line
2.51.1
#1 lore raw

PATCH v4 1/5 powerpc/rtas: Handle ibm,open-errinjct return format

ibm,open-errinjct uses a non-standard RTAS return layout:

rets[0] = session token (output parameter)
rets[1] = status code

Unlike all other RTAS functions which use:

rets[0] = status code
rets[1..] = output parameters

Add rtas_token_is_open_errinjct() to identify this call, and rtas_status_from_args() to extract status from the correct position.

Add an early guard in rtas_call() that rejects ibm,open-errinjct invocations where nret < 2, since reading rets[1] would be out of bounds:

  if (rtas_token_is_open_errinjct(token) && nret < 2) {
          WARN_ON_ONCE(1);
          return RTAS_INVALID_PARAMETER;
  }

Adjust the output-copy loop so that for ibm,open-errinjct:

return value = rets[1] (status)
outputs[0] = rets[0] (session token)

For all other functions the existing convention is preserved:

return value = rets[0] (status)
outputs[0..] = rets[1..] (non-status outputs)

Move the "/* A -1 return code... */" comment immediately before the ret == -1 check so it documents the check it guards.

Also fix sys_rtas() last-error status detection: ibm,open-errinjct places status at rets[1], so the -1 sentinel check must use rets[1] for that function rather than always using rets[0].

Reference: OpenPOWER PAPR documentation
https://files.openpower.foundation/s/XFgfMaqLMD5Bcm8

---
 arch/powerpc/kernel/rtas.c | 78 +++++++++++++++++++++++++++++++++-----
 1 file changed, 68 insertions(+), 10 deletions(-)
arch/powerpc/kernel/rtas.c +68 -10
index 8d81c1e7a8db..7131870655c6 100644
@@ -1117,6 +1117,28 @@ static bool token_is_restricted_errinjct(s32 token)
 	       token == rtas_function_token(RTAS_FN_IBM_ERRINJCT);
 }
 
+/*
+ * ibm,open-errinjct uses a non-standard return layout:
+ *   rets[0] = session token  (output parameter)
+ *   rets[1] = status code
+ *
+ * All other RTAS functions use the standard layout:
+ *   rets[0] = status code
+ *   rets[1..] = output parameters
+ */
+static inline bool rtas_token_is_open_errinjct(int token)
+{
+	return token == rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT);
+}
+
+static int rtas_status_from_args(int token, struct rtas_args *args, int nret)
+{
+	if (rtas_token_is_open_errinjct(token))
+		return be32_to_cpu(args->rets[1]);
+
+	return nret > 0 ? be32_to_cpu(args->rets[0]) : 0;
+}
+
 /**
  * rtas_call() - Invoke an RTAS firmware function.
  * @token: Identifies the function being invoked.
@@ -1198,6 +1220,16 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 			return -1;
 	}
 
+	/*
+	 * ibm,open-errinjct returns rets[0]=session_token, rets[1]=status.
+	 * We need nret >= 2 to read status from rets[1].  Reject early if
+	 * the caller forgot to account for the extra return cell.
+	 */
+	if (rtas_token_is_open_errinjct(token) && nret < 2) {
+		WARN_ON_ONCE(1);
+		return RTAS_INVALID_PARAMETER;
+	}
+
 	if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) {
 		WARN_ON_ONCE(1);
 		return -1;
@@ -1213,15 +1245,33 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 	va_rtas_call_unlocked(args, token, nargs, nret, list);
 	va_end(list);
 
+	ret = rtas_status_from_args(token, args, nret);
+
 	/* A -1 return code indicates that the last command couldn't
-	   be completed due to a hardware error. */
-	if (be32_to_cpu(args->rets[0]) == -1)
+	 * be completed due to a hardware error.
+	 */
+	if (ret == -1)
 		buff_copy = __fetch_rtas_last_error(NULL);
 
-	if (nret > 1 && outputs != NULL)
-		for (i = 0; i < nret-1; ++i)
-			outputs[i] = be32_to_cpu(args->rets[i + 1]);
-	ret = (nret > 0) ? be32_to_cpu(args->rets[0]) : 0;
+	/*
+	 * Copy non-status outputs to the caller's buffer.
+	 *
+	 * For ibm,open-errinjct the layout is:
+	 *   rets[0] = session token  -> outputs[0]
+	 *   rets[1] = status         (returned, not copied)
+	 *
+	 * For all other RTAS functions:
+	 *   rets[0] = status         (returned, not copied)
+	 *   rets[1..nret-1] -> outputs[0..nret-2]
+	 */
+	if (outputs != NULL) {
+		if (rtas_token_is_open_errinjct(token)) {
+			outputs[0] = be32_to_cpu(args->rets[0]);
+		} else if (nret > 1) {
+			for (i = 0; i < nret - 1; ++i)
+				outputs[i] = be32_to_cpu(args->rets[i + 1]);
+		}
+	}
 
 	lockdep_unpin_lock(&rtas_lock, cookie);
 	raw_spin_unlock_irqrestore(&rtas_lock, flags);
@@ -1942,10 +1992,18 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
 	do_enter_rtas(&rtas_args);
 	args = rtas_args;
 
-	/* A -1 return code indicates that the last command couldn't
-	   be completed due to a hardware error. */
-	if (be32_to_cpu(args.rets[0]) == -1)
-		errbuf = __fetch_rtas_last_error(buff_copy);
+	/*
+	 * A -1 return code indicates that the last command couldn't
+	 * be completed due to a hardware error.  ibm,open-errinjct
+	 * places status at rets[1] rather than rets[0]; check the
+	 * correct position for the -1 sentinel.
+	 */
+	{
+		__be32 status_cell = (token == rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT) &&
+				      nret >= 2) ? args.rets[1] : args.rets[0];
+		if (be32_to_cpu(status_cell) == -1)
+			errbuf = __fetch_rtas_last_error(buff_copy);
+	}
 
 	lockdep_unpin_lock(&rtas_lock, cookie);
 	raw_spin_unlock_irqrestore(&rtas_lock, flags);
Signature · 1 line
2.51.1
#2 lore raw

Re: [PATCH v4 1/5] powerpc/rtas: Handle ibm,open-errinjct return format

Quoted 81 lines · Narayana Murty N

ibm,open-errinjct uses a non-standard RTAS return layout:

rets[0] = session token (output parameter)
rets[1] = status code

Unlike all other RTAS functions which use:

rets[0] = status code
rets[1..] = output parameters

Add rtas_token_is_open_errinjct() to identify this call, and rtas_status_from_args() to extract status from the correct position.

Add an early guard in rtas_call() that rejects ibm,open-errinjct invocations where nret < 2, since reading rets[1] would be out of bounds:

   if (rtas_token_is_open_errinjct(token) && nret < 2) {
           WARN_ON_ONCE(1);
           return RTAS_INVALID_PARAMETER;
   }

Adjust the output-copy loop so that for ibm,open-errinjct:

return value = rets[1] (status)
outputs[0] = rets[0] (session token)

For all other functions the existing convention is preserved:

return value = rets[0] (status)
outputs[0..] = rets[1..] (non-status outputs)

Move the "/* A -1 return code... */" comment immediately before the ret == -1 check so it documents the check it guards.

Also fix sys_rtas() last-error status detection: ibm,open-errinjct places status at rets[1], so the -1 sentinel check must use rets[1] for that function rather than always using rets[0].

Reference: OpenPOWER PAPR documentation
https://files.openpower.foundation/s/XFgfMaqLMD5Bcm8

---
  arch/powerpc/kernel/rtas.c | 78 +++++++++++++++++++++++++++++++++-----
  1 file changed, 68 insertions(+), 10 deletions(-)
arch/powerpc/kernel/rtas.c +22 -0
index 8d81c1e7a8db..7131870655c6 100644
@@ -1117,6 +1117,28 @@ static bool token_is_restricted_errinjct(s32 token)
  	       token == rtas_function_token(RTAS_FN_IBM_ERRINJCT);
  }
 
+/*
+ * ibm,open-errinjct uses a non-standard return layout:
+ *   rets[0] = session token  (output parameter)
+ *   rets[1] = status code
+ *
+ * All other RTAS functions use the standard layout:
+ *   rets[0] = status code
+ *   rets[1..] = output parameters
+ */
+static inline bool rtas_token_is_open_errinjct(int token)
+{
+	return token == rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT);
+}
+
+static int rtas_status_from_args(int token, struct rtas_args *args, int nret)
+{
+	if (rtas_token_is_open_errinjct(token))
+		return be32_to_cpu(args->rets[1]);
+
+	return nret > 0 ? be32_to_cpu(args->rets[0]) : 0;
+}
+
  /**
   * rtas_call() - Invoke an RTAS firmware function.
   * @token: Identifies the function being invoked.
@@ -1198,6 +1220,16 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
  			return -1;

arch/powerpc/kernel/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 1230

Quoted 10 lines
arch/powerpc/kernel/rtas.c +8 -0
  	}
 
+	/*
+	 * ibm,open-errinjct returns rets[0]=session_token, rets[1]=status.
+	 * We need nret >= 2 to read status from rets[1].  Reject early if
+	 * the caller forgot to account for the extra return cell.
+	 */
+	if (rtas_token_is_open_errinjct(token) && nret < 2) {
+		WARN_ON_ONCE(1);
+		return RTAS_INVALID_PARAMETER;

Nit: I would prefer -EINVAL instead. RTAS_INVALID_PARAMETER is RTAS error code but here kernel is validating the parameter so I think -EINVAL would be better.

Quoted 54 lines
arch/powerpc/kernel/rtas.c +27 -10
+	}
+
  	if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) {
  		WARN_ON_ONCE(1);
  		return -1;
@@ -1213,15 +1245,33 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
  	va_rtas_call_unlocked(args, token, nargs, nret, list);
  	va_end(list);
 
+	ret = rtas_status_from_args(token, args, nret);
+
  	/* A -1 return code indicates that the last command couldn't
-	   be completed due to a hardware error. */
-	if (be32_to_cpu(args->rets[0]) == -1)
+	 * be completed due to a hardware error.
+	 */
+	if (ret == -1)
  		buff_copy = __fetch_rtas_last_error(NULL);
 
-	if (nret > 1 && outputs != NULL)
-		for (i = 0; i < nret-1; ++i)
-			outputs[i] = be32_to_cpu(args->rets[i + 1]);
-	ret = (nret > 0) ? be32_to_cpu(args->rets[0]) : 0;
+	/*
+	 * Copy non-status outputs to the caller's buffer.
+	 *
+	 * For ibm,open-errinjct the layout is:
+	 *   rets[0] = session token  -> outputs[0]
+	 *   rets[1] = status         (returned, not copied)
+	 *
+	 * For all other RTAS functions:
+	 *   rets[0] = status         (returned, not copied)
+	 *   rets[1..nret-1] -> outputs[0..nret-2]
+	 */
+	if (outputs != NULL) {
+		if (rtas_token_is_open_errinjct(token)) {
+			outputs[0] = be32_to_cpu(args->rets[0]);
+		} else if (nret > 1) {
+			for (i = 0; i < nret - 1; ++i)
+				outputs[i] = be32_to_cpu(args->rets[i + 1]);
+		}
+	}
 
  	lockdep_unpin_lock(&rtas_lock, cookie);
  	raw_spin_unlock_irqrestore(&rtas_lock, flags);
@@ -1942,10 +1992,18 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
  	do_enter_rtas(&rtas_args);
  	args = rtas_args;
 
-	/* A -1 return code indicates that the last command couldn't
-	   be completed due to a hardware error. */
-	if (be32_to_cpu(args.rets[0]) == -1)
-		errbuf = __fetch_rtas_last_error(buff_copy);
+	/*

arch/powerpc/kernel/rtas.c SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs) · line 2005

Quoted 10 lines
arch/powerpc/kernel/rtas.c +10 -0
+	 * A -1 return code indicates that the last command couldn't
+	 * be completed due to a hardware error.  ibm,open-errinjct
+	 * places status at rets[1] rather than rets[0]; check the
+	 * correct position for the -1 sentinel.
+	 */
+	{
+		__be32 status_cell = (token == rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT) &&
+				      nret >= 2) ? args.rets[1] : args.rets[0];
+		if (be32_to_cpu(status_cell) == -1)
+			errbuf = __fetch_rtas_last_error(buff_copy);

Do we know what happens when the ibm,open-errinjct RTAS call is made with nret < 2?

The reason I’m asking is that, even with the above changes, args.rets[0] is used as the return code if the ibm,open-errinjct call is made with nret < 2.

I like the approach you took in rtas_call() of pre-validating nret for the ibm,open-errinjct RTAS call and returning early if it is less than
2. I think we can use a similar approach here as well. If we do that, the above code changes will be much cleaner. In that case, we don't have to figure out how RTAS processes ibm,open-errinjct with nret < 2.

The only concern I have is that this change would alter the system call behavior. Right now, the kernel accepts nret < 2 for ibm,open-errinjct and makes the RTAS call, but with the above suggested change, the kernel would return early if nret < 2.

The prominent user of this system call is librtas, which passes nret = 2 for ibm,open-errinjct:

https://github.com/ibm-power-utilities/librtas/blob/d321a1f5ae3d528ba027fc748d1cc1123dd4ae29/librtas_src/syscall_calls.c#L488

Also, as per PAPR, users are supposed to pass nret = 2 for this RTAS call. So I think it should be fine to validate nret in sys_rtas for ibm,open-errinjct and return early if it is found to be less than 2.

Since this is a change in system call behavior, I want to be a little cautious. So, I’d like to hear your thoughts and would also like to know what others think about making the above change.

Thanks, Sourabh Jain

Quoted 4 lines
arch/powerpc/kernel/rtas.c +1 -0
+	}
 
  	lockdep_unpin_lock(&rtas_lock, cookie);
  	raw_spin_unlock_irqrestore(&rtas_lock, flags);
#3 lore raw

Re: [PATCH v4 1/5] powerpc/rtas: Handle ibm,open-errinjct return format

Hi Sourabh,

Thanks for the detailed review.

Quoted 88 lines · Sourabh Jain
Quoted 86 lines · Narayana Murty N

ibm,open-errinjct uses a non-standard RTAS return layout:

   rets[0] = session token  (output parameter)
   rets[1] = status code

Unlike all other RTAS functions which use:

   rets[0] = status code
   rets[1..] = output parameters

Add rtas_token_is_open_errinjct() to identify this call, and rtas_status_from_args() to extract status from the correct position.

Add an early guard in rtas_call() that rejects ibm,open-errinjct invocations where nret < 2, since reading rets[1] would be out of bounds:

   if (rtas_token_is_open_errinjct(token) && nret < 2) { WARN_ON_ONCE(1);
           return RTAS_INVALID_PARAMETER;
   }

Adjust the output-copy loop so that for ibm,open-errinjct:

   return value = rets[1]   (status)
   outputs[0]   = rets[0]   (session token)

For all other functions the existing convention is preserved:

   return value = rets[0]   (status)
   outputs[0..] = rets[1..] (non-status outputs)

Move the "/* A -1 return code... */" comment immediately before the ret == -1 check so it documents the check it guards.

Also fix sys_rtas() last-error status detection: ibm,open-errinjct places status at rets[1], so the -1 sentinel check must use rets[1] for that function rather than always using rets[0].

Reference: OpenPOWER PAPR documentation
            https://files.openpower.foundation/s/XFgfMaqLMD5Bcm8

---
  arch/powerpc/kernel/rtas.c | 78 +++++++++++++++++++++++++++++++++-----

  1 file changed, 68 insertions(+), 10 deletions(-)
arch/powerpc/kernel/rtas.c +22 -0
index 8d81c1e7a8db..7131870655c6 100644
@@ -1117,6 +1117,28 @@ static bool token_is_restricted_errinjct(s32 
 token)
              token == rtas_function_token(RTAS_FN_IBM_ERRINJCT);
   }
+/*
+ * ibm,open-errinjct uses a non-standard return layout:
+ *   rets[0] = session token  (output parameter)
+ *   rets[1] = status code
+ *
+ * All other RTAS functions use the standard layout:
+ *   rets[0] = status code
+ *   rets[1..] = output parameters
+ */
+static inline bool rtas_token_is_open_errinjct(int token)
+{
+    return token == rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT);
+}
+
+static int rtas_status_from_args(int token, struct rtas_args *args, 
 int nret)
+{
+    if (rtas_token_is_open_errinjct(token))
+        return be32_to_cpu(args->rets[1]);
+
+    return nret > 0 ? be32_to_cpu(args->rets[0]) : 0;
+}
+
   /**
    * rtas_call() - Invoke an RTAS firmware function.

   * @token: Identifies the function being invoked.

arch/powerpc/kernel/rtas.c +2 -0
@@ -1198,6 +1220,16 @@ int rtas_call(int token, int nargs, int nret, 
 int *outputs, ...)
               return -1;
       }
+    /*
+     * ibm,open-errinjct returns rets[0]=session_token, rets[1]=status.
Quoted 8 lines

arch/powerpc/kernel/rtas.c int rtas_call(int token, int nargs, int nret, int *outputs, ...) · line 1230

Quoted 6 lines
arch/powerpc/kernel/rtas.c +6 -0
+     * We need nret >= 2 to read status from rets[1].  Reject early if
+     * the caller forgot to account for the extra return cell.
+     */
+    if (rtas_token_is_open_errinjct(token) && nret < 2) {
+        WARN_ON_ONCE(1);
+        return RTAS_INVALID_PARAMETER;

Nit: I would prefer -EINVAL instead. RTAS_INVALID_PARAMETER is RTAS error code but here kernel is validating the parameter so I think - EINVAL would be better.

I agree this is a kernel-side validation before entering RTAS, but for rtas_call() I think keeping RTAS_INVALID_PARAMETER is safer.

The return value from rtas_call() is normally interpreted by callers as an RTAS status code. If we return -EINVAL from rtas_call(), the caller may still treat it as an RTAS return value. Since this is an invalid RTAS call layout for ibm,open-errinjct, I would prefer to keep the return value as RTAS_INVALID_PARAMETER in rtas_call().

Quoted 87 lines
Quoted 59 lines
arch/powerpc/kernel/rtas.c +34 -10
+    }
+
       if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) {
           WARN_ON_ONCE(1);
           return -1;
@@ -1213,15 +1245,33 @@ int rtas_call(int token, int nargs, int nret, 
 int *outputs, ...)
       va_rtas_call_unlocked(args, token, nargs, nret, list);
       va_end(list);
+    ret = rtas_status_from_args(token, args, nret);
+
       /* A -1 return code indicates that the last command couldn't
-       be completed due to a hardware error. */
-    if (be32_to_cpu(args->rets[0]) == -1)
+     * be completed due to a hardware error.
+     */
+    if (ret == -1)
           buff_copy = __fetch_rtas_last_error(NULL);
-    if (nret > 1 && outputs != NULL)
-        for (i = 0; i < nret-1; ++i)
-            outputs[i] = be32_to_cpu(args->rets[i + 1]);
-    ret = (nret > 0) ? be32_to_cpu(args->rets[0]) : 0;
+    /*
+     * Copy non-status outputs to the caller's buffer.
+     *
+     * For ibm,open-errinjct the layout is:
+     *   rets[0] = session token  -> outputs[0]
+     *   rets[1] = status         (returned, not copied)
+     *
+     * For all other RTAS functions:
+     *   rets[0] = status         (returned, not copied)
+     *   rets[1..nret-1] -> outputs[0..nret-2]
+     */
+    if (outputs != NULL) {
+        if (rtas_token_is_open_errinjct(token)) {
+            outputs[0] = be32_to_cpu(args->rets[0]);
+        } else if (nret > 1) {
+            for (i = 0; i < nret - 1; ++i)
+                outputs[i] = be32_to_cpu(args->rets[i + 1]);
+        }
+    }
       lockdep_unpin_lock(&rtas_lock, cookie);
       raw_spin_unlock_irqrestore(&rtas_lock, flags);
@@ -1942,10 +1992,18 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user 
 *, uargs)
       do_enter_rtas(&rtas_args);
       args = rtas_args;
-    /* A -1 return code indicates that the last command couldn't
-       be completed due to a hardware error. */
-    if (be32_to_cpu(args.rets[0]) == -1)
-        errbuf = __fetch_rtas_last_error(buff_copy);
+    /*
+     * A -1 return code indicates that the last command couldn't
+     * be completed due to a hardware error.  ibm,open-errinjct
+     * places status at rets[1] rather than rets[0]; check the
+     * correct position for the -1 sentinel.
+     */
+    {
+        __be32 status_cell = (token == 
Quoted 4 lines

rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT) &&
+                      nret >= 2) ? args.rets[1] : args.rets[0]; +        if (be32_to_cpu(status_cell) == -1)
+            errbuf = __fetch_rtas_last_error(buff_copy);

Do we know what happens when the ibm,open-errinjct RTAS call is made with nret < 2?

The reason I’m asking is that, even with the above changes, args.rets[0] is used as the return code if the ibm,open-errinjct call is made with nret < 2.

I like the approach you took in rtas_call() of pre-validating nret for the ibm,open-errinjct RTAS call and returning early if it is less than
2. I think we can use a similar approach here as well. If we do that, the above code changes will be much cleaner. In that case, we don't have to figure out how RTAS processes ibm,open-errinjct with nret < 2.

The only concern I have is that this change would alter the system call behavior. Right now, the kernel accepts nret < 2 for ibm,open-errinjct and makes the RTAS call, but with the above suggested change, the kernel would return early if nret < 2.

The prominent user of this system call is librtas, which passes nret = 2 for ibm,open-errinjct:

https://github.com/ibm-power-utilities/librtas/blob/ d321a1f5ae3d528ba027fc748d1cc1123dd4ae29/librtas_src/syscall_calls.c#L488

Quoted 8 lines

Also, as per PAPR, users are supposed to pass nret = 2 for this RTAS call. So I think it should be fine to validate nret in sys_rtas for ibm,open-errinjct and return early if it is found to be less than 2.

Since this is a change in system call behavior, I want to be a little cautious. So, I’d like to hear your thoughts and would also like to know what others think about making the above change.

Agreed.

For ibm,open-errinjct, rets[0] is the session token and rets[1] is the
status. So when nret < 2, we should not fall back to rets[0], because that can interpret a session token as a status code.

I will add an early validation in sys_rtas() before entering RTAS for this case and return -EINVAL there, since sys_rtas() is the userspace syscall boundary.

So the split will be:
rtas_call(): return RTAS_INVALID_PARAMETER for invalid RTAS return layout

sys_rtas(): return -EINVAL for invalid userspace syscall arguments

This should not affect the valid librtas path, since librtas already passes nret = 2 for ibm,open-errinjct, as required by PAPR.

Thanks,
Narayana

Quoted 5 lines

Thanks, Sourabh Jain

Quoted 3 lines

+    }
      lockdep_unpin_lock(&rtas_lock, cookie); raw_spin_unlock_irqrestore(&rtas_lock, flags);

#4 lore raw

PATCH v4 2/5 vfio/spapr_tce: Normalize EEH IOA error injection addresses

In reply to Narayana Murty N

VFIO_EEH_PE_INJECT_ERR receives a target address from userspace. For userspace such as QEMU, this address may be derived from the host Linux BAR resource. The platform EEH backends, however, expect the PCI/IOA bus address used by firmware error-injection interfaces.

Normalize IOA/MMIO error-injection addresses in the sPAPR VFIO EEH ioctl path before dispatching to eeh_pe_inject_err(). If the supplied address is already a PCI bus BAR address it is left unchanged. If it is a Linux resource address, translate it to the corresponding PCI bus address using the BAR-relative offset.

Keep the helper local to VFIO so the address semantics of other in-kernel EEH callers are unchanged. This provides common handling for both pseries and PowerNV backends.

---
 drivers/vfio/vfio_iommu_spapr_tce.c | 90 +++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
drivers/vfio/vfio_iommu_spapr_tce.c +90 -0
index 1c0eec228cc6..8a6cc856d7da 100644
@@ -774,6 +774,85 @@ static long tce_iommu_create_default_window(struct tce_container *container)
 	return ret;
 }
 
+static bool vfio_spapr_eeh_err_needs_addr_normalize(unsigned int type)
+{
+	if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
+		return false;
+
+	return true;
+}
+
+static int vfio_spapr_eeh_normalize_addr(struct eeh_pe *pe,
+					 unsigned long addr,
+					 unsigned long *normalized)
+{
+	struct pci_bus_region region;
+	struct eeh_dev *edev, *tmp;
+	struct pci_dev *pdev;
+	struct resource *res;
+	resource_size_t pci_start, pci_len;
+	resource_size_t res_start, res_len;
+	resource_size_t offset;
+	int bar;
+
+	if (!pe || !normalized)
+		return -EINVAL;
+
+	if (!addr) {
+		*normalized = addr;
+		return 0;
+	}
+
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (!pdev)
+			continue;
+
+		for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+			res = &pdev->resource[bar];
+
+			if (!resource_size(res))
+				continue;
+
+			if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
+				continue;
+
+			pcibios_resource_to_bus(pdev->bus, &region, res);
+
+			pci_start = region.start;
+			pci_len = resource_size(res);
+
+			/*
+			 * Case 1: userspace already supplied PCI/IOA
+			 * bus address.
+			 */
+			if ((resource_size_t)addr >= pci_start &&
+			    ((resource_size_t)addr - pci_start) < pci_len) {
+				*normalized = addr;
+				return 0;
+			}
+
+			/*
+			 * Case 2: userspace supplied Linux resource/CPU
+			 * address Convert it back to PCI/IOA bus address
+			 * before calling the platform EEH backend.
+			 */
+			res_start = res->start;
+			res_len = resource_size(res);
+
+			if ((resource_size_t)addr >= res_start &&
+			    ((resource_size_t)addr - res_start) < res_len) {
+				offset = (resource_size_t)addr - res_start;
+				*normalized = region.start + offset;
+
+				return 0;
+			}
+		}
+	}
+
+	return -EINVAL;
+}
+
 static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
 				       unsigned long arg)
 {
@@ -818,6 +897,17 @@ static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
 		if (copy_from_user(&op, (void __user *)arg, minsz))
 			return -EFAULT;
 
+		if (vfio_spapr_eeh_err_needs_addr_normalize(op.err.type)) {
+			unsigned long normalized;
+			long ret;
+
+			ret = vfio_spapr_eeh_normalize_addr(pe, op.err.addr, &normalized);
+			if (ret)
+				return ret;
+
+			op.err.addr = normalized;
+		}
+
 		return eeh_pe_inject_err(pe, op.err.type, op.err.func,
 					 op.err.addr, op.err.mask);
 	default:
Signature · 1 line
2.51.1
#5 lore raw

Re: [PATCH v4 2/5] vfio/spapr_tce: Normalize EEH IOA error injection addresses

Quoted 25 lines · Narayana Murty N

VFIO_EEH_PE_INJECT_ERR receives a target address from userspace. For userspace such as QEMU, this address may be derived from the host Linux BAR resource. The platform EEH backends, however, expect the PCI/IOA bus address used by firmware error-injection interfaces.

Normalize IOA/MMIO error-injection addresses in the sPAPR VFIO EEH ioctl path before dispatching to eeh_pe_inject_err(). If the supplied address is already a PCI bus BAR address it is left unchanged. If it is a Linux resource address, translate it to the corresponding PCI bus address using the BAR-relative offset.

Keep the helper local to VFIO so the address semantics of other in-kernel EEH callers are unchanged. This provides common handling for both pseries and PowerNV backends.

---
  drivers/vfio/vfio_iommu_spapr_tce.c | 90 +++++++++++++++++++++++++++++
  1 file changed, 90 insertions(+)
drivers/vfio/vfio_iommu_spapr_tce.c +0 -0
index 1c0eec228cc6..8a6cc856d7da 100644

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_create_default_window(struct tce_container *container) · line 783

Quoted 10 lines
drivers/vfio/vfio_iommu_spapr_tce.c +7 -0
@@ -774,6 +774,85 @@ static long tce_iommu_create_default_window(struct tce_container *container)
  	return ret;
  }
 
+static bool vfio_spapr_eeh_err_needs_addr_normalize(unsigned int type)
+{
+	if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
+		return false;
+
+	return true;
+}

Do we really need this function? It is only used once.

Quoted 11 lines
drivers/vfio/vfio_iommu_spapr_tce.c +11 -0
+
+static int vfio_spapr_eeh_normalize_addr(struct eeh_pe *pe,
+					 unsigned long addr,
+					 unsigned long *normalized)
+{
+	struct pci_bus_region region;
+	struct eeh_dev *edev, *tmp;
+	struct pci_dev *pdev;
+	struct resource *res;
+	resource_size_t pci_start, pci_len;
+	resource_size_t res_start, res_len;

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_create_default_window(struct tce_container *container) · line 804

Quoted 10 lines
drivers/vfio/vfio_iommu_spapr_tce.c +10 -0
+	resource_size_t offset;
+	int bar;
+
+	if (!pe || !normalized)
+		return -EINVAL;
+
+	if (!addr) {
+		*normalized = addr;
+		return 0;
+	}

This silently bypasses normalization for addr == 0. A comment on why zero is exempted would be helpful.

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_create_default_window(struct tce_container *container) · line 812

Quoted 8 lines
drivers/vfio/vfio_iommu_spapr_tce.c +8 -0
+
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (!pdev)
+			continue;
+
+		for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+			res = &pdev->resource[bar];

I'm not very familiar with this PCI resource regions, so quick question. is pdev->resource[] limited to PCI_STD_NUM_BARS?

Also doesn't this need locking around the traversal? This dereferences pdev->resource with no lock held, what stops hot-removal or BAR reassignment racing this?

Quoted 47 lines
drivers/vfio/vfio_iommu_spapr_tce.c +43 -0
+
+			if (!resource_size(res))
+				continue;
+
+			if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
+				continue;
+
+			pcibios_resource_to_bus(pdev->bus, &region, res);
+
+			pci_start = region.start;
+			pci_len = resource_size(res);
+
+			/*
+			 * Case 1: userspace already supplied PCI/IOA
+			 * bus address.
+			 */
+			if ((resource_size_t)addr >= pci_start &&
+			    ((resource_size_t)addr - pci_start) < pci_len) {
+				*normalized = addr;
+				return 0;
+			}
+
+			/*
+			 * Case 2: userspace supplied Linux resource/CPU
+			 * address Convert it back to PCI/IOA bus address
+			 * before calling the platform EEH backend.
+			 */
+			res_start = res->start;
+			res_len = resource_size(res);
+
+			if ((resource_size_t)addr >= res_start &&
+			    ((resource_size_t)addr - res_start) < res_len) {
+				offset = (resource_size_t)addr - res_start;
+				*normalized = region.start + offset;
+
+				return 0;
+			}
+		}
+	}
+
+	return -EINVAL;
+}
+
  static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
  				       unsigned long arg)
  {

drivers/vfio/vfio_iommu_spapr_tce.c static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group, · line 900

Quoted 4 lines
drivers/vfio/vfio_iommu_spapr_tce.c +1 -0
@@ -818,6 +897,17 @@ static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
  		if (copy_from_user(&op, (void __user *)arg, minsz))
  			return -EFAULT;
 
+		if (vfio_spapr_eeh_err_needs_addr_normalize(op.err.type)) {

At this point eeh_pe_inject_err is still using eeh_pe_inject_mmio_error, so why to normalize the address in this patch itself? Isn't this is a good candidate for 4/5 patch?

drivers/vfio/vfio_iommu_spapr_tce.c static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group, · line 906

Quoted 6 lines
drivers/vfio/vfio_iommu_spapr_tce.c +6 -0
+			unsigned long normalized;
+			long ret;
+
+			ret = vfio_spapr_eeh_normalize_addr(pe, op.err.addr, &normalized);
+			if (ret)
+				return ret;

We should have error/debug log to tell eeh is not going through.

Quoted 7 lines
drivers/vfio/vfio_iommu_spapr_tce.c +4 -0
+
+			op.err.addr = normalized;
+		}
+
  		return eeh_pe_inject_err(pe, op.err.type, op.err.func,
  					 op.err.addr, op.err.mask);
  	default:
#6 lore raw

Re: [PATCH v4 2/5] vfio/spapr_tce: Normalize EEH IOA error injection addresses

Hi Sourabh,

Quoted 57 lines · Sourabh Jain
Quoted 25 lines · Narayana Murty N

VFIO_EEH_PE_INJECT_ERR receives a target address from userspace.  For userspace such as QEMU, this address may be derived from the host Linux BAR resource.  The platform EEH backends, however, expect the PCI/IOA bus address used by firmware error-injection interfaces.

Normalize IOA/MMIO error-injection addresses in the sPAPR VFIO EEH ioctl path before dispatching to eeh_pe_inject_err().  If the supplied address is already a PCI bus BAR address it is left unchanged.  If it is a Linux resource address, translate it to the corresponding PCI bus address using the BAR-relative offset.

Keep the helper local to VFIO so the address semantics of other in-kernel EEH callers are unchanged.  This provides common handling for both pseries and PowerNV backends.

---
  drivers/vfio/vfio_iommu_spapr_tce.c | 90 +++++++++++++++++++++++++++++

  1 file changed, 90 insertions(+)
drivers/vfio/vfio_iommu_spapr_tce.c +0 -0
index 1c0eec228cc6..8a6cc856d7da 100644

drivers/vfio/vfio_iommu_spapr_tce.c static long · line 783

Quoted 10 lines
drivers/vfio/vfio_iommu_spapr_tce.c +7 -0
@@ -774,6 +774,85 @@ static long 
 tce_iommu_create_default_window(struct tce_container *container)
       return ret;
   }
+static bool vfio_spapr_eeh_err_needs_addr_normalize(unsigned int type)
+{
+    if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
+        return false;
+
+    return true;
+}

Do we really need this function? It is only used once.

Quoted 16 lines
drivers/vfio/vfio_iommu_spapr_tce.c +16 -0
+
+static int vfio_spapr_eeh_normalize_addr(struct eeh_pe *pe,
+                     unsigned long addr,
+                     unsigned long *normalized)
+{
+    struct pci_bus_region region;
+    struct eeh_dev *edev, *tmp;
+    struct pci_dev *pdev;
+    struct resource *res;
+    resource_size_t pci_start, pci_len;
+    resource_size_t res_start, res_len;
+    resource_size_t offset;
+    int bar;
+
+    if (!pe || !normalized)
+        return -EINVAL;
Quoted 8 lines

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_create_default_window(struct tce_container *container) · line 804

Quoted 5 lines
drivers/vfio/vfio_iommu_spapr_tce.c +5 -0
+
+    if (!addr) {
+        *normalized = addr;
+        return 0;
+    }

This silently bypasses normalization for addr == 0. A comment on why zero is exempted would be helpful.

Good point.

In v5 I made the no-address case explicit as addr == 0 && mask == 0, rather than bypassing normalization based on the address alone.

I also added a comment explaining that this represents an injection with no specific IOA target and therefore does not require BAR translation.

Quoted 13 lines

drivers/vfio/vfio_iommu_spapr_tce.c static long tce_iommu_create_default_window(struct tce_container *container) · line 812

Quoted 9 lines
drivers/vfio/vfio_iommu_spapr_tce.c +8 -0
+
+    eeh_pe_for_each_dev(pe, edev, tmp) {
+        pdev = eeh_dev_to_pci_dev(edev);
+        if (!pdev)
+            continue;
+
+        for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+            res = &pdev->resource[bar];

I'm not very familiar with this PCI resource regions, so quick question. is pdev->resource[] limited to PCI_STD_NUM_BARS?

Quoted 3 lines

Also doesn't this need locking around the traversal? This dereferences pdev->resource with no lock held, what stops hot-removal or BAR reassignment racing this?

pdev->resource[] is not limited to the standard BARs. The lookup intentionally searches only BAR0..BAR5 using PCI_STD_NUM_BARS; ROM and bridge resources are not considered IOA bus-error targets.

For the lifetime issue, agreed. In v5 I hold pci_rescan_remove_lock while walking the PE devices and accessing their BAR resources so device removal cannot race the traversal.

Quoted 46 lines
Quoted 46 lines
drivers/vfio/vfio_iommu_spapr_tce.c +43 -0
+
+            if (!resource_size(res))
+                continue;
+
+            if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
+                continue;
+
+            pcibios_resource_to_bus(pdev->bus, &region, res);
+
+            pci_start = region.start;
+            pci_len = resource_size(res);
+
+            /*
+             * Case 1: userspace already supplied PCI/IOA
+             * bus address.
+             */
+            if ((resource_size_t)addr >= pci_start &&
+                ((resource_size_t)addr - pci_start) < pci_len) {
+                *normalized = addr;
+                return 0;
+            }
+
+            /*
+             * Case 2: userspace supplied Linux resource/CPU
+             * address Convert it back to PCI/IOA bus address
+             * before calling the platform EEH backend.
+             */
+            res_start = res->start;
+            res_len = resource_size(res);
+
+            if ((resource_size_t)addr >= res_start &&
+                ((resource_size_t)addr - res_start) < res_len) {
+                offset = (resource_size_t)addr - res_start;
+                *normalized = region.start + offset;
+
+                return 0;
+            }
+        }
+    }
+
+    return -EINVAL;
+}
+
   static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
                          unsigned long arg)
Quoted 8 lines

drivers/vfio/vfio_iommu_spapr_tce.c static long vfio_spapr_ioctl_eeh_pe_op(struct · line 900

Quoted 6 lines
drivers/vfio/vfio_iommu_spapr_tce.c +1 -0
   {
@@ -818,6 +897,17 @@ static long vfio_spapr_ioctl_eeh_pe_op(struct 
 iommu_group *group,
           if (copy_from_user(&op, (void __user *)arg, minsz))
               return -EFAULT;
+        if (vfio_spapr_eeh_err_needs_addr_normalize(op.err.type)) {

At this point eeh_pe_inject_err is still using eeh_pe_inject_mmio_error, so why to normalize the address in this patch itself? Isn't this is a good candidate for 4/5 patch?

Agreed.

I reordered the series in v5 so that the pSeries RTAS error-injection implementation is added before the VFIO address-normalization patch.

The normalization patch is now 4/5, after the pSeries backend has switched from the legacy MMIO injection path to RTAS.

Quoted 10 lines

drivers/vfio/vfio_iommu_spapr_tce.c static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group, · line 906

Quoted 7 lines
drivers/vfio/vfio_iommu_spapr_tce.c +6 -0
+            unsigned long normalized;
+            long ret;
+
+            ret = vfio_spapr_eeh_normalize_addr(pe, op.err.addr, 
 &normalized);
+            if (ret)
+                return ret;

We should have error/debug log to tell eeh is not going through.

Agreed. I added a pr_debug() in v5 when the supplied address cannot be matched against any PCI bus BAR of the PE.

Thanks,
Narayana

Quoted 8 lines
Quoted 7 lines
drivers/vfio/vfio_iommu_spapr_tce.c +4 -0
+
+            op.err.addr = normalized;
+        }
+
           return eeh_pe_inject_err(pe, op.err.type, op.err.func,
                        op.err.addr, op.err.mask);

      default:

#7 lore raw

PATCH v4 3/5 powerpc/pseries/eeh: Add RTAS error validation helpers

In reply to Narayana Murty N

Add pseries-specific infrastructure for RTAS-based EEH error injection.

Define pr_fmt unconditionally at the top of eeh_pseries.c so all pr_*() calls carry the "EEH: " prefix:

  #define pr_fmt(fmt) "EEH: " fmt

Define RTAS firmware error-type encodings as file-local constants. Only the two IOA bus-error types are supported; other PAPR encodings are not reachable through the generic EEH_ERR_TYPE_* UAPI:

RTAS_ERR_TYPE_IOA_BUS_ERROR 0x07
RTAS_ERR_TYPE_IOA_BUS_ERROR_64 0x0f

Add RTAS_ERRINJCT_BUF_SIZE for the ibm,errinjct work buffer size.

Add pseries_eeh_type_to_rtas() to translate the two generic EEH error types (EEH_ERR_TYPE_32, EEH_ERR_TYPE_64) into the corresponding RTAS ibm,errinjct encodings.

Add validate_addr_mask_in_pe() to verify that a caller-provided address falls within a BAR of some device in the PE. RTAS IOA bus-error injection requires a PCI/IOA bus address; validate_addr_mask_in_pe() accepts only PCI bus addresses. VFIO userspace resource addresses are normalized to PCI bus addresses in vfio_iommu_spapr_tce.c before reaching this backend; no resource-address fallback is added here. Use pcibios_resource_to_bus() to convert each BAR resource to PCI bus address space before validating the address. Returns -EINVAL (Linux errno) rather than a raw RTAS status.

Add validate_errinjct_args() as a top-level validation wrapper that checks the PE pointer, maps the generic type, and validates the function range. Address/mask validation against BARs is done in the buffer preparation step.

Add prepare_errinjct_buffer() which:
- guards against NULL buf/pe/pe->phb
- zeroes the buffer unconditionally with memset()
- checks 32-bit truncation for the IOA_BUS_ERROR case
- calls validate_addr_mask_in_pe() for address validation
- supports only the IOA bus-error types (unreachable non-IOA RTAS cases removed)
The caller provides an exclusive per-call RTAS work-area buffer. Firmware session serialization is left to the caller.

pseries_eeh_err_inject() retains the existing MMIO injection body unchanged in this patch. It is replaced by the full RTAS session implementation in the next patch. This keeps the patch bisectable:
each commit builds and functions correctly independently.

---
 arch/powerpc/platforms/pseries/eeh_pseries.c | 215 +++++++++++++++++++
 1 file changed, 215 insertions(+)
arch/powerpc/platforms/pseries/eeh_pseries.c +215 -0
index b12ef382fec7..fafe0004e738 100644
@@ -12,6 +12,8 @@
  * Copyright Linas Vepstas 2005, 2006
  */
 
+#define pr_fmt(fmt) "EEH: " fmt
+
 #include <linux/atomic.h>
 #include <linux/delay.h>
 #include <linux/export.h>
@@ -786,6 +788,219 @@ static int pseries_notify_resume(struct eeh_dev *edev)
 }
 #endif
 
+/*
+ * RTAS firmware error-type encodings (PAPR).  These are pseries-private;
+ * user-space sees only the generic EEH_ERR_TYPE_* values from eeh.h.
+ * Only IOA bus-error types are supported; other PAPR encodings are not
+ * reachable through the generic EEH_ERR_TYPE_* UAPI.
+ */
+#define RTAS_ERR_TYPE_IOA_BUS_ERROR		0x07
+#define RTAS_ERR_TYPE_IOA_BUS_ERROR_64		0x0f
+
+/* Size of the ibm,errinjct work buffer as defined by PAPR */
+#define RTAS_ERRINJCT_BUF_SIZE			SZ_1K
+
+/**
+ * pseries_eeh_type_to_rtas - Map generic EEH error type to RTAS encoding
+ * @type: generic EEH error type (EEH_ERR_TYPE_32, EEH_ERR_TYPE_64)
+ *
+ * Translates the generic VFIO/EEH error type passed from userspace into
+ * the RTAS-specific ibm,errinjct error type encoding defined by PAPR.
+ *
+ * Return: RTAS error type on success, -EINVAL for unknown types.
+ */
+static int pseries_eeh_type_to_rtas(int type)
+{
+	switch (type) {
+	case EEH_ERR_TYPE_32:
+		return RTAS_ERR_TYPE_IOA_BUS_ERROR;
+	case EEH_ERR_TYPE_64:
+		return RTAS_ERR_TYPE_IOA_BUS_ERROR_64;
+	default:
+		return -EINVAL;
+	}
+}
+
+/**
+ * validate_addr_mask_in_pe - Validate addr against PCI bus BAR addresses in PE
+ * @pe:   EEH PE containing one or more PCI devices
+ * @addr: PCI bus address to validate
+ * @mask: address mask (passed to firmware unchanged)
+ *
+ * RTAS IOA bus-error injection uses PCI bus addresses.  Linux PCI resources
+ * are CPU/resource addresses and may differ on pseries due to PHB window
+ * translation.  Convert each BAR resource to PCI bus address space before
+ * validating @addr.
+ *
+ * Zero addr and mask are accepted without BAR lookup (no-address injection).
+ *
+ * Return: 0 if valid, -EINVAL on invalid input.
+ */
+static int validate_addr_mask_in_pe(struct eeh_pe *pe, unsigned long addr,
+				    unsigned long mask)
+{
+	struct pci_bus_region region;
+	struct eeh_dev *edev, *tmp;
+	struct pci_dev *pdev;
+	struct resource *res;
+	resource_size_t bus_start;
+	resource_size_t bus_len;
+	int bar;
+
+	if (!addr && !mask)
+		return 0;
+
+	if (!pe)
+		return -EINVAL;
+
+	/*
+	 * RTAS IOA bus-error injection uses PCI bus addresses. Linux PCI
+	 * resources are CPU/resource addresses and may differ on pseries due
+	 * to PHB window translation. Convert each BAR resource to PCI bus
+	 * address space before validating @addr.
+	 */
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (!pdev)
+			continue;
+
+		for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+			res = &pdev->resource[bar];
+
+			if (!resource_size(res))
+				continue;
+
+			if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
+				continue;
+
+			pcibios_resource_to_bus(pdev->bus, &region, res);
+
+			bus_start = region.start;
+			bus_len = resource_size(res);
+
+			if ((resource_size_t)addr >= bus_start &&
+			    ((resource_size_t)addr - bus_start) < bus_len) {
+				pr_debug("addr=0x%lx mask=0x%lx validated in PCI bus BAR[%d] of %s: bus range 0x%llx-0x%llx\n",
+					 addr, mask, bar, pci_name(pdev),
+					 (unsigned long long)region.start,
+					 (unsigned long long)region.end);
+				return 0;
+			}
+		}
+	}
+
+	pr_err("addr=0x%lx mask=0x%lx not within any PCI bus BAR of any device in PE\n",
+	       addr, mask);
+	return -EINVAL;
+}
+
+/**
+ * validate_errinjct_args - Top-level validation for RTAS error injection arguments
+ * @pe:   EEH PE for the target device
+ * @type: generic EEH error type
+ * @func: error function selector
+ * @addr: address argument (type-dependent, may be zero)
+ * @mask: mask argument (type-dependent, may be zero)
+ *
+ * Validates all parameters before opening an RTAS injection session.
+ * Returns Linux errno values; does not return raw RTAS status codes.
+ *
+ * Return: 0 if all parameters are valid, negative errno otherwise.
+ */
+static int validate_errinjct_args(struct eeh_pe *pe, int type, int func,
+				  unsigned long addr, unsigned long mask)
+{
+	if (!pe || !pe->phb)
+		return -EINVAL;
+
+	if (pseries_eeh_type_to_rtas(type) < 0)
+		return -EINVAL;
+
+	if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
+		return -EINVAL;
+
+	return 0;
+}
+
+/**
+ * prepare_errinjct_buffer() - Build ibm,errinjct work buffer
+ * @buf: RTAS error-injection work buffer
+ * @pe: EEH PE associated with the injection target
+ * @rtas_type: RTAS firmware error-injection type
+ * @func: Error function selector
+ * @addr: Target PCI bus address
+ * @mask: Address mask passed to firmware
+ *
+ * Zeroes @buf and populates it according to the PAPR layout for the
+ * selected IOA bus-error injection type.
+ *
+ * The caller provides an exclusive per-call RTAS work-area buffer.
+ * Firmware session serialization is handled by pseries_eeh_err_inject().
+ *
+ * Return: 0 on success or a negative errno on invalid input.
+ */
+static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,
+				   int rtas_type, int func,
+				   unsigned long addr, unsigned long mask)
+{
+	__be64 *buf64 = (__be64 *)buf;
+	__be32 *buf32 = (__be32 *)buf;
+	int rc;
+
+	if (!buf || !pe || !pe->phb)
+		return -EINVAL;
+
+	memset(buf, 0, RTAS_ERRINJCT_BUF_SIZE);
+
+	switch (rtas_type) {
+	case RTAS_ERR_TYPE_IOA_BUS_ERROR:
+		if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
+			return -EINVAL;
+
+		if (upper_32_bits(addr) || upper_32_bits(mask)) {
+			pr_err("32-bit IOA injection cannot encode addr=%#lx mask=%#lx\n",
+			       addr, mask);
+			return -EINVAL;
+		}
+
+		rc = validate_addr_mask_in_pe(pe, addr, mask);
+		if (rc)
+			return rc;
+
+		buf32[0] = cpu_to_be32((u32)addr);
+		buf32[1] = cpu_to_be32((u32)mask);
+		buf32[2] = cpu_to_be32(pe->addr);
+		buf32[3] = cpu_to_be32(BUID_HI(pe->phb->buid));
+		buf32[4] = cpu_to_be32(BUID_LO(pe->phb->buid));
+		buf32[5] = cpu_to_be32(func);
+		break;
+
+	case RTAS_ERR_TYPE_IOA_BUS_ERROR_64:
+		if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
+			return -EINVAL;
+
+		rc = validate_addr_mask_in_pe(pe, addr, mask);
+		if (rc)
+			return rc;
+
+		buf64[0] = cpu_to_be64(addr);
+		buf64[1] = cpu_to_be64(mask);
+		buf32[4] = cpu_to_be32(pe->addr);
+		buf32[5] = cpu_to_be32(BUID_HI(pe->phb->buid));
+		buf32[6] = cpu_to_be32(BUID_LO(pe->phb->buid));
+		buf32[7] = cpu_to_be32(func);
+		break;
+
+	default:
+		pr_err("unsupported RTAS error injection type 0x%x\n", rtas_type);
+		return -EINVAL;
+	}
+
+	pr_debug("errinjct buffer ready: rtas_type=0x%x func=%d addr=0x%lx mask=0x%lx\n",
+		 rtas_type, func, addr, mask);
+	return 0;
+}
+
 /**
  * pseries_eeh_err_inject - Inject specified error to the indicated PE
  * @pe: the indicated PE
Signature · 1 line
2.51.1
#8 lore raw

Re: [PATCH v4 3/5] powerpc/pseries/eeh: Add RTAS error validation helpers

Quoted 6 lines · Narayana Murty N

Add pseries-specific infrastructure for RTAS-based EEH error injection.

Define pr_fmt unconditionally at the top of eeh_pseries.c so all pr_*() calls carry the "EEH: " prefix:

   #define pr_fmt(fmt) "EEH: " fmt

Could you please describe the overall goal of the helpers introduced by this patch first, before discussing pr_fmt and the other macros?

Quoted 197 lines

Define RTAS firmware error-type encodings as file-local constants. Only the two IOA bus-error types are supported; other PAPR encodings are not reachable through the generic EEH_ERR_TYPE_* UAPI:

   RTAS_ERR_TYPE_IOA_BUS_ERROR     0x07
   RTAS_ERR_TYPE_IOA_BUS_ERROR_64  0x0f

Add RTAS_ERRINJCT_BUF_SIZE for the ibm,errinjct work buffer size.

Add pseries_eeh_type_to_rtas() to translate the two generic EEH error types (EEH_ERR_TYPE_32, EEH_ERR_TYPE_64) into the corresponding RTAS ibm,errinjct encodings.

Add validate_addr_mask_in_pe() to verify that a caller-provided address falls within a BAR of some device in the PE. RTAS IOA bus-error injection requires a PCI/IOA bus address; validate_addr_mask_in_pe() accepts only PCI bus addresses. VFIO userspace resource addresses are normalized to PCI bus addresses in vfio_iommu_spapr_tce.c before reaching this backend; no resource-address fallback is added here. Use pcibios_resource_to_bus() to convert each BAR resource to PCI bus address space before validating the address. Returns -EINVAL (Linux errno) rather than a raw RTAS status.

Add validate_errinjct_args() as a top-level validation wrapper that checks the PE pointer, maps the generic type, and validates the function range. Address/mask validation against BARs is done in the buffer preparation step.

Add prepare_errinjct_buffer() which:
- guards against NULL buf/pe/pe->phb
- zeroes the buffer unconditionally with memset()
- checks 32-bit truncation for the IOA_BUS_ERROR case
- calls validate_addr_mask_in_pe() for address validation
- supports only the IOA bus-error types (unreachable non-IOA RTAS cases removed)
The caller provides an exclusive per-call RTAS work-area buffer. Firmware session serialization is left to the caller.

pseries_eeh_err_inject() retains the existing MMIO injection body unchanged in this patch. It is replaced by the full RTAS session implementation in the next patch. This keeps the patch bisectable:
each commit builds and functions correctly independently.

---
  arch/powerpc/platforms/pseries/eeh_pseries.c | 215 +++++++++++++++++++
  1 file changed, 215 insertions(+)
arch/powerpc/platforms/pseries/eeh_pseries.c +131 -0
index b12ef382fec7..fafe0004e738 100644
@@ -12,6 +12,8 @@
   * Copyright Linas Vepstas 2005, 2006
   */
 
+#define pr_fmt(fmt) "EEH: " fmt
+
  #include <linux/atomic.h>
  #include <linux/delay.h>
  #include <linux/export.h>
@@ -786,6 +788,219 @@ static int pseries_notify_resume(struct eeh_dev *edev)
  }
  #endif
 
+/*
+ * RTAS firmware error-type encodings (PAPR).  These are pseries-private;
+ * user-space sees only the generic EEH_ERR_TYPE_* values from eeh.h.
+ * Only IOA bus-error types are supported; other PAPR encodings are not
+ * reachable through the generic EEH_ERR_TYPE_* UAPI.
+ */
+#define RTAS_ERR_TYPE_IOA_BUS_ERROR		0x07
+#define RTAS_ERR_TYPE_IOA_BUS_ERROR_64		0x0f
+
+/* Size of the ibm,errinjct work buffer as defined by PAPR */
+#define RTAS_ERRINJCT_BUF_SIZE			SZ_1K
+
+/**
+ * pseries_eeh_type_to_rtas - Map generic EEH error type to RTAS encoding
+ * @type: generic EEH error type (EEH_ERR_TYPE_32, EEH_ERR_TYPE_64)
+ *
+ * Translates the generic VFIO/EEH error type passed from userspace into
+ * the RTAS-specific ibm,errinjct error type encoding defined by PAPR.
+ *
+ * Return: RTAS error type on success, -EINVAL for unknown types.
+ */
+static int pseries_eeh_type_to_rtas(int type)
+{
+	switch (type) {
+	case EEH_ERR_TYPE_32:
+		return RTAS_ERR_TYPE_IOA_BUS_ERROR;
+	case EEH_ERR_TYPE_64:
+		return RTAS_ERR_TYPE_IOA_BUS_ERROR_64;
+	default:
+		return -EINVAL;
+	}
+}
+
+/**
+ * validate_addr_mask_in_pe - Validate addr against PCI bus BAR addresses in PE
+ * @pe:   EEH PE containing one or more PCI devices
+ * @addr: PCI bus address to validate
+ * @mask: address mask (passed to firmware unchanged)
+ *
+ * RTAS IOA bus-error injection uses PCI bus addresses.  Linux PCI resources
+ * are CPU/resource addresses and may differ on pseries due to PHB window
+ * translation.  Convert each BAR resource to PCI bus address space before
+ * validating @addr.
+ *
+ * Zero addr and mask are accepted without BAR lookup (no-address injection).
+ *
+ * Return: 0 if valid, -EINVAL on invalid input.
+ */
+static int validate_addr_mask_in_pe(struct eeh_pe *pe, unsigned long addr,
+				    unsigned long mask)
+{
+	struct pci_bus_region region;
+	struct eeh_dev *edev, *tmp;
+	struct pci_dev *pdev;
+	struct resource *res;
+	resource_size_t bus_start;
+	resource_size_t bus_len;
+	int bar;
+
+	if (!addr && !mask)
+		return 0;
+
+	if (!pe)
+		return -EINVAL;
+
+	/*
+	 * RTAS IOA bus-error injection uses PCI bus addresses. Linux PCI
+	 * resources are CPU/resource addresses and may differ on pseries due
+	 * to PHB window translation. Convert each BAR resource to PCI bus
+	 * address space before validating @addr.
+	 */
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (!pdev)
+			continue;
+
+		for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+			res = &pdev->resource[bar];
+
+			if (!resource_size(res))
+				continue;
+
+			if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
+				continue;
+
+			pcibios_resource_to_bus(pdev->bus, &region, res);
+
+			bus_start = region.start;
+			bus_len = resource_size(res);
+
+			if ((resource_size_t)addr >= bus_start &&
+			    ((resource_size_t)addr - bus_start) < bus_len) {
+				pr_debug("addr=0x%lx mask=0x%lx validated in PCI bus BAR[%d] of %s: bus range 0x%llx-0x%llx\n",
+					 addr, mask, bar, pci_name(pdev),
+					 (unsigned long long)region.start,
+					 (unsigned long long)region.end);
+				return 0;
+			}
+		}
+	}
+
+	pr_err("addr=0x%lx mask=0x%lx not within any PCI bus BAR of any device in PE\n",
+	       addr, mask);
+	return -EINVAL;
+}
+
+/**
+ * validate_errinjct_args - Top-level validation for RTAS error injection arguments
+ * @pe:   EEH PE for the target device
+ * @type: generic EEH error type
+ * @func: error function selector
+ * @addr: address argument (type-dependent, may be zero)
+ * @mask: mask argument (type-dependent, may be zero)
+ *
+ * Validates all parameters before opening an RTAS injection session.
+ * Returns Linux errno values; does not return raw RTAS status codes.
+ *
+ * Return: 0 if all parameters are valid, negative errno otherwise.
+ */
+static int validate_errinjct_args(struct eeh_pe *pe, int type, int func,
+				  unsigned long addr, unsigned long mask)
+{
+	if (!pe || !pe->phb)
+		return -EINVAL;
+
+	if (pseries_eeh_type_to_rtas(type) < 0)
+		return -EINVAL;
+
+	if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)

arch/powerpc/platforms/pseries/eeh_pseries.c static int pseries_notify_resume(struct eeh_dev *edev) · line 929

Quoted 10 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +10 -0
+		return -EINVAL;
+
+	return 0;
+}
+
+/**
+ * prepare_errinjct_buffer() - Build ibm,errinjct work buffer
+ * @buf: RTAS error-injection work buffer
+ * @pe: EEH PE associated with the injection target
+ * @rtas_type: RTAS firmware error-injection type

rtas_type is confusing to me. How about err_type?

Quoted 19 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +19 -0
+ * @func: Error function selector
+ * @addr: Target PCI bus address
+ * @mask: Address mask passed to firmware
+ *
+ * Zeroes @buf and populates it according to the PAPR layout for the
+ * selected IOA bus-error injection type.
+ *
+ * The caller provides an exclusive per-call RTAS work-area buffer.
+ * Firmware session serialization is handled by pseries_eeh_err_inject().
+ *
+ * Return: 0 on success or a negative errno on invalid input.
+ */
+static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,
+				   int rtas_type, int func,
+				   unsigned long addr, unsigned long mask)
+{
+	__be64 *buf64 = (__be64 *)buf;
+	__be32 *buf32 = (__be32 *)buf;
+	int rc;

arch/powerpc/platforms/pseries/eeh_pseries.c static int pseries_notify_resume(struct eeh_dev *edev) · line 958

Quoted 10 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +10 -0
+
+	if (!buf || !pe || !pe->phb)
+		return -EINVAL;
+
+	memset(buf, 0, RTAS_ERRINJCT_BUF_SIZE);
+
+	switch (rtas_type) {
+	case RTAS_ERR_TYPE_IOA_BUS_ERROR:
+		if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
+			return -EINVAL;

This condition is common for both cases, so let take it out of switch. And lets move the func check before memset.

Quoted 48 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +45 -0
+
+		if (upper_32_bits(addr) || upper_32_bits(mask)) {
+			pr_err("32-bit IOA injection cannot encode addr=%#lx mask=%#lx\n",
+			       addr, mask);
+			return -EINVAL;
+		}
+
+		rc = validate_addr_mask_in_pe(pe, addr, mask);
+		if (rc)
+			return rc;
+
+		buf32[0] = cpu_to_be32((u32)addr);
+		buf32[1] = cpu_to_be32((u32)mask);
+		buf32[2] = cpu_to_be32(pe->addr);
+		buf32[3] = cpu_to_be32(BUID_HI(pe->phb->buid));
+		buf32[4] = cpu_to_be32(BUID_LO(pe->phb->buid));
+		buf32[5] = cpu_to_be32(func);
+		break;
+
+	case RTAS_ERR_TYPE_IOA_BUS_ERROR_64:
+		if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
+			return -EINVAL;
+
+		rc = validate_addr_mask_in_pe(pe, addr, mask);
+		if (rc)
+			return rc;
+
+		buf64[0] = cpu_to_be64(addr);
+		buf64[1] = cpu_to_be64(mask);
+		buf32[4] = cpu_to_be32(pe->addr);
+		buf32[5] = cpu_to_be32(BUID_HI(pe->phb->buid));
+		buf32[6] = cpu_to_be32(BUID_LO(pe->phb->buid));
+		buf32[7] = cpu_to_be32(func);
+		break;
+
+	default:
+		pr_err("unsupported RTAS error injection type 0x%x\n", rtas_type);
+		return -EINVAL;
+	}
+
+	pr_debug("errinjct buffer ready: rtas_type=0x%x func=%d addr=0x%lx mask=0x%lx\n",
+		 rtas_type, func, addr, mask);
+	return 0;
+}
+
  /**
   * pseries_eeh_err_inject - Inject specified error to the indicated PE
   * @pe: the indicated PE
#9 lore raw

Re: [PATCH v4 3/5] powerpc/pseries/eeh: Add RTAS error validation helpers

Hi Sourabh,

Quoted 4 lines · Sourabh Jain
Quoted 2 lines · Narayana Murty N

Add pseries-specific infrastructure for RTAS-based EEH error injection.

Quoted 8 lines
Quoted 4 lines

Define pr_fmt unconditionally at the top of eeh_pseries.c so all pr_*() calls carry the "EEH: " prefix:

   #define pr_fmt(fmt) "EEH: " fmt

Could you please describe the overall goal of the helpers introduced by this patch first, before discussing pr_fmt and the other macros?

Agreed. I reworked the commit message in v5 to start with the purpose of the helpers: mapping the generic EEH types, validating the injection arguments and target address, and constructing the PAPR ibm,errinjct parameter buffer.

The implementation details such as pr_fmt and the RTAS constants follow that description.

Quoted 212 lines
Quoted 212 lines

Define RTAS firmware error-type encodings as file-local constants. Only the two IOA bus-error types are supported; other PAPR encodings are not reachable through the generic EEH_ERR_TYPE_* UAPI:

   RTAS_ERR_TYPE_IOA_BUS_ERROR     0x07
   RTAS_ERR_TYPE_IOA_BUS_ERROR_64  0x0f

Add RTAS_ERRINJCT_BUF_SIZE for the ibm,errinjct work buffer size.

Add pseries_eeh_type_to_rtas() to translate the two generic EEH error types (EEH_ERR_TYPE_32, EEH_ERR_TYPE_64) into the corresponding RTAS ibm,errinjct encodings.

Add validate_addr_mask_in_pe() to verify that a caller-provided address falls within a BAR of some device in the PE.  RTAS IOA bus-error injection requires a PCI/IOA bus address; validate_addr_mask_in_pe() accepts only PCI bus addresses.  VFIO userspace resource addresses are normalized to PCI bus addresses in vfio_iommu_spapr_tce.c before reaching this backend; no resource-address fallback is added here. Use pcibios_resource_to_bus() to convert each BAR resource to PCI bus address space before validating the address.  Returns -EINVAL (Linux errno) rather than a raw RTAS status.

Add validate_errinjct_args() as a top-level validation wrapper that checks the PE pointer, maps the generic type, and validates the function range.  Address/mask validation against BARs is done in the buffer preparation step.

Add prepare_errinjct_buffer() which:
   - guards against NULL buf/pe/pe->phb
   - zeroes the buffer unconditionally with memset()
   - checks 32-bit truncation for the IOA_BUS_ERROR case
   - calls validate_addr_mask_in_pe() for address validation
   - supports only the IOA bus-error types (unreachable non-IOA RTAS cases removed)
The caller provides an exclusive per-call RTAS work-area buffer. Firmware session serialization is left to the caller.

pseries_eeh_err_inject() retains the existing MMIO injection body unchanged in this patch.  It is replaced by the full RTAS session implementation in the next patch.  This keeps the patch bisectable:
each commit builds and functions correctly independently.

---
  arch/powerpc/platforms/pseries/eeh_pseries.c | 215 +++++++++++++++++++

  1 file changed, 215 insertions(+)
arch/powerpc/platforms/pseries/eeh_pseries.c +16 -0
index b12ef382fec7..fafe0004e738 100644
@@ -12,6 +12,8 @@
    * Copyright Linas Vepstas 2005, 2006
    */
+#define pr_fmt(fmt) "EEH: " fmt
+
   #include <linux/atomic.h>
   #include <linux/delay.h>
   #include <linux/export.h>
@@ -786,6 +788,219 @@ static int pseries_notify_resume(struct eeh_dev 
 *edev)
   }
   #endif
+/*
+ * RTAS firmware error-type encodings (PAPR).  These are pseries- 
 private;
+ * user-space sees only the generic EEH_ERR_TYPE_* values from eeh.h.
+ * Only IOA bus-error types are supported; other PAPR encodings are not
+ * reachable through the generic EEH_ERR_TYPE_* UAPI.
+ */
+#define RTAS_ERR_TYPE_IOA_BUS_ERROR        0x07
+#define RTAS_ERR_TYPE_IOA_BUS_ERROR_64        0x0f
+
+/* Size of the ibm,errinjct work buffer as defined by PAPR */
+#define RTAS_ERRINJCT_BUF_SIZE            SZ_1K
+
+/**
+ * pseries_eeh_type_to_rtas - Map generic EEH error type to RTAS 

encoding
+ * @type: generic EEH error type (EEH_ERR_TYPE_32, EEH_ERR_TYPE_64)
+ *
+ * Translates the generic VFIO/EEH error type passed from userspace into
+ * the RTAS-specific ibm,errinjct error type encoding defined by PAPR.
+ *
+ * Return: RTAS error type on success, -EINVAL for unknown types.
+ */

arch/powerpc/platforms/pseries/eeh_pseries.c +14 -0
+static int pseries_eeh_type_to_rtas(int type)
+{
+    switch (type) {
+    case EEH_ERR_TYPE_32:
+        return RTAS_ERR_TYPE_IOA_BUS_ERROR;
+    case EEH_ERR_TYPE_64:
+        return RTAS_ERR_TYPE_IOA_BUS_ERROR_64;
+    default:
+        return -EINVAL;
+    }
+}
+
+/**
+ * validate_addr_mask_in_pe - Validate addr against PCI bus BAR 

addresses in PE
+ * @pe:   EEH PE containing one or more PCI devices
+ * @addr: PCI bus address to validate
+ * @mask: address mask (passed to firmware unchanged)
+ *
+ * RTAS IOA bus-error injection uses PCI bus addresses.  Linux PCI resources
+ * are CPU/resource addresses and may differ on pseries due to PHB window
+ * translation.  Convert each BAR resource to PCI bus address space before
+ * validating @addr.
+ *
+ * Zero addr and mask are accepted without BAR lookup (no-address injection).
+ *
+ * Return: 0 if valid, -EINVAL on invalid input.
+ */
+static int validate_addr_mask_in_pe(struct eeh_pe *pe, unsigned long addr,
+                    unsigned long mask)

arch/powerpc/platforms/pseries/eeh_pseries.c +18 -0
+{
+    struct pci_bus_region region;
+    struct eeh_dev *edev, *tmp;
+    struct pci_dev *pdev;
+    struct resource *res;
+    resource_size_t bus_start;
+    resource_size_t bus_len;
+    int bar;
+
+    if (!addr && !mask)
+        return 0;
+
+    if (!pe)
+        return -EINVAL;
+
+    /*
+     * RTAS IOA bus-error injection uses PCI bus addresses. Linux PCI
+     * resources are CPU/resource addresses and may differ on pseries 

due
+     * to PHB window translation. Convert each BAR resource to PCI bus +     * address space before validating @addr.
+     */
+    eeh_pe_for_each_dev(pe, edev, tmp) {
+        pdev = eeh_dev_to_pci_dev(edev);
+        if (!pdev)
+            continue;

arch/powerpc/platforms/pseries/eeh_pseries.c +18 -0
+
+        for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+            res = &pdev->resource[bar];
+
+            if (!resource_size(res))
+                continue;
+
+            if (!(res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
+                continue;
+
+            pcibios_resource_to_bus(pdev->bus, &region, res);
+
+            bus_start = region.start;
+            bus_len = resource_size(res);
+
+            if ((resource_size_t)addr >= bus_start &&
+                ((resource_size_t)addr - bus_start) < bus_len) {
+                pr_debug("addr=0x%lx mask=0x%lx validated in PCI bus 

BAR[%d] of %s: bus range 0x%llx-0x%llx\n",
+                     addr, mask, bar, pci_name(pdev),
+                     (unsigned long long)region.start,
+                     (unsigned long long)region.end);
+                return 0;
+            }
+        }
+    }
+
+    pr_err("addr=0x%lx mask=0x%lx not within any PCI bus BAR of any device in PE\n",
+           addr, mask);
+    return -EINVAL;

arch/powerpc/platforms/pseries/eeh_pseries.c +4 -0
+}
+
+/**
+ * validate_errinjct_args - Top-level validation for RTAS error 

injection arguments
+ * @pe:   EEH PE for the target device
+ * @type: generic EEH error type
+ * @func: error function selector
+ * @addr: address argument (type-dependent, may be zero)
+ * @mask: mask argument (type-dependent, may be zero)
+ *
+ * Validates all parameters before opening an RTAS injection session.
+ * Returns Linux errno values; does not return raw RTAS status codes.
+ *
+ * Return: 0 if all parameters are valid, negative errno otherwise.
+ */

arch/powerpc/platforms/pseries/eeh_pseries.c +14 -0
+static int validate_errinjct_args(struct eeh_pe *pe, int type, int func,
+                  unsigned long addr, unsigned long mask)
+{
+    if (!pe || !pe->phb)
+        return -EINVAL;
+
+    if (pseries_eeh_type_to_rtas(type) < 0)
+        return -EINVAL;
+
+    if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
+        return -EINVAL;
+
+    return 0;
+}
Quoted 8 lines

arch/powerpc/platforms/pseries/eeh_pseries.c static int pseries_notify_resume(struct eeh_dev *edev) · line 929

Quoted 6 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +6 -0
+
+/**
+ * prepare_errinjct_buffer() - Build ibm,errinjct work buffer
+ * @buf: RTAS error-injection work buffer
+ * @pe: EEH PE associated with the injection target
+ * @rtas_type: RTAS firmware error-injection type

rtas_type is confusing to me. How about err_type?

Agreed on making the distinction clearer. In v5 I renamed it to rtas_err_type, since this value is specifically the RTAS/PAPR error-injection type after mapping from the generic EEH type.

Quoted 26 lines
Quoted 26 lines

+ * @func: Error function selector
+ * @addr: Target PCI bus address
+ * @mask: Address mask passed to firmware
+ *
+ * Zeroes @buf and populates it according to the PAPR layout for the
+ * selected IOA bus-error injection type.
+ *
+ * The caller provides an exclusive per-call RTAS work-area buffer.
+ * Firmware session serialization is handled by pseries_eeh_err_inject().
+ *
+ * Return: 0 on success or a negative errno on invalid input.
+ */

arch/powerpc/platforms/pseries/eeh_pseries.c +12 -0
+static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,
+                   int rtas_type, int func,
+                   unsigned long addr, unsigned long mask)
+{
+    __be64 *buf64 = (__be64 *)buf;
+    __be32 *buf32 = (__be32 *)buf;
+    int rc;
+
+    if (!buf || !pe || !pe->phb)
+        return -EINVAL;
+
+    memset(buf, 0, RTAS_ERRINJCT_BUF_SIZE);
Quoted 8 lines
Quoted 5 lines

+
+    switch (rtas_type) {
+    case RTAS_ERR_TYPE_IOA_BUS_ERROR:
+        if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX) +            return -EINVAL;

This condition is common for both cases, so let take it out of switch. And lets move the func check before memset.

Agreed.

In v5 the function-range check is moved out of the type-specific switch and is owned by validate_errinjct_args(), which runs before prepare_errinjct_buffer().

prepare_errinjct_buffer() therefore no longer duplicates the function validation and can zero/build the buffer after the common arguments have already been validated.

Thanks,
Narayana

Quoted 53 lines
Quoted 52 lines

+
+        if (upper_32_bits(addr) || upper_32_bits(mask)) { +            pr_err("32-bit IOA injection cannot encode addr=%#lx mask=%#lx\n",
+                   addr, mask);
+            return -EINVAL;
+        }

arch/powerpc/platforms/pseries/eeh_pseries.c +31 -0
+
+        rc = validate_addr_mask_in_pe(pe, addr, mask);
+        if (rc)
+            return rc;
+
+        buf32[0] = cpu_to_be32((u32)addr);
+        buf32[1] = cpu_to_be32((u32)mask);
+        buf32[2] = cpu_to_be32(pe->addr);
+        buf32[3] = cpu_to_be32(BUID_HI(pe->phb->buid));
+        buf32[4] = cpu_to_be32(BUID_LO(pe->phb->buid));
+        buf32[5] = cpu_to_be32(func);
+        break;
+
+    case RTAS_ERR_TYPE_IOA_BUS_ERROR_64:
+        if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
+            return -EINVAL;
+
+        rc = validate_addr_mask_in_pe(pe, addr, mask);
+        if (rc)
+            return rc;
+
+        buf64[0] = cpu_to_be64(addr);
+        buf64[1] = cpu_to_be64(mask);
+        buf32[4] = cpu_to_be32(pe->addr);
+        buf32[5] = cpu_to_be32(BUID_HI(pe->phb->buid));
+        buf32[6] = cpu_to_be32(BUID_LO(pe->phb->buid));
+        buf32[7] = cpu_to_be32(func);
+        break;
+
+    default:
+        pr_err("unsupported RTAS error injection type 0x%x\n", 

rtas_type);
+        return -EINVAL;
+    }
+
+    pr_debug("errinjct buffer ready: rtas_type=0x%x func=%d addr=0x%lx mask=0x%lx\n",
+         rtas_type, func, addr, mask);
+    return 0;

arch/powerpc/platforms/pseries/eeh_pseries.c +2 -0
+}
+

  /**
   * pseries_eeh_err_inject - Inject specified error to the indicated PE
   * @pe: the indicated PE

#10 lore raw

PATCH v4 4/5 powerpc/pseries/eeh: Implement RTAS-based EEH error injection

In reply to Narayana Murty N

Replace the legacy MMIO stub in pseries_eeh_err_inject() with a full PAPR-compliant RTAS error injection path using the existing RTAS work-area allocator.

The mutex is not a buffer lock; it serializes the firmware session open/inject/close sequence as required by PAPR. No global buffer is allocated or used.

VFIO EEH error injection exposes a generic userspace ABI. pSeries maps the generic EEH error types to RTAS ibm,errinjct encodings via pseries_eeh_type_to_rtas(). EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 are unchanged; their values are defined in arch/powerpc/include/uapi/asm/eeh.h and are not renumbered.

Tested with corresponding QEMU patches:
https://lore.kernel.org/all/[email protected]/

---
 arch/powerpc/platforms/pseries/eeh_pseries.c | 123 ++++++++++++++-----
 1 file changed, 95 insertions(+), 28 deletions(-)
arch/powerpc/platforms/pseries/eeh_pseries.c +95 -28
index fafe0004e738..fcb8c560d813 100644
@@ -25,6 +25,7 @@
 #include <linux/rbtree.h>
 #include <linux/sched.h>
 #include <linux/seq_file.h>
+#include <linux/mutex.h>
 #include <linux/spinlock.h>
 #include <linux/crash_dump.h>
 
@@ -34,6 +35,7 @@
 #include <asm/machdep.h>
 #include <asm/ppc-pci.h>
 #include <asm/rtas.h>
+#include <asm/rtas-work-area.h>
 
 /* RTAS tokens */
 static int ibm_set_eeh_option;
@@ -958,8 +960,6 @@ static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,
 			return -EINVAL;
 
 		if (upper_32_bits(addr) || upper_32_bits(mask)) {
-			pr_err("32-bit IOA injection cannot encode addr=%#lx mask=%#lx\n",
-			       addr, mask);
 			return -EINVAL;
 		}
 
@@ -992,50 +992,117 @@ static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,
 		break;
 
 	default:
-		pr_err("unsupported RTAS error injection type 0x%x\n", rtas_type);
+		pr_err("unsupported RTAS error injection type 0x%x\n",
+		       rtas_type);
 		return -EINVAL;
 	}
 
-	pr_debug("errinjct buffer ready: rtas_type=0x%x func=%d addr=0x%lx mask=0x%lx\n",
-		 rtas_type, func, addr, mask);
 	return 0;
 }
 
+/* pseries-local mutex serializes the open/inject/close RTAS session */
+static DEFINE_MUTEX(pseries_errinjct_mutex);
+
 /**
  * pseries_eeh_err_inject - Inject specified error to the indicated PE
  * @pe: the indicated PE
- * @type: error type
- * @func: specific error type
- * @addr: address
- * @mask: address mask
- * The routine is called to inject specified error, which is
- * determined by @type and @func, to the indicated PE
+ * @type: generic EEH error type (EEH_ERR_TYPE_32 or EEH_ERR_TYPE_64)
+ * @func: specific error function
+ * @addr: address argument (type-dependent, may be zero)
+ * @mask: address mask (type-dependent, may be zero)
+ *
+ * Implements PAPR-compliant error injection using:
+ *   ibm,open-errinjct -> ibm,errinjct -> ibm,close-errinjct
+ *
+ * A short-lived RTAS work area is allocated per call; no global buffer
+ * is used.  pseries_errinjct_mutex serializes the open/inject/close
+ * session sequence.
+ *
+ * Return: 0 on success, negative errno on failure.
  */
 static int pseries_eeh_err_inject(struct eeh_pe *pe, int type, int func,
 				  unsigned long addr, unsigned long mask)
 {
-	struct	eeh_dev	*pdev;
+	struct rtas_work_area *area;
+	phys_addr_t area_phys;
+	u32 buf_phys;
+	void *buf;
+	int open_token, errinjct_token, close_token;
+	int session_token;
+	int rtas_type;
+	int close_rc;
+	int rc;
+
+	rc = validate_errinjct_args(pe, type, func, addr, mask);
+	if (rc)
+		return rc;
 
-	/* Check on PCI error type */
-	if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
+	rtas_type = pseries_eeh_type_to_rtas(type);
+	if (rtas_type < 0)
 		return -EINVAL;
 
-	switch (func) {
-	case EEH_ERR_FUNC_LD_MEM_ADDR:
-	case EEH_ERR_FUNC_LD_MEM_DATA:
-	case EEH_ERR_FUNC_ST_MEM_ADDR:
-	case EEH_ERR_FUNC_ST_MEM_DATA:
-		/* injects a MMIO error for all pdev's belonging to PE */
-		pci_lock_rescan_remove();
-		list_for_each_entry(pdev, &pe->edevs, entry)
-			eeh_pe_inject_mmio_error(pdev->pdev);
-		pci_unlock_rescan_remove();
-		break;
-	default:
-		return -ERANGE;
+	open_token    = rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT);
+	errinjct_token = rtas_function_token(RTAS_FN_IBM_ERRINJCT);
+	close_token   = rtas_function_token(RTAS_FN_IBM_CLOSE_ERRINJCT);
+
+	if (open_token    == RTAS_UNKNOWN_SERVICE ||
+	    errinjct_token == RTAS_UNKNOWN_SERVICE ||
+	    close_token   == RTAS_UNKNOWN_SERVICE)
+		return -ENODEV;
+
+	area = rtas_work_area_alloc(RTAS_ERRINJCT_BUF_SIZE);
+	buf  = rtas_work_area_raw_buf(area);
+	area_phys = rtas_work_area_phys(area);
+
+	if (WARN_ON_ONCE(upper_32_bits(area_phys))) {
+		rc = -ERANGE;
+		goto out_free_area;
 	}
 
-	return 0;
+	buf_phys = lower_32_bits(area_phys);
+
+	rc = prepare_errinjct_buffer(buf, pe, rtas_type, func, addr, mask);
+	if (rc)
+		goto out_free_area;
+
+	mutex_lock(&pseries_errinjct_mutex);
+
+	do {
+		rc = rtas_call(open_token, 0, 2, &session_token);
+	} while (rtas_busy_delay(rc));
+
+	if (rc) {
+		pr_err("ibm,open-errinjct failed: status=%d\n", rc);
+		rc = rtas_error_rc(rc);
+		goto out_unlock;
+	}
+
+	do {
+		rc = rtas_call(errinjct_token, 3, 1, NULL,
+			       rtas_type, session_token, buf_phys);
+	} while (rtas_busy_delay(rc));
+
+	if (rc) {
+		pr_err("ibm,errinjct failed: status=%d\n", rc);
+		rc = rtas_error_rc(rc);
+	}
+
+	do {
+		close_rc = rtas_call(close_token, 1, 1, NULL, session_token);
+	} while (rtas_busy_delay(close_rc));
+
+	if (close_rc) {
+		pr_warn("ibm,close-errinjct failed: status=%d\n", close_rc);
+		if (!rc)
+			rc = rtas_error_rc(close_rc);
+	}
+
+out_unlock:
+	mutex_unlock(&pseries_errinjct_mutex);
+
+out_free_area:
+	rtas_work_area_free(area);
+	return rc;
 }
 
 static struct eeh_ops pseries_eeh_ops = {
Signature · 1 line
2.51.1
#11 lore raw

Re: [PATCH v4 4/5] powerpc/pseries/eeh: Implement RTAS-based EEH error injection

Quoted 5 lines · Narayana Murty N

Replace the legacy MMIO stub in pseries_eeh_err_inject() with a full PAPR-compliant RTAS error injection path using the existing RTAS work-area allocator.

The mutex is not a buffer lock;

Sorry but I didn't get this...

Quoted 3 lines

it serializes the firmware session
open/inject/close sequence as required by PAPR. No global buffer is allocated or used.

Nit:
I think the above para is influenced form old suggestion to not use global dedicated buffer. Lets drop the global buffer thing from commit message explain just the current approach.

Quoted 36 lines

VFIO EEH error injection exposes a generic userspace ABI. pSeries maps the generic EEH error types to RTAS ibm,errinjct encodings via pseries_eeh_type_to_rtas(). EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 are unchanged; their values are defined in arch/powerpc/include/uapi/asm/eeh.h and are not renumbered.

Tested with corresponding QEMU patches:
https://lore.kernel.org/all/[email protected]/

---
  arch/powerpc/platforms/pseries/eeh_pseries.c | 123 ++++++++++++++-----
  1 file changed, 95 insertions(+), 28 deletions(-)
arch/powerpc/platforms/pseries/eeh_pseries.c +2 -0
index fafe0004e738..fcb8c560d813 100644
@@ -25,6 +25,7 @@
  #include <linux/rbtree.h>
  #include <linux/sched.h>
  #include <linux/seq_file.h>
+#include <linux/mutex.h>
  #include <linux/spinlock.h>
  #include <linux/crash_dump.h>
 
@@ -34,6 +35,7 @@
  #include <asm/machdep.h>
  #include <asm/ppc-pci.h>
  #include <asm/rtas.h>
+#include <asm/rtas-work-area.h>
 
  /* RTAS tokens */
  static int ibm_set_eeh_option;

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 962

Quoted 5 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +0 -2
@@ -958,8 +960,6 @@ static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,
  			return -EINVAL;
 
  		if (upper_32_bits(addr) || upper_32_bits(mask)) {
-			pr_err("32-bit IOA injection cannot encode addr=%#lx mask=%#lx\n",
-			       addr, mask);

We are returning -EINVAL remove the error message, what is the need?

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 996

Quoted 10 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +2 -1
  			return -EINVAL;
  		}
 
@@ -992,50 +992,117 @@ static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,
  		break;
 
  	default:
-		pr_err("unsupported RTAS error injection type 0x%x\n", rtas_type);
+		pr_err("unsupported RTAS error injection type 0x%x\n",
+		       rtas_type);

Above change is not necessary..

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 1000

Quoted 5 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +0 -2
  		return -EINVAL;
  	}
 
-	pr_debug("errinjct buffer ready: rtas_type=0x%x func=%d addr=0x%lx mask=0x%lx\n",
-		 rtas_type, func, addr, mask);

What is need to remove this debug message?

Quoted 15 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +3 -6
  	return 0;
  }
 
+/* pseries-local mutex serializes the open/inject/close RTAS session */
+static DEFINE_MUTEX(pseries_errinjct_mutex);
+
  /**
   * pseries_eeh_err_inject - Inject specified error to the indicated PE
   * @pe: the indicated PE
- * @type: error type
- * @func: specific error type
- * @addr: address
- * @mask: address mask
- * The routine is called to inject specified error, which is
- * determined by @type and @func, to the indicated PE

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,

Quoted 10 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +10 -0
+ * @type: generic EEH error type (EEH_ERR_TYPE_32 or EEH_ERR_TYPE_64)
+ * @func: specific error function
+ * @addr: address argument (type-dependent, may be zero)
+ * @mask: address mask (type-dependent, may be zero)
+ *
+ * Implements PAPR-compliant error injection using:
+ *   ibm,open-errinjct -> ibm,errinjct -> ibm,close-errinjct
+ *
+ * A short-lived RTAS work area is allocated per call; no global buffer
+ * is used.

Mentioning "no global buffer is used" is not adding any value, I think.

Quoted 44 lines

pseries_errinjct_mutex serializes the open/inject/close
+ * session sequence.
+ *
+ * Return: 0 on success, negative errno on failure.
*/
static int pseries_eeh_err_inject(struct eeh_pe *pe, int type, int func,
unsigned long addr, unsigned long mask)
{

arch/powerpc/platforms/pseries/eeh_pseries.c +17 -16
-	struct	eeh_dev	*pdev;
+	struct rtas_work_area *area;
+	phys_addr_t area_phys;
+	u32 buf_phys;
+	void *buf;
+	int open_token, errinjct_token, close_token;
+	int session_token;
+	int rtas_type;
+	int close_rc;
+	int rc;
+
+	rc = validate_errinjct_args(pe, type, func, addr, mask);
+	if (rc)
+		return rc;
 
-	/* Check on PCI error type */
-	if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
+	rtas_type = pseries_eeh_type_to_rtas(type);
+	if (rtas_type < 0)
  		return -EINVAL;
 
-	switch (func) {
-	case EEH_ERR_FUNC_LD_MEM_ADDR:
-	case EEH_ERR_FUNC_LD_MEM_DATA:
-	case EEH_ERR_FUNC_ST_MEM_ADDR:
-	case EEH_ERR_FUNC_ST_MEM_DATA:
-		/* injects a MMIO error for all pdev's belonging to PE */
-		pci_lock_rescan_remove();
-		list_for_each_entry(pdev, &pe->edevs, entry)
-			eeh_pe_inject_mmio_error(pdev->pdev);
-		pci_unlock_rescan_remove();
-		break;
-	default:
-		return -ERANGE;
+	open_token    = rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT);
+	errinjct_token = rtas_function_token(RTAS_FN_IBM_ERRINJCT);

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 1055

Quoted 10 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +10 -0
+	close_token   = rtas_function_token(RTAS_FN_IBM_CLOSE_ERRINJCT);
+
+	if (open_token    == RTAS_UNKNOWN_SERVICE ||
+	    errinjct_token == RTAS_UNKNOWN_SERVICE ||
+	    close_token   == RTAS_UNKNOWN_SERVICE)
+		return -ENODEV;
+
+	area = rtas_work_area_alloc(RTAS_ERRINJCT_BUF_SIZE);
+	buf  = rtas_work_area_raw_buf(area);
+	area_phys = rtas_work_area_phys(area);

I was wondering if there is any need to hold the work area buffer until we acquire pseries_errinjct_mutex.

Would it make sense to allocate the buffer only after acquiring the mutex? I suggested an order for the open, errinjct, and close calls below, which I think could also help address the above comment.

Nit: this file is under pseries platform so pseries_errinjct_mutex can renamed to errinct_mutex.

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 1062

Quoted 8 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +5 -1
+
+	if (WARN_ON_ONCE(upper_32_bits(area_phys))) {
+		rc = -ERANGE;
+		goto out_free_area;
  	}
 
-	return 0;
+	buf_phys = lower_32_bits(area_phys);

I don't understand the above logic. First, we check area_phys and exit early if the address is above 4G, and then we take the lower 32 bits of the same address.

I think the RTAS work area allocation API should be responsible for allocating the work area buffer at the right location. The user shouldn't have to worry about where exactly the buffer is allocated unless they have a specific requirement.

Is the work area buffer allocated by the API not meeting your requirement? If so, could you please explain what the issue is? Let's see if we can fix it in the work area allocation API. Otherwise, I would suggest removing the checking and truncation done around area_phys.

Let me know your opinion.

Quoted 6 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +6 -0
+
+	rc = prepare_errinjct_buffer(buf, pe, rtas_type, func, addr, mask);
+	if (rc)
+		goto out_free_area;
+
+	mutex_lock(&pseries_errinjct_mutex);

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 1078

Quoted 10 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +10 -0
+
+	do {
+		rc = rtas_call(open_token, 0, 2, &session_token);
+	} while (rtas_busy_delay(rc));
+
+	if (rc) {
+		pr_err("ibm,open-errinjct failed: status=%d\n", rc);
+		rc = rtas_error_rc(rc);
+		goto out_unlock;
+	}

I think we should allocate the work area buffer only after the open-errinjct call succeeds. My preferred order is:

- Call RTAS open-errinjct
- Allocate the work area buffer
- Populate the work area buffer and make the errinjct RTAS call
- Release the work area buffer
- Call RTAS close-errinjct

This way, the work area buffer is held only for as long as it is needed, and we also avoid allocating it if open-errinjct fails.

Quoted 30 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +27 -0
+
+	do {
+		rc = rtas_call(errinjct_token, 3, 1, NULL,
+			       rtas_type, session_token, buf_phys);
+	} while (rtas_busy_delay(rc));
+
+	if (rc) {
+		pr_err("ibm,errinjct failed: status=%d\n", rc);
+		rc = rtas_error_rc(rc);
+	}
+
+	do {
+		close_rc = rtas_call(close_token, 1, 1, NULL, session_token);
+	} while (rtas_busy_delay(close_rc));
+
+	if (close_rc) {
+		pr_warn("ibm,close-errinjct failed: status=%d\n", close_rc);
+		if (!rc)
+			rc = rtas_error_rc(close_rc);
+	}
+
+out_unlock:
+	mutex_unlock(&pseries_errinjct_mutex);
+
+out_free_area:
+	rtas_work_area_free(area);
+	return rc;
  }
 
  static struct eeh_ops pseries_eeh_ops = {
#12 lore raw

Re: [PATCH v4 4/5] powerpc/pseries/eeh: Implement RTAS-based EEH error injection

Quoted 10 lines · Sourabh Jain
Quoted 5 lines · Narayana Murty N

Replace the legacy MMIO stub in pseries_eeh_err_inject() with a full PAPR-compliant RTAS error injection path using the existing RTAS work-area allocator.

The mutex is not a buffer lock;

Sorry but I didn't get this...

Quoted 8 lines
Quoted 3 lines

it serializes the firmware session
open/inject/close sequence as required by PAPR.  No global buffer is allocated or used.

Nit:
I think the above para is influenced form old suggestion to not use global dedicated buffer. Lets drop the global buffer thing from commit message explain just the current approach.

Agreed. I will clean up the commit message and describe only the current implementation. I will drop the "mutex is not a buffer lock" and "no global buffer is used" wording.

Quoted 34 lines
Quoted 34 lines

VFIO EEH error injection exposes a generic userspace ABI.  pSeries maps the generic EEH error types to RTAS ibm,errinjct encodings via pseries_eeh_type_to_rtas().  EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 are unchanged; their values are defined in arch/powerpc/include/uapi/asm/ eeh.h and are not renumbered.

Tested with corresponding QEMU patches:
https://lore.kernel.org/all/20251029150618.186803-1- [email protected]/

---
  arch/powerpc/platforms/pseries/eeh_pseries.c | 123 ++++++++++++++-----

  1 file changed, 95 insertions(+), 28 deletions(-)
arch/powerpc/platforms/pseries/eeh_pseries.c +2 -0
index fafe0004e738..fcb8c560d813 100644
@@ -25,6 +25,7 @@
   #include <linux/rbtree.h>
   #include <linux/sched.h>
   #include <linux/seq_file.h>
+#include <linux/mutex.h>
   #include <linux/spinlock.h>
   #include <linux/crash_dump.h>
@@ -34,6 +35,7 @@
   #include <asm/machdep.h>
   #include <asm/ppc-pci.h>
   #include <asm/rtas.h>
+#include <asm/rtas-work-area.h>
   /* RTAS tokens */
   static int ibm_set_eeh_option;
@@ -958,8 +960,6 @@ static int prepare_errinjct_buffer(void *buf, 
Quoted 9 lines

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 962

Quoted 6 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +0 -2
 struct eeh_pe *pe,
               return -EINVAL;
           if (upper_32_bits(addr) || upper_32_bits(mask)) {
-            pr_err("32-bit IOA injection cannot encode addr=%#lx 
 mask=%#lx\n",
-                   addr, mask);

We are returning -EINVAL remove the error message, what is the need?

Agreed. I will remove this unnecessary error message and just return -EINVAL.

Quoted 4 lines
Quoted 4 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +0 -0
               return -EINVAL;
           }
@@ -992,50 +992,117 @@ static int prepare_errinjct_buffer(void *buf, 
 struct eeh_pe *pe,
Quoted 9 lines
Quoted 6 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +0 -0
           break;

      default:
-        pr_err("unsupported RTAS error injection type 0x%x\n", rtas_type);
+        pr_err("unsupported RTAS error injection type 0x%x\n", +               rtas_type);

Above change is not necessary..

Agreed. I will drop this unrelated formatting-only change.

Quoted 7 lines
Quoted 5 lines

          return -EINVAL;
      }
-    pr_debug("errinjct buffer ready: rtas_type=0x%x func=%d addr=0x%lx mask=0x%lx\n",
-         rtas_type, func, addr, mask);

What is need to remove this debug message?

There is no strong reason to remove it. I will keep the existing debug message.

Quoted 18 lines
Quoted 18 lines

      return 0;
  }

arch/powerpc/platforms/pseries/eeh_pseries.c +3 -0
+/* pseries-local mutex serializes the open/inject/close RTAS session */
+static DEFINE_MUTEX(pseries_errinjct_mutex);
+

  /**
   * pseries_eeh_err_inject - Inject specified error to the indicated PE
   * @pe: the indicated PE
- * @type: error type
- * @func: specific error type
- * @addr: address
- * @mask: address mask
- * The routine is called to inject specified error, which is
- * determined by @type and @func, to the indicated PE
+ * @type: generic EEH error type (EEH_ERR_TYPE_32 or EEH_ERR_TYPE_64)
+ * @func: specific error function
+ * @addr: address argument (type-dependent, may be zero)

Quoted 9 lines
Quoted 7 lines

+ * @mask: address mask (type-dependent, may be zero)
+ *
+ * Implements PAPR-compliant error injection using:
+ *   ibm,open-errinjct -> ibm,errinjct -> ibm,close-errinjct
+ *
+ * A short-lived RTAS work area is allocated per call; no global buffer
+ * is used.

Mentioning "no global buffer is used" is not adding any value, I think.

Agreed.

Quoted 57 lines
Quoted 43 lines

pseries_errinjct_mutex serializes the open/inject/close
+ * session sequence.
+ *
+ * Return: 0 on success, negative errno on failure.
   */
  static int pseries_eeh_err_inject(struct eeh_pe *pe, int type, int func, unsigned long addr, unsigned long mask)
  {
-    struct    eeh_dev    *pdev;
+    struct rtas_work_area *area;
+    phys_addr_t area_phys;
+    u32 buf_phys;
+    void *buf;
+    int open_token, errinjct_token, close_token;
+    int session_token;
+    int rtas_type;
+    int close_rc;
+    int rc;
+
+    rc = validate_errinjct_args(pe, type, func, addr, mask); +    if (rc)
+        return rc;
-    /* Check on PCI error type */
-    if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
+    rtas_type = pseries_eeh_type_to_rtas(type);
+    if (rtas_type < 0)
          return -EINVAL;
-    switch (func) {
-    case EEH_ERR_FUNC_LD_MEM_ADDR:
-    case EEH_ERR_FUNC_LD_MEM_DATA:
-    case EEH_ERR_FUNC_ST_MEM_ADDR:
-    case EEH_ERR_FUNC_ST_MEM_DATA:
-        /* injects a MMIO error for all pdev's belonging to PE */ -        pci_lock_rescan_remove();
-        list_for_each_entry(pdev, &pe->edevs, entry)
-            eeh_pe_inject_mmio_error(pdev->pdev);
-        pci_unlock_rescan_remove();
-        break;
-    default:
-        return -ERANGE;
+    open_token    = rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT); +    errinjct_token = rtas_function_token(RTAS_FN_IBM_ERRINJCT); +    close_token   = rtas_function_token(RTAS_FN_IBM_CLOSE_ERRINJCT);

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 1055

Quoted 9 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +9 -0
+
+    if (open_token    == RTAS_UNKNOWN_SERVICE ||
+        errinjct_token == RTAS_UNKNOWN_SERVICE ||
+        close_token   == RTAS_UNKNOWN_SERVICE)
+        return -ENODEV;
+
+    area = rtas_work_area_alloc(RTAS_ERRINJCT_BUF_SIZE);
+    buf  = rtas_work_area_raw_buf(area);
+    area_phys = rtas_work_area_phys(area);

I was wondering if there is any need to hold the work area buffer until we acquire pseries_errinjct_mutex.

Quoted 4 lines

Would it make sense to allocate the buffer only after acquiring the mutex? I suggested an order for the open, errinjct, and close calls below, which I think could also help address the above comment.

Agreed. I will reorder the flow as suggested:

1. acquire the mutex
2. call ibm,open-errinjct
3. allocate the RTAS work area
4. populate the work buffer
5. call ibm,errinjct
6. free the RTAS work area
7. call ibm,close-errinjct
8. release the mutex

This avoids allocating the work area if ibm,open-errinjct fails and keeps the work area held only while it is needed.

Quoted 1 line

Nit: this file is under pseries platform so pseries_errinjct_mutex can renamed to errinct_mutex.

Agreed. I will rename it to errinjct_mutex.

Quoted 17 lines
Quoted 7 lines

+
+    if (WARN_ON_ONCE(upper_32_bits(area_phys))) { +        rc = -ERANGE;
+        goto out_free_area;
      }
-    return 0;
+    buf_phys = lower_32_bits(area_phys);

I don't understand the above logic. First, we check area_phys and exit early if the address is above 4G, and then we take the lower 32 bits of the same address.

I think the RTAS work area allocation API should be responsible for allocating the work area buffer at the right location. The user shouldn't have to worry about where exactly the buffer is allocated unless they have a specific requirement.

Quoted 8 lines

Is the work area buffer allocated by the API not meeting your requirement? If so, could you please explain what the issue is? Let's see if we can fix it in the work area allocation API. Otherwise, I would suggest removing the checking and truncation done around area_phys.

Let me know your opinion.

Agreed. Since this path uses the RTAS work area allocator, I will remove the explicit upper_32_bits(area_phys) check and the local address truncation logic. If the allocator does not provide an RTAS-suitable address, that should be handled in the allocator rather than in this EEH path.

Quoted 21 lines
Quoted 7 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +6 -0
+
+    rc = prepare_errinjct_buffer(buf, pe, rtas_type, func, addr, mask);
+    if (rc)
+        goto out_free_area;
+
+    mutex_lock(&pseries_errinjct_mutex);

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 1078

Quoted 10 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +10 -0
+
+    do {
+        rc = rtas_call(open_token, 0, 2, &session_token);
+    } while (rtas_busy_delay(rc));
+
+    if (rc) {
+        pr_err("ibm,open-errinjct failed: status=%d\n", rc);
+        rc = rtas_error_rc(rc);
+        goto out_unlock;
+    }

I think we should allocate the work area buffer only after the open-errinjct call succeeds. My preferred order is:

Quoted 6 lines

- Call RTAS open-errinjct
- Allocate the work area buffer
- Populate the work area buffer and make the errinjct RTAS call
- Release the work area buffer
- Call RTAS close-errinjct

As stated above, will address in next version.

Quoted 2 lines

This way, the work area buffer is held only for as long as it is needed, and we also avoid allocating it if open-errinjct fails.

Agreed.

Quoted 31 lines
Quoted 30 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +27 -0
+
+    do {
+        rc = rtas_call(errinjct_token, 3, 1, NULL,
+                   rtas_type, session_token, buf_phys);
+    } while (rtas_busy_delay(rc));
+
+    if (rc) {
+        pr_err("ibm,errinjct failed: status=%d\n", rc);
+        rc = rtas_error_rc(rc);
+    }
+
+    do {
+        close_rc = rtas_call(close_token, 1, 1, NULL, session_token);
+    } while (rtas_busy_delay(close_rc));
+
+    if (close_rc) {
+        pr_warn("ibm,close-errinjct failed: status=%d\n", close_rc);
+        if (!rc)
+            rc = rtas_error_rc(close_rc);
+    }
+
+out_unlock:
+    mutex_unlock(&pseries_errinjct_mutex);
+
+out_free_area:
+    rtas_work_area_free(area);
+    return rc;

  }
  static struct eeh_ops pseries_eeh_ops = {

#13 lore raw

Re: [PATCH v4 4/5] powerpc/pseries/eeh: Implement RTAS-based EEH error injection

In reply to Narayana Murty N
Quoted 115 lines · Narayana Murty N

Replace the legacy MMIO stub in pseries_eeh_err_inject() with a full PAPR-compliant RTAS error injection path using the existing RTAS work-area allocator.

The mutex is not a buffer lock; it serializes the firmware session open/inject/close sequence as required by PAPR. No global buffer is allocated or used.

VFIO EEH error injection exposes a generic userspace ABI. pSeries maps the generic EEH error types to RTAS ibm,errinjct encodings via pseries_eeh_type_to_rtas(). EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 are unchanged; their values are defined in arch/powerpc/include/uapi/asm/eeh.h and are not renumbered.

Tested with corresponding QEMU patches:
https://lore.kernel.org/all/[email protected]/

---
  arch/powerpc/platforms/pseries/eeh_pseries.c | 123 ++++++++++++++-----
  1 file changed, 95 insertions(+), 28 deletions(-)
arch/powerpc/platforms/pseries/eeh_pseries.c +35 -14
index fafe0004e738..fcb8c560d813 100644
@@ -25,6 +25,7 @@
  #include <linux/rbtree.h>
  #include <linux/sched.h>
  #include <linux/seq_file.h>
+#include <linux/mutex.h>
  #include <linux/spinlock.h>
  #include <linux/crash_dump.h>
 
@@ -34,6 +35,7 @@
  #include <asm/machdep.h>
  #include <asm/ppc-pci.h>
  #include <asm/rtas.h>
+#include <asm/rtas-work-area.h>
 
  /* RTAS tokens */
  static int ibm_set_eeh_option;
@@ -958,8 +960,6 @@ static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,
  			return -EINVAL;
 
  		if (upper_32_bits(addr) || upper_32_bits(mask)) {
-			pr_err("32-bit IOA injection cannot encode addr=%#lx mask=%#lx\n",
-			       addr, mask);
  			return -EINVAL;
  		}
 
@@ -992,50 +992,117 @@ static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe,
  		break;
 
  	default:
-		pr_err("unsupported RTAS error injection type 0x%x\n", rtas_type);
+		pr_err("unsupported RTAS error injection type 0x%x\n",
+		       rtas_type);
  		return -EINVAL;
  	}
 
-	pr_debug("errinjct buffer ready: rtas_type=0x%x func=%d addr=0x%lx mask=0x%lx\n",
-		 rtas_type, func, addr, mask);
  	return 0;
  }
 
+/* pseries-local mutex serializes the open/inject/close RTAS session */
+static DEFINE_MUTEX(pseries_errinjct_mutex);
+
  /**
   * pseries_eeh_err_inject - Inject specified error to the indicated PE
   * @pe: the indicated PE
- * @type: error type
- * @func: specific error type
- * @addr: address
- * @mask: address mask
- * The routine is called to inject specified error, which is
- * determined by @type and @func, to the indicated PE
+ * @type: generic EEH error type (EEH_ERR_TYPE_32 or EEH_ERR_TYPE_64)
+ * @func: specific error function
+ * @addr: address argument (type-dependent, may be zero)
+ * @mask: address mask (type-dependent, may be zero)
+ *
+ * Implements PAPR-compliant error injection using:
+ *   ibm,open-errinjct -> ibm,errinjct -> ibm,close-errinjct
+ *
+ * A short-lived RTAS work area is allocated per call; no global buffer
+ * is used.  pseries_errinjct_mutex serializes the open/inject/close
+ * session sequence.
+ *
+ * Return: 0 on success, negative errno on failure.
   */
  static int pseries_eeh_err_inject(struct eeh_pe *pe, int type, int func,
  				  unsigned long addr, unsigned long mask)
  {
-	struct	eeh_dev	*pdev;
+	struct rtas_work_area *area;
+	phys_addr_t area_phys;
+	u32 buf_phys;
+	void *buf;
+	int open_token, errinjct_token, close_token;
+	int session_token;
+	int rtas_type;
+	int close_rc;
+	int rc;
+
+	rc = validate_errinjct_args(pe, type, func, addr, mask);
+	if (rc)
+		return rc;
 
-	/* Check on PCI error type */
-	if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
+	rtas_type = pseries_eeh_type_to_rtas(type);
+	if (rtas_type < 0)
  		return -EINVAL;

arch/powerpc/platforms/pseries/eeh_pseries.c static int prepare_errinjct_buffer(void *buf, struct eeh_pe *pe, · line 1031

Quoted 10 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +0 -9
 
-	switch (func) {
-	case EEH_ERR_FUNC_LD_MEM_ADDR:
-	case EEH_ERR_FUNC_LD_MEM_DATA:
-	case EEH_ERR_FUNC_ST_MEM_ADDR:
-	case EEH_ERR_FUNC_ST_MEM_DATA:
-		/* injects a MMIO error for all pdev's belonging to PE */
-		pci_lock_rescan_remove();
-		list_for_each_entry(pdev, &pe->edevs, entry)
-			eeh_pe_inject_mmio_error(pdev->pdev);

Only caller of eeh_pe_inject_mmio_erro() is getting removed here. Should we remove the eeh_pe_inject_mmio_error() from the source?

Quoted 70 lines
arch/powerpc/platforms/pseries/eeh_pseries.c +60 -5
-		pci_unlock_rescan_remove();
-		break;
-	default:
-		return -ERANGE;
+	open_token    = rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT);
+	errinjct_token = rtas_function_token(RTAS_FN_IBM_ERRINJCT);
+	close_token   = rtas_function_token(RTAS_FN_IBM_CLOSE_ERRINJCT);
+
+	if (open_token    == RTAS_UNKNOWN_SERVICE ||
+	    errinjct_token == RTAS_UNKNOWN_SERVICE ||
+	    close_token   == RTAS_UNKNOWN_SERVICE)
+		return -ENODEV;
+
+	area = rtas_work_area_alloc(RTAS_ERRINJCT_BUF_SIZE);
+	buf  = rtas_work_area_raw_buf(area);
+	area_phys = rtas_work_area_phys(area);
+
+	if (WARN_ON_ONCE(upper_32_bits(area_phys))) {
+		rc = -ERANGE;
+		goto out_free_area;
  	}
 
-	return 0;
+	buf_phys = lower_32_bits(area_phys);
+
+	rc = prepare_errinjct_buffer(buf, pe, rtas_type, func, addr, mask);
+	if (rc)
+		goto out_free_area;
+
+	mutex_lock(&pseries_errinjct_mutex);
+
+	do {
+		rc = rtas_call(open_token, 0, 2, &session_token);
+	} while (rtas_busy_delay(rc));
+
+	if (rc) {
+		pr_err("ibm,open-errinjct failed: status=%d\n", rc);
+		rc = rtas_error_rc(rc);
+		goto out_unlock;
+	}
+
+	do {
+		rc = rtas_call(errinjct_token, 3, 1, NULL,
+			       rtas_type, session_token, buf_phys);
+	} while (rtas_busy_delay(rc));
+
+	if (rc) {
+		pr_err("ibm,errinjct failed: status=%d\n", rc);
+		rc = rtas_error_rc(rc);
+	}
+
+	do {
+		close_rc = rtas_call(close_token, 1, 1, NULL, session_token);
+	} while (rtas_busy_delay(close_rc));
+
+	if (close_rc) {
+		pr_warn("ibm,close-errinjct failed: status=%d\n", close_rc);
+		if (!rc)
+			rc = rtas_error_rc(close_rc);
+	}
+
+out_unlock:
+	mutex_unlock(&pseries_errinjct_mutex);
+
+out_free_area:
+	rtas_work_area_free(area);
+	return rc;
  }
 
  static struct eeh_ops pseries_eeh_ops = {
#14 lore raw

Re: [PATCH v4 4/5] powerpc/pseries/eeh: Implement RTAS-based EEH error injection

Hi Sourabh,

Quoted 118 lines · Sourabh Jain
Quoted 115 lines · Narayana Murty N

Replace the legacy MMIO stub in pseries_eeh_err_inject() with a full PAPR-compliant RTAS error injection path using the existing RTAS work-area allocator.

The mutex is not a buffer lock; it serializes the firmware session open/inject/close sequence as required by PAPR.  No global buffer is allocated or used.

VFIO EEH error injection exposes a generic userspace ABI.  pSeries maps the generic EEH error types to RTAS ibm,errinjct encodings via pseries_eeh_type_to_rtas().  EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 are unchanged; their values are defined in arch/powerpc/include/uapi/asm/ eeh.h and are not renumbered.

Tested with corresponding QEMU patches:
https://lore.kernel.org/all/20251029150618.186803-1- [email protected]/

---
  arch/powerpc/platforms/pseries/eeh_pseries.c | 123 ++++++++++++++-----

  1 file changed, 95 insertions(+), 28 deletions(-)
arch/powerpc/platforms/pseries/eeh_pseries.c +2 -2
index fafe0004e738..fcb8c560d813 100644
@@ -25,6 +25,7 @@
   #include <linux/rbtree.h>
   #include <linux/sched.h>
   #include <linux/seq_file.h>
+#include <linux/mutex.h>
   #include <linux/spinlock.h>
   #include <linux/crash_dump.h>
@@ -34,6 +35,7 @@
   #include <asm/machdep.h>
   #include <asm/ppc-pci.h>
   #include <asm/rtas.h>
+#include <asm/rtas-work-area.h>
   /* RTAS tokens */
   static int ibm_set_eeh_option;
@@ -958,8 +960,6 @@ static int prepare_errinjct_buffer(void *buf, 
 struct eeh_pe *pe,
               return -EINVAL;
           if (upper_32_bits(addr) || upper_32_bits(mask)) {
-            pr_err("32-bit IOA injection cannot encode addr=%#lx 
 mask=%#lx\n",
-                   addr, mask);
               return -EINVAL;
           }
@@ -992,50 +992,117 @@ static int prepare_errinjct_buffer(void *buf, 
 struct eeh_pe *pe,
           break;

      default:
-        pr_err("unsupported RTAS error injection type 0x%x\n", rtas_type);
+        pr_err("unsupported RTAS error injection type 0x%x\n", +               rtas_type);
          return -EINVAL;
      }
-    pr_debug("errinjct buffer ready: rtas_type=0x%x func=%d addr=0x%lx mask=0x%lx\n",
-         rtas_type, func, addr, mask);
      return 0;
  }

arch/powerpc/platforms/pseries/eeh_pseries.c +3 -0
+/* pseries-local mutex serializes the open/inject/close RTAS session */
+static DEFINE_MUTEX(pseries_errinjct_mutex);
+

  /**
   * pseries_eeh_err_inject - Inject specified error to the indicated PE
   * @pe: the indicated PE
- * @type: error type
- * @func: specific error type
- * @addr: address
- * @mask: address mask
- * The routine is called to inject specified error, which is
- * determined by @type and @func, to the indicated PE
+ * @type: generic EEH error type (EEH_ERR_TYPE_32 or EEH_ERR_TYPE_64)
+ * @func: specific error function
+ * @addr: address argument (type-dependent, may be zero)
+ * @mask: address mask (type-dependent, may be zero)
+ *
+ * Implements PAPR-compliant error injection using:
+ *   ibm,open-errinjct -> ibm,errinjct -> ibm,close-errinjct
+ *
+ * A short-lived RTAS work area is allocated per call; no global buffer
+ * is used.  pseries_errinjct_mutex serializes the open/inject/close
+ * session sequence.
+ *
+ * Return: 0 on success, negative errno on failure.
   */
  static int pseries_eeh_err_inject(struct eeh_pe *pe, int type, int func, unsigned long addr, unsigned long mask)
  {
-    struct    eeh_dev    *pdev;
+    struct rtas_work_area *area;
+    phys_addr_t area_phys;
+    u32 buf_phys;
+    void *buf;
+    int open_token, errinjct_token, close_token;
+    int session_token;
+    int rtas_type;
+    int close_rc;
+    int rc;
+
+    rc = validate_errinjct_args(pe, type, func, addr, mask); +    if (rc)
+        return rc;
-    /* Check on PCI error type */
-    if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
+    rtas_type = pseries_eeh_type_to_rtas(type);
+    if (rtas_type < 0)
          return -EINVAL;
-    switch (func) {
-    case EEH_ERR_FUNC_LD_MEM_ADDR:
-    case EEH_ERR_FUNC_LD_MEM_DATA:

Quoted 8 lines
Quoted 6 lines

-    case EEH_ERR_FUNC_ST_MEM_ADDR:
-    case EEH_ERR_FUNC_ST_MEM_DATA:
-        /* injects a MMIO error for all pdev's belonging to PE */ -        pci_lock_rescan_remove();
-        list_for_each_entry(pdev, &pe->edevs, entry)
-            eeh_pe_inject_mmio_error(pdev->pdev);

Only caller of eeh_pe_inject_mmio_erro() is getting removed here. Should we remove the eeh_pe_inject_mmio_error() from the source?

Yes, this removes the last caller of eeh_pe_inject_mmio_error().

I have removed eeh_pe_inject_mmio_error() and its declaration in v5.

Thanks,
Narayana

Quoted 69 lines
Quoted 68 lines

-        pci_unlock_rescan_remove();
-        break;
-    default:
-        return -ERANGE;
+    open_token    = rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT); +    errinjct_token = rtas_function_token(RTAS_FN_IBM_ERRINJCT); +    close_token   = rtas_function_token(RTAS_FN_IBM_CLOSE_ERRINJCT);

arch/powerpc/platforms/pseries/eeh_pseries.c +13 -0
+
+    if (open_token    == RTAS_UNKNOWN_SERVICE ||
+        errinjct_token == RTAS_UNKNOWN_SERVICE ||
+        close_token   == RTAS_UNKNOWN_SERVICE)
+        return -ENODEV;
+
+    area = rtas_work_area_alloc(RTAS_ERRINJCT_BUF_SIZE);
+    buf  = rtas_work_area_raw_buf(area);
+    area_phys = rtas_work_area_phys(area);
+
+    if (WARN_ON_ONCE(upper_32_bits(area_phys))) {
+        rc = -ERANGE;
+        goto out_free_area;

      }
-    return 0;
+    buf_phys = lower_32_bits(area_phys);

arch/powerpc/platforms/pseries/eeh_pseries.c +43 -0
+
+    rc = prepare_errinjct_buffer(buf, pe, rtas_type, func, addr, mask);
+    if (rc)
+        goto out_free_area;
+
+    mutex_lock(&pseries_errinjct_mutex);
+
+    do {
+        rc = rtas_call(open_token, 0, 2, &session_token);
+    } while (rtas_busy_delay(rc));
+
+    if (rc) {
+        pr_err("ibm,open-errinjct failed: status=%d\n", rc);
+        rc = rtas_error_rc(rc);
+        goto out_unlock;
+    }
+
+    do {
+        rc = rtas_call(errinjct_token, 3, 1, NULL,
+                   rtas_type, session_token, buf_phys);
+    } while (rtas_busy_delay(rc));
+
+    if (rc) {
+        pr_err("ibm,errinjct failed: status=%d\n", rc);
+        rc = rtas_error_rc(rc);
+    }
+
+    do {
+        close_rc = rtas_call(close_token, 1, 1, NULL, session_token);
+    } while (rtas_busy_delay(close_rc));
+
+    if (close_rc) {
+        pr_warn("ibm,close-errinjct failed: status=%d\n", close_rc);
+        if (!rc)
+            rc = rtas_error_rc(close_rc);
+    }
+
+out_unlock:
+    mutex_unlock(&pseries_errinjct_mutex);
+
+out_free_area:
+    rtas_work_area_free(area);
+    return rc;

  }
  static struct eeh_ops pseries_eeh_ops = {

#15 lore raw

PATCH v4 5/5 powerpc/powernv/eeh: Map VFIO EEH error injection to OPAL

In reply to Narayana Murty N

VFIO EEH error injection exposes a generic userspace ABI. pSeries maps the generic EEH error types to RTAS ibm,errinjct encodings, while PowerNV maps the same generic ABI values to OPAL-specific encodings.

Keep EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 unchanged. The mapping is internal to the platform backend.

Add a switch in pnv_eeh_err_inject() that translates:
EEH_ERR_TYPE_32 -> OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR EEH_ERR_TYPE_64 -> OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64

Return -EINVAL for unsupported error types rather than comparing directly against OPAL constants. This decouples the VFIO/EEH interface from OPAL implementation details, paralleling the approach used on the pSeries RTAS path.

---
 arch/powerpc/platforms/powernv/eeh-powernv.c | 27 +++++++++++++++-----
 1 file changed, 21 insertions(+), 6 deletions(-)
arch/powerpc/platforms/powernv/eeh-powernv.c +21 -6
index db3370d1673c..53c4c05aaef2 100644
@@ -1167,13 +1167,28 @@ static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
 {
 	struct pci_controller *hose = pe->phb;
 	struct pnv_phb *phb = hose->private_data;
+	int opal_type;
 	s64 rc;
 
-	if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
-	    type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
-		pr_warn("%s: Invalid error type %d\n",
-			__func__, type);
-		return -ERANGE;
+	/*
+	 * VFIO EEH error injection exposes a generic userspace ABI.
+	 * pSeries maps the generic EEH error types to RTAS ibm,errinjct
+	 * encodings, while PowerNV maps the same generic ABI values to
+	 * OPAL-specific encodings.
+	 *
+	 * Keep EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 unchanged.  The
+	 * mapping is internal to the platform backend.
+	 */
+	switch (type) {
+	case EEH_ERR_TYPE_32:
+		opal_type = OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR;
+		break;
+	case EEH_ERR_TYPE_64:
+		opal_type = OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64;
+		break;
+	default:
+		pr_warn("%s: Invalid error type %d\n", __func__, type);
+		return -EINVAL;
 	}
 
 	if (func < OPAL_ERR_INJECT_FUNC_IOA_LD_MEM_ADDR ||
@@ -1192,7 +1207,7 @@ static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
 
 	/* Do error injection */
 	rc = opal_pci_err_inject(phb->opal_id, pe->addr,
-				 type, func, addr, mask);
+				 opal_type, func, addr, mask);
 	if (rc != OPAL_SUCCESS) {
 		pr_warn("%s: Failure %lld injecting error "
 			"%d-%d to PHB#%x-PE#%x\n",
Signature · 1 line
2.51.1
#16 lore raw

Re: [PATCH v4 5/5] powerpc/powernv/eeh: Map VFIO EEH error injection to OPAL

Can you update the commit message to explain why and how this change is needed?

Quoted 26 lines · Narayana Murty N

VFIO EEH error injection exposes a generic userspace ABI. pSeries maps the generic EEH error types to RTAS ibm,errinjct encodings, while PowerNV maps the same generic ABI values to OPAL-specific encodings.

Keep EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 unchanged. The mapping is internal to the platform backend.

Add a switch in pnv_eeh_err_inject() that translates:

   EEH_ERR_TYPE_32 -> OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR
   EEH_ERR_TYPE_64 -> OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64

Return -EINVAL for unsupported error types rather than comparing directly against OPAL constants. This decouples the VFIO/EEH interface from OPAL implementation details, paralleling the approach used on the pSeries RTAS path.

---
  arch/powerpc/platforms/powernv/eeh-powernv.c | 27 +++++++++++++++-----
  1 file changed, 21 insertions(+), 6 deletions(-)
arch/powerpc/platforms/powernv/eeh-powernv.c +0 -0
index db3370d1673c..53c4c05aaef2 100644

arch/powerpc/platforms/powernv/eeh-powernv.c static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func, · line 1170

Quoted 4 lines
arch/powerpc/platforms/powernv/eeh-powernv.c +1 -0
@@ -1167,13 +1167,28 @@ static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
  {
  	struct pci_controller *hose = pe->phb;
  	struct pnv_phb *phb = hose->private_data;
+	int opal_type;

opal_type is confusing... How about opal_err_type or just err_type?

Quoted 38 lines
arch/powerpc/platforms/powernv/eeh-powernv.c +20 -6
  	s64 rc;
 
-	if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
-	    type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
-		pr_warn("%s: Invalid error type %d\n",
-			__func__, type);
-		return -ERANGE;
+	/*
+	 * VFIO EEH error injection exposes a generic userspace ABI.
+	 * pSeries maps the generic EEH error types to RTAS ibm,errinjct
+	 * encodings, while PowerNV maps the same generic ABI values to
+	 * OPAL-specific encodings.
+	 *
+	 * Keep EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 unchanged.  The
+	 * mapping is internal to the platform backend.
+	 */
+	switch (type) {
+	case EEH_ERR_TYPE_32:
+		opal_type = OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR;
+		break;
+	case EEH_ERR_TYPE_64:
+		opal_type = OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64;
+		break;
+	default:
+		pr_warn("%s: Invalid error type %d\n", __func__, type);
+		return -EINVAL;
  	}
 
  	if (func < OPAL_ERR_INJECT_FUNC_IOA_LD_MEM_ADDR ||
@@ -1192,7 +1207,7 @@ static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
 
  	/* Do error injection */
  	rc = opal_pci_err_inject(phb->opal_id, pe->addr,
-				 type, func, addr, mask);
+				 opal_type, func, addr, mask);
  	if (rc != OPAL_SUCCESS) {
  		pr_warn("%s: Failure %lld injecting error "
  			"%d-%d to PHB#%x-PE#%x\n",
#17 lore raw

Re: [PATCH v4 5/5] powerpc/powernv/eeh: Map VFIO EEH error injection to OPAL

Hi Sourabh,

Quoted 3 lines · Sourabh Jain

Can you update the commit message to explain why and how this change is needed?

Sure. I expanded the v5 commit message to explain that eeh_pe_inject_err() receives generic EEH userspace ABI values, while the PowerNV firmware interface uses OPAL-specific error types.

The backend now explicitly maps the generic EEH type to the corresponding OPAL error type rather than relying on the two sets of constants having identical numeric values.

Quoted 23 lines · Narayana Murty N
Quoted 21 lines

VFIO EEH error injection exposes a generic userspace ABI.  pSeries maps the generic EEH error types to RTAS ibm,errinjct encodings, while PowerNV maps the same generic ABI values to OPAL-specific encodings.

Keep EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 unchanged.  The mapping is internal to the platform backend.

Add a switch in pnv_eeh_err_inject() that translates:
   EEH_ERR_TYPE_32 -> OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR EEH_ERR_TYPE_64 -> OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64

Return -EINVAL for unsupported error types rather than comparing directly against OPAL constants.  This decouples the VFIO/EEH interface from OPAL implementation details, paralleling the approach used on the pSeries RTAS path.

---
  arch/powerpc/platforms/powernv/eeh-powernv.c | 27 +++++++++++++++-----

  1 file changed, 21 insertions(+), 6 deletions(-)
Quoted 13 lines

arch/powerpc/platforms/powernv/eeh-powernv.c static int pnv_eeh_err_inject(struct eeh_pe · line 1171

Quoted 10 lines
arch/powerpc/platforms/powernv/eeh-powernv.c +1 -0
index db3370d1673c..53c4c05aaef2 100644
@@ -1167,13 +1167,28 @@ static int pnv_eeh_err_inject(struct eeh_pe 
 *pe, int type, int func,
   {
       struct pci_controller *hose = pe->phb;
       struct pnv_phb *phb = hose->private_data;
+    int opal_type;

opal_type is confusing... How about opal_err_type or just err_type?

Agreed. Renamed it to opal_err_type in v5.

Thanks,
Narayana

Quoted 37 lines
Quoted 36 lines
arch/powerpc/platforms/powernv/eeh-powernv.c +19 -5
       s64 rc;
-    if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
-        type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
-        pr_warn("%s: Invalid error type %d\n",
-            __func__, type);
-        return -ERANGE;
+    /*
+     * VFIO EEH error injection exposes a generic userspace ABI.
+     * pSeries maps the generic EEH error types to RTAS ibm,errinjct
+     * encodings, while PowerNV maps the same generic ABI values to
+     * OPAL-specific encodings.
+     *
+     * Keep EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 unchanged.  The
+     * mapping is internal to the platform backend.
+     */
+    switch (type) {
+    case EEH_ERR_TYPE_32:
+        opal_type = OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR;
+        break;
+    case EEH_ERR_TYPE_64:
+        opal_type = OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64;
+        break;
+    default:
+        pr_warn("%s: Invalid error type %d\n", __func__, type);
+        return -EINVAL;
       }

      if (func < OPAL_ERR_INJECT_FUNC_IOA_LD_MEM_ADDR ||

arch/powerpc/platforms/powernv/eeh-powernv.c +1 -1
@@ -1192,7 +1207,7 @@ static int pnv_eeh_err_inject(struct eeh_pe *pe, 
 int type, int func,
       /* Do error injection */
       rc = opal_pci_err_inject(phb->opal_id, pe->addr,
-                 type, func, addr, mask);
+                 opal_type, func, addr, mask);
       if (rc != OPAL_SUCCESS) {

          pr_warn("%s: Failure %lld injecting error " "%d-%d to PHB#%x-PE#%x\n",

#18 lore raw
In reply to Narayana Murty N

Hi,

I noticed that I missed adding the changelog in the v4 cover letter. Adding it here for reference v4 changes from v3:

Quoted 69 lines · Narayana Murty N

The pSeries EEH error-injection backend currently implements a limited software-generated MMIO failure and does not use the error-injection services provided by RTAS.

This series replaces that implementation with the PAPR-defined RTAS workflow based on:

- ibm,open-errinjct
- ibm,errinjct
- ibm,close-errinjct

The implementation opens an RTAS error-injection session, prepares the firmware work buffer, performs the requested injection, and closes the session on both success and failure paths.

The existing EEH userspace ABI is preserved. EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 continue to represent generic 32-bit and 64-bit IOA bus-error injection requests. The pSeries backend maps these values to the corresponding RTAS error types, while the PowerNV backend explicitly maps them to the corresponding OPAL types.

Additional generic EEH error types (EEH_ERR_TYPE_RECOVERED_SPECIAL_EVENT, EEH_ERR_TYPE_CORRUPTED_PAGE, and the cache/TLB corruption types) are now defined in the UAPI header and mapped explicitly to RTAS firmware encodings by the pSeries backend. Platform backends that do not support a valid generic type return -EOPNOTSUPP.

No existing userspace ABI values are changed.

The current injection path can be exercised for VFIO-assigned devices through VFIO_EEH_PE_INJECT_ERR. The guest or userspace VFIO application continues to use the same generic EEH type and function values, independent of whether the host platform uses RTAS or OPAL.

The series also handles the unusual return format of ibm,open-errinjct:

rets[0] = error-injection session token
rets[1] = RTAS status

rtas_call() now returns rets[1] as the status and places the session token in outputs[0], preserving the normal kernel rtas_call() convention.

sys_rtas() is intentionally unchanged because it exposes the raw RTAS return cells to userspace. Userspace therefore continues to receive the session token and status in their PAPR-defined positions.

The RTAS work buffer is allocated during RTAS initialization below:

min(ppc64_rma_size, RTAS_INSTANTIATE_MAX)

using the same accessible-memory limit used for rtas_rmo_buf. The kernel populates the buffer through its virtual mapping but passes its physical address to firmware.

The complete open, inject and close sequence is serialized with a mutex. RTAS busy and extended-delay return values are handled for all three calls. A session token value of zero is accepted, and session state is tracked independently from the token value.

The patches are organised as follows:

Handle the special ibm,open-errinjct return format in rtas_call(). Allocate an RTAS-accessible error-injection work buffer. Add pSeries RTAS parameter validation and buffer encoding helpers. Implement RTAS-based pSeries EEH error injection. Explicitly map generic EEH error types to OPAL types on PowerNV.

Quoted 6 lines

Testing was performed on PowerVM with firmware providing the RTAS error-injection services and with the corresponding QEMU support:

https://lore.kernel.org/qemu-devel/[email protected]/

The corresponding QEMU v5 series is posted here:
https://lore.kernel.org/qemu-devel/[email protected]/

Change Log:

V3 -> V4:
* Use the RTAS work area allocator instead of adding a dedicated
global error-injection buffer.
* Keep sys_rtas() raw return-cell semantics unchanged while handling
the special ibm,open-errinjct return format in rtas_call().
* Treat ibm,open-errinjct with fewer than two return cells as invalid
rtas_call() usage instead of interpreting rets[0] as status.
* Use normal Linux errno values in internal validation helpers instead
of RTAS status values.
* Simplify the pSeries RTAS open/inject/close flow.
* Keep close-session failure as cleanup reporting without overwriting
the primary operation status.
* Add VFIO sPAPR-side address normalization before dispatching IOA
error injection to the platform EEH backend.
* Normalize IOA addresses based on the IOA error type, not only
selected function codes.
* Keep the generic EEH ABI values unchanged and keep pSeries RTAS and
PowerNV OPAL mappings explicit.

V2 -> V3:
* Fixed ibm,open-errinjct return handling to correctly process
firmware responses.
* Allocate the error-injection buffer in RTAS-accessible memory
instead of general kernel memory.
* Pass the physical address of the error-injection buffer to firmware
(previously incorrect address type).
* Accept session token zero as a valid token (previously rejected
erroneously).
* Handle RTAS busy and extended-delay return codes for open-inject,
and close calls.
* Simplified the validation helper — reduced complexity and removed
redundant checks.
* Simplified the buffer-preparation helper for cleaner, more
maintainable code.
* Validate that all required RTAS tokens are present before attempting
to open a session.
* Added explicit generic EEH-to-OPAL error-type mapping for the
PowerNV platform.

v1 -> v2: https://lore.kernel.org/all/[email protected]/
* Addressed all review comments from Sourabh Jain
- Removed unnecessary empty line in rtas_call()
- Enhanced comment to explain PAPR specification requirements
- Corrected misleading comment about output handling
- Improved else block comment for better code clarity
* Fixed kernel test robot warnings
- Fixed kernel-doc warning for __maybe_unused parameter
- Confirmed sparse warnings are false positives (correct endianness
handling)
* Added PowerNV platform abstraction layer (new Patch 5)
- Maps EEH error types to OPAL-specific types
- Simplifies type handling by direct variable update
* Improved code comments and documentation throughout
* Added Reported-by tags for kernel test robot findings
* Split into logical 5-patch series for better review

RFC -> v1: https://lore.kernel.org/all/[email protected]/
* Initial 4-patch series
* Fixed PAPR ibm,open-errinjct output format (token,status order)
* Added pr_fmt handling for EEH subsystem compatibility
* Implemented comprehensive validation helpers

RFC: https://lore.kernel.org/all/[email protected]/
* Initial RFC implementation

Thanks,
Narayana

Quoted 14 lines

Narayana Murty N (5):
powerpc/rtas: Handle ibm,open-errinjct return format vfio/spapr_tce: Normalize EEH IOA error injection addresses powerpc/pseries/eeh: Add RTAS error validation helpers powerpc/pseries/eeh: Implement RTAS-based EEH error injection powerpc/powernv/eeh: Map VFIO EEH error injection to OPAL

  arch/powerpc/kernel/rtas.c                   |  78 ++++-
  arch/powerpc/platforms/powernv/eeh-powernv.c |  27 +-
  arch/powerpc/platforms/pseries/eeh_pseries.c | 328 +++++++++++++++++--
  drivers/vfio/vfio_iommu_spapr_tce.c          |  90 +++++
  4 files changed, 484 insertions(+), 39 deletions(-)