Skip to contentSkip to conversation

KVM: s390: Fix lockdep issue in vm memop

PATCH In review Janis Schoetterl-Glausch 4 participants 8 messages Mar 22, 2022 – Mar 23, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: s390: Fix lockdep issue in vm memop

Issuing a memop on a protected vm does not make sense, neither is the memory readable/writable, nor does it make sense to check storage keys. This is why the ioctl will return -EINVAL when it detects the vm to be protected. However, in order to ensure that the vm cannot become protected during the memop, the kvm->lock would need to be taken for the duration of the ioctl. This is also required because kvm_s390_pv_is_protected asserts that the lock must be held. Instead, don't try to prevent this. If user space enables secure execution concurrently with a memop it must accecpt the possibility of the memop failing.
Still check if the vm is currently protected, but without locking and consider it a heuristic.

Fixes ef11c9463ae0 ("KVM: s390: Add vm IOCTL for key checked guest absolute memory access")
---
 arch/s390/kvm/kvm-s390.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +10 -1
index ca96f84db2cc..53adbe86a68f 100644
@@ -2385,7 +2385,16 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
 		return -EINVAL;
 	if (mop->size > MEM_OP_MAX_SIZE)
 		return -E2BIG;
-	if (kvm_s390_pv_is_protected(kvm))
+	/*
+	 * This is technically a heuristic only, if the kvm->lock is not
+	 * taken, it is not guaranteed that the vm is/remains non-protected.
+	 * This is ok from a kernel perspective, wrongdoing is detected
+	 * on the access, -EFAULT is returned and the vm may crash the
+	 * next time it accesses the memory in question.
+	 * There is no sane usecase to do switching and a memop on two
+	 * different CPUs at the same time.
+	 */
+	if (kvm_s390_pv_get_handle(kvm))
 		return -EINVAL;
 	if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
 		if (access_key_invalid(mop->key))

base-commit: c9b8fecddb5bb4b67e351bbaeaa648a6f7456912

Signature · 1 line
2.32.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · On Tue, 22 Mar 2022 16:32:04 +0100 Janis Schoetterl-Glausch…

Issuing a memop on a protected vm does not make sense, neither is the memory readable/writable, nor does it make sense to check storage keys. This is why the ioctl will return -EINVAL when it detects the vm to be protected. However, in order to ensure that the vm cannot become protected during the memop, the kvm->lock would need to be taken for the duration of the ioctl. This is also required because kvm_s390_pv_is_protected asserts that the lock must be held. Instead, don't try to prevent this. If user space enables secure execution concurrently with a memop it must accecpt the possibility of the memop failing.
Still check if the vm is currently protected, but without locking and consider it a heuristic.

Quoted 2 lines
Fixes ef11c9463ae0 ("KVM: s390: Add vm IOCTL for key checked guest absolute memory access")
Quoted 28 lines
---
 arch/s390/kvm/kvm-s390.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +10 -1
index ca96f84db2cc..53adbe86a68f 100644
@@ -2385,7 +2385,16 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
 		return -EINVAL;
 	if (mop->size > MEM_OP_MAX_SIZE)
 		return -E2BIG;
-	if (kvm_s390_pv_is_protected(kvm))
+	/*
+	 * This is technically a heuristic only, if the kvm->lock is not
+	 * taken, it is not guaranteed that the vm is/remains non-protected.
+	 * This is ok from a kernel perspective, wrongdoing is detected
+	 * on the access, -EFAULT is returned and the vm may crash the
+	 * next time it accesses the memory in question.
+	 * There is no sane usecase to do switching and a memop on two
+	 * different CPUs at the same time.
+	 */
+	if (kvm_s390_pv_get_handle(kvm))
 		return -EINVAL;
 	if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
 		if (access_key_invalid(mop->key))

base-commit: c9b8fecddb5bb4b67e351bbaeaa648a6f7456912

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Janis Schoetterl-Glausch
Quoted 1 line · Janis Schoetterl-Glausch

