Skip to contentSkip to conversation

dma-buf: Add a capabilities directory

PATCH Superseded Simon Ser 4 participants 8 messages May 27, 2022 – May 31, 2022 DMA buffer sharing framework

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Simon Ser

author
#0 lore raw

PATCH v3 1/1 dma-buf: Add a capabilities directory

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.

v2: Add missing files lost in a rebase

v3:
- Create separate file in Documentation/ABI/testing/, add it to MAINTAINERS
- Fix kernel version (Daniel)
- Remove unnecessary brackets (Jason)
- Fix SPDX comment style

Cc Daniel Vetter <[email protected]>
Cc Bas Nieuwenhuizen <[email protected]>
Cc Christian König <[email protected]>
Cc Greg KH <[email protected]>
---
 .../ABI/testing/sysfs-kernel-dmabuf-caps      | 13 +++++
 MAINTAINERS                                   |  1 +
 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         | 16 ++----
 drivers/dma-buf/dma-buf-sysfs-stats.h         |  6 ++-
 drivers/dma-buf/dma-buf.c                     | 43 ++++++++++++++--
 include/uapi/linux/dma-buf.h                  |  6 +++
 9 files changed, 134 insertions(+), 20 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-kernel-dmabuf-caps
 create mode 100644 drivers/dma-buf/dma-buf-sysfs-caps.c
 create mode 100644 drivers/dma-buf/dma-buf-sysfs-caps.h
Documentation/ABI/testing/sysfs-kernel-dmabuf-caps new file +13 -0
new file mode 100644
index 000000000000..f83af422fd18
@@ -0,0 +1,13 @@
+What:		/sys/kernel/dmabuf/caps
+Date:		May 2022
+KernelVersion:	v5.20
+Contact:	Simon Ser <[email protected]>
+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.20
+Contact:	Simon Ser <[email protected]>
+Description:	This file is read-only and advertises support for importing and
+		exporting sync_files from/to DMA-BUFs.
MAINTAINERS +1 -0
index 11da16bfa123..8966686f7231 100644
@@ -5871,6 +5871,7 @@ L:	[email protected]
 L:	[email protected] (moderated for non-subscribers)
 S:	Maintained
 T:	git git://anongit.freedesktop.org/drm/drm-misc
