[PATCH] dma-buf: add no-op stubs when CONFIG_DMA_SHARED_BUFFER is disabled

Viresh Kumar posted 1 patch 4 days, 13 hours ago
include/linux/dma-buf.h | 116 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 116 insertions(+)
[PATCH] dma-buf: add no-op stubs when CONFIG_DMA_SHARED_BUFFER is disabled
Posted by Viresh Kumar 4 days, 13 hours ago
Move several dma-buf function declarations under
CONFIG_DMA_SHARED_BUFFER and provide static inline no-op implementations
for the disabled case to allow the callers to build when the feature is
not compiled in.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 include/linux/dma-buf.h | 116 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)

diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index d58e329ac0e7..06e494d8f6b0 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -568,6 +568,7 @@ static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
 	return !!dmabuf->ops->pin;
 }
 
+#ifdef CONFIG_DMA_SHARED_BUFFER
 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
 					  struct device *dev);
 struct dma_buf_attachment *
@@ -609,4 +610,119 @@ int dma_buf_vmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map);
 void dma_buf_vunmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map);
 struct dma_buf *dma_buf_iter_begin(void);
 struct dma_buf *dma_buf_iter_next(struct dma_buf *dmbuf);
+
+#else
+static inline struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
+							struct device *dev)
+{
+	return NULL;
+}
+
+static inline struct dma_buf_attachment *
+dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
+		       const struct dma_buf_attach_ops *importer_ops,
+		       void *importer_priv)
+{
+	return NULL;
+}
+
+static inline void dma_buf_detach(struct dma_buf *dmabuf,
+				  struct dma_buf_attachment *attach) { }
+
+static inline int dma_buf_pin(struct dma_buf_attachment *attach)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void dma_buf_unpin(struct dma_buf_attachment *attach) { }
+
+static inline struct dma_buf *
+dma_buf_export(const struct dma_buf_export_info *exp_info)
+{
+	return NULL;
+}
+
+
+static inline int dma_buf_fd(struct dma_buf *dmabuf, int flags)
+{
+	return -EOPNOTSUPP;
+}
+
+
+static inline struct dma_buf *dma_buf_get(int fd)
+{
+	return NULL;
+}
+
+static inline void dma_buf_put(struct dma_buf *dmabuf) { }
+
+static inline struct sg_table *
+dma_buf_map_attachment(struct dma_buf_attachment *, enum dma_data_direction)
+{
+	return NULL;
+}
+
+static inline void dma_buf_unmap_attachment(struct dma_buf_attachment *,
+					    struct sg_table *,
+					    enum dma_data_direction) { }
+
+static inline void dma_buf_move_notify(struct dma_buf *dma_buf) { }
+
+static inline int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
+					   enum dma_data_direction dir)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
+					 enum dma_data_direction dir)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline struct sg_table *
+dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
+				enum dma_data_direction direction)
+{
+	return NULL;
+}
+
+static inline void
+dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
+				  struct sg_table *sg_table,
+				  enum dma_data_direction direction) { }
+
+static inline int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
+			       unsigned long)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
+{ }
+
+static inline int dma_buf_vmap_unlocked(struct dma_buf *dmabuf,
+					struct iosys_map *map)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void dma_buf_vunmap_unlocked(struct dma_buf *dmabuf,
+					   struct iosys_map *map) { }
+
+static inline struct dma_buf *dma_buf_iter_begin(void)
+{
+	return NULL;
+}
+
+static inline struct dma_buf *dma_buf_iter_next(struct dma_buf *dmbuf)
+{
+	return NULL;
+}
+#endif /* CONFIG_DMA_SHARED_BUFFER */
 #endif /* __DMA_BUF_H__ */
-- 
2.31.1.272.g89b43f80a514
Re: [PATCH] dma-buf: add no-op stubs when CONFIG_DMA_SHARED_BUFFER is disabled
Posted by Christian König 4 days, 13 hours ago
On 11/27/25 08:40, Viresh Kumar wrote:
> Move several dma-buf function declarations under
> CONFIG_DMA_SHARED_BUFFER and provide static inline no-op implementations
> for the disabled case to allow the callers to build when the feature is
> not compiled in.

