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 */ gb_loopback_async_wait_to_send(gb); if (kthread_should_stop()) break;
mutex_lock(&gb->mutex);
- /* Optionally terminate */ if (gb->send_count == gb->iteration_max) { mutex_unlock(&gb->mutex); - - /* Wait for synchronous and asynchronous completion */ - gb_loopback_async_wait_all(gb); - - /* Mark complete unless user-space has poked us */ - 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); + gb_loopback_handle_completion(gb, bundle); continue; } + size = gb->size; us_wait = gb->us_wait; type = gb->type; if (ktime_to_ns(gb->ts) == 0) gb->ts = ktime_get();
- /* Else operations to perform */ - 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 { - /* We are effectively single threaded here */ - 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); - } + gb_loopback_dispatch_operation(gb, type, size); + gb->send_count++; mutex_unlock(&gb->mutex);
- if (us_wait) { - if (us_wait < 20000) - usleep_range(us_wait, us_wait + 100); - else - msleep(us_wait / 1000); - } + gb_loopback_delay_if_needed(us_wait); }
gb_pm_runtime_put_autosuspend(bundle);