Skip to content

Commit 63244a0

Browse files
Md Sadre Alamgregkh
Md Sadre Alam
authored andcommitted
mtd: rawnand: qcom: fix memory corruption that causes panic
commit ba7542e upstream. 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 [raspberrypi#1] PREEMPT SMP ARM | Modules linked in: | CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.17.0-01473-g13ae1769cfb0 raspberrypi#38 | Hardware name: Generic DT based system | PC is at nandc_set_reg+0x8/0x1c | LR is at qcom_nandc_command+0x20c/0x5d0 | pc : [<c088b74c>] lr : [<c088d9c8>] 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 or 8 based on page size. 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 or 8 based on page size in qcom_nand_attach_chip call back after freeing the previously allocated memory for bam txn as per nandc->max_cwperpage = 1 and then again allocating bam txn as per nandc->max_cwperpage = 4 or 8 based on page size in qcom_nand_attach_chip call back itself. Cc: [email protected] Fixes: 6a3cec6 ("mtd: rawnand: qcom: convert driver to nand_scan()") Reported-by: Konrad Dybcio <[email protected]> Reviewed-by: Manivannan Sadhasivam <[email protected]> Co-developed-by: Sricharan R <[email protected]> Signed-off-by: Sricharan R <[email protected]> Signed-off-by: Md Sadre Alam <[email protected]> Signed-off-by: Miquel Raynal <[email protected]> Link: https://lore.kernel.org/linux-mtd/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 802282d commit 63244a0

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

drivers/mtd/nand/raw/qcom_nandc.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,10 +2651,23 @@ static int qcom_nand_attach_chip(struct nand_chip *chip)
26512651
ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
26522652

26532653
mtd_set_ooblayout(mtd, &qcom_nand_ooblayout_ops);
2654+
/* Free the initially allocated BAM transaction for reading the ONFI params */
2655+
if (nandc->props->is_bam)
2656+
free_bam_transaction(nandc);
26542657

26552658
nandc->max_cwperpage = max_t(unsigned int, nandc->max_cwperpage,
26562659
cwperpage);
26572660

2661+
/* Now allocate the BAM transaction based on updated max_cwperpage */
2662+
if (nandc->props->is_bam) {
2663+
nandc->bam_txn = alloc_bam_transaction(nandc);
2664+
if (!nandc->bam_txn) {
2665+
dev_err(nandc->dev,
2666+
"failed to allocate bam transaction\n");
2667+
return -ENOMEM;
2668+
}
2669+
}
2670+
26582671
/*
26592672
* DATA_UD_BYTES varies based on whether the read/write command protects
26602673
* 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,
29552968
if (ret)
29562969
return ret;
29572970

2958-
if (nandc->props->is_bam) {
2959-
free_bam_transaction(nandc);
2960-
nandc->bam_txn = alloc_bam_transaction(nandc);
2961-
if (!nandc->bam_txn) {
2962-
dev_err(nandc->dev,
2963-
"failed to allocate bam transaction\n");
2964-
nand_cleanup(chip);
2965-
return -ENOMEM;
2966-
}
2967-
}
2968-
29692971
ret = mtd_device_parse_register(mtd, probes, NULL, NULL, 0);
29702972
if (ret)
29712973
nand_cleanup(chip);

0 commit comments

Comments
 (0)