Skip to content

Commit bb42df4

Browse files
dma-buf: add dynamic DMA-buf handling v15
On the exporter side we add optional explicit pinning callbacks. Which are called when the importer doesn't implement dynamic handling, move notification or need the DMA-buf locked in place for its use case. On the importer side we add an optional move_notify callback. This callback is used by the exporter to inform the importers that their mappings should be destroyed as soon as possible. This allows the exporter to provide the mappings without the need to pin the backing store. v2: don't try to invalidate mappings when the callback is NULL, lock the reservation obj while using the attachments, add helper to set the callback v3: move flag for invalidation support into the DMA-buf, use new attach_info structure to set the callback v4: use importer_priv field instead of mangling exporter priv. v5: drop invalidation_supported flag v6: squash together with pin/unpin changes v7: pin/unpin takes an attachment now v8: nuke dma_buf_attachment_(map|unmap)_locked, everything is now handled backward compatible v9: always cache when export/importer don't agree on dynamic handling v10: minimal style cleanup v11: drop automatically re-entry avoidance v12: rename callback to move_notify v13: add might_lock in appropriate places v14: rebase on separated locking change v15: add EXPERIMENTAL flag, some more code comments Signed-off-by: Christian König <christian.koenig@amd.com> Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> Link: https://patchwork.freedesktop.org/patch/353993/?series=73646&rev=1
1 parent 258145e commit bb42df4

4 files changed

Lines changed: 188 additions & 20 deletions

File tree

‎drivers/dma-buf/Kconfig‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ config UDMABUF
3939
A driver to let userspace turn memfd regions into dma-bufs.
4040
Qemu can use this to create host dmabufs for guest framebuffers.
4141

42+
config DMABUF_MOVE_NOTIFY
43+
bool "Move notify between drivers (EXPERIMENTAL)"
44+
default n
45+
help
46+
Don''t pin buffers if the dynamic DMA-buf interface is available on both the
47+
exporter as well as the importer. This fixes a security problem where
48+
userspace is able to pin unrestricted amounts of memory through DMA-buf.
49+
But marked experimental because we don''t jet have a consistent execution
50+
context and memory management between drivers.
51+
4252
config DMABUF_SELFTESTS
4353
tristate "Selftests for the dma-buf interfaces"
4454
default n

‎drivers/dma-buf/dma-buf.c‎

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
527527
exp_info->ops->dynamic_mapping))
528528
return ERR_PTR(-EINVAL);
529529

530+
if (WARN_ON(!exp_info->ops->dynamic_mapping &&
531+
(exp_info->ops->pin || exp_info->ops->unpin)))
532+
return ERR_PTR(-EINVAL);
533+
530534
if (!try_module_get(exp_info->owner))
531535
return ERR_PTR(-ENOENT);
532536

