Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 0/2] KVM: PPC: Don't alias unclassified guest state IDs onto bit 0
@ 2026-12-02 18:16 Amit Machhiwal
  2026-12-02 18:16 ` [PATCH 1/2] KVM: PPC: Reject unknown guest state IDs in kvmppc_gse_parse() Amit Machhiwal
  2026-12-02 18:16 ` [PATCH 2/2] KVM: PPC: Don't map unclassified guest state IDs onto bit 0 Amit Machhiwal
  0 siblings, 2 replies; 4+ messages in thread
From: Amit Machhiwal @ 2026-12-02 18:16 UTC (permalink / raw)
  To: Madhavan Srinivasan, linuxppc-dev
  Cc: Amit Machhiwal, Gautam Menghani, Harsh Prateek Bora,
	Nicholas Piggin, Michael Ellerman, Christophe Leroy (CS GROUP),
	Ritesh Harjani (IBM), Shrikanth Hegde, Jordan Niethe, kvm, stable

The Guest State Buffer helpers map a guest state ID to a bitmap position with
kvmppc_gse_flatten_iden().  For an ID that does not belong to any guest state
class, it returns 0, which is also the valid position of KVMPPC_GSID_BLANK.
Such an ID therefore silently aliases bit 0, and because ID 0 is the terminator
of the kvmppc_gsbm_for_each() and kvmppc_gsp_for_each() iterators, a stray bit 0
makes them stop before visiting any other element.  Nothing reports an error.

I found this while debugging an L2 guest that failed to start under a nestedv2
L1 with trap = 0xffffffea (-EINVAL) and an all-zero vCPU state.  The cause was
a new guest state ID, defined by a change that is not in mainline, without a
matching class.  Setting the corresponding register marked bit 0 dirty in the
vCPU message bitmap, the message iterator then stopped at once, and nothing was
flushed to L0.  These patches make the helpers fail visibly instead of aliasing
bit 0 when an ID has no class, which is easy to hit when adding a new ID.

 - Patch 1 closes the only path by which an unclassified ID can arrive from L0:
   kvmppc_gse_parse() accepts a zero-length element with such an ID, because
   kvmppc_gsid_size() returns 0 for it.  It now rejects the element with
   -EINVAL.

 - Patch 2 fixes the root cause.  kvmppc_gse_flatten_iden() returns -1 instead
   of 0 for an unclassified ID, and its users handle that.  This protects
   callers that pass IDs chosen by the kernel itself, such as when marking state
   dirty, where a missing class used to silently corrupt the bitmap.  Those
   cases now warn once and do nothing.  The warnings can only come from a kernel
   bug, because patch 1 filters IDs from L0 before they reach these helpers.
   That is also why patch 2 must be applied after patch 1.

IDs defined in mainline today all have a class, so existing behaviour is
unchanged.  The series makes a missing class fail visibly instead of silently
aliasing bit 0.

Testing: with a nestedv2 L1 running under QEMU TCG as L0, the L2 guest fails
with trap = 0xffffffea without this series.  With it applied, the L2 guest boots
and the new WARN_ON_ONCE() reports the ID that has no class.

On a Power10 LPAR (PowerVM as L0), with the series applied on top of a mainline
kernel, L2 guests boot and reboot normally.  A kernel build run as a workload
inside the KVM guest completed with no problems seen.

Amit Machhiwal (2):
  KVM: PPC: Reject unknown guest state IDs in kvmppc_gse_parse()
  KVM: PPC: Don't map unclassified guest state IDs onto bit 0

 arch/powerpc/kvm/guest-state-buffer.c | 51 ++++++++++++++++++++++-----
 1 file changed, 43 insertions(+), 8 deletions(-)


base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] KVM: PPC: Reject unknown guest state IDs in kvmppc_gse_parse()
  2026-12-02 18:16 [PATCH 0/2] KVM: PPC: Don't alias unclassified guest state IDs onto bit 0 Amit Machhiwal
@ 2026-12-02 18:16 ` Amit Machhiwal
  2026-12-02 18:16 ` [PATCH 2/2] KVM: PPC: Don't map unclassified guest state IDs onto bit 0 Amit Machhiwal
  1 sibling, 0 replies; 4+ messages in thread
From: Amit Machhiwal @ 2026-12-02 18:16 UTC (permalink / raw)
  To: Madhavan Srinivasan, linuxppc-dev
  Cc: Amit Machhiwal, Gautam Menghani, Harsh Prateek Bora,
	Nicholas Piggin, Michael Ellerman, Christophe Leroy (CS GROUP),
	Ritesh Harjani (IBM), Shrikanth Hegde, Jordan Niethe, kvm, stable

