Received: by 10.213.65.68 with SMTP id h4csp1336812imn; Wed, 21 Mar 2018 08:16:29 -0700 (PDT) X-Google-Smtp-Source: AG47ELvpmfBQ4vxMsw2AKvw3Idw1VgJg3vqweGrBT0TXW+LPpCFw3BHpMrb8Myd9ej3WETKXZEw0 X-Received: by 2002:a17:902:8a94:: with SMTP id p20-v6mr21319543plo.74.1521645389098; Wed, 21 Mar 2018 08:16:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521645389; cv=none; d=google.com; s=arc-20160816; b=auHrkAU0soV5mUBwLU0CzxkdpNKczwsLN5MwVKF3KAkZ7KcCespeJDlZqRejFgyc+n XHp6KVG7Ah2a/jjTO7bgTw2lH2dKnZS27szzqOLxw6jyfJiTKVxzIkM2USqpyerS7kV0 07YL3pO/Xirym4RJ9peDXHmpwXomWnuXmfkG2eTUU7Kuxi8Ir21z8B206U2Z/u8yX7Gl swwk8e+KevgQqlKgacX2R6Lsy6VwCFseuIFOs0QxH9n0w56PZuJtim/mxL+a42fQRWo5 cCnKv6ZxMuTB9RuveGsCm2NZl7fNcK/aowePAxwF1C/nn86PdWJe8FKdUsIN/qfe8Cpj 0TCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:reply-to :arc-authentication-results; bh=5JB6rQZheFo8+/PQHDq8emKTOInk6iKiKl5FTmiiwJY=; b=CYy/0nwhjxtyEYEH1IVrAazOX+FCziL4Ctba9+L9ocupBGZ/e4/T8SyEglz3dRmd1A b5hdKaLQcaKaGUA4tbiXJSPxGOJZzmn6tMhQ+j7D53jNSltFNyTYc8Ww7f2NrC8lsKJD FcwFREd5Cm50j2FdPjCvNUT1iMXHwMBNKM3CVJaTDTTl/qPgwIw873NYSq5uSVL4lWeR OEWD9P23pmHrmf33JJxh77/R+6ASzhV7yneUwHs3kMFyHmGT1Im25BQsP+iFwspCYqGP Rq4kPsk0SqxbOKdLdG7BHTS5+SyR8rHoRDM+l5upwcTV5jD6k0p4XsVQxX+mUFkNps8W yxyQ== 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 c3-v6si4079046pld.545.2018.03.21.08.16.14; Wed, 21 Mar 2018 08:16:29 -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 S1752622AbeCUPPK (ORCPT + 99 others); Wed, 21 Mar 2018 11:15:10 -0400 Received: from mga11.intel.com ([192.55.52.93]:36754 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752594AbeCUPPJ (ORCPT ); Wed, 21 Mar 2018 11:15:09 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Mar 2018 08:12:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,340,1517904000"; d="scan'208";a="40944494" Received: from tthayer-hp-z620-workstation.an.intel.com (HELO [10.122.105.144]) ([10.122.105.144]) by orsmga001.jf.intel.com with ESMTP; 21 Mar 2018 08:12:24 -0700 Reply-To: thor.thayer@linux.intel.com Subject: Re: [PATCH] mtd: spi-nor: Fix Cadence QSPI page fault kernel panic To: Marek Vasut , cyrille.pitchen@wedev4u.fr, dwmw2@infradead.org, computersforpeace@gmail.com, boris.brezillon@bootlin.com, richard@nod.at Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org References: <1521485124-24882-1-git-send-email-thor.thayer@linux.intel.com> From: Thor Thayer Message-ID: Date: Wed, 21 Mar 2018 10:15:14 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Marek, On 03/19/2018 05:33 PM, Marek Vasut wrote: > On 03/19/2018 07:45 PM, thor.thayer@linux.intel.com wrote: >> From: Thor Thayer >> >> 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, a 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. >> >> Similar changes were made for the write routine. >> >> Signed-off-by: Thor Thayer >> --- >> drivers/mtd/spi-nor/cadence-quadspi.c | 39 ++++++++++++++++++++++++++++------- >> 1 file changed, 31 insertions(+), 8 deletions(-) >> >> diff --git a/drivers/mtd/spi-nor/cadence-quadspi.c b/drivers/mtd/spi-nor/cadence-quadspi.c >> index 4b8e9183489a..b22ed982f896 100644 >> --- a/drivers/mtd/spi-nor/cadence-quadspi.c >> +++ b/drivers/mtd/spi-nor/cadence-quadspi.c >> @@ -501,7 +501,8 @@ static int cqspi_indirect_read_execute(struct spi_nor *nor, u8 *rxbuf, >> void __iomem *reg_base = cqspi->iobase; >> void __iomem *ahb_base = cqspi->ahb_base; >> unsigned int remaining = n_rx; >> - unsigned int bytes_to_read = 0; >> + unsigned int mod_bytes, words_to_read, bytes_to_read = 0; >> + u8 *rxbuf_end = rxbuf + n_rx; >> int ret = 0; >> >> writel(from_addr, reg_base + CQSPI_REG_INDIRECTRDSTARTADDR); >> @@ -533,9 +534,21 @@ static int cqspi_indirect_read_execute(struct spi_nor *nor, u8 *rxbuf, >> 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)); >> - rxbuf += bytes_to_read; >> + /* Read 4 byte chunks before using single byte mode */ >> + words_to_read = bytes_to_read / 4; >> + mod_bytes = bytes_to_read % 4; >> + if (words_to_read) { >> + ioread32_rep(ahb_base, rxbuf, words_to_read); >> + rxbuf += (words_to_read * 4); >> + } >> + if (mod_bytes) { >> + unsigned int temp = ioread32(ahb_base); >> + >> + memcpy(rxbuf, &temp, min((unsigned int) >> + (rxbuf_end - rxbuf), >> + mod_bytes)); >> + rxbuf += mod_bytes; >> + } > > Wouldn't it make more sense to read in 4 byte increments all the time > except for the one last read instead ? This code above where you always > check for trailing bytes can make the next read cycle do ioreaad32 into > unaligned memory address and thus cause slowdown. (consider the example > where the controller first reports it has 5 bytes ready, then reports it > has 7 bytes ready. If you read 4 bytes in the first cycle, wait a bit > and then check how much data the controller has in the next cycle, it > will be 8 bytes, all nicely aligned). > > Does it make sense ? > Ahh yes, Thanks for the example - I see your point. I'll look into that change. Thanks.