Skip to content

Commit

Permalink
Merge branch 'icc-debugfs' into icc-next
Browse files Browse the repository at this point in the history
This series introduces interconnect debugfs files that support voting
for any interconnect path the framework supports. It is useful for debug,
test and verification.

* icc-debugfs
  debugfs: Add write support to debugfs_create_str()
  interconnect: Reintroduce icc_get()
  interconnect: Add debugfs test client

Link: https://lore.kernel.org/r/20230807142914.12480-1-quic_mdtipton@quicinc.com
Signed-off-by: Georgi Djakov <djakov@kernel.org>
  • Loading branch information
Georgi Djakov committed Aug 22, 2023
2 parents 3a60e2a + 770c69f commit 6ac3f01
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 3 deletions.
25 changes: 25 additions & 0 deletions Documentation/driver-api/interconnect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,28 @@ through dot to generate diagrams in many graphical formats::

$ cat /sys/kernel/debug/interconnect/interconnect_graph | \
dot -Tsvg > interconnect_graph.svg

The ``test-client`` directory provides interfaces for issuing BW requests to
any arbitrary path. Note that for safety reasons, this feature is disabled by
default without a Kconfig to enable it. Enabling it requires code changes to
``#define INTERCONNECT_ALLOW_WRITE_DEBUGFS``. Example usage::

cd /sys/kernel/debug/interconnect/test-client/

# Configure node endpoints for the path from CPU to DDR on
# qcom/sm8550.
echo chm_apps > src_node
echo ebi > dst_node

# Get path between src_node and dst_node. This is only
# necessary after updating the node endpoints.
echo 1 > get

# Set desired BW to 1GBps avg and 2GBps peak.
echo 1000000 > avg_bw
echo 2000000 > peak_bw

# Vote for avg_bw and peak_bw on the latest path from "get".
# Voting for multiple paths is possible by repeating this
# process for different nodes endpoints.
echo 1 > commit
2 changes: 1 addition & 1 deletion drivers/interconnect/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: GPL-2.0

CFLAGS_core.o := -I$(src)
icc-core-objs := core.o bulk.o
icc-core-objs := core.o bulk.o debugfs-client.o

obj-$(CONFIG_INTERCONNECT) += icc-core.o
obj-$(CONFIG_INTERCONNECT_IMX) += imx/
Expand Down
66 changes: 66 additions & 0 deletions drivers/interconnect/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ static struct icc_node *node_find(const int id)
return idr_find(&icc_idr, id);
}

static struct icc_node *node_find_by_name(const char *name)
{
struct icc_provider *provider;
struct icc_node *n;

list_for_each_entry(provider, &icc_providers, provider_list) {
list_for_each_entry(n, &provider->nodes, node_list) {
if (!strcmp(n->name, name))
return n;
}
}

return NULL;
}

static struct icc_path *path_init(struct device *dev, struct icc_node *dst,
ssize_t num_nodes)
{
Expand Down Expand Up @@ -562,6 +577,54 @@ struct icc_path *of_icc_get(struct device *dev, const char *name)
}
EXPORT_SYMBOL_GPL(of_icc_get);

/**
* icc_get() - get a path handle between two endpoints
* @dev: device pointer for the consumer device
* @src: source node name
* @dst: destination node name
*
* This function will search for a path between two endpoints and return an
* icc_path handle on success. Use icc_put() to release constraints when they
* are not needed anymore.
*
* Return: icc_path pointer on success or ERR_PTR() on error. NULL is returned
* when the API is disabled.
*/
struct icc_path *icc_get(struct device *dev, const char *src, const char *dst)
{
struct icc_node *src_node, *dst_node;
struct icc_path *path = ERR_PTR(-EPROBE_DEFER);

mutex_lock(&icc_lock);

src_node = node_find_by_name(src);
if (!src_node) {
dev_err(dev, "%s: invalid src=%s\n", __func__, src);
goto out;
}

dst_node = node_find_by_name(dst);
if (!dst_node) {
dev_err(dev, "%s: invalid dst=%s\n", __func__, dst);
goto out;
}

path = path_find(dev, src_node, dst_node);
if (IS_ERR(path)) {
dev_err(dev, "%s: invalid path=%ld\n", __func__, PTR_ERR(path));
goto out;
}

path->name = kasprintf(GFP_KERNEL, "%s-%s", src_node->name, dst_node->name);
if (!path->name) {
kfree(path);
path = ERR_PTR(-ENOMEM);
}
out:
mutex_unlock(&icc_lock);
return path;
}

/**
* icc_set_tag() - set an optional tag on a path
* @path: the path we want to tag
Expand Down Expand Up @@ -1065,6 +1128,9 @@ static int __init icc_init(void)
icc_debugfs_dir, NULL, &icc_summary_fops);
debugfs_create_file("interconnect_graph", 0444,
icc_debugfs_dir, NULL, &icc_graph_fops);

icc_debugfs_client_init(icc_debugfs_dir);

return 0;
}

Expand Down
168 changes: 168 additions & 0 deletions drivers/interconnect/debugfs-client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
*/
#include <linux/debugfs.h>
#include <linux/interconnect.h>
#include <linux/platform_device.h>

#include "internal.h"

/*
* This can be dangerous, therefore don't provide any real compile time
* configuration option for this feature.
* People who want to use this will need to modify the source code directly.
*/
#undef INTERCONNECT_ALLOW_WRITE_DEBUGFS

#if defined(INTERCONNECT_ALLOW_WRITE_DEBUGFS) && defined(CONFIG_DEBUG_FS)

