On 12/1/26 10:33, Alistair Popple wrote:
> On 2026-12-01 at 19:11 +1100, Christian König <[email protected]> 
> wrote...
>> On 12/1/26 08:13, Alistair Popple wrote:
>>> On 2026-12-01 at 15:05 +1100, Dave Airlie <[email protected]> wrote...
>>>>> The problem is I can't figure out how to build that low level API.
>>
>> Seconded.
>>
>> I can give you quite a bunch of requirements based on what the high level 
>> DMA-buf API and uAPI has on functionality and based on what I have seen on 
>> use cases in the wild.
> 
> I think a bunch of requirements would be helpful, because they've kind of been
> scattered across threads at this point and I concluded it was kind of 
> impossible
> to build given the constraints.
> 
> In particular the one thing that PCIe P2P, IOMMUFD, KVM, etc. all need is the
> physical address. That is the vendor agnostic thing below but providing that 
> has
> been consistently NACKed and I can't see how you can make any low level API 
> work
> without having the physical address.

Yeah good point, let's start with that one. There are multiple problems with 
using physical addresses.

First of all a physical address is only what the CPU can see, but there are a 
lot of use cases where you need to access resources the CPU can't reach. For 
example hidden device memory or even system memory where the CPU intentionally 
has no address for because of digital rights management reasons etc... Taking a 
look at all the use cases I have seen I would say that DMA-buf actually 
contains more non CPU accessible resources than CPU accessible ones.

Then especially for PCI a physical address is only halve the information you 
need. The importer can also need additional info to stitch together the TLP, 
like SH, snooping bit, atomic access ops etc....

Then there is the reason that we have seen a tremendous amount of cases where 
importers abused the physical address and/or struct page pointer which was 
unfortunately accessible through the SGT. To be honest I can't count the number 
of CVE entries given to such bugs.

There are some use cases for KVM for example which need physical addresses, but 
to be honest they look like corner cases.

Ideally even the exporter shouldn't work with physically addresses I think. For 
example even when you have a PCI P2P API which fills in your mapping it should 
work with PCI BAR, offset and length and and not a physical address into the 
BAR.

> I know in the past you've suggested passing all the importer context through 
> to
> the exporter, but that is also a complete non-starter. It lets arbitrary 
> drivers
> implement the mapping wrong and likely get all the life-times, which belong to
> the importer, wrong. So until we can solve that I'm not really seeing much of
> way forward here.

WOW, wait a second. With the exception of the old interface which pins things 
down the lifetime of the mapping belongs to the exporter and not the importer!

That's one of the reasons why the exporter needs the IOMMU context of the 
importer for example, otherwise you can't forcefully remove IOMMU mappings 
without relying on the importer doing that for you.

>> But I can't say either what the low level API needs to look like and one of 
>> the major reasons I want DMA-buf separated from that low level API is that 
>> it seems like this needs a different field of expertise and responsibilities.
> 
> I get it, and I do appreciate the expertise provided here. However if DMA-buf
> is supposed to be some separate mechanism just for passing pointer types 
> between
> exporter/importer with some lifetime and location negotiation rules it 
> shouldn't
> also be seeking to impose constraints on what information those types actually
> contain. That is none of it's business and is up to what the low level API
> needs.

I have to admit that's a pretty good argument, but I would say the restriction 
is on the exporter, importer and which role is using what and not the low level 
API itself.

> If the low level API needs importer context and physical addresses to do
> anything useful then so be it.

As long as the low level API is only called by the exporter I have no problem 
with it taking physical addresses when it is sufficient for the use case.

I just think that we have use cases where the exporter needs to give additional 
information to the physical address to the IOMMU driver for example.

I need to attend some customer stuff now and will try to answer the rest of the 
mail later today.

Regards,
Christian.