kvmppc_gse_parse() validates each element by comparing its length
against kvmppc_gsid_size() for its ID.  For an ID that does not belong
to any guest state class, kvmppc_gsid_size() returns 0, so an element
with such an ID and a length of 0 passes the check.

kvmppc_gsp_insert() then calls kvmppc_gse_flatten_iden(), which returns
0 for an unclassified ID.  That is the bitmap position of
KVMPPC_GSID_BLANK, so the element is stored in gses[0] and bit 0 is set
in the parser's iterator bitmap.

kvmppc_gsp_for_each() uses ID 0 as its terminator, and bit 0 maps back
to ID 0 in kvmppc_gse_unflatten_iden().  Because bit 0 is the lowest
position, the iteration ends on its first step and none of the other
parsed elements are visited.  Callers such as
gs_msg_ops_vcpu_refresh_info() still return 0, so the vCPU state is left
stale without any error.

The buffer contents come from the L0 hypervisor, so L1 should reject an
element it cannot interpret instead of folding it onto an unrelated bit.
Every type in kvmppc_gse_iden_len[] has a non-zero size, so a size of 0
can only mean the ID has no defined type.  Treat that as invalid and
return -EINVAL, the same as a length mismatch.

Fixes: 6ccbbc33f06a ("KVM: PPC: Add helper library for Guest State Buffers")
Cc: stable@vger.kernel.org
Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
---
 arch/powerpc/kvm/guest-state-buffer.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/kvm/guest-state-buffer.c b/arch/powerpc/kvm/guest-state-buffer.c
index aeb586ba4523..21466bc4741e 100644
--- a/arch/powerpc/kvm/guest-state-buffer.c
+++ b/arch/powerpc/kvm/guest-state-buffer.c
@@ -304,10 +304,17 @@ int kvmppc_gse_parse(struct kvmppc_gs_parser *gsp, struct kvmppc_gs_buff *gsb)
 {
 	struct kvmppc_gs_elem *curr;
 	int rem, i;
+	u16 size;
 
 	kvmppc_gsb_for_each_elem(i, curr, gsb, rem) {
-		if (kvmppc_gse_len(curr) !=
-		    kvmppc_gsid_size(kvmppc_gse_iden(curr)))
+		size = kvmppc_gsid_size(kvmppc_gse_iden(curr));
+
+		/*
+		 * A size of 0 means the ID is unknown (or has no defined
+		 * type). Don't let a zero length element with such an ID
+		 * slip through the length check below.
+		 */
+		if (!size || kvmppc_gse_len(curr) != size)
 			return -EINVAL;
 		kvmppc_gsp_insert(gsp, kvmppc_gse_iden(curr), curr);
 	}
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] KVM: PPC: Don't map unclassified guest state IDs onto bit 0
  2026-12-02 18:16 [PATCH 0/2] KVM: PPC: Don't alias unclassified guest state IDs onto bit 0 Amit Machhiwal
  2026-12-02 18:16 ` [PATCH 1/2] KVM: PPC: Reject unknown guest state IDs in kvmppc_gse_parse() Amit Machhiwal
@ 2026-12-02 18:16 ` Amit Machhiwal
  2026-12-02 18:21   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Amit Machhiwal @ 2026-12-02 18:16 UTC (permalink / raw)
  To: Madhavan Srinivasan, linuxppc-dev
  Cc: Amit Machhiwal, Gautam Menghani, Harsh Prateek Bora,
	Nicholas Piggin, Michael Ellerman, Christophe Leroy (CS GROUP),
	Ritesh Harjani (IBM), Shrikanth Hegde, Jordan Niethe, kvm, stable

kvmppc_gse_flatten_iden() converts a guest state ID into a bitmap
position.  For an ID that does not belong to any guest state class it
returns 0, which is also the valid position of KVMPPC_GSID_BLANK.  A
caller passing such an ID therefore silently reads or modifies the
state of an unrelated element, and because ID 0 terminates the
kvmppc_gsbm_for_each() style iterators, a stray bit 0 also hides every
other element from them.

Return -1 for an unclassified ID instead and make the users of
kvmppc_gse_flatten_iden() cope with it:

 - kvmppc_gsp_insert(), kvmppc_gsbm_set() and kvmppc_gsbm_clear() warn
   once and do nothing.
 - kvmppc_gsbm_test() warns once and returns false.
 - kvmppc_gsbm_next() warns once and ends the iteration if the previous
   ID is unclassified.  The old "flatten(prev) + 1" form would have
   turned -1 back into position 0, so check the result first.
 - kvmppc_gsp_lookup() returns NULL, as for an element that is absent.

