On Thu, May 26, 2022 at 12:40 PM Simon Ser <cont...@emersion.fr> wrote:
> To discover support for new DMA-BUF IOCTLs, user-space has no > choice but to try to perform the IOCTL on an existing DMA-BUF. > However, user-space may want to figure out whether or not the > IOCTL is available before it has a DMA-BUF at hand, e.g. at > initialization time in a Wayland compositor. > > Add a /sys/kernel/dmabuf/caps directory which allows the DMA-BUF > subsystem to advertise supported features. Add a > sync_file_import_export entry which indicates that importing and > exporting sync_files from/to DMA-BUFs is supported. > > Signed-off-by: Simon Ser <cont...@emersion.fr> > Cc: Jason Ekstrand <ja...@jlekstrand.net> > Cc: Daniel Vetter <daniel.vet...@ffwll.ch> > Cc: Bas Nieuwenhuizen <b...@basnieuwenhuizen.nl> > Cc: Christian König <christian.koe...@amd.com> > --- > > Oops, I forgot to check in new files after spliting a commit. > Fixed. > > This depends on: > https://patchwork.freedesktop.org/series/103715/ > > .../ABI/testing/sysfs-kernel-dmabuf-buffers | 14 +++++ > drivers/dma-buf/Makefile | 2 +- > drivers/dma-buf/dma-buf-sysfs-caps.c | 51 +++++++++++++++++++ > drivers/dma-buf/dma-buf-sysfs-caps.h | 16 ++++++ > drivers/dma-buf/dma-buf-sysfs-stats.c | 13 +---- > drivers/dma-buf/dma-buf-sysfs-stats.h | 6 ++- > drivers/dma-buf/dma-buf.c | 43 ++++++++++++++-- > include/uapi/linux/dma-buf.h | 6 +++ > 8 files changed, 133 insertions(+), 18 deletions(-) > create mode 100644 drivers/dma-buf/dma-buf-sysfs-caps.c > create mode 100644 drivers/dma-buf/dma-buf-sysfs-caps.h > > diff --git a/Documentation/ABI/testing/sysfs-kernel-dmabuf-buffers > b/Documentation/ABI/testing/sysfs-kernel-dmabuf-buffers > index 5d3bc997dc64..682d313689d8 100644 > --- a/Documentation/ABI/testing/sysfs-kernel-dmabuf-buffers > +++ b/Documentation/ABI/testing/sysfs-kernel-dmabuf-buffers > @@ -22,3 +22,17 @@ KernelVersion: v5.13 > Contact: Hridya Valsaraju <hri...@google.com> > Description: This file is read-only and specifies the size of the > DMA-BUF in > bytes. > + > +What: /sys/kernel/dmabuf/caps > +Date: May 2022 > +KernelVersion: v5.19 > +Contact: Simon Ser <cont...@emersion.fr> > +Description: This directory advertises DMA-BUF capabilities supported > by the > + kernel. > + > +What: /sys/kernel/dmabuf/caps/sync_file_import_export > +Date: May 2022 > +KernelVersion: v5.19 > +Contact: Simon Ser <cont...@emersion.fr> > +Description: This file is read-only and advertises support for > importing and > + exporting sync_files from/to DMA-BUFs. > diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile > index 4c9eb53ba3f8..afc874272710 100644 > --- a/drivers/dma-buf/Makefile > +++ b/drivers/dma-buf/Makefile > @@ -1,6 +1,6 @@ > # SPDX-License-Identifier: GPL-2.0-only > obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \ > - dma-resv.o > + dma-resv.o dma-buf-sysfs-caps.o > obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o > obj-$(CONFIG_DMABUF_HEAPS) += heaps/ > obj-$(CONFIG_SYNC_FILE) += sync_file.o > diff --git a/drivers/dma-buf/dma-buf-sysfs-caps.c > b/drivers/dma-buf/dma-buf-sysfs-caps.c > new file mode 100644 > index 000000000000..c760e55353bc > --- /dev/null > +++ b/drivers/dma-buf/dma-buf-sysfs-caps.c > @@ -0,0 +1,51 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > +/* > + * DMA-BUF sysfs capabilities. > + * > + * Copyright (C) 2022 Simon Ser > + */ > + > +#include <linux/kobject.h> > +#include <linux/sysfs.h> > + > +#include "dma-buf-sysfs-caps.h" > + > +static ssize_t sync_file_import_export_show(struct kobject *kobj, > + struct kobj_attribute *attr, > + char *buf) > +{ > + return sysfs_emit(buf, "1\n"); > +} > + > +static struct kobj_attribute dma_buf_sync_file_import_export_attr = > + __ATTR_RO(sync_file_import_export); > + > +static struct attribute *dma_buf_caps_attrs[] = { > + &dma_buf_sync_file_import_export_attr.attr, > + NULL, > +}; > + > +static const struct attribute_group dma_buf_caps_attr_group = { > + .attrs = dma_buf_caps_attrs, > +}; > + > +static struct kobject *dma_buf_caps_kobj; > + > +int dma_buf_init_sysfs_capabilities(struct kset *kset) > +{ > + int ret; > + > + dma_buf_caps_kobj = kobject_create_and_add("caps", &kset->kobj); > + if (!dma_buf_caps_kobj) > + return -ENOMEM; > + > + ret = sysfs_create_group(dma_buf_caps_kobj, > &dma_buf_caps_attr_group); > + if (ret) > + kobject_put(dma_buf_caps_kobj); > + return ret; > +} > + > +void dma_buf_uninit_sysfs_capabilities(void) > +{ > + kobject_put(dma_buf_caps_kobj); > +} > diff --git a/drivers/dma-buf/dma-buf-sysfs-caps.h > b/drivers/dma-buf/dma-buf-sysfs-caps.h > new file mode 100644 > index 000000000000..d7bcef490b31 > --- /dev/null > +++ b/drivers/dma-buf/dma-buf-sysfs-caps.h > @@ -0,0 +1,16 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > +/* > + * DMA-BUF sysfs capabilities. > + * > + * Copyright (C) 2022 Simon Ser > + */ > + > +#ifndef _DMA_BUF_SYSFS_CAPS_H > +#define _DMA_BUF_SYSFS_CAPS_H > + > +struct kset; > + > +int dma_buf_init_sysfs_capabilities(struct kset *kset); > +void dma_buf_uninit_sysfs_capabilities(void); > + > +#endif // _DMA_BUF_SYSFS_CAPS_H > diff --git a/drivers/dma-buf/dma-buf-sysfs-stats.c > b/drivers/dma-buf/dma-buf-sysfs-stats.c > index 2bba0babcb62..09e43c8891d6 100644 > --- a/drivers/dma-buf/dma-buf-sysfs-stats.c > +++ b/drivers/dma-buf/dma-buf-sysfs-stats.c > @@ -141,21 +141,13 @@ static const struct kset_uevent_ops > dmabuf_sysfs_no_uevent_ops = { > .filter = dmabuf_sysfs_uevent_filter, > }; > > -static struct kset *dma_buf_stats_kset; > static struct kset *dma_buf_per_buffer_stats_kset; > -int dma_buf_init_sysfs_statistics(void) > +int dma_buf_init_sysfs_statistics(struct kset *kset) > { > - dma_buf_stats_kset = kset_create_and_add("dmabuf", > - > &dmabuf_sysfs_no_uevent_ops, > - kernel_kobj); > - if (!dma_buf_stats_kset) > - return -ENOMEM; > - > dma_buf_per_buffer_stats_kset = kset_create_and_add("buffers", > > &dmabuf_sysfs_no_uevent_ops, > - > &dma_buf_stats_kset->kobj); > + &kset->kobj); > if (!dma_buf_per_buffer_stats_kset) { > You can drop the braces now. Otherwise, Reviewed-by: Jason Ekstrand <jason.ekstr...@collabora.com> I've updated my Mesa MR to use this so someone should go review that patch so we have userspace. I've tested everything and it works nicely together. > - kset_unregister(dma_buf_stats_kset); > return -ENOMEM; > } > > @@ -165,7 +157,6 @@ int dma_buf_init_sysfs_statistics(void) > void dma_buf_uninit_sysfs_statistics(void) > { > kset_unregister(dma_buf_per_buffer_stats_kset); > - kset_unregister(dma_buf_stats_kset); > } > > int dma_buf_stats_setup(struct dma_buf *dmabuf) > diff --git a/drivers/dma-buf/dma-buf-sysfs-stats.h > b/drivers/dma-buf/dma-buf-sysfs-stats.h > index a49c6e2650cc..798c54fb8ee3 100644 > --- a/drivers/dma-buf/dma-buf-sysfs-stats.h > +++ b/drivers/dma-buf/dma-buf-sysfs-stats.h > @@ -8,9 +8,11 @@ > #ifndef _DMA_BUF_SYSFS_STATS_H > #define _DMA_BUF_SYSFS_STATS_H > > +struct kset; > + > #ifdef CONFIG_DMABUF_SYSFS_STATS > > -int dma_buf_init_sysfs_statistics(void); > +int dma_buf_init_sysfs_statistics(struct kset *kset); > void dma_buf_uninit_sysfs_statistics(void); > > int dma_buf_stats_setup(struct dma_buf *dmabuf); > @@ -18,7 +20,7 @@ int dma_buf_stats_setup(struct dma_buf *dmabuf); > void dma_buf_stats_teardown(struct dma_buf *dmabuf); > #else > > -static inline int dma_buf_init_sysfs_statistics(void) > +static inline int dma_buf_init_sysfs_statistics(struct kset *kset) > { > return 0; > } > diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c > index 5e1b0534b3ce..b5c5a5050508 100644 > --- a/drivers/dma-buf/dma-buf.c > +++ b/drivers/dma-buf/dma-buf.c > @@ -30,6 +30,7 @@ > #include <uapi/linux/dma-buf.h> > #include <uapi/linux/magic.h> > > +#include "dma-buf-sysfs-caps.h" > #include "dma-buf-sysfs-stats.h" > > static inline int is_dma_buf_file(struct file *); > @@ -1546,22 +1547,54 @@ static inline void dma_buf_uninit_debugfs(void) > } > #endif > > +/* Capabilities and statistics files do not need to send uevents. */ > +static int dmabuf_sysfs_uevent_filter(struct kobject *kobj) > +{ > + return 0; > +} > + > +static const struct kset_uevent_ops dmabuf_sysfs_no_uevent_ops = { > + .filter = dmabuf_sysfs_uevent_filter, > +}; > + > +static struct kset *dma_buf_kset; > + > static int __init dma_buf_init(void) > { > int ret; > > - ret = dma_buf_init_sysfs_statistics(); > + dma_buf_kset = kset_create_and_add("dmabuf", > + &dmabuf_sysfs_no_uevent_ops, > + kernel_kobj); > + if (!dma_buf_kset) > + return -ENOMEM; > + > + ret = dma_buf_init_sysfs_capabilities(dma_buf_kset); > if (ret) > - return ret; > + goto err_kset; > + > + ret = dma_buf_init_sysfs_statistics(dma_buf_kset); > + if (ret) > + goto err_sysfs_caps; > > dma_buf_mnt = kern_mount(&dma_buf_fs_type); > - if (IS_ERR(dma_buf_mnt)) > - return PTR_ERR(dma_buf_mnt); > + if (IS_ERR(dma_buf_mnt)) { > + ret = PTR_ERR(dma_buf_mnt); > + goto err_sysfs_stats; > + } > > mutex_init(&db_list.lock); > INIT_LIST_HEAD(&db_list.head); > dma_buf_init_debugfs(); > return 0; > + > +err_sysfs_stats: > + dma_buf_uninit_sysfs_statistics(); > +err_sysfs_caps: > + dma_buf_uninit_sysfs_capabilities(); > +err_kset: > + kset_unregister(dma_buf_kset); > + return ret; > } > subsys_initcall(dma_buf_init); > > @@ -1570,5 +1603,7 @@ static void __exit dma_buf_deinit(void) > dma_buf_uninit_debugfs(); > kern_unmount(dma_buf_mnt); > dma_buf_uninit_sysfs_statistics(); > + dma_buf_uninit_sysfs_capabilities(); > + kset_unregister(dma_buf_kset); > } > __exitcall(dma_buf_deinit); > diff --git a/include/uapi/linux/dma-buf.h b/include/uapi/linux/dma-buf.h > index 70e213a0d7d9..ab3afd5da75a 100644 > --- a/include/uapi/linux/dma-buf.h > +++ b/include/uapi/linux/dma-buf.h > @@ -114,6 +114,9 @@ struct dma_buf_sync { > * ordering via these fences, it is the respnosibility of userspace to use > * locks or other mechanisms to ensure that no other context adds fences > or > * submits work between steps 1 and 3 above. > + * > + * Userspace can check the availability of this API via > + * /sys/kernel/dmabuf/caps/sync_file_import_export. > */ > struct dma_buf_export_sync_file { > /** > @@ -146,6 +149,9 @@ struct dma_buf_export_sync_file { > * synchronized APIs such as Vulkan to inter-op with dma-buf consumers > * which expect implicit synchronization such as OpenGL or most media > * drivers/video. > + * > + * Userspace can check the availability of this API via > + * /sys/kernel/dmabuf/caps/sync_file_import_export. > */ > struct dma_buf_import_sync_file { > /** > -- > 2.36.1 > > >