From patchwork Sun Jan 15 11:12:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Mc Guire X-Patchwork-Id: 9517351 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1017760571 for ; Sun, 15 Jan 2017 11:11:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0CFCB2819A for ; Sun, 15 Jan 2017 11:11:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F423A2840A; Sun, 15 Jan 2017 11:11:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B370E2819A for ; Sun, 15 Jan 2017 11:11:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750969AbdAOLLm (ORCPT ); Sun, 15 Jan 2017 06:11:42 -0500 Received: from www.osadl.org ([62.245.132.105]:45386 "EHLO www.osadl.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750824AbdAOLLm (ORCPT ); Sun, 15 Jan 2017 06:11:42 -0500 Received: from debian01.hofrr.at (92-243-34-74.adsl.nanet.at [92.243.34.74] (may be forged)) by www.osadl.org (8.13.8/8.13.8/OSADL-2007092901) with ESMTP id v0FBBOWq028961; Sun, 15 Jan 2017 12:11:24 +0100 From: Nicholas Mc Guire To: Dmitry Torokhov Cc: Henrik Rydberg , Dudley Du , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Nicholas Mc Guire Subject: [PATCH RFC V2] Input: cyapa: use time based retry loop Date: Sun, 15 Jan 2017 12:12:38 +0100 Message-Id: <1484478758-13133-1-git-send-email-hofrat@osadl.org> X-Mailer: git-send-email 2.1.4 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Using counter based retry loops for peripherals results in the delay being significantly overrun during high-load situations where delay functions tend to be vary imprecise and overrun there timeouts. So condition the termination on the actual condition of 2s for the re-calibration to have been successful. As this is a very long delay there is no advantage in using high-resolution timers thus switching this to msleep(). Signed-off-by: Nicholas Mc Guire --- V2: The maximum recalibration timeout should have been 2 * HZ as Dmitry Torokhov noted - V2 sets the timeout boundary to 2s now. Problem detected with coccinelle script (usleep_range for long delay) The basic model for the fix up here is to use timeout = jiffies + (retry_time) do { check() mleep() } while (time_is_after_jiffies(timeout)); rather than using a counter and have very long retry loops under load conditions. In the specific case it is sleep()/check() though to wait on the previous write to complete. The initial intent was to move the usleep_range() to msleep() as the delays are very long - if this refactoring of the timeout loop is desirable or not is not clear (both time and count based models seem to be common). Patch was compile tested with: x86_64_defconfig + CONFIG_MOUSE_CYAPA=m Patch is against 4.10-rc3 (localversion-next is next-20170113) drivers/input/mouse/cyapa_gen3.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/drivers/input/mouse/cyapa_gen3.c b/drivers/input/mouse/cyapa_gen3.c index f9600753..0c544de 100644 --- a/drivers/input/mouse/cyapa_gen3.c +++ b/drivers/input/mouse/cyapa_gen3.c @@ -789,7 +789,7 @@ static ssize_t cyapa_gen3_do_calibrate(struct device *dev, const char *buf, size_t count) { struct cyapa *cyapa = dev_get_drvdata(dev); - int tries; + unsigned long timeout; int ret; ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS); @@ -812,31 +812,28 @@ static ssize_t cyapa_gen3_do_calibrate(struct device *dev, goto out; } - tries = 20; /* max recalibration timeout 2s. */ + /* max recalibration timeout 2s. */ + timeout = jiffies + 2 * HZ; do { /* * For this recalibration, the max time will not exceed 2s. * The average time is approximately 500 - 700 ms, and we * will check the status every 100 - 200ms. */ - usleep_range(100000, 200000); - + msleep(100); ret = cyapa_read_byte(cyapa, CYAPA_CMD_DEV_STATUS); if (ret < 0) { - dev_err(dev, "Error reading dev status: %d\n", - ret); + dev_err(dev, "Error reading dev status: %d\n", ret); goto out; } - if ((ret & CYAPA_DEV_NORMAL) == CYAPA_DEV_NORMAL) - break; - } while (--tries); + if ((ret & CYAPA_DEV_NORMAL) == CYAPA_DEV_NORMAL) { + dev_dbg(dev, "Calibration successful.\n"); + goto out; + } + } while (time_is_after_jiffies(timeout)); - if (tries == 0) { - dev_err(dev, "Failed to calibrate. Timeout.\n"); - ret = -ETIMEDOUT; - goto out; - } - dev_dbg(dev, "Calibration successful.\n"); + dev_err(dev, "Failed to calibrate. Timeout.\n"); + ret = -ETIMEDOUT; out: return ret < 0 ? ret : count;