From patchwork Thu Dec 17 22:29:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 344997 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D091C2BBCF for ; Thu, 17 Dec 2020 22:30:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 17792238A0 for ; Thu, 17 Dec 2020 22:30:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732051AbgLQWaQ (ORCPT ); Thu, 17 Dec 2020 17:30:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731957AbgLQWaQ (ORCPT ); Thu, 17 Dec 2020 17:30:16 -0500 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C98BAC061794 for ; Thu, 17 Dec 2020 14:29:35 -0800 (PST) Received: by mail-pg1-x52a.google.com with SMTP id n16so85655pgm.0 for ; Thu, 17 Dec 2020 14:29:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=v7xCKWj505XOK8pmaVepIcbXuQ+piHeDyFLhHQR5U2Q=; b=MUIEHvh95AUP6V1mzbTcu2YJqrE/3xPIlBC0YJsbKW3ewh99f5g1nMnI0mXabiEg/B UroJpxJF6AzkjbS43C706tVZONxmo4K8NmNtdSn1RIU0XCnO0qFM4j4bY2tcd8IJt4z3 WF6i2QGXLrhaI3qashWV5StE1S7SDu8OKFN0M= 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:mime-version :content-transfer-encoding; bh=v7xCKWj505XOK8pmaVepIcbXuQ+piHeDyFLhHQR5U2Q=; b=CkNuYCX8KQuPbdE7lEYBjMRejCHZvLYj/jtVyePMLP5GrD2Mt9gcWGcddiqFdy38Q2 BHFb3/6cdDu8auNHCidKspiwk3OYbhwuR9B651H/FXetMYvHcpqvkxkPkIYfLzfiRU9+ reFCNWLcqfZ0dtzWbRhrdYHpQ8Z++G0cYqRonhJfMr9lepV7+mEh7vQVWa2vr5mWrmfg 3SPhbDHxyrJQbBhBP88S5fPeqN/TEMTUe+abmdbFyvXz7Rwb8hTdMfEk82tC55QG5NX0 sKvj14PKNaYmKX9vNiFCDDO1wj9kPONCQ9BgxXDFfwxrgDUSgQ4k/WlYO0fJ2HO4BTPm qr+Q== X-Gm-Message-State: AOAM5332buCu8cNby0yAcfS26LGMiy1FsbywNo31drgUZVQ2baNwBwap FcjXoqF/GPqh1gYlIIzHJVtRFg== X-Google-Smtp-Source: ABdhPJz6Dtc4cRRUFyl0u5B3TKJYZN6oBuUE1DwLdrGi6ti46ehE1k4YtBfuwM8sp2oD3Q2patXwcg== X-Received: by 2002:aa7:93b0:0:b029:19d:e625:2062 with SMTP id x16-20020aa793b00000b029019de6252062mr1320109pff.47.1608244175337; Thu, 17 Dec 2020 14:29:35 -0800 (PST) Received: from tictac2.mtv.corp.google.com ([2620:15c:202:1:42b0:34ff:fe3d:58e6]) by smtp.gmail.com with ESMTPSA id gm18sm5805850pjb.55.2020.12.17.14.29.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Dec 2020 14:29:34 -0800 (PST) From: Douglas Anderson To: Mark Brown Cc: msavaliy@qti.qualcomm.com, Stephen Boyd , akashast@codeaurora.org, Roja Rani Yarubandi , Douglas Anderson , Alok Chauhan , Andy Gross , Bjorn Andersson , Dilip Kota , Girish Mahadevan , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-spi@vger.kernel.org Subject: [PATCH v3 1/4] spi: spi-geni-qcom: Fix geni_spi_isr() NULL dereference in timeout case Date: Thu, 17 Dec 2020 14:29:11 -0800 Message-Id: <20201217142842.v3.1.I99ee04f0cb823415df59bd4f550d6ff5756e43d6@changeid> X-Mailer: git-send-email 2.29.2.684.gfbc64c5ab5-goog MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org In commit 7ba9bdcb91f6 ("spi: spi-geni-qcom: Don't keep a local state variable") we changed handle_fifo_timeout() so that we set "mas->cur_xfer" to NULL to make absolutely sure that we don't mess with the buffers from the previous transfer in the timeout case. Unfortunately, this caused the IRQ handler to dereference NULL in some cases. One case: CPU0 CPU1 ---- ---- setup_fifo_xfer() geni_se_setup_m_cmd() ... handle_fifo_timeout() spin_lock_irq(mas->lock) mas->cur_xfer = NULL geni_se_cancel_m_cmd() spin_unlock_irq(mas->lock) geni_spi_isr() spin_lock(mas->lock) if (m_irq & M_RX_FIFO_WATERMARK_EN) geni_spi_handle_rx() mas->cur_xfer NULL dereference! tl;dr: Seriously delayed interrupts for RX/TX can lead to timeout handling setting mas->cur_xfer to NULL. Let's check for the NULL transfer in the TX and RX cases and reset the watermark or clear out the fifo respectively to put the hardware back into a sane state. NOTE: things still could get confused if we get timeouts all the way through handle_fifo_timeout() and then start a new transfer because interrupts from the old transfer / cancel / abort could still be pending. A future patch will help this corner case. Fixes: 561de45f72bd ("spi: spi-geni-qcom: Add SPI driver support for GENI based QUP") Signed-off-by: Douglas Anderson Reviewed-by: Stephen Boyd Reviewed-by: Stephen Boyd Reviewed-by: Stephen Boyd --- Changes in v3: - (ptr == NULL) => (!ptr), take 2. - while loop => for loop Changes in v2: - (ptr == NULL) => (!ptr). - Addressed loop nits in geni_spi_handle_rx(). - Commit message rewording from Stephen. drivers/spi/spi-geni-qcom.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/drivers/spi/spi-geni-qcom.c b/drivers/spi/spi-geni-qcom.c index 25810a7eef10..6939c6cabe91 100644 --- a/drivers/spi/spi-geni-qcom.c +++ b/drivers/spi/spi-geni-qcom.c @@ -354,6 +354,12 @@ static bool geni_spi_handle_tx(struct spi_geni_master *mas) unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas); unsigned int i = 0; + /* Stop the watermark IRQ if nothing to send */ + if (!mas->cur_xfer) { + writel(0, se->base + SE_GENI_TX_WATERMARK_REG); + return false; + } + max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word; if (mas->tx_rem_bytes < max_bytes) max_bytes = mas->tx_rem_bytes; @@ -396,6 +402,14 @@ static void geni_spi_handle_rx(struct spi_geni_master *mas) if (rx_last_byte_valid && rx_last_byte_valid < 4) rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid; } + + /* Clear out the FIFO and bail if nowhere to put it */ + if (!mas->cur_xfer) { + for (i = 0; i < DIV_ROUND_UP(rx_bytes, bytes_per_fifo_word); i++) + readl(se->base + SE_GENI_RX_FIFOn); + return; + } + if (mas->rx_rem_bytes < rx_bytes) rx_bytes = mas->rx_rem_bytes; From patchwork Thu Dec 17 22:29:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 344996 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1906C3526A for ; Thu, 17 Dec 2020 22:31:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A8C69238E8 for ; Thu, 17 Dec 2020 22:31:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732156AbgLQWa6 (ORCPT ); Thu, 17 Dec 2020 17:30:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731867AbgLQWa6 (ORCPT ); Thu, 17 Dec 2020 17:30:58 -0500 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 984F0C0611CC for ; Thu, 17 Dec 2020 14:29:40 -0800 (PST) Received: by mail-pg1-x52e.google.com with SMTP id e2so71515pgi.5 for ; Thu, 17 Dec 2020 14:29:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sWkiWzk6Rso22yNG+cnGpuPXvotVryC+O2+4me1Qo6c=; b=ElbqSAKABhF+sBG9f/NXWoC5A3GLpmmFLgEKHC/ljsaQ0mqLK+bKaIZb5pKKbzznO+ uNXIW5SRSl/YV2W0YOt8zDCqT8ZhE0Hy07HV/rWRf5vK68PN5W6jOIUVf4CGY3fSS4fs 9qx4vynHQFlxaz1Ga7ugGbnHY7SrTvEOMwuRk= 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:mime-version:content-transfer-encoding; bh=sWkiWzk6Rso22yNG+cnGpuPXvotVryC+O2+4me1Qo6c=; b=a01nL3qdagIN0aL8Dc27lRGmeEUIFtSMhJ00EJqVkgUpSOnntP/GJPUiZSnVuLVkm5 Fz0TGXE8bC3NfVnPFNyP8+r9Yh8uxxOHIInqs5plHUOvQOI0o1JABPX6u7j1Qf/rNWlV iPRlk2dDqUJsg7D2vc68Ccc6HH6VSXQIntF3PpXPCOlwWRAjMfnjMzq3mzupODoI9wiI H0bhVl0vWiQ+ctFM2usFoDbaNkGIAxA54vVLd6DqZAOylm2Ys6b42YbQxM1i6zlc7Zn2 RXO5nk5FwZcaLnpYxnig4NXLgnpXpeT/9rRv5uMjZ5f1WsqbWtiST6EzEFkL4w205YAg eV7A== X-Gm-Message-State: AOAM532FSkWH8FRcninF8AU4VjsOGCfJzOiNId6pwg+z2wjmAcRtdTht fj1RF0CU9wPL7qzWGWel4p+bsw== X-Google-Smtp-Source: ABdhPJy4bycT77LtZjm+hgxVOqL/vnjb7rOjBzQ6uEr8599KO9Wl4MBrwV7m7A8mi9p0ChEeI87r5g== X-Received: by 2002:a62:32c3:0:b029:1a1:c2f2:d771 with SMTP id y186-20020a6232c30000b02901a1c2f2d771mr1364793pfy.29.1608244180129; Thu, 17 Dec 2020 14:29:40 -0800 (PST) Received: from tictac2.mtv.corp.google.com ([2620:15c:202:1:42b0:34ff:fe3d:58e6]) by smtp.gmail.com with ESMTPSA id gm18sm5805850pjb.55.2020.12.17.14.29.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Dec 2020 14:29:39 -0800 (PST) From: Douglas Anderson To: Mark Brown Cc: msavaliy@qti.qualcomm.com, Stephen Boyd , akashast@codeaurora.org, Roja Rani Yarubandi , Douglas Anderson , Andy Gross , Bjorn Andersson , linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-spi@vger.kernel.org Subject: [PATCH v3 4/4] spi: spi-geni-qcom: Print an error when we timeout setting the CS Date: Thu, 17 Dec 2020 14:29:14 -0800 Message-Id: <20201217142842.v3.4.I666b37646de9652cef438ac7c2c6c2053367fc6b@changeid> X-Mailer: git-send-email 2.29.2.684.gfbc64c5ab5-goog In-Reply-To: <20201217142842.v3.1.I99ee04f0cb823415df59bd4f550d6ff5756e43d6@changeid> References: <20201217142842.v3.1.I99ee04f0cb823415df59bd4f550d6ff5756e43d6@changeid> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org If we're using geni to manage the chip select line (don't do it--use a GPIO!) and we happen to get a timeout waiting for the chip select command to be completed, no errors are printed even though things might not be in the best shape. Let's add a print. Signed-off-by: Douglas Anderson Reviewed-by: Stephen Boyd --- (no changes since v2) Changes in v2: - ("spi: spi-geni-qcom: Print an error when we timeout setting the CS") new for v2 drivers/spi/spi-geni-qcom.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/spi/spi-geni-qcom.c b/drivers/spi/spi-geni-qcom.c index b3ba092db489..5472d895a9e0 100644 --- a/drivers/spi/spi-geni-qcom.c +++ b/drivers/spi/spi-geni-qcom.c @@ -223,8 +223,10 @@ static void spi_geni_set_cs(struct spi_device *slv, bool set_flag) spin_unlock_irq(&mas->lock); time_left = wait_for_completion_timeout(&mas->cs_done, HZ); - if (!time_left) + if (!time_left) { + dev_warn(mas->dev, "Timeout setting chip select\n"); handle_fifo_timeout(spi, NULL); + } exit: pm_runtime_put(mas->dev);