Issuing a memop on a protected vm does not make sense,

Issuing a vm memop on a protected vm...

The cpu memop still makes sense, no?

Quoted 12 lines

neither is the memory readable/writable, nor does it make sense to check storage keys. This is why the ioctl will return -EINVAL when it detects the vm to be protected. However, in order to ensure that the vm cannot become protected during the memop, the kvm->lock would need to be taken for the duration of the ioctl. This is also required because kvm_s390_pv_is_protected asserts that the lock must be held. Instead, don't try to prevent this. If user space enables secure execution concurrently with a memop it must accecpt the possibility of the memop failing.
Still check if the vm is currently protected, but without locking and consider it a heuristic.

Quoted 2 lines
Fixes ef11c9463ae0 ("KVM: s390: Add vm IOCTL for key checked guest absolute memory access")

Makes sense to me.

Quoted 28 lines
---
  arch/s390/kvm/kvm-s390.c | 11 ++++++++++-
  1 file changed, 10 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +10 -1
index ca96f84db2cc..53adbe86a68f 100644
@@ -2385,7 +2385,16 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
  		return -EINVAL;
  	if (mop->size > MEM_OP_MAX_SIZE)
  		return -E2BIG;
-	if (kvm_s390_pv_is_protected(kvm))
+	/*
+	 * This is technically a heuristic only, if the kvm->lock is not
+	 * taken, it is not guaranteed that the vm is/remains non-protected.
+	 * This is ok from a kernel perspective, wrongdoing is detected
+	 * on the access, -EFAULT is returned and the vm may crash the
+	 * next time it accesses the memory in question.
+	 * There is no sane usecase to do switching and a memop on two
+	 * different CPUs at the same time.
+	 */
+	if (kvm_s390_pv_get_handle(kvm))
  		return -EINVAL;
  	if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
  		if (access_key_invalid(mop->key))

base-commit: c9b8fecddb5bb4b67e351bbaeaa648a6f7456912

#3 lore raw
Quoted 6 lines · Janosch Frank
Quoted 1 line · Janis Schoetterl-Glausch

Issuing a memop on a protected vm does not make sense,

Issuing a vm memop on a protected vm...

The cpu memop still makes sense, no?

The vcpu memop does hold the vcpu->lock, so no lockdep issue. If you issue a vcpu memop while enabling protected virtualization, the memop might find that the vcpu is not protected, while other vcpus might already be, but I don't think there's a way to create secure memory concurrent with the memop.

Quoted 49 lines
Quoted 13 lines

neither is the memory readable/writable, nor does it make sense to check storage keys. This is why the ioctl will return -EINVAL when it detects the vm to be protected. However, in order to ensure that the vm cannot become protected during the memop, the kvm->lock would need to be taken for the duration of the ioctl. This is also required because kvm_s390_pv_is_protected asserts that the lock must be held. Instead, don't try to prevent this. If user space enables secure execution concurrently with a memop it must accecpt the possibility of the memop failing.
Still check if the vm is currently protected, but without locking and consider it a heuristic.

Quoted 2 lines
Fixes ef11c9463ae0 ("KVM: s390: Add vm IOCTL for key checked guest absolute memory access")

Makes sense to me.

Quoted 28 lines

---
  arch/s390/kvm/kvm-s390.c | 11 ++++++++++-

  1 file changed, 10 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +10 -1
index ca96f84db2cc..53adbe86a68f 100644
@@ -2385,7 +2385,16 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
           return -EINVAL;
       if (mop->size > MEM_OP_MAX_SIZE)
           return -E2BIG;
-    if (kvm_s390_pv_is_protected(kvm))
+    /*
+     * This is technically a heuristic only, if the kvm->lock is not
+     * taken, it is not guaranteed that the vm is/remains non-protected.
+     * This is ok from a kernel perspective, wrongdoing is detected
+     * on the access, -EFAULT is returned and the vm may crash the
+     * next time it accesses the memory in question.
+     * There is no sane usecase to do switching and a memop on two
+     * different CPUs at the same time.
+     */
+    if (kvm_s390_pv_get_handle(kvm))
           return -EINVAL;
       if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
           if (access_key_invalid(mop->key))

base-commit: c9b8fecddb5bb4b67e351bbaeaa648a6f7456912

maintainer
#4 lore raw
Quoted 6 lines · Janis Schoetterl-Glausch
Quoted 5 lines · Janosch Frank
Quoted 1 line · Janis Schoetterl-Glausch

Issuing a memop on a protected vm does not make sense,

Issuing a vm memop on a protected vm...

Quoted 7 lines
Quoted 1 line

The cpu memop still makes sense, no?

The vcpu memop does hold the vcpu->lock, so no lockdep issue. If you issue a vcpu memop while enabling protected virtualization, the memop might find that the vcpu is not protected, while other vcpus might already be, but I don't think there's a way to create secure memory concurrent with the memop.

I just wanted you to make this a bit more specific since we now have vm and vcpu memops. vm memops don't make sense for pv guests but vcpu ones are needed to access the sida.

Quoted 50 lines
Quoted 49 lines
Quoted 13 lines

neither is the memory readable/writable, nor does it make sense to check storage keys. This is why the ioctl will return -EINVAL when it detects the vm to be protected. However, in order to ensure that the vm cannot become protected during the memop, the kvm->lock would need to be taken for the duration of the ioctl. This is also required because kvm_s390_pv_is_protected asserts that the lock must be held. Instead, don't try to prevent this. If user space enables secure execution concurrently with a memop it must accecpt the possibility of the memop failing.
Still check if the vm is currently protected, but without locking and consider it a heuristic.

Quoted 2 lines
Fixes ef11c9463ae0 ("KVM: s390: Add vm IOCTL for key checked guest absolute memory access")

Makes sense to me.

Quoted 28 lines
---
   arch/s390/kvm/kvm-s390.c | 11 ++++++++++-
   1 file changed, 10 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +10 -1
index ca96f84db2cc..53adbe86a68f 100644
@@ -2385,7 +2385,16 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
           return -EINVAL;
       if (mop->size > MEM_OP_MAX_SIZE)
           return -E2BIG;
-    if (kvm_s390_pv_is_protected(kvm))
+    /*
+     * This is technically a heuristic only, if the kvm->lock is not
+     * taken, it is not guaranteed that the vm is/remains non-protected.
+     * This is ok from a kernel perspective, wrongdoing is detected
+     * on the access, -EFAULT is returned and the vm may crash the
+     * next time it accesses the memory in question.
+     * There is no sane usecase to do switching and a memop on two
+     * different CPUs at the same time.
+     */
+    if (kvm_s390_pv_get_handle(kvm))
           return -EINVAL;
       if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
           if (access_key_invalid(mop->key))

base-commit: c9b8fecddb5bb4b67e351bbaeaa648a6f7456912

#5 lore raw

Am 23.03.22 um 09:57 schrieb Janosch Frank:

Quoted 9 lines · Janis Schoetterl-Glausch
Quoted 8 lines · Janosch Frank
Quoted 6 lines · Janis Schoetterl-Glausch
Quoted 1 line

Issuing a memop on a protected vm does not make sense,

Issuing a vm memop on a protected vm...

The cpu memop still makes sense, no?

Quoted 7 lines
Quoted 5 lines

The vcpu memop does hold the vcpu->lock, so no lockdep issue. If you issue a vcpu memop while enabling protected virtualization, the memop might find that the vcpu is not protected, while other vcpus might already be, but I don't think there's a way to create secure memory concurrent with the memop.

I just wanted you to make this a bit more specific since we now have vm and vcpu memops. vm memops don't make sense for pv guests but vcpu ones are needed to access the sida.

Right, I think changing the commit messages
- Issuing a memop on a protected vm does not make sense
+ Issuing a vm memop on a protected vm does not make sense

does make sense.

Quoted 52 lines
Quoted 51 lines
Quoted 50 lines
Quoted 14 lines