@@ -651,7 +655,8 @@ EXPORT_SYMBOL_GPL(dma_buf_put);
651655
* calls attach() of dma_buf_ops to allow device-specific attach functionality
652656
* @dmabuf: [in] buffer to attach device to.
653657
* @dev: [in] device to be attached.
654-
* @dynamic_mapping: [in] calling convention for map/unmap
658+
* @importer_ops [in] importer operations for the attachment
659+
* @importer_priv [in] importer private pointer for the attachment
655660
*
656661
* Returns struct dma_buf_attachment pointer for this attachment. Attachments
657662
* must be cleaned up by calling dma_buf_detach().
@@ -667,11 +672,13 @@ EXPORT_SYMBOL_GPL(dma_buf_put);
667672
*/
668673
struct dma_buf_attachment *
669674
dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
670-
bool dynamic_mapping)
675+
const struct dma_buf_attach_ops *importer_ops,
676+
void *importer_priv)
671677
{
672678
struct dma_buf_attachment *attach;
673679
int ret;
674680

681+
/* TODO: make move_notify mandatory if importer_ops are provided. */
675682
if (WARN_ON(!dmabuf || !dev))
676683
return ERR_PTR(-EINVAL);
677684

@@ -681,7 +688,8 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
681688

682689
attach->dev = dev;
683690
attach->dmabuf = dmabuf;
684-
attach->dynamic_mapping = dynamic_mapping;
691+
attach->importer_ops = importer_ops;
692+
attach->importer_priv = importer_priv;
685693

686694
if (dmabuf->ops->attach) {
687695
ret = dmabuf->ops->attach(dmabuf, attach);
@@ -700,15 +708,19 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
700708
dma_buf_is_dynamic(dmabuf)) {
701709
struct sg_table *sgt;
702710

703-
if (dma_buf_is_dynamic(attach->dmabuf))
711+
if (dma_buf_is_dynamic(attach->dmabuf)) {
704712
dma_resv_lock(attach->dmabuf->resv, NULL);
713+
ret = dma_buf_pin(attach);
714+
if (ret)
715+
goto err_unlock;
716+
}
705717

706718
sgt = dmabuf->ops->map_dma_buf(attach, DMA_BIDIRECTIONAL);
707719
if (!sgt)
708720
sgt = ERR_PTR(-ENOMEM);
709721
if (IS_ERR(sgt)) {
710722
ret = PTR_ERR(sgt);
711-
goto err_unlock;
723+
goto err_unpin;
712724
}
713725
if (dma_buf_is_dynamic(attach->dmabuf))
714726
dma_resv_unlock(attach->dmabuf->resv);
@@ -722,6 +734,10 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
722734
kfree(attach);
723735
return ERR_PTR(ret);
724736

737+
err_unpin:
738+
if (dma_buf_is_dynamic(attach->dmabuf))
739+
dma_buf_unpin(attach);
740+
725741
err_unlock:
726742
if (dma_buf_is_dynamic(attach->dmabuf))
727743
dma_resv_unlock(attach->dmabuf->resv);
@@ -742,7 +758,7 @@ EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach);
742758
struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
743759
struct device *dev)
744760
{
745-
return dma_buf_dynamic_attach(dmabuf, dev, false);
761+
return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
746762
}
747763
EXPORT_SYMBOL_GPL(dma_buf_attach);
748764

@@ -765,8 +781,10 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
765781

766782
dmabuf->ops->unmap_dma_buf(attach, attach->sgt, attach->dir);
767783

768-
if (dma_buf_is_dynamic(attach->dmabuf))
784+
if (dma_buf_is_dynamic(attach->dmabuf)) {
785+
dma_buf_unpin(attach);
769786
dma_resv_unlock(attach->dmabuf->resv);
787+
}
770788
}
771789

772790
dma_resv_lock(dmabuf->resv, NULL);
@@ -779,6 +797,44 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
779797
}
780798
EXPORT_SYMBOL_GPL(dma_buf_detach);
781799

800+
/**
801+
* dma_buf_pin - Lock down the DMA-buf
802+
*
803+
* @attach: [in] attachment which should be pinned
804+
*
805+
* Returns:
806+
* 0 on success, negative error code on failure.
807+
*/
808+
int dma_buf_pin(struct dma_buf_attachment *attach)
809+
{
810+
struct dma_buf *dmabuf = attach->dmabuf;
811+
int ret = 0;
812+
813+
dma_resv_assert_held(dmabuf->resv);
814+
815+
if (dmabuf->ops->pin)
816+
ret = dmabuf->ops->pin(attach);
817+
818+
return ret;
819+
}
820+
EXPORT_SYMBOL_GPL(dma_buf_pin);
821+
822+
/**
823+
* dma_buf_unpin - Remove lock from DMA-buf
824+
*
825+
* @attach: [in] attachment which should be unpinned
826+
*/
827+
void dma_buf_unpin(struct dma_buf_attachment *attach)
828+
{
829+
struct dma_buf *dmabuf = attach->dmabuf;
830+
831+
dma_resv_assert_held(dmabuf->resv);
832+
833+
if (dmabuf->ops->unpin)
834+
dmabuf->ops->unpin(attach);
835+
}
836+
EXPORT_SYMBOL_GPL(dma_buf_unpin);
837+
782838
/**
783839
* dma_buf_map_attachment - Returns the scatterlist table of the attachment;
784840
* mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
@@ -798,6 +854,7 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
798854
enum dma_data_direction direction)
799855
{
800856
struct sg_table *sg_table;
857+
int r;
801858

802859
might_sleep();
803860

@@ -819,13 +876,25 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
819876
return attach->sgt;
820877
}
821878

822-
if (dma_buf_is_dynamic(attach->dmabuf))
879+
if (dma_buf_is_dynamic(attach->dmabuf)) {
823880
dma_resv_assert_held(attach->dmabuf->resv);
881+
if (!attach->importer_ops->move_notify ||
882+
!IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)) {
883+
r = dma_buf_pin(attach);
884+
if (r)
885+
return ERR_PTR(r);
886+
}
887+
}
824888

825889
sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
826890
if (!sg_table)
827891
sg_table = ERR_PTR(-ENOMEM);
828892

893+
if (IS_ERR(sg_table) && dma_buf_is_dynamic(attach->dmabuf) &&
894+
(!attach->importer_ops->move_notify ||
895+
!IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)))
896+
dma_buf_unpin(attach);
897+
829898
if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
830899
attach->sgt = sg_table;
831900
attach->dir = direction;
@@ -864,9 +933,34 @@ void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
864933
dma_resv_assert_held(attach->dmabuf->resv);
865934

866935
attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
936+
937+
if (dma_buf_is_dynamic(attach->dmabuf) &&
938+
(!attach->importer_ops->move_notify ||
939+
!IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)))
940+
dma_buf_unpin(attach);
867941
}
868942
EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
869943

944+
/**
945+
* dma_buf_move_notify - notify attachments that DMA-buf is moving
946+
*
947+
* @dmabuf: [in] buffer which is moving
948+
*
949+
* Informs all attachmenst that they need to destroy and recreated all their
950+
* mappings.
951+
*/
952+
void dma_buf_move_notify(struct dma_buf *dmabuf)
953+
{
954+
struct dma_buf_attachment *attach;
955+
956+
dma_resv_assert_held(dmabuf->resv);
957+
958+
list_for_each_entry(attach, &dmabuf->attachments, node)
959+
if (attach->importer_ops && attach->importer_ops->move_notify)
960+
attach->importer_ops->move_notify(attach);
961+
}
962+
EXPORT_SYMBOL_GPL(dma_buf_move_notify);
963+
870964
/**
871965
* DOC: cpu access
872966
*

‎drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
412412
return ERR_PTR(ret);
413413
}
414414

415+
static const struct dma_buf_attach_ops amdgpu_dma_buf_attach_ops = {
416+
};
417+
415418
/**
416419
* amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
417420
* @dev: DRM device
@@ -444,7 +447,8 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
444447
if (IS_ERR(obj))
445448
return obj;
446449

447-
attach = dma_buf_dynamic_attach(dma_buf, dev->dev, true);
450+
attach = dma_buf_dynamic_attach(dma_buf, dev->dev,
451+
&amdgpu_dma_buf_attach_ops, NULL);
448452
if (IS_ERR(attach)) {
449453
drm_gem_object_put(obj);
450454
return ERR_CAST(attach);

‎include/linux/dma-buf.h‎

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,41 @@ struct dma_buf_ops {
9393
*/
9494
void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
9595

96+
/**
97+
* @pin:
98+
*
99+
* This is called by dma_buf_pin and lets the exporter know that the
100+
* DMA-buf can't be moved any more.
101+
*
102+
* This is called with the dmabuf->resv object locked.
103+
*
104+
* This callback is optional and should only be used in limited use
105+
* cases like scanout and not for temporary pin operations.
106+
*
107+
* Returns:
108+
*
109+
* 0 on success, negative error code on failure.
110+
*/
111+
int (*pin)(struct dma_buf_attachment *attach);
112+
113+
/**
114+
* @unpin:
115+
*
116+
* This is called by dma_buf_unpin and lets the exporter know that the
117+
* DMA-buf can be moved again.
118+
*
119+
* This is called with the dmabuf->resv object locked.
120+
*
121+
* This callback is optional.
122+
*/
123+
void (*unpin)(struct dma_buf_attachment *attach);
124+
96125
/**
97126
* @map_dma_buf:
98127
*
99128
* This is called by dma_buf_map_attachment() and is used to map a
100129
* shared &dma_buf into device address space, and it is mandatory. It
101-
* can only be called if @attach has been called successfully. This
102-
* essentially pins the DMA buffer into place, and it cannot be moved
103-
* any more
130+
* can only be called if @attach has been called successfully.
104131
*
105132
* This call may sleep, e.g. when the backing storage first needs to be
106133
* allocated, or moved to a location suitable for all currently attached
@@ -141,9 +168,8 @@ struct dma_buf_ops {
141168
*
142169
* This is called by dma_buf_unmap_attachment() and should unmap and
143170
* release the &sg_table allocated in @map_dma_buf, and it is mandatory.
144-
* It should also unpin the backing storage if this is the last mapping
145-
* of the DMA buffer, it the exporter supports backing storage
146-
* migration.
171+
* For static dma_buf handling this might also unpins the backing
172+
* storage if this is the last mapping of the DMA buffer.
147173
*/
148174
void (*unmap_dma_buf)(struct dma_buf_attachment *,
149175
struct sg_table *,
@@ -311,6 +337,34 @@ struct dma_buf {
311337
} cb_excl, cb_shared;
312338
};
313339

