Received: by 10.192.165.148 with SMTP id m20csp4171981imm; Mon, 30 Apr 2018 13:08:45 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqCMEZAd2WhDRhEpLGmf/Rwr9nHfHuW0hDW0GtGErjm5Yalht0TsFpRyi+A9Amb8yXMhfGE X-Received: by 2002:a65:600a:: with SMTP id m10-v6mr10938717pgu.281.1525118925562; Mon, 30 Apr 2018 13:08:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525118925; cv=none; d=google.com; s=arc-20160816; b=EKxeOtJ1lQIs3j9wAkvZHu4dWVYi+4jCiePpyOwEURHfKqrOM30oPyQnsMf4QENxYM OCSc1Ja5Md6tIGMgSCaNR8bkuMqmTbbKowtT57CUnFb3nvSPprttVwJPG7nN59D68cdv mrBbYp9LCEn/zzzDhc08MSEZM3DNc0XOmhztvVH1BFRwPKuX+Fpk9VnN4VwApsAMftDo nNnsSHy3ruY8CjJE7JMJ5Qu8Ba5KyAgghEkX+/5UMb6UNvQPkCmnsfrxbzm3zlb6Cb9P PBseLwb6U/ojkiYB5PHe1CilvPhHnC3y09Ze56hT1pTUuaQNWo9mzaqOoGpl9Y4CwToU cxaQ== 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=YaVp98QCZezH7UIQVSegWVrd1bs/PO4h0aeWlmTdpX0=; b=tvpyKQ1+n4n74HmdTUsDuE84U/fpkxSrdgWJOjLRi35A73KUjaQmegk2/C7eSTBfrC f0WWHvEhCZ/Jilqf52MFUQrKERQX/aKIGTmpOaW62h8HVqoalDokdQKBemFdVRQQnK1R DC+87B+uwGeRd2JZKbnBzvtedH3+TFEraPpy3dKJizC9SmRefo0unqqsblRvzGIDMpq7 LghTZw+jPITtcdfHXnq6qGZmzHl7u9BaqP0QIjQ18uvfqX2GQtvi4AVJhVKmLpdkzPLf rDWcxUCN852LOqBIS8opKDNwI+VyzaS0OviZD6UJPM2ybSbaKr776iq7yb/qDaFudNSh 1kPA== 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 g63si8113179pfd.93.2018.04.30.13.08.31; Mon, 30 Apr 2018 13:08:45 -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 S1755985AbeD3UIU (ORCPT + 99 others); Mon, 30 Apr 2018 16:08:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:33728 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755945AbeD3T1V (ORCPT ); Mon, 30 Apr 2018 15:27: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 DC53922DC1; Mon, 30 Apr 2018 19:27:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DC53922DC1 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.14 50/91] mtd: spi-nor: cadence-quadspi: Fix page fault kernel panic Date: Mon, 30 Apr 2018 12:24:32 -0700 Message-Id: <20180430184006.796020072@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180430184004.216234025@linuxfoundation.org> References: <20180430184004.216234025@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.14-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 @@ -495,7 +495,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(remaining, reg_base + CQSPI_REG_INDIRECTRDBYTES); @@ -523,11 +525,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);