neither is the memory readable/writable, nor does it make sense to check storage keys. This is why the ioctl will return -EINVAL when it detects the vm to be protected. However, in order to ensure that the vm cannot become protected during the memop, the kvm->lock would need to be taken for the duration of the ioctl. This is also required because kvm_s390_pv_is_protected asserts that the lock must be held. Instead, don't try to prevent this. If user space enables secure execution concurrently with a memop it must accecpt the possibility of the memop failing.
Still check if the vm is currently protected, but without locking and consider it a heuristic.

Quoted 2 lines
Fixes ef11c9463ae0 ("KVM: s390: Add vm IOCTL for key checked guest absolute memory access")

Makes sense to me.

Quoted 28 lines

---
   arch/s390/kvm/kvm-s390.c | 11 ++++++++++-

   1 file changed, 10 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +10 -1
index ca96f84db2cc..53adbe86a68f 100644
@@ -2385,7 +2385,16 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
            return -EINVAL;
        if (mop->size > MEM_OP_MAX_SIZE)
            return -E2BIG;
-    if (kvm_s390_pv_is_protected(kvm))
+    /*
+     * This is technically a heuristic only, if the kvm->lock is not
+     * taken, it is not guaranteed that the vm is/remains non-protected.
+     * This is ok from a kernel perspective, wrongdoing is detected
+     * on the access, -EFAULT is returned and the vm may crash the
+     * next time it accesses the memory in question.
+     * There is no sane usecase to do switching and a memop on two
+     * different CPUs at the same time.
+     */
+    if (kvm_s390_pv_get_handle(kvm))
            return -EINVAL;
        if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
            if (access_key_invalid(mop->key))

base-commit: c9b8fecddb5bb4b67e351bbaeaa648a6f7456912

#6 lore raw
Quoted 18 lines · Christian Borntraeger

Am 23.03.22 um 09:57 schrieb Janosch Frank:

Quoted 15 lines · Janis Schoetterl-Glausch
Quoted 13 lines · Janosch Frank
Quoted 6 lines · Janis Schoetterl-Glausch
Quoted 1 line

Issuing a memop on a protected vm does not make sense,

Issuing a vm memop on a protected vm...

The cpu memop still makes sense, no?

The vcpu memop does hold the vcpu->lock, so no lockdep issue. If you issue a vcpu memop while enabling protected virtualization, the memop might find that the vcpu is not protected, while other vcpus might already be, but I don't think there's a way to create secure memory concurrent with the memop.

Quoted 7 lines
Quoted 1 line

I just wanted you to make this a bit more specific since we now have vm and vcpu memops. vm memops don't make sense for pv guests but vcpu ones are needed to access the sida.

Right, I think changing the commit messages
- Issuing a memop on a protected vm does not make sense
+ Issuing a vm memop on a protected vm does not make sense

does make sense.

Ok, want me to send a v2?

Quoted 53 lines
Quoted 53 lines
Quoted 52 lines
Quoted 51 lines
Quoted 15 lines

neither is the memory readable/writable, nor does it make sense to check storage keys. This is why the ioctl will return -EINVAL when it detects the vm to be protected. However, in order to ensure that the vm cannot become protected during the memop, the kvm->lock would need to be taken for the duration of the ioctl. This is also required because kvm_s390_pv_is_protected asserts that the lock must be held. Instead, don't try to prevent this. If user space enables secure execution concurrently with a memop it must accecpt the possibility of the memop failing.
Still check if the vm is currently protected, but without locking and consider it a heuristic.

Quoted 2 lines
Fixes ef11c9463ae0 ("KVM: s390: Add vm IOCTL for key checked guest absolute memory access")

Makes sense to me.

Quoted 28 lines

---
   arch/s390/kvm/kvm-s390.c | 11 ++++++++++-

   1 file changed, 10 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +10 -1
index ca96f84db2cc..53adbe86a68f 100644
@@ -2385,7 +2385,16 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
            return -EINVAL;
        if (mop->size > MEM_OP_MAX_SIZE)
            return -E2BIG;
