Skip to contentSkip to conversation

Fix unsynchronized access to sev members through svm_register_enc_region

PATCH Applied Peter Gonda 4 participants 4 messages Jan 27, 2021 – Jan 28, 2021

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

PATCH v2 1/1 Fix unsynchronized access to sev members through svm_register_enc_region

Grab kvm->lock before pinning memory when registering an encrypted region; sev_pin_memory() relies on kvm->lock being held to ensure correctness when checking and updating the number of pinned pages.

Add a lockdep assertion to help prevent future regressions.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Brijesh Singh <[email protected]>
Cc Sean Christopherson <[email protected]>
Fixes 1e80fdc09d12 ("KVM: SVM: Pin guest memory when SEV is active")

V2
- Fix up patch description
- Correct file paths svm.c -> sev.c
- Add unlock of kvm->lock on sev_pin_memory error

V1
- https://lore.kernel.org/kvm/[email protected]/

---
 arch/x86/kvm/svm/sev.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
arch/x86/kvm/svm/sev.c +10 -7
index c8ffdbc81709..b80e9bf0a31b 100644
@@ -342,6 +342,8 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
 	unsigned long first, last;
 	int ret;
 
+	lockdep_assert_held(&kvm->lock);
+
 	if (ulen == 0 || uaddr + ulen < uaddr)
 		return ERR_PTR(-EINVAL);
 