340+
/**
341+
* struct dma_buf_attach_ops - importer operations for an attachment
342+
* @move_notify: [optional] notification that the DMA-buf is moving
343+
*
344+
* Attachment operations implemented by the importer.
345+
*/
346+
struct dma_buf_attach_ops {
347+
/**
348+
* @move_notify
349+
*
350+
* If this callback is provided the framework can avoid pinning the
351+
* backing store while mappings exists.
352+
*
353+
* This callback is called with the lock of the reservation object
354+
* associated with the dma_buf held and the mapping function must be
355+
* called with this lock held as well. This makes sure that no mapping
356+
* is created concurrently with an ongoing move operation.
357+
*
358+
* Mappings stay valid and are not directly affected by this callback.
359+
* But the DMA-buf can now be in a different physical location, so all
360+
* mappings should be destroyed and re-created as soon as possible.
361+
*
362+
* New mappings can be created after this callback returns, and will
363+
* point to the new location of the DMA-buf.
364+
*/
365+
void (*move_notify)(struct dma_buf_attachment *attach);
366+
};
367+
314368
/**
315369
* struct dma_buf_attachment - holds device-buffer attachment data
316370
* @dmabuf: buffer for this attachment.
@@ -319,8 +373,9 @@ struct dma_buf {
319373
* @sgt: cached mapping.
320374
* @dir: direction of cached mapping.
321375
* @priv: exporter specific attachment data.
322-
* @dynamic_mapping: true if dma_buf_map/unmap_attachment() is called with the
323-
* dma_resv lock held.
376+
* @importer_ops: importer operations for this attachment, if provided
377+
* dma_buf_map/unmap_attachment() must be called with the dma_resv lock held.
378+
* @importer_priv: importer specific attachment data.
324379
*
325380
* This structure holds the attachment information between the dma_buf buffer
326381
* and its user device(s). The list contains one attachment struct per device
@@ -337,7 +392,8 @@ struct dma_buf_attachment {
337392
struct list_head node;
338393
struct sg_table *sgt;
339394
enum dma_data_direction dir;
340-
bool dynamic_mapping;
395+
const struct dma_buf_attach_ops *importer_ops;
396+
void *importer_priv;
341397
void *priv;
342398
};
343399

@@ -399,6 +455,7 @@ static inline void get_dma_buf(struct dma_buf *dmabuf)
399455
*/
400456
static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
401457
{
458+
/* TODO: switch to using pin/unpin functions as indicator. */
402459
return dmabuf->ops->dynamic_mapping;
403460
}
404461

@@ -413,16 +470,19 @@ static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
413470
static inline bool
414471
dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
415472
{
416-
return attach->dynamic_mapping;
473+
return !!attach->importer_ops;
417474
}
418475

419476
struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
420477
struct device *dev);
421478
struct dma_buf_attachment *
422479
dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
423-
bool dynamic_mapping);
480+
const struct dma_buf_attach_ops *importer_ops,
481+
void *importer_priv);
424482
void dma_buf_detach(struct dma_buf *dmabuf,
425483
struct dma_buf_attachment *attach);
484+
int dma_buf_pin(struct dma_buf_attachment *attach);
485+
void dma_buf_unpin(struct dma_buf_attachment *attach);
426486

427487
struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
428488

0 commit comments

Comments
 (0)