-    if (kvm_s390_pv_is_protected(kvm))
+    /*
+     * This is technically a heuristic only, if the kvm->lock is not
+     * taken, it is not guaranteed that the vm is/remains non-protected.
+     * This is ok from a kernel perspective, wrongdoing is detected
+     * on the access, -EFAULT is returned and the vm may crash the
+     * next time it accesses the memory in question.
+     * There is no sane usecase to do switching and a memop on two
+     * different CPUs at the same time.
+     */
+    if (kvm_s390_pv_get_handle(kvm))
            return -EINVAL;
        if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
            if (access_key_invalid(mop->key))

base-commit: c9b8fecddb5bb4b67e351bbaeaa648a6f7456912

#7 lore raw

Am 23.03.22 um 10:39 schrieb Janis Schoetterl-Glausch:

Quoted 21 lines · Christian Borntraeger
Quoted 20 lines

Am 23.03.22 um 09:57 schrieb Janosch Frank:

Quoted 16 lines · Janis Schoetterl-Glausch
Quoted 6 lines · Janosch Frank
Quoted 5 lines · Janis Schoetterl-Glausch
Quoted 1 line

Issuing a memop on a protected vm does not make sense,

Issuing a vm memop on a protected vm...

Quoted 7 lines
Quoted 1 line

The cpu memop still makes sense, no?

The vcpu memop does hold the vcpu->lock, so no lockdep issue. If you issue a vcpu memop while enabling protected virtualization, the memop might find that the vcpu is not protected, while other vcpus might already be, but I don't think there's a way to create secure memory concurrent with the memop.

I just wanted you to make this a bit more specific since we now have vm and vcpu memops. vm memops don't make sense for pv guests but vcpu ones are needed to access the sida.

Quoted 7 lines
Quoted 5 lines

Right, I think changing the commit messages
- Issuing a memop on a protected vm does not make sense
+ Issuing a vm memop on a protected vm does not make sense

does make sense.

Ok, want me to send a v2?

I can fixup when applying. Done and queued for kvm.

Quoted 54 lines
Quoted 53 lines
Quoted 53 lines
Quoted 52 lines
Quoted 51 lines
Quoted 15 lines

neither is the memory readable/writable, nor does it make sense to check storage keys. This is why the ioctl will return -EINVAL when it detects the vm to be protected. However, in order to ensure that the vm cannot become protected during the memop, the kvm->lock would need to be taken for the duration of the ioctl. This is also required because kvm_s390_pv_is_protected asserts that the lock must be held. Instead, don't try to prevent this. If user space enables secure execution concurrently with a memop it must accecpt the possibility of the memop failing.
Still check if the vm is currently protected, but without locking and consider it a heuristic.

Quoted 2 lines
Fixes ef11c9463ae0 ("KVM: s390: Add vm IOCTL for key checked guest absolute memory access")

Makes sense to me.

Quoted 28 lines
---
    arch/s390/kvm/kvm-s390.c | 11 ++++++++++-
    1 file changed, 10 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +10 -1
index ca96f84db2cc..53adbe86a68f 100644
@@ -2385,7 +2385,16 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
            return -EINVAL;
        if (mop->size > MEM_OP_MAX_SIZE)
            return -E2BIG;
-    if (kvm_s390_pv_is_protected(kvm))
+    /*
+     * This is technically a heuristic only, if the kvm->lock is not
+     * taken, it is not guaranteed that the vm is/remains non-protected.
+     * This is ok from a kernel perspective, wrongdoing is detected
+     * on the access, -EFAULT is returned and the vm may crash the
+     * next time it accesses the memory in question.
+     * There is no sane usecase to do switching and a memop on two
+     * different CPUs at the same time.
+     */
+    if (kvm_s390_pv_get_handle(kvm))
            return -EINVAL;
        if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
            if (access_key_invalid(mop->key))

base-commit: c9b8fecddb5bb4b67e351bbaeaa648a6f7456912