Good point, but which driver actually needs that?

At least the whole DRM subsystem selects CONFIG_DMA_SHARED_BUFFER and others (like V4L) usually don't compile their whole infrastructure when that option isn't selected.

In other words there should be a concrete example of what breaks in the commit message.

> 
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  include/linux/dma-buf.h | 116 ++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 116 insertions(+)
> 
> diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
> index d58e329ac0e7..06e494d8f6b0 100644
> --- a/include/linux/dma-buf.h
> +++ b/include/linux/dma-buf.h
> @@ -568,6 +568,7 @@ static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
>  	return !!dmabuf->ops->pin;
>  }
>  
> +#ifdef CONFIG_DMA_SHARED_BUFFER
>  struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
>  					  struct device *dev);
>  struct dma_buf_attachment *
> @@ -609,4 +610,119 @@ int dma_buf_vmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map);
>  void dma_buf_vunmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map);
>  struct dma_buf *dma_buf_iter_begin(void);
>  struct dma_buf *dma_buf_iter_next(struct dma_buf *dmbuf);
> +
> +#else
> +static inline struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
> +							struct device *dev)
> +{
> +	return NULL;

This should probably be an ERR_PTR(-EOPNOTSUPP);

> +}
> +
> +static inline struct dma_buf_attachment *
> +dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
> +		       const struct dma_buf_attach_ops *importer_ops,
> +		       void *importer_priv)
> +{
> +	return NULL;

Same here, ERR_PTR(-EOPNOTSUPP).

> +}
> +
> +static inline void dma_buf_detach(struct dma_buf *dmabuf,
> +				  struct dma_buf_attachment *attach) { }
> +
> +static inline int dma_buf_pin(struct dma_buf_attachment *attach)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
> +static inline void dma_buf_unpin(struct dma_buf_attachment *attach) { }
> +
> +static inline struct dma_buf *
> +dma_buf_export(const struct dma_buf_export_info *exp_info)
> +{
> +	return NULL;
> +}
> +
> +
> +static inline int dma_buf_fd(struct dma_buf *dmabuf, int flags)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
> +
> +static inline struct dma_buf *dma_buf_get(int fd)
> +{
> +	return NULL;

And here ERR_PTR(-EINVAL).

> +}
> +
> +static inline void dma_buf_put(struct dma_buf *dmabuf) { }
> +
> +static inline struct sg_table *
> +dma_buf_map_attachment(struct dma_buf_attachment *, enum dma_data_direction)
> +{
> +	return NULL;

ERR_PTR(-EINVAL)

> +}
> +
> +static inline void dma_buf_unmap_attachment(struct dma_buf_attachment *,
> +					    struct sg_table *,
> +					    enum dma_data_direction) { }
> +
> +static inline void dma_buf_move_notify(struct dma_buf *dma_buf) { }
> +
> +static inline int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
> +					   enum dma_data_direction dir)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
> +static inline int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
> +					 enum dma_data_direction dir)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
> +static inline struct sg_table *
> +dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
> +				enum dma_data_direction direction)
> +{
> +	return NULL;

ERR_PTR(-EINVAL)

> +}
> +
> +static inline void
> +dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
> +				  struct sg_table *sg_table,
> +				  enum dma_data_direction direction) { }
> +
> +static inline int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
> +			       unsigned long)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
> +static inline int dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
> +static inline void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
> +{ }
> +
> +static inline int dma_buf_vmap_unlocked(struct dma_buf *dmabuf,
> +					struct iosys_map *map)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
> +static inline void dma_buf_vunmap_unlocked(struct dma_buf *dmabuf,
> +					   struct iosys_map *map) { }
> +


> +static inline struct dma_buf *dma_buf_iter_begin(void)
> +{
> +	return NULL;
> +}
> +
> +static inline struct dma_buf *dma_buf_iter_next(struct dma_buf *dmbuf)
> +{
> +	return NULL;
> +}