>>>>>
>>>>> The current low level DMA API can't correctly handle PCIe P2P, or 
>>>>> IOMMUFD, or
>>>>> the KVM/confidential compute use-cases (I'm less familiar with those). It 
>>>>> can
>>>>> only handle creating a DMA mapping for system memory for a specific 
>>>>> importing
>>>>> device.
>>>>>
>>>>> So I assume you are talking about some new API. Following Dave's lead 
>>>>> lets call
>>>>> that shared_buf_map(). That would need to take some importing device 
>>>>> context
>>>>> so that it could dispatch to the correct mapping code and a list of 
>>>>> physical
>>>>> locations the exporter needs mapped.
>>>>>
>>>>> At a high level something like:
>>>>>
>>>>> struct dma_addr_list *shared_buf_map(import_context, location_list);
>>>>>
>>>>> Where the import_context is some importer specific struct opaque to the 
>>>>> exporter
>>>>> and the location list is a list of either PCIe MMIO or system RAM physical
>>>>> addresses.
>>>>>
>>>>> Every exporter ->map_dma_buf() should call shared_buf_map() instead of
>>>>> open-coding their own mapping code, as I think we all agree we don't want 
>>>>> that
>>>>> open-coded.
>>>>>
>>>>> Placement selection can be dealt with as a separate topic, so setting that
>>>>> aside means passing in the same location list regardless of importing 
>>>>> device.
>>>>> shared_buf_map() then has to dispatch to the correct mapping generation 
>>>>> code -
>>>>> either DMA API, PCIe peer-to-peer API, IOMMUFD API, KVM API or maybe some 
>>>>> other
>>>>> importing device/driver specific API.
>>
>> That sounds totally sane to me, yes.
> 
> Ok. The problem is how this shared_buf_map() gets the importer context.
> 
> It would be easy enough to make work if an impoter could call eg.
> shared_buf_map(import_type, attachment) or something like that, but I got the
> impression from the middle-layer discussion that that would be undesirable and
> get NACKed because only exporter should call that. So not sure how to actually
> make that work, unless my impression there is wrong.
> 
>>>>>
>>>>> I could see how that could be made to work, but it seems more complex than
>>>>> a dma_buf exporter op that returns a list of physical locations and a 
>>>>> helper
>>>>> library that importers call to do the mapping into whatever importer 
>>>>> specific
>>>>> context is needed. Instead you would have to add importer specific 
>>>>> callbacks
>>>>> that the shared_buf_map code could call along with plumbing the importer 
>>>>> context
>>>>> through.
>>
>> I've thought about that as well for a while but came to the conclusion that 
>> we most likely want some more differentiation here.
>>
>> On the one hand we have the vendor agnostic things like PCI P2P, IOMMU, 
>> IOVAs, bus addresses, CPU physical addresses etc... which a vendor agnostic 
>> API should be dealing with.
> 
> Right - I'd like to tackle the vendor agnostic things, as they provide the 
> most
> value. Vendor specific things have already been hacked up to work in all sorts
> of horrible ways, which is why I want to fix this so I can at least remove 
> some
> of the horrid hacks from our code.
> 
>> On the other hand we have vendor specific things like XGMI, GDS/GWS/OA, I2C, 
>> I2S, doorbells, <just name the strange thing people stuffed into a DMA-buf> 
>> which only specific drivers understand.
>>
>> The problem is now that those things at least partially overlap, for example 
>> a doorbell which is ringed by a PCI P2P write.
> 
> I'd argue that a doorbell rung by a PCI P2P write isn't any different than any
> other PCI P2P access. The data in these buffers doesn't change the type from a
> buffer sharing perspective, the type of data in these buffers is always up to
> some other layer (eg. userspace), so I don't see the overlap.
> 
>>>>
>>>> This is where I got to thinking it might end up, not that we have an
>>>> export function that actually exports anything, but merely advertises
>>>> this provider has an object with this fd and it can export it in these
>>>> ways.
>>>>
>>>> Then on import we'd have to go into the exporter and setup the object
>>>> that gets shared between that specific importer/exporter combination
>>>> and not always have a singular export, but one per import, that I
>>>> suppose could be refcounted per export if lots of importers wanted the
>>>> same thing.
>>
>> Yes that is a requirement of ROCm as well and we have hacked around such 
>> things massively with the KFD and all the rat tail of consequences seen by 
>> it.
> 
> I should introduce you to RM :-)
> 
> Thanks.
> 
>  - Alistair
> 
>>> That sounds reasonably complex. Do you have a vague plan, ideally in the 
>>> form
>>> of data-structures/API definitions, that would help explain what it would 
>>> look
>>> like?
>>>
>>> For now I have sent a simplified RFC series -
>>> https://lore.kernel.org/dri-devel/20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7...@nvidia.com/T/#t
>>>  - that drops the placement
>>> bits and implements the rough plan I outlined in
>>> https://lore.kernel.org/dri-devel/[email protected]/ . I'm 
>>> not
>>> sure it addresses everything, but I'd be interested in getting feedback on 
>>> what
>>> it gets right and what specifically needs fixing/changing, and how, to 
>>> satisfy
>>> the design constraints.
>>>
>>> Thanks.
>>>
>>>  - Alistair
>>>
>>>> Now maybe we can't call this dma-buf, but I think dma-buf is a subset of 
>>>> this.
>>
>> I would say that DMA-buf is the high level API which allows importers and 
>> exporters to exchange the data structures of the low level API.
>>
>> Christian.
>>
>>>>
>>>> Dave.
>>

Reply via email to