Received: by 10.223.164.200 with SMTP id h8csp643849wrb; Sun, 5 Nov 2017 17:33:36 -0800 (PST) X-Google-Smtp-Source: ABhQp+Two9Wkizv0Q7R4PQHvZnUQMDTkrnlPvvx3Ge0VysmgZ1GEpnhCiKmHKE34Ct6SwM3e46f0 X-Received: by 10.84.175.3 with SMTP id s3mr13099114plb.440.1509932016149; Sun, 05 Nov 2017 17:33:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1509932016; cv=none; d=google.com; s=arc-20160816; b=i8vwaSpAp224RKW/maPp8zII/vx7KZwH8b2hvzbrsTN8R8jNhp46z47TcgQHFWieyI cDDPH9v5TqpfqZmTt9jycb64E35i9DCbUYXJN8V6F19kp3y6EcBdEBgP+2wsYrsHTeBJ 5gfaXWfN2tXEh7Gb+sM5W4XGv/lVs3WzU7OFyRHazxbZ2S9oxTHzN7+TvgFl8707ogZZ yTaGUSbQaaXjEB+V1rinjIFuuJoA76406+8wUViIaqR4MQsy/WrQwqsWQvH7ffoiZe7i mlb11/QuweeH0tslGBRtd69OXjqJuXW29JZEaMpUifsejuF6XvhYFuxutyuNmysDedXL S/yw== 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=iJk8tQMvmXhQfWF7GJxrK2Vrvx5tzxmFf/YI0yuRN/e28YKPMDGCb/EPYJ2+lZ9foN uFdvTTRwcyZMthfRCR9QC654w2A5uiJjlB0ctvw0A6AwNcxi3FL3xoe6cvO/YlcAFC3d WRukX2auydKD/SIrUV5SpaOsqIhvQliga0EMYlIP4De3ioM9dbf1SgYt3omeZInI0Tle IWHh40Il+WpP2cp52RBOTDCdHLq52aeFRIMit5cjsCSlHb+HrJt1uGpmn7Oeb8f7iwtW RISBAW/eOUOMFDSQFTSF0yT+uu/VTVDAmUdiZAYlVVXxXq+4Bt3K8ZicXA2ZxHFsaCwu qVSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nexus-software-ie.20150623.gappssmtp.com header.s=20150623 header.b=GtXgvZ+7; 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 h33si8848386plh.23.2017.11.05.17.33.23; Sun, 05 Nov 2017 17:33:36 -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=GtXgvZ+7; 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 S1751669AbdKFBcb (ORCPT + 96 others); Sun, 5 Nov 2017 20:32:31 -0500 Received: from mail-wm0-f67.google.com ([74.125.82.67]:52248 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751527AbdKFBc2 (ORCPT ); Sun, 5 Nov 2017 20:32:28 -0500 Received: by mail-wm0-f67.google.com with SMTP id t139so11095969wmt.1 for ; Sun, 05 Nov 2017 17:32:28 -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=GtXgvZ+7xgP0TS3Kg1xzFqTM/gwThGuDdiJ/BJUCJIyOsgG9f7pekjw2b9TJHGDkRk SGsveTMYhfcLfLS5ebjI8tKBFozVpg9k5+llUfwXADbPttYEAKRjtpyZOSg4xmvCJ2mz 5npTtcUaHhkN51m66Z062/GRkjapZTl281V6czOgIxu/W4WQir44WLOoeRaH7MTsRAp4 8beZVZfk5lBgoR21I41yOGyH03o1sW52AgjDe7WbKhb1Fi8crklh9OA4ufvpaFqseNTS reh1zANv/bds6zudbpF7c2mXqMb7MESBw9236wgylcGM/rKuCifN4vI/YCEnLFZHr8qJ Dm6A== 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=YARDXH7xoKUz2VwVyIMc3oAeoG2To/CDsNq/iknMJhMrJX5J2nta5/apFPZAzjqpeX 1bwxv+oPZCKrJL2za5TJOTwljLPZkICv7Loh5EzSl0zWgCU8SJBPCTjcUjKX858isuWS JboYaSjOkXuR5surKbmEjhAIhpz4TNDHafWhEZ/BdxX0oQEOQ0m8RH8iLgelUw6EWsZy k1QZSItYZBAzbY4JoTw0dvaU7Q4lHbHjs73meOuQY6Y5E44W837kZhvKiPsz/4s1Fm8b nUHMqNpmvQx9Q+Si7CFvQtM8ogmbc9pjdwYQEnWagwpTZrwuAiMJEmGDcNnx+G2vJRk8 zs4A== X-Gm-Message-State: AMCzsaWAV0zfB7QW05QlGYHpeoD1BYEbH09HXAiLaZq3bz+OMFx+XtuT oK/CQ4cPQNSAhz+pbgrG/Axupifh X-Received: by 10.80.221.71 with SMTP id u7mr18192757edk.86.1509931947669; Sun, 05 Nov 2017 17:32:27 -0800 (PST) Received: from localhost.localdomain ([109.255.42.2]) by smtp.gmail.com with ESMTPSA id c7sm10603213edc.26.2017.11.05.17.32.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 05 Nov 2017 17:32:27 -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 v3 4/4] staging: greybus: loopback: convert loopback to use generic async operations Date: Mon, 6 Nov 2017 01:32:22 +0000 Message-Id: <1509931942-25712-5-git-send-email-pure.logic@nexus-software.ie> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1509931942-25712-1-git-send-email-pure.logic@nexus-software.ie> References: <1509931942-25712-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 1583278092394183345@xxx Mon Nov 06 01:27:32 +0000 2017 X-GM-THRID: 1583195112474769520 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread