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