The guest state buffer is produced by the L0 hypervisor.  PowerVM is not
expected to send an element that L1 cannot interpret, but other L0
implementations exist, for example the QEMU TCG nestedv2 model, and L1
should not depend on that.  Elements received from L0 are already
rejected by kvmppc_gse_parse() if their ID is unclassified, so they
never reach these helpers.  The warnings can therefore only be triggered
by a kernel bug, such as adding a new guest state ID without a class,
and not by the contents of a guest state buffer.

Cc: stable@vger.kernel.org
Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
---
 arch/powerpc/kvm/guest-state-buffer.c | 40 +++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 6 deletions(-)

diff --git a/arch/powerpc/kvm/guest-state-buffer.c b/arch/powerpc/kvm/guest-state-buffer.c
index 21466bc4741e..40966a8b5989 100644
--- a/arch/powerpc/kvm/guest-state-buffer.c
+++ b/arch/powerpc/kvm/guest-state-buffer.c
@@ -325,12 +325,19 @@ int kvmppc_gse_parse(struct kvmppc_gs_parser *gsp, struct kvmppc_gs_buff *gsb)
 }
 EXPORT_SYMBOL_GPL(kvmppc_gse_parse);
 
+/*
+ * Returns the bitmap position of @iden, or -1 if @iden does not belong to any
+ * guest state class. Bit 0 is a valid position (KVMPPC_GSID_BLANK) so an
+ * unclassified ID must not be mapped onto it.
+ */
 static inline int kvmppc_gse_flatten_iden(u16 iden)
 {
 	int bit = 0;
 	int class;
 
 	class = kvmppc_gsid_class(iden);
+	if (class < 0)
+		return -1;
 
 	if (class == KVMPPC_GS_CLASS_GUESTWIDE) {
 		bit += iden - KVMPPC_GSE_GUESTWIDE_START;
@@ -379,7 +386,7 @@ static inline int kvmppc_gse_flatten_iden(u16 iden)
 		return bit;
 	}
 
-	return 0;
+	return -1;
 }
 
 static inline u16 kvmppc_gse_unflatten_iden(int bit)
@@ -442,6 +449,8 @@ void kvmppc_gsp_insert(struct kvmppc_gs_parser *gsp, u16 iden,
 	int i;
 
 	i = kvmppc_gse_flatten_iden(iden);
+	if (WARN_ON_ONCE(i < 0))
+		return;
 	kvmppc_gsbm_set(&gsp->iterator, iden);
 	gsp->gses[i] = gse;
 }
@@ -459,6 +468,8 @@ struct kvmppc_gs_elem *kvmppc_gsp_lookup(struct kvmppc_gs_parser *gsp, u16 iden)
 	int i;
 
 	i = kvmppc_gse_flatten_iden(iden);
+	if (i < 0)
+		return NULL;
 	return gsp->gses[i];
 }
 EXPORT_SYMBOL_GPL(kvmppc_gsp_lookup);
@@ -470,7 +481,11 @@ EXPORT_SYMBOL_GPL(kvmppc_gsp_lookup);
  */
 void kvmppc_gsbm_set(struct kvmppc_gs_bitmap *gsbm, u16 iden)
 {
-	set_bit(kvmppc_gse_flatten_iden(iden), gsbm->bitmap);
+	int bit = kvmppc_gse_flatten_iden(iden);
+
+	if (WARN_ON_ONCE(bit < 0))
+		return;
+	set_bit(bit, gsbm->bitmap);
 }
 EXPORT_SYMBOL_GPL(kvmppc_gsbm_set);
 
@@ -481,7 +496,11 @@ EXPORT_SYMBOL_GPL(kvmppc_gsbm_set);
  */
 void kvmppc_gsbm_clear(struct kvmppc_gs_bitmap *gsbm, u16 iden)
 {
-	clear_bit(kvmppc_gse_flatten_iden(iden), gsbm->bitmap);
+	int bit = kvmppc_gse_flatten_iden(iden);
+
+	if (WARN_ON_ONCE(bit < 0))
+		return;
+	clear_bit(bit, gsbm->bitmap);
 }
 EXPORT_SYMBOL_GPL(kvmppc_gsbm_clear);
 
