Skip to contentSkip to conversation

KVM: SEV: drop FOLL_WRITE for encrypted region registration

PATCH Applied Pankaj Gupta 4 participants 7 messages Jul 15 – Jul 31

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

PATCH v2 1/1 KVM: SEV: drop FOLL_WRITE for encrypted region registration

Commit 7e066cb9b71a ("KVM: SEV: Use long-term pin when registering encrypted memory regions") added FOLL_LONGTERM to sev_mem_enc_register_region() so anonymous guest RAM is migrated out of MIGRATE_CMA/ZONE_MOVABLE before a long term pin. It also kept FOLL_WRITE on the pin.

Combining FOLL_WRITE with FOLL_LONGTERM breaks registration of file-backed guest memory, such as virtio-pmem host memory-backend-file mappings (MAP_SHARED). GUP rejects long-term writable pins on dirty tracked file mappings since:

commit 8ac268436e6d ("mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings") commit a6e79df92e4a ("mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings").

Region registration only requires long-term pin to prevent page migration and does not write through this GUP pin.

Drop FOLL_WRITE and pin guest memory only with FOLL_LONGTERM.

Fixes 7e066cb9b71a ("KVM: SEV: Use long-term pin when registering encrypted memory regions")

---
v1 -> v2
- Remove FOLL_WRITE when the pin is not used for host writes

v1: https://lore.kernel.org/all/[email protected]/

 arch/x86/kvm/svm/sev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +1 -1
index 427229347876..5f2998761462 100644
@@ -2752,7 +2752,7 @@ int sev_mem_enc_register_region(struct kvm *kvm,
 		return -ENOMEM;
 
 	region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages,
-				       FOLL_WRITE | FOLL_LONGTERM);
+				       FOLL_LONGTERM);
 	if (IS_ERR(region->pages)) {
 		ret = PTR_ERR(region->pages);
 		goto e_free;
Signature · 1 line
2.34.1
Acked-by on v2 1/1
#1 lore raw
Quoted 17 lines · Pankaj Gupta

Commit 7e066cb9b71a ("KVM: SEV: Use long-term pin when registering encrypted memory regions") added FOLL_LONGTERM to sev_mem_enc_register_region() so anonymous guest RAM is migrated out of MIGRATE_CMA/ZONE_MOVABLE before a long term pin. It also kept FOLL_WRITE on the pin.

Combining FOLL_WRITE with FOLL_LONGTERM breaks registration of file-backed guest memory, such as virtio-pmem host memory-backend-file mappings (MAP_SHARED). GUP rejects long-term writable pins on dirty tracked file mappings since:

commit 8ac268436e6d ("mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings") commit a6e79df92e4a ("mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings").

Region registration only requires long-term pin to prevent page migration and does not write through this GUP pin.

Quoted 7 lines

Drop FOLL_WRITE and pin guest memory only with FOLL_LONGTERM.

Fixes 7e066cb9b71a ("KVM: SEV: Use long-term pin when registering encrypted memory regions")

Nice and simple, works for me :) So:

Quoted 23 lines

---
v1 -> v2
- Remove FOLL_WRITE when the pin is not used for host writes

v1: https://lore.kernel.org/all/[email protected]/

 arch/x86/kvm/svm/sev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +1 -2
index 427229347876..5f2998761462 100644
@@ -2752,7 +2752,7 @@ int sev_mem_enc_register_region(struct kvm *kvm,
 		return -ENOMEM;
 
 	region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages,
-				       FOLL_WRITE | FOLL_LONGTERM);
+				       FOLL_LONGTERM);
 	if (IS_ERR(region->pages)) {
 		ret = PTR_ERR(region->pages);
 		goto e_free;
--
Quoted 2 lines

2.34.1

Cheers, Lorenzo

Acked-by on v2 1/1
#2 lore raw
In reply to Pankaj Gupta
Quoted 11 lines · Pankaj Gupta

Commit 7e066cb9b71a ("KVM: SEV: Use long-term pin when registering encrypted memory regions") added FOLL_LONGTERM to sev_mem_enc_register_region() so anonymous guest RAM is migrated out of MIGRATE_CMA/ZONE_MOVABLE before a long term pin. It also kept FOLL_WRITE on the pin.

Combining FOLL_WRITE with FOLL_LONGTERM breaks registration of file-backed guest memory, such as virtio-pmem host memory-backend-file mappings (MAP_SHARED). GUP rejects long-term writable pins on dirty tracked file mappings since:

Quoted 7 lines

commit 8ac268436e6d ("mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings") commit a6e79df92e4a ("mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings").

Region registration only requires long-term pin to prevent page migration and does not write through this GUP pin.

Drop FOLL_WRITE and pin guest memory only with FOLL_LONGTERM.

Worth mentioning here something like

"In the past, FOLL_WRITE was required to trigger CoW unsharing, making sure that we don't end up replacing the page in the page tables during a later write fault after already having pinned a (shared) page in MAP_PRIVATE mappings. FOLL_LONGTERM does that nowadays (see gup_must_unshare()) even without FOLL_WRITE.

Given that SEV only pins RAM for XYZ and doesn't actually write to the pinned pages, we can just drop the FOLL_WRITE"

Fill out XYZ :)

In general, LGTM

Signature · 3 lines
Cheers,

David
#3 lore raw
Quoted 25 lines
Quoted 11 lines

Commit 7e066cb9b71a ("KVM: SEV: Use long-term pin when registering encrypted memory regions") added FOLL_LONGTERM to sev_mem_enc_register_region() so anonymous guest RAM is migrated out of MIGRATE_CMA/ZONE_MOVABLE before a long term pin. It also kept FOLL_WRITE on the pin.

Combining FOLL_WRITE with FOLL_LONGTERM breaks registration of file-backed guest memory, such as virtio-pmem host memory-backend-file mappings (MAP_SHARED). GUP rejects long-term writable pins on dirty tracked file mappings since:

Quoted 7 lines

commit 8ac268436e6d ("mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings") commit a6e79df92e4a ("mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings").

Region registration only requires long-term pin to prevent page migration and does not write through this GUP pin.

Drop FOLL_WRITE and pin guest memory only with FOLL_LONGTERM.

Worth mentioning here something like

"In the past, FOLL_WRITE was required to trigger CoW unsharing, making sure that we don't end up replacing the page in the page tables during a later write fault after already having pinned a (shared) page in MAP_PRIVATE mappings. FOLL_LONGTERM does that nowadays (see gup_must_unshare()) even without FOLL_WRITE.

Quoted 4 lines

Given that SEV only pins RAM for XYZ and doesn't actually write to the pinned pages, we can just drop the FOLL_WRITE"

Fill out XYZ :)

Sure :)

I will update the commit message in v3.

Quoted 4 lines

In general, LGTM

thanks

Pankaj

#4 lore raw
Quoted 17 lines · Pankaj Gupta
Quoted 17 lines
Quoted 17 lines

Commit 7e066cb9b71a ("KVM: SEV: Use long-term pin when registering encrypted memory regions") added FOLL_LONGTERM to sev_mem_enc_register_region() so anonymous guest RAM is migrated out of MIGRATE_CMA/ZONE_MOVABLE before a long term pin. It also kept FOLL_WRITE on the pin.

Combining FOLL_WRITE with FOLL_LONGTERM breaks registration of file-backed guest memory, such as virtio-pmem host memory-backend-file mappings (MAP_SHARED). GUP rejects long-term writable pins on dirty tracked file mappings since:

commit 8ac268436e6d ("mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings") commit a6e79df92e4a ("mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings").

Region registration only requires long-term pin to prevent page migration and does not write through this GUP pin.

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

Drop FOLL_WRITE and pin guest memory only with FOLL_LONGTERM.

Worth mentioning here something like

"In the past, FOLL_WRITE was required to trigger CoW unsharing, making sure that we don't end up replacing the page in the page tables during a later write fault after already having pinned a (shared) page in MAP_PRIVATE mappings. FOLL_LONGTERM does that nowadays (see gup_must_unshare()) even without FOLL_WRITE.

Heh, this was going to be my exact question about why it was safe to drop FOLL_WRITE :-)

Quoted 8 lines
Quoted 4 lines

Given that SEV only pins RAM for XYZ and doesn't actually write to the pinned pages, we can just drop the FOLL_WRITE"

Fill out XYZ :)

Sure :)

I will update the commit message in v3.

No need for a v3, I'll add a blurb when applying.

#5 lore raw
Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

Commit 7e066cb9b71a ("KVM: SEV: Use long-term pin when registering encrypted memory regions") added FOLL_LONGTERM to sev_mem_enc_register_region() so anonymous guest RAM is migrated out of MIGRATE_CMA/ZONE_MOVABLE before a long term pin. It also kept FOLL_WRITE on the pin.

Combining FOLL_WRITE with FOLL_LONGTERM breaks registration of file-backed guest memory, such as virtio-pmem host memory-backend-file mappings (MAP_SHARED). GUP rejects long-term writable pins on dirty tracked file mappings since:

commit 8ac268436e6d ("mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings") commit a6e79df92e4a ("mm/gup: disallow FOLL_LONGTERM GUP-fast writing to file-backed mappings").

Region registration only requires long-term pin to prevent page migration and does not write through this GUP pin.

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

Drop FOLL_WRITE and pin guest memory only with FOLL_LONGTERM.

Worth mentioning here something like

"In the past, FOLL_WRITE was required to trigger CoW unsharing, making sure that we don't end up replacing the page in the page tables during a later write fault after already having pinned a (shared) page in MAP_PRIVATE mappings. FOLL_LONGTERM does that nowadays (see gup_must_unshare()) even without FOLL_WRITE.

Heh, this was going to be my exact question about why it was safe to drop FOLL_WRITE :-)

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

Given that SEV only pins RAM for XYZ and doesn't actually write to the pinned pages, we can just drop the FOLL_WRITE"

Fill out XYZ :)

Sure :)

I will update the commit message in v3.

No need for a v3, I'll add a blurb when applying.

Thanks, Sean!

Best regards,

Pankaj

maintainer Applied
#6 lore raw
In reply to Pankaj Gupta
Quoted 6 lines · Pankaj Gupta

Commit 7e066cb9b71a ("KVM: SEV: Use long-term pin when registering encrypted memory regions") added FOLL_LONGTERM to sev_mem_enc_register_region() so anonymous guest RAM is migrated out of MIGRATE_CMA/ZONE_MOVABLE before a long term pin. It also kept FOLL_WRITE on the pin.

Quoted 6 lines

Combining FOLL_WRITE with FOLL_LONGTERM breaks registration of file-backed guest memory, such as virtio-pmem host memory-backend-file mappings (MAP_SHARED). GUP rejects long-term writable pins on dirty tracked file mappings since:

[...]

Applied to kvm-x86 svm, thanks!

[1/1] KVM: SEV: drop FOLL_WRITE for encrypted region registration

      https://github.com/kvm-x86/linux/commit/ee1a586dd1fa

--
https://github.com/kvm-x86/linux/tree/next