dma-buf-sysfs-stats.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <linux/workqueue.h>
  14. #include "dma-buf-sysfs-stats.h"
  15. #define to_dma_buf_entry_from_kobj(x) container_of(x, struct dma_buf_sysfs_entry, kobj)
  16. /**
  17. * DOC: overview
  18. *
  19. * ``/sys/kernel/debug/dma_buf/bufinfo`` provides an overview of every DMA-BUF
  20. * in the system. However, since debugfs is not safe to be mounted in
  21. * production, procfs and sysfs can be used to gather DMA-BUF statistics on
  22. * production systems.
  23. *
  24. * The ``/proc/<pid>/fdinfo/<fd>`` files in procfs can be used to gather
  25. * information about DMA-BUF fds. Detailed documentation about the interface
  26. * is present in Documentation/filesystems/proc.rst.
  27. *
  28. * Unfortunately, the existing procfs interfaces can only provide information
  29. * about the DMA-BUFs for which processes hold fds or have the buffers mmapped
  30. * into their address space. This necessitated the creation of the DMA-BUF sysfs
  31. * statistics interface to provide per-buffer information on production systems.
  32. *
  33. * The interface at ``/sys/kernel/dma-buf/buffers`` exposes information about
  34. * every DMA-BUF when ``CONFIG_DMABUF_SYSFS_STATS`` is enabled.
  35. *
  36. * The following stats are exposed by the interface:
  37. *
  38. * * ``/sys/kernel/dmabuf/buffers/<inode_number>/exporter_name``
  39. * * ``/sys/kernel/dmabuf/buffers/<inode_number>/size``
  40. *
  41. * The information in the interface can also be used to derive per-exporter
  42. * statistics. The data from the interface can be gathered on error conditions
  43. * or other important events to provide a snapshot of DMA-BUF usage.
  44. * It can also be collected periodically by telemetry to monitor various metrics.
  45. *
  46. * Detailed documentation about the interface is present in
  47. * Documentation/ABI/testing/sysfs-kernel-dmabuf-buffers.
  48. */
  49. struct dma_buf_stats_attribute {
  50. struct attribute attr;
  51. ssize_t (*show)(struct dma_buf *dmabuf,
  52. struct dma_buf_stats_attribute *attr, char *buf);
  53. };
  54. #define to_dma_buf_stats_attr(x) container_of(x, struct dma_buf_stats_attribute, attr)
  55. static ssize_t dma_buf_stats_attribute_show(struct kobject *kobj,
  56. struct attribute *attr,
  57. char *buf)
  58. {
  59. struct dma_buf_stats_attribute *attribute;
  60. struct dma_buf_sysfs_entry *sysfs_entry;
  61. struct dma_buf *dmabuf;
  62. attribute = to_dma_buf_stats_attr(attr);
  63. sysfs_entry = to_dma_buf_entry_from_kobj(kobj);
  64. dmabuf = sysfs_entry->dmabuf;
  65. if (!dmabuf || !attribute->show)
  66. return -EIO;
  67. return attribute->show(dmabuf, attribute, buf);
  68. }
  69. static const struct sysfs_ops dma_buf_stats_sysfs_ops = {
  70. .show = dma_buf_stats_attribute_show,
  71. };
  72. static ssize_t exporter_name_show(struct dma_buf *dmabuf,
  73. struct dma_buf_stats_attribute *attr,
  74. char *buf)
  75. {
  76. return sysfs_emit(buf, "%s\n", dmabuf->exp_name);
  77. }
  78. static ssize_t size_show(struct dma_buf *dmabuf,
  79. struct dma_buf_stats_attribute *attr,
  80. char *buf)
  81. {
  82. return sysfs_emit(buf, "%zu\n", dmabuf->size);
  83. }
  84. static struct dma_buf_stats_attribute exporter_name_attribute =
  85. __ATTR_RO(exporter_name);
  86. static struct dma_buf_stats_attribute size_attribute = __ATTR_RO(size);
  87. static struct attribute *dma_buf_stats_default_attrs[] = {
  88. &exporter_name_attribute.attr,
  89. &size_attribute.attr,
  90. NULL,
  91. };
  92. ATTRIBUTE_GROUPS(dma_buf_stats_default);
  93. static void dma_buf_sysfs_release(struct kobject *kobj)
  94. {
  95. struct dma_buf_sysfs_entry *sysfs_entry;
  96. sysfs_entry = to_dma_buf_entry_from_kobj(kobj);
  97. kfree(sysfs_entry);
  98. }
  99. static struct kobj_type dma_buf_ktype = {
  100. .sysfs_ops = &dma_buf_stats_sysfs_ops,
  101. .release = dma_buf_sysfs_release,
  102. .default_groups = dma_buf_stats_default_groups,
  103. };
  104. void dma_buf_stats_teardown(struct dma_buf *dmabuf)
  105. {
  106. struct dma_buf_sysfs_entry *sysfs_entry;
  107. sysfs_entry = dmabuf->sysfs_entry;
  108. if (!sysfs_entry)
  109. return;
  110. kobject_del(&sysfs_entry->kobj);
  111. kobject_put(&sysfs_entry->kobj);
  112. }
  113. /* Statistics files do not need to send uevents. */
  114. static int dmabuf_sysfs_uevent_filter(struct kobject *kobj)
  115. {
  116. return 0;
  117. }
  118. static const struct kset_uevent_ops dmabuf_sysfs_no_uevent_ops = {
  119. .filter = dmabuf_sysfs_uevent_filter,
  120. };
  121. static struct kset *dma_buf_stats_kset;
  122. static struct kset *dma_buf_per_buffer_stats_kset;
  123. int dma_buf_init_sysfs_statistics(void)
  124. {
  125. dma_buf_stats_kset = kset_create_and_add("dmabuf",
  126. &dmabuf_sysfs_no_uevent_ops,
  127. kernel_kobj);
  128. if (!dma_buf_stats_kset)
  129. return -ENOMEM;
  130. dma_buf_per_buffer_stats_kset = kset_create_and_add("buffers",
  131. &dmabuf_sysfs_no_uevent_ops,
  132. &dma_buf_stats_kset->kobj);
  133. if (!dma_buf_per_buffer_stats_kset) {
  134. kset_unregister(dma_buf_stats_kset);
  135. return -ENOMEM;
  136. }
  137. return 0;
  138. }
  139. void dma_buf_uninit_sysfs_statistics(void)
  140. {
  141. kset_unregister(dma_buf_per_buffer_stats_kset);
  142. kset_unregister(dma_buf_stats_kset);
  143. }
  144. struct dma_buf_create_sysfs_entry {
  145. struct dma_buf *dmabuf;
  146. struct work_struct work;
  147. };
  148. union dma_buf_create_sysfs_work_entry {
  149. struct dma_buf_create_sysfs_entry create_entry;
  150. struct dma_buf_sysfs_entry sysfs_entry;
  151. };
  152. static void sysfs_add_workfn(struct work_struct *work)
  153. {
  154. struct dma_buf_create_sysfs_entry *create_entry =
  155. container_of(work, struct dma_buf_create_sysfs_entry, work);
  156. struct dma_buf *dmabuf = create_entry->dmabuf;
  157. /*
  158. * A dmabuf is ref-counted via its file member. If this handler holds the only
  159. * reference to the dmabuf, there is no need for sysfs kobject creation. This is an
  160. * optimization and a race; when the reference count drops to 1 immediately after
  161. * this check it is not harmful as the sysfs entry will still get cleaned up in
  162. * dma_buf_stats_teardown, which won't get called until the final dmabuf reference
  163. * is released, and that can't happen until the end of this function.
  164. */
  165. if (file_count(dmabuf->file) > 1) {
  166. dmabuf->sysfs_entry->dmabuf = dmabuf;
  167. /*
  168. * kobject_init_and_add expects kobject to be zero-filled, but we have populated it
  169. * (the sysfs_add_work union member) to trigger this work function.
  170. */
  171. memset(&dmabuf->sysfs_entry->kobj, 0, sizeof(dmabuf->sysfs_entry->kobj));
  172. dmabuf->sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
  173. if (kobject_init_and_add(&dmabuf->sysfs_entry->kobj, &dma_buf_ktype, NULL,
  174. "%lu", file_inode(dmabuf->file)->i_ino)) {
  175. kobject_put(&dmabuf->sysfs_entry->kobj);
  176. dmabuf->sysfs_entry = NULL;
  177. }
  178. } else {
  179. /*
  180. * Free the sysfs_entry and reset the pointer so dma_buf_stats_teardown doesn't
  181. * attempt to operate on it.
  182. */
  183. kfree(dmabuf->sysfs_entry);
  184. dmabuf->sysfs_entry = NULL;
  185. }
  186. dma_buf_put(dmabuf);
  187. }
  188. int dma_buf_stats_setup(struct dma_buf *dmabuf, struct file *file)
  189. {
  190. struct dma_buf_create_sysfs_entry *create_entry;
  191. union dma_buf_create_sysfs_work_entry *work_entry;
  192. if (!dmabuf->exp_name) {
  193. pr_err("exporter name must not be empty if stats needed\n");
  194. return -EINVAL;
  195. }
  196. work_entry = kmalloc(sizeof(union dma_buf_create_sysfs_work_entry), GFP_KERNEL);
  197. if (!work_entry)
  198. return -ENOMEM;
  199. dmabuf->sysfs_entry = &work_entry->sysfs_entry;
  200. create_entry = &work_entry->create_entry;
  201. create_entry->dmabuf = dmabuf;
  202. INIT_WORK(&create_entry->work, sysfs_add_workfn);
  203. get_dma_buf(dmabuf); /* This reference will be dropped in sysfs_add_workfn. */
  204. schedule_work(&create_entry->work);
  205. return 0;
  206. }