@@ -492,7 +511,11 @@ EXPORT_SYMBOL_GPL(kvmppc_gsbm_clear);
  */
 bool kvmppc_gsbm_test(struct kvmppc_gs_bitmap *gsbm, u16 iden)
 {
-	return test_bit(kvmppc_gse_flatten_iden(iden), gsbm->bitmap);
+	int bit = kvmppc_gse_flatten_iden(iden);
+
+	if (WARN_ON_ONCE(bit < 0))
+		return false;
+	return test_bit(bit, gsbm->bitmap);
 }
 EXPORT_SYMBOL_GPL(kvmppc_gsbm_test);
 
@@ -503,9 +526,14 @@ EXPORT_SYMBOL_GPL(kvmppc_gsbm_test);
  */
 u16 kvmppc_gsbm_next(struct kvmppc_gs_bitmap *gsbm, u16 prev)
 {
-	int bit, pbit;
+	int bit, pbit = 0;
 
-	pbit = prev ? kvmppc_gse_flatten_iden(prev) + 1 : 0;
+	if (prev) {
+		pbit = kvmppc_gse_flatten_iden(prev);
+		if (WARN_ON_ONCE(pbit < 0))
+			return 0;
+		pbit++;
+	}
 	bit = find_next_bit(gsbm->bitmap, KVMPPC_GSE_IDEN_COUNT, pbit);
 
 	if (bit < KVMPPC_GSE_IDEN_COUNT)
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] KVM: PPC: Don't map unclassified guest state IDs onto bit 0
  2026-12-02 18:16 ` [PATCH 2/2] KVM: PPC: Don't map unclassified guest state IDs onto bit 0 Amit Machhiwal
@ 2026-12-02 18:21   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-02 18:21 UTC (permalink / raw)
  To: Amit Machhiwal; +Cc: kvm

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: PPC: missing Fixes trailer on kvmppc_gse_flatten_iden() bug
  fix
--

commit 3fb79be85605a3aaa7279630bfc52cabade894f3
Author: Amit Machhiwal <amachhiw@linux.ibm.com>

> KVM: PPC: Don't map unclassified guest state IDs onto bit 0
>
> kvmppc_gse_flatten_iden() converts a guest state ID into a bitmap
> position.  For an ID that does not belong to any guest state class it
> returns 0, which is also the valid position of KVMPPC_GSID_BLANK.  A
> caller passing such an ID therefore silently reads or modifies the
> state of an unrelated element, and because ID 0 terminates the
> kvmppc_gsbm_for_each() style iterators, a stray bit 0 also hides every
> other element from them.
>
> Return -1 for an unclassified ID instead and make the users of
> kvmppc_gse_flatten_iden() cope with it:
>
>  - kvmppc_gsp_insert(), kvmppc_gsbm_set() and kvmppc_gsbm_clear() warn
>    once and do nothing.
>  - kvmppc_gsbm_test() warns once and returns false.
>  - kvmppc_gsbm_next() warns once and ends the iteration if the previous
>    ID is unclassified.  The old "flatten(prev) + 1" form would have
>    turned -1 back into position 0, so check the result first.
>  - kvmppc_gsp_lookup() returns NULL, as for an element that is absent.
>
> The guest state buffer is produced by the L0 hypervisor.  PowerVM is not
> expected to send an element that L1 cannot interpret, but other L0
> implementations exist, for example the QEMU TCG nestedv2 model, and L1
> should not depend on that.  Elements received from L0 are already
> rejected by kvmppc_gse_parse() if their ID is unclassified, so they
> never reach these helpers.  The warnings can therefore only be triggered
> by a kernel bug, such as adding a new guest state ID without a class,
> and not by the contents of a guest state buffer.
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>

[Severity: Low]
This fixes a bug in kvmppc_gse_flatten_iden() and carries a stable tag.
Should this include:

  Fixes: 6ccbbc33f06a ("KVM: PPC: Add helper library for Guest State Buffers")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202181630.21203-1-amachhiw@linux.ibm.com?part=2

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-02 18:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:16 [PATCH 0/2] KVM: PPC: Don't alias unclassified guest state IDs onto bit 0 Amit Machhiwal
2026-12-02 18:16 ` [PATCH 1/2] KVM: PPC: Reject unknown guest state IDs in kvmppc_gse_parse() Amit Machhiwal
2026-12-02 18:16 ` [PATCH 2/2] KVM: PPC: Don't map unclassified guest state IDs onto bit 0 Amit Machhiwal
2026-12-02 18:21   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox