From: hshoexer Subject: Re: SEV-SNP: bus_dma(9): Rescind and claim DMA bounce buffer To: tech@openbsd.org Date: Fri, 6 Nov 2026 10:41:18 +0100 On Fri, Oct 02, 2026 at 11:24:26PM -0700, Mike Larkin wrote: > On Thu, Sep 24, 2026 at 06:14:41PM +0200, hshoexer wrote: > > Hi, > > > > when creating DMA maps, rescind DMA bounce buffer pages to the > > hypervisor. When destroying DMA maps, claim these pages back from > > the hypervisor. > > > > ok? > > > > i don't understand why we are unconditionally doing snp-related stuff in the > shared busdma path here. I followed the trail back into that first diff and > don't see anywhere where we short-circuit if snp is not in use. > > these snp calls should have something like if (snp) { ... } around them IMO. The shortcut is in arch/amd64/amd64/snp.c (diff #1): ... void snp_claim_pages(struct pglist *mlist) { struct vm_page *pg; if (!ISSET(cpu_sev_guestmode, SEV_STAT_SNP_ACTIVE)) return; TAILQ_FOREACH(pg, mlist, pageq) { snp_claim(VM_PAGE_TO_PHYS(pg), VM_PAGE_TO_PHYS(pg) + PAGE_SIZE); } } void snp_rescind_pages(struct pglist *mlist) { struct vm_page *pg; if (!ISSET(cpu_sev_guestmode, SEV_STAT_SNP_ACTIVE)) return; TAILQ_FOREACH(pg, mlist, pageq) { snp_rescind(VM_PAGE_TO_PHYS(pg), VM_PAGE_TO_PHYS(pg) + PAGE_SIZE); } } ... I can move the ISSET() check up into bus_dma.c. Would be more explicit. Similar to int use_bounce_buffer: ... /* allocate and use bounce buffers when running as SEV guest */ int use_bounce_buffer = cpu_sev_guestmode || FORCE_BOUNCE_BUFFER; + int snp = ISSET(cpu_sev_guestmode, SEV_STAT_SNP_ACTIVE); struct uvm_constraint_range *constraint = &no_constraint; ... And then use "if (snp) { ... }". > also now snp is sneaking into the busdma path and causing some > contamination here... is there a better way to do this? I see your point. The SNP stuff is in the "bounce buffer portion" of _bus_dmamap_create() and _bus_dmamap_destroy(). I introduced the bounce buffer code for SEV (see PMAP_NOCRYPT); and it got more generic use for non-DMA reachable memory. So, the SNP calls are "on top" of the SEV use case. Using "if (snp)" and "#ifdef AMDSEV" in bus_dma.c might at least make this more epxlicit and less sneaky; but adds more pollution, though. I will re-work the diffs then it's easier to reason about it :-) > > Take care, > > HJ. > > --- > > sys/arch/amd64/amd64/bus_dma.c | 5 +++++ > > 1 file changed, 5 insertions(+) > > > > diff --git a/sys/arch/amd64/amd64/bus_dma.c b/sys/arch/amd64/amd64/bus_dma.c > > index f4efff95a18..2e69319739c 100644 > > --- a/sys/arch/amd64/amd64/bus_dma.c > > +++ b/sys/arch/amd64/amd64/bus_dma.c > > @@ -100,6 +100,7 @@ > > #endif > > > > #include > > +#include > > > > #include > > > > @@ -255,6 +256,8 @@ _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, > > return (ENOMEM); > > } > > > > + snp_rescind_pages(&mlist); > > + > > sva = va; > > ssize = sz; > > pgnext = TAILQ_FIRST(&mlist); > > @@ -269,6 +272,7 @@ _bus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, > > map->_dm_npages = 0; > > km_free((void *)sva, ssize, &kv_any, &kp_none); > > free(map, M_DEVBUF, mapsize); > > + snp_claim_pages(&mlist); > > uvm_pglistfree(&mlist); > > return (ENOMEM); > > } > > @@ -307,6 +311,7 @@ _bus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map) > > for (pg = map->_dm_pages; map->_dm_npages--; pg++) { > > TAILQ_INSERT_TAIL(&mlist, *pg, pageq); > > } > > + snp_claim_pages(&mlist); > > uvm_pglistfree(&mlist); > > } > > > > -- > > 2.47.3 > > >