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