Those two are only for BPF and not driver use.

Regards,
Christian.

> +#endif /* CONFIG_DMA_SHARED_BUFFER */
>  #endif /* __DMA_BUF_H__ */
Re: [PATCH] dma-buf: add no-op stubs when CONFIG_DMA_SHARED_BUFFER is disabled
Posted by Viresh Kumar 4 days, 13 hours ago
On 27-11-25, 09:07, Christian König wrote:
> On 11/27/25 08:40, Viresh Kumar wrote:
> > Move several dma-buf function declarations under
> > CONFIG_DMA_SHARED_BUFFER and provide static inline no-op implementations
> > for the disabled case to allow the callers to build when the feature is
> > not compiled in.
> 
> Good point, but which driver actually needs that?

This broke some WIP stuff [1] which isn't posted upstream yet. That's why I
didn't mention anything in the commit log, though I could have added a comment
about that in the non-commit-log part.

> In other words there should be a concrete example of what breaks in the commit message.

There is time for those changes to be posted and not sure if they will be
accepted or not. But either way, this change made sense in general and so I
thought there is nothing wrong to get this upstream right away.

> > +static inline struct dma_buf *dma_buf_get(int fd)
> > +{
> > +	return NULL;
> 
> And here ERR_PTR(-EINVAL).

I am not really sure if this should be EINVAL in this case. EOPNOTSUPP still
makes sense as the API isn't supported.

> > +static inline struct dma_buf *dma_buf_iter_begin(void)
> > +{
> > +	return NULL;
> > +}
> > +
> > +static inline struct dma_buf *dma_buf_iter_next(struct dma_buf *dmbuf)
> > +{
> > +	return NULL;
> > +}
> 
> Those two are only for BPF and not driver use.

Will drop them.

-- 
viresh

[1] https://git.kernel.org/pub/scm/linux/kernel/git/vireshk/linux.git/log/?h=virtio/msg
Re: [PATCH] dma-buf: add no-op stubs when CONFIG_DMA_SHARED_BUFFER is disabled
Posted by Christian König 4 days, 11 hours ago
On 11/27/25 09:23, Viresh Kumar wrote:
> On 27-11-25, 09:07, Christian König wrote:
>> On 11/27/25 08:40, Viresh Kumar wrote:
>>> Move several dma-buf function declarations under
>>> CONFIG_DMA_SHARED_BUFFER and provide static inline no-op implementations
>>> for the disabled case to allow the callers to build when the feature is
>>> not compiled in.
>>
>> Good point, but which driver actually needs that?
> 
> This broke some WIP stuff [1] which isn't posted upstream yet. That's why I
> didn't mention anything in the commit log, though I could have added a comment
> about that in the non-commit-log part.

Well then better send that out with the full patch set.

>> In other words there should be a concrete example of what breaks in the commit message.
> 
> There is time for those changes to be posted and not sure if they will be
> accepted or not. But either way, this change made sense in general and so I
> thought there is nothing wrong to get this upstream right away.

Yeah when it is unused intermediately then that is usually a no-go even if I agree that it makes sense.

>>> +static inline struct dma_buf *dma_buf_get(int fd)
>>> +{
>>> +	return NULL;
>>
>> And here ERR_PTR(-EINVAL).
> 
> I am not really sure if this should be EINVAL in this case. EOPNOTSUPP still
> makes sense as the API isn't supported.

When the API isn't compiled in the fd can't be valid (because you can't create a dma_buf object in the first place).

So returning -EINVAL still makes a lot of sense.

Regards,
Christian.

> 
>>> +static inline struct dma_buf *dma_buf_iter_begin(void)
>>> +{
>>> +	return NULL;
>>> +}
>>> +
>>> +static inline struct dma_buf *dma_buf_iter_next(struct dma_buf *dmbuf)
>>> +{
>>> +	return NULL;
>>> +}
>>
>> Those two are only for BPF and not driver use.
> 
> Will drop them.
>