Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2456108pxb; Sun, 17 Apr 2022 23:54:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGTdszGD+HOlOuEYQUsndqdWDD8C4bmExtY/cKtEBWU4n7kO+ygqj5kMRI1dPlfPhlvyaS X-Received: by 2002:a17:902:8b83:b0:158:e061:4df2 with SMTP id ay3-20020a1709028b8300b00158e0614df2mr9672308plb.90.1650264895921; Sun, 17 Apr 2022 23:54:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650264895; cv=none; d=google.com; s=arc-20160816; b=nY/sZOSW0dmqC7RaGQDQdW+bqnNmtPMvT8lCAxHsmfX16yBoB2V31sY2hMUTXNIplU RtxLGXCsEeMqfGcFrgubqzM3iMoyG2U3GqxTgfiwwa1jyJlYUQDtNO12rAVc5a4vTYcv ag+H/1fjy5EZBAQ9OBxcGd9vIl7X7yvtTZudmKQW26oJrqSxrPEyFBwD5CKmc9lwiDk5 1eZ0NCkJpHGhN5ZLQ8g2OVZohJoU31d5DdpExoGFz2TW4K6AA7IVyEYtzBlLJrPmi9JQ YCbp42UteDwdvQN7yT7xztp4wH6qTrCbpexZF+EAGokWaL66maU4X7173jzEFAfCQoZl mPTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=QI1EkIt4Q1gSxpvMu/0SJKKoqS9PFB31m/LrUTdujZc=; b=XJjtqlQLreVe1PLaU/1FB1Vl1ltH5Igr51Sf5cB+B3LlqqG31Wo3to0gtObGfxQ3pr 4i/BCIrMkq5UaomgsoMJ+2d/Vft8i/p6pnY7u0R9LSByCwL1mxVsvEgycdRgE+jTOXEX nXS889ohX68DPcV0ZLVftMp17+OAwT+Jm0vnB0/C8gbNIQzCmR69TSozGyIVOXBbsTNi kiZ8AIk89MGMWnK6GeC1iTNzU+OXV02fnD74P8ctbhMlho7hlRCIX0yYfRA6Q3ZV3AS+ B4CgTtNA+eJfJVZTZtn+tJ/v+7C88USNlEEBo5UEu/RUCgAImdLivboG+5Wc/JE3UKTX i+3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=vQ4MYWST; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pf7-20020a17090b1d8700b001cb3e126550si1466990pjb.106.2022.04.17.23.54.39; Sun, 17 Apr 2022 23:54:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=vQ4MYWST; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236647AbiDRFdP (ORCPT + 99 others); Mon, 18 Apr 2022 01:33:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231880AbiDRFdL (ORCPT ); Mon, 18 Apr 2022 01:33:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1061410C9; Sun, 17 Apr 2022 22:30:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 74F3F60FA4; Mon, 18 Apr 2022 05:30:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C9D06C385A1; Mon, 18 Apr 2022 05:30:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1650259831; bh=WWUJM0Dq3fdCcxIjJvR0UhznAAHwcINexYWtspu+OSE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=vQ4MYWSTb/xP0BUP7nWdGk8YeYZqKdAjokw01TOAPwM8IbkHR6HbSNwQHgQe9Qn7F aHs70sVgZO+NUlZUhq0e25QYd6DCwneXCxGwnlZOo9tLbkfLAOwwVw9jtLCFC6TRs9 D9POlsREQ62UUj7pXSLsPs/ckSvkzq9wJSvPJ9gY80B3FA+4DqCmYViI7uKqz4SDGJ vwxv0APY7TRs/GZLmsPRrzLEO4Ipv5M+bo3BR6hZBSeYFttHqjkVarywKiRd8V0UWS Y37qSU+f7RcZi/s1yLTAE4JUASF5BaAvEfe0K7zg4p65SMq/yfLFml536WHrhbK5da lVGai4KlstFMA== Date: Mon, 18 Apr 2022 11:00:24 +0530 From: Manivannan Sadhasivam To: Md Sadre Alam Cc: mani@kernel.org, miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com, linux-mtd@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, konrad.dybcio@somainline.org, quic_srichara@quicinc.com, stable@vger.kernel.org Subject: Re: [PATCH V3] mtd: rawnand: qcom: fix memory corruption that causes panic Message-ID: <20220418053024.GA7431@thinkpad> References: <1650259141-20923-1-git-send-email-quic_mdalam@quicinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1650259141-20923-1-git-send-email-quic_mdalam@quicinc.com> X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 18, 2022 at 10:49:01AM +0530, Md Sadre Alam wrote: > This patch fixes a memory corruption that occurred in the > nand_scan() path for Hynix nand device. > > On boot, for Hynix nand device will panic at a weird place: > | Unable to handle kernel NULL pointer dereference at virtual > address 00000070 > | [00000070] *pgd=00000000 > | Internal error: Oops: 5 [#1] PREEMPT SMP ARM > | Modules linked in: > | CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.17.0-01473-g13ae1769cfb0 > #38 > | Hardware name: Generic DT based system > | PC is at nandc_set_reg+0x8/0x1c > | LR is at qcom_nandc_command+0x20c/0x5d0 > | pc : [] lr : [] psr: 00000113 > | sp : c14adc50 ip : c14ee208 fp : c0cc970c > | r10: 000000a3 r9 : 00000000 r8 : 00000040 > | r7 : c16f6a00 r6 : 00000090 r5 : 00000004 r4 :c14ee040 > | r3 : 00000000 r2 : 0000000b r1 : 00000000 r0 :c14ee040 > | Flags: nzcv IRQs on FIQs on Mode SVC_32 ISA ARM Segment none > | Control: 10c5387d Table: 8020406a DAC: 00000051 > | Register r0 information: slab kmalloc-2k start c14ee000 pointer offset > 64 size 2048 > | Process swapper/0 (pid: 1, stack limit = 0x(ptrval)) > | nandc_set_reg from qcom_nandc_command+0x20c/0x5d0 > | qcom_nandc_command from nand_readid_op+0x198/0x1e8 > | nand_readid_op from hynix_nand_has_valid_jedecid+0x30/0x78 > | hynix_nand_has_valid_jedecid from hynix_nand_init+0xb8/0x454 > | hynix_nand_init from nand_scan_with_ids+0xa30/0x14a8 > | nand_scan_with_ids from qcom_nandc_probe+0x648/0x7b0 > | qcom_nandc_probe from platform_probe+0x58/0xac > > The problem is that the nand_scan()'s qcom_nand_attach_chip callback > is updating the nandc->max_cwperpage from 1 to 4.This causes the > sg_init_table of clear_bam_transaction() in the driver's > qcom_nandc_command() to memset much more than what was initially > allocated by alloc_bam_transaction(). > > This patch will update nandc->max_cwperpage 1 to 4 after nand_scan() > returns, and remove updating nandc->max_cwperpage from > qcom_nand_attach_chip call back. The above statement is still wrong. > > Cc: stable@vger.kernel.org > Fixes: 6a3cec64f18c ("mtd: rawnand: qcom: convert driver to nand_scan()") > Reported-by: Konrad Dybcio > Signed-off-by: Md Sadre Alam > Signed-off-by: Sricharan R > --- > [V3] > * Updated commit message Fixes, Cc, Reported-by Missing the previous changelogs. Thanks, Mani > > drivers/mtd/nand/raw/qcom_nandc.c | 24 +++++++++++++----------- > 1 file changed, 13 insertions(+), 11 deletions(-) > > diff --git a/drivers/mtd/nand/raw/qcom_nandc.c b/drivers/mtd/nand/raw/qcom_nandc.c > index 1a77542..048b255 100644 > --- a/drivers/mtd/nand/raw/qcom_nandc.c > +++ b/drivers/mtd/nand/raw/qcom_nandc.c > @@ -2651,10 +2651,23 @@ static int qcom_nand_attach_chip(struct nand_chip *chip) > ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; > > mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops); > + /* Free the initially allocated BAM transaction for reading the ONFI params */ > + if (nandc->props->is_bam) > + free_bam_transaction(nandc); > > nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage, > cwperpage); > > + /* Now allocate the BAM transaction based on updated max_cwperpage */ > + if (nandc->props->is_bam) { > + nandc->bam_txn = alloc_bam_transaction(nandc); > + if (!nandc->bam_txn) { > + dev_err(nandc->dev, > + "failed to allocate bam transaction\n"); > + return -ENOMEM; > + } > + } > + > /* > * DATA_UD_BYTES varies based on whether the read/write command protects > * spare data with ECC too. We protect spare data by default, so we set > @@ -2955,17 +2968,6 @@ static int qcom_nand_host_init_and_register(struct qcom_nand_controller *nandc, > if (ret) > return ret; > > - if (nandc->props->is_bam) { > - free_bam_transaction(nandc); > - nandc->bam_txn = alloc_bam_transaction(nandc); > - if (!nandc->bam_txn) { > - dev_err(nandc->dev, > - "failed to allocate bam transaction\n"); > - nand_cleanup(chip); > - return -ENOMEM; > - } > - } > - > ret = mtd_device_parse_register(mtd, probes, NULL, NULL, 0); > if (ret) > nand_cleanup(chip); > -- > 2.7.4 >