Lines Matching refs:the
8 This document serves as a guide to device-driver writers on what is the dma-buf
12 either the 'exporter' of buffers, or the 'user' of buffers.
14 Say a driver A wants to use buffers created by driver B, then we call B as the
18 - implements and manages operations[1] for the buffer
19 - allows other users to share the buffer by using dma_buf sharing APIs,
20 - manages the details of buffer allocation,
21 - decides about the actual backing storage where this allocation happens,
26 - is one of (many) sharing users of the buffer.
27 - doesn't need to worry about how the buffer is allocated, or where.
28 - needs a mechanism to get access to the scatterlist that makes up this buffer
29 in memory, mapped into its own address space, so it can access the same area
35 The dma_buf buffer sharing API usage contains the following steps:
38 2. Userspace gets the file descriptor associated with the exported buffer, and
40 3. Each buffer-user 'connects' itself to the buffer
41 4. When needed, buffer-user requests access to the buffer from exporter
42 5. When finished with its use, the buffer-user notifies end-of-DMA to exporter
44 itself from the buffer.
51 that can be performed on the exported dma_buf, and flags for the file
53 dma_buf_export_info, defined via the DEFINE_DMA_BUF_EXPORT_INFO macro.
60 returns a pointer to the same. It also associates an anonymous file with this
61 buffer, so it can be exported. On failure to allocate the dma_buf object,
64 'exp_name' in struct dma_buf_export_info is the name of exporter - to
69 DEFINE_DMA_BUF_EXPORT_INFO macro defines the struct dma_buf_export_info,
75 Userspace entity requests for a file-descriptor (fd) which is a handle to the
76 anonymous file associated with the buffer. It can then share the fd with other
82 This API installs an fd for the anonymous file associated with this buffer;
85 3. Each buffer-user 'connects' itself to the buffer
87 Each buffer-user now gets a reference to the buffer, using the fd passed to
93 This API will return a reference to the dma_buf, and increment refcount for
96 After this, the buffer-user needs to attach its device with the buffer, which
97 helps the exporter to know of device buffer constraints.
104 for scatterlist operations. It will optionally call the 'attach' dma_buf
105 operation, if provided by the exporter.
107 The dma-buf sharing framework does the bookkeeping bits related to managing
108 the list of all attachments to a buffer.
110 Until this stage, the buffer-exporter has the option to choose not to actually
111 allocate the backing storage for this buffer, but wait for the first buffer-user
115 4. When needed, buffer-user requests access to the buffer
117 Whenever a buffer-user wants to use the buffer for any DMA, it asks for
118 access to the buffer using dma_buf_map_attachment API. At least one attach to
119 the buffer must have happened before map_dma_buf can be called.
125 This is a wrapper to dma_buf->ops->map_dma_buf operation, which hides the
126 "dma_buf->ops->" indirection from the users of this interface.
132 It is one of the buffer operations that must be implemented by the exporter.
133 It should return the sg_table containing scatterlist for this buffer, mapped
136 If this is being called for the first time, the exporter can now choose to
137 scan through the list of attachments for this buffer, collate the requirements
138 of the attached devices, and choose an appropriate backing storage for the
142 accessing at the same time (for reading, maybe), or any other kind of sharing
143 that the exporter might wish to make available to buffer-users.
148 5. When finished, the buffer-user notifies end-of-DMA to exporter
150 Once the DMA for the current buffer-user is over, it signals 'end-of-DMA' to
151 the exporter using the dma_buf_unmap_attachment API.
157 This is a wrapper to dma_buf->ops->unmap_dma_buf() operation, which hides the
158 "dma_buf->ops->" indirection from the users of this interface.
165 unmap_dma_buf signifies the end-of-DMA for the attachment provided. Like
166 map_dma_buf, this API also must be implemented by the exporter.
169 6. when buffer-user is done using this buffer, it 'disconnects' itself from the
172 After the buffer-user has no more interest in using this buffer, it should
173 disconnect itself from the buffer:
175 - it first detaches itself from the buffer.
181 This API removes the attachment from the list in dmabuf, and optionally calls
184 - Then, the buffer-user returns the buffer reference to exporter.
189 This API then reduces the refcount for this buffer.
191 If, as a result of this call, the refcount becomes 0, the 'release' file
192 operation related to this fd is called. It calls the dmabuf->ops->release()
193 operation in turn, and frees the memory allocated for dmabuf when exported.
197 The attach-detach calls allow the exporter to figure out backing-storage
198 constraints for the currently-interested devices. This allows preferential
209 - and the backing storage has been allocated for this buffer,
211 be allowed, if possible for the exporter.
213 In case it is allowed by the exporter:
214 if the new buffer-user has stricter 'backing-storage constraints', and the
215 exporter can handle these constraints, the exporter can just stall on the
218 Once all users have finished accessing and have unmapped this buffer, the
219 exporter could potentially move the buffer to the stricter backing-storage,
221 from the migrated backing-storage.
223 If the exporter cannot fulfill the backing-storage constraints of the new
225 denote non-compatibility of the new buffer-sharing request with the current
228 If the exporter chooses not to allow an attach() operation once a
234 The motivation to allow cpu access from the kernel to a dma-buf object from the
236 - fallback operations, e.g. if the devices is connected to a usb bus and the
237 kernel needs to shuffle the data around first before sending it away.
238 - full transparency for existing users on the importer side, i.e. userspace
239 should not notice the difference between a normal object from that subsystem
241 opengl drivers that expect to still use all the existing upload/download
244 Access to a dma_buf from the kernel context involves three steps:
246 1. Prepare access, which invalidate any necessary caches and make the object
248 2. Access the object page-by-page with the dma_buf map apis
254 Before an importer can access a dma_buf object with the cpu from the kernel
255 context, it needs to notify the exporter of the access that is about to
263 This allows the exporter to ensure that the memory is actually available for
264 cpu access - the exporter might need to allocate or swap-in and pin the
266 coherent for the given range and access direction. The range and access
267 direction can be used by the exporter to optimize the cache flushing, i.e.
268 access outside of the range or with a different direction (read instead of
269 write) might return stale or even bogus data (e.g. when the exporter needs to
270 copy the data to temporary storage).
274 2. Accessing the buffer
279 a pointer in kernel virtual address space. Afterwards the chunk needs to be
281 and unmapped, i.e. the importer does not need to call begin_cpu_access again
282 before mapping the same chunk again.
289 facilitate non-blocking fast-paths. Neither the importer nor the exporter (in
290 the callback) is allowed to block when using these.
296 For importers all the restrictions of using kmap apply, like the limited
298 atomic dma_buf kmaps at the same time (in any given process context).
300 dma_buf kmap calls outside of the range specified in begin_cpu_access are
301 undefined. If the range is not PAGE_SIZE aligned, kmap needs to succeed on
302 the partial chunks at the beginning and end but may return stale or bogus
303 data outside of the range (in these partial chunks).
308 For some cases the overhead of kmap can be too high, a vmap interface
316 The vmap call can fail if there is no vmap support in the exporter, or if it
318 the dma-buf layer keeps a reference count for all vmap access and calls down
319 into the exporter's vmap function only when no vmapping exists, and only
321 by taking the dma_buf->lock mutex.
325 When the importer is done accessing the range specified in begin_cpu_access,
326 it needs to announce this to the exporter (to facilitate cache flushing and
345 In many processing pipelines it is sometimes required that the cpu can access
346 the data in a dma-buf (e.g. for thumbnail creation, snapshots, ...). To avoid
347 the need to handle this specially in userspace frameworks for buffer sharing
348 it's ideal if the dma_buf fd itself can be used to access the backing storage
356 No special interfaces, userspace simply calls mmap on the dma-buf fd.
360 Similar to the motivation for kernel cpu access it is again important that
361 the userspace code of a given importing subsystem can use the same interfaces
363 especially important for drm where the userspace part of contemporary OpenGL,
367 The assumption in the current dma-buf interfaces is that redirecting the
368 initial mmap is all that's needed. A survey of some of the existing
370 up with outstanding asynchronous processing on the device or allocating
373 increase the complexity quite a bit.
379 If the importing subsystem simply provides a special-purpose mmap call to set
385 Because dma-buf buffers have invariant size over their lifetime, the dma-buf
390 userspace, the exporter needs to set up a coherent mapping. If that's not
392 leaving the cpu domain and flushing caches at fault time. Note that all the
393 dma_buf files share the same anon inode, hence the exporter needs to replace
394 the dma_buf file stored in vma->vm_file with it's own if pte shootdown is
395 required. This is because the kernel uses the underlying inode's address_space
399 If the above shootdown dance turns out to be too expensive in certain
401 for userspace mappings. But the current assumption is that using mmap is
406 Synchronization is an orthogonal issue to sharing the backing storage of a
410 interesting ways depending upong the exporter (if userspace starts depending
413 Other Interfaces Exposed to Userspace on the dma-buf FD
416 - Since kernel 3.12 the dma-buf FD supports the llseek system call, but only
418 the usual size discover pattern size = SEEK_END(0); SEEK_SET(0). Every other
421 If llseek on dma-buf FDs isn't support the kernel will report -ESPIPE for all
422 cases. Userspace can use this to detect support for discovering the dma-buf
428 - Any exporters or users of the dma-buf buffer sharing framework must have
431 - In order to avoid fd leaks on exec, the FD_CLOEXEC flag must be set
432 on the file descriptor. This is not just a resource leak, but a
433 potential security hole. It could give the newly exec'd application
434 access to buffers, via the leaked fd, to which it should otherwise
438 atomically when the fd is created, is that this is inherently racy in a
440 opening/creating the file descriptor, as the application may not even be
441 aware of the fd's.
444 flag be set when the dma-buf fd is created. So any API provided by
445 the exporting driver to create a dmabuf fd must provide a way to let
449 coherency for mmap support, it needs to be able to zap all the ptes pointing
450 at the backing storage. Now linux mm needs a struct address_space associated
451 with the struct file stored in vma->vm_file to do that with the function
452 unmap_mapping_range. But the dma_buf framework only backs every dma_buf fd
453 with the anon_file struct file, i.e. all dma_bufs share the same file.
456 by setting vma->vm_file and adjusting vma->vm_pgoff in the dma_buf mmap
457 callback. In the specific case of a gem driver the exporter could use the
459 zap ptes by unmapping the corresponding range of the struct address_space