dma-buf-sysfs-stats.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * DMA-BUF sysfs statistics.
  4. *
  5. * Copyright (C) 2021 Google LLC.
  6. */
  7. #include <linux/dma-buf.h>
  8. #include <linux/dma-resv.h>
  9. #include <linux/kobject.h>
  10. #include <linux/printk.h>
  11. #include <linux/slab.h>
  12. #include <linux/sysfs.h>
  13. #include "dma-buf-sysfs-stats.h"
  14. #define to_dma_buf_entry_from_kobj(x) container_of(x, struct dma_buf_sysfs_entry, kobj)
  15. /**
  16. * DOC: overview
  17. *
  18. * ``/sys/kernel/debug/dma_buf/bufinfo`` provides an overview of every DMA-BUF
  19. * in the system. However, since debugfs is not safe to be mounted in
  20. * production, procfs and sysfs can be used to gather DMA-BUF statistics on
  21. * production systems.
  22. *
  23. * The ``/proc/<pid>/fdinfo/<fd>`` files in procfs can be used to gather
  24. * information about DMA-BUF fds. Detailed documentation about the interface
  25. * is present in Documentation/filesystems/proc.rst.
  26. *
  27. * Unfortunately, the existing procfs interfaces can only provide information
  28. * about the DMA-BUFs for which processes hold fds or have the buffers mmapped
  29. * into their address space. This necessitated the creation of the DMA-BUF sysfs
  30. * statistics interface to provide per-buffer information on production systems.
  31. *
  32. * The interface at ``/sys/kernel/dmabuf/buffers`` exposes information about
  33. * every DMA-BUF when ``CONFIG_DMABUF_SYSFS_STATS`` is enabled.
  34. *
  35. * The following stats are exposed by the interface:
  36. *
  37. * * ``/sys/kernel/dmabuf/buffers/<inode_number>/exporter_name``
  38. * * ``/sys/kernel/dmabuf/buffers/<inode_number>/size``
  39. *
  40. * The information in the interface can also be used to derive per-exporter
  41. * statistics. The data from the interface can be gathered on error conditions
  42. * or other important events to provide a snapshot of DMA-BUF usage.
  43. * It can also be collected periodically by telemetry to monitor various metrics.
  44. *
  45. * Detailed documentation about the interface is present in
  46. * Documentation/ABI/testing/sysfs-kernel-dmabuf-buffers.
  47. */
  48. struct dma_buf_stats_attribute {
  49. struct attribute attr;
  50. ssize_t (*show)(struct dma_buf *dmabuf,
  51. struct dma_buf_stats_attribute *attr, char *buf);
  52. };
  53. #define to_dma_buf_stats_attr(x) container_of(x, struct dma_buf_stats_attribute, attr)
  54. static ssize_t dma_buf_stats_attribute_show(struct kobject *kobj,
  55. struct attribute *attr,
  56. char *buf)
  57. {
  58. struct dma_buf_stats_attribute *attribute;
  59. struct dma_buf_sysfs_entry *sysfs_entry;
  60. struct dma_buf *dmabuf;
  61. attribute = to_dma_buf_stats_attr(attr);
  62. sysfs_entry = to_dma_buf_entry_from_kobj(kobj);
  63. dmabuf = sysfs_entry->dmabuf;
  64. if (!dmabuf || !attribute->show)
  65. return -EIO;
  66. return attribute->show(dmabuf, attribute, buf);
  67. }
  68. static const struct sysfs_ops dma_buf_stats_sysfs_ops = {
  69. .show = dma_buf_stats_attribute_show,
  70. };
  71. static ssize_t exporter_name_show(struct dma_buf *dmabuf,
  72. struct dma_buf_stats_attribute *attr,
  73. char *buf)
  74. {
  75. return sysfs_emit(buf, "%s\n", dmabuf->exp_name);
  76. }
  77. static ssize_t size_show(struct dma_buf *dmabuf,
  78. struct dma_buf_stats_attribute *attr,
  79. char *buf)
  80. {
  81. return sysfs_emit(buf, "%zu\n", dmabuf->size);
  82. }
  83. static struct dma_buf_stats_attribute exporter_name_attribute =
  84. __ATTR_RO(exporter_name);
  85. static struct dma_buf_stats_attribute size_attribute = __ATTR_RO(size);
  86. static struct attribute *dma_buf_stats_default_attrs[] = {
  87. &exporter_name_attribute.attr,
  88. &size_attribute.attr,
  89. NULL,
  90. };
  91. ATTRIBUTE_GROUPS(dma_buf_stats_default);
  92. static void dma_buf_sysfs_release(struct kobject *kobj)
  93. {
  94. struct dma_buf_sysfs_entry *sysfs_entry;
  95. sysfs_entry = to_dma_buf_entry_from_kobj(kobj);
  96. kfree(sysfs_entry);
  97. }
  98. static const struct kobj_type dma_buf_ktype = {
  99. .sysfs_ops = &dma_buf_stats_sysfs_ops,
  100. .release = dma_buf_sysfs_release,
  101. .default_groups = dma_buf_stats_default_groups,
  102. };
  103. void dma_buf_stats_teardown(struct dma_buf *dmabuf)
  104. {
  105. struct dma_buf_sysfs_entry *sysfs_entry;
  106. sysfs_entry = dmabuf->sysfs_entry;
  107. if (!sysfs_entry)
  108. return;
  109. kobject_del(&sysfs_entry->kobj);
  110. kobject_put(&sysfs_entry->kobj);
  111. }
  112. /* Statistics files do not need to send uevents. */
  113. static int dmabuf_sysfs_uevent_filter(const struct kobject *kobj)
  114. {
  115. return 0;
  116. }
  117. static const struct kset_uevent_ops dmabuf_sysfs_no_uevent_ops = {
  118. .filter = dmabuf_sysfs_uevent_filter,
  119. };
  120. static struct kset *dma_buf_stats_kset;
  121. static struct kset *dma_buf_per_buffer_stats_kset;
  122. int dma_buf_init_sysfs_statistics(void)
  123. {
  124. dma_buf_stats_kset = kset_create_and_add("dmabuf",
  125. &dmabuf_sysfs_no_uevent_ops,
  126. kernel_kobj);
  127. if (!dma_buf_stats_kset)
  128. return -ENOMEM;
  129. dma_buf_per_buffer_stats_kset = kset_create_and_add("buffers",
  130. &dmabuf_sysfs_no_uevent_ops,
  131. &dma_buf_stats_kset->kobj);
  132. if (!dma_buf_per_buffer_stats_kset) {
  133. kset_unregister(dma_buf_stats_kset);
  134. return -ENOMEM;
  135. }
  136. return 0;
  137. }
  138. void dma_buf_uninit_sysfs_statistics(void)
  139. {
  140. kset_unregister(dma_buf_per_buffer_stats_kset);
  141. kset_unregister(dma_buf_stats_kset);
  142. }
  143. int dma_buf_stats_setup(struct dma_buf *dmabuf, struct file *file)
  144. {
  145. struct dma_buf_sysfs_entry *sysfs_entry;
  146. int ret;
  147. if (!dmabuf->exp_name) {
  148. pr_err("exporter name must not be empty if stats needed\n");
  149. return -EINVAL;
  150. }
  151. sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry), GFP_KERNEL);
  152. if (!sysfs_entry)
  153. return -ENOMEM;
  154. sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
  155. sysfs_entry->dmabuf = dmabuf;
  156. dmabuf->sysfs_entry = sysfs_entry;
  157. /* create the directory for buffer stats */
  158. ret = kobject_init_and_add(&sysfs_entry->kobj, &dma_buf_ktype, NULL,
  159. "%lu", file_inode(file)->i_ino);
  160. if (ret)
  161. goto err_sysfs_dmabuf;
  162. return 0;
  163. err_sysfs_dmabuf:
  164. kobject_put(&sysfs_entry->kobj);
  165. dmabuf->sysfs_entry = NULL;
  166. return ret;
  167. }