@@ -1119,12 +1121,20 @@ int svm_register_enc_region(struct kvm *kvm,
 	if (!region)
 		return -ENOMEM;
 
+	mutex_lock(&kvm->lock);
 	region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
 	if (IS_ERR(region->pages)) {
 		ret = PTR_ERR(region->pages);
+		mutex_unlock(&kvm->lock);
 		goto e_free;
 	}
 
+	region->uaddr = range->addr;
+	region->size = range->size;
+
+	list_add_tail(&region->list, &sev->regions_list);
+	mutex_unlock(&kvm->lock);
+
 	/*
 	 * The guest may change the memory encryption attribute from C=0 -> C=1
 	 * or vice versa for this memory range. Lets make sure caches are
@@ -1133,13 +1143,6 @@ int svm_register_enc_region(struct kvm *kvm,
 	 */
 	sev_clflush_pages(region->pages, region->npages);
 
-	region->uaddr = range->addr;
-	region->size = range->size;
-
-	mutex_lock(&kvm->lock);
-	list_add_tail(&region->list, &sev->regions_list);
-	mutex_unlock(&kvm->lock);
-
 	return ret;
 
 e_free:
Signature · 1 line
2.30.0.280.ga3ce27912f-goog
#1 lore raw
Quoted 21 lines · Peter Gonda

Grab kvm->lock before pinning memory when registering an encrypted region; sev_pin_memory() relies on kvm->lock being held to ensure correctness when checking and updating the number of pinned pages.

Add a lockdep assertion to help prevent future regressions.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Brijesh Singh <[email protected]>
Cc Sean Christopherson <[email protected]>
Fixes 1e80fdc09d12 ("KVM: SVM: Pin guest memory when SEV is active")
Quoted 7 lines

V2
- Fix up patch description
- Correct file paths svm.c -> sev.c
- Add unlock of kvm->lock on sev_pin_memory error

V1
- https://lore.kernel.org/kvm/[email protected]/

Put version info, and anything else that shouldn't be in the final commit, below the three dashes. AFAIK that requires manually editing the patch file before sending it.

Quoted 2 lines

---

Version info goes here.

Quoted 38 lines
 arch/x86/kvm/svm/sev.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
arch/x86/kvm/svm/sev.c +10 -0
index c8ffdbc81709..b80e9bf0a31b 100644
@@ -342,6 +342,8 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
 	unsigned long first, last;
 	int ret;
 
+	lockdep_assert_held(&kvm->lock);
+
 	if (ulen == 0 || uaddr + ulen < uaddr)
 		return ERR_PTR(-EINVAL);
 
@@ -1119,12 +1121,20 @@ int svm_register_enc_region(struct kvm *kvm,
 	if (!region)
 		return -ENOMEM;
 
+	mutex_lock(&kvm->lock);
 	region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
 	if (IS_ERR(region->pages)) {
 		ret = PTR_ERR(region->pages);
+		mutex_unlock(&kvm->lock);
 		goto e_free;
 	}
 
+	region->uaddr = range->addr;
+	region->size = range->size;
+
+	list_add_tail(&region->list, &sev->regions_list);
+	mutex_unlock(&kvm->lock);
+
 	/*
 	 * The guest may change the memory encryption attribute from C=0 -> C=1
 	 * or vice versa for this memory range. Lets make sure caches are

arch/x86/kvm/svm/sev.c int svm_register_enc_region(struct kvm *kvm, · line 1144

Quoted 2 lines
arch/x86/kvm/svm/sev.c +0 -0
@@ -1133,13 +1143,6 @@ int svm_register_enc_region(struct kvm *kvm,
 	 */
 	sev_clflush_pages(region->pages, region->npages);

I don't think it actually matters, but it feels like the flush should be done before adding the region to the list. That would also make this sequence consistent with the other flows.

Tom, any thoughts?

Quoted 14 lines
arch/x86/kvm/svm/sev.c +0 -7
-	region->uaddr = range->addr;
-	region->size = range->size;
-
-	mutex_lock(&kvm->lock);
-	list_add_tail(&region->list, &sev->regions_list);
-	mutex_unlock(&kvm->lock);
-
 	return ret;
 
 e_free:
Signature · 1 line
2.30.0.280.ga3ce27912f-goog
#2 lore raw
Quoted 5 lines · Sean Christopherson
Quoted 4 lines · Peter Gonda

Grab kvm->lock before pinning memory when registering an encrypted region; sev_pin_memory() relies on kvm->lock being held to ensure correctness when checking and updating the number of pinned pages.

...

Quoted 15 lines

arch/x86/kvm/svm/sev.c int svm_register_enc_region(struct kvm *kvm, · line 1144

Quoted 10 lines
arch/x86/kvm/svm/sev.c +4 -0
+
+	list_add_tail(&region->list, &sev->regions_list);
+	mutex_unlock(&kvm->lock);
+
  	/*
  	 * The guest may change the memory encryption attribute from C=0 -> C=1
  	 * or vice versa for this memory range. Lets make sure caches are
@@ -1133,13 +1143,6 @@ int svm_register_enc_region(struct kvm *kvm,
  	 */
  	sev_clflush_pages(region->pages, region->npages);

I don't think it actually matters, but it feels like the flush should be done before adding the region to the list. That would also make this sequence consistent with the other flows.

Quoted 1 line

Tom, any thoughts?

I don't think it matters, either. This does keep the flushing outside of the mutex, so if you are doing parallel operations, that should help speed things up a bit.

Thanks,
Tom

Quoted 1 line
maintainer Applied
#3 lore raw
In reply to Peter Gonda
Quoted 72 lines · Peter Gonda

Grab kvm->lock before pinning memory when registering an encrypted region; sev_pin_memory() relies on kvm->lock being held to ensure correctness when checking and updating the number of pinned pages.

Add a lockdep assertion to help prevent future regressions.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Brijesh Singh <[email protected]>
Cc Sean Christopherson <[email protected]>
Fixes 1e80fdc09d12 ("KVM: SVM: Pin guest memory when SEV is active")

V2
- Fix up patch description
- Correct file paths svm.c -> sev.c
- Add unlock of kvm->lock on sev_pin_memory error

V1
- https://lore.kernel.org/kvm/[email protected]/

---
  arch/x86/kvm/svm/sev.c | 17 ++++++++++-------
  1 file changed, 10 insertions(+), 7 deletions(-)
arch/x86/kvm/svm/sev.c +10 -0
index c8ffdbc81709..b80e9bf0a31b 100644
@@ -342,6 +342,8 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
  	unsigned long first, last;
  	int ret;
 
+	lockdep_assert_held(&kvm->lock);
+
  	if (ulen == 0 || uaddr + ulen < uaddr)
  		return ERR_PTR(-EINVAL);
 
@@ -1119,12 +1121,20 @@ int svm_register_enc_region(struct kvm *kvm,
  	if (!region)
  		return -ENOMEM;
 
+	mutex_lock(&kvm->lock);
  	region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
  	if (IS_ERR(region->pages)) {
  		ret = PTR_ERR(region->pages);
+		mutex_unlock(&kvm->lock);
  		goto e_free;
  	}
 
+	region->uaddr = range->addr;
+	region->size = range->size;
+
+	list_add_tail(&region->list, &sev->regions_list);
+	mutex_unlock(&kvm->lock);
+
  	/*
  	 * The guest may change the memory encryption attribute from C=0 -> C=1
  	 * or vice versa for this memory range. Lets make sure caches are
@@ -1133,13 +1143,6 @@ int svm_register_enc_region(struct kvm *kvm,
  	 */
  	sev_clflush_pages(region->pages, region->npages);
 

arch/x86/kvm/svm/sev.c int svm_register_enc_region(struct kvm *kvm, · line 1148

Quoted 10 lines
arch/x86/kvm/svm/sev.c +0 -7
-	region->uaddr = range->addr;
-	region->size = range->size;
-
-	mutex_lock(&kvm->lock);
-	list_add_tail(&region->list, &sev->regions_list);
-	mutex_unlock(&kvm->lock);
-
  	return ret;
 
  e_free:

Queued, thanks.

Paolo