18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * DMA-BUF sysfs statistics.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2021 Google LLC.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/dma-buf.h>
98c2ecf20Sopenharmony_ci#include <linux/dma-resv.h>
108c2ecf20Sopenharmony_ci#include <linux/kobject.h>
118c2ecf20Sopenharmony_ci#include <linux/printk.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/sysfs.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "dma-buf-sysfs-stats.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define to_dma_buf_entry_from_kobj(x) container_of(x, struct dma_buf_sysfs_entry, kobj)
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/**
208c2ecf20Sopenharmony_ci * DOC: overview
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * ``/sys/kernel/debug/dma_buf/bufinfo`` provides an overview of every DMA-BUF
238c2ecf20Sopenharmony_ci * in the system. However, since debugfs is not safe to be mounted in
248c2ecf20Sopenharmony_ci * production, procfs and sysfs can be used to gather DMA-BUF statistics on
258c2ecf20Sopenharmony_ci * production systems.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * The ``/proc/<pid>/fdinfo/<fd>`` files in procfs can be used to gather
288c2ecf20Sopenharmony_ci * information about DMA-BUF fds. Detailed documentation about the interface
298c2ecf20Sopenharmony_ci * is present in Documentation/filesystems/proc.rst.
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * Unfortunately, the existing procfs interfaces can only provide information
328c2ecf20Sopenharmony_ci * about the DMA-BUFs for which processes hold fds or have the buffers mmapped
338c2ecf20Sopenharmony_ci * into their address space. This necessitated the creation of the DMA-BUF sysfs
348c2ecf20Sopenharmony_ci * statistics interface to provide per-buffer information on production systems.
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * The interface at ``/sys/kernel/dma-buf/buffers`` exposes information about
378c2ecf20Sopenharmony_ci * every DMA-BUF when ``CONFIG_DMABUF_SYSFS_STATS`` is enabled.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * The following stats are exposed by the interface:
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * * ``/sys/kernel/dmabuf/buffers/<inode_number>/exporter_name``
428c2ecf20Sopenharmony_ci * * ``/sys/kernel/dmabuf/buffers/<inode_number>/size``
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * The information in the interface can also be used to derive per-exporter
458c2ecf20Sopenharmony_ci * statistics. The data from the interface can be gathered on error conditions
468c2ecf20Sopenharmony_ci * or other important events to provide a snapshot of DMA-BUF usage.
478c2ecf20Sopenharmony_ci * It can also be collected periodically by telemetry to monitor various metrics.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * Detailed documentation about the interface is present in
508c2ecf20Sopenharmony_ci * Documentation/ABI/testing/sysfs-kernel-dmabuf-buffers.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistruct dma_buf_stats_attribute {
548c2ecf20Sopenharmony_ci	struct attribute attr;
558c2ecf20Sopenharmony_ci	ssize_t (*show)(struct dma_buf *dmabuf,
568c2ecf20Sopenharmony_ci			struct dma_buf_stats_attribute *attr, char *buf);
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci#define to_dma_buf_stats_attr(x) container_of(x, struct dma_buf_stats_attribute, attr)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic ssize_t dma_buf_stats_attribute_show(struct kobject *kobj,
618c2ecf20Sopenharmony_ci					    struct attribute *attr,
628c2ecf20Sopenharmony_ci					    char *buf)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct dma_buf_stats_attribute *attribute;
658c2ecf20Sopenharmony_ci	struct dma_buf_sysfs_entry *sysfs_entry;
668c2ecf20Sopenharmony_ci	struct dma_buf *dmabuf;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	attribute = to_dma_buf_stats_attr(attr);
698c2ecf20Sopenharmony_ci	sysfs_entry = to_dma_buf_entry_from_kobj(kobj);
708c2ecf20Sopenharmony_ci	dmabuf = sysfs_entry->dmabuf;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (!dmabuf || !attribute->show)
738c2ecf20Sopenharmony_ci		return -EIO;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return attribute->show(dmabuf, attribute, buf);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic const struct sysfs_ops dma_buf_stats_sysfs_ops = {
798c2ecf20Sopenharmony_ci	.show = dma_buf_stats_attribute_show,
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic ssize_t exporter_name_show(struct dma_buf *dmabuf,
838c2ecf20Sopenharmony_ci				  struct dma_buf_stats_attribute *attr,
848c2ecf20Sopenharmony_ci				  char *buf)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	return sysfs_emit(buf, "%s\n", dmabuf->exp_name);
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic ssize_t size_show(struct dma_buf *dmabuf,
908c2ecf20Sopenharmony_ci			 struct dma_buf_stats_attribute *attr,
918c2ecf20Sopenharmony_ci			 char *buf)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	return sysfs_emit(buf, "%zu\n", dmabuf->size);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic struct dma_buf_stats_attribute exporter_name_attribute =
978c2ecf20Sopenharmony_ci	__ATTR_RO(exporter_name);
988c2ecf20Sopenharmony_cistatic struct dma_buf_stats_attribute size_attribute = __ATTR_RO(size);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic struct attribute *dma_buf_stats_default_attrs[] = {
1018c2ecf20Sopenharmony_ci	&exporter_name_attribute.attr,
1028c2ecf20Sopenharmony_ci	&size_attribute.attr,
1038c2ecf20Sopenharmony_ci	NULL,
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(dma_buf_stats_default);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic void dma_buf_sysfs_release(struct kobject *kobj)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct dma_buf_sysfs_entry *sysfs_entry;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	sysfs_entry = to_dma_buf_entry_from_kobj(kobj);
1128c2ecf20Sopenharmony_ci	kfree(sysfs_entry);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic struct kobj_type dma_buf_ktype = {
1168c2ecf20Sopenharmony_ci	.sysfs_ops = &dma_buf_stats_sysfs_ops,
1178c2ecf20Sopenharmony_ci	.release = dma_buf_sysfs_release,
1188c2ecf20Sopenharmony_ci	.default_groups = dma_buf_stats_default_groups,
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_civoid dma_buf_stats_teardown(struct dma_buf *dmabuf)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct dma_buf_sysfs_entry *sysfs_entry;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	sysfs_entry = dmabuf->sysfs_entry;
1268c2ecf20Sopenharmony_ci	if (!sysfs_entry)
1278c2ecf20Sopenharmony_ci		return;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	kobject_del(&sysfs_entry->kobj);
1308c2ecf20Sopenharmony_ci	kobject_put(&sysfs_entry->kobj);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* Statistics files do not need to send uevents. */
1358c2ecf20Sopenharmony_cistatic int dmabuf_sysfs_uevent_filter(struct kset *kset, struct kobject *kobj)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	return 0;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic const struct kset_uevent_ops dmabuf_sysfs_no_uevent_ops = {
1418c2ecf20Sopenharmony_ci	.filter = dmabuf_sysfs_uevent_filter,
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic struct kset *dma_buf_stats_kset;
1458c2ecf20Sopenharmony_cistatic struct kset *dma_buf_per_buffer_stats_kset;
1468c2ecf20Sopenharmony_ciint dma_buf_init_sysfs_statistics(void)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	dma_buf_stats_kset = kset_create_and_add("dmabuf",
1498c2ecf20Sopenharmony_ci						 &dmabuf_sysfs_no_uevent_ops,
1508c2ecf20Sopenharmony_ci						 kernel_kobj);
1518c2ecf20Sopenharmony_ci	if (!dma_buf_stats_kset)
1528c2ecf20Sopenharmony_ci		return -ENOMEM;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	dma_buf_per_buffer_stats_kset = kset_create_and_add("buffers",
1558c2ecf20Sopenharmony_ci							    &dmabuf_sysfs_no_uevent_ops,
1568c2ecf20Sopenharmony_ci							    &dma_buf_stats_kset->kobj);
1578c2ecf20Sopenharmony_ci	if (!dma_buf_per_buffer_stats_kset) {
1588c2ecf20Sopenharmony_ci		kset_unregister(dma_buf_stats_kset);
1598c2ecf20Sopenharmony_ci		return -ENOMEM;
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return 0;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_civoid dma_buf_uninit_sysfs_statistics(void)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	kset_unregister(dma_buf_per_buffer_stats_kset);
1688c2ecf20Sopenharmony_ci	kset_unregister(dma_buf_stats_kset);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciint dma_buf_stats_setup(struct dma_buf *dmabuf)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct dma_buf_sysfs_entry *sysfs_entry;
1748c2ecf20Sopenharmony_ci	int ret;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (!dmabuf || !dmabuf->file)
1778c2ecf20Sopenharmony_ci		return -EINVAL;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (!dmabuf->exp_name) {
1808c2ecf20Sopenharmony_ci		pr_err("exporter name must not be empty if stats needed\n");
1818c2ecf20Sopenharmony_ci		return -EINVAL;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry), GFP_KERNEL);
1858c2ecf20Sopenharmony_ci	if (!sysfs_entry)
1868c2ecf20Sopenharmony_ci		return -ENOMEM;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
1898c2ecf20Sopenharmony_ci	sysfs_entry->dmabuf = dmabuf;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	dmabuf->sysfs_entry = sysfs_entry;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* create the directory for buffer stats */
1948c2ecf20Sopenharmony_ci	ret = kobject_init_and_add(&sysfs_entry->kobj, &dma_buf_ktype, NULL,
1958c2ecf20Sopenharmony_ci				   "%lu", file_inode(dmabuf->file)->i_ino);
1968c2ecf20Sopenharmony_ci	if (ret)
1978c2ecf20Sopenharmony_ci		goto err_sysfs_dmabuf;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	return 0;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cierr_sysfs_dmabuf:
2028c2ecf20Sopenharmony_ci	kobject_put(&sysfs_entry->kobj);
2038c2ecf20Sopenharmony_ci	dmabuf->sysfs_entry = NULL;
2048c2ecf20Sopenharmony_ci	return ret;
2058c2ecf20Sopenharmony_ci}
206