From patchwork Thu Jun 22 09:06:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 697177 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17BCEEB64D8 for ; Thu, 22 Jun 2023 09:16:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232271AbjFVJQx (ORCPT ); Thu, 22 Jun 2023 05:16:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232357AbjFVJOv (ORCPT ); Thu, 22 Jun 2023 05:14:51 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::222]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFE2A5254 for ; Thu, 22 Jun 2023 02:06:38 -0700 (PDT) X-GND-Sasl: miquel.raynal@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1687424797; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PhIQmvwCqc8f2ldfvdDwrf5zFPY90uFTbI6lO/wBafo=; b=i6LxrBfmEyVAED6//TqtsJRl9Yyr2HhOn49PmsrhLcVmi8MJlLOXW+RYw5jcifjLX9pbYx zk6aLfYgaTXutwDG7gHCnKso1wla9Ekx/5eQLwN0WvvBn/lOdqm1OXaJWRzLxPriHjNAwy capbdvwCrPYuUcgjDOaHEXwJYdyLdGIonpr8Rwq4hNyrlU1RouwWea8Y0QLRj/VNR9h3tj iaNn/S0D2dT/UOIQrpBPopYS3N2yypjfa2tU4ReRWOYC6FSWJgHzhteqlALF93cdovy2BC UIAg8Y5c0csX0I7G8UFtE4pdHruF2PdfGDtWXk4KWs4ObLg1MFTCMFwegIZGgg== X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id 5993640010; Thu, 22 Jun 2023 09:06:36 +0000 (UTC) From: Miquel Raynal To: Mark Brown , Cc: Thomas Petazzoni , Tudor Ambarus , Nicolas Ferre , Claudiu Beznea , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , , Miquel Raynal Subject: [PATCH v3 1/3] spi: Create a helper to derive adaptive timeouts Date: Thu, 22 Jun 2023 11:06:32 +0200 Message-Id: <20230622090634.3411468-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230622090634.3411468-1-miquel.raynal@bootlin.com> References: <20230622090634.3411468-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org Big transfers might take a bit of time, too constraining timeouts might lead to false positives. In order to simplify the drivers work and with the goal of factorizing code in mind, let's add a helper that can be used by any spi controller driver to derive a relevant per-transfer timeout value. The logic is simple: we know how much time it would take to transfer a byte, we can easily derive the total theoretical amount of time involved for each transfer. We multiply it by two to have a bit of margin and enforce a minimum of 500ms. Suggested-by: Mark Brown Signed-off-by: Miquel Raynal --- include/linux/spi/spi.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index fbf8c0d95968..4d6636c50465 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -1186,6 +1186,23 @@ static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw) return false; } +/** + * spi_controller_xfer_timeout - Compute a suitable timeout value + * @ctlr: SPI device + * @xfer: Transfer descriptor + * + * Compute a relevant timeout value for the given transfer. We derive the time + * that it would take on a single data line and take twice this amount of time + * with a minimum of 500ms to avoid false positives on loaded systems. + * + * Returns: Transfer timeout value in milliseconds. + */ +static inline unsigned int spi_controller_xfer_timeout(struct spi_controller *ctlr, + struct spi_transfer *xfer) +{ + return max(xfer->len * 8 * 2 / (xfer->speed_hz / 1000), 500U); +} + /*---------------------------------------------------------------------------*/ /* SPI transfer replacement methods which make use of spi_res */ From patchwork Thu Jun 22 09:06:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 697178 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 306A4EB64DB for ; Thu, 22 Jun 2023 09:15:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230506AbjFVJPe (ORCPT ); Thu, 22 Jun 2023 05:15:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232387AbjFVJOy (ORCPT ); Thu, 22 Jun 2023 05:14:54 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42D627EDA for ; Thu, 22 Jun 2023 02:06:39 -0700 (PDT) X-GND-Sasl: miquel.raynal@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1687424798; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=I2183b6oDtby4XAIhp5msAg+vrfOlFnEs4/AO2GjwDM=; b=iVQtyiRuPipbaG/ykeAFvBFn49hkb2v9AQ9KnuvgqDJBW/KZzi9nwysilXKeIpFGdN7XPc fAfOVRAuuJMRHwMzcrc/vjiirp7bF0jEEFPfqrT8WfV/+QHNyswY/RSDOKNFwzKGPNrLnU Cbc1Fhy/ar6AUGzqYMR4E5K2foGRIoD/SkHoG18G4ycvQFldWDSJgu66m7UW+jxdWJ6SfD uZ3tQHjR0mUT5x+qEUAK/wf1gNMukVEI7k5h2qnd+6tPUeWRjNiWv/t7xL+IPMZSKh2l0T viTDH8m8wYGmBAfhH1YbSbNYUhd3aYzPg9XENJX05T+HtGScWXelUE6cznv1YQ== X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id 62D8B40008; Thu, 22 Jun 2023 09:06:37 +0000 (UTC) From: Miquel Raynal To: Mark Brown , Cc: Thomas Petazzoni , Tudor Ambarus , Nicolas Ferre , Claudiu Beznea , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , , Miquel Raynal Subject: [PATCH v3 2/3] spi: atmel: Prevent false timeouts on long transfers Date: Thu, 22 Jun 2023 11:06:33 +0200 Message-Id: <20230622090634.3411468-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230622090634.3411468-1-miquel.raynal@bootlin.com> References: <20230622090634.3411468-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org A slow SPI bus clocks at ~20MHz, which means it would transfer about 2500 bytes per second with a single data line. Big transfers, like when dealing with flashes can easily reach a few MiB. The current DMA timeout is set to 1 second, which means any working transfer of about 4MiB will always be cancelled. With the above derivations, on a slow bus, we can assume every byte will take at most 0.4ms. Said otherwise, we could add 4ms to the 1-second timeout delay every 10kiB. On a 4MiB transfer, it would bring the timeout delay up to 2.6s which still seems rather acceptable for a timeout. The consequence of this is that long transfers might be allowed, which hence requires the need to interrupt the transfer if wanted by the user. We can hence switch to the _interruptible variant of wait_for_completion. This leads to a little bit more handling to also handle the interrupted case but looks really acceptable overall. While at it, we drop the useless, noisy and redundant WARN_ON() call. Signed-off-by: Miquel Raynal --- drivers/spi/spi-atmel.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c index 943548aab8af..d87be2890597 100644 --- a/drivers/spi/spi-atmel.c +++ b/drivers/spi/spi-atmel.c @@ -233,7 +233,8 @@ */ #define DMA_MIN_BYTES 16 -#define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000)) +#define SPI_DMA_MIN_TIMEOUT (msecs_to_jiffies(1000)) +#define SPI_DMA_TIMEOUT_PER_10K (msecs_to_jiffies(4)) #define AUTOSUSPEND_TIMEOUT 2000 @@ -1279,7 +1280,8 @@ static int atmel_spi_one_transfer(struct spi_controller *host, struct atmel_spi_device *asd; int timeout; int ret; - unsigned long dma_timeout; + unsigned int dma_timeout; + long ret_timeout; as = spi_controller_get_devdata(host); @@ -1333,11 +1335,13 @@ static int atmel_spi_one_transfer(struct spi_controller *host, atmel_spi_unlock(as); } - dma_timeout = wait_for_completion_timeout(&as->xfer_completion, - SPI_DMA_TIMEOUT); - if (WARN_ON(dma_timeout == 0)) { - dev_err(&spi->dev, "spi transfer timeout\n"); - as->done_status = -EIO; + dma_timeout = msecs_to_jiffies(spi_controller_xfer_timeout(host, xfer)); + ret_timeout = wait_for_completion_interruptible_timeout(&as->xfer_completion, + dma_timeout); + if (ret_timeout <= 0) { + dev_err(&spi->dev, "spi transfer %s\n", + !ret_timeout ? "timeout" : "canceled"); + as->done_status = ret_timeout < 0 ? ret_timeout : -EIO; } if (as->done_status) From patchwork Thu Jun 22 09:06:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 695194 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25ACEEB64DC for ; Thu, 22 Jun 2023 09:15:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231504AbjFVJPe (ORCPT ); Thu, 22 Jun 2023 05:15:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232394AbjFVJOz (ORCPT ); Thu, 22 Jun 2023 05:14:55 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA75B7EDF for ; Thu, 22 Jun 2023 02:06:40 -0700 (PDT) X-GND-Sasl: miquel.raynal@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1687424799; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uIrH6l6h4FVOlNSp29zZGkHu1wVR3pubO6q4OyhE0xo=; b=ZBuZQNF4lTle5ynALJZoxupoY97a6e2GchnIfLnvBQOypxDR5TAK2RbzZhykao/udWvOoQ zbOyDx3WXByizNLNVnxpJxbwIjEMRrJmKDX7qMGJmhBQ6MGPJH4AvCo+PNBdxHXz0niT/z g51ZXFdBuNQUFqDS3FUMpzFdtVhnLtStjNuFLjsYjP/PTnIBDSXTBqp6U/gi05DTL79NAW npWJN5Q+WeWeEYRUlSdgwoqaR81RfMR4IaPFXNkYUFSABdl3uHwg3/7c8Xj1EjTvJyMi0m ThE86jkW3WXQ0iploMiug+jiwagU9rQ/CD1c8jmP4Pw/ga3meGcI6Sgu6qGQ/Q== X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id 6F0D84000F; Thu, 22 Jun 2023 09:06:38 +0000 (UTC) From: Miquel Raynal To: Mark Brown , Cc: Thomas Petazzoni , Tudor Ambarus , Nicolas Ferre , Claudiu Beznea , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , , Miquel Raynal Subject: [PATCH v3 3/3] spi: sun6i: Use the new helper to derive the xfer timeout value Date: Thu, 22 Jun 2023 11:06:34 +0200 Message-Id: <20230622090634.3411468-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230622090634.3411468-1-miquel.raynal@bootlin.com> References: <20230622090634.3411468-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org A helper was recently added to the core to factorize common code between drivers, like the amount of time a driver should wait for a transfer to happen. It is of course possible to use a default value (like eg. 1s) but it is way stronger to adapt this amount of time to the transfer. Indeed, long transfers (eg. 4MiB) on a slow single-spi bus might take more than the usual second of timeout and prevent lengthy transfers. The core helper was heavily inspired by the logic applied in this driver, the only difference being the minimum amount of time which was enlarged from 0.1s to 0.5s. Use this helper instead of open-coding it. Signed-off-by: Miquel Raynal --- drivers/spi/spi-sun6i.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/spi/spi-sun6i.c b/drivers/spi/spi-sun6i.c index 23ad052528db..180094dfae19 100644 --- a/drivers/spi/spi-sun6i.c +++ b/drivers/spi/spi-sun6i.c @@ -422,7 +422,7 @@ static int sun6i_spi_transfer_one(struct spi_master *master, reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG); sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg | SUN6I_TFR_CTL_XCH); - tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U); + tx_time = spi_controller_xfer_timeout(master, tfr); start = jiffies; timeout = wait_for_completion_timeout(&sspi->done, msecs_to_jiffies(tx_time));