Re: [PATCH v3 03/24] dma-buf: Add the SGT DMA mapping type
From: Alistair Popple
Date: Thu Dec 03 2026 - 01:34:23 EST
On 2026-12-03 at 14:44 +1100, Dave Airlie <airlied@xxxxxxxxx> wrote...
> On Wed, 2 Dec 2026 at 13:54, Alistair Popple <apopple@xxxxxxxxxx> wrote:
> >
> > On 2026-12-02 at 12:41 +1100, Dave Airlie <airlied@xxxxxxxxx> wrote...
> > > On Wed, 2 Dec 2026 at 11:30, Jason Gunthorpe <jgg@xxxxxxxx> wrote:
> > > >
> > > > On Wed, Dec 02, 2026 at 10:47:28AM +1000, Dave Airlie wrote:
> > > >
> > > > > I think this is clearly where the divide is. I'm with Christian here,
> > > > > the exporter allocate the resource, then it owns the lifetime of the
> > > > > resource. In order for it to have complete ownership of the lifetime
> > > > > of the resource it needs the ability to remove the resource from other
> > > > > users (i.e. importers). If the exporter wasn't to free some pages, it
> > > > > should have all the information necessary to do so but traversing a
> > > > > list of places the object has been imported and a method to forcible
> > > > > unlink it from that side.
> > > >
> > > > If you change iommu mappings without telling the driver you will
> > > > create data corruption as new mappings can fall to places the device
> > > > is still doing DMA.
> > > >
> > > > The driver MUST be involved in any reclaim/move/revoke process and
> > > > DMABUF already has APIs for that. Managing this lifetime is one of the
> > > > main things it does.
> > > >
> > > > I don't like this notion we can trust one side more than the other, it
> > > > isn't like the exporter drivers are amazing clean things either. There
> > > > is a reasonable level of nannying..
> > >
> > > This isn't just about trust, it's about lifetime and ownership. If the
> > > exporter owns the resource, the exporter should have all necessary
> > > information to invalidate the resource and give it back to the system.
> >
> > It does - that is what the reclaim/move/revoke callbacks are for - to tell the
> > importer to stop using the resource and invalidate any mappings it holds to the
> > resource because it's about to give it back to the system.
> >
> > That necessarily has to live in the importer. Take for example PCIe P2P - there
> > might not be any IOMMU involved, so the only way to tear down the mapping is to
> > program some importing devices specific MMU (or stop work or whatever importer
> > specific thing needs doing).
> >
> > > > Importers are expected to unmap. I think we have to consider any one
> > > > that doesn't to be broken. You can't fix broken with more broken
> > > > things like force DMA unmapping and hoping that there is an iommu,
> > > > then hoping no corruption happens to crash the system.
> > >
> > > But we should allow the exporter to tell the imported to invalidate,
> > > wait for a response and then have the exporter free it. What the
> > > importer does in the invalidate is up to it, but the exporter controls
> > > the resource lifetime and any things bound to it. If the importer
> > > fails to invalidate the resource then that is a clear problem. But an
> > > exporter can at least control all the mappings for the resource and
> > > tear them down without leaking things.
> >
> > I think we may be conflating two things here - the importer contract and a
> > method of enforcing it.
>
> Actually I'd forgotten one more point, we are just talking about the
> closing down end of the bargain.
>
> We also don't want to trust importers at the setup phase, giving
> importers the physical addresses had led to problems in the past,
> hence we why make sure we remap the pages through their struct device
> before giving them the dma_addr_t pages back, it means the importer
> can't just go remapping the physical addresses and bypass any iommu
> etc.
I think there are two main points of contention we need to resolve:
1) What owns the life-time of a DMA mapping - importing device or exporting
device.
2) What data should be exposed to the importer.
Though if there is no iommu or it's PCIe P2P how can we avoid exposing the
physical addresses to the importer? I don't really think we should be designing
an API around a constraint that needs to be violated to make things work.
> In the past these have been buggy importers but they have happened,
> they weren't caught in review, who even is on the hook to review a
> dma-buf importer in another subsystem, like I get Jason isn't going to
> write a bad importer on purpose, but the number of groups that can
> write a good importer on purpose is small, it's more likely we get a
> bad importer by accident. The resource belongs to the exporter, bugs
> stop at it and anything we can do to make importers not open-code a
> phys->ANYTHING else is worth doing, because it's clearly been broken
> before so we are just opening up bugs that we spend years squishing.
>
> We don't worry about bad exporters because bad exporters will get the
> bug reports for bad exports since the pages were allocated by it.
Except they won't, if say they don't do the mapping correctly or teardown the
mapping unexpectedly. Then the importer gets the bug report, because that's the
thing that will crash the system or corrupt data if the exporter gets it wrong.
The problems are symmetrical, and I think what you're really saying is exporters
are more trusted than importers. But I agree with Jason - I don't think in a
monolithic kernel it makes much sense to say one type of module is more trusted
than another. I have seen plenty of bugs on both sides...
> In that sense I do get the "Switzerland" idea. But I still think we
> should lean into the exporter controlling what imports receive.
But then the exporters have to recieve a bunch of importer private context,
which Jason has NACK'ed for much the same reason. i.e. he doesn't want exporters
screwing with things they shouldn't.
My hope was we could define a helper that an importer would call to create
a mapping that would act as "Switzerland". That helper would take importer
specific context (eg. device, IOMMU domain, whatever) and call the exporter to
obtain the list of physical addresses which it maps.
This keeps both importer and exporter context isolated from each other.
> Like we have a flow of
> Exporter - advertises a shared-buf available as dma pages or iommufd or whatever
> Importer asks for the buf under one of those categories, hands the
> metadata to the exporter (could be completely opaque), exporter hands
> all the info to the swiss border patrol, it returns the necessary info
> for the importer and it gets sent back.
I do think that's possible but it seems a bit odd to me because all exporters
can advertise support for everything - dma pages or iommufd or PCIe P2P
(depending on location, which is separate discussion) - all they need to provide
to "Switzerland" is a physical location/address and "Switzerland" can figure out
what it's available as.
But I guess if the exporter absolutely must control the mapping without an
importer going via "Switzerland" itself then we could pass some opaque struct
into the exporter and create some API the exporter calls which knows how to
decode what type the opaque importer struct is and map the physical address
into it.
> I can see this working for the common cross-device ones, but I'm not
> really sure what to do about transport specific things like ualink or
> protected VRAM. Since who would write/control the swiss piece of the
> library in those cases?
That might come back to what Christian was suggesting earlier - separate those
cases out and for them dma-buf for ualink/nvlink/XGMI is really just a way to
hand some opaque data back to the importer.
- Alistair
> Dave.