On Sun, Apr 13, 2025 at 07:32:19AM +0000, Ganesh Kumar Pittala wrote:
This patch refactors the gb_loopback_fn() function in loopback.c by splitting large blocks of logic into well-named static helpers to improve clarity, readability, and maintainability.
The control flow remains unchanged. No functional modifications are introduced.
This aligns with kernel coding style guidelines for long functions and helps future contributors understand and modify loopback behavior more easily.
Signed-off-by: Ganesh Kumar Pittala ganeshkpittala@gmail.com
drivers/staging/greybus/loopback.c | 152 ++++++++++++++++------------- 1 file changed, 82 insertions(+), 70 deletions(-)
diff --git a/drivers/staging/greybus/loopback.c b/drivers/staging/greybus/loopback.c index c194afea941a..1e3644ede1b6 100644 --- a/drivers/staging/greybus/loopback.c +++ b/drivers/staging/greybus/loopback.c @@ -832,105 +832,117 @@ static void gb_loopback_async_wait_to_send(struct gb_loopback *gb) kthread_should_stop()); } -static int gb_loopback_fn(void *data) +static bool gb_loopback_should_stop(struct gb_loopback *gb,
struct gb_bundle *bundle)
+{
- if (!gb->type) {
gb_pm_runtime_put_autosuspend(bundle);
wait_event_interruptible(gb->wq,
gb->type || kthread_should_stop());
if (kthread_should_stop())
return true;
gb_pm_runtime_get_sync(bundle);
- }
- return kthread_should_stop();
+}
+static void gb_loopback_handle_completion(struct gb_loopback *gb,
struct gb_bundle *bundle)
+{
- gb_loopback_async_wait_all(gb);
- mutex_lock(&gb->mutex);
- if (gb->iteration_count == gb->iteration_max) {
gb->type = 0;
gb->send_count = 0;
sysfs_notify(&gb->dev->kobj, NULL, "iteration_count");
dev_dbg(&bundle->dev, "load test complete\n");
- } else {
dev_dbg(&bundle->dev, "continuing on with new test set\n");
- }
- mutex_unlock(&gb->mutex);
+}
+static void gb_loopback_dispatch_operation(struct gb_loopback *gb, int type,
u32 size)
{ int error = 0;
- int us_wait = 0;
- int type;
- int ret;
- u32 size;
- if (gb->async) {
if (type == GB_LOOPBACK_TYPE_PING)
error = gb_loopback_async_ping(gb);
else if (type == GB_LOOPBACK_TYPE_TRANSFER)
error = gb_loopback_async_transfer(gb, size);
else if (type == GB_LOOPBACK_TYPE_SINK)
error = gb_loopback_async_sink(gb, size);
if (error) {
gb->error++;
gb->iteration_count++;
}
- } else {
if (type == GB_LOOPBACK_TYPE_PING)
error = gb_loopback_sync_ping(gb);
else if (type == GB_LOOPBACK_TYPE_TRANSFER)
error = gb_loopback_sync_transfer(gb, size);
else if (type == GB_LOOPBACK_TYPE_SINK)
error = gb_loopback_sync_sink(gb, size);
if (error)
gb->error++;
gb->iteration_count++;
gb_loopback_calculate_stats(gb, !!error);
- }
+}
+static void gb_loopback_delay_if_needed(int us_wait) +{
- if (us_wait) {
if (us_wait < 20000)
usleep_range(us_wait, us_wait + 100);
else
msleep(us_wait / 1000);
- }
+}
+static int gb_loopback_fn(void *data) +{
- int us_wait = 0, type;
- u32 size; struct gb_loopback *gb = data; struct gb_bundle *bundle = gb->connection->bundle;
- ret = gb_pm_runtime_get_sync(bundle);
- if (ret)
return ret;
- if (gb_pm_runtime_get_sync(bundle))
return -EIO;
while (1) {
if (!gb->type) {
gb_pm_runtime_put_autosuspend(bundle);
wait_event_interruptible(gb->wq, gb->type ||
kthread_should_stop());
ret = gb_pm_runtime_get_sync(bundle);
if (ret)
return ret;
}
if (kthread_should_stop())
if (gb_loopback_should_stop(gb, bundle)) break;
/* Limit the maximum number of in-flight async operations */
Why is it ok to remove this comment?
And why was this function broken up? Is it confusing such that it now needs subfunctions that are only called once? Now you have to jump around to follow the logic of this big while(1) loop, making it harder to follow.
Remember, we write code for people first, compilers second, and I think you just made it harder for people to manage this code over time as it now takes extra work to determine how it all fits together.
thanks,
greg k-h