Received: by 10.223.164.200 with SMTP id h8csp640206wrb; Sun, 5 Nov 2017 17:27:32 -0800 (PST) X-Google-Smtp-Source: ABhQp+TMEKIv2QftrKClkH/P9XblsPnvHpzXNBgNc9DeysGovBQojIAy/DzqjgzLHxEsw0Rpm8+g X-Received: by 10.101.80.132 with SMTP id r4mr13568768pgp.428.1509931652615; Sun, 05 Nov 2017 17:27:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1509931652; cv=none; d=google.com; s=arc-20160816; b=EpyoH08AOjSQTNF6uyZAiWpGxzvkB28Ffgvm2RyoXKZA5EccoPfXfn3petWihI6EgT Nyzr7G3mrgTxwHIy/gXSmXlL7PH7FOwjWxR+DeKFm1Gpo1vWfYTrwl8qW989ofIS++zL 2hWGdgBpJWs+CyXIDHkjfFIfNqoyQ5iV0hUnu7l287D4KGBWawCQv9doqr8WhnVvvDQv QuyL3ZBYNUuLbo40k9lYkBWFYNz74IKUgIIM2+7MvCSDkrkI4p+hZIKYS054YznASd17 H1VTq4KVJzscP/tITAKKnG6quGdajrzMpfHvnpfVZa7E9blvyIT0QhhGVM3AcRNnGClQ nxSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=heM2WdDWDk0uIWqxRa2SBXm+v6KLgq34/01UQbqOYj8=; b=JqdcYkwZ1haA20v40exdVUSJtycb614+UldQVmBTqlLFi7DnV9j25kuEzXYQ4xr326 pdpnm+t8zoIh0obvH4vQF+o63OjTmp1JO02Rf8J4onpDvD7WOTJWDwWuCIxox+d6rB9f mOCOBO7l5b7ooqdgZcq+f/yYOwS2rGs3Rjc2UXepF6Gb8mQdVN0R3WtOYewXtVuQ0LkL ePBmsnb5G5f/ce5L3Is3knW85J2phlZBQij9eTL1M8Y0dp0fq6u8KD5w/pSEcE5h75vv jYK8pUQG4z1cEOqok/YnTyF6rvf4TvkyiK0xConusjDgQIY2dR5RUbTp9Su0w9/L8e/D /mTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nexus-software-ie.20150623.gappssmtp.com header.s=20150623 header.b=Kdcucsnv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x66si11282097pfe.396.2017.11.05.17.27.18; Sun, 05 Nov 2017 17:27:32 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@nexus-software-ie.20150623.gappssmtp.com header.s=20150623 header.b=Kdcucsnv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751593AbdKFB02 (ORCPT + 96 others); Sun, 5 Nov 2017 20:26:28 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:57082 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751495AbdKFB0R (ORCPT ); Sun, 5 Nov 2017 20:26:17 -0500 Received: by mail-wm0-f66.google.com with SMTP id z3so11041347wme.5 for ; Sun, 05 Nov 2017 17:26:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nexus-software-ie.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=heM2WdDWDk0uIWqxRa2SBXm+v6KLgq34/01UQbqOYj8=; b=Kdcucsnv3lFuX3inqbMhfRSwwOw/kJgbethoUZlZh3nKtMEtxf8NaEfWBvHT9zYxoy AfpMZD8lGZsNpPT3RnU0MLgsr7qXsbcNyz1EAhTbeKpEXkTUxzUVI+DcaHFpmehNkfks 0jBAz99Mzf7bks7po/Ifk++bogjGe/3Ntp9yJo0v8CDJkFDhOSaAxxKokFei0kuYpn7P FS1hViheHe4tQBZIC7uaIn52dLW/A6tHsnW1n7C5UMtLqLnLqEAKW07iSCnQlgKGUy7C RWkhvnPwSXyoJR0f7RG/POzR5xEAMCH3y14B4uD7tJEQLhNug3fFhmxOluIHR4AqxaIv 2OuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=heM2WdDWDk0uIWqxRa2SBXm+v6KLgq34/01UQbqOYj8=; b=kSSjS8tL4/F5E+SHw3uMzCHy2ttLsR5SYD3srRRI6+vpw1ORQZooLBGbVe7KCAbslF mhDdSgR3GpQAjZR34eFAa08PV7xwRkShDykGInCe6YUEBf8z77vFvQtqp6jMqDi6EqQF wk4tlhkxyhBnE/B3NpUBxnS9UCDjMoHe+PxB2xBoCgLCn1C6DcbiqRcJm2wVoSdSHADR aJ2NedHEGUssbQrmmVOCBpkT3ykgjjvomadHPUEG9j4T5j4t5eXwOiwRs/ZthU3Ay3Z3 MLCDon/L7zdScNj3UkITSL1FksAnVxzb/OB+Zm4Mcq4FHccpW/n6X4a4HZNJaht7LMT1 3bDg== X-Gm-Message-State: AMCzsaUCnHJ2ZdUmOznURZbCwxegjcUw3qCB2BRy6HfsBadFpzhfCKVm 5ifMANZ6QWJKeIUEvyOaorEtBA== X-Received: by 10.80.139.185 with SMTP id m54mr17887966edm.237.1509931576027; Sun, 05 Nov 2017 17:26:16 -0800 (PST) Received: from localhost.localdomain ([109.255.42.2]) by smtp.gmail.com with ESMTPSA id c32sm10258015eda.57.2017.11.05.17.26.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 05 Nov 2017 17:26:15 -0800 (PST) From: Bryan O'Donoghue To: johan@kernel.org, elder@kernel.org, gregkh@linuxfoundation.org, devel@driverdev.osuosl.org, keescook@chromium.org, linux-kernel@vger.kernel.org Cc: Bryan O'Donoghue , Mitch Tasman , greybus-dev@lists.linaro.org Subject: [PATCH v2 4/4] staging: greybus: loopback: convert loopback to use generic async operations Date: Mon, 6 Nov 2017 01:26:11 +0000 Message-Id: <1509931571-24621-5-git-send-email-pure.logic@nexus-software.ie> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1509931571-24621-1-git-send-email-pure.logic@nexus-software.ie> References: <1509931571-24621-1-git-send-email-pure.logic@nexus-software.ie> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Loopback has its own internal method for tracking and timing out asynchronous operations however previous patches make it possible to use functionality provided by operation.c to do this instead. Using the code in operation.c means we can completely subtract the timer, the work-queue, the kref and the cringe-worthy 'pending' flag. The completion callback triggered by operation.c will provide an authoritative result code - including -ETIMEDOUT for asynchronous operations. Signed-off-by: Bryan O'Donoghue Cc: Johan Hovold Cc: Alex Elder Cc: Greg Kroah-Hartman Cc: Kees Cook Cc: Mitch Tasman Cc: greybus-dev@lists.linaro.org Cc: devel@driverdev.osuosl.org Cc: linux-kernel@vger.kernel.org --- drivers/staging/greybus/loopback.c | 168 +++++++------------------------------ 1 file changed, 29 insertions(+), 139 deletions(-) diff --git a/drivers/staging/greybus/loopback.c b/drivers/staging/greybus/loopback.c index 9c5980c..6d51998 100644 --- a/drivers/staging/greybus/loopback.c +++ b/drivers/staging/greybus/loopback.c @@ -59,11 +59,6 @@ struct gb_loopback_async_operation { struct gb_loopback *gb; struct gb_operation *operation; ktime_t ts; - struct timer_list timer; - struct list_head entry; - struct work_struct work; - struct kref kref; - bool pending; int (*completion)(struct gb_loopback_async_operation *op_async); }; @@ -427,56 +422,6 @@ static int gb_loopback_operation_sync(struct gb_loopback *gb, int type, return ret; } -static void __gb_loopback_async_operation_destroy(struct kref *kref) -{ - struct gb_loopback_async_operation *op_async; - - op_async = container_of(kref, struct gb_loopback_async_operation, kref); - - list_del(&op_async->entry); - if (op_async->operation) - gb_operation_put(op_async->operation); - atomic_dec(&op_async->gb->outstanding_operations); - wake_up(&op_async->gb->wq_completion); - kfree(op_async); -} - -static void gb_loopback_async_operation_get(struct gb_loopback_async_operation - *op_async) -{ - kref_get(&op_async->kref); -} - -static void gb_loopback_async_operation_put(struct gb_loopback_async_operation - *op_async) -{ - unsigned long flags; - - spin_lock_irqsave(&gb_dev.lock, flags); - kref_put(&op_async->kref, __gb_loopback_async_operation_destroy); - spin_unlock_irqrestore(&gb_dev.lock, flags); -} - -static struct gb_loopback_async_operation * - gb_loopback_operation_find(u16 id) -{ - struct gb_loopback_async_operation *op_async; - bool found = false; - unsigned long flags; - - spin_lock_irqsave(&gb_dev.lock, flags); - list_for_each_entry(op_async, &gb_dev.list_op_async, entry) { - if (op_async->operation->id == id) { - gb_loopback_async_operation_get(op_async); - found = true; - break; - } - } - spin_unlock_irqrestore(&gb_dev.lock, flags); - - return found ? op_async : NULL; -} - static void gb_loopback_async_wait_all(struct gb_loopback *gb) { wait_event(gb->wq_completion, @@ -488,83 +433,41 @@ static void gb_loopback_async_operation_callback(struct gb_operation *operation) struct gb_loopback_async_operation *op_async; struct gb_loopback *gb; ktime_t te; - bool err = false; + int result; te = ktime_get(); - op_async = gb_loopback_operation_find(operation->id); - if (!op_async) - return; - + result = gb_operation_result(operation); + op_async = gb_operation_get_data(operation); gb = op_async->gb; + mutex_lock(&gb->mutex); - if (!op_async->pending || gb_operation_result(operation)) { - err = true; - } else { - if (op_async->completion) - if (op_async->completion(op_async)) - err = true; - } + if (!result && op_async->completion) + result = op_async->completion(op_async); - if (!err) + if (!result) { gb->elapsed_nsecs = gb_loopback_calc_latency(op_async->ts, te); - - if (op_async->pending) { - if (err) - gb->error++; - gb->iteration_count++; - op_async->pending = false; - del_timer_sync(&op_async->timer); - gb_loopback_async_operation_put(op_async); - gb_loopback_calculate_stats(gb, err); + } else { + gb->error++; + if (result == -ETIMEDOUT) + gb->requests_timedout++; } - mutex_unlock(&gb->mutex); - - dev_dbg(&gb->connection->bundle->dev, "complete operation %d\n", - operation->id); - - gb_loopback_async_operation_put(op_async); -} - -static void gb_loopback_async_operation_work(struct work_struct *work) -{ - struct gb_loopback *gb; - struct gb_operation *operation; - struct gb_loopback_async_operation *op_async; - op_async = container_of(work, struct gb_loopback_async_operation, work); - gb = op_async->gb; - operation = op_async->operation; + gb->iteration_count++; + gb_loopback_calculate_stats(gb, result); - mutex_lock(&gb->mutex); - if (op_async->pending) { - gb->requests_timedout++; - gb->error++; - gb->iteration_count++; - op_async->pending = false; - gb_loopback_async_operation_put(op_async); - gb_loopback_calculate_stats(gb, true); - } mutex_unlock(&gb->mutex); - dev_dbg(&gb->connection->bundle->dev, "timeout operation %d\n", + dev_dbg(&gb->connection->bundle->dev, "complete operation %d\n", operation->id); - gb_operation_cancel(operation, -ETIMEDOUT); - gb_loopback_async_operation_put(op_async); -} - -static void gb_loopback_async_operation_timeout(unsigned long data) -{ - struct gb_loopback_async_operation *op_async; - u16 id = data; + /* Wake up waiters */ + atomic_dec(&op_async->gb->outstanding_operations); + wake_up(&gb->wq_completion); - op_async = gb_loopback_operation_find(id); - if (!op_async) { - pr_err("operation %d not found - time out ?\n", id); - return; - } - schedule_work(&op_async->work); + /* Release resources */ + gb_operation_put(operation); + kfree(op_async); } static int gb_loopback_async_operation(struct gb_loopback *gb, int type, @@ -575,15 +478,11 @@ static int gb_loopback_async_operation(struct gb_loopback *gb, int type, struct gb_loopback_async_operation *op_async; struct gb_operation *operation; int ret; - unsigned long flags; op_async = kzalloc(sizeof(*op_async), GFP_KERNEL); if (!op_async) return -ENOMEM; - INIT_WORK(&op_async->work, gb_loopback_async_operation_work); - kref_init(&op_async->kref); - operation = gb_operation_create(gb->connection, type, request_size, response_size, GFP_KERNEL); if (!operation) { @@ -594,33 +493,24 @@ static int gb_loopback_async_operation(struct gb_loopback *gb, int type, if (request_size) memcpy(operation->request->payload, request, request_size); + gb_operation_set_data(operation, op_async); + op_async->gb = gb; op_async->operation = operation; op_async->completion = completion; - spin_lock_irqsave(&gb_dev.lock, flags); - list_add_tail(&op_async->entry, &gb_dev.list_op_async); - spin_unlock_irqrestore(&gb_dev.lock, flags); - op_async->ts = ktime_get(); - op_async->pending = true; + atomic_inc(&gb->outstanding_operations); ret = gb_operation_request_send(operation, gb_loopback_async_operation_callback, - 0, + jiffies_to_msecs(gb->jiffy_timeout), GFP_KERNEL); - if (ret) - goto error; - - setup_timer(&op_async->timer, gb_loopback_async_operation_timeout, - (unsigned long)operation->id); - op_async->timer.expires = jiffies + gb->jiffy_timeout; - add_timer(&op_async->timer); - - goto done; -error: - gb_loopback_async_operation_put(op_async); -done: + if (ret) { + atomic_dec(&gb->outstanding_operations); + gb_operation_put(operation); + kfree(op_async); + } return ret; } -- 2.7.4 From 1583273021811941071@xxx Mon Nov 06 00:06:56 +0000 2017 X-GM-THRID: 1583195112474769520 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread