Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mtd: fsl-quadspi: Fix module unbound

When removing the fsl-quadspi module and running 'cat /proc/mtd' afterwards,
we see garbage data like:

$ rmmod fsl-quadspi
$ cat /proc/mtd
dev: size erasesize name
mtd0: 00000000 00000000 "(null)"
mtd0: 00000000 00000000 "(null)"
mtd0: 00000000 00000000 "(null)"
...
mtd0: a22296c6c756e28 00000000 "(null)"
mtd0: a22296c6c756e28 3064746d "(null)"

If we continue doing multiple module load/unload operations, then it will also
lead to a kernel crash.

The reason for this is due to the wrong mtd index used in
mtd_device_unregister() in the remove function.

We need to keep the mtd unregister index aligned with the one used in the probe
function, which means we need to take into account the 'has_second_chip'
property. By doing so we can guarantee that the mtd index is the same in the
registration and unregistration functions.

With this patch applied we can load/unload the fsl-quadspi driver several times
and it will result in no crash.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>
Acked-by: Huang Shijie <shijie.huang@intel.com>
Tested-by: Frank Li <Frank.Li@freescale.com>
Acked-by: Allen Xu <han.xu@freescale.com>
Signed-off-by: Brian Norris <computersforpeace@gmail.com>

authored by

Fabio Estevam and committed by
Brian Norris
cfe4af3a fcc87a95

+13 -6
+13 -6
drivers/mtd/spi-nor/fsl-quadspi.c
··· 227 227 u32 nor_num; 228 228 u32 clk_rate; 229 229 unsigned int chip_base_addr; /* We may support two chips. */ 230 + bool has_second_chip; 230 231 }; 231 232 232 233 static inline int is_vybrid_qspi(struct fsl_qspi *q) ··· 784 783 struct spi_nor *nor; 785 784 struct mtd_info *mtd; 786 785 int ret, i = 0; 787 - bool has_second_chip = false; 788 786 const struct of_device_id *of_id = 789 787 of_match_device(fsl_qspi_dt_ids, &pdev->dev); 790 788 ··· 860 860 goto irq_failed; 861 861 862 862 if (of_get_property(np, "fsl,qspi-has-second-chip", NULL)) 863 - has_second_chip = true; 863 + q->has_second_chip = true; 864 864 865 865 /* iterate the subnodes. */ 866 866 for_each_available_child_of_node(dev->of_node, np) { 867 867 char modalias[40]; 868 868 869 869 /* skip the holes */ 870 - if (!has_second_chip) 870 + if (!q->has_second_chip) 871 871 i *= 2; 872 872 873 873 nor = &q->nor[i]; ··· 943 943 return 0; 944 944 945 945 last_init_failed: 946 - for (i = 0; i < q->nor_num; i++) 946 + for (i = 0; i < q->nor_num; i++) { 947 + /* skip the holes */ 948 + if (!q->has_second_chip) 949 + i *= 2; 947 950 mtd_device_unregister(&q->mtd[i]); 948 - 951 + } 949 952 irq_failed: 950 953 clk_disable_unprepare(q->clk); 951 954 clk_failed: ··· 963 960 struct fsl_qspi *q = platform_get_drvdata(pdev); 964 961 int i; 965 962 966 - for (i = 0; i < q->nor_num; i++) 963 + for (i = 0; i < q->nor_num; i++) { 964 + /* skip the holes */ 965 + if (!q->has_second_chip) 966 + i *= 2; 967 967 mtd_device_unregister(&q->mtd[i]); 968 + } 968 969 969 970 /* disable the hardware */ 970 971 writel(QUADSPI_MCR_MDIS_MASK, q->iobase + QUADSPI_MCR);