+F:	Documentation/ABI/testing/sysfs-kernel-dmabuf-caps
 F:	Documentation/driver-api/dma-buf.rst
 F:	drivers/dma-buf/
 F:	include/linux/*fence.h
drivers/dma-buf/Makefile +1 -1
index 4c9eb53ba3f8..afc874272710 100644
@@ -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
drivers/dma-buf/dma-buf-sysfs-caps.c new file +51 -0

51 lines not on this page · raw message

drivers/dma-buf/dma-buf-sysfs-caps.h new file +16 -0
new file mode 100644
index 000000000000..d7bcef490b31
@@ -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
drivers/dma-buf/dma-buf-sysfs-stats.c +3 -13
index 2bba0babcb62..e2e62f83ce18 100644
@@ -141,23 +141,14 @@ 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);
-	if (!dma_buf_per_buffer_stats_kset) {
-		kset_unregister(dma_buf_stats_kset);
+							    &kset->kobj);
+	if (!dma_buf_per_buffer_stats_kset)
 		return -ENOMEM;
-	}
 
 	return 0;
 }
@@ -165,7 +156,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)
drivers/dma-buf/dma-buf-sysfs-stats.h +4 -2
index a49c6e2650cc..798c54fb8ee3 100644
@@ -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;
 }
drivers/dma-buf/dma-buf.c +39 -4

72 lines not on this page · raw message

include/uapi/linux/dma-buf.h +6 -0
index 70e213a0d7d9..ab3afd5da75a 100644
@@ -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 {
 	/**
Signature · 1 line
2.36.1
maintainer
#1 lore raw

Am 27.05.22 um 09:34 schrieb Simon Ser:

Quoted 10 lines

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.

I find a separate directory rather unusual, but can't come up with any better idea either.

IIRC the security module had a mask file with names for the supported capabilities.

Quoted 100 lines

v2: Add missing files lost in a rebase

v3:
- Create separate file in Documentation/ABI/testing/, add it to MAINTAINERS
- Fix kernel version (Daniel)
- Remove unnecessary brackets (Jason)
- Fix SPDX comment style

Cc Daniel Vetter <[email protected]>
Cc Bas Nieuwenhuizen <[email protected]>
Cc Christian König <[email protected]>
Cc Greg KH <[email protected]>
---
  .../ABI/testing/sysfs-kernel-dmabuf-caps      | 13 +++++
  MAINTAINERS                                   |  1 +
  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         | 16 ++----
  drivers/dma-buf/dma-buf-sysfs-stats.h         |  6 ++-
  drivers/dma-buf/dma-buf.c                     | 43 ++++++++++++++--
  include/uapi/linux/dma-buf.h                  |  6 +++
  9 files changed, 134 insertions(+), 20 deletions(-)

create mode 100644 Documentation/ABI/testing/sysfs-kernel-dmabuf-caps create mode 100644 drivers/dma-buf/dma-buf-sysfs-caps.c create mode 100644 drivers/dma-buf/dma-buf-sysfs-caps.h

Documentation/ABI/testing/sysfs-kernel-dmabuf-caps new file +13 -0
new file mode 100644
index 000000000000..f83af422fd18
@@ -0,0 +1,13 @@
+What:		/sys/kernel/dmabuf/caps
+Date:		May 2022
+KernelVersion:	v5.20
+Contact:	Simon Ser <[email protected]>
+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.20
+Contact:	Simon Ser <[email protected]>
+Description:	This file is read-only and advertises support for importing and
+		exporting sync_files from/to DMA-BUFs.
MAINTAINERS +1 -0
index 11da16bfa123..8966686f7231 100644
@@ -5871,6 +5871,7 @@ L:	[email protected]
  L:	[email protected] (moderated for non-subscribers)
  S:	Maintained
  T:	git git://anongit.freedesktop.org/drm/drm-misc
+F:	Documentation/ABI/testing/sysfs-kernel-dmabuf-caps
  F:	Documentation/driver-api/dma-buf.rst
  F:	drivers/dma-buf/
  F:	include/linux/*fence.h
drivers/dma-buf/Makefile +1 -1
index 4c9eb53ba3f8..afc874272710 100644
@@ -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
drivers/dma-buf/dma-buf-sysfs-caps.c new file +20 -0
new file mode 100644
index 000000000000..82b91eb874a9
@@ -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 =

drivers/dma-buf/dma-buf-sysfs-caps.c line 30

Quoted 10 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +10 -0
+	__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,
+};

Didn't we had macros for those? I think I have seen something for that.

Apart from those two random thoughts looks good to me as well.

Regards,
Christian.

Quoted 211 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +21 -0
+
+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);
+}
drivers/dma-buf/dma-buf-sysfs-caps.h new file +16 -0
new file mode 100644
index 000000000000..d7bcef490b31
@@ -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
drivers/dma-buf/dma-buf-sysfs-stats.c +3 -13
index 2bba0babcb62..e2e62f83ce18 100644
@@ -141,23 +141,14 @@ 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);
-	if (!dma_buf_per_buffer_stats_kset) {
-		kset_unregister(dma_buf_stats_kset);
+							    &kset->kobj);
+	if (!dma_buf_per_buffer_stats_kset)
  		return -ENOMEM;
-	}
 
  	return 0;
  }
@@ -165,7 +156,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)
drivers/dma-buf/dma-buf-sysfs-stats.h +4 -2
index a49c6e2650cc..798c54fb8ee3 100644
@@ -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;
  }
drivers/dma-buf/dma-buf.c +39 -4
index 5e1b0534b3ce..b5c5a5050508 100644
@@ -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);
include/uapi/linux/dma-buf.h +6 -0
index 70e213a0d7d9..ab3afd5da75a 100644
@@ -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 lore raw
Quoted 122 lines · Christian König

Am 27.05.22 um 09:34 schrieb Simon Ser:

Quoted 10 lines

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.

I find a separate directory rather unusual, but can't come up with any better idea either.

IIRC the security module had a mask file with names for the supported capabilities.

Quoted 104 lines

v2: Add missing files lost in a rebase

v3:
- Create separate file in Documentation/ABI/testing/, add it to MAINTAINERS
- Fix kernel version (Daniel)
- Remove unnecessary brackets (Jason)
- Fix SPDX comment style

Cc Daniel Vetter <[email protected]>
Cc Bas Nieuwenhuizen <[email protected]>
Cc Christian König <[email protected]>
Cc Greg KH <[email protected]>
---
  .../ABI/testing/sysfs-kernel-dmabuf-caps      | 13 +++++
  MAINTAINERS                                   |  1 +
  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         | 16 ++----
  drivers/dma-buf/dma-buf-sysfs-stats.h         |  6 ++-
  drivers/dma-buf/dma-buf.c                     | 43 ++++++++++++++--
  include/uapi/linux/dma-buf.h                  |  6 +++
  9 files changed, 134 insertions(+), 20 deletions(-)

create mode 100644 Documentation/ABI/testing/sysfs-kernel-dmabuf-caps create mode 100644 drivers/dma-buf/dma-buf-sysfs-caps.c create mode 100644 drivers/dma-buf/dma-buf-sysfs-caps.h

Documentation/ABI/testing/sysfs-kernel-dmabuf-caps new file +13 -0
new file mode 100644
index 000000000000..f83af422fd18
@@ -0,0 +1,13 @@
+What:		/sys/kernel/dmabuf/caps
+Date:		May 2022
+KernelVersion:	v5.20
+Contact:	Simon Ser <[email protected]>
+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.20
+Contact:	Simon Ser <[email protected]>
+Description:	This file is read-only and advertises support for importing and
+		exporting sync_files from/to DMA-BUFs.
MAINTAINERS +1 -0
index 11da16bfa123..8966686f7231 100644
@@ -5871,6 +5871,7 @@ L:	[email protected]
  L:	[email protected] (moderated for non-subscribers)
  S:	Maintained
  T:	git git://anongit.freedesktop.org/drm/drm-misc
+F:	Documentation/ABI/testing/sysfs-kernel-dmabuf-caps
  F:	Documentation/driver-api/dma-buf.rst
  F:	drivers/dma-buf/
  F:	include/linux/*fence.h
drivers/dma-buf/Makefile +1 -1
index 4c9eb53ba3f8..afc874272710 100644
@@ -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
drivers/dma-buf/dma-buf-sysfs-caps.c new file +24 -0
new file mode 100644
index 000000000000..82b91eb874a9
@@ -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,
Quoted 8 lines

drivers/dma-buf/dma-buf-sysfs-caps.c line 30

Quoted 6 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +6 -0
+	NULL,
+};
+
+static const struct attribute_group dma_buf_caps_attr_group = {
+	.attrs = dma_buf_caps_attrs,
+};

Didn't we had macros for those? I think I have seen something for that.

Yes, please use ATTRIBUTE_GROUPS()

Quoted 4 lines
Quoted 4 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +4 -0
+
+static struct kobject *dma_buf_caps_kobj;
+
+int dma_buf_init_sysfs_capabilities(struct kset *kset)
Quoted 8 lines

drivers/dma-buf/dma-buf-sysfs-caps.c line 42

Quoted 8 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +8 -0
+{
+	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);

Why do we have "raw" kobjects here?

A group can have a name, which puts it in the subdirectory of the object it is attached to. Please do that and do not create a new kobject.

thanks,

greg k-h

author
#3 lore raw
Quoted 11 lines · Greg KH
Quoted 10 lines

drivers/dma-buf/dma-buf-sysfs-caps.c line 30

Quoted 8 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +8 -0
+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,
+};

Didn't we had macros for those? I think I have seen something for that.

Quoted 1 line

Yes, please use ATTRIBUTE_GROUPS()

This doesn't allow the user to set a group name, and creates an unused "_groups" variable, causing warnings.

Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +4 -0
+
+static struct kobject *dma_buf_caps_kobj;
+
+int dma_buf_init_sysfs_capabilities(struct kset *kset)
Quoted 8 lines

drivers/dma-buf/dma-buf-sysfs-caps.c line 42

Quoted 8 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +8 -0
+{
+	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);

Why do we have "raw" kobjects here?

Quoted 2 lines

A group can have a name, which puts it in the subdirectory of the object it is attached to. Please do that and do not create a new kobject.

I see, I'll switch to sysfs_create_group with a group name in the next version.

Thanks for the pointers!

#4 lore raw
Quoted 13 lines · Simon Ser
Quoted 11 lines · Greg KH
Quoted 10 lines

drivers/dma-buf/dma-buf-sysfs-caps.c line 30

Quoted 8 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +8 -0
+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,
+};

Didn't we had macros for those? I think I have seen something for that.

Quoted 4 lines
Quoted 1 line

Yes, please use ATTRIBUTE_GROUPS()

This doesn't allow the user to set a group name, and creates an unused "_groups" variable, causing warnings.

Then set a group name.

But you really want to almost always be using lists of groups, which is why that macro works that way.

Quoted 16 lines
Quoted 16 lines
Quoted 13 lines

drivers/dma-buf/dma-buf-sysfs-caps.c line 42

Quoted 13 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +12 -0
+
+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);

Why do we have "raw" kobjects here?

Quoted 4 lines
Quoted 2 lines

A group can have a name, which puts it in the subdirectory of the object it is attached to. Please do that and do not create a new kobject.

I see, I'll switch to sysfs_create_group with a group name in the next version.

No, do not do that, add it to the list of groups for the original kobject.

thanks,

greg k-h

#5 lore raw
Quoted 23 lines · Greg KH
Quoted 16 lines · Simon Ser
Quoted 13 lines · Greg KH
Quoted 12 lines
Quoted 9 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +6 -0
+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,
+};

Didn't we had macros for those? I think I have seen something for that.

Quoted 5 lines
Quoted 1 line

Yes, please use ATTRIBUTE_GROUPS()

This doesn't allow the user to set a group name, and creates an unused
"_groups" variable, causing warnings.

Quoted 5 lines

Then set a group name.

But you really want to almost always be using lists of groups, which is
why that macro works that way.

I think I see the confusion here. The ATTRIBUTE_GROUPS() macro is intended for device drivers and to be used with add_device(). However, this is dma-buf so there is no device and no add_device() call to hook. Unless there are other magic macros to use in this case, I think we're stuck doing it manually.

--Jason

Quoted 32 lines
Quoted 18 lines
Quoted 18 lines
Quoted 15 lines
Quoted 9 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +8 -0
+
+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",
Quoted 6 lines

&kset->kobj);
+       if (!dma_buf_caps_kobj)
+               return -ENOMEM;
+
+       ret = sysfs_create_group(dma_buf_caps_kobj, &dma_buf_caps_attr_group);

Why do we have "raw" kobjects here?

Quoted 7 lines
Quoted 4 lines

A group can have a name, which puts it in the subdirectory of the object
it is attached to.  Please do that and do not create a new kobject.

I see, I'll switch to sysfs_create_group with a group name in the next version.

No, do not do that, add it to the list of groups for the original kobject.

thanks,

greg k-h

#6 lore raw
Quoted 30 lines · Jason Ekstrand
Quoted 23 lines · Greg KH
Quoted 16 lines · Simon Ser
Quoted 13 lines · Greg KH
Quoted 12 lines
Quoted 9 lines
drivers/dma-buf/dma-buf-sysfs-caps.c new file +6 -0
+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,
+};

Didn't we had macros for those? I think I have seen something for that.

Quoted 5 lines
Quoted 1 line

Yes, please use ATTRIBUTE_GROUPS()

This doesn't allow the user to set a group name, and creates an unused
"_groups" variable, causing warnings.

Quoted 5 lines

Then set a group name.

But you really want to almost always be using lists of groups, which is
why that macro works that way.

Quoted 5 lines

I think I see the confusion here. The ATTRIBUTE_GROUPS() macro is intended for device drivers and to be used with add_device(). However, this is dma-buf so there is no device and no add_device() call to hook. Unless there are other magic macros to use in this case, I think we're stuck doing it manually.

Have a list of attribute groups and add it to the kobject when you create it so they all get created at the same time.

Don't do piece-meal "add one, and then another, and then another" as that just gets messy and complex and impossible to unwind the error conditions from.

sysfs_create_groups() is what you need to use here. I need to drop sysfs_create_group() one day...

thanks,

greg k-h

author
#7 lore raw
In reply to Christian König
Quoted 5 lines · Christian König

I find a separate directory rather unusual, but can't come up with any better idea either.

IIRC the security module had a mask file with names for the supported capabilities.

Are you referring to /sys/kernel/security/lsm?

This sounds more painful to parse from user-space. Instead of a simple stat(), user-space would need to read the file, split on commas, and compare strings.