• 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 
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/dma-buf/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 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(struct kobject * kobj)136 static int dmabuf_sysfs_uevent_filter(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 
172 struct dma_buf_create_sysfs_entry {
173 	struct dma_buf *dmabuf;
174 	struct work_struct work;
175 };
176 
177 union dma_buf_create_sysfs_work_entry {
178 	struct dma_buf_create_sysfs_entry create_entry;
179 	struct dma_buf_sysfs_entry sysfs_entry;
180 };
181 
sysfs_add_workfn(struct work_struct * work)182 static void sysfs_add_workfn(struct work_struct *work)
183 {
184 	struct dma_buf_create_sysfs_entry *create_entry =
185 		container_of(work, struct dma_buf_create_sysfs_entry, work);
186 	struct dma_buf *dmabuf = create_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 		dmabuf->sysfs_entry->dmabuf = dmabuf;
198 		/*
199 		 * kobject_init_and_add expects kobject to be zero-filled, but we have populated it
200 		 * (the sysfs_add_work union member) to trigger this work function.
201 		 */
202 		memset(&dmabuf->sysfs_entry->kobj, 0, sizeof(dmabuf->sysfs_entry->kobj));
203 		dmabuf->sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
204 		if (kobject_init_and_add(&dmabuf->sysfs_entry->kobj, &dma_buf_ktype, NULL,
205 						"%lu", file_inode(dmabuf->file)->i_ino)) {
206 			kobject_put(&dmabuf->sysfs_entry->kobj);
207 			dmabuf->sysfs_entry = NULL;
208 		}
209 	} else {
210 		/*
211 		 * Free the sysfs_entry and reset the pointer so dma_buf_stats_teardown doesn't
212 		 * attempt to operate on it.
213 		 */
214 		kfree(dmabuf->sysfs_entry);
215 		dmabuf->sysfs_entry = NULL;
216 	}
217 	dma_buf_put(dmabuf);
218 }
219 
dma_buf_stats_setup(struct dma_buf * dmabuf,struct file * file)220 int dma_buf_stats_setup(struct dma_buf *dmabuf, struct file *file)
221 {
222 	struct dma_buf_create_sysfs_entry *create_entry;
223 	union dma_buf_create_sysfs_work_entry *work_entry;
224 
225 	if (!dmabuf->exp_name) {
226 		pr_err("exporter name must not be empty if stats needed\n");
227 		return -EINVAL;
228 	}
229 
230 	work_entry = kmalloc(sizeof(union dma_buf_create_sysfs_work_entry), GFP_KERNEL);
231 	if (!work_entry)
232 		return -ENOMEM;
233 
234 	dmabuf->sysfs_entry = &work_entry->sysfs_entry;
235 
236 	create_entry = &work_entry->create_entry;
237 	create_entry->dmabuf = dmabuf;
238 
239 	INIT_WORK(&create_entry->work, sysfs_add_workfn);
240 	get_dma_buf(dmabuf); /* This reference will be dropped in sysfs_add_workfn. */
241 	schedule_work(&create_entry->work);
242 
243 	return 0;
244 }
245