MALI: bifrost: Optimize gpu mem sysfs entry

One process corresponds to only one node,
simplifying the upper-level access.

ls -l sys/class/misc/mali0/device/kprcs/(pid)/
-r--r--r-- 1 root root 4096 2025-06-16 09:47 private_gpu_mem
-r--r--r-- 1 root root 4096 2025-06-16 09:47 total_gpu_mem

Signed-off-by: Weixin Zhou <zwx@rock-chips.com>
Change-Id: Ia198059560c84c5097bfb04f9a16c056db1edaf3
This commit is contained in:
Weixin Zhou
2025-06-16 17:46:11 +08:00
committed by Tao Huang
parent 8ea643bc7c
commit fe59c73bbf
2 changed files with 83 additions and 70 deletions

View File

@@ -72,6 +72,75 @@ static struct kbase_process *find_process_node(struct rb_node *node, pid_t tgid)
return kprcs;
}
static ssize_t kbase_kctx_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct kobj_attribute *kattr = container_of(attr, struct kobj_attribute, attr);
if (kattr->show)
return kattr->show(kobj, kattr, buf);
return -EIO;
}
static ssize_t kbase_total_gpu_mem_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
struct kbase_context *kctx = container_of(kobj, struct kbase_context, kobj);
return scnprintf(buf, PAGE_SIZE, "%zu\n", kctx->kprcs->total_gpu_pages << PAGE_SHIFT);
}
static ssize_t kbase_private_gpu_mem_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
struct kbase_context *kctx = container_of(kobj, struct kbase_context, kobj);
struct kbase_process *kprcs = kctx->kprcs;
struct kbase_context *tmp_kctx;
size_t total_pages = 0;
/* Sum up used_pages from all contexts in the process */
list_for_each_entry(tmp_kctx, &kprcs->kctx_list, kprcs_link) {
total_pages += atomic_read(&tmp_kctx->used_pages);
}
return scnprintf(buf, PAGE_SIZE, "%zu\n", total_pages << PAGE_SHIFT);
}
static struct kobj_attribute kbase_total_gpu_mem_attr = {
.attr = {
.name = "total_gpu_mem",
.mode = 0444,
},
.show = kbase_total_gpu_mem_show,
.store = NULL,
};
static struct kobj_attribute kbase_private_gpu_mem_attr = {
.attr = {
.name = "private_gpu_mem",
.mode = 0444,
},
.show = kbase_private_gpu_mem_show,
.store = NULL,
};
static struct attribute *kbase_kctx_attrs[] = {
&kbase_total_gpu_mem_attr.attr,
&kbase_private_gpu_mem_attr.attr,
NULL,
};
static const struct attribute_group kbase_kctx_attr_group = {
.attrs = kbase_kctx_attrs,
};
static const struct sysfs_ops kbase_kctx_sysfs_ops = {
.show = kbase_kctx_attr_show,
};
static const struct kobj_type kbase_kctx_ktype = {
.sysfs_ops = &kbase_kctx_sysfs_ops,
.default_groups = (const struct attribute_group *[]) { &kbase_kctx_attr_group, NULL },
};
/**
* kbase_insert_kctx_to_process - Initialise kbase process context.
*
@@ -100,6 +169,8 @@ static int kbase_insert_kctx_to_process(struct kbase_context *kctx)
*/
if (!kprcs) {
struct rb_node **new = &prcs_root->rb_node, *parent = NULL;
char kctx_name[64];
int ret = 0;
kprcs = kzalloc(sizeof(*kprcs), GFP_KERNEL);
if (kprcs == NULL)
@@ -109,6 +180,15 @@ static int kbase_insert_kctx_to_process(struct kbase_context *kctx)
kprcs->dma_buf_root = RB_ROOT;
kprcs->total_gpu_pages = 0;
if (unlikely(!scnprintf(kctx_name, 64, "%d", tgid)))
return -ENOMEM;
ret = kobject_init_and_add(&kctx->kobj, &kbase_kctx_ktype, kctx->kbdev->kprcs_kobj, kctx_name);
if (ret) {
dev_err(kctx->kbdev->dev, "Failed to create kctx kobject");
kobject_put(&kctx->kobj);
}
while (*new) {
struct kbase_process *prcs_node;
@@ -260,6 +340,7 @@ static void kbase_remove_kctx_from_process(struct kbase_context *kctx)
* we can remove it from the process rb_tree.
*/
if (list_empty(&kprcs->kctx_list)) {
kobject_put(&kctx->kobj);
rb_erase(&kprcs->kprcs_node, &kctx->kbdev->process_root);
/* Add checks, so that the terminating process Should not
* hold any gpu_memory.

View File

@@ -360,8 +360,6 @@ static void kbase_file_delete(struct kbase_file *const kfile)
#endif
kbase_context_debugfs_term(kctx);
kobject_put(&kctx->kobj);
kbase_destroy_context(kctx);
dev_dbg(kbdev->dev, "deleted base context\n");
@@ -626,74 +624,14 @@ static const struct file_operations kbase_force_same_va_fops = {
};
#endif /* CONFIG_DEBUG_FS */
static ssize_t kbase_kctx_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct kobj_attribute *kattr = container_of(attr, struct kobj_attribute, attr);
if (kattr->show)
return kattr->show(kobj, kattr, buf);
return -EIO;
}
static ssize_t kbase_total_gpu_mem_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
struct kbase_context *kctx = container_of(kobj, struct kbase_context, kobj);
return scnprintf(buf, PAGE_SIZE, "%zu\n", kctx->kprcs->total_gpu_pages << PAGE_SHIFT);
}
static ssize_t kbase_private_gpu_mem_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
struct kbase_context *kctx = container_of(kobj, struct kbase_context, kobj);
return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&kctx->used_pages) << PAGE_SHIFT);
}
static struct kobj_attribute kbase_total_gpu_mem_attr = {
.attr = {
.name = "total_gpu_mem",
.mode = 0444,
},
.show = kbase_total_gpu_mem_show,
.store = NULL,
};
static struct kobj_attribute kbase_private_gpu_mem_attr = {
.attr = {
.name = "private_gpu_mem",
.mode = 0444,
},
.show = kbase_private_gpu_mem_show,
.store = NULL,
};
static struct attribute *kbase_kctx_attrs[] = {
&kbase_total_gpu_mem_attr.attr,
&kbase_private_gpu_mem_attr.attr,
NULL,
};
static const struct attribute_group kbase_kctx_attr_group = {
.attrs = kbase_kctx_attrs,
};
static const struct sysfs_ops kbase_kctx_sysfs_ops = {
.show = kbase_kctx_attr_show,
};
static const struct kobj_type kbase_kctx_ktype = {
.sysfs_ops = &kbase_kctx_sysfs_ops,
.default_groups = (const struct attribute_group *[]) { &kbase_kctx_attr_group, NULL },
};
static int kbase_file_create_kctx(struct kbase_file *const kfile,
base_context_create_flags const flags)
{
struct kbase_device *kbdev = NULL;
struct kbase_context *kctx = NULL;
#if IS_ENABLED(CONFIG_DEBUG_FS)
char kctx_name[64];
int ret = 0;
#endif
if (WARN_ON(!kfile))
return -EINVAL;
@@ -738,12 +676,6 @@ static int kbase_file_create_kctx(struct kbase_file *const kfile,
}
#endif /* CONFIG_DEBUG_FS */
ret = kobject_init_and_add(&kctx->kobj, &kbase_kctx_ktype, kbdev->kprcs_kobj, kctx_name);
if (ret) {
dev_err(kbdev->dev, "Failed to create kctx kobject");
kobject_put(&kctx->kobj);
}
dev_dbg(kbdev->dev, "created base context\n");
kfile->kctx = kctx;