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