Received: by 10.192.165.148 with SMTP id m20csp4153661imm; Mon, 30 Apr 2018 12:47:14 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqu8yojrxnjbr06aJPBGXjxxoFxEtS27jkrsORHQOyeCTdS4GrdJl1t5PUyfL6hGiYW3Tzr X-Received: by 2002:a63:744a:: with SMTP id e10-v6mr6654652pgn.275.1525117634750; Mon, 30 Apr 2018 12:47:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525117634; cv=none; d=google.com; s=arc-20160816; b=Y9bWnKMX4+qrJisa0KeEgrkf6+5CxhF33olGfWzE6UKEsNlq0wh1jdW6HqJx7b8Yvi fDUOXo60C4umJRsoOT7Ff7Ujoryb/sRvnwg1wyovVr2rd9+yWSaZICqPuHcEH7CigNsJ U/vDwQQIUZGn35I3nUcgHgjSenco94wpJvv9rDOoyL13PCSDZOgj1wy1GOZtmJaGEU4f 210/ezi9Z88tseJxv8nySkOKuujbge3h2TLIVB0UVOhHQoQbp1g0ZqwxoTXC7G2daB7W 9uJuYtNqIcfQUpTd8alkvrEoIJHbc4KUQFUnzYN1ddFDIWrR2cRIB5upAL1qNlhhtavF sdMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dmarc-filter :arc-authentication-results; bh=ianx270AN/l8UszCWfMBt/b/cDsMdQd2EPpkhiYGyZQ=; b=hE0djPAZm0QjOz/oRiOCWv5U3kcTIGcOebXkfHq1P/rNXBkDghr+ZdoIEavxU4GTsN CqlnwFgSad6g1fi17oaPkMw+huAfupDZN1goHP3JQkPIMVsfTQTNqSrVOLYNoQi/dj3x PVx54Il6hsT5YrjgAVxfmCCLGdUoV7lpOHT64DQ11SqttvBAQ5o0ZYHFVuCsBFfOcI+i 0hx0XpCSKQ2uL9n4LEOziG0aByIDkzb+8dg4P4T/OnIAkRycKHqJIrovn6TAfL19HZWm MsozBj7gGRd/7a2geK5//stxVyaa9pzfN5+2mJ2oqR12a1xuQlZxw3+dEPMLi7Q57qdw KnWg== ARC-Authentication-Results: i=1; mx.google.com; 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 a23-v6si6601731pgv.429.2018.04.30.12.47.00; Mon, 30 Apr 2018 12:47:14 -0700 (PDT) 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; 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 S1756397AbeD3TqX (ORCPT + 99 others); Mon, 30 Apr 2018 15:46:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:35956 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932242AbeD3T2V (ORCPT ); Mon, 30 Apr 2018 15:28:21 -0400 Received: from localhost (unknown [104.132.1.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D1CDD22DC1; Mon, 30 Apr 2018 19:28:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D1CDD22DC1 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linuxfoundation.org Authentication-Results: mail.kernel.org; spf=fail smtp.mailfrom=gregkh@linuxfoundation.org From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thor Thayer , Marek Vasut , Boris Brezillon Subject: [PATCH 4.16 054/113] mtd: spi-nor: cadence-quadspi: Fix page fault kernel panic Date: Mon, 30 Apr 2018 12:24:25 -0700 Message-Id: <20180430184017.343577956@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180430184015.043892819@linuxfoundation.org> References: <20180430184015.043892819@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Thor Thayer commit 47016b341fc3b3fd4909e058c6fa38f165b53646 upstream. The current Cadence QSPI driver caused a kernel panic when loading a Root Filesystem from QSPI. The problem was caused by reading more bytes than needed because the QSPI operated on 4 bytes at a time. [ 7.947754] spi_nor_read[1048]:from 0x037cad74, len 1 [bfe07fff] [ 7.956247] cqspi_read[910]:offset 0x58502516, buffer=bfe07fff [ 7.956247] [ 7.966046] Unable to handle kernel paging request at virtual address bfe08002 [ 7.973239] pgd = eebfc000 [ 7.975931] [bfe08002] *pgd=2fffb811, *pte=00000000, *ppte=00000000 Notice above how only 1 byte needed to be read but by reading 4 bytes into the end of a mapped page, an unrecoverable page fault occurred. This patch uses a temporary buffer to hold the 4 bytes read and then copies only the bytes required into the buffer. A min() function is used to limit the length to prevent buffer overflows. Request testing of this patch on other platforms. This was tested on the Intel Arria10 SoCFPGA DevKit. Fixes: 0cf1725676a97fc8 ("mtd: spi-nor: cqspi: Fix build on arches missing readsl/writesl") Signed-off-by: Thor Thayer Cc: Reviewed-by: Marek Vasut Signed-off-by: Boris Brezillon Signed-off-by: Greg Kroah-Hartman --- drivers/mtd/spi-nor/cadence-quadspi.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) --- a/drivers/mtd/spi-nor/cadence-quadspi.c +++ b/drivers/mtd/spi-nor/cadence-quadspi.c @@ -501,7 +501,9 @@ static int cqspi_indirect_read_execute(s void __iomem *reg_base = cqspi->iobase; void __iomem *ahb_base = cqspi->ahb_base; unsigned int remaining = n_rx; + unsigned int mod_bytes = n_rx % 4; unsigned int bytes_to_read = 0; + u8 *rxbuf_end = rxbuf + n_rx; int ret = 0; writel(from_addr, reg_base + CQSPI_REG_INDIRECTRDSTARTADDR); @@ -530,11 +532,24 @@ static int cqspi_indirect_read_execute(s } while (bytes_to_read != 0) { + unsigned int word_remain = round_down(remaining, 4); + bytes_to_read *= cqspi->fifo_width; bytes_to_read = bytes_to_read > remaining ? remaining : bytes_to_read; - ioread32_rep(ahb_base, rxbuf, - DIV_ROUND_UP(bytes_to_read, 4)); + bytes_to_read = round_down(bytes_to_read, 4); + /* Read 4 byte word chunks then single bytes */ + if (bytes_to_read) { + ioread32_rep(ahb_base, rxbuf, + (bytes_to_read / 4)); + } else if (!word_remain && mod_bytes) { + unsigned int temp = ioread32(ahb_base); + + bytes_to_read = mod_bytes; + memcpy(rxbuf, &temp, min((unsigned int) + (rxbuf_end - rxbuf), + bytes_to_read)); + } rxbuf += bytes_to_read; remaining -= bytes_to_read; bytes_to_read = cqspi_get_rd_sram_level(cqspi);