From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.246.85.4 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795512276; b=K81uiTbElgQb4EGnSBY8lnOz0GvxI7r90DYXsU73FCpFvNxHbwZ2QM+Z55uzVWw8EkcX rAXoS24v1ogewukvan/U0mE9Tlr3Qet3kvztSQ/YfTmuL6aRY+Itn1A0W8LvQg6ZxWheh DaGP/fFZqbpsS3iNerU/JAKnjpzzNhaUVR4TQZAnvkemNQI1wgLb1AfASA6LdgOLmliOj saalPqtIBYizIuA/9Hyqj+L/4fz4j066WWza23COeaztPw1qPYWb8K4OPjQQNn85Rpasf PVuZmt8npIP5xFZpUO3HF81XvuxLGB63WsDibSM9/1gTSsy6FWIiUaCiV+CqUuIkaYA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795512276; h=DMARC-Filter:Received:Received:Received:DKIM-Signature:From:Date: Subject:MIME-Version:Content-Type:Content-Transfer-Encoding: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Last-TLS-Session-Version; bh=aoI8AlSI3VF7hQCQWNsK0cPXv5qsgsj2161PVO/bKVs=; b=BBVbFv+3wXp4yuylSxIY2KmFzjR0/9z5fRGUM1tgVD5uFQLEzoQ+LVs2sipWjEZFPvKN hQY8VvJSO3LUk/kKcvr94y6tyPqLI51hlOUbsWfBdCOIBuoGhHGw1RjqUDX2P6sLA604S V8YnZixHmRhL69FxhlndePXY99+/DxAlkRRKzts3b0g4keLGIDpD+iVp4TsU6mDFdN9XO Tuf2VkvGUcn1fCwSODzOLWUI3lV/cBUox4wfDZ+R5OFwUhixU4/20nedPKnsRuOyeryBL HUMbIJzhIK8duSwDvZlEjTzaAVOlHcJjt0+Sl/pfkpvU+DUs062uYU9cjE2DAyy9hwA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=Ip0+fvPH; arc=none smtp.remote-ip=185.246.85.4 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4686606A680 for ; Tue, 24 Nov 2026 09:24:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A4686606A680 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.a=rsa-sha256 header.s=dkim header.b=Ip0+fvPH Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id 4B20C4E4175B; Tue, 24 Nov 2026 09:24:34 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 1ABBA601E1; Tue, 24 Nov 2026 09:24:34 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 2218111E6A9B3; Tue, 24 Nov 2026 10:24:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1795512273; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=aoI8AlSI3VF7hQCQWNsK0cPXv5qsgsj2161PVO/bKVs=; b=Ip0+fvPHtXLgnCo+SM/S6acz+lzzOI2el35iIWyX2xi4rAK9gb3vRfvcvlvXPPDKYDc4pt f+PAElG4w5jIS5GbdLpOd83QRbfjcDZeV3udvjURNFvdrNEzGgXU0Ctajj0KyfnEV2NUdH VBOl338USGubER0qQInlPiieVM4PtCJBv47K/kXWaMRhwQVCiKcyF1y6BzWXCRPV2Of3Rw VuGTcAkLAaQBlGfRlNpJR0LxVscdRbIKXDa7vtxt90DQr9o4TH59WISWZ33QnWUhXxI4SV qZ65R6dd9vXt2+oKXji30m6KeBZWPEi7i+YRYMJEOVXFAKs24WUkWEo4tpd60g== From: Miquel Raynal Date: Tue, 24 Nov 2026 10:24:23 +0100 Subject: [PATCH v2 2/3] mtd: rawnand: cadence: Resize the bounce buffer before the BBT scan X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261124-perso-upstreaming-rawnand-fixes-v2-2-69db17ad07bd@bootlin.com> References: <20261124-perso-upstreaming-rawnand-fixes-v2-0-69db17ad07bd@bootlin.com> In-Reply-To: <20261124-perso-upstreaming-rawnand-fixes-v2-0-69db17ad07bd@bootlin.com> To: Richard Weinberger , Vignesh Raghavendra , Niravkumar L Rabara Cc: Thomas Petazzoni , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Miquel Raynal , stable@vger.kernel.org X-Mailer: b4 0.14.3 X-Last-TLS-Session-Version: TLSv1.3 The bounce buffer is allocated with a default size and only resized to the actual page + OOB size once all the chips have been scanned. However, the bad block table is scanned during nand_scan(), before that resize happens, using the same bounce buffer. Chips with a page + OOB size larger than the default size may therefore overflow the heap buffer. In practice, this default is safe since it is set to 16kiB and I currently know no chip with a bigger page size, but chances are that this is not a future-proof bet, so let's resize the bounce buffer whenever we attach a chip whose geometry needs a bigger buffer. Fixes: ec4ba01e894d ("mtd: rawnand: Add new Cadence NAND driver to MTD subsystem") Cc: stable@vger.kernel.org Signed-off-by: Miquel Raynal --- drivers/mtd/nand/raw/cadence-nand-controller.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/drivers/mtd/nand/raw/cadence-nand-controller.c b/drivers/mtd/nand/raw/cadence-nand-controller.c index 42cc62b36457..912148f90ece 100644 --- a/drivers/mtd/nand/raw/cadence-nand-controller.c +++ b/drivers/mtd/nand/raw/cadence-nand-controller.c @@ -2970,8 +2970,17 @@ static int cadence_nand_attach_chip(struct nand_chip *chip) chip->ecc.read_oob_raw = cadence_nand_read_oob_raw; chip->ecc.write_oob_raw = cadence_nand_write_oob_raw; - if ((mtd->writesize + mtd->oobsize) > cdns_ctrl->buf_size) + if ((mtd->writesize + mtd->oobsize) > cdns_ctrl->buf_size) { + u8 *buf; + + buf = krealloc(cdns_ctrl->buf, mtd->writesize + mtd->oobsize, + GFP_KERNEL); + if (!buf) + return -ENOMEM; + + cdns_ctrl->buf = buf; cdns_ctrl->buf_size = mtd->writesize + mtd->oobsize; + } /* Is 32-bit DMA supported? */ ret = dma_set_mask(cdns_ctrl->dev, DMA_BIT_MASK(32)); @@ -3136,8 +3145,11 @@ static int cadence_nand_init(struct cdns_nand_ctrl *cdns_ctrl) if (!cdns_ctrl->cdma_desc) return -ENOMEM; - cdns_ctrl->buf_size = SZ_16K; - cdns_ctrl->buf = kmalloc(cdns_ctrl->buf_size, GFP_KERNEL); + /* + * Allocate a scratch buffer for the identification phase. It gets + * resized to the actual chip geometry in ->attach_chip(). + */ + cdns_ctrl->buf = kmalloc(SZ_16K, GFP_KERNEL); if (!cdns_ctrl->buf) { ret = -ENOMEM; goto free_buf_desc; @@ -3194,13 +3206,6 @@ static int cadence_nand_init(struct cdns_nand_ctrl *cdns_ctrl) goto unmap_dma_resource; } - kfree(cdns_ctrl->buf); - cdns_ctrl->buf = kzalloc(cdns_ctrl->buf_size, GFP_KERNEL); - if (!cdns_ctrl->buf) { - ret = -ENOMEM; - goto unmap_dma_resource; - } - return 0; unmap_dma_resource: -- 2.56.0