static LIST_HEAD(debugfs_paths);
static DEFINE_MUTEX(debugfs_lock);

static struct platform_device *pdev;
static struct icc_path *cur_path;

static char *src_node;
static char *dst_node;
static u32 avg_bw;
static u32 peak_bw;
static u32 tag;

struct debugfs_path {
const char *src;
const char *dst;
struct icc_path *path;
struct list_head list;
};

static struct icc_path *get_path(const char *src, const char *dst)
{
struct debugfs_path *path;

list_for_each_entry(path, &debugfs_paths, list) {
if (!strcmp(path->src, src) && !strcmp(path->dst, dst))
return path->path;
}

return NULL;
}

static int icc_get_set(void *data, u64 val)
{
struct debugfs_path *debugfs_path;
char *src, *dst;
int ret = 0;

mutex_lock(&debugfs_lock);

rcu_read_lock();
src = rcu_dereference(src_node);
dst = rcu_dereference(dst_node);

/*
* If we've already looked up a path, then use the existing one instead
* of calling icc_get() again. This allows for updating previous BW
* votes when "get" is written to multiple times for multiple paths.
*/
cur_path = get_path(src, dst);
if (cur_path) {
rcu_read_unlock();
goto out;
}

src = kstrdup(src, GFP_ATOMIC);
dst = kstrdup(dst, GFP_ATOMIC);
rcu_read_unlock();

if (!src || !dst) {
ret = -ENOMEM;
goto err_free;
}

cur_path = icc_get(&pdev->dev, src, dst);
if (IS_ERR(cur_path)) {
ret = PTR_ERR(cur_path);
goto err_free;
}

debugfs_path = kzalloc(sizeof(*debugfs_path), GFP_KERNEL);
if (!debugfs_path) {
ret = -ENOMEM;
goto err_put;
}

debugfs_path->path = cur_path;
debugfs_path->src = src;
debugfs_path->dst = dst;
list_add_tail(&debugfs_path->list, &debugfs_paths);

goto out;

err_put:
icc_put(cur_path);
err_free:
kfree(src);
kfree(dst);
out:
mutex_unlock(&debugfs_lock);
return ret;
}

DEFINE_DEBUGFS_ATTRIBUTE(icc_get_fops, NULL, icc_get_set, "%llu\n");

static int icc_commit_set(void *data, u64 val)
{
int ret;

mutex_lock(&debugfs_lock);

if (IS_ERR_OR_NULL(cur_path)) {
ret = PTR_ERR(cur_path);
goto out;
}

icc_set_tag(cur_path, tag);
ret = icc_set_bw(cur_path, avg_bw, peak_bw);
out:
mutex_unlock(&debugfs_lock);
return ret;
}

DEFINE_DEBUGFS_ATTRIBUTE(icc_commit_fops, NULL, icc_commit_set, "%llu\n");

int icc_debugfs_client_init(struct dentry *icc_dir)
{
struct dentry *client_dir;
int ret;

pdev = platform_device_alloc("icc-debugfs-client", PLATFORM_DEVID_NONE);

ret = platform_device_add(pdev);
if (ret) {
pr_err("%s: failed to add platform device: %d\n", __func__, ret);
platform_device_put(pdev);
return ret;
}

client_dir = debugfs_create_dir("test_client", icc_dir);

debugfs_create_str("src_node", 0600, client_dir, &src_node);
debugfs_create_str("dst_node", 0600, client_dir, &dst_node);
debugfs_create_file("get", 0200, client_dir, NULL, &icc_get_fops);
debugfs_create_u32("avg_bw", 0600, client_dir, &avg_bw);
debugfs_create_u32("peak_bw", 0600, client_dir, &peak_bw);
debugfs_create_u32("tag", 0600, client_dir, &tag);
debugfs_create_file("commit", 0200, client_dir, NULL, &icc_commit_fops);

return 0;
}

#else

int icc_debugfs_client_init(struct dentry *icc_dir)
{
return 0;
}

#endif
3 changes: 3 additions & 0 deletions drivers/interconnect/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ struct icc_path {
struct icc_req reqs[] __counted_by(num_nodes);
};

struct icc_path *icc_get(struct device *dev, const char *src, const char *dst);
int icc_debugfs_client_init(struct dentry *icc_dir);

#endif
48 changes: 46 additions & 2 deletions fs/debugfs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,52 @@ EXPORT_SYMBOL_GPL(debugfs_create_str);
static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
size_t count, loff_t *ppos)
{
/* This is really only for read-only strings */
return -EINVAL;
struct dentry *dentry = F_DENTRY(file);
char *old, *new = NULL;
int pos = *ppos;
int r;

r = debugfs_file_get(dentry);
if (unlikely(r))
return r;

old = *(char **)file->private_data;

/* only allow strict concatenation */
r = -EINVAL;
if (pos && pos != strlen(old))
goto error;

r = -E2BIG;
if (pos + count + 1 > PAGE_SIZE)
goto error;

r = -ENOMEM;
new = kmalloc(pos + count + 1, GFP_KERNEL);
if (!new)
goto error;

if (pos)
memcpy(new, old, pos);

r = -EFAULT;
if (copy_from_user(new + pos, user_buf, count))
goto error;

new[pos + count] = '\0';
strim(new);

rcu_assign_pointer(*(char **)file->private_data, new);
synchronize_rcu();
kfree(old);

debugfs_file_put(dentry);
return count;

error:
kfree(new);
debugfs_file_put(dentry);
return r;
}

static const struct file_operations fops_str = {
Expand Down

0 comments on commit 6ac3f01

Please sign in to comment.