On 2026-11-16 at 23:57 +1100, Christian König <[email protected]>
wrote...
> On 11/16/26 07:27, Alistair Popple wrote:
> > On 2026-11-09 at 21:59 +1100, Christian König <[email protected]>
> > wrote...
> >> On 11/9/26 03:58, Alistair Popple wrote:
> >>> On 2026-11-07 at 19:57 +1100, Karl Mehltretter <[email protected]>
> >>> wrote...
> >>>> On Thu, Nov 05, 2026 at 05:40:41PM +0100, Alistair Popple wrote:
> >>>>> + * A pin or map that fails after dma_buf_mapping_select() may reopen
> >>>>> the
> >>>>> + * choice only if it was the one that made it. A selection that
> >>>>> predates the
> >>>>> + * call belongs to a live pin or mapping and must survive, otherwise
> >>>>> the next
> >>>>> + * pin or map would reselect under it without an invalidate.
> >>>>> + */
> >>>>> +void dma_buf_mapping_unselect(struct dma_buf_attachment *attach,
> >>>>> + bool was_selected)
> >>>>> +{
> >>>>> + dma_resv_assert_held(attach->dmabuf->resv);
> >>>>> +
> >>>>> + if (was_selected)
> >>>>> + return;
> >>>>> +
> >>>>> + attach->selected = false;
> >>>>> + attach->selected_idx = 0;
> >>>>> +}
> >>>>> +
> >>>>
> >>>> After dma_buf_invalidate_mappings(), attach->selected is false while
> >>>> attach->selected_idx still holds that mapping's entry for a pending
> >>>> unmap.
> >>>>
> >>>> If a pin/map fails before that unmap runs, was_selected is false and
> >>>> dma_buf_mapping_unselect() resets attach->selected_idx to 0.
> >>>>
> >>>> A subsequent dma_buf_unmap_sgt() then uses matches[0].
> >>>
> >>> Actually there's a bigger issue here. The contract at the moment states
> >>> that
> >>> mapping type may change after invalidation. However that doesn't account
> >>> for the
> >>> fact mappings can outlive the invalidation.
> >>>
> >>> This could be dealt with by switching everything over to the generic
> >>> dma_buf_mapping_map/unmap() path, but that requires an allocation per
> >>> mapping
> >>> just to track the mapping type.
> >>
> >> You need an allocation per mapping anyway to identify the mapping anyway.
> >>
> >> We currently abuse the sg_table for that, but essentially the idea of
> >> having a structure/object to represent the mapping needs to stay around as
> >> far as I can see.
> >
> > Oh right, I see that now.
> >
> > Do you think it makes sense to convert all exporters to return a mapping
> > type
> > specific struct (eg. dma_buf_sgt_mapping) rather than the sgt directly?
>
> No, we essentially want to get away from the whole sgt approach.
Right, I meant as an interim step. Basically a "no behaviour change" patch
series that changes exporters to return some map-type specific data structure
from their map_dma_buf() callbacks rather than an sgt directly.
Initially that is a dma_buf_sgt_mapping embedding the sgt as I don't think it's
tractable to convert all importers in one go. Then we add map types for each
address space and start converting importers to use those instead of sgt.
My rough plan to get there is an almost no behavioural change patch series
introducing the map-types and the selection contract/logic. Essentially that's a
reposting of this that converts exporters to allocate and return a type specific
data struct, minus the p2p bits.
Then follow that up with a couple of series building on top - one that makes
PCIe P2P it's own-map (as I think Thomas suggested), one implementing the
IOMMUFD bits that tries to address concerns there and another that shows how we
can start converting importers to no longer get an SGT but instead some other
type (eg. a list of DMA addresses as suggested).
Let me know if you have any thoughts though, or I can just start posting the
code and let people take a look.
> The whole purpose of DMA-buf was to exchange DMA addresses between drivers
> and sgt is problematic for that job because it mixed both the input as well
> as the output of the DMA mapping operation into one structure.
Yep, so broadly I'm hoping I can try to keep the exporter specific stuff in
the attachment map type, and the mapping struct can hold only what the importer
needs/gets to consume (and nothing more).
- Alistair
> What we need is the functionality to either say map the buffer at this
> specific address in that address space (IOMMU domain, internal address space
> whatever) or just to return DMA addresses plus the information in which
> address space those addresses are valid.
>
> Regards,
> Christian.
>
> > Ie. change map_dma_buf() callbacks to return *dma_buf_sgt_mapping instead of
> > *sg_table and convert exporters to allocate that instead of a *sg_table.
> >
> > I've taken a look at doing that and whilst it touches all the exporters the
> > changes are fairly mechanical in nature, with only two or three annoying
> > exporters that have slightly different patterns (one of which is buggy).
> >
> > - Alistair
> >
> >> Regards,
> >> Christian.
> >>
> >>>
> >>> The alternate is we update the contract to say mapping type cannot change
> >>> until
> >>> after all importers have unmapped. Enforcing this would require tracking
> >>> the
> >>> number of live mappings/pins per-attachment, but I think this is the
> >>> better
> >>> solution so will add that for the next version.
> >>>
> >>>>> int dma_buf_pin(struct dma_buf_attachment *attach)
> >>>>> {
> >>>>> struct dma_buf *dmabuf = attach->dmabuf;
> >>>>> + bool selected = attach->selected;
> >>>>> int ret = 0;
> >>>>>
> >>>>> WARN_ON(!attach->importer_ops);
> >>>>>
> >>>>> dma_resv_assert_held(dmabuf->resv);
> >>>>>
> >>>>> - if (dmabuf->ops->pin)
> >>>>> + ret = dma_buf_mapping_select(attach);
> >>>>> + if (ret)
> >>>>> + return ret;
> >>>>> +
> >>>>> + if (dmabuf->ops->pin) {
> >>>>> ret = dmabuf->ops->pin(attach);
> >>>>> + if (ret)
> >>>>> + dma_buf_mapping_unselect(attach, selected);
> >>>>> + }
> >>>>
> >>>> dma_buf_mapping_select() sets attach->selected = true before ops->pin()
> >>>> and the map callbacks run.
> >>>>
> >>>> In patches 14/15, xe/amdgpu migrate a VRAM buffer to system memory
> >>>> during pin and map when the attachment does not accept P2P.
> >>>>
> >>>> That migration calls dma_buf_invalidate_mappings() and clears
> >>>> attach->selected on every attachment, also the one being pinned/mapped.
> >>>>
> >>>> When the callback succeeds, the attachment stays pinned or mapped with
> >>>> attach->selected == false.
> >>>>
> >>>> A later map/pin on the same attachment can then reselect without an
> >>>> invalidation.
> >>>
> >>> Argh. Thanks. Will fix.
> >>>
> >>> - Alistair
> >>>
> >>>> Thanks,
> >>>> Karl
> >>
>