Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * DMA-BUF sysfs statistics.
0004  *
0005  * Copyright (C) 2021 Google LLC.
0006  */
0007 
0008 #include <linux/dma-buf.h>
0009 #include <linux/dma-resv.h>
0010 #include <linux/kobject.h>
0011 #include <linux/printk.h>
0012 #include <linux/slab.h>
0013 #include <linux/sysfs.h>
0014 
0015 #include "dma-buf-sysfs-stats.h"
0016 
0017 #define to_dma_buf_entry_from_kobj(x) container_of(x, struct dma_buf_sysfs_entry, kobj)
0018 
0019 /**
0020  * DOC: overview
0021  *
0022  * ``/sys/kernel/debug/dma_buf/bufinfo`` provides an overview of every DMA-BUF
0023  * in the system. However, since debugfs is not safe to be mounted in
0024  * production, procfs and sysfs can be used to gather DMA-BUF statistics on
0025  * production systems.
0026  *
0027  * The ``/proc/<pid>/fdinfo/<fd>`` files in procfs can be used to gather
0028  * information about DMA-BUF fds. Detailed documentation about the interface
0029  * is present in Documentation/filesystems/proc.rst.
0030  *
0031  * Unfortunately, the existing procfs interfaces can only provide information
0032  * about the DMA-BUFs for which processes hold fds or have the buffers mmapped
0033  * into their address space. This necessitated the creation of the DMA-BUF sysfs
0034  * statistics interface to provide per-buffer information on production systems.
0035  *
0036  * The interface at ``/sys/kernel/dma-buf/buffers`` exposes information about
0037  * every DMA-BUF when ``CONFIG_DMABUF_SYSFS_STATS`` is enabled.
0038  *
0039  * The following stats are exposed by the interface:
0040  *
0041  * * ``/sys/kernel/dmabuf/buffers/<inode_number>/exporter_name``
0042  * * ``/sys/kernel/dmabuf/buffers/<inode_number>/size``
0043  *
0044  * The information in the interface can also be used to derive per-exporter
0045  * statistics. The data from the interface can be gathered on error conditions
0046  * or other important events to provide a snapshot of DMA-BUF usage.
0047  * It can also be collected periodically by telemetry to monitor various metrics.
0048  *
0049  * Detailed documentation about the interface is present in
0050  * Documentation/ABI/testing/sysfs-kernel-dmabuf-buffers.
0051  */
0052 
0053 struct dma_buf_stats_attribute {
0054     struct attribute attr;
0055     ssize_t (*show)(struct dma_buf *dmabuf,
0056             struct dma_buf_stats_attribute *attr, char *buf);
0057 };
0058 #define to_dma_buf_stats_attr(x) container_of(x, struct dma_buf_stats_attribute, attr)
0059 
0060 static ssize_t dma_buf_stats_attribute_show(struct kobject *kobj,
0061                         struct attribute *attr,
0062                         char *buf)
0063 {
0064     struct dma_buf_stats_attribute *attribute;
0065     struct dma_buf_sysfs_entry *sysfs_entry;
0066     struct dma_buf *dmabuf;
0067 
0068     attribute = to_dma_buf_stats_attr(attr);
0069     sysfs_entry = to_dma_buf_entry_from_kobj(kobj);
0070     dmabuf = sysfs_entry->dmabuf;
0071 
0072     if (!dmabuf || !attribute->show)
0073         return -EIO;
0074 
0075     return attribute->show(dmabuf, attribute, buf);
0076 }
0077 
0078 static const struct sysfs_ops dma_buf_stats_sysfs_ops = {
0079     .show = dma_buf_stats_attribute_show,
0080 };
0081 
0082 static ssize_t exporter_name_show(struct dma_buf *dmabuf,
0083                   struct dma_buf_stats_attribute *attr,
0084                   char *buf)
0085 {
0086     return sysfs_emit(buf, "%s\n", dmabuf->exp_name);
0087 }
0088 
0089 static ssize_t size_show(struct dma_buf *dmabuf,
0090              struct dma_buf_stats_attribute *attr,
0091              char *buf)
0092 {
0093     return sysfs_emit(buf, "%zu\n", dmabuf->size);
0094 }
0095 
0096 static struct dma_buf_stats_attribute exporter_name_attribute =
0097     __ATTR_RO(exporter_name);
0098 static struct dma_buf_stats_attribute size_attribute = __ATTR_RO(size);
0099 
0100 static struct attribute *dma_buf_stats_default_attrs[] = {
0101     &exporter_name_attribute.attr,
0102     &size_attribute.attr,
0103     NULL,
0104 };
0105 ATTRIBUTE_GROUPS(dma_buf_stats_default);
0106 
0107 static void dma_buf_sysfs_release(struct kobject *kobj)
0108 {
0109     struct dma_buf_sysfs_entry *sysfs_entry;
0110 
0111     sysfs_entry = to_dma_buf_entry_from_kobj(kobj);
0112     kfree(sysfs_entry);
0113 }
0114 
0115 static struct kobj_type dma_buf_ktype = {
0116     .sysfs_ops = &dma_buf_stats_sysfs_ops,
0117     .release = dma_buf_sysfs_release,
0118     .default_groups = dma_buf_stats_default_groups,
0119 };
0120 
0121 void dma_buf_stats_teardown(struct dma_buf *dmabuf)
0122 {
0123     struct dma_buf_sysfs_entry *sysfs_entry;
0124 
0125     sysfs_entry = dmabuf->sysfs_entry;
0126     if (!sysfs_entry)
0127         return;
0128 
0129     kobject_del(&sysfs_entry->kobj);
0130     kobject_put(&sysfs_entry->kobj);
0131 }
0132 
0133 
0134 /* Statistics files do not need to send uevents. */
0135 static int dmabuf_sysfs_uevent_filter(struct kobject *kobj)
0136 {
0137     return 0;
0138 }
0139 
0140 static const struct kset_uevent_ops dmabuf_sysfs_no_uevent_ops = {
0141     .filter = dmabuf_sysfs_uevent_filter,
0142 };
0143 
0144 static struct kset *dma_buf_stats_kset;
0145 static struct kset *dma_buf_per_buffer_stats_kset;
0146 int dma_buf_init_sysfs_statistics(void)
0147 {
0148     dma_buf_stats_kset = kset_create_and_add("dmabuf",
0149                          &dmabuf_sysfs_no_uevent_ops,
0150                          kernel_kobj);
0151     if (!dma_buf_stats_kset)
0152         return -ENOMEM;
0153 
0154     dma_buf_per_buffer_stats_kset = kset_create_and_add("buffers",
0155                                 &dmabuf_sysfs_no_uevent_ops,
0156                                 &dma_buf_stats_kset->kobj);
0157     if (!dma_buf_per_buffer_stats_kset) {
0158         kset_unregister(dma_buf_stats_kset);
0159         return -ENOMEM;
0160     }
0161 
0162     return 0;
0163 }
0164 
0165 void dma_buf_uninit_sysfs_statistics(void)
0166 {
0167     kset_unregister(dma_buf_per_buffer_stats_kset);
0168     kset_unregister(dma_buf_stats_kset);
0169 }
0170 
0171 int dma_buf_stats_setup(struct dma_buf *dmabuf)
0172 {
0173     struct dma_buf_sysfs_entry *sysfs_entry;
0174     int ret;
0175 
0176     if (!dmabuf || !dmabuf->file)
0177         return -EINVAL;
0178 
0179     if (!dmabuf->exp_name) {
0180         pr_err("exporter name must not be empty if stats needed\n");
0181         return -EINVAL;
0182     }
0183 
0184     sysfs_entry = kzalloc(sizeof(struct dma_buf_sysfs_entry), GFP_KERNEL);
0185     if (!sysfs_entry)
0186         return -ENOMEM;
0187 
0188     sysfs_entry->kobj.kset = dma_buf_per_buffer_stats_kset;
0189     sysfs_entry->dmabuf = dmabuf;
0190 
0191     dmabuf->sysfs_entry = sysfs_entry;
0192 
0193     /* create the directory for buffer stats */
0194     ret = kobject_init_and_add(&sysfs_entry->kobj, &dma_buf_ktype, NULL,
0195                    "%lu", file_inode(dmabuf->file)->i_ino);
0196     if (ret)
0197         goto err_sysfs_dmabuf;
0198 
0199     return 0;
0200 
0201 err_sysfs_dmabuf:
0202     kobject_put(&sysfs_entry->kobj);
0203     dmabuf->sysfs_entry = NULL;
0204